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>
29 #include <sys/param.h>
30 #include <sys/reboot.h>
31 #include <sys/linker.h>
32 #include <machine/bootinfo.h>
33 #include <machine/cpufunc.h>
34 #include <machine/metadata.h>
35 #include <machine/psl.h>
36 #include <machine/specialreg.h>
37 #include "bootstrap.h"
42 #ifdef LOADER_GELI_SUPPORT
47 * Check to see if this CPU supports long mode.
57 /* Check for presence of "cpuid". */
58 eflags = read_eflags();
59 write_eflags(eflags ^ PSL_ID);
60 if (!((eflags ^ read_eflags()) & PSL_ID))
63 /* Fetch the vendor string. */
68 cpu_vendor = (char *)vendor;
70 /* Check for vendors that support AMD features. */
71 if (strncmp(cpu_vendor, INTEL_VENDOR_ID, 12) != 0 &&
72 strncmp(cpu_vendor, AMD_VENDOR_ID, 12) != 0 &&
73 strncmp(cpu_vendor, HYGON_VENDOR_ID, 12) != 0 &&
74 strncmp(cpu_vendor, CENTAUR_VENDOR_ID, 12) != 0)
77 /* Has to support AMD features. */
78 do_cpuid(0x80000000, regs);
79 if (!(regs[0] >= 0x80000001))
82 /* Check for long mode. */
83 do_cpuid(0x80000001, regs);
84 return (regs[3] & AMDID_LM);
88 * Load the information expected by an amd64 kernel.
90 * - The 'boothowto' argument is constructed
91 * - The 'bootdev' argument is constructed
92 * - The 'bootinfo' struct is constructed, and copied into the kernel space.
93 * - The kernel environment is copied into kernel space.
94 * - Module metadata are formatted and placed in kernel space.
97 bi_load64(char *args, vm_offset_t *modulep,
98 vm_offset_t *kernendp, int add_smap)
100 struct preloaded_file *xp, *kfp;
101 struct i386_devdesc *rootdev;
102 struct file_metadata *md;
111 if (!bi_checkcpu()) {
112 printf("CPU doesn't support long mode\n");
116 howto = bi_getboothowto(args);
119 * Allow the environment variable 'rootdev' to override the supplied device
120 * This should perhaps go to MI code and/or have $rootdev tested/set by
121 * MI code before launching the kernel.
123 rootdevname = getenv("rootdev");
124 i386_getdev((void **)(&rootdev), rootdevname, NULL);
125 if (rootdev == NULL) { /* bad $rootdev/$currdev */
126 printf("can't determine root device\n");
130 /* Try reading the /etc/fstab file to select the root device */
131 getrootmount(devformat(&rootdev->dd));
134 /* find the last module in the chain */
135 for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
136 if (addr < (xp->f_addr + xp->f_size))
137 addr = xp->f_addr + xp->f_size;
139 /* pad to a page boundary */
140 addr = roundup(addr, PAGE_SIZE);
142 addr = build_font_module(addr);
144 /* place the metadata before anything */
145 module = *modulep = addr;
147 kfp = file_findfile(NULL, "elf kernel");
149 kfp = file_findfile(NULL, "elf64 kernel");
151 panic("can't find kernel file");
152 kernend = 0; /* fill it in later */
153 file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof howto, &howto);
154 file_addmetadata(kfp, MODINFOMD_ENVP, sizeof envp, &envp);
155 file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof kernend, &kernend);
156 file_addmetadata(kfp, MODINFOMD_MODULEP, sizeof module, &module);
158 bios_addsmapdata(kfp);
159 #ifdef LOADER_GELI_SUPPORT
160 geli_export_key_metadata(kfp);
162 bi_load_vbe_data(kfp);
164 size = md_copymodules(0, true);
166 /* copy our environment */
167 envp = roundup(addr + size, PAGE_SIZE);
168 addr = md_copyenv(envp);
171 kernend = roundup(addr, PAGE_SIZE);
174 /* patch MODINFOMD_KERNEND */
175 md = file_findmetadata(kfp, MODINFOMD_KERNEND);
176 bcopy(&kernend, md->md_data, sizeof kernend);
178 /* patch MODINFOMD_ENVP */
179 md = file_findmetadata(kfp, MODINFOMD_ENVP);
180 bcopy(&envp, md->md_data, sizeof envp);
182 /* copy module list and metadata */
183 (void)md_copymodules(*modulep, true);