Subversion Repositories HelenOS

Rev

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