xref: /illumos-gate/usr/src/cmd/bhyve/kernemu_dev.c (revision 32640292)
1154972afSPatrick Mooney /*-
2*32640292SAndy Fiddaman  * SPDX-License-Identifier: BSD-2-Clause
3154972afSPatrick Mooney  *
4154972afSPatrick Mooney  * Copyright 2020 Conrad Meyer <cem@FreeBSD.org>.  All rights reserved.
5154972afSPatrick Mooney  *
6154972afSPatrick Mooney  * Redistribution and use in source and binary forms, with or without
7154972afSPatrick Mooney  * modification, are permitted provided that the following conditions
8154972afSPatrick Mooney  * are met:
9154972afSPatrick Mooney  * 1. Redistributions of source code must retain the above copyright
10154972afSPatrick Mooney  *    notice, this list of conditions and the following disclaimer.
11154972afSPatrick Mooney  * 2. Redistributions in binary form must reproduce the above copyright
12154972afSPatrick Mooney  *    notice, this list of conditions and the following disclaimer in the
13154972afSPatrick Mooney  *    documentation and/or other materials provided with the distribution.
14154972afSPatrick Mooney  *
15154972afSPatrick Mooney  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16154972afSPatrick Mooney  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17154972afSPatrick Mooney  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18154972afSPatrick Mooney  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19154972afSPatrick Mooney  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20154972afSPatrick Mooney  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21154972afSPatrick Mooney  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22154972afSPatrick Mooney  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23154972afSPatrick Mooney  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24154972afSPatrick Mooney  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25154972afSPatrick Mooney  * SUCH DAMAGE.
26154972afSPatrick Mooney  */
27154972afSPatrick Mooney #include <sys/cdefs.h>
28154972afSPatrick Mooney 
29154972afSPatrick Mooney #include <sys/param.h>
30154972afSPatrick Mooney #include <sys/errno.h>
31154972afSPatrick Mooney #include <sys/tree.h>
32154972afSPatrick Mooney 
33*32640292SAndy Fiddaman #include <machine/vmm.h>
34154972afSPatrick Mooney #include <x86/include/apicreg.h>
35154972afSPatrick Mooney struct vm;
36154972afSPatrick Mooney struct vm_hpet_cap;
37154972afSPatrick Mooney #include <vmm/io/vioapic.h>
38154972afSPatrick Mooney #include <vmm/io/vhpet.h>
39154972afSPatrick Mooney 
40154972afSPatrick Mooney #include <err.h>
41154972afSPatrick Mooney #include <errno.h>
42154972afSPatrick Mooney #include <vmmapi.h>
43154972afSPatrick Mooney 
44154972afSPatrick Mooney #include "kernemu_dev.h"
45154972afSPatrick Mooney #include "mem.h"
46154972afSPatrick Mooney 
47154972afSPatrick Mooney static int
apic_handler(struct vcpu * vcpu,int dir,uint64_t addr,int size,uint64_t * val,void * arg1 __unused,long arg2 __unused)48*32640292SAndy Fiddaman apic_handler(struct vcpu *vcpu, int dir, uint64_t addr, int size,
49154972afSPatrick Mooney     uint64_t *val, void *arg1 __unused, long arg2 __unused)
50154972afSPatrick Mooney {
51*32640292SAndy Fiddaman 	if (vm_readwrite_kernemu_device(vcpu, addr, (dir == MEM_F_WRITE),
52154972afSPatrick Mooney 	    size, val) != 0)
53154972afSPatrick Mooney 		return (errno);
54154972afSPatrick Mooney 	return (0);
55154972afSPatrick Mooney }
56154972afSPatrick Mooney 
57154972afSPatrick Mooney static struct mem_range lapic_mmio = {
58154972afSPatrick Mooney 	.name = "kern-lapic-mmio",
59154972afSPatrick Mooney 	.base = DEFAULT_APIC_BASE,
60154972afSPatrick Mooney 	.size = PAGE_SIZE,
61154972afSPatrick Mooney 	.flags = MEM_F_RW | MEM_F_IMMUTABLE,
62154972afSPatrick Mooney 	.handler = apic_handler,
63154972afSPatrick Mooney 
64154972afSPatrick Mooney };
65154972afSPatrick Mooney static struct mem_range ioapic_mmio = {
66154972afSPatrick Mooney 	.name = "kern-ioapic-mmio",
67154972afSPatrick Mooney 	.base = VIOAPIC_BASE,
68154972afSPatrick Mooney 	.size = VIOAPIC_SIZE,
69154972afSPatrick Mooney 	.flags = MEM_F_RW | MEM_F_IMMUTABLE,
70154972afSPatrick Mooney 	.handler = apic_handler,
71154972afSPatrick Mooney };
72154972afSPatrick Mooney static struct mem_range hpet_mmio = {
73154972afSPatrick Mooney 	.name = "kern-hpet-mmio",
74154972afSPatrick Mooney 	.base = VHPET_BASE,
75154972afSPatrick Mooney 	.size = VHPET_SIZE,
76154972afSPatrick Mooney 	.flags = MEM_F_RW | MEM_F_IMMUTABLE,
77154972afSPatrick Mooney 	.handler = apic_handler,
78154972afSPatrick Mooney };
79154972afSPatrick Mooney 
80154972afSPatrick Mooney void
kernemu_dev_init(void)81154972afSPatrick Mooney kernemu_dev_init(void)
82154972afSPatrick Mooney {
83154972afSPatrick Mooney 	int rc;
84154972afSPatrick Mooney 
85154972afSPatrick Mooney 	rc = register_mem(&lapic_mmio);
86154972afSPatrick Mooney 	if (rc != 0)
87154972afSPatrick Mooney 		errc(4, rc, "register_mem: LAPIC (0x%08x)",
88154972afSPatrick Mooney 		    (unsigned)lapic_mmio.base);
89154972afSPatrick Mooney 	rc = register_mem(&ioapic_mmio);
90154972afSPatrick Mooney 	if (rc != 0)
91154972afSPatrick Mooney 		errc(4, rc, "register_mem: IOAPIC (0x%08x)",
92154972afSPatrick Mooney 		    (unsigned)ioapic_mmio.base);
93154972afSPatrick Mooney 	rc = register_mem(&hpet_mmio);
94154972afSPatrick Mooney 	if (rc != 0)
95154972afSPatrick Mooney 		errc(4, rc, "register_mem: HPET (0x%08x)",
96154972afSPatrick Mooney 		    (unsigned)hpet_mmio.base);
97154972afSPatrick Mooney }
98