├── .clang-format ├── BUILD.md ├── LICENSE ├── README.md ├── build.sh ├── firmware ├── SCORPI_EFI.fd ├── SCORPI_VARS.fd └── u-boot.bin ├── include ├── acpi.h ├── acpi_device.h ├── ahci.h ├── arch.h ├── arch │ ├── arm64 │ │ ├── armreg.h │ │ ├── pci_irq.h │ │ ├── pte.h │ │ ├── vmm.h │ │ ├── vmm_dev.h │ │ └── vmm_instruction_emul.h │ └── x86 │ │ ├── pci_irq.h │ │ ├── vmm.h │ │ └── vmm_instruction_emul.h ├── basl.h ├── bhyvegc.h ├── bhyverun.h ├── block_if.h ├── bootrom.h ├── cfi_reg.h ├── cnc.h ├── common.h ├── compat.h ├── config.h ├── console.h ├── debug.h ├── e1000_defines.h ├── e1000_regs.h ├── edid.h ├── iov.h ├── mem.h ├── mevent.h ├── mii.h ├── net_backends.h ├── net_backends_priv.h ├── net_utils.h ├── pci_emul.h ├── pci_irq.h ├── pci_xhci.h ├── qemu_fwcfg.h ├── qemu_loader.h ├── rfb.h ├── rtc_pl031.h ├── run.h ├── smbiostbl.h ├── sockstream.h ├── support │ ├── acpica │ │ ├── acapps.h │ │ ├── acbuffer.h │ │ ├── acclib.h │ │ ├── accommon.h │ │ ├── acconfig.h │ │ ├── acconvert.h │ │ ├── acdebug.h │ │ ├── acdisasm.h │ │ ├── acdispat.h │ │ ├── acevents.h │ │ ├── acexcep.h │ │ ├── acglobal.h │ │ ├── achware.h │ │ ├── acinterp.h │ │ ├── aclocal.h │ │ ├── acmacros.h │ │ ├── acnames.h │ │ ├── acnamesp.h │ │ ├── acobject.h │ │ ├── acopcode.h │ │ ├── acoutput.h │ │ ├── acparser.h │ │ ├── acpi.h │ │ ├── acpiosxf.h │ │ ├── acpixf.h │ │ ├── acpredef.h │ │ ├── acresrc.h │ │ ├── acrestyp.h │ │ ├── acstruct.h │ │ ├── actables.h │ │ ├── actbinfo.h │ │ ├── actbl.h │ │ ├── actbl1.h │ │ ├── actbl2.h │ │ ├── actbl3.h │ │ ├── actypes.h │ │ ├── acutils.h │ │ ├── acuuid.h │ │ ├── amlcode.h │ │ ├── amlresrc.h │ │ └── platform │ │ │ ├── accygwin.h │ │ │ ├── acdragonfly.h │ │ │ ├── acdragonflyex.h │ │ │ ├── acefi.h │ │ │ ├── acefiex.h │ │ │ ├── acenv.h │ │ │ ├── acenvex.h │ │ │ ├── acfreebsd.h │ │ │ ├── acgcc.h │ │ │ ├── acgccex.h │ │ │ ├── achaiku.h │ │ │ ├── acintel.h │ │ │ ├── aclinux.h │ │ │ ├── aclinuxex.h │ │ │ ├── acmacosx.h │ │ │ ├── acmsvc.h │ │ │ ├── acmsvcex.h │ │ │ ├── acnetbsd.h │ │ │ ├── acos2.h │ │ │ ├── acqnx.h │ │ │ ├── acwin.h │ │ │ ├── acwin64.h │ │ │ └── aczephyr.h │ ├── ata.h │ ├── atomic.h │ ├── bitset.h │ ├── cpuset.h │ ├── endian.h │ ├── ethernet.h │ ├── if_tap.h │ ├── if_tun.h │ ├── linker_set.h │ ├── md5.h │ ├── pci_reg.h │ ├── pciio.h │ ├── psci.h │ ├── smccc.h │ └── tree.h ├── tpm_device.h ├── tpm_emul.h ├── tpm_intf.h ├── tpm_ppi.h ├── uart_backend.h ├── uart_emul.h ├── usb.h ├── usb_cdc.h ├── usb_emul.h ├── usb_endian.h ├── usbdi.h ├── virtio.h ├── virtio │ ├── virtio.h │ ├── virtio10.h │ ├── virtio_config.h │ ├── virtio_endian.h │ ├── virtio_ids.h │ ├── virtio_pci_legacy_var.h │ ├── virtio_pci_var.h │ └── virtio_ring.h ├── virtio_gpu.h ├── vmexit.h ├── vmgenc.h ├── vmm.h ├── vmm_instruction_emul.h ├── vmmapi.h └── xhcireg.h ├── libfdt ├── fdt.c ├── fdt.h ├── fdt_addresses.c ├── fdt_empty_tree.c ├── fdt_overlay.c ├── fdt_ro.c ├── fdt_rw.c ├── fdt_strerror.c ├── fdt_sw.c ├── fdt_wip.c ├── libfdt.h ├── libfdt_env.h ├── libfdt_internal.h └── meson.build ├── libnv ├── bsd_nvpair.c ├── libnv.c ├── meson.build ├── nv.h ├── nv_impl.h ├── nvlist_impl.h └── nvpair_impl.h ├── libutil ├── expand_number.c ├── hexdump.c ├── libutil.h ├── md5.c └── meson.build ├── meson.build ├── mjson ├── meson.build ├── mjson.c └── mjson.h ├── run.sh ├── scorpi.entitlements ├── src ├── acpi.c ├── acpi_device.c ├── arch │ └── arm64 │ │ ├── bhyverun.c │ │ ├── fdt.c │ │ ├── fdt.h │ │ ├── pci_irq.c │ │ └── vmexit.c ├── basl.c ├── bhyvegc.c ├── bhyverun.c ├── block_if.c ├── bootrom.c ├── cnc.c ├── compat.c ├── config.c ├── console.c ├── debug.c ├── iov.c ├── libvmm │ └── hvf │ │ ├── internal.h │ │ ├── vgic.c │ │ ├── vmm.c │ │ ├── vmm_instruction_emul.c │ │ └── vmmapi.c ├── mem.c ├── mevent.c ├── net_backends.c ├── net_utils.c ├── pci_82545.c ├── pci_ahci.c ├── pci_emul.c ├── pci_fbuf.c ├── pci_hostbridge.c ├── pci_virtio_block.c ├── pci_virtio_console.c ├── pci_virtio_gpu.c ├── pci_virtio_net.c ├── pci_virtio_rnd.c ├── pci_xhci.c ├── qemu_fwcfg.c ├── qemu_loader.c ├── rfb.c ├── rtc_pl031.c ├── slirp_net_backend.c ├── smbiostbl.c ├── sockstream.c ├── tpm_device.c ├── uart_backend.c ├── uart_emul.c ├── uart_pl011.c ├── usb_emul.c ├── usb_kbd.c ├── usb_mouse.c ├── usb_net.c ├── virtio.c ├── vmgenc.c └── vmnet_net_backend.c ├── tests ├── fdt_test.c ├── meson.build ├── nvlist_test.c └── test.dts └── util ├── console ├── meson.build ├── tty.c ├── websock_client.c └── websock_client.h ├── tty.c └── virtio_tty.c /BUILD.md: -------------------------------------------------------------------------------- 1 | ## Prerequsuites 2 | You need to have cmake and meson installed. 3 | Scorpi depends on libwebsockets library 4 | 5 | ## Building libwebsockets library 6 | ``` 7 | git clone https://github.com/warmcat/libwebsockets.git 8 | cd libwebsockets 9 | mkdir build; cd build 10 | cmake -DLWS_WITH_SSL=0 .. 11 | make 12 | sudo make install 13 | ``` 14 | 15 | ## Building Scorpi 16 | Make sure to export CODESIGN_IDENTITY env variable. It should be in form 'Developer ID Application: [Your Team]' 17 | ``` 18 | CC=clang meson setup --buildtype=release builddir 19 | meson compile -C builddir 20 | ``` -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | SPDX-License-Identifier: BSD-2-Clause 2 | 3 | Copyright (c) 1992-2025 The FreeBSD Project. 4 | Copyright (c) 2025 Alex Fishman 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions 8 | are met: 9 | 1. Redistributions of source code must retain the above copyright 10 | notice, this list of conditions and the following disclaimer. 11 | 2. Redistributions in binary form must reproduce the above copyright 12 | notice, this list of conditions and the following disclaimer in the 13 | documentation and/or other materials provided with the distribution. 14 | 15 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 | OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 | HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 | OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 | SUCH DAMAGE. 26 | 27 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Scorpi - A Modern Lightweight General-Purpose Hypervisor 2 | 3 | ## Overview 4 | 5 | Scorpi is a modern, lightweight, general-purpose hypervisor designed to be an alternative to QEMU. 6 | 7 | ### Key Features 8 | 9 | - **Modern**: Implements only modern devices, primarily VirtIO-based, avoiding legacy emulations. 10 | - **Lightweight**: Built on FreeBSD Bhyve and written in C, with minimal code base for emulating devices. 11 | - **General-Purpose**: Supports headless and graphical VMs, EFI boot loader, and ACPI. Can run Linux and Windows VMs. 12 | - **Modular**: Designed to be used as an API in other applications and services. Graphics, UI and user input are separate modules, and networking can be modularized as well. 13 | 14 | ## Platform Support 15 | 16 | Currently, Scorpi runs on Mac ARM64 using Apple's Hypervisor Framework. The plan is to expand support to: 17 | 18 | - **Linux x86 and ARM** using **KVM** 19 | - **Additional architectures**, including **RISC-V** 20 | 21 | ## Available Bootloaders 22 | 23 | 1. **U-Boot** - Fast and compact but lacks some advanced features such as ACPI and graphics. Best used for headless VMs that require fast start.\ 24 | [Source Code](https://github.com/macos-fuse-t/u-boot) 25 | 26 | 2. **EDK2 UEFI** - Full-featured bootloader that provides ACPI support, frame buffer, and a variety of boot device drivers.\ 27 | [Source Code](https://github.com/macos-fuse-t/edk2) 28 | 29 | ## Running Linux VMs 30 | 31 | 1. Download an ISO that supports ARM64 architecture. 32 | 2. Create an empty disk with: 33 | ```sh 34 | mkfile -n [size] [img_file] 35 | ``` 36 | 3. Example command to start a VM: 37 | ```sh 38 | ./builddir/scorpi -s 0,hostbridge -o console=stdio -o bootrom=./firmware/SCORPI_EFI.fd -s 1,xhci -u kbd -u tablet -s 2,virtio-blk,[img_file] -s 3,virtio-blk,[iso_file],ro -s 4,virtio-net,slirp -s 5,virtio-gpu,hdpi=on -m 2G -c 2 -l /tmp/vm_sock vm1 39 | ``` 40 | To use a graphical viewer, refer to the following reference project: [ScorpiViewer](https://github.com/macos-fuse-t/ScorpiViewer) 41 | 42 | ## Running a Windows VM 43 | 44 | The easiest way to try a Windows 11 VM is to download a Microsoft HyperV preview image and convert it to Scorpi. 45 | 46 | 1. Download a VHDX image from:\ 47 | [Windows Insider Preview ARM64](https://www.microsoft.com/en-us/software-download/windowsinsiderpreviewarm64) 48 | 2. Convert the disk image to Scorpi format using `qemu-img`: 49 | ```sh 50 | qemu-img convert -f vhdx -O raw input.vhdx win11.img 51 | ``` 52 | 3. Run Scorpi: 53 | ```sh 54 | ./builddir/scorpi -s 0,hostbridge -o bootrom=./firmware/SCORPI_EFI.fd -s 1,xhci -u kbd -u tablet -u net,backend=slirp -s 2,ahci-hd,win11.img -s 3,virtio-gpu,fb=on -l /tmp/vm_sock -c 4 -m 4G vm1 55 | ``` 56 | 4. Run ScorpiViewer. 57 | 58 | ## Future Roadmap 59 | 60 | - Implement and add missing features (file sharing, copy/paste support) 61 | - Implement Linux support on top of KVM. 62 | - Add Windows DirectX 12 display driver. 63 | - Extend support to RISC-V and other platforms. 64 | 65 | ## Releated Projects 66 | [U-Boot bootloader](https://github.com/macos-fuse-t/u-boot) 67 | [EDK2 bootloader](https://github.com/macos-fuse-t/edk2) 68 | [Scorpi Viewer](https://github.com/macos-fuse-t/ScorpiViewer) 69 | 70 | ## Licensing 71 | 72 | Scorpi is released under a **permissive license**, providing flexibility for various use cases. 73 | 74 | ## Get Involved 75 | 76 | Contributions and feedback are welcome! Stay tuned for updates as Scorpi evolves into a powerful and versatile hypervisor. 77 | 78 | For inquiries, contact **Alex Fishman** at [alex@fuse-t.org](mailto\:alex@fuse-t.org). 79 | 80 | -------------------------------------------------------------------------------- /build.sh: -------------------------------------------------------------------------------- 1 | CC=clang meson setup --buildtype=debug builddir 2 | meson compile -C builddir 3 | -------------------------------------------------------------------------------- /firmware/SCORPI_EFI.fd: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macos-fuse-t/scorpi/50075c3c761987ecef14c0fa8a0b410d2038d907/firmware/SCORPI_EFI.fd -------------------------------------------------------------------------------- /firmware/SCORPI_VARS.fd: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macos-fuse-t/scorpi/50075c3c761987ecef14c0fa8a0b410d2038d907/firmware/SCORPI_VARS.fd -------------------------------------------------------------------------------- /firmware/u-boot.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/macos-fuse-t/scorpi/50075c3c761987ecef14c0fa8a0b410d2038d907/firmware/u-boot.bin -------------------------------------------------------------------------------- /include/acpi.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2012 NetApp, Inc. 5 | * Copyright (c) 2025 Alex Fishman 6 | * All rights reserved. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions 10 | * are met: 11 | * 1. Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * 2. Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in the 15 | * documentation and/or other materials provided with the distribution. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 | * SUCH DAMAGE. 28 | */ 29 | 30 | #ifndef _ACPI_H_ 31 | #define _ACPI_H_ 32 | 33 | #include "acpi_device.h" 34 | 35 | #define SCI_INT 9 36 | 37 | #define SMI_CMD 0xb2 38 | #define BHYVE_ACPI_ENABLE 0xa0 39 | #define BHYVE_ACPI_DISABLE 0xa1 40 | 41 | #define PM1A_EVT_ADDR 0x400 42 | #define PM1A_CNT_ADDR 0x404 43 | 44 | #define IO_PMTMR 0x408 /* 4-byte i/o port for the timer */ 45 | 46 | #define IO_GPE0_BLK 0x40c /* 2x 1-byte IO port for GPE0_STS/EN */ 47 | #define IO_GPE0_LEN 0x2 48 | 49 | #define IO_GPE0_STS IO_GPE0_BLK 50 | #define IO_GPE0_EN (IO_GPE0_BLK + (IO_GPE0_LEN / 2)) 51 | 52 | /* Allocated GPE bits. */ 53 | #define GPE_VMGENC 0 54 | 55 | struct vmctx; 56 | 57 | int acpi_build(struct vmctx *ctx, int ncpu); 58 | void acpi_raise_gpe(struct vmctx *ctx, unsigned bit); 59 | int acpi_tables_add_device(const struct acpi_device *const dev); 60 | void dsdt_line(const char *fmt, ...); 61 | void dsdt_fixed_ioport(uint16_t iobase, uint16_t length); 62 | void dsdt_fixed_irq(uint8_t irq); 63 | void dsdt_fixed_mem32(uint32_t base, uint32_t length); 64 | void dsdt_indent(int levels); 65 | void dsdt_unindent(int levels); 66 | void sci_init(struct vmctx *ctx); 67 | 68 | #endif /* _ACPI_H_ */ -------------------------------------------------------------------------------- /include/acpi_device.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2021 Beckhoff Automation GmbH & Co. KG 5 | * Author: Corvin Köhne 6 | */ 7 | 8 | #pragma once 9 | 10 | #pragma GCC diagnostic push 11 | #pragma GCC diagnostic ignored "-Wunused-parameter" 12 | //#include 13 | #pragma GCC diagnostic pop 14 | 15 | struct vmctx; 16 | 17 | struct acpi_device; 18 | 19 | /** 20 | * Device specific information and emulation. 21 | * 22 | * @param name Used as device name in the DSDT. 23 | * @param hid Used as _HID in the DSDT. 24 | * @param build_table Called to build a device specific ACPI table like the TPM2 25 | * table. 26 | * @param write_dsdt Called to append the DSDT with device specific 27 | * information. 28 | */ 29 | struct acpi_device_emul { 30 | const char *name; 31 | const char *hid; 32 | 33 | int (*build_table)(const struct acpi_device *dev); 34 | int (*write_dsdt)(const struct acpi_device *dev); 35 | }; 36 | 37 | /** 38 | * Creates an ACPI device. 39 | * 40 | * @param[out] new_dev Returns the newly create ACPI device. 41 | * @param[in] softc Pointer to the software context of the ACPI device. 42 | * @param[in] vm_ctx VM context the ACPI device is created in. 43 | * @param[in] emul Device emulation struct. It contains some information 44 | * like the name of the ACPI device and some device specific 45 | * functions. 46 | */ 47 | int acpi_device_create(struct acpi_device **new_dev, void *softc, 48 | struct vmctx *vm_ctx, const struct acpi_device_emul *emul); 49 | void acpi_device_destroy(struct acpi_device *dev); 50 | 51 | int acpi_device_add_res_fixed_ioport(struct acpi_device *dev, UINT16 port, 52 | UINT8 length); 53 | int acpi_device_add_res_fixed_memory32(struct acpi_device *dev, 54 | UINT8 write_protected, UINT32 address, UINT32 length); 55 | 56 | void *acpi_device_get_softc(const struct acpi_device *dev); 57 | 58 | int acpi_device_build_table(const struct acpi_device *dev); 59 | int acpi_device_write_dsdt(const struct acpi_device *dev); -------------------------------------------------------------------------------- /include/arch.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2025 Alex Fishman 5 | * All rights reserved. 6 | * 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. 15 | * 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. 27 | */ 28 | 29 | #pragma once 30 | 31 | #if defined(__aarch64__) 32 | #include "arch/arm64/armreg.h" 33 | #include "arch/arm64/pte.h" 34 | #elif defined(__x86_64__) 35 | // TBD 36 | #else 37 | #error "Unsupported architecture" 38 | #endif 39 | -------------------------------------------------------------------------------- /include/arch/arm64/pci_irq.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2024 Jessica Clarke 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions 8 | * are met: 9 | * 1. Redistributions of source code must retain the above copyright 10 | * notice, this list of conditions and the following disclaimer. 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in the 13 | * documentation and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 | * SUCH DAMAGE. 26 | */ 27 | 28 | #ifndef __PCI_IRQ_MD_H__ 29 | #define __PCI_IRQ_MD_H__ 30 | 31 | struct pci_irq { 32 | int gic_irq; 33 | }; 34 | 35 | void pci_irq_init(int intrs[static 4]); 36 | 37 | static inline void 38 | pci_irq_init_irq(struct pci_irq *irq) 39 | { 40 | irq->gic_irq = 0; 41 | } 42 | 43 | static inline uint8_t 44 | pci_irq_intline(struct pci_irq *irq __unused) 45 | { 46 | return (255); 47 | } 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /include/arch/arm64/vmm_instruction_emul.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2015 Mihai Carabas 3 | * Copyright (c) 2025 Alex Fishman 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions 8 | * are met: 9 | * 1. Redistributions of source code must retain the above copyright 10 | * notice, this list of conditions and the following disclaimer. 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in the 13 | * documentation and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE 19 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 | * SUCH DAMAGE. 26 | */ 27 | 28 | #ifndef _VMM_INSTRUCTION_EMUL_H_ 29 | #define _VMM_INSTRUCTION_EMUL_H_ 30 | 31 | /* 32 | * Callback functions to read and write memory regions. 33 | */ 34 | typedef int (*mem_region_read_t)(struct vcpu *vcpu, uint64_t gpa, 35 | uint64_t *rval, int rsize, void *arg); 36 | typedef int (*mem_region_write_t)(struct vcpu *vcpu, uint64_t gpa, 37 | uint64_t wval, int wsize, void *arg); 38 | 39 | /* 40 | * Callback functions to read and write registers. 41 | */ 42 | typedef int (*reg_read_t)(struct vcpu *vcpu, uint64_t *rval, void *arg); 43 | typedef int (*reg_write_t)(struct vcpu *vcpu, uint64_t wval, void *arg); 44 | 45 | /* 46 | * Emulate the decoded 'vie' instruction when it contains a memory operation. 47 | * 48 | * The callbacks 'mrr' and 'mrw' emulate reads and writes to the memory region 49 | * containing 'gpa'. 'mrarg' is an opaque argument that is passed into the 50 | * callback functions. 51 | * 52 | * 'void *vm' should be 'struct vm *' when called from kernel context and 53 | * 'struct vmctx *' when called from user context. 54 | * 55 | */ 56 | int vmm_emulate_instruction(struct vcpu *vcpu, uint64_t gpa, struct vie *vie, 57 | struct vm_guest_paging *paging, mem_region_read_t mrr, 58 | mem_region_write_t mrw, void *mrarg); 59 | 60 | /* 61 | * Emulate the decoded 'vre' instruction when it contains a register access. 62 | * 63 | * The callbacks 'regread' and 'regwrite' emulate reads and writes to the 64 | * register from 'vie'. 'regarg' is an opaque argument that is passed into the 65 | * callback functions. 66 | * 67 | * 'void *vm' should be 'struct vm *' when called from kernel context and 68 | * 'struct vmctx *' when called from user context. 69 | * 70 | */ 71 | int vmm_emulate_register(struct vcpu *vcpu, struct vre *vre, reg_read_t regread, 72 | reg_write_t regwrite, void *regarg); 73 | 74 | #endif /* _VMM_INSTRUCTION_EMUL_H_ */ 75 | -------------------------------------------------------------------------------- /include/arch/x86/pci_irq.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2014 Hudson River Trading LLC 5 | * Written by: John H. Baldwin 6 | * Copyright (c) 2025 Alex Fishman 7 | * All rights reserved. 8 | * 9 | * Redistribution and use in source and binary forms, with or without 10 | * modification, are permitted provided that the following conditions 11 | * are met: 12 | * 1. Redistributions of source code must retain the above copyright 13 | * notice, this list of conditions and the following disclaimer. 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 | * SUCH DAMAGE. 29 | */ 30 | 31 | #ifndef __PCI_IRQ_MD_H__ 32 | #define __PCI_IRQ_MD_H__ 33 | 34 | struct vmctx; 35 | 36 | struct pci_irq { 37 | int pirq_pin; 38 | int ioapic_irq; 39 | }; 40 | 41 | void pci_irq_init(struct vmctx *ctx); 42 | void pci_irq_reserve(int irq); 43 | void pci_irq_use(int irq); 44 | int pirq_irq(int pin); 45 | uint8_t pirq_read(int pin); 46 | void pirq_write(struct vmctx *ctx, int pin, uint8_t val); 47 | 48 | static inline void 49 | pci_irq_init_irq(struct pci_irq *irq) 50 | { 51 | irq->pirq_pin = 0; 52 | irq->ioapic_irq = 0; 53 | } 54 | 55 | static inline uint8_t 56 | pci_irq_intline(struct pci_irq *irq) 57 | { 58 | return (pirq_irq(irq->pirq_pin)); 59 | } 60 | 61 | #endif 62 | -------------------------------------------------------------------------------- /include/arch/x86/vmm_instruction_emul.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2012 NetApp, Inc. 5 | * Copyright (c) 2025 Alex Fishman 6 | * All rights reserved. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions 10 | * are met: 11 | * 1. Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * 2. Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in the 15 | * documentation and/or other materials provided with the distribution. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 | * SUCH DAMAGE. 28 | */ 29 | 30 | #ifndef _VMM_INSTRUCTION_EMUL_H_ 31 | #define _VMM_INSTRUCTION_EMUL_H_ 32 | 33 | #include 34 | 35 | /* 36 | * Callback functions to read and write memory regions. 37 | */ 38 | typedef int (*mem_region_read_t)(struct vcpu *vcpu, uint64_t gpa, 39 | uint64_t *rval, int rsize, void *arg); 40 | 41 | typedef int (*mem_region_write_t)(struct vcpu *vcpu, uint64_t gpa, 42 | uint64_t wval, int wsize, void *arg); 43 | 44 | /* 45 | * Emulate the decoded 'vie' instruction. 46 | * 47 | * The callbacks 'mrr' and 'mrw' emulate reads and writes to the memory region 48 | * containing 'gpa'. 'mrarg' is an opaque argument that is passed into the 49 | * callback functions. 50 | * 51 | * 'void *vm' should be 'struct vm *' when called from kernel context and 52 | * 'struct vmctx *' when called from user context. 53 | * s 54 | */ 55 | int vmm_emulate_instruction(struct vcpu *vcpu, uint64_t gpa, struct vie *vie, 56 | struct vm_guest_paging *paging, mem_region_read_t mrr, 57 | mem_region_write_t mrw, void *mrarg); 58 | 59 | int vie_update_register(struct vcpu *vcpu, enum vm_reg_name reg, 60 | uint64_t val, int size); 61 | 62 | /* 63 | * Returns 1 if an alignment check exception should be injected and 0 otherwise. 64 | */ 65 | int vie_alignment_check(int cpl, int operand_size, uint64_t cr0, 66 | uint64_t rflags, uint64_t gla); 67 | 68 | /* Returns 1 if the 'gla' is not canonical and 0 otherwise. */ 69 | int vie_canonical_check(enum vm_cpu_mode cpu_mode, uint64_t gla); 70 | 71 | uint64_t vie_size2mask(int size); 72 | 73 | int vie_calculate_gla(enum vm_cpu_mode cpu_mode, enum vm_reg_name seg, 74 | struct seg_desc *desc, uint64_t off, int length, int addrsize, int prot, 75 | uint64_t *gla); 76 | 77 | void vie_restart(struct vie *vie); 78 | void vie_init(struct vie *vie, const char *inst_bytes, int inst_length); 79 | 80 | /* 81 | * Decode the instruction fetched into 'vie' so it can be emulated. 82 | * 83 | * 'gla' is the guest linear address provided by the hardware assist 84 | * that caused the nested page table fault. It is used to verify that 85 | * the software instruction decoding is in agreement with the hardware. 86 | * 87 | * Some hardware assists do not provide the 'gla' to the hypervisor. 88 | * To skip the 'gla' verification for this or any other reason pass 89 | * in VIE_INVALID_GLA instead. 90 | */ 91 | #ifdef _KERNEL 92 | #define VIE_INVALID_GLA (1UL << 63) /* a non-canonical address */ 93 | int vmm_decode_instruction(struct vcpu *vcpu, uint64_t gla, 94 | enum vm_cpu_mode cpu_mode, int csd, struct vie *vie); 95 | #else /* !_KERNEL */ 96 | /* 97 | * Permit instruction decoding logic to be compiled outside of the kernel for 98 | * rapid iteration and validation. No GLA validation is performed, obviously. 99 | */ 100 | int vmm_decode_instruction(enum vm_cpu_mode cpu_mode, int csd, 101 | struct vie *vie); 102 | #endif /* _KERNEL */ 103 | 104 | #endif /* _VMM_INSTRUCTION_EMUL_H_ */ 105 | -------------------------------------------------------------------------------- /include/basl.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2022 Beckhoff Automation GmbH & Co. KG 5 | */ 6 | 7 | #pragma once 8 | 9 | #pragma GCC diagnostic push 10 | #pragma GCC diagnostic ignored "-Wunused-parameter" 11 | #include 12 | #pragma GCC diagnostic pop 13 | 14 | #include "qemu_fwcfg.h" 15 | 16 | #define ACPI_GAS_ACCESS_WIDTH_LEGACY 0 17 | #define ACPI_GAS_ACCESS_WIDTH_UNDEFINED 0 18 | #define ACPI_GAS_ACCESS_WIDTH_BYTE 1 19 | #define ACPI_GAS_ACCESS_WIDTH_WORD 2 20 | #define ACPI_GAS_ACCESS_WIDTH_DWORD 3 21 | #define ACPI_GAS_ACCESS_WIDTH_QWORD 4 22 | 23 | #define ACPI_SPCR_INTERRUPT_TYPE_8259 0x1 24 | #define ACPI_SPCR_INTERRUPT_TYPE_APIC 0x2 25 | #define ACPI_SPCR_INTERRUPT_TYPE_SAPIC 0x4 26 | #define ACPI_SPCR_INTERRUPT_TYPE_GIC 0x8 27 | 28 | #define ACPI_SPCR_BAUD_RATE_9600 3 29 | #define ACPI_SPCR_BAUD_RATE_19200 4 30 | #define ACPI_SPCR_BAUD_RATE_57600 6 31 | #define ACPI_SPCR_BAUD_RATE_115200 7 32 | 33 | #define ACPI_SPCR_PARITY_NO_PARITY 0 34 | 35 | #define ACPI_SPCR_STOP_BITS_1 1 36 | 37 | #define ACPI_SPCR_FLOW_CONTROL_DCD 0x1 38 | #define ACPI_SPCR_FLOW_CONTROL_RTS_CTS 0x2 39 | #define ACPI_SPCR_FLOW_CONTROL_XON_XOFF 0x4 40 | 41 | #define ACPI_SPCR_TERMINAL_TYPE_VT100 0 42 | #define ACPI_SPCR_TERMINAL_TYPE_VT100_PLUS 1 43 | #define ACPI_SPCR_TERMINAL_TYPE_VT_UTF8 2 44 | #define ACPI_SPCR_TERMINAL_TYPE_ANSI 3 45 | 46 | #define BHYVE_ACPI_BASE 0xf2400 47 | 48 | #define BASL_TABLE_ALIGNMENT 0x10 49 | #define BASL_TABLE_ALIGNMENT_FACS 0x40 50 | 51 | #define BASL_TABLE_CHECKSUM_LEN_FULL_TABLE (-1U) 52 | 53 | #define BASL_EXEC(x) \ 54 | do { \ 55 | const int error = (x); \ 56 | if (error) { \ 57 | warnc(error, \ 58 | "BASL failed @ %s:%d\n Failed to execute %s", \ 59 | __func__, __LINE__, #x); \ 60 | return (error); \ 61 | } \ 62 | } while (0) 63 | 64 | struct basl_table; 65 | 66 | void basl_fill_gas(ACPI_GENERIC_ADDRESS *gas, uint8_t space_id, 67 | uint8_t bit_width, uint8_t bit_offset, uint8_t access_width, 68 | uint64_t address); 69 | int basl_finish(void); 70 | int basl_init(struct vmctx *ctx); 71 | int basl_table_add_checksum(struct basl_table *const table, const uint32_t off, 72 | const uint32_t start, const uint32_t len); 73 | int basl_table_add_length(struct basl_table *const table, const uint32_t off, 74 | const uint8_t size); 75 | int basl_table_add_pointer(struct basl_table *const table, 76 | const uint8_t src_signature[ACPI_NAMESEG_SIZE], const uint32_t off, 77 | const uint8_t size); 78 | int basl_table_append_bytes(struct basl_table *table, const void *bytes, 79 | uint32_t len); 80 | int basl_table_append_checksum(struct basl_table *table, uint32_t start, 81 | uint32_t len); 82 | /* Add an ACPI_TABLE_* to basl without its header. */ 83 | int basl_table_append_content(struct basl_table *table, void *data, 84 | uint32_t len); 85 | int basl_table_append_fwcfg(struct basl_table *table, 86 | const uint8_t *fwcfg_name, uint32_t alignment, 87 | uint8_t size); 88 | int basl_table_append_gas(struct basl_table *table, uint8_t space_id, 89 | uint8_t bit_width, uint8_t bit_offset, uint8_t access_width, 90 | uint64_t address); 91 | int basl_table_append_header(struct basl_table *table, 92 | const uint8_t signature[ACPI_NAMESEG_SIZE], uint8_t revision, 93 | uint32_t oem_revision); 94 | int basl_table_append_int(struct basl_table *table, uint64_t val, uint8_t size); 95 | int basl_table_append_length(struct basl_table *table, uint8_t size); 96 | int basl_table_append_pointer(struct basl_table *table, 97 | const uint8_t src_signature[ACPI_NAMESEG_SIZE], uint8_t size); 98 | int basl_table_create(struct basl_table **table, struct vmctx *ctx, 99 | const uint8_t *name, uint32_t alignment); 100 | /* Adds the table to RSDT and XSDT */ 101 | int basl_table_register_to_rsdt(struct basl_table *table); 102 | -------------------------------------------------------------------------------- /include/bhyvegc.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2015 Tycho Nightingale 5 | * Copyright (c) 2025 Alex Fishman 6 | * All rights reserved. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions 10 | * are met: 11 | * 1. Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * 2. Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in the 15 | * documentation and/or other materials provided with the distribution. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 | * SUCH DAMAGE. 28 | */ 29 | 30 | #ifndef _BHYVEGC_H_ 31 | #define _BHYVEGC_H_ 32 | 33 | struct bhyvegc; 34 | 35 | struct bhyvegc_image { 36 | int vgamode; 37 | int width; 38 | int height; 39 | uint32_t *data; 40 | }; 41 | 42 | struct bhyvegc *bhyvegc_init(int width, int height, void *fbaddr); 43 | void bhyvegc_set_fbaddr(struct bhyvegc *gc, void *fbaddr); 44 | void bhyvegc_resize(struct bhyvegc *gc, int width, int height); 45 | struct bhyvegc_image *bhyvegc_get_image(struct bhyvegc *gc); 46 | 47 | #endif /* _BHYVEGC_H_ */ 48 | -------------------------------------------------------------------------------- /include/bhyverun.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2011 NetApp, Inc. 5 | * Copyright (c) 2025 Alex Fishman 6 | * All rights reserved. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions 10 | * are met: 11 | * 1. Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * 2. Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in the 15 | * documentation and/or other materials provided with the distribution. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 | * SUCH DAMAGE. 28 | */ 29 | 30 | #ifndef _BHYVERUN_H_ 31 | #define _BHYVERUN_H_ 32 | 33 | #include 34 | 35 | #define VMEXIT_CONTINUE (0) 36 | #define VMEXIT_QUIT (1) 37 | #define VMEXIT_ABORT (-1) 38 | 39 | extern int guest_ncpus; 40 | extern uint16_t cpu_cores, cpu_sockets, cpu_threads; 41 | 42 | #ifdef BHYVE_SNAPSHOT 43 | extern char *restore_file; 44 | #endif 45 | 46 | struct vcpu; 47 | struct vmctx; 48 | struct vm_run; 49 | 50 | void *paddr_guest2host(struct vmctx *ctx, uintptr_t addr, size_t len); 51 | #ifdef BHYVE_SNAPSHOT 52 | uintptr_t paddr_host2guest(struct vmctx *ctx, void *addr); 53 | #endif 54 | 55 | struct vcpu; 56 | struct vcpu *fbsdrun_vcpu(int vcpuid); 57 | void fbsdrun_addcpu(int vcpuid); 58 | void fbsdrun_deletecpu(int vcpuid); 59 | int fbsdrun_suspendcpu(int vcpuid); 60 | 61 | int msix_supported(void); 62 | int msi_supported(void); 63 | 64 | typedef int (*vmexit_handler_t)(struct vmctx *, struct vcpu *, struct vm_run *); 65 | 66 | /* Interfaces implemented by machine-dependent code. */ 67 | void bhyve_init_config(void); 68 | void bhyve_optparse(int argc, char **argv); 69 | void bhyve_usage(int code); 70 | 71 | /* Interfaces used by command-line option-parsing code. */ 72 | bool bhyve_parse_config_option(const char *option); 73 | void bhyve_parse_simple_config_file(const char *path); 74 | #ifdef BHYVE_GDB 75 | void bhyve_parse_gdb_options(const char *opt); 76 | #endif 77 | int bhyve_pincpu_parse(const char *opt); 78 | int bhyve_topology_parse(const char *opt); 79 | 80 | void bhyve_init_vcpu(struct vcpu *vcpu); 81 | void bhyve_start_vcpu(struct vcpu *vcpu, bool bsp); 82 | int bhyve_init_platform(struct vmctx *ctx, struct vcpu *bsp); 83 | int bhyve_init_platform_late(struct vmctx *ctx, struct vcpu *bsp); 84 | 85 | #endif 86 | -------------------------------------------------------------------------------- /include/block_if.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2013 Peter Grehan 5 | * Copyright (c) 2025 Alex Fishman 6 | * All rights reserved. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions 10 | * are met: 11 | * 1. Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * 2. Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in the 15 | * documentation and/or other materials provided with the distribution. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 | * SUCH DAMAGE. 28 | */ 29 | 30 | /* 31 | * The block API to be used by bhyve block-device emulations. The routines 32 | * are thread safe, with no assumptions about the context of the completion 33 | * callback - it may occur in the caller's context, or asynchronously in 34 | * another thread. 35 | */ 36 | 37 | #ifndef _BLOCK_IF_H_ 38 | #define _BLOCK_IF_H_ 39 | 40 | #include 41 | #include 42 | #include 43 | 44 | struct vm_snapshot_meta; 45 | 46 | 47 | /* 48 | * BLOCKIF_IOV_MAX is the maximum number of scatter/gather entries in 49 | * a single request. BLOCKIF_RING_MAX is the maxmimum number of 50 | * pending requests that can be queued. 51 | */ 52 | #define BLOCKIF_IOV_MAX 128 /* not practical to be IOV_MAX */ 53 | #define BLOCKIF_RING_MAX 128 54 | 55 | struct blockif_req { 56 | int br_iovcnt; 57 | off_t br_offset; 58 | ssize_t br_resid; 59 | void (*br_callback)(struct blockif_req *req, int err); 60 | void *br_param; 61 | struct iovec br_iov[BLOCKIF_IOV_MAX]; 62 | }; 63 | 64 | struct pci_devinst; 65 | struct blockif_ctxt; 66 | 67 | typedef void blockif_resize_cb(struct blockif_ctxt *, void *, size_t); 68 | 69 | int blockif_legacy_config(nvlist_t *nvl, const char *opts); 70 | int blockif_add_boot_device(struct pci_devinst *const pi, struct blockif_ctxt *const bc); 71 | struct blockif_ctxt *blockif_open(nvlist_t *nvl, const char *ident); 72 | int blockif_register_resize_callback(struct blockif_ctxt *bc, 73 | blockif_resize_cb *cb, void *cb_arg); 74 | off_t blockif_size(struct blockif_ctxt *bc); 75 | void blockif_chs(struct blockif_ctxt *bc, uint16_t *c, uint8_t *h, 76 | uint8_t *s); 77 | int blockif_sectsz(struct blockif_ctxt *bc); 78 | void blockif_psectsz(struct blockif_ctxt *bc, int *size, int *off); 79 | int blockif_queuesz(struct blockif_ctxt *bc); 80 | int blockif_is_ro(struct blockif_ctxt *bc); 81 | int blockif_candelete(struct blockif_ctxt *bc); 82 | int blockif_read(struct blockif_ctxt *bc, struct blockif_req *breq); 83 | int blockif_write(struct blockif_ctxt *bc, struct blockif_req *breq); 84 | int blockif_flush(struct blockif_ctxt *bc, struct blockif_req *breq); 85 | int blockif_delete(struct blockif_ctxt *bc, struct blockif_req *breq); 86 | int blockif_cancel(struct blockif_ctxt *bc, struct blockif_req *breq); 87 | int blockif_close(struct blockif_ctxt *bc); 88 | #ifdef BHYVE_SNAPSHOT 89 | void blockif_pause(struct blockif_ctxt *bc); 90 | void blockif_resume(struct blockif_ctxt *bc); 91 | #endif 92 | 93 | #endif /* _BLOCK_IF_H_ */ 94 | -------------------------------------------------------------------------------- /include/bootrom.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2015 Neel Natu 5 | * Copyright (c) 2025 Alex Fishman 6 | * All rights reserved. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions 10 | * are met: 11 | * 1. Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * 2. Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in the 15 | * documentation and/or other materials provided with the distribution. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 | * SUCH DAMAGE. 28 | */ 29 | 30 | #ifndef _BOOTROM_H_ 31 | #define _BOOTROM_H_ 32 | 33 | #include 34 | #include 35 | #include 36 | #include 37 | 38 | #include "config.h" 39 | 40 | struct vmctx; 41 | 42 | void init_bootrom(struct vmctx *ctx); 43 | enum { 44 | BOOTROM_ALLOC_TOP = 0x80, 45 | _FORCE_INT = INT_MIN, 46 | }; 47 | int bootrom_alloc(struct vmctx *ctx, size_t len, int prot, int flags, 48 | char **region_out, uint64_t *gpa_out); 49 | bool bootrom_boot(void); 50 | int bootrom_loadrom(struct vmctx *ctx); 51 | char *bootrom_romptr(); 52 | uint64_t bootrom_rombase(); 53 | uint64_t bootrom_romsize(); 54 | int bootrom_vars(uint64_t *addr, uint64_t* size); 55 | 56 | #endif -------------------------------------------------------------------------------- /include/cnc.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2025 Alex Fishman 5 | * All rights reserved. 6 | * 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. 15 | * 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. 27 | */ 28 | 29 | #pragma once 30 | 31 | #include 32 | #include 33 | 34 | typedef struct cnc_conn_t* cnc_conn_t; 35 | 36 | typedef void (*CMD_HANDLER)(cnc_conn_t conn, int req_id, int argc, char *argv[], void *param); 37 | 38 | struct cnc_command { 39 | const char* cmd; 40 | CMD_HANDLER cmd_handler; 41 | 42 | const void* param; 43 | 44 | LIST_ENTRY(cnc_command) entries; 45 | }; 46 | 47 | void cnc_register_command(const char *cmd, CMD_HANDLER handler, void *param); 48 | int cnc_start_srv(); 49 | 50 | void cnc_send_response(struct cnc_conn_t *c, int response_id, const char *data); 51 | void cnc_send_notification(const char *data); -------------------------------------------------------------------------------- /include/common.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2025 Alex Fishman 5 | * All rights reserved. 6 | * 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. 15 | * 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. 27 | */ 28 | 29 | #pragma once 30 | 31 | #include 32 | #include 33 | 34 | #if defined(__GNUC__) || defined(__clang__) 35 | #define __packed __attribute__((packed)) 36 | #define __aligned(x) __attribute__((aligned(x))) 37 | #else 38 | #error "Compiler does not support __packed attribute" 39 | #endif 40 | 41 | typedef unsigned long UINT64; 42 | typedef unsigned int UINT32; 43 | typedef unsigned short UINT16; 44 | typedef unsigned char UINT8; 45 | 46 | typedef uint64_t vm_paddr_t; 47 | typedef uintptr_t vm_offset_t; 48 | typedef uintptr_t vm_size_t; 49 | 50 | #define PAGE_SHIFT 12 51 | #define PAGE_SIZE (1 << PAGE_SHIFT) // 4096 52 | #define PAGE_MASK (PAGE_SIZE - 1) 53 | 54 | #define PAGE_SHIFT_4K 12 55 | #define PAGE_SIZE_4K (1 << PAGE_SHIFT_4K) 56 | 57 | #define PAGE_SHIFT_16K 14 58 | #define PAGE_SIZE_16K (1 << PAGE_SHIFT_16K) 59 | 60 | #define PAGE_SHIFT_64K 16 61 | #define PAGE_SIZE_64K (1 << PAGE_SHIFT_64K) 62 | #define PAGE_MASK_64K (PAGE_SIZE_64K - 1) 63 | 64 | #define round_page(x) (((unsigned long)(x) + PAGE_MASK) & ~PAGE_MASK) 65 | #define trunc_page(x) ((unsigned long)(x) & ~PAGE_MASK) 66 | 67 | #define round_page64k(x) (((unsigned long)(x) + PAGE_MASK_64K) & ~PAGE_MASK_64K) 68 | 69 | #define nitems(x) (sizeof(x) / sizeof((x)[0])) 70 | 71 | #define roundup2(x, y) (((x)+((y)-1))&(~((y)-1))) -------------------------------------------------------------------------------- /include/compat.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2025 Alex Fishman 5 | * All rights reserved. 6 | * 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. 15 | * 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. 27 | */ 28 | 29 | #pragma once 30 | 31 | #include 32 | 33 | int compat_set_thread_name(pthread_t thread, const char *name); 34 | int compat_set_thread_affinity(pthread_t thread, int core_id, cpuset_t *cpuset); -------------------------------------------------------------------------------- /include/console.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2015 Tycho Nightingale 5 | * Copyright (c) 2025 Alex Fishman 6 | * All rights reserved. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions 10 | * are met: 11 | * 1. Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * 2. Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in the 15 | * documentation and/or other materials provided with the distribution. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 | * SUCH DAMAGE. 28 | */ 29 | 30 | #ifndef _CONSOLE_H_ 31 | #define _CONSOLE_H_ 32 | 33 | #include 34 | 35 | struct bhyvegc; 36 | 37 | typedef void (*fb_render_func_t)(void *arg); 38 | typedef void (*kbd_event_func_t)(int down, uint8_t hidcode, uint8_t modifiers, void *arg); 39 | typedef void (*ptr_event_func_t)(uint8_t mask, int x, int y, void *arg); 40 | typedef void (*resize_event_func_t)(int x, int y, void *arg); 41 | 42 | void console_init(); 43 | 44 | bool console_is_active(); 45 | 46 | int console_scanout_size(int *w, int *h); 47 | 48 | void console_set_hdpi(bool hdpi); 49 | 50 | void console_set_scanout(bool scanout_active, int w, int h, int sride, uint32_t format, 51 | const char *shm_name, size_t shm_size, bool redrawOnTimer); 52 | 53 | void console_set_mouse_scanout(bool scanout_active, int w, int h, uint32_t format, 54 | int hot_x, int hot_y, const char *shm_name); 55 | 56 | struct bhyvegc_image *console_get_image(void); 57 | 58 | void console_fb_register(fb_render_func_t render_cb, void *arg); 59 | void console_refresh(void); 60 | 61 | void console_kbd_register(kbd_event_func_t event_cb, void *arg, int pri); 62 | void console_key_event(int down, uint32_t keysym, uint32_t keycode); 63 | 64 | void console_ptr_register(ptr_event_func_t event_cb, void *arg, int pri); 65 | void console_ptr_event(uint8_t button, int x, int y); 66 | void console_resize_register(resize_event_func_t event_cb, void *arg); 67 | 68 | #endif /* _CONSOLE_H_ */ 69 | -------------------------------------------------------------------------------- /include/debug.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2019 Vincenzo Maffione 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions 8 | * are met: 9 | * 1. Redistributions of source code must retain the above copyright 10 | * notice, this list of conditions and the following disclaimer. 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in the 13 | * documentation and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 | * SUCH DAMAGE. 26 | */ 27 | 28 | #ifndef _DEBUG_H_ 29 | #define _DEBUG_H_ 30 | 31 | 32 | extern int raw_stdio; 33 | 34 | #define FPRINTLN(filep, fmt, arg...) \ 35 | do { \ 36 | if (raw_stdio) \ 37 | fprintf(filep, fmt "\r\n", ##arg); \ 38 | else \ 39 | fprintf(filep, fmt "\n", ##arg); \ 40 | } while (0) 41 | 42 | #define PRINTLN(fmt, arg...) FPRINTLN(stdout, fmt, ##arg) 43 | #define EPRINTLN(fmt, arg...) FPRINTLN(stderr, fmt, ##arg) 44 | 45 | #endif -------------------------------------------------------------------------------- /include/iov.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2016 Jakub Klama . 5 | * Copyright (c) 2018 Alexander Motin 6 | * Copyright (c) 2025 Alex Fishman 7 | * All rights reserved. 8 | * 9 | * Redistribution and use in source and binary forms, with or without 10 | * modification, are permitted provided that the following conditions 11 | * are met: 12 | * 1. Redistributions of source code must retain the above copyright 13 | * notice, this list of conditions and the following disclaimer 14 | * in this position and unchanged. 15 | * 2. Redistributions in binary form must reproduce the above copyright 16 | * notice, this list of conditions and the following disclaimer in the 17 | * documentation and/or other materials provided with the distribution. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 | * SUCH DAMAGE. 30 | */ 31 | 32 | #ifndef _IOV_H_ 33 | #define _IOV_H_ 34 | 35 | #include 36 | 37 | void seek_iov(const struct iovec *iov1, int niov1, struct iovec *iov2, 38 | int *niov2, size_t seek); 39 | void truncate_iov(struct iovec *iov, int *niov, size_t length); 40 | size_t count_iov(const struct iovec *iov, int niov); 41 | ssize_t iov_to_buf(const struct iovec *iov, int niov, void **buf); 42 | ssize_t buf_to_iov(const void *buf, size_t buflen, const struct iovec *iov, 43 | int niov, size_t seek); 44 | struct iovec *iov_trim(struct iovec *iov, int *iovcnt, size_t hlen); 45 | size_t iov_copy(void *buf, size_t len, const struct iovec *iov, int iovcnt, 46 | size_t offset); 47 | size_t make_iov(const struct iovec *iov1, int niov1, struct iovec *iov2, 48 | int *niov2, size_t seek, size_t len); 49 | 50 | #endif /* _IOV_H_ */ 51 | -------------------------------------------------------------------------------- /include/mem.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2012 NetApp, Inc. 5 | * Copyright (c) 2025 Alex Fishman 6 | * All rights reserved. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions 10 | * are met: 11 | * 1. Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * 2. Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in the 15 | * documentation and/or other materials provided with the distribution. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 | * SUCH DAMAGE. 28 | */ 29 | 30 | #ifndef _MEM_H_ 31 | #define _MEM_H_ 32 | 33 | #include 34 | 35 | struct vcpu; 36 | 37 | typedef int (*mem_func_t)(struct vcpu *vcpu, int dir, uint64_t addr, 38 | int size, uint64_t *val, void *arg1, long arg2); 39 | 40 | struct mem_range { 41 | const char *name; 42 | int flags; 43 | mem_func_t handler; 44 | void *arg1; 45 | long arg2; 46 | uint64_t base; 47 | uint64_t size; 48 | }; 49 | #define MEM_F_READ 0x1 50 | #define MEM_F_WRITE 0x2 51 | #define MEM_F_RW 0x3 52 | #define MEM_F_IMMUTABLE 0x4 /* mem_range cannot be unregistered */ 53 | 54 | void init_mem(int ncpu); 55 | int emulate_mem(struct vcpu *vcpu, uint64_t paddr, struct vie *vie, 56 | struct vm_guest_paging *paging); 57 | 58 | int read_mem(struct vcpu *vpu, uint64_t gpa, uint64_t *rval, int size); 59 | int register_mem(struct mem_range *memp); 60 | int register_mem_fallback(struct mem_range *memp); 61 | int unregister_mem(struct mem_range *memp); 62 | int write_mem(struct vcpu *vcpu, uint64_t gpa, uint64_t wval, int size); 63 | 64 | #endif /* _MEM_H_ */ 65 | -------------------------------------------------------------------------------- /include/mevent.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2011 NetApp, Inc. 5 | * Copyright (c) 2025 Alex Fishman 6 | * All rights reserved. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions 10 | * are met: 11 | * 1. Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * 2. Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in the 15 | * documentation and/or other materials provided with the distribution. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 | * SUCH DAMAGE. 28 | */ 29 | 30 | #ifndef _MEVENT_H_ 31 | #define _MEVENT_H_ 32 | 33 | enum ev_type { 34 | EVF_READ, 35 | EVF_WRITE, 36 | EVF_TIMER, 37 | EVF_SIGNAL, 38 | EVF_VNODE, 39 | }; 40 | 41 | /* Filter flags for EVF_VNODE */ 42 | #define EVFF_ATTRIB 0x0001 43 | 44 | struct mevent; 45 | 46 | struct mevent *mevent_add(int fd, enum ev_type type, 47 | void (*func)(int, enum ev_type, void *), 48 | void *param); 49 | struct mevent *mevent_add_flags(int fd, enum ev_type type, int fflags, 50 | void (*func)(int, enum ev_type, void *), 51 | void *param); 52 | struct mevent *mevent_add_disabled(int fd, enum ev_type type, 53 | void (*func)(int, enum ev_type, void *), 54 | void *param); 55 | int mevent_enable(struct mevent *evp); 56 | int mevent_disable(struct mevent *evp); 57 | int mevent_delete(struct mevent *evp); 58 | int mevent_delete_close(struct mevent *evp); 59 | int mevent_timer_update(struct mevent *evp, int msecs); 60 | 61 | void mevent_dispatch(void); 62 | 63 | #endif /* _MEVENT_H_ */ 64 | -------------------------------------------------------------------------------- /include/net_backends.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2019 Vincenzo Maffione 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions 8 | * are met: 9 | * 1. Redistributions of source code must retain the above copyright 10 | * notice, this list of conditions and the following disclaimer. 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in the 13 | * documentation and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS``AS IS'' AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS 19 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 20 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 21 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 22 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 24 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 25 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #ifndef __NET_BACKENDS_H__ 29 | #define __NET_BACKENDS_H__ 30 | 31 | #include "nv.h" 32 | #include 33 | #include 34 | 35 | #include "mevent.h" 36 | 37 | /* Opaque type representing a network backend. */ 38 | typedef struct net_backend net_backend_t; 39 | 40 | /* Interface between network frontends and the network backends. */ 41 | typedef void (*net_be_rxeof_t)(int, enum ev_type, void *param); 42 | int netbe_init(net_backend_t **be, nvlist_t *nvl, net_be_rxeof_t cb, 43 | void *param); 44 | int netbe_legacy_config(nvlist_t *nvl, const char *opts); 45 | void netbe_cleanup(net_backend_t *be); 46 | uint64_t netbe_get_cap(net_backend_t *be); 47 | int netbe_set_cap(net_backend_t *be, uint64_t cap, 48 | unsigned vnet_hdr_len); 49 | size_t netbe_get_vnet_hdr_len(net_backend_t *be); 50 | ssize_t netbe_send(net_backend_t *be, const struct iovec *iov, int iovcnt); 51 | int netbe_poll(net_backend_t *b, int timeout); 52 | ssize_t netbe_recv(net_backend_t *be, const struct iovec *iov, int iovcnt); 53 | ssize_t netbe_rx_discard(net_backend_t *be); 54 | void netbe_rx_disable(net_backend_t *be); 55 | void netbe_rx_enable(net_backend_t *be); 56 | 57 | 58 | /* 59 | * Network device capabilities taken from the VirtIO standard. 60 | * Despite the name, these capabilities can be used by different frontends 61 | * (virtio-net, ptnet) and supported by different backends (netmap, tap, ...). 62 | */ 63 | #define VIRTIO_NET_F_CSUM (1 << 0) /* host handles partial cksum */ 64 | #define VIRTIO_NET_F_GUEST_CSUM (1 << 1) /* guest handles partial cksum */ 65 | #define VIRTIO_NET_F_MTU (1 << 3) /* initial MTU advice */ 66 | #define VIRTIO_NET_F_MAC (1 << 5) /* host supplies MAC */ 67 | #define VIRTIO_NET_F_GSO_DEPREC (1 << 6) /* deprecated: host handles GSO */ 68 | #define VIRTIO_NET_F_GUEST_TSO4 (1 << 7) /* guest can rcv TSOv4 */ 69 | #define VIRTIO_NET_F_GUEST_TSO6 (1 << 8) /* guest can rcv TSOv6 */ 70 | #define VIRTIO_NET_F_GUEST_ECN (1 << 9) /* guest can rcv TSO with ECN */ 71 | #define VIRTIO_NET_F_GUEST_UFO (1 << 10) /* guest can rcv UFO */ 72 | #define VIRTIO_NET_F_HOST_TSO4 (1 << 11) /* host can rcv TSOv4 */ 73 | #define VIRTIO_NET_F_HOST_TSO6 (1 << 12) /* host can rcv TSOv6 */ 74 | #define VIRTIO_NET_F_HOST_ECN (1 << 13) /* host can rcv TSO with ECN */ 75 | #define VIRTIO_NET_F_HOST_UFO (1 << 14) /* host can rcv UFO */ 76 | #define VIRTIO_NET_F_MRG_RXBUF (1 << 15) /* host can merge RX buffers */ 77 | #define VIRTIO_NET_F_STATUS (1 << 16) /* config status field available */ 78 | #define VIRTIO_NET_F_CTRL_VQ (1 << 17) /* control channel available */ 79 | #define VIRTIO_NET_F_CTRL_RX (1 << 18) /* control channel RX mode support */ 80 | #define VIRTIO_NET_F_CTRL_VLAN (1 << 19) /* control channel VLAN filtering */ 81 | #define VIRTIO_NET_F_GUEST_ANNOUNCE \ 82 | (1 << 21) /* guest can send gratuitous pkts */ 83 | #define VIRTIO_NET_F_MQ (1 << 22) /* host supports multiple VQ pairs */ 84 | 85 | /* 86 | * Fixed network header size 87 | */ 88 | struct virtio_net_rxhdr { 89 | uint8_t vrh_flags; 90 | uint8_t vrh_gso_type; 91 | uint16_t vrh_hdr_len; 92 | uint16_t vrh_gso_size; 93 | uint16_t vrh_csum_start; 94 | uint16_t vrh_csum_offset; 95 | uint16_t vrh_bufs; 96 | } __packed; 97 | #define VNET_HDR_LEN sizeof(struct virtio_net_rxhdr) 98 | 99 | #endif /* __NET_BACKENDS_H__ */ 100 | -------------------------------------------------------------------------------- /include/net_utils.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2019 Vincenzo Maffione 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions 8 | * are met: 9 | * 1. Redistributions of source code must retain the above copyright 10 | * notice, this list of conditions and the following disclaimer. 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in the 13 | * documentation and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS``AS IS'' AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS 19 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 20 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 21 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 22 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 24 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 25 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #ifndef _NET_UTILS_H_ 29 | #define _NET_UTILS_H_ 30 | 31 | #include 32 | #include "pci_emul.h" 33 | 34 | void net_genmac(struct pci_devinst *pi, uint8_t *macaddr); 35 | int net_parsemac(const char *mac_str, uint8_t *mac_addr); 36 | int net_parsemtu(const char *mtu_str, unsigned long *mtu); 37 | 38 | #endif /* _NET_UTILS_H_ */ 39 | -------------------------------------------------------------------------------- /include/pci_irq.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2014 Hudson River Trading LLC 5 | * Written by: John H. Baldwin 6 | * Copyright (c) 2025 Alex Fishman 7 | * All rights reserved. 8 | * 9 | * Redistribution and use in source and binary forms, with or without 10 | * modification, are permitted provided that the following conditions 11 | * are met: 12 | * 1. Redistributions of source code must retain the above copyright 13 | * notice, this list of conditions and the following disclaimer. 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 | * SUCH DAMAGE. 29 | */ 30 | 31 | #ifndef __PCI_IRQ_H__ 32 | #define __PCI_IRQ_H__ 33 | 34 | struct pci_devinst; 35 | 36 | #if defined(__amd64__) 37 | #include "arch/x86/pci_irq.h" 38 | #elif defined(__aarch64__) 39 | #include "arch/arm64/pci_irq.h" 40 | #else 41 | #error Unsupported platform 42 | #endif 43 | 44 | void pci_irq_assert(struct pci_devinst *pi); 45 | void pci_irq_deassert(struct pci_devinst *pi); 46 | void pci_irq_route(struct pci_devinst *pi, struct pci_irq *irq); 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /include/qemu_fwcfg.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2021 Beckhoff Automation GmbH & Co. KG 5 | * Author: Corvin Köhne 6 | */ 7 | 8 | #pragma once 9 | 10 | #include 11 | 12 | #define QEMU_FWCFG_MAX_ARCHS 0x2 13 | #define QEMU_FWCFG_MAX_ENTRIES 0x4000 14 | #define QEMU_FWCFG_MAX_NAME 56 15 | 16 | #define QEMU_FWCFG_FILE_TABLE_LOADER "etc/table-loader" 17 | 18 | struct qemu_fwcfg_item { 19 | uint32_t size; 20 | uint8_t *data; 21 | }; 22 | 23 | int qemu_fwcfg_add_file(const char *name, 24 | const uint32_t size, void *const data); 25 | int qemu_fwcfg_init(struct vmctx *const ctx, uint64_t mmio_base, size_t mmio_size); 26 | int qemu_fwcfg_parse_cmdline_arg(const char *opt); 27 | -------------------------------------------------------------------------------- /include/qemu_loader.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2022 Beckhoff Automation GmbH & Co. KG 5 | * Author: Corvin Köhne 6 | */ 7 | 8 | #pragma once 9 | 10 | #include "qemu_fwcfg.h" 11 | 12 | struct qemu_loader; 13 | 14 | /* 15 | * Some guest bios like seabios assume the RSDP to be located in the FSEG. Bhyve 16 | * only supports OVMF which has no such requirement. 17 | */ 18 | enum qemu_loader_zone { 19 | QEMU_LOADER_ALLOC_HIGH = 1, 20 | QEMU_LOADER_ALLOC_FSEG, /* 0x0F000000 - 0x100000 */ 21 | }; 22 | 23 | /** 24 | * Loads a fwcfg item into guest memory. This command has to be issued before 25 | * any subsequent command can be used. 26 | * 27 | * @param loader Qemu loader instance the command should be added to. 28 | * @param name Name of the fwcfg item which should be allocated. 29 | * @param alignment Alignment required by the data. 30 | * @param zone Memory zone in which it should be loaded. 31 | */ 32 | int qemu_loader_alloc(struct qemu_loader *loader, const uint8_t *name, 33 | uint32_t alignment, enum qemu_loader_zone zone); 34 | /** 35 | * Calculates a checksum for @p name and writes it to @p name + @p off . The 36 | * checksum calculation ranges from @p start to @p start + @p len. The checksum 37 | * field is always one byte large and all bytes in the specified range, 38 | * including the checksum, have to sum up to 0. 39 | * 40 | * @param loader Qemu loader instance the command should be added to. 41 | * @param name Name of the fwcfg item which should be patched. 42 | * @param off Offset into @p name . 43 | * @param start Start offset of checksum calculation. 44 | * @param len Length of the checksum calculation. 45 | */ 46 | int qemu_loader_add_checksum(struct qemu_loader *loader, const uint8_t *name, 47 | uint32_t off, uint32_t start, uint32_t len); 48 | /** 49 | * Adds the address of @p src_name to the value at @p dest_name + @p off . The 50 | * size of the pointer is determined by @p dest_size and should be 1, 2, 4 or 8. 51 | * 52 | * @param loader Qemu loader instance the command should be added to. 53 | * @param dest_name Name of the fwcfg item which should be patched. 54 | * @param src_name Name of the fwcfg item which address should be written to 55 | * @p dest_name + @p off. 56 | * @param off Offset into @p dest_name . 57 | * @param size Size of the pointer (1, 2, 4 or 8). 58 | */ 59 | int qemu_loader_add_pointer(struct qemu_loader *loader, 60 | const uint8_t *dest_name, const uint8_t *src_name, uint32_t off, 61 | uint8_t size); 62 | 63 | /** 64 | * Creates a qemu loader instance. 65 | * 66 | * @param new_loader Returns the newly created qemu loader instance. 67 | * @param fwcfg_name Name of the FwCfg item which represents the qemu loader 68 | */ 69 | int qemu_loader_create(struct qemu_loader **new_loader, 70 | const uint8_t *fwcfg_name); 71 | /** 72 | * Signals that all commands are written to the qemu loader. This function 73 | * creates a proper FwCfg item and registers it. 74 | * 75 | * @param loader Qemu loader instance which should be finished. 76 | */ 77 | int qemu_loader_finish(struct qemu_loader *loader); 78 | -------------------------------------------------------------------------------- /include/rfb.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2015 Tycho Nightingale 5 | * Copyright (c) 2025 Alex Fishman 6 | * All rights reserved. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions 10 | * are met: 11 | * 1. Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * 2. Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in the 15 | * documentation and/or other materials provided with the distribution. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 | * SUCH DAMAGE. 28 | */ 29 | 30 | #ifndef _RFB_H_ 31 | #define _RFB_H_ 32 | 33 | #define RFB_PORT 5900 34 | 35 | int rfb_init(const char *hostname, int port, int wait, 36 | const char *password); 37 | 38 | #endif /* _RFB_H_ */ 39 | -------------------------------------------------------------------------------- /include/rtc_pl031.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2024 Jessica Clarke 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions 8 | * are met: 9 | * 1. Redistributions of source code must retain the above copyright 10 | * notice, this list of conditions and the following disclaimer. 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in the 13 | * documentation and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 | * SUCH DAMAGE. 26 | */ 27 | 28 | #ifndef _RTC_PL031_H_ 29 | #define _RTC_PL031_H_ 30 | 31 | struct rtc_pl031_softc; 32 | typedef void (*rtc_pl031_intr_func_t)(void *arg); 33 | 34 | struct rtc_pl031_softc *rtc_pl031_init(rtc_pl031_intr_func_t intr_assert, 35 | rtc_pl031_intr_func_t intr_deassert, void *arg); 36 | void rtc_pl031_write(struct rtc_pl031_softc *sc, int offset, 37 | uint32_t value); 38 | uint32_t rtc_pl031_read(struct rtc_pl031_softc *sc, int offset); 39 | 40 | #endif /* _RTC_PL031_H_ */ 41 | -------------------------------------------------------------------------------- /include/run.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2011 NetApp, Inc. 5 | * Copyright (c) 2025 Alex Fishman 6 | * All rights reserved. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions 10 | * are met: 11 | * 1. Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * 2. Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in the 15 | * documentation and/or other materials provided with the distribution. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 | * SUCH DAMAGE. 28 | */ 29 | 30 | #ifndef _BHYVERUN_H_ 31 | #define _BHYVERUN_H_ 32 | 33 | #include 34 | 35 | #define VMEXIT_CONTINUE (0) 36 | #define VMEXIT_ABORT (-1) 37 | 38 | extern int guest_ncpus; 39 | extern uint16_t cpu_cores, cpu_sockets, cpu_threads; 40 | 41 | #ifdef BHYVE_SNAPSHOT 42 | extern char *restore_file; 43 | #endif 44 | 45 | struct vcpu; 46 | struct vmctx; 47 | struct vm_run; 48 | 49 | void *paddr_guest2host(struct vmctx *ctx, uintptr_t addr, size_t len); 50 | #ifdef BHYVE_SNAPSHOT 51 | uintptr_t paddr_host2guest(struct vmctx *ctx, void *addr); 52 | #endif 53 | 54 | struct vcpu; 55 | struct vcpu *fbsdrun_vcpu(int vcpuid); 56 | void fbsdrun_addcpu(int vcpuid); 57 | void fbsdrun_deletecpu(int vcpuid); 58 | int fbsdrun_suspendcpu(int vcpuid); 59 | 60 | int fbsdrun_virtio_msix(void); 61 | 62 | typedef int (*vmexit_handler_t)(struct vmctx *, struct vcpu *, struct vm_run *); 63 | 64 | /* Interfaces implemented by machine-dependent code. */ 65 | void bhyve_init_config(void); 66 | void bhyve_optparse(int argc, char **argv); 67 | void bhyve_usage(int code); 68 | 69 | /* Interfaces used by command-line option-parsing code. */ 70 | bool bhyve_parse_config_option(const char *option); 71 | void bhyve_parse_simple_config_file(const char *path); 72 | #ifdef BHYVE_GDB 73 | void bhyve_parse_gdb_options(const char *opt); 74 | #endif 75 | int bhyve_pincpu_parse(const char *opt); 76 | int bhyve_topology_parse(const char *opt); 77 | 78 | void bhyve_init_vcpu(struct vcpu *vcpu); 79 | void bhyve_start_vcpu(struct vcpu *vcpu, bool bsp); 80 | int bhyve_init_platform(struct vmctx *ctx, struct vcpu *bsp); 81 | int bhyve_init_platform_late(struct vmctx *ctx, struct vcpu *bsp); 82 | 83 | #endif -------------------------------------------------------------------------------- /include/smbiostbl.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2014 Tycho Nightingale 5 | * Copyright (c) 2025 Alex Fishman 6 | * All rights reserved. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions 10 | * are met: 11 | * 1. Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * 2. Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in the 15 | * documentation and/or other materials provided with the distribution. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 | * SUCH DAMAGE. 28 | */ 29 | 30 | #ifndef _SMBIOSTBL_H_ 31 | #define _SMBIOSTBL_H_ 32 | 33 | struct vmctx; 34 | 35 | int smbios_build(struct vmctx *ctx, void **addr, size_t *len); 36 | 37 | #endif /* _SMBIOSTBL_H_ */ 38 | -------------------------------------------------------------------------------- /include/sockstream.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2015 Nahanni Systems, Inc. 5 | * Copyright (c) 2025 Alex Fishman 6 | * All rights reserved. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions 10 | * are met: 11 | * 1. Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * 2. Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in the 15 | * documentation and/or other materials provided with the distribution. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 | * SUCH DAMAGE. 28 | */ 29 | 30 | #include 31 | #include 32 | 33 | ssize_t stream_read(int fd, void *buf, ssize_t nbytes); 34 | ssize_t stream_write(int fd, const void *buf, ssize_t nbytes); 35 | -------------------------------------------------------------------------------- /include/support/atomic.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2025 Alex Fishman 5 | * All rights reserved. 6 | * 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. 15 | * 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. 27 | */ 28 | 29 | #pragma once 30 | 31 | #include 32 | #include 33 | #include 34 | #include 35 | 36 | static inline void atomic_set_long(volatile u_long *ptr, long mask) { 37 | atomic_fetch_or((volatile _Atomic long *)ptr, mask); 38 | } 39 | 40 | static inline void atomic_clear_long(volatile u_long *ptr, long mask) { 41 | atomic_fetch_and((volatile _Atomic long *)ptr, ~mask); 42 | } 43 | 44 | static inline void atomic_store_rel_long(volatile u_long *ptr, long value) { 45 | atomic_store_explicit((volatile _Atomic long *)ptr, value, memory_order_release); 46 | } 47 | 48 | static inline int atomic_cmpset_ptr(long *ptr, long expected, long desired) { 49 | return atomic_compare_exchange_strong((atomic_long*) ptr, &expected, desired); 50 | } 51 | 52 | static inline int32_t atomic_load_32(const int32_t *obj) { 53 | return __atomic_load_n(obj, __ATOMIC_SEQ_CST); 54 | } 55 | 56 | static inline int64_t atomic_load_64(const int64_t *obj) { 57 | return __atomic_load_n(obj, __ATOMIC_SEQ_CST); 58 | } 59 | 60 | static inline int __bitcountl(long value) { 61 | #if defined(__GNUC__) || defined(__clang__) 62 | return __builtin_popcountl(value); 63 | #else 64 | // Generic implementation 65 | int count = 0; 66 | while (value) { 67 | count += value & 1; 68 | value >>= 1; 69 | } 70 | return count; 71 | #endif 72 | } 73 | 74 | #ifndef __APPLE__ 75 | static inline int ffsl(long value) { 76 | #if defined(__GNUC__) || defined(__clang__) 77 | return __builtin_ffsl(value); 78 | #else 79 | // Generic implementation 80 | int pos = 1; 81 | while (value) { 82 | if (value & 1) { 83 | return pos; 84 | } 85 | value >>= 1; 86 | pos++; 87 | } 88 | return 0; 89 | #endif 90 | } 91 | #endif -------------------------------------------------------------------------------- /include/support/endian.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2025 Alex Fishman 5 | * All rights reserved. 6 | * 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. 15 | * 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. 27 | */ 28 | 29 | #pragma once 30 | 31 | #ifdef __APPLE__ 32 | #include 33 | #define htole32(x) OSSwapHostToLittleInt32(x) 34 | #define htole16(x) OSSwapHostToLittleInt16(x) 35 | #define htole64(x) OSSwapHostToLittleInt64(x) 36 | #define htobe16(x) OSSwapHostToBigInt16(x) 37 | #define htobe32(x) OSSwapHostToBigInt32(x) 38 | #define htobe64(x) OSSwapHostToBigInt64(x) 39 | 40 | #define be16toh(x) OSSwapBigToHostInt16(x) 41 | #define be32toh(x) OSSwapBigToHostInt32(x) 42 | #define be64toh(x) OSSwapBigToHostInt64(x) 43 | 44 | #define le16toh(x) OSSwapLittleToHostInt16(x) 45 | #define le32toh(x) OSSwapLittleToHostInt32(x) 46 | #define le64toh(x) OSSwapLittleToHostInt64(x) 47 | #elif defined(__linux__) 48 | #include 49 | #endif 50 | 51 | /* Alignment-agnostic encode/decode bytestream to/from little/big endian. */ 52 | static __inline uint16_t 53 | be16dec(const void *pp) 54 | { 55 | uint8_t const *p = (uint8_t const *)pp; 56 | 57 | return ((p[0] << 8) | p[1]); 58 | } 59 | 60 | static __inline uint32_t 61 | be32dec(const void *pp) 62 | { 63 | uint8_t const *p = (uint8_t const *)pp; 64 | 65 | return (((unsigned)p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]); 66 | } 67 | 68 | static __inline uint64_t 69 | be64dec(const void *pp) 70 | { 71 | uint8_t const *p = (uint8_t const *)pp; 72 | 73 | return (((uint64_t)be32dec(p) << 32) | be32dec(p + 4)); 74 | } 75 | 76 | static __inline uint16_t 77 | le16dec(const void *pp) 78 | { 79 | uint8_t const *p = (uint8_t const *)pp; 80 | 81 | return ((p[1] << 8) | p[0]); 82 | } 83 | 84 | static __inline uint32_t 85 | le32dec(const void *pp) 86 | { 87 | uint8_t const *p = (uint8_t const *)pp; 88 | 89 | return (((unsigned)p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]); 90 | } 91 | 92 | static __inline uint64_t 93 | le64dec(const void *pp) 94 | { 95 | uint8_t const *p = (uint8_t const *)pp; 96 | 97 | return (((uint64_t)le32dec(p + 4) << 32) | le32dec(p)); 98 | } 99 | 100 | static __inline void 101 | be16enc(void *pp, uint16_t u) 102 | { 103 | uint8_t *p = (uint8_t *)pp; 104 | 105 | p[0] = (u >> 8) & 0xff; 106 | p[1] = u & 0xff; 107 | } 108 | 109 | static __inline void 110 | be32enc(void *pp, uint32_t u) 111 | { 112 | uint8_t *p = (uint8_t *)pp; 113 | 114 | p[0] = (u >> 24) & 0xff; 115 | p[1] = (u >> 16) & 0xff; 116 | p[2] = (u >> 8) & 0xff; 117 | p[3] = u & 0xff; 118 | } 119 | 120 | static __inline void 121 | be64enc(void *pp, uint64_t u) 122 | { 123 | uint8_t *p = (uint8_t *)pp; 124 | 125 | be32enc(p, (uint32_t)(u >> 32)); 126 | be32enc(p + 4, (uint32_t)(u & 0xffffffffU)); 127 | } 128 | 129 | static __inline void 130 | le16enc(void *pp, uint16_t u) 131 | { 132 | uint8_t *p = (uint8_t *)pp; 133 | 134 | p[0] = u & 0xff; 135 | p[1] = (u >> 8) & 0xff; 136 | } 137 | 138 | static __inline void 139 | le32enc(void *pp, uint32_t u) 140 | { 141 | uint8_t *p = (uint8_t *)pp; 142 | 143 | p[0] = u & 0xff; 144 | p[1] = (u >> 8) & 0xff; 145 | p[2] = (u >> 16) & 0xff; 146 | p[3] = (u >> 24) & 0xff; 147 | } 148 | 149 | static __inline void 150 | le64enc(void *pp, uint64_t u) 151 | { 152 | uint8_t *p = (uint8_t *)pp; 153 | 154 | le32enc(p, (uint32_t)(u & 0xffffffffU)); 155 | le32enc(p + 4, (uint32_t)(u >> 32)); 156 | } 157 | 158 | static __inline 159 | uint16_t SwapBytes16(uint16_t value) { 160 | return (value >> 8) | (value << 8); 161 | } 162 | 163 | static __inline 164 | uint32_t SwapBytes32(uint32_t value) { 165 | return ((value >> 24) & 0x000000FF) | 166 | ((value >> 8) & 0x0000FF00) | 167 | ((value << 8) & 0x00FF0000) | 168 | ((value << 24) & 0xFF000000); 169 | } -------------------------------------------------------------------------------- /include/support/if_tap.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (C) 1999-2000 by Maksim Yevmenkin 5 | * Copyright (c) 2025 Alex Fishman 6 | * All rights reserved. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions 10 | * are met: 11 | * 1. Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * 2. Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in the 15 | * documentation and/or other materials provided with the distribution. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 | * SUCH DAMAGE. 28 | * 29 | * BASED ON: 30 | * ------------------------------------------------------------------------- 31 | * 32 | * Copyright (c) 1988, Julian Onions 33 | * Nottingham University 1987. 34 | */ 35 | 36 | /* 37 | * $Id: if_tap.h,v 0.7 2000/07/12 04:12:51 max Exp $ 38 | */ 39 | 40 | #ifndef _NET_IF_TAP_H_ 41 | #define _NET_IF_TAP_H_ 42 | 43 | #include "if_tun.h" 44 | 45 | /* maximum receive packet size (hard limit) */ 46 | #define TAPMRU 65535 47 | 48 | #define tapinfo tuninfo 49 | 50 | /* 51 | * ioctl's for get/set debug; these are aliases of TUN* ioctls, see net/if_tun.h 52 | * for details. 53 | */ 54 | #define TAPSDEBUG TUNSDEBUG 55 | #define TAPGDEBUG TUNGDEBUG 56 | #define TAPSIFINFO TUNSIFINFO 57 | #define TAPGIFINFO TUNGIFINFO 58 | #define TAPGIFNAME TUNGIFNAME 59 | #define TAPSVNETHDR _IOW('t', 91, int) 60 | #define TAPGVNETHDR _IOR('t', 94, int) 61 | 62 | /* VMware ioctl's */ 63 | #define VMIO_SIOCSIFFLAGS _IO('V', 0) 64 | #define VMIO_SIOCSKEEP _IO('V', 1) 65 | #define VMIO_SIOCSIFBR _IO('V', 2) 66 | #define VMIO_SIOCSLADRF _IO('V', 3) 67 | 68 | /* XXX -- unimplemented */ 69 | #define VMIO_SIOCSETMACADDR _IO('V', 4) 70 | 71 | /* XXX -- not used? */ 72 | #define VMIO_SIOCPORT _IO('V', 5) 73 | #define VMIO_SIOCBRIDGE _IO('V', 6) 74 | #define VMIO_SIOCNETIF _IO('V', 7) 75 | 76 | #endif /* !_NET_IF_TAP_H_ */ 77 | -------------------------------------------------------------------------------- /include/support/if_tun.h: -------------------------------------------------------------------------------- 1 | /* $NetBSD: if_tun.h,v 1.5 1994/06/29 06:36:27 cgd Exp $ */ 2 | 3 | /*- 4 | * Copyright (c) 1988, Julian Onions 5 | * Nottingham University 1987. 6 | * 7 | * This source may be freely distributed, however I would be interested 8 | * in any changes that are made. 9 | * 10 | * This driver takes packets off the IP i/f and hands them up to a 11 | * user process to have its wicked way with. This driver has it's 12 | * roots in a similar driver written by Phil Cockcroft (formerly) at 13 | * UCL. This driver is based much more on read/write/select mode of 14 | * operation though. 15 | */ 16 | 17 | #ifndef _NET_IF_TUN_H_ 18 | #define _NET_IF_TUN_H_ 19 | 20 | /* Refer to if_tunvar.h for the softc stuff */ 21 | 22 | /* Maximum transmit packet size (default) */ 23 | #define TUNMTU 1500 24 | 25 | /* Maximum receive packet size (hard limit) */ 26 | #define TUNMRU 65535 27 | 28 | struct tuninfo { 29 | int baudrate; /* linespeed */ 30 | unsigned short mtu; /* maximum transmission unit */ 31 | u_char type; /* ethernet, tokenring, etc. */ 32 | u_char dummy; /* place holder */ 33 | }; 34 | 35 | /* ioctl's for get/set debug */ 36 | #define TUNSDEBUG _IOW('t', 90, int) 37 | #define TUNGDEBUG _IOR('t', 89, int) 38 | #define TUNSIFINFO _IOW('t', 91, struct tuninfo) 39 | #define TUNGIFINFO _IOR('t', 92, struct tuninfo) 40 | #define TUNSLMODE _IOW('t', 93, int) 41 | #define TUNGIFNAME _IOR('t', 93, struct ifreq) 42 | #define TUNSIFMODE _IOW('t', 94, int) 43 | #define TUNSIFPID _IO('t', 95) 44 | #define TUNSIFHEAD _IOW('t', 96, int) 45 | #define TUNGIFHEAD _IOR('t', 97, int) 46 | 47 | #endif /* !_NET_IF_TUN_H_ */ 48 | -------------------------------------------------------------------------------- /include/support/linker_set.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2025 Alex Fishman 5 | * All rights reserved. 6 | * 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. 15 | * 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. 27 | */ 28 | 29 | /* mach_linker_set.h (example) */ 30 | 31 | #pragma once 32 | 33 | #if !defined(__used) 34 | # define __used __attribute__((used)) 35 | #endif 36 | 37 | /* 38 | * Put each set entry (pointer) in the "__DATA,__" section. Mark it as 39 | * used so LTO doesn't discard it. 40 | */ 41 | #define __MAKE_SET(set, sym) \ 42 | __attribute__((no_sanitize("address"), used, section("__DATA,__" #set))) \ 43 | static void const *__set_##set##_sym_##sym = &(sym) 44 | 45 | /* For convenience: */ 46 | #define DATA_SET(set, sym) __MAKE_SET(set, sym) 47 | 48 | /* 49 | * Provide Mach-O boundary symbols: 50 | * section$start$__DATA$__ 51 | * section$end$__DATA$__ 52 | */ 53 | #define SET_DECLARE(set, T) \ 54 | extern T __weak *__start_##set __asm("section$start$__DATA$__" #set); \ 55 | extern T __weak *__stop_##set __asm("section$end$__DATA$__" #set) 56 | 57 | /* Common iteration macros: */ 58 | #define SET_BEGIN(set) (&__start_##set) 59 | #define SET_LIMIT(set) (&__stop_##set) 60 | #define SET_COUNT(set) (SET_LIMIT(set) - SET_BEGIN(set)) 61 | #define SET_FOREACH(pvar, set) \ 62 | for ((pvar) = SET_BEGIN(set); (pvar) < SET_LIMIT(set); (pvar)++) 63 | -------------------------------------------------------------------------------- /include/support/md5.h: -------------------------------------------------------------------------------- 1 | /* MD5.H - header file for MD5C.C 2 | */ 3 | 4 | /*- 5 | SPDX-License-Identifier: RSA-MD 6 | 7 | Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All 8 | rights reserved. 9 | 10 | License to copy and use this software is granted provided that it 11 | is identified as the "RSA Data Security, Inc. MD5 Message-Digest 12 | Algorithm" in all material mentioning or referencing this software 13 | or this function. 14 | 15 | License is also granted to make and use derivative works provided 16 | that such works are identified as "derived from the RSA Data 17 | Security, Inc. MD5 Message-Digest Algorithm" in all material 18 | mentioning or referencing the derived work. 19 | 20 | RSA Data Security, Inc. makes no representations concerning either 21 | the merchantability of this software or the suitability of this 22 | software for any particular purpose. It is provided "as is" 23 | without express or implied warranty of any kind. 24 | 25 | These notices must be retained in any copies of any part of this 26 | documentation and/or software. 27 | */ 28 | 29 | #ifndef _SYS_MD5_H_ 30 | #define _SYS_MD5_H_ 31 | 32 | #include 33 | 34 | #define MD5_BLOCK_LENGTH 64 35 | #define MD5_DIGEST_LENGTH 16 36 | #define MD5_DIGEST_STRING_LENGTH (MD5_DIGEST_LENGTH * 2 + 1) 37 | 38 | /* MD5 context. */ 39 | typedef struct MD5Context { 40 | u_int32_t state[4]; /* state (ABCD) */ 41 | u_int32_t count[2]; /* number of bits, modulo 2^64 (lsb first) */ 42 | unsigned char buffer[64]; /* input buffer */ 43 | } MD5_CTX; 44 | 45 | #ifndef _KERNEL 46 | 47 | /* Ensure libmd symbols do not clash with libcrypto */ 48 | 49 | #ifndef MD5Init 50 | #define MD5Init _libmd_MD5Init 51 | #endif 52 | #ifndef MD5Update 53 | #define MD5Update _libmd_MD5Update 54 | #endif 55 | #ifndef MD5Pad 56 | #define MD5Pad _libmd_MD5Pad 57 | #endif 58 | #ifndef MD5Final 59 | #define MD5Final _libmd_MD5Final 60 | #endif 61 | #ifndef MD5Transform 62 | #define MD5Transform _libmd_MD5Transform 63 | #endif 64 | #ifndef MD5End 65 | #define MD5End _libmd_MD5End 66 | #endif 67 | #ifndef MD5Fd 68 | #define MD5Fd _libmd_MD5Fd 69 | #endif 70 | #ifndef MD5FdChunk 71 | #define MD5FdChunk _libmd_MD5FdChunk 72 | #endif 73 | #ifndef MD5File 74 | #define MD5File _libmd_MD5File 75 | #endif 76 | #ifndef MD5FileChunk 77 | #define MD5FileChunk _libmd_MD5FileChunk 78 | #endif 79 | #ifndef MD5Data 80 | #define MD5Data _libmd_MD5Data 81 | #endif 82 | 83 | #endif 84 | 85 | #include 86 | 87 | __BEGIN_DECLS 88 | void MD5Init (MD5_CTX *); 89 | void MD5Update (MD5_CTX *, const void *, unsigned int); 90 | void MD5Final (unsigned char[MD5_DIGEST_LENGTH], MD5_CTX *); 91 | #ifndef _KERNEL 92 | char * MD5End(MD5_CTX *, char *); 93 | char * MD5Fd(int, char *); 94 | char * MD5FdChunk(int, char *, off_t, off_t); 95 | char * MD5File(const char *, char *); 96 | char * MD5FileChunk(const char *, char *, off_t, off_t); 97 | char * MD5Data(const void *, unsigned int, char *); 98 | #endif 99 | __END_DECLS 100 | #endif /* _SYS_MD5_H_ */ 101 | -------------------------------------------------------------------------------- /include/support/psci.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2013, 2014 Robin Randhawa 3 | * Copyright (c) 2025 Alex Fishman 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions 8 | * are met: 9 | * 1. Redistributions of source code must retain the above copyright 10 | * notice, this list of conditions and the following disclaimer. 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in the 13 | * documentation and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 | * SUCH DAMAGE. 26 | */ 27 | 28 | #ifndef _MACHINE_PSCI_H_ 29 | #define _MACHINE_PSCI_H_ 30 | 31 | #include 32 | #include "smccc.h" 33 | 34 | /* 35 | * PSCI return codes. 36 | */ 37 | #define PSCI_RETVAL_SUCCESS 0 38 | #define PSCI_RETVAL_NOT_SUPPORTED -1 39 | #define PSCI_RETVAL_INVALID_PARAMS -2 40 | #define PSCI_RETVAL_DENIED -3 41 | #define PSCI_RETVAL_ALREADY_ON -4 42 | #define PSCI_RETVAL_ON_PENDING -5 43 | #define PSCI_RETVAL_INTERNAL_FAILURE -6 44 | #define PSCI_RETVAL_NOT_PRESENT -7 45 | #define PSCI_RETVAL_DISABLED -8 46 | /* 47 | * Used to signal PSCI is not available, e.g. to start a CPU. 48 | */ 49 | #define PSCI_MISSING 1 50 | 51 | /* 52 | * PSCI function codes (as per PSCI v0.2). 53 | */ 54 | #ifdef __aarch64__ 55 | #define PSCI_FNID_VERSION 0x84000000 56 | #define PSCI_FNID_CPU_SUSPEND 0xc4000001 57 | #define PSCI_FNID_CPU_OFF 0x84000002 58 | #define PSCI_FNID_CPU_ON 0xc4000003 59 | #define PSCI_FNID_AFFINITY_INFO 0xc4000004 60 | #define PSCI_FNID_MIGRATE 0xc4000005 61 | #define PSCI_FNID_MIGRATE_INFO_TYPE 0x84000006 62 | #define PSCI_FNID_MIGRATE_INFO_UP_CPU 0xc4000007 63 | #define PSCI_FNID_SYSTEM_OFF 0x84000008 64 | #define PSCI_FNID_SYSTEM_RESET 0x84000009 65 | #define PSCI_FNID_FEATURES 0x8400000a 66 | #else 67 | #define PSCI_FNID_VERSION 0x84000000 68 | #define PSCI_FNID_CPU_SUSPEND 0x84000001 69 | #define PSCI_FNID_CPU_OFF 0x84000002 70 | #define PSCI_FNID_CPU_ON 0x84000003 71 | #define PSCI_FNID_AFFINITY_INFO 0x84000004 72 | #define PSCI_FNID_MIGRATE 0x84000005 73 | #define PSCI_FNID_MIGRATE_INFO_TYPE 0x84000006 74 | #define PSCI_FNID_MIGRATE_INFO_UP_CPU 0x84000007 75 | #define PSCI_FNID_SYSTEM_OFF 0x84000008 76 | #define PSCI_FNID_SYSTEM_RESET 0x84000009 77 | #define PSCI_FNID_FEATURES 0x8400000a 78 | #endif 79 | 80 | #define PSCI_VER_MAJOR(v) (((v) >> 16) & 0xFF) 81 | #define PSCI_VER_MINOR(v) ((v) & 0xFF) 82 | #define PSCI_VER(maj, min) (((maj) << 16) | (min)) 83 | 84 | #define PSCI_AFFINITY_INFO_ON 0 85 | #define PSCI_AFFINITY_INFO_OFF 1 86 | #define PSCI_AFFINITY_INFO_ON_PENDING 2 87 | 88 | #endif /* _MACHINE_PSCI_H_ */ 89 | -------------------------------------------------------------------------------- /include/tpm_device.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2023 Beckhoff Automation GmbH & Co. KG 5 | * Author: Corvin Köhne 6 | */ 7 | 8 | #pragma once 9 | 10 | #include 11 | 12 | #include "config.h" 13 | 14 | struct tpm_device; 15 | 16 | int tpm_device_create(struct tpm_device **new_dev, struct vmctx *vm_ctx, 17 | nvlist_t *nvl); 18 | void tpm_device_destroy(struct tpm_device *dev); 19 | 20 | int init_tpm(struct vmctx *ctx); 21 | -------------------------------------------------------------------------------- /include/tpm_emul.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2023 Beckhoff Automation GmbH & Co. KG 5 | * Author: Corvin Köhne 6 | */ 7 | 8 | #pragma once 9 | 10 | #include 11 | 12 | #include "config.h" 13 | 14 | struct tpm_device; 15 | 16 | struct tpm_emul { 17 | const char *name; 18 | 19 | int (*init)(void **sc, nvlist_t *nvl); 20 | void (*deinit)(void *sc); 21 | int (*execute_cmd)(void *sc, void *cmd, uint32_t cmd_size, void *rsp, 22 | uint32_t rsp_size); 23 | }; 24 | #define TPM_EMUL_SET(x) DATA_SET(tpm_emul_set, x) 25 | -------------------------------------------------------------------------------- /include/tpm_intf.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2022 Beckhoff Automation GmbH & Co. KG 5 | * Author: Corvin Köhne 6 | */ 7 | 8 | #pragma once 9 | 10 | #include 11 | 12 | #include "acpi_device.h" 13 | #include "config.h" 14 | #include "tpm_device.h" 15 | #include "tpm_emul.h" 16 | 17 | #define TPM_INTF_TYPE_FIFO_PTP 0x0 18 | #define TPM_INTF_TYPE_CRB 0x1 19 | #define TPM_INTF_TYPE_FIFO_TIS 0xF 20 | 21 | #define TPM_INTF_VERSION_FIFO 0 22 | #define TPM_INTF_VERSION_CRB 1 23 | 24 | #define TPM_INTF_CAP_CRB_DATA_XFER_SIZE_4 0 25 | #define TPM_INTF_CAP_CRB_DATA_XFER_SIZE_8 1 26 | #define TPM_INTF_CAP_CRB_DATA_XFER_SIZE_32 2 27 | #define TPM_INTF_CAP_CRB_DATA_XFER_SIZE_64 3 28 | 29 | #define TPM_INTF_SELECTOR_FIFO 0 30 | #define TPM_INTF_SELECTOR_CRB 1 31 | 32 | struct tpm_intf { 33 | const char *name; 34 | 35 | int (*init)(void **sc, struct tpm_emul *emul, void *emul_sc, 36 | struct acpi_device *acpi_dev); 37 | void (*deinit)(void *sc); 38 | int (*build_acpi_table)(void *sc, struct vmctx *vm_ctx); 39 | }; 40 | #define TPM_INTF_SET(x) DATA_SET(tpm_intf_set, x) 41 | -------------------------------------------------------------------------------- /include/tpm_ppi.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 | * 4 | * Copyright (c) 2022 Beckhoff Automation GmbH & Co. KG 5 | * Author: Corvin Köhne 6 | */ 7 | 8 | #pragma once 9 | 10 | #include "config.h" 11 | 12 | struct tpm_ppi { 13 | const char *name; 14 | 15 | int (*init)(void **sc); 16 | void (*deinit)(void *sc); 17 | int (*write_dsdt_regions)(void *sc); 18 | int (*write_dsdt_dsm)(void *sc); 19 | }; 20 | #define TPM_PPI_SET(x) DATA_SET(tpm_ppi_set, x) 21 | -------------------------------------------------------------------------------- /include/uart_backend.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2012 NetApp, Inc. 5 | * Copyright (c) 2013 Neel Natu 6 | * Copyright (c) 2025 Alex Fishman 7 | * All rights reserved. 8 | * 9 | * Redistribution and use in source and binary forms, with or without 10 | * modification, are permitted provided that the following conditions 11 | * are met: 12 | * 1. Redistributions of source code must retain the above copyright 13 | * notice, this list of conditions and the following disclaimer. 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND 19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 | * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 | * SUCH DAMAGE. 29 | */ 30 | 31 | #ifndef _UART_BACKEND_H_ 32 | #define _UART_BACKEND_H_ 33 | 34 | #include 35 | 36 | #include "mevent.h" 37 | 38 | struct uart_softc; 39 | struct vm_snapshot_meta; 40 | 41 | void uart_rxfifo_drain(struct uart_softc *sc, bool loopback); 42 | int uart_rxfifo_getchar(struct uart_softc *sc); 43 | int uart_rxfifo_numchars(struct uart_softc *sc); 44 | int uart_rxfifo_putchar(struct uart_softc *sc, uint8_t ch, bool loopback); 45 | void uart_rxfifo_reset(struct uart_softc *sc, int size); 46 | int uart_rxfifo_size(struct uart_softc *sc); 47 | #ifdef BHYVE_SNAPSHOT 48 | int uart_rxfifo_snapshot(struct uart_softc *sc, 49 | struct vm_snapshot_meta *meta); 50 | #endif 51 | 52 | struct uart_softc *uart_init(void); 53 | int uart_tty_open(struct uart_softc *sc, const char *path, 54 | void (*drain)(int, enum ev_type, void *), void *arg); 55 | void uart_softc_lock(struct uart_softc *sc); 56 | void uart_softc_unlock(struct uart_softc *sc); 57 | #endif /* _UART_BACKEND_H_ */ 58 | -------------------------------------------------------------------------------- /include/uart_emul.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2013 Neel Natu 5 | * Copyright (c) 2025 Alex Fishman 6 | * All rights reserved. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions 10 | * are met: 11 | * 1. Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * 2. Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in the 15 | * documentation and/or other materials provided with the distribution. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 | * SUCH DAMAGE. 28 | */ 29 | 30 | #ifndef _UART_EMUL_H_ 31 | #define _UART_EMUL_H_ 32 | 33 | #define UART_NS16550_IO_BAR_SIZE 8 34 | 35 | struct uart_ns16550_softc; 36 | struct uart_pl011_softc; 37 | struct vm_snapshot_meta; 38 | 39 | typedef void (*uart_intr_func_t)(void *arg); 40 | 41 | int uart_legacy_alloc(int unit, int *ioaddr, int *irq); 42 | 43 | struct uart_ns16550_softc *uart_ns16550_init(uart_intr_func_t intr_assert, 44 | uart_intr_func_t intr_deassert, void *arg); 45 | uint8_t uart_ns16550_read(struct uart_ns16550_softc *sc, int offset); 46 | void uart_ns16550_write(struct uart_ns16550_softc *sc, int offset, 47 | uint8_t value); 48 | int uart_ns16550_tty_open(struct uart_ns16550_softc *sc, 49 | const char *device); 50 | #ifdef BHYVE_SNAPSHOT 51 | int uart_ns16550_snapshot(struct uart_ns16550_softc *sc, 52 | struct vm_snapshot_meta *meta); 53 | #endif 54 | 55 | uint32_t uart_pl011_read(struct uart_pl011_softc *sc, int offset); 56 | void uart_pl011_write(struct uart_pl011_softc *sc, int offset, 57 | uint32_t value); 58 | struct uart_pl011_softc *uart_pl011_init(uart_intr_func_t intr_assert, 59 | uart_intr_func_t intr_deassert, void *arg); 60 | int uart_pl011_tty_open(struct uart_pl011_softc *sc, const char *device); 61 | 62 | #endif /* _UART_EMUL_H_ */ 63 | -------------------------------------------------------------------------------- /include/usb_endian.h: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2025 Alex Fishman 5 | * Copyright (c) 2008 Hans Petter Selasky. All rights reserved. 6 | * 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. 15 | * 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``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. 27 | */ 28 | 29 | #ifndef _USB_ENDIAN_H_ 30 | #define _USB_ENDIAN_H_ 31 | 32 | #include 33 | #include 34 | 35 | /* 36 | * Declare the basic USB record types. USB records have an alignment 37 | * of 1 byte and are always packed. 38 | */ 39 | typedef uint8_t uByte; 40 | typedef uint8_t uWord[2]; 41 | typedef uint8_t uDWord[4]; 42 | typedef uint8_t uQWord[8]; 43 | 44 | /* 45 | * Define a set of macros that can get and set data independent of 46 | * CPU endianness and CPU alignment requirements: 47 | */ 48 | #define UGETB(w) \ 49 | ((w)[0]) 50 | 51 | #define UGETW(w) \ 52 | ((w)[0] | \ 53 | (((uint16_t)((w)[1])) << 8)) 54 | 55 | #define UGETDW(w) \ 56 | ((w)[0] | \ 57 | (((uint16_t)((w)[1])) << 8) | \ 58 | (((uint32_t)((w)[2])) << 16) | \ 59 | (((uint32_t)((w)[3])) << 24)) 60 | 61 | #define UGETQW(w) \ 62 | ((w)[0] | \ 63 | (((uint16_t)((w)[1])) << 8) | \ 64 | (((uint32_t)((w)[2])) << 16) | \ 65 | (((uint32_t)((w)[3])) << 24) | \ 66 | (((uint64_t)((w)[4])) << 32) | \ 67 | (((uint64_t)((w)[5])) << 40) | \ 68 | (((uint64_t)((w)[6])) << 48) | \ 69 | (((uint64_t)((w)[7])) << 56)) 70 | 71 | #define USETB(w,v) do { \ 72 | (w)[0] = (uint8_t)(v); \ 73 | } while (0) 74 | 75 | #define USETW(w,v) do { \ 76 | (w)[0] = (uint8_t)(v); \ 77 | (w)[1] = (uint8_t)((v) >> 8); \ 78 | } while (0) 79 | 80 | #define USETDW(w,v) do { \ 81 | (w)[0] = (uint8_t)(v); \ 82 | (w)[1] = (uint8_t)((v) >> 8); \ 83 | (w)[2] = (uint8_t)((v) >> 16); \ 84 | (w)[3] = (uint8_t)((v) >> 24); \ 85 | } while (0) 86 | 87 | #define USETQW(w,v) do { \ 88 | (w)[0] = (uint8_t)(v); \ 89 | (w)[1] = (uint8_t)((v) >> 8); \ 90 | (w)[2] = (uint8_t)((v) >> 16); \ 91 | (w)[3] = (uint8_t)((v) >> 24); \ 92 | (w)[4] = (uint8_t)((v) >> 32); \ 93 | (w)[5] = (uint8_t)((v) >> 40); \ 94 | (w)[6] = (uint8_t)((v) >> 48); \ 95 | (w)[7] = (uint8_t)((v) >> 56); \ 96 | } while (0) 97 | 98 | #define USETW2(w,b1,b0) do { \ 99 | (w)[0] = (uint8_t)(b0); \ 100 | (w)[1] = (uint8_t)(b1); \ 101 | } while (0) 102 | 103 | #define USETW4(w,b3,b2,b1,b0) do { \ 104 | (w)[0] = (uint8_t)(b0); \ 105 | (w)[1] = (uint8_t)(b1); \ 106 | (w)[2] = (uint8_t)(b2); \ 107 | (w)[3] = (uint8_t)(b3); \ 108 | } while (0) 109 | 110 | #define USETW8(w,b7,b6,b5,b4,b3,b2,b1,b0) do { \ 111 | (w)[0] = (uint8_t)(b0); \ 112 | (w)[1] = (uint8_t)(b1); \ 113 | (w)[2] = (uint8_t)(b2); \ 114 | (w)[3] = (uint8_t)(b3); \ 115 | (w)[4] = (uint8_t)(b4); \ 116 | (w)[5] = (uint8_t)(b5); \ 117 | (w)[6] = (uint8_t)(b6); \ 118 | (w)[7] = (uint8_t)(b7); \ 119 | } while (0) 120 | 121 | #endif /* _USB_ENDIAN_H_ */ 122 | -------------------------------------------------------------------------------- /include/virtio/virtio_endian.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2017, Bryan Venteicher 5 | * Copyright (c) 2025 Alex Fishman 6 | * All rights reserved. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions 10 | * are met: 11 | * 1. Redistributions of source code must retain the above copyright 12 | * notice unmodified, this list of conditions, and the following 13 | * disclaimer. 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | 30 | #ifndef _VIRTIO_ENDIAN_H_ 31 | #define _VIRTIO_ENDIAN_H_ 32 | 33 | #include 34 | #include 35 | 36 | /* 37 | * VirtIO V1 (modern) uses little endian, while legacy VirtIO uses the guest's 38 | * native endian. These functions convert to and from the Guest's (driver's) 39 | * and the Host's (device's) endianness when needed. 40 | */ 41 | 42 | static inline uint16_t 43 | virtio_htog16(bool modern, uint16_t val) 44 | { 45 | if (modern) 46 | return (le16toh(val)); 47 | else 48 | return (val); 49 | } 50 | 51 | static inline uint16_t 52 | virtio_gtoh16(bool modern, uint16_t val) 53 | { 54 | if (modern) 55 | return (htole16(val)); 56 | else 57 | return (val); 58 | } 59 | 60 | static inline uint32_t 61 | virtio_htog32(bool modern, uint32_t val) 62 | { 63 | if (modern) 64 | return (le32toh(val)); 65 | else 66 | return (val); 67 | } 68 | 69 | static inline uint32_t 70 | virtio_gtoh32(bool modern, uint32_t val) 71 | { 72 | if (modern) 73 | return (htole32(val)); 74 | else 75 | return (val); 76 | } 77 | 78 | static inline uint64_t 79 | virtio_htog64(bool modern, uint64_t val) 80 | { 81 | if (modern) 82 | return (le64toh(val)); 83 | else 84 | return (val); 85 | } 86 | 87 | static inline uint64_t 88 | virtio_gtoh64(bool modern, uint64_t val) 89 | { 90 | if (modern) 91 | return (htole64(val)); 92 | else 93 | return (val); 94 | } 95 | 96 | #endif /* _VIRTIO_ENDIAN_H_ */ 97 | -------------------------------------------------------------------------------- /include/virtio/virtio_ids.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2025 Alex Fishman 5 | * All rights reserved. 6 | * 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. 15 | * 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. 27 | */ 28 | 29 | /*- 30 | * This header is BSD licensed so anyone can use the definitions to implement 31 | * compatible drivers/servers. 32 | * 33 | * Redistribution and use in source and binary forms, with or without 34 | * modification, are permitted provided that the following conditions 35 | * are met: 36 | * 1. Redistributions of source code must retain the above copyright 37 | * notice, this list of conditions and the following disclaimer. 38 | * 2. Redistributions in binary form must reproduce the above copyright 39 | * notice, this list of conditions and the following disclaimer in the 40 | * documentation and/or other materials provided with the distribution. 41 | * 3. Neither the name of IBM nor the names of its contributors 42 | * may be used to endorse or promote products derived from this software 43 | * without specific prior written permission. 44 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 45 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 46 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 47 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL IBM OR CONTRIBUTORS BE LIABLE 48 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 49 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 50 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 51 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 52 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 53 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 54 | * SUCH DAMAGE. 55 | */ 56 | 57 | #ifndef _VIRTIO_IDS_H_ 58 | #define _VIRTIO_IDS_H_ 59 | 60 | /* VirtIO device IDs. */ 61 | #define VIRTIO_ID_NETWORK 1 62 | #define VIRTIO_ID_BLOCK 2 63 | #define VIRTIO_ID_CONSOLE 3 64 | #define VIRTIO_ID_ENTROPY 4 65 | #define VIRTIO_ID_BALLOON 5 66 | #define VIRTIO_ID_IOMEMORY 6 67 | #define VIRTIO_ID_RPMSG 7 68 | #define VIRTIO_ID_SCSI 8 69 | #define VIRTIO_ID_9P 9 70 | #define VIRTIO_ID_RPROC_SERIAL 11 71 | #define VIRTIO_ID_CAIF 12 72 | #define VIRTIO_ID_GPU 16 73 | #define VIRTIO_ID_INPUT 18 74 | #define VIRTIO_ID_VSOCK 19 75 | #define VIRTIO_ID_CRYPTO 20 76 | #define VIRTIO_ID_IOMMU 23 77 | #define VIRTIO_ID_SOUND 25 78 | #define VIRTIO_ID_FS 26 79 | #define VIRTIO_ID_PMEM 27 80 | #define VIRTIO_ID_RPMB 28 81 | #define VIRTIO_ID_SCMI 32 82 | #define VIRTIO_ID_GPIO 41 83 | 84 | #endif /* _VIRTIO_IDS_H_ */ 85 | -------------------------------------------------------------------------------- /include/virtio/virtio_pci_legacy_var.h: -------------------------------------------------------------------------------- 1 | 2 | /*- 3 | * SPDX-License-Identifier: BSD-3-Clause 4 | * 5 | * Copyright IBM Corp. 2007 6 | * 7 | * Authors: 8 | * Anthony Liguori 9 | * 10 | * This header is BSD licensed so anyone can use the definitions to implement 11 | * compatible drivers/servers. 12 | * 13 | * Redistribution and use in source and binary forms, with or without 14 | * modification, are permitted provided that the following conditions 15 | * are met: 16 | * 1. Redistributions of source code must retain the above copyright 17 | * notice, this list of conditions and the following disclaimer. 18 | * 2. Redistributions in binary form must reproduce the above copyright 19 | * notice, this list of conditions and the following disclaimer in the 20 | * documentation and/or other materials provided with the distribution. 21 | * 3. Neither the name of IBM nor the names of its contributors 22 | * may be used to endorse or promote products derived from this software 23 | * without specific prior written permission. 24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 25 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 26 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 27 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL IBM OR CONTRIBUTORS BE LIABLE 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 | * SUCH DAMAGE. 35 | */ 36 | 37 | #ifndef _VIRTIO_PCI_LEGACY_VAR_H 38 | #define _VIRTIO_PCI_LEGACY_VAR_H 39 | 40 | #include "virtio_pci_var.h" 41 | 42 | /* VirtIO ABI version, this must match exactly. */ 43 | #define VIRTIO_PCI_ABI_VERSION 0 44 | 45 | /* 46 | * VirtIO Header, located in BAR 0. 47 | */ 48 | #define VIRTIO_PCI_HOST_FEATURES 0 /* host's supported features (32bit, RO)*/ 49 | #define VIRTIO_PCI_GUEST_FEATURES 4 /* guest's supported features (32, RW) */ 50 | #define VIRTIO_PCI_QUEUE_PFN 8 /* physical address of VQ (32, RW) */ 51 | #define VIRTIO_PCI_QUEUE_NUM 12 /* number of ring entries (16, RO) */ 52 | #define VIRTIO_PCI_QUEUE_SEL 14 /* current VQ selection (16, RW) */ 53 | #define VIRTIO_PCI_QUEUE_NOTIFY 16 /* notify host regarding VQ (16, RW) */ 54 | #define VIRTIO_PCI_STATUS 18 /* device status register (8, RW) */ 55 | #define VIRTIO_PCI_ISR 19 /* interrupt status register, reading 56 | * also clears the register (8, RO) */ 57 | /* Only if MSIX is enabled: */ 58 | #define VIRTIO_MSI_CONFIG_VECTOR 20 /* configuration change vector (16, RW) */ 59 | #define VIRTIO_MSI_QUEUE_VECTOR 22 /* vector for selected VQ notifications 60 | (16, RW) */ 61 | 62 | /* 63 | * The remaining space is defined by each driver as the per-driver 64 | * configuration space. 65 | */ 66 | #define VIRTIO_PCI_CONFIG_OFF(msix_enabled) ((msix_enabled) ? 24 : 20) 67 | 68 | /* 69 | * How many bits to shift physical queue address written to QUEUE_PFN. 70 | * 12 is historical, and due to x86 page size. 71 | */ 72 | #define VIRTIO_PCI_QUEUE_ADDR_SHIFT 12 73 | 74 | /* The alignment to use between consumer and producer parts of vring. */ 75 | #define VIRTIO_PCI_VRING_ALIGN 4096 76 | 77 | #endif /* _VIRTIO_PCI_LEGACY_VAR_H */ 78 | 79 | -------------------------------------------------------------------------------- /include/virtio/virtio_pci_var.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-3-Clause 3 | * 4 | * Copyright IBM Corp. 2007 5 | * 6 | * Authors: 7 | * Anthony Liguori 8 | * 9 | * This header is BSD licensed so anyone can use the definitions to implement 10 | * compatible drivers/servers. 11 | * 12 | * Redistribution and use in source and binary forms, with or without 13 | * modification, are permitted provided that the following conditions 14 | * are met: 15 | * 1. Redistributions of source code must retain the above copyright 16 | * notice, this list of conditions and the following disclaimer. 17 | * 2. Redistributions in binary form must reproduce the above copyright 18 | * notice, this list of conditions and the following disclaimer in the 19 | * documentation and/or other materials provided with the distribution. 20 | * 3. Neither the name of IBM nor the names of its contributors 21 | * may be used to endorse or promote products derived from this software 22 | * without specific prior written permission. 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 25 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 26 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL IBM OR CONTRIBUTORS BE LIABLE 27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 | * SUCH DAMAGE. 34 | */ 35 | 36 | #ifndef _VIRTIO_PCI_VAR_H 37 | #define _VIRTIO_PCI_VAR_H 38 | 39 | /* VirtIO PCI vendor/device ID. */ 40 | #define VIRTIO_PCI_VENDORID 0x1AF4 41 | #define VIRTIO_PCI_DEVICEID_MIN 0x1000 42 | #define VIRTIO_PCI_DEVICEID_LEGACY_MAX 0x103F 43 | #define VIRTIO_PCI_DEVICEID_MODERN_MIN 0x1040 44 | #define VIRTIO_PCI_DEVICEID_MODERN_MAX 0x107F 45 | 46 | /* The bit of the ISR which indicates a device has an interrupt. */ 47 | #define VIRTIO_PCI_ISR_INTR 0x1 48 | /* The bit of the ISR which indicates a device configuration change. */ 49 | #define VIRTIO_PCI_ISR_CONFIG 0x2 50 | /* Vector value used to disable MSI for queue. */ 51 | #define VIRTIO_MSI_NO_VECTOR 0xFFFF 52 | 53 | #endif /* _VIRTIO_PCI_VAR_H */ 54 | -------------------------------------------------------------------------------- /include/vmexit.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2011 NetApp, Inc. 5 | * Copyright (c) 2025 Alex Fishman 6 | * All rights reserved. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions 10 | * are met: 11 | * 1. Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * 2. Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in the 15 | * documentation and/or other materials provided with the distribution. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 | * SUCH DAMAGE. 28 | */ 29 | 30 | #ifndef _VMEXIT_H_ 31 | #define _VMEXIT_H_ 32 | 33 | extern const vmexit_handler_t vmexit_handlers[VM_EXITCODE_MAX]; 34 | 35 | #endif /* !_VMEXIT_H_ */ 36 | -------------------------------------------------------------------------------- /include/vmgenc.h: -------------------------------------------------------------------------------- 1 | 2 | /*- 3 | * SPDX-License-Identifier: BSD-2-Clause 4 | * 5 | * Copyright (c) 2025 Alex Fishman 6 | * Copyright 2020 Conrad Meyer . All rights reserved. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions 10 | * are met: 11 | * 1. Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * 2. Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in the 15 | * documentation and/or other materials provided with the distribution. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 | * SUCH DAMAGE. 28 | */ 29 | #pragma once 30 | void vmgenc_init(struct vmctx *); 31 | void vmgenc_write_dsdt(void); 32 | 33 | -------------------------------------------------------------------------------- /include/vmm.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2025 Alex Fishman 5 | * All rights reserved. 6 | * 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. 15 | * 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. 27 | */ 28 | 29 | #pragma once 30 | 31 | #if defined(__aarch64__) 32 | #include "arch/arm64/vmm_dev.h" 33 | #include "arch/arm64/vmm.h" 34 | #elif defined(__x86_64__) 35 | #include "arch/x86/vmm.h" 36 | #else 37 | #error "Unsupported architecture" 38 | #endif 39 | -------------------------------------------------------------------------------- /include/vmm_instruction_emul.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2025 Alex Fishman 5 | * All rights reserved. 6 | * 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. 15 | * 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. 27 | */ 28 | 29 | #pragma once 30 | 31 | #if defined(__aarch64__) 32 | #include "arch/arm64/vmm_instruction_emul.h" 33 | #elif defined(__x86_64__) 34 | #include "arch/x86/vmm_instruction_emul.h" 35 | #else 36 | #error "Unsupported architecture" 37 | #endif 38 | -------------------------------------------------------------------------------- /libfdt/fdt.h: -------------------------------------------------------------------------------- 1 | #ifndef FDT_H 2 | #define FDT_H 3 | /* 4 | * libfdt - Flat Device Tree manipulation 5 | * Copyright (C) 2006 David Gibson, IBM Corporation. 6 | * Copyright 2012 Kim Phillips, Freescale Semiconductor. 7 | * 8 | * libfdt is dual licensed: you can use it either under the terms of 9 | * the GPL, or the BSD license, at your option. 10 | * 11 | * a) This library is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU General Public License as 13 | * published by the Free Software Foundation; either version 2 of the 14 | * License, or (at your option) any later version. 15 | * 16 | * This library is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 | * GNU General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU General Public 22 | * License along with this library; if not, write to the Free 23 | * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, 24 | * MA 02110-1301 USA 25 | * 26 | * Alternatively, 27 | * 28 | * b) Redistribution and use in source and binary forms, with or 29 | * without modification, are permitted provided that the following 30 | * conditions are met: 31 | * 32 | * 1. Redistributions of source code must retain the above 33 | * copyright notice, this list of conditions and the following 34 | * disclaimer. 35 | * 2. Redistributions in binary form must reproduce the above 36 | * copyright notice, this list of conditions and the following 37 | * disclaimer in the documentation and/or other materials 38 | * provided with the distribution. 39 | * 40 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 41 | * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 42 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 43 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 44 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 45 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 46 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 47 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 48 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 49 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 50 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 51 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 52 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 53 | */ 54 | 55 | #ifndef __ASSEMBLY__ 56 | 57 | struct fdt_header { 58 | fdt32_t magic; /* magic word FDT_MAGIC */ 59 | fdt32_t totalsize; /* total size of DT block */ 60 | fdt32_t off_dt_struct; /* offset to structure */ 61 | fdt32_t off_dt_strings; /* offset to strings */ 62 | fdt32_t off_mem_rsvmap; /* offset to memory reserve map */ 63 | fdt32_t version; /* format version */ 64 | fdt32_t last_comp_version; /* last compatible version */ 65 | 66 | /* version 2 fields below */ 67 | fdt32_t boot_cpuid_phys; /* Which physical CPU id we're 68 | booting on */ 69 | /* version 3 fields below */ 70 | fdt32_t size_dt_strings; /* size of the strings block */ 71 | 72 | /* version 17 fields below */ 73 | fdt32_t size_dt_struct; /* size of the structure block */ 74 | }; 75 | 76 | struct fdt_reserve_entry { 77 | fdt64_t address; 78 | fdt64_t size; 79 | }; 80 | 81 | struct fdt_node_header { 82 | fdt32_t tag; 83 | char name[0]; 84 | }; 85 | 86 | struct fdt_property { 87 | fdt32_t tag; 88 | fdt32_t len; 89 | fdt32_t nameoff; 90 | char data[0]; 91 | }; 92 | 93 | #endif /* !__ASSEMBLY */ 94 | 95 | #define FDT_MAGIC 0xd00dfeed /* 4: version, 4: total size */ 96 | #define FDT_TAGSIZE sizeof(fdt32_t) 97 | 98 | #define FDT_BEGIN_NODE 0x1 /* Start node: full name */ 99 | #define FDT_END_NODE 0x2 /* End node */ 100 | #define FDT_PROP 0x3 /* Property: name off, 101 | size, content */ 102 | #define FDT_NOP 0x4 /* nop */ 103 | #define FDT_END 0x9 104 | 105 | #define FDT_V1_SIZE (7*sizeof(fdt32_t)) 106 | #define FDT_V2_SIZE (FDT_V1_SIZE + sizeof(fdt32_t)) 107 | #define FDT_V3_SIZE (FDT_V2_SIZE + sizeof(fdt32_t)) 108 | #define FDT_V16_SIZE FDT_V3_SIZE 109 | #define FDT_V17_SIZE (FDT_V16_SIZE + sizeof(fdt32_t)) 110 | 111 | #endif /* FDT_H */ 112 | -------------------------------------------------------------------------------- /libfdt/fdt_addresses.c: -------------------------------------------------------------------------------- 1 | /* 2 | * libfdt - Flat Device Tree manipulation 3 | * Copyright (C) 2014 David Gibson 4 | * 5 | * libfdt is dual licensed: you can use it either under the terms of 6 | * the GPL, or the BSD license, at your option. 7 | * 8 | * a) This library is free software; you can redistribute it and/or 9 | * modify it under the terms of the GNU General Public License as 10 | * published by the Free Software Foundation; either version 2 of the 11 | * License, or (at your option) any later version. 12 | * 13 | * This library is distributed in the hope that it will be useful, 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | * GNU General Public License for more details. 17 | * 18 | * You should have received a copy of the GNU General Public 19 | * License along with this library; if not, write to the Free 20 | * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, 21 | * MA 02110-1301 USA 22 | * 23 | * Alternatively, 24 | * 25 | * b) Redistribution and use in source and binary forms, with or 26 | * without modification, are permitted provided that the following 27 | * conditions are met: 28 | * 29 | * 1. Redistributions of source code must retain the above 30 | * copyright notice, this list of conditions and the following 31 | * disclaimer. 32 | * 2. Redistributions in binary form must reproduce the above 33 | * copyright notice, this list of conditions and the following 34 | * disclaimer in the documentation and/or other materials 35 | * provided with the distribution. 36 | * 37 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 38 | * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 39 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 40 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 41 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 42 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 43 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 44 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 45 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 46 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 47 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 48 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 49 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 50 | */ 51 | #include "libfdt_env.h" 52 | 53 | #include 54 | #include 55 | 56 | #include "libfdt_internal.h" 57 | 58 | int fdt_address_cells(const void *fdt, int nodeoffset) 59 | { 60 | const fdt32_t *ac; 61 | int val; 62 | int len; 63 | 64 | ac = fdt_getprop(fdt, nodeoffset, "#address-cells", &len); 65 | if (!ac) 66 | return 2; 67 | 68 | if (len != sizeof(*ac)) 69 | return -FDT_ERR_BADNCELLS; 70 | 71 | val = fdt32_to_cpu(*ac); 72 | if ((val <= 0) || (val > FDT_MAX_NCELLS)) 73 | return -FDT_ERR_BADNCELLS; 74 | 75 | return val; 76 | } 77 | 78 | int fdt_size_cells(const void *fdt, int nodeoffset) 79 | { 80 | const fdt32_t *sc; 81 | int val; 82 | int len; 83 | 84 | sc = fdt_getprop(fdt, nodeoffset, "#size-cells", &len); 85 | if (!sc) 86 | return 2; 87 | 88 | if (len != sizeof(*sc)) 89 | return -FDT_ERR_BADNCELLS; 90 | 91 | val = fdt32_to_cpu(*sc); 92 | if ((val < 0) || (val > FDT_MAX_NCELLS)) 93 | return -FDT_ERR_BADNCELLS; 94 | 95 | return val; 96 | } 97 | -------------------------------------------------------------------------------- /libfdt/fdt_empty_tree.c: -------------------------------------------------------------------------------- 1 | /* 2 | * libfdt - Flat Device Tree manipulation 3 | * Copyright (C) 2012 David Gibson, IBM Corporation. 4 | * 5 | * libfdt is dual licensed: you can use it either under the terms of 6 | * the GPL, or the BSD license, at your option. 7 | * 8 | * a) This library is free software; you can redistribute it and/or 9 | * modify it under the terms of the GNU General Public License as 10 | * published by the Free Software Foundation; either version 2 of the 11 | * License, or (at your option) any later version. 12 | * 13 | * This library is distributed in the hope that it will be useful, 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | * GNU General Public License for more details. 17 | * 18 | * You should have received a copy of the GNU General Public 19 | * License along with this library; if not, write to the Free 20 | * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, 21 | * MA 02110-1301 USA 22 | * 23 | * Alternatively, 24 | * 25 | * b) Redistribution and use in source and binary forms, with or 26 | * without modification, are permitted provided that the following 27 | * conditions are met: 28 | * 29 | * 1. Redistributions of source code must retain the above 30 | * copyright notice, this list of conditions and the following 31 | * disclaimer. 32 | * 2. Redistributions in binary form must reproduce the above 33 | * copyright notice, this list of conditions and the following 34 | * disclaimer in the documentation and/or other materials 35 | * provided with the distribution. 36 | * 37 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 38 | * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 39 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 40 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 41 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 42 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 43 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 44 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 45 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 46 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 47 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 48 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 49 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 50 | */ 51 | #include "libfdt_env.h" 52 | 53 | #include 54 | #include 55 | 56 | #include "libfdt_internal.h" 57 | 58 | int fdt_create_empty_tree(void *buf, int bufsize) 59 | { 60 | int err; 61 | 62 | err = fdt_create(buf, bufsize); 63 | if (err) 64 | return err; 65 | 66 | err = fdt_finish_reservemap(buf); 67 | if (err) 68 | return err; 69 | 70 | err = fdt_begin_node(buf, ""); 71 | if (err) 72 | return err; 73 | 74 | err = fdt_end_node(buf); 75 | if (err) 76 | return err; 77 | 78 | err = fdt_finish(buf); 79 | if (err) 80 | return err; 81 | 82 | return fdt_open_into(buf, buf, bufsize); 83 | } 84 | -------------------------------------------------------------------------------- /libfdt/fdt_strerror.c: -------------------------------------------------------------------------------- 1 | /* 2 | * libfdt - Flat Device Tree manipulation 3 | * Copyright (C) 2006 David Gibson, IBM Corporation. 4 | * 5 | * libfdt is dual licensed: you can use it either under the terms of 6 | * the GPL, or the BSD license, at your option. 7 | * 8 | * a) This library is free software; you can redistribute it and/or 9 | * modify it under the terms of the GNU General Public License as 10 | * published by the Free Software Foundation; either version 2 of the 11 | * License, or (at your option) any later version. 12 | * 13 | * This library is distributed in the hope that it will be useful, 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | * GNU General Public License for more details. 17 | * 18 | * You should have received a copy of the GNU General Public 19 | * License along with this library; if not, write to the Free 20 | * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, 21 | * MA 02110-1301 USA 22 | * 23 | * Alternatively, 24 | * 25 | * b) Redistribution and use in source and binary forms, with or 26 | * without modification, are permitted provided that the following 27 | * conditions are met: 28 | * 29 | * 1. Redistributions of source code must retain the above 30 | * copyright notice, this list of conditions and the following 31 | * disclaimer. 32 | * 2. Redistributions in binary form must reproduce the above 33 | * copyright notice, this list of conditions and the following 34 | * disclaimer in the documentation and/or other materials 35 | * provided with the distribution. 36 | * 37 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 38 | * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 39 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 40 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 41 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 42 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 43 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 44 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 45 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 46 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 47 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 48 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 49 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 50 | */ 51 | #include "libfdt_env.h" 52 | 53 | #include 54 | #include 55 | 56 | #include "libfdt_internal.h" 57 | 58 | struct fdt_errtabent { 59 | const char *str; 60 | }; 61 | 62 | #define FDT_ERRTABENT(val) \ 63 | [(val)] = { .str = #val, } 64 | 65 | static struct fdt_errtabent fdt_errtable[] = { 66 | FDT_ERRTABENT(FDT_ERR_NOTFOUND), 67 | FDT_ERRTABENT(FDT_ERR_EXISTS), 68 | FDT_ERRTABENT(FDT_ERR_NOSPACE), 69 | 70 | FDT_ERRTABENT(FDT_ERR_BADOFFSET), 71 | FDT_ERRTABENT(FDT_ERR_BADPATH), 72 | FDT_ERRTABENT(FDT_ERR_BADPHANDLE), 73 | FDT_ERRTABENT(FDT_ERR_BADSTATE), 74 | 75 | FDT_ERRTABENT(FDT_ERR_TRUNCATED), 76 | FDT_ERRTABENT(FDT_ERR_BADMAGIC), 77 | FDT_ERRTABENT(FDT_ERR_BADVERSION), 78 | FDT_ERRTABENT(FDT_ERR_BADSTRUCTURE), 79 | FDT_ERRTABENT(FDT_ERR_BADLAYOUT), 80 | FDT_ERRTABENT(FDT_ERR_INTERNAL), 81 | FDT_ERRTABENT(FDT_ERR_BADNCELLS), 82 | FDT_ERRTABENT(FDT_ERR_BADVALUE), 83 | FDT_ERRTABENT(FDT_ERR_BADOVERLAY), 84 | FDT_ERRTABENT(FDT_ERR_NOPHANDLES), 85 | }; 86 | #define FDT_ERRTABSIZE (sizeof(fdt_errtable) / sizeof(fdt_errtable[0])) 87 | 88 | const char *fdt_strerror(int errval) 89 | { 90 | if (errval > 0) 91 | return ""; 92 | else if (errval == 0) 93 | return ""; 94 | else if (errval > -FDT_ERRTABSIZE) { 95 | const char *s = fdt_errtable[-errval].str; 96 | 97 | if (s) 98 | return s; 99 | } 100 | 101 | return ""; 102 | } 103 | -------------------------------------------------------------------------------- /libfdt/fdt_wip.c: -------------------------------------------------------------------------------- 1 | /* 2 | * libfdt - Flat Device Tree manipulation 3 | * Copyright (C) 2006 David Gibson, IBM Corporation. 4 | * 5 | * libfdt is dual licensed: you can use it either under the terms of 6 | * the GPL, or the BSD license, at your option. 7 | * 8 | * a) This library is free software; you can redistribute it and/or 9 | * modify it under the terms of the GNU General Public License as 10 | * published by the Free Software Foundation; either version 2 of the 11 | * License, or (at your option) any later version. 12 | * 13 | * This library is distributed in the hope that it will be useful, 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | * GNU General Public License for more details. 17 | * 18 | * You should have received a copy of the GNU General Public 19 | * License along with this library; if not, write to the Free 20 | * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, 21 | * MA 02110-1301 USA 22 | * 23 | * Alternatively, 24 | * 25 | * b) Redistribution and use in source and binary forms, with or 26 | * without modification, are permitted provided that the following 27 | * conditions are met: 28 | * 29 | * 1. Redistributions of source code must retain the above 30 | * copyright notice, this list of conditions and the following 31 | * disclaimer. 32 | * 2. Redistributions in binary form must reproduce the above 33 | * copyright notice, this list of conditions and the following 34 | * disclaimer in the documentation and/or other materials 35 | * provided with the distribution. 36 | * 37 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 38 | * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 39 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 40 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 41 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 42 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 43 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 44 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 45 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 46 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 47 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 48 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 49 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 50 | */ 51 | #include "libfdt_env.h" 52 | 53 | #include 54 | #include 55 | 56 | #include "libfdt_internal.h" 57 | 58 | int fdt_setprop_inplace_namelen_partial(void *fdt, int nodeoffset, 59 | const char *name, int namelen, 60 | uint32_t idx, const void *val, 61 | int len) 62 | { 63 | void *propval; 64 | int proplen; 65 | 66 | propval = fdt_getprop_namelen_w(fdt, nodeoffset, name, namelen, 67 | &proplen); 68 | if (!propval) 69 | return proplen; 70 | 71 | if (proplen < (len + idx)) 72 | return -FDT_ERR_NOSPACE; 73 | 74 | memcpy((char *)propval + idx, val, len); 75 | return 0; 76 | } 77 | 78 | int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name, 79 | const void *val, int len) 80 | { 81 | const void *propval; 82 | int proplen; 83 | 84 | propval = fdt_getprop(fdt, nodeoffset, name, &proplen); 85 | if (!propval) 86 | return proplen; 87 | 88 | if (proplen != len) 89 | return -FDT_ERR_NOSPACE; 90 | 91 | return fdt_setprop_inplace_namelen_partial(fdt, nodeoffset, name, 92 | strlen(name), 0, 93 | val, len); 94 | } 95 | 96 | static void fdt_nop_region_(void *start, int len) 97 | { 98 | fdt32_t *p; 99 | 100 | for (p = start; (char *)p < ((char *)start + len); p++) 101 | *p = cpu_to_fdt32(FDT_NOP); 102 | } 103 | 104 | int fdt_nop_property(void *fdt, int nodeoffset, const char *name) 105 | { 106 | struct fdt_property *prop; 107 | int len; 108 | 109 | prop = fdt_get_property_w(fdt, nodeoffset, name, &len); 110 | if (!prop) 111 | return len; 112 | 113 | fdt_nop_region_(prop, len + sizeof(*prop)); 114 | 115 | return 0; 116 | } 117 | 118 | int fdt_node_end_offset_(void *fdt, int offset) 119 | { 120 | int depth = 0; 121 | 122 | while ((offset >= 0) && (depth >= 0)) 123 | offset = fdt_next_node(fdt, offset, &depth); 124 | 125 | return offset; 126 | } 127 | 128 | int fdt_nop_node(void *fdt, int nodeoffset) 129 | { 130 | int endoffset; 131 | 132 | endoffset = fdt_node_end_offset_(fdt, nodeoffset); 133 | if (endoffset < 0) 134 | return endoffset; 135 | 136 | fdt_nop_region_(fdt_offset_ptr_w(fdt, nodeoffset, 0), 137 | endoffset - nodeoffset); 138 | return 0; 139 | } 140 | -------------------------------------------------------------------------------- /libfdt/libfdt_internal.h: -------------------------------------------------------------------------------- 1 | #ifndef LIBFDT_INTERNAL_H 2 | #define LIBFDT_INTERNAL_H 3 | /* 4 | * libfdt - Flat Device Tree manipulation 5 | * Copyright (C) 2006 David Gibson, IBM Corporation. 6 | * 7 | * libfdt is dual licensed: you can use it either under the terms of 8 | * the GPL, or the BSD license, at your option. 9 | * 10 | * a) This library is free software; you can redistribute it and/or 11 | * modify it under the terms of the GNU General Public License as 12 | * published by the Free Software Foundation; either version 2 of the 13 | * License, or (at your option) any later version. 14 | * 15 | * This library is distributed in the hope that it will be useful, 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | * GNU General Public License for more details. 19 | * 20 | * You should have received a copy of the GNU General Public 21 | * License along with this library; if not, write to the Free 22 | * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, 23 | * MA 02110-1301 USA 24 | * 25 | * Alternatively, 26 | * 27 | * b) Redistribution and use in source and binary forms, with or 28 | * without modification, are permitted provided that the following 29 | * conditions are met: 30 | * 31 | * 1. Redistributions of source code must retain the above 32 | * copyright notice, this list of conditions and the following 33 | * disclaimer. 34 | * 2. Redistributions in binary form must reproduce the above 35 | * copyright notice, this list of conditions and the following 36 | * disclaimer in the documentation and/or other materials 37 | * provided with the distribution. 38 | * 39 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 40 | * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 41 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 42 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 43 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 44 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 45 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 46 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 47 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 49 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 50 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 51 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 52 | */ 53 | #include 54 | 55 | #define FDT_ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1)) 56 | #define FDT_TAGALIGN(x) (FDT_ALIGN((x), FDT_TAGSIZE)) 57 | 58 | #define FDT_CHECK_HEADER(fdt) \ 59 | { \ 60 | int err_; \ 61 | if ((err_ = fdt_check_header(fdt)) != 0) \ 62 | return err_; \ 63 | } 64 | 65 | int fdt_check_node_offset_(const void *fdt, int offset); 66 | int fdt_check_prop_offset_(const void *fdt, int offset); 67 | const char *fdt_find_string_(const char *strtab, int tabsize, const char *s); 68 | int fdt_node_end_offset_(void *fdt, int nodeoffset); 69 | 70 | static inline const void *fdt_offset_ptr_(const void *fdt, int offset) 71 | { 72 | return (const char *)fdt + fdt_off_dt_struct(fdt) + offset; 73 | } 74 | 75 | static inline void *fdt_offset_ptr_w_(void *fdt, int offset) 76 | { 77 | return (void *)(uintptr_t)fdt_offset_ptr_(fdt, offset); 78 | } 79 | 80 | static inline const struct fdt_reserve_entry *fdt_mem_rsv_(const void *fdt, int n) 81 | { 82 | const struct fdt_reserve_entry *rsv_table = 83 | (const struct fdt_reserve_entry *) 84 | ((const char *)fdt + fdt_off_mem_rsvmap(fdt)); 85 | 86 | return rsv_table + n; 87 | } 88 | static inline struct fdt_reserve_entry *fdt_mem_rsv_w_(void *fdt, int n) 89 | { 90 | return (void *)(uintptr_t)fdt_mem_rsv_(fdt, n); 91 | } 92 | 93 | #define FDT_SW_MAGIC (~FDT_MAGIC) 94 | 95 | #endif /* LIBFDT_INTERNAL_H */ 96 | -------------------------------------------------------------------------------- /libfdt/meson.build: -------------------------------------------------------------------------------- 1 | libfdt_src = files( 2 | 'fdt.c', 3 | 'fdt_empty_tree.c', 4 | 'fdt_ro.c', 5 | 'fdt_strerror.c', 6 | 'fdt_wip.c', 7 | 'fdt_addresses.c', 8 | 'fdt_overlay.c', 9 | 'fdt_rw.c', 10 | 'fdt_sw.c', 11 | ) 12 | 13 | libfdt = static_library( 14 | 'fdt', 15 | libfdt_src, 16 | include_directories: inc 17 | ) 18 | 19 | libfdt_dep = declare_dependency(link_with: libfdt) 20 | 21 | -------------------------------------------------------------------------------- /libnv/meson.build: -------------------------------------------------------------------------------- 1 | libnv_src = files('libnv.c', 'bsd_nvpair.c') 2 | 3 | libnv = static_library( 4 | 'nv', 5 | libnv_src, 6 | include_directories: inc 7 | ) 8 | 9 | libnv_dep = declare_dependency(link_with: libnv) 10 | 11 | -------------------------------------------------------------------------------- /libnv/nvlist_impl.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2013 The FreeBSD Foundation 5 | * Copyright (c) 2013-2015 Mariusz Zaborski 6 | * Copyright (c) 2025 Alex Fishman 7 | * All rights reserved. 8 | * 9 | * This software was developed by Pawel Jakub Dawidek under sponsorship from 10 | * the FreeBSD Foundation. 11 | * 12 | * Redistribution and use in source and binary forms, with or without 13 | * modification, are permitted provided that the following conditions 14 | * are met: 15 | * 1. Redistributions of source code must retain the above copyright 16 | * notice, this list of conditions and the following disclaimer. 17 | * 2. Redistributions in binary form must reproduce the above copyright 18 | * notice, this list of conditions and the following disclaimer in the 19 | * documentation and/or other materials provided with the distribution. 20 | * 21 | * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 | * SUCH DAMAGE. 32 | * 33 | * $FreeBSD$ 34 | */ 35 | 36 | #ifndef _NVLIST_IMPL_H_ 37 | #define _NVLIST_IMPL_H_ 38 | 39 | #include "nv.h" 40 | #include 41 | 42 | void nvlist_report_missing(int type, const char *name); 43 | nvpair_t *nvlist_get_nvpair_parent(const nvlist_t *nvl); 44 | const unsigned char *nvlist_unpack_header(nvlist_t *nvl, 45 | const unsigned char *ptr, size_t nfds, bool *isbep, size_t *leftp); 46 | 47 | #endif /* !_NVLIST_IMPL_H_ */ 48 | -------------------------------------------------------------------------------- /libutil/expand_number.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2007 Eric Anderson 5 | * Copyright (c) 2007 Pawel Jakub Dawidek 6 | * Copyright (c) 2025 Alex Fishman 7 | * All rights reserved. 8 | * 9 | * Redistribution and use in source and binary forms, with or without 10 | * modification, are permitted provided that the following conditions 11 | * are met: 12 | * 1. Redistributions of source code must retain the above copyright 13 | * notice, this list of conditions and the following disclaimer. 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 | * SUCH DAMAGE. 29 | */ 30 | 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | 38 | int 39 | expand_number(const char *buf, uint64_t *num) 40 | { 41 | char *endptr; 42 | uintmax_t umaxval; 43 | uint64_t number; 44 | unsigned shift; 45 | int serrno; 46 | 47 | serrno = errno; 48 | errno = 0; 49 | umaxval = strtoumax(buf, &endptr, 0); 50 | if (umaxval > UINT64_MAX) 51 | errno = ERANGE; 52 | if (errno != 0) 53 | return (-1); 54 | errno = serrno; 55 | number = umaxval; 56 | 57 | switch (tolower((unsigned char)*endptr)) { 58 | case 'e': 59 | shift = 60; 60 | break; 61 | case 'p': 62 | shift = 50; 63 | break; 64 | case 't': 65 | shift = 40; 66 | break; 67 | case 'g': 68 | shift = 30; 69 | break; 70 | case 'm': 71 | shift = 20; 72 | break; 73 | case 'k': 74 | shift = 10; 75 | break; 76 | case 'b': 77 | shift = 0; 78 | break; 79 | case '\0': /* No unit. */ 80 | *num = number; 81 | return (0); 82 | default: 83 | /* Unrecognized unit. */ 84 | errno = EINVAL; 85 | return (-1); 86 | } 87 | 88 | /* 89 | * Treat 'b' as an ignored suffix for all unit except 'b', 90 | * otherwise there should be no remaining character(s). 91 | */ 92 | endptr++; 93 | if (shift != 0 && tolower((unsigned char)*endptr) == 'b') 94 | endptr++; 95 | if (*endptr != '\0') { 96 | errno = EINVAL; 97 | return (-1); 98 | } 99 | 100 | if ((number << shift) >> shift != number) { 101 | /* Overflow */ 102 | errno = ERANGE; 103 | return (-1); 104 | } 105 | *num = number << shift; 106 | return (0); 107 | } 108 | -------------------------------------------------------------------------------- /libutil/hexdump.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-3-Clause 3 | * 4 | * Copyright (c) 1986, 1988, 1991, 1993 5 | * Copyright (c) 2025 Alex Fishman 6 | * The Regents of the University of California. All rights reserved. 7 | * (c) UNIX System Laboratories, Inc. 8 | * All or some portions of this file are derived from material licensed 9 | * to the University of California by American Telephone and Telegraph 10 | * Co. or Unix System Laboratories, Inc. and are reproduced herein with 11 | * the permission of UNIX System Laboratories, Inc. 12 | * 13 | * Redistribution and use in source and binary forms, with or without 14 | * modification, are permitted provided that the following conditions 15 | * are met: 16 | * 1. Redistributions of source code must retain the above copyright 17 | * notice, this list of conditions and the following disclaimer. 18 | * 2. Redistributions in binary form must reproduce the above copyright 19 | * notice, this list of conditions and the following disclaimer in the 20 | * documentation and/or other materials provided with the distribution. 21 | * 3. Neither the name of the University nor the names of its contributors 22 | * may be used to endorse or promote products derived from this software 23 | * without specific prior written permission. 24 | * 25 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 | * SUCH DAMAGE. 36 | */ 37 | 38 | #include 39 | #include 40 | 41 | #define HD_COLUMN_MASK 0xff 42 | #define HD_DELIM_MASK 0xff00 43 | #define HD_OMIT_COUNT (1 << 16) 44 | #define HD_OMIT_HEX (1 << 17) 45 | #define HD_OMIT_CHARS (1 << 18) 46 | 47 | void 48 | hexdump(const void *ptr, int length, const char *hdr, int flags) 49 | { 50 | int i, j, k; 51 | int cols; 52 | const unsigned char *cp; 53 | char delim; 54 | 55 | if ((flags & HD_DELIM_MASK) != 0) 56 | delim = (flags & HD_DELIM_MASK) >> 8; 57 | else 58 | delim = ' '; 59 | 60 | if ((flags & HD_COLUMN_MASK) != 0) 61 | cols = flags & HD_COLUMN_MASK; 62 | else 63 | cols = 16; 64 | 65 | cp = ptr; 66 | for (i = 0; i < length; i += cols) { 67 | if (hdr != NULL) 68 | printf("%s", hdr); 69 | 70 | if ((flags & HD_OMIT_COUNT) == 0) 71 | printf("%04x ", i); 72 | 73 | if ((flags & HD_OMIT_HEX) == 0) { 74 | for (j = 0; j < cols; j++) { 75 | k = i + j; 76 | if (k < length) 77 | printf("%c%02x", delim, cp[k]); 78 | else 79 | printf(" "); 80 | } 81 | } 82 | 83 | if ((flags & HD_OMIT_CHARS) == 0) { 84 | printf(" |"); 85 | for (j = 0; j < cols; j++) { 86 | k = i + j; 87 | if (k >= length) 88 | printf(" "); 89 | else if (cp[k] >= ' ' && cp[k] <= '~') 90 | printf("%c", cp[k]); 91 | else 92 | printf("."); 93 | } 94 | printf("|"); 95 | } 96 | printf("\n"); 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /libutil/meson.build: -------------------------------------------------------------------------------- 1 | libutil_src = files('hexdump.c', 'expand_number.c', 'md5.c') 2 | 3 | libutil = static_library( 4 | 'util', 5 | libutil_src, 6 | include_directories: inc 7 | ) 8 | 9 | libutil_dep = declare_dependency(link_with: libutil) 10 | -------------------------------------------------------------------------------- /meson.build: -------------------------------------------------------------------------------- 1 | project('scorpi', 'c', default_options: ['c_std=c11']) 2 | 3 | add_project_arguments('-Wno-pointer-sign', language: 'c') 4 | 5 | zlib_dep = dependency('zlib', required: true) 6 | slirp_dep = dependency('slirp', required: true, static: false) 7 | libwebsockets_dep = dependency('libwebsockets', required: true) 8 | 9 | codesign_identity = run_command('sh', '-c', 'echo $CODESIGN_IDENTITY').stdout().strip() 10 | 11 | if host_machine.system() == 'darwin' 12 | hv_framework = [ 13 | '-framework', 'Hypervisor', 14 | '-framework', 'VMNet' ] 15 | add_project_link_arguments(hv_framework, language: 'c') 16 | add_project_link_arguments(['-lresolv'], language: 'c' ) 17 | #add_project_link_arguments(['-ld64'], language: 'c' ) 18 | endif 19 | 20 | arch = host_machine.cpu_family() 21 | 22 | inc = include_directories( 23 | 'include', 24 | 'libnv', 25 | 'libfdt', 26 | 'libutil', 27 | 'mjson', 28 | ) 29 | 30 | subdir('libnv') 31 | subdir('libfdt') 32 | subdir('libutil') 33 | subdir('mjson') 34 | subdir('tests') 35 | subdir('util/console') 36 | 37 | sources = files( 38 | 'src/compat.c', 39 | 'src/bootrom.c', 40 | 'src/config.c', 41 | 'src/bhyverun.c', 42 | 'src/mem.c', 43 | 'src/pci_emul.c', 44 | 'src/acpi.c', 45 | 'src/acpi_device.c', 46 | 'src/basl.c', 47 | 'src/tpm_device.c', 48 | 'src/mevent.c', 49 | 'src/qemu_fwcfg.c', 50 | 'src/qemu_loader.c', 51 | 'src/rtc_pl031.c', 52 | 'src/uart_pl011.c', 53 | 'src/uart_backend.c', 54 | 'src/vmgenc.c', 55 | 'src/pci_hostbridge.c', 56 | 'src/pci_ahci.c', 57 | 'src/block_if.c', 58 | 'src/virtio.c', 59 | 'src/pci_virtio_block.c', 60 | 'src/pci_virtio_net.c', 61 | 'src/pci_virtio_rnd.c', 62 | 'src/pci_82545.c', 63 | 'src/net_backends.c', 64 | 'src/rfb.c', 65 | 'src/pci_fbuf.c', 66 | 'src/bhyvegc.c', 67 | 'src/console.c', 68 | 'src/sockstream.c', 69 | 'src/iov.c', 70 | 'src/net_utils.c', 71 | 'src/cnc.c', 72 | 'src/pci_xhci.c', 73 | 'src/usb_emul.c', 74 | 'src/usb_mouse.c', 75 | 'src/usb_kbd.c', 76 | 'src/smbiostbl.c', 77 | 'src/slirp_net_backend.c', 78 | 'src/usb_net.c', 79 | 'src/pci_virtio_gpu.c', 80 | 'src/pci_virtio_console.c', 81 | ) 82 | 83 | # Set architecture-specific source 84 | if arch == 'x86_64' 85 | arch_src = files( 86 | 'src/arch/x86/bhyverun.c' 87 | ) 88 | elif arch == 'aarch64' 89 | arch_src = files( 90 | 'src/arch/arm64/bhyverun.c', 91 | 'src/arch/arm64/fdt.c', 92 | 'src/arch/arm64/pci_irq.c', 93 | 'src/arch/arm64/vmexit.c', 94 | ) 95 | else 96 | error('Unsupported architecture: ' + arch) 97 | endif 98 | 99 | if host_machine.system() == 'darwin' 100 | arch_src += files( 101 | 'src/libvmm/hvf/vmmapi.c', 102 | 'src/libvmm/hvf/vmm.c', 103 | 'src/libvmm/hvf/vgic.c', 104 | 'src/libvmm/hvf/vmm_instruction_emul.c', 105 | 'src/vmnet_net_backend.c', 106 | ) 107 | endif 108 | 109 | sources += arch_src 110 | 111 | scorpi_executable=executable( 112 | 'scorpi', 113 | sources, 114 | dependencies: [libnv_dep, libfdt_dep, libutil_dep, libmjson_dep, zlib_dep, slirp_dep, libwebsockets_dep], 115 | include_directories: inc, 116 | ) 117 | 118 | # Add codesigning step for macOS 119 | if host_machine.system() == 'darwin' 120 | codesign = custom_target( 121 | 'codesign-scorpi', 122 | input: scorpi_executable, 123 | output: 'scorpi-signed', 124 | command: [ 125 | 'codesign', 126 | '--entitlements', meson.project_source_root() + '/scorpi.entitlements', 127 | '--sign', codesign_identity, 128 | '--force', 129 | '@INPUT@' 130 | ], 131 | build_by_default: true 132 | ) 133 | endif 134 | -------------------------------------------------------------------------------- /mjson/meson.build: -------------------------------------------------------------------------------- 1 | libmjson_src = files( 2 | 'mjson.c', 3 | ) 4 | 5 | libmjson = static_library( 6 | 'mjson', 7 | libmjson_src, 8 | include_directories: inc 9 | ) 10 | 11 | libmjson_dep = declare_dependency(link_with: libmjson) 12 | 13 | -------------------------------------------------------------------------------- /run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | IMAGE_URL="https://download.freebsd.org/releases/ISO-IMAGES/14.2/FreeBSD-14.2-RELEASE-arm64-aarch64-bootonly.iso" 4 | IMAGE_NAME="FreeBSD-14.2-RELEASE-arm64-aarch64-bootonly.iso" 5 | 6 | # Check if the image file already exists 7 | if [ -f "$IMAGE_NAME" ]; then 8 | echo "Image file '$IMAGE_NAME' already exists. No need to download." 9 | else 10 | echo "Image file not found. Downloading..." 11 | 12 | if command -v curl &> /dev/null; then 13 | curl -o "$IMAGE_NAME" "$IMAGE_URL" 14 | else 15 | echo "curl is not installed. Please install one to proceed." 16 | exit 1 17 | fi 18 | 19 | echo "Download completed." 20 | fi 21 | 22 | ./builddir/scorpi -s 0,hostbridge -o console=stdio -o bootrom=./firmware/u-boot.bin -s 1,virtio-net,slirp -s 2,virtio-blk,$IMAGE_NAME,ro -m 2G -c 4 vm1 23 | -------------------------------------------------------------------------------- /scorpi.entitlements: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | com.apple.security.hypervisor 5 | 6 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /src/arch/arm64/fdt.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2022 The FreeBSD Foundation 5 | * 6 | * This software was developed by Andrew Turner under sponsorship from 7 | * the FreeBSD Foundation. 8 | * 9 | * Redistribution and use in source and binary forms, with or without 10 | * modification, are permitted provided that the following conditions 11 | * are met: 12 | * 1. Redistributions of source code must retain the above copyright 13 | * notice, this list of conditions and the following disclaimer. 14 | * 2. Redistributions in binary form must reproduce the above copyright 15 | * notice, this list of conditions and the following disclaimer in the 16 | * documentation and/or other materials provided with the distribution. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 | * SUCH DAMAGE. 29 | */ 30 | 31 | #ifndef _FDT_H_ 32 | #define _FDT_H_ 33 | 34 | #include 35 | #include "common.h" 36 | 37 | struct vmctx; 38 | 39 | int fdt_init(struct vmctx *ctx, int ncpu, void *addrp, 40 | vm_size_t size); 41 | void fdt_add_cpus(int ncpu); 42 | void fdt_add_gic(uint64_t dist_base, uint64_t dist_size, 43 | uint64_t redist_base, uint64_t redist_size, 44 | uint64_t mmio_base, int spi_intid_base, int spi_count); 45 | void fdt_add_timer(void); 46 | void fdt_add_pcie(int intrs[static 4], uint64_t iobase, uint64_t iolimit, 47 | uint64_t mmio_base32, uint64_t mmio_base_limit); 48 | void fdt_add_uart(uint64_t uart_base, uint64_t uart_size, int intr); 49 | void fdt_add_rtc(uint64_t rtc_base, uint64_t rtc_size, int intr); 50 | void fdt_add_fwcfg(uint64_t mmio_base, uint64_t mmio_size); 51 | void fdt_add_flash(uint64_t base, uint64_t size); 52 | void fdt_finalize(void); 53 | 54 | #endif /* _FDT_H_ */ 55 | -------------------------------------------------------------------------------- /src/arch/arm64/pci_irq.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2024 Jessica Clarke 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions 8 | * are met: 9 | * 1. Redistributions of source code must retain the above copyright 10 | * notice, this list of conditions and the following disclaimer. 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in the 13 | * documentation and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 | * SUCH DAMAGE. 26 | */ 27 | 28 | #include 29 | 30 | #include "pci_emul.h" 31 | #include "pci_irq.h" 32 | 33 | static int gic_irqs[4]; 34 | 35 | void 36 | pci_irq_init(int intrs[static 4]) 37 | { 38 | int i; 39 | 40 | for (i = 0; i < 4; ++i) 41 | gic_irqs[i] = intrs[i]; 42 | } 43 | 44 | void 45 | pci_irq_assert(struct pci_devinst *pi) 46 | { 47 | vm_assert_irq(pi->pi_vmctx, pi->pi_lintr.irq.gic_irq); 48 | } 49 | 50 | void 51 | pci_irq_deassert(struct pci_devinst *pi) 52 | { 53 | vm_deassert_irq(pi->pi_vmctx, pi->pi_lintr.irq.gic_irq); 54 | } 55 | 56 | void 57 | pci_irq_route(struct pci_devinst *pi, struct pci_irq *irq) 58 | { 59 | /* 60 | * Assign swizzled IRQ for this INTx if one is not yet assigned. Must 61 | * match fdt_add_pcie(). 62 | */ 63 | if (irq->gic_irq == 0) 64 | irq->gic_irq = 65 | gic_irqs[(pi->pi_slot + pi->pi_lintr.pin - 1) % 4]; 66 | } 67 | -------------------------------------------------------------------------------- /src/bhyvegc.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2015 Tycho Nightingale 5 | * Copyright (c) 2025 Alex Fishman 6 | * All rights reserved. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions 10 | * are met: 11 | * 1. Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * 2. Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in the 15 | * documentation and/or other materials provided with the distribution. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 | * SUCH DAMAGE. 28 | */ 29 | 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include 36 | 37 | #include "bhyvegc.h" 38 | 39 | #define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4)) 40 | 41 | void * 42 | reallocarray(void *optr, size_t nmemb, size_t size) 43 | { 44 | if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) && 45 | nmemb > 0 && SIZE_MAX / nmemb < size) { 46 | errno = ENOMEM; 47 | return (NULL); 48 | } 49 | return (realloc(optr, size * nmemb)); 50 | } 51 | 52 | struct bhyvegc { 53 | struct bhyvegc_image *gc_image; 54 | int raw; 55 | }; 56 | 57 | struct bhyvegc * 58 | bhyvegc_init(int width, int height, void *fbaddr) 59 | { 60 | struct bhyvegc *gc; 61 | struct bhyvegc_image *gc_image; 62 | 63 | gc = calloc(1, sizeof(struct bhyvegc)); 64 | 65 | gc_image = calloc(1, sizeof(struct bhyvegc_image)); 66 | gc_image->width = width; 67 | gc_image->height = height; 68 | if (fbaddr) { 69 | gc_image->data = fbaddr; 70 | gc->raw = 1; 71 | } else { 72 | gc_image->data = calloc(width * height, sizeof(uint32_t)); 73 | gc->raw = 0; 74 | } 75 | 76 | gc->gc_image = gc_image; 77 | 78 | return (gc); 79 | } 80 | 81 | void 82 | bhyvegc_set_fbaddr(struct bhyvegc *gc, void *fbaddr) 83 | { 84 | gc->raw = 1; 85 | if (gc->gc_image->data && gc->gc_image->data != fbaddr) 86 | free(gc->gc_image->data); 87 | gc->gc_image->data = fbaddr; 88 | } 89 | 90 | void 91 | bhyvegc_resize(struct bhyvegc *gc, int width, int height) 92 | { 93 | struct bhyvegc_image *gc_image; 94 | 95 | gc_image = gc->gc_image; 96 | 97 | gc_image->width = width; 98 | gc_image->height = height; 99 | if (!gc->raw) { 100 | gc_image->data = reallocarray(gc_image->data, width * height, 101 | sizeof(uint32_t)); 102 | if (gc_image->data != NULL) 103 | memset(gc_image->data, 0, 104 | width * height * sizeof(uint32_t)); 105 | } 106 | } 107 | 108 | struct bhyvegc_image * 109 | bhyvegc_get_image(struct bhyvegc *gc) 110 | { 111 | if (gc == NULL) 112 | return (NULL); 113 | 114 | return (gc->gc_image); 115 | } 116 | -------------------------------------------------------------------------------- /src/compat.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2025 Alex Fishman 5 | * All rights reserved. 6 | * 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. 15 | * 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. 27 | */ 28 | 29 | #include 30 | #include 31 | #include 32 | #include "compat.h" 33 | #include "debug.h" 34 | 35 | #ifdef __linux__ 36 | #include 37 | #include 38 | #elif __APPLE__ 39 | #include 40 | #include 41 | #endif 42 | 43 | // Cross-platform function to set the thread name 44 | int 45 | compat_set_thread_name(pthread_t thread, const char *name) 46 | { 47 | int res; 48 | #ifdef __linux__ 49 | if ((res = pthread_setname_np(pthread_self(), name)) != 0) { 50 | EPRINTLN("pthread_setname_np failed"); 51 | } 52 | #elif __APPLE__ 53 | if (pthread_equal(pthread_self(), thread)) { 54 | res = pthread_setname_np(name); 55 | } else { 56 | // EPRINTLN("Cannot set name for threads other than the current 57 | // thread on macOS"); 58 | res = -1; 59 | } 60 | #else 61 | EPRINTLN("Setting thread name is not supported on this platform"); 62 | res = -1; 63 | #endif 64 | return res; 65 | } 66 | 67 | // Cross-platform function to set thread affinity 68 | int 69 | compat_set_thread_affinity(pthread_t thread, int core_id, cpuset_t *cpuset) 70 | { 71 | #ifdef __linux__ 72 | int res; 73 | if ((res = pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), 74 | cpuset)) != 0) { 75 | EPRINTLN("pthread_setaffinity_np failed"); 76 | } 77 | return res; 78 | #elif __APPLE__ 79 | // macOS: Use thread_policy_set (suggest affinity, no strict binding) 80 | if (pthread_equal(pthread_self(), thread)) { 81 | thread_port_t mach_thread = mach_thread_self(); 82 | 83 | thread_affinity_policy_data_t policy; 84 | policy.affinity_tag = core_id; 85 | 86 | kern_return_t kr = thread_policy_set(mach_thread, 87 | THREAD_AFFINITY_POLICY, (thread_policy_t)&policy, 88 | THREAD_AFFINITY_POLICY_COUNT); 89 | if (kr != KERN_SUCCESS) { 90 | EPRINTLN("thread_policy_set failed on macOS"); 91 | return -1; 92 | } 93 | return 0; 94 | } else { 95 | EPRINTLN( 96 | "Cannot set affinity for threads other than the current thread on macOS"); 97 | return -1; 98 | } 99 | #else 100 | // Unsupported platform 101 | EPRINTLN("Setting thread affinity is not supported on this platform"); 102 | return -1; 103 | #endif 104 | } -------------------------------------------------------------------------------- /src/debug.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2025 Alex Fishman 5 | * All rights reserved. 6 | * 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. 15 | * 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. 27 | */ 28 | 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include "vmm.h" 34 | -------------------------------------------------------------------------------- /src/libvmm/hvf/internal.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2025 Alex Fishman 5 | * All rights reserved. 6 | * 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. 15 | * 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. 27 | */ 28 | 29 | #pragma once 30 | 31 | enum { 32 | VM_MEMSEG_LOW, 33 | VM_MEMSEG_HIGH, 34 | VM_MEMSEG_COUNT, 35 | }; 36 | 37 | #define VM_MAX_MEMORY_SEGMENTS 16 38 | 39 | struct mem_range { 40 | uint64_t gpa; 41 | size_t len; 42 | void *object; 43 | }; 44 | 45 | #define MAX_VCPUS 64 46 | 47 | struct vmctx { 48 | struct { 49 | vm_paddr_t base; 50 | vm_size_t size; 51 | uintptr_t addr; 52 | } memsegs[VM_MEMSEG_COUNT]; 53 | 54 | int num_mem_ranges; 55 | struct mem_range mem_ranges[VM_MAX_MEMORY_SEGMENTS]; 56 | char *name; 57 | enum vm_suspend_how suspend_reason; 58 | 59 | cpuset_t active_cpus; 60 | cpuset_t suspended_cpus; 61 | struct vcpu *vcpus[MAX_VCPUS]; 62 | }; 63 | 64 | int init_apple_vgic(struct vmctx *ctx, uint64_t dist_start, size_t dist_size, 65 | uint64_t redist_start, size_t redist_size, uint64_t mmio_base, 66 | uint32_t spi_intid_base, uint32_t spi_intid_count); -------------------------------------------------------------------------------- /src/libvmm/hvf/vmm.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2025 Alex Fishman 5 | * All rights reserved. 6 | * 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. 15 | * 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. 27 | */ 28 | 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include "debug.h" 34 | #include "vmmapi.h" 35 | #include "internal.h" 36 | #include "vmm_instruction_emul.h" 37 | 38 | // hypervisor framework implementation 39 | int 40 | vm_assert_irq(struct vmctx *ctx, uint32_t irq) 41 | { 42 | hv_gic_set_spi(irq, 1); 43 | return 0; 44 | } 45 | 46 | int 47 | vm_deassert_irq(struct vmctx *ctx, uint32_t irq) 48 | { 49 | hv_gic_set_spi(irq, 0); 50 | return 0; 51 | } 52 | 53 | int 54 | vm_raise_msi(struct vmctx *ctx, uint64_t addr, uint64_t msg, int bus, int slot, 55 | int func) 56 | { 57 | // The reason why MSI doesn't work is because OSes use dist_base and 58 | // GICD_SETSPI_NSR while apple gic expects mmio_base and 59 | // GICM_SETSPI_NSR. Even when set can be worked around, clear from the 60 | // OS side doesn't work 61 | #if 0 62 | if (hv_gic_send_msi(addr, msg) != HV_SUCCESS) { 63 | EPRINTLN("hv_gic_send_msi() %llx, %llx", addr, msg); 64 | exit(-1); 65 | } 66 | #else 67 | EPRINTLN("Add support for MSI interrupts: addr %llx %llu", addr, msg); 68 | exit(-1); 69 | #endif 70 | return 0; 71 | } 72 | 73 | int 74 | vm_get_spi_interrupt_range(uint32_t *spi_intid_base, uint32_t *spi_intid_count) 75 | { 76 | return hv_gic_get_spi_interrupt_range(spi_intid_base, 77 | spi_intid_count) == HV_SUCCESS ? 78 | 0 : 79 | -1; 80 | } 81 | 82 | int 83 | vm_attach_vgic(struct vmctx *ctx, uint64_t dist_start, size_t dist_size, 84 | uint64_t redist_start, size_t redist_size, uint64_t mmio_base, 85 | uint32_t spi_intid_base, uint32_t spi_intid_count) 86 | { 87 | return init_apple_vgic(ctx, dist_start, dist_size, redist_start, 88 | redist_size, mmio_base, spi_intid_base, spi_intid_count); 89 | } 90 | 91 | /* 92 | * Create a device memory segment identified by 'segid'. 93 | * 94 | * Returns a pointer to the memory segment on success and MAP_FAILED otherwise. 95 | */ 96 | /*void * 97 | vm_create_devmem(struct vmctx *ctx, int segid, const char *name, size_t len) 98 | { 99 | return NULL; 100 | }*/ 101 | 102 | struct vmctx * 103 | vm_openf(const char *name, int flags) 104 | { 105 | hv_return_t res = hv_vm_create(NULL); 106 | if (res != HV_SUCCESS) { 107 | EPRINTLN("failed to create a vm"); 108 | exit(-1); 109 | } 110 | struct vmctx *ctx = malloc(sizeof(struct vmctx)); 111 | memset(ctx, 0, sizeof(*ctx)); 112 | 113 | return ctx; 114 | } 115 | -------------------------------------------------------------------------------- /src/libvmm/hvf/vmm_instruction_emul.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2025 Alex Fishman 5 | * All rights reserved. 6 | * 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. 15 | * 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. 27 | */ 28 | 29 | #include 30 | #include 31 | // #include 32 | 33 | #include 34 | 35 | #include 36 | #include 37 | #include 38 | #include 39 | 40 | #include 41 | 42 | int 43 | vmm_emulate_instruction(struct vcpu *vcpu, uint64_t gpa, struct vie *vie, 44 | struct vm_guest_paging *paging __unused, mem_region_read_t memread, 45 | mem_region_write_t memwrite, void *memarg) 46 | { 47 | uint64_t val = 0; 48 | int error; 49 | 50 | if (vie->dir == VM_DIR_READ) { 51 | error = memread(vcpu, gpa, &val, vie->access_size, memarg); 52 | if (error) 53 | goto out; 54 | error = vm_set_register(vcpu, vie->reg, val); 55 | } else { 56 | error = vm_get_register(vcpu, vie->reg, &val); 57 | if (error) 58 | goto out; 59 | /* Mask any unneeded bits from the register */ 60 | if (vie->access_size < 8) 61 | val &= (1ul << (vie->access_size * 8)) - 1; 62 | error = memwrite(vcpu, gpa, val, vie->access_size, memarg); 63 | } 64 | 65 | out: 66 | return (error); 67 | } 68 | 69 | int 70 | vmm_emulate_register(struct vcpu *vcpu, struct vre *vre, reg_read_t regread, 71 | reg_write_t regwrite, void *regarg) 72 | { 73 | uint64_t val; 74 | int error; 75 | 76 | if (vre->dir == VM_DIR_READ) { 77 | error = regread(vcpu, &val, regarg); 78 | if (error) 79 | goto out; 80 | error = vm_set_register(vcpu, vre->reg, val); 81 | } else { 82 | error = vm_get_register(vcpu, vre->reg, &val); 83 | if (error) 84 | goto out; 85 | error = regwrite(vcpu, val, regarg); 86 | } 87 | 88 | out: 89 | return (error); 90 | } -------------------------------------------------------------------------------- /src/net_utils.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2011 NetApp, Inc. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions 8 | * are met: 9 | * 1. Redistributions of source code must retain the above copyright 10 | * notice, this list of conditions and the following disclaimer. 11 | * 2. Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in the 13 | * documentation and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS``AS IS'' AND 16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS 19 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 20 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 21 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 22 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 24 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 25 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | */ 27 | 28 | #include 29 | #include "common.h" 30 | #include 31 | 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | #include 39 | 40 | #include "bhyverun.h" 41 | #include "config.h" 42 | #include "debug.h" 43 | #include "net_utils.h" 44 | 45 | int 46 | net_parsemac(const char *mac_str, uint8_t *mac_addr) 47 | { 48 | struct ether_addr *ea; 49 | char zero_addr[ETHER_ADDR_LEN] = { 0, 0, 0, 0, 0, 0 }; 50 | 51 | if (mac_str == NULL) 52 | return (EINVAL); 53 | 54 | ea = ether_aton(mac_str); 55 | 56 | if (ea == NULL || ETHER_IS_MULTICAST(ea->octet) || 57 | memcmp(ea->octet, zero_addr, ETHER_ADDR_LEN) == 0) { 58 | EPRINTLN("Invalid MAC %s", mac_str); 59 | return (EINVAL); 60 | } else 61 | memcpy(mac_addr, ea->octet, ETHER_ADDR_LEN); 62 | 63 | return (0); 64 | } 65 | 66 | int 67 | net_parsemtu(const char *mtu_str, unsigned long *mtu) 68 | { 69 | char *end; 70 | unsigned long val; 71 | 72 | assert(mtu_str != NULL); 73 | 74 | if (*mtu_str == '-') 75 | goto err; 76 | 77 | val = strtoul(mtu_str, &end, 0); 78 | 79 | if (*end != '\0') 80 | goto err; 81 | 82 | if (val == ULONG_MAX) 83 | return (ERANGE); 84 | 85 | if (val == 0 && errno == EINVAL) 86 | return (EINVAL); 87 | 88 | *mtu = val; 89 | 90 | return (0); 91 | 92 | err: 93 | errno = EINVAL; 94 | return (EINVAL); 95 | } 96 | 97 | void 98 | net_genmac(struct pci_devinst *pi, uint8_t *macaddr) 99 | { 100 | /* 101 | * The default MAC address is the standard NetApp OUI of 00-a0-98, 102 | * followed by an MD5 of the PCI slot/func number and dev name 103 | */ 104 | MD5_CTX mdctx; 105 | unsigned char digest[16]; 106 | char nstr[80]; 107 | 108 | snprintf(nstr, sizeof(nstr), "%d-%d-%s", pi->pi_slot, pi->pi_func, 109 | get_config_value("name")); 110 | 111 | MD5Init(&mdctx); 112 | MD5Update(&mdctx, nstr, (unsigned int)strlen(nstr)); 113 | MD5Final(digest, &mdctx); 114 | 115 | macaddr[0] = 0x00; 116 | macaddr[1] = 0xa0; 117 | macaddr[2] = 0x98; 118 | macaddr[3] = digest[0]; 119 | macaddr[4] = digest[1]; 120 | macaddr[5] = digest[2]; 121 | } 122 | -------------------------------------------------------------------------------- /src/pci_hostbridge.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2011 NetApp, Inc. 5 | * Copyright (c) 2025 Alex Fishman 6 | * All rights reserved. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions 10 | * are met: 11 | * 1. Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * 2. Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in the 15 | * documentation and/or other materials provided with the distribution. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 | * SUCH DAMAGE. 28 | */ 29 | 30 | #include 31 | #include 32 | #include 33 | 34 | #include "config.h" 35 | #include "pci_emul.h" 36 | 37 | static int 38 | pci_hostbridge_init(struct pci_devinst *pi, nvlist_t *nvl) 39 | { 40 | const char *value; 41 | u_int vendor, device; 42 | 43 | vendor = 0x1275; /* NetApp */ 44 | device = 0x1275; /* NetApp */ 45 | 46 | value = get_config_value_node(nvl, "vendor"); 47 | if (value != NULL) 48 | vendor = strtol(value, NULL, 0); 49 | else 50 | vendor = pci_config_read_reg(NULL, nvl, PCIR_VENDOR, 2, vendor); 51 | value = get_config_value_node(nvl, "devid"); 52 | if (value != NULL) 53 | device = strtol(value, NULL, 0); 54 | else 55 | device = pci_config_read_reg(NULL, nvl, PCIR_DEVICE, 2, device); 56 | 57 | /* config space */ 58 | pci_set_cfgdata16(pi, PCIR_VENDOR, vendor); 59 | pci_set_cfgdata16(pi, PCIR_DEVICE, device); 60 | pci_set_cfgdata8(pi, PCIR_HDRTYPE, PCIM_HDRTYPE_NORMAL); 61 | pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_BRIDGE); 62 | pci_set_cfgdata8(pi, PCIR_SUBCLASS, PCIS_BRIDGE_HOST); 63 | 64 | pci_emul_add_pciecap(pi, PCIEM_TYPE_ROOT_PORT); 65 | 66 | return (0); 67 | } 68 | 69 | static int 70 | pci_amd_hostbridge_legacy_config(nvlist_t *nvl, const char *opts __unused) 71 | { 72 | nvlist_t *pci_regs; 73 | 74 | pci_regs = create_relative_config_node(nvl, "pcireg"); 75 | if (pci_regs == NULL) { 76 | warnx("amd_hostbridge: failed to create pciregs node"); 77 | return (-1); 78 | } 79 | 80 | set_config_value_node(pci_regs, "vendor", "0x1022"); /* AMD */ 81 | set_config_value_node(pci_regs, "device", "0x7432"); /* made up */ 82 | 83 | return (0); 84 | } 85 | 86 | #ifdef BHYVE_SNAPSHOT 87 | static int 88 | pci_de_snapshot(struct vm_snapshot_meta *meta __unused) 89 | { 90 | return (0); 91 | } 92 | #endif 93 | 94 | static const struct pci_devemu pci_de_amd_hostbridge = { 95 | .pe_emu = "amd_hostbridge", 96 | .pe_legacy_config = pci_amd_hostbridge_legacy_config, 97 | .pe_alias = "hostbridge", 98 | }; 99 | PCI_EMUL_SET(pci_de_amd_hostbridge); 100 | 101 | static const struct pci_devemu pci_de_hostbridge = { 102 | .pe_emu = "hostbridge", 103 | .pe_init = pci_hostbridge_init, 104 | #ifdef BHYVE_SNAPSHOT 105 | .pe_snapshot = pci_de_snapshot, 106 | #endif 107 | }; 108 | PCI_EMUL_SET(pci_de_hostbridge); 109 | -------------------------------------------------------------------------------- /src/sockstream.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2015 Nahanni Systems, Inc. 5 | * Copyright (c) 2025 Alex Fishman 6 | * All rights reserved. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions 10 | * are met: 11 | * 1. Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * 2. Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in the 15 | * documentation and/or other materials provided with the distribution. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 | * SUCH DAMAGE. 28 | */ 29 | 30 | #include 31 | #include 32 | #include 33 | #include 34 | 35 | #include "sockstream.h" 36 | 37 | ssize_t 38 | stream_read(int fd, void *buf, ssize_t nbytes) 39 | { 40 | uint8_t *p; 41 | ssize_t len = 0; 42 | ssize_t n; 43 | 44 | p = buf; 45 | 46 | while (len < nbytes) { 47 | n = read(fd, p + len, nbytes - len); 48 | if (n == 0) 49 | break; 50 | 51 | if (n < 0) { 52 | if (errno == EINTR || errno == EAGAIN) 53 | continue; 54 | return (n); 55 | } 56 | len += n; 57 | } 58 | return (len); 59 | } 60 | 61 | ssize_t 62 | stream_write(int fd, const void *buf, ssize_t nbytes) 63 | { 64 | const uint8_t *p; 65 | ssize_t len = 0; 66 | ssize_t n; 67 | 68 | p = buf; 69 | 70 | while (len < nbytes) { 71 | n = write(fd, p + len, nbytes - len); 72 | if (n == 0) 73 | break; 74 | if (n < 0) { 75 | if (errno == EINTR || errno == EAGAIN) 76 | continue; 77 | return (n); 78 | } 79 | len += n; 80 | } 81 | return (len); 82 | } 83 | -------------------------------------------------------------------------------- /src/vmgenc.c: -------------------------------------------------------------------------------- 1 | 2 | /*- 3 | * SPDX-License-Identifier: BSD-2-Clause 4 | * 5 | * Copyright (c) 2025 Alex Fishman 6 | * Copyright 2020 Conrad Meyer . All rights reserved. 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions 10 | * are met: 11 | * 1. Redistributions of source code must retain the above copyright 12 | * notice, this list of conditions and the following disclaimer. 13 | * 2. Redistributions in binary form must reproduce the above copyright 14 | * notice, this list of conditions and the following disclaimer in the 15 | * documentation and/or other materials provided with the distribution. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 | * SUCH DAMAGE. 28 | */ 29 | 30 | #include 31 | #include 32 | 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | #include 39 | #include 40 | #include 41 | #include 42 | #include 43 | #include 44 | 45 | #include 46 | #include "vmm.h" 47 | 48 | #include "acpi.h" 49 | #include "bootrom.h" 50 | #include "vmgenc.h" 51 | 52 | static uint64_t vmgen_gpa; 53 | 54 | void 55 | vmgenc_init(struct vmctx *ctx) 56 | { 57 | char *region; 58 | int error; 59 | 60 | error = bootrom_alloc(ctx, PAGE_SIZE, PROT_READ, 0, ®ion, 61 | &vmgen_gpa); 62 | if (error != 0) 63 | errx(4, "%s: bootrom_alloc", __func__); 64 | 65 | /* XXX When we have suspend/resume/rollback. */ 66 | #if 0 67 | acpi_raise_gpe(ctx, GPE_VMGENC); 68 | #endif 69 | } 70 | 71 | void 72 | vmgenc_write_dsdt(void) 73 | { 74 | dsdt_line(""); 75 | dsdt_indent(1); 76 | dsdt_line("Scope (_SB)"); 77 | dsdt_line("{"); 78 | 79 | dsdt_line(" Device (GENC)"); 80 | dsdt_line(" {"); 81 | 82 | dsdt_indent(2); 83 | dsdt_line("Name (_CID, \"VM_Gen_Counter\")"); 84 | dsdt_line("Method (_HID, 0, NotSerialized)"); 85 | dsdt_line("{"); 86 | dsdt_line(" Return (\"Bhyve_V_Gen_Counter_V1\")"); 87 | dsdt_line("}"); 88 | dsdt_line("Name (_UID, 0)"); 89 | dsdt_line("Name (_DDN, \"VM_Gen_Counter\")"); 90 | dsdt_line("Name (ADDR, Package (0x02)"); 91 | dsdt_line("{"); 92 | dsdt_line(" 0x%08x,", (uint32_t)vmgen_gpa); 93 | dsdt_line(" 0x%08x", (uint32_t)(vmgen_gpa >> 32)); 94 | dsdt_line("})"); 95 | 96 | dsdt_unindent(2); 97 | dsdt_line(" }"); /* Device (GENC) */ 98 | 99 | dsdt_line("}"); /* Scope (_SB) */ 100 | dsdt_line(""); 101 | 102 | dsdt_line("Scope (_GPE)"); 103 | dsdt_line("{"); 104 | dsdt_line(" Method (_E%02x, 0, NotSerialized)", GPE_VMGENC); 105 | dsdt_line(" {"); 106 | dsdt_line(" Notify (\\_SB.GENC, 0x80)"); 107 | dsdt_line(" }"); 108 | dsdt_line("}"); 109 | dsdt_unindent(1); 110 | } 111 | -------------------------------------------------------------------------------- /tests/meson.build: -------------------------------------------------------------------------------- 1 | test_src = files('nvlist_test.c') 2 | 3 | nv_test_executable = executable( 4 | 'nvlist_test', # Name of the executable 5 | test_src, 6 | dependencies: libnv_dep, 7 | include_directories: inc 8 | ) 9 | 10 | test('nvlist_test', nv_test_executable) 11 | 12 | dtb = custom_target('test_dtb', 13 | input: 'test.dts', 14 | output: 'test.dtb', 15 | command: ['dtc', '-I', 'dts', '-O', 'dtb', '-o', '@OUTPUT@', '@INPUT@'] 16 | ) 17 | 18 | fdt_test_src = files('fdt_test.c') 19 | 20 | fdt_test_executable = executable( 21 | 'fdt_test', 22 | fdt_test_src, 23 | dependencies: libfdt_dep, 24 | include_directories: inc, 25 | ) 26 | 27 | test_env = environment() 28 | test_env.set('TEST_DTB_FILE', meson.current_build_dir() / 'test.dtb') 29 | test('fdt_test', fdt_test_executable, env: test_env, depends: dtb) -------------------------------------------------------------------------------- /tests/nvlist_test.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2025 Alex Fishman 5 | * All rights reserved. 6 | * 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. 15 | * 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. 27 | */ 28 | 29 | #include 30 | #include 31 | #include 32 | #include "nv.h" 33 | 34 | void 35 | test_nvlist() 36 | { 37 | // Create a new nvlist 38 | nvlist_t *nvl = nvlist_create(0); 39 | if (nvl == NULL) { 40 | perror("nvlist_create failed"); 41 | exit(EXIT_FAILURE); 42 | } 43 | 44 | // Add various types of data to the nvlist 45 | nvlist_add_string(nvl, "key1", "value1"); 46 | nvlist_add_number(nvl, "key2", 12345); 47 | nvlist_add_bool(nvl, "key3", true); 48 | nvlist_add_binary(nvl, "key4", "binarydata", 10); 49 | 50 | // Serialize the nvlist to a buffer 51 | size_t nvlist_size; 52 | void *nvlist_buf = nvlist_pack(nvl, &nvlist_size); 53 | if (nvlist_buf == NULL) { 54 | perror("nvlist_pack failed"); 55 | nvlist_destroy(nvl); 56 | exit(EXIT_FAILURE); 57 | } 58 | 59 | printf("Serialized nvlist size: %zu\n", nvlist_size); 60 | 61 | // Free the original nvlist 62 | nvlist_destroy(nvl); 63 | 64 | // Deserialize the buffer back into an nvlist 65 | nvlist_t *unpacked_nvl = nvlist_unpack(nvlist_buf, nvlist_size, 0); 66 | free(nvlist_buf); 67 | if (unpacked_nvl == NULL) { 68 | perror("nvlist_unpack failed"); 69 | exit(EXIT_FAILURE); 70 | } 71 | 72 | // Access and verify the data 73 | const char *str_val = nvlist_get_string(unpacked_nvl, "key1"); 74 | printf("key1: %s\n", str_val); 75 | 76 | int64_t num_val = nvlist_get_number(unpacked_nvl, "key2"); 77 | printf("key2: %lld\n", num_val); 78 | 79 | bool bool_val = nvlist_get_bool(unpacked_nvl, "key3"); 80 | printf("key3: %s\n", bool_val ? "true" : "false"); 81 | 82 | size_t binary_len; 83 | const void *binary_data = nvlist_get_binary(unpacked_nvl, "key4", 84 | &binary_len); 85 | printf("key4: binary data length = %zu\n", binary_len); 86 | for (size_t i = 0; i < binary_len; i++) { 87 | printf("%02x ", ((unsigned char *)binary_data)[i]); 88 | } 89 | printf("\n"); 90 | 91 | // Free the unpacked nvlist 92 | nvlist_destroy(unpacked_nvl); 93 | } 94 | 95 | int 96 | main() 97 | { 98 | printf("Testing FreeBSD nvlist...\n"); 99 | test_nvlist(); 100 | printf("Test completed successfully.\n"); 101 | return 0; 102 | } 103 | -------------------------------------------------------------------------------- /tests/test.dts: -------------------------------------------------------------------------------- 1 | /dts-v1/; 2 | 3 | / { 4 | compatible = "test,device-tree"; 5 | model = "Test Device Tree Blob"; 6 | 7 | chosen { 8 | bootargs = "console=ttyS0,115200"; 9 | }; 10 | 11 | test-node { 12 | compatible = "test,subnode"; 13 | new-prop = "value"; 14 | }; 15 | }; 16 | 17 | -------------------------------------------------------------------------------- /util/console/meson.build: -------------------------------------------------------------------------------- 1 | console_src = files('tty.c', 'websock_client.c') 2 | 3 | libwebsockets_dep = dependency('libwebsockets', required: true) 4 | 5 | console_executable = executable( 6 | 'scorpi_console', 7 | console_src, 8 | dependencies: [libwebsockets_dep] 9 | ) 10 | 11 | if host_machine.system() == 'darwin' 12 | codesign = custom_target( 13 | 'codesign-scorpi', 14 | input: console_executable, 15 | output: 'scorpi-console', 16 | command: [ 17 | 'codesign', 18 | '--sign', codesign_identity, 19 | '--force', 20 | '@INPUT@' 21 | ], 22 | build_by_default: true 23 | ) 24 | endif 25 | -------------------------------------------------------------------------------- /util/console/websock_client.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * SPDX-License-Identifier: BSD-2-Clause 3 | * 4 | * Copyright (c) 2025 Alex Fishman 5 | * All rights reserved. 6 | * 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. 15 | * 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. 27 | */ 28 | 29 | #pragma once 30 | 31 | int vm_connect(const char *path); 32 | int vm_resize_request(int port, int rows, int cols); --------------------------------------------------------------------------------