Subversion Repositories HelenOS

Rev

Rev 1293 | Rev 1329 | 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. #include <arch.h>
  30. #include <proc/task.h>
  31. #include <proc/thread.h>
  32. #include <errno.h>
  33. #include <memstr.h>
  34. #include <debug.h>
  35. #include <ipc/ipc.h>
  36. #include <ipc/sysipc.h>
  37. #include <ipc/irq.h>
  38. #include <ipc/ipcrsc.h>
  39. #include <arch/interrupt.h>
  40. #include <print.h>
  41. #include <syscall/copy.h>
  42. #include <security/cap.h>
  43.  
  44. #define GET_CHECK_PHONE(phone,phoneid,err) { \
  45.       if (phoneid > IPC_MAX_PHONES) { err; } \
  46.       phone = &TASK->phones[phoneid]; \
  47. }
  48.  
  49. #define STRUCT_TO_USPACE(dst,src) copy_to_uspace(dst,src,sizeof(*(src)))
  50.  
  51. /** Return true if the method is a system method */
  52. static inline int is_system_method(__native method)
  53. {
  54.     if (method <= IPC_M_LAST_SYSTEM)
  55.         return 1;
  56.     return 0;
  57. }
  58.  
  59. /** Return true if the message with this method is forwardable
  60.  *
  61.  * - some system messages may be forwarded, for some of them
  62.  *   it is useless
  63.  */
  64. static inline int is_forwardable(__native method)
  65. {
  66.     if (method == IPC_M_PHONE_HUNGUP)
  67.         return 0; /* This message is meant only for the receiver */
  68.     return 1;
  69. }
  70.  
  71. /****************************************************/
  72. /* Functions that preprocess answer before sending
  73.  * it to the recepient
  74.  */
  75.  
  76. /** Return true if the caller (ipc_answer) should save
  77.  * the old call contents for answer_preprocess
  78.  */
  79. static inline int answer_need_old(call_t *call)
  80. {
  81.     if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_TO_ME)
  82.         return 1;
  83.     if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_ME_TO)
  84.         return 1;
  85.     return 0;
  86. }
  87.  
  88. /** Interpret process answer as control information */
  89. static inline void answer_preprocess(call_t *answer, ipc_data_t *olddata)
  90. {
  91.     int phoneid;
  92.  
  93.     if (IPC_GET_RETVAL(answer->data) == EHANGUP) {
  94.         /* In case of forward, hangup the forwared phone,
  95.          * not the originator
  96.          */
  97.         spinlock_lock(&answer->data.phone->lock);
  98.         spinlock_lock(&TASK->answerbox.lock);
  99.         if (answer->data.phone->callee) {
  100.             list_remove(&answer->data.phone->list);
  101.             answer->data.phone->callee = 0;
  102.         }
  103.         spinlock_unlock(&TASK->answerbox.lock);
  104.         spinlock_unlock(&answer->data.phone->lock);
  105.     }
  106.  
  107.     if (!olddata)
  108.         return;
  109.  
  110.     if (IPC_GET_METHOD(*olddata) == IPC_M_CONNECT_TO_ME) {
  111.         phoneid = IPC_GET_ARG3(*olddata);
  112.         if (IPC_GET_RETVAL(answer->data)) {
  113.             /* The connection was not accepted */
  114.             phone_dealloc(phoneid);
  115.         } else {
  116.             /* The connection was accepted */
  117.             phone_connect(phoneid,&answer->sender->answerbox);
  118.             /* Set 'phone identification' as arg3 of response */
  119.             IPC_SET_ARG3(answer->data, (__native)&TASK->phones[phoneid]);
  120.         }
  121.     } else if (IPC_GET_METHOD(*olddata) == IPC_M_CONNECT_ME_TO) {
  122.         /* If the users accepted call, connect */
  123.         if (!IPC_GET_RETVAL(answer->data)) {
  124.             ipc_phone_connect((phone_t *)IPC_GET_ARG3(*olddata),
  125.                       &TASK->answerbox);
  126.         }
  127.     }
  128. }
  129.  
  130. /** Called before the request is sent
  131.  *
  132.  * @return 0 - no error, -1 - report error to user
  133.  */
  134. static int request_preprocess(call_t *call)
  135. {
  136.     int newphid;
  137.  
  138.     switch (IPC_GET_METHOD(call->data)) {
  139.     case IPC_M_CONNECT_ME_TO:
  140.         newphid = phone_alloc();
  141.         if (newphid < 0)
  142.             return ELIMIT;
  143.         /* Set arg3 for server */
  144.         IPC_SET_ARG3(call->data, (__native)&TASK->phones[newphid]);
  145.         call->flags |= IPC_CALL_CONN_ME_TO;
  146.         call->private = newphid;
  147.         break;
  148.     default:
  149.         break;
  150.     }
  151.     return 0;
  152. }
  153.  
  154. /****************************************************/
  155. /* Functions called to process received call/answer
  156.  * before passing to uspace
  157.  */
  158.  
  159. /** Do basic kernel processing of received call answer */
  160. static void process_answer(call_t *call)
  161. {
  162.     if (IPC_GET_RETVAL(call->data) == EHANGUP && \
  163.         call->flags & IPC_CALL_FORWARDED)
  164.         IPC_SET_RETVAL(call->data, EFORWARD);
  165.  
  166.     if (call->flags & IPC_CALL_CONN_ME_TO) {
  167.         if (IPC_GET_RETVAL(call->data))
  168.             phone_dealloc(call->private);
  169.         else
  170.             IPC_SET_ARG3(call->data, call->private);
  171.     }
  172. }
  173.  
  174. /** Do basic kernel processing of received call request
  175.  *
  176.  * @return 0 - the call should be passed to userspace, 1 - ignore call
  177.  */
  178. static int process_request(answerbox_t *box,call_t *call)
  179. {
  180.     int phoneid;
  181.  
  182.     if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_TO_ME) {
  183.         phoneid = phone_alloc();
  184.         if (phoneid < 0) { /* Failed to allocate phone */
  185.             IPC_SET_RETVAL(call->data, ELIMIT);
  186.             ipc_answer(box,call);
  187.             return -1;
  188.         }
  189.         IPC_SET_ARG3(call->data, phoneid);
  190.     }
  191.     return 0;
  192. }
  193.  
  194. /** Send a call over IPC, wait for reply, return to user
  195.  *
  196.  * @return Call identification, returns -1 on fatal error,
  197.            -2 on 'Too many async request, handle answers first
  198.  */
  199. __native sys_ipc_call_sync_fast(__native phoneid, __native method,
  200.                 __native arg1, ipc_data_t *data)
  201. {
  202.     call_t call;
  203.     phone_t *phone;
  204.     int res;
  205.  
  206.     GET_CHECK_PHONE(phone, phoneid, return ENOENT);
  207.  
  208.     ipc_call_static_init(&call);
  209.     IPC_SET_METHOD(call.data, method);
  210.     IPC_SET_ARG1(call.data, arg1);
  211.  
  212.     if (!(res=request_preprocess(&call))) {
  213.         ipc_call_sync(phone, &call);
  214.         process_answer(&call);
  215.     } else
  216.         IPC_SET_RETVAL(call.data, res);
  217.     STRUCT_TO_USPACE(&data->args, &call.data.args);
  218.  
  219.     return 0;
  220. }
  221.  
  222. /** Synchronous IPC call allowing to send whole message */
  223. __native sys_ipc_call_sync(__native phoneid, ipc_data_t *question,
  224.                ipc_data_t *reply)
  225. {
  226.     call_t call;
  227.     phone_t *phone;
  228.     int res;
  229.     int rc;
  230.  
  231.     ipc_call_static_init(&call);
  232.     rc = copy_from_uspace(&call.data.args, &question->args, sizeof(call.data.args));
  233.     if (rc != 0)
  234.         return (__native) rc;
  235.  
  236.     GET_CHECK_PHONE(phone, phoneid, return ENOENT);
  237.  
  238.     if (!(res=request_preprocess(&call))) {
  239.         ipc_call_sync(phone, &call);
  240.         process_answer(&call);
  241.     } else
  242.         IPC_SET_RETVAL(call.data, res);
  243.  
  244.     rc = STRUCT_TO_USPACE(&reply->args, &call.data.args);
  245.     if (rc != 0)
  246.         return rc;
  247.  
  248.     return 0;
  249. }
  250.  
  251. /** Check that the task did not exceed allowed limit
  252.  *
  253.  * @return 0 - Limit OK,   -1 - limit exceeded
  254.  */
  255. static int check_call_limit(void)
  256. {
  257.     if (atomic_preinc(&TASK->active_calls) > IPC_MAX_ASYNC_CALLS) {
  258.         atomic_dec(&TASK->active_calls);
  259.         return -1;
  260.     }
  261.     return 0;
  262. }
  263.  
  264. /** Send an asynchronous call over ipc
  265.  *
  266.  * @return Call identification, returns -1 on fatal error,
  267.            -2 on 'Too many async request, handle answers first
  268.  */
  269. __native sys_ipc_call_async_fast(__native phoneid, __native method,
  270.                  __native arg1, __native arg2)
  271. {
  272.     call_t *call;
  273.     phone_t *phone;
  274.     int res;
  275.  
  276.     if (check_call_limit())
  277.         return IPC_CALLRET_TEMPORARY;
  278.  
  279.     GET_CHECK_PHONE(phone, phoneid, return IPC_CALLRET_FATAL);
  280.  
  281.     call = ipc_call_alloc(0);
  282.     IPC_SET_METHOD(call->data, method);
  283.     IPC_SET_ARG1(call->data, arg1);
  284.     IPC_SET_ARG2(call->data, arg2);
  285.  
  286.     if (!(res=request_preprocess(call)))
  287.         ipc_call(phone, call);
  288.     else
  289.         ipc_backsend_err(phone, call, res);
  290.  
  291.     return (__native) call;
  292. }
  293.  
  294. /** Synchronous IPC call allowing to send whole message
  295.  *
  296.  * @return The same as sys_ipc_call_async
  297.  */
  298. __native sys_ipc_call_async(__native phoneid, ipc_data_t *data)
  299. {
  300.     call_t *call;
  301.     phone_t *phone;
  302.     int res;
  303.     int rc;
  304.  
  305.     if (check_call_limit())
  306.         return IPC_CALLRET_TEMPORARY;
  307.  
  308.     GET_CHECK_PHONE(phone, phoneid, return IPC_CALLRET_FATAL);
  309.  
  310.     call = ipc_call_alloc(0);
  311.     rc = copy_from_uspace(&call->data.args, &data->args, sizeof(call->data.args));
  312.     if (rc != 0) {
  313.         ipc_call_free(call);
  314.         return (__native) rc;
  315.     }
  316.     if (!(res=request_preprocess(call)))
  317.         ipc_call(phone, call);
  318.     else
  319.         ipc_backsend_err(phone, call, res);
  320.  
  321.     return (__native) call;
  322. }
  323.  
  324. /** Forward received call to another destination
  325.  *
  326.  * The arg1 and arg2 are changed in the forwarded message
  327.  *
  328.  * Warning: If implementing non-fast version, make sure that
  329.  *          arg3 is not rewritten for certain system IPC
  330.  */
  331. __native sys_ipc_forward_fast(__native callid, __native phoneid,
  332.                   __native method, __native arg1)
  333. {
  334.     call_t *call;
  335.     phone_t *phone;
  336.  
  337.     call = get_call(callid);
  338.     if (!call)
  339.         return ENOENT;
  340.  
  341.     call->flags |= IPC_CALL_FORWARDED;
  342.  
  343.     GET_CHECK_PHONE(phone, phoneid, {
  344.         IPC_SET_RETVAL(call->data, EFORWARD);
  345.         ipc_answer(&TASK->answerbox, call);
  346.         return ENOENT;
  347.     });    
  348.  
  349.     if (!is_forwardable(IPC_GET_METHOD(call->data))) {
  350.         IPC_SET_RETVAL(call->data, EFORWARD);
  351.         ipc_answer(&TASK->answerbox, call);
  352.         return EPERM;
  353.     }
  354.  
  355.     /* Userspace is not allowed to change method of system methods
  356.      * on forward, allow changing ARG1 and ARG2 by means of method and arg1
  357.      */
  358.     if (is_system_method(IPC_GET_METHOD(call->data))) {
  359.         if (IPC_GET_METHOD(call->data) == IPC_M_CONNECT_TO_ME)
  360.             phone_dealloc(IPC_GET_ARG3(call->data));
  361.  
  362.         IPC_SET_ARG1(call->data, method);
  363.         IPC_SET_ARG2(call->data, arg1);
  364.     } else {
  365.         IPC_SET_METHOD(call->data, method);
  366.         IPC_SET_ARG1(call->data, arg1);
  367.     }
  368.  
  369.     return ipc_forward(call, phone, &TASK->answerbox);
  370. }
  371.  
  372. /** Send IPC answer */
  373. __native sys_ipc_answer_fast(__native callid, __native retval,
  374.                  __native arg1, __native arg2)
  375. {
  376.     call_t *call;
  377.     ipc_data_t saved_data;
  378.     int saveddata = 0;
  379.  
  380.     call = get_call(callid);
  381.     if (!call)
  382.         return ENOENT;
  383.  
  384.     if (answer_need_old(call)) {
  385.         memcpy(&saved_data, &call->data, sizeof(call->data));
  386.         saveddata = 1;
  387.     }
  388.  
  389.     IPC_SET_RETVAL(call->data, retval);
  390.     IPC_SET_ARG1(call->data, arg1);
  391.     IPC_SET_ARG2(call->data, arg2);
  392.     answer_preprocess(call, saveddata ? &saved_data : NULL);
  393.  
  394.     ipc_answer(&TASK->answerbox, call);
  395.     return 0;
  396. }
  397.  
  398. /** Send IPC answer */
  399. __native sys_ipc_answer(__native callid, ipc_data_t *data)
  400. {
  401.     call_t *call;
  402.     ipc_data_t saved_data;
  403.     int saveddata = 0;
  404.     int rc;
  405.  
  406.     call = get_call(callid);
  407.     if (!call)
  408.         return ENOENT;
  409.  
  410.     if (answer_need_old(call)) {
  411.         memcpy(&saved_data, &call->data, sizeof(call->data));
  412.         saveddata = 1;
  413.     }
  414.     rc = copy_from_uspace(&call->data.args, &data->args,
  415.              sizeof(call->data.args));
  416.     if (rc != 0)
  417.         return rc;
  418.  
  419.     answer_preprocess(call, saveddata ? &saved_data : NULL);
  420.    
  421.     ipc_answer(&TASK->answerbox, call);
  422.  
  423.     return 0;
  424. }
  425.  
  426. /** Hang up the phone
  427.  *
  428.  */
  429. __native sys_ipc_hangup(int phoneid)
  430. {
  431.     phone_t *phone;
  432.  
  433.     GET_CHECK_PHONE(phone, phoneid, return ENOENT);
  434.  
  435.     if (ipc_phone_hangup(phone))
  436.         return -1;
  437.  
  438.     return 0;
  439. }
  440.  
  441. /** Wait for incoming ipc call or answer
  442.  *
  443.  * @param calldata Pointer to buffer where the call/answer data is stored
  444.  * @param flags
  445.  * @return Callid, if callid & 1, then the call is answer
  446.  */
  447. __native sys_ipc_wait_for_call(ipc_data_t *calldata, __native flags)
  448. {
  449.     call_t *call;
  450.  
  451. restart:   
  452.     call = ipc_wait_for_call(&TASK->answerbox, flags);
  453.     if (!call)
  454.         return 0;
  455.  
  456.     if (call->flags & IPC_CALL_NOTIF) {
  457.         ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
  458.         STRUCT_TO_USPACE(&calldata->args, &call->data.args);
  459.         ipc_call_free(call);
  460.        
  461.         return ((__native)call) | IPC_CALLID_NOTIFICATION;
  462.     }
  463.  
  464.     if (call->flags & IPC_CALL_ANSWERED) {
  465.         process_answer(call);
  466.  
  467.         ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
  468.  
  469.         atomic_dec(&TASK->active_calls);
  470.  
  471.         if (call->flags & IPC_CALL_DISCARD_ANSWER) {
  472.             ipc_call_free(call);
  473.             goto restart;
  474.         }
  475.  
  476.         STRUCT_TO_USPACE(&calldata->args, &call->data.args);
  477.         ipc_call_free(call);
  478.  
  479.         return ((__native)call) | IPC_CALLID_ANSWERED;
  480.     }
  481.  
  482.     if (process_request(&TASK->answerbox, call))
  483.         goto restart;
  484.  
  485.     /* Include phone address('id') of the caller in the request,
  486.      * copy whole call->data, not only call->data.args */
  487.     STRUCT_TO_USPACE(calldata, &call->data);
  488.     return (__native)call;
  489. }
  490.  
  491. /** Connect irq handler to task */
  492. __native sys_ipc_register_irq(__native irq, irq_code_t *ucode)
  493. {
  494.     if (!(cap_get(TASK) & CAP_IRQ_REG))
  495.         return EPERM;
  496.  
  497.     if (irq >= IRQ_COUNT)
  498.         return (__native) ELIMIT;
  499.  
  500.     irq_ipc_bind_arch(irq);
  501.  
  502.     return ipc_irq_register(&TASK->answerbox, irq, ucode);
  503. }
  504.  
  505. /* Disconnect irq handler from task */
  506. __native sys_ipc_unregister_irq(__native irq)
  507. {
  508.     if (!(cap_get(TASK) & CAP_IRQ_REG))
  509.         return EPERM;
  510.  
  511.     if (irq >= IRQ_COUNT)
  512.         return (__native) ELIMIT;
  513.  
  514.     ipc_irq_unregister(&TASK->answerbox, irq);
  515.  
  516.     return 0;
  517. }
  518.