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 "modinfo.h"
41 #include "libuserboot.h"
42 
43 /*
44  * Check to see if this CPU supports long mode.
45  */
46 static int
47 bi_checkcpu(void)
48 {
49 #if 0
50     char *cpu_vendor;
51     int vendor[3];
52     int eflags, regs[4];
53 
54     /* Check for presence of "cpuid". */
55     eflags = read_eflags();
56     write_eflags(eflags ^ PSL_ID);
57     if (!((eflags ^ read_eflags()) & PSL_ID))
58 	return (0);
59 
60     /* Fetch the vendor string. */
61     do_cpuid(0, regs);
62     vendor[0] = regs[1];
63     vendor[1] = regs[3];
64     vendor[2] = regs[2];
65     cpu_vendor = (char *)vendor;
66 
67     /* Check for vendors that support AMD features. */
68     if (strncmp(cpu_vendor, INTEL_VENDOR_ID, 12) != 0 &&
69 	strncmp(cpu_vendor, AMD_VENDOR_ID, 12) != 0 &&
70 	strncmp(cpu_vendor, CENTAUR_VENDOR_ID, 12) != 0)
71 	return (0);
72 
73     /* Has to support AMD features. */
74     do_cpuid(0x80000000, regs);
75     if (!(regs[0] >= 0x80000001))
76 	return (0);
77 
78     /* Check for long mode. */
79     do_cpuid(0x80000001, regs);
80     return (regs[3] & AMDID_LM);
81 #else
82 	return (1);
83 #endif
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, vm_offset_t *kernendp)
97 {
98     struct preloaded_file	*xp, *kfp;
99     struct devdesc		*rootdev;
100     struct file_metadata	*md;
101     vm_offset_t			addr;
102     uint64_t			kernend;
103     uint64_t			envp;
104     vm_offset_t			size;
105     char			*rootdevname;
106     int				howto;
107 
108     if (!bi_checkcpu()) {
109 	printf("CPU doesn't support long mode\n");
110 	return (EINVAL);
111     }
112 
113     howto = bi_getboothowto(args);
114 
115     /*
116      * Allow the environment variable 'rootdev' to override the supplied device
117      * This should perhaps go to MI code and/or have $rootdev tested/set by
118      * MI code before launching the kernel.
119      */
120     rootdevname = getenv("rootdev");
121     userboot_getdev((void **)(&rootdev), rootdevname, NULL);
122     if (rootdev == NULL) {		/* bad $rootdev/$currdev */
123 	printf("can't determine root device\n");
124 	return(EINVAL);
125     }
126 
127     /* Try reading the /etc/fstab file to select the root device */
128     getrootmount(devformat(rootdev));
129 
130     /* find the last module in the chain */
131     addr = 0;
132     for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
133 	if (addr < (xp->f_addr + xp->f_size))
134 	    addr = xp->f_addr + xp->f_size;
135     }
136     /* pad to a page boundary */
137     addr = roundup(addr, PAGE_SIZE);
138 
139     /* copy our environment */
140     envp = addr;
141     addr = md_copyenv(addr);
142 
143     /* pad to a page boundary */
144     addr = roundup(addr, PAGE_SIZE);
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     bios_addsmapdata(kfp);
156 
157     /* Figure out the size and location of the metadata */
158     *modulep = addr;
159     size = md_copymodules(0, true);
160     kernend = roundup(addr + size, PAGE_SIZE);
161     *kernendp = kernend;
162 
163     /* patch MODINFOMD_KERNEND */
164     md = file_findmetadata(kfp, MODINFOMD_KERNEND);
165     bcopy(&kernend, md->md_data, sizeof kernend);
166 
167     /* copy module list and metadata */
168     (void)md_copymodules(addr, true);
169 
170     return(0);
171 }
172