1221828Sgrehan/*-
2221828Sgrehan * Copyright (c) 2011 NetApp, Inc.
3221828Sgrehan * All rights reserved.
4221828Sgrehan *
5221828Sgrehan * Redistribution and use in source and binary forms, with or without
6221828Sgrehan * modification, are permitted provided that the following conditions
7221828Sgrehan * are met:
8221828Sgrehan * 1. Redistributions of source code must retain the above copyright
9221828Sgrehan *    notice, this list of conditions and the following disclaimer.
10221828Sgrehan * 2. Redistributions in binary form must reproduce the above copyright
11221828Sgrehan *    notice, this list of conditions and the following disclaimer in the
12221828Sgrehan *    documentation and/or other materials provided with the distribution.
13221828Sgrehan *
14221828Sgrehan * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
15221828Sgrehan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16221828Sgrehan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17221828Sgrehan * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
18221828Sgrehan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19221828Sgrehan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20221828Sgrehan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21221828Sgrehan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22221828Sgrehan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23221828Sgrehan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24221828Sgrehan * SUCH DAMAGE.
25221828Sgrehan *
26221828Sgrehan * $FreeBSD: stable/11/lib/libvmmapi/vmmapi.h 348201 2019-05-23 21:23:18Z rgrimes $
27221828Sgrehan */
28221828Sgrehan
29221828Sgrehan#ifndef _VMMAPI_H_
30221828Sgrehan#define	_VMMAPI_H_
31221828Sgrehan
32266933Sneel#include <sys/param.h>
33266933Sneel#include <sys/cpuset.h>
34266933Sneel
35277310Sneel/*
36277310Sneel * API version for out-of-tree consumers like grub-bhyve for making compile
37277310Sneel * time decisions.
38277310Sneel */
39320866Sgrehan#define	VMMAPI_VERSION	0103	/* 2 digit major followed by 2 digit minor */
40277310Sneel
41266708Sneelstruct iovec;
42221828Sgrehanstruct vmctx;
43240922Sneelenum x2apic_state;
44221828Sgrehan
45248477Sneel/*
46248477Sneel * Different styles of mapping the memory assigned to a VM into the address
47248477Sneel * space of the controlling process.
48248477Sneel */
49248477Sneelenum vm_mmap_style {
50248477Sneel	VM_MMAP_NONE,		/* no mapping */
51248477Sneel	VM_MMAP_ALL,		/* fully and statically mapped */
52248477Sneel	VM_MMAP_SPARSE,		/* mappings created on-demand */
53248477Sneel};
54248477Sneel
55284539Sneel/*
56284539Sneel * 'flags' value passed to 'vm_set_memflags()'.
57284539Sneel */
58265951Sneel#define	VM_MEM_F_INCORE	0x01	/* include guest memory in core file */
59284539Sneel#define	VM_MEM_F_WIRED	0x02	/* guest memory is wired */
60265951Sneel
61284539Sneel/*
62284539Sneel * Identifiers for memory segments:
63284539Sneel * - vm_setup_memory() uses VM_SYSMEM for the system memory segment.
64284539Sneel * - the remaining identifiers can be used to create devmem segments.
65284539Sneel */
66284539Sneelenum {
67284539Sneel	VM_SYSMEM,
68284539Sneel	VM_BOOTROM,
69284539Sneel	VM_FRAMEBUFFER,
70284539Sneel};
71284539Sneel
72284539Sneel/*
73284539Sneel * Get the length and name of the memory segment identified by 'segid'.
74284539Sneel * Note that system memory segments are identified with a nul name.
75284539Sneel *
76284539Sneel * Returns 0 on success and non-zero otherwise.
77284539Sneel */
78284539Sneelint	vm_get_memseg(struct vmctx *ctx, int ident, size_t *lenp, char *name,
79284539Sneel	    size_t namesiz);
80284539Sneel
81284539Sneel/*
82284539Sneel * Iterate over the guest address space. This function finds an address range
83284539Sneel * that starts at an address >= *gpa.
84284539Sneel *
85284539Sneel * Returns 0 if the next address range was found and non-zero otherwise.
86284539Sneel */
87284539Sneelint	vm_mmap_getnext(struct vmctx *ctx, vm_paddr_t *gpa, int *segid,
88284539Sneel	    vm_ooffset_t *segoff, size_t *len, int *prot, int *flags);
89284539Sneel/*
90284539Sneel * Create a device memory segment identified by 'segid'.
91284539Sneel *
92284539Sneel * Returns a pointer to the memory segment on success and MAP_FAILED otherwise.
93284539Sneel */
94284539Sneelvoid	*vm_create_devmem(struct vmctx *ctx, int segid, const char *name,
95284539Sneel	    size_t len);
96284539Sneel
97284539Sneel/*
98284539Sneel * Map the memory segment identified by 'segid' into the guest address space
99284539Sneel * at [gpa,gpa+len) with protection 'prot'.
100284539Sneel */
101284539Sneelint	vm_mmap_memseg(struct vmctx *ctx, vm_paddr_t gpa, int segid,
102284539Sneel	    vm_ooffset_t segoff, size_t len, int prot);
103284539Sneel
104221828Sgrehanint	vm_create(const char *name);
105320866Sgrehanint	vm_get_device_fd(struct vmctx *ctx);
106221828Sgrehanstruct vmctx *vm_open(const char *name);
107221828Sgrehanvoid	vm_destroy(struct vmctx *ctx);
108256176Sneelint	vm_parse_memsize(const char *optarg, size_t *memsize);
109248477Sneelint	vm_setup_memory(struct vmctx *ctx, size_t len, enum vm_mmap_style s);
110248477Sneelvoid	*vm_map_gpa(struct vmctx *ctx, vm_paddr_t gaddr, size_t len);
111256072Sneelint	vm_get_gpa_pmap(struct vmctx *, uint64_t gpa, uint64_t *pte, int *num);
112280929Stychonint	vm_gla2gpa(struct vmctx *, int vcpuid, struct vm_guest_paging *paging,
113282558Sneel		   uint64_t gla, int prot, uint64_t *gpa, int *fault);
114248477Sneeluint32_t vm_get_lowmem_limit(struct vmctx *ctx);
115248477Sneelvoid	vm_set_lowmem_limit(struct vmctx *ctx, uint32_t limit);
116265951Sneelvoid	vm_set_memflags(struct vmctx *ctx, int flags);
117284539Sneelint	vm_get_memflags(struct vmctx *ctx);
118267811Sneelsize_t	vm_get_lowmem_size(struct vmctx *ctx);
119267811Sneelsize_t	vm_get_highmem_size(struct vmctx *ctx);
120221828Sgrehanint	vm_set_desc(struct vmctx *ctx, int vcpu, int reg,
121221828Sgrehan		    uint64_t base, uint32_t limit, uint32_t access);
122221828Sgrehanint	vm_get_desc(struct vmctx *ctx, int vcpu, int reg,
123221828Sgrehan		    uint64_t *base, uint32_t *limit, uint32_t *access);
124269008Sneelint	vm_get_seg_desc(struct vmctx *ctx, int vcpu, int reg,
125269008Sneel			struct seg_desc *seg_desc);
126221828Sgrehanint	vm_set_register(struct vmctx *ctx, int vcpu, int reg, uint64_t val);
127221828Sgrehanint	vm_get_register(struct vmctx *ctx, int vcpu, int reg, uint64_t *retval);
128277310Sneelint	vm_run(struct vmctx *ctx, int vcpu, struct vm_exit *ret_vmexit);
129265062Sneelint	vm_suspend(struct vmctx *ctx, enum vm_suspend_how how);
130267216Sneelint	vm_reinit(struct vmctx *ctx);
131239026Sneelint	vm_apicid2vcpu(struct vmctx *ctx, int apicid);
132277310Sneelint	vm_inject_exception(struct vmctx *ctx, int vcpu, int vector,
133277310Sneel    int errcode_valid, uint32_t errcode, int restart_instruction);
134221828Sgrehanint	vm_lapic_irq(struct vmctx *ctx, int vcpu, int vector);
135259779Sjhbint	vm_lapic_local_irq(struct vmctx *ctx, int vcpu, int vector);
136259482Sneelint	vm_lapic_msi(struct vmctx *ctx, uint64_t addr, uint64_t msg);
137258075Sneelint	vm_ioapic_assert_irq(struct vmctx *ctx, int irq);
138258075Sneelint	vm_ioapic_deassert_irq(struct vmctx *ctx, int irq);
139258494Sneelint	vm_ioapic_pulse_irq(struct vmctx *ctx, int irq);
140261268Sjhbint	vm_ioapic_pincount(struct vmctx *ctx, int *pincount);
141264468Stychonint	vm_isa_assert_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq);
142263035Stychonint	vm_isa_deassert_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq);
143263035Stychonint	vm_isa_pulse_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq);
144266125Sjhbint	vm_isa_set_irq_trigger(struct vmctx *ctx, int atpic_irq,
145266125Sjhb	    enum vm_intr_trigger trigger);
146221828Sgrehanint	vm_inject_nmi(struct vmctx *ctx, int vcpu);
147221828Sgrehanint	vm_capability_name2type(const char *capname);
148241486Sneelconst char *vm_capability_type2name(int type);
149221828Sgrehanint	vm_get_capability(struct vmctx *ctx, int vcpu, enum vm_cap_type cap,
150221828Sgrehan			  int *retval);
151221828Sgrehanint	vm_set_capability(struct vmctx *ctx, int vcpu, enum vm_cap_type cap,
152221828Sgrehan			  int val);
153221828Sgrehanint	vm_assign_pptdev(struct vmctx *ctx, int bus, int slot, int func);
154221828Sgrehanint	vm_unassign_pptdev(struct vmctx *ctx, int bus, int slot, int func);
155221828Sgrehanint	vm_map_pptdev_mmio(struct vmctx *ctx, int bus, int slot, int func,
156221828Sgrehan			   vm_paddr_t gpa, size_t len, vm_paddr_t hpa);
157259537Sneelint	vm_setup_pptdev_msi(struct vmctx *ctx, int vcpu, int bus, int slot,
158259537Sneel	    int func, uint64_t addr, uint64_t msg, int numvec);
159259537Sneelint	vm_setup_pptdev_msix(struct vmctx *ctx, int vcpu, int bus, int slot,
160259537Sneel	    int func, int idx, uint64_t addr, uint64_t msg,
161259537Sneel	    uint32_t vector_control);
162221828Sgrehan
163268889Sneelint	vm_get_intinfo(struct vmctx *ctx, int vcpu, uint64_t *i1, uint64_t *i2);
164268889Sneelint	vm_set_intinfo(struct vmctx *ctx, int vcpu, uint64_t exit_intinfo);
165268889Sneel
166320866Sgrehanconst cap_ioctl_t *vm_get_ioctls(size_t *len);
167320866Sgrehan
168221828Sgrehan/*
169221828Sgrehan * Return a pointer to the statistics buffer. Note that this is not MT-safe.
170221828Sgrehan */
171221828Sgrehanuint64_t *vm_get_stats(struct vmctx *ctx, int vcpu, struct timeval *ret_tv,
172221828Sgrehan		       int *ret_entries);
173221828Sgrehanconst char *vm_get_stat_desc(struct vmctx *ctx, int index);
174221828Sgrehan
175240922Sneelint	vm_get_x2apic_state(struct vmctx *ctx, int vcpu, enum x2apic_state *s);
176240922Sneelint	vm_set_x2apic_state(struct vmctx *ctx, int vcpu, enum x2apic_state s);
177240922Sneel
178258579Sneelint	vm_get_hpet_capabilities(struct vmctx *ctx, uint32_t *capabilities);
179258579Sneel
180266708Sneel/*
181266708Sneel * Translate the GLA range [gla,gla+len) into GPA segments in 'iov'.
182298896Spfg * The 'iovcnt' should be big enough to accommodate all GPA segments.
183282558Sneel *
184282558Sneel * retval	fault		Interpretation
185282558Sneel *   0		  0		Success
186282558Sneel *   0		  1		An exception was injected into the guest
187282558Sneel * EFAULT	 N/A		Error
188266708Sneel */
189269008Sneelint	vm_copy_setup(struct vmctx *ctx, int vcpu, struct vm_guest_paging *pg,
190282558Sneel	    uint64_t gla, size_t len, int prot, struct iovec *iov, int iovcnt,
191282558Sneel	    int *fault);
192266708Sneelvoid	vm_copyin(struct vmctx *ctx, int vcpu, struct iovec *guest_iov,
193266708Sneel	    void *host_dst, size_t len);
194266708Sneelvoid	vm_copyout(struct vmctx *ctx, int vcpu, const void *host_src,
195266708Sneel	    struct iovec *guest_iov, size_t len);
196277359Sneelvoid	vm_copy_teardown(struct vmctx *ctx, int vcpu, struct iovec *iov,
197277359Sneel	    int iovcnt);
198266633Sneel
199276428Sneel/* RTC */
200276428Sneelint	vm_rtc_write(struct vmctx *ctx, int offset, uint8_t value);
201276428Sneelint	vm_rtc_read(struct vmctx *ctx, int offset, uint8_t *retval);
202276428Sneelint	vm_rtc_settime(struct vmctx *ctx, time_t secs);
203276428Sneelint	vm_rtc_gettime(struct vmctx *ctx, time_t *secs);
204276428Sneel
205221828Sgrehan/* Reset vcpu register state */
206221828Sgrehanint	vcpu_reset(struct vmctx *ctx, int vcpu);
207221828Sgrehan
208266933Sneelint	vm_active_cpus(struct vmctx *ctx, cpuset_t *cpus);
209266933Sneelint	vm_suspended_cpus(struct vmctx *ctx, cpuset_t *cpus);
210266933Sneelint	vm_activate_cpu(struct vmctx *ctx, int vcpu);
211266933Sneel
212348201Srgrimes/* CPU topology */
213348201Srgrimesint	vm_set_topology(struct vmctx *ctx, uint16_t sockets, uint16_t cores,
214348201Srgrimes	    uint16_t threads, uint16_t maxcpus);
215348201Srgrimesint	vm_get_topology(struct vmctx *ctx, uint16_t *sockets, uint16_t *cores,
216348201Srgrimes	    uint16_t *threads, uint16_t *maxcpus);
217348201Srgrimes
218221828Sgrehan/*
219221828Sgrehan * FreeBSD specific APIs
220221828Sgrehan */
221221828Sgrehanint	vm_setup_freebsd_registers(struct vmctx *ctx, int vcpu,
222221828Sgrehan				uint64_t rip, uint64_t cr3, uint64_t gdtbase,
223221828Sgrehan				uint64_t rsp);
224261504Sjhbint	vm_setup_freebsd_registers_i386(struct vmctx *vmctx, int vcpu,
225261504Sjhb					uint32_t eip, uint32_t gdtbase,
226261504Sjhb					uint32_t esp);
227221828Sgrehanvoid	vm_setup_freebsd_gdt(uint64_t *gdtr);
228221828Sgrehan#endif	/* _VMMAPI_H_ */
229