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
RevLine 
[b9e97fb]1/*
[df4ed85]2 * Copyright (c) 2005 Ondrej Palkovsky
[b9e97fb]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
[287920f]29/** @addtogroup amd64
[b45c443]30 * @{
31 */
32/** @file
33 */
34
[b9e97fb]35#include <arch.h>
36
[d99c1d2]37#include <typedefs.h>
[b9e97fb]38
39#include <config.h>
40
[281b607]41#include <proc/thread.h>
[5d8d71e]42#include <genarch/multiboot/multiboot.h>
[1f5c9c96]43#include <genarch/multiboot/multiboot2.h>
[91825d90]44#include <genarch/drivers/legacy/ia32/io.h>
[f245145]45#include <genarch/drivers/ega/ega.h>
[1f5c9c96]46#include <genarch/fb/bfb.h>
[411b6a6]47#include <genarch/drivers/i8042/i8042.h>
48#include <genarch/kbrd/kbrd.h>
[80d31883]49#include <arch/drivers/i8254.h>
50#include <arch/drivers/i8259.h>
[5d8d71e]51#include <arch/boot/boot.h>
[b9e97fb]52
[26678e5]53#ifdef CONFIG_SMP
54#include <arch/smp/apic.h>
55#endif
56
[b9e97fb]57#include <arch/bios/bios.h>
[89344d85]58#include <arch/cpu.h>
59#include <print.h>
60#include <arch/cpuid.h>
[e16e036a]61#include <genarch/acpi/acpi.h>
[3396f59]62#include <panic.h>
[fcfac420]63#include <interrupt.h>
[dd4d6b0]64#include <arch/syscall.h>
[4e49572]65#include <arch/debugger.h>
[281b607]66#include <syscall/syscall.h>
[41d33ac]67#include <console/console.h>
[8607db8]68#include <ddi/irq.h>
[4c7257b]69#include <sysinfo/sysinfo.h>
[41df2827]70#include <memstr.h>
[281b607]71
[49a39c2]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{
[f24d300]78 asm volatile (
[4cc2ddd]79 "pushfq\n"
80 "pop %%rax\n"
81 "and $~(0x7000), %%rax\n"
82 "pushq %%rax\n"
83 "popfq\n"
[f24d300]84 ::: "%rax"
[49a39c2]85 );
86}
87
88/** Disable alignment check
89 *
90 * Clean AM(18) flag in CR0 register
91 */
92static void clean_AM_flag(void)
93{
[f24d300]94 asm volatile (
[4cc2ddd]95 "mov %%cr0, %%rax\n"
96 "and $~(0x40000), %%rax\n"
97 "mov %%rax, %%cr0\n"
[f24d300]98 ::: "%rax"
[49a39c2]99 );
100}
101
[5d8d71e]102/** Perform amd64-specific initialization before main_bsp() is called.
103 *
[1f5c9c96]104 * @param signature Multiboot signature.
105 * @param info Multiboot information structure.
106 *
[5d8d71e]107 */
[1f5c9c96]108void arch_pre_main(uint32_t signature, void *info)
[5d8d71e]109{
110 /* Parse multiboot information obtained from the bootloader. */
[1f5c9c96]111 multiboot_info_parse(signature, (multiboot_info_t *) info);
112 multiboot2_info_parse(signature, (multiboot2_info_t *) info);
[5d8d71e]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
[b9e97fb]121void arch_pre_mm_init(void)
122{
[4fb6bf36]123 /* Enable no-execute pages */
[89344d85]124 set_efer_flag(AMD_NXE_FLAG);
[3396f59]125 /* Enable FPU */
126 cpu_setup_fpu();
[2ddcc7b]127
[49a39c2]128 /* Initialize segmentation */
[b9e97fb]129 pm_init();
[4fb6bf36]130
131 /* Disable I/O on nonprivileged levels
132 * clear the NT (nested-thread) flag
[49a39c2]133 */
134 clean_IOPL_NT_flags();
135 /* Disable alignment check */
136 clean_AM_flag();
[2ddcc7b]137
[b9e97fb]138 if (config.cpu_active == 1) {
[8607db8]139 interrupt_init();
[b9e97fb]140 bios_init();
[8607db8]141
142 /* PIC */
143 i8259_init();
[b9e97fb]144 }
145}
146
[4cc2ddd]147
[b9e97fb]148void arch_post_mm_init(void)
149{
150 if (config.cpu_active == 1) {
[8607db8]151 /* Initialize IRQ routing */
152 irq_init(IRQ_COUNT, IRQ_COUNT);
153
154 /* hard clock */
155 i8254_init();
[ec944b1]156
[a71c158]157#if (defined(CONFIG_FB) || defined(CONFIG_EGA))
[1f5c9c96]158 bool bfb = false;
[a71c158]159#endif
160
[de07bcf]161#ifdef CONFIG_FB
[1f5c9c96]162 bfb = bfb_init();
[de07bcf]163#endif
[a71c158]164
[ec944b1]165#ifdef CONFIG_EGA
[1f5c9c96]166 if (!bfb) {
[a71c158]167 outdev_t *egadev = ega_init(EGA_BASE, EGA_VIDEORAM);
168 if (egadev)
169 stdout_wire(egadev);
170 }
[ec944b1]171#endif
[8607db8]172
[4e49572]173 /* Enable debugger */
174 debugger_init();
[381465e]175 /* Merge all memory zones to 1 big zone */
176 zone_merge_all();
[b9e97fb]177 }
[4cc2ddd]178
[dd4d6b0]179 /* Setup fast SYSCALL/SYSRET */
180 syscall_setup_cpu();
[b9e97fb]181}
[7df54df]182
[26678e5]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
[7453929]193void arch_pre_smp_init(void)
[7df54df]194{
195 if (config.cpu_active == 1) {
[0b5f9fa]196#ifdef CONFIG_SMP
[7df54df]197 acpi_init();
[0b5f9fa]198#endif /* CONFIG_SMP */
[7df54df]199 }
200}
201
[7453929]202void arch_post_smp_init(void)
203{
[eff1f033]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
[4df7d3a]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 */
[c2417bc]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);
[385a3d6]223 trap_virtual_enable_irqs(1 << IRQ_MOUSE);
[c2417bc]224 }
[4df7d3a]225 }
226
[4c7257b]227 /*
228 * This is the necessary evil until the userspace driver is entirely
229 * self-sufficient.
230 */
[385a3d6]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,
[ff685c9]235 (uintptr_t) I8042_BASE);
[385a3d6]236 sysinfo_set_item_val("i8042.address.kernel", NULL,
[ff685c9]237 (uintptr_t) I8042_BASE);
[4df7d3a]238#endif
[849ed54]239
[acc7ce4]240 if (irqs_info != NULL)
241 sysinfo_set_item_val(irqs_info, NULL, true);
242
[f902d36]243 sysinfo_set_item_val("netif.ne2000.inr", NULL, IRQ_NE2000);
[7453929]244}
245
[7df54df]246void calibrate_delay_loop(void)
247{
248 i8254_calibrate_delay_loop();
[8607db8]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 }
[7df54df]256}
[281b607]257
[e1be3b6]258/** Set thread-local-storage pointer
[281b607]259 *
260 * TLS pointer is set in FS register. Unfortunately the 64-bit
261 * part can be set only in CPL0 mode.
262 *
[e1be3b6]263 * The specs say, that on %fs:0 there is stored contents of %fs register,
[281b607]264 * we need not to go to CPL0 to read it.
265 */
[96b02eb9]266sysarg_t sys_tls_set(sysarg_t addr)
[281b607]267{
[a6d4ceb]268 THREAD->arch.tls = addr;
[281b607]269 write_msr(AMD_MSR_FS, addr);
[2ddcc7b]270
[281b607]271 return 0;
272}
[41d33ac]273
[6da1013f]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
[149d14e5]288void arch_reboot(void)
289{
290#ifdef CONFIG_PC_KBD
291 i8042_cpu_reset((i8042_t *) I8042_BASE);
292#endif
293}
294
[3a2f8aa]295void irq_initialize_arch(irq_t *irq)
296{
297 (void) irq;
298}
299
[287920f]300/** @}
[b45c443]301 */
Note: See TracBrowser for help on using the repository browser.