Subversion Repositories HelenOS

Rev

Rev 4420 | 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/kbox.h>
  46. #include <udebug/udebug_ipc.h>
  47. #include <arch/interrupt.h>
  48. #include <syscall/copy.h>
  49. #include <security/cap.h>
  50. #include <mm/as.h>
  51. #include <print.h>
  52.  
  53. /**
  54.  * Maximum buffer size allowed for IPC_M_DATA_WRITE and IPC_M_DATA_READ
  55.  * requests.
  56.  */
  57. #define DATA_XFER_LIMIT     (64 * 1024)
  58.  
  59. #define GET_CHECK_PHONE(phone, phoneid, err) \
  60. { \
  61.     if (phoneid > IPC_MAX_PHONES) { \
  62.         err; \
  63.     } \
  64.     phone = &TASK->phones[phoneid]; \
  65. }
  66.  
  67. #define STRUCT_TO_USPACE(dst, src)  copy_to_uspace(dst, src, sizeof(*(src)))
  68.  
  69. /** Decide if the method is a system method.
  70.  *
  71.  * @param method    Method to be decided.
  72.  *
  73.  * @return      Return 1 if the method is a system method.
  74.  *          Otherwise return 0.
  75.  */
  76. static inline int method_is_system(unative_t method)
  77. {
  78.     if (method <= IPC_M_LAST_SYSTEM)
  79.         return 1;
  80.     return 0;
  81. }
  82.  
  83. /** Decide if the message with this method is forwardable.
  84.  *
  85.  * - some system messages may be forwarded, for some of them
  86.  *   it is useless
  87.  *
  88.  * @param method    Method to be decided.
  89.  *
  90.  * @return      Return 1 if the method is forwardable.
  91.  *          Otherwise return 0.
  92.  */
  93. static inline int method_is_forwardable(unative_t method)
  94. {
  95.     switch (method) {
  96.     case IPC_M_CONNECTION_CLONE:
  97.     case IPC_M_CONNECT_ME:
  98.     case IPC_M_PHONE_HUNGUP:
  99.         /* This message is meant only for the original recipient. */
  100.         return 0;
  101.     default:
  102.         return 1;
  103.     }
  104. }
  105.  
  106. /** Decide if the message with this method is immutable on forward.
  107.  *
  108.  * - some system messages may be forwarded but their content cannot be altered
  109.  *
  110.  * @param method    Method to be decided.
  111.  *
  112.  * @return      Return 1 if the method is immutable on forward.
  113.  *          Otherwise return 0.
  114.  */
  115. static inline int method_is_immutable(unative_t method)
  116. {
  117.     switch (method) {
  118.     case IPC_M_SHARE_OUT:
  119.     case IPC_M_SHARE_IN:
  120.     case IPC_M_DATA_WRITE:
  121.     case IPC_M_DATA_READ:
  122.         return 1;
  123.         break;
  124.     default:
  125.         return 0;
  126.     }
  127. }
  128.  
  129.  
  130. /***********************************************************************
  131.  * Functions that preprocess answer before sending it to the recepient.
  132.  ***********************************************************************/
  133.  
  134. /** Decide if the caller (e.g. ipc_answer()) should save the old call contents
  135.  * for answer_preprocess().
  136.  *
  137.  * @param call      Call structure to be decided.
  138.  *
  139.  * @return      Return 1 if the old call contents should be saved.
  140.  *          Return 0 otherwise.
  141.  */
  142. static inline int answer_need_old(call_t *call)
  143. {
  144.     switch (IPC_GET_METHOD(call->data)) {
  145.     case IPC_M_CONNECTION_CLONE:
  146.     case IPC_M_CONNECT_ME:
  147.     case IPC_M_CONNECT_TO_ME:
  148.     case IPC_M_CONNECT_ME_TO:
  149.     case IPC_M_SHARE_OUT:
  150.     case IPC_M_SHARE_IN:
  151.     case IPC_M_DATA_WRITE:
  152.     case IPC_M_DATA_READ:
  153.         return 1;
  154.     default:
  155.         return 0;
  156.     }
  157. }
  158.  
  159. /** Interpret process answer as control information.
  160.  *
  161.  * This function is called directly after sys_ipc_answer().
  162.  *
  163.  * @param answer    Call structure with the answer.
  164.  * @param olddata   Saved data of the request.
  165.  *
  166.  * @return      Return 0 on success or an error code.
  167.  */
  168. static inline int answer_preprocess(call_t *answer, ipc_data_t *olddata)
  169. {
  170.     int phoneid;
  171.  
  172.     if ((native_t) IPC_GET_RETVAL(answer->data) == EHANGUP) {
  173.         /* In case of forward, hangup the forwared phone,
  174.          * not the originator
  175.          */
  176.         mutex_lock(&answer->data.phone->lock);
  177.         spinlock_lock(&TASK->answerbox.lock);
  178.         if (answer->data.phone->state == IPC_PHONE_CONNECTED) {
  179.             list_remove(&answer->data.phone->link);
  180.             answer->data.phone->state = IPC_PHONE_SLAMMED;
  181.         }
  182.         spinlock_unlock(&TASK->answerbox.lock);
  183.         mutex_unlock(&answer->data.phone->lock);
  184.     }
  185.  
  186.     if (!olddata)
  187.         return 0;
  188.  
  189.     if (IPC_GET_METHOD(*olddata) == IPC_M_CONNECTION_CLONE) {
  190.         phoneid = IPC_GET_ARG1(*olddata);
  191.         phone_t *phone = &TASK->phones[phoneid];
  192.         if (IPC_GET_RETVAL(answer->data) != EOK) {
  193.             /*
  194.              * The recipient of the cloned phone rejected the offer.
  195.              * In this case, the connection was established at the
  196.              * request time and therefore we need to slam the phone.
  197.              * We don't merely hangup as that would result in
  198.              * sending IPC_M_HUNGUP to the third party on the
  199.              * other side of the cloned phone.
  200.              */
  201.             mutex_lock(&phone->lock);
  202.             if (phone->state == IPC_PHONE_CONNECTED) {
  203.                 spinlock_lock(&phone->callee->lock);
  204.                 list_remove(&phone->link);
  205.                 phone->state = IPC_PHONE_SLAMMED;
  206.                 spinlock_unlock(&phone->callee->lock);
  207.             }
  208.             mutex_unlock(&phone->lock);
  209.         }
  210.     } else if (IPC_GET_METHOD(*olddata) == IPC_M_CONNECT_ME) {
  211.         phone_t *phone = (phone_t *)IPC_GET_ARG5(*olddata);
  212.         if (IPC_GET_RETVAL(answer->data) != EOK) {
  213.             /*
  214.              * The other party on the cloned phoned rejected our
  215.              * request for connection on the protocol level.
  216.              * We need to break the connection without sending
  217.              * IPC_M_HUNGUP back.
  218.              */
  219.             mutex_lock(&phone->lock);
  220.             if (phone->state == IPC_PHONE_CONNECTED) {
  221.                 spinlock_lock(&phone->callee->lock);
  222.                 list_remove(&phone->link);
  223.                 phone->state = IPC_PHONE_SLAMMED;
  224.                 spinlock_unlock(&phone->callee->lock);
  225.             }
  226.             mutex_unlock(&phone->lock);
  227.         }
  228.     } else if (IPC_GET_METHOD(*olddata) == IPC_M_CONNECT_TO_ME) {
  229.         phoneid = IPC_GET_ARG5(*olddata);
  230.         if (IPC_GET_RETVAL(answer->data) != EOK) {
  231.             /* The connection was not accepted */
  232.             phone_dealloc(phoneid);
  233.         } else {
  234.             /* The connection was accepted */
  235.             phone_connect(phoneid, &answer->sender->answerbox);
  236.             /* Set 'phone hash' as arg5 of response */
  237.             IPC_SET_ARG5(answer->data,
  238.                 (unative_t) &TASK->phones[phoneid]);
  239.         }
  240.     } else if (IPC_GET_METHOD(*olddata) == IPC_M_CONNECT_ME_TO) {
  241.         /* If the users accepted call, connect */
  242.         if (IPC_GET_RETVAL(answer->data) == EOK) {
  243.             ipc_phone_connect((phone_t *) IPC_GET_ARG5(*olddata),
  244.                 &TASK->answerbox);
  245.         }
  246.     } else if (IPC_GET_METHOD(*olddata) == IPC_M_SHARE_OUT) {
  247.         if (!IPC_GET_RETVAL(answer->data)) {
  248.             /* Accepted, handle as_area receipt */
  249.             ipl_t ipl;
  250.             int rc;
  251.             as_t *as;
  252.            
  253.             ipl = interrupts_disable();
  254.             spinlock_lock(&answer->sender->lock);
  255.             as = answer->sender->as;
  256.             spinlock_unlock(&answer->sender->lock);
  257.             interrupts_restore(ipl);
  258.            
  259.             rc = as_area_share(as, IPC_GET_ARG1(*olddata),
  260.                 IPC_GET_ARG2(*olddata), AS,
  261.                 IPC_GET_ARG1(answer->data), IPC_GET_ARG3(*olddata));
  262.             IPC_SET_RETVAL(answer->data, rc);
  263.             return rc;
  264.         }
  265.     } else if (IPC_GET_METHOD(*olddata) == IPC_M_SHARE_IN) {
  266.         if (!IPC_GET_RETVAL(answer->data)) {
  267.             ipl_t ipl;
  268.             as_t *as;
  269.             int rc;
  270.            
  271.             ipl = interrupts_disable();
  272.             spinlock_lock(&answer->sender->lock);
  273.             as = answer->sender->as;
  274.             spinlock_unlock(&answer->sender->lock);
  275.             interrupts_restore(ipl);
  276.            
  277.             rc = as_area_share(AS, IPC_GET_ARG1(answer->data),
  278.                 IPC_GET_ARG2(*olddata), as, IPC_GET_ARG1(*olddata),
  279.                 IPC_GET_ARG2(answer->data));
  280.             IPC_SET_RETVAL(answer->data, rc);
  281.         }
  282.     } else if (IPC_GET_METHOD(*olddata) == IPC_M_DATA_READ) {
  283.         ASSERT(!answer->buffer);
  284.         if (!IPC_GET_RETVAL(answer->data)) {
  285.             /* The recipient agreed to send data. */
  286.             uintptr_t src = IPC_GET_ARG1(answer->data);
  287.             uintptr_t dst = IPC_GET_ARG1(*olddata);
  288.             size_t max_size = IPC_GET_ARG2(*olddata);
  289.             size_t size = IPC_GET_ARG2(answer->data);
  290.             if (size && size <= max_size) {
  291.                 /*
  292.                  * Copy the destination VA so that this piece of
  293.                  * information is not lost.
  294.                  */
  295.                 IPC_SET_ARG1(answer->data, dst);
  296.  
  297.                 answer->buffer = malloc(size, 0);
  298.                 int rc = copy_from_uspace(answer->buffer,
  299.                     (void *) src, size);
  300.                 if (rc) {
  301.                     IPC_SET_RETVAL(answer->data, rc);
  302.                     free(answer->buffer);
  303.                     answer->buffer = NULL;
  304.                 }
  305.             } else if (!size) {
  306.                 IPC_SET_RETVAL(answer->data, EOK);
  307.             } else {
  308.                 IPC_SET_RETVAL(answer->data, ELIMIT);
  309.             }
  310.         }
  311.     } else if (IPC_GET_METHOD(*olddata) == IPC_M_DATA_WRITE) {
  312.         ASSERT(answer->buffer);
  313.         if (!IPC_GET_RETVAL(answer->data)) {
  314.             /* The recipient agreed to receive data. */
  315.             int rc;
  316.             uintptr_t dst;
  317.             size_t size;
  318.             size_t max_size;
  319.  
  320.             dst = (uintptr_t)IPC_GET_ARG1(answer->data);
  321.             size = (size_t)IPC_GET_ARG2(answer->data);
  322.             max_size = (size_t)IPC_GET_ARG2(*olddata);
  323.  
  324.             if (size <= max_size) {
  325.                 rc = copy_to_uspace((void *) dst,
  326.                     answer->buffer, size);
  327.                 if (rc)
  328.                     IPC_SET_RETVAL(answer->data, rc);
  329.             } else {
  330.                 IPC_SET_RETVAL(answer->data, ELIMIT);
  331.             }
  332.         }
  333.         free(answer->buffer);
  334.         answer->buffer = NULL;
  335.     }
  336.     return 0;
  337. }
  338.  
  339. /** Called before the request is sent.
  340.  *
  341.  * @param call      Call structure with the request.
  342.  * @param phone     Phone that the call will be sent through.
  343.  *
  344.  * @return      Return 0 on success, ELIMIT or EPERM on error.
  345.  */
  346. static int request_preprocess(call_t *call, phone_t *phone)
  347. {
  348.     int newphid;
  349.     size_t size;
  350.     uintptr_t src;
  351.     int rc;
  352.  
  353.     switch (IPC_GET_METHOD(call->data)) {
  354.     case IPC_M_CONNECTION_CLONE: {
  355.         phone_t *cloned_phone;
  356.         GET_CHECK_PHONE(cloned_phone, IPC_GET_ARG1(call->data),
  357.             return ENOENT);
  358.         if (cloned_phone < phone) {
  359.             mutex_lock(&cloned_phone->lock);
  360.             mutex_lock(&phone->lock);
  361.         } else if (cloned_phone > phone) {
  362.             mutex_lock(&phone->lock);
  363.             mutex_lock(&cloned_phone->lock);
  364.         } else {
  365.             mutex_lock(&phone->lock);
  366.         }
  367.         if ((cloned_phone->state != IPC_PHONE_CONNECTED) ||
  368.             phone->state != IPC_PHONE_CONNECTED) {
  369.             if (cloned_phone != phone)
  370.                 mutex_unlock(&cloned_phone->lock);
  371.             mutex_unlock(&phone->lock);
  372.             return EINVAL;
  373.         }
  374.         /*
  375.          * We can be pretty sure now that both tasks exist and we are
  376.          * connected to them. As we continue to hold the phone locks,
  377.          * we are effectively preventing them from finishing their
  378.          * potential cleanup.
  379.          */
  380.         newphid = phone_alloc(phone->callee->task);
  381.         if (newphid < 0) {
  382.             if (cloned_phone != phone)
  383.                 mutex_unlock(&cloned_phone->lock);
  384.             mutex_unlock(&phone->lock);
  385.             return ELIMIT;
  386.         }
  387.         ipc_phone_connect(&phone->callee->task->phones[newphid],
  388.             cloned_phone->callee);
  389.         if (cloned_phone != phone)
  390.             mutex_unlock(&cloned_phone->lock);
  391.         mutex_unlock(&phone->lock);
  392.         /* Set the new phone for the callee. */
  393.         IPC_SET_ARG1(call->data, newphid);
  394.         break;
  395.     }
  396.     case IPC_M_CONNECT_ME:
  397.         IPC_SET_ARG5(call->data, (unative_t) phone);
  398.         break;
  399.     case IPC_M_CONNECT_ME_TO:
  400.         newphid = phone_alloc(TASK);
  401.         if (newphid < 0)
  402.             return ELIMIT;
  403.         /* Set arg5 for server */
  404.         IPC_SET_ARG5(call->data, (unative_t) &TASK->phones[newphid]);
  405.         call->flags |= IPC_CALL_CONN_ME_TO;
  406.         call->priv = newphid;
  407.         break;
  408.     case IPC_M_SHARE_OUT:
  409.         size = as_area_get_size(IPC_GET_ARG1(call->data));
  410.         if (!size)
  411.             return EPERM;
  412.         IPC_SET_ARG2(call->data, size);
  413.         break;
  414.     case IPC_M_DATA_READ:
  415.         size = IPC_GET_ARG2(call->data);
  416.         if ((size <= 0 || (size > DATA_XFER_LIMIT)))
  417.             return ELIMIT;
  418.         break;
  419.     case IPC_M_DATA_WRITE:
  420.         src = IPC_GET_ARG1(call->data);
  421.         size = IPC_GET_ARG2(call->data);
  422.        
  423.         if (size > DATA_XFER_LIMIT)
  424.             return ELIMIT;
  425.        
  426.         call->buffer = (uint8_t *) malloc(size, 0);
  427.         rc = copy_from_uspace(call->buffer, (void *) src, size);
  428.         if (rc != 0) {
  429.             free(call->buffer);
  430.             return rc;
  431.         }
  432.         break;
  433. #ifdef CONFIG_UDEBUG
  434.     case IPC_M_DEBUG_ALL:
  435.         return udebug_request_preprocess(call, phone);
  436. #endif
  437.     default:
  438.         break;
  439.     }
  440.     return 0;
  441. }
  442.  
  443. /*******************************************************************************
  444.  * Functions called to process received call/answer before passing it to uspace.
  445.  *******************************************************************************/
  446.  
  447. /** Do basic kernel processing of received call answer.
  448.  *
  449.  * @param call      Call structure with the answer.
  450.  */
  451. static void process_answer(call_t *call)
  452. {
  453.     if (((native_t) IPC_GET_RETVAL(call->data) == EHANGUP) &&
  454.         (call->flags & IPC_CALL_FORWARDED))
  455.         IPC_SET_RETVAL(call->data, EFORWARD);
  456.  
  457.     if (call->flags & IPC_CALL_CONN_ME_TO) {
  458.         if (IPC_GET_RETVAL(call->data))
  459.             phone_dealloc(call->priv);
  460.         else
  461.             IPC_SET_ARG5(call->data, call->priv);
  462.     }
  463.  
  464.     if (call->buffer) {
  465.         /* This must be an affirmative answer to IPC_M_DATA_READ. */
  466.         /* or IPC_M_DEBUG_ALL/UDEBUG_M_MEM_READ... */
  467.         uintptr_t dst = IPC_GET_ARG1(call->data);
  468.         size_t size = IPC_GET_ARG2(call->data);
  469.         int rc = copy_to_uspace((void *) dst, call->buffer, size);
  470.         if (rc)
  471.             IPC_SET_RETVAL(call->data, rc);
  472.         free(call->buffer);
  473.         call->buffer = NULL;
  474.     }
  475. }
  476.  
  477. /** Do basic kernel processing of received call request.
  478.  *
  479.  * @param box       Destination answerbox structure.
  480.  * @param call      Call structure with the request.
  481.  *
  482.  * @return      Return 0 if the call should be passed to userspace.
  483.  *          Return -1 if the call should be ignored.
  484.  */
  485. static int process_request(answerbox_t *box, call_t *call)
  486. {
  487.     int phoneid;
  488.  
  489.     if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_TO_ME) {
  490.         phoneid = phone_alloc(TASK);
  491.         if (phoneid < 0) { /* Failed to allocate phone */
  492.             IPC_SET_RETVAL(call->data, ELIMIT);
  493.             ipc_answer(box, call);
  494.             return -1;
  495.         }
  496.         IPC_SET_ARG5(call->data, phoneid);
  497.     }
  498.     switch (IPC_GET_METHOD(call->data)) {
  499.     case IPC_M_DEBUG_ALL:
  500.         return -1;
  501.     default:
  502.         break;
  503.     }
  504.     return 0;
  505. }
  506.  
  507. /** Make a fast call over IPC, wait for reply and return to user.
  508.  *
  509.  * This function can handle only three arguments of payload, but is faster than
  510.  * the generic function (i.e. sys_ipc_call_sync_slow()).
  511.  *
  512.  * @param phoneid   Phone handle for the call.
  513.  * @param method    Method of the call.
  514.  * @param arg1      Service-defined payload argument.
  515.  * @param arg2      Service-defined payload argument.
  516.  * @param arg3      Service-defined payload argument.
  517.  * @param data      Address of userspace structure where the reply call will
  518.  *          be stored.
  519.  *
  520.  * @return      Returns 0 on success.
  521.  *          Return ENOENT if there is no such phone handle.
  522.  */
  523. unative_t sys_ipc_call_sync_fast(unative_t phoneid, unative_t method,
  524.     unative_t arg1, unative_t arg2, unative_t arg3, ipc_data_t *data)
  525. {
  526.     call_t call;
  527.     phone_t *phone;
  528.     int res;
  529.     int rc;
  530.    
  531.     GET_CHECK_PHONE(phone, phoneid, return ENOENT);
  532.  
  533.     ipc_call_static_init(&call);
  534.     IPC_SET_METHOD(call.data, method);
  535.     IPC_SET_ARG1(call.data, arg1);
  536.     IPC_SET_ARG2(call.data, arg2);
  537.     IPC_SET_ARG3(call.data, arg3);
  538.     /*
  539.      * To achieve deterministic behavior, zero out arguments that are beyond
  540.      * the limits of the fast version.
  541.      */
  542.     IPC_SET_ARG4(call.data, 0);
  543.     IPC_SET_ARG5(call.data, 0);
  544.  
  545.     if (!(res = request_preprocess(&call, phone))) {
  546. #ifdef CONFIG_UDEBUG
  547.         udebug_stoppable_begin();
  548. #endif
  549.         rc = ipc_call_sync(phone, &call);
  550. #ifdef CONFIG_UDEBUG
  551.         udebug_stoppable_end();
  552. #endif
  553.         if (rc != EOK)
  554.             return rc;
  555.         process_answer(&call);
  556.  
  557.     } else {
  558.         IPC_SET_RETVAL(call.data, res);
  559.     }
  560.     rc = STRUCT_TO_USPACE(&data->args, &call.data.args);
  561.     if (rc != 0)
  562.         return rc;
  563.  
  564.     return 0;
  565. }
  566.  
  567. /** Make a synchronous IPC call allowing to transmit the entire payload.
  568.  *
  569.  * @param phoneid   Phone handle for the call.
  570.  * @param question  Userspace address of call data with the request.
  571.  * @param reply     Userspace address of call data where to store the
  572.  *          answer.
  573.  *
  574.  * @return      Zero on success or an error code.
  575.  */
  576. unative_t sys_ipc_call_sync_slow(unative_t phoneid, ipc_data_t *question,
  577.     ipc_data_t *reply)
  578. {
  579.     call_t call;
  580.     phone_t *phone;
  581.     int res;
  582.     int rc;
  583.  
  584.     ipc_call_static_init(&call);
  585.     rc = copy_from_uspace(&call.data.args, &question->args,
  586.         sizeof(call.data.args));
  587.     if (rc != 0)
  588.         return (unative_t) rc;
  589.  
  590.     GET_CHECK_PHONE(phone, phoneid, return ENOENT);
  591.  
  592.     if (!(res = request_preprocess(&call, phone))) {
  593. #ifdef CONFIG_UDEBUG
  594.         udebug_stoppable_begin();
  595. #endif
  596.         rc = ipc_call_sync(phone, &call);
  597. #ifdef CONFIG_UDEBUG
  598.         udebug_stoppable_end();
  599. #endif
  600.         if (rc != EOK)
  601.             return rc;
  602.         process_answer(&call);
  603.     } else
  604.         IPC_SET_RETVAL(call.data, res);
  605.  
  606.     rc = STRUCT_TO_USPACE(&reply->args, &call.data.args);
  607.     if (rc != 0)
  608.         return rc;
  609.  
  610.     return 0;
  611. }
  612.  
  613. /** Check that the task did not exceed the allowed limit of asynchronous calls.
  614.  *
  615.  * @return      Return 0 if limit not reached or -1 if limit exceeded.
  616.  */
  617. static int check_call_limit(void)
  618. {
  619.     if (atomic_preinc(&TASK->active_calls) > IPC_MAX_ASYNC_CALLS) {
  620.         atomic_dec(&TASK->active_calls);
  621.         return -1;
  622.     }
  623.     return 0;
  624. }
  625.  
  626. /** Make a fast asynchronous call over IPC.
  627.  *
  628.  * This function can only handle four arguments of payload, but is faster than
  629.  * the generic function sys_ipc_call_async_slow().
  630.  *
  631.  * @param phoneid   Phone handle for the call.
  632.  * @param method    Method of the call.
  633.  * @param arg1      Service-defined payload argument.
  634.  * @param arg2      Service-defined payload argument.
  635.  * @param arg3      Service-defined payload argument.
  636.  * @param arg4      Service-defined payload argument.
  637.  *
  638.  * @return      Return call hash on success.
  639.  *          Return IPC_CALLRET_FATAL in case of a fatal error and
  640.  *          IPC_CALLRET_TEMPORARY if there are too many pending
  641.  *          asynchronous requests; answers should be handled first.
  642.  */
  643. unative_t sys_ipc_call_async_fast(unative_t phoneid, unative_t method,
  644.     unative_t arg1, unative_t arg2, unative_t arg3, unative_t arg4)
  645. {
  646.     call_t *call;
  647.     phone_t *phone;
  648.     int res;
  649.  
  650.     if (check_call_limit())
  651.         return IPC_CALLRET_TEMPORARY;
  652.  
  653.     GET_CHECK_PHONE(phone, phoneid, return IPC_CALLRET_FATAL);
  654.  
  655.     call = ipc_call_alloc(0);
  656.     IPC_SET_METHOD(call->data, method);
  657.     IPC_SET_ARG1(call->data, arg1);
  658.     IPC_SET_ARG2(call->data, arg2);
  659.     IPC_SET_ARG3(call->data, arg3);
  660.     IPC_SET_ARG4(call->data, arg4);
  661.     /*
  662.      * To achieve deterministic behavior, zero out arguments that are beyond
  663.      * the limits of the fast version.
  664.      */
  665.     IPC_SET_ARG5(call->data, 0);
  666.  
  667.     if (!(res = request_preprocess(call, phone)))
  668.         ipc_call(phone, call);
  669.     else
  670.         ipc_backsend_err(phone, call, res);
  671.  
  672.     return (unative_t) call;
  673. }
  674.  
  675. /** Make an asynchronous IPC call allowing to transmit the entire payload.
  676.  *
  677.  * @param phoneid   Phone handle for the call.
  678.  * @param data      Userspace address of call data with the request.
  679.  *
  680.  * @return      See sys_ipc_call_async_fast().
  681.  */
  682. unative_t sys_ipc_call_async_slow(unative_t phoneid, ipc_data_t *data)
  683. {
  684.     call_t *call;
  685.     phone_t *phone;
  686.     int res;
  687.     int rc;
  688.  
  689.     if (check_call_limit())
  690.         return IPC_CALLRET_TEMPORARY;
  691.  
  692.     GET_CHECK_PHONE(phone, phoneid, return IPC_CALLRET_FATAL);
  693.  
  694.     call = ipc_call_alloc(0);
  695.     rc = copy_from_uspace(&call->data.args, &data->args,
  696.         sizeof(call->data.args));
  697.     if (rc != 0) {
  698.         ipc_call_free(call);
  699.         return (unative_t) rc;
  700.     }
  701.     if (!(res = request_preprocess(call, phone)))
  702.         ipc_call(phone, call);
  703.     else
  704.         ipc_backsend_err(phone, call, res);
  705.  
  706.     return (unative_t) call;
  707. }
  708.  
  709. /** Forward a received call to another destination - common code for both the
  710.  * fast and the slow version.
  711.  *
  712.  * @param callid    Hash of the call to forward.
  713.  * @param phoneid   Phone handle to use for forwarding.
  714.  * @param method    New method to use for the forwarded call.
  715.  * @param arg1      New value of the first argument for the forwarded call.
  716.  * @param arg2      New value of the second argument for the forwarded call.
  717.  * @param arg3      New value of the third argument for the forwarded call.
  718.  * @param arg4      New value of the fourth argument for the forwarded call.
  719.  * @param arg5      New value of the fifth argument for the forwarded call.
  720.  * @param mode      Flags that specify mode of the forward operation.
  721.  * @param slow      If true, arg3, arg4 and arg5 are considered. Otherwise
  722.  *          the function considers only the fast version arguments:
  723.  *          i.e. arg1 and arg2.
  724.  *
  725.  * @return      Return 0 on succes, otherwise return an error code.
  726.  *
  727.  * Warning: Make sure that ARG5 is not rewritten for certain system IPC
  728.  */
  729. static unative_t sys_ipc_forward_common(unative_t callid, unative_t phoneid,
  730.     unative_t method, unative_t arg1, unative_t arg2, unative_t arg3,
  731.     unative_t arg4, unative_t arg5, int mode, bool slow)
  732. {
  733.     call_t *call;
  734.     phone_t *phone;
  735.  
  736.     call = get_call(callid);
  737.     if (!call)
  738.         return ENOENT;
  739.    
  740.     call->flags |= IPC_CALL_FORWARDED;
  741.  
  742.     GET_CHECK_PHONE(phone, phoneid, {
  743.         IPC_SET_RETVAL(call->data, EFORWARD);
  744.         ipc_answer(&TASK->answerbox, call);
  745.         return ENOENT;
  746.     });
  747.  
  748.     if (!method_is_forwardable(IPC_GET_METHOD(call->data))) {
  749.         IPC_SET_RETVAL(call->data, EFORWARD);
  750.         ipc_answer(&TASK->answerbox, call);
  751.         return EPERM;
  752.     }
  753.  
  754.     /*
  755.      * Userspace is not allowed to change method of system methods on
  756.      * forward, allow changing ARG1, ARG2, ARG3 and ARG4 by means of method,
  757.      * arg1, arg2 and arg3.
  758.      * If the method is immutable, don't change anything.
  759.      */
  760.     if (!method_is_immutable(IPC_GET_METHOD(call->data))) {
  761.         if (method_is_system(IPC_GET_METHOD(call->data))) {
  762.             if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_TO_ME)
  763.                 phone_dealloc(IPC_GET_ARG5(call->data));
  764.  
  765.             IPC_SET_ARG1(call->data, method);
  766.             IPC_SET_ARG2(call->data, arg1);
  767.             IPC_SET_ARG3(call->data, arg2);
  768.             if (slow) {
  769.                 IPC_SET_ARG4(call->data, arg3);
  770.                 /*
  771.                  * For system methods we deliberately don't
  772.                  * overwrite ARG5.
  773.                  */
  774.             }
  775.         } else {
  776.             IPC_SET_METHOD(call->data, method);
  777.             IPC_SET_ARG1(call->data, arg1);
  778.             IPC_SET_ARG2(call->data, arg2);
  779.             if (slow) {
  780.                 IPC_SET_ARG3(call->data, arg3);
  781.                 IPC_SET_ARG4(call->data, arg4);
  782.                 IPC_SET_ARG5(call->data, arg5);
  783.             }
  784.         }
  785.     }
  786.  
  787.     return ipc_forward(call, phone, &TASK->answerbox, mode);
  788. }
  789.  
  790. /** Forward a received call to another destination - fast version.
  791.  *
  792.  * @param callid    Hash of the call to forward.
  793.  * @param phoneid   Phone handle to use for forwarding.
  794.  * @param method    New method to use for the forwarded call.
  795.  * @param arg1      New value of the first argument for the forwarded call.
  796.  * @param arg2      New value of the second argument for the forwarded call.
  797.  * @param mode      Flags that specify mode of the forward operation.
  798.  *
  799.  * @return      Return 0 on succes, otherwise return an error code.
  800.  *
  801.  * In case the original method is a system method, ARG1, ARG2 and ARG3 are
  802.  * overwritten in the forwarded message with the new method and the new
  803.  * arg1 and arg2, respectively. Otherwise the METHOD, ARG1 and ARG2 are
  804.  * rewritten with the new method, arg1 and arg2, respectively. Also note there
  805.  * is a set of immutable methods, for which the new method and arguments are not
  806.  * set and these values are ignored.
  807.  */
  808. unative_t sys_ipc_forward_fast(unative_t callid, unative_t phoneid,
  809.     unative_t method, unative_t arg1, unative_t arg2, int mode)
  810. {
  811.     return sys_ipc_forward_common(callid, phoneid, method, arg1, arg2, 0, 0,
  812.         0, mode, false);
  813. }
  814.  
  815. /** Forward a received call to another destination - slow version.
  816.  *
  817.  * @param callid    Hash of the call to forward.
  818.  * @param phoneid   Phone handle to use for forwarding.
  819.  * @param data      Userspace address of the new IPC data.
  820.  * @param mode      Flags that specify mode of the forward operation.
  821.  *
  822.  * @return      Return 0 on succes, otherwise return an error code.
  823.  *
  824.  * This function is the slow verision of the sys_ipc_forward_fast interface.
  825.  * It can copy all five new arguments and the new method from the userspace.
  826.  * It naturally extends the functionality of the fast version. For system
  827.  * methods, it additionally stores the new value of arg3 to ARG4. For non-system
  828.  * methods, it additionally stores the new value of arg3, arg4 and arg5,
  829.  * respectively, to ARG3, ARG4 and ARG5, respectively.
  830.  */
  831. unative_t sys_ipc_forward_slow(unative_t callid, unative_t phoneid,
  832.     ipc_data_t *data, int mode)
  833. {
  834.     ipc_data_t newdata;
  835.     int rc;
  836.  
  837.     rc = copy_from_uspace(&newdata.args, &data->args,
  838.         sizeof(newdata.args));
  839.     if (rc != 0)
  840.         return (unative_t) rc;
  841.  
  842.     return sys_ipc_forward_common(callid, phoneid,
  843.         IPC_GET_METHOD(newdata), IPC_GET_ARG1(newdata),
  844.         IPC_GET_ARG2(newdata), IPC_GET_ARG3(newdata),
  845.         IPC_GET_ARG4(newdata), IPC_GET_ARG5(newdata), mode, true);
  846. }
  847.  
  848. /** Answer an IPC call - fast version.
  849.  *
  850.  * This function can handle only two return arguments of payload, but is faster
  851.  * than the generic sys_ipc_answer().
  852.  *
  853.  * @param callid    Hash of the call to be answered.
  854.  * @param retval    Return value of the answer.
  855.  * @param arg1      Service-defined return value.
  856.  * @param arg2      Service-defined return value.
  857.  * @param arg3      Service-defined return value.
  858.  * @param arg4      Service-defined return value.
  859.  *
  860.  * @return      Return 0 on success, otherwise return an error code.   
  861.  */
  862. unative_t sys_ipc_answer_fast(unative_t callid, unative_t retval,
  863.     unative_t arg1, unative_t arg2, unative_t arg3, unative_t arg4)
  864. {
  865.     call_t *call;
  866.     ipc_data_t saved_data;
  867.     int saveddata = 0;
  868.     int rc;
  869.  
  870.     /* Do not answer notification callids */
  871.     if (callid & IPC_CALLID_NOTIFICATION)
  872.         return 0;
  873.  
  874.     call = get_call(callid);
  875.     if (!call)
  876.         return ENOENT;
  877.  
  878.     if (answer_need_old(call)) {
  879.         memcpy(&saved_data, &call->data, sizeof(call->data));
  880.         saveddata = 1;
  881.     }
  882.  
  883.     IPC_SET_RETVAL(call->data, retval);
  884.     IPC_SET_ARG1(call->data, arg1);
  885.     IPC_SET_ARG2(call->data, arg2);
  886.     IPC_SET_ARG3(call->data, arg3);
  887.     IPC_SET_ARG4(call->data, arg4);
  888.     /*
  889.      * To achieve deterministic behavior, zero out arguments that are beyond
  890.      * the limits of the fast version.
  891.      */
  892.     IPC_SET_ARG5(call->data, 0);
  893.     rc = answer_preprocess(call, saveddata ? &saved_data : NULL);
  894.  
  895.     ipc_answer(&TASK->answerbox, call);
  896.     return rc;
  897. }
  898.  
  899. /** Answer an IPC call.
  900.  *
  901.  * @param callid    Hash of the call to be answered.
  902.  * @param data      Userspace address of call data with the answer.
  903.  *
  904.  * @return      Return 0 on success, otherwise return an error code.
  905.  */
  906. unative_t sys_ipc_answer_slow(unative_t callid, ipc_data_t *data)
  907. {
  908.     call_t *call;
  909.     ipc_data_t saved_data;
  910.     int saveddata = 0;
  911.     int rc;
  912.  
  913.     /* Do not answer notification callids */
  914.     if (callid & IPC_CALLID_NOTIFICATION)
  915.         return 0;
  916.  
  917.     call = get_call(callid);
  918.     if (!call)
  919.         return ENOENT;
  920.  
  921.     if (answer_need_old(call)) {
  922.         memcpy(&saved_data, &call->data, sizeof(call->data));
  923.         saveddata = 1;
  924.     }
  925.     rc = copy_from_uspace(&call->data.args, &data->args,
  926.         sizeof(call->data.args));
  927.     if (rc != 0)
  928.         return rc;
  929.  
  930.     rc = answer_preprocess(call, saveddata ? &saved_data : NULL);
  931.    
  932.     ipc_answer(&TASK->answerbox, call);
  933.  
  934.     return rc;
  935. }
  936.  
  937. /** Hang up a phone.
  938.  *
  939.  * @param       Phone handle of the phone to be hung up.
  940.  *
  941.  * @return      Return 0 on success or an error code.
  942.  */
  943. unative_t sys_ipc_hangup(int phoneid)
  944. {
  945.     phone_t *phone;
  946.  
  947.     GET_CHECK_PHONE(phone, phoneid, return ENOENT);
  948.  
  949.     if (ipc_phone_hangup(phone))
  950.         return -1;
  951.  
  952.     return 0;
  953. }
  954.  
  955. /** Wait for an incoming IPC call or an answer.
  956.  *
  957.  * @param calldata  Pointer to buffer where the call/answer data is stored.
  958.  * @param usec      Timeout. See waitq_sleep_timeout() for explanation.
  959.  * @param flags     Select mode of sleep operation. See waitq_sleep_timeout()
  960.  *          for explanation.
  961.  *
  962.  * @return      Hash of the call.
  963.  *          If IPC_CALLID_NOTIFICATION bit is set in the hash, the
  964.  *          call is a notification. IPC_CALLID_ANSWERED denotes an
  965.  *          answer.
  966.  */
  967. unative_t sys_ipc_wait_for_call(ipc_data_t *calldata, uint32_t usec, int flags)
  968. {
  969.     call_t *call;
  970.  
  971. restart:
  972.  
  973. #ifdef CONFIG_UDEBUG
  974.     udebug_stoppable_begin();
  975. #endif 
  976.     call = ipc_wait_for_call(&TASK->answerbox, usec,
  977.         flags | SYNCH_FLAGS_INTERRUPTIBLE);
  978.  
  979. #ifdef CONFIG_UDEBUG
  980.     udebug_stoppable_end();
  981. #endif
  982.     if (!call)
  983.         return 0;
  984.  
  985.     if (call->flags & IPC_CALL_NOTIF) {
  986.         ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
  987.  
  988.         /* Set in_phone_hash to the interrupt counter */
  989.         call->data.phone = (void *) call->priv;
  990.        
  991.         STRUCT_TO_USPACE(calldata, &call->data);
  992.  
  993.         ipc_call_free(call);
  994.        
  995.         return ((unative_t) call) | IPC_CALLID_NOTIFICATION;
  996.     }
  997.  
  998.     if (call->flags & IPC_CALL_ANSWERED) {
  999.         process_answer(call);
  1000.  
  1001.         ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
  1002.  
  1003.         if (call->flags & IPC_CALL_DISCARD_ANSWER) {
  1004.             ipc_call_free(call);
  1005.             goto restart;
  1006.         } else {
  1007.             /*
  1008.              * Decrement the counter of active calls only if the
  1009.              * call is not an answer to IPC_M_PHONE_HUNGUP,
  1010.              * which doesn't contribute to the counter.
  1011.              */
  1012.             atomic_dec(&TASK->active_calls);
  1013.         }
  1014.  
  1015.         STRUCT_TO_USPACE(&calldata->args, &call->data.args);
  1016.         ipc_call_free(call);
  1017.  
  1018.         return ((unative_t) call) | IPC_CALLID_ANSWERED;
  1019.     }
  1020.  
  1021.     if (process_request(&TASK->answerbox, call))
  1022.         goto restart;
  1023.  
  1024.     /* Include phone address('id') of the caller in the request,
  1025.      * copy whole call->data, not only call->data.args */
  1026.     if (STRUCT_TO_USPACE(calldata, &call->data)) {
  1027.         /*
  1028.          * The callee will not receive this call and no one else has
  1029.          * a chance to answer it. Reply with the EPARTY error code.
  1030.          */
  1031.         ipc_data_t saved_data;
  1032.         int saveddata = 0;
  1033.  
  1034.         if (answer_need_old(call)) {
  1035.             memcpy(&saved_data, &call->data, sizeof(call->data));
  1036.             saveddata = 1;
  1037.         }
  1038.        
  1039.         IPC_SET_RETVAL(call->data, EPARTY);
  1040.         (void) answer_preprocess(call, saveddata ? &saved_data : NULL);
  1041.         ipc_answer(&TASK->answerbox, call);
  1042.         return 0;
  1043.     }
  1044.     return (unative_t)call;
  1045. }
  1046.  
  1047. /** Connect an IRQ handler to a task.
  1048.  *
  1049.  * @param inr       IRQ number.
  1050.  * @param devno     Device number.
  1051.  * @param method    Method to be associated with the notification.
  1052.  * @param ucode     Uspace pointer to the top-half pseudocode.
  1053.  *
  1054.  * @return      EPERM or a return code returned by ipc_irq_register().
  1055.  */
  1056. unative_t sys_ipc_register_irq(inr_t inr, devno_t devno, unative_t method,
  1057.     irq_code_t *ucode)
  1058. {
  1059.     if (!(cap_get(TASK) & CAP_IRQ_REG))
  1060.         return EPERM;
  1061.  
  1062.     return ipc_irq_register(&TASK->answerbox, inr, devno, method, ucode);
  1063. }
  1064.  
  1065. /** Disconnect an IRQ handler from a task.
  1066.  *
  1067.  * @param inr       IRQ number.
  1068.  * @param devno     Device number.
  1069.  *
  1070.  * @return      Zero on success or EPERM on error..
  1071.  */
  1072. unative_t sys_ipc_unregister_irq(inr_t inr, devno_t devno)
  1073. {
  1074.     if (!(cap_get(TASK) & CAP_IRQ_REG))
  1075.         return EPERM;
  1076.  
  1077.     ipc_irq_unregister(&TASK->answerbox, inr, devno);
  1078.  
  1079.     return 0;
  1080. }
  1081.  
  1082. #include <console/console.h>
  1083.  
  1084. /**
  1085.  * Syscall connect to a task by id.
  1086.  *
  1087.  * @return      Phone id on success, or negative error code.
  1088.  */
  1089. unative_t sys_ipc_connect_kbox(sysarg64_t *uspace_taskid_arg)
  1090. {
  1091. #ifdef CONFIG_UDEBUG
  1092.     sysarg64_t taskid_arg;
  1093.     int rc;
  1094.    
  1095.     rc = copy_from_uspace(&taskid_arg, uspace_taskid_arg, sizeof(sysarg64_t));
  1096.     if (rc != 0)
  1097.         return (unative_t) rc;
  1098.  
  1099.     LOG("sys_ipc_connect_kbox(%" PRIu64 ")\n", taskid_arg.value);
  1100.  
  1101.     return ipc_connect_kbox(taskid_arg.value);
  1102. #else
  1103.     return (unative_t) ENOTSUP;
  1104. #endif
  1105. }
  1106.  
  1107. /** @}
  1108.  */
  1109.