15325 bhyve upstream sync 2023 January
[illumos-gate.git] / usr / src / cmd / bhyve / bootrom.c
blobfa20fa8ac9420067cd7b9f03099e46eb50af6236
1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2015 Neel Natu <neel@freebsd.org>
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
29 #include <sys/param.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/types.h>
33 #include <sys/mman.h>
34 #include <sys/stat.h>
36 #include <machine/vmm.h>
38 #include <err.h>
39 #include <errno.h>
40 #include <fcntl.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45 #include <stdbool.h>
47 #include <vmmapi.h>
49 #include "bhyverun.h"
50 #include "bootrom.h"
51 #include "debug.h"
52 #include "mem.h"
54 #define BOOTROM_SIZE (16 * 1024 * 1024) /* 16 MB */
57 * ROM region is 16 MB at the top of 4GB ("low") memory.
59 * The size is limited so it doesn't encroach into reserved MMIO space (e.g.,
60 * APIC, HPET, MSI).
62 * It is allocated in page-multiple blocks on a first-come first-serve basis,
63 * from high to low, during initialization, and does not change at runtime.
65 static char *romptr; /* Pointer to userspace-mapped bootrom region. */
66 static vm_paddr_t gpa_base; /* GPA of low end of region. */
67 static vm_paddr_t gpa_allocbot; /* Low GPA of free region. */
68 static vm_paddr_t gpa_alloctop; /* High GPA, minus 1, of free region. */
70 #define CFI_BCS_WRITE_BYTE 0x10
71 #define CFI_BCS_CLEAR_STATUS 0x50
72 #define CFI_BCS_READ_STATUS 0x70
73 #define CFI_BCS_READ_ARRAY 0xff
75 static struct bootrom_var_state {
76 uint8_t *mmap;
77 uint64_t gpa;
78 off_t size;
79 uint8_t cmd;
80 } var = { NULL, 0, 0, CFI_BCS_READ_ARRAY };
83 * Emulate just those CFI basic commands that will convince EDK II
84 * that the Firmware Volume area is writable and persistent.
86 static int
87 bootrom_var_mem_handler(struct vmctx *ctx __unused, int vcpu __unused, int dir,
88 uint64_t addr, int size, uint64_t *val, void *arg1 __unused,
89 long arg2 __unused)
91 off_t offset;
93 offset = addr - var.gpa;
94 if (offset + size > var.size || offset < 0 || offset + size <= offset)
95 return (EINVAL);
97 if (dir == MEM_F_WRITE) {
98 switch (var.cmd) {
99 case CFI_BCS_WRITE_BYTE:
100 memcpy(var.mmap + offset, val, size);
101 var.cmd = CFI_BCS_READ_ARRAY;
102 break;
103 default:
104 var.cmd = *(uint8_t *)val;
106 } else {
107 switch (var.cmd) {
108 case CFI_BCS_CLEAR_STATUS:
109 case CFI_BCS_READ_STATUS:
110 memset(val, 0, size);
111 var.cmd = CFI_BCS_READ_ARRAY;
112 break;
113 default:
114 memcpy(val, var.mmap + offset, size);
115 break;
118 return (0);
121 void
122 init_bootrom(struct vmctx *ctx)
124 romptr = vm_create_devmem(ctx, VM_BOOTROM, "bootrom", BOOTROM_SIZE);
125 if (romptr == MAP_FAILED)
126 err(4, "%s: vm_create_devmem", __func__);
127 gpa_base = (1ULL << 32) - BOOTROM_SIZE;
128 gpa_allocbot = gpa_base;
129 gpa_alloctop = (1ULL << 32) - 1;
133 bootrom_alloc(struct vmctx *ctx, size_t len, int prot, int flags,
134 char **region_out, uint64_t *gpa_out)
136 static const int bootrom_valid_flags = BOOTROM_ALLOC_TOP;
138 vm_paddr_t gpa;
139 vm_ooffset_t segoff;
141 if (flags & ~bootrom_valid_flags) {
142 warnx("%s: Invalid flags: %x", __func__,
143 flags & ~bootrom_valid_flags);
144 return (EINVAL);
146 if (prot & ~_PROT_ALL) {
147 warnx("%s: Invalid protection: %x", __func__,
148 prot & ~_PROT_ALL);
149 return (EINVAL);
152 if (len == 0 || len > BOOTROM_SIZE) {
153 warnx("ROM size %zu is invalid", len);
154 return (EINVAL);
156 if (len & PAGE_MASK) {
157 warnx("ROM size %zu is not a multiple of the page size",
158 len);
159 return (EINVAL);
162 if (flags & BOOTROM_ALLOC_TOP) {
163 gpa = (gpa_alloctop - len) + 1;
164 if (gpa < gpa_allocbot) {
165 warnx("No room for %zu ROM in bootrom region", len);
166 return (ENOMEM);
168 } else {
169 gpa = gpa_allocbot;
170 if (gpa > (gpa_alloctop - len) + 1) {
171 warnx("No room for %zu ROM in bootrom region", len);
172 return (ENOMEM);
176 segoff = gpa - gpa_base;
177 if (vm_mmap_memseg(ctx, gpa, VM_BOOTROM, segoff, len, prot) != 0) {
178 int serrno = errno;
179 warn("%s: vm_mmap_mapseg", __func__);
180 return (serrno);
183 if (flags & BOOTROM_ALLOC_TOP)
184 gpa_alloctop = gpa - 1;
185 else
186 gpa_allocbot = gpa + len;
188 *region_out = romptr + segoff;
189 if (gpa_out != NULL)
190 *gpa_out = gpa;
191 return (0);
195 bootrom_loadrom(struct vmctx *ctx, const nvlist_t *nvl)
197 struct stat sbuf;
198 ssize_t rlen;
199 off_t rom_size, var_size, total_size;
200 char *ptr, *romfile;
201 int fd, varfd, i, rv;
202 const char *bootrom, *varfile;
204 rv = -1;
205 varfd = -1;
207 bootrom = get_config_value_node(nvl, "bootrom");
208 if (bootrom == NULL) {
209 return (-1);
213 * get_config_value_node may use a thread local buffer to return
214 * variables. So, when we query the second variable, the first variable
215 * might get overwritten. For that reason, the bootrom should be
216 * duplicated.
218 romfile = strdup(bootrom);
219 if (romfile == NULL) {
220 return (-1);
223 fd = open(romfile, O_RDONLY);
224 if (fd < 0) {
225 EPRINTLN("Error opening bootrom \"%s\": %s",
226 romfile, strerror(errno));
227 goto done;
230 if (fstat(fd, &sbuf) < 0) {
231 EPRINTLN("Could not fstat bootrom file \"%s\": %s", romfile,
232 strerror(errno));
233 goto done;
236 rom_size = sbuf.st_size;
238 varfile = get_config_value_node(nvl, "bootvars");
239 var_size = 0;
240 if (varfile != NULL) {
241 varfd = open(varfile, O_RDWR);
242 if (varfd < 0) {
243 fprintf(stderr, "Error opening bootrom variable file "
244 "\"%s\": %s\n", varfile, strerror(errno));
245 goto done;
248 if (fstat(varfd, &sbuf) < 0) {
249 fprintf(stderr,
250 "Could not fstat bootrom variable file \"%s\": %s\n",
251 varfile, strerror(errno));
252 goto done;
255 var_size = sbuf.st_size;
258 if (var_size > BOOTROM_SIZE ||
259 (var_size != 0 && var_size < PAGE_SIZE)) {
260 fprintf(stderr, "Invalid bootrom variable size %ld\n",
261 var_size);
262 goto done;
265 total_size = rom_size + var_size;
267 if (total_size > BOOTROM_SIZE) {
268 fprintf(stderr, "Invalid bootrom and variable aggregate size "
269 "%ld\n", total_size);
270 goto done;
273 /* Map the bootrom into the guest address space */
274 if (bootrom_alloc(ctx, rom_size, PROT_READ | PROT_EXEC,
275 BOOTROM_ALLOC_TOP, &ptr, NULL) != 0) {
276 goto done;
279 /* Read 'romfile' into the guest address space */
280 for (i = 0; i < rom_size / PAGE_SIZE; i++) {
281 rlen = read(fd, ptr + i * PAGE_SIZE, PAGE_SIZE);
282 if (rlen != PAGE_SIZE) {
283 EPRINTLN("Incomplete read of page %d of bootrom "
284 "file %s: %ld bytes", i, romfile, rlen);
285 goto done;
289 if (varfd >= 0) {
290 #ifdef __FreeBSD__
291 var.mmap = mmap(NULL, var_size, PROT_READ | PROT_WRITE,
292 MAP_SHARED, varfd, 0);
293 #else
294 var.mmap = (uint8_t *)mmap(NULL, var_size,
295 PROT_READ | PROT_WRITE, MAP_SHARED, varfd, 0);
296 #endif
297 if (var.mmap == MAP_FAILED)
298 goto done;
299 var.size = var_size;
300 var.gpa = (gpa_alloctop - var_size) + 1;
301 gpa_alloctop = var.gpa - 1;
302 rv = register_mem(&(struct mem_range){
303 .name = "bootrom variable",
304 .flags = MEM_F_RW,
305 .handler = bootrom_var_mem_handler,
306 .base = var.gpa,
307 .size = var.size,
309 if (rv != 0)
310 goto done;
313 rv = 0;
314 done:
315 if (varfd >= 0)
316 close(varfd);
317 if (fd >= 0)
318 close(fd);
319 free(romfile);
320 return (rv);