2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
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"
43 #ifdef LOADER_GELI_SUPPORT
48 * Copy module-related data into the load area, where it can be
49 * used as a directory for loaded modules.
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.
54 * Currently, the following data are saved:
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
63 #define COPY32(v, a, c) { \
66 i386_copyin(&x, a, sizeof(x)); \
70 #define MOD_STR(t, a, s, c) { \
72 COPY32(strlen(s) + 1, a, c); \
74 i386_copyin(s, a, strlen(s) + 1); \
75 a += roundup(strlen(s) + 1, sizeof(uint64_t));\
78 #define MOD_NAME(a, s, c) MOD_STR(MODINFO_NAME, a, s, c)
79 #define MOD_TYPE(a, s, c) MOD_STR(MODINFO_TYPE, a, s, c)
80 #define MOD_ARGS(a, s, c) MOD_STR(MODINFO_ARGS, a, s, c)
82 #define MOD_VAR(t, a, s, c) { \
84 COPY32(sizeof(s), a, c); \
86 i386_copyin(&s, a, sizeof(s)); \
87 a += roundup(sizeof(s), sizeof(uint64_t)); \
90 #define MOD_ADDR(a, s, c) MOD_VAR(MODINFO_ADDR, a, s, c)
91 #define MOD_SIZE(a, s, c) MOD_VAR(MODINFO_SIZE, a, s, c)
93 #define MOD_METADATA(a, mm, c) { \
94 COPY32(MODINFO_METADATA | mm->md_type, a, c); \
95 COPY32(mm->md_size, a, c); \
97 i386_copyin(mm->md_data, a, mm->md_size); \
98 a += roundup(mm->md_size, sizeof(uint64_t));\
101 #define MOD_END(a, c) { \
102 COPY32(MODINFO_END, a, c); \
107 bi_copymodules64(vm_offset_t addr)
109 struct preloaded_file *fp;
110 struct file_metadata *md;
115 /* start with the first module on the list, should be the kernel */
116 for (fp = file_findfile(NULL, NULL); fp != NULL; fp = fp->f_next) {
118 MOD_NAME(addr, fp->f_name, c); /* this field must come first */
119 MOD_TYPE(addr, fp->f_type, c);
121 MOD_ARGS(addr, fp->f_args, c);
123 MOD_ADDR(addr, v, c);
125 MOD_SIZE(addr, v, c);
126 for (md = fp->f_metadata; md != NULL; md = md->md_next)
127 if (!(md->md_type & MODINFOMD_NOCOPY))
128 MOD_METADATA(addr, md, c);
135 * Check to see if this CPU supports long mode.
143 unsigned int regs[4];
145 /* Check for presence of "cpuid". */
146 eflags = read_eflags();
147 write_eflags(eflags ^ PSL_ID);
148 if (!((eflags ^ read_eflags()) & PSL_ID))
151 /* Fetch the vendor string. */
156 cpu_vendor = (char *)vendor;
158 /* Check for vendors that support AMD features. */
159 if (strncmp(cpu_vendor, INTEL_VENDOR_ID, 12) != 0 &&
160 strncmp(cpu_vendor, AMD_VENDOR_ID, 12) != 0 &&
161 strncmp(cpu_vendor, CENTAUR_VENDOR_ID, 12) != 0)
164 /* Has to support AMD features. */
165 do_cpuid(0x80000000, regs);
166 if (!(regs[0] >= 0x80000001))
169 /* Check for long mode. */
170 do_cpuid(0x80000001, regs);
171 return (regs[3] & AMDID_LM);
175 * Load the information expected by an amd64 kernel.
177 * - The 'boothowto' argument is constructed
178 * - The 'bootdev' argument is constructed
179 * - The 'bootinfo' struct is constructed, and copied into the kernel space.
180 * - The kernel environment is copied into kernel space.
181 * - Module metadata are formatted and placed in kernel space.
184 bi_load64(char *args, vm_offset_t addr, vm_offset_t *modulep,
185 vm_offset_t *kernendp, int add_smap)
187 struct preloaded_file *xp, *kfp;
188 struct i386_devdesc *rootdev;
189 struct file_metadata *md;
197 if (!bi_checkcpu()) {
198 printf("CPU doesn't support long mode\n");
202 howto = bi_getboothowto(args);
205 * Allow the environment variable 'rootdev' to override the supplied device
206 * This should perhaps go to MI code and/or have $rootdev tested/set by
207 * MI code before launching the kernel.
209 rootdevname = getenv("rootdev");
210 i386_getdev((void **)(&rootdev), rootdevname, NULL);
211 if (rootdev == NULL) { /* bad $rootdev/$currdev */
212 printf("can't determine root device\n");
216 /* Try reading the /etc/fstab file to select the root device */
217 getrootmount(i386_fmtdev((void *)rootdev));
220 /* find the last module in the chain */
221 for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
222 if (addr < (xp->f_addr + xp->f_size))
223 addr = xp->f_addr + xp->f_size;
226 /* pad to a page boundary */
227 addr = roundup(addr, PAGE_SIZE);
229 /* place the metadata before anything */
230 module = *modulep = addr;
232 kfp = file_findfile(NULL, "elf kernel");
234 kfp = file_findfile(NULL, "elf64 kernel");
236 panic("can't find kernel file");
237 kernend = 0; /* fill it in later */
238 file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof howto, &howto);
239 file_addmetadata(kfp, MODINFOMD_ENVP, sizeof envp, &envp);
240 file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof kernend, &kernend);
241 file_addmetadata(kfp, MODINFOMD_MODULEP, sizeof module, &module);
243 bios_addsmapdata(kfp);
244 #ifdef LOADER_GELI_SUPPORT
245 geli_export_key_metadata(kfp);
248 size = bi_copymodules64(0);
250 /* copy our environment */
251 envp = roundup(addr + size, PAGE_SIZE);
252 addr = bi_copyenv(envp);
255 kernend = roundup(addr, PAGE_SIZE);
258 /* patch MODINFOMD_KERNEND */
259 md = file_findmetadata(kfp, MODINFOMD_KERNEND);
260 bcopy(&kernend, md->md_data, sizeof kernend);
262 /* patch MODINFOMD_ENVP */
263 md = file_findmetadata(kfp, MODINFOMD_ENVP);
264 bcopy(&envp, md->md_data, sizeof envp);
266 /* copy module list and metadata */
267 (void)bi_copymodules64(*modulep);