xref: /netbsd/lib/libnvmm/nvmm.h (revision 3d5c2360)
1 /*	$NetBSD: nvmm.h,v 1.18 2020/09/05 07:22:25 maxv Exp $	*/
2 
3 /*
4  * Copyright (c) 2018-2020 Maxime Villard, m00nbsd.net
5  * All rights reserved.
6  *
7  * This code is part of the NVMM hypervisor.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #ifndef _LIBNVMM_H_
32 #define _LIBNVMM_H_
33 
34 #include <stdint.h>
35 #include <stdbool.h>
36 
37 #include <dev/nvmm/nvmm.h>
38 #include <dev/nvmm/nvmm_ioctl.h>
39 
40 #define NVMM_USER_VERSION	1
41 
42 struct nvmm_io;
43 struct nvmm_mem;
44 
45 struct nvmm_assist_callbacks {
46 	void (*io)(struct nvmm_io *);
47 	void (*mem)(struct nvmm_mem *);
48 };
49 
50 struct nvmm_machine {
51 	nvmm_machid_t machid;
52 	struct nvmm_comm_page **pages;
53 	void *areas; /* opaque */
54 };
55 
56 struct nvmm_vcpu {
57 	nvmm_cpuid_t cpuid;
58 	struct nvmm_assist_callbacks cbs;
59 	struct nvmm_vcpu_state *state;
60 	struct nvmm_vcpu_event *event;
61 	struct nvmm_vcpu_exit *exit;
62 };
63 
64 struct nvmm_io {
65 	struct nvmm_machine *mach;
66 	struct nvmm_vcpu *vcpu;
67 	uint16_t port;
68 	bool in;
69 	size_t size;
70 	uint8_t *data;
71 };
72 
73 struct nvmm_mem {
74 	struct nvmm_machine *mach;
75 	struct nvmm_vcpu *vcpu;
76 	gpaddr_t gpa;
77 	bool write;
78 	size_t size;
79 	uint8_t *data;
80 };
81 
82 #define NVMM_VCPU_CONF_CALLBACKS	NVMM_VCPU_CONF_LIBNVMM_BEGIN
83 
84 #define NVMM_PROT_READ		0x01
85 #define NVMM_PROT_WRITE		0x02
86 #define NVMM_PROT_EXEC		0x04
87 #define NVMM_PROT_USER		0x08
88 #define NVMM_PROT_ALL		0x0F
89 typedef uint64_t nvmm_prot_t;
90 
91 int nvmm_init(void);
92 int nvmm_root_init(void);
93 
94 int nvmm_capability(struct nvmm_capability *);
95 
96 int nvmm_machine_create(struct nvmm_machine *);
97 int nvmm_machine_destroy(struct nvmm_machine *);
98 int nvmm_machine_configure(struct nvmm_machine *, uint64_t, void *);
99 
100 int nvmm_vcpu_create(struct nvmm_machine *, nvmm_cpuid_t, struct nvmm_vcpu *);
101 int nvmm_vcpu_destroy(struct nvmm_machine *, struct nvmm_vcpu *);
102 int nvmm_vcpu_configure(struct nvmm_machine *, struct nvmm_vcpu *, uint64_t,
103     void *);
104 int nvmm_vcpu_setstate(struct nvmm_machine *, struct nvmm_vcpu *, uint64_t);
105 int nvmm_vcpu_getstate(struct nvmm_machine *, struct nvmm_vcpu *, uint64_t);
106 int nvmm_vcpu_inject(struct nvmm_machine *, struct nvmm_vcpu *);
107 int nvmm_vcpu_run(struct nvmm_machine *, struct nvmm_vcpu *);
108 
109 int nvmm_gpa_map(struct nvmm_machine *, uintptr_t, gpaddr_t, size_t, int);
110 int nvmm_gpa_unmap(struct nvmm_machine *, uintptr_t, gpaddr_t, size_t);
111 int nvmm_hva_map(struct nvmm_machine *, uintptr_t, size_t);
112 int nvmm_hva_unmap(struct nvmm_machine *, uintptr_t, size_t);
113 
114 int nvmm_gva_to_gpa(struct nvmm_machine *, struct nvmm_vcpu *, gvaddr_t, gpaddr_t *,
115     nvmm_prot_t *);
116 int nvmm_gpa_to_hva(struct nvmm_machine *, gpaddr_t, uintptr_t *,
117     nvmm_prot_t *);
118 
119 int nvmm_assist_io(struct nvmm_machine *, struct nvmm_vcpu *);
120 int nvmm_assist_mem(struct nvmm_machine *, struct nvmm_vcpu *);
121 
122 int nvmm_ctl(int, void *, size_t);
123 
124 int nvmm_vcpu_dump(struct nvmm_machine *, struct nvmm_vcpu *);
125 
126 #endif /* _LIBNVMM_H_ */
127