Subversion Repositories HelenOS

Rev

Rev 2927 | Rev 3004 | 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 <atomic.h>
  45. #include <synch/spinlock.h>
  46. #include <synch/waitq.h>
  47. #include <arch.h>
  48. #include <panic.h>
  49. #include <adt/avl.h>
  50. #include <adt/btree.h>
  51. #include <adt/list.h>
  52. #include <ipc/ipc.h>
  53. #include <security/cap.h>
  54. #include <memstr.h>
  55. #include <print.h>
  56. #include <lib/elf.h>
  57. #include <errno.h>
  58. #include <func.h>
  59. #include <syscall/copy.h>
  60.  
  61. #ifndef LOADED_PROG_STACK_PAGES_NO
  62. #define LOADED_PROG_STACK_PAGES_NO 1
  63. #endif
  64.  
  65. /** Spinlock protecting the tasks_tree AVL tree. */
  66. SPINLOCK_INITIALIZE(tasks_lock);
  67.  
  68. /** AVL tree of active tasks.
  69.  *
  70.  * The task is guaranteed to exist after it was found in the tasks_tree as
  71.  * long as:
  72.  * @li the tasks_lock is held,
  73.  * @li the task's lock is held when task's lock is acquired before releasing
  74.  *     tasks_lock or
  75.  * @li the task's refcount is greater than 0
  76.  *
  77.  */
  78. avltree_t tasks_tree;
  79.  
  80. static task_id_t task_counter = 0;
  81.  
  82. /**
  83.  * Points to the binary image used as the program loader. All non-initial
  84.  * tasks are created from this executable image.
  85.  */
  86. void *program_loader = NULL;
  87.  
  88.  
  89. /** Initialize tasks
  90.  *
  91.  * Initialize kernel tasks support.
  92.  *
  93.  */
  94. void task_init(void)
  95. {
  96.     TASK = NULL;
  97.     avltree_create(&tasks_tree);
  98. }
  99.  
  100. /*
  101.  * The idea behind this walker is to remember a single task different from TASK.
  102.  */
  103. static bool task_done_walker(avltree_node_t *node, void *arg)
  104. {
  105.     task_t *t = avltree_get_instance(node, task_t, tasks_tree_node);
  106.     task_t **tp = (task_t **) arg;
  107.  
  108.     if (t != TASK) {
  109.         *tp = t;
  110.         return false;   /* stop walking */
  111.     }
  112.  
  113.     return true;    /* continue the walk */
  114. }
  115.  
  116. /** Kill all tasks except the current task.
  117.  *
  118.  */
  119. void task_done(void)
  120. {
  121.     task_t *t;
  122.     do { /* Repeat until there are any tasks except TASK */
  123.        
  124.         /* Messing with task structures, avoid deadlock */
  125.         ipl_t ipl = interrupts_disable();
  126.         spinlock_lock(&tasks_lock);
  127.        
  128.         t = NULL;
  129.         avltree_walk(&tasks_tree, task_done_walker, &t);
  130.        
  131.         if (t != NULL) {
  132.             task_id_t id = t->taskid;
  133.            
  134.             spinlock_unlock(&tasks_lock);
  135.             interrupts_restore(ipl);
  136.            
  137. #ifdef CONFIG_DEBUG
  138.             printf("Killing task %llu\n", id);
  139. #endif         
  140.             task_kill(id);
  141.             thread_usleep(10000);
  142.         } else {
  143.             spinlock_unlock(&tasks_lock);
  144.             interrupts_restore(ipl);
  145.         }
  146.        
  147.     } while (t != NULL);
  148. }
  149.  
  150. /** Create new task
  151.  *
  152.  * Create new task with no threads.
  153.  *
  154.  * @param as Task's address space.
  155.  * @param name Symbolic name.
  156.  *
  157.  * @return New task's structure
  158.  *
  159.  */
  160. task_t *task_create(as_t *as, char *name)
  161. {
  162.     ipl_t ipl;
  163.     task_t *ta;
  164.     int i;
  165.    
  166.     ta = (task_t *) malloc(sizeof(task_t), 0);
  167.  
  168.     task_create_arch(ta);
  169.  
  170.     spinlock_initialize(&ta->lock, "task_ta_lock");
  171.     list_initialize(&ta->th_head);
  172.     ta->as = as;
  173.     ta->name = name;
  174.     atomic_set(&ta->refcount, 0);
  175.     atomic_set(&ta->lifecount, 0);
  176.     ta->context = CONTEXT;
  177.  
  178.     ta->capabilities = 0;
  179.     ta->cycles = 0;
  180.    
  181.     ipc_answerbox_init(&ta->answerbox, ta);
  182.     for (i = 0; i < IPC_MAX_PHONES; i++)
  183.         ipc_phone_init(&ta->phones[i]);
  184.     if ((ipc_phone_0) && (context_check(ipc_phone_0->task->context,
  185.         ta->context)))
  186.         ipc_phone_connect(&ta->phones[0], ipc_phone_0);
  187.     atomic_set(&ta->active_calls, 0);
  188.  
  189.     mutex_initialize(&ta->futexes_lock);
  190.     btree_create(&ta->futexes);
  191.    
  192.     ipl = interrupts_disable();
  193.  
  194.     /*
  195.      * Increment address space reference count.
  196.      */
  197.     atomic_inc(&as->refcount);
  198.  
  199.     spinlock_lock(&tasks_lock);
  200.     ta->taskid = ++task_counter;
  201.     avltree_node_initialize(&ta->tasks_tree_node);
  202.     ta->tasks_tree_node.key = ta->taskid;
  203.     avltree_insert(&tasks_tree, &ta->tasks_tree_node);
  204.     spinlock_unlock(&tasks_lock);
  205.     interrupts_restore(ipl);
  206.  
  207.     return ta;
  208. }
  209.  
  210. /** Destroy task.
  211.  *
  212.  * @param t Task to be destroyed.
  213.  */
  214. void task_destroy(task_t *t)
  215. {
  216.     /*
  217.      * Remove the task from the task B+tree.
  218.      */
  219.     spinlock_lock(&tasks_lock);
  220.     avltree_delete(&tasks_tree, &t->tasks_tree_node);
  221.     spinlock_unlock(&tasks_lock);
  222.  
  223.     /*
  224.      * Perform architecture specific task destruction.
  225.      */
  226.     task_destroy_arch(t);
  227.  
  228.     /*
  229.      * Free up dynamically allocated state.
  230.      */
  231.     btree_destroy(&t->futexes);
  232.  
  233.     /*
  234.      * Drop our reference to the address space.
  235.      */
  236.     if (atomic_predec(&t->as->refcount) == 0)
  237.         as_destroy(t->as);
  238.    
  239.     free(t);
  240.     TASK = NULL;
  241. }
  242.  
  243. /** Create new task with 1 thread and run it
  244.  *
  245.  * @param as Address space containing a binary program image.
  246.  * @param entry_addr Program entry-point address in program address space.
  247.  * @param name Program name.
  248.  *
  249.  * @return Task of the running program or NULL on error.
  250.  */
  251. task_t *task_create_from_as(as_t *as, uintptr_t entry_addr, char *name)
  252. {
  253.     as_area_t *a;
  254.     thread_t *t;
  255.     task_t *task;
  256.     uspace_arg_t *kernel_uarg;
  257.  
  258.     kernel_uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0);
  259.     kernel_uarg->uspace_entry = (void *) entry_addr;
  260.     kernel_uarg->uspace_stack = (void *) USTACK_ADDRESS;
  261.     kernel_uarg->uspace_thread_function = NULL;
  262.     kernel_uarg->uspace_thread_arg = NULL;
  263.     kernel_uarg->uspace_uarg = NULL;
  264.    
  265.     task = task_create(as, name);
  266.     ASSERT(task);
  267.  
  268.     /*
  269.      * Create the data as_area.
  270.      */
  271.     a = as_area_create(as, AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE,
  272.         LOADED_PROG_STACK_PAGES_NO * PAGE_SIZE, USTACK_ADDRESS,
  273.         AS_AREA_ATTR_NONE, &anon_backend, NULL);
  274.  
  275.     /*
  276.      * Create the main thread.
  277.      */
  278.     t = thread_create(uinit, kernel_uarg, task, THREAD_FLAG_USPACE,
  279.         "uinit", false);
  280.     ASSERT(t);
  281.    
  282.     return task;
  283. }
  284.  
  285. /** Parse an executable image in the physical memory.
  286.  *
  287.  * If the image belongs to a program loader, it is registered as such,
  288.  * (and *task is set to NULL). Otherwise a task is created from the
  289.  * executable image. The task is returned in *task.
  290.  *
  291.  * @param program_addr Address of program executable image.
  292.  * @param name Program name.
  293.  * @param task Where to store the pointer to the newly created task.
  294.  *
  295.  * @return EOK on success or negative error code.
  296.  */
  297. int task_parse_initial(void *program_addr, char *name, task_t **task)
  298. {
  299.     as_t *as;
  300.     unsigned int rc;
  301.  
  302.     as = as_create(0);
  303.     ASSERT(as);
  304.  
  305.     rc = elf_load((elf_header_t *) program_addr, as, 0);
  306.     if (rc != EE_OK) {
  307.         as_destroy(as);
  308.         *task = NULL;
  309.         if (rc != EE_LOADER)
  310.             return ENOTSUP;
  311.        
  312.         /* Register image as the program loader */
  313.         ASSERT(program_loader == NULL);
  314.         program_loader = program_addr;
  315.         return EOK;
  316.     }
  317.  
  318.     *task = task_create_from_as(as, ((elf_header_t *) program_addr)->e_entry,
  319.         name);
  320.  
  321.     return EOK;
  322. }
  323.  
  324. /** Create a task from the program loader image.
  325.  *
  326.  * @param program_addr Address of program executable image.
  327.  * @param name Program name.
  328.  *
  329.  * @return Task of the running program or NULL on error.
  330.  */
  331. task_t *task_create_from_loader(char *name)
  332. {
  333.     as_t *as;
  334.     unsigned int rc;
  335.  
  336.     as = as_create(0);
  337.     ASSERT(as);
  338.  
  339.     rc = elf_load((elf_header_t *) program_loader, as, ELD_F_LOADER);
  340.     if (rc != EE_OK) {
  341.         as_destroy(as);
  342.         return NULL;
  343.     }
  344.  
  345.     return task_create_from_as(
  346.         as, ((elf_header_t *) program_loader)->e_entry, name); 
  347. }
  348.  
  349. /** Make task ready.
  350.  *
  351.  * Switch task's thread to the ready state.
  352.  *
  353.  * @param ta Task to make ready.
  354.  */
  355. void task_ready(task_t *t)
  356. {
  357.     thread_t *th;
  358.  
  359.     th = list_get_instance(t->th_head.next, thread_t, th_link);
  360.     thread_ready(th);
  361. }
  362.  
  363. /** Syscall for reading task ID from userspace.
  364.  *
  365.  * @param uspace_task_id Userspace address of 8-byte buffer where to store
  366.  * current task ID.
  367.  *
  368.  * @return 0 on success or an error code from @ref errno.h.
  369.  */
  370. unative_t sys_task_get_id(task_id_t *uspace_task_id)
  371. {
  372.     /*
  373.      * No need to acquire lock on TASK because taskid
  374.      * remains constant for the lifespan of the task.
  375.      */
  376.     return (unative_t) copy_to_uspace(uspace_task_id, &TASK->taskid,
  377.         sizeof(TASK->taskid));
  378. }
  379.  
  380. /** Syscall for creating a new task from userspace.
  381.  *
  382.  * Creates a new task from the program loader image and stores its
  383.  * task id into the provided buffer.
  384.  *
  385.  * @param uspace_task_id Userspace address of 8-byte buffer where to store
  386.  * current task ID.
  387.  *
  388.  * @return 0 on success or an error code from @ref errno.h.
  389.  */
  390. unative_t sys_task_spawn(task_id_t *uspace_task_id)
  391. {
  392.     task_t *t;
  393.     task_id_t fake_id;
  394.     int rc;
  395.  
  396.     /* Before we even try creating the task, see if we can write the id */
  397.     rc = (unative_t) copy_to_uspace(uspace_task_id, &fake_id,
  398.         sizeof(fake_id));
  399.     if (rc != 0)
  400.         return rc;
  401.  
  402.     t = task_create_from_loader("loader");
  403.  
  404.     /* No need to aquire lock before task_ready() */
  405.     rc = (unative_t) copy_to_uspace(uspace_task_id, &t->taskid,
  406.         sizeof(t->taskid));
  407.     if (rc != 0) {
  408.         /* Ooops */
  409.         task_kill(t->taskid);
  410.         return rc;
  411.     }
  412.  
  413.     task_ready(t);
  414.  
  415.     return EOK;
  416. }
  417.  
  418. /** Find task structure corresponding to task ID.
  419.  *
  420.  * The tasks_lock must be already held by the caller of this function
  421.  * and interrupts must be disabled.
  422.  *
  423.  * @param id Task ID.
  424.  *
  425.  * @return Task structure address or NULL if there is no such task ID.
  426.  */
  427. task_t *task_find_by_id(task_id_t id)
  428. {
  429.     avltree_node_t *node;
  430.    
  431.     node = avltree_search(&tasks_tree, (avltree_key_t) id);
  432.  
  433.     if (node)
  434.         return avltree_get_instance(node, task_t, tasks_tree_node);
  435.     return NULL;
  436. }
  437.  
  438. /** Get accounting data of given task.
  439.  *
  440.  * Note that task lock of 't' must be already held and
  441.  * interrupts must be already disabled.
  442.  *
  443.  * @param t Pointer to thread.
  444.  *
  445.  */
  446. uint64_t task_get_accounting(task_t *t)
  447. {
  448.     /* Accumulated value of task */
  449.     uint64_t ret = t->cycles;
  450.    
  451.     /* Current values of threads */
  452.     link_t *cur;
  453.     for (cur = t->th_head.next; cur != &t->th_head; cur = cur->next) {
  454.         thread_t *thr = list_get_instance(cur, thread_t, th_link);
  455.        
  456.         spinlock_lock(&thr->lock);
  457.         /* Process only counted threads */
  458.         if (!thr->uncounted) {
  459.             if (thr == THREAD) {
  460.                 /* Update accounting of current thread */
  461.                 thread_update_accounting();
  462.             }
  463.             ret += thr->cycles;
  464.         }
  465.         spinlock_unlock(&thr->lock);
  466.     }
  467.    
  468.     return ret;
  469. }
  470.  
  471. /** Kill task.
  472.  *
  473.  * This function is idempotent.
  474.  * It signals all the task's threads to bail it out.
  475.  *
  476.  * @param id ID of the task to be killed.
  477.  *
  478.  * @return 0 on success or an error code from errno.h
  479.  */
  480. int task_kill(task_id_t id)
  481. {
  482.     ipl_t ipl;
  483.     task_t *ta;
  484.     link_t *cur;
  485.  
  486.     if (id == 1)
  487.         return EPERM;
  488.    
  489.     ipl = interrupts_disable();
  490.     spinlock_lock(&tasks_lock);
  491.     if (!(ta = task_find_by_id(id))) {
  492.         spinlock_unlock(&tasks_lock);
  493.         interrupts_restore(ipl);
  494.         return ENOENT;
  495.     }
  496.     spinlock_unlock(&tasks_lock);
  497.    
  498.     /*
  499.      * Interrupt all threads except ktaskclnp.
  500.      */
  501.     spinlock_lock(&ta->lock);
  502.     for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) {
  503.         thread_t *thr;
  504.         bool sleeping = false;
  505.        
  506.         thr = list_get_instance(cur, thread_t, th_link);
  507.            
  508.         spinlock_lock(&thr->lock);
  509.         thr->interrupted = true;
  510.         if (thr->state == Sleeping)
  511.             sleeping = true;
  512.         spinlock_unlock(&thr->lock);
  513.        
  514.         if (sleeping)
  515.             waitq_interrupt_sleep(thr);
  516.     }
  517.     spinlock_unlock(&ta->lock);
  518.     interrupts_restore(ipl);
  519.    
  520.     return 0;
  521. }
  522.  
  523. static bool task_print_walker(avltree_node_t *node, void *arg)
  524. {
  525.     task_t *t = avltree_get_instance(node, task_t, tasks_tree_node);
  526.     int j;
  527.        
  528.     spinlock_lock(&t->lock);
  529.            
  530.     uint64_t cycles;
  531.     char suffix;
  532.     order(task_get_accounting(t), &cycles, &suffix);
  533.    
  534.     if (sizeof(void *) == 4)
  535.         printf("%-6llu %-10s %-3ld %#10zx %#10zx %9llu%c %7zd %6zd",
  536.             t->taskid, t->name, t->context, t, t->as, cycles, suffix,
  537.             t->refcount, atomic_get(&t->active_calls));
  538.     else
  539.         printf("%-6llu %-10s %-3ld %#18zx %#18zx %9llu%c %7zd %6zd",
  540.             t->taskid, t->name, t->context, t, t->as, cycles, suffix,
  541.             t->refcount, atomic_get(&t->active_calls));
  542.     for (j = 0; j < IPC_MAX_PHONES; j++) {
  543.         if (t->phones[j].callee)
  544.             printf(" %zd:%#zx", j, t->phones[j].callee);
  545.     }
  546.     printf("\n");
  547.            
  548.     spinlock_unlock(&t->lock);
  549.     return true;
  550. }
  551.  
  552. /** Print task list */
  553. void task_print_list(void)
  554. {
  555.     ipl_t ipl;
  556.    
  557.     /* Messing with task structures, avoid deadlock */
  558.     ipl = interrupts_disable();
  559.     spinlock_lock(&tasks_lock);
  560.    
  561.     if (sizeof(void *) == 4) {
  562.         printf("taskid name       ctx address    as         "
  563.             "cycles     threads calls  callee\n");
  564.         printf("------ ---------- --- ---------- ---------- "
  565.             "---------- ------- ------ ------>\n");
  566.     } else {
  567.         printf("taskid name       ctx address            as                 "
  568.             "cycles     threads calls  callee\n");
  569.         printf("------ ---------- --- ------------------ ------------------ "
  570.             "---------- ------- ------ ------>\n");
  571.     }
  572.  
  573.     avltree_walk(&tasks_tree, task_print_walker, NULL);
  574.  
  575.     spinlock_unlock(&tasks_lock);
  576.     interrupts_restore(ipl);
  577. }
  578.  
  579. /** @}
  580.  */
  581.