Rev 3982 | Rev 4022 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3982 | Rev 4015 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2001-2004 Jakub Jermar |
2 | * Copyright (c) 2001-2004 Jakub Jermar |
- | 3 | * Copyright (c) 2009 Jiri Svoboda |
|
- | 4 | * Copyright (c) 2009 Martin Decky |
|
3 | * All rights reserved. |
5 | * All rights reserved. |
4 | * |
6 | * |
5 | * Redistribution and use in source and binary forms, with or without |
7 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
8 | * modification, are permitted provided that the following conditions |
7 | * are met: |
9 | * are met: |
8 | * |
10 | * |
9 | * - Redistributions of source code must retain the above copyright |
11 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
12 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
13 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
14 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
15 | * documentation and/or other materials provided with the distribution. |
14 | * - The name of the author may not be used to endorse or promote products |
16 | * - The name of the author may not be used to endorse or promote products |
15 | * derived from this software without specific prior written permission. |
17 | * derived from this software without specific prior written permission. |
16 | * |
18 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
20 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
21 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
22 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
24 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
27 | * (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. |
28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
29 | */ |
28 | 30 | ||
29 | /** @addtogroup ia32 |
31 | /** @addtogroup ia32 |
30 | * @{ |
32 | * @{ |
31 | */ |
33 | */ |
32 | /** @file |
34 | /** @file |
33 | */ |
35 | */ |
34 | 36 | ||
35 | #include <arch.h> |
37 | #include <arch.h> |
36 | 38 | ||
37 | #include <arch/types.h> |
39 | #include <arch/types.h> |
38 | 40 | ||
39 | #include <arch/pm.h> |
41 | #include <arch/pm.h> |
40 | 42 | ||
41 | #include <genarch/drivers/legacy/ia32/io.h> |
43 | #include <genarch/drivers/legacy/ia32/io.h> |
42 | #include <genarch/drivers/ega/ega.h> |
44 | #include <genarch/drivers/ega/ega.h> |
43 | #include <arch/drivers/vesa.h> |
45 | #include <arch/drivers/vesa.h> |
44 | #include <genarch/kbd/i8042.h> |
46 | #include <genarch/kbd/i8042.h> |
45 | #include <arch/drivers/i8254.h> |
47 | #include <arch/drivers/i8254.h> |
46 | #include <arch/drivers/i8259.h> |
48 | #include <arch/drivers/i8259.h> |
47 | 49 | ||
48 | #include <arch/context.h> |
50 | #include <arch/context.h> |
49 | 51 | ||
50 | #include <config.h> |
52 | #include <config.h> |
51 | 53 | ||
52 | #include <arch/interrupt.h> |
54 | #include <arch/interrupt.h> |
53 | #include <arch/asm.h> |
55 | #include <arch/asm.h> |
54 | #include <genarch/acpi/acpi.h> |
56 | #include <genarch/acpi/acpi.h> |
55 | 57 | ||
56 | #include <arch/bios/bios.h> |
58 | #include <arch/bios/bios.h> |
57 | 59 | ||
58 | #include <interrupt.h> |
60 | #include <interrupt.h> |
59 | #include <ddi/irq.h> |
61 | #include <ddi/irq.h> |
60 | #include <arch/debugger.h> |
62 | #include <arch/debugger.h> |
61 | #include <proc/thread.h> |
63 | #include <proc/thread.h> |
62 | #include <syscall/syscall.h> |
64 | #include <syscall/syscall.h> |
63 | #include <console/console.h> |
65 | #include <console/console.h> |
64 | #include <ddi/device.h> |
66 | #include <ddi/device.h> |
65 | #include <sysinfo/sysinfo.h> |
67 | #include <sysinfo/sysinfo.h> |
- | 68 | #include <arch/boot/boot.h> |
|
- | 69 | #include <string.h> |
|
- | 70 | #include <macros.h> |
|
66 | 71 | ||
67 | #ifdef CONFIG_SMP |
72 | #ifdef CONFIG_SMP |
68 | #include <arch/smp/apic.h> |
73 | #include <arch/smp/apic.h> |
69 | #endif |
74 | #endif |
70 | 75 | ||
- | 76 | /** Extract command name from the multiboot module command line. |
|
- | 77 | * |
|
- | 78 | * @param buf Destination buffer (will always null-terminate). |
|
- | 79 | * @param n Size of destination buffer. |
|
- | 80 | * @param cmd_line Input string (the command line). |
|
- | 81 | * |
|
- | 82 | */ |
|
- | 83 | static void extract_command(char *buf, size_t n, const char *cmd_line) |
|
- | 84 | { |
|
- | 85 | const char *start, *end, *cp; |
|
- | 86 | size_t max_len; |
|
- | 87 | ||
- | 88 | /* Find the first space. */ |
|
- | 89 | end = strchr(cmd_line, ' '); |
|
- | 90 | if (end == NULL) |
|
- | 91 | end = cmd_line + strlen(cmd_line); |
|
- | 92 | ||
- | 93 | /* |
|
- | 94 | * Find last occurence of '/' before 'end'. If found, place start at |
|
- | 95 | * next character. Otherwise, place start at beginning of buffer. |
|
- | 96 | */ |
|
- | 97 | cp = end; |
|
- | 98 | start = buf; |
|
- | 99 | while (cp != start) { |
|
- | 100 | if (*cp == '/') { |
|
- | 101 | start = cp + 1; |
|
- | 102 | break; |
|
- | 103 | } |
|
- | 104 | --cp; |
|
- | 105 | } |
|
- | 106 | ||
- | 107 | /* Copy the command and null-terminate the string. */ |
|
- | 108 | max_len = min(n - 1, (size_t) (end - start)); |
|
- | 109 | strncpy(buf, start, max_len + 1); |
|
- | 110 | buf[max_len] = '\0'; |
|
- | 111 | } |
|
- | 112 | ||
- | 113 | /** C part of ia32 boot sequence. |
|
- | 114 | * |
|
- | 115 | * @param signature Should contain the multiboot signature. |
|
- | 116 | * @param mi Pointer to the multiboot information structure. |
|
- | 117 | */ |
|
- | 118 | void arch_pre_main(uint32_t signature, const mb_info_t *mi) |
|
- | 119 | { |
|
- | 120 | uint32_t flags; |
|
- | 121 | mb_mod_t *mods; |
|
- | 122 | uint32_t i; |
|
- | 123 | ||
- | 124 | if (signature == MULTIBOOT_LOADER_MAGIC) |
|
- | 125 | flags = mi->flags; |
|
- | 126 | else { |
|
- | 127 | /* No multiboot info available. */ |
|
- | 128 | flags = 0; |
|
- | 129 | } |
|
- | 130 | ||
- | 131 | /* Copy module information. */ |
|
- | 132 | ||
- | 133 | if ((flags & MBINFO_FLAGS_MODS) != 0) { |
|
- | 134 | init.cnt = mi->mods_count; |
|
- | 135 | mods = mi->mods_addr; |
|
- | 136 | ||
- | 137 | for (i = 0; i < init.cnt; i++) { |
|
- | 138 | init.tasks[i].addr = mods[i].start + 0x80000000; |
|
- | 139 | init.tasks[i].size = mods[i].end - mods[i].start; |
|
- | 140 | ||
- | 141 | /* Copy command line, if available. */ |
|
- | 142 | if (mods[i].string) { |
|
- | 143 | extract_command(init.tasks[i].name, |
|
- | 144 | CONFIG_TASK_NAME_BUFLEN, |
|
- | 145 | mods[i].string); |
|
- | 146 | } else |
|
- | 147 | init.tasks[i].name[0] = '\0'; |
|
- | 148 | } |
|
- | 149 | } else |
|
- | 150 | init.cnt = 0; |
|
- | 151 | ||
- | 152 | /* Copy memory map. */ |
|
- | 153 | ||
- | 154 | int32_t mmap_length; |
|
- | 155 | mb_mmap_t *mme; |
|
- | 156 | uint32_t size; |
|
- | 157 | ||
- | 158 | if ((flags & MBINFO_FLAGS_MMAP) != 0) { |
|
- | 159 | mmap_length = mi->mmap_length; |
|
- | 160 | mme = mi->mmap_addr; |
|
- | 161 | e820counter = 0; |
|
- | 162 | ||
- | 163 | i = 0; |
|
- | 164 | while (mmap_length > 0) { |
|
- | 165 | e820table[i++] = mme->mm_info; |
|
- | 166 | ||
- | 167 | /* Compute address of next structure. */ |
|
- | 168 | size = sizeof(mme->size) + mme->size; |
|
- | 169 | mme = ((void *) mme) + size; |
|
- | 170 | mmap_length -= size; |
|
- | 171 | } |
|
- | 172 | ||
- | 173 | e820counter = i; |
|
- | 174 | } else |
|
- | 175 | e820counter = 0; |
|
- | 176 | ||
- | 177 | #ifdef CONFIG_SMP |
|
- | 178 | /* Copy AP bootstrap routines below 1 MB. */ |
|
- | 179 | memcpy((void *) AP_BOOT_OFFSET, (void *) BOOT_OFFSET, |
|
- | 180 | (size_t) &_hardcoded_unmapped_size); |
|
- | 181 | #endif |
|
- | 182 | } |
|
- | 183 | ||
71 | void arch_pre_mm_init(void) |
184 | void arch_pre_mm_init(void) |
72 | { |
185 | { |
73 | pm_init(); |
186 | pm_init(); |
74 | 187 | ||
75 | if (config.cpu_active == 1) { |
188 | if (config.cpu_active == 1) { |
76 | interrupt_init(); |
189 | interrupt_init(); |
77 | bios_init(); |
190 | bios_init(); |
78 | 191 | ||
79 | /* PIC */ |
192 | /* PIC */ |
80 | i8259_init(); |
193 | i8259_init(); |
81 | } |
194 | } |
82 | } |
195 | } |
83 | 196 | ||
84 | void arch_post_mm_init(void) |
197 | void arch_post_mm_init(void) |
85 | { |
198 | { |
86 | if (config.cpu_active == 1) { |
199 | if (config.cpu_active == 1) { |
87 | /* Initialize IRQ routing */ |
200 | /* Initialize IRQ routing */ |
88 | irq_init(IRQ_COUNT, IRQ_COUNT); |
201 | irq_init(IRQ_COUNT, IRQ_COUNT); |
89 | 202 | ||
90 | /* hard clock */ |
203 | /* hard clock */ |
91 | i8254_init(); |
204 | i8254_init(); |
92 | 205 | ||
93 | #ifdef CONFIG_FB |
206 | #ifdef CONFIG_FB |
94 | if (vesa_present()) |
207 | if (vesa_present()) |
95 | vesa_init(); |
208 | vesa_init(); |
96 | else |
209 | else |
97 | #endif |
210 | #endif |
98 | ega_init(EGA_BASE, EGA_VIDEORAM); /* video */ |
211 | ega_init(EGA_BASE, EGA_VIDEORAM); /* video */ |
99 | 212 | ||
100 | /* Enable debugger */ |
213 | /* Enable debugger */ |
101 | debugger_init(); |
214 | debugger_init(); |
102 | /* Merge all memory zones to 1 big zone */ |
215 | /* Merge all memory zones to 1 big zone */ |
103 | zone_merge_all(); |
216 | zone_merge_all(); |
104 | } |
217 | } |
105 | } |
218 | } |
106 | 219 | ||
107 | void arch_post_cpu_init() |
220 | void arch_post_cpu_init() |
108 | { |
221 | { |
109 | #ifdef CONFIG_SMP |
222 | #ifdef CONFIG_SMP |
110 | if (config.cpu_active > 1) { |
223 | if (config.cpu_active > 1) { |
111 | l_apic_init(); |
224 | l_apic_init(); |
112 | l_apic_debug(); |
225 | l_apic_debug(); |
113 | } |
226 | } |
114 | #endif |
227 | #endif |
115 | } |
228 | } |
116 | 229 | ||
117 | void arch_pre_smp_init(void) |
230 | void arch_pre_smp_init(void) |
118 | { |
231 | { |
119 | if (config.cpu_active == 1) { |
232 | if (config.cpu_active == 1) { |
120 | #ifdef CONFIG_SMP |
233 | #ifdef CONFIG_SMP |
121 | acpi_init(); |
234 | acpi_init(); |
122 | #endif /* CONFIG_SMP */ |
235 | #endif /* CONFIG_SMP */ |
123 | } |
236 | } |
124 | } |
237 | } |
125 | 238 | ||
126 | void arch_post_smp_init(void) |
239 | void arch_post_smp_init(void) |
127 | { |
240 | { |
128 | devno_t devno = device_assign_devno(); |
241 | devno_t devno = device_assign_devno(); |
129 | /* keyboard controller */ |
242 | /* keyboard controller */ |
130 | (void) i8042_init((i8042_t *) I8042_BASE, devno, IRQ_KBD); |
243 | (void) i8042_init((i8042_t *) I8042_BASE, devno, IRQ_KBD); |
131 | 244 | ||
132 | /* |
245 | /* |
133 | * This is the necessary evil until the userspace driver is entirely |
246 | * This is the necessary evil until the userspace driver is entirely |
134 | * self-sufficient. |
247 | * self-sufficient. |
135 | */ |
248 | */ |
136 | sysinfo_set_item_val("kbd", NULL, true); |
249 | sysinfo_set_item_val("kbd", NULL, true); |
137 | sysinfo_set_item_val("kbd.devno", NULL, devno); |
250 | sysinfo_set_item_val("kbd.devno", NULL, devno); |
138 | sysinfo_set_item_val("kbd.inr", NULL, IRQ_KBD); |
251 | sysinfo_set_item_val("kbd.inr", NULL, IRQ_KBD); |
139 | } |
252 | } |
140 | 253 | ||
141 | void calibrate_delay_loop(void) |
254 | void calibrate_delay_loop(void) |
142 | { |
255 | { |
143 | i8254_calibrate_delay_loop(); |
256 | i8254_calibrate_delay_loop(); |
144 | if (config.cpu_active == 1) { |
257 | if (config.cpu_active == 1) { |
145 | /* |
258 | /* |
146 | * This has to be done only on UP. |
259 | * This has to be done only on UP. |
147 | * On SMP, i8254 is not used for time keeping and its interrupt pin remains masked. |
260 | * On SMP, i8254 is not used for time keeping and its interrupt pin remains masked. |
148 | */ |
261 | */ |
149 | i8254_normal_operation(); |
262 | i8254_normal_operation(); |
150 | } |
263 | } |
151 | } |
264 | } |
152 | 265 | ||
153 | /** Set thread-local-storage pointer |
266 | /** Set thread-local-storage pointer |
154 | * |
267 | * |
155 | * TLS pointer is set in GS register. That means, the GS contains |
268 | * TLS pointer is set in GS register. That means, the GS contains |
156 | * selector, and the descriptor->base is the correct address. |
269 | * selector, and the descriptor->base is the correct address. |
157 | */ |
270 | */ |
158 | unative_t sys_tls_set(unative_t addr) |
271 | unative_t sys_tls_set(unative_t addr) |
159 | { |
272 | { |
160 | THREAD->arch.tls = addr; |
273 | THREAD->arch.tls = addr; |
161 | set_tls_desc(addr); |
274 | set_tls_desc(addr); |
162 | 275 | ||
163 | return 0; |
276 | return 0; |
164 | } |
277 | } |
165 | 278 | ||
166 | /** Acquire console back for kernel |
279 | /** Acquire console back for kernel |
167 | * |
280 | * |
168 | */ |
281 | */ |
169 | void arch_grab_console(void) |
282 | void arch_grab_console(void) |
170 | { |
283 | { |
171 | #ifdef CONFIG_FB |
284 | #ifdef CONFIG_FB |
172 | vesa_redraw(); |
285 | vesa_redraw(); |
173 | #else |
286 | #else |
174 | ega_redraw(); |
287 | ega_redraw(); |
175 | #endif |
288 | #endif |
176 | } |
289 | } |
177 | 290 | ||
178 | /** Return console to userspace |
291 | /** Return console to userspace |
179 | * |
292 | * |
180 | */ |
293 | */ |
181 | void arch_release_console(void) |
294 | void arch_release_console(void) |
182 | { |
295 | { |
183 | } |
296 | } |
184 | 297 | ||
185 | /** Construct function pointer |
298 | /** Construct function pointer |
186 | * |
299 | * |
187 | * @param fptr function pointer structure |
300 | * @param fptr function pointer structure |
188 | * @param addr function address |
301 | * @param addr function address |
189 | * @param caller calling function address |
302 | * @param caller calling function address |
190 | * |
303 | * |
191 | * @return address of the function pointer |
304 | * @return address of the function pointer |
192 | * |
305 | * |
193 | */ |
306 | */ |
194 | void *arch_construct_function(fncptr_t *fptr, void *addr, void *caller) |
307 | void *arch_construct_function(fncptr_t *fptr, void *addr, void *caller) |
195 | { |
308 | { |
196 | return addr; |
309 | return addr; |
197 | } |
310 | } |
198 | 311 | ||
199 | /** @} |
312 | /** @} |
200 | */ |
313 | */ |
201 | 314 |