]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - sys/boot/userboot/userboot/bootinfo64.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / sys / boot / userboot / userboot / bootinfo64.c
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 __FBSDID("$FreeBSD$");
29
30 #include <stand.h>
31 #include <sys/param.h>
32 #include <sys/reboot.h>
33 #include <sys/linker.h>
34 #include <i386/include/bootinfo.h>
35 #include <machine/cpufunc.h>
36 #include <machine/psl.h>
37 #include <machine/specialreg.h>
38
39 #include "bootstrap.h"
40 #include "libuserboot.h"
41
42 /*
43  * Copy module-related data into the load area, where it can be
44  * used as a directory for loaded modules.
45  *
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.
48  *
49  * Currently, the following data are saved:
50  *
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
57  */
58 #define COPY32(v, a, c) {                       \
59     u_int32_t   x = (v);                        \
60     if (c)                                      \
61         CALLBACK(copyin, &x, a, sizeof(x));     \
62     a += sizeof(x);                             \
63 }
64
65 #define MOD_STR(t, a, s, c) {                   \
66     COPY32(t, a, c);                            \
67     COPY32(strlen(s) + 1, a, c);                \
68     if (c)                                      \
69         CALLBACK(copyin, s, a, strlen(s) + 1);  \
70     a += roundup(strlen(s) + 1, sizeof(u_int64_t));\
71 }
72
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)
76
77 #define MOD_VAR(t, a, s, c) {                   \
78     COPY32(t, a, c);                            \
79     COPY32(sizeof(s), a, c);                    \
80     if (c)                                      \
81         CALLBACK(copyin, &s, a, sizeof(s));     \
82     a += roundup(sizeof(s), sizeof(u_int64_t)); \
83 }
84
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)
87
88 #define MOD_METADATA(a, mm, c) {                \
89     COPY32(MODINFO_METADATA | mm->md_type, a, c); \
90     COPY32(mm->md_size, a, c);                  \
91     if (c)                                      \
92         CALLBACK(copyin, mm->md_data, a, mm->md_size);    \
93     a += roundup(mm->md_size, sizeof(u_int64_t));\
94 }
95
96 #define MOD_END(a, c) {                         \
97     COPY32(MODINFO_END, a, c);                  \
98     COPY32(0, a, c);                            \
99 }
100
101 static vm_offset_t
102 bi_copymodules64(vm_offset_t addr)
103 {
104     struct preloaded_file       *fp;
105     struct file_metadata        *md;
106     int                         c;
107     u_int64_t                   v;
108
109     c = addr != 0;
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) {
112
113         MOD_NAME(addr, fp->f_name, c);  /* this field must come first */
114         MOD_TYPE(addr, fp->f_type, c);
115         if (fp->f_args)
116             MOD_ARGS(addr, fp->f_args, c);
117         v = fp->f_addr;
118         MOD_ADDR(addr, v, c);
119         v = fp->f_size;
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);
124     }
125     MOD_END(addr, c);
126     return(addr);
127 }
128
129 /*
130  * Check to see if this CPU supports long mode.
131  */
132 static int
133 bi_checkcpu(void)
134 {
135 #if 0
136     char *cpu_vendor;
137     int vendor[3];
138     int eflags, regs[4];
139
140     /* Check for presence of "cpuid". */
141     eflags = read_eflags();
142     write_eflags(eflags ^ PSL_ID);
143     if (!((eflags ^ read_eflags()) & PSL_ID))
144         return (0);
145
146     /* Fetch the vendor string. */
147     do_cpuid(0, regs);
148     vendor[0] = regs[1];
149     vendor[1] = regs[3];
150     vendor[2] = regs[2];
151     cpu_vendor = (char *)vendor;
152
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)
157         return (0);
158
159     /* Has to support AMD features. */
160     do_cpuid(0x80000000, regs);
161     if (!(regs[0] >= 0x80000001))
162         return (0);
163
164     /* Check for long mode. */
165     do_cpuid(0x80000001, regs);
166     return (regs[3] & AMDID_LM);
167 #else
168         return (1);
169 #endif
170 }
171
172 struct smap {
173         uint64_t       base;
174         uint64_t       length;
175         uint32_t       type;
176 } __packed;
177
178 /* From FreeBSD <machine/pc/bios.h> */
179 #define SMAP_TYPE_MEMORY        1
180
181 #define GB (1024UL * 1024 * 1024)
182
183 #define MODINFOMD_SMAP          0x1001
184
185 static void
186 bios_addsmapdata(struct preloaded_file *kfp)
187 {
188         uint64_t lowmem, highmem;
189         int smapnum, len;
190         struct smap smap[3], *sm;
191
192         CALLBACK(getmem, &lowmem, &highmem);
193
194         sm = &smap[0];
195
196         sm->base = 0;                           /* base memory */
197         sm->length = 640 * 1024;
198         sm->type = SMAP_TYPE_MEMORY;
199         sm++;
200
201         sm->base = 0x100000;                    /* extended memory */
202         sm->length = lowmem - 0x100000;
203         sm->type = SMAP_TYPE_MEMORY;
204         sm++;
205
206         smapnum = 2;
207
208         if (highmem != 0) {
209                 sm->base = 4 * GB;
210                 sm->length = highmem;
211                 sm->type = SMAP_TYPE_MEMORY;
212                 smapnum++;
213         }
214
215         len = smapnum * sizeof (struct smap);
216         file_addmetadata(kfp, MODINFOMD_SMAP, len, &smap[0]);
217 }
218
219 /*
220  * Load the information expected by an amd64 kernel.
221  *
222  * - The 'boothowto' argument is constructed
223  * - The 'bootdev' argument is constructed
224  * - The 'bootinfo' struct is constructed, and copied into the kernel space.
225  * - The kernel environment is copied into kernel space.
226  * - Module metadata are formatted and placed in kernel space.
227  */
228 int
229 bi_load64(char *args, vm_offset_t *modulep, vm_offset_t *kernendp)
230 {
231     struct preloaded_file       *xp, *kfp;
232     struct userboot_devdesc     *rootdev;
233     struct file_metadata        *md;
234     vm_offset_t                 addr;
235     u_int64_t                   kernend;
236     u_int64_t                   envp;
237     vm_offset_t                 size;
238     char                        *rootdevname;
239     int                         howto;
240
241     if (!bi_checkcpu()) {
242         printf("CPU doesn't support long mode\n");
243         return (EINVAL);
244     }
245
246     howto = bi_getboothowto(args);
247
248     /* 
249      * Allow the environment variable 'rootdev' to override the supplied device 
250      * This should perhaps go to MI code and/or have $rootdev tested/set by
251      * MI code before launching the kernel.
252      */
253     rootdevname = getenv("rootdev");
254     userboot_getdev((void **)(&rootdev), rootdevname, NULL);
255     if (rootdev == NULL) {              /* bad $rootdev/$currdev */
256         printf("can't determine root device\n");
257         return(EINVAL);
258     }
259
260     /* Try reading the /etc/fstab file to select the root device */
261     getrootmount(userboot_fmtdev((void *)rootdev));
262
263     /* find the last module in the chain */
264     addr = 0;
265     for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
266         if (addr < (xp->f_addr + xp->f_size))
267             addr = xp->f_addr + xp->f_size;
268     }
269     /* pad to a page boundary */
270     addr = roundup(addr, PAGE_SIZE);
271
272     /* copy our environment */
273     envp = addr;
274     addr = bi_copyenv(addr);
275
276     /* pad to a page boundary */
277     addr = roundup(addr, PAGE_SIZE);
278
279     kfp = file_findfile(NULL, "elf kernel");
280     if (kfp == NULL)
281       kfp = file_findfile(NULL, "elf64 kernel");
282     if (kfp == NULL)
283         panic("can't find kernel file");
284     kernend = 0;        /* fill it in later */
285     file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof howto, &howto);
286     file_addmetadata(kfp, MODINFOMD_ENVP, sizeof envp, &envp);
287     file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof kernend, &kernend);
288     bios_addsmapdata(kfp);
289
290     /* Figure out the size and location of the metadata */
291     *modulep = addr;
292     size = bi_copymodules64(0);
293     kernend = roundup(addr + size, PAGE_SIZE);
294     *kernendp = kernend;
295
296     /* patch MODINFOMD_KERNEND */
297     md = file_findmetadata(kfp, MODINFOMD_KERNEND);
298     bcopy(&kernend, md->md_data, sizeof kernend);
299
300     /* copy module list and metadata */
301     (void)bi_copymodules64(addr);
302
303     return(0);
304 }