2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2015 Neel Natu <neel@freebsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
29 #include <sys/param.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/types.h>
36 #include <machine/vmm.h>
51 #define BOOTROM_SIZE (16 * 1024 * 1024) /* 16 MB */
54 * ROM region is 16 MB at the top of 4GB ("low") memory.
56 * The size is limited so it doesn't encroach into reserved MMIO space (e.g.,
59 * It is allocated in page-multiple blocks on a first-come first-serve basis,
60 * from high to low, during initialization, and does not change at runtime.
62 static char *romptr; /* Pointer to userspace-mapped bootrom region. */
63 static vm_paddr_t gpa_base; /* GPA of low end of region. */
64 static vm_paddr_t gpa_allocbot; /* Low GPA of free region. */
65 static vm_paddr_t gpa_alloctop; /* High GPA, minus 1, of free region. */
68 init_bootrom(struct vmctx *ctx)
70 romptr = vm_create_devmem(ctx, VM_BOOTROM, "bootrom", BOOTROM_SIZE);
71 if (romptr == MAP_FAILED)
72 err(4, "%s: vm_create_devmem", __func__);
73 gpa_base = (1ULL << 32) - BOOTROM_SIZE;
74 gpa_allocbot = gpa_base;
75 gpa_alloctop = (1ULL << 32) - 1;
79 bootrom_alloc(struct vmctx *ctx, size_t len, int prot, int flags,
80 char **region_out, uint64_t *gpa_out)
82 static const int bootrom_valid_flags = BOOTROM_ALLOC_TOP;
87 if (flags & ~bootrom_valid_flags) {
88 warnx("%s: Invalid flags: %x", __func__,
89 flags & ~bootrom_valid_flags);
92 if (prot & ~_PROT_ALL) {
93 warnx("%s: Invalid protection: %x", __func__,
98 if (len == 0 || len > BOOTROM_SIZE) {
99 warnx("ROM size %zu is invalid", len);
102 if (len & PAGE_MASK) {
103 warnx("ROM size %zu is not a multiple of the page size",
108 if (flags & BOOTROM_ALLOC_TOP) {
109 gpa = (gpa_alloctop - len) + 1;
110 if (gpa < gpa_allocbot) {
111 warnx("No room for %zu ROM in bootrom region", len);
116 if (gpa > (gpa_alloctop - len) + 1) {
117 warnx("No room for %zu ROM in bootrom region", len);
122 segoff = gpa - gpa_base;
123 if (vm_mmap_memseg(ctx, gpa, VM_BOOTROM, segoff, len, prot) != 0) {
125 warn("%s: vm_mmap_mapseg", __func__);
129 if (flags & BOOTROM_ALLOC_TOP)
130 gpa_alloctop = gpa - 1;
132 gpa_allocbot = gpa + len;
134 *region_out = romptr + segoff;
141 bootrom_loadrom(struct vmctx *ctx, const char *romfile)
149 fd = open(romfile, O_RDONLY);
151 EPRINTLN("Error opening bootrom \"%s\": %s",
152 romfile, strerror(errno));
156 if (fstat(fd, &sbuf) < 0) {
157 EPRINTLN("Could not fstat bootrom file \"%s\": %s",
158 romfile, strerror(errno));
162 /* Map the bootrom into the guest address space */
163 if (bootrom_alloc(ctx, sbuf.st_size, PROT_READ | PROT_EXEC,
164 BOOTROM_ALLOC_TOP, &ptr, NULL) != 0)
167 /* Read 'romfile' into the guest address space */
168 for (i = 0; i < sbuf.st_size / PAGE_SIZE; i++) {
169 rlen = read(fd, ptr + i * PAGE_SIZE, PAGE_SIZE);
170 if (rlen != PAGE_SIZE) {
171 EPRINTLN("Incomplete read of page %d of bootrom "
172 "file %s: %ld bytes", i, romfile, rlen);