xref: /freebsd/stand/i386/libi386/bootinfo64.c (revision a8089ea5aee578e08acab2438e82fc9a9ae50ed8)
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 <stand.h>
28 #include <sys/param.h>
29 #include <sys/reboot.h>
30 #include <sys/linker.h>
31 #include <machine/bootinfo.h>
32 #include <machine/cpufunc.h>
33 #include <machine/metadata.h>
34 #include <machine/psl.h>
35 #include <machine/specialreg.h>
36 #include "bootstrap.h"
37 #include "modinfo.h"
38 #include "libi386.h"
39 #include "btxv86.h"
40 
41 #ifdef LOADER_GELI_SUPPORT
42 #include "geliboot.h"
43 #endif
44 
45 /*
46  * Check to see if this CPU supports long mode.
47  */
48 static int
49 bi_checkcpu(void)
50 {
51     char *cpu_vendor;
52     int vendor[3];
53     int eflags;
54     unsigned int regs[4];
55 
56     /* Check for presence of "cpuid". */
57     eflags = read_eflags();
58     write_eflags(eflags ^ PSL_ID);
59     if (!((eflags ^ read_eflags()) & PSL_ID))
60 	return (0);
61 
62     /* Fetch the vendor string. */
63     do_cpuid(0, regs);
64     vendor[0] = regs[1];
65     vendor[1] = regs[3];
66     vendor[2] = regs[2];
67     cpu_vendor = (char *)vendor;
68 
69     /* Check for vendors that support AMD features. */
70     if (strncmp(cpu_vendor, INTEL_VENDOR_ID, 12) != 0 &&
71 	strncmp(cpu_vendor, AMD_VENDOR_ID, 12) != 0 &&
72 	strncmp(cpu_vendor, HYGON_VENDOR_ID, 12) != 0 &&
73 	strncmp(cpu_vendor, CENTAUR_VENDOR_ID, 12) != 0)
74 	return (0);
75 
76     /* Has to support AMD features. */
77     do_cpuid(0x80000000, regs);
78     if (!(regs[0] >= 0x80000001))
79 	return (0);
80 
81     /* Check for long mode. */
82     do_cpuid(0x80000001, regs);
83     return (regs[3] & AMDID_LM);
84 }
85 
86 /*
87  * Load the information expected by an amd64 kernel.
88  *
89  * - The 'boothowto' argument is constructed
90  * - The 'bootdev' argument is constructed
91  * - The 'bootinfo' struct is constructed, and copied into the kernel space.
92  * - The kernel environment is copied into kernel space.
93  * - Module metadata are formatted and placed in kernel space.
94  */
95 int
96 bi_load64(char *args, vm_offset_t *modulep,
97     vm_offset_t *kernendp, int add_smap)
98 {
99     struct preloaded_file	*xp, *kfp;
100     struct i386_devdesc		*rootdev;
101     struct file_metadata	*md;
102     uint64_t			kernend;
103     uint64_t			envp;
104     uint64_t			module;
105     uint64_t			addr;
106     vm_offset_t			size;
107     char			*rootdevname;
108     int				howto;
109 
110     if (!bi_checkcpu()) {
111 	printf("CPU doesn't support long mode\n");
112 	return (EINVAL);
113     }
114 
115     howto = bi_getboothowto(args);
116 
117     /*
118      * Allow the environment variable 'rootdev' to override the supplied device
119      * This should perhaps go to MI code and/or have $rootdev tested/set by
120      * MI code before launching the kernel.
121      */
122     rootdevname = getenv("rootdev");
123     i386_getdev((void **)(&rootdev), rootdevname, NULL);
124     if (rootdev == NULL) {		/* bad $rootdev/$currdev */
125 	printf("can't determine root device\n");
126 	return(EINVAL);
127     }
128 
129     /* Try reading the /etc/fstab file to select the root device */
130     getrootmount(devformat(&rootdev->dd));
131 
132     addr = 0;
133     /* find the last module in the chain */
134     for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
135         if (addr < (xp->f_addr + xp->f_size))
136             addr = xp->f_addr + xp->f_size;
137     }
138     /* pad to a page boundary */
139     addr = roundup(addr, PAGE_SIZE);
140 
141     addr = build_font_module(addr);
142 
143     /* place the metadata before anything */
144     module = *modulep = addr;
145 
146     kfp = file_findfile(NULL, "elf kernel");
147     if (kfp == NULL)
148       kfp = file_findfile(NULL, "elf64 kernel");
149     if (kfp == NULL)
150 	panic("can't find kernel file");
151     kernend = 0;	/* fill it in later */
152     file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof howto, &howto);
153     file_addmetadata(kfp, MODINFOMD_ENVP, sizeof envp, &envp);
154     file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof kernend, &kernend);
155     file_addmetadata(kfp, MODINFOMD_MODULEP, sizeof module, &module);
156     if (add_smap != 0)
157         bios_addsmapdata(kfp);
158 #ifdef LOADER_GELI_SUPPORT
159     geli_export_key_metadata(kfp);
160 #endif
161     bi_load_vbe_data(kfp);
162 
163     size = md_copymodules(0, true);
164 
165     /* copy our environment */
166     envp = roundup(addr + size, PAGE_SIZE);
167     addr = md_copyenv(envp);
168 
169     /* set kernend */
170     kernend = roundup(addr, PAGE_SIZE);
171     *kernendp = kernend;
172 
173     /* patch MODINFOMD_KERNEND */
174     md = file_findmetadata(kfp, MODINFOMD_KERNEND);
175     bcopy(&kernend, md->md_data, sizeof kernend);
176 
177     /* patch MODINFOMD_ENVP */
178     md = file_findmetadata(kfp, MODINFOMD_ENVP);
179     bcopy(&envp, md->md_data, sizeof envp);
180 
181     /* copy module list and metadata */
182     (void)md_copymodules(*modulep, true);
183 
184     return(0);
185 }
186