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