Subversion Repositories HelenOS

Rev

Rev 2601 | Rev 2623 | 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. /* Lock ordering
  36.  *
  37.  * First the answerbox, then the phone.
  38.  */
  39.  
  40. #include <synch/spinlock.h>
  41. #include <synch/waitq.h>
  42. #include <synch/synch.h>
  43. #include <ipc/ipc.h>
  44. #include <errno.h>
  45. #include <mm/slab.h>
  46. #include <arch.h>
  47. #include <proc/task.h>
  48. #include <memstr.h>
  49. #include <debug.h>
  50.  
  51. #include <print.h>
  52. #include <proc/thread.h>
  53. #include <arch/interrupt.h>
  54. #include <ipc/irq.h>
  55.  
  56. /** Open channel that is assigned automatically to new tasks */
  57. answerbox_t *ipc_phone_0 = NULL;
  58.  
  59. static slab_cache_t *ipc_call_slab;
  60.  
  61. /** Initialize a call structure.
  62.  *
  63.  * @param call      Call structure to be initialized.
  64.  */
  65. static void _ipc_call_init(call_t *call)
  66. {
  67.     memsetb((uintptr_t) call, sizeof(*call), 0);
  68.     call->callerbox = &TASK->answerbox;
  69.     call->sender = TASK;
  70.     call->buffer = NULL;
  71. }
  72.  
  73. /** Allocate and initialize a call structure.
  74.  *
  75.  * The call is initialized, so that the reply will be directed to
  76.  * TASK->answerbox.
  77.  *
  78.  * @param flags     Parameters for slab_alloc (e.g FRAME_ATOMIC).
  79.  *
  80.  * @return      If flags permit it, return NULL, or initialized kernel
  81.  *          call structure.
  82.  */
  83. call_t *ipc_call_alloc(int flags)
  84. {
  85.     call_t *call;
  86.  
  87.     call = slab_alloc(ipc_call_slab, flags);
  88.     _ipc_call_init(call);
  89.  
  90.     return call;
  91. }
  92.  
  93. /** Initialize a statically allocated call structure.
  94.  *
  95.  * @param call      Statically allocated kernel call structure to be
  96.  *          initialized.
  97.  */
  98. void ipc_call_static_init(call_t *call)
  99. {
  100.     _ipc_call_init(call);
  101.     call->flags |= IPC_CALL_STATIC_ALLOC;
  102. }
  103.  
  104. /** Deallocate a call structure.
  105.  *
  106.  * @param call      Call structure to be freed.
  107.  */
  108. void ipc_call_free(call_t *call)
  109. {
  110.     ASSERT(!(call->flags & IPC_CALL_STATIC_ALLOC));
  111.     /* Check to see if we have data in the IPC_M_DATA_SEND buffer. */
  112.     if (call->buffer)
  113.         free(call->buffer);
  114.     slab_free(ipc_call_slab, call);
  115. }
  116.  
  117. /** Initialize an answerbox structure.
  118.  *
  119.  * @param box       Answerbox structure to be initialized.
  120.  */
  121. void ipc_answerbox_init(answerbox_t *box)
  122. {
  123.     spinlock_initialize(&box->lock, "ipc_box_lock");
  124.     spinlock_initialize(&box->irq_lock, "ipc_box_irqlock");
  125.     waitq_initialize(&box->wq);
  126.     list_initialize(&box->connected_phones);
  127.     list_initialize(&box->calls);
  128.     list_initialize(&box->dispatched_calls);
  129.     list_initialize(&box->answers);
  130.     list_initialize(&box->irq_notifs);
  131.     list_initialize(&box->irq_head);
  132.     box->task = TASK;
  133. }
  134.  
  135. /** Connect a phone to an answerbox.
  136.  *
  137.  * @param phone     Initialized phone structure.
  138.  * @param box       Initialized answerbox structure.
  139.  */
  140. void ipc_phone_connect(phone_t *phone, answerbox_t *box)
  141. {
  142.     spinlock_lock(&phone->lock);
  143.  
  144.     phone->state = IPC_PHONE_CONNECTED;
  145.     phone->callee = box;
  146.  
  147.     spinlock_lock(&box->lock);
  148.     list_append(&phone->link, &box->connected_phones);
  149.     spinlock_unlock(&box->lock);
  150.  
  151.     spinlock_unlock(&phone->lock);
  152. }
  153.  
  154. /** Initialize a phone structure.
  155.  *
  156.  * @param phone     Phone structure to be initialized.
  157.  */
  158. void ipc_phone_init(phone_t *phone)
  159. {
  160.     spinlock_initialize(&phone->lock, "phone_lock");
  161.     phone->callee = NULL;
  162.     phone->state = IPC_PHONE_FREE;
  163.     atomic_set(&phone->active_calls, 0);
  164. }
  165.  
  166. /** Helper function to facilitate synchronous calls.
  167.  *
  168.  * @param phone     Destination kernel phone structure.
  169.  * @param request   Call structure with request.
  170.  */
  171. void ipc_call_sync(phone_t *phone, call_t *request)
  172. {
  173.     answerbox_t sync_box;
  174.  
  175.     ipc_answerbox_init(&sync_box);
  176.  
  177.     /* We will receive data in a special box. */
  178.     request->callerbox = &sync_box;
  179.  
  180.     ipc_call(phone, request);
  181.     ipc_wait_for_call(&sync_box, SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE);
  182. }
  183.  
  184. /** Answer a message which was not dispatched and is not listed in any queue.
  185.  *
  186.  * @param call      Call structure to be answered.
  187.  */
  188. static void _ipc_answer_free_call(call_t *call)
  189. {
  190.     answerbox_t *callerbox = call->callerbox;
  191.  
  192.     call->flags |= IPC_CALL_ANSWERED;
  193.  
  194.     spinlock_lock(&callerbox->lock);
  195.     list_append(&call->link, &callerbox->answers);
  196.     spinlock_unlock(&callerbox->lock);
  197.     waitq_wakeup(&callerbox->wq, WAKEUP_FIRST);
  198. }
  199.  
  200. /** Answer a message which is in a callee queue.
  201.  *
  202.  * @param box       Answerbox that is answering the message.
  203.  * @param call      Modified request that is being sent back.
  204.  */
  205. void ipc_answer(answerbox_t *box, call_t *call)
  206. {
  207.     /* Remove from active box */
  208.     spinlock_lock(&box->lock);
  209.     list_remove(&call->link);
  210.     spinlock_unlock(&box->lock);
  211.     /* Send back answer */
  212.     _ipc_answer_free_call(call);
  213. }
  214.  
  215. /** Simulate sending back a message.
  216.  *
  217.  * Most errors are better handled by forming a normal backward
  218.  * message and sending it as a normal answer.
  219.  *
  220.  * @param phone     Phone structure the call should appear to come from.
  221.  * @param call      Call structure to be answered.
  222.  * @param err       Return value to be used for the answer.
  223.  */
  224. void ipc_backsend_err(phone_t *phone, call_t *call, unative_t err)
  225. {
  226.     call->data.phone = phone;
  227.     atomic_inc(&phone->active_calls);
  228.     IPC_SET_RETVAL(call->data, err);
  229.     _ipc_answer_free_call(call);
  230. }
  231.  
  232. /** Unsafe unchecking version of ipc_call.
  233.  *
  234.  * @param phone     Phone structure the call comes from.
  235.  * @param box       Destination answerbox structure.
  236.  * @param call      Call structure with request.
  237.  */
  238. static void _ipc_call(phone_t *phone, answerbox_t *box, call_t *call)
  239. {
  240.     if (!(call->flags & IPC_CALL_FORWARDED)) {
  241.         atomic_inc(&phone->active_calls);
  242.         call->data.phone = phone;
  243.     }
  244.  
  245.     spinlock_lock(&box->lock);
  246.     list_append(&call->link, &box->calls);
  247.     spinlock_unlock(&box->lock);
  248.     waitq_wakeup(&box->wq, WAKEUP_FIRST);
  249. }
  250.  
  251. /** Send an asynchronous request using a phone to an answerbox.
  252.  *
  253.  * @param phone     Phone structure the call comes from and which is
  254.  *          connected to the destination answerbox.
  255.  * @param call      Call structure with request.
  256.  *
  257.  * @return      Return 0 on success, ENOENT on error.
  258.  */
  259. int ipc_call(phone_t *phone, call_t *call)
  260. {
  261.     answerbox_t *box;
  262.  
  263.     spinlock_lock(&phone->lock);
  264.     if (phone->state != IPC_PHONE_CONNECTED) {
  265.         spinlock_unlock(&phone->lock);
  266.         if (call->flags & IPC_CALL_FORWARDED) {
  267.             IPC_SET_RETVAL(call->data, EFORWARD);
  268.             _ipc_answer_free_call(call);
  269.         } else {
  270.             if (phone->state == IPC_PHONE_HUNGUP)
  271.                 ipc_backsend_err(phone, call, EHANGUP);
  272.             else
  273.                 ipc_backsend_err(phone, call, ENOENT);
  274.         }
  275.         return ENOENT;
  276.     }
  277.     box = phone->callee;
  278.     _ipc_call(phone, box, call);
  279.    
  280.     spinlock_unlock(&phone->lock);
  281.     return 0;
  282. }
  283.  
  284. /** Disconnect phone from answerbox.
  285.  *
  286.  * This call leaves the phone in the HUNGUP state. The change to 'free' is done
  287.  * lazily later.
  288.  *
  289.  * @param phone     Phone structure to be hung up.
  290.  *              
  291.  * @return      Return 0 if the phone is disconnected.
  292.  *          Return -1 if the phone was already disconnected.
  293.  */
  294. int ipc_phone_hangup(phone_t *phone)
  295. {
  296.     answerbox_t *box;
  297.     call_t *call;
  298.    
  299.     spinlock_lock(&phone->lock);
  300.     if (phone->state == IPC_PHONE_FREE ||
  301.         phone->state == IPC_PHONE_HUNGUP ||
  302.         phone->state == IPC_PHONE_CONNECTING) {
  303.         spinlock_unlock(&phone->lock);
  304.         return -1;
  305.     }
  306.     box = phone->callee;
  307.     if (phone->state != IPC_PHONE_SLAMMED) {
  308.         /* Remove myself from answerbox */
  309.         spinlock_lock(&box->lock);
  310.         list_remove(&phone->link);
  311.         spinlock_unlock(&box->lock);
  312.  
  313.         if (phone->state != IPC_PHONE_SLAMMED) {
  314.             call = ipc_call_alloc(0);
  315.             IPC_SET_METHOD(call->data, IPC_M_PHONE_HUNGUP);
  316.             call->flags |= IPC_CALL_DISCARD_ANSWER;
  317.             _ipc_call(phone, box, call);
  318.         }
  319.     }
  320.  
  321.     phone->state = IPC_PHONE_HUNGUP;
  322.     spinlock_unlock(&phone->lock);
  323.  
  324.     return 0;
  325. }
  326.  
  327. /** Forwards call from one answerbox to another one.
  328.  *
  329.  * @param call      Call structure to be redirected.
  330.  * @param newphone  Phone structure to target answerbox.
  331.  * @param oldbox    Old answerbox structure.
  332.  * @param mode      Flags that specify mode of the forward operation.
  333.  *
  334.  * @return      Return 0 if forwarding succeeded or an error code if
  335.  *          there was error.
  336.  *
  337.  * The return value serves only as an information for the forwarder,
  338.  * the original caller is notified automatically with EFORWARD.
  339.  */
  340. int ipc_forward(call_t *call, phone_t *newphone, answerbox_t *oldbox, int mode)
  341. {
  342.     spinlock_lock(&oldbox->lock);
  343.     list_remove(&call->link);
  344.     spinlock_unlock(&oldbox->lock);
  345.  
  346.     return ipc_call(newphone, call);
  347. }
  348.  
  349.  
  350. /** Wait for a phone call.
  351.  *
  352.  * @param box       Answerbox expecting the call.
  353.  * @param usec      Timeout in microseconds. See documentation for
  354.  *          waitq_sleep_timeout() for decription of its special
  355.  *          meaning.
  356.  * @param flags     Select mode of sleep operation. See documentation for
  357.  *          waitq_sleep_timeout() for description of its special
  358.  *          meaning.
  359.  * @return      Recived call structure or NULL.
  360.  *
  361.  * To distinguish between a call and an answer, have a look at call->flags.
  362.  */
  363. call_t *ipc_wait_for_call(answerbox_t *box, uint32_t usec, int flags)
  364. {
  365.     call_t *request;
  366.     ipl_t ipl;
  367.     int rc;
  368.  
  369. restart:
  370.     rc = waitq_sleep_timeout(&box->wq, usec, flags);
  371.     if (SYNCH_FAILED(rc))
  372.         return NULL;
  373.    
  374.     spinlock_lock(&box->lock);
  375.     if (!list_empty(&box->irq_notifs)) {
  376.         ipl = interrupts_disable();
  377.         spinlock_lock(&box->irq_lock);
  378.  
  379.         request = list_get_instance(box->irq_notifs.next, call_t, link);
  380.         list_remove(&request->link);
  381.  
  382.         spinlock_unlock(&box->irq_lock);
  383.         interrupts_restore(ipl);
  384.     } else if (!list_empty(&box->answers)) {
  385.         /* Handle asynchronous answers */
  386.         request = list_get_instance(box->answers.next, call_t, link);
  387.         list_remove(&request->link);
  388.         atomic_dec(&request->data.phone->active_calls);
  389.     } else if (!list_empty(&box->calls)) {
  390.         /* Handle requests */
  391.         request = list_get_instance(box->calls.next, call_t, link);
  392.         list_remove(&request->link);
  393.         /* Append request to dispatch queue */
  394.         list_append(&request->link, &box->dispatched_calls);
  395.     } else {
  396.         /* This can happen regularly after ipc_cleanup */
  397.         spinlock_unlock(&box->lock);
  398.         goto restart;
  399.     }
  400.     spinlock_unlock(&box->lock);
  401.     return request;
  402. }
  403.  
  404. /** Answer all calls from list with EHANGUP answer.
  405.  *
  406.  * @param lst       Head of the list to be cleaned up.
  407.  */
  408. static void ipc_cleanup_call_list(link_t *lst)
  409. {
  410.     call_t *call;
  411.  
  412.     while (!list_empty(lst)) {
  413.         call = list_get_instance(lst->next, call_t, link);
  414.         list_remove(&call->link);
  415.  
  416.         IPC_SET_RETVAL(call->data, EHANGUP);
  417.         _ipc_answer_free_call(call);
  418.     }
  419. }
  420.  
  421. /** Cleans up all IPC communication of the current task.
  422.  *
  423.  * Note: ipc_hangup sets returning answerbox to TASK->answerbox, you
  424.  * have to change it as well if you want to cleanup other tasks than TASK.
  425.  */
  426. void ipc_cleanup(void)
  427. {
  428.     int i;
  429.     call_t *call;
  430.     phone_t *phone;
  431.     DEADLOCK_PROBE_INIT(p_phonelck);
  432.  
  433.     /* Disconnect all our phones ('ipc_phone_hangup') */
  434.     for (i = 0; i < IPC_MAX_PHONES; i++)
  435.         ipc_phone_hangup(&TASK->phones[i]);
  436.  
  437.     /* Disconnect all connected irqs */
  438.     ipc_irq_cleanup(&TASK->answerbox);
  439.  
  440.     /* Disconnect all phones connected to our answerbox */
  441. restart_phones:
  442.     spinlock_lock(&TASK->answerbox.lock);
  443.     while (!list_empty(&TASK->answerbox.connected_phones)) {
  444.         phone = list_get_instance(TASK->answerbox.connected_phones.next,
  445.             phone_t, link);
  446.         if (!spinlock_trylock(&phone->lock)) {
  447.             spinlock_unlock(&TASK->answerbox.lock);
  448.             DEADLOCK_PROBE(p_phonelck, DEADLOCK_THRESHOLD);
  449.             goto restart_phones;
  450.         }
  451.        
  452.         /* Disconnect phone */
  453.         ASSERT(phone->state == IPC_PHONE_CONNECTED);
  454.         phone->state = IPC_PHONE_SLAMMED;
  455.         list_remove(&phone->link);
  456.  
  457.         spinlock_unlock(&phone->lock);
  458.     }
  459.  
  460.     /* Answer all messages in 'calls' and 'dispatched_calls' queues */
  461.     ipc_cleanup_call_list(&TASK->answerbox.dispatched_calls);
  462.     ipc_cleanup_call_list(&TASK->answerbox.calls);
  463.     spinlock_unlock(&TASK->answerbox.lock);
  464.    
  465.     /* Wait for all async answers to arrive */
  466.     while (1) {
  467.         /* Go through all phones, until all are FREE... */
  468.         /* Locking not needed, no one else should modify
  469.          * it, when we are in cleanup */
  470.         for (i = 0; i < IPC_MAX_PHONES; i++) {
  471.             if (TASK->phones[i].state == IPC_PHONE_HUNGUP &&
  472.                 atomic_get(&TASK->phones[i].active_calls) == 0)
  473.                 TASK->phones[i].state = IPC_PHONE_FREE;
  474.            
  475.             /* Just for sure, we might have had some
  476.              * IPC_PHONE_CONNECTING phones */
  477.             if (TASK->phones[i].state == IPC_PHONE_CONNECTED)
  478.                 ipc_phone_hangup(&TASK->phones[i]);
  479.             /* If the hangup succeeded, it has sent a HANGUP
  480.              * message, the IPC is now in HUNGUP state, we
  481.              * wait for the reply to come */
  482.            
  483.             if (TASK->phones[i].state != IPC_PHONE_FREE)
  484.                 break;
  485.         }
  486.         /* Voila, got into cleanup */
  487.         if (i == IPC_MAX_PHONES)
  488.             break;
  489.        
  490.         call = ipc_wait_for_call(&TASK->answerbox, SYNCH_NO_TIMEOUT,
  491.             SYNCH_FLAGS_NONE);
  492.         ASSERT((call->flags & IPC_CALL_ANSWERED) ||
  493.             (call->flags & IPC_CALL_NOTIF));
  494.         ASSERT(!(call->flags & IPC_CALL_STATIC_ALLOC));
  495.        
  496.         atomic_dec(&TASK->active_calls);
  497.         ipc_call_free(call);
  498.     }
  499. }
  500.  
  501.  
  502. /** Initilize IPC subsystem */
  503. void ipc_init(void)
  504. {
  505.     ipc_call_slab = slab_cache_create("ipc_call", sizeof(call_t), 0, NULL,
  506.         NULL, 0);
  507. }
  508.  
  509.  
  510. /** List answerbox contents.
  511.  *
  512.  * @param taskid    Task ID.
  513.  */
  514. void ipc_print_task(task_id_t taskid)
  515. {
  516.     task_t *task;
  517.     int i;
  518.     call_t *call;
  519.     link_t *tmp;
  520.    
  521.     spinlock_lock(&tasks_lock);
  522.     task = task_find_by_id(taskid);
  523.     if (task)
  524.         spinlock_lock(&task->lock);
  525.     spinlock_unlock(&tasks_lock);
  526.     if (!task)
  527.         return;
  528.  
  529.     /* Print opened phones & details */
  530.     printf("PHONE:\n");
  531.     for (i = 0; i < IPC_MAX_PHONES; i++) {
  532.         spinlock_lock(&task->phones[i].lock);
  533.         if (task->phones[i].state != IPC_PHONE_FREE) {
  534.             printf("%d: ", i);
  535.             switch (task->phones[i].state) {
  536.             case IPC_PHONE_CONNECTING:
  537.                 printf("connecting ");
  538.                 break;
  539.             case IPC_PHONE_CONNECTED:
  540.                 printf("connected to: %p ",
  541.                        task->phones[i].callee);
  542.                 break;
  543.             case IPC_PHONE_SLAMMED:
  544.                 printf("slammed by: %p ",
  545.                        task->phones[i].callee);
  546.                 break;
  547.             case IPC_PHONE_HUNGUP:
  548.                 printf("hung up - was: %p ",
  549.                        task->phones[i].callee);
  550.                 break;
  551.             default:
  552.                 break;
  553.             }
  554.             printf("active: %d\n",
  555.                 atomic_get(&task->phones[i].active_calls));
  556.         }
  557.         spinlock_unlock(&task->phones[i].lock);
  558.     }
  559.  
  560.  
  561.     /* Print answerbox - calls */
  562.     spinlock_lock(&task->answerbox.lock);
  563.     printf("ABOX - CALLS:\n");
  564.     for (tmp = task->answerbox.calls.next; tmp != &task->answerbox.calls;
  565.         tmp = tmp->next) {
  566.         call = list_get_instance(tmp, call_t, link);
  567.         printf("Callid: %p Srctask:%llu M:%d A1:%d A2:%d A3:%d "
  568.             "Flags:%x\n", call, call->sender->taskid,
  569.             IPC_GET_METHOD(call->data), IPC_GET_ARG1(call->data),
  570.             IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data),
  571.             call->flags);
  572.     }
  573.     /* Print answerbox - calls */
  574.     printf("ABOX - DISPATCHED CALLS:\n");
  575.     for (tmp=task->answerbox.dispatched_calls.next;
  576.          tmp != &task->answerbox.dispatched_calls;
  577.          tmp = tmp->next) {
  578.         call = list_get_instance(tmp, call_t, link);
  579.         printf("Callid: %p Srctask:%llu M:%d A1:%d A2:%d A3:%d "
  580.             "Flags:%x\n", call, call->sender->taskid,
  581.             IPC_GET_METHOD(call->data), IPC_GET_ARG1(call->data),
  582.             IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data),
  583.             call->flags);
  584.     }
  585.     /* Print answerbox - calls */
  586.     printf("ABOX - ANSWERS:\n");
  587.     for (tmp = task->answerbox.answers.next; tmp != &task->answerbox.answers;
  588.         tmp = tmp->next) {
  589.         call = list_get_instance(tmp, call_t, link);
  590.         printf("Callid:%p M:%d A1:%d A2:%d A3:%d Flags:%x\n", call,
  591.             IPC_GET_METHOD(call->data), IPC_GET_ARG1(call->data),
  592.             IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data),
  593.             call->flags);
  594.     }
  595.  
  596.     spinlock_unlock(&task->answerbox.lock);
  597.     spinlock_unlock(&task->lock);
  598. }
  599.  
  600. /** @}
  601.  */
  602.