xref: /netbsd/sys/arch/amd64/amd64/prekern.c (revision 5becad1b)
1 /*	$NetBSD: prekern.c,v 1.4 2018/08/12 12:42:53 maxv 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/bootinfo.h>
44 #include <machine/cpufunc.h>
45 
46 #include <dev/isa/isareg.h>
47 #include <machine/isa_machdep.h>
48 
49 #define PREKERN_API_VERSION	2
50 
51 struct prekern_args {
52 	int version;
53 	int boothowto;
54 	void *bootinfo;
55 	void *bootspace;
56 	int esym;
57 	int biosextmem;
58 	int biosbasemem;
59 	int cpuid_level;
60 	uint32_t nox_flag;
61 	uint64_t PDPpaddr;
62 	vaddr_t atdevbase;
63 	vaddr_t lwp0uarea;
64 	paddr_t first_avail;
65 };
66 
67 void main(void);
68 void init_slotspace(void);
69 void init_x86_64(paddr_t);
70 
71 static void prekern_copy_args(struct prekern_args *);
72 static void prekern_unmap(void);
73 int start_prekern(struct prekern_args *);
74 
75 static void
76 prekern_copy_args(struct prekern_args *pkargs)
77 {
78 	extern int boothowto;
79 	extern struct bootinfo bootinfo;
80 	extern struct bootspace bootspace;
81 	extern int esym;
82 	extern int biosextmem;
83 	extern int biosbasemem;
84 	extern int cpuid_level;
85 	extern uint32_t nox_flag;
86 	extern uint64_t PDPpaddr;
87 	extern vaddr_t lwp0uarea;
88 
89 	boothowto = pkargs->boothowto;
90 	memcpy(&bootinfo, pkargs->bootinfo, sizeof(bootinfo));
91 	memcpy(&bootspace, pkargs->bootspace, sizeof(bootspace));
92 	esym = pkargs->esym;
93 
94 #ifndef REALEXTMEM
95 	biosextmem = pkargs->biosextmem;
96 #else
97 	biosextmem = REALEXTMEM;
98 #endif
99 
100 #ifndef REALBASEMEM
101 	biosbasemem = pkargs->biosbasemem;
102 #else
103 	biosbasemem = REALBASEMEM;
104 #endif
105 
106 	cpuid_level = pkargs->cpuid_level;
107 	nox_flag = pkargs->nox_flag;
108 	PDPpaddr = pkargs->PDPpaddr;
109 	atdevbase = pkargs->atdevbase;
110 	lwp0uarea = pkargs->lwp0uarea;
111 }
112 
113 static void
114 prekern_unmap(void)
115 {
116 	L4_BASE[0] = 0;
117 	tlbflushg();
118 }
119 
120 /*
121  * The prekern jumps here.
122  */
123 int
124 start_prekern(struct prekern_args *pkargs)
125 {
126 	paddr_t first_avail;
127 
128 	if (pkargs->version != PREKERN_API_VERSION) {
129 		return -1;
130 	}
131 
132 	prekern_copy_args(pkargs);
133 	first_avail = pkargs->first_avail;
134 
135 	init_slotspace();
136 	init_x86_64(first_avail);
137 
138 	prekern_unmap();
139 
140 	main();
141 
142 	panic("main returned");
143 
144 	return 0;
145 }
146