Subversion Repositories HelenOS-historic

Rev

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