Subversion Repositories HelenOS

Rev

Rev 3665 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (c) 2008 Jiri Svoboda
  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. #include <synch/synch.h>
  36. #include <synch/spinlock.h>
  37. #include <synch/mutex.h>
  38. #include <ipc/ipc.h>
  39. #include <ipc/ipcrsc.h>
  40. #include <arch.h>
  41. #include <arch/asm.h>
  42. #include <errno.h>
  43. #include <debug.h>
  44. #include <print.h>
  45. #include <udebug/udebug_ipc.h>
  46. #include <ipc/kbox.h>
  47.  
  48. void ipc_kbox_cleanup(void)
  49. {
  50.     ipl_t ipl;
  51.     bool have_kb_thread;
  52.  
  53.     /*
  54.      * Only hold kb.cleanup_lock while setting kb.finished -
  55.      * this is enough.
  56.      */
  57.     mutex_lock(&TASK->kb.cleanup_lock);
  58.     TASK->kb.finished = true;
  59.     mutex_unlock(&TASK->kb.cleanup_lock);
  60.  
  61.     have_kb_thread = (TASK->kb.thread != NULL);
  62.  
  63.     /*
  64.      * From now on nobody will try to connect phones or attach
  65.      * kbox threads
  66.      */
  67.  
  68.     /*
  69.      * Disconnect all phones connected to our kbox. Passing true for
  70.      * notify_box causes a HANGUP message to be inserted for each
  71.      * disconnected phone. This ensures the kbox thread is going to
  72.      * wake up and terminate.
  73.      */
  74.     ipc_answerbox_slam_phones(&TASK->kb.box, have_kb_thread);
  75.  
  76.     /*
  77.      * If the task was being debugged, clean up debugging session.
  78.      * This is necessarry as slamming the phones won't force
  79.      * kbox thread to clean it up since sender != debugger.
  80.      */
  81.     ipl = interrupts_disable();
  82.     spinlock_lock(&TASK->lock);
  83.     udebug_task_cleanup(TASK);
  84.     spinlock_unlock(&TASK->lock);
  85.     interrupts_restore(ipl);
  86.    
  87.     if (have_kb_thread) {
  88.         LOG("join kb.thread..\n");
  89.         thread_join(TASK->kb.thread);
  90.         thread_detach(TASK->kb.thread);
  91.         LOG("join done\n");
  92.         TASK->kb.thread = NULL;
  93.     }
  94.  
  95.     /* Answer all messages in 'calls' and 'dispatched_calls' queues. */
  96.     spinlock_lock(&TASK->kb.box.lock);
  97.     ipc_cleanup_call_list(&TASK->kb.box.dispatched_calls);
  98.     ipc_cleanup_call_list(&TASK->kb.box.calls);
  99.     spinlock_unlock(&TASK->kb.box.lock);
  100. }
  101.  
  102. /** Handle hangup message in kbox.
  103.  *
  104.  * @param call  The IPC_M_PHONE_HUNGUP call structure.
  105.  * @param last  Output, the function stores @c true here if
  106.  *      this was the last phone, @c false otherwise.
  107.  **/
  108. static void kbox_proc_phone_hungup(call_t *call, bool *last)
  109. {
  110.     ipl_t ipl;
  111.  
  112.     LOG("kbox_proc_phone_hungup()\n");
  113.  
  114.     /* Was it our debugger, who hung up? */
  115.     if (call->sender == TASK->udebug.debugger) {
  116.         /* Terminate debugging session (if any). */
  117.         LOG("kbox: terminate debug session\n");
  118.         ipl = interrupts_disable();
  119.         spinlock_lock(&TASK->lock);
  120.         udebug_task_cleanup(TASK);
  121.         spinlock_unlock(&TASK->lock);
  122.         interrupts_restore(ipl);
  123.     } else {
  124.         LOG("kbox: was not debugger\n");
  125.     }
  126.  
  127.     LOG("kbox: continue with hangup message\n");
  128.     IPC_SET_RETVAL(call->data, 0);
  129.     ipc_answer(&TASK->kb.box, call);
  130.  
  131.     ipl = interrupts_disable();
  132.     spinlock_lock(&TASK->lock);
  133.     spinlock_lock(&TASK->answerbox.lock);
  134.     if (list_empty(&TASK->answerbox.connected_phones)) {
  135.         /*
  136.          * Last phone has been disconnected. Detach this thread so it
  137.          * gets freed and signal to the caller.
  138.          */
  139.  
  140.         /* Only detach kbox thread unless already terminating. */
  141.         mutex_lock(&TASK->kb.cleanup_lock);
  142.         if (&TASK->kb.finished == false) {
  143.             /* Detach kbox thread so it gets freed from memory. */
  144.             thread_detach(TASK->kb.thread);
  145.             TASK->kb.thread = NULL;
  146.         }
  147.         mutex_unlock(&TASK->kb.cleanup_lock);
  148.  
  149.         LOG("phone list is empty\n");
  150.         *last = true;
  151.     } else {
  152.         *last = false;
  153.     }
  154.  
  155.     spinlock_unlock(&TASK->answerbox.lock);
  156.     spinlock_unlock(&TASK->lock);
  157.     interrupts_restore(ipl);
  158. }
  159.  
  160. /** Implementing function for the kbox thread.
  161.  *
  162.  * This function listens for debug requests. It terminates
  163.  * when all phones are disconnected from the kbox.
  164.  *
  165.  * @param arg   Ignored.
  166.  */
  167. static void kbox_thread_proc(void *arg)
  168. {
  169.     call_t *call;
  170.     bool done;
  171.  
  172.     (void)arg;
  173.     LOG("kbox_thread_proc()\n");
  174.     done = false;
  175.  
  176.     while (!done) {
  177.         call = ipc_wait_for_call(&TASK->kb.box, SYNCH_NO_TIMEOUT,
  178.             SYNCH_FLAGS_NONE);
  179.  
  180.         if (call == NULL)
  181.             continue;   /* Try again. */
  182.  
  183.         switch (IPC_GET_METHOD(call->data)) {
  184.  
  185.         case IPC_M_DEBUG_ALL:
  186.             /* Handle debug call. */
  187.             udebug_call_receive(call);
  188.             break;
  189.  
  190.         case IPC_M_PHONE_HUNGUP:
  191.             /*
  192.              * Process the hangup call. If this was the last
  193.              * phone, done will be set to true and the
  194.              * while loop will terminate.
  195.              */
  196.             kbox_proc_phone_hungup(call, &done);
  197.             break;
  198.  
  199.         default:
  200.             /* Ignore */
  201.             break;
  202.         }
  203.     }
  204.  
  205.     LOG("kbox: finished\n");
  206. }
  207.  
  208.  
  209. /**
  210.  * Connect phone to a task kernel-box specified by id.
  211.  *
  212.  * Note that this is not completely atomic. For optimisation reasons, the task
  213.  * might start cleaning up kbox after the phone has been connected and before
  214.  * a kbox thread has been created. This must be taken into account in the
  215.  * cleanup code.
  216.  *
  217.  * @return      Phone id on success, or negative error code.
  218.  */
  219. int ipc_connect_kbox(task_id_t taskid)
  220. {
  221.     int newphid;
  222.     task_t *ta;
  223.     thread_t *kb_thread;
  224.     ipl_t ipl;
  225.  
  226.     ipl = interrupts_disable();
  227.     spinlock_lock(&tasks_lock);
  228.  
  229.     ta = task_find_by_id(taskid);
  230.     if (ta == NULL) {
  231.         spinlock_unlock(&tasks_lock);
  232.         interrupts_restore(ipl);
  233.         return ENOENT;
  234.     }
  235.  
  236.     atomic_inc(&ta->refcount);
  237.  
  238.     spinlock_unlock(&tasks_lock);
  239.     interrupts_restore(ipl);
  240.  
  241.     mutex_lock(&ta->kb.cleanup_lock);
  242.  
  243.     if (atomic_predec(&ta->refcount) == 0) {
  244.         mutex_unlock(&ta->kb.cleanup_lock);
  245.         task_destroy(ta);
  246.         return ENOENT;
  247.     }
  248.  
  249.     if (ta->kb.finished != false) {
  250.         mutex_unlock(&ta->kb.cleanup_lock);
  251.         return EINVAL;
  252.     }
  253.  
  254.     newphid = phone_alloc();
  255.     if (newphid < 0) {
  256.         mutex_unlock(&ta->kb.cleanup_lock);
  257.         return ELIMIT;
  258.     }
  259.  
  260.     /* Connect the newly allocated phone to the kbox */
  261.     ipc_phone_connect(&TASK->phones[newphid], &ta->kb.box);
  262.  
  263.     if (ta->kb.thread != NULL) {
  264.         mutex_unlock(&ta->kb.cleanup_lock);
  265.         return newphid;
  266.     }
  267.  
  268.     /* Create a kbox thread */
  269.     kb_thread = thread_create(kbox_thread_proc, NULL, ta, 0,
  270.         "kbox", false);
  271.     if (!kb_thread) {
  272.         mutex_unlock(&ta->kb.cleanup_lock);
  273.         return ENOMEM;
  274.     }
  275.  
  276.     ta->kb.thread = kb_thread;
  277.     thread_ready(kb_thread);
  278.  
  279.     mutex_unlock(&ta->kb.cleanup_lock);
  280.  
  281.     return newphid;
  282. }
  283.  
  284. /** @}
  285.  */
  286.