xref: /freebsd/usr.sbin/bhyve/amd64/kernemu_dev.c (revision 4d65a7c6951cea0333f1a0c1b32c38489cdfa6c5)
1a1642451SMark Johnston /*-
2a1642451SMark Johnston  * SPDX-License-Identifier: BSD-2-Clause
3a1642451SMark Johnston  *
4a1642451SMark Johnston  * Copyright 2020 Conrad Meyer <cem@FreeBSD.org>.  All rights reserved.
5a1642451SMark Johnston  *
6a1642451SMark Johnston  * Redistribution and use in source and binary forms, with or without
7a1642451SMark Johnston  * modification, are permitted provided that the following conditions
8a1642451SMark Johnston  * are met:
9a1642451SMark Johnston  * 1. Redistributions of source code must retain the above copyright
10a1642451SMark Johnston  *    notice, this list of conditions and the following disclaimer.
11a1642451SMark Johnston  * 2. Redistributions in binary form must reproduce the above copyright
12a1642451SMark Johnston  *    notice, this list of conditions and the following disclaimer in the
13a1642451SMark Johnston  *    documentation and/or other materials provided with the distribution.
14a1642451SMark Johnston  *
15a1642451SMark Johnston  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16a1642451SMark Johnston  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17a1642451SMark Johnston  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18a1642451SMark Johnston  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19a1642451SMark Johnston  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20a1642451SMark Johnston  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21a1642451SMark Johnston  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22a1642451SMark Johnston  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23a1642451SMark Johnston  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24a1642451SMark Johnston  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25a1642451SMark Johnston  * SUCH DAMAGE.
26a1642451SMark Johnston  */
27*4d65a7c6SWarner Losh 
28a1642451SMark Johnston #include <sys/param.h>
29a1642451SMark Johnston #include <sys/errno.h>
30a1642451SMark Johnston #include <sys/tree.h>
31a1642451SMark Johnston 
32a1642451SMark Johnston #include <machine/vmm.h>
33a1642451SMark Johnston #include <x86/include/apicreg.h>
34a1642451SMark Johnston struct vm;
35a1642451SMark Johnston struct vm_hpet_cap;
36a1642451SMark Johnston #include <vmm/io/vioapic.h>
37a1642451SMark Johnston #include <vmm/io/vhpet.h>
38a1642451SMark Johnston 
39a1642451SMark Johnston #include <err.h>
40a1642451SMark Johnston #include <errno.h>
41a1642451SMark Johnston #include <vmmapi.h>
42a1642451SMark Johnston 
43a1642451SMark Johnston #include "kernemu_dev.h"
44a1642451SMark Johnston #include "mem.h"
45a1642451SMark Johnston 
46a1642451SMark Johnston static int
apic_handler(struct vcpu * vcpu,int dir,uint64_t addr,int size,uint64_t * val,void * arg1 __unused,long arg2 __unused)47a1642451SMark Johnston apic_handler(struct vcpu *vcpu, int dir, uint64_t addr, int size,
48a1642451SMark Johnston     uint64_t *val, void *arg1 __unused, long arg2 __unused)
49a1642451SMark Johnston {
50a1642451SMark Johnston 	if (vm_readwrite_kernemu_device(vcpu, addr, (dir == MEM_F_WRITE),
51a1642451SMark Johnston 	    size, val) != 0)
52a1642451SMark Johnston 		return (errno);
53a1642451SMark Johnston 	return (0);
54a1642451SMark Johnston }
55a1642451SMark Johnston 
56a1642451SMark Johnston static struct mem_range lapic_mmio = {
57a1642451SMark Johnston 	.name = "kern-lapic-mmio",
58a1642451SMark Johnston 	.base = DEFAULT_APIC_BASE,
59a1642451SMark Johnston 	.size = PAGE_SIZE,
60a1642451SMark Johnston 	.flags = MEM_F_RW | MEM_F_IMMUTABLE,
61a1642451SMark Johnston 	.handler = apic_handler,
62a1642451SMark Johnston 
63a1642451SMark Johnston };
64a1642451SMark Johnston static struct mem_range ioapic_mmio = {
65a1642451SMark Johnston 	.name = "kern-ioapic-mmio",
66a1642451SMark Johnston 	.base = VIOAPIC_BASE,
67a1642451SMark Johnston 	.size = VIOAPIC_SIZE,
68a1642451SMark Johnston 	.flags = MEM_F_RW | MEM_F_IMMUTABLE,
69a1642451SMark Johnston 	.handler = apic_handler,
70a1642451SMark Johnston };
71a1642451SMark Johnston static struct mem_range hpet_mmio = {
72a1642451SMark Johnston 	.name = "kern-hpet-mmio",
73a1642451SMark Johnston 	.base = VHPET_BASE,
74a1642451SMark Johnston 	.size = VHPET_SIZE,
75a1642451SMark Johnston 	.flags = MEM_F_RW | MEM_F_IMMUTABLE,
76a1642451SMark Johnston 	.handler = apic_handler,
77a1642451SMark Johnston };
78a1642451SMark Johnston 
79a1642451SMark Johnston void
kernemu_dev_init(void)80a1642451SMark Johnston kernemu_dev_init(void)
81a1642451SMark Johnston {
82a1642451SMark Johnston 	int rc;
83a1642451SMark Johnston 
84a1642451SMark Johnston 	rc = register_mem(&lapic_mmio);
85a1642451SMark Johnston 	if (rc != 0)
86a1642451SMark Johnston 		errc(4, rc, "register_mem: LAPIC (0x%08x)",
87a1642451SMark Johnston 		    (unsigned)lapic_mmio.base);
88a1642451SMark Johnston 	rc = register_mem(&ioapic_mmio);
89a1642451SMark Johnston 	if (rc != 0)
90a1642451SMark Johnston 		errc(4, rc, "register_mem: IOAPIC (0x%08x)",
91a1642451SMark Johnston 		    (unsigned)ioapic_mmio.base);
92a1642451SMark Johnston 	rc = register_mem(&hpet_mmio);
93a1642451SMark Johnston 	if (rc != 0)
94a1642451SMark Johnston 		errc(4, rc, "register_mem: HPET (0x%08x)",
95a1642451SMark Johnston 		    (unsigned)hpet_mmio.base);
96a1642451SMark Johnston }
97