Subversion Repositories HelenOS

Rev

Rev 2557 | Rev 2617 | 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 <arch/interrupt.h>
  46. #include <print.h>
  47. #include <syscall/copy.h>
  48. #include <security/cap.h>
  49. #include <mm/as.h>
  50. #include <print.h>
  51.  
  52. /** Maximum buffer size allowed for IPC_M_DATA_SEND requests. */
  53. #define DATA_SEND_LIMIT     (64 * 1024)
  54.  
  55. #define GET_CHECK_PHONE(phone, phoneid, err) \
  56. { \
  57.     if (phoneid > IPC_MAX_PHONES) { \
  58.         err; \
  59.     } \
  60.     phone = &TASK->phones[phoneid]; \
  61. }
  62.  
  63. #define STRUCT_TO_USPACE(dst, src)  copy_to_uspace(dst, src, sizeof(*(src)))
  64.  
  65. /** Decide if the method is a system method.
  66.  *
  67.  * @param method    Method to be decided.
  68.  *
  69.  * @return      Return 1 if the method is a system method.
  70.  *          Otherwise return 0.
  71.  */
  72. static inline int method_is_system(unative_t method)
  73. {
  74.     if (method <= IPC_M_LAST_SYSTEM)
  75.         return 1;
  76.     return 0;
  77. }
  78.  
  79. /** Decide if the message with this method is forwardable.
  80.  *
  81.  * - some system messages may be forwarded, for some of them
  82.  *   it is useless
  83.  *
  84.  * @param method    Method to be decided.
  85.  *
  86.  * @return      Return 1 if the method is forwardable.
  87.  *          Otherwise return 0.
  88.  */
  89. static inline int method_is_forwardable(unative_t method)
  90. {
  91.     switch (method) {
  92.     case IPC_M_PHONE_HUNGUP:
  93.         /* This message is meant only for the original recipient. */
  94.         return 0;
  95.     default:
  96.         return 1;
  97.     }
  98. }
  99.  
  100. /** Decide if the message with this method is immutable on forward.
  101.  *
  102.  * - some system messages may be forwarded but their content cannot be altered
  103.  *
  104.  * @param method    Method to be decided.
  105.  *
  106.  * @return      Return 1 if the method is immutable on forward.
  107.  *          Otherwise return 0.
  108.  */
  109. static inline int method_is_immutable(unative_t method)
  110. {
  111.     switch (method) {
  112.     case IPC_M_AS_AREA_SEND:
  113.     case IPC_M_AS_AREA_RECV:
  114.     case IPC_M_DATA_SEND:
  115.         return 1;
  116.         break;
  117.     default:
  118.         return 0;
  119.     }
  120. }
  121.  
  122.  
  123. /***********************************************************************
  124.  * Functions that preprocess answer before sending it to the recepient.
  125.  ***********************************************************************/
  126.  
  127. /** Decide if the caller (e.g. ipc_answer()) should save the old call contents
  128.  * for answer_preprocess().
  129.  *
  130.  * @param call      Call structure to be decided.
  131.  *
  132.  * @return      Return 1 if the old call contents should be saved.
  133.  *          Return 0 otherwise.
  134.  */
  135. static inline int answer_need_old(call_t *call)
  136. {
  137.     switch (IPC_GET_METHOD(call->data)) {
  138.     case IPC_M_CONNECT_TO_ME:
  139.     case IPC_M_CONNECT_ME_TO:
  140.     case IPC_M_AS_AREA_SEND:
  141.     case IPC_M_AS_AREA_RECV:
  142.     case IPC_M_DATA_SEND:
  143.         return 1;
  144.     default:
  145.         return 0;
  146.     }
  147. }
  148.  
  149. /** Interpret process answer as control information.
  150.  *
  151.  * This function is called directly after sys_ipc_answer().
  152.  *
  153.  * @param answer    Call structure with the answer.
  154.  * @param olddata   Saved data of the request.
  155.  *
  156.  * @return      Return 0 on success or an error code.
  157.  */
  158. static inline int answer_preprocess(call_t *answer, ipc_data_t *olddata)
  159. {
  160.     int phoneid;
  161.  
  162.     if (IPC_GET_RETVAL(answer->data) == EHANGUP) {
  163.         /* In case of forward, hangup the forwared phone,
  164.          * not the originator
  165.          */
  166.         spinlock_lock(&answer->data.phone->lock);
  167.         spinlock_lock(&TASK->answerbox.lock);
  168.         if (answer->data.phone->state == IPC_PHONE_CONNECTED) {
  169.             list_remove(&answer->data.phone->link);
  170.             answer->data.phone->state = IPC_PHONE_SLAMMED;
  171.         }
  172.         spinlock_unlock(&TASK->answerbox.lock);
  173.         spinlock_unlock(&answer->data.phone->lock);
  174.     }
  175.  
  176.     if (!olddata)
  177.         return 0;
  178.  
  179.     if (IPC_GET_METHOD(*olddata) == IPC_M_CONNECT_TO_ME) {
  180.         phoneid = IPC_GET_ARG3(*olddata);
  181.         if (IPC_GET_RETVAL(answer->data)) {
  182.             /* The connection was not accepted */
  183.             phone_dealloc(phoneid);
  184.         } else {
  185.             /* The connection was accepted */
  186.             phone_connect(phoneid, &answer->sender->answerbox);
  187.             /* Set 'phone hash' as arg3 of response */
  188.             IPC_SET_ARG3(answer->data,
  189.                 (unative_t) &TASK->phones[phoneid]);
  190.         }
  191.     } else if (IPC_GET_METHOD(*olddata) == IPC_M_CONNECT_ME_TO) {
  192.         /* If the users accepted call, connect */
  193.         if (!IPC_GET_RETVAL(answer->data)) {
  194.             ipc_phone_connect((phone_t *) IPC_GET_ARG3(*olddata),
  195.                 &TASK->answerbox);
  196.         }
  197.     } else if (IPC_GET_METHOD(*olddata) == IPC_M_AS_AREA_SEND) {
  198.         if (!IPC_GET_RETVAL(answer->data)) {
  199.             /* Accepted, handle as_area receipt */
  200.             ipl_t ipl;
  201.             int rc;
  202.             as_t *as;
  203.            
  204.             ipl = interrupts_disable();
  205.             spinlock_lock(&answer->sender->lock);
  206.             as = answer->sender->as;
  207.             spinlock_unlock(&answer->sender->lock);
  208.             interrupts_restore(ipl);
  209.            
  210.             rc = as_area_share(as, IPC_GET_ARG1(*olddata),
  211.                 IPC_GET_ARG2(*olddata), AS,
  212.                 IPC_GET_ARG1(answer->data), IPC_GET_ARG3(*olddata));
  213.             IPC_SET_RETVAL(answer->data, rc);
  214.             return rc;
  215.         }
  216.     } else if (IPC_GET_METHOD(*olddata) == IPC_M_AS_AREA_RECV) {
  217.         if (!IPC_GET_RETVAL(answer->data)) {
  218.             ipl_t ipl;
  219.             as_t *as;
  220.             int rc;
  221.            
  222.             ipl = interrupts_disable();
  223.             spinlock_lock(&answer->sender->lock);
  224.             as = answer->sender->as;
  225.             spinlock_unlock(&answer->sender->lock);
  226.             interrupts_restore(ipl);
  227.            
  228.             rc = as_area_share(AS, IPC_GET_ARG1(answer->data),
  229.                 IPC_GET_ARG2(*olddata), as, IPC_GET_ARG1(*olddata),
  230.                 IPC_GET_ARG2(answer->data));
  231.             IPC_SET_RETVAL(answer->data, rc);
  232.         }
  233.     } else if (IPC_GET_METHOD(*olddata) == IPC_M_DATA_SEND) {
  234.         if (!IPC_GET_RETVAL(answer->data)) {
  235.             int rc;
  236.             uintptr_t dst;
  237.             uintptr_t size;
  238.  
  239.             ASSERT(answer->buffer);
  240.  
  241.             dst = IPC_GET_ARG1(answer->data);
  242.             size = IPC_GET_ARG3(answer->data);
  243.  
  244.             rc = copy_to_uspace((void *) dst, answer->buffer, size);
  245.             if (rc != 0)
  246.                 IPC_SET_RETVAL(answer->data, rc);
  247.             free(answer->buffer);
  248.             answer->buffer = NULL;
  249.         }
  250.     }
  251.     return 0;
  252. }
  253.  
  254. /** Called before the request is sent.
  255.  *
  256.  * @param call      Call structure with the request.
  257.  *
  258.  * @return      Return 0 on success, ELIMIT or EPERM on error.
  259.  */
  260. static int request_preprocess(call_t *call)
  261. {
  262.     int newphid;
  263.     size_t size;
  264.     uintptr_t src;
  265.     int rc;
  266.  
  267.     switch (IPC_GET_METHOD(call->data)) {
  268.     case IPC_M_CONNECT_ME_TO:
  269.         newphid = phone_alloc();
  270.         if (newphid < 0)
  271.             return ELIMIT;
  272.         /* Set arg3 for server */
  273.         IPC_SET_ARG3(call->data, (unative_t) &TASK->phones[newphid]);
  274.         call->flags |= IPC_CALL_CONN_ME_TO;
  275.         call->priv = newphid;
  276.         break;
  277.     case IPC_M_AS_AREA_SEND:
  278.         size = as_area_get_size(IPC_GET_ARG1(call->data));
  279.         if (!size)
  280.             return EPERM;
  281.         IPC_SET_ARG2(call->data, size);
  282.         break;
  283.     case IPC_M_DATA_SEND:
  284.         src = IPC_GET_ARG2(call->data);
  285.         size = IPC_GET_ARG3(call->data);
  286.        
  287.         if ((size <= 0) || (size > DATA_SEND_LIMIT))
  288.             return ELIMIT;
  289.        
  290.         call->buffer = (uint8_t *) malloc(size, 0);
  291.         rc = copy_from_uspace(call->buffer, (void *) src, size);
  292.         if (rc != 0) {
  293.             free(call->buffer);
  294.             return rc;
  295.         }
  296.         break;
  297.     default:
  298.         break;
  299.     }
  300.     return 0;
  301. }
  302.  
  303. /*******************************************************************************
  304.  * Functions called to process received call/answer before passing it to uspace.
  305.  *******************************************************************************/
  306.  
  307. /** Do basic kernel processing of received call answer.
  308.  *
  309.  * @param call      Call structure with the answer.
  310.  */
  311. static void process_answer(call_t *call)
  312. {
  313.     if (IPC_GET_RETVAL(call->data) == EHANGUP &&
  314.         (call->flags & IPC_CALL_FORWARDED))
  315.         IPC_SET_RETVAL(call->data, EFORWARD);
  316.  
  317.     if (call->flags & IPC_CALL_CONN_ME_TO) {
  318.         if (IPC_GET_RETVAL(call->data))
  319.             phone_dealloc(call->priv);
  320.         else
  321.             IPC_SET_ARG3(call->data, call->priv);
  322.     }
  323. }
  324.  
  325. /** Do basic kernel processing of received call request.
  326.  *
  327.  * @param box       Destination answerbox structure.
  328.  * @param call      Call structure with the request.
  329.  *
  330.  * @return      Return 0 if the call should be passed to userspace.
  331.  *          Return -1 if the call should be ignored.
  332.  */
  333. static int process_request(answerbox_t *box, call_t *call)
  334. {
  335.     int phoneid;
  336.  
  337.     if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_TO_ME) {
  338.         phoneid = phone_alloc();
  339.         if (phoneid < 0) { /* Failed to allocate phone */
  340.             IPC_SET_RETVAL(call->data, ELIMIT);
  341.             ipc_answer(box, call);
  342.             return -1;
  343.         }
  344.         IPC_SET_ARG3(call->data, phoneid);
  345.     }
  346.     return 0;
  347. }
  348.  
  349. /** Make a fast call over IPC, wait for reply and return to user.
  350.  *
  351.  * This function can handle only one argument of payload, but is faster than
  352.  * the generic function (i.e. sys_ipc_call_sync()).
  353.  *
  354.  * @param phoneid   Phone handle for the call.
  355.  * @param method    Method of the call.
  356.  * @param arg1      Service-defined payload argument.
  357.  * @param data      Address of userspace structure where the reply call will
  358.  *          be stored.
  359.  *
  360.  * @return      Returns 0 on success.
  361.  *          Return ENOENT if there is no such phone handle.
  362.  */
  363. unative_t sys_ipc_call_sync_fast(unative_t phoneid, unative_t method,
  364.     unative_t arg1, ipc_data_t *data)
  365. {
  366.     call_t call;
  367.     phone_t *phone;
  368.     int res;
  369.  
  370.     GET_CHECK_PHONE(phone, phoneid, return ENOENT);
  371.  
  372.     ipc_call_static_init(&call);
  373.     IPC_SET_METHOD(call.data, method);
  374.     IPC_SET_ARG1(call.data, arg1);
  375.  
  376.     if (!(res = request_preprocess(&call))) {
  377.         ipc_call_sync(phone, &call);
  378.         process_answer(&call);
  379.     } else {
  380.         IPC_SET_RETVAL(call.data, res);
  381.     }
  382.     STRUCT_TO_USPACE(&data->args, &call.data.args);
  383.  
  384.     return 0;
  385. }
  386.  
  387. /** Make a synchronous IPC call allowing to transmit the entire payload.
  388.  *
  389.  * @param phoneid   Phone handle for the call.
  390.  * @param question  Userspace address of call data with the request.
  391.  * @param reply     Userspace address of call data where to store the
  392.  *          answer.
  393.  *
  394.  * @return      Zero on success or an error code.
  395.  */
  396. unative_t sys_ipc_call_sync(unative_t phoneid, ipc_data_t *question,
  397.     ipc_data_t *reply)
  398. {
  399.     call_t call;
  400.     phone_t *phone;
  401.     int res;
  402.     int rc;
  403.  
  404.     ipc_call_static_init(&call);
  405.     rc = copy_from_uspace(&call.data.args, &question->args,
  406.         sizeof(call.data.args));
  407.     if (rc != 0)
  408.         return (unative_t) rc;
  409.  
  410.     GET_CHECK_PHONE(phone, phoneid, return ENOENT);
  411.  
  412.     if (!(res = request_preprocess(&call))) {
  413.         ipc_call_sync(phone, &call);
  414.         process_answer(&call);
  415.     } else
  416.         IPC_SET_RETVAL(call.data, res);
  417.  
  418.     rc = STRUCT_TO_USPACE(&reply->args, &call.data.args);
  419.     if (rc != 0)
  420.         return rc;
  421.  
  422.     return 0;
  423. }
  424.  
  425. /** Check that the task did not exceed the allowed limit of asynchronous calls.
  426.  *
  427.  * @return      Return 0 if limit not reached or -1 if limit exceeded.
  428.  */
  429. static int check_call_limit(void)
  430. {
  431.     if (atomic_preinc(&TASK->active_calls) > IPC_MAX_ASYNC_CALLS) {
  432.         atomic_dec(&TASK->active_calls);
  433.         return -1;
  434.     }
  435.     return 0;
  436. }
  437.  
  438. /** Make a fast asynchronous call over IPC.
  439.  *
  440.  * This function can only handle two arguments of payload, but is faster than
  441.  * the generic function sys_ipc_call_async().
  442.  *
  443.  * @param phoneid   Phone handle for the call.
  444.  * @param method    Method of the call.
  445.  * @param arg1      Service-defined payload argument.
  446.  * @param arg2      Service-defined payload argument.
  447.  *
  448.  * @return      Return call hash on success.
  449.  *          Return IPC_CALLRET_FATAL in case of a fatal error and
  450.  *          IPC_CALLRET_TEMPORARY if there are too many pending
  451.  *          asynchronous requests; answers should be handled first.
  452.  */
  453. unative_t sys_ipc_call_async_fast(unative_t phoneid, unative_t method,
  454.     unative_t arg1, unative_t arg2)
  455. {
  456.     call_t *call;
  457.     phone_t *phone;
  458.     int res;
  459.  
  460.     if (check_call_limit())
  461.         return IPC_CALLRET_TEMPORARY;
  462.  
  463.     GET_CHECK_PHONE(phone, phoneid, return IPC_CALLRET_FATAL);
  464.  
  465.     call = ipc_call_alloc(0);
  466.     IPC_SET_METHOD(call->data, method);
  467.     IPC_SET_ARG1(call->data, arg1);
  468.     IPC_SET_ARG2(call->data, arg2);
  469.     IPC_SET_ARG3(call->data, 0);
  470.  
  471.     if (!(res = request_preprocess(call)))
  472.         ipc_call(phone, call);
  473.     else
  474.         ipc_backsend_err(phone, call, res);
  475.  
  476.     return (unative_t) call;
  477. }
  478.  
  479. /** Make an asynchronous IPC call allowing to transmit the entire payload.
  480.  *
  481.  * @param phoneid   Phone handle for the call.
  482.  * @param data      Userspace address of call data with the request.
  483.  *
  484.  * @return      See sys_ipc_call_async_fast().
  485.  */
  486. unative_t sys_ipc_call_async(unative_t phoneid, ipc_data_t *data)
  487. {
  488.     call_t *call;
  489.     phone_t *phone;
  490.     int res;
  491.     int rc;
  492.  
  493.     if (check_call_limit())
  494.         return IPC_CALLRET_TEMPORARY;
  495.  
  496.     GET_CHECK_PHONE(phone, phoneid, return IPC_CALLRET_FATAL);
  497.  
  498.     call = ipc_call_alloc(0);
  499.     rc = copy_from_uspace(&call->data.args, &data->args,
  500.         sizeof(call->data.args));
  501.     if (rc != 0) {
  502.         ipc_call_free(call);
  503.         return (unative_t) rc;
  504.     }
  505.     if (!(res = request_preprocess(call)))
  506.         ipc_call(phone, call);
  507.     else
  508.         ipc_backsend_err(phone, call, res);
  509.  
  510.     return (unative_t) call;
  511. }
  512.  
  513. /** Forward a received call to another destination.
  514.  *
  515.  * @param callid    Hash of the call to forward.
  516.  * @param phoneid   Phone handle to use for forwarding.
  517.  * @param method    New method to use for the forwarded call.
  518.  * @param arg1      New value of the first argument for the forwarded call.
  519.  *
  520.  * @return      Return 0 on succes, otherwise return an error code.
  521.  *
  522.  * In case the original method is a system method, ARG1 and ARG2 are overwritten
  523.  * in the forwarded message with the new method and the new arg1, respectively.
  524.  * Otherwise the METHOD and ARG1 are rewritten with the new method and arg1,
  525.  * respectively. Also note there is a set of immutable methods, for which the
  526.  * new method and argument is not set and these values are ignored.
  527.  *
  528.  * Warning: If implementing non-fast version, make sure that
  529.  *          ARG3 is not rewritten for certain system IPC
  530.  */
  531. unative_t sys_ipc_forward_fast(unative_t callid, unative_t phoneid,
  532.     unative_t method, unative_t arg1)
  533. {
  534.     call_t *call;
  535.     phone_t *phone;
  536.  
  537.     call = get_call(callid);
  538.     if (!call)
  539.         return ENOENT;
  540.  
  541.     call->flags |= IPC_CALL_FORWARDED;
  542.  
  543.     GET_CHECK_PHONE(phone, phoneid, {
  544.         IPC_SET_RETVAL(call->data, EFORWARD);
  545.         ipc_answer(&TASK->answerbox, call);
  546.         return ENOENT;
  547.     });    
  548.  
  549.     if (!method_is_forwardable(IPC_GET_METHOD(call->data))) {
  550.         IPC_SET_RETVAL(call->data, EFORWARD);
  551.         ipc_answer(&TASK->answerbox, call);
  552.         return EPERM;
  553.     }
  554.  
  555.     /*
  556.      * Userspace is not allowed to change method of system methods on
  557.      * forward, allow changing ARG1 and ARG2 by means of method and arg1.
  558.      * If the method is immutable, don't change anything.
  559.      */
  560.     if (!method_is_immutable(IPC_GET_METHOD(call->data))) {
  561.         if (method_is_system(IPC_GET_METHOD(call->data))) {
  562.             if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_TO_ME)
  563.                 phone_dealloc(IPC_GET_ARG3(call->data));
  564.  
  565.             IPC_SET_ARG1(call->data, method);
  566.             IPC_SET_ARG2(call->data, arg1);
  567.         } else {
  568.             IPC_SET_METHOD(call->data, method);
  569.             IPC_SET_ARG1(call->data, arg1);
  570.         }
  571.     }
  572.  
  573.     return ipc_forward(call, phone, &TASK->answerbox);
  574. }
  575.  
  576. /** Answer an IPC call - fast version.
  577.  *
  578.  * This function can handle only two return arguments of payload, but is faster
  579.  * than the generic sys_ipc_answer().
  580.  *
  581.  * @param callid    Hash of the call to be answered.
  582.  * @param retval    Return value of the answer.
  583.  * @param arg1      Service-defined return value.
  584.  * @param arg2      Service-defined return value.
  585.  *
  586.  * @return      Return 0 on success, otherwise return an error code.   
  587.  */
  588. unative_t sys_ipc_answer_fast(unative_t callid, unative_t retval,
  589.     unative_t arg1, unative_t arg2)
  590. {
  591.     call_t *call;
  592.     ipc_data_t saved_data;
  593.     int saveddata = 0;
  594.     int rc;
  595.  
  596.     /* Do not answer notification callids */
  597.     if (callid & IPC_CALLID_NOTIFICATION)
  598.         return 0;
  599.  
  600.     call = get_call(callid);
  601.     if (!call)
  602.         return ENOENT;
  603.  
  604.     if (answer_need_old(call)) {
  605.         memcpy(&saved_data, &call->data, sizeof(call->data));
  606.         saveddata = 1;
  607.     }
  608.  
  609.     IPC_SET_RETVAL(call->data, retval);
  610.     IPC_SET_ARG1(call->data, arg1);
  611.     IPC_SET_ARG2(call->data, arg2);
  612.     rc = answer_preprocess(call, saveddata ? &saved_data : NULL);
  613.  
  614.     ipc_answer(&TASK->answerbox, call);
  615.     return rc;
  616. }
  617.  
  618. /** Answer an IPC call.
  619.  *
  620.  * @param callid    Hash of the call to be answered.
  621.  * @param data      Userspace address of call data with the answer.
  622.  *
  623.  * @return      Return 0 on success, otherwise return an error code.
  624.  */
  625. unative_t sys_ipc_answer(unative_t callid, ipc_data_t *data)
  626. {
  627.     call_t *call;
  628.     ipc_data_t saved_data;
  629.     int saveddata = 0;
  630.     int rc;
  631.  
  632.     /* Do not answer notification callids */
  633.     if (callid & IPC_CALLID_NOTIFICATION)
  634.         return 0;
  635.  
  636.     call = get_call(callid);
  637.     if (!call)
  638.         return ENOENT;
  639.  
  640.     if (answer_need_old(call)) {
  641.         memcpy(&saved_data, &call->data, sizeof(call->data));
  642.         saveddata = 1;
  643.     }
  644.     rc = copy_from_uspace(&call->data.args, &data->args,
  645.         sizeof(call->data.args));
  646.     if (rc != 0)
  647.         return rc;
  648.  
  649.     rc = answer_preprocess(call, saveddata ? &saved_data : NULL);
  650.    
  651.     ipc_answer(&TASK->answerbox, call);
  652.  
  653.     return rc;
  654. }
  655.  
  656. /** Hang up a phone.
  657.  *
  658.  * @param       Phone handle of the phone to be hung up.
  659.  *
  660.  * @return      Return 0 on success or an error code.
  661.  */
  662. unative_t sys_ipc_hangup(int phoneid)
  663. {
  664.     phone_t *phone;
  665.  
  666.     GET_CHECK_PHONE(phone, phoneid, return ENOENT);
  667.  
  668.     if (ipc_phone_hangup(phone))
  669.         return -1;
  670.  
  671.     return 0;
  672. }
  673.  
  674. /** Wait for an incoming IPC call or an answer.
  675.  *
  676.  * @param calldata  Pointer to buffer where the call/answer data is stored.
  677.  * @param usec      Timeout. See waitq_sleep_timeout() for explanation.
  678.  * @param flags     Select mode of sleep operation. See waitq_sleep_timeout()
  679.  *          for explanation.
  680.  *
  681.  * @return      Hash of the call.
  682.  *          If IPC_CALLID_NOTIFICATION bit is set in the hash, the
  683.  *          call is a notification. IPC_CALLID_ANSWERED denotes an
  684.  *          answer.
  685.  */
  686. unative_t sys_ipc_wait_for_call(ipc_data_t *calldata, uint32_t usec, int flags)
  687. {
  688.     call_t *call;
  689.  
  690. restart:   
  691.     call = ipc_wait_for_call(&TASK->answerbox, usec,
  692.         flags | SYNCH_FLAGS_INTERRUPTIBLE);
  693.     if (!call)
  694.         return 0;
  695.  
  696.     if (call->flags & IPC_CALL_NOTIF) {
  697.         ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
  698.  
  699.         /* Set in_phone_hash to the interrupt counter */
  700.         call->data.phone = (void *) call->priv;
  701.        
  702.         STRUCT_TO_USPACE(calldata, &call->data);
  703.  
  704.         ipc_call_free(call);
  705.        
  706.         return ((unative_t) call) | IPC_CALLID_NOTIFICATION;
  707.     }
  708.  
  709.     if (call->flags & IPC_CALL_ANSWERED) {
  710.         process_answer(call);
  711.  
  712.         ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
  713.  
  714.         atomic_dec(&TASK->active_calls);
  715.  
  716.         if (call->flags & IPC_CALL_DISCARD_ANSWER) {
  717.             ipc_call_free(call);
  718.             goto restart;
  719.         }
  720.  
  721.         STRUCT_TO_USPACE(&calldata->args, &call->data.args);
  722.         ipc_call_free(call);
  723.  
  724.         return ((unative_t) call) | IPC_CALLID_ANSWERED;
  725.     }
  726.  
  727.     if (process_request(&TASK->answerbox, call))
  728.         goto restart;
  729.  
  730.     /* Include phone address('id') of the caller in the request,
  731.      * copy whole call->data, not only call->data.args */
  732.     if (STRUCT_TO_USPACE(calldata, &call->data)) {
  733.         return 0;
  734.     }
  735.     return (unative_t)call;
  736. }
  737.  
  738. /** Connect an IRQ handler to a task.
  739.  *
  740.  * @param inr       IRQ number.
  741.  * @param devno     Device number.
  742.  * @param method    Method to be associated with the notification.
  743.  * @param ucode     Uspace pointer to the top-half pseudocode.
  744.  *
  745.  * @return      EPERM or a return code returned by ipc_irq_register().
  746.  */
  747. unative_t sys_ipc_register_irq(inr_t inr, devno_t devno, unative_t method,
  748.     irq_code_t *ucode)
  749. {
  750.     if (!(cap_get(TASK) & CAP_IRQ_REG))
  751.         return EPERM;
  752.  
  753.     return ipc_irq_register(&TASK->answerbox, inr, devno, method, ucode);
  754. }
  755.  
  756. /** Disconnect an IRQ handler from a task.
  757.  *
  758.  * @param inr       IRQ number.
  759.  * @param devno     Device number.
  760.  *
  761.  * @return      Zero on success or EPERM on error..
  762.  */
  763. unative_t sys_ipc_unregister_irq(inr_t inr, devno_t devno)
  764. {
  765.     if (!(cap_get(TASK) & CAP_IRQ_REG))
  766.         return EPERM;
  767.  
  768.     ipc_irq_unregister(&TASK->answerbox, inr, devno);
  769.  
  770.     return 0;
  771. }
  772.  
  773. /** @}
  774.  */
  775.