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