xref: /illumos-gate/usr/src/cmd/bhyve/kernemu_dev.c (revision 32640292)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright 2020 Conrad Meyer <cem@FreeBSD.org>.  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 #include <sys/cdefs.h>
28 
29 #include <sys/param.h>
30 #include <sys/errno.h>
31 #include <sys/tree.h>
32 
33 #include <machine/vmm.h>
34 #include <x86/include/apicreg.h>
35 struct vm;
36 struct vm_hpet_cap;
37 #include <vmm/io/vioapic.h>
38 #include <vmm/io/vhpet.h>
39 
40 #include <err.h>
41 #include <errno.h>
42 #include <vmmapi.h>
43 
44 #include "kernemu_dev.h"
45 #include "mem.h"
46 
47 static int
apic_handler(struct vcpu * vcpu,int dir,uint64_t addr,int size,uint64_t * val,void * arg1 __unused,long arg2 __unused)48 apic_handler(struct vcpu *vcpu, int dir, uint64_t addr, int size,
49     uint64_t *val, void *arg1 __unused, long arg2 __unused)
50 {
51 	if (vm_readwrite_kernemu_device(vcpu, addr, (dir == MEM_F_WRITE),
52 	    size, val) != 0)
53 		return (errno);
54 	return (0);
55 }
56 
57 static struct mem_range lapic_mmio = {
58 	.name = "kern-lapic-mmio",
59 	.base = DEFAULT_APIC_BASE,
60 	.size = PAGE_SIZE,
61 	.flags = MEM_F_RW | MEM_F_IMMUTABLE,
62 	.handler = apic_handler,
63 
64 };
65 static struct mem_range ioapic_mmio = {
66 	.name = "kern-ioapic-mmio",
67 	.base = VIOAPIC_BASE,
68 	.size = VIOAPIC_SIZE,
69 	.flags = MEM_F_RW | MEM_F_IMMUTABLE,
70 	.handler = apic_handler,
71 };
72 static struct mem_range hpet_mmio = {
73 	.name = "kern-hpet-mmio",
74 	.base = VHPET_BASE,
75 	.size = VHPET_SIZE,
76 	.flags = MEM_F_RW | MEM_F_IMMUTABLE,
77 	.handler = apic_handler,
78 };
79 
80 void
kernemu_dev_init(void)81 kernemu_dev_init(void)
82 {
83 	int rc;
84 
85 	rc = register_mem(&lapic_mmio);
86 	if (rc != 0)
87 		errc(4, rc, "register_mem: LAPIC (0x%08x)",
88 		    (unsigned)lapic_mmio.base);
89 	rc = register_mem(&ioapic_mmio);
90 	if (rc != 0)
91 		errc(4, rc, "register_mem: IOAPIC (0x%08x)",
92 		    (unsigned)ioapic_mmio.base);
93 	rc = register_mem(&hpet_mmio);
94 	if (rc != 0)
95 		errc(4, rc, "register_mem: HPET (0x%08x)",
96 		    (unsigned)hpet_mmio.base);
97 }
98