Home | History | Annotate | Line # | Download | only in prekern
prekern.c revision 1.6
      1  1.6  maxv /*	$NetBSD: prekern.c,v 1.6 2017/11/17 07:07:52 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.6  maxv static uint8_t idtstore[PAGE_SIZE];
     50  1.6  maxv static uint8_t faultstack[PAGE_SIZE];
     51  1.6  maxv static struct x86_64_tss prekern_tss;
     52  1.1  maxv 
     53  1.1  maxv /* GDT offsets */
     54  1.1  maxv #define PREKERN_GDT_NUL_OFF	(0 * 8)
     55  1.1  maxv #define PREKERN_GDT_CS_OFF	(1 * 8)
     56  1.1  maxv #define PREKERN_GDT_DS_OFF	(2 * 8)
     57  1.1  maxv #define PREKERN_GDT_TSS_OFF	(3 * 8)
     58  1.1  maxv 
     59  1.1  maxv #define IDTVEC(name) __CONCAT(X, name)
     60  1.1  maxv typedef void (vector)(void);
     61  1.1  maxv extern vector *IDTVEC(exceptions)[];
     62  1.1  maxv 
     63  1.1  maxv void fatal(char *msg)
     64  1.1  maxv {
     65  1.1  maxv 	print("\n");
     66  1.1  maxv 	print_ext(RED_ON_BLACK, "********** FATAL ***********\n");
     67  1.1  maxv 	print_ext(RED_ON_BLACK, msg);
     68  1.1  maxv 	print("\n");
     69  1.1  maxv 	print_ext(RED_ON_BLACK, "****************************\n");
     70  1.1  maxv 
     71  1.1  maxv 	while (1);
     72  1.1  maxv }
     73  1.1  maxv 
     74  1.1  maxv /* -------------------------------------------------------------------------- */
     75  1.1  maxv 
     76  1.1  maxv struct smallframe {
     77  1.1  maxv 	uint64_t sf_trapno;
     78  1.1  maxv 	uint64_t sf_err;
     79  1.1  maxv 	uint64_t sf_rip;
     80  1.1  maxv 	uint64_t sf_cs;
     81  1.1  maxv 	uint64_t sf_rflags;
     82  1.1  maxv 	uint64_t sf_rsp;
     83  1.1  maxv 	uint64_t sf_ss;
     84  1.1  maxv };
     85  1.1  maxv 
     86  1.1  maxv static void setregion(struct region_descriptor *, void *, uint16_t);
     87  1.1  maxv static void setgate(struct gate_descriptor *, void *, int, int, int, int);
     88  1.1  maxv static void set_sys_segment(struct sys_segment_descriptor *, void *,
     89  1.1  maxv     size_t, int, int, int);
     90  1.1  maxv static void set_sys_gdt(int, void *, size_t, int, int, int);
     91  1.5  maxv static void init_tss(void);
     92  1.5  maxv static void init_idt(void);
     93  1.1  maxv 
     94  1.1  maxv void trap(struct smallframe *);
     95  1.1  maxv 
     96  1.1  maxv static char *trap_type[] = {
     97  1.1  maxv 	"privileged instruction fault",		/*  0 T_PRIVINFLT */
     98  1.1  maxv 	"breakpoint trap",			/*  1 T_BPTFLT */
     99  1.1  maxv 	"arithmetic trap",			/*  2 T_ARITHTRAP */
    100  1.1  maxv 	"asynchronous system trap",		/*  3 T_ASTFLT */
    101  1.1  maxv 	"protection fault",			/*  4 T_PROTFLT */
    102  1.1  maxv 	"trace trap",				/*  5 T_TRCTRAP */
    103  1.1  maxv 	"page fault",				/*  6 T_PAGEFLT */
    104  1.1  maxv 	"alignment fault",			/*  7 T_ALIGNFLT */
    105  1.1  maxv 	"integer divide fault",			/*  8 T_DIVIDE */
    106  1.1  maxv 	"non-maskable interrupt",		/*  9 T_NMI */
    107  1.1  maxv 	"overflow trap",			/* 10 T_OFLOW */
    108  1.1  maxv 	"bounds check fault",			/* 11 T_BOUND */
    109  1.1  maxv 	"FPU not available fault",		/* 12 T_DNA */
    110  1.1  maxv 	"double fault",				/* 13 T_DOUBLEFLT */
    111  1.1  maxv 	"FPU operand fetch fault",		/* 14 T_FPOPFLT */
    112  1.1  maxv 	"invalid TSS fault",			/* 15 T_TSSFLT */
    113  1.1  maxv 	"segment not present fault",		/* 16 T_SEGNPFLT */
    114  1.1  maxv 	"stack fault",				/* 17 T_STKFLT */
    115  1.1  maxv 	"machine check fault",			/* 18 T_MCA */
    116  1.1  maxv 	"SSE FP exception",			/* 19 T_XMM */
    117  1.1  maxv 	"reserved trap",			/* 20 T_RESERVED */
    118  1.1  maxv };
    119  1.6  maxv static int trap_types = __arraycount(trap_type);
    120  1.1  maxv 
    121  1.1  maxv /*
    122  1.1  maxv  * Trap handler.
    123  1.1  maxv  */
    124  1.1  maxv void
    125  1.1  maxv trap(struct smallframe *sf)
    126  1.1  maxv {
    127  1.1  maxv 	uint64_t trapno = sf->sf_trapno;
    128  1.1  maxv 	char *buf;
    129  1.1  maxv 
    130  1.1  maxv 	if (trapno < trap_types) {
    131  1.1  maxv 		buf = trap_type[trapno];
    132  1.1  maxv 	} else {
    133  1.1  maxv 		buf = "unknown trap";
    134  1.1  maxv 	}
    135  1.1  maxv 
    136  1.1  maxv 	print("\n");
    137  1.1  maxv 	print_ext(RED_ON_BLACK, "****** FAULT OCCURRED ******\n");
    138  1.1  maxv 	print_ext(RED_ON_BLACK, buf);
    139  1.1  maxv 	print("\n");
    140  1.1  maxv 	print_ext(RED_ON_BLACK, "****************************\n");
    141  1.1  maxv 
    142  1.1  maxv 	while (1);
    143  1.1  maxv }
    144  1.1  maxv 
    145  1.1  maxv static void
    146  1.1  maxv setregion(struct region_descriptor *rd, void *base, uint16_t limit)
    147  1.1  maxv {
    148  1.1  maxv 	rd->rd_limit = limit;
    149  1.1  maxv 	rd->rd_base = (uint64_t)base;
    150  1.1  maxv }
    151  1.1  maxv 
    152  1.1  maxv static void
    153  1.1  maxv setgate(struct gate_descriptor *gd, void *func, int ist, int type, int dpl,
    154  1.6  maxv     int sel)
    155  1.1  maxv {
    156  1.1  maxv 	gd->gd_looffset = (uint64_t)func & 0xffff;
    157  1.1  maxv 	gd->gd_selector = sel;
    158  1.1  maxv 	gd->gd_ist = ist;
    159  1.1  maxv 	gd->gd_type = type;
    160  1.1  maxv 	gd->gd_dpl = dpl;
    161  1.1  maxv 	gd->gd_p = 1;
    162  1.1  maxv 	gd->gd_hioffset = (uint64_t)func >> 16;
    163  1.1  maxv 	gd->gd_zero = 0;
    164  1.1  maxv 	gd->gd_xx1 = 0;
    165  1.1  maxv 	gd->gd_xx2 = 0;
    166  1.1  maxv 	gd->gd_xx3 = 0;
    167  1.1  maxv }
    168  1.1  maxv 
    169  1.1  maxv static void
    170  1.1  maxv set_sys_segment(struct sys_segment_descriptor *sd, void *base, size_t limit,
    171  1.6  maxv     int type, int dpl, int gran)
    172  1.1  maxv {
    173  1.1  maxv 	memset(sd, 0, sizeof(*sd));
    174  1.1  maxv 	sd->sd_lolimit = (unsigned)limit;
    175  1.1  maxv 	sd->sd_lobase = (uint64_t)base;
    176  1.1  maxv 	sd->sd_type = type;
    177  1.1  maxv 	sd->sd_dpl = dpl;
    178  1.1  maxv 	sd->sd_p = 1;
    179  1.1  maxv 	sd->sd_hilimit = (unsigned)limit >> 16;
    180  1.1  maxv 	sd->sd_gran = gran;
    181  1.1  maxv 	sd->sd_hibase = (uint64_t)base >> 24;
    182  1.1  maxv }
    183  1.1  maxv 
    184  1.1  maxv static void
    185  1.1  maxv set_sys_gdt(int slotoff, void *base, size_t limit, int type, int dpl, int gran)
    186  1.1  maxv {
    187  1.1  maxv 	struct sys_segment_descriptor sd;
    188  1.6  maxv 	extern uint64_t *gdt64_start;
    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.6  maxv static void
    196  1.6  maxv init_tss(void)
    197  1.1  maxv {
    198  1.1  maxv 	memset(&prekern_tss, 0, sizeof(prekern_tss));
    199  1.1  maxv 	prekern_tss.tss_ist[0] = (uintptr_t)(&faultstack[PAGE_SIZE-1]) & ~0xf;
    200  1.1  maxv 
    201  1.1  maxv 	set_sys_gdt(PREKERN_GDT_TSS_OFF, &prekern_tss,
    202  1.1  maxv 	    sizeof(struct x86_64_tss) - 1, SDT_SYS386TSS, SEL_KPL, 0);
    203  1.1  maxv }
    204  1.1  maxv 
    205  1.6  maxv static void
    206  1.6  maxv init_idt(void)
    207  1.1  maxv {
    208  1.1  maxv 	struct region_descriptor region;
    209  1.1  maxv 	struct gate_descriptor *idt;
    210  1.1  maxv 	size_t i;
    211  1.1  maxv 
    212  1.1  maxv 	idt = (struct gate_descriptor *)&idtstore;
    213  1.1  maxv 	for (i = 0; i < NCPUIDT; i++) {
    214  1.1  maxv 		setgate(&idt[i], IDTVEC(exceptions)[i], 0, SDT_SYS386IGT,
    215  1.1  maxv 		    SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
    216  1.1  maxv 	}
    217  1.1  maxv 
    218  1.1  maxv 	setregion(&region, &idtstore, PAGE_SIZE - 1);
    219  1.1  maxv 	lidt(&region);
    220  1.1  maxv }
    221  1.1  maxv 
    222  1.1  maxv /* -------------------------------------------------------------------------- */
    223  1.1  maxv 
    224  1.1  maxv struct prekern_args {
    225  1.1  maxv 	int boothowto;
    226  1.1  maxv 	void *bootinfo;
    227  1.1  maxv 	void *bootspace;
    228  1.1  maxv 	int esym;
    229  1.1  maxv 	int biosextmem;
    230  1.1  maxv 	int biosbasemem;
    231  1.1  maxv 	int cpuid_level;
    232  1.1  maxv 	uint32_t nox_flag;
    233  1.1  maxv 	uint64_t PDPpaddr;
    234  1.1  maxv 	vaddr_t atdevbase;
    235  1.1  maxv 	vaddr_t lwp0uarea;
    236  1.1  maxv 	paddr_t first_avail;
    237  1.1  maxv };
    238  1.1  maxv 
    239  1.1  maxv struct prekern_args pkargs;
    240  1.1  maxv 
    241  1.1  maxv static void
    242  1.5  maxv init_prekern_args(void)
    243  1.1  maxv {
    244  1.3  maxv 	extern struct bootspace bootspace;
    245  1.1  maxv 	extern int esym;
    246  1.1  maxv 	extern int biosextmem;
    247  1.1  maxv 	extern int biosbasemem;
    248  1.1  maxv 	extern int cpuid_level;
    249  1.1  maxv 	extern uint32_t nox_flag;
    250  1.1  maxv 	extern uint64_t PDPpaddr;
    251  1.1  maxv 	extern vaddr_t iom_base;
    252  1.1  maxv 	extern paddr_t stkpa;
    253  1.1  maxv 	extern paddr_t pa_avail;
    254  1.1  maxv 
    255  1.1  maxv 	memset(&pkargs, 0, sizeof(pkargs));
    256  1.1  maxv 	pkargs.boothowto = boothowto;
    257  1.1  maxv 	pkargs.bootinfo = (void *)&bootinfo;
    258  1.1  maxv 	pkargs.bootspace = &bootspace;
    259  1.1  maxv 	pkargs.esym = esym;
    260  1.1  maxv 	pkargs.biosextmem = biosextmem;
    261  1.1  maxv 	pkargs.biosbasemem = biosbasemem;
    262  1.1  maxv 	pkargs.cpuid_level = cpuid_level;
    263  1.1  maxv 	pkargs.nox_flag = nox_flag;
    264  1.1  maxv 	pkargs.PDPpaddr = PDPpaddr;
    265  1.1  maxv 	pkargs.atdevbase = iom_base;
    266  1.3  maxv 	pkargs.lwp0uarea = bootspace.boot.va + (stkpa - bootspace.boot.pa);
    267  1.1  maxv 	pkargs.first_avail = pa_avail;
    268  1.1  maxv 
    269  1.1  maxv 	extern vaddr_t stkva;
    270  1.1  maxv 	stkva = pkargs.lwp0uarea + (USPACE - FRAMESIZE);
    271  1.1  maxv }
    272  1.1  maxv 
    273  1.1  maxv void
    274  1.1  maxv exec_kernel(vaddr_t ent)
    275  1.1  maxv {
    276  1.1  maxv 	int (*jumpfunc)(struct prekern_args *);
    277  1.1  maxv 	int ret;
    278  1.1  maxv 
    279  1.1  maxv 	/*
    280  1.1  maxv 	 * Normally, the function does not return. If it does, it means the
    281  1.1  maxv 	 * kernel had trouble processing the arguments, and we panic here. The
    282  1.1  maxv 	 * return value is here for debug.
    283  1.1  maxv 	 */
    284  1.1  maxv 	jumpfunc = (void *)ent;
    285  1.1  maxv 	ret = (*jumpfunc)(&pkargs);
    286  1.1  maxv 
    287  1.1  maxv 	if (ret == -1) {
    288  1.1  maxv 		fatal("kernel returned -1");
    289  1.1  maxv 	} else {
    290  1.1  maxv 		fatal("kernel returned unknown value");
    291  1.1  maxv 	}
    292  1.1  maxv }
    293  1.1  maxv 
    294  1.1  maxv /*
    295  1.1  maxv  * Main entry point of the Prekern.
    296  1.1  maxv  */
    297  1.1  maxv void
    298  1.1  maxv init_prekern(paddr_t pa_start)
    299  1.1  maxv {
    300  1.3  maxv 	vaddr_t ent;
    301  1.1  maxv 
    302  1.1  maxv 	init_cons();
    303  1.1  maxv 	print_banner();
    304  1.1  maxv 
    305  1.1  maxv 	if (kernpa_start == 0 || kernpa_end == 0) {
    306  1.1  maxv 		fatal("init_prekern: unable to locate the kernel");
    307  1.1  maxv 	}
    308  1.1  maxv 	if (kernpa_start != (1UL << 21)) {
    309  1.1  maxv 		fatal("init_prekern: invalid kernpa_start");
    310  1.1  maxv 	}
    311  1.1  maxv 	if (kernpa_start % PAGE_SIZE != 0) {
    312  1.1  maxv 		fatal("init_prekern: kernpa_start not aligned");
    313  1.1  maxv 	}
    314  1.1  maxv 	if (kernpa_end % PAGE_SIZE != 0) {
    315  1.1  maxv 		fatal("init_prekern: kernpa_end not aligned");
    316  1.1  maxv 	}
    317  1.1  maxv 	if (kernpa_end <= kernpa_start) {
    318  1.1  maxv 		fatal("init_prekern: kernpa_end >= kernpa_start");
    319  1.1  maxv 	}
    320  1.1  maxv 
    321  1.1  maxv 	/*
    322  1.1  maxv 	 * Our physical space starts after the end of the kernel.
    323  1.1  maxv 	 */
    324  1.1  maxv 	if (pa_start < kernpa_end) {
    325  1.1  maxv 		fatal("init_prekern: physical space inside kernel");
    326  1.1  maxv 	}
    327  1.1  maxv 	mm_init(pa_start);
    328  1.1  maxv 
    329  1.1  maxv 	/*
    330  1.1  maxv 	 * Init the TSS and IDT. We mostly don't care about this, they are just
    331  1.1  maxv 	 * here to properly handle traps.
    332  1.1  maxv 	 */
    333  1.1  maxv 	init_tss();
    334  1.1  maxv 	init_idt();
    335  1.1  maxv 
    336  1.1  maxv 	print_state(true, "Prekern loaded");
    337  1.1  maxv 
    338  1.1  maxv 	/*
    339  1.1  maxv 	 * Relocate the kernel.
    340  1.1  maxv 	 */
    341  1.3  maxv 	mm_map_kernel();
    342  1.3  maxv 	ent = elf_kernel_reloc();
    343  1.4  maxv 	mm_bootspace_mprotect();
    344  1.1  maxv 
    345  1.1  maxv 	/*
    346  1.1  maxv 	 * Build the arguments.
    347  1.1  maxv 	 */
    348  1.3  maxv 	init_prekern_args();
    349  1.1  maxv 
    350  1.1  maxv 	/*
    351  1.1  maxv 	 * Finally, jump into the kernel.
    352  1.1  maxv 	 */
    353  1.1  maxv 	print_state(true, "Jumping into the kernel");
    354  1.1  maxv 	jump_kernel(ent);
    355  1.1  maxv 
    356  1.1  maxv 	fatal("init_prekern: unreachable!");
    357  1.1  maxv }
    358