nvmm: Rename a few things for clarity
[dragonfly.git] / sys / dev / virtual / nvmm / nvmm_internal.h
1 /*      $NetBSD: nvmm_internal.h,v 1.20 2021/03/26 15:59:53 reinoud 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 _NVMM_INTERNAL_H_
32 #define _NVMM_INTERNAL_H_
33
34 #ifndef _KERNEL
35 #error "This file should not be included by userland programs."
36 #endif
37
38 #define NVMM_MAX_MACHINES       128
39 #define NVMM_MAX_VCPUS          256
40 #define NVMM_MAX_HMAPPINGS      32
41 #define NVMM_MAX_RAM            (128ULL * 1024ULL * (1 << 30))
42
43 struct nvmm_owner {
44         pid_t pid;
45 };
46
47 struct nvmm_cpu {
48         /* Shared. */
49         bool present;
50         nvmm_cpuid_t cpuid;
51         kmutex_t lock;
52
53         /* Comm page. */
54         struct nvmm_comm_page *comm;
55
56         /* Last host CPU on which the VCPU ran. */
57         int hcpu_last;
58
59         /* Implementation-specific. */
60         void *cpudata;
61 };
62
63 struct nvmm_hmapping {
64         bool present;
65         uintptr_t hva;
66         size_t size;
67         struct uvm_object *vmobj;
68 };
69
70 struct nvmm_machine {
71         bool present;
72         nvmm_machid_t machid;
73         time_t time;
74         struct nvmm_owner *owner;
75         krwlock_t lock;
76
77         /* Comm */
78         struct uvm_object *commvmobj;
79
80         /* Kernel */
81         struct vmspace *vm;
82         gpaddr_t gpa_begin;
83         gpaddr_t gpa_end;
84
85         /* Host Mappings */
86         struct nvmm_hmapping hmap[NVMM_MAX_HMAPPINGS];
87
88         /* CPU */
89         volatile unsigned int ncpus;
90         struct nvmm_cpu cpus[NVMM_MAX_VCPUS];
91
92         /* Implementation-specific */
93         void *machdata;
94 };
95
96 struct nvmm_impl {
97         const char *name;
98         bool (*ident)(void);
99         void (*init)(void);
100         void (*fini)(void);
101         void (*capability)(struct nvmm_capability *);
102
103         size_t mach_conf_max;
104         const size_t *mach_conf_sizes;
105
106         size_t vcpu_conf_max;
107         const size_t *vcpu_conf_sizes;
108
109         size_t state_size;
110
111         void (*machine_create)(struct nvmm_machine *);
112         void (*machine_destroy)(struct nvmm_machine *);
113         int (*machine_configure)(struct nvmm_machine *, uint64_t, void *);
114
115         int (*vcpu_create)(struct nvmm_machine *, struct nvmm_cpu *);
116         void (*vcpu_destroy)(struct nvmm_machine *, struct nvmm_cpu *);
117         int (*vcpu_configure)(struct nvmm_cpu *, uint64_t, void *);
118         void (*vcpu_setstate)(struct nvmm_cpu *);
119         void (*vcpu_getstate)(struct nvmm_cpu *);
120         int (*vcpu_inject)(struct nvmm_cpu *);
121         int (*vcpu_run)(struct nvmm_machine *, struct nvmm_cpu *,
122             struct nvmm_vcpu_exit *);
123 };
124
125 #if defined(__x86_64__)
126 extern const struct nvmm_impl nvmm_x86_svm;
127 extern const struct nvmm_impl nvmm_x86_vmx;
128 #endif
129
130 static inline bool
131 nvmm_return_needed(void)
132 {
133         if (__predict_false(nvmm_break_wanted())) {
134                 return true;
135         }
136         if (__predict_false(curlwp->lwp_mpflags & LWP_MP_URETMASK)) {
137                 return true;
138         }
139         return false;
140 }
141
142 #endif /* _NVMM_INTERNAL_H_ */