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"
44 #ifdef LOADER_GELI_SUPPORT
49 * Check to see if this CPU supports long mode.
59 /* Check for presence of "cpuid". */
60 eflags = read_eflags();
61 write_eflags(eflags ^ PSL_ID);
62 if (!((eflags ^ read_eflags()) & PSL_ID))
65 /* Fetch the vendor string. */
70 cpu_vendor = (char *)vendor;
72 /* Check for vendors that support AMD features. */
73 if (strncmp(cpu_vendor, INTEL_VENDOR_ID, 12) != 0 &&
74 strncmp(cpu_vendor, AMD_VENDOR_ID, 12) != 0 &&
75 strncmp(cpu_vendor, HYGON_VENDOR_ID, 12) != 0 &&
76 strncmp(cpu_vendor, CENTAUR_VENDOR_ID, 12) != 0)
79 /* Has to support AMD features. */
80 do_cpuid(0x80000000, regs);
81 if (!(regs[0] >= 0x80000001))
84 /* Check for long mode. */
85 do_cpuid(0x80000001, regs);
86 return (regs[3] & AMDID_LM);
90 * Load the information expected by an amd64 kernel.
92 * - The 'boothowto' argument is constructed
93 * - The 'bootdev' argument is constructed
94 * - The 'bootinfo' struct is constructed, and copied into the kernel space.
95 * - The kernel environment is copied into kernel space.
96 * - Module metadata are formatted and placed in kernel space.
99 bi_load64(char *args, vm_offset_t *modulep,
100 vm_offset_t *kernendp, int add_smap)
102 struct preloaded_file *xp, *kfp;
103 struct i386_devdesc *rootdev;
104 struct file_metadata *md;
113 if (!bi_checkcpu()) {
114 printf("CPU doesn't support long mode\n");
118 howto = bi_getboothowto(args);
121 * Allow the environment variable 'rootdev' to override the supplied device
122 * This should perhaps go to MI code and/or have $rootdev tested/set by
123 * MI code before launching the kernel.
125 rootdevname = getenv("rootdev");
126 i386_getdev((void **)(&rootdev), rootdevname, NULL);
127 if (rootdev == NULL) { /* bad $rootdev/$currdev */
128 printf("can't determine root device\n");
132 /* Try reading the /etc/fstab file to select the root device */
133 getrootmount(devformat(&rootdev->dd));
136 /* find the last module in the chain */
137 for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
138 if (addr < (xp->f_addr + xp->f_size))
139 addr = xp->f_addr + xp->f_size;
141 /* pad to a page boundary */
142 addr = roundup(addr, PAGE_SIZE);
144 addr = build_font_module(addr);
146 /* place the metadata before anything */
147 module = *modulep = addr;
149 kfp = file_findfile(NULL, "elf kernel");
151 kfp = file_findfile(NULL, "elf64 kernel");
153 panic("can't find kernel file");
154 kernend = 0; /* fill it in later */
155 file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof howto, &howto);
156 file_addmetadata(kfp, MODINFOMD_ENVP, sizeof envp, &envp);
157 file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof kernend, &kernend);
158 file_addmetadata(kfp, MODINFOMD_MODULEP, sizeof module, &module);
160 bios_addsmapdata(kfp);
161 #ifdef LOADER_GELI_SUPPORT
162 geli_export_key_metadata(kfp);
164 bi_load_vbe_data(kfp);
166 size = md_copymodules(0, true);
168 /* copy our environment */
169 envp = roundup(addr + size, PAGE_SIZE);
170 addr = md_copyenv(envp);
173 kernend = roundup(addr, PAGE_SIZE);
176 /* patch MODINFOMD_KERNEND */
177 md = file_findmetadata(kfp, MODINFOMD_KERNEND);
178 bcopy(&kernend, md->md_data, sizeof kernend);
180 /* patch MODINFOMD_ENVP */
181 md = file_findmetadata(kfp, MODINFOMD_ENVP);
182 bcopy(&envp, md->md_data, sizeof envp);
184 /* copy module list and metadata */
185 (void)md_copymodules(*modulep, true);