00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00049 #include <arch/asm.h>
00050 #include <context.h>
00051 #include <print.h>
00052 #include <panic.h>
00053 #include <debug.h>
00054 #include <config.h>
00055 #include <time/clock.h>
00056 #include <proc/scheduler.h>
00057 #include <proc/thread.h>
00058 #include <proc/task.h>
00059 #include <main/kinit.h>
00060 #include <main/version.h>
00061 #include <console/kconsole.h>
00062 #include <cpu.h>
00063 #include <align.h>
00064 #include <interrupt.h>
00065 #include <arch/mm/memory_init.h>
00066 #include <mm/frame.h>
00067 #include <mm/page.h>
00068 #include <genarch/mm/page_pt.h>
00069 #include <mm/tlb.h>
00070 #include <mm/as.h>
00071 #include <mm/slab.h>
00072 #include <synch/waitq.h>
00073 #include <synch/futex.h>
00074 #include <arch/arch.h>
00075 #include <arch.h>
00076 #include <arch/faddr.h>
00077 #include <typedefs.h>
00078 #include <ipc/ipc.h>
00079 #include <macros.h>
00080 #include <adt/btree.h>
00081 #include <console/klog.h>
00082
00083 #ifdef CONFIG_SMP
00084 #include <arch/smp/apic.h>
00085 #include <arch/smp/mps.h>
00086 #endif
00087 #include <smp/smp.h>
00088
00090 config_t config = {
00091 .mm_initialized = false
00092 };
00093
00095 init_t init = {
00096 0
00097 };
00098
00099 context_t ctx;
00100
00106 __address hardcoded_load_address = 0;
00107 size_t hardcoded_ktext_size = 0;
00108 size_t hardcoded_kdata_size = 0;
00109
00110 void main_bsp(void);
00111 void main_ap(void);
00112
00113
00114
00115
00116
00117
00118
00119 static void main_bsp_separated_stack(void);
00120 #ifdef CONFIG_SMP
00121 static void main_ap_separated_stack(void);
00122 #endif
00123
00124 #define CONFIG_STACK_SIZE ((1<<STACK_FRAMES)*STACK_SIZE)
00125
00135 void main_bsp(void)
00136 {
00137 __address stackaddr;
00138
00139 config.cpu_count = 1;
00140 config.cpu_active = 1;
00141
00142 config.base = hardcoded_load_address;
00143 config.memory_size = get_memory_size();
00144
00145 config.kernel_size = ALIGN_UP(hardcoded_ktext_size + hardcoded_kdata_size, PAGE_SIZE);
00146 stackaddr = config.base + config.kernel_size;
00147
00148
00149 count_t i;
00150 bool overlap = false;
00151 for (i = 0; i < init.cnt; i++)
00152 if (PA_overlaps(stackaddr, CONFIG_STACK_SIZE, init.tasks[i].addr, init.tasks[i].size)) {
00153 stackaddr = ALIGN_UP(init.tasks[i].addr + init.tasks[i].size, CONFIG_STACK_SIZE);
00154 init.tasks[i].size = ALIGN_UP(init.tasks[i].size, CONFIG_STACK_SIZE) + CONFIG_STACK_SIZE;
00155 overlap = true;
00156 }
00157
00158 if (!overlap)
00159 config.kernel_size += CONFIG_STACK_SIZE;
00160
00161 context_save(&ctx);
00162 context_set(&ctx, FADDR(main_bsp_separated_stack), stackaddr, THREAD_STACK_SIZE);
00163 context_restore(&ctx);
00164
00165 }
00166
00167
00173 void main_bsp_separated_stack(void)
00174 {
00175 task_t *k;
00176 thread_t *t;
00177 count_t i;
00178
00179 the_initialize(THE);
00180
00181
00182
00183
00184
00185
00186 kconsole_init();
00187
00188
00189
00190
00191
00192 exc_init();
00193
00194
00195
00196
00197 arch_pre_mm_init();
00198 frame_init();
00199 slab_cache_init();
00200 btree_init();
00201 as_init();
00202 page_init();
00203 tlb_init();
00204 config.mm_initialized = true;
00205 arch_post_mm_init();
00206
00207 version_print();
00208 printf("%.*p: hardcoded_ktext_size=%zdK, hardcoded_kdata_size=%zdK\n", sizeof(__address) * 2, config.base, hardcoded_ktext_size >> 10, hardcoded_kdata_size >> 10);
00209
00210 arch_pre_smp_init();
00211 smp_init();
00212
00213 slab_enable_cpucache();
00214
00215 printf("config.memory_size=%zdM\n", config.memory_size >> 20);
00216 printf("config.cpu_count=%zd\n", config.cpu_count);
00217 cpu_init();
00218
00219 calibrate_delay_loop();
00220 clock_counter_init();
00221 timeout_init();
00222 scheduler_init();
00223 task_init();
00224 thread_init();
00225 futex_init();
00226 klog_init();
00227
00228 for (i = 0; i < init.cnt; i++)
00229 printf("init[%zd].addr=%.*p, init[%zd].size=%zd\n", i, sizeof(__address) * 2, init.tasks[i].addr, i, init.tasks[i].size);
00230
00231 ipc_init();
00232
00233
00234
00235
00236 k = task_create(AS_KERNEL, "KERNEL");
00237 if (!k)
00238 panic("can't create kernel task\n");
00239
00240
00241
00242
00243 t = thread_create(kinit, NULL, k, 0, "kinit");
00244 if (!t)
00245 panic("can't create kinit thread\n");
00246 thread_ready(t);
00247
00248
00249
00250
00251
00252 scheduler();
00253
00254 }
00255
00256
00257 #ifdef CONFIG_SMP
00258
00268 void main_ap(void)
00269 {
00270
00271
00272
00273
00274
00275
00276 config.cpu_active++;
00277
00278
00279
00280
00281 the_initialize(THE);
00282
00283 arch_pre_mm_init();
00284 frame_init();
00285 page_init();
00286 tlb_init();
00287 arch_post_mm_init();
00288
00289 cpu_init();
00290
00291 calibrate_delay_loop();
00292
00293 l_apic_init();
00294 l_apic_debug();
00295
00296 the_copy(THE, (the_t *) CPU->stack);
00297
00298
00299
00300
00301
00302
00303 context_set(&CPU->saved_context, FADDR(main_ap_separated_stack), (__address) CPU->stack, CPU_STACK_SIZE);
00304 context_restore(&CPU->saved_context);
00305
00306 }
00307
00308
00314 void main_ap_separated_stack(void)
00315 {
00316
00317
00318
00319 timeout_init();
00320
00321 waitq_wakeup(&ap_completion_wq, WAKEUP_FIRST);
00322 scheduler();
00323
00324 }
00325 #endif
00326