]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - stand/i386/libi386/bootinfo64.c
libi386: bd_io_workaround() is to be called for reads only
[FreeBSD/FreeBSD.git] / stand / i386 / libi386 / 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 <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"
40 #include "libi386.h"
41 #include "btxv86.h"
42
43 #ifdef LOADER_GELI_SUPPORT
44 #include "geliboot.h"
45 #endif
46
47 /*
48  * Copy module-related data into the load area, where it can be
49  * used as a directory for loaded modules.
50  *
51  * Module data is presented in a self-describing format.  Each datum
52  * is preceded by a 32-bit identifier and a 32-bit size field.
53  *
54  * Currently, the following data are saved:
55  *
56  * MOD_NAME     (variable)              module name (string)
57  * MOD_TYPE     (variable)              module type (string)
58  * MOD_ARGS     (variable)              module parameters (string)
59  * MOD_ADDR     sizeof(vm_offset_t)     module load address
60  * MOD_SIZE     sizeof(size_t)          module size
61  * MOD_METADATA (variable)              type-specific metadata
62  */
63 #define COPY32(v, a, c) {                       \
64     uint32_t    x = (v);                        \
65     if (c)                                      \
66         i386_copyin(&x, a, sizeof(x));          \
67     a += sizeof(x);                             \
68 }
69
70 #define MOD_STR(t, a, s, c) {                   \
71     COPY32(t, a, c);                            \
72     COPY32(strlen(s) + 1, a, c);                \
73     if (c)                                      \
74         i386_copyin(s, a, strlen(s) + 1);       \
75     a += roundup(strlen(s) + 1, sizeof(uint64_t));\
76 }
77
78 #define MOD_NAME(a, s, c)       MOD_STR(MODINFO_NAME, a, s, c)
79 #define MOD_TYPE(a, s, c)       MOD_STR(MODINFO_TYPE, a, s, c)
80 #define MOD_ARGS(a, s, c)       MOD_STR(MODINFO_ARGS, a, s, c)
81
82 #define MOD_VAR(t, a, s, c) {                   \
83     COPY32(t, a, c);                            \
84     COPY32(sizeof(s), a, c);                    \
85     if (c)                                      \
86         i386_copyin(&s, a, sizeof(s));          \
87     a += roundup(sizeof(s), sizeof(uint64_t));  \
88 }
89
90 #define MOD_ADDR(a, s, c)       MOD_VAR(MODINFO_ADDR, a, s, c)
91 #define MOD_SIZE(a, s, c)       MOD_VAR(MODINFO_SIZE, a, s, c)
92
93 #define MOD_METADATA(a, mm, c) {                \
94     COPY32(MODINFO_METADATA | mm->md_type, a, c); \
95     COPY32(mm->md_size, a, c);                  \
96     if (c)                                      \
97         i386_copyin(mm->md_data, a, mm->md_size); \
98     a += roundup(mm->md_size, sizeof(uint64_t));\
99 }
100
101 #define MOD_END(a, c) {                         \
102     COPY32(MODINFO_END, a, c);                  \
103     COPY32(0, a, c);                            \
104 }
105
106 static vm_offset_t
107 bi_copymodules64(vm_offset_t addr)
108 {
109     struct preloaded_file       *fp;
110     struct file_metadata        *md;
111     int                         c;
112     uint64_t                    v;
113
114     c = addr != 0;
115     /* start with the first module on the list, should be the kernel */
116     for (fp = file_findfile(NULL, NULL); fp != NULL; fp = fp->f_next) {
117
118         MOD_NAME(addr, fp->f_name, c);  /* this field must come first */
119         MOD_TYPE(addr, fp->f_type, c);
120         if (fp->f_args)
121             MOD_ARGS(addr, fp->f_args, c);
122         v = fp->f_addr;
123         MOD_ADDR(addr, v, c);
124         v = fp->f_size;
125         MOD_SIZE(addr, v, c);
126         for (md = fp->f_metadata; md != NULL; md = md->md_next)
127             if (!(md->md_type & MODINFOMD_NOCOPY))
128                 MOD_METADATA(addr, md, c);
129     }
130     MOD_END(addr, c);
131     return(addr);
132 }
133
134 /*
135  * Check to see if this CPU supports long mode.
136  */
137 static int
138 bi_checkcpu(void)
139 {
140     char *cpu_vendor;
141     int vendor[3];
142     int eflags;
143     unsigned int regs[4];
144
145     /* Check for presence of "cpuid". */
146     eflags = read_eflags();
147     write_eflags(eflags ^ PSL_ID);
148     if (!((eflags ^ read_eflags()) & PSL_ID))
149         return (0);
150
151     /* Fetch the vendor string. */
152     do_cpuid(0, regs);
153     vendor[0] = regs[1];
154     vendor[1] = regs[3];
155     vendor[2] = regs[2];
156     cpu_vendor = (char *)vendor;
157
158     /* Check for vendors that support AMD features. */
159     if (strncmp(cpu_vendor, INTEL_VENDOR_ID, 12) != 0 &&
160         strncmp(cpu_vendor, AMD_VENDOR_ID, 12) != 0 &&
161         strncmp(cpu_vendor, CENTAUR_VENDOR_ID, 12) != 0)
162         return (0);
163
164     /* Has to support AMD features. */
165     do_cpuid(0x80000000, regs);
166     if (!(regs[0] >= 0x80000001))
167         return (0);
168
169     /* Check for long mode. */
170     do_cpuid(0x80000001, regs);
171     return (regs[3] & AMDID_LM);
172 }
173
174 /*
175  * Load the information expected by an amd64 kernel.
176  *
177  * - The 'boothowto' argument is constructed
178  * - The 'bootdev' argument is constructed
179  * - The 'bootinfo' struct is constructed, and copied into the kernel space.
180  * - The kernel environment is copied into kernel space.
181  * - Module metadata are formatted and placed in kernel space.
182  */
183 int
184 bi_load64(char *args, vm_offset_t addr, vm_offset_t *modulep,
185     vm_offset_t *kernendp, int add_smap)
186 {
187     struct preloaded_file       *xp, *kfp;
188     struct i386_devdesc         *rootdev;
189     struct file_metadata        *md;
190     uint64_t                    kernend;
191     uint64_t                    envp;
192     uint64_t                    module;
193     vm_offset_t                 size;
194     char                        *rootdevname;
195     int                         howto;
196
197     if (!bi_checkcpu()) {
198         printf("CPU doesn't support long mode\n");
199         return (EINVAL);
200     }
201
202     howto = bi_getboothowto(args);
203
204     /*
205      * Allow the environment variable 'rootdev' to override the supplied device
206      * This should perhaps go to MI code and/or have $rootdev tested/set by
207      * MI code before launching the kernel.
208      */
209     rootdevname = getenv("rootdev");
210     i386_getdev((void **)(&rootdev), rootdevname, NULL);
211     if (rootdev == NULL) {              /* bad $rootdev/$currdev */
212         printf("can't determine root device\n");
213         return(EINVAL);
214     }
215
216     /* Try reading the /etc/fstab file to select the root device */
217     getrootmount(i386_fmtdev((void *)rootdev));
218
219     if (addr == 0) {
220         /* find the last module in the chain */
221         for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
222             if (addr < (xp->f_addr + xp->f_size))
223                 addr = xp->f_addr + xp->f_size;
224         }
225     }
226     /* pad to a page boundary */
227     addr = roundup(addr, PAGE_SIZE);
228
229     /* place the metadata before anything */
230     module = *modulep = addr;
231
232     kfp = file_findfile(NULL, "elf kernel");
233     if (kfp == NULL)
234       kfp = file_findfile(NULL, "elf64 kernel");
235     if (kfp == NULL)
236         panic("can't find kernel file");
237     kernend = 0;        /* fill it in later */
238     file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof howto, &howto);
239     file_addmetadata(kfp, MODINFOMD_ENVP, sizeof envp, &envp);
240     file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof kernend, &kernend);
241     file_addmetadata(kfp, MODINFOMD_MODULEP, sizeof module, &module);
242     if (add_smap != 0)
243         bios_addsmapdata(kfp);
244 #ifdef LOADER_GELI_SUPPORT
245     geli_export_key_metadata(kfp);
246 #endif
247
248     size = bi_copymodules64(0);
249
250     /* copy our environment */
251     envp = roundup(addr + size, PAGE_SIZE);
252     addr = bi_copyenv(envp);
253
254     /* set kernend */
255     kernend = roundup(addr, PAGE_SIZE);
256     *kernendp = kernend;
257
258     /* patch MODINFOMD_KERNEND */
259     md = file_findmetadata(kfp, MODINFOMD_KERNEND);
260     bcopy(&kernend, md->md_data, sizeof kernend);
261
262     /* patch MODINFOMD_ENVP */
263     md = file_findmetadata(kfp, MODINFOMD_ENVP);
264     bcopy(&envp, md->md_data, sizeof envp);
265
266     /* copy module list and metadata */
267     (void)bi_copymodules64(*modulep);
268
269     return(0);
270 }