| 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/drivers/i8254.h>
|
|---|
| 46 | #include <arch/drivers/i8259.h>
|
|---|
| 47 | #include <arch/syscall.h>
|
|---|
| 48 | #include <genarch/acpi/acpi.h>
|
|---|
| 49 | #include <genarch/drivers/ega/ega.h>
|
|---|
| 50 | #include <genarch/drivers/i8042/i8042.h>
|
|---|
| 51 | #include <genarch/drivers/legacy/ia32/io.h>
|
|---|
| 52 | #include <genarch/fb/bfb.h>
|
|---|
| 53 | #include <genarch/kbrd/kbrd.h>
|
|---|
| 54 | #include <genarch/multiboot/multiboot.h>
|
|---|
| 55 | #include <genarch/multiboot/multiboot2.h>
|
|---|
| 56 |
|
|---|
| 57 | #ifdef CONFIG_SMP
|
|---|
| 58 | #include <arch/smp/apic.h>
|
|---|
| 59 | #endif
|
|---|
| 60 |
|
|---|
| 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 | {
|
|---|
| 67 | asm volatile (
|
|---|
| 68 | "pushfq\n"
|
|---|
| 69 | "pop %%rax\n"
|
|---|
| 70 | "and $~(0x7000), %%rax\n"
|
|---|
| 71 | "pushq %%rax\n"
|
|---|
| 72 | "popfq\n"
|
|---|
| 73 | ::: "%rax"
|
|---|
| 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 | {
|
|---|
| 83 | asm volatile (
|
|---|
| 84 | "mov %%cr0, %%rax\n"
|
|---|
| 85 | "and $~(0x40000), %%rax\n"
|
|---|
| 86 | "mov %%rax, %%cr0\n"
|
|---|
| 87 | ::: "%rax"
|
|---|
| 88 | );
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | /** Perform amd64-specific initialization before main_bsp() is called.
|
|---|
| 92 | *
|
|---|
| 93 | * @param signature Multiboot signature.
|
|---|
| 94 | * @param info Multiboot information structure.
|
|---|
| 95 | *
|
|---|
| 96 | */
|
|---|
| 97 | void arch_pre_main(uint32_t signature, void *info)
|
|---|
| 98 | {
|
|---|
| 99 | /* Parse multiboot information obtained from the bootloader. */
|
|---|
| 100 | multiboot_info_parse(signature, (multiboot_info_t *) info);
|
|---|
| 101 | multiboot2_info_parse(signature, (multiboot2_info_t *) info);
|
|---|
| 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 |
|
|---|
| 110 | void arch_pre_mm_init(void)
|
|---|
| 111 | {
|
|---|
| 112 | /* Enable no-execute pages */
|
|---|
| 113 | set_efer_flag(AMD_NXE_FLAG);
|
|---|
| 114 | /* Enable FPU */
|
|---|
| 115 | cpu_setup_fpu();
|
|---|
| 116 |
|
|---|
| 117 | /* Initialize segmentation */
|
|---|
| 118 | pm_init();
|
|---|
| 119 |
|
|---|
| 120 | /* Disable I/O on nonprivileged levels
|
|---|
| 121 | * clear the NT (nested-thread) flag
|
|---|
| 122 | */
|
|---|
| 123 | clean_IOPL_NT_flags();
|
|---|
| 124 | /* Disable alignment check */
|
|---|
| 125 | clean_AM_flag();
|
|---|
| 126 |
|
|---|
| 127 | if (config.cpu_active == 1) {
|
|---|
| 128 | interrupt_init();
|
|---|
| 129 | bios_init();
|
|---|
| 130 |
|
|---|
| 131 | /* PIC */
|
|---|
| 132 | i8259_init();
|
|---|
| 133 | }
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 |
|
|---|
| 137 | void arch_post_mm_init(void)
|
|---|
| 138 | {
|
|---|
| 139 | if (config.cpu_active == 1) {
|
|---|
| 140 | /* Initialize IRQ routing */
|
|---|
| 141 | irq_init(IRQ_COUNT, IRQ_COUNT);
|
|---|
| 142 |
|
|---|
| 143 | /* hard clock */
|
|---|
| 144 | i8254_init();
|
|---|
| 145 |
|
|---|
| 146 | #if (defined(CONFIG_FB) || defined(CONFIG_EGA))
|
|---|
| 147 | bool bfb = false;
|
|---|
| 148 | #endif
|
|---|
| 149 |
|
|---|
| 150 | #ifdef CONFIG_FB
|
|---|
| 151 | bfb = bfb_init();
|
|---|
| 152 | #endif
|
|---|
| 153 |
|
|---|
| 154 | #ifdef CONFIG_EGA
|
|---|
| 155 | if (!bfb) {
|
|---|
| 156 | outdev_t *egadev = ega_init(EGA_BASE, EGA_VIDEORAM);
|
|---|
| 157 | if (egadev)
|
|---|
| 158 | stdout_wire(egadev);
|
|---|
| 159 | }
|
|---|
| 160 | #endif
|
|---|
| 161 |
|
|---|
| 162 | /* Merge all memory zones to 1 big zone */
|
|---|
| 163 | zone_merge_all();
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | /* Setup fast SYSCALL/SYSRET */
|
|---|
| 167 | syscall_setup_cpu();
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 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 |
|
|---|
| 180 | void arch_pre_smp_init(void)
|
|---|
| 181 | {
|
|---|
| 182 | if (config.cpu_active == 1) {
|
|---|
| 183 | #ifdef CONFIG_SMP
|
|---|
| 184 | acpi_init();
|
|---|
| 185 | #endif /* CONFIG_SMP */
|
|---|
| 186 | }
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 | void arch_post_smp_init(void)
|
|---|
| 190 | {
|
|---|
| 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 |
|
|---|
| 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 | */
|
|---|
| 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);
|
|---|
| 210 | trap_virtual_enable_irqs(1 << IRQ_MOUSE);
|
|---|
| 211 | }
|
|---|
| 212 | }
|
|---|
| 213 | #endif
|
|---|
| 214 |
|
|---|
| 215 | if (irqs_info != NULL)
|
|---|
| 216 | sysinfo_set_item_val(irqs_info, NULL, true);
|
|---|
| 217 | }
|
|---|
| 218 |
|
|---|
| 219 | void calibrate_delay_loop(void)
|
|---|
| 220 | {
|
|---|
| 221 | i8254_calibrate_delay_loop();
|
|---|
| 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 | }
|
|---|
| 229 | }
|
|---|
| 230 |
|
|---|
| 231 | /** Set thread-local-storage pointer
|
|---|
| 232 | *
|
|---|
| 233 | * TLS pointer is set in FS register. Unfortunately the 64-bit
|
|---|
| 234 | * part can be set only in CPL0 mode.
|
|---|
| 235 | *
|
|---|
| 236 | * The specs say, that on %fs:0 there is stored contents of %fs register,
|
|---|
| 237 | * we need not to go to CPL0 to read it.
|
|---|
| 238 | */
|
|---|
| 239 | sysarg_t sys_tls_set(uintptr_t addr)
|
|---|
| 240 | {
|
|---|
| 241 | THREAD->arch.tls = addr;
|
|---|
| 242 | write_msr(AMD_MSR_FS, addr);
|
|---|
| 243 |
|
|---|
| 244 | return EOK;
|
|---|
| 245 | }
|
|---|
| 246 |
|
|---|
| 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 |
|
|---|
| 261 | void arch_reboot(void)
|
|---|
| 262 | {
|
|---|
| 263 | #ifdef CONFIG_PC_KBD
|
|---|
| 264 | i8042_cpu_reset((i8042_t *) I8042_BASE);
|
|---|
| 265 | #endif
|
|---|
| 266 | }
|
|---|
| 267 |
|
|---|
| 268 | void irq_initialize_arch(irq_t *irq)
|
|---|
| 269 | {
|
|---|
| 270 | (void) irq;
|
|---|
| 271 | }
|
|---|
| 272 |
|
|---|
| 273 | /** @}
|
|---|
| 274 | */
|
|---|