Subversion Repositories HelenOS

Rev

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