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