[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/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>
|
---|
[f245145] | 50 | #include <genarch/drivers/ega/ega.h>
|
---|
[411b6a6] | 51 | #include <genarch/drivers/i8042/i8042.h>
|
---|
[d8db519] | 52 | #include <genarch/drivers/legacy/ia32/io.h>
|
---|
| 53 | #include <genarch/fb/bfb.h>
|
---|
[411b6a6] | 54 | #include <genarch/kbrd/kbrd.h>
|
---|
[d8db519] | 55 | #include <genarch/multiboot/multiboot.h>
|
---|
| 56 | #include <genarch/multiboot/multiboot2.h>
|
---|
[b9e97fb] | 57 |
|
---|
[26678e5] | 58 | #ifdef CONFIG_SMP
|
---|
| 59 | #include <arch/smp/apic.h>
|
---|
| 60 | #endif
|
---|
| 61 |
|
---|
[49a39c2] | 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 | {
|
---|
[f24d300] | 68 | asm volatile (
|
---|
[4cc2ddd] | 69 | "pushfq\n"
|
---|
| 70 | "pop %%rax\n"
|
---|
| 71 | "and $~(0x7000), %%rax\n"
|
---|
| 72 | "pushq %%rax\n"
|
---|
| 73 | "popfq\n"
|
---|
[f24d300] | 74 | ::: "%rax"
|
---|
[49a39c2] | 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 | {
|
---|
[f24d300] | 84 | asm volatile (
|
---|
[4cc2ddd] | 85 | "mov %%cr0, %%rax\n"
|
---|
| 86 | "and $~(0x40000), %%rax\n"
|
---|
| 87 | "mov %%rax, %%cr0\n"
|
---|
[f24d300] | 88 | ::: "%rax"
|
---|
[49a39c2] | 89 | );
|
---|
| 90 | }
|
---|
| 91 |
|
---|
[5d8d71e] | 92 | /** Perform amd64-specific initialization before main_bsp() is called.
|
---|
| 93 | *
|
---|
[1f5c9c96] | 94 | * @param signature Multiboot signature.
|
---|
| 95 | * @param info Multiboot information structure.
|
---|
| 96 | *
|
---|
[5d8d71e] | 97 | */
|
---|
[1f5c9c96] | 98 | void arch_pre_main(uint32_t signature, void *info)
|
---|
[5d8d71e] | 99 | {
|
---|
| 100 | /* Parse multiboot information obtained from the bootloader. */
|
---|
[1f5c9c96] | 101 | multiboot_info_parse(signature, (multiboot_info_t *) info);
|
---|
| 102 | multiboot2_info_parse(signature, (multiboot2_info_t *) info);
|
---|
[5d8d71e] | 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 |
|
---|
[b9e97fb] | 111 | void arch_pre_mm_init(void)
|
---|
| 112 | {
|
---|
[4fb6bf36] | 113 | /* Enable no-execute pages */
|
---|
[89344d85] | 114 | set_efer_flag(AMD_NXE_FLAG);
|
---|
[3396f59] | 115 | /* Enable FPU */
|
---|
| 116 | cpu_setup_fpu();
|
---|
[2ddcc7b] | 117 |
|
---|
[49a39c2] | 118 | /* Initialize segmentation */
|
---|
[b9e97fb] | 119 | pm_init();
|
---|
[4fb6bf36] | 120 |
|
---|
| 121 | /* Disable I/O on nonprivileged levels
|
---|
| 122 | * clear the NT (nested-thread) flag
|
---|
[49a39c2] | 123 | */
|
---|
| 124 | clean_IOPL_NT_flags();
|
---|
| 125 | /* Disable alignment check */
|
---|
| 126 | clean_AM_flag();
|
---|
[2ddcc7b] | 127 |
|
---|
[b9e97fb] | 128 | if (config.cpu_active == 1) {
|
---|
[8607db8] | 129 | interrupt_init();
|
---|
[b9e97fb] | 130 | bios_init();
|
---|
[8607db8] | 131 |
|
---|
| 132 | /* PIC */
|
---|
| 133 | i8259_init();
|
---|
[b9e97fb] | 134 | }
|
---|
| 135 | }
|
---|
| 136 |
|
---|
[4cc2ddd] | 137 |
|
---|
[b9e97fb] | 138 | void arch_post_mm_init(void)
|
---|
| 139 | {
|
---|
| 140 | if (config.cpu_active == 1) {
|
---|
[8607db8] | 141 | /* Initialize IRQ routing */
|
---|
| 142 | irq_init(IRQ_COUNT, IRQ_COUNT);
|
---|
| 143 |
|
---|
| 144 | /* hard clock */
|
---|
| 145 | i8254_init();
|
---|
[ec944b1] | 146 |
|
---|
[a71c158] | 147 | #if (defined(CONFIG_FB) || defined(CONFIG_EGA))
|
---|
[1f5c9c96] | 148 | bool bfb = false;
|
---|
[a71c158] | 149 | #endif
|
---|
| 150 |
|
---|
[de07bcf] | 151 | #ifdef CONFIG_FB
|
---|
[1f5c9c96] | 152 | bfb = bfb_init();
|
---|
[de07bcf] | 153 | #endif
|
---|
[a71c158] | 154 |
|
---|
[ec944b1] | 155 | #ifdef CONFIG_EGA
|
---|
[1f5c9c96] | 156 | if (!bfb) {
|
---|
[a71c158] | 157 | outdev_t *egadev = ega_init(EGA_BASE, EGA_VIDEORAM);
|
---|
| 158 | if (egadev)
|
---|
| 159 | stdout_wire(egadev);
|
---|
| 160 | }
|
---|
[ec944b1] | 161 | #endif
|
---|
[8607db8] | 162 |
|
---|
[4e49572] | 163 | /* Enable debugger */
|
---|
| 164 | debugger_init();
|
---|
[381465e] | 165 | /* Merge all memory zones to 1 big zone */
|
---|
| 166 | zone_merge_all();
|
---|
[b9e97fb] | 167 | }
|
---|
[4cc2ddd] | 168 |
|
---|
[dd4d6b0] | 169 | /* Setup fast SYSCALL/SYSRET */
|
---|
| 170 | syscall_setup_cpu();
|
---|
[b9e97fb] | 171 | }
|
---|
[7df54df] | 172 |
|
---|
[49e6c6b4] | 173 | void arch_post_cpu_init(void)
|
---|
[26678e5] | 174 | {
|
---|
| 175 | #ifdef CONFIG_SMP
|
---|
| 176 | if (config.cpu_active > 1) {
|
---|
| 177 | l_apic_init();
|
---|
| 178 | l_apic_debug();
|
---|
| 179 | }
|
---|
| 180 | #endif
|
---|
| 181 | }
|
---|
| 182 |
|
---|
[7453929] | 183 | void arch_pre_smp_init(void)
|
---|
[7df54df] | 184 | {
|
---|
| 185 | if (config.cpu_active == 1) {
|
---|
[0b5f9fa] | 186 | #ifdef CONFIG_SMP
|
---|
[7df54df] | 187 | acpi_init();
|
---|
[0b5f9fa] | 188 | #endif /* CONFIG_SMP */
|
---|
[7df54df] | 189 | }
|
---|
| 190 | }
|
---|
| 191 |
|
---|
[7453929] | 192 | void arch_post_smp_init(void)
|
---|
| 193 | {
|
---|
[eff1f033] | 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 |
|
---|
[4df7d3a] | 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 | */
|
---|
[c2417bc] | 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);
|
---|
[385a3d6] | 213 | trap_virtual_enable_irqs(1 << IRQ_MOUSE);
|
---|
[c2417bc] | 214 | }
|
---|
[4df7d3a] | 215 | }
|
---|
| 216 | #endif
|
---|
[849ed54] | 217 |
|
---|
[acc7ce4] | 218 | if (irqs_info != NULL)
|
---|
| 219 | sysinfo_set_item_val(irqs_info, NULL, true);
|
---|
[7453929] | 220 | }
|
---|
| 221 |
|
---|
[7df54df] | 222 | void calibrate_delay_loop(void)
|
---|
| 223 | {
|
---|
| 224 | i8254_calibrate_delay_loop();
|
---|
[8607db8] | 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 | }
|
---|
[7df54df] | 232 | }
|
---|
[281b607] | 233 |
|
---|
[e1be3b6] | 234 | /** Set thread-local-storage pointer
|
---|
[281b607] | 235 | *
|
---|
| 236 | * TLS pointer is set in FS register. Unfortunately the 64-bit
|
---|
| 237 | * part can be set only in CPL0 mode.
|
---|
| 238 | *
|
---|
[e1be3b6] | 239 | * The specs say, that on %fs:0 there is stored contents of %fs register,
|
---|
[281b607] | 240 | * we need not to go to CPL0 to read it.
|
---|
| 241 | */
|
---|
[d8db519] | 242 | sysarg_t sys_tls_set(uintptr_t addr)
|
---|
[281b607] | 243 | {
|
---|
[a6d4ceb] | 244 | THREAD->arch.tls = addr;
|
---|
[281b607] | 245 | write_msr(AMD_MSR_FS, addr);
|
---|
[2ddcc7b] | 246 |
|
---|
[d8db519] | 247 | return EOK;
|
---|
[281b607] | 248 | }
|
---|
[41d33ac] | 249 |
|
---|
[6da1013f] | 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 |
|
---|
[149d14e5] | 264 | void arch_reboot(void)
|
---|
| 265 | {
|
---|
| 266 | #ifdef CONFIG_PC_KBD
|
---|
| 267 | i8042_cpu_reset((i8042_t *) I8042_BASE);
|
---|
| 268 | #endif
|
---|
| 269 | }
|
---|
| 270 |
|
---|
[3a2f8aa] | 271 | void irq_initialize_arch(irq_t *irq)
|
---|
| 272 | {
|
---|
| 273 | (void) irq;
|
---|
| 274 | }
|
---|
| 275 |
|
---|
[287920f] | 276 | /** @}
|
---|
[b45c443] | 277 | */
|
---|