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