2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2021 Beckhoff Automation GmbH & Co. KG
5 * Author: Corvin Köhne <c.koehne@beckhoff.com>
9 #include <sys/endian.h>
10 #include <sys/queue.h>
13 #include <machine/vmm.h>
23 #include "acpi_device.h"
27 #include "qemu_fwcfg.h"
29 #define QEMU_FWCFG_ACPI_DEVICE_NAME "FWCF"
30 #define QEMU_FWCFG_ACPI_HARDWARE_ID "QEMU0002"
32 #define QEMU_FWCFG_SELECTOR_PORT_NUMBER 0x510
33 #define QEMU_FWCFG_SELECTOR_PORT_SIZE 1
34 #define QEMU_FWCFG_SELECTOR_PORT_FLAGS IOPORT_F_INOUT
35 #define QEMU_FWCFG_DATA_PORT_NUMBER 0x511
36 #define QEMU_FWCFG_DATA_PORT_SIZE 1
37 #define QEMU_FWCFG_DATA_PORT_FLAGS \
38 IOPORT_F_INOUT /* QEMU v2.4+ ignores writes */
40 #define QEMU_FWCFG_ARCHITECTURE_MASK 0x0001
41 #define QEMU_FWCFG_INDEX_MASK 0x3FFF
43 #define QEMU_FWCFG_SELECT_READ 0
44 #define QEMU_FWCFG_SELECT_WRITE 1
46 #define QEMU_FWCFG_ARCHITECTURE_GENERIC 0
47 #define QEMU_FWCFG_ARCHITECTURE_SPECIFIC 1
49 #define QEMU_FWCFG_INDEX_SIGNATURE 0x00
50 #define QEMU_FWCFG_INDEX_ID 0x01
51 #define QEMU_FWCFG_INDEX_NB_CPUS 0x05
52 #define QEMU_FWCFG_INDEX_MAX_CPUS 0x0F
53 #define QEMU_FWCFG_INDEX_FILE_DIR 0x19
55 #define QEMU_FWCFG_FIRST_FILE_INDEX 0x20
57 #define QEMU_FWCFG_MIN_FILES 10
61 union qemu_fwcfg_selector {
64 uint16_t writeable : 1;
65 uint16_t architecture : 1;
70 struct qemu_fwcfg_signature {
74 struct qemu_fwcfg_id {
75 uint32_t interface : 1; /* always set */
77 uint32_t reserved : 30;
80 struct qemu_fwcfg_file {
84 uint8_t name[QEMU_FWCFG_MAX_NAME];
87 struct qemu_fwcfg_directory {
89 struct qemu_fwcfg_file files[0];
94 struct qemu_fwcfg_softc {
95 struct acpi_device *acpi_dev;
98 union qemu_fwcfg_selector selector;
99 struct qemu_fwcfg_item items[QEMU_FWCFG_MAX_ARCHS]
100 [QEMU_FWCFG_MAX_ENTRIES];
101 struct qemu_fwcfg_directory *directory;
104 static struct qemu_fwcfg_softc fwcfg_sc;
106 struct qemu_fwcfg_user_file {
107 STAILQ_ENTRY(qemu_fwcfg_user_file) chain;
108 uint8_t name[QEMU_FWCFG_MAX_NAME];
112 static STAILQ_HEAD(qemu_fwcfg_user_file_list,
113 qemu_fwcfg_user_file) user_files = STAILQ_HEAD_INITIALIZER(user_files);
116 qemu_fwcfg_selector_port_handler(struct vmctx *const ctx __unused, const int in,
117 const int port __unused, const int bytes, uint32_t *const eax,
118 void *const arg __unused)
120 if (bytes != sizeof(uint16_t)) {
121 warnx("%s: invalid size (%d) of IO port access", __func__,
127 *eax = htole16(fwcfg_sc.selector.bits);
131 fwcfg_sc.data_offset = 0;
132 fwcfg_sc.selector.bits = le16toh(*eax);
138 qemu_fwcfg_data_port_handler(struct vmctx *const ctx __unused, const int in,
139 const int port __unused, const int bytes, uint32_t *const eax,
140 void *const arg __unused)
142 if (bytes != sizeof(uint8_t)) {
143 warnx("%s: invalid size (%d) of IO port access", __func__,
149 warnx("%s: Writes to qemu fwcfg data port aren't allowed",
155 struct qemu_fwcfg_item *const item =
156 &fwcfg_sc.items[fwcfg_sc.selector.architecture]
157 [fwcfg_sc.selector.index];
158 if (item->data == NULL) {
160 "%s: qemu fwcfg item doesn't exist (architecture %s index 0x%x)",
162 fwcfg_sc.selector.architecture ? "specific" : "generic",
163 fwcfg_sc.selector.index);
166 } else if (fwcfg_sc.data_offset >= item->size) {
168 "%s: qemu fwcfg item read exceeds size (architecture %s index 0x%x size 0x%x offset 0x%x)",
170 fwcfg_sc.selector.architecture ? "specific" : "generic",
171 fwcfg_sc.selector.index, item->size, fwcfg_sc.data_offset);
176 /* return item data */
177 *eax = item->data[fwcfg_sc.data_offset];
178 fwcfg_sc.data_offset++;
184 qemu_fwcfg_add_item(const uint16_t architecture, const uint16_t index,
185 const uint32_t size, void *const data)
187 /* truncate architecture and index to their desired size */
188 const uint16_t arch = architecture & QEMU_FWCFG_ARCHITECTURE_MASK;
189 const uint16_t idx = index & QEMU_FWCFG_INDEX_MASK;
191 /* get pointer to item specified by selector */
192 struct qemu_fwcfg_item *const fwcfg_item = &fwcfg_sc.items[arch][idx];
194 /* check if item is already used */
195 if (fwcfg_item->data != NULL) {
196 warnx("%s: qemu fwcfg item exists (architecture %s index 0x%x)",
197 __func__, arch ? "specific" : "generic", idx);
201 /* save data of the item */
202 fwcfg_item->size = size;
203 fwcfg_item->data = data;
209 qemu_fwcfg_add_item_file_dir(void)
211 const size_t size = sizeof(struct qemu_fwcfg_directory) +
212 QEMU_FWCFG_MIN_FILES * sizeof(struct qemu_fwcfg_file);
213 struct qemu_fwcfg_directory *const fwcfg_directory = calloc(1, size);
214 if (fwcfg_directory == NULL) {
218 fwcfg_sc.directory = fwcfg_directory;
220 return (qemu_fwcfg_add_item(QEMU_FWCFG_ARCHITECTURE_GENERIC,
221 QEMU_FWCFG_INDEX_FILE_DIR, sizeof(struct qemu_fwcfg_directory),
222 (uint8_t *)fwcfg_sc.directory));
226 qemu_fwcfg_add_item_id(void)
228 struct qemu_fwcfg_id *const fwcfg_id = calloc(1,
229 sizeof(struct qemu_fwcfg_id));
230 if (fwcfg_id == NULL) {
234 fwcfg_id->interface = 1;
237 uint32_t *const le_fwcfg_id_ptr = (uint32_t *)fwcfg_id;
238 *le_fwcfg_id_ptr = htole32(*le_fwcfg_id_ptr);
240 return (qemu_fwcfg_add_item(QEMU_FWCFG_ARCHITECTURE_GENERIC,
241 QEMU_FWCFG_INDEX_ID, sizeof(struct qemu_fwcfg_id),
242 (uint8_t *)fwcfg_id));
246 qemu_fwcfg_add_item_max_cpus(void)
248 uint16_t *fwcfg_max_cpus = calloc(1, sizeof(uint16_t));
249 if (fwcfg_max_cpus == NULL) {
254 * We don't support cpu hotplug yet. For that reason, use guest_ncpus instead
257 *fwcfg_max_cpus = htole16(guest_ncpus);
259 return (qemu_fwcfg_add_item(QEMU_FWCFG_ARCHITECTURE_GENERIC,
260 QEMU_FWCFG_INDEX_MAX_CPUS, sizeof(uint16_t), fwcfg_max_cpus));
264 qemu_fwcfg_add_item_nb_cpus(void)
266 uint16_t *fwcfg_max_cpus = calloc(1, sizeof(uint16_t));
267 if (fwcfg_max_cpus == NULL) {
271 *fwcfg_max_cpus = htole16(guest_ncpus);
273 return (qemu_fwcfg_add_item(QEMU_FWCFG_ARCHITECTURE_GENERIC,
274 QEMU_FWCFG_INDEX_NB_CPUS, sizeof(uint16_t), fwcfg_max_cpus));
278 qemu_fwcfg_add_item_signature(void)
280 struct qemu_fwcfg_signature *const fwcfg_signature = calloc(1,
281 sizeof(struct qemu_fwcfg_signature));
282 if (fwcfg_signature == NULL) {
286 fwcfg_signature->signature[0] = 'Q';
287 fwcfg_signature->signature[1] = 'E';
288 fwcfg_signature->signature[2] = 'M';
289 fwcfg_signature->signature[3] = 'U';
291 return (qemu_fwcfg_add_item(QEMU_FWCFG_ARCHITECTURE_GENERIC,
292 QEMU_FWCFG_INDEX_SIGNATURE, sizeof(struct qemu_fwcfg_signature),
293 (uint8_t *)fwcfg_signature));
297 qemu_fwcfg_register_port(const char *const name, const int port, const int size,
298 const int flags, const inout_func_t handler)
300 struct inout_port iop;
302 bzero(&iop, sizeof(iop));
307 iop.handler = handler;
309 return (register_inout(&iop));
313 qemu_fwcfg_add_file(const char *name, const uint32_t size, void *const data)
315 if (strlen(name) >= QEMU_FWCFG_MAX_NAME)
319 * QEMU specifies count as big endian.
320 * Convert it to host endian to work with it.
322 const uint32_t count = be32toh(fwcfg_sc.directory->be_count) + 1;
324 /* add file to items list */
325 const uint32_t index = QEMU_FWCFG_FIRST_FILE_INDEX + count - 1;
326 const int error = qemu_fwcfg_add_item(QEMU_FWCFG_ARCHITECTURE_GENERIC,
333 * files should be sorted alphabetical, get index for new file
336 for (file_index = 0; file_index < count - 1; ++file_index) {
337 if (strcmp(name, fwcfg_sc.directory->files[file_index].name) <
342 if (count > QEMU_FWCFG_MIN_FILES) {
343 /* alloc new file directory */
344 const uint64_t new_size = sizeof(struct qemu_fwcfg_directory) +
345 count * sizeof(struct qemu_fwcfg_file);
346 struct qemu_fwcfg_directory *const new_directory = calloc(1,
348 if (new_directory == NULL) {
350 "%s: Unable to allocate a new qemu fwcfg files directory (count %d)",
355 /* copy files below file_index to new directory */
356 memcpy(new_directory->files, fwcfg_sc.directory->files,
357 file_index * sizeof(struct qemu_fwcfg_file));
359 /* copy files above file_index to directory */
360 memcpy(&new_directory->files[file_index + 1],
361 &fwcfg_sc.directory->files[file_index],
362 (count - file_index) * sizeof(struct qemu_fwcfg_file));
364 /* free old directory */
365 free(fwcfg_sc.directory);
367 /* set directory pointer to new directory */
368 fwcfg_sc.directory = new_directory;
370 /* adjust directory pointer */
371 fwcfg_sc.items[0][QEMU_FWCFG_INDEX_FILE_DIR].data =
372 (uint8_t *)fwcfg_sc.directory;
374 /* shift files behind file_index */
375 for (uint32_t i = QEMU_FWCFG_MIN_FILES - 1; i > file_index;
377 memcpy(&fwcfg_sc.directory->files[i],
378 &fwcfg_sc.directory->files[i - 1],
379 sizeof(struct qemu_fwcfg_file));
384 * QEMU specifies count, size and index as big endian.
385 * Save these values in big endian to simplify guest reads of these
388 fwcfg_sc.directory->be_count = htobe32(count);
389 fwcfg_sc.directory->files[file_index].be_size = htobe32(size);
390 fwcfg_sc.directory->files[file_index].be_selector = htobe16(index);
391 strcpy(fwcfg_sc.directory->files[file_index].name, name);
393 /* set new size for the fwcfg_file_directory */
394 fwcfg_sc.items[0][QEMU_FWCFG_INDEX_FILE_DIR].size =
395 sizeof(struct qemu_fwcfg_directory) +
396 count * sizeof(struct qemu_fwcfg_file);
402 qemu_fwcfg_add_user_files(void)
404 const struct qemu_fwcfg_user_file *fwcfg_file;
407 STAILQ_FOREACH(fwcfg_file, &user_files, chain) {
408 error = qemu_fwcfg_add_file(fwcfg_file->name, fwcfg_file->size,
417 static const struct acpi_device_emul qemu_fwcfg_acpi_device_emul = {
418 .name = QEMU_FWCFG_ACPI_DEVICE_NAME,
419 .hid = QEMU_FWCFG_ACPI_HARDWARE_ID,
423 qemu_fwcfg_init(struct vmctx *const ctx)
428 * Bhyve supports fwctl (bhyve) and fwcfg (qemu) as firmware interfaces.
429 * Both are using the same ports. So, it's not possible to provide both
430 * interfaces at the same time to the guest. Therefore, only create acpi
431 * tables and register io ports for fwcfg, if it's used.
433 if (strcmp(lpc_fwcfg(), "qemu") == 0) {
434 error = acpi_device_create(&fwcfg_sc.acpi_dev, &fwcfg_sc, ctx,
435 &qemu_fwcfg_acpi_device_emul);
437 warnx("%s: failed to create ACPI device for QEMU FwCfg",
442 error = acpi_device_add_res_fixed_ioport(fwcfg_sc.acpi_dev,
443 QEMU_FWCFG_SELECTOR_PORT_NUMBER, 2);
445 warnx("%s: failed to add fixed IO port for QEMU FwCfg",
450 /* add handlers for fwcfg ports */
451 if ((error = qemu_fwcfg_register_port("qemu_fwcfg_selector",
452 QEMU_FWCFG_SELECTOR_PORT_NUMBER,
453 QEMU_FWCFG_SELECTOR_PORT_SIZE,
454 QEMU_FWCFG_SELECTOR_PORT_FLAGS,
455 qemu_fwcfg_selector_port_handler)) != 0) {
457 "%s: Unable to register qemu fwcfg selector port 0x%x",
458 __func__, QEMU_FWCFG_SELECTOR_PORT_NUMBER);
461 if ((error = qemu_fwcfg_register_port("qemu_fwcfg_data",
462 QEMU_FWCFG_DATA_PORT_NUMBER, QEMU_FWCFG_DATA_PORT_SIZE,
463 QEMU_FWCFG_DATA_PORT_FLAGS,
464 qemu_fwcfg_data_port_handler)) != 0) {
466 "%s: Unable to register qemu fwcfg data port 0x%x",
467 __func__, QEMU_FWCFG_DATA_PORT_NUMBER);
472 /* add common fwcfg items */
473 if ((error = qemu_fwcfg_add_item_signature()) != 0) {
474 warnx("%s: Unable to add signature item", __func__);
477 if ((error = qemu_fwcfg_add_item_id()) != 0) {
478 warnx("%s: Unable to add id item", __func__);
481 if ((error = qemu_fwcfg_add_item_nb_cpus()) != 0) {
482 warnx("%s: Unable to add nb_cpus item", __func__);
485 if ((error = qemu_fwcfg_add_item_max_cpus()) != 0) {
486 warnx("%s: Unable to add max_cpus item", __func__);
489 if ((error = qemu_fwcfg_add_item_file_dir()) != 0) {
490 warnx("%s: Unable to add file_dir item", __func__);
493 /* add user defined fwcfg files */
494 if ((error = qemu_fwcfg_add_user_files()) != 0) {
495 warnx("%s: Unable to add user files", __func__);
501 acpi_device_destroy(fwcfg_sc.acpi_dev);
508 qemu_fwcfg_usage(const char *opt)
510 warnx("Invalid fw_cfg option \"%s\"", opt);
511 warnx("-f [name=]<name>,(string|file)=<value>");
515 * Parses the cmdline argument for user defined fw_cfg items. The cmdline
516 * argument has the format:
517 * "-f [name=]<name>,(string|file)=<value>"
519 * E.g.: "-f opt/com.page/example,string=Hello"
522 qemu_fwcfg_parse_cmdline_arg(const char *opt)
524 struct qemu_fwcfg_user_file *fwcfg_file;
526 const char *opt_ptr, *opt_end;
530 fwcfg_file = malloc(sizeof(*fwcfg_file));
531 if (fwcfg_file == NULL) {
532 warnx("Unable to allocate fw_cfg_user_file");
536 /* get pointer to <name> */
538 /* If [name=] is specified, skip it */
539 if (strncmp(opt_ptr, "name=", sizeof("name=") - 1) == 0) {
540 opt_ptr += sizeof("name=") - 1;
543 /* get the end of <name> */
544 opt_end = strchr(opt_ptr, ',');
545 if (opt_end == NULL) {
546 qemu_fwcfg_usage(opt);
550 /* check if <name> is too long */
551 if (opt_end - opt_ptr >= QEMU_FWCFG_MAX_NAME) {
552 warnx("fw_cfg name too long: \"%s\"", opt);
557 strncpy(fwcfg_file->name, opt_ptr, opt_end - opt_ptr);
558 fwcfg_file->name[opt_end - opt_ptr] = '\0';
560 /* set opt_ptr and opt_end to <value> */
561 opt_ptr = opt_end + 1;
562 opt_end = opt_ptr + strlen(opt_ptr);
564 if (strncmp(opt_ptr, "string=", sizeof("string=") - 1) == 0) {
565 opt_ptr += sizeof("string=") - 1;
566 fwcfg_file->data = strdup(opt_ptr);
567 if (fwcfg_file->data == NULL) {
568 warnx("Can't duplicate fw_cfg_user_file string \"%s\"",
572 fwcfg_file->size = strlen(opt_ptr) + 1;
573 } else if (strncmp(opt_ptr, "file=", sizeof("file=") - 1) == 0) {
574 opt_ptr += sizeof("file=") - 1;
576 fd = open(opt_ptr, O_RDONLY);
578 warn("Can't open fw_cfg_user_file file \"%s\"",
583 if (fstat(fd, &sb) < 0) {
584 warn("Unable to get size of file \"%s\"", opt_ptr);
589 fwcfg_file->data = malloc(sb.st_size);
590 if (fwcfg_file->data == NULL) {
592 "Can't allocate fw_cfg_user_file file \"%s\" (size: 0x%16lx)",
593 opt_ptr, sb.st_size);
597 bytes_read = read(fd, fwcfg_file->data, sb.st_size);
598 if (bytes_read < 0 || bytes_read != sb.st_size) {
599 warn("Unable to read file \"%s\"", opt_ptr);
600 free(fwcfg_file->data);
604 fwcfg_file->size = bytes_read;
608 qemu_fwcfg_usage(opt);
612 STAILQ_INSERT_TAIL(&user_files, fwcfg_file, chain);