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