Subversion Repositories HelenOS-historic

Rev

Rev 1072 | Rev 1086 | 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.  
  32. #include <errno.h>
  33. #include <mm/page.h>
  34. #include <memstr.h>
  35. #include <debug.h>
  36. #include <ipc/ipc.h>
  37. #include <ipc/sysipc.h>
  38. #include <ipc/ipcrsc.h>
  39.  
  40.  
  41. #include <print.h>
  42. #include <arch.h>
  43. #include <proc/thread.h>
  44.  
  45. /* TODO: multi-threaded connect-to-me can cause race condition
  46.  * on phone, add counter + thread_kill??
  47.  *
  48.  */
  49.  
  50. #define GET_CHECK_PHONE(phone,phoneid,err) { \
  51.       if (phoneid > IPC_MAX_PHONES) { err; } \
  52.       phone = &TASK->phones[phoneid]; \
  53. }
  54.  
  55.  
  56. /** Return true if the method is a system method */
  57. static inline int is_system_method(__native method)
  58. {
  59.     if (method <= IPC_M_LAST_SYSTEM)
  60.         return 1;
  61.     return 0;
  62. }
  63.  
  64. /** Return true if the message with this method is forwardable
  65.  *
  66.  * - some system messages may be forwarded, for some of them
  67.  *   it is useless
  68.  */
  69. static inline int is_forwardable(__native method)
  70. {
  71.     return 1;
  72. }
  73.  
  74. /****************************************************/
  75. /* Functions that preprocess answer before sending
  76.  * it to the recepient
  77.  */
  78.  
  79. /** Return true if the caller (ipc_answer) should save
  80.  * the old call contents and call answer_preprocess
  81.  */
  82. static inline int answer_will_preprocess(call_t *call)
  83. {
  84.     if (IPC_GET_METHOD(call->data) == IPC_M_CONNECTTOME)
  85.         return 1;
  86.     if (IPC_GET_METHOD(call->data) == IPC_M_CONNECTMETO)
  87.         return 1;
  88.     return 0;
  89. }
  90.  
  91. /** Interpret process answer as control information */
  92. static inline void answer_preprocess(call_t *answer, ipc_data_t *olddata)
  93. {
  94.     int phoneid;
  95.  
  96.     if (IPC_GET_METHOD(*olddata) == IPC_M_CONNECTTOME) {
  97.         phoneid = IPC_GET_ARG3(*olddata);
  98.         if (IPC_GET_RETVAL(answer->data)) {
  99.             /* The connection was not accepted */
  100.             phone_dealloc(phoneid);
  101.         } else {
  102.             /* The connection was accepted */
  103.             phone_connect(phoneid,&answer->sender->answerbox);
  104.         }
  105.     } else if (IPC_GET_METHOD(*olddata) == IPC_M_CONNECTMETO) {
  106.         /* If the users accepted call, connect */
  107.         if (!IPC_GET_RETVAL(answer->data)) {
  108.             ipc_phone_connect((phone_t *)IPC_GET_ARG3(*olddata),
  109.                       &TASK->answerbox);
  110.         }
  111.     }
  112. }
  113.  
  114. /****************************************************/
  115. /* Functions called to process received call/answer
  116.  * before passing to uspace
  117.  */
  118.  
  119. /** Do basic kernel processing of received call answer */
  120. static int process_answer(answerbox_t *box,call_t *call)
  121. {
  122.     return 0;
  123. }
  124.  
  125. /** Do basic kernel processing of received call request
  126.  *
  127.  * @return 0 - the call should be passed to userspace, 1 - ignore call
  128.  */
  129. static int process_request(answerbox_t *box,call_t *call)
  130. {
  131.     int phoneid;
  132.  
  133.     if (IPC_GET_METHOD(call->data) == IPC_M_CONNECTTOME) {
  134.         phoneid = phone_alloc();
  135.         if (phoneid < 0) { /* Failed to allocate phone */
  136.             IPC_SET_RETVAL(call->data, ELIMIT);
  137.             ipc_answer(box,call);
  138.             return -1;
  139.         }
  140.         IPC_SET_ARG3(call->data, phoneid);
  141.     }
  142.     return 0;
  143. }
  144.  
  145. /** Send a call over IPC, wait for reply, return to user
  146.  *
  147.  * @return Call identification, returns -1 on fatal error,
  148.            -2 on 'Too many async request, handle answers first
  149.  */
  150. __native sys_ipc_call_sync_fast(__native phoneid, __native method,
  151.                 __native arg1, __native *data)
  152. {
  153.     call_t call;
  154.     phone_t *phone;
  155.  
  156.     if (is_system_method(method))
  157.         return EPERM;
  158.  
  159.     GET_CHECK_PHONE(phone, phoneid, return ENOENT);
  160.  
  161.     ipc_call_static_init(&call);
  162.     IPC_SET_METHOD(call.data, method);
  163.     IPC_SET_ARG1(call.data, arg1);
  164.    
  165.     ipc_call_sync(phone, &call);
  166.  
  167.     copy_to_uspace(data, &call.data, sizeof(call.data));
  168.  
  169.     return 0;
  170. }
  171.  
  172. /** Synchronous IPC call allowing to send whole message */
  173. __native sys_ipc_call_sync(__native phoneid, __native *question,
  174.                __native *reply)
  175. {
  176.     call_t call;
  177.     phone_t *phone;
  178.  
  179.     ipc_call_static_init(&call);
  180.     copy_from_uspace(&call.data, question, sizeof(call.data));
  181.  
  182.     if (is_system_method(IPC_GET_METHOD(call.data)))
  183.         return EPERM;
  184.    
  185.     GET_CHECK_PHONE(phone, phoneid, return ENOENT);
  186.  
  187.     ipc_call_sync(phone, &call);
  188.  
  189.     copy_to_uspace(reply, &call.data, sizeof(call.data));
  190.  
  191.     return 0;
  192. }
  193.  
  194. /** Check that the task did not exceed allowed limit
  195.  *
  196.  * @return 0 - Limit OK,   -1 - limit exceeded
  197.  */
  198. static int check_call_limit(void)
  199. {
  200.     if (atomic_preinc(&TASK->active_calls) > IPC_MAX_ASYNC_CALLS) {
  201.         atomic_dec(&TASK->active_calls);
  202.         return -1;
  203.     }
  204.     return 0;
  205. }
  206.  
  207. /** Send an asynchronous call over ipc
  208.  *
  209.  * @return Call identification, returns -1 on fatal error,
  210.            -2 on 'Too many async request, handle answers first
  211.  */
  212. __native sys_ipc_call_async_fast(__native phoneid, __native method,
  213.                  __native arg1, __native arg2)
  214. {
  215.     call_t *call;
  216.     phone_t *phone;
  217.  
  218.     if (is_system_method(method))
  219.         return IPC_CALLRET_FATAL;
  220.  
  221.     if (check_call_limit())
  222.         return IPC_CALLRET_TEMPORARY;
  223.  
  224.     GET_CHECK_PHONE(phone, phoneid, return ENOENT);
  225.  
  226.     call = ipc_call_alloc();
  227.     IPC_SET_METHOD(call->data, method);
  228.     IPC_SET_ARG1(call->data, arg1);
  229.     IPC_SET_ARG2(call->data, arg2);
  230.  
  231.     ipc_call(phone, call);
  232.  
  233.     return (__native) call;
  234. }
  235.  
  236. /** Synchronous IPC call allowing to send whole message
  237.  *
  238.  * @return The same as sys_ipc_call_async
  239.  */
  240. __native sys_ipc_call_async(__native phoneid, __native *data)
  241. {
  242.     call_t *call;
  243.     phone_t *phone;
  244.  
  245.     if (check_call_limit())
  246.         return IPC_CALLRET_TEMPORARY;
  247.  
  248.     GET_CHECK_PHONE(phone, phoneid, return ENOENT);
  249.  
  250.     call = ipc_call_alloc();
  251.     copy_from_uspace(&call->data, data, sizeof(call->data));
  252.  
  253.     if (is_system_method(IPC_GET_METHOD(call->data))) {
  254.         ipc_call_free(call);
  255.         return EPERM;
  256.     }
  257.    
  258.     ipc_call(phone, call);
  259.  
  260.     return (__native) call;
  261. }
  262.  
  263. /** Forward received call to another destination
  264.  *
  265.  * The arg1 and arg2 are changed in the forwarded message
  266.  *
  267.  * Warning: If implementing non-fast version, make sure that
  268.  *          arg3 is not rewritten for certain system IPC
  269.  */
  270. __native sys_ipc_forward_fast(__native callid, __native phoneid,
  271.                   __native method, __native arg1)
  272. {
  273.     call_t *call;
  274.     phone_t *phone;
  275.  
  276.     call = get_call(callid);
  277.     if (!call)
  278.         return ENOENT;
  279.  
  280.     GET_CHECK_PHONE(phone, phoneid, {
  281.         IPC_SET_RETVAL(call->data, EFORWARD);
  282.         ipc_answer(&TASK->answerbox, call);
  283.         return ENOENT;
  284.     });    
  285.  
  286.     if (!is_forwardable(IPC_GET_METHOD(call->data))) {
  287.         IPC_SET_RETVAL(call->data, EFORWARD);
  288.         ipc_answer(&TASK->answerbox, call);
  289.         return EPERM;
  290.     }
  291.  
  292.     /* Userspace is not allowed to change method of system methods
  293.      * on forward, allow changing ARG1 and ARG2 by means of method and arg1
  294.      */
  295.     if (is_system_method(IPC_GET_METHOD(call->data))) {
  296.         IPC_SET_ARG1(call->data, method);
  297.         IPC_SET_ARG2(call->data, arg1);
  298.     } else {
  299.         IPC_SET_METHOD(call->data, method);
  300.         IPC_SET_ARG1(call->data, arg1);
  301.     }
  302.  
  303.     ipc_forward(call, phone, &TASK->answerbox);
  304.  
  305.     return 0;
  306. }
  307.  
  308. /** Send IPC answer */
  309. __native sys_ipc_answer_fast(__native callid, __native retval,
  310.                  __native arg1, __native arg2)
  311. {
  312.     call_t *call;
  313.     ipc_data_t saved_data;
  314.     int preprocess = 0;
  315.  
  316.     call = get_call(callid);
  317.     if (!call)
  318.         return ENOENT;
  319.  
  320.     if (answer_will_preprocess(call)) {
  321.         memcpy(&saved_data, &call->data, sizeof(call->data));
  322.         preprocess = 1;
  323.     }
  324.  
  325.     IPC_SET_RETVAL(call->data, retval);
  326.     IPC_SET_ARG1(call->data, arg1);
  327.     IPC_SET_ARG2(call->data, arg2);
  328.  
  329.     if (preprocess)
  330.         answer_preprocess(call, &saved_data);
  331.  
  332.     ipc_answer(&TASK->answerbox, call);
  333.     return 0;
  334. }
  335.  
  336. /** Send IPC answer */
  337. inline __native sys_ipc_answer(__native callid, __native *data)
  338. {
  339.     call_t *call;
  340.     ipc_data_t saved_data;
  341.     int preprocess = 0;
  342.  
  343.     call = get_call(callid);
  344.     if (!call)
  345.         return ENOENT;
  346.  
  347.     if (answer_will_preprocess(call)) {
  348.         memcpy(&saved_data, &call->data, sizeof(call->data));
  349.         preprocess = 1;
  350.     }
  351.     copy_from_uspace(&call->data, data, sizeof(call->data));
  352.  
  353.     if (preprocess)
  354.         answer_preprocess(call, &saved_data);
  355.    
  356.     ipc_answer(&TASK->answerbox, call);
  357.  
  358.     return 0;
  359. }
  360.  
  361. /** Ask the other side of connection to do 'callback' connection
  362.  *
  363.  * @return 0 if no error, error otherwise
  364.  */
  365. __native sys_ipc_connect_to_me(__native phoneid, __native arg1,
  366.                    __native arg2, task_id_t *taskid)
  367. {
  368.     call_t call;
  369.     phone_t *phone;
  370.  
  371.     ipc_call_static_init(&call);
  372.     IPC_SET_METHOD(call.data, IPC_M_CONNECTTOME);
  373.     IPC_SET_ARG1(call.data, arg1);
  374.     IPC_SET_ARG2(call.data, arg2);
  375.    
  376.     GET_CHECK_PHONE(phone, phoneid, return ENOENT);
  377.  
  378.     ipc_call_sync(phone, &call);
  379.  
  380.     if (!IPC_GET_RETVAL(call.data) && taskid)
  381.         copy_to_uspace(taskid,
  382.                    &phone->callee->task->taskid,
  383.                    sizeof(TASK->taskid));
  384.  
  385.     return IPC_GET_RETVAL(call.data);
  386. }
  387.  
  388. /** Ask target process to connect me somewhere
  389.  *
  390.  * @return phoneid - on success, error otherwise
  391.  */
  392. __native sys_ipc_connect_me_to(__native phoneid, __native arg1,
  393.                    __native arg2)
  394. {
  395.     call_t call;
  396.     phone_t *phone;
  397.     int newphid;
  398.  
  399.     GET_CHECK_PHONE(phone, phoneid, return ENOENT);
  400.  
  401.     newphid = phone_alloc();
  402.     if (newphid < 0)
  403.         return ELIMIT;
  404.  
  405.     ipc_call_static_init(&call);
  406.     IPC_SET_METHOD(call.data, IPC_M_CONNECTMETO);
  407.     IPC_SET_ARG1(call.data, arg1);
  408.     IPC_SET_ARG2(call.data, arg2);
  409.     IPC_SET_ARG3(call.data, (__native)&TASK->phones[newphid]);
  410.  
  411.     ipc_call_sync(phone, &call);
  412.  
  413.     if (IPC_GET_RETVAL(call.data)) { /* Connection failed */
  414.         phone_dealloc(newphid);
  415.         return IPC_GET_RETVAL(call.data);
  416.     }
  417.  
  418.     return newphid;
  419. }
  420.  
  421. /** Wait for incoming ipc call or answer
  422.  *
  423.  * Generic function - can serve either as inkernel or userspace call
  424.  * - inside kernel does probably unnecessary copying of data (TODO)
  425.  *
  426.  * @param result
  427.  * @param taskid
  428.  * @param flags
  429.  * @return Callid, if callid & 1, then the call is answer
  430.  */
  431. inline __native sys_ipc_wait_for_call(ipc_data_t *calldata,
  432.                       task_id_t *taskid,
  433.                       __native flags)
  434.                          
  435. {
  436.     call_t *call;
  437.  
  438. restart:   
  439.     call = ipc_wait_for_call(&TASK->answerbox, flags);
  440.  
  441.     if (call->flags & IPC_CALL_ANSWERED) {
  442.         if (process_answer(&TASK->answerbox, call))
  443.             goto restart;
  444.  
  445.         copy_to_uspace(calldata, &call->data, sizeof(call->data));
  446.         atomic_dec(&TASK->active_calls);
  447.         ASSERT(! (call->flags & IPC_CALL_STATIC_ALLOC));
  448.         ipc_call_free(call);
  449.  
  450.         return ((__native)call) | IPC_CALLID_ANSWERED;
  451.     }
  452.     if (process_request(&TASK->answerbox, call))
  453.         goto restart;
  454.     copy_to_uspace(calldata, &call->data, sizeof(call->data));
  455.     copy_to_uspace(taskid, (void *)&TASK->taskid, sizeof(TASK->taskid));
  456.     return (__native)call;
  457. }
  458.  
  459.