Path: blob/main/usr.sbin/bhyve/amd64/kernemu_dev.c
105642 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright 2020 Conrad Meyer <[email protected]>. All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND15* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE18* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24* SUCH DAMAGE.25*/2627#include <sys/param.h>28#include <sys/errno.h>29#include <sys/tree.h>3031#include <machine/vmm.h>32#include <x86/include/apicreg.h>33struct vm;34struct vm_hpet_cap;35#include <vmm/io/vioapic.h>36#include <vmm/io/vhpet.h>3738#include <err.h>39#include <errno.h>40#include <vmmapi.h>4142#include "kernemu_dev.h"43#include "mem.h"4445static int46apic_handler(struct vcpu *vcpu, int dir, uint64_t addr, int size,47uint64_t *val, void *arg1 __unused, long arg2 __unused)48{49if (vm_readwrite_kernemu_device(vcpu, addr, (dir == MEM_F_WRITE),50size, val) != 0)51return (errno);52return (0);53}5455static struct mem_range lapic_mmio = {56.name = "kern-lapic-mmio",57.base = DEFAULT_APIC_BASE,58.size = PAGE_SIZE,59.flags = MEM_F_RW | MEM_F_IMMUTABLE,60.handler = apic_handler,6162};63static struct mem_range ioapic_mmio = {64.name = "kern-ioapic-mmio",65.base = VIOAPIC_BASE,66.size = VIOAPIC_SIZE,67.flags = MEM_F_RW | MEM_F_IMMUTABLE,68.handler = apic_handler,69};70static struct mem_range hpet_mmio = {71.name = "kern-hpet-mmio",72.base = VHPET_BASE,73.size = VHPET_SIZE,74.flags = MEM_F_RW | MEM_F_IMMUTABLE,75.handler = apic_handler,76};7778void79kernemu_dev_init(void)80{81int rc;8283rc = register_mem(&lapic_mmio);84if (rc != 0)85errc(4, rc, "register_mem: LAPIC (0x%08x)",86(unsigned)lapic_mmio.base);87rc = register_mem(&ioapic_mmio);88if (rc != 0)89errc(4, rc, "register_mem: IOAPIC (0x%08x)",90(unsigned)ioapic_mmio.base);91rc = register_mem(&hpet_mmio);92if (rc != 0)93errc(4, rc, "register_mem: HPET (0x%08x)",94(unsigned)hpet_mmio.base);95}969798