xref: /freebsd/stand/i386/libi386/bootinfo64.c (revision 3e15b01d6914c927e37d1699645783acf286655c)
1ca987d46SWarner Losh /*-
2ca987d46SWarner Losh  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3ca987d46SWarner Losh  * All rights reserved.
4ca987d46SWarner Losh  *
5ca987d46SWarner Losh  * Redistribution and use in source and binary forms, with or without
6ca987d46SWarner Losh  * modification, are permitted provided that the following conditions
7ca987d46SWarner Losh  * are met:
8ca987d46SWarner Losh  * 1. Redistributions of source code must retain the above copyright
9ca987d46SWarner Losh  *    notice, this list of conditions and the following disclaimer.
10ca987d46SWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
11ca987d46SWarner Losh  *    notice, this list of conditions and the following disclaimer in the
12ca987d46SWarner Losh  *    documentation and/or other materials provided with the distribution.
13ca987d46SWarner Losh  *
14ca987d46SWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15ca987d46SWarner Losh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16ca987d46SWarner Losh  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17ca987d46SWarner Losh  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18ca987d46SWarner Losh  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19ca987d46SWarner Losh  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20ca987d46SWarner Losh  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21ca987d46SWarner Losh  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22ca987d46SWarner Losh  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23ca987d46SWarner Losh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24ca987d46SWarner Losh  * SUCH DAMAGE.
25ca987d46SWarner Losh  */
26ca987d46SWarner Losh 
27ca987d46SWarner Losh #include <stand.h>
28ca987d46SWarner Losh #include <sys/param.h>
29ca987d46SWarner Losh #include <sys/reboot.h>
30ca987d46SWarner Losh #include <sys/linker.h>
31ca987d46SWarner Losh #include <machine/bootinfo.h>
32ca987d46SWarner Losh #include <machine/cpufunc.h>
33ca987d46SWarner Losh #include <machine/metadata.h>
34ca987d46SWarner Losh #include <machine/psl.h>
35ca987d46SWarner Losh #include <machine/specialreg.h>
36ca987d46SWarner Losh #include "bootstrap.h"
37bca9c87bSWarner Losh #include "modinfo.h"
38ca987d46SWarner Losh #include "libi386.h"
39ca987d46SWarner Losh #include "btxv86.h"
40ca987d46SWarner Losh 
41ca987d46SWarner Losh #ifdef LOADER_GELI_SUPPORT
42ca987d46SWarner Losh #include "geliboot.h"
43ca987d46SWarner Losh #endif
44ca987d46SWarner Losh 
45ca987d46SWarner Losh /*
46ca987d46SWarner Losh  * Check to see if this CPU supports long mode.
47ca987d46SWarner Losh  */
48ca987d46SWarner Losh static int
bi_checkcpu(void)49ca987d46SWarner Losh bi_checkcpu(void)
50ca987d46SWarner Losh {
51ca987d46SWarner Losh     char *cpu_vendor;
52ca987d46SWarner Losh     int vendor[3];
53ca987d46SWarner Losh     int eflags;
54ca987d46SWarner Losh     unsigned int regs[4];
55ca987d46SWarner Losh 
56ca987d46SWarner Losh     /* Check for presence of "cpuid". */
57ca987d46SWarner Losh     eflags = read_eflags();
58ca987d46SWarner Losh     write_eflags(eflags ^ PSL_ID);
59ca987d46SWarner Losh     if (!((eflags ^ read_eflags()) & PSL_ID))
60ca987d46SWarner Losh 	return (0);
61ca987d46SWarner Losh 
62ca987d46SWarner Losh     /* Fetch the vendor string. */
63ca987d46SWarner Losh     do_cpuid(0, regs);
64ca987d46SWarner Losh     vendor[0] = regs[1];
65ca987d46SWarner Losh     vendor[1] = regs[3];
66ca987d46SWarner Losh     vendor[2] = regs[2];
67ca987d46SWarner Losh     cpu_vendor = (char *)vendor;
68ca987d46SWarner Losh 
69ca987d46SWarner Losh     /* Check for vendors that support AMD features. */
70ca987d46SWarner Losh     if (strncmp(cpu_vendor, INTEL_VENDOR_ID, 12) != 0 &&
71ca987d46SWarner Losh 	strncmp(cpu_vendor, AMD_VENDOR_ID, 12) != 0 &&
722ee49facSKonstantin Belousov 	strncmp(cpu_vendor, HYGON_VENDOR_ID, 12) != 0 &&
73ca987d46SWarner Losh 	strncmp(cpu_vendor, CENTAUR_VENDOR_ID, 12) != 0)
74ca987d46SWarner Losh 	return (0);
75ca987d46SWarner Losh 
76ca987d46SWarner Losh     /* Has to support AMD features. */
77ca987d46SWarner Losh     do_cpuid(0x80000000, regs);
78ca987d46SWarner Losh     if (!(regs[0] >= 0x80000001))
79ca987d46SWarner Losh 	return (0);
80ca987d46SWarner Losh 
81ca987d46SWarner Losh     /* Check for long mode. */
82ca987d46SWarner Losh     do_cpuid(0x80000001, regs);
83ca987d46SWarner Losh     return (regs[3] & AMDID_LM);
84ca987d46SWarner Losh }
85ca987d46SWarner Losh 
86ca987d46SWarner Losh /*
87ca987d46SWarner Losh  * Load the information expected by an amd64 kernel.
88ca987d46SWarner Losh  *
89ca987d46SWarner Losh  * - The 'boothowto' argument is constructed
90ca987d46SWarner Losh  * - The 'bootdev' argument is constructed
91ca987d46SWarner Losh  * - The 'bootinfo' struct is constructed, and copied into the kernel space.
92ca987d46SWarner Losh  * - The kernel environment is copied into kernel space.
93ca987d46SWarner Losh  * - Module metadata are formatted and placed in kernel space.
94ca987d46SWarner Losh  */
95ca987d46SWarner Losh int
bi_load64(char * args,vm_offset_t * modulep,vm_offset_t * kernendp,int add_smap)9645ecda8eSRoger Pau Monné bi_load64(char *args, vm_offset_t *modulep,
97ca987d46SWarner Losh     vm_offset_t *kernendp, int add_smap)
98ca987d46SWarner Losh {
99ca987d46SWarner Losh     struct preloaded_file	*xp, *kfp;
100ca987d46SWarner Losh     struct i386_devdesc		*rootdev;
101ca987d46SWarner Losh     struct file_metadata	*md;
10256e53cb8SWarner Losh     uint64_t			kernend;
10356e53cb8SWarner Losh     uint64_t			envp;
10456e53cb8SWarner Losh     uint64_t			module;
10545ecda8eSRoger Pau Monné     uint64_t			addr;
106ca987d46SWarner Losh     vm_offset_t			size;
107ca987d46SWarner Losh     char			*rootdevname;
108ca987d46SWarner Losh     int				howto;
109ca987d46SWarner Losh 
110ca987d46SWarner Losh     if (!bi_checkcpu()) {
111ca987d46SWarner Losh 	printf("CPU doesn't support long mode\n");
112ca987d46SWarner Losh 	return (EINVAL);
113ca987d46SWarner Losh     }
114ca987d46SWarner Losh 
115ca987d46SWarner Losh     howto = bi_getboothowto(args);
116ca987d46SWarner Losh 
117ca987d46SWarner Losh     /*
118ca987d46SWarner Losh      * Allow the environment variable 'rootdev' to override the supplied device
119ca987d46SWarner Losh      * This should perhaps go to MI code and/or have $rootdev tested/set by
120ca987d46SWarner Losh      * MI code before launching the kernel.
121ca987d46SWarner Losh      */
122ca987d46SWarner Losh     rootdevname = getenv("rootdev");
123ca987d46SWarner Losh     i386_getdev((void **)(&rootdev), rootdevname, NULL);
124ca987d46SWarner Losh     if (rootdev == NULL) {		/* bad $rootdev/$currdev */
125ca987d46SWarner Losh 	printf("can't determine root device\n");
126ca987d46SWarner Losh 	return(EINVAL);
127ca987d46SWarner Losh     }
128ca987d46SWarner Losh 
129ca987d46SWarner Losh     /* Try reading the /etc/fstab file to select the root device */
13036232229SWarner Losh     getrootmount(devformat(&rootdev->dd));
131ca987d46SWarner Losh 
13245ecda8eSRoger Pau Monné     addr = 0;
133ca987d46SWarner Losh     /* find the last module in the chain */
134ca987d46SWarner Losh     for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
135ca987d46SWarner Losh         if (addr < (xp->f_addr + xp->f_size))
136ca987d46SWarner Losh             addr = xp->f_addr + xp->f_size;
137ca987d46SWarner Losh     }
138ca987d46SWarner Losh     /* pad to a page boundary */
139ca987d46SWarner Losh     addr = roundup(addr, PAGE_SIZE);
140ca987d46SWarner Losh 
1413630506bSToomas Soome     addr = build_font_module(addr);
1423630506bSToomas Soome 
143ca987d46SWarner Losh     /* place the metadata before anything */
144ca987d46SWarner Losh     module = *modulep = addr;
145ca987d46SWarner Losh 
146ca987d46SWarner Losh     kfp = file_findfile(NULL, "elf kernel");
147ca987d46SWarner Losh     if (kfp == NULL)
148ca987d46SWarner Losh       kfp = file_findfile(NULL, "elf64 kernel");
149ca987d46SWarner Losh     if (kfp == NULL)
150ca987d46SWarner Losh 	panic("can't find kernel file");
151ca987d46SWarner Losh     kernend = 0;	/* fill it in later */
152ca987d46SWarner Losh     file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof howto, &howto);
153ca987d46SWarner Losh     file_addmetadata(kfp, MODINFOMD_ENVP, sizeof envp, &envp);
154ca987d46SWarner Losh     file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof kernend, &kernend);
155ca987d46SWarner Losh     file_addmetadata(kfp, MODINFOMD_MODULEP, sizeof module, &module);
156ca987d46SWarner Losh     if (add_smap != 0)
157ca987d46SWarner Losh         bios_addsmapdata(kfp);
158ca987d46SWarner Losh #ifdef LOADER_GELI_SUPPORT
159c1418270SIan Lepore     geli_export_key_metadata(kfp);
160ca987d46SWarner Losh #endif
1613630506bSToomas Soome     bi_load_vbe_data(kfp);
162ca987d46SWarner Losh 
1635d1531d9SWarner Losh     size = md_copymodules(0, true);
164ca987d46SWarner Losh 
165ca987d46SWarner Losh     /* copy our environment */
166ca987d46SWarner Losh     envp = roundup(addr + size, PAGE_SIZE);
167*fc352701SWarner Losh     addr = md_copyenv(envp);
168ca987d46SWarner Losh 
169ca987d46SWarner Losh     /* set kernend */
170ca987d46SWarner Losh     kernend = roundup(addr, PAGE_SIZE);
171ca987d46SWarner Losh     *kernendp = kernend;
172ca987d46SWarner Losh 
173ca987d46SWarner Losh     /* patch MODINFOMD_KERNEND */
174ca987d46SWarner Losh     md = file_findmetadata(kfp, MODINFOMD_KERNEND);
175ca987d46SWarner Losh     bcopy(&kernend, md->md_data, sizeof kernend);
176ca987d46SWarner Losh 
177ca987d46SWarner Losh     /* patch MODINFOMD_ENVP */
178ca987d46SWarner Losh     md = file_findmetadata(kfp, MODINFOMD_ENVP);
179ca987d46SWarner Losh     bcopy(&envp, md->md_data, sizeof envp);
180ca987d46SWarner Losh 
181ca987d46SWarner Losh     /* copy module list and metadata */
1825d1531d9SWarner Losh     (void)md_copymodules(*modulep, true);
183ca987d46SWarner Losh 
184ca987d46SWarner Losh     return(0);
185ca987d46SWarner Losh }
186