Subversion Repositories HelenOS

Rev

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