prekern.c revision 1.3 1 1.2 maxv /* $NetBSD: prekern.c,v 1.3 2017/10/29 11:28:30 maxv Exp $ */
2 1.1 maxv
3 1.1 maxv /*
4 1.1 maxv * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
5 1.1 maxv *
6 1.1 maxv * This code is derived from software contributed to The NetBSD Foundation
7 1.1 maxv * by Maxime Villard.
8 1.1 maxv *
9 1.1 maxv * Redistribution and use in source and binary forms, with or without
10 1.1 maxv * modification, are permitted provided that the following conditions
11 1.1 maxv * are met:
12 1.1 maxv * 1. Redistributions of source code must retain the above copyright
13 1.1 maxv * notice, this list of conditions and the following disclaimer.
14 1.1 maxv * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 maxv * notice, this list of conditions and the following disclaimer in the
16 1.1 maxv * documentation and/or other materials provided with the distribution.
17 1.1 maxv *
18 1.1 maxv * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 1.1 maxv * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 1.1 maxv * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 1.1 maxv * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 1.1 maxv * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 1.1 maxv * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 1.1 maxv * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 1.1 maxv * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 1.1 maxv * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 1.1 maxv * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 1.1 maxv * POSSIBILITY OF SUCH DAMAGE.
29 1.1 maxv */
30 1.1 maxv
31 1.1 maxv #include "prekern.h"
32 1.1 maxv
33 1.1 maxv #include <machine/reg.h>
34 1.1 maxv #include <machine/specialreg.h>
35 1.1 maxv #include <machine/frame.h>
36 1.1 maxv
37 1.1 maxv #define _KERNEL
38 1.1 maxv #include <machine/bootinfo.h>
39 1.1 maxv #undef _KERNEL
40 1.1 maxv
41 1.1 maxv #include <machine/tss.h>
42 1.1 maxv #include <machine/segments.h>
43 1.1 maxv
44 1.1 maxv int boothowto;
45 1.1 maxv struct bootinfo bootinfo;
46 1.1 maxv
47 1.1 maxv extern paddr_t kernpa_start, kernpa_end;
48 1.1 maxv
49 1.1 maxv extern uint64_t *gdt64_start;
50 1.1 maxv uint8_t idtstore[PAGE_SIZE];
51 1.1 maxv uint8_t faultstack[PAGE_SIZE];
52 1.1 maxv struct x86_64_tss prekern_tss;
53 1.1 maxv
54 1.1 maxv /* GDT offsets */
55 1.1 maxv #define PREKERN_GDT_NUL_OFF (0 * 8)
56 1.1 maxv #define PREKERN_GDT_CS_OFF (1 * 8)
57 1.1 maxv #define PREKERN_GDT_DS_OFF (2 * 8)
58 1.1 maxv #define PREKERN_GDT_TSS_OFF (3 * 8)
59 1.1 maxv
60 1.1 maxv #define IDTVEC(name) __CONCAT(X, name)
61 1.1 maxv typedef void (vector)(void);
62 1.1 maxv extern vector *IDTVEC(exceptions)[];
63 1.1 maxv
64 1.1 maxv void fatal(char *msg)
65 1.1 maxv {
66 1.1 maxv print("\n");
67 1.1 maxv print_ext(RED_ON_BLACK, "********** FATAL ***********\n");
68 1.1 maxv print_ext(RED_ON_BLACK, msg);
69 1.1 maxv print("\n");
70 1.1 maxv print_ext(RED_ON_BLACK, "****************************\n");
71 1.1 maxv
72 1.1 maxv while (1);
73 1.1 maxv }
74 1.1 maxv
75 1.1 maxv /* -------------------------------------------------------------------------- */
76 1.1 maxv
77 1.1 maxv struct smallframe {
78 1.1 maxv uint64_t sf_trapno;
79 1.1 maxv uint64_t sf_err;
80 1.1 maxv uint64_t sf_rip;
81 1.1 maxv uint64_t sf_cs;
82 1.1 maxv uint64_t sf_rflags;
83 1.1 maxv uint64_t sf_rsp;
84 1.1 maxv uint64_t sf_ss;
85 1.1 maxv };
86 1.1 maxv
87 1.1 maxv static void setregion(struct region_descriptor *, void *, uint16_t);
88 1.1 maxv static void setgate(struct gate_descriptor *, void *, int, int, int, int);
89 1.1 maxv static void set_sys_segment(struct sys_segment_descriptor *, void *,
90 1.1 maxv size_t, int, int, int);
91 1.1 maxv static void set_sys_gdt(int, void *, size_t, int, int, int);
92 1.1 maxv static void init_tss();
93 1.1 maxv static void init_idt();
94 1.1 maxv
95 1.1 maxv void trap(struct smallframe *);
96 1.1 maxv
97 1.1 maxv static char *trap_type[] = {
98 1.1 maxv "privileged instruction fault", /* 0 T_PRIVINFLT */
99 1.1 maxv "breakpoint trap", /* 1 T_BPTFLT */
100 1.1 maxv "arithmetic trap", /* 2 T_ARITHTRAP */
101 1.1 maxv "asynchronous system trap", /* 3 T_ASTFLT */
102 1.1 maxv "protection fault", /* 4 T_PROTFLT */
103 1.1 maxv "trace trap", /* 5 T_TRCTRAP */
104 1.1 maxv "page fault", /* 6 T_PAGEFLT */
105 1.1 maxv "alignment fault", /* 7 T_ALIGNFLT */
106 1.1 maxv "integer divide fault", /* 8 T_DIVIDE */
107 1.1 maxv "non-maskable interrupt", /* 9 T_NMI */
108 1.1 maxv "overflow trap", /* 10 T_OFLOW */
109 1.1 maxv "bounds check fault", /* 11 T_BOUND */
110 1.1 maxv "FPU not available fault", /* 12 T_DNA */
111 1.1 maxv "double fault", /* 13 T_DOUBLEFLT */
112 1.1 maxv "FPU operand fetch fault", /* 14 T_FPOPFLT */
113 1.1 maxv "invalid TSS fault", /* 15 T_TSSFLT */
114 1.1 maxv "segment not present fault", /* 16 T_SEGNPFLT */
115 1.1 maxv "stack fault", /* 17 T_STKFLT */
116 1.1 maxv "machine check fault", /* 18 T_MCA */
117 1.1 maxv "SSE FP exception", /* 19 T_XMM */
118 1.1 maxv "reserved trap", /* 20 T_RESERVED */
119 1.1 maxv };
120 1.1 maxv int trap_types = __arraycount(trap_type);
121 1.1 maxv
122 1.1 maxv /*
123 1.1 maxv * Trap handler.
124 1.1 maxv */
125 1.1 maxv void
126 1.1 maxv trap(struct smallframe *sf)
127 1.1 maxv {
128 1.1 maxv uint64_t trapno = sf->sf_trapno;
129 1.1 maxv char *buf;
130 1.1 maxv
131 1.1 maxv if (trapno < trap_types) {
132 1.1 maxv buf = trap_type[trapno];
133 1.1 maxv } else {
134 1.1 maxv buf = "unknown trap";
135 1.1 maxv }
136 1.1 maxv
137 1.1 maxv print("\n");
138 1.1 maxv print_ext(RED_ON_BLACK, "****** FAULT OCCURRED ******\n");
139 1.1 maxv print_ext(RED_ON_BLACK, buf);
140 1.1 maxv print("\n");
141 1.1 maxv print_ext(RED_ON_BLACK, "****************************\n");
142 1.1 maxv
143 1.1 maxv while (1);
144 1.1 maxv }
145 1.1 maxv
146 1.1 maxv static void
147 1.1 maxv setregion(struct region_descriptor *rd, void *base, uint16_t limit)
148 1.1 maxv {
149 1.1 maxv rd->rd_limit = limit;
150 1.1 maxv rd->rd_base = (uint64_t)base;
151 1.1 maxv }
152 1.1 maxv
153 1.1 maxv static void
154 1.1 maxv setgate(struct gate_descriptor *gd, void *func, int ist, int type, int dpl,
155 1.1 maxv int sel)
156 1.1 maxv {
157 1.1 maxv gd->gd_looffset = (uint64_t)func & 0xffff;
158 1.1 maxv gd->gd_selector = sel;
159 1.1 maxv gd->gd_ist = ist;
160 1.1 maxv gd->gd_type = type;
161 1.1 maxv gd->gd_dpl = dpl;
162 1.1 maxv gd->gd_p = 1;
163 1.1 maxv gd->gd_hioffset = (uint64_t)func >> 16;
164 1.1 maxv gd->gd_zero = 0;
165 1.1 maxv gd->gd_xx1 = 0;
166 1.1 maxv gd->gd_xx2 = 0;
167 1.1 maxv gd->gd_xx3 = 0;
168 1.1 maxv }
169 1.1 maxv
170 1.1 maxv static void
171 1.1 maxv set_sys_segment(struct sys_segment_descriptor *sd, void *base, size_t limit,
172 1.1 maxv int type, int dpl, int gran)
173 1.1 maxv {
174 1.1 maxv memset(sd, 0, sizeof(*sd));
175 1.1 maxv sd->sd_lolimit = (unsigned)limit;
176 1.1 maxv sd->sd_lobase = (uint64_t)base;
177 1.1 maxv sd->sd_type = type;
178 1.1 maxv sd->sd_dpl = dpl;
179 1.1 maxv sd->sd_p = 1;
180 1.1 maxv sd->sd_hilimit = (unsigned)limit >> 16;
181 1.1 maxv sd->sd_gran = gran;
182 1.1 maxv sd->sd_hibase = (uint64_t)base >> 24;
183 1.1 maxv }
184 1.1 maxv
185 1.1 maxv static void
186 1.1 maxv set_sys_gdt(int slotoff, void *base, size_t limit, int type, int dpl, int gran)
187 1.1 maxv {
188 1.1 maxv struct sys_segment_descriptor sd;
189 1.1 maxv
190 1.1 maxv set_sys_segment(&sd, base, limit, type, dpl, gran);
191 1.1 maxv
192 1.1 maxv memcpy(&gdt64_start + slotoff, &sd, sizeof(sd));
193 1.1 maxv }
194 1.1 maxv
195 1.1 maxv static void init_tss()
196 1.1 maxv {
197 1.1 maxv memset(&prekern_tss, 0, sizeof(prekern_tss));
198 1.1 maxv prekern_tss.tss_ist[0] = (uintptr_t)(&faultstack[PAGE_SIZE-1]) & ~0xf;
199 1.1 maxv
200 1.1 maxv set_sys_gdt(PREKERN_GDT_TSS_OFF, &prekern_tss,
201 1.1 maxv sizeof(struct x86_64_tss) - 1, SDT_SYS386TSS, SEL_KPL, 0);
202 1.1 maxv }
203 1.1 maxv
204 1.1 maxv static void init_idt()
205 1.1 maxv {
206 1.1 maxv struct region_descriptor region;
207 1.1 maxv struct gate_descriptor *idt;
208 1.1 maxv size_t i;
209 1.1 maxv
210 1.1 maxv idt = (struct gate_descriptor *)&idtstore;
211 1.1 maxv for (i = 0; i < NCPUIDT; i++) {
212 1.1 maxv setgate(&idt[i], IDTVEC(exceptions)[i], 0, SDT_SYS386IGT,
213 1.1 maxv SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
214 1.1 maxv }
215 1.1 maxv
216 1.1 maxv setregion(®ion, &idtstore, PAGE_SIZE - 1);
217 1.1 maxv lidt(®ion);
218 1.1 maxv }
219 1.1 maxv
220 1.1 maxv /* -------------------------------------------------------------------------- */
221 1.1 maxv
222 1.1 maxv struct prekern_args {
223 1.1 maxv int boothowto;
224 1.1 maxv void *bootinfo;
225 1.1 maxv void *bootspace;
226 1.1 maxv int esym;
227 1.1 maxv int biosextmem;
228 1.1 maxv int biosbasemem;
229 1.1 maxv int cpuid_level;
230 1.1 maxv uint32_t nox_flag;
231 1.1 maxv uint64_t PDPpaddr;
232 1.1 maxv vaddr_t atdevbase;
233 1.1 maxv vaddr_t lwp0uarea;
234 1.1 maxv paddr_t first_avail;
235 1.1 maxv };
236 1.1 maxv
237 1.1 maxv struct prekern_args pkargs;
238 1.1 maxv
239 1.1 maxv static void
240 1.3 maxv init_prekern_args()
241 1.1 maxv {
242 1.3 maxv extern struct bootspace bootspace;
243 1.1 maxv extern int esym;
244 1.1 maxv extern int biosextmem;
245 1.1 maxv extern int biosbasemem;
246 1.1 maxv extern int cpuid_level;
247 1.1 maxv extern uint32_t nox_flag;
248 1.1 maxv extern uint64_t PDPpaddr;
249 1.1 maxv extern vaddr_t iom_base;
250 1.1 maxv extern paddr_t stkpa;
251 1.1 maxv extern paddr_t pa_avail;
252 1.1 maxv
253 1.1 maxv memset(&pkargs, 0, sizeof(pkargs));
254 1.1 maxv pkargs.boothowto = boothowto;
255 1.1 maxv pkargs.bootinfo = (void *)&bootinfo;
256 1.1 maxv pkargs.bootspace = &bootspace;
257 1.1 maxv pkargs.esym = esym;
258 1.1 maxv pkargs.biosextmem = biosextmem;
259 1.1 maxv pkargs.biosbasemem = biosbasemem;
260 1.1 maxv pkargs.cpuid_level = cpuid_level;
261 1.1 maxv pkargs.nox_flag = nox_flag;
262 1.1 maxv pkargs.PDPpaddr = PDPpaddr;
263 1.1 maxv pkargs.atdevbase = iom_base;
264 1.3 maxv pkargs.lwp0uarea = bootspace.boot.va + (stkpa - bootspace.boot.pa);
265 1.1 maxv pkargs.first_avail = pa_avail;
266 1.1 maxv
267 1.1 maxv extern vaddr_t stkva;
268 1.1 maxv stkva = pkargs.lwp0uarea + (USPACE - FRAMESIZE);
269 1.1 maxv }
270 1.1 maxv
271 1.1 maxv void
272 1.1 maxv exec_kernel(vaddr_t ent)
273 1.1 maxv {
274 1.1 maxv int (*jumpfunc)(struct prekern_args *);
275 1.1 maxv int ret;
276 1.1 maxv
277 1.1 maxv /*
278 1.1 maxv * Normally, the function does not return. If it does, it means the
279 1.1 maxv * kernel had trouble processing the arguments, and we panic here. The
280 1.1 maxv * return value is here for debug.
281 1.1 maxv */
282 1.1 maxv jumpfunc = (void *)ent;
283 1.1 maxv ret = (*jumpfunc)(&pkargs);
284 1.1 maxv
285 1.1 maxv if (ret == -1) {
286 1.1 maxv fatal("kernel returned -1");
287 1.1 maxv } else {
288 1.1 maxv fatal("kernel returned unknown value");
289 1.1 maxv }
290 1.1 maxv }
291 1.1 maxv
292 1.1 maxv /*
293 1.1 maxv * Main entry point of the Prekern.
294 1.1 maxv */
295 1.1 maxv void
296 1.1 maxv init_prekern(paddr_t pa_start)
297 1.1 maxv {
298 1.3 maxv vaddr_t ent;
299 1.1 maxv
300 1.1 maxv init_cons();
301 1.1 maxv print_banner();
302 1.1 maxv
303 1.1 maxv if (kernpa_start == 0 || kernpa_end == 0) {
304 1.1 maxv fatal("init_prekern: unable to locate the kernel");
305 1.1 maxv }
306 1.1 maxv if (kernpa_start != (1UL << 21)) {
307 1.1 maxv fatal("init_prekern: invalid kernpa_start");
308 1.1 maxv }
309 1.1 maxv if (kernpa_start % PAGE_SIZE != 0) {
310 1.1 maxv fatal("init_prekern: kernpa_start not aligned");
311 1.1 maxv }
312 1.1 maxv if (kernpa_end % PAGE_SIZE != 0) {
313 1.1 maxv fatal("init_prekern: kernpa_end not aligned");
314 1.1 maxv }
315 1.1 maxv if (kernpa_end <= kernpa_start) {
316 1.1 maxv fatal("init_prekern: kernpa_end >= kernpa_start");
317 1.1 maxv }
318 1.1 maxv
319 1.1 maxv /*
320 1.1 maxv * Our physical space starts after the end of the kernel.
321 1.1 maxv */
322 1.1 maxv if (pa_start < kernpa_end) {
323 1.1 maxv fatal("init_prekern: physical space inside kernel");
324 1.1 maxv }
325 1.1 maxv mm_init(pa_start);
326 1.1 maxv
327 1.1 maxv /*
328 1.1 maxv * Init the TSS and IDT. We mostly don't care about this, they are just
329 1.1 maxv * here to properly handle traps.
330 1.1 maxv */
331 1.1 maxv init_tss();
332 1.1 maxv init_idt();
333 1.1 maxv
334 1.1 maxv print_state(true, "Prekern loaded");
335 1.1 maxv
336 1.1 maxv /*
337 1.1 maxv * Relocate the kernel.
338 1.1 maxv */
339 1.3 maxv mm_map_kernel();
340 1.3 maxv ent = elf_kernel_reloc();
341 1.1 maxv
342 1.1 maxv /*
343 1.1 maxv * Build the arguments.
344 1.1 maxv */
345 1.3 maxv init_prekern_args();
346 1.1 maxv
347 1.1 maxv /*
348 1.1 maxv * Finally, jump into the kernel.
349 1.1 maxv */
350 1.1 maxv print_state(true, "Jumping into the kernel");
351 1.1 maxv jump_kernel(ent);
352 1.1 maxv
353 1.1 maxv fatal("init_prekern: unreachable!");
354 1.1 maxv }
355 1.1 maxv
356