Rev 106 | Rev 146 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 106 | Rev 108 | ||
---|---|---|---|
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 <config.h> |
33 | #include <config.h> |
34 | #include <time/clock.h> |
34 | #include <time/clock.h> |
35 | #include <proc/scheduler.h> |
35 | #include <proc/scheduler.h> |
36 | #include <proc/thread.h> |
36 | #include <proc/thread.h> |
37 | #include <proc/task.h> |
37 | #include <proc/task.h> |
38 | #include <mm/vm.h> |
38 | #include <mm/vm.h> |
39 | #include <main/kinit.h> |
39 | #include <main/kinit.h> |
40 | #include <cpu.h> |
40 | #include <cpu.h> |
41 | #include <mm/heap.h> |
41 | #include <mm/heap.h> |
42 | 42 | ||
43 | #ifdef __SMP__ |
43 | #ifdef __SMP__ |
44 | #include <arch/smp/apic.h> |
44 | #include <arch/smp/apic.h> |
45 | #include <arch/smp/mps.h> |
45 | #include <arch/smp/mps.h> |
46 | #endif /* __SMP__ */ |
46 | #endif /* __SMP__ */ |
47 | 47 | ||
48 | #include <smp/smp.h> |
48 | #include <smp/smp.h> |
49 | 49 | ||
50 | #include <mm/frame.h> |
50 | #include <mm/frame.h> |
51 | #include <mm/page.h> |
51 | #include <mm/page.h> |
52 | #include <mm/tlb.h> |
52 | #include <mm/tlb.h> |
53 | #include <synch/waitq.h> |
53 | #include <synch/waitq.h> |
54 | 54 | ||
55 | #include <arch.h> |
55 | #include <arch.h> |
56 | #include <arch/faddr.h> |
56 | #include <arch/faddr.h> |
57 | 57 | ||
58 | #include <typedefs.h> |
58 | #include <typedefs.h> |
59 | 59 | ||
60 | char *project = "SPARTAN kernel"; |
60 | char *project = "SPARTAN kernel"; |
61 | char *copyright = "Copyright (C) 2001-2005 Jakub Jermar\nCopyright (C) 2005 HelenOS project"; |
61 | char *copyright = "Copyright (C) 2001-2005 Jakub Jermar\nCopyright (C) 2005 HelenOS project"; |
62 | 62 | ||
63 | config_t config; |
63 | config_t config; |
64 | context_t ctx; |
64 | context_t ctx; |
65 | 65 | ||
66 | /* |
66 | /* |
67 | * These 'hardcoded' variables will be intialised by |
67 | * These 'hardcoded' variables will be intialised by |
68 | * the linker or the low level assembler code with |
68 | * the linker or the low level assembler code with |
69 | * appropriate sizes and addresses. |
69 | * appropriate sizes and addresses. |
70 | */ |
70 | */ |
71 | __address hardcoded_load_address = 0; |
71 | __address hardcoded_load_address = 0; |
72 | size_t hardcoded_ktext_size = 0; |
72 | size_t hardcoded_ktext_size = 0; |
73 | size_t hardcoded_kdata_size = 0; |
73 | size_t hardcoded_kdata_size = 0; |
74 | 74 | ||
75 | void main_bsp(void); |
75 | void main_bsp(void); |
76 | void main_ap(void); |
76 | void main_ap(void); |
77 | 77 | ||
78 | /* |
78 | /* |
79 | * These two functions prevent stack from underflowing during the |
79 | * These two functions prevent stack from underflowing during the |
80 | * kernel boot phase when SP is set to the very top of the reserved |
80 | * kernel boot phase when SP is set to the very top of the reserved |
81 | * space. The stack could get corrupted by a fooled compiler-generated |
81 | * space. The stack could get corrupted by a fooled compiler-generated |
82 | * pop sequence otherwise. |
82 | * pop sequence otherwise. |
83 | */ |
83 | */ |
84 | static void main_bsp_separated_stack(void); |
84 | static void main_bsp_separated_stack(void); |
85 | static void main_ap_separated_stack(void); |
85 | static void main_ap_separated_stack(void); |
86 | 86 | ||
- | 87 | ||
- | 88 | /** Bootstrap CPU main kernel routine |
|
87 | /* |
89 | * |
- | 90 | * Initializes the kernel by bootstrap CPU. |
|
- | 91 | * |
|
88 | * Executed by the bootstrap processor. cpu_priority_high()'d |
92 | * Assuming cpu_priority_high(). |
- | 93 | * |
|
89 | */ |
94 | */ |
90 | void main_bsp(void) |
95 | void main_bsp(void) |
91 | { |
96 | { |
92 | config.cpu_count = 1; |
97 | config.cpu_count = 1; |
93 | config.cpu_active = 1; |
98 | config.cpu_active = 1; |
94 | 99 | ||
95 | config.base = hardcoded_load_address; |
100 | config.base = hardcoded_load_address; |
96 | config.memory_size = CONFIG_MEMORY_SIZE; |
101 | config.memory_size = CONFIG_MEMORY_SIZE; |
97 | config.kernel_size = hardcoded_ktext_size + hardcoded_kdata_size + CONFIG_HEAP_SIZE + CONFIG_STACK_SIZE; |
102 | config.kernel_size = hardcoded_ktext_size + hardcoded_kdata_size + CONFIG_HEAP_SIZE + CONFIG_STACK_SIZE; |
98 | 103 | ||
99 | context_save(&ctx); |
104 | context_save(&ctx); |
100 | context_set(&ctx, FADDR(main_bsp_separated_stack), config.base + hardcoded_ktext_size + hardcoded_kdata_size + CONFIG_HEAP_SIZE, CONFIG_STACK_SIZE); |
105 | context_set(&ctx, FADDR(main_bsp_separated_stack), config.base + hardcoded_ktext_size + hardcoded_kdata_size + CONFIG_HEAP_SIZE, CONFIG_STACK_SIZE); |
101 | context_restore(&ctx); |
106 | context_restore(&ctx); |
102 | /* not reached */ |
107 | /* not reached */ |
103 | } |
108 | } |
104 | 109 | ||
- | 110 | ||
- | 111 | /** Bootstrap CPU main kernel routine stack wrapper |
|
- | 112 | * |
|
- | 113 | * Second part of main_bsp(). |
|
- | 114 | * |
|
- | 115 | */ |
|
105 | void main_bsp_separated_stack(void) { |
116 | void main_bsp_separated_stack(void) { |
106 | vm_t *m; |
117 | vm_t *m; |
107 | task_t *k; |
118 | task_t *k; |
108 | thread_t *t; |
119 | thread_t *t; |
109 | 120 | ||
110 | arch_pre_mm_init(); |
121 | arch_pre_mm_init(); |
111 | 122 | ||
112 | heap_init(config.base + hardcoded_ktext_size + hardcoded_kdata_size, CONFIG_HEAP_SIZE); |
123 | heap_init(config.base + hardcoded_ktext_size + hardcoded_kdata_size, CONFIG_HEAP_SIZE); |
113 | frame_init(); |
124 | frame_init(); |
114 | page_init(); |
125 | page_init(); |
115 | tlb_init(); |
126 | tlb_init(); |
116 | 127 | ||
117 | arch_post_mm_init(); |
128 | arch_post_mm_init(); |
118 | 129 | ||
119 | printf("%s\n%s\n", project, copyright); |
130 | printf("%s\n%s\n", project, copyright); |
120 | printf("%P: hardcoded_ktext_size=%dK, hardcoded_kdata_size=%dK\n", |
131 | printf("%P: hardcoded_ktext_size=%dK, hardcoded_kdata_size=%dK\n", |
121 | config.base, hardcoded_ktext_size/1024, hardcoded_kdata_size/1024); |
132 | config.base, hardcoded_ktext_size/1024, hardcoded_kdata_size/1024); |
122 | 133 | ||
123 | arch_late_init(); |
134 | arch_late_init(); |
124 | 135 | ||
125 | smp_init(); |
136 | smp_init(); |
126 | printf("config.cpu_count=%d\n", config.cpu_count); |
137 | printf("config.cpu_count=%d\n", config.cpu_count); |
127 | 138 | ||
128 | cpu_init(); |
139 | cpu_init(); |
129 | calibrate_delay_loop(); |
140 | calibrate_delay_loop(); |
130 | 141 | ||
131 | timeout_init(); |
142 | timeout_init(); |
132 | scheduler_init(); |
143 | scheduler_init(); |
133 | task_init(); |
144 | task_init(); |
134 | thread_init(); |
145 | thread_init(); |
135 | 146 | ||
136 | /* |
147 | /* |
137 | * Create kernel vm mapping. |
148 | * Create kernel vm mapping. |
138 | */ |
149 | */ |
139 | m = vm_create(); |
150 | m = vm_create(); |
140 | if (!m) panic("can't create kernel vm address space\n"); |
151 | if (!m) panic("can't create kernel vm address space\n"); |
141 | 152 | ||
142 | /* |
153 | /* |
143 | * Create kernel task. |
154 | * Create kernel task. |
144 | */ |
155 | */ |
145 | k = task_create(m); |
156 | k = task_create(m); |
146 | if (!k) panic("can't create kernel task\n"); |
157 | if (!k) panic("can't create kernel task\n"); |
147 | 158 | ||
148 | /* |
159 | /* |
149 | * Create the first thread. |
160 | * Create the first thread. |
150 | */ |
161 | */ |
151 | t = thread_create(kinit, NULL, k, 0); |
162 | t = thread_create(kinit, NULL, k, 0); |
152 | if (!t) panic("can't create kinit thread\n"); |
163 | if (!t) panic("can't create kinit thread\n"); |
153 | 164 | ||
154 | thread_ready(t); |
165 | thread_ready(t); |
155 | 166 | ||
156 | /* |
167 | /* |
157 | * This call to scheduler() will return to kinit, |
168 | * This call to scheduler() will return to kinit, |
158 | * starting the thread of kernel threads. |
169 | * starting the thread of kernel threads. |
159 | */ |
170 | */ |
160 | scheduler(); |
171 | scheduler(); |
161 | /* not reached */ |
172 | /* not reached */ |
162 | } |
173 | } |
163 | 174 | ||
- | 175 | ||
164 | #ifdef __SMP__ |
176 | #ifdef __SMP__ |
- | 177 | /** Application CPUs main kernel routine |
|
165 | /* |
178 | * |
166 | * Executed by application processors. cpu_priority_high()'d |
179 | * Executed by application processors, temporary stack |
167 | * Temporary stack is at ctx.sp which was set during BP boot. |
180 | * is at ctx.sp which was set during BP boot. |
- | 181 | * |
|
- | 182 | * Assuming cpu_priority_high(). |
|
- | 183 | * |
|
168 | */ |
184 | */ |
169 | void main_ap(void) |
185 | void main_ap(void) |
170 | { |
186 | { |
171 | /* |
187 | /* |
172 | * Incrementing the active CPU counter will guarantee that the |
188 | * Incrementing the active CPU counter will guarantee that the |
173 | * pm_init() will not attempt to build GDT and IDT tables again. |
189 | * pm_init() will not attempt to build GDT and IDT tables again. |
174 | * Neither frame_init() will do the complete thing. Neither cpu_init() |
190 | * Neither frame_init() will do the complete thing. Neither cpu_init() |
175 | * will do. |
191 | * will do. |
176 | */ |
192 | */ |
177 | config.cpu_active++; |
193 | config.cpu_active++; |
178 | 194 | ||
179 | arch_pre_mm_init(); |
195 | arch_pre_mm_init(); |
180 | frame_init(); |
196 | frame_init(); |
181 | page_init(); |
197 | page_init(); |
182 | arch_post_mm_init(); |
198 | arch_post_mm_init(); |
183 | 199 | ||
184 | cpu_init(); |
200 | cpu_init(); |
185 | calibrate_delay_loop(); |
201 | calibrate_delay_loop(); |
186 | 202 | ||
187 | l_apic_init(); |
203 | l_apic_init(); |
188 | l_apic_debug(); |
204 | l_apic_debug(); |
189 | 205 | ||
190 | 206 | ||
191 | /* |
207 | /* |
192 | * If we woke kmp up before we left the kernel stack, we could |
208 | * If we woke kmp up before we left the kernel stack, we could |
193 | * collide with another CPU coming up. To prevent this, we |
209 | * collide with another CPU coming up. To prevent this, we |
194 | * switch to this cpu's private stack prior to waking kmp up. |
210 | * switch to this cpu's private stack prior to waking kmp up. |
195 | */ |
211 | */ |
196 | context_set(&CPU->saved_context, FADDR(main_ap_separated_stack), CPU->stack, CPU_STACK_SIZE); |
212 | context_set(&CPU->saved_context, FADDR(main_ap_separated_stack), CPU->stack, CPU_STACK_SIZE); |
197 | context_restore(&CPU->saved_context); |
213 | context_restore(&CPU->saved_context); |
198 | /* not reached */ |
214 | /* not reached */ |
199 | } |
215 | } |
200 | 216 | ||
- | 217 | ||
- | 218 | /** Application CPUs main kernel routine stack wrapper |
|
- | 219 | * |
|
- | 220 | * Second part of main_ap(). |
|
- | 221 | * |
|
- | 222 | */ |
|
201 | void main_ap_separated_stack(void) |
223 | void main_ap_separated_stack(void) |
202 | { |
224 | { |
203 | /* |
225 | /* |
204 | * Configure timeouts for this cpu. |
226 | * Configure timeouts for this cpu. |
205 | */ |
227 | */ |
206 | timeout_init(); |
228 | timeout_init(); |
207 | 229 | ||
208 | waitq_wakeup(&ap_completion_wq, WAKEUP_FIRST); |
230 | waitq_wakeup(&ap_completion_wq, WAKEUP_FIRST); |
209 | scheduler(); |
231 | scheduler(); |
210 | /* not reached */ |
232 | /* not reached */ |
211 | } |
233 | } |
212 | #endif /* __SMP__*/ |
234 | #endif /* __SMP__*/ |
213 | 235 |