Subversion Repositories HelenOS-historic

Rev

Rev 1662 | Rev 1705 | 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 main
  30.  * @ingroup kernel
  31.  * @{
  32.  */
  33.  
  34. /**
  35.  * @file
  36.  * @brief   Kernel initialization thread.
  37.  *
  38.  * This file contains kinit kernel thread which carries out
  39.  * high level system initialization.
  40.  *
  41.  * This file is responsible for finishing SMP configuration
  42.  * and creation of userspace init tasks.
  43.  */
  44.  
  45. #include <main/kinit.h>
  46. #include <config.h>
  47. #include <arch.h>
  48. #include <proc/scheduler.h>
  49. #include <proc/task.h>
  50. #include <proc/thread.h>
  51. #include <panic.h>
  52. #include <func.h>
  53. #include <cpu.h>
  54. #include <arch/asm.h>
  55. #include <mm/page.h>
  56. #include <arch/mm/page.h>
  57. #include <mm/as.h>
  58. #include <mm/frame.h>
  59. #include <print.h>
  60. #include <memstr.h>
  61. #include <console/console.h>
  62. #include <interrupt.h>
  63. #include <console/kconsole.h>
  64. #include <security/cap.h>
  65.  
  66. #ifdef CONFIG_SMP
  67. #include <arch/smp/mps.h>
  68. #endif /* CONFIG_SMP */
  69.  
  70. #include <synch/waitq.h>
  71. #include <synch/spinlock.h>
  72.  
  73. #ifdef CONFIG_TEST
  74. #include <test.h>
  75. #endif /* CONFIG_TEST */
  76.  
  77. /** Kernel initialization thread.
  78.  *
  79.  * kinit takes care of higher level kernel
  80.  * initialization (i.e. thread creation,
  81.  * userspace initialization etc.).
  82.  *
  83.  * @param arg Not used.
  84.  */
  85. void kinit(void *arg)
  86. {
  87.     thread_t *t;
  88.  
  89.     /*
  90.      * Detach kinit as nobody will call thread_join_timeout() on it.
  91.      */
  92.     thread_detach(THREAD);
  93.  
  94.     interrupts_disable();
  95.  
  96. #ifdef CONFIG_SMP          
  97.     if (config.cpu_count > 1) {
  98.         /*
  99.          * Create the kmp thread and wait for its completion.
  100.          * cpu1 through cpuN-1 will come up consecutively and
  101.          * not mess together with kcpulb threads.
  102.          * Just a beautification.
  103.          */
  104.         if ((t = thread_create(kmp, NULL, TASK, 0, "kmp"))) {
  105.             spinlock_lock(&t->lock);
  106.             t->flags |= X_WIRED;
  107.             t->cpu = &cpus[0];
  108.             spinlock_unlock(&t->lock);
  109.             thread_ready(t);
  110.         }
  111.         else {
  112.             panic("thread_create/kmp\n");
  113.         }
  114.         thread_join(t);
  115.         thread_detach(t);
  116.     }
  117. #endif /* CONFIG_SMP */
  118.     /*
  119.      * Now that all CPUs are up, we can report what we've found.
  120.      */
  121.     cpu_list();
  122.  
  123. #ifdef CONFIG_SMP
  124.     if (config.cpu_count > 1) {
  125.         int i;
  126.        
  127.         /*
  128.          * For each CPU, create its load balancing thread.
  129.          */
  130.         for (i = 0; i < config.cpu_count; i++) {
  131.  
  132.             if ((t = thread_create(kcpulb, NULL, TASK, 0, "kcpulb"))) {
  133.                 spinlock_lock(&t->lock);           
  134.                 t->flags |= X_WIRED;
  135.                 t->cpu = &cpus[i];
  136.                 spinlock_unlock(&t->lock);
  137.                 thread_ready(t);
  138.             }
  139.             else panic("thread_create/kcpulb\n");
  140.  
  141.         }
  142.     }
  143. #endif /* CONFIG_SMP */
  144.  
  145.     /*
  146.      * At this point SMP, if present, is configured.
  147.      */
  148.     arch_post_smp_init();
  149.  
  150.     /*
  151.      * Create kernel console.
  152.      */
  153.     if ((t = thread_create(kconsole, "kconsole", TASK, 0, "kconsole")))
  154.         thread_ready(t);
  155.     else
  156.         panic("thread_create/kconsole\n");
  157.  
  158.     interrupts_enable();
  159.  
  160. #ifdef CONFIG_TEST
  161.     test();
  162.     printf("\nTest finished, please reboot\n");
  163. #else  /* CONFIG_TEST */
  164.  
  165.     task_t *utask;
  166.     count_t i;
  167.     for (i = 0; i < init.cnt; i++) {
  168.         /*
  169.          * Run user tasks.
  170.          */
  171.        
  172.         if (init.tasks[i].addr % FRAME_SIZE)
  173.             panic("init[%d].addr is not frame aligned", i);
  174.  
  175.         utask = task_run_program((void *) init.tasks[i].addr, "USPACE");
  176.         if (utask) {
  177.             /*
  178.              * Set capabilities to init userspace tasks.
  179.              */
  180.             cap_set(utask, CAP_CAP | CAP_MEM_MANAGER | CAP_IO_MANAGER | CAP_PREEMPT_CONTROL | CAP_IRQ_REG);
  181.            
  182.             if (!ipc_phone_0)
  183.                 ipc_phone_0 = &utask->answerbox;
  184.         } else
  185.             printf("Init task %zd not started.\n", i);
  186.     }
  187.  
  188.  
  189.     if (!stdin) {
  190.         while (1) {
  191.             thread_sleep(1);
  192.             printf("kinit... ");
  193.         }
  194.     }
  195. #endif /* CONFIG_TEST */
  196.  
  197. }
  198.  
  199.  /** @}
  200.  */
  201.  
  202.