xref: /freebsd/stand/i386/libi386/bootinfo64.c (revision 8b19d28d)
1 /*-
2  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <stand.h>
31 #include <sys/param.h>
32 #include <sys/reboot.h>
33 #include <sys/linker.h>
34 #include <machine/bootinfo.h>
35 #include <machine/cpufunc.h>
36 #include <machine/metadata.h>
37 #include <machine/psl.h>
38 #include <machine/specialreg.h>
39 #include "bootstrap.h"
40 #include "libi386.h"
41 #include "btxv86.h"
42 
43 #ifdef LOADER_GELI_SUPPORT
44 #include "geliboot.h"
45 #endif
46 
47 /*
48  * Copy module-related data into the load area, where it can be
49  * used as a directory for loaded modules.
50  *
51  * Module data is presented in a self-describing format.  Each datum
52  * is preceded by a 32-bit identifier and a 32-bit size field.
53  *
54  * Currently, the following data are saved:
55  *
56  * MOD_NAME	(variable)		module name (string)
57  * MOD_TYPE	(variable)		module type (string)
58  * MOD_ARGS	(variable)		module parameters (string)
59  * MOD_ADDR	sizeof(vm_offset_t)	module load address
60  * MOD_SIZE	sizeof(size_t)		module size
61  * MOD_METADATA	(variable)		type-specific metadata
62  */
63 #define MOD_ALIGN(l)	roundup(l, sizeof(uint64_t))
64 #define COPY32(v, a, c) {			\
65     uint32_t	x = (v);			\
66     if (c)					\
67 	archsw.arch_copyin(&x, a, sizeof(x));	\
68     a += sizeof(x);				\
69 }
70 
71 #define MOD_STR(t, a, s, c) {			\
72     COPY32(t, a, c);				\
73     COPY32(strlen(s) + 1, a, c);		\
74     if (c)					\
75 	archsw.arch_copyin(s, a, strlen(s) + 1); \
76     a += MOD_ALIGN(strlen(s) + 1);		\
77 }
78 
79 #define MOD_NAME(a, s, c)	MOD_STR(MODINFO_NAME, a, s, c)
80 #define MOD_TYPE(a, s, c)	MOD_STR(MODINFO_TYPE, a, s, c)
81 #define MOD_ARGS(a, s, c)	MOD_STR(MODINFO_ARGS, a, s, c)
82 
83 #define MOD_VAR(t, a, s, c) {			\
84     COPY32(t, a, c);				\
85     COPY32(sizeof(s), a, c);			\
86     if (c)					\
87 	archsw.arch_copyin(&s, a, sizeof(s));	\
88     a += MOD_ALIGN(sizeof(s));			\
89 }
90 
91 #define MOD_ADDR(a, s, c)	MOD_VAR(MODINFO_ADDR, a, s, c)
92 #define MOD_SIZE(a, s, c)	MOD_VAR(MODINFO_SIZE, a, s, c)
93 
94 #define MOD_METADATA(a, mm, c) {		\
95     COPY32(MODINFO_METADATA | mm->md_type, a, c); \
96     COPY32(mm->md_size, a, c);			\
97     if (c)					\
98 	archsw.arch_copyin(mm->md_data, a, mm->md_size); \
99     a += MOD_ALIGN(mm->md_size);		\
100 }
101 
102 #define MOD_END(a, c) {				\
103     COPY32(MODINFO_END, a, c);			\
104     COPY32(0, a, c);				\
105 }
106 
107 static vm_offset_t
108 bi_copymodules64(vm_offset_t addr)
109 {
110     struct preloaded_file	*fp;
111     struct file_metadata	*md;
112     int				c;
113     uint64_t			v;
114 
115     c = addr != 0;
116     /* start with the first module on the list, should be the kernel */
117     for (fp = file_findfile(NULL, NULL); fp != NULL; fp = fp->f_next) {
118 
119 	MOD_NAME(addr, fp->f_name, c);	/* this field must come first */
120 	MOD_TYPE(addr, fp->f_type, c);
121 	if (fp->f_args)
122 	    MOD_ARGS(addr, fp->f_args, c);
123 	v = fp->f_addr;
124 	MOD_ADDR(addr, v, c);
125 	v = fp->f_size;
126 	MOD_SIZE(addr, v, c);
127 	for (md = fp->f_metadata; md != NULL; md = md->md_next)
128 	    if (!(md->md_type & MODINFOMD_NOCOPY))
129 		MOD_METADATA(addr, md, c);
130     }
131     MOD_END(addr, c);
132     return(addr);
133 }
134 
135 /*
136  * Check to see if this CPU supports long mode.
137  */
138 static int
139 bi_checkcpu(void)
140 {
141     char *cpu_vendor;
142     int vendor[3];
143     int eflags;
144     unsigned int regs[4];
145 
146     /* Check for presence of "cpuid". */
147     eflags = read_eflags();
148     write_eflags(eflags ^ PSL_ID);
149     if (!((eflags ^ read_eflags()) & PSL_ID))
150 	return (0);
151 
152     /* Fetch the vendor string. */
153     do_cpuid(0, regs);
154     vendor[0] = regs[1];
155     vendor[1] = regs[3];
156     vendor[2] = regs[2];
157     cpu_vendor = (char *)vendor;
158 
159     /* Check for vendors that support AMD features. */
160     if (strncmp(cpu_vendor, INTEL_VENDOR_ID, 12) != 0 &&
161 	strncmp(cpu_vendor, AMD_VENDOR_ID, 12) != 0 &&
162 	strncmp(cpu_vendor, HYGON_VENDOR_ID, 12) != 0 &&
163 	strncmp(cpu_vendor, CENTAUR_VENDOR_ID, 12) != 0)
164 	return (0);
165 
166     /* Has to support AMD features. */
167     do_cpuid(0x80000000, regs);
168     if (!(regs[0] >= 0x80000001))
169 	return (0);
170 
171     /* Check for long mode. */
172     do_cpuid(0x80000001, regs);
173     return (regs[3] & AMDID_LM);
174 }
175 
176 /*
177  * Load the information expected by an amd64 kernel.
178  *
179  * - The 'boothowto' argument is constructed
180  * - The 'bootdev' argument is constructed
181  * - The 'bootinfo' struct is constructed, and copied into the kernel space.
182  * - The kernel environment is copied into kernel space.
183  * - Module metadata are formatted and placed in kernel space.
184  */
185 int
186 bi_load64(char *args, vm_offset_t *modulep,
187     vm_offset_t *kernendp, int add_smap)
188 {
189     struct preloaded_file	*xp, *kfp;
190     struct i386_devdesc		*rootdev;
191     struct file_metadata	*md;
192     uint64_t			kernend;
193     uint64_t			envp;
194     uint64_t			module;
195     uint64_t			addr;
196     vm_offset_t			size;
197     char			*rootdevname;
198     int				howto;
199 
200     if (!bi_checkcpu()) {
201 	printf("CPU doesn't support long mode\n");
202 	return (EINVAL);
203     }
204 
205     howto = bi_getboothowto(args);
206 
207     /*
208      * Allow the environment variable 'rootdev' to override the supplied device
209      * This should perhaps go to MI code and/or have $rootdev tested/set by
210      * MI code before launching the kernel.
211      */
212     rootdevname = getenv("rootdev");
213     i386_getdev((void **)(&rootdev), rootdevname, NULL);
214     if (rootdev == NULL) {		/* bad $rootdev/$currdev */
215 	printf("can't determine root device\n");
216 	return(EINVAL);
217     }
218 
219     /* Try reading the /etc/fstab file to select the root device */
220     getrootmount(devformat(&rootdev->dd));
221 
222     addr = 0;
223     /* find the last module in the chain */
224     for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
225         if (addr < (xp->f_addr + xp->f_size))
226             addr = xp->f_addr + xp->f_size;
227     }
228     /* pad to a page boundary */
229     addr = roundup(addr, PAGE_SIZE);
230 
231     addr = build_font_module(addr);
232 
233     /* place the metadata before anything */
234     module = *modulep = addr;
235 
236     kfp = file_findfile(NULL, "elf kernel");
237     if (kfp == NULL)
238       kfp = file_findfile(NULL, "elf64 kernel");
239     if (kfp == NULL)
240 	panic("can't find kernel file");
241     kernend = 0;	/* fill it in later */
242     file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof howto, &howto);
243     file_addmetadata(kfp, MODINFOMD_ENVP, sizeof envp, &envp);
244     file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof kernend, &kernend);
245     file_addmetadata(kfp, MODINFOMD_MODULEP, sizeof module, &module);
246     if (add_smap != 0)
247         bios_addsmapdata(kfp);
248 #ifdef LOADER_GELI_SUPPORT
249     geli_export_key_metadata(kfp);
250 #endif
251     bi_load_vbe_data(kfp);
252 
253     size = bi_copymodules64(0);
254 
255     /* copy our environment */
256     envp = roundup(addr + size, PAGE_SIZE);
257     addr = bi_copyenv(envp);
258 
259     /* set kernend */
260     kernend = roundup(addr, PAGE_SIZE);
261     *kernendp = kernend;
262 
263     /* patch MODINFOMD_KERNEND */
264     md = file_findmetadata(kfp, MODINFOMD_KERNEND);
265     bcopy(&kernend, md->md_data, sizeof kernend);
266 
267     /* patch MODINFOMD_ENVP */
268     md = file_findmetadata(kfp, MODINFOMD_ENVP);
269     bcopy(&envp, md->md_data, sizeof envp);
270 
271     /* copy module list and metadata */
272     (void)bi_copymodules64(*modulep);
273 
274     return(0);
275 }
276