Home | History | Annotate | Line # | Download | only in prekern
prekern.c revision 1.7.4.2
      1  1.7.4.2  pgoyette /*	$NetBSD: prekern.c,v 1.7.4.2 2018/09/06 06:55:24 pgoyette 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.7.4.1  pgoyette extern vector *x86_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.7.4.1  pgoyette 		setgate(&idt[i], x86_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.7.4.2  pgoyette #define PREKERN_API_VERSION	2
    225  1.7.4.2  pgoyette 
    226      1.1      maxv struct prekern_args {
    227  1.7.4.2  pgoyette 	int version;
    228      1.1      maxv 	int boothowto;
    229      1.1      maxv 	void *bootinfo;
    230      1.1      maxv 	void *bootspace;
    231      1.1      maxv 	int esym;
    232      1.1      maxv 	int biosextmem;
    233      1.1      maxv 	int biosbasemem;
    234      1.1      maxv 	int cpuid_level;
    235      1.1      maxv 	uint32_t nox_flag;
    236      1.1      maxv 	uint64_t PDPpaddr;
    237      1.1      maxv 	vaddr_t atdevbase;
    238      1.1      maxv 	vaddr_t lwp0uarea;
    239      1.1      maxv 	paddr_t first_avail;
    240      1.1      maxv };
    241      1.1      maxv 
    242      1.1      maxv struct prekern_args pkargs;
    243      1.1      maxv 
    244      1.1      maxv static void
    245      1.5      maxv init_prekern_args(void)
    246      1.1      maxv {
    247      1.3      maxv 	extern struct bootspace bootspace;
    248      1.1      maxv 	extern int esym;
    249      1.1      maxv 	extern int biosextmem;
    250      1.1      maxv 	extern int biosbasemem;
    251      1.1      maxv 	extern int cpuid_level;
    252      1.1      maxv 	extern uint32_t nox_flag;
    253      1.1      maxv 	extern uint64_t PDPpaddr;
    254      1.1      maxv 	extern vaddr_t iom_base;
    255      1.1      maxv 	extern paddr_t stkpa;
    256      1.1      maxv 	extern paddr_t pa_avail;
    257      1.1      maxv 
    258      1.1      maxv 	memset(&pkargs, 0, sizeof(pkargs));
    259  1.7.4.2  pgoyette 	pkargs.version = PREKERN_API_VERSION;
    260      1.1      maxv 	pkargs.boothowto = boothowto;
    261      1.1      maxv 	pkargs.bootinfo = (void *)&bootinfo;
    262      1.1      maxv 	pkargs.bootspace = &bootspace;
    263      1.1      maxv 	pkargs.esym = esym;
    264      1.1      maxv 	pkargs.biosextmem = biosextmem;
    265      1.1      maxv 	pkargs.biosbasemem = biosbasemem;
    266      1.1      maxv 	pkargs.cpuid_level = cpuid_level;
    267      1.1      maxv 	pkargs.nox_flag = nox_flag;
    268      1.1      maxv 	pkargs.PDPpaddr = PDPpaddr;
    269      1.1      maxv 	pkargs.atdevbase = iom_base;
    270      1.3      maxv 	pkargs.lwp0uarea = bootspace.boot.va + (stkpa - bootspace.boot.pa);
    271      1.1      maxv 	pkargs.first_avail = pa_avail;
    272      1.1      maxv 
    273      1.1      maxv 	extern vaddr_t stkva;
    274      1.1      maxv 	stkva = pkargs.lwp0uarea + (USPACE - FRAMESIZE);
    275      1.1      maxv }
    276      1.1      maxv 
    277      1.1      maxv void
    278      1.1      maxv exec_kernel(vaddr_t ent)
    279      1.1      maxv {
    280      1.1      maxv 	int (*jumpfunc)(struct prekern_args *);
    281      1.1      maxv 	int ret;
    282      1.1      maxv 
    283      1.1      maxv 	/*
    284      1.1      maxv 	 * Normally, the function does not return. If it does, it means the
    285      1.1      maxv 	 * kernel had trouble processing the arguments, and we panic here. The
    286      1.1      maxv 	 * return value is here for debug.
    287      1.1      maxv 	 */
    288      1.1      maxv 	jumpfunc = (void *)ent;
    289      1.1      maxv 	ret = (*jumpfunc)(&pkargs);
    290      1.1      maxv 
    291      1.1      maxv 	if (ret == -1) {
    292  1.7.4.2  pgoyette 		fatal("kernel returned: wrong API version");
    293      1.1      maxv 	} else {
    294  1.7.4.2  pgoyette 		fatal("kernel returned: unknown value");
    295      1.1      maxv 	}
    296      1.1      maxv }
    297      1.1      maxv 
    298      1.1      maxv /*
    299      1.1      maxv  * Main entry point of the Prekern.
    300      1.1      maxv  */
    301      1.1      maxv void
    302      1.1      maxv init_prekern(paddr_t pa_start)
    303      1.1      maxv {
    304      1.3      maxv 	vaddr_t ent;
    305      1.1      maxv 
    306      1.1      maxv 	init_cons();
    307      1.1      maxv 	print_banner();
    308      1.1      maxv 
    309      1.1      maxv 	if (kernpa_start == 0 || kernpa_end == 0) {
    310      1.1      maxv 		fatal("init_prekern: unable to locate the kernel");
    311      1.1      maxv 	}
    312      1.1      maxv 	if (kernpa_start != (1UL << 21)) {
    313      1.1      maxv 		fatal("init_prekern: invalid kernpa_start");
    314      1.1      maxv 	}
    315      1.1      maxv 	if (kernpa_start % PAGE_SIZE != 0) {
    316      1.1      maxv 		fatal("init_prekern: kernpa_start not aligned");
    317      1.1      maxv 	}
    318      1.1      maxv 	if (kernpa_end % PAGE_SIZE != 0) {
    319      1.1      maxv 		fatal("init_prekern: kernpa_end not aligned");
    320      1.1      maxv 	}
    321      1.1      maxv 	if (kernpa_end <= kernpa_start) {
    322      1.1      maxv 		fatal("init_prekern: kernpa_end >= kernpa_start");
    323      1.1      maxv 	}
    324      1.1      maxv 
    325      1.1      maxv 	/*
    326      1.1      maxv 	 * Our physical space starts after the end of the kernel.
    327      1.1      maxv 	 */
    328      1.1      maxv 	if (pa_start < kernpa_end) {
    329      1.1      maxv 		fatal("init_prekern: physical space inside kernel");
    330      1.1      maxv 	}
    331      1.1      maxv 	mm_init(pa_start);
    332      1.1      maxv 
    333      1.1      maxv 	/*
    334      1.1      maxv 	 * Init the TSS and IDT. We mostly don't care about this, they are just
    335      1.1      maxv 	 * here to properly handle traps.
    336      1.1      maxv 	 */
    337      1.1      maxv 	init_tss();
    338      1.1      maxv 	init_idt();
    339      1.1      maxv 
    340      1.1      maxv 	print_state(true, "Prekern loaded");
    341      1.1      maxv 
    342      1.1      maxv 	/*
    343      1.7      maxv 	 * Init the PRNG.
    344      1.7      maxv 	 */
    345      1.7      maxv 	prng_init();
    346      1.7      maxv 
    347      1.7      maxv 	/*
    348      1.1      maxv 	 * Relocate the kernel.
    349      1.1      maxv 	 */
    350      1.3      maxv 	mm_map_kernel();
    351      1.3      maxv 	ent = elf_kernel_reloc();
    352      1.4      maxv 	mm_bootspace_mprotect();
    353      1.1      maxv 
    354      1.1      maxv 	/*
    355      1.1      maxv 	 * Build the arguments.
    356      1.1      maxv 	 */
    357      1.3      maxv 	init_prekern_args();
    358      1.1      maxv 
    359      1.1      maxv 	/*
    360      1.1      maxv 	 * Finally, jump into the kernel.
    361      1.1      maxv 	 */
    362      1.1      maxv 	print_state(true, "Jumping into the kernel");
    363      1.1      maxv 	jump_kernel(ent);
    364      1.1      maxv 
    365      1.1      maxv 	fatal("init_prekern: unreachable!");
    366      1.1      maxv }
    367