Subversion Repositories HelenOS-historic

Rev

Rev 564 | Rev 623 | 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. #include <arch/asm.h>
  30. #include <context.h>
  31. #include <print.h>
  32. #include <panic.h>
  33. #include <debug.h>
  34. #include <config.h>
  35. #include <time/clock.h>
  36. #include <proc/scheduler.h>
  37. #include <proc/thread.h>
  38. #include <proc/task.h>
  39. #include <main/kinit.h>
  40. #include <console/kconsole.h>
  41. #include <cpu.h>
  42. #include <align.h>
  43. #include <interrupt.h>
  44.  
  45. #ifdef CONFIG_SMP
  46. #include <arch/smp/apic.h>
  47. #include <arch/smp/mps.h>
  48. #endif /* CONFIG_SMP */
  49.  
  50. #include <smp/smp.h>
  51.  
  52. #include <arch/mm/memory_init.h>
  53. #include <mm/heap.h>
  54. #include <mm/frame.h>
  55. #include <mm/page.h>
  56. #include <mm/tlb.h>
  57. #include <mm/vm.h>
  58.  
  59. #include <synch/waitq.h>
  60.  
  61. #include <arch/arch.h>
  62. #include <arch.h>
  63. #include <arch/faddr.h>
  64.  
  65. #include <typedefs.h>
  66.  
  67. char *project = "SPARTAN kernel";
  68. char *copyright = "Copyright (C) 2001-2005 HelenOS project";
  69. char *release = RELEASE;
  70. char *name = NAME;
  71. char *arch = STRING(ARCH);
  72.  
  73. #ifdef REVISION
  74.     char *revision = ", revision " REVISION;
  75. #else
  76.     char *revision = "";
  77. #endif
  78.  
  79. #ifdef TIMESTAMP
  80.     char *timestamp = " on " TIMESTAMP;
  81. #else
  82.     char *timestamp = "";
  83. #endif
  84.  
  85.  
  86. config_t config;
  87. context_t ctx;
  88.  
  89. /**
  90.  * These 'hardcoded' variables will be intialized by
  91.  * the linker or the low level assembler code with
  92.  * appropriate sizes and addresses.
  93.  */
  94. __address hardcoded_load_address = 0;
  95. size_t hardcoded_ktext_size = 0;
  96. size_t hardcoded_kdata_size = 0;
  97.  
  98. __address init_addr = 0;
  99. size_t init_size = 0;
  100.  
  101. /**
  102.  * Size of memory in bytes taken by kernel and heap.
  103.  */
  104. static size_t kernel_size;
  105.  
  106. /**
  107.  * Size of heap.
  108.  */
  109. static size_t heap_size;
  110.  
  111.  
  112. /**
  113.  * Extra space between heap and stack
  114.  * enforced by alignment requirements.
  115.  */
  116. static size_t heap_delta;
  117.  
  118. void main_bsp(void);
  119. void main_ap(void);
  120.  
  121. /*
  122.  * These two functions prevent stack from underflowing during the
  123.  * kernel boot phase when SP is set to the very top of the reserved
  124.  * space. The stack could get corrupted by a fooled compiler-generated
  125.  * pop sequence otherwise.
  126.  */
  127. static void main_bsp_separated_stack(void);
  128. static void main_ap_separated_stack(void);
  129.  
  130. /** Bootstrap CPU main kernel routine
  131.  *
  132.  * Initializes the kernel by bootstrap CPU.
  133.  *
  134.  * Assuming interrupts_disable().
  135.  *
  136.  */
  137. void main_bsp(void)
  138. {
  139.     config.cpu_count = 1;
  140.     config.cpu_active = 1;
  141.     config.base = hardcoded_load_address;
  142.     config.memory_size = get_memory_size();
  143.  
  144.     heap_size = CONFIG_HEAP_SIZE + (config.memory_size/FRAME_SIZE)*sizeof(frame_t);
  145.     kernel_size = ALIGN_UP(hardcoded_ktext_size + hardcoded_kdata_size + heap_size, PAGE_SIZE);
  146.     heap_delta = kernel_size - (hardcoded_ktext_size + hardcoded_kdata_size + heap_size);
  147.    
  148.     config.kernel_size = kernel_size + CONFIG_STACK_SIZE;
  149.    
  150.     context_save(&ctx);
  151.     early_mapping(config.base + hardcoded_ktext_size + hardcoded_kdata_size, CONFIG_STACK_SIZE + heap_size + heap_delta);
  152.     context_set(&ctx, FADDR(main_bsp_separated_stack), config.base + kernel_size, CONFIG_STACK_SIZE);
  153.     context_restore(&ctx);
  154.     /* not reached */
  155. }
  156.  
  157.  
  158. /** Bootstrap CPU main kernel routine stack wrapper
  159.  *
  160.  * Second part of main_bsp().
  161.  *
  162.  */
  163. void main_bsp_separated_stack(void)
  164. {
  165.     vm_t *m;
  166.     task_t *k;
  167.     thread_t *t;
  168.    
  169.     the_initialize(THE);
  170.    
  171.     /*
  172.      * kconsole data structures must be initialized very early
  173.      * because other subsystems will register their respective
  174.      * commands.
  175.      */
  176.     kconsole_init();
  177.     /* Exception handler initialization, before architecture
  178.      * starts adding it's own handlers
  179.      */
  180.     exc_init();
  181.    
  182.     arch_pre_mm_init();
  183.     early_heap_init(config.base + hardcoded_ktext_size + hardcoded_kdata_size, heap_size + heap_delta);
  184.     frame_init();
  185.     page_init();
  186.     tlb_init();
  187.     arch_post_mm_init();
  188.  
  189.     printf("%s, release %s (%s)%s\nBuilt%s for %s\n%s\n", project, release, name, revision, timestamp, arch, copyright);
  190.     printf("%P: hardcoded_ktext_size=%dK, hardcoded_kdata_size=%dK\n",
  191.         config.base, hardcoded_ktext_size/1024, hardcoded_kdata_size/1024);
  192.  
  193.     arch_pre_smp_init();
  194.     smp_init();
  195.     printf("config.memory_size=%dM\n", config.memory_size/(1024*1024));
  196.     printf("config.cpu_count=%d\n", config.cpu_count);
  197.  
  198.     cpu_init();
  199.  
  200.     calibrate_delay_loop();
  201.    
  202.     timeout_init();
  203.     scheduler_init();
  204.     task_init();
  205.     thread_init();
  206.  
  207.     /*
  208.      * Create kernel vm mapping.
  209.      */
  210.     m = vm_create(GET_PTL0_ADDRESS());
  211.     if (!m)
  212.         panic("can't create kernel vm address space\n");
  213.  
  214.     /*
  215.      * Create kernel task.
  216.      */
  217.     k = task_create(m);
  218.     if (!k)
  219.         panic("can't create kernel task\n");
  220.        
  221.     /*
  222.      * Create the first thread.
  223.      */
  224.     t = thread_create(kinit, NULL, k, 0);
  225.     if (!t)
  226.         panic("can't create kinit thread\n");
  227.     thread_ready(t);
  228.     /*
  229.      * This call to scheduler() will return to kinit,
  230.      * starting the thread of kernel threads.
  231.      */
  232.     scheduler();
  233.     /* not reached */
  234. }
  235.  
  236.  
  237. #ifdef CONFIG_SMP
  238. /** Application CPUs main kernel routine
  239.  *
  240.  * Executed by application processors, temporary stack
  241.  * is at ctx.sp which was set during BP boot.
  242.  *
  243.  * Assuming interrupts_disable()'d.
  244.  *
  245.  */
  246. void main_ap(void)
  247. {
  248.     /*
  249.      * Incrementing the active CPU counter will guarantee that the
  250.      * pm_init() will not attempt to build GDT and IDT tables again.
  251.      * Neither frame_init() will do the complete thing. Neither cpu_init()
  252.      * will do.
  253.      */
  254.     config.cpu_active++;
  255.  
  256.     /*
  257.      * The THE structure is well defined because ctx.sp is used as stack.
  258.      */
  259.     the_initialize(THE);
  260.    
  261.     arch_pre_mm_init();
  262.     frame_init();
  263.     page_init();
  264.     tlb_init();
  265.     arch_post_mm_init();
  266.    
  267.     cpu_init();
  268.    
  269.     calibrate_delay_loop();
  270.  
  271.     l_apic_init();
  272.     l_apic_debug();
  273.  
  274.     the_copy(THE, (the_t *) CPU->stack);
  275.  
  276.     /*
  277.      * If we woke kmp up before we left the kernel stack, we could
  278.      * collide with another CPU coming up. To prevent this, we
  279.      * switch to this cpu's private stack prior to waking kmp up.
  280.      */
  281.     context_set(&CPU->saved_context, FADDR(main_ap_separated_stack), (__address) CPU->stack, CPU_STACK_SIZE);
  282.     context_restore(&CPU->saved_context);
  283.     /* not reached */
  284. }
  285.  
  286.  
  287. /** Application CPUs main kernel routine stack wrapper
  288.  *
  289.  * Second part of main_ap().
  290.  *
  291.  */
  292. void main_ap_separated_stack(void)
  293. {
  294.     /*
  295.      * Configure timeouts for this cpu.
  296.      */
  297.     timeout_init();
  298.  
  299.     waitq_wakeup(&ap_completion_wq, WAKEUP_FIRST);
  300.     scheduler();
  301.     /* not reached */
  302. }
  303. #endif /* CONFIG_SMP */
  304.