xref: /illumos-gate/usr/src/uts/intel/io/vmm/sys/vmm_kernel.h (revision fdad6fbf87b201fdb96a704fc41fa8be1e4efbc8)
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  * This file and its contents are supplied under the terms of the
30  * Common Development and Distribution License ("CDDL"), version 1.0.
31  * You may only use this file in accordance with the terms of version
32  * 1.0 of the CDDL.
33  *
34  * A full copy of the text of the CDDL should have accompanied this
35  * source.  A copy of the CDDL is also available via the Internet at
36  * http://www.illumos.org/license/CDDL.
37  */
38 /* This file is dual-licensed; see usr/src/contrib/bhyve/LICENSE */
39 
40 /*
41  * Copyright 2015 Pluribus Networks Inc.
42  * Copyright 2019 Joyent, Inc.
43  * Copyright 2025 Oxide Computer Company
44  * Copyright 2021 OmniOS Community Edition (OmniOSce) Association.
45  */
46 
47 #ifndef _VMM_KERNEL_H_
48 #define	_VMM_KERNEL_H_
49 
50 #include <sys/sdt.h>
51 #include <x86/segments.h>
52 #include <sys/vmm.h>
53 #include <sys/vmm_data.h>
54 #include <sys/linker_set.h>
55 
56 SDT_PROVIDER_DECLARE(vmm);
57 
58 struct vm;
59 struct vm_exception;
60 struct seg_desc;
61 struct vm_exit;
62 struct vie;
63 struct vm_run;
64 struct vhpet;
65 struct vioapic;
66 struct vlapic;
67 struct vmspace;
68 struct vm_client;
69 struct vm_object;
70 struct vm_guest_paging;
71 struct vmm_data_req;
72 
73 /* Return values for architecture-specific calculation of the TSC multiplier */
74 typedef enum {
75 	FR_VALID,			/* valid multiplier, scaling needed */
76 	FR_SCALING_NOT_NEEDED,		/* scaling not required */
77 	FR_SCALING_NOT_SUPPORTED,	/* scaling not supported by platform */
78 	FR_OUT_OF_RANGE,		/* freq ratio out of supported range */
79 } freqratio_res_t;
80 
81 typedef int	(*vmm_init_func_t)(void);
82 typedef int	(*vmm_cleanup_func_t)(void);
83 typedef void	(*vmm_resume_func_t)(void);
84 typedef void *	(*vmi_init_func_t)(struct vm *vm);
85 typedef int	(*vmi_run_func_t)(void *vmi, int vcpu, uint64_t rip);
86 typedef void	(*vmi_cleanup_func_t)(void *vmi);
87 typedef int	(*vmi_get_register_t)(void *vmi, int vcpu, int num,
88     uint64_t *retval);
89 typedef int	(*vmi_set_register_t)(void *vmi, int vcpu, int num,
90     uint64_t val);
91 typedef int	(*vmi_get_desc_t)(void *vmi, int vcpu, int num,
92     struct seg_desc *desc);
93 typedef int	(*vmi_set_desc_t)(void *vmi, int vcpu, int num,
94     const struct seg_desc *desc);
95 typedef int	(*vmi_get_cap_t)(void *vmi, int vcpu, int num, int *retval);
96 typedef int	(*vmi_set_cap_t)(void *vmi, int vcpu, int num, int val);
97 typedef struct vlapic *(*vmi_vlapic_init)(void *vmi, int vcpu);
98 typedef void	(*vmi_vlapic_cleanup)(void *vmi, struct vlapic *vlapic);
99 typedef void	(*vmi_savectx)(void *vmi, int vcpu);
100 typedef void	(*vmi_restorectx)(void *vmi, int vcpu);
101 typedef void	(*vmi_pause_t)(void *vmi, int vcpu);
102 
103 typedef int	(*vmi_get_msr_t)(void *vmi, int vcpu, uint32_t msr,
104     uint64_t *valp);
105 typedef int	(*vmi_set_msr_t)(void *vmi, int vcpu, uint32_t msr,
106     uint64_t val);
107 typedef freqratio_res_t	(*vmi_freqratio_t)(uint64_t guest_hz,
108     uint64_t host_hz, uint64_t *mult);
109 
110 struct vmm_ops {
111 	vmm_init_func_t		init;		/* module wide initialization */
112 	vmm_cleanup_func_t	cleanup;
113 	vmm_resume_func_t	resume;
114 
115 	vmi_init_func_t		vminit;		/* vm-specific initialization */
116 	vmi_run_func_t		vmrun;
117 	vmi_cleanup_func_t	vmcleanup;
118 	vmi_get_register_t	vmgetreg;
119 	vmi_set_register_t	vmsetreg;
120 	vmi_get_desc_t		vmgetdesc;
121 	vmi_set_desc_t		vmsetdesc;
122 	vmi_get_cap_t		vmgetcap;
123 	vmi_set_cap_t		vmsetcap;
124 	vmi_vlapic_init		vlapic_init;
125 	vmi_vlapic_cleanup	vlapic_cleanup;
126 	vmi_pause_t		vmpause;
127 
128 	vmi_savectx		vmsavectx;
129 	vmi_restorectx		vmrestorectx;
130 
131 	vmi_get_msr_t		vmgetmsr;
132 	vmi_set_msr_t		vmsetmsr;
133 
134 	vmi_freqratio_t		vmfreqratio;
135 	uint32_t		fr_intsize;
136 	uint32_t		fr_fracsize;
137 };
138 
139 extern struct vmm_ops vmm_ops_intel;
140 extern struct vmm_ops vmm_ops_amd;
141 
142 int vm_create(uint64_t flags, struct vm **retvm);
143 void vm_destroy(struct vm *vm);
144 int vm_reinit(struct vm *vm, uint64_t);
145 uint16_t vm_get_maxcpus(struct vm *vm);
146 void vm_get_topology(struct vm *vm, uint16_t *sockets, uint16_t *cores,
147     uint16_t *threads, uint16_t *maxcpus);
148 int vm_set_topology(struct vm *vm, uint16_t sockets, uint16_t cores,
149     uint16_t threads, uint16_t maxcpus);
150 
151 int vm_pause_instance(struct vm *);
152 int vm_resume_instance(struct vm *);
153 bool vm_is_paused(struct vm *);
154 
155 /*
156  * APIs that race against hardware.
157  */
158 int vm_track_dirty_pages(struct vm *, uint64_t, size_t, uint8_t *);
159 int vm_npt_do_operation(struct vm *, uint64_t, size_t, uint32_t, uint8_t *,
160     int *);
161 
162 /*
163  * APIs that modify the guest memory map require all vcpus to be frozen.
164  */
165 int vm_mmap_memseg(struct vm *vm, vm_paddr_t gpa, int segid, vm_ooffset_t off,
166     size_t len, int prot, int flags);
167 int vm_munmap_memseg(struct vm *vm, vm_paddr_t gpa, size_t len);
168 int vm_alloc_memseg(struct vm *vm, int ident, size_t len, bool sysmem);
169 void vm_free_memseg(struct vm *vm, int ident);
170 int vm_map_mmio(struct vm *vm, vm_paddr_t gpa, size_t len, vm_paddr_t hpa);
171 int vm_unmap_mmio(struct vm *vm, vm_paddr_t gpa, size_t len);
172 int vm_assign_pptdev(struct vm *vm, int pptfd);
173 int vm_unassign_pptdev(struct vm *vm, int pptfd);
174 
175 /*
176  * APIs that inspect the guest memory map require only a *single* vcpu to
177  * be frozen. This acts like a read lock on the guest memory map since any
178  * modification requires *all* vcpus to be frozen.
179  */
180 int vm_mmap_getnext(struct vm *vm, vm_paddr_t *gpa, int *segid,
181     vm_ooffset_t *segoff, size_t *len, int *prot, int *flags);
182 int vm_get_memseg(struct vm *vm, int ident, size_t *len, bool *sysmem,
183     struct vm_object **objptr);
184 vm_paddr_t vmm_sysmem_maxaddr(struct vm *vm);
185 bool vm_mem_allocated(struct vm *vm, int vcpuid, vm_paddr_t gpa);
186 
187 int vm_get_register(struct vm *vm, int vcpu, int reg, uint64_t *retval);
188 int vm_set_register(struct vm *vm, int vcpu, int reg, uint64_t val);
189 int vm_get_seg_desc(struct vm *vm, int vcpu, int reg,
190     struct seg_desc *ret_desc);
191 int vm_set_seg_desc(struct vm *vm, int vcpu, int reg,
192     const struct seg_desc *desc);
193 int vm_get_run_state(struct vm *vm, int vcpuid, uint32_t *state,
194     uint8_t *sipi_vec);
195 int vm_set_run_state(struct vm *vm, int vcpuid, uint32_t state,
196     uint8_t sipi_vec);
197 int vm_get_fpu(struct vm *vm, int vcpuid, void *buf, size_t len);
198 int vm_set_fpu(struct vm *vm, int vcpuid, void *buf, size_t len);
199 int vm_run(struct vm *vm, int vcpuid, const struct vm_entry *);
200 int vm_suspend(struct vm *, enum vm_suspend_how, int);
201 int vm_inject_nmi(struct vm *vm, int vcpu);
202 bool vm_nmi_pending(struct vm *vm, int vcpuid);
203 void vm_nmi_clear(struct vm *vm, int vcpuid);
204 int vm_inject_extint(struct vm *vm, int vcpu);
205 bool vm_extint_pending(struct vm *vm, int vcpuid);
206 void vm_extint_clear(struct vm *vm, int vcpuid);
207 int vm_inject_init(struct vm *vm, int vcpuid);
208 int vm_inject_sipi(struct vm *vm, int vcpuid, uint8_t vec);
209 struct vlapic *vm_lapic(struct vm *vm, int cpu);
210 struct vioapic *vm_ioapic(struct vm *vm);
211 struct vhpet *vm_hpet(struct vm *vm);
212 int vm_get_capability(struct vm *vm, int vcpu, int type, int *val);
213 int vm_set_capability(struct vm *vm, int vcpu, int type, int val);
214 int vm_get_x2apic_state(struct vm *vm, int vcpu, enum x2apic_state *state);
215 int vm_set_x2apic_state(struct vm *vm, int vcpu, enum x2apic_state state);
216 int vm_apicid2vcpuid(struct vm *vm, int apicid);
217 int vm_activate_cpu(struct vm *vm, int vcpu);
218 int vm_suspend_cpu(struct vm *vm, int vcpu);
219 int vm_resume_cpu(struct vm *vm, int vcpu);
220 struct vm_exit *vm_exitinfo(struct vm *vm, int vcpuid);
221 struct vie *vm_vie_ctx(struct vm *vm, int vcpuid);
222 void vm_exit_suspended(struct vm *vm, int vcpuid, uint64_t rip);
223 void vm_exit_debug(struct vm *vm, int vcpuid, uint64_t rip);
224 void vm_exit_astpending(struct vm *vm, int vcpuid, uint64_t rip);
225 void vm_exit_reqidle(struct vm *vm, int vcpuid, uint64_t rip);
226 void vm_exit_run_state(struct vm *vm, int vcpuid, uint64_t rip);
227 int vm_service_mmio_read(struct vm *vm, int cpuid, uint64_t gpa, uint64_t *rval,
228     int rsize);
229 int vm_service_mmio_write(struct vm *vm, int cpuid, uint64_t gpa, uint64_t wval,
230     int wsize);
231 
232 #ifdef _SYS__CPUSET_H_
233 cpuset_t vm_active_cpus(struct vm *vm);
234 cpuset_t vm_debug_cpus(struct vm *vm);
235 #endif	/* _SYS__CPUSET_H_ */
236 
237 bool vcpu_entry_bailout_checks(struct vm *vm, int vcpuid, uint64_t rip);
238 bool vcpu_run_state_pending(struct vm *vm, int vcpuid);
239 int vcpu_arch_reset(struct vm *vm, int vcpuid, bool init_only);
240 int vm_vcpu_barrier(struct vm *, int);
241 
242 /*
243  * Return true if device indicated by bus/slot/func is supposed to be a
244  * pci passthrough device.
245  *
246  * Return false otherwise.
247  */
248 bool vmm_is_pptdev(int bus, int slot, int func);
249 
250 void *vm_iommu_domain(struct vm *vm);
251 
252 enum vcpu_state {
253 	VCPU_IDLE,
254 	VCPU_FROZEN,
255 	VCPU_RUNNING,
256 	VCPU_SLEEPING,
257 };
258 
259 int vcpu_set_state(struct vm *vm, int vcpu, enum vcpu_state state,
260     bool from_idle);
261 enum vcpu_state vcpu_get_state(struct vm *vm, int vcpu, int *hostcpu);
262 void vcpu_block_run(struct vm *, int);
263 void vcpu_unblock_run(struct vm *, int);
264 
265 uint64_t vcpu_tsc_offset(struct vm *vm, int vcpuid, bool phys_adj);
266 hrtime_t vm_normalize_hrtime(struct vm *, hrtime_t);
267 hrtime_t vm_denormalize_hrtime(struct vm *, hrtime_t);
268 uint64_t vm_get_freq_multiplier(struct vm *);
269 
270 static __inline bool
vcpu_is_running(struct vm * vm,int vcpu,int * hostcpu)271 vcpu_is_running(struct vm *vm, int vcpu, int *hostcpu)
272 {
273 	return (vcpu_get_state(vm, vcpu, hostcpu) == VCPU_RUNNING);
274 }
275 
276 #ifdef _SYS_THREAD_H
277 static __inline int
vcpu_should_yield(struct vm * vm,int vcpu)278 vcpu_should_yield(struct vm *vm, int vcpu)
279 {
280 
281 	if (curthread->t_astflag)
282 		return (1);
283 	else if (CPU->cpu_runrun)
284 		return (1);
285 	else
286 		return (0);
287 }
288 #endif /* _SYS_THREAD_H */
289 
290 typedef enum vcpu_notify {
291 	VCPU_NOTIFY_NONE,
292 	VCPU_NOTIFY_APIC,	/* Posted intr notification (if possible) */
293 	VCPU_NOTIFY_EXIT,	/* IPI to cause VM exit */
294 } vcpu_notify_t;
295 
296 void *vcpu_stats(struct vm *vm, int vcpu);
297 void vcpu_notify_event(struct vm *vm, int vcpuid);
298 void vcpu_notify_event_type(struct vm *vm, int vcpuid, vcpu_notify_t);
299 void *vm_get_cookie(struct vm *);
300 struct vmspace *vm_get_vmspace(struct vm *vm);
301 struct vm_client *vm_get_vmclient(struct vm *vm, int vcpuid);
302 struct vatpic *vm_atpic(struct vm *vm);
303 struct vatpit *vm_atpit(struct vm *vm);
304 struct vpmtmr *vm_pmtmr(struct vm *vm);
305 struct vrtc *vm_rtc(struct vm *vm);
306 
307 /*
308  * Inject exception 'vector' into the guest vcpu. This function returns 0 on
309  * success and non-zero on failure.
310  *
311  * Wrapper functions like 'vm_inject_gp()' should be preferred to calling
312  * this function directly because they enforce the trap-like or fault-like
313  * behavior of an exception.
314  *
315  * This function should only be called in the context of the thread that is
316  * executing this vcpu.
317  */
318 int vm_inject_exception(struct vm *vm, int vcpuid, uint8_t vector,
319     bool err_valid, uint32_t errcode, bool restart_instruction);
320 
321 /*
322  * This function is called after a VM-exit that occurred during exception or
323  * interrupt delivery through the IDT. The format of 'intinfo' is described
324  * in Figure 15-1, "EXITINTINFO for All Intercepts", APM, Vol 2.
325  *
326  * If a VM-exit handler completes the event delivery successfully then it
327  * should call vm_exit_intinfo() to extinguish the pending event. For e.g.,
328  * if the task switch emulation is triggered via a task gate then it should
329  * call this function with 'intinfo=0' to indicate that the external event
330  * is not pending anymore.
331  *
332  * Return value is 0 on success and non-zero on failure.
333  */
334 int vm_exit_intinfo(struct vm *vm, int vcpuid, uint64_t intinfo);
335 
336 /*
337  * This function is called before every VM-entry to retrieve a pending
338  * event that should be injected into the guest. This function combines
339  * nested events into a double or triple fault.
340  *
341  * Returns false if there are no events that need to be injected into the guest.
342  */
343 bool vm_entry_intinfo(struct vm *vm, int vcpuid, uint64_t *info);
344 
345 int vm_get_intinfo(struct vm *vm, int vcpuid, uint64_t *info1, uint64_t *info2);
346 
347 enum vm_reg_name vm_segment_name(int seg_encoding);
348 
349 struct vm_copyinfo {
350 	uint64_t	gpa;
351 	size_t		len;
352 	int		prot;
353 	void		*hva;
354 	void		*cookie;
355 };
356 
357 /*
358  * Set up 'copyinfo[]' to copy to/from guest linear address space starting
359  * at 'gla' and 'len' bytes long. The 'prot' should be set to PROT_READ for
360  * a copyin or PROT_WRITE for a copyout.
361  *
362  * retval	is_fault	Interpretation
363  *   0		   0		Success
364  *   0		   1		An exception was injected into the guest
365  * EFAULT	  N/A		Unrecoverable error
366  *
367  * The 'copyinfo[]' can be passed to 'vm_copyin()' or 'vm_copyout()' only if
368  * the return value is 0. The 'copyinfo[]' resources should be freed by calling
369  * 'vm_copy_teardown()' after the copy is done.
370  */
371 int vm_copy_setup(struct vm *vm, int vcpuid, struct vm_guest_paging *paging,
372     uint64_t gla, size_t len, int prot, struct vm_copyinfo *copyinfo,
373     uint_t num_copyinfo, int *is_fault);
374 void vm_copy_teardown(struct vm *vm, int vcpuid, struct vm_copyinfo *copyinfo,
375     uint_t num_copyinfo);
376 void vm_copyin(struct vm *vm, int vcpuid, struct vm_copyinfo *copyinfo,
377     void *kaddr, size_t len);
378 void vm_copyout(struct vm *vm, int vcpuid, const void *kaddr,
379     struct vm_copyinfo *copyinfo, size_t len);
380 
381 int vcpu_trace_exceptions(struct vm *vm, int vcpuid);
382 int vcpu_trap_wbinvd(struct vm *vm, int vcpuid);
383 
384 void vm_inject_ud(struct vm *vm, int vcpuid);
385 void vm_inject_gp(struct vm *vm, int vcpuid);
386 void vm_inject_ac(struct vm *vm, int vcpuid, uint32_t errcode);
387 void vm_inject_ss(struct vm *vm, int vcpuid, uint32_t errcode);
388 void vm_inject_pf(struct vm *vm, int vcpuid, uint32_t errcode, uint64_t cr2);
389 
390 /*
391  * Both SVM and VMX have complex logic for injecting events such as exceptions
392  * or interrupts into the guest.  Within those two backends, the progress of
393  * event injection is tracked by event_inject_state, hopefully making it easier
394  * to reason about.
395  */
396 enum event_inject_state {
397 	EIS_CAN_INJECT	= 0, /* exception/interrupt can be injected */
398 	EIS_EV_EXISTING	= 1, /* blocked by existing event */
399 	EIS_EV_INJECTED	= 2, /* blocked by injected event */
400 	EIS_GI_BLOCK	= 3, /* blocked by guest interruptability */
401 
402 	/*
403 	 * Flag to request an immediate exit from VM context after event
404 	 * injection in order to perform more processing
405 	 */
406 	EIS_REQ_EXIT	= (1 << 15),
407 };
408 
409 /* Possible result codes for MSR access emulation */
410 typedef enum vm_msr_result {
411 	VMR_OK		= 0, /* succesfully emulated */
412 	VMR_GP		= 1, /* #GP should be injected */
413 	VMR_UNHANLDED	= 2, /* handle in userspace, kernel cannot emulate */
414 } vm_msr_result_t;
415 
416 enum vm_cpuid_capability {
417 	VCC_NONE,
418 	VCC_NO_EXECUTE,
419 	VCC_FFXSR,
420 	VCC_TCE,
421 	VCC_LAST
422 };
423 
424 /* Possible flags and entry count limit definited in sys/vmm.h */
425 typedef struct vcpu_cpuid_config {
426 	uint32_t		vcc_flags;
427 	uint32_t		vcc_nent;
428 	struct vcpu_cpuid_entry	*vcc_entries;
429 } vcpu_cpuid_config_t;
430 
431 vcpu_cpuid_config_t *vm_cpuid_config(struct vm *, int);
432 int vm_get_cpuid(struct vm *, int, vcpu_cpuid_config_t *);
433 int vm_set_cpuid(struct vm *, int, const vcpu_cpuid_config_t *);
434 void vcpu_emulate_cpuid(struct vm *, int, uint64_t *, uint64_t *, uint64_t *,
435     uint64_t *);
436 void legacy_emulate_cpuid(struct vm *, int, uint32_t *, uint32_t *, uint32_t *,
437     uint32_t *);
438 void vcpu_cpuid_init(vcpu_cpuid_config_t *);
439 void vcpu_cpuid_cleanup(vcpu_cpuid_config_t *);
440 
441 bool vm_cpuid_capability(struct vm *, int, enum vm_cpuid_capability);
442 bool validate_guest_xcr0(uint64_t, uint64_t);
443 
444 void vmm_sol_glue_init(void);
445 void vmm_sol_glue_cleanup(void);
446 
447 void *vmm_contig_alloc(size_t);
448 void vmm_contig_free(void *, size_t);
449 
450 int vmm_mod_load(void);
451 int vmm_mod_unload(void);
452 
453 bool vmm_check_iommu(void);
454 
455 void vmm_call_trap(uint64_t);
456 
457 uint64_t vmm_host_tsc_delta(void);
458 
459 /*
460  * Because of tangled headers, this is not exposed directly via the vmm_drv
461  * interface, but rather mirrored as vmm_drv_iop_cb_t in vmm_drv.h.
462  */
463 typedef int (*ioport_handler_t)(void *, bool, uint16_t, uint8_t, uint32_t *);
464 
465 int vm_ioport_access(struct vm *vm, int vcpuid, bool in, uint16_t port,
466     uint8_t bytes, uint32_t *val);
467 
468 int vm_ioport_attach(struct vm *vm, uint16_t port, ioport_handler_t func,
469     void *arg, void **cookie);
470 int vm_ioport_detach(struct vm *vm, void **cookie, ioport_handler_t *old_func,
471     void **old_arg);
472 
473 int vm_ioport_hook(struct vm *, uint16_t, ioport_handler_t, void *, void **);
474 void vm_ioport_unhook(struct vm *, void **);
475 
476 enum vcpu_ustate {
477 	VU_INIT = 0,	/* initialized but has not yet attempted to run */
478 	VU_RUN,		/* running in guest context */
479 	VU_IDLE,	/* idle (HLTed, wait-for-SIPI, etc) */
480 	VU_EMU_KERN,	/* emulation performed in-kernel */
481 	VU_EMU_USER,	/* emulation performed in userspace */
482 	VU_SCHED,	/* off-cpu for interrupt, preempt, lock contention */
483 	VU_MAX
484 };
485 
486 void vcpu_ustate_change(struct vm *, int, enum vcpu_ustate);
487 
488 typedef struct vmm_kstats {
489 	kstat_named_t	vk_name;
490 } vmm_kstats_t;
491 
492 typedef struct vmm_vcpu_kstats {
493 	kstat_named_t	vvk_vcpu;
494 	kstat_named_t	vvk_time_init;
495 	kstat_named_t	vvk_time_run;
496 	kstat_named_t	vvk_time_idle;
497 	kstat_named_t	vvk_time_emu_kern;
498 	kstat_named_t	vvk_time_emu_user;
499 	kstat_named_t	vvk_time_sched;
500 } vmm_vcpu_kstats_t;
501 
502 #define	VMM_KSTAT_CLASS	"misc"
503 
504 int vmm_kstat_update_vcpu(struct kstat *, int);
505 
506 typedef struct vmm_data_req {
507 	uint16_t	vdr_class;
508 	uint16_t	vdr_version;
509 	uint32_t	vdr_flags;
510 	uint32_t	vdr_len;
511 	void		*vdr_data;
512 	uint32_t	*vdr_result_len;
513 	int		vdr_vcpuid;
514 } vmm_data_req_t;
515 
516 typedef int (*vmm_data_writef_t)(void *, const vmm_data_req_t *);
517 typedef int (*vmm_data_readf_t)(void *, const vmm_data_req_t *);
518 typedef int (*vmm_data_vcpu_writef_t)(struct vm *, int, const vmm_data_req_t *);
519 typedef int (*vmm_data_vcpu_readf_t)(struct vm *, int, const vmm_data_req_t *);
520 
521 typedef struct vmm_data_version_entry {
522 	uint16_t		vdve_class;
523 	uint16_t		vdve_version;
524 
525 	/*
526 	 * If these handlers accept/emit a single item of a fixed length, it
527 	 * should be specified in vdve_len_expect.  The vmm-data logic will then
528 	 * ensure that requests possess at least that specified length before
529 	 * calling into the defined handlers.
530 	 */
531 	uint16_t		vdve_len_expect;
532 
533 	/*
534 	 * For handlers which deal with (potentially) multiple items of a fixed
535 	 * length, vdve_len_per_item is used to hint (via the VDC_VERSION class)
536 	 * to userspace what that item size is.  Although not strictly mutually
537 	 * exclusive with vdve_len_expect, it is nonsensical to set them both.
538 	 */
539 	uint16_t		vdve_len_per_item;
540 
541 	/*
542 	 * A vmm-data handler is expected to provide read/write functions which
543 	 * are either VM-wide (via vdve_readf and vdve_writef) or per-vCPU
544 	 * (via vdve_vcpu_readf and vdve_vcpu_writef).  Providing both is not
545 	 * allowed (but is not currently checked at compile time).
546 	 */
547 
548 	/* VM-wide handlers */
549 	vmm_data_readf_t	vdve_readf;
550 	vmm_data_writef_t	vdve_writef;
551 
552 	/* Per-vCPU handlers */
553 	vmm_data_vcpu_readf_t	vdve_vcpu_readf;
554 	vmm_data_vcpu_writef_t	vdve_vcpu_writef;
555 
556 	/*
557 	 * The vdve_vcpu_readf/writef handlers can rely on vcpuid to be within
558 	 * the [0, VM_MAXCPU) bounds.  If they also can handle vcpuid == -1 (for
559 	 * VM-wide data), then they can opt into such cases by setting
560 	 * vdve_vcpu_wildcard to true.
561 	 *
562 	 * At a later time, it would make sense to improve the logic so a
563 	 * vmm-data class could define both the VM-wide and per-vCPU handlers,
564 	 * letting the incoming vcpuid determine which would be called.  Until
565 	 * then, vdve_vcpu_wildcard is the stopgap.
566 	 */
567 	bool			vdve_vcpu_wildcard;
568 } vmm_data_version_entry_t;
569 
570 #define	VMM_DATA_VERSION(sym)	SET_ENTRY(vmm_data_version_entries, sym)
571 
572 int vmm_data_read(struct vm *, const vmm_data_req_t *);
573 int vmm_data_write(struct vm *, const vmm_data_req_t *);
574 
575 /*
576  * TSC Scaling
577  */
578 uint64_t vmm_calc_freq_multiplier(uint64_t guest_hz, uint64_t host_hz,
579     uint32_t frac);
580 
581 /* represents a multiplier for a guest in which no scaling is required */
582 #define	VM_TSCM_NOSCALE	0
583 
584 #endif /* _VMM_KERNEL_H_ */
585