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 #include <stand.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"
38 #include "modinfo.h"
39 #include "libi386.h"
40 #include "btxv86.h"
41
42 #ifdef LOADER_GELI_SUPPORT
43 #include "geliboot.h"
44 #endif
45
46 /*
47 * Check to see if this CPU supports long mode.
48 */
49 static int
bi_checkcpu(void)50 bi_checkcpu(void)
51 {
52 char *cpu_vendor;
53 int vendor[3];
54 int eflags;
55 unsigned int regs[4];
56
57 /* Check for presence of "cpuid". */
58 eflags = read_eflags();
59 write_eflags(eflags ^ PSL_ID);
60 if (!((eflags ^ read_eflags()) & PSL_ID))
61 return (0);
62
63 /* Fetch the vendor string. */
64 do_cpuid(0, regs);
65 vendor[0] = regs[1];
66 vendor[1] = regs[3];
67 vendor[2] = regs[2];
68 cpu_vendor = (char *)vendor;
69
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)
75 return (0);
76
77 /* Has to support AMD features. */
78 do_cpuid(0x80000000, regs);
79 if (!(regs[0] >= 0x80000001))
80 return (0);
81
82 /* Check for long mode. */
83 do_cpuid(0x80000001, regs);
84 return (regs[3] & AMDID_LM);
85 }
86
87 /*
88 * Load the information expected by an amd64 kernel.
89 *
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.
95 */
96 int
bi_load64(char * args,vm_offset_t * modulep,vm_offset_t * kernendp,int add_smap)97 bi_load64(char *args, vm_offset_t *modulep,
98 vm_offset_t *kernendp, int add_smap)
99 {
100 struct preloaded_file *xp, *kfp;
101 struct i386_devdesc *rootdev;
102 struct file_metadata *md;
103 uint64_t kernend;
104 uint64_t envp;
105 uint64_t module;
106 uint64_t addr;
107 vm_offset_t size;
108 char *rootdevname;
109 int howto;
110
111 if (!bi_checkcpu()) {
112 printf("CPU doesn't support long mode\n");
113 return (EINVAL);
114 }
115
116 howto = bi_getboothowto(args);
117
118 /*
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.
122 */
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");
127 return(EINVAL);
128 }
129
130 /* Try reading the /etc/fstab file to select the root device */
131 getrootmount(devformat(&rootdev->dd));
132
133 addr = 0;
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;
138 }
139 /* pad to a page boundary */
140 addr = roundup(addr, PAGE_SIZE);
141
142 addr = build_font_module(addr);
143
144 /* place the metadata before anything */
145 module = *modulep = addr;
146
147 kfp = file_findfile(NULL, "elf kernel");
148 if (kfp == NULL)
149 kfp = file_findfile(NULL, "elf64 kernel");
150 if (kfp == NULL)
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);
157 if (add_smap != 0)
158 bios_addsmapdata(kfp);
159 #ifdef LOADER_GELI_SUPPORT
160 geli_export_key_metadata(kfp);
161 #endif
162 bi_load_vbe_data(kfp);
163
164 size = md_copymodules(0, true);
165
166 /* copy our environment */
167 envp = roundup(addr + size, PAGE_SIZE);
168 addr = md_copyenv(envp);
169
170 /* set kernend */
171 kernend = roundup(addr, PAGE_SIZE);
172 *kernendp = kernend;
173
174 /* patch MODINFOMD_KERNEND */
175 md = file_findmetadata(kfp, MODINFOMD_KERNEND);
176 bcopy(&kernend, md->md_data, sizeof kernend);
177
178 /* patch MODINFOMD_ENVP */
179 md = file_findmetadata(kfp, MODINFOMD_ENVP);
180 bcopy(&envp, md->md_data, sizeof envp);
181
182 /* copy module list and metadata */
183 (void)md_copymodules(*modulep, true);
184
185 return(0);
186 }
187