Subversion Repositories HelenOS

Rev

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