Subversion Repositories HelenOS

Rev

Rev 3042 | Rev 3425 | 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/synch.h>
  41. #include <synch/spinlock.h>
  42. #include <synch/mutex.h>
  43. #include <synch/waitq.h>
  44. #include <synch/synch.h>
  45. #include <ipc/ipc.h>
  46. #include <errno.h>
  47. #include <mm/slab.h>
  48. #include <arch.h>
  49. #include <proc/task.h>
  50. #include <memstr.h>
  51. #include <debug.h>
  52.  
  53. #include <print.h>
  54. #include <console/console.h>
  55. #include <proc/thread.h>
  56. #include <arch/interrupt.h>
  57. #include <ipc/irq.h>
  58.  
  59. /** Open channel that is assigned automatically to new tasks */
  60. answerbox_t *ipc_phone_0 = NULL;
  61.  
  62. static slab_cache_t *ipc_call_slab;
  63.  
  64. /** Initialize a call structure.
  65.  *
  66.  * @param call      Call structure to be initialized.
  67.  */
  68. static void _ipc_call_init(call_t *call)
  69. {
  70.     memsetb(call, sizeof(*call), 0);
  71.     call->callerbox = &TASK->answerbox;
  72.     call->sender = TASK;
  73.     call->buffer = NULL;
  74. }
  75.  
  76. /** Allocate and initialize a call structure.
  77.  *
  78.  * The call is initialized, so that the reply will be directed to
  79.  * TASK->answerbox.
  80.  *
  81.  * @param flags     Parameters for slab_alloc (e.g FRAME_ATOMIC).
  82.  *
  83.  * @return      If flags permit it, return NULL, or initialized kernel
  84.  *          call structure.
  85.  */
  86. call_t *ipc_call_alloc(int flags)
  87. {
  88.     call_t *call;
  89.  
  90.     call = slab_alloc(ipc_call_slab, flags);
  91.     _ipc_call_init(call);
  92.  
  93.     return call;
  94. }
  95.  
  96. /** Initialize a statically allocated call structure.
  97.  *
  98.  * @param call      Statically allocated kernel call structure to be
  99.  *          initialized.
  100.  */
  101. void ipc_call_static_init(call_t *call)
  102. {
  103.     _ipc_call_init(call);
  104.     call->flags |= IPC_CALL_STATIC_ALLOC;
  105. }
  106.  
  107. /** Deallocate a call structure.
  108.  *
  109.  * @param call      Call structure to be freed.
  110.  */
  111. void ipc_call_free(call_t *call)
  112. {
  113.     ASSERT(!(call->flags & IPC_CALL_STATIC_ALLOC));
  114.     /* Check to see if we have data in the IPC_M_DATA_SEND buffer. */
  115.     if (call->buffer)
  116.         free(call->buffer);
  117.     slab_free(ipc_call_slab, call);
  118. }
  119.  
  120. /** Initialize an answerbox structure.
  121.  *
  122.  * @param box       Answerbox structure to be initialized.
  123.  * @param task      Task to which the answerbox belongs.
  124.  */
  125. void ipc_answerbox_init(answerbox_t *box, task_t *task)
  126. {
  127.     spinlock_initialize(&box->lock, "ipc_box_lock");
  128.     spinlock_initialize(&box->irq_lock, "ipc_box_irqlock");
  129.     waitq_initialize(&box->wq);
  130.     list_initialize(&box->connected_phones);
  131.     list_initialize(&box->calls);
  132.     list_initialize(&box->dispatched_calls);
  133.     list_initialize(&box->answers);
  134.     list_initialize(&box->irq_notifs);
  135.     list_initialize(&box->irq_head);
  136.     box->task = task;
  137. }
  138.  
  139. /** Connect a phone to an answerbox.
  140.  *
  141.  * @param phone     Initialized phone structure.
  142.  * @param box       Initialized answerbox structure.
  143.  */
  144. void ipc_phone_connect(phone_t *phone, answerbox_t *box)
  145. {
  146.     mutex_lock(&phone->lock);
  147.  
  148.     phone->state = IPC_PHONE_CONNECTED;
  149.     phone->callee = box;
  150.  
  151.     spinlock_lock(&box->lock);
  152.     list_append(&phone->link, &box->connected_phones);
  153.     spinlock_unlock(&box->lock);
  154.  
  155.     mutex_unlock(&phone->lock);
  156. }
  157.  
  158. /** Initialize a phone structure.
  159.  *
  160.  * @param phone     Phone structure to be initialized.
  161.  */
  162. void ipc_phone_init(phone_t *phone)
  163. {
  164.     mutex_initialize(&phone->lock);
  165.     phone->callee = NULL;
  166.     phone->state = IPC_PHONE_FREE;
  167.     atomic_set(&phone->active_calls, 0);
  168. }
  169.  
  170. /** Helper function to facilitate synchronous calls.
  171.  *
  172.  * @param phone     Destination kernel phone structure.
  173.  * @param request   Call structure with request.
  174.  */
  175. void ipc_call_sync(phone_t *phone, call_t *request)
  176. {
  177.     answerbox_t sync_box;
  178.  
  179.     ipc_answerbox_init(&sync_box, TASK);
  180.  
  181.     /* We will receive data in a special box. */
  182.     request->callerbox = &sync_box;
  183.  
  184.     ipc_call(phone, request);
  185.     ipc_wait_for_call(&sync_box, SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE);
  186. }
  187.  
  188. /** Answer a message which was not dispatched and is not listed in any queue.
  189.  *
  190.  * @param call      Call structure to be answered.
  191.  */
  192. static void _ipc_answer_free_call(call_t *call)
  193. {
  194.     answerbox_t *callerbox = call->callerbox;
  195.  
  196.     call->flags |= IPC_CALL_ANSWERED;
  197.  
  198.     spinlock_lock(&callerbox->lock);
  199.     list_append(&call->link, &callerbox->answers);
  200.     spinlock_unlock(&callerbox->lock);
  201.     waitq_wakeup(&callerbox->wq, WAKEUP_FIRST);
  202. }
  203.  
  204. /** Answer a message which is in a callee queue.
  205.  *
  206.  * @param box       Answerbox that is answering the message.
  207.  * @param call      Modified request that is being sent back.
  208.  */
  209. void ipc_answer(answerbox_t *box, call_t *call)
  210. {
  211.     /* Remove from active box */
  212.     spinlock_lock(&box->lock);
  213.     list_remove(&call->link);
  214.     spinlock_unlock(&box->lock);
  215.     /* Send back answer */
  216.     _ipc_answer_free_call(call);
  217. }
  218.  
  219. /** Simulate sending back a message.
  220.  *
  221.  * Most errors are better handled by forming a normal backward
  222.  * message and sending it as a normal answer.
  223.  *
  224.  * @param phone     Phone structure the call should appear to come from.
  225.  * @param call      Call structure to be answered.
  226.  * @param err       Return value to be used for the answer.
  227.  */
  228. void ipc_backsend_err(phone_t *phone, call_t *call, unative_t err)
  229. {
  230.     call->data.phone = phone;
  231.     atomic_inc(&phone->active_calls);
  232.     IPC_SET_RETVAL(call->data, err);
  233.     _ipc_answer_free_call(call);
  234. }
  235.  
  236. /** Unsafe unchecking version of ipc_call.
  237.  *
  238.  * @param phone     Phone structure the call comes from.
  239.  * @param box       Destination answerbox structure.
  240.  * @param call      Call structure with request.
  241.  */
  242. static void _ipc_call(phone_t *phone, answerbox_t *box, call_t *call)
  243. {
  244.     if (!(call->flags & IPC_CALL_FORWARDED)) {
  245.         atomic_inc(&phone->active_calls);
  246.         call->data.phone = phone;
  247.     }
  248.  
  249.     spinlock_lock(&box->lock);
  250.     list_append(&call->link, &box->calls);
  251.     spinlock_unlock(&box->lock);
  252.     waitq_wakeup(&box->wq, WAKEUP_FIRST);
  253. }
  254.  
  255. /** Send an asynchronous request using a phone to an answerbox.
  256.  *
  257.  * @param phone     Phone structure the call comes from and which is
  258.  *          connected to the destination answerbox.
  259.  * @param call      Call structure with request.
  260.  *
  261.  * @return      Return 0 on success, ENOENT on error.
  262.  */
  263. int ipc_call(phone_t *phone, call_t *call)
  264. {
  265.     answerbox_t *box;
  266.  
  267.     mutex_lock(&phone->lock);
  268.     if (phone->state != IPC_PHONE_CONNECTED) {
  269.         mutex_unlock(&phone->lock);
  270.         if (call->flags & IPC_CALL_FORWARDED) {
  271.             IPC_SET_RETVAL(call->data, EFORWARD);
  272.             _ipc_answer_free_call(call);
  273.         } else {
  274.             if (phone->state == IPC_PHONE_HUNGUP)
  275.                 ipc_backsend_err(phone, call, EHANGUP);
  276.             else
  277.                 ipc_backsend_err(phone, call, ENOENT);
  278.         }
  279.         return ENOENT;
  280.     }
  281.     box = phone->callee;
  282.     _ipc_call(phone, box, call);
  283.    
  284.     mutex_unlock(&phone->lock);
  285.     return 0;
  286. }
  287.  
  288. /** Disconnect phone from answerbox.
  289.  *
  290.  * This call leaves the phone in the HUNGUP state. The change to 'free' is done
  291.  * lazily later.
  292.  *
  293.  * @param phone     Phone structure to be hung up.
  294.  *              
  295.  * @return      Return 0 if the phone is disconnected.
  296.  *          Return -1 if the phone was already disconnected.
  297.  */
  298. int ipc_phone_hangup(phone_t *phone)
  299. {
  300.     answerbox_t *box;
  301.     call_t *call;
  302.    
  303.     mutex_lock(&phone->lock);
  304.     if (phone->state == IPC_PHONE_FREE ||
  305.         phone->state == IPC_PHONE_HUNGUP ||
  306.         phone->state == IPC_PHONE_CONNECTING) {
  307.         mutex_unlock(&phone->lock);
  308.         return -1;
  309.     }
  310.     box = phone->callee;
  311.     if (phone->state != IPC_PHONE_SLAMMED) {
  312.         /* Remove myself from answerbox */
  313.         spinlock_lock(&box->lock);
  314.         list_remove(&phone->link);
  315.         spinlock_unlock(&box->lock);
  316.  
  317.         if (phone->state != IPC_PHONE_SLAMMED) {
  318.             call = ipc_call_alloc(0);
  319.             IPC_SET_METHOD(call->data, IPC_M_PHONE_HUNGUP);
  320.             call->flags |= IPC_CALL_DISCARD_ANSWER;
  321.             _ipc_call(phone, box, call);
  322.         }
  323.     }
  324.  
  325.     phone->state = IPC_PHONE_HUNGUP;
  326.     mutex_unlock(&phone->lock);
  327.  
  328.     return 0;
  329. }
  330.  
  331. /** Forwards call from one answerbox to another one.
  332.  *
  333.  * @param call      Call structure to be redirected.
  334.  * @param newphone  Phone structure to target answerbox.
  335.  * @param oldbox    Old answerbox structure.
  336.  * @param mode      Flags that specify mode of the forward operation.
  337.  *
  338.  * @return      Return 0 if forwarding succeeded or an error code if
  339.  *          there was error.
  340.  *
  341.  * The return value serves only as an information for the forwarder,
  342.  * the original caller is notified automatically with EFORWARD.
  343.  */
  344. int ipc_forward(call_t *call, phone_t *newphone, answerbox_t *oldbox, int mode)
  345. {
  346.     spinlock_lock(&oldbox->lock);
  347.     list_remove(&call->link);
  348.     spinlock_unlock(&oldbox->lock);
  349.  
  350.     if (mode & IPC_FF_ROUTE_FROM_ME)
  351.         call->data.phone = newphone;
  352.  
  353.     return ipc_call(newphone, call);
  354. }
  355.  
  356.  
  357. /** Wait for a phone call.
  358.  *
  359.  * @param box       Answerbox expecting the call.
  360.  * @param usec      Timeout in microseconds. See documentation for
  361.  *          waitq_sleep_timeout() for decription of its special
  362.  *          meaning.
  363.  * @param flags     Select mode of sleep operation. See documentation for
  364.  *          waitq_sleep_timeout() for description of its special
  365.  *          meaning.
  366.  * @return      Recived call structure or NULL.
  367.  *
  368.  * To distinguish between a call and an answer, have a look at call->flags.
  369.  */
  370. call_t *ipc_wait_for_call(answerbox_t *box, uint32_t usec, int flags)
  371. {
  372.     call_t *request;
  373.     ipl_t ipl;
  374.     int rc;
  375.  
  376. restart:
  377.     rc = waitq_sleep_timeout(&box->wq, usec, flags);
  378.     if (SYNCH_FAILED(rc))
  379.         return NULL;
  380.    
  381.     spinlock_lock(&box->lock);
  382.     if (!list_empty(&box->irq_notifs)) {
  383.         ipl = interrupts_disable();
  384.         spinlock_lock(&box->irq_lock);
  385.  
  386.         request = list_get_instance(box->irq_notifs.next, call_t, link);
  387.         list_remove(&request->link);
  388.  
  389.         spinlock_unlock(&box->irq_lock);
  390.         interrupts_restore(ipl);
  391.     } else if (!list_empty(&box->answers)) {
  392.         /* Handle asynchronous answers */
  393.         request = list_get_instance(box->answers.next, call_t, link);
  394.         list_remove(&request->link);
  395.         atomic_dec(&request->data.phone->active_calls);
  396.     } else if (!list_empty(&box->calls)) {
  397.         /* Handle requests */
  398.         request = list_get_instance(box->calls.next, call_t, link);
  399.         list_remove(&request->link);
  400.         /* Append request to dispatch queue */
  401.         list_append(&request->link, &box->dispatched_calls);
  402.     } else {
  403.         /* This can happen regularly after ipc_cleanup */
  404.         spinlock_unlock(&box->lock);
  405.         goto restart;
  406.     }
  407.     spinlock_unlock(&box->lock);
  408.     return request;
  409. }
  410.  
  411. /** Answer all calls from list with EHANGUP answer.
  412.  *
  413.  * @param lst       Head of the list to be cleaned up.
  414.  */
  415. static void ipc_cleanup_call_list(link_t *lst)
  416. {
  417.     call_t *call;
  418.  
  419.     while (!list_empty(lst)) {
  420.         call = list_get_instance(lst->next, call_t, link);
  421.         if (call->buffer)
  422.             free(call->buffer);
  423.         list_remove(&call->link);
  424.  
  425.         IPC_SET_RETVAL(call->data, EHANGUP);
  426.         _ipc_answer_free_call(call);
  427.     }
  428. }
  429.  
  430. /** Disconnects all phones connected to an answerbox.
  431.  *
  432.  * @param box       Answerbox to disconnect phones from.
  433.  * @param notify_box    If true, the answerbox will get a hangup message for
  434.  *          each disconnected phone.
  435.  */
  436. static void ipc_answerbox_slam_phones(answerbox_t *box, bool notify_box)
  437. {
  438.     phone_t *phone;
  439.     DEADLOCK_PROBE_INIT(p_phonelck);
  440.     ipl_t ipl;
  441.     call_t *call;
  442.  
  443.     call = ipc_call_alloc(0);
  444.  
  445.     /* Disconnect all phones connected to our answerbox */
  446. restart_phones:
  447.     ipl = interrupts_disable();
  448.     spinlock_lock(&box->lock);
  449.     while (!list_empty(&box->connected_phones)) {
  450.         phone = list_get_instance(box->connected_phones.next,
  451.             phone_t, link);
  452.         if (SYNCH_FAILED(mutex_trylock(&phone->lock))) {
  453.             spinlock_unlock(&box->lock);
  454.             interrupts_restore(ipl);
  455.             DEADLOCK_PROBE(p_phonelck, DEADLOCK_THRESHOLD);
  456.             goto restart_phones;
  457.         }
  458.        
  459.         /* Disconnect phone */
  460.         ASSERT(phone->state == IPC_PHONE_CONNECTED);
  461.  
  462.         list_remove(&phone->link);
  463.         phone->state = IPC_PHONE_SLAMMED;
  464.  
  465.         if (notify_box) {
  466.             mutex_unlock(&phone->lock);
  467.             spinlock_unlock(&box->lock);
  468.             interrupts_restore(ipl);
  469.  
  470.             /*
  471.              * Send one message to the answerbox for each
  472.              * phone. Used to make sure the kbox thread
  473.              * wakes up after the last phone has been
  474.              * disconnected.
  475.              */
  476.             IPC_SET_METHOD(call->data, IPC_M_PHONE_HUNGUP);
  477.             call->flags |= IPC_CALL_DISCARD_ANSWER;
  478.             _ipc_call(phone, box, call);
  479.  
  480.             /* Allocate another call in advance */
  481.             call = ipc_call_alloc(0);
  482.  
  483.             /* Must start again */
  484.             goto restart_phones;
  485.         }
  486.  
  487.         mutex_unlock(&phone->lock);
  488.     }
  489.  
  490.     spinlock_unlock(&box->lock);
  491.     interrupts_restore(ipl);
  492.  
  493.     /* Free unused call */
  494.     if (call) ipc_call_free(call);
  495. }
  496.  
  497. static void ipc_kbox_cleanup()
  498. {
  499.     bool have_kb_thread;
  500.  
  501.     /* Only hold kb_cleanup_lock while setting kb_finished - this is enough */
  502.     mutex_lock(&TASK->kb_cleanup_lock);
  503.     TASK->kb_finished = true;
  504.     mutex_unlock(&TASK->kb_cleanup_lock);
  505.  
  506.     have_kb_thread = (TASK->kb_thread != NULL);
  507.  
  508.     /* From now on nobody will try to connect phones or attach kbox threads */
  509.  
  510.     /*
  511.      * Disconnect all phones connected to our kbox. Passing true for
  512.      * notify_box causes a HANGUP message to be inserted for each
  513.      * disconnected phone. This ensures the kbox thread is going to
  514.      * wake up and terminate.
  515.      */
  516.     ipc_answerbox_slam_phones(&TASK->kernel_box, have_kb_thread);
  517.    
  518.     if (have_kb_thread) {
  519.         printf("join kb_thread..\n");
  520.         thread_join(TASK->kb_thread);
  521.         thread_detach(TASK->kb_thread);
  522.         printf("join done\n");
  523.         TASK->kb_thread = NULL;
  524.     }
  525.  
  526.     /* Answer all messages in 'calls' and 'dispatched_calls' queues */
  527.     spinlock_lock(&TASK->kernel_box.lock);
  528.     ipc_cleanup_call_list(&TASK->kernel_box.dispatched_calls);
  529.     ipc_cleanup_call_list(&TASK->kernel_box.calls);
  530.     spinlock_unlock(&TASK->kernel_box.lock);
  531. }
  532.  
  533.  
  534. /** Cleans up all IPC communication of the current task.
  535.  *
  536.  * Note: ipc_hangup sets returning answerbox to TASK->answerbox, you
  537.  * have to change it as well if you want to cleanup other tasks than TASK.
  538.  */
  539. void ipc_cleanup(void)
  540. {
  541.     int i;
  542.     call_t *call;
  543.  
  544.     /* Disconnect all our phones ('ipc_phone_hangup') */
  545.     for (i = 0; i < IPC_MAX_PHONES; i++)
  546.         ipc_phone_hangup(&TASK->phones[i]);
  547.  
  548.     /* Disconnect all connected irqs */
  549.     ipc_irq_cleanup(&TASK->answerbox);
  550.  
  551.     /* Disconnect all phones connected to our regular answerbox */
  552.     ipc_answerbox_slam_phones(&TASK->answerbox, false);
  553.  
  554.     /* Clean up kbox thread and communications */
  555.     ipc_kbox_cleanup();
  556.  
  557.     /* Answer all messages in 'calls' and 'dispatched_calls' queues */
  558.     spinlock_lock(&TASK->answerbox.lock);
  559.     ipc_cleanup_call_list(&TASK->answerbox.dispatched_calls);
  560.     ipc_cleanup_call_list(&TASK->answerbox.calls);
  561.     spinlock_unlock(&TASK->answerbox.lock);
  562.    
  563.     /* Wait for all async answers to arrive */
  564.     while (1) {
  565.         /* Go through all phones, until all are FREE... */
  566.         /* Locking not needed, no one else should modify
  567.          * it, when we are in cleanup */
  568.         for (i = 0; i < IPC_MAX_PHONES; i++) {
  569.             if (TASK->phones[i].state == IPC_PHONE_HUNGUP &&
  570.                 atomic_get(&TASK->phones[i].active_calls) == 0)
  571.                 TASK->phones[i].state = IPC_PHONE_FREE;
  572.            
  573.             /* Just for sure, we might have had some
  574.              * IPC_PHONE_CONNECTING phones */
  575.             if (TASK->phones[i].state == IPC_PHONE_CONNECTED)
  576.                 ipc_phone_hangup(&TASK->phones[i]);
  577.             /* If the hangup succeeded, it has sent a HANGUP
  578.              * message, the IPC is now in HUNGUP state, we
  579.              * wait for the reply to come */
  580.            
  581.             if (TASK->phones[i].state != IPC_PHONE_FREE)
  582.                 break;
  583.         }
  584.         /* Voila, got into cleanup */
  585.         if (i == IPC_MAX_PHONES)
  586.             break;
  587.        
  588.         call = ipc_wait_for_call(&TASK->answerbox, SYNCH_NO_TIMEOUT,
  589.             SYNCH_FLAGS_NONE);
  590.         ASSERT((call->flags & IPC_CALL_ANSWERED) ||
  591.             (call->flags & IPC_CALL_NOTIF));
  592.         ASSERT(!(call->flags & IPC_CALL_STATIC_ALLOC));
  593.        
  594.         atomic_dec(&TASK->active_calls);
  595.         ipc_call_free(call);
  596.     }
  597. }
  598.  
  599.  
  600. /** Initilize IPC subsystem */
  601. void ipc_init(void)
  602. {
  603.     ipc_call_slab = slab_cache_create("ipc_call", sizeof(call_t), 0, NULL,
  604.         NULL, 0);
  605. }
  606.  
  607.  
  608. /** List answerbox contents.
  609.  *
  610.  * @param taskid    Task ID.
  611.  */
  612. void ipc_print_task(task_id_t taskid)
  613. {
  614.     task_t *task;
  615.     int i;
  616.     call_t *call;
  617.     link_t *tmp;
  618.    
  619.     spinlock_lock(&tasks_lock);
  620.     task = task_find_by_id(taskid);
  621.     if (task)
  622.         spinlock_lock(&task->lock);
  623.     spinlock_unlock(&tasks_lock);
  624.     if (!task)
  625.         return;
  626.  
  627.     /* Print opened phones & details */
  628.     printf("PHONE:\n");
  629.     for (i = 0; i < IPC_MAX_PHONES; i++) {
  630.         if (SYNCH_FAILED(mutex_trylock(&task->phones[i].lock))) {
  631.             printf("%d: mutex busy\n", i);
  632.             continue;
  633.         }
  634.         if (task->phones[i].state != IPC_PHONE_FREE) {
  635.             printf("%d: ", i);
  636.             switch (task->phones[i].state) {
  637.             case IPC_PHONE_CONNECTING:
  638.                 printf("connecting ");
  639.                 break;
  640.             case IPC_PHONE_CONNECTED:
  641.                 printf("connected to: %p ",
  642.                        task->phones[i].callee);
  643.                 break;
  644.             case IPC_PHONE_SLAMMED:
  645.                 printf("slammed by: %p ",
  646.                        task->phones[i].callee);
  647.                 break;
  648.             case IPC_PHONE_HUNGUP:
  649.                 printf("hung up - was: %p ",
  650.                        task->phones[i].callee);
  651.                 break;
  652.             default:
  653.                 break;
  654.             }
  655.             printf("active: %ld\n",
  656.                 atomic_get(&task->phones[i].active_calls));
  657.         }
  658.         mutex_unlock(&task->phones[i].lock);
  659.     }
  660.  
  661.  
  662.     /* Print answerbox - calls */
  663.     spinlock_lock(&task->answerbox.lock);
  664.     printf("ABOX - CALLS:\n");
  665.     for (tmp = task->answerbox.calls.next; tmp != &task->answerbox.calls;
  666.         tmp = tmp->next) {
  667.         call = list_get_instance(tmp, call_t, link);
  668.         printf("Callid: %p Srctask:%" PRIu64 " M:%" PRIun
  669.             " A1:%" PRIun " A2:%" PRIun " A3:%" PRIun
  670.             " A4:%" PRIun " A5:%" PRIun " Flags:%x\n", call, call->sender->taskid,
  671.             IPC_GET_METHOD(call->data), IPC_GET_ARG1(call->data),
  672.             IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data),
  673.             IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data),
  674.             call->flags);
  675.     }
  676.     /* Print answerbox - calls */
  677.     printf("ABOX - DISPATCHED CALLS:\n");
  678.     for (tmp = task->answerbox.dispatched_calls.next;
  679.         tmp != &task->answerbox.dispatched_calls;
  680.         tmp = tmp->next) {
  681.         call = list_get_instance(tmp, call_t, link);
  682.         printf("Callid: %p Srctask:%" PRIu64 " M:%" PRIun
  683.             " A1:%" PRIun " A2:%" PRIun " A3:%" PRIun
  684.             " A4:%" PRIun " A5:%" PRIun " Flags:%x\n", call, call->sender->taskid,
  685.             IPC_GET_METHOD(call->data), IPC_GET_ARG1(call->data),
  686.             IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data),
  687.             IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data),
  688.             call->flags);
  689.     }
  690.     /* Print answerbox - calls */
  691.     printf("ABOX - ANSWERS:\n");
  692.     for (tmp = task->answerbox.answers.next; tmp != &task->answerbox.answers;
  693.         tmp = tmp->next) {
  694.         call = list_get_instance(tmp, call_t, link);
  695.         printf("Callid:%p M:%" PRIun " A1:%" PRIun " A2:%" PRIun
  696.             " A3:%" PRIun " A4:%" PRIun " A5:%" PRIun " Flags:%x\n",
  697.             call, IPC_GET_METHOD(call->data), IPC_GET_ARG1(call->data),
  698.             IPC_GET_ARG2(call->data), IPC_GET_ARG3(call->data),
  699.             IPC_GET_ARG4(call->data), IPC_GET_ARG5(call->data),
  700.             call->flags);
  701.     }
  702.  
  703.     spinlock_unlock(&task->answerbox.lock);
  704.     spinlock_unlock(&task->lock);
  705. }
  706.  
  707. #include <ipc/ipcrsc.h>
  708. #include <print.h>
  709. #include <udebug/udebug_ipc.h>
  710.  
  711. static void kbox_thread_proc(void *arg)
  712. {
  713.     call_t *call;
  714.     int method;
  715.     bool done;
  716.     ipl_t ipl;
  717.  
  718.     (void)arg;
  719.     printf("kbox_thread_proc()\n");
  720.     done = false;
  721.  
  722.     while (!done) {
  723.         //printf("kbox: wait for call\n");
  724.         call = ipc_wait_for_call(&TASK->kernel_box, SYNCH_NO_TIMEOUT,
  725.             SYNCH_FLAGS_NONE);
  726.  
  727.         if (call != NULL) {
  728.             method = IPC_GET_METHOD(call->data);
  729.  
  730.             if (method == IPC_M_DEBUG_ALL) {
  731.                 udebug_call_receive(call);
  732.             }
  733.  
  734.             if (method == IPC_M_PHONE_HUNGUP) {
  735.                 printf("kbox: handle hangup message\n");
  736.  
  737.                 /* Was it our debugger, who hung up? */
  738.                 if (call->sender == TASK->udebug.debugger) {
  739.                     /* Terminate debugging session (if any) */
  740.                     printf("kbox: terminate debug session\n");
  741.                     ipl = interrupts_disable();
  742.                     spinlock_lock(&TASK->lock);
  743.                     udebug_task_cleanup(TASK);
  744.                     spinlock_unlock(&TASK->lock);
  745.                     interrupts_restore(ipl);
  746.                 } else {
  747.                     printf("kbox: was not debugger\n");
  748.                 }
  749.  
  750.                 printf("kbox: continue with hangup message\n");
  751.                 IPC_SET_RETVAL(call->data, 0);
  752.                 ipc_answer(&TASK->kernel_box, call);
  753.  
  754.                 ipl = interrupts_disable();
  755.                 spinlock_lock(&TASK->lock);
  756.                 spinlock_lock(&TASK->answerbox.lock);
  757.                 if (list_empty(&TASK->answerbox.connected_phones)) {
  758.                     /* Last phone has been disconnected */
  759.                     TASK->kb_thread = NULL;
  760.                     done = true;
  761.                     printf("phone list is empty\n");
  762.                 }
  763.                 spinlock_unlock(&TASK->answerbox.lock);
  764.                 spinlock_unlock(&TASK->lock);
  765.                 interrupts_restore(ipl);
  766.             }
  767.         }
  768.     }
  769.  
  770.     printf("kbox: finished\n");
  771. }
  772.  
  773.  
  774. /**
  775.  * Connect phone to a task kernel-box specified by id.
  776.  *
  777.  * Note that this is not completely atomic. For optimisation reasons,
  778.  * The task might start cleaning up kbox after the phone has been connected
  779.  * and before a kbox thread has been created. This must be taken into account
  780.  * in the cleanup code.
  781.  *
  782.  * @return      Phone id on success, or negative error code.
  783.  */
  784. int ipc_connect_kbox(task_id_t taskid)
  785. {
  786.     int newphid;
  787.     task_t *ta;
  788.     thread_t *kb_thread;
  789.     ipl_t ipl;
  790.  
  791.     ipl = interrupts_disable();
  792.     spinlock_lock(&tasks_lock);
  793.  
  794.     ta = task_find_by_id(taskid);
  795.     if (ta == NULL) {
  796.         spinlock_unlock(&tasks_lock);
  797.         interrupts_restore(ipl);
  798.         return ENOENT;
  799.     }
  800.  
  801.     atomic_inc(&ta->refcount);
  802.  
  803.     spinlock_unlock(&tasks_lock);
  804.     interrupts_restore(ipl);
  805.  
  806.     mutex_lock(&ta->kb_cleanup_lock);
  807.  
  808.     if (atomic_predec(&ta->refcount) == 0) {
  809.         mutex_unlock(&ta->kb_cleanup_lock);
  810.         task_destroy(ta);
  811.         return ENOENT;
  812.     }
  813.  
  814.     if (ta->kb_finished != false) {
  815.         mutex_unlock(&ta->kb_cleanup_lock);
  816.         return EINVAL;
  817.     }
  818.  
  819.     newphid = phone_alloc();
  820.     if (newphid < 0) {
  821.         mutex_unlock(&ta->kb_cleanup_lock);
  822.         return ELIMIT;
  823.     }
  824.  
  825.     /* Connect the newly allocated phone to the kbox */
  826.     ipc_phone_connect(&TASK->phones[newphid], &ta->kernel_box);
  827.  
  828.     if (ta->kb_thread != NULL) {
  829.         mutex_unlock(&ta->kb_cleanup_lock);
  830.         return newphid;
  831.     }
  832.  
  833.     /* Create a kbox thread */
  834.     kb_thread = thread_create(kbox_thread_proc, NULL, ta, 0, "kbox", false);
  835.     if (!kb_thread) {
  836.         mutex_unlock(&ta->kb_cleanup_lock);
  837.         return ENOMEM;
  838.     }
  839.  
  840.     ta->kb_thread = kb_thread;
  841.     thread_ready(kb_thread);
  842.  
  843.     mutex_unlock(&ta->kb_cleanup_lock);
  844.  
  845.     return newphid;
  846. }
  847.  
  848. /** @}
  849.  */
  850.