[236] | 1 | /* |
---|
| 2 | * hal_smpboot.S - Kernel SMP entry point |
---|
| 3 | * |
---|
| 4 | * Copyright (c) 2017 Maxime Villard |
---|
| 5 | * |
---|
| 6 | * This file is part of ALMOS-MKH. |
---|
| 7 | * |
---|
| 8 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
---|
| 9 | * under the terms of the GNU General Public License as published by |
---|
| 10 | * the Free Software Foundation; version 2.0 of the License. |
---|
| 11 | * |
---|
| 12 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 15 | * General Public License for more details. |
---|
| 16 | * |
---|
| 17 | * You should have received a copy of the GNU General Public License |
---|
| 18 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 20 | */ |
---|
| 21 | |
---|
| 22 | /* |
---|
| 23 | * Copyright (c) 2000, 2016 The NetBSD Foundation, Inc. |
---|
| 24 | * All rights reserved. |
---|
| 25 | * |
---|
| 26 | * This code is derived from software contributed to The NetBSD Foundation |
---|
| 27 | * by RedBack Networks Inc. (Author: Bill Sommerfeld), and Maxime Villard. |
---|
| 28 | * |
---|
| 29 | * Redistribution and use in source and binary forms, with or without |
---|
| 30 | * modification, are permitted provided that the following conditions |
---|
| 31 | * are met: |
---|
| 32 | * 1. Redistributions of source code must retain the above copyright |
---|
| 33 | * notice, this list of conditions and the following disclaimer. |
---|
| 34 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
| 35 | * notice, this list of conditions and the following disclaimer in the |
---|
| 36 | * documentation and/or other materials provided with the distribution. |
---|
| 37 | * |
---|
| 38 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS |
---|
| 39 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
---|
| 40 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
---|
| 41 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS |
---|
| 42 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
| 43 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
| 44 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
| 45 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
| 46 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
| 47 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
| 48 | * POSSIBILITY OF SUCH DAMAGE. |
---|
| 49 | */ |
---|
| 50 | |
---|
| 51 | /* |
---|
| 52 | * Copyright (c) 1999 Stefan Grefen |
---|
| 53 | * |
---|
| 54 | * Redistribution and use in source and binary forms, with or without |
---|
| 55 | * modification, are permitted provided that the following conditions |
---|
| 56 | * are met: |
---|
| 57 | * 1. Redistributions of source code must retain the above copyright |
---|
| 58 | * notice, this list of conditions and the following disclaimer. |
---|
| 59 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
| 60 | * notice, this list of conditions and the following disclaimer in the |
---|
| 61 | * documentation and/or other materials provided with the distribution. |
---|
| 62 | * 3. All advertising materials mentioning features or use of this software |
---|
| 63 | * must display the following acknowledgement: |
---|
| 64 | * This product includes software developed by the NetBSD |
---|
| 65 | * Foundation, Inc. and its contributors. |
---|
| 66 | * 4. Neither the name of The NetBSD Foundation nor the names of its |
---|
| 67 | * contributors may be used to endorse or promote products derived |
---|
| 68 | * from this software without specific prior written permission. |
---|
| 69 | * |
---|
| 70 | * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY |
---|
| 71 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
| 72 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
| 73 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR AND CONTRIBUTORS BE LIABLE |
---|
| 74 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
| 75 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
| 76 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
| 77 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
| 78 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
| 79 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
| 80 | * SUCH DAMAGE. |
---|
| 81 | */ |
---|
| 82 | |
---|
| 83 | #define x86_ASM |
---|
| 84 | #include <hal_boot.h> |
---|
| 85 | #include <hal_register.h> |
---|
| 86 | #include <hal_kentry.h> |
---|
| 87 | #include <hal_segmentation.h> |
---|
| 88 | |
---|
| 89 | #define RELOC(x) ((x) - KERNBASE) |
---|
| 90 | #define TRAMP_LABEL(a) a = . - cpuN_boot_trampoline + SMP_TRAMPOLINE_PA |
---|
| 91 | |
---|
| 92 | .global cpuN_boot_trampoline |
---|
| 93 | .global cpuN_boot_trampoline_end |
---|
| 94 | .global init_x86_64_cpuN |
---|
| 95 | |
---|
| 96 | .text |
---|
| 97 | .align 4,0x0 |
---|
| 98 | .code16 |
---|
| 99 | |
---|
| 100 | LABEL(cpuN_boot_trampoline) |
---|
| 101 | cli |
---|
| 102 | xorw %ax,%ax |
---|
| 103 | movw %ax,%ds |
---|
| 104 | movw %ax,%es |
---|
| 105 | movw %ax,%ss |
---|
| 106 | data32 addr32 lgdt (smptramp_gdt32_desc) /* load flat descriptor table */ |
---|
| 107 | |
---|
| 108 | /* enable protected mode */ |
---|
| 109 | movl %cr0,%eax |
---|
| 110 | orl $CR0_PE,%eax |
---|
| 111 | movl %eax,%cr0 |
---|
| 112 | ljmpl $0x8,$smp_startup |
---|
| 113 | |
---|
| 114 | TRAMP_LABEL(smp_startup) |
---|
| 115 | .code32 |
---|
| 116 | |
---|
| 117 | movl $0x10,%eax /* data segment */ |
---|
| 118 | movw %ax,%ds |
---|
| 119 | movw %ax,%ss |
---|
| 120 | movw %ax,%es |
---|
| 121 | movw %ax,%fs |
---|
| 122 | movw %ax,%gs |
---|
| 123 | |
---|
| 124 | /* bootstrap stack end, with scratch space.. */ |
---|
| 125 | movl $(SMP_TRAMPOLINE_PA+PAGE_SIZE-16),%esp |
---|
| 126 | |
---|
| 127 | /* First, reset the PSL. */ |
---|
| 128 | pushl $PSL_MBO |
---|
| 129 | popfl |
---|
| 130 | |
---|
| 131 | /* Enable PAE, SSE, and PSE */ |
---|
| 132 | movl %cr4,%eax |
---|
| 133 | orl $(CR4_PAE|CR4_OSFXSR|CR4_OSXMMEXCPT|CR4_PGE|CR4_PSE),%eax |
---|
| 134 | movl %eax,%cr4 |
---|
| 135 | |
---|
| 136 | /* |
---|
| 137 | * Set Long Mode Enable in EFER. Also enable the syscall extensions, |
---|
| 138 | * and NOX. |
---|
| 139 | */ |
---|
| 140 | movl $MSR_EFER,%ecx |
---|
| 141 | rdmsr |
---|
| 142 | xorl %eax,%eax |
---|
| 143 | orl $(EFER_LME|EFER_SCE|EFER_NXE),%eax |
---|
| 144 | wrmsr |
---|
| 145 | |
---|
| 146 | /* Load %cr3. */ |
---|
| 147 | movl RELOC(smp_L4pa),%ecx /* guaranteed < 4G */ |
---|
| 148 | movl %ecx,%cr3 /* load PTD addr into MMU */ |
---|
| 149 | |
---|
| 150 | /* Enable paging and the rest of it. */ |
---|
| 151 | movl %cr0,%eax |
---|
| 152 | orl $(CR0_PE|CR0_PG|CR0_NE|CR0_TS|CR0_MP|CR0_WP|CR0_AM),%eax |
---|
[243] | 153 | andl $~(CR0_CD|CR0_NW),%eax |
---|
[368] | 154 | orl $(CR0_EM),%eax /* FPU not implemented yet */ |
---|
[236] | 155 | movl %eax,%cr0 |
---|
| 156 | |
---|
| 157 | jmp smptramp_compat |
---|
| 158 | smptramp_compat: |
---|
| 159 | |
---|
| 160 | movl $GDT_FIXED_SEL(GDT_KDATA_SEL, SEL_KPL),%eax /* switch to new segment */ |
---|
| 161 | movl %eax,%ds |
---|
| 162 | movl %eax,%es |
---|
| 163 | movl %eax,%ss |
---|
| 164 | |
---|
| 165 | movl $smptramp_gdt64_desc,%eax |
---|
| 166 | lgdt (%eax) |
---|
| 167 | movl $smptramp_jmp64,%eax |
---|
| 168 | ljmp *(%eax) |
---|
| 169 | |
---|
| 170 | TRAMP_LABEL(smptramp_jmp64) |
---|
| 171 | .long smptramp_longmode |
---|
| 172 | .word GDT_FIXED_SEL(GDT_KCODE_SEL, SEL_KPL) |
---|
| 173 | |
---|
| 174 | #define GDT_LIMIT 0x17 /* 23 = 3 * 8 - 1 */ |
---|
| 175 | TRAMP_LABEL(smptramp_gdt32) |
---|
| 176 | .quad 0x0000000000000000 |
---|
| 177 | .quad 0x00cf9f000000ffff /* CS */ |
---|
| 178 | .quad 0x00cf93000000ffff /* DS */ |
---|
| 179 | TRAMP_LABEL(smptramp_gdt32_desc) |
---|
| 180 | .word GDT_LIMIT |
---|
| 181 | .long smptramp_gdt32 |
---|
| 182 | |
---|
| 183 | TRAMP_LABEL(smptramp_gdt64) |
---|
| 184 | .quad 0x0000000000000000 |
---|
| 185 | .quad 0x00af9a000000ffff /* CS */ |
---|
| 186 | .quad 0x00cf92000000ffff /* DS */ |
---|
| 187 | TRAMP_LABEL(smptramp_gdt64_desc) |
---|
| 188 | .word GDT_LIMIT |
---|
| 189 | .long smptramp_gdt64 |
---|
| 190 | #undef GDT_LIMIT |
---|
| 191 | |
---|
| 192 | TRAMP_LABEL(smptramp_longmode) |
---|
| 193 | .code64 |
---|
| 194 | movabsq $cpuN_boot_trampoline_end,%rax |
---|
| 195 | jmp *%rax |
---|
| 196 | |
---|
| 197 | LABEL(cpuN_boot_trampoline_end) |
---|
| 198 | movq smp_stkva(%rip),%rsp |
---|
[329] | 199 | xorq %rbp,%rbp |
---|
[236] | 200 | call init_x86_64_cpuN |
---|
| 201 | |
---|
| 202 | .data |
---|
| 203 | |
---|
| 204 | .globl smp_L4pa |
---|
| 205 | .globl smp_stkva |
---|
| 206 | |
---|
| 207 | .type smp_L4pa, @object |
---|
| 208 | smp_L4pa: .quad 0 /* paddr of L4 */ |
---|
| 209 | |
---|
| 210 | .type smp_stkva, @object |
---|
| 211 | smp_stkva: .quad 0 /* vaddr of stack */ |
---|
| 212 | |
---|