source: trunk/hal/x86_64/core/hal_smpboot.S @ 243

Last change on this file since 243 was 243, checked in by max@…, 4 years ago

Mmh, some CPUs can boot with the cache disabled, so explicitly
enable it.

File size: 6.6 KB
Line 
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
100LABEL(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
114TRAMP_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
153        andl    $~(CR0_CD|CR0_NW),%eax
154        movl    %eax,%cr0
155
156        jmp     smptramp_compat
157smptramp_compat:
158
159        movl    $GDT_FIXED_SEL(GDT_KDATA_SEL, SEL_KPL),%eax     /* switch to new segment */
160        movl    %eax,%ds
161        movl    %eax,%es
162        movl    %eax,%ss
163
164        movl    $smptramp_gdt64_desc,%eax
165        lgdt    (%eax)
166        movl    $smptramp_jmp64,%eax
167        ljmp    *(%eax)
168
169TRAMP_LABEL(smptramp_jmp64)
170        .long   smptramp_longmode
171        .word   GDT_FIXED_SEL(GDT_KCODE_SEL, SEL_KPL)
172
173#define GDT_LIMIT       0x17    /* 23 = 3 * 8 - 1 */
174TRAMP_LABEL(smptramp_gdt32)
175        .quad 0x0000000000000000
176        .quad 0x00cf9f000000ffff        /* CS */
177        .quad 0x00cf93000000ffff        /* DS */
178TRAMP_LABEL(smptramp_gdt32_desc)
179        .word GDT_LIMIT
180        .long smptramp_gdt32
181
182TRAMP_LABEL(smptramp_gdt64)
183        .quad 0x0000000000000000
184        .quad 0x00af9a000000ffff        /* CS */
185        .quad 0x00cf92000000ffff        /* DS */
186TRAMP_LABEL(smptramp_gdt64_desc)
187        .word GDT_LIMIT
188        .long smptramp_gdt64
189#undef  GDT_LIMIT
190
191TRAMP_LABEL(smptramp_longmode)
192        .code64
193        movabsq $cpuN_boot_trampoline_end,%rax
194        jmp     *%rax
195
196LABEL(cpuN_boot_trampoline_end)
197        movq    smp_stkva(%rip),%rsp
198        call    init_x86_64_cpuN
199
200        .data
201
202        .globl  smp_L4pa
203        .globl  smp_stkva
204
205        .type   smp_L4pa, @object
206        smp_L4pa:       .quad   0       /* paddr of L4 */
207
208        .type   smp_stkva, @object
209        smp_stkva:      .quad   0       /* vaddr of stack */
210
Note: See TracBrowser for help on using the repository browser.