source: mainline/generic/src/main/main.c@ 05e2a7ad

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 05e2a7ad was 880de6e, checked in by Jakub Jermar <jakub@…>, 20 years ago

Clean up.

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