Subversion Repositories HelenOS

Rev

Rev 5 | 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 <arch/context.h>
  31. #include <print.h>
  32. #include <config.h>
  33. #include <time/clock.h>
  34. #include <proc/scheduler.h>
  35. #include <proc/thread.h>
  36. #include <proc/task.h>
  37. #include <mm/vm.h>
  38. #include <main/kinit.h>
  39. #include <cpu.h>
  40. #include <mm/heap.h>
  41.  
  42. #ifdef __SMP__
  43. #include <arch/apic.h>
  44. #include <arch/mp.h>
  45. #endif /* __SMP__ */
  46.  
  47. #include <mm/frame.h>
  48. #include <mm/page.h>
  49. #include <synch/waitq.h>
  50.  
  51. #include <arch.h>
  52.  
  53. char *project = "SPARTAN kernel";
  54. char *copyright = "Copyright (C) 2001-2005 Jakub Jermar";
  55.  
  56. config_t config;
  57. context_t ctx;
  58.  
  59. /*
  60.  * These 'hardcoded' variables will be intialised by
  61.  * the linker with appropriate sizes and addresses.
  62.  */
  63. __u32 hardcoded_load_address = 0;
  64. __u32 hardcoded_ktext_size = 0;
  65. __u32 hardcoded_kdata_size = 0;
  66.  
  67. void main_bsp(void);
  68. void main_ap(void);
  69.  
  70. /*
  71.  * These two functions prevent stack from underflowing during the
  72.  * kernel boot phase when SP is set to the very top of the reserved
  73.  * space. The stack could get corrupted by a fooled compiler-generated
  74.  * pop sequence otherwise.
  75.  */
  76. static void main_bsp_separated_stack(void);
  77. static void main_ap_separated_stack(void);
  78.  
  79. /*
  80.  * Executed by the bootstrap processor. cpu_priority_high()'d
  81.  */
  82. void main_bsp(void)
  83. {
  84.     config.cpu_count = 1;
  85.     config.cpu_active = 1;
  86.  
  87.     config.base = hardcoded_load_address;
  88.     config.memory_size = CONFIG_MEMORY_SIZE;
  89.     config.kernel_size = hardcoded_ktext_size + hardcoded_kdata_size + CONFIG_HEAP_SIZE + CONFIG_STACK_SIZE;
  90.  
  91.     context_save(&ctx);
  92.     ctx.sp = config.base + config.kernel_size - 8;
  93.     ctx.pc = (__address) main_bsp_separated_stack;
  94.     context_restore(&ctx);
  95.     /* not reached */
  96. }
  97.  
  98. void main_bsp_separated_stack(void) {
  99.     vm_t *m;
  100.     task_t *k;
  101.     thread_t *t;
  102.  
  103.     arch_init();
  104.  
  105.     printf("%s, %s\n", project, copyright);
  106.  
  107.     printf("%L: hardcoded_ktext_size=%dK, hardcoded_kdata_size=%dK\n",
  108.         config.base, hardcoded_ktext_size/1024, hardcoded_kdata_size/1024);
  109.  
  110.     heap_init(config.base + hardcoded_ktext_size + hardcoded_kdata_size, CONFIG_HEAP_SIZE);
  111.     frame_init();
  112.     page_init();
  113.  
  114.     #ifdef __SMP__
  115.     mp_init();  /* Multiprocessor */
  116.     #endif /* __SMP__ */
  117.  
  118.     cpu_init();
  119.     calibrate_delay_loop();
  120.  
  121.     timeout_init();
  122.     scheduler_init();
  123.     task_init();
  124.     thread_init();
  125.  
  126.     /*
  127.      * Create kernel vm mapping.
  128.      */
  129.     m = vm_create();
  130.     if (!m) panic("can't create kernel vm address space\n");
  131.  
  132.     /*
  133.      * Create kernel task.
  134.      */
  135.     k = task_create(m);
  136.     if (!k) panic("can't create kernel task\n");
  137.  
  138.     /*
  139.      * Create the first thread.
  140.      */
  141.     t = thread_create(kinit, NULL, k, 0);
  142.     if (!t) panic("can't create kinit thread\n");
  143.  
  144.     thread_ready(t);
  145.  
  146.     /*
  147.      * This call to scheduler() will return to kinit,
  148.      * starting the thread of kernel threads.
  149.      */
  150.     scheduler();
  151.     /* not reached */
  152. }
  153.  
  154. #ifdef __SMP__
  155. /*
  156.  * Executed by application processors. cpu_priority_high()'d
  157.  * Temporary stack is at ctx.sp which was set during BP boot.
  158.  */
  159. void main_ap(void)
  160. {
  161.     /*
  162.      * Incrementing the active CPU counter will guarantee that the
  163.      * pm_init() will not attempt to build GDT and IDT tables again.
  164.      * Neither frame_init() will do the complete thing. Neither cpu_init()
  165.      * will do.
  166.      */
  167.     config.cpu_active++;
  168.  
  169.     arch_init();
  170.     frame_init();
  171.     page_init();
  172.  
  173.     cpu_init();
  174.     calibrate_delay_loop();
  175.  
  176.     l_apic_init();
  177.     l_apic_debug();
  178.  
  179.  
  180.     /*
  181.      * If we woke kmp up before we left the kernel stack, we could
  182.      * collide with another CPU coming up. To prevent this, we
  183.      * switch to this cpu's private stack prior to waking kmp up.
  184.      */
  185.     the->cpu->saved_context.sp = (__address) &the->cpu->stack[CPU_STACK_SIZE-8];
  186.     the->cpu->saved_context.pc = (__address) main_ap_separated_stack;
  187.     context_restore(&the->cpu->saved_context);
  188.     /* not reached */
  189. }
  190.  
  191. void main_ap_separated_stack(void)
  192. {
  193.     /*
  194.      * Configure timeouts for this cpu.
  195.      */
  196.     timeout_init();
  197.  
  198.     waitq_wakeup(&ap_completion_wq, WAKEUP_FIRST);
  199.     scheduler();
  200.     /* not reached */
  201. }
  202. #endif /* __SMP__*/
  203.