Subversion Repositories HelenOS

Rev

Rev 2802 | Rev 3186 | 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. /** Initialize tasks
  83.  *
  84.  * Initialize kernel tasks support.
  85.  *
  86.  */
  87. void task_init(void)
  88. {
  89.     TASK = NULL;
  90.     avltree_create(&tasks_tree);
  91. }
  92.  
  93. /*
  94.  * The idea behind this walker is to remember a single task different from TASK.
  95.  */
  96. static bool task_done_walker(avltree_node_t *node, void *arg)
  97. {
  98.     task_t *t = avltree_get_instance(node, task_t, tasks_tree_node);
  99.     task_t **tp = (task_t **) arg;
  100.  
  101.     if (t != TASK) {
  102.         *tp = t;
  103.         return false;   /* stop walking */
  104.     }
  105.  
  106.     return true;    /* continue the walk */
  107. }
  108.  
  109. /** Kill all tasks except the current task.
  110.  *
  111.  */
  112. void task_done(void)
  113. {
  114.     task_t *t;
  115.     do { /* Repeat until there are any tasks except TASK */
  116.        
  117.         /* Messing with task structures, avoid deadlock */
  118.         ipl_t ipl = interrupts_disable();
  119.         spinlock_lock(&tasks_lock);
  120.        
  121.         t = NULL;
  122.         avltree_walk(&tasks_tree, task_done_walker, &t);
  123.        
  124.         if (t != NULL) {
  125.             task_id_t id = t->taskid;
  126.            
  127.             spinlock_unlock(&tasks_lock);
  128.             interrupts_restore(ipl);
  129.            
  130. #ifdef CONFIG_DEBUG
  131.             printf("Killing task %" PRIu64 "\n", id);
  132. #endif         
  133.             task_kill(id);
  134.             thread_usleep(10000);
  135.         } else {
  136.             spinlock_unlock(&tasks_lock);
  137.             interrupts_restore(ipl);
  138.         }
  139.        
  140.     } while (t != NULL);
  141. }
  142.  
  143. /** Create new task
  144.  *
  145.  * Create new task with no threads.
  146.  *
  147.  * @param as Task's address space.
  148.  * @param name Symbolic name.
  149.  *
  150.  * @return New task's structure
  151.  *
  152.  */
  153. task_t *task_create(as_t *as, char *name)
  154. {
  155.     ipl_t ipl;
  156.     task_t *ta;
  157.     int i;
  158.    
  159.     ta = (task_t *) malloc(sizeof(task_t), 0);
  160.  
  161.     task_create_arch(ta);
  162.  
  163.     spinlock_initialize(&ta->lock, "task_ta_lock");
  164.     list_initialize(&ta->th_head);
  165.     ta->as = as;
  166.     ta->name = name;
  167.     atomic_set(&ta->refcount, 0);
  168.     atomic_set(&ta->lifecount, 0);
  169.     ta->context = CONTEXT;
  170.  
  171.     ta->capabilities = 0;
  172.     ta->cycles = 0;
  173.    
  174.     ipc_answerbox_init(&ta->answerbox, ta);
  175.     for (i = 0; i < IPC_MAX_PHONES; i++)
  176.         ipc_phone_init(&ta->phones[i]);
  177.     if ((ipc_phone_0) && (context_check(ipc_phone_0->task->context,
  178.         ta->context)))
  179.         ipc_phone_connect(&ta->phones[0], ipc_phone_0);
  180.     atomic_set(&ta->active_calls, 0);
  181.  
  182.     mutex_initialize(&ta->futexes_lock);
  183.     btree_create(&ta->futexes);
  184.    
  185.     ipl = interrupts_disable();
  186.  
  187.     /*
  188.      * Increment address space reference count.
  189.      */
  190.     atomic_inc(&as->refcount);
  191.  
  192.     spinlock_lock(&tasks_lock);
  193.     ta->taskid = ++task_counter;
  194.     avltree_node_initialize(&ta->tasks_tree_node);
  195.     ta->tasks_tree_node.key = ta->taskid;
  196.     avltree_insert(&tasks_tree, &ta->tasks_tree_node);
  197.     spinlock_unlock(&tasks_lock);
  198.     interrupts_restore(ipl);
  199.  
  200.     return ta;
  201. }
  202.  
  203. /** Destroy task.
  204.  *
  205.  * @param t Task to be destroyed.
  206.  */
  207. void task_destroy(task_t *t)
  208. {
  209.     /*
  210.      * Remove the task from the task B+tree.
  211.      */
  212.     spinlock_lock(&tasks_lock);
  213.     avltree_delete(&tasks_tree, &t->tasks_tree_node);
  214.     spinlock_unlock(&tasks_lock);
  215.  
  216.     /*
  217.      * Perform architecture specific task destruction.
  218.      */
  219.     task_destroy_arch(t);
  220.  
  221.     /*
  222.      * Free up dynamically allocated state.
  223.      */
  224.     btree_destroy(&t->futexes);
  225.  
  226.     /*
  227.      * Drop our reference to the address space.
  228.      */
  229.     if (atomic_predec(&t->as->refcount) == 0)
  230.         as_destroy(t->as);
  231.    
  232.     free(t);
  233.     TASK = NULL;
  234. }
  235.  
  236. /** Syscall for reading task ID from userspace.
  237.  *
  238.  * @param uspace_task_id Userspace address of 8-byte buffer where to store
  239.  * 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,
  250.         sizeof(TASK->taskid));
  251. }
  252.  
  253. unative_t sys_task_spawn(void *image, size_t size)
  254. {
  255.     void *kimage = malloc(size, 0);
  256.     if (kimage == NULL)
  257.         return ENOMEM;
  258.    
  259.     int rc = copy_from_uspace(kimage, image, size);
  260.     if (rc != EOK)
  261.         return rc;
  262.    
  263.     uspace_arg_t *kernel_uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0);
  264.     if (kernel_uarg == NULL) {
  265.         free(kimage);
  266.         return ENOMEM;
  267.     }
  268.    
  269.     kernel_uarg->uspace_entry =
  270.         (void *) ((elf_header_t *) kimage)->e_entry;
  271.     kernel_uarg->uspace_stack = (void *) USTACK_ADDRESS;
  272.     kernel_uarg->uspace_thread_function = NULL;
  273.     kernel_uarg->uspace_thread_arg = NULL;
  274.     kernel_uarg->uspace_uarg = NULL;
  275.    
  276.     as_t *as = as_create(0);
  277.     if (as == NULL) {
  278.         free(kernel_uarg);
  279.         free(kimage);
  280.         return ENOMEM;
  281.     }
  282.    
  283.     unsigned int erc = elf_load((elf_header_t *) kimage, as);
  284.     if (erc != EE_OK) {
  285.         as_destroy(as);
  286.         free(kernel_uarg);
  287.         free(kimage);
  288.         return ENOENT;
  289.     }
  290.    
  291.     as_area_t *area = as_area_create(as,
  292.         AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE,
  293.         LOADED_PROG_STACK_PAGES_NO * PAGE_SIZE, USTACK_ADDRESS,
  294.         AS_AREA_ATTR_NONE, &anon_backend, NULL);
  295.     if (area == NULL) {
  296.         as_destroy(as);
  297.         free(kernel_uarg);
  298.         free(kimage);
  299.         return ENOMEM;
  300.     }
  301.    
  302.     task_t *task = task_create(as, "app");
  303.     if (task == NULL) {
  304.         as_destroy(as);
  305.         free(kernel_uarg);
  306.         free(kimage);
  307.         return ENOENT;
  308.     }
  309.    
  310.     // FIXME: control the capabilities
  311.     cap_set(task, cap_get(TASK));
  312.    
  313.     thread_t *thread = thread_create(uinit, kernel_uarg, task,
  314.         THREAD_FLAG_USPACE, "user", false);
  315.     if (thread == NULL) {
  316.         task_destroy(task);
  317.         as_destroy(as);
  318.         free(kernel_uarg);
  319.         free(kimage);
  320.         return ENOENT;
  321.     }
  322.    
  323.     thread_ready(thread);
  324.    
  325.     return EOK;
  326. }
  327.  
  328. /** Find task structure corresponding to task ID.
  329.  *
  330.  * The tasks_lock must be already held by the caller of this function
  331.  * and interrupts must be disabled.
  332.  *
  333.  * @param id Task ID.
  334.  *
  335.  * @return Task structure address or NULL if there is no such task ID.
  336.  */
  337. task_t *task_find_by_id(task_id_t id)
  338. {
  339.     avltree_node_t *node;
  340.    
  341.     node = avltree_search(&tasks_tree, (avltree_key_t) id);
  342.  
  343.     if (node)
  344.         return avltree_get_instance(node, task_t, tasks_tree_node);
  345.     return NULL;
  346. }
  347.  
  348. /** Get accounting data of given task.
  349.  *
  350.  * Note that task lock of 't' must be already held and
  351.  * interrupts must be already disabled.
  352.  *
  353.  * @param t Pointer to thread.
  354.  *
  355.  */
  356. uint64_t task_get_accounting(task_t *t)
  357. {
  358.     /* Accumulated value of task */
  359.     uint64_t ret = t->cycles;
  360.    
  361.     /* Current values of threads */
  362.     link_t *cur;
  363.     for (cur = t->th_head.next; cur != &t->th_head; cur = cur->next) {
  364.         thread_t *thr = list_get_instance(cur, thread_t, th_link);
  365.        
  366.         spinlock_lock(&thr->lock);
  367.         /* Process only counted threads */
  368.         if (!thr->uncounted) {
  369.             if (thr == THREAD) {
  370.                 /* Update accounting of current thread */
  371.                 thread_update_accounting();
  372.             }
  373.             ret += thr->cycles;
  374.         }
  375.         spinlock_unlock(&thr->lock);
  376.     }
  377.    
  378.     return ret;
  379. }
  380.  
  381. /** Kill task.
  382.  *
  383.  * This function is idempotent.
  384.  * It signals all the task's threads to bail it out.
  385.  *
  386.  * @param id ID of the task to be killed.
  387.  *
  388.  * @return 0 on success or an error code from errno.h
  389.  */
  390. int task_kill(task_id_t id)
  391. {
  392.     ipl_t ipl;
  393.     task_t *ta;
  394.     link_t *cur;
  395.  
  396.     if (id == 1)
  397.         return EPERM;
  398.    
  399.     ipl = interrupts_disable();
  400.     spinlock_lock(&tasks_lock);
  401.     if (!(ta = task_find_by_id(id))) {
  402.         spinlock_unlock(&tasks_lock);
  403.         interrupts_restore(ipl);
  404.         return ENOENT;
  405.     }
  406.     spinlock_unlock(&tasks_lock);
  407.    
  408.     /*
  409.      * Interrupt all threads except ktaskclnp.
  410.      */
  411.     spinlock_lock(&ta->lock);
  412.     for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) {
  413.         thread_t *thr;
  414.         bool sleeping = false;
  415.        
  416.         thr = list_get_instance(cur, thread_t, th_link);
  417.            
  418.         spinlock_lock(&thr->lock);
  419.         thr->interrupted = true;
  420.         if (thr->state == Sleeping)
  421.             sleeping = true;
  422.         spinlock_unlock(&thr->lock);
  423.        
  424.         if (sleeping)
  425.             waitq_interrupt_sleep(thr);
  426.     }
  427.     spinlock_unlock(&ta->lock);
  428.     interrupts_restore(ipl);
  429.    
  430.     return 0;
  431. }
  432.  
  433. static bool task_print_walker(avltree_node_t *node, void *arg)
  434. {
  435.     task_t *t = avltree_get_instance(node, task_t, tasks_tree_node);
  436.     int j;
  437.        
  438.     spinlock_lock(&t->lock);
  439.            
  440.     uint64_t cycles;
  441.     char suffix;
  442.     order(task_get_accounting(t), &cycles, &suffix);
  443.  
  444. #ifdef __32_BITS__ 
  445.     printf("%-6" PRIu64 " %-10s %-3" PRIu32 " %10p %10p %9" PRIu64 "%c %7ld %6ld",
  446.         t->taskid, t->name, t->context, t, t->as, cycles, suffix,
  447.         atomic_get(&t->refcount), atomic_get(&t->active_calls));
  448. #endif
  449.  
  450. #ifdef __64_BITS__
  451.     printf("%-6" PRIu64 " %-10s %-3" PRIu32 " %18p %18p %9" PRIu64 "%c %7ld %6ld",
  452.         t->taskid, t->name, t->context, t, t->as, cycles, suffix,
  453.         atomic_get(&t->refcount), atomic_get(&t->active_calls));
  454. #endif
  455.  
  456.     for (j = 0; j < IPC_MAX_PHONES; j++) {
  457.         if (t->phones[j].callee)
  458.             printf(" %d:%p", j, t->phones[j].callee);
  459.     }
  460.     printf("\n");
  461.            
  462.     spinlock_unlock(&t->lock);
  463.     return true;
  464. }
  465.  
  466. /** Print task list */
  467. void task_print_list(void)
  468. {
  469.     ipl_t ipl;
  470.    
  471.     /* Messing with task structures, avoid deadlock */
  472.     ipl = interrupts_disable();
  473.     spinlock_lock(&tasks_lock);
  474.  
  475. #ifdef __32_BITS__ 
  476.     printf("taskid name       ctx address    as         "
  477.         "cycles     threads calls  callee\n");
  478.     printf("------ ---------- --- ---------- ---------- "
  479.         "---------- ------- ------ ------>\n");
  480. #endif
  481.  
  482. #ifdef __64_BITS__
  483.     printf("taskid name       ctx address            as                 "
  484.         "cycles     threads calls  callee\n");
  485.     printf("------ ---------- --- ------------------ ------------------ "
  486.         "---------- ------- ------ ------>\n");
  487. #endif
  488.  
  489.     avltree_walk(&tasks_tree, task_print_walker, NULL);
  490.  
  491.     spinlock_unlock(&tasks_lock);
  492.     interrupts_restore(ipl);
  493. }
  494.  
  495. /** @}
  496.  */
  497.