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/psl.h>
37 #include <machine/specialreg.h>
38 #include "bootstrap.h"
43 * Copy module-related data into the load area, where it can be
44 * used as a directory for loaded modules.
46 * Module data is presented in a self-describing format. Each datum
47 * is preceded by a 32-bit identifier and a 32-bit size field.
49 * Currently, the following data are saved:
51 * MOD_NAME (variable) module name (string)
52 * MOD_TYPE (variable) module type (string)
53 * MOD_ARGS (variable) module parameters (string)
54 * MOD_ADDR sizeof(vm_offset_t) module load address
55 * MOD_SIZE sizeof(size_t) module size
56 * MOD_METADATA (variable) type-specific metadata
58 #define COPY32(v, a, c) { \
61 i386_copyin(&x, a, sizeof(x)); \
65 #define MOD_STR(t, a, s, c) { \
67 COPY32(strlen(s) + 1, a, c); \
69 i386_copyin(s, a, strlen(s) + 1); \
70 a += roundup(strlen(s) + 1, sizeof(u_int64_t));\
73 #define MOD_NAME(a, s, c) MOD_STR(MODINFO_NAME, a, s, c)
74 #define MOD_TYPE(a, s, c) MOD_STR(MODINFO_TYPE, a, s, c)
75 #define MOD_ARGS(a, s, c) MOD_STR(MODINFO_ARGS, a, s, c)
77 #define MOD_VAR(t, a, s, c) { \
79 COPY32(sizeof(s), a, c); \
81 i386_copyin(&s, a, sizeof(s)); \
82 a += roundup(sizeof(s), sizeof(u_int64_t)); \
85 #define MOD_ADDR(a, s, c) MOD_VAR(MODINFO_ADDR, a, s, c)
86 #define MOD_SIZE(a, s, c) MOD_VAR(MODINFO_SIZE, a, s, c)
88 #define MOD_METADATA(a, mm, c) { \
89 COPY32(MODINFO_METADATA | mm->md_type, a, c); \
90 COPY32(mm->md_size, a, c); \
92 i386_copyin(mm->md_data, a, mm->md_size); \
93 a += roundup(mm->md_size, sizeof(u_int64_t));\
96 #define MOD_END(a, c) { \
97 COPY32(MODINFO_END, a, c); \
102 bi_copymodules64(vm_offset_t addr)
104 struct preloaded_file *fp;
105 struct file_metadata *md;
110 /* start with the first module on the list, should be the kernel */
111 for (fp = file_findfile(NULL, NULL); fp != NULL; fp = fp->f_next) {
113 MOD_NAME(addr, fp->f_name, c); /* this field must come first */
114 MOD_TYPE(addr, fp->f_type, c);
116 MOD_ARGS(addr, fp->f_args, c);
118 MOD_ADDR(addr, v, c);
120 MOD_SIZE(addr, v, c);
121 for (md = fp->f_metadata; md != NULL; md = md->md_next)
122 if (!(md->md_type & MODINFOMD_NOCOPY))
123 MOD_METADATA(addr, md, c);
130 * Check to see if this CPU supports long mode.
138 unsigned int regs[4];
140 /* Check for presence of "cpuid". */
141 eflags = read_eflags();
142 write_eflags(eflags ^ PSL_ID);
143 if (!((eflags ^ read_eflags()) & PSL_ID))
146 /* Fetch the vendor string. */
151 cpu_vendor = (char *)vendor;
153 /* Check for vendors that support AMD features. */
154 if (strncmp(cpu_vendor, INTEL_VENDOR_ID, 12) != 0 &&
155 strncmp(cpu_vendor, AMD_VENDOR_ID, 12) != 0 &&
156 strncmp(cpu_vendor, CENTAUR_VENDOR_ID, 12) != 0)
159 /* Has to support AMD features. */
160 do_cpuid(0x80000000, regs);
161 if (!(regs[0] >= 0x80000001))
164 /* Check for long mode. */
165 do_cpuid(0x80000001, regs);
166 return (regs[3] & AMDID_LM);
170 * Load the information expected by an amd64 kernel.
172 * - The 'boothowto' argument is constructed
173 * - The 'bootdev' argument is constructed
174 * - The 'bootinfo' struct is constructed, and copied into the kernel space.
175 * - The kernel environment is copied into kernel space.
176 * - Module metadata are formatted and placed in kernel space.
179 bi_load64(char *args, vm_offset_t *modulep, vm_offset_t *kernendp)
181 struct preloaded_file *xp, *kfp;
182 struct i386_devdesc *rootdev;
183 struct file_metadata *md;
191 if (!bi_checkcpu()) {
192 printf("CPU doesn't support long mode\n");
196 howto = bi_getboothowto(args);
199 * Allow the environment variable 'rootdev' to override the supplied device
200 * This should perhaps go to MI code and/or have $rootdev tested/set by
201 * MI code before launching the kernel.
203 rootdevname = getenv("rootdev");
204 i386_getdev((void **)(&rootdev), rootdevname, NULL);
205 if (rootdev == NULL) { /* bad $rootdev/$currdev */
206 printf("can't determine root device\n");
210 /* Try reading the /etc/fstab file to select the root device */
211 getrootmount(i386_fmtdev((void *)rootdev));
213 /* find the last module in the chain */
215 for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
216 if (addr < (xp->f_addr + xp->f_size))
217 addr = xp->f_addr + xp->f_size;
219 /* pad to a page boundary */
220 addr = roundup(addr, PAGE_SIZE);
222 /* copy our environment */
224 addr = bi_copyenv(addr);
226 /* pad to a page boundary */
227 addr = roundup(addr, PAGE_SIZE);
229 kfp = file_findfile(NULL, "elf kernel");
231 kfp = file_findfile(NULL, "elf64 kernel");
233 panic("can't find kernel file");
234 kernend = 0; /* fill it in later */
235 file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof howto, &howto);
236 file_addmetadata(kfp, MODINFOMD_ENVP, sizeof envp, &envp);
237 file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof kernend, &kernend);
238 bios_addsmapdata(kfp);
240 /* Figure out the size and location of the metadata */
242 size = bi_copymodules64(0);
243 kernend = roundup(addr + size, PAGE_SIZE);
246 /* patch MODINFOMD_KERNEND */
247 md = file_findmetadata(kfp, MODINFOMD_KERNEND);
248 bcopy(&kernend, md->md_data, sizeof kernend);
250 /* copy module list and metadata */
251 (void)bi_copymodules64(addr);