xref: /freebsd/lib/libvmmapi/riscv/vmmapi_machdep.c (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2011 NetApp, Inc.
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 NETAPP, INC ``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 NETAPP, INC 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 <sys/types.h>
30 #include <sys/ioctl.h>
31 
32 #include <machine/vmm.h>
33 #include <machine/vmm_dev.h>
34 #include <machine/vmm_snapshot.h>
35 
36 #include <assert.h>
37 #include <string.h>
38 
39 #include "vmmapi.h"
40 #include "internal.h"
41 
42 const char *vm_capstrmap[] = {
43 	[VM_CAP_MAX] = NULL,
44 };
45 
46 #define	VM_MD_IOCTLS		\
47 	VM_ATTACH_APLIC,	\
48 	VM_ASSERT_IRQ,		\
49 	VM_DEASSERT_IRQ,	\
50 	VM_RAISE_MSI
51 
52 const cap_ioctl_t vm_ioctl_cmds[] = {
53 	VM_COMMON_IOCTLS,
54 	VM_MD_IOCTLS,
55 };
56 size_t vm_ioctl_ncmds = nitems(vm_ioctl_cmds);
57 
58 int
59 vm_attach_aplic(struct vmctx *ctx, uint64_t mem_start, size_t mem_size)
60 {
61 	struct vm_aplic_descr aplic;
62 
63 	bzero(&aplic, sizeof(aplic));
64 	aplic.mem_start = mem_start;
65 	aplic.mem_size = mem_size;
66 
67 	return (ioctl(ctx->fd, VM_ATTACH_APLIC, &aplic));
68 }
69 
70 int
71 vm_assert_irq(struct vmctx *ctx, uint32_t irq)
72 {
73 	struct vm_irq vi;
74 
75 	bzero(&vi, sizeof(vi));
76 	vi.irq = irq;
77 
78 	return (ioctl(ctx->fd, VM_ASSERT_IRQ, &vi));
79 }
80 
81 int
82 vm_deassert_irq(struct vmctx *ctx, uint32_t irq)
83 {
84 	struct vm_irq vi;
85 
86 	bzero(&vi, sizeof(vi));
87 	vi.irq = irq;
88 
89 	return (ioctl(ctx->fd, VM_DEASSERT_IRQ, &vi));
90 }
91 
92 int
93 vm_raise_msi(struct vmctx *ctx, uint64_t addr, uint64_t msg,
94     int bus, int slot, int func)
95 {
96 	struct vm_msi vmsi;
97 
98 	bzero(&vmsi, sizeof(vmsi));
99 	vmsi.addr = addr;
100 	vmsi.msg = msg;
101 	vmsi.bus = bus;
102 	vmsi.slot = slot;
103 	vmsi.func = func;
104 
105 	return (ioctl(ctx->fd, VM_RAISE_MSI, &vmsi));
106 }
107 
108 int
109 vm_inject_exception(struct vcpu *vcpu, uint64_t scause)
110 {
111 	struct vm_exception vmexc;
112 
113 	bzero(&vmexc, sizeof(vmexc));
114 	vmexc.scause = scause;
115 
116 	return (vcpu_ioctl(vcpu, VM_INJECT_EXCEPTION, &vmexc));
117 }
118