| [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>
|
|---|
| [d99c1d2] | 36 | #include <typedefs.h>
|
|---|
| [d8db519] | 37 | #include <errno.h>
|
|---|
| 38 | #include <memstr.h>
|
|---|
| 39 | #include <interrupt.h>
|
|---|
| 40 | #include <console/console.h>
|
|---|
| 41 | #include <syscall/syscall.h>
|
|---|
| 42 | #include <sysinfo/sysinfo.h>
|
|---|
| 43 | #include <arch/bios/bios.h>
|
|---|
| 44 | #include <arch/boot/boot.h>
|
|---|
| 45 | #include <arch/drivers/i8254.h>
|
|---|
| 46 | #include <arch/drivers/i8259.h>
|
|---|
| 47 | #include <arch/syscall.h>
|
|---|
| 48 | #include <genarch/acpi/acpi.h>
|
|---|
| [f245145] | 49 | #include <genarch/drivers/ega/ega.h>
|
|---|
| [411b6a6] | 50 | #include <genarch/drivers/i8042/i8042.h>
|
|---|
| [d8db519] | 51 | #include <genarch/drivers/legacy/ia32/io.h>
|
|---|
| 52 | #include <genarch/fb/bfb.h>
|
|---|
| [411b6a6] | 53 | #include <genarch/kbrd/kbrd.h>
|
|---|
| [d8db519] | 54 | #include <genarch/multiboot/multiboot.h>
|
|---|
| 55 | #include <genarch/multiboot/multiboot2.h>
|
|---|
| [b9e97fb] | 56 |
|
|---|
| [26678e5] | 57 | #ifdef CONFIG_SMP
|
|---|
| 58 | #include <arch/smp/apic.h>
|
|---|
| 59 | #endif
|
|---|
| 60 |
|
|---|
| [49a39c2] | 61 | /** Disable I/O on non-privileged levels
|
|---|
| 62 | *
|
|---|
| 63 | * Clean IOPL(12,13) and NT(14) flags in EFLAGS register
|
|---|
| 64 | */
|
|---|
| 65 | static void clean_IOPL_NT_flags(void)
|
|---|
| 66 | {
|
|---|
| [f24d300] | 67 | asm volatile (
|
|---|
| [4cc2ddd] | 68 | "pushfq\n"
|
|---|
| 69 | "pop %%rax\n"
|
|---|
| 70 | "and $~(0x7000), %%rax\n"
|
|---|
| 71 | "pushq %%rax\n"
|
|---|
| 72 | "popfq\n"
|
|---|
| [f24d300] | 73 | ::: "%rax"
|
|---|
| [49a39c2] | 74 | );
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | /** Disable alignment check
|
|---|
| 78 | *
|
|---|
| 79 | * Clean AM(18) flag in CR0 register
|
|---|
| 80 | */
|
|---|
| 81 | static void clean_AM_flag(void)
|
|---|
| 82 | {
|
|---|
| [f24d300] | 83 | asm volatile (
|
|---|
| [4cc2ddd] | 84 | "mov %%cr0, %%rax\n"
|
|---|
| 85 | "and $~(0x40000), %%rax\n"
|
|---|
| 86 | "mov %%rax, %%cr0\n"
|
|---|
| [f24d300] | 87 | ::: "%rax"
|
|---|
| [49a39c2] | 88 | );
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| [5d8d71e] | 91 | /** Perform amd64-specific initialization before main_bsp() is called.
|
|---|
| 92 | *
|
|---|
| [1f5c9c96] | 93 | * @param signature Multiboot signature.
|
|---|
| 94 | * @param info Multiboot information structure.
|
|---|
| 95 | *
|
|---|
| [5d8d71e] | 96 | */
|
|---|
| [1f5c9c96] | 97 | void arch_pre_main(uint32_t signature, void *info)
|
|---|
| [5d8d71e] | 98 | {
|
|---|
| 99 | /* Parse multiboot information obtained from the bootloader. */
|
|---|
| [1f5c9c96] | 100 | multiboot_info_parse(signature, (multiboot_info_t *) info);
|
|---|
| 101 | multiboot2_info_parse(signature, (multiboot2_info_t *) info);
|
|---|
| [5d8d71e] | 102 |
|
|---|
| 103 | #ifdef CONFIG_SMP
|
|---|
| 104 | /* Copy AP bootstrap routines below 1 MB. */
|
|---|
| 105 | memcpy((void *) AP_BOOT_OFFSET, (void *) BOOT_OFFSET,
|
|---|
| 106 | (size_t) &_hardcoded_unmapped_size);
|
|---|
| 107 | #endif
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| [b9e97fb] | 110 | void arch_pre_mm_init(void)
|
|---|
| 111 | {
|
|---|
| [4fb6bf36] | 112 | /* Enable no-execute pages */
|
|---|
| [89344d85] | 113 | set_efer_flag(AMD_NXE_FLAG);
|
|---|
| [3396f59] | 114 | /* Enable FPU */
|
|---|
| 115 | cpu_setup_fpu();
|
|---|
| [2ddcc7b] | 116 |
|
|---|
| [49a39c2] | 117 | /* Initialize segmentation */
|
|---|
| [b9e97fb] | 118 | pm_init();
|
|---|
| [4fb6bf36] | 119 |
|
|---|
| 120 | /* Disable I/O on nonprivileged levels
|
|---|
| 121 | * clear the NT (nested-thread) flag
|
|---|
| [49a39c2] | 122 | */
|
|---|
| 123 | clean_IOPL_NT_flags();
|
|---|
| 124 | /* Disable alignment check */
|
|---|
| 125 | clean_AM_flag();
|
|---|
| [2ddcc7b] | 126 |
|
|---|
| [b9e97fb] | 127 | if (config.cpu_active == 1) {
|
|---|
| [8607db8] | 128 | interrupt_init();
|
|---|
| [b9e97fb] | 129 | bios_init();
|
|---|
| [8607db8] | 130 |
|
|---|
| 131 | /* PIC */
|
|---|
| 132 | i8259_init();
|
|---|
| [b9e97fb] | 133 | }
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| [4cc2ddd] | 136 |
|
|---|
| [b9e97fb] | 137 | void arch_post_mm_init(void)
|
|---|
| 138 | {
|
|---|
| 139 | if (config.cpu_active == 1) {
|
|---|
| [8607db8] | 140 | /* Initialize IRQ routing */
|
|---|
| 141 | irq_init(IRQ_COUNT, IRQ_COUNT);
|
|---|
| 142 |
|
|---|
| 143 | /* hard clock */
|
|---|
| 144 | i8254_init();
|
|---|
| [ec944b1] | 145 |
|
|---|
| [a71c158] | 146 | #if (defined(CONFIG_FB) || defined(CONFIG_EGA))
|
|---|
| [1f5c9c96] | 147 | bool bfb = false;
|
|---|
| [a71c158] | 148 | #endif
|
|---|
| 149 |
|
|---|
| [de07bcf] | 150 | #ifdef CONFIG_FB
|
|---|
| [1f5c9c96] | 151 | bfb = bfb_init();
|
|---|
| [de07bcf] | 152 | #endif
|
|---|
| [a71c158] | 153 |
|
|---|
| [ec944b1] | 154 | #ifdef CONFIG_EGA
|
|---|
| [1f5c9c96] | 155 | if (!bfb) {
|
|---|
| [a71c158] | 156 | outdev_t *egadev = ega_init(EGA_BASE, EGA_VIDEORAM);
|
|---|
| 157 | if (egadev)
|
|---|
| 158 | stdout_wire(egadev);
|
|---|
| 159 | }
|
|---|
| [ec944b1] | 160 | #endif
|
|---|
| [8607db8] | 161 |
|
|---|
| [381465e] | 162 | /* Merge all memory zones to 1 big zone */
|
|---|
| 163 | zone_merge_all();
|
|---|
| [b9e97fb] | 164 | }
|
|---|
| [4cc2ddd] | 165 |
|
|---|
| [dd4d6b0] | 166 | /* Setup fast SYSCALL/SYSRET */
|
|---|
| 167 | syscall_setup_cpu();
|
|---|
| [b9e97fb] | 168 | }
|
|---|
| [7df54df] | 169 |
|
|---|
| [26678e5] | 170 | void arch_post_cpu_init()
|
|---|
| 171 | {
|
|---|
| 172 | #ifdef CONFIG_SMP
|
|---|
| 173 | if (config.cpu_active > 1) {
|
|---|
| 174 | l_apic_init();
|
|---|
| 175 | l_apic_debug();
|
|---|
| 176 | }
|
|---|
| 177 | #endif
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| [7453929] | 180 | void arch_pre_smp_init(void)
|
|---|
| [7df54df] | 181 | {
|
|---|
| 182 | if (config.cpu_active == 1) {
|
|---|
| [0b5f9fa] | 183 | #ifdef CONFIG_SMP
|
|---|
| [7df54df] | 184 | acpi_init();
|
|---|
| [0b5f9fa] | 185 | #endif /* CONFIG_SMP */
|
|---|
| [7df54df] | 186 | }
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| [7453929] | 189 | void arch_post_smp_init(void)
|
|---|
| 190 | {
|
|---|
| [eff1f033] | 191 | /* Currently the only supported platform for amd64 is 'pc'. */
|
|---|
| 192 | static const char *platform = "pc";
|
|---|
| 193 |
|
|---|
| 194 | sysinfo_set_item_data("platform", NULL, (void *) platform,
|
|---|
| 195 | str_size(platform));
|
|---|
| 196 |
|
|---|
| [4df7d3a] | 197 | #ifdef CONFIG_PC_KBD
|
|---|
| 198 | /*
|
|---|
| 199 | * Initialize the i8042 controller. Then initialize the keyboard
|
|---|
| 200 | * module and connect it to i8042. Enable keyboard interrupts.
|
|---|
| 201 | */
|
|---|
| [c2417bc] | 202 | i8042_instance_t *i8042_instance = i8042_init((i8042_t *) I8042_BASE, IRQ_KBD);
|
|---|
| 203 | if (i8042_instance) {
|
|---|
| 204 | kbrd_instance_t *kbrd_instance = kbrd_init();
|
|---|
| 205 | if (kbrd_instance) {
|
|---|
| 206 | indev_t *sink = stdin_wire();
|
|---|
| 207 | indev_t *kbrd = kbrd_wire(kbrd_instance, sink);
|
|---|
| 208 | i8042_wire(i8042_instance, kbrd);
|
|---|
| 209 | trap_virtual_enable_irqs(1 << IRQ_KBD);
|
|---|
| [385a3d6] | 210 | trap_virtual_enable_irqs(1 << IRQ_MOUSE);
|
|---|
| [c2417bc] | 211 | }
|
|---|
| [4df7d3a] | 212 | }
|
|---|
| 213 | #endif
|
|---|
| [849ed54] | 214 |
|
|---|
| [acc7ce4] | 215 | if (irqs_info != NULL)
|
|---|
| 216 | sysinfo_set_item_val(irqs_info, NULL, true);
|
|---|
| [7453929] | 217 | }
|
|---|
| 218 |
|
|---|
| [7df54df] | 219 | void calibrate_delay_loop(void)
|
|---|
| 220 | {
|
|---|
| 221 | i8254_calibrate_delay_loop();
|
|---|
| [8607db8] | 222 | if (config.cpu_active == 1) {
|
|---|
| 223 | /*
|
|---|
| 224 | * This has to be done only on UP.
|
|---|
| 225 | * On SMP, i8254 is not used for time keeping and its interrupt pin remains masked.
|
|---|
| 226 | */
|
|---|
| 227 | i8254_normal_operation();
|
|---|
| 228 | }
|
|---|
| [7df54df] | 229 | }
|
|---|
| [281b607] | 230 |
|
|---|
| [e1be3b6] | 231 | /** Set thread-local-storage pointer
|
|---|
| [281b607] | 232 | *
|
|---|
| 233 | * TLS pointer is set in FS register. Unfortunately the 64-bit
|
|---|
| 234 | * part can be set only in CPL0 mode.
|
|---|
| 235 | *
|
|---|
| [e1be3b6] | 236 | * The specs say, that on %fs:0 there is stored contents of %fs register,
|
|---|
| [281b607] | 237 | * we need not to go to CPL0 to read it.
|
|---|
| 238 | */
|
|---|
| [d8db519] | 239 | sysarg_t sys_tls_set(uintptr_t addr)
|
|---|
| [281b607] | 240 | {
|
|---|
| [a6d4ceb] | 241 | THREAD->arch.tls = addr;
|
|---|
| [281b607] | 242 | write_msr(AMD_MSR_FS, addr);
|
|---|
| [2ddcc7b] | 243 |
|
|---|
| [d8db519] | 244 | return EOK;
|
|---|
| [281b607] | 245 | }
|
|---|
| [41d33ac] | 246 |
|
|---|
| [6da1013f] | 247 | /** Construct function pointer
|
|---|
| 248 | *
|
|---|
| 249 | * @param fptr function pointer structure
|
|---|
| 250 | * @param addr function address
|
|---|
| 251 | * @param caller calling function address
|
|---|
| 252 | *
|
|---|
| 253 | * @return address of the function pointer
|
|---|
| 254 | *
|
|---|
| 255 | */
|
|---|
| 256 | void *arch_construct_function(fncptr_t *fptr, void *addr, void *caller)
|
|---|
| 257 | {
|
|---|
| 258 | return addr;
|
|---|
| 259 | }
|
|---|
| 260 |
|
|---|
| [149d14e5] | 261 | void arch_reboot(void)
|
|---|
| 262 | {
|
|---|
| 263 | #ifdef CONFIG_PC_KBD
|
|---|
| 264 | i8042_cpu_reset((i8042_t *) I8042_BASE);
|
|---|
| 265 | #endif
|
|---|
| 266 | }
|
|---|
| 267 |
|
|---|
| [3a2f8aa] | 268 | void irq_initialize_arch(irq_t *irq)
|
|---|
| 269 | {
|
|---|
| 270 | (void) irq;
|
|---|
| 271 | }
|
|---|
| 272 |
|
|---|
| [287920f] | 273 | /** @}
|
|---|
| [b45c443] | 274 | */
|
|---|