Subversion Repositories HelenOS

Rev

Rev 3431 | Rev 3471 | Go to most recent revision | 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/ipc_kbox.h>
  46. #include <udebug/udebug_ipc.h>
  47. #include <arch/interrupt.h>
  48. #include <print.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.         rc = ipc_call_sync(phone, &call);
  460.         if (rc != EOK)
  461.             return rc;
  462.         process_answer(&call);
  463.     } else {
  464.         IPC_SET_RETVAL(call.data, res);
  465.     }
  466.     rc = STRUCT_TO_USPACE(&data->args, &call.data.args);
  467.     if (rc != 0)
  468.         return rc;
  469.  
  470.     return 0;
  471. }
  472.  
  473. /** Make a synchronous IPC call allowing to transmit the entire payload.
  474.  *
  475.  * @param phoneid   Phone handle for the call.
  476.  * @param question  Userspace address of call data with the request.
  477.  * @param reply     Userspace address of call data where to store the
  478.  *          answer.
  479.  *
  480.  * @return      Zero on success or an error code.
  481.  */
  482. unative_t sys_ipc_call_sync_slow(unative_t phoneid, ipc_data_t *question,
  483.     ipc_data_t *reply)
  484. {
  485.     call_t call;
  486.     phone_t *phone;
  487.     int res;
  488.     int rc;
  489.  
  490.     ipc_call_static_init(&call);
  491.     rc = copy_from_uspace(&call.data.args, &question->args,
  492.         sizeof(call.data.args));
  493.     if (rc != 0)
  494.         return (unative_t) rc;
  495.  
  496.     GET_CHECK_PHONE(phone, phoneid, return ENOENT);
  497.  
  498.     if (!(res = request_preprocess(&call, phone))) {
  499.         rc = ipc_call_sync(phone, &call);
  500.         if (rc != EOK)
  501.             return rc;
  502.         process_answer(&call);
  503.     } else
  504.         IPC_SET_RETVAL(call.data, res);
  505.  
  506.     rc = STRUCT_TO_USPACE(&reply->args, &call.data.args);
  507.     if (rc != 0)
  508.         return rc;
  509.  
  510.     return 0;
  511. }
  512.  
  513. /** Check that the task did not exceed the allowed limit of asynchronous calls.
  514.  *
  515.  * @return      Return 0 if limit not reached or -1 if limit exceeded.
  516.  */
  517. static int check_call_limit(void)
  518. {
  519.     if (atomic_preinc(&TASK->active_calls) > IPC_MAX_ASYNC_CALLS) {
  520.         atomic_dec(&TASK->active_calls);
  521.         return -1;
  522.     }
  523.     return 0;
  524. }
  525.  
  526. /** Make a fast asynchronous call over IPC.
  527.  *
  528.  * This function can only handle four arguments of payload, but is faster than
  529.  * the generic function sys_ipc_call_async_slow().
  530.  *
  531.  * @param phoneid   Phone handle for the call.
  532.  * @param method    Method of the call.
  533.  * @param arg1      Service-defined payload argument.
  534.  * @param arg2      Service-defined payload argument.
  535.  * @param arg3      Service-defined payload argument.
  536.  * @param arg4      Service-defined payload argument.
  537.  *
  538.  * @return      Return call hash on success.
  539.  *          Return IPC_CALLRET_FATAL in case of a fatal error and
  540.  *          IPC_CALLRET_TEMPORARY if there are too many pending
  541.  *          asynchronous requests; answers should be handled first.
  542.  */
  543. unative_t sys_ipc_call_async_fast(unative_t phoneid, unative_t method,
  544.     unative_t arg1, unative_t arg2, unative_t arg3, unative_t arg4)
  545. {
  546.     call_t *call;
  547.     phone_t *phone;
  548.     int res;
  549.  
  550.     if (check_call_limit())
  551.         return IPC_CALLRET_TEMPORARY;
  552.  
  553.     GET_CHECK_PHONE(phone, phoneid, return IPC_CALLRET_FATAL);
  554.  
  555.     call = ipc_call_alloc(0);
  556.     IPC_SET_METHOD(call->data, method);
  557.     IPC_SET_ARG1(call->data, arg1);
  558.     IPC_SET_ARG2(call->data, arg2);
  559.     IPC_SET_ARG3(call->data, arg3);
  560.     IPC_SET_ARG4(call->data, arg4);
  561.     /*
  562.      * To achieve deterministic behavior, zero out arguments that are beyond
  563.      * the limits of the fast version.
  564.      */
  565.     IPC_SET_ARG5(call->data, 0);
  566.  
  567.     if (!(res = request_preprocess(call, phone)))
  568.         ipc_call(phone, call);
  569.     else
  570.         ipc_backsend_err(phone, call, res);
  571.  
  572.     return (unative_t) call;
  573. }
  574.  
  575. /** Make an asynchronous IPC call allowing to transmit the entire payload.
  576.  *
  577.  * @param phoneid   Phone handle for the call.
  578.  * @param data      Userspace address of call data with the request.
  579.  *
  580.  * @return      See sys_ipc_call_async_fast().
  581.  */
  582. unative_t sys_ipc_call_async_slow(unative_t phoneid, ipc_data_t *data)
  583. {
  584.     call_t *call;
  585.     phone_t *phone;
  586.     int res;
  587.     int rc;
  588.  
  589.     if (check_call_limit())
  590.         return IPC_CALLRET_TEMPORARY;
  591.  
  592.     GET_CHECK_PHONE(phone, phoneid, return IPC_CALLRET_FATAL);
  593.  
  594.     call = ipc_call_alloc(0);
  595.     rc = copy_from_uspace(&call->data.args, &data->args,
  596.         sizeof(call->data.args));
  597.     if (rc != 0) {
  598.         ipc_call_free(call);
  599.         return (unative_t) rc;
  600.     }
  601.     if (!(res = request_preprocess(call, phone)))
  602.         ipc_call(phone, call);
  603.     else
  604.         ipc_backsend_err(phone, call, res);
  605.  
  606.     return (unative_t) call;
  607. }
  608.  
  609. /** Forward a received call to another destination.
  610.  *
  611.  * @param callid    Hash of the call to forward.
  612.  * @param phoneid   Phone handle to use for forwarding.
  613.  * @param method    New method to use for the forwarded call.
  614.  * @param arg1      New value of the first argument for the forwarded call.
  615.  * @param arg2      New value of the second argument for the forwarded call.
  616.  * @param mode      Flags that specify mode of the forward operation.
  617.  *
  618.  * @return      Return 0 on succes, otherwise return an error code.
  619.  *
  620.  * In case the original method is a system method, ARG1, ARG2 and ARG3 are
  621.  * overwritten in the forwarded message with the new method and the new arg1 and
  622.  * arg2, respectively. Otherwise the METHOD, ARG1 and ARG2 are rewritten with
  623.  * the new method, arg1 and arg2, respectively. Also note there is a set of
  624.  * immutable methods, for which the new method and argument is not set and
  625.  * these values are ignored.
  626.  *
  627.  * Warning: When implementing support for changing additional payload
  628.  *      arguments, make sure that ARG5 is not rewritten for certain
  629.  *      system IPC
  630.  */
  631. unative_t sys_ipc_forward_fast(unative_t callid, unative_t phoneid,
  632.     unative_t method, unative_t arg1, unative_t arg2, int mode)
  633. {
  634.     call_t *call;
  635.     phone_t *phone;
  636.  
  637.     call = get_call(callid);
  638.     if (!call)
  639.         return ENOENT;
  640.  
  641.     call->flags |= IPC_CALL_FORWARDED;
  642.  
  643.     GET_CHECK_PHONE(phone, phoneid, {
  644.         IPC_SET_RETVAL(call->data, EFORWARD);
  645.         ipc_answer(&TASK->answerbox, call);
  646.         return ENOENT;
  647.     });
  648.  
  649.     if (!method_is_forwardable(IPC_GET_METHOD(call->data))) {
  650.         IPC_SET_RETVAL(call->data, EFORWARD);
  651.         ipc_answer(&TASK->answerbox, call);
  652.         return EPERM;
  653.     }
  654.  
  655.     /*
  656.      * Userspace is not allowed to change method of system methods on
  657.      * forward, allow changing ARG1, ARG2 and ARG3 by means of method,
  658.      * arg1 and arg2.
  659.      * If the method is immutable, don't change anything.
  660.      */
  661.     if (!method_is_immutable(IPC_GET_METHOD(call->data))) {
  662.         if (method_is_system(IPC_GET_METHOD(call->data))) {
  663.             if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_TO_ME)
  664.                 phone_dealloc(IPC_GET_ARG5(call->data));
  665.  
  666.             IPC_SET_ARG1(call->data, method);
  667.             IPC_SET_ARG2(call->data, arg1);
  668.             IPC_SET_ARG3(call->data, arg2);
  669.         } else {
  670.             IPC_SET_METHOD(call->data, method);
  671.             IPC_SET_ARG1(call->data, arg1);
  672.             IPC_SET_ARG2(call->data, arg2);
  673.         }
  674.     }
  675.  
  676.     return ipc_forward(call, phone, &TASK->answerbox, mode);
  677. }
  678.  
  679. /** Answer an IPC call - fast version.
  680.  *
  681.  * This function can handle only two return arguments of payload, but is faster
  682.  * than the generic sys_ipc_answer().
  683.  *
  684.  * @param callid    Hash of the call to be answered.
  685.  * @param retval    Return value of the answer.
  686.  * @param arg1      Service-defined return value.
  687.  * @param arg2      Service-defined return value.
  688.  * @param arg3      Service-defined return value.
  689.  * @param arg4      Service-defined return value.
  690.  *
  691.  * @return      Return 0 on success, otherwise return an error code.   
  692.  */
  693. unative_t sys_ipc_answer_fast(unative_t callid, unative_t retval,
  694.     unative_t arg1, unative_t arg2, unative_t arg3, unative_t arg4)
  695. {
  696.     call_t *call;
  697.     ipc_data_t saved_data;
  698.     int saveddata = 0;
  699.     int rc;
  700.  
  701.     /* Do not answer notification callids */
  702.     if (callid & IPC_CALLID_NOTIFICATION)
  703.         return 0;
  704.  
  705.     call = get_call(callid);
  706.     if (!call)
  707.         return ENOENT;
  708.  
  709.     if (answer_need_old(call)) {
  710.         memcpy(&saved_data, &call->data, sizeof(call->data));
  711.         saveddata = 1;
  712.     }
  713.  
  714.     IPC_SET_RETVAL(call->data, retval);
  715.     IPC_SET_ARG1(call->data, arg1);
  716.     IPC_SET_ARG2(call->data, arg2);
  717.     IPC_SET_ARG3(call->data, arg3);
  718.     IPC_SET_ARG4(call->data, arg4);
  719.     /*
  720.      * To achieve deterministic behavior, zero out arguments that are beyond
  721.      * the limits of the fast version.
  722.      */
  723.     IPC_SET_ARG5(call->data, 0);
  724.     rc = answer_preprocess(call, saveddata ? &saved_data : NULL);
  725.  
  726.     ipc_answer(&TASK->answerbox, call);
  727.     return rc;
  728. }
  729.  
  730. /** Answer an IPC call.
  731.  *
  732.  * @param callid    Hash of the call to be answered.
  733.  * @param data      Userspace address of call data with the answer.
  734.  *
  735.  * @return      Return 0 on success, otherwise return an error code.
  736.  */
  737. unative_t sys_ipc_answer_slow(unative_t callid, ipc_data_t *data)
  738. {
  739.     call_t *call;
  740.     ipc_data_t saved_data;
  741.     int saveddata = 0;
  742.     int rc;
  743.  
  744.     /* Do not answer notification callids */
  745.     if (callid & IPC_CALLID_NOTIFICATION)
  746.         return 0;
  747.  
  748.     call = get_call(callid);
  749.     if (!call)
  750.         return ENOENT;
  751.  
  752.     if (answer_need_old(call)) {
  753.         memcpy(&saved_data, &call->data, sizeof(call->data));
  754.         saveddata = 1;
  755.     }
  756.     rc = copy_from_uspace(&call->data.args, &data->args,
  757.         sizeof(call->data.args));
  758.     if (rc != 0)
  759.         return rc;
  760.  
  761.     rc = answer_preprocess(call, saveddata ? &saved_data : NULL);
  762.    
  763.     ipc_answer(&TASK->answerbox, call);
  764.  
  765.     return rc;
  766. }
  767.  
  768. /** Hang up a phone.
  769.  *
  770.  * @param       Phone handle of the phone to be hung up.
  771.  *
  772.  * @return      Return 0 on success or an error code.
  773.  */
  774. unative_t sys_ipc_hangup(int phoneid)
  775. {
  776.     phone_t *phone;
  777.  
  778.     GET_CHECK_PHONE(phone, phoneid, return ENOENT);
  779.  
  780.     if (ipc_phone_hangup(phone))
  781.         return -1;
  782.  
  783.     return 0;
  784. }
  785.  
  786. /** Wait for an incoming IPC call or an answer.
  787.  *
  788.  * @param calldata  Pointer to buffer where the call/answer data is stored.
  789.  * @param usec      Timeout. See waitq_sleep_timeout() for explanation.
  790.  * @param flags     Select mode of sleep operation. See waitq_sleep_timeout()
  791.  *          for explanation.
  792.  *
  793.  * @return      Hash of the call.
  794.  *          If IPC_CALLID_NOTIFICATION bit is set in the hash, the
  795.  *          call is a notification. IPC_CALLID_ANSWERED denotes an
  796.  *          answer.
  797.  */
  798. unative_t sys_ipc_wait_for_call(ipc_data_t *calldata, uint32_t usec, int flags)
  799. {
  800.     call_t *call;
  801.  
  802. restart:   
  803.     call = ipc_wait_for_call(&TASK->answerbox, usec,
  804.         flags | SYNCH_FLAGS_INTERRUPTIBLE);
  805.     if (!call)
  806.         return 0;
  807.  
  808.     if (call->flags & IPC_CALL_NOTIF) {
  809.         ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
  810.  
  811.         /* Set in_phone_hash to the interrupt counter */
  812.         call->data.phone = (void *) call->priv;
  813.        
  814.         STRUCT_TO_USPACE(calldata, &call->data);
  815.  
  816.         ipc_call_free(call);
  817.        
  818.         return ((unative_t) call) | IPC_CALLID_NOTIFICATION;
  819.     }
  820.  
  821.     if (call->flags & IPC_CALL_ANSWERED) {
  822.         process_answer(call);
  823.  
  824.         ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
  825.  
  826.         atomic_dec(&TASK->active_calls);
  827.  
  828.         if (call->flags & IPC_CALL_DISCARD_ANSWER) {
  829.             ipc_call_free(call);
  830.             goto restart;
  831.         }
  832.  
  833.         STRUCT_TO_USPACE(&calldata->args, &call->data.args);
  834.         ipc_call_free(call);
  835.  
  836.         return ((unative_t) call) | IPC_CALLID_ANSWERED;
  837.     }
  838.  
  839.     if (process_request(&TASK->answerbox, call))
  840.         goto restart;
  841.  
  842.     /* Include phone address('id') of the caller in the request,
  843.      * copy whole call->data, not only call->data.args */
  844.     if (STRUCT_TO_USPACE(calldata, &call->data)) {
  845.         return 0;
  846.     }
  847.     return (unative_t)call;
  848. }
  849.  
  850. /** Connect an IRQ handler to a task.
  851.  *
  852.  * @param inr       IRQ number.
  853.  * @param devno     Device number.
  854.  * @param method    Method to be associated with the notification.
  855.  * @param ucode     Uspace pointer to the top-half pseudocode.
  856.  *
  857.  * @return      EPERM or a return code returned by ipc_irq_register().
  858.  */
  859. unative_t sys_ipc_register_irq(inr_t inr, devno_t devno, unative_t method,
  860.     irq_code_t *ucode)
  861. {
  862.     if (!(cap_get(TASK) & CAP_IRQ_REG))
  863.         return EPERM;
  864.  
  865.     return ipc_irq_register(&TASK->answerbox, inr, devno, method, ucode);
  866. }
  867.  
  868. /** Disconnect an IRQ handler from a task.
  869.  *
  870.  * @param inr       IRQ number.
  871.  * @param devno     Device number.
  872.  *
  873.  * @return      Zero on success or EPERM on error..
  874.  */
  875. unative_t sys_ipc_unregister_irq(inr_t inr, devno_t devno)
  876. {
  877.     if (!(cap_get(TASK) & CAP_IRQ_REG))
  878.         return EPERM;
  879.  
  880.     ipc_irq_unregister(&TASK->answerbox, inr, devno);
  881.  
  882.     return 0;
  883. }
  884.  
  885. #include <console/console.h>
  886.  
  887. /**
  888.  * Syscall connect to a task by id.
  889.  *
  890.  * @return      Phone id on success, or negative error code.
  891.  */
  892. unative_t sys_ipc_connect_kbox(sysarg64_t *uspace_taskid_arg)
  893. {
  894. #ifdef CONFIG_UDEBUG
  895.     sysarg64_t taskid_arg;
  896.     int rc;
  897.    
  898.     rc = copy_from_uspace(&taskid_arg, uspace_taskid_arg, sizeof(sysarg64_t));
  899.     if (rc != 0)
  900.         return (unative_t) rc;
  901.  
  902.     printf("sys_ipc_connect_kbox(%lld, %d)\n", taskid_arg.value);
  903.  
  904.     return ipc_connect_kbox(taskid_arg.value);
  905. #else
  906.     return (unative_t) ENOTSUP;
  907. #endif
  908. }
  909.  
  910. /** @}
  911.  */
  912.