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