Rev 628 | Rev 651 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 628 | Rev 640 | ||
---|---|---|---|
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 | #include <interrupt.h> |
44 | 44 | ||
45 | #ifdef CONFIG_SMP |
45 | #ifdef CONFIG_SMP |
46 | #include <arch/smp/apic.h> |
46 | #include <arch/smp/apic.h> |
47 | #include <arch/smp/mps.h> |
47 | #include <arch/smp/mps.h> |
48 | #endif /* CONFIG_SMP */ |
48 | #endif /* CONFIG_SMP */ |
49 | 49 | ||
50 | #include <smp/smp.h> |
50 | #include <smp/smp.h> |
51 | 51 | ||
52 | #include <arch/mm/memory_init.h> |
52 | #include <arch/mm/memory_init.h> |
53 | #include <mm/heap.h> |
53 | #include <mm/heap.h> |
54 | #include <mm/frame.h> |
54 | #include <mm/frame.h> |
55 | #include <mm/page.h> |
55 | #include <mm/page.h> |
56 | #include <mm/tlb.h> |
56 | #include <mm/tlb.h> |
57 | #include <mm/vm.h> |
57 | #include <mm/vm.h> |
58 | 58 | ||
59 | #include <synch/waitq.h> |
59 | #include <synch/waitq.h> |
60 | 60 | ||
61 | #include <arch/arch.h> |
61 | #include <arch/arch.h> |
62 | #include <arch.h> |
62 | #include <arch.h> |
63 | #include <arch/faddr.h> |
63 | #include <arch/faddr.h> |
64 | 64 | ||
65 | #include <typedefs.h> |
65 | #include <typedefs.h> |
66 | 66 | ||
67 | char *project = "SPARTAN kernel"; |
67 | char *project = "SPARTAN kernel"; |
68 | char *copyright = "Copyright (C) 2001-2005 HelenOS project"; |
68 | char *copyright = "Copyright (C) 2001-2005 HelenOS project"; |
69 | char *release = RELEASE; |
69 | char *release = RELEASE; |
70 | char *name = NAME; |
70 | char *name = NAME; |
71 | char *arch = ARCH; |
71 | char *arch = ARCH; |
72 | 72 | ||
73 | #ifdef REVISION |
73 | #ifdef REVISION |
74 | char *revision = ", revision " REVISION; |
74 | char *revision = ", revision " REVISION; |
75 | #else |
75 | #else |
76 | char *revision = ""; |
76 | char *revision = ""; |
77 | #endif |
77 | #endif |
78 | 78 | ||
79 | #ifdef TIMESTAMP |
79 | #ifdef TIMESTAMP |
80 | char *timestamp = " on " TIMESTAMP; |
80 | char *timestamp = " on " TIMESTAMP; |
81 | #else |
81 | #else |
82 | char *timestamp = ""; |
82 | char *timestamp = ""; |
83 | #endif |
83 | #endif |
84 | 84 | ||
85 | 85 | ||
86 | config_t config; |
86 | config_t config; |
87 | context_t ctx; |
87 | context_t ctx; |
88 | 88 | ||
89 | /** |
89 | /** |
90 | * These 'hardcoded' variables will be intialized by |
90 | * These 'hardcoded' variables will be intialized by |
91 | * the linker or the low level assembler code with |
91 | * the linker or the low level assembler code with |
92 | * appropriate sizes and addresses. |
92 | * appropriate sizes and addresses. |
93 | */ |
93 | */ |
94 | __address hardcoded_load_address = 0; |
94 | __address hardcoded_load_address = 0; |
95 | size_t hardcoded_ktext_size = 0; |
95 | size_t hardcoded_ktext_size = 0; |
96 | size_t hardcoded_kdata_size = 0; |
96 | size_t hardcoded_kdata_size = 0; |
97 | 97 | ||
98 | __address init_addr = 0; |
98 | __address init_addr = 0; |
99 | size_t init_size = 0; |
99 | size_t init_size = 0; |
100 | 100 | ||
101 | /** |
101 | /** |
102 | * Size of memory in bytes taken by kernel and heap. |
102 | * Size of memory in bytes taken by kernel and heap. |
103 | */ |
103 | */ |
104 | static size_t kernel_size; |
104 | static size_t kernel_size; |
105 | 105 | ||
106 | /** |
106 | /** |
107 | * Size of heap. |
107 | * Size of heap. |
108 | */ |
108 | */ |
109 | static size_t heap_size; |
109 | static size_t heap_size; |
110 | 110 | ||
111 | 111 | ||
112 | /** |
112 | /** |
113 | * Extra space between heap and stack |
113 | * Extra space between heap and stack |
114 | * enforced by alignment requirements. |
114 | * enforced by alignment requirements. |
115 | */ |
115 | */ |
116 | static size_t heap_delta; |
116 | static size_t heap_delta; |
117 | 117 | ||
118 | void main_bsp(void); |
118 | void main_bsp(void); |
119 | void main_ap(void); |
119 | void main_ap(void); |
120 | 120 | ||
121 | /* |
121 | /* |
122 | * These two functions prevent stack from underflowing during the |
122 | * These two functions prevent stack from underflowing during the |
123 | * 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 |
124 | * space. The stack could get corrupted by a fooled compiler-generated |
124 | * space. The stack could get corrupted by a fooled compiler-generated |
125 | * pop sequence otherwise. |
125 | * pop sequence otherwise. |
126 | */ |
126 | */ |
127 | static void main_bsp_separated_stack(void); |
127 | static void main_bsp_separated_stack(void); |
128 | #ifdef CONFIG_SMP |
128 | #ifdef CONFIG_SMP |
129 | static void main_ap_separated_stack(void); |
129 | static void main_ap_separated_stack(void); |
130 | #endif |
130 | #endif |
131 | 131 | ||
132 | /** Bootstrap CPU main kernel routine |
132 | /** Bootstrap CPU main kernel routine |
133 | * |
133 | * |
134 | * Initializes the kernel by bootstrap CPU. |
134 | * Initializes the kernel by bootstrap CPU. |
135 | * |
135 | * |
136 | * Assuming interrupts_disable(). |
136 | * Assuming interrupts_disable(). |
137 | * |
137 | * |
138 | */ |
138 | */ |
139 | void main_bsp(void) |
139 | void main_bsp(void) |
140 | { |
140 | { |
141 | config.cpu_count = 1; |
141 | config.cpu_count = 1; |
142 | config.cpu_active = 1; |
142 | config.cpu_active = 1; |
143 | config.base = hardcoded_load_address; |
143 | config.base = hardcoded_load_address; |
144 | config.memory_size = get_memory_size(); |
144 | config.memory_size = get_memory_size(); |
145 | config.init_addr = init_addr; |
145 | config.init_addr = init_addr; |
146 | config.init_size = init_size; |
146 | config.init_size = init_size; |
147 | 147 | ||
148 | heap_size = CONFIG_HEAP_SIZE + (config.memory_size/FRAME_SIZE)*sizeof(frame_t); |
148 | heap_size = CONFIG_HEAP_SIZE + (config.memory_size/FRAME_SIZE)*sizeof(frame_t); |
149 | kernel_size = ALIGN_UP(hardcoded_ktext_size + hardcoded_kdata_size + heap_size, PAGE_SIZE); |
149 | kernel_size = ALIGN_UP(hardcoded_ktext_size + hardcoded_kdata_size + heap_size, PAGE_SIZE); |
150 | heap_delta = kernel_size - (hardcoded_ktext_size + hardcoded_kdata_size + heap_size); |
150 | heap_delta = kernel_size - (hardcoded_ktext_size + hardcoded_kdata_size + heap_size); |
151 | 151 | ||
152 | config.kernel_size = kernel_size + CONFIG_STACK_SIZE; |
152 | config.kernel_size = kernel_size + CONFIG_STACK_SIZE; |
153 | 153 | ||
154 | context_save(&ctx); |
154 | context_save(&ctx); |
155 | early_mapping(config.base + hardcoded_ktext_size + hardcoded_kdata_size, CONFIG_STACK_SIZE + heap_size + heap_delta); |
155 | early_mapping(config.base + hardcoded_ktext_size + hardcoded_kdata_size, CONFIG_STACK_SIZE + heap_size + heap_delta); |
156 | context_set(&ctx, FADDR(main_bsp_separated_stack), config.base + kernel_size, CONFIG_STACK_SIZE); |
156 | context_set(&ctx, FADDR(main_bsp_separated_stack), config.base + kernel_size, CONFIG_STACK_SIZE); |
157 | context_restore(&ctx); |
157 | context_restore(&ctx); |
158 | /* not reached */ |
158 | /* not reached */ |
159 | } |
159 | } |
160 | 160 | ||
161 | 161 | ||
162 | /** Bootstrap CPU main kernel routine stack wrapper |
162 | /** Bootstrap CPU main kernel routine stack wrapper |
163 | * |
163 | * |
164 | * Second part of main_bsp(). |
164 | * Second part of main_bsp(). |
165 | * |
165 | * |
166 | */ |
166 | */ |
167 | void main_bsp_separated_stack(void) |
167 | void main_bsp_separated_stack(void) |
168 | { |
168 | { |
169 | vm_t *m; |
169 | vm_t *m; |
170 | task_t *k; |
170 | task_t *k; |
171 | thread_t *t; |
171 | thread_t *t; |
172 | 172 | ||
173 | the_initialize(THE); |
173 | the_initialize(THE); |
174 | 174 | ||
175 | /* |
175 | /* |
176 | * kconsole data structures must be initialized very early |
176 | * kconsole data structures must be initialized very early |
177 | * because other subsystems will register their respective |
177 | * because other subsystems will register their respective |
178 | * commands. |
178 | * commands. |
179 | */ |
179 | */ |
180 | kconsole_init(); |
180 | kconsole_init(); |
181 | 181 | ||
182 | /* Exception handler initialization, before architecture |
182 | /* Exception handler initialization, before architecture |
183 | * starts adding it's own handlers |
183 | * starts adding it's own handlers |
184 | */ |
184 | */ |
185 | exc_init(); |
185 | exc_init(); |
186 | 186 | ||
187 | arch_pre_mm_init(); |
187 | arch_pre_mm_init(); |
188 | early_heap_init(config.base + hardcoded_ktext_size + hardcoded_kdata_size, heap_size + heap_delta); |
188 | early_heap_init(config.base + hardcoded_ktext_size + hardcoded_kdata_size, heap_size + heap_delta); |
189 | frame_init(); |
189 | frame_init(); |
190 | page_init(); |
190 | page_init(); |
191 | tlb_init(); |
191 | tlb_init(); |
192 | arch_post_mm_init(); |
192 | arch_post_mm_init(); |
193 | 193 | ||
194 | printf("%s, release %s (%s)%s\nBuilt%s for %s\n%s\n", project, release, name, revision, timestamp, arch, copyright); |
194 | printf("%s, release %s (%s)%s\nBuilt%s for %s\n%s\n", project, release, name, revision, timestamp, arch, copyright); |
195 | printf("%P: hardcoded_ktext_size=%dK, hardcoded_kdata_size=%dK\n", |
195 | printf("%P: hardcoded_ktext_size=%dK, hardcoded_kdata_size=%dK\n", |
196 | config.base, hardcoded_ktext_size/1024, hardcoded_kdata_size/1024); |
196 | config.base, hardcoded_ktext_size/1024, hardcoded_kdata_size/1024); |
197 | 197 | ||
198 | arch_pre_smp_init(); |
198 | arch_pre_smp_init(); |
199 | smp_init(); |
199 | smp_init(); |
200 | printf("config.memory_size=%dM\n", config.memory_size/(1024*1024)); |
200 | printf("config.memory_size=%dM\n", config.memory_size/(1024*1024)); |
201 | printf("config.cpu_count=%d\n", config.cpu_count); |
201 | printf("config.cpu_count=%d\n", config.cpu_count); |
202 | 202 | ||
203 | cpu_init(); |
203 | cpu_init(); |
204 | 204 | ||
205 | calibrate_delay_loop(); |
205 | calibrate_delay_loop(); |
206 | 206 | ||
207 | timeout_init(); |
207 | timeout_init(); |
208 | scheduler_init(); |
208 | scheduler_init(); |
209 | task_init(); |
209 | task_init(); |
210 | thread_init(); |
210 | thread_init(); |
211 | 211 | ||
212 | if (config.init_size > 0) |
212 | if (config.init_size > 0) |
213 | printf("config.init_addr=%X, config.init_size=%d\n", config.init_addr, config.init_size); |
213 | printf("config.init_addr=%X, config.init_size=%d\n", config.init_addr, config.init_size); |
214 | 214 | ||
215 | /* |
215 | /* |
216 | * Create kernel vm mapping. |
216 | * Create kernel vm mapping. |
217 | */ |
217 | */ |
218 | m = vm_create(GET_PTL0_ADDRESS()); |
218 | m = vm_create(GET_PTL0_ADDRESS()); |
219 | if (!m) |
219 | if (!m) |
220 | panic("can't create kernel vm address space\n"); |
220 | panic("can't create kernel vm address space\n"); |
221 | 221 | ||
222 | /* |
222 | /* |
223 | * Create kernel task. |
223 | * Create kernel task. |
224 | */ |
224 | */ |
225 | k = task_create(m); |
225 | k = task_create(m); |
226 | if (!k) |
226 | if (!k) |
227 | panic("can't create kernel task\n"); |
227 | panic("can't create kernel task\n"); |
228 | 228 | ||
229 | /* |
229 | /* |
230 | * Create the first thread. |
230 | * Create the first thread. |
231 | */ |
231 | */ |
232 | t = thread_create(kinit, NULL, k, 0); |
232 | t = thread_create(kinit, NULL, k, 0); |
233 | if (!t) |
233 | if (!t) |
234 | panic("can't create kinit thread\n"); |
234 | panic("can't create kinit thread\n"); |
235 | thread_ready(t); |
235 | thread_ready(t); |
236 | /* |
236 | /* |
237 | * This call to scheduler() will return to kinit, |
237 | * This call to scheduler() will return to kinit, |
238 | * starting the thread of kernel threads. |
238 | * starting the thread of kernel threads. |
239 | */ |
239 | */ |
240 | scheduler(); |
240 | scheduler(); |
241 | /* not reached */ |
241 | /* not reached */ |
242 | } |
242 | } |
243 | 243 | ||
244 | 244 | ||
245 | #ifdef CONFIG_SMP |
245 | #ifdef CONFIG_SMP |
246 | /** Application CPUs main kernel routine |
246 | /** Application CPUs main kernel routine |
247 | * |
247 | * |
248 | * Executed by application processors, temporary stack |
248 | * Executed by application processors, temporary stack |
249 | * is at ctx.sp which was set during BP boot. |
249 | * is at ctx.sp which was set during BP boot. |
250 | * |
250 | * |
251 | * Assuming interrupts_disable()'d. |
251 | * Assuming interrupts_disable()'d. |
252 | * |
252 | * |
253 | */ |
253 | */ |
254 | void main_ap(void) |
254 | void main_ap(void) |
255 | { |
255 | { |
256 | /* |
256 | /* |
257 | * Incrementing the active CPU counter will guarantee that the |
257 | * Incrementing the active CPU counter will guarantee that the |
258 | * pm_init() will not attempt to build GDT and IDT tables again. |
258 | * pm_init() will not attempt to build GDT and IDT tables again. |
259 | * Neither frame_init() will do the complete thing. Neither cpu_init() |
259 | * Neither frame_init() will do the complete thing. Neither cpu_init() |
260 | * will do. |
260 | * will do. |
261 | */ |
261 | */ |
262 | config.cpu_active++; |
262 | config.cpu_active++; |
263 | 263 | ||
264 | /* |
264 | /* |
265 | * The THE structure is well defined because ctx.sp is used as stack. |
265 | * The THE structure is well defined because ctx.sp is used as stack. |
266 | */ |
266 | */ |
267 | the_initialize(THE); |
267 | the_initialize(THE); |
268 | 268 | ||
269 | arch_pre_mm_init(); |
269 | arch_pre_mm_init(); |
270 | frame_init(); |
270 | frame_init(); |
271 | page_init(); |
271 | page_init(); |
272 | tlb_init(); |
272 | tlb_init(); |
273 | arch_post_mm_init(); |
273 | arch_post_mm_init(); |
274 | 274 | ||
275 | cpu_init(); |
275 | cpu_init(); |
276 | 276 | ||
277 | calibrate_delay_loop(); |
277 | calibrate_delay_loop(); |
278 | 278 | ||
279 | l_apic_init(); |
279 | l_apic_init(); |
280 | l_apic_debug(); |
280 | l_apic_debug(); |
281 | 281 | ||
282 | the_copy(THE, (the_t *) CPU->stack); |
282 | the_copy(THE, (the_t *) CPU->stack); |
283 | 283 | ||
284 | /* |
284 | /* |
285 | * If we woke kmp up before we left the kernel stack, we could |
285 | * If we woke kmp up before we left the kernel stack, we could |
286 | * collide with another CPU coming up. To prevent this, we |
286 | * collide with another CPU coming up. To prevent this, we |
287 | * switch to this cpu's private stack prior to waking kmp up. |
287 | * switch to this cpu's private stack prior to waking kmp up. |
288 | */ |
288 | */ |
289 | context_set(&CPU->saved_context, FADDR(main_ap_separated_stack), (__address) CPU->stack, CPU_STACK_SIZE); |
289 | context_set(&CPU->saved_context, FADDR(main_ap_separated_stack), (__address) CPU->stack, CPU_STACK_SIZE); |
290 | context_restore(&CPU->saved_context); |
290 | context_restore(&CPU->saved_context); |
291 | /* not reached */ |
291 | /* not reached */ |
292 | } |
292 | } |
293 | 293 | ||
294 | 294 | ||
295 | /** Application CPUs main kernel routine stack wrapper |
295 | /** Application CPUs main kernel routine stack wrapper |
296 | * |
296 | * |
297 | * Second part of main_ap(). |
297 | * Second part of main_ap(). |
298 | * |
298 | * |
299 | */ |
299 | */ |
300 | void main_ap_separated_stack(void) |
300 | void main_ap_separated_stack(void) |
301 | { |
301 | { |
302 | /* |
302 | /* |
303 | * Configure timeouts for this cpu. |
303 | * Configure timeouts for this cpu. |
304 | */ |
304 | */ |
305 | timeout_init(); |
305 | timeout_init(); |
306 | 306 | ||
307 | waitq_wakeup(&ap_completion_wq, WAKEUP_FIRST); |
307 | waitq_wakeup(&ap_completion_wq, WAKEUP_FIRST); |
308 | scheduler(); |
308 | scheduler(); |
309 | /* not reached */ |
309 | /* not reached */ |
310 | } |
310 | } |
311 | #endif /* CONFIG_SMP */ |
311 | #endif /* CONFIG_SMP */ |
312 | 312 |