source: mainline/kernel/arch/amd64/src/amd64.c@ 1f5c9c96

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1f5c9c96 was 1f5c9c96, checked in by Martin Decky <martin@…>, 14 years ago

implement multiboot v2 specification and use it in GRUB for UEFI

  • improve multiboot v1 code, move defines to a common location
  • rename VESA framebuffer stuff to generic "boot framebuffer"
  • small collateral changes
  • Property mode set to 100644
File size: 7.0 KB
Line 
1/*
2 * Copyright (c) 2005 Ondrej Palkovsky
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
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
15 * derived from this software without specific prior written permission.
16 *
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
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
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
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
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup amd64
30 * @{
31 */
32/** @file
33 */
34
35#include <arch.h>
36
37#include <typedefs.h>
38
39#include <config.h>
40
41#include <proc/thread.h>
42#include <genarch/multiboot/multiboot.h>
43#include <genarch/multiboot/multiboot2.h>
44#include <genarch/drivers/legacy/ia32/io.h>
45#include <genarch/drivers/ega/ega.h>
46#include <genarch/fb/bfb.h>
47#include <genarch/drivers/i8042/i8042.h>
48#include <genarch/kbrd/kbrd.h>
49#include <arch/drivers/i8254.h>
50#include <arch/drivers/i8259.h>
51#include <arch/boot/boot.h>
52
53#ifdef CONFIG_SMP
54#include <arch/smp/apic.h>
55#endif
56
57#include <arch/bios/bios.h>
58#include <arch/cpu.h>
59#include <print.h>
60#include <arch/cpuid.h>
61#include <genarch/acpi/acpi.h>
62#include <panic.h>
63#include <interrupt.h>
64#include <arch/syscall.h>
65#include <arch/debugger.h>
66#include <syscall/syscall.h>
67#include <console/console.h>
68#include <ddi/irq.h>
69#include <sysinfo/sysinfo.h>
70#include <memstr.h>
71
72/** Disable I/O on non-privileged levels
73 *
74 * Clean IOPL(12,13) and NT(14) flags in EFLAGS register
75 */
76static void clean_IOPL_NT_flags(void)
77{
78 asm volatile (
79 "pushfq\n"
80 "pop %%rax\n"
81 "and $~(0x7000), %%rax\n"
82 "pushq %%rax\n"
83 "popfq\n"
84 ::: "%rax"
85 );
86}
87
88/** Disable alignment check
89 *
90 * Clean AM(18) flag in CR0 register
91 */
92static void clean_AM_flag(void)
93{
94 asm volatile (
95 "mov %%cr0, %%rax\n"
96 "and $~(0x40000), %%rax\n"
97 "mov %%rax, %%cr0\n"
98 ::: "%rax"
99 );
100}
101
102/** Perform amd64-specific initialization before main_bsp() is called.
103 *
104 * @param signature Multiboot signature.
105 * @param info Multiboot information structure.
106 *
107 */
108void arch_pre_main(uint32_t signature, void *info)
109{
110 /* Parse multiboot information obtained from the bootloader. */
111 multiboot_info_parse(signature, (multiboot_info_t *) info);
112 multiboot2_info_parse(signature, (multiboot2_info_t *) info);
113
114#ifdef CONFIG_SMP
115 /* Copy AP bootstrap routines below 1 MB. */
116 memcpy((void *) AP_BOOT_OFFSET, (void *) BOOT_OFFSET,
117 (size_t) &_hardcoded_unmapped_size);
118#endif
119}
120
121void arch_pre_mm_init(void)
122{
123 /* Enable no-execute pages */
124 set_efer_flag(AMD_NXE_FLAG);
125 /* Enable FPU */
126 cpu_setup_fpu();
127
128 /* Initialize segmentation */
129 pm_init();
130
131 /* Disable I/O on nonprivileged levels
132 * clear the NT (nested-thread) flag
133 */
134 clean_IOPL_NT_flags();
135 /* Disable alignment check */
136 clean_AM_flag();
137
138 if (config.cpu_active == 1) {
139 interrupt_init();
140 bios_init();
141
142 /* PIC */
143 i8259_init();
144 }
145}
146
147
148void arch_post_mm_init(void)
149{
150 if (config.cpu_active == 1) {
151 /* Initialize IRQ routing */
152 irq_init(IRQ_COUNT, IRQ_COUNT);
153
154 /* hard clock */
155 i8254_init();
156
157#if (defined(CONFIG_FB) || defined(CONFIG_EGA))
158 bool bfb = false;
159#endif
160
161#ifdef CONFIG_FB
162 bfb = bfb_init();
163#endif
164
165#ifdef CONFIG_EGA
166 if (!bfb) {
167 outdev_t *egadev = ega_init(EGA_BASE, EGA_VIDEORAM);
168 if (egadev)
169 stdout_wire(egadev);
170 }
171#endif
172
173 /* Enable debugger */
174 debugger_init();
175 /* Merge all memory zones to 1 big zone */
176 zone_merge_all();
177 }
178
179 /* Setup fast SYSCALL/SYSRET */
180 syscall_setup_cpu();
181}
182
183void arch_post_cpu_init()
184{
185#ifdef CONFIG_SMP
186 if (config.cpu_active > 1) {
187 l_apic_init();
188 l_apic_debug();
189 }
190#endif
191}
192
193void arch_pre_smp_init(void)
194{
195 if (config.cpu_active == 1) {
196#ifdef CONFIG_SMP
197 acpi_init();
198#endif /* CONFIG_SMP */
199 }
200}
201
202void arch_post_smp_init(void)
203{
204 /* Currently the only supported platform for amd64 is 'pc'. */
205 static const char *platform = "pc";
206
207 sysinfo_set_item_data("platform", NULL, (void *) platform,
208 str_size(platform));
209
210#ifdef CONFIG_PC_KBD
211 /*
212 * Initialize the i8042 controller. Then initialize the keyboard
213 * module and connect it to i8042. Enable keyboard interrupts.
214 */
215 i8042_instance_t *i8042_instance = i8042_init((i8042_t *) I8042_BASE, IRQ_KBD);
216 if (i8042_instance) {
217 kbrd_instance_t *kbrd_instance = kbrd_init();
218 if (kbrd_instance) {
219 indev_t *sink = stdin_wire();
220 indev_t *kbrd = kbrd_wire(kbrd_instance, sink);
221 i8042_wire(i8042_instance, kbrd);
222 trap_virtual_enable_irqs(1 << IRQ_KBD);
223 trap_virtual_enable_irqs(1 << IRQ_MOUSE);
224 }
225 }
226
227 /*
228 * This is the necessary evil until the userspace driver is entirely
229 * self-sufficient.
230 */
231 sysinfo_set_item_val("i8042", NULL, true);
232 sysinfo_set_item_val("i8042.inr_a", NULL, IRQ_KBD);
233 sysinfo_set_item_val("i8042.inr_b", NULL, IRQ_MOUSE);
234 sysinfo_set_item_val("i8042.address.physical", NULL,
235 (uintptr_t) I8042_BASE);
236 sysinfo_set_item_val("i8042.address.kernel", NULL,
237 (uintptr_t) I8042_BASE);
238#endif
239
240 if (irqs_info != NULL)
241 sysinfo_set_item_val(irqs_info, NULL, true);
242
243 sysinfo_set_item_val("netif.ne2000.inr", NULL, IRQ_NE2000);
244}
245
246void calibrate_delay_loop(void)
247{
248 i8254_calibrate_delay_loop();
249 if (config.cpu_active == 1) {
250 /*
251 * This has to be done only on UP.
252 * On SMP, i8254 is not used for time keeping and its interrupt pin remains masked.
253 */
254 i8254_normal_operation();
255 }
256}
257
258/** Set thread-local-storage pointer
259 *
260 * TLS pointer is set in FS register. Unfortunately the 64-bit
261 * part can be set only in CPL0 mode.
262 *
263 * The specs say, that on %fs:0 there is stored contents of %fs register,
264 * we need not to go to CPL0 to read it.
265 */
266sysarg_t sys_tls_set(sysarg_t addr)
267{
268 THREAD->arch.tls = addr;
269 write_msr(AMD_MSR_FS, addr);
270
271 return 0;
272}
273
274/** Construct function pointer
275 *
276 * @param fptr function pointer structure
277 * @param addr function address
278 * @param caller calling function address
279 *
280 * @return address of the function pointer
281 *
282 */
283void *arch_construct_function(fncptr_t *fptr, void *addr, void *caller)
284{
285 return addr;
286}
287
288void arch_reboot(void)
289{
290#ifdef CONFIG_PC_KBD
291 i8042_cpu_reset((i8042_t *) I8042_BASE);
292#endif
293}
294
295void irq_initialize_arch(irq_t *irq)
296{
297 (void) irq;
298}
299
300/** @}
301 */
Note: See TracBrowser for help on using the repository browser.