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