xref: /freebsd/lib/libvmmapi/riscv/vmmapi_machdep.c (revision a7bf553d175a27b03cbad7d0f94e40991df2958b)
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_SSTC] = "sstc",
44 	[VM_CAP_MAX] = NULL,
45 };
46 
47 #define	VM_MD_IOCTLS		\
48 	VM_ATTACH_APLIC,	\
49 	VM_ASSERT_IRQ,		\
50 	VM_DEASSERT_IRQ,	\
51 	VM_RAISE_MSI
52 
53 const cap_ioctl_t vm_ioctl_cmds[] = {
54 	VM_COMMON_IOCTLS,
55 	VM_MD_IOCTLS,
56 };
57 size_t vm_ioctl_ncmds = nitems(vm_ioctl_cmds);
58 
59 int
vm_attach_aplic(struct vmctx * ctx,uint64_t mem_start,size_t mem_size)60 vm_attach_aplic(struct vmctx *ctx, uint64_t mem_start, size_t mem_size)
61 {
62 	struct vm_aplic_descr aplic;
63 
64 	bzero(&aplic, sizeof(aplic));
65 	aplic.mem_start = mem_start;
66 	aplic.mem_size = mem_size;
67 
68 	return (ioctl(ctx->fd, VM_ATTACH_APLIC, &aplic));
69 }
70 
71 int
vm_assert_irq(struct vmctx * ctx,uint32_t irq)72 vm_assert_irq(struct vmctx *ctx, uint32_t irq)
73 {
74 	struct vm_irq vi;
75 
76 	bzero(&vi, sizeof(vi));
77 	vi.irq = irq;
78 
79 	return (ioctl(ctx->fd, VM_ASSERT_IRQ, &vi));
80 }
81 
82 int
vm_deassert_irq(struct vmctx * ctx,uint32_t irq)83 vm_deassert_irq(struct vmctx *ctx, uint32_t irq)
84 {
85 	struct vm_irq vi;
86 
87 	bzero(&vi, sizeof(vi));
88 	vi.irq = irq;
89 
90 	return (ioctl(ctx->fd, VM_DEASSERT_IRQ, &vi));
91 }
92 
93 int
vm_raise_msi(struct vmctx * ctx,uint64_t addr,uint64_t msg,int bus,int slot,int func)94 vm_raise_msi(struct vmctx *ctx, uint64_t addr, uint64_t msg,
95     int bus, int slot, int func)
96 {
97 	struct vm_msi vmsi;
98 
99 	bzero(&vmsi, sizeof(vmsi));
100 	vmsi.addr = addr;
101 	vmsi.msg = msg;
102 	vmsi.bus = bus;
103 	vmsi.slot = slot;
104 	vmsi.func = func;
105 
106 	return (ioctl(ctx->fd, VM_RAISE_MSI, &vmsi));
107 }
108 
109 int
vm_inject_exception(struct vcpu * vcpu,uint64_t scause)110 vm_inject_exception(struct vcpu *vcpu, uint64_t scause)
111 {
112 	struct vm_exception vmexc;
113 
114 	bzero(&vmexc, sizeof(vmexc));
115 	vmexc.scause = scause;
116 
117 	return (vcpu_ioctl(vcpu, VM_INJECT_EXCEPTION, &vmexc));
118 }
119