Subversion Repositories HelenOS

Rev

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