Subversion Repositories HelenOS

Rev

Rev 1780 | Rev 1839 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (C) 2001-2004 Jakub Jermar
  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 genericproc
  30.  * @{
  31.  */
  32.  
  33. /**
  34.  * @file
  35.  * @brief   Task management.
  36.  */
  37.  
  38. #include <main/uinit.h>
  39. #include <proc/thread.h>
  40. #include <proc/task.h>
  41. #include <proc/uarg.h>
  42. #include <mm/as.h>
  43. #include <mm/slab.h>
  44. #include <synch/spinlock.h>
  45. #include <arch.h>
  46. #include <panic.h>
  47. #include <adt/btree.h>
  48. #include <adt/list.h>
  49. #include <ipc/ipc.h>
  50. #include <security/cap.h>
  51. #include <memstr.h>
  52. #include <print.h>
  53. #include <elf.h>
  54. #include <errno.h>
  55. #include <syscall/copy.h>
  56. #include <console/klog.h>
  57.  
  58. #ifndef LOADED_PROG_STACK_PAGES_NO
  59. #define LOADED_PROG_STACK_PAGES_NO 1
  60. #endif
  61.  
  62. /** Spinlock protecting the tasks_btree B+tree. */
  63. SPINLOCK_INITIALIZE(tasks_lock);
  64.  
  65. /** B+tree of active tasks.
  66.  *
  67.  * The task is guaranteed to exist after it was found in the tasks_btree as long as:
  68.  * @li the tasks_lock is held,
  69.  * @li the task's lock is held when task's lock is acquired before releasing tasks_lock or
  70.  * @li the task's refcount is grater than 0
  71.  *
  72.  */
  73. btree_t tasks_btree;
  74.  
  75. static task_id_t task_counter = 0;
  76.  
  77. static void ktaskclnp(void *arg);
  78. static void ktaskgc(void *arg);
  79.  
  80. /** Initialize tasks
  81.  *
  82.  * Initialize kernel tasks support.
  83.  *
  84.  */
  85. void task_init(void)
  86. {
  87.     TASK = NULL;
  88.     btree_create(&tasks_btree);
  89. }
  90.  
  91.  
  92. /** Create new task
  93.  *
  94.  * Create new task with no threads.
  95.  *
  96.  * @param as Task's address space.
  97.  * @param name Symbolic name.
  98.  *
  99.  * @return New task's structure
  100.  *
  101.  */
  102. task_t *task_create(as_t *as, char *name)
  103. {
  104.     ipl_t ipl;
  105.     task_t *ta;
  106.     int i;
  107.    
  108.     ta = (task_t *) malloc(sizeof(task_t), 0);
  109.  
  110.     task_create_arch(ta);
  111.  
  112.     spinlock_initialize(&ta->lock, "task_ta_lock");
  113.     list_initialize(&ta->th_head);
  114.     ta->as = as;
  115.     ta->name = name;
  116.     ta->main_thread = NULL;
  117.     ta->refcount = 0;
  118.  
  119.     ta->capabilities = 0;
  120.     ta->accept_new_threads = true;
  121.    
  122.     ipc_answerbox_init(&ta->answerbox);
  123.     for (i=0; i < IPC_MAX_PHONES;i++)
  124.         ipc_phone_init(&ta->phones[i]);
  125.     if (ipc_phone_0)
  126.         ipc_phone_connect(&ta->phones[0], ipc_phone_0);
  127.     atomic_set(&ta->active_calls, 0);
  128.  
  129.     mutex_initialize(&ta->futexes_lock);
  130.     btree_create(&ta->futexes);
  131.    
  132.     ipl = interrupts_disable();
  133.  
  134.     /*
  135.      * Increment address space reference count.
  136.      * TODO: Reconsider the locking scheme.
  137.      */
  138.     mutex_lock(&as->lock);
  139.     as->refcount++;
  140.     mutex_unlock(&as->lock);
  141.  
  142.     spinlock_lock(&tasks_lock);
  143.  
  144.     ta->taskid = ++task_counter;
  145.     btree_insert(&tasks_btree, (btree_key_t) ta->taskid, (void *) ta, NULL);
  146.  
  147.     spinlock_unlock(&tasks_lock);
  148.     interrupts_restore(ipl);
  149.  
  150.     return ta;
  151. }
  152.  
  153. /** Destroy task.
  154.  *
  155.  * @param t Task to be destroyed.
  156.  */
  157. void task_destroy(task_t *t)
  158. {
  159.     task_destroy_arch(t);
  160.     btree_destroy(&t->futexes);
  161.  
  162.     mutex_lock_active(&t->as->lock);
  163.     if (--t->as->refcount == 0) {
  164.         mutex_unlock(&t->as->lock);
  165.         as_destroy(t->as);
  166.         /*
  167.          * t->as is destroyed.
  168.          */
  169.     } else {
  170.         mutex_unlock(&t->as->lock);
  171.     }
  172.    
  173.     free(t);
  174.     TASK = NULL;
  175. }
  176.  
  177. /** Create new task with 1 thread and run it
  178.  *
  179.  * @param program_addr Address of program executable image.
  180.  * @param name Program name.
  181.  *
  182.  * @return Task of the running program or NULL on error.
  183.  */
  184. task_t * task_run_program(void *program_addr, char *name)
  185. {
  186.     as_t *as;
  187.     as_area_t *a;
  188.     int rc;
  189.     thread_t *t1, *t2;
  190.     task_t *task;
  191.     uspace_arg_t *kernel_uarg;
  192.  
  193.     as = as_create(0);
  194.     ASSERT(as);
  195.  
  196.     rc = elf_load((elf_header_t *) program_addr, as);
  197.     if (rc != EE_OK) {
  198.         as_destroy(as);
  199.         return NULL;
  200.     }
  201.    
  202.     kernel_uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0);
  203.     kernel_uarg->uspace_entry = (void *) ((elf_header_t *) program_addr)->e_entry;
  204.     kernel_uarg->uspace_stack = (void *) USTACK_ADDRESS;
  205.     kernel_uarg->uspace_thread_function = NULL;
  206.     kernel_uarg->uspace_thread_arg = NULL;
  207.     kernel_uarg->uspace_uarg = NULL;
  208.    
  209.     task = task_create(as, name);
  210.     ASSERT(task);
  211.  
  212.     /*
  213.      * Create the data as_area.
  214.      */
  215.     a = as_area_create(as, AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE,
  216.         LOADED_PROG_STACK_PAGES_NO*PAGE_SIZE,
  217.         USTACK_ADDRESS, AS_AREA_ATTR_NONE, &anon_backend, NULL);
  218.  
  219.     /*
  220.      * Create the main thread.
  221.      */
  222.     t1 = thread_create(uinit, kernel_uarg, task, 0, "uinit");
  223.     ASSERT(t1);
  224.    
  225.     /*
  226.      * Create killer thread for the new task.
  227.      */
  228.     t2 = thread_create(ktaskgc, t1, task, 0, "ktaskgc");
  229.     ASSERT(t2);
  230.     thread_ready(t2);
  231.  
  232.     thread_ready(t1);
  233.  
  234.     return task;
  235. }
  236.  
  237. /** Syscall for reading task ID from userspace.
  238.  *
  239.  * @param uspace_task_id Userspace address of 8-byte buffer where to store current task ID.
  240.  *
  241.  * @return 0 on success or an error code from @ref errno.h.
  242.  */
  243. unative_t sys_task_get_id(task_id_t *uspace_task_id)
  244. {
  245.     /*
  246.      * No need to acquire lock on TASK because taskid
  247.      * remains constant for the lifespan of the task.
  248.      */
  249.     return (unative_t) copy_to_uspace(uspace_task_id, &TASK->taskid, sizeof(TASK->taskid));
  250. }
  251.  
  252. /** Find task structure corresponding to task ID.
  253.  *
  254.  * The tasks_lock must be already held by the caller of this function
  255.  * and interrupts must be disabled.
  256.  *
  257.  * @param id Task ID.
  258.  *
  259.  * @return Task structure address or NULL if there is no such task ID.
  260.  */
  261. task_t *task_find_by_id(task_id_t id)
  262. {
  263.     btree_node_t *leaf;
  264.    
  265.     return (task_t *) btree_search(&tasks_btree, (btree_key_t) id, &leaf);
  266. }
  267.  
  268. /** Kill task.
  269.  *
  270.  * @param id ID of the task to be killed.
  271.  *
  272.  * @return 0 on success or an error code from errno.h
  273.  */
  274. int task_kill(task_id_t id)
  275. {
  276.     ipl_t ipl;
  277.     task_t *ta;
  278.     thread_t *t;
  279.     link_t *cur;
  280.  
  281.     if (id == 1)
  282.         return EPERM;
  283.    
  284.     ipl = interrupts_disable();
  285.     spinlock_lock(&tasks_lock);
  286.  
  287.     if (!(ta = task_find_by_id(id))) {
  288.         spinlock_unlock(&tasks_lock);
  289.         interrupts_restore(ipl);
  290.         return ENOENT;
  291.     }
  292.  
  293.     spinlock_lock(&ta->lock);
  294.     ta->refcount++;
  295.     spinlock_unlock(&ta->lock);
  296.  
  297.     btree_remove(&tasks_btree, ta->taskid, NULL);
  298.     spinlock_unlock(&tasks_lock);
  299.    
  300.     t = thread_create(ktaskclnp, NULL, ta, 0, "ktaskclnp");
  301.    
  302.     spinlock_lock(&ta->lock);
  303.     ta->accept_new_threads = false;
  304.     ta->refcount--;
  305.  
  306.     /*
  307.      * Interrupt all threads except ktaskclnp.
  308.      */
  309.     for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) {
  310.         thread_t *thr;
  311.         bool  sleeping = false;
  312.        
  313.         thr = list_get_instance(cur, thread_t, th_link);
  314.         if (thr == t)
  315.             continue;
  316.            
  317.         spinlock_lock(&thr->lock);
  318.         thr->interrupted = true;
  319.         if (thr->state == Sleeping)
  320.             sleeping = true;
  321.         spinlock_unlock(&thr->lock);
  322.        
  323.         if (sleeping)
  324.             waitq_interrupt_sleep(thr);
  325.     }
  326.    
  327.     spinlock_unlock(&ta->lock);
  328.     interrupts_restore(ipl);
  329.    
  330.     if (t)
  331.         thread_ready(t);
  332.  
  333.     return 0;
  334. }
  335.  
  336. /** Print task list */
  337. void task_print_list(void)
  338. {
  339.     link_t *cur;
  340.     ipl_t ipl;
  341.    
  342.     /* Messing with thread structures, avoid deadlock */
  343.     ipl = interrupts_disable();
  344.     spinlock_lock(&tasks_lock);
  345.  
  346.     for (cur = tasks_btree.leaf_head.next; cur != &tasks_btree.leaf_head; cur = cur->next) {
  347.         btree_node_t *node;
  348.         int i;
  349.        
  350.         node = list_get_instance(cur, btree_node_t, leaf_link);
  351.         for (i = 0; i < node->keys; i++) {
  352.             task_t *t;
  353.             int j;
  354.  
  355.             t = (task_t *) node->value[i];
  356.        
  357.             spinlock_lock(&t->lock);
  358.             printf("%s(%lld): address=%#zx, as=%#zx, ActiveCalls: %zd",
  359.                 t->name, t->taskid, t, t->as, atomic_get(&t->active_calls));
  360.             for (j=0; j < IPC_MAX_PHONES; j++) {
  361.                 if (t->phones[j].callee)
  362.                     printf(" Ph(%zd): %#zx ", j, t->phones[j].callee);
  363.             }
  364.             printf("\n");
  365.             spinlock_unlock(&t->lock);
  366.         }
  367.     }
  368.  
  369.     spinlock_unlock(&tasks_lock);
  370.     interrupts_restore(ipl);
  371. }
  372.  
  373. /** Kernel thread used to cleanup the task after it is killed. */
  374. void ktaskclnp(void *arg)
  375. {
  376.     ipl_t ipl;
  377.     thread_t *t = NULL, *main_thread;
  378.     link_t *cur;
  379.     bool again;
  380.  
  381.     thread_detach(THREAD);
  382.  
  383. loop:
  384.     ipl = interrupts_disable();
  385.     spinlock_lock(&TASK->lock);
  386.    
  387.     main_thread = TASK->main_thread;
  388.    
  389.     /*
  390.      * Find a thread to join.
  391.      */
  392.     again = false;
  393.     for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) {
  394.         t = list_get_instance(cur, thread_t, th_link);
  395.  
  396.         spinlock_lock(&t->lock);
  397.         if (t == THREAD) {
  398.             spinlock_unlock(&t->lock);
  399.             continue;
  400.         } else if (t == main_thread) {
  401.             spinlock_unlock(&t->lock);
  402.             continue;
  403.         } else if (t->join_type != None) {
  404.             spinlock_unlock(&t->lock);
  405.             again = true;
  406.             continue;
  407.         } else {
  408.             t->join_type = TaskClnp;
  409.             spinlock_unlock(&t->lock);
  410.             again = false;
  411.             break;
  412.         }
  413.     }
  414.    
  415.     spinlock_unlock(&TASK->lock);
  416.     interrupts_restore(ipl);
  417.    
  418.     if (again) {
  419.         /*
  420.          * Other cleanup (e.g. ktaskgc) is in progress.
  421.          */
  422.         scheduler();
  423.         goto loop;
  424.     }
  425.    
  426.     if (t != THREAD) {
  427.         ASSERT(t != main_thread);   /* uninit is joined and detached in ktaskgc */
  428.         thread_join(t);
  429.         thread_detach(t);
  430.         goto loop;  /* go for another thread */
  431.     }
  432.    
  433.     /*
  434.      * Now there are no other threads in this task
  435.      * and no new threads can be created.
  436.      */
  437.    
  438.     ipc_cleanup();
  439.     futex_cleanup();
  440.     klog_printf("Cleanup of task %lld completed.", TASK->taskid);
  441. }
  442.  
  443. /** Kernel thread used to kill the userspace task when its main thread exits.
  444.  *
  445.  * This thread waits until the main userspace thread (i.e. uninit) exits.
  446.  * When this happens, the task is killed. In the meantime, exited threads
  447.  * are garbage collected.
  448.  *
  449.  * @param arg Pointer to the thread structure of the task's main thread.
  450.  */
  451. void ktaskgc(void *arg)
  452. {
  453.     thread_t *t = (thread_t *) arg;
  454. loop:  
  455.     /*
  456.      * Userspace threads cannot detach themselves,
  457.      * therefore the thread pointer is guaranteed to be valid.
  458.      */
  459.     if (thread_join_timeout(t, 1000000, SYNCH_FLAGS_NONE) == ESYNCH_TIMEOUT) {  /* sleep uninterruptibly here! */
  460.         ipl_t ipl;
  461.         link_t *cur;
  462.         thread_t *thr = NULL;
  463.    
  464.         /*
  465.          * The join timed out. Try to do some garbage collection of Undead threads.
  466.          */
  467. more_gc:       
  468.         ipl = interrupts_disable();
  469.         spinlock_lock(&TASK->lock);
  470.        
  471.         for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) {
  472.             thr = list_get_instance(cur, thread_t, th_link);
  473.             spinlock_lock(&thr->lock);
  474.             if (thr != t && thr->state == Undead && thr->join_type == None) {
  475.                 thr->join_type = TaskGC;
  476.                 spinlock_unlock(&thr->lock);
  477.                 break;
  478.             }
  479.             spinlock_unlock(&thr->lock);
  480.             thr = NULL;
  481.         }
  482.         spinlock_unlock(&TASK->lock);
  483.         interrupts_restore(ipl);
  484.        
  485.         if (thr) {
  486.             thread_join(thr);
  487.             thread_detach(thr);
  488.             scheduler();
  489.             goto more_gc;
  490.         }
  491.            
  492.         goto loop;
  493.     }
  494.     thread_detach(t);
  495.     task_kill(TASK->taskid);
  496. }
  497.  
  498. /** @}
  499.  */
  500.