Home | History | Annotate | Line # | Download | only in amd64
      1 /*	$NetBSD: prekern.c,v 1.6 2022/08/21 14:05:52 mlelstv Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
      5  *
      6  * This code is derived from software contributed to The NetBSD Foundation
      7  * by Maxime Villard.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     20  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     28  * POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #include <sys/cdefs.h>
     32 
     33 #include "opt_realmem.h"
     34 
     35 #include <sys/param.h>
     36 #include <sys/systm.h>
     37 #include <sys/kernel.h>
     38 #include <sys/cpu.h>
     39 #include <sys/conf.h>
     40 
     41 #include <uvm/uvm.h>
     42 #include <machine/pmap.h>
     43 #include <machine/pmap_private.h>
     44 #include <machine/bootinfo.h>
     45 #include <machine/cpufunc.h>
     46 
     47 #include <x86/bootspace.h>
     48 
     49 #include <dev/isa/isareg.h>
     50 #include <machine/isa_machdep.h>
     51 
     52 #define PREKERN_API_VERSION	2
     53 
     54 struct prekern_args {
     55 	int version;
     56 	int boothowto;
     57 	void *bootinfo;
     58 	void *bootspace;
     59 	int esym;
     60 	int biosextmem;
     61 	int biosbasemem;
     62 	int cpuid_level;
     63 	uint32_t nox_flag;
     64 	uint64_t PDPpaddr;
     65 	vaddr_t atdevbase;
     66 	vaddr_t lwp0uarea;
     67 	paddr_t first_avail;
     68 };
     69 
     70 void main(void);
     71 void init_slotspace(void);
     72 void init_x86_64(paddr_t);
     73 
     74 static void prekern_copy_args(struct prekern_args *);
     75 static void prekern_unmap(void);
     76 int start_prekern(struct prekern_args *);
     77 
     78 static void
     79 prekern_copy_args(struct prekern_args *pkargs)
     80 {
     81 	extern int boothowto;
     82 	extern struct bootinfo bootinfo;
     83 	extern struct bootspace bootspace;
     84 	extern int esym;
     85 	extern int biosextmem;
     86 	extern int biosbasemem;
     87 	extern int cpuid_level;
     88 	extern uint32_t nox_flag;
     89 	extern uint64_t PDPpaddr;
     90 	extern vaddr_t lwp0uarea;
     91 
     92 	boothowto = pkargs->boothowto;
     93 	memcpy(&bootinfo, pkargs->bootinfo, sizeof(bootinfo));
     94 	memcpy(&bootspace, pkargs->bootspace, sizeof(bootspace));
     95 	esym = pkargs->esym;
     96 
     97 #ifndef REALEXTMEM
     98 	biosextmem = pkargs->biosextmem;
     99 #else
    100 	biosextmem = REALEXTMEM;
    101 #endif
    102 
    103 #ifndef REALBASEMEM
    104 	biosbasemem = pkargs->biosbasemem;
    105 #else
    106 	biosbasemem = REALBASEMEM;
    107 #endif
    108 
    109 	cpuid_level = pkargs->cpuid_level;
    110 	nox_flag = pkargs->nox_flag;
    111 	PDPpaddr = pkargs->PDPpaddr;
    112 	atdevbase = pkargs->atdevbase;
    113 	lwp0uarea = pkargs->lwp0uarea;
    114 }
    115 
    116 static void
    117 prekern_unmap_pte(void)
    118 {
    119 	extern struct bootspace bootspace;
    120 	pd_entry_t *pdir = (pd_entry_t *)bootspace.pdir;
    121 
    122 	/* Unmap the prekern recursive PTE slot. */
    123 	pdir[509] = 0;
    124 	tlbflushg();
    125 }
    126 
    127 static void
    128 prekern_unmap(void)
    129 {
    130 	/* Unmap the prekern itself. */
    131 	L4_BASE[0] = 0;
    132 	tlbflushg();
    133 }
    134 
    135 /*
    136  * The prekern jumps here.
    137  */
    138 int
    139 start_prekern(struct prekern_args *pkargs)
    140 {
    141 	paddr_t first_avail;
    142 
    143 	if (pkargs->version != PREKERN_API_VERSION) {
    144 		return -1;
    145 	}
    146 
    147 	prekern_copy_args(pkargs);
    148 	first_avail = pkargs->first_avail;
    149 
    150 	prekern_unmap_pte();
    151 	init_slotspace();
    152 	init_x86_64(first_avail);
    153 	prekern_unmap();
    154 
    155 	main();
    156 
    157 	panic("main returned");
    158 
    159 	return 0;
    160 }
    161