Subversion Repositories HelenOS

Rev

Rev 3665 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (c) 2006 Ondrej Palkovsky
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  * - Redistributions of source code must retain the above copyright
  10.  *   notice, this list of conditions and the following disclaimer.
  11.  * - Redistributions in binary form must reproduce the above copyright
  12.  *   notice, this list of conditions and the following disclaimer in the
  13.  *   documentation and/or other materials provided with the distribution.
  14.  * - The name of the author may not be used to endorse or promote products
  15.  *   derived from this software without specific prior written permission.
  16.  *
  17.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27.  */
  28.  
  29. /** @addtogroup genericipc
  30.  * @{
  31.  */
  32. /** @file
  33.  */
  34.  
  35. #include <arch.h>
  36. #include <proc/task.h>
  37. #include <proc/thread.h>
  38. #include <errno.h>
  39. #include <memstr.h>
  40. #include <debug.h>
  41. #include <ipc/ipc.h>
  42. #include <ipc/sysipc.h>
  43. #include <ipc/irq.h>
  44. #include <ipc/ipcrsc.h>
  45. #include <ipc/kbox.h>
  46. #include <udebug/udebug_ipc.h>
  47. #include <arch/asm.h>
  48. #include <arch/interrupt.h>
  49. #include <syscall/copy.h>
  50. #include <security/cap.h>
  51. #include <mm/as.h>
  52. #include <print.h>
  53.  
  54. /**
  55.  * Maximum buffer size allowed for IPC_M_DATA_WRITE and IPC_M_DATA_READ
  56.  * requests.
  57.  */
  58. #define DATA_XFER_LIMIT     (64 * 1024)
  59.  
  60. #define GET_CHECK_PHONE(phone, phoneid, err) \
  61. { \
  62.     if (phoneid > IPC_MAX_PHONES) { \
  63.         err; \
  64.     } \
  65.     phone = &TASK->phones[phoneid]; \
  66. }
  67.  
  68. #define STRUCT_TO_USPACE(dst, src)  copy_to_uspace(dst, src, sizeof(*(src)))
  69.  
  70. /** Decide if the method is a system method.
  71.  *
  72.  * @param method    Method to be decided.
  73.  *
  74.  * @return      Return 1 if the method is a system method.
  75.  *          Otherwise return 0.
  76.  */
  77. static inline int method_is_system(unative_t method)
  78. {
  79.     if (method <= IPC_M_LAST_SYSTEM)
  80.         return 1;
  81.     return 0;
  82. }
  83.  
  84. /** Decide if the message with this method is forwardable.
  85.  *
  86.  * - some system messages may be forwarded, for some of them
  87.  *   it is useless
  88.  *
  89.  * @param method    Method to be decided.
  90.  *
  91.  * @return      Return 1 if the method is forwardable.
  92.  *          Otherwise return 0.
  93.  */
  94. static inline int method_is_forwardable(unative_t method)
  95. {
  96.     switch (method) {
  97.     case IPC_M_PHONE_HUNGUP:
  98.         /* This message is meant only for the original recipient. */
  99.         return 0;
  100.     default:
  101.         return 1;
  102.     }
  103. }
  104.  
  105. /** Decide if the message with this method is immutable on forward.
  106.  *
  107.  * - some system messages may be forwarded but their content cannot be altered
  108.  *
  109.  * @param method    Method to be decided.
  110.  *
  111.  * @return      Return 1 if the method is immutable on forward.
  112.  *          Otherwise return 0.
  113.  */
  114. static inline int method_is_immutable(unative_t method)
  115. {
  116.     switch (method) {
  117.     case IPC_M_SHARE_OUT:
  118.     case IPC_M_SHARE_IN:
  119.     case IPC_M_DATA_WRITE:
  120.     case IPC_M_DATA_READ:
  121.         return 1;
  122.         break;
  123.     default:
  124.         return 0;
  125.     }
  126. }
  127.  
  128.  
  129. /***********************************************************************
  130.  * Functions that preprocess answer before sending it to the recepient.
  131.  ***********************************************************************/
  132.  
  133. /** Decide if the caller (e.g. ipc_answer()) should save the old call contents
  134.  * for answer_preprocess().
  135.  *
  136.  * @param call      Call structure to be decided.
  137.  *
  138.  * @return      Return 1 if the old call contents should be saved.
  139.  *          Return 0 otherwise.
  140.  */
  141. static inline int answer_need_old(call_t *call)
  142. {
  143.     switch (IPC_GET_METHOD(call->data)) {
  144.     case IPC_M_CONNECT_TO_ME:
  145.     case IPC_M_CONNECT_ME_TO:
  146.     case IPC_M_SHARE_OUT:
  147.     case IPC_M_SHARE_IN:
  148.     case IPC_M_DATA_WRITE:
  149.     case IPC_M_DATA_READ:
  150.         return 1;
  151.     default:
  152.         return 0;
  153.     }
  154. }
  155.  
  156. /** Interpret process answer as control information.
  157.  *
  158.  * This function is called directly after sys_ipc_answer().
  159.  *
  160.  * @param answer    Call structure with the answer.
  161.  * @param olddata   Saved data of the request.
  162.  *
  163.  * @return      Return 0 on success or an error code.
  164.  */
  165. static inline int answer_preprocess(call_t *answer, ipc_data_t *olddata)
  166. {
  167.     int phoneid;
  168.  
  169.     if ((native_t) IPC_GET_RETVAL(answer->data) == EHANGUP) {
  170.         /* In case of forward, hangup the forwared phone,
  171.          * not the originator
  172.          */
  173.         mutex_lock(&answer->data.phone->lock);
  174.         spinlock_lock(&TASK->answerbox.lock);
  175.         if (answer->data.phone->state == IPC_PHONE_CONNECTED) {
  176.             list_remove(&answer->data.phone->link);
  177.             answer->data.phone->state = IPC_PHONE_SLAMMED;
  178.         }
  179.         spinlock_unlock(&TASK->answerbox.lock);
  180.         mutex_unlock(&answer->data.phone->lock);
  181.     }
  182.  
  183.     if (!olddata)
  184.         return 0;
  185.  
  186.     if (IPC_GET_METHOD(*olddata) == IPC_M_CONNECT_TO_ME) {
  187.         phoneid = IPC_GET_ARG5(*olddata);
  188.         if (IPC_GET_RETVAL(answer->data)) {
  189.             /* The connection was not accepted */
  190.             phone_dealloc(phoneid);
  191.         } else {
  192.             /* The connection was accepted */
  193.             phone_connect(phoneid, &answer->sender->answerbox);
  194.             /* Set 'phone hash' as arg5 of response */
  195.             IPC_SET_ARG5(answer->data,
  196.                 (unative_t) &TASK->phones[phoneid]);
  197.         }
  198.     } else if (IPC_GET_METHOD(*olddata) == IPC_M_CONNECT_ME_TO) {
  199.         /* If the users accepted call, connect */
  200.         if (!IPC_GET_RETVAL(answer->data)) {
  201.             ipc_phone_connect((phone_t *) IPC_GET_ARG5(*olddata),
  202.                 &TASK->answerbox);
  203.         }
  204.     } else if (IPC_GET_METHOD(*olddata) == IPC_M_SHARE_OUT) {
  205.         if (!IPC_GET_RETVAL(answer->data)) {
  206.             /* Accepted, handle as_area receipt */
  207.             ipl_t ipl;
  208.             int rc;
  209.             as_t *as;
  210.            
  211.             ipl = interrupts_disable();
  212.             spinlock_lock(&answer->sender->lock);
  213.             as = answer->sender->as;
  214.             spinlock_unlock(&answer->sender->lock);
  215.             interrupts_restore(ipl);
  216.            
  217.             rc = as_area_share(as, IPC_GET_ARG1(*olddata),
  218.                 IPC_GET_ARG2(*olddata), AS,
  219.                 IPC_GET_ARG1(answer->data), IPC_GET_ARG3(*olddata));
  220.             IPC_SET_RETVAL(answer->data, rc);
  221.             return rc;
  222.         }
  223.     } else if (IPC_GET_METHOD(*olddata) == IPC_M_SHARE_IN) {
  224.         if (!IPC_GET_RETVAL(answer->data)) {
  225.             ipl_t ipl;
  226.             as_t *as;
  227.             int rc;
  228.            
  229.             ipl = interrupts_disable();
  230.             spinlock_lock(&answer->sender->lock);
  231.             as = answer->sender->as;
  232.             spinlock_unlock(&answer->sender->lock);
  233.             interrupts_restore(ipl);
  234.            
  235.             rc = as_area_share(AS, IPC_GET_ARG1(answer->data),
  236.                 IPC_GET_ARG2(*olddata), as, IPC_GET_ARG1(*olddata),
  237.                 IPC_GET_ARG2(answer->data));
  238.             IPC_SET_RETVAL(answer->data, rc);
  239.         }
  240.     } else if (IPC_GET_METHOD(*olddata) == IPC_M_DATA_READ) {
  241.         ASSERT(!answer->buffer);
  242.         if (!IPC_GET_RETVAL(answer->data)) {
  243.             /* The recipient agreed to send data. */
  244.             uintptr_t src = IPC_GET_ARG1(answer->data);
  245.             uintptr_t dst = IPC_GET_ARG1(*olddata);
  246.             size_t max_size = IPC_GET_ARG2(*olddata);
  247.             size_t size = IPC_GET_ARG2(answer->data);
  248.             if (size && size <= max_size) {
  249.                 /*
  250.                  * Copy the destination VA so that this piece of
  251.                  * information is not lost.
  252.                  */
  253.                 IPC_SET_ARG1(answer->data, dst);
  254.  
  255.                 answer->buffer = malloc(size, 0);
  256.                 int rc = copy_from_uspace(answer->buffer,
  257.                     (void *) src, size);
  258.                 if (rc) {
  259.                     IPC_SET_RETVAL(answer->data, rc);
  260.                     free(answer->buffer);
  261.                     answer->buffer = NULL;
  262.                 }
  263.             } else if (!size) {
  264.                 IPC_SET_RETVAL(answer->data, EOK);
  265.             } else {
  266.                 IPC_SET_RETVAL(answer->data, ELIMIT);
  267.             }
  268.         }
  269.     } else if (IPC_GET_METHOD(*olddata) == IPC_M_DATA_WRITE) {
  270.         ASSERT(answer->buffer);
  271.         if (!IPC_GET_RETVAL(answer->data)) {
  272.             /* The recipient agreed to receive data. */
  273.             int rc;
  274.             uintptr_t dst;
  275.             size_t size;
  276.             size_t max_size;
  277.  
  278.             dst = (uintptr_t)IPC_GET_ARG1(answer->data);
  279.             size = (size_t)IPC_GET_ARG2(answer->data);
  280.             max_size = (size_t)IPC_GET_ARG2(*olddata);
  281.  
  282.             if (size <= max_size) {
  283.                 rc = copy_to_uspace((void *) dst,
  284.                     answer->buffer, size);
  285.                 if (rc)
  286.                     IPC_SET_RETVAL(answer->data, rc);
  287.             } else {
  288.                 IPC_SET_RETVAL(answer->data, ELIMIT);
  289.             }
  290.         }
  291.         free(answer->buffer);
  292.         answer->buffer = NULL;
  293.     }
  294.     return 0;
  295. }
  296.  
  297. /** Called before the request is sent.
  298.  *
  299.  * @param call      Call structure with the request.
  300.  * @param phone     Phone that the call will be sent through.
  301.  *
  302.  * @return      Return 0 on success, ELIMIT or EPERM on error.
  303.  */
  304. static int request_preprocess(call_t *call, phone_t *phone)
  305. {
  306.     int newphid;
  307.     size_t size;
  308.     uintptr_t src;
  309.     int rc;
  310.  
  311.     switch (IPC_GET_METHOD(call->data)) {
  312.     case IPC_M_CONNECT_ME_TO:
  313.         newphid = phone_alloc();
  314.         if (newphid < 0)
  315.             return ELIMIT;
  316.         /* Set arg5 for server */
  317.         IPC_SET_ARG5(call->data, (unative_t) &TASK->phones[newphid]);
  318.         call->flags |= IPC_CALL_CONN_ME_TO;
  319.         call->priv = newphid;
  320.         break;
  321.     case IPC_M_SHARE_OUT:
  322.         size = as_area_get_size(IPC_GET_ARG1(call->data));
  323.         if (!size)
  324.             return EPERM;
  325.         IPC_SET_ARG2(call->data, size);
  326.         break;
  327.     case IPC_M_DATA_READ:
  328.         size = IPC_GET_ARG2(call->data);
  329.         if ((size <= 0 || (size > DATA_XFER_LIMIT)))
  330.             return ELIMIT;
  331.         break;
  332.     case IPC_M_DATA_WRITE:
  333.         src = IPC_GET_ARG1(call->data);
  334.         size = IPC_GET_ARG2(call->data);
  335.        
  336.         if ((size <= 0) || (size > DATA_XFER_LIMIT))
  337.             return ELIMIT;
  338.        
  339.         call->buffer = (uint8_t *) malloc(size, 0);
  340.         rc = copy_from_uspace(call->buffer, (void *) src, size);
  341.         if (rc != 0) {
  342.             free(call->buffer);
  343.             return rc;
  344.         }
  345.         break;
  346. #ifdef CONFIG_UDEBUG
  347.     case IPC_M_DEBUG_ALL:
  348.         return udebug_request_preprocess(call, phone);
  349. #endif
  350.     default:
  351.         break;
  352.     }
  353.     return 0;
  354. }
  355.  
  356. /*******************************************************************************
  357.  * Functions called to process received call/answer before passing it to uspace.
  358.  *******************************************************************************/
  359.  
  360. /** Do basic kernel processing of received call answer.
  361.  *
  362.  * @param call      Call structure with the answer.
  363.  */
  364. static void process_answer(call_t *call)
  365. {
  366.     if (((native_t) IPC_GET_RETVAL(call->data) == EHANGUP) &&
  367.         (call->flags & IPC_CALL_FORWARDED))
  368.         IPC_SET_RETVAL(call->data, EFORWARD);
  369.  
  370.     if (call->flags & IPC_CALL_CONN_ME_TO) {
  371.         if (IPC_GET_RETVAL(call->data))
  372.             phone_dealloc(call->priv);
  373.         else
  374.             IPC_SET_ARG5(call->data, call->priv);
  375.     }
  376.  
  377.     if (call->buffer) {
  378.         /* This must be an affirmative answer to IPC_M_DATA_READ. */
  379.         /* or IPC_M_DEBUG_ALL/UDEBUG_M_MEM_READ... */
  380.         uintptr_t dst = IPC_GET_ARG1(call->data);
  381.         size_t size = IPC_GET_ARG2(call->data);
  382.         int rc = copy_to_uspace((void *) dst, call->buffer, size);
  383.         if (rc)
  384.             IPC_SET_RETVAL(call->data, rc);
  385.         free(call->buffer);
  386.         call->buffer = NULL;
  387.     }
  388. }
  389.  
  390. /** Do basic kernel processing of received call request.
  391.  *
  392.  * @param box       Destination answerbox structure.
  393.  * @param call      Call structure with the request.
  394.  *
  395.  * @return      Return 0 if the call should be passed to userspace.
  396.  *          Return -1 if the call should be ignored.
  397.  */
  398. static int process_request(answerbox_t *box, call_t *call)
  399. {
  400.     int phoneid;
  401.  
  402.     if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_TO_ME) {
  403.         phoneid = phone_alloc();
  404.         if (phoneid < 0) { /* Failed to allocate phone */
  405.             IPC_SET_RETVAL(call->data, ELIMIT);
  406.             ipc_answer(box, call);
  407.             return -1;
  408.         }
  409.         IPC_SET_ARG5(call->data, phoneid);
  410.     }
  411.     switch (IPC_GET_METHOD(call->data)) {
  412.     case IPC_M_DEBUG_ALL:
  413.         return -1;
  414.     default:
  415.         break;
  416.     }
  417.     return 0;
  418. }
  419.  
  420. /** Make a fast call over IPC, wait for reply and return to user.
  421.  *
  422.  * This function can handle only three arguments of payload, but is faster than
  423.  * the generic function (i.e. sys_ipc_call_sync_slow()).
  424.  *
  425.  * @param phoneid   Phone handle for the call.
  426.  * @param method    Method of the call.
  427.  * @param arg1      Service-defined payload argument.
  428.  * @param arg2      Service-defined payload argument.
  429.  * @param arg3      Service-defined payload argument.
  430.  * @param data      Address of userspace structure where the reply call will
  431.  *          be stored.
  432.  *
  433.  * @return      Returns 0 on success.
  434.  *          Return ENOENT if there is no such phone handle.
  435.  */
  436. unative_t sys_ipc_call_sync_fast(unative_t phoneid, unative_t method,
  437.     unative_t arg1, unative_t arg2, unative_t arg3, ipc_data_t *data)
  438. {
  439.     call_t call;
  440.     phone_t *phone;
  441.     int res;
  442.     int rc;
  443.    
  444.     GET_CHECK_PHONE(phone, phoneid, return ENOENT);
  445.  
  446.     ipc_call_static_init(&call);
  447.     IPC_SET_METHOD(call.data, method);
  448.     IPC_SET_ARG1(call.data, arg1);
  449.     IPC_SET_ARG2(call.data, arg2);
  450.     IPC_SET_ARG3(call.data, arg3);
  451.     /*
  452.      * To achieve deterministic behavior, zero out arguments that are beyond
  453.      * the limits of the fast version.
  454.      */
  455.     IPC_SET_ARG4(call.data, 0);
  456.     IPC_SET_ARG5(call.data, 0);
  457.  
  458.     if (!(res = request_preprocess(&call, phone))) {
  459. #ifdef CONFIG_UDEBUG
  460.         udebug_stoppable_begin();
  461. #endif
  462.         rc = ipc_call_sync(phone, &call);
  463. #ifdef CONFIG_UDEBUG
  464.         udebug_stoppable_end();
  465. #endif
  466.         if (rc != EOK)
  467.             return rc;
  468.         process_answer(&call);
  469.  
  470.     } else {
  471.         IPC_SET_RETVAL(call.data, res);
  472.     }
  473.     rc = STRUCT_TO_USPACE(&data->args, &call.data.args);
  474.     if (rc != 0)
  475.         return rc;
  476.  
  477.     return 0;
  478. }
  479.  
  480. /** Make a synchronous IPC call allowing to transmit the entire payload.
  481.  *
  482.  * @param phoneid   Phone handle for the call.
  483.  * @param question  Userspace address of call data with the request.
  484.  * @param reply     Userspace address of call data where to store the
  485.  *          answer.
  486.  *
  487.  * @return      Zero on success or an error code.
  488.  */
  489. unative_t sys_ipc_call_sync_slow(unative_t phoneid, ipc_data_t *question,
  490.     ipc_data_t *reply)
  491. {
  492.     call_t call;
  493.     phone_t *phone;
  494.     int res;
  495.     int rc;
  496.  
  497.     ipc_call_static_init(&call);
  498.     rc = copy_from_uspace(&call.data.args, &question->args,
  499.         sizeof(call.data.args));
  500.     if (rc != 0)
  501.         return (unative_t) rc;
  502.  
  503.     GET_CHECK_PHONE(phone, phoneid, return ENOENT);
  504.  
  505.     if (!(res = request_preprocess(&call, phone))) {
  506. #ifdef CONFIG_UDEBUG
  507.         udebug_stoppable_begin();
  508. #endif
  509.         rc = ipc_call_sync(phone, &call);
  510. #ifdef CONFIG_UDEBUG
  511.         udebug_stoppable_end();
  512. #endif
  513.         if (rc != EOK)
  514.             return rc;
  515.         process_answer(&call);
  516.     } else
  517.         IPC_SET_RETVAL(call.data, res);
  518.  
  519.     rc = STRUCT_TO_USPACE(&reply->args, &call.data.args);
  520.     if (rc != 0)
  521.         return rc;
  522.  
  523.     return 0;
  524. }
  525.  
  526. /** Check that the task did not exceed the allowed limit of asynchronous calls.
  527.  *
  528.  * @return      Return 0 if limit not reached or -1 if limit exceeded.
  529.  */
  530. static int check_call_limit(void)
  531. {
  532.     if (atomic_preinc(&TASK->active_calls) > IPC_MAX_ASYNC_CALLS) {
  533.         atomic_dec(&TASK->active_calls);
  534.         return -1;
  535.     }
  536.     return 0;
  537. }
  538.  
  539. /** Make a fast asynchronous call over IPC.
  540.  *
  541.  * This function can only handle four arguments of payload, but is faster than
  542.  * the generic function sys_ipc_call_async_slow().
  543.  *
  544.  * @param phoneid   Phone handle for the call.
  545.  * @param method    Method of the call.
  546.  * @param arg1      Service-defined payload argument.
  547.  * @param arg2      Service-defined payload argument.
  548.  * @param arg3      Service-defined payload argument.
  549.  * @param arg4      Service-defined payload argument.
  550.  *
  551.  * @return      Return call hash on success.
  552.  *          Return IPC_CALLRET_FATAL in case of a fatal error and
  553.  *          IPC_CALLRET_TEMPORARY if there are too many pending
  554.  *          asynchronous requests; answers should be handled first.
  555.  */
  556. unative_t sys_ipc_call_async_fast(unative_t phoneid, unative_t method,
  557.     unative_t arg1, unative_t arg2, unative_t arg3, unative_t arg4)
  558. {
  559.     call_t *call;
  560.     phone_t *phone;
  561.     int res;
  562.  
  563.     if (check_call_limit())
  564.         return IPC_CALLRET_TEMPORARY;
  565.  
  566.     GET_CHECK_PHONE(phone, phoneid, return IPC_CALLRET_FATAL);
  567.  
  568.     call = ipc_call_alloc(0);
  569.     IPC_SET_METHOD(call->data, method);
  570.     IPC_SET_ARG1(call->data, arg1);
  571.     IPC_SET_ARG2(call->data, arg2);
  572.     IPC_SET_ARG3(call->data, arg3);
  573.     IPC_SET_ARG4(call->data, arg4);
  574.     /*
  575.      * To achieve deterministic behavior, zero out arguments that are beyond
  576.      * the limits of the fast version.
  577.      */
  578.     IPC_SET_ARG5(call->data, 0);
  579.  
  580.     if (!(res = request_preprocess(call, phone)))
  581.         ipc_call(phone, call);
  582.     else
  583.         ipc_backsend_err(phone, call, res);
  584.  
  585.     return (unative_t) call;
  586. }
  587.  
  588. /** Make an asynchronous IPC call allowing to transmit the entire payload.
  589.  *
  590.  * @param phoneid   Phone handle for the call.
  591.  * @param data      Userspace address of call data with the request.
  592.  *
  593.  * @return      See sys_ipc_call_async_fast().
  594.  */
  595. unative_t sys_ipc_call_async_slow(unative_t phoneid, ipc_data_t *data)
  596. {
  597.     call_t *call;
  598.     phone_t *phone;
  599.     int res;
  600.     int rc;
  601.  
  602.     if (check_call_limit())
  603.         return IPC_CALLRET_TEMPORARY;
  604.  
  605.     GET_CHECK_PHONE(phone, phoneid, return IPC_CALLRET_FATAL);
  606.  
  607.     call = ipc_call_alloc(0);
  608.     rc = copy_from_uspace(&call->data.args, &data->args,
  609.         sizeof(call->data.args));
  610.     if (rc != 0) {
  611.         ipc_call_free(call);
  612.         return (unative_t) rc;
  613.     }
  614.     if (!(res = request_preprocess(call, phone)))
  615.         ipc_call(phone, call);
  616.     else
  617.         ipc_backsend_err(phone, call, res);
  618.  
  619.     return (unative_t) call;
  620. }
  621.  
  622. /** Forward a received call to another destination.
  623.  *
  624.  * @param callid    Hash of the call to forward.
  625.  * @param phoneid   Phone handle to use for forwarding.
  626.  * @param method    New method to use for the forwarded call.
  627.  * @param arg1      New value of the first argument for the forwarded call.
  628.  * @param arg2      New value of the second argument for the forwarded call.
  629.  * @param mode      Flags that specify mode of the forward operation.
  630.  *
  631.  * @return      Return 0 on succes, otherwise return an error code.
  632.  *
  633.  * In case the original method is a system method, ARG1, ARG2 and ARG3 are
  634.  * overwritten in the forwarded message with the new method and the new arg1 and
  635.  * arg2, respectively. Otherwise the METHOD, ARG1 and ARG2 are rewritten with
  636.  * the new method, arg1 and arg2, respectively. Also note there is a set of
  637.  * immutable methods, for which the new method and argument is not set and
  638.  * these values are ignored.
  639.  *
  640.  * Warning: When implementing support for changing additional payload
  641.  *      arguments, make sure that ARG5 is not rewritten for certain
  642.  *      system IPC
  643.  */
  644. unative_t sys_ipc_forward_fast(unative_t callid, unative_t phoneid,
  645.     unative_t method, unative_t arg1, unative_t arg2, int mode)
  646. {
  647.     call_t *call;
  648.     phone_t *phone;
  649.  
  650.     call = get_call(callid);
  651.     if (!call)
  652.         return ENOENT;
  653.  
  654.     call->flags |= IPC_CALL_FORWARDED;
  655.  
  656.     GET_CHECK_PHONE(phone, phoneid, {
  657.         IPC_SET_RETVAL(call->data, EFORWARD);
  658.         ipc_answer(&TASK->answerbox, call);
  659.         return ENOENT;
  660.     });
  661.  
  662.     if (!method_is_forwardable(IPC_GET_METHOD(call->data))) {
  663.         IPC_SET_RETVAL(call->data, EFORWARD);
  664.         ipc_answer(&TASK->answerbox, call);
  665.         return EPERM;
  666.     }
  667.  
  668.     /*
  669.      * Userspace is not allowed to change method of system methods on
  670.      * forward, allow changing ARG1, ARG2 and ARG3 by means of method,
  671.      * arg1 and arg2.
  672.      * If the method is immutable, don't change anything.
  673.      */
  674.     if (!method_is_immutable(IPC_GET_METHOD(call->data))) {
  675.         if (method_is_system(IPC_GET_METHOD(call->data))) {
  676.             if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_TO_ME)
  677.                 phone_dealloc(IPC_GET_ARG5(call->data));
  678.  
  679.             IPC_SET_ARG1(call->data, method);
  680.             IPC_SET_ARG2(call->data, arg1);
  681.             IPC_SET_ARG3(call->data, arg2);
  682.         } else {
  683.             IPC_SET_METHOD(call->data, method);
  684.             IPC_SET_ARG1(call->data, arg1);
  685.             IPC_SET_ARG2(call->data, arg2);
  686.         }
  687.     }
  688.  
  689.     return ipc_forward(call, phone, &TASK->answerbox, mode);
  690. }
  691.  
  692. /** Answer an IPC call - fast version.
  693.  *
  694.  * This function can handle only two return arguments of payload, but is faster
  695.  * than the generic sys_ipc_answer().
  696.  *
  697.  * @param callid    Hash of the call to be answered.
  698.  * @param retval    Return value of the answer.
  699.  * @param arg1      Service-defined return value.
  700.  * @param arg2      Service-defined return value.
  701.  * @param arg3      Service-defined return value.
  702.  * @param arg4      Service-defined return value.
  703.  *
  704.  * @return      Return 0 on success, otherwise return an error code.   
  705.  */
  706. unative_t sys_ipc_answer_fast(unative_t callid, unative_t retval,
  707.     unative_t arg1, unative_t arg2, unative_t arg3, unative_t arg4)
  708. {
  709.     call_t *call;
  710.     ipc_data_t saved_data;
  711.     int saveddata = 0;
  712.     int rc;
  713.  
  714.     /* Do not answer notification callids */
  715.     if (callid & IPC_CALLID_NOTIFICATION)
  716.         return 0;
  717.  
  718.     call = get_call(callid);
  719.     if (!call)
  720.         return ENOENT;
  721.  
  722.     if (answer_need_old(call)) {
  723.         memcpy(&saved_data, &call->data, sizeof(call->data));
  724.         saveddata = 1;
  725.     }
  726.  
  727.     IPC_SET_RETVAL(call->data, retval);
  728.     IPC_SET_ARG1(call->data, arg1);
  729.     IPC_SET_ARG2(call->data, arg2);
  730.     IPC_SET_ARG3(call->data, arg3);
  731.     IPC_SET_ARG4(call->data, arg4);
  732.     /*
  733.      * To achieve deterministic behavior, zero out arguments that are beyond
  734.      * the limits of the fast version.
  735.      */
  736.     IPC_SET_ARG5(call->data, 0);
  737.     rc = answer_preprocess(call, saveddata ? &saved_data : NULL);
  738.  
  739.     ipc_answer(&TASK->answerbox, call);
  740.     return rc;
  741. }
  742.  
  743. /** Answer an IPC call.
  744.  *
  745.  * @param callid    Hash of the call to be answered.
  746.  * @param data      Userspace address of call data with the answer.
  747.  *
  748.  * @return      Return 0 on success, otherwise return an error code.
  749.  */
  750. unative_t sys_ipc_answer_slow(unative_t callid, ipc_data_t *data)
  751. {
  752.     call_t *call;
  753.     ipc_data_t saved_data;
  754.     int saveddata = 0;
  755.     int rc;
  756.  
  757.     /* Do not answer notification callids */
  758.     if (callid & IPC_CALLID_NOTIFICATION)
  759.         return 0;
  760.  
  761.     call = get_call(callid);
  762.     if (!call)
  763.         return ENOENT;
  764.  
  765.     if (answer_need_old(call)) {
  766.         memcpy(&saved_data, &call->data, sizeof(call->data));
  767.         saveddata = 1;
  768.     }
  769.     rc = copy_from_uspace(&call->data.args, &data->args,
  770.         sizeof(call->data.args));
  771.     if (rc != 0)
  772.         return rc;
  773.  
  774.     rc = answer_preprocess(call, saveddata ? &saved_data : NULL);
  775.    
  776.     ipc_answer(&TASK->answerbox, call);
  777.  
  778.     return rc;
  779. }
  780.  
  781. /** Hang up a phone.
  782.  *
  783.  * @param       Phone handle of the phone to be hung up.
  784.  *
  785.  * @return      Return 0 on success or an error code.
  786.  */
  787. unative_t sys_ipc_hangup(int phoneid)
  788. {
  789.     phone_t *phone;
  790.  
  791.     GET_CHECK_PHONE(phone, phoneid, return ENOENT);
  792.  
  793.     if (ipc_phone_hangup(phone))
  794.         return -1;
  795.  
  796.     return 0;
  797. }
  798.  
  799. /** Wait for an incoming IPC call or an answer.
  800.  *
  801.  * @param calldata  Pointer to buffer where the call/answer data is stored.
  802.  * @param usec      Timeout. See waitq_sleep_timeout() for explanation.
  803.  * @param flags     Select mode of sleep operation. See waitq_sleep_timeout()
  804.  *          for explanation.
  805.  *
  806.  * @return      Hash of the call.
  807.  *          If IPC_CALLID_NOTIFICATION bit is set in the hash, the
  808.  *          call is a notification. IPC_CALLID_ANSWERED denotes an
  809.  *          answer.
  810.  */
  811. unative_t sys_ipc_wait_for_call(ipc_data_t *calldata, uint32_t usec, int flags)
  812. {
  813.     call_t *call;
  814.  
  815. restart:
  816.  
  817. #ifdef CONFIG_UDEBUG
  818.     udebug_stoppable_begin();
  819. #endif 
  820.     call = ipc_wait_for_call(&TASK->answerbox, usec,
  821.         flags | SYNCH_FLAGS_INTERRUPTIBLE);
  822.  
  823. #ifdef CONFIG_UDEBUG
  824.     udebug_stoppable_end();
  825. #endif
  826.     if (!call)
  827.         return 0;
  828.  
  829.     if (call->flags & IPC_CALL_NOTIF) {
  830.         ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
  831.  
  832.         /* Set in_phone_hash to the interrupt counter */
  833.         call->data.phone = (void *) call->priv;
  834.        
  835.         STRUCT_TO_USPACE(calldata, &call->data);
  836.  
  837.         ipc_call_free(call);
  838.        
  839.         return ((unative_t) call) | IPC_CALLID_NOTIFICATION;
  840.     }
  841.  
  842.     if (call->flags & IPC_CALL_ANSWERED) {
  843.         process_answer(call);
  844.  
  845.         ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
  846.  
  847.         if (call->flags & IPC_CALL_DISCARD_ANSWER) {
  848.             ipc_call_free(call);
  849.             goto restart;
  850.         } else {
  851.             /*
  852.              * Decrement the counter of active calls only if the
  853.              * call is not an answer to IPC_M_PHONE_HUNGUP,
  854.              * which doesn't contribute to the counter.
  855.              */
  856.             atomic_dec(&TASK->active_calls);
  857.         }
  858.  
  859.         STRUCT_TO_USPACE(&calldata->args, &call->data.args);
  860.         ipc_call_free(call);
  861.  
  862.         return ((unative_t) call) | IPC_CALLID_ANSWERED;
  863.     }
  864.  
  865.     if (process_request(&TASK->answerbox, call))
  866.         goto restart;
  867.  
  868.     /* Include phone address('id') of the caller in the request,
  869.      * copy whole call->data, not only call->data.args */
  870.     if (STRUCT_TO_USPACE(calldata, &call->data)) {
  871.         return 0;
  872.     }
  873.     return (unative_t)call;
  874. }
  875.  
  876. /** Connect an IRQ handler to a task.
  877.  *
  878.  * @param inr       IRQ number.
  879.  * @param devno     Device number.
  880.  * @param method    Method to be associated with the notification.
  881.  * @param ucode     Uspace pointer to the top-half pseudocode.
  882.  *
  883.  * @return      EPERM or a return code returned by ipc_irq_register().
  884.  */
  885. unative_t sys_ipc_register_irq(inr_t inr, devno_t devno, unative_t method,
  886.     irq_code_t *ucode)
  887. {
  888.     if (!(cap_get(TASK) & CAP_IRQ_REG))
  889.         return EPERM;
  890.  
  891.     return ipc_irq_register(&TASK->answerbox, inr, devno, method, ucode);
  892. }
  893.  
  894. /** Disconnect an IRQ handler from a task.
  895.  *
  896.  * @param inr       IRQ number.
  897.  * @param devno     Device number.
  898.  *
  899.  * @return      Zero on success or EPERM on error..
  900.  */
  901. unative_t sys_ipc_unregister_irq(inr_t inr, devno_t devno)
  902. {
  903.     if (!(cap_get(TASK) & CAP_IRQ_REG))
  904.         return EPERM;
  905.  
  906.     ipc_irq_unregister(&TASK->answerbox, inr, devno);
  907.  
  908.     return 0;
  909. }
  910.  
  911. #include <console/console.h>
  912.  
  913. /**
  914.  * Syscall connect to a task by id.
  915.  *
  916.  * @return      Phone id on success, or negative error code.
  917.  */
  918. unative_t sys_ipc_connect_kbox(sysarg64_t *uspace_taskid_arg)
  919. {
  920. #ifdef CONFIG_UDEBUG
  921.     sysarg64_t taskid_arg;
  922.     int rc;
  923.    
  924.     rc = copy_from_uspace(&taskid_arg, uspace_taskid_arg, sizeof(sysarg64_t));
  925.     if (rc != 0)
  926.         return (unative_t) rc;
  927.  
  928.     LOG("sys_ipc_connect_kbox(%" PRIu64 ")\n", taskid_arg.value);
  929.  
  930.     return ipc_connect_kbox(taskid_arg.value);
  931. #else
  932.     return (unative_t) ENOTSUP;
  933. #endif
  934. }
  935.  
  936. /** @}
  937.  */
  938.