Subversion Repositories HelenOS-historic

Rev

Rev 1460 | Rev 1579 | 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. /**
  30.  * @file    task.c
  31.  * @brief   Task management.
  32.  */
  33.  
  34. #include <main/uinit.h>
  35. #include <proc/thread.h>
  36. #include <proc/task.h>
  37. #include <proc/uarg.h>
  38. #include <mm/as.h>
  39. #include <mm/slab.h>
  40. #include <synch/spinlock.h>
  41. #include <arch.h>
  42. #include <panic.h>
  43. #include <adt/btree.h>
  44. #include <adt/list.h>
  45. #include <ipc/ipc.h>
  46. #include <security/cap.h>
  47. #include <memstr.h>
  48. #include <print.h>
  49. #include <elf.h>
  50. #include <syscall/copy.h>
  51.  
  52. #ifndef LOADED_PROG_STACK_PAGES_NO
  53. #define LOADED_PROG_STACK_PAGES_NO 1
  54. #endif
  55.  
  56. SPINLOCK_INITIALIZE(tasks_lock);
  57. btree_t tasks_btree;
  58. static task_id_t task_counter = 0;
  59.  
  60. /** Initialize tasks
  61.  *
  62.  * Initialize kernel tasks support.
  63.  *
  64.  */
  65. void task_init(void)
  66. {
  67.     TASK = NULL;
  68.     btree_create(&tasks_btree);
  69. }
  70.  
  71.  
  72. /** Create new task
  73.  *
  74.  * Create new task with no threads.
  75.  *
  76.  * @param as Task's address space.
  77.  * @param name Symbolic name.
  78.  *
  79.  * @return New task's structure
  80.  *
  81.  */
  82. task_t *task_create(as_t *as, char *name)
  83. {
  84.     ipl_t ipl;
  85.     task_t *ta;
  86.     int i;
  87.    
  88.     ta = (task_t *) malloc(sizeof(task_t), 0);
  89.  
  90.     task_create_arch(ta);
  91.  
  92.     spinlock_initialize(&ta->lock, "task_ta_lock");
  93.     list_initialize(&ta->th_head);
  94.     ta->as = as;
  95.     ta->name = name;
  96.  
  97.     ta->capabilities = 0;
  98.    
  99.     ipc_answerbox_init(&ta->answerbox);
  100.     for (i=0; i < IPC_MAX_PHONES;i++)
  101.         ipc_phone_init(&ta->phones[i]);
  102.     if (ipc_phone_0)
  103.         ipc_phone_connect(&ta->phones[0], ipc_phone_0);
  104.     atomic_set(&ta->active_calls, 0);
  105.  
  106.     mutex_initialize(&ta->futexes_lock);
  107.     btree_create(&ta->futexes);
  108.    
  109.     ipl = interrupts_disable();
  110.  
  111.     /*
  112.      * Increment address space reference count.
  113.      * TODO: Reconsider the locking scheme.
  114.      */
  115.     mutex_lock(&as->lock);
  116.     as->refcount++;
  117.     mutex_unlock(&as->lock);
  118.  
  119.     spinlock_lock(&tasks_lock);
  120.  
  121.     ta->taskid = ++task_counter;
  122.     btree_insert(&tasks_btree, (btree_key_t) ta->taskid, (void *) ta, NULL);
  123.  
  124.     spinlock_unlock(&tasks_lock);
  125.     interrupts_restore(ipl);
  126.  
  127.     return ta;
  128. }
  129.  
  130. /** Create new task with 1 thread and run it
  131.  *
  132.  * @param program_addr Address of program executable image.
  133.  * @param name Program name.
  134.  *
  135.  * @return Task of the running program or NULL on error.
  136.  */
  137. task_t * task_run_program(void *program_addr, char *name)
  138. {
  139.     as_t *as;
  140.     as_area_t *a;
  141.     int rc;
  142.     thread_t *t;
  143.     task_t *task;
  144.     uspace_arg_t *kernel_uarg;
  145.  
  146.     as = as_create(0);
  147.     ASSERT(as);
  148.  
  149.     rc = elf_load((elf_header_t *) program_addr, as);
  150.     if (rc != EE_OK) {
  151.         as_destroy(as);
  152.         return NULL;
  153.     }
  154.    
  155.     kernel_uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0);
  156.     kernel_uarg->uspace_entry = (void *) ((elf_header_t *) program_addr)->e_entry;
  157.     kernel_uarg->uspace_stack = (void *) USTACK_ADDRESS;
  158.     kernel_uarg->uspace_thread_function = NULL;
  159.     kernel_uarg->uspace_thread_arg = NULL;
  160.     kernel_uarg->uspace_uarg = NULL;
  161.    
  162.     task = task_create(as, name);
  163.     ASSERT(task);
  164.  
  165.     /*
  166.      * Create the data as_area.
  167.      */
  168.     a = as_area_create(as, AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE,
  169.         LOADED_PROG_STACK_PAGES_NO*PAGE_SIZE,
  170.         USTACK_ADDRESS, AS_AREA_ATTR_NONE, &anon_backend, NULL);
  171.  
  172.     t = thread_create(uinit, kernel_uarg, task, 0, "uinit");
  173.     ASSERT(t);
  174.     thread_ready(t);
  175.    
  176.     return task;
  177. }
  178.  
  179. /** Syscall for reading task ID from userspace.
  180.  *
  181.  * @param uspace_task_id Userspace address of 8-byte buffer where to store current task ID.
  182.  *
  183.  * @return 0 on success or an error code from @ref errno.h.
  184.  */
  185. __native sys_task_get_id(task_id_t *uspace_task_id)
  186. {
  187.     /*
  188.      * No need to acquire lock on TASK because taskid
  189.      * remains constant for the lifespan of the task.
  190.      */
  191.     return (__native) copy_to_uspace(uspace_task_id, &TASK->taskid, sizeof(TASK->taskid));
  192. }
  193.  
  194. /** Find task structure corresponding to task ID.
  195.  *
  196.  * The tasks_lock must be already held by the caller of this function
  197.  * and interrupts must be disabled.
  198.  *
  199.  * @param id Task ID.
  200.  *
  201.  * @return Task structure address or NULL if there is no such task ID.
  202.  */
  203. task_t *task_find_by_id(task_id_t id)
  204. {
  205.     btree_node_t *leaf;
  206.    
  207.     return (task_t *) btree_search(&tasks_btree, (btree_key_t) id, &leaf);
  208. }
  209.  
  210. /** Print task list */
  211. void task_print_list(void)
  212. {
  213.     link_t *cur;
  214.     ipl_t ipl;
  215.    
  216.     /* Messing with thread structures, avoid deadlock */
  217.     ipl = interrupts_disable();
  218.     spinlock_lock(&tasks_lock);
  219.  
  220.     for (cur = tasks_btree.leaf_head.next; cur != &tasks_btree.leaf_head; cur = cur->next) {
  221.         btree_node_t *node;
  222.         int i;
  223.        
  224.         node = list_get_instance(cur, btree_node_t, leaf_link);
  225.         for (i = 0; i < node->keys; i++) {
  226.             task_t *t;
  227.             int j;
  228.  
  229.             t = (task_t *) node->value[i];
  230.        
  231.             spinlock_lock(&t->lock);
  232.             printf("%s: address=%#zX, taskid=%#llX, as=%#zX, ActiveCalls: %zd",
  233.                 t->name, t, t->taskid, t->as, atomic_get(&t->active_calls));
  234.             for (j=0; j < IPC_MAX_PHONES; j++) {
  235.                 if (t->phones[j].callee)
  236.                     printf(" Ph(%zd): %#zX ", j, t->phones[j].callee);
  237.             }
  238.             printf("\n");
  239.             spinlock_unlock(&t->lock);
  240.         }
  241.     }
  242.  
  243.     spinlock_unlock(&tasks_lock);
  244.     interrupts_restore(ipl);
  245. }
  246.