xref: /freebsd/lib/libvmmapi/vmmapi.h (revision 01d822d33b0cd1f801ce39d0ed25fa7f57b5a429)
1366f6083SPeter Grehan /*-
25e53a4f9SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
35e53a4f9SPedro F. Giffuni  *
4366f6083SPeter Grehan  * Copyright (c) 2011 NetApp, Inc.
5366f6083SPeter Grehan  * All rights reserved.
6366f6083SPeter Grehan  *
7366f6083SPeter Grehan  * Redistribution and use in source and binary forms, with or without
8366f6083SPeter Grehan  * modification, are permitted provided that the following conditions
9366f6083SPeter Grehan  * are met:
10366f6083SPeter Grehan  * 1. Redistributions of source code must retain the above copyright
11366f6083SPeter Grehan  *    notice, this list of conditions and the following disclaimer.
12366f6083SPeter Grehan  * 2. Redistributions in binary form must reproduce the above copyright
13366f6083SPeter Grehan  *    notice, this list of conditions and the following disclaimer in the
14366f6083SPeter Grehan  *    documentation and/or other materials provided with the distribution.
15366f6083SPeter Grehan  *
16366f6083SPeter Grehan  * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
17366f6083SPeter Grehan  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18366f6083SPeter Grehan  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19366f6083SPeter Grehan  * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
20366f6083SPeter Grehan  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21366f6083SPeter Grehan  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22366f6083SPeter Grehan  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23366f6083SPeter Grehan  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24366f6083SPeter Grehan  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25366f6083SPeter Grehan  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26366f6083SPeter Grehan  * SUCH DAMAGE.
27366f6083SPeter Grehan  *
28366f6083SPeter Grehan  * $FreeBSD$
29366f6083SPeter Grehan  */
30366f6083SPeter Grehan 
31366f6083SPeter Grehan #ifndef _VMMAPI_H_
32366f6083SPeter Grehan #define	_VMMAPI_H_
33366f6083SPeter Grehan 
3495ebc360SNeel Natu #include <sys/param.h>
3595ebc360SNeel Natu #include <sys/cpuset.h>
3695ebc360SNeel Natu 
37d087a399SNeel Natu /*
38d087a399SNeel Natu  * API version for out-of-tree consumers like grub-bhyve for making compile
39d087a399SNeel Natu  * time decisions.
40d087a399SNeel Natu  */
4100ef17beSBartek Rutkowski #define	VMMAPI_VERSION	0103	/* 2 digit major followed by 2 digit minor */
42d087a399SNeel Natu 
436303b65dSNeel Natu struct iovec;
44366f6083SPeter Grehan struct vmctx;
45e9027382SNeel Natu enum x2apic_state;
46366f6083SPeter Grehan 
47b060ba50SNeel Natu /*
48b060ba50SNeel Natu  * Different styles of mapping the memory assigned to a VM into the address
49b060ba50SNeel Natu  * space of the controlling process.
50b060ba50SNeel Natu  */
51b060ba50SNeel Natu enum vm_mmap_style {
52b060ba50SNeel Natu 	VM_MMAP_NONE,		/* no mapping */
53b060ba50SNeel Natu 	VM_MMAP_ALL,		/* fully and statically mapped */
54b060ba50SNeel Natu 	VM_MMAP_SPARSE,		/* mappings created on-demand */
55b060ba50SNeel Natu };
56b060ba50SNeel Natu 
579b1aa8d6SNeel Natu /*
589b1aa8d6SNeel Natu  * 'flags' value passed to 'vm_set_memflags()'.
599b1aa8d6SNeel Natu  */
600dd10c00SNeel Natu #define	VM_MEM_F_INCORE	0x01	/* include guest memory in core file */
619b1aa8d6SNeel Natu #define	VM_MEM_F_WIRED	0x02	/* guest memory is wired */
629b1aa8d6SNeel Natu 
639b1aa8d6SNeel Natu /*
649b1aa8d6SNeel Natu  * Identifiers for memory segments:
659b1aa8d6SNeel Natu  * - vm_setup_memory() uses VM_SYSMEM for the system memory segment.
669b1aa8d6SNeel Natu  * - the remaining identifiers can be used to create devmem segments.
679b1aa8d6SNeel Natu  */
689b1aa8d6SNeel Natu enum {
699b1aa8d6SNeel Natu 	VM_SYSMEM,
709b1aa8d6SNeel Natu 	VM_BOOTROM,
719b1aa8d6SNeel Natu 	VM_FRAMEBUFFER,
729b1aa8d6SNeel Natu };
739b1aa8d6SNeel Natu 
749b1aa8d6SNeel Natu /*
759b1aa8d6SNeel Natu  * Get the length and name of the memory segment identified by 'segid'.
769b1aa8d6SNeel Natu  * Note that system memory segments are identified with a nul name.
779b1aa8d6SNeel Natu  *
789b1aa8d6SNeel Natu  * Returns 0 on success and non-zero otherwise.
799b1aa8d6SNeel Natu  */
809b1aa8d6SNeel Natu int	vm_get_memseg(struct vmctx *ctx, int ident, size_t *lenp, char *name,
819b1aa8d6SNeel Natu 	    size_t namesiz);
829b1aa8d6SNeel Natu 
839b1aa8d6SNeel Natu /*
849b1aa8d6SNeel Natu  * Iterate over the guest address space. This function finds an address range
859b1aa8d6SNeel Natu  * that starts at an address >= *gpa.
869b1aa8d6SNeel Natu  *
879b1aa8d6SNeel Natu  * Returns 0 if the next address range was found and non-zero otherwise.
889b1aa8d6SNeel Natu  */
899b1aa8d6SNeel Natu int	vm_mmap_getnext(struct vmctx *ctx, vm_paddr_t *gpa, int *segid,
909b1aa8d6SNeel Natu 	    vm_ooffset_t *segoff, size_t *len, int *prot, int *flags);
919b1aa8d6SNeel Natu /*
929b1aa8d6SNeel Natu  * Create a device memory segment identified by 'segid'.
939b1aa8d6SNeel Natu  *
949b1aa8d6SNeel Natu  * Returns a pointer to the memory segment on success and MAP_FAILED otherwise.
959b1aa8d6SNeel Natu  */
969b1aa8d6SNeel Natu void	*vm_create_devmem(struct vmctx *ctx, int segid, const char *name,
979b1aa8d6SNeel Natu 	    size_t len);
989b1aa8d6SNeel Natu 
999b1aa8d6SNeel Natu /*
1009b1aa8d6SNeel Natu  * Map the memory segment identified by 'segid' into the guest address space
1019b1aa8d6SNeel Natu  * at [gpa,gpa+len) with protection 'prot'.
1029b1aa8d6SNeel Natu  */
1039b1aa8d6SNeel Natu int	vm_mmap_memseg(struct vmctx *ctx, vm_paddr_t gpa, int segid,
1049b1aa8d6SNeel Natu 	    vm_ooffset_t segoff, size_t len, int prot);
1050dd10c00SNeel Natu 
106366f6083SPeter Grehan int	vm_create(const char *name);
10700ef17beSBartek Rutkowski int	vm_get_device_fd(struct vmctx *ctx);
108366f6083SPeter Grehan struct vmctx *vm_open(const char *name);
109366f6083SPeter Grehan void	vm_destroy(struct vmctx *ctx);
110200758f1SNeel Natu int	vm_parse_memsize(const char *optarg, size_t *memsize);
111b060ba50SNeel Natu int	vm_setup_memory(struct vmctx *ctx, size_t len, enum vm_mmap_style s);
112b060ba50SNeel Natu void	*vm_map_gpa(struct vmctx *ctx, vm_paddr_t gaddr, size_t len);
113318224bbSNeel Natu int	vm_get_gpa_pmap(struct vmctx *, uint64_t gpa, uint64_t *pte, int *num);
114ef7c2a82STycho Nightingale int	vm_gla2gpa(struct vmctx *, int vcpuid, struct vm_guest_paging *paging,
1159c4d5478SNeel Natu 		   uint64_t gla, int prot, uint64_t *gpa, int *fault);
1165f8754c0SJohn Baldwin int	vm_gla2gpa_nofault(struct vmctx *, int vcpuid,
1175f8754c0SJohn Baldwin 		   struct vm_guest_paging *paging, uint64_t gla, int prot,
1185f8754c0SJohn Baldwin 		   uint64_t *gpa, int *fault);
119b060ba50SNeel Natu uint32_t vm_get_lowmem_limit(struct vmctx *ctx);
120b060ba50SNeel Natu void	vm_set_lowmem_limit(struct vmctx *ctx, uint32_t limit);
1210dd10c00SNeel Natu void	vm_set_memflags(struct vmctx *ctx, int flags);
1229b1aa8d6SNeel Natu int	vm_get_memflags(struct vmctx *ctx);
123be679db4SNeel Natu size_t	vm_get_lowmem_size(struct vmctx *ctx);
124be679db4SNeel Natu size_t	vm_get_highmem_size(struct vmctx *ctx);
125366f6083SPeter Grehan int	vm_set_desc(struct vmctx *ctx, int vcpu, int reg,
126366f6083SPeter Grehan 		    uint64_t base, uint32_t limit, uint32_t access);
127366f6083SPeter Grehan int	vm_get_desc(struct vmctx *ctx, int vcpu, int reg,
128366f6083SPeter Grehan 		    uint64_t *base, uint32_t *limit, uint32_t *access);
129d665d229SNeel Natu int	vm_get_seg_desc(struct vmctx *ctx, int vcpu, int reg,
130d665d229SNeel Natu 			struct seg_desc *seg_desc);
131366f6083SPeter Grehan int	vm_set_register(struct vmctx *ctx, int vcpu, int reg, uint64_t val);
132366f6083SPeter Grehan int	vm_get_register(struct vmctx *ctx, int vcpu, int reg, uint64_t *retval);
1334f866698SJohn Baldwin int	vm_set_register_set(struct vmctx *ctx, int vcpu, unsigned int count,
1344f866698SJohn Baldwin     const int *regnums, uint64_t *regvals);
1354f866698SJohn Baldwin int	vm_get_register_set(struct vmctx *ctx, int vcpu, unsigned int count,
1364f866698SJohn Baldwin     const int *regnums, uint64_t *regvals);
137d087a399SNeel Natu int	vm_run(struct vmctx *ctx, int vcpu, struct vm_exit *ret_vmexit);
138f0fdcfe2SNeel Natu int	vm_suspend(struct vmctx *ctx, enum vm_suspend_how how);
1395fcf252fSNeel Natu int	vm_reinit(struct vmctx *ctx);
14090d4b48fSNeel Natu int	vm_apicid2vcpu(struct vmctx *ctx, int apicid);
141d087a399SNeel Natu int	vm_inject_exception(struct vmctx *ctx, int vcpu, int vector,
142d087a399SNeel Natu     int errcode_valid, uint32_t errcode, int restart_instruction);
143366f6083SPeter Grehan int	vm_lapic_irq(struct vmctx *ctx, int vcpu, int vector);
144330baf58SJohn Baldwin int	vm_lapic_local_irq(struct vmctx *ctx, int vcpu, int vector);
1454f8be175SNeel Natu int	vm_lapic_msi(struct vmctx *ctx, uint64_t addr, uint64_t msg);
146565bbb86SNeel Natu int	vm_ioapic_assert_irq(struct vmctx *ctx, int irq);
147565bbb86SNeel Natu int	vm_ioapic_deassert_irq(struct vmctx *ctx, int irq);
148ac7304a7SNeel Natu int	vm_ioapic_pulse_irq(struct vmctx *ctx, int irq);
1493cbf3585SJohn Baldwin int	vm_ioapic_pincount(struct vmctx *ctx, int *pincount);
150b96be57aSTycho Nightingale int	vm_isa_assert_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq);
151762fd208STycho Nightingale int	vm_isa_deassert_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq);
152762fd208STycho Nightingale int	vm_isa_pulse_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq);
153b3e9732aSJohn Baldwin int	vm_isa_set_irq_trigger(struct vmctx *ctx, int atpic_irq,
154b3e9732aSJohn Baldwin 	    enum vm_intr_trigger trigger);
155366f6083SPeter Grehan int	vm_inject_nmi(struct vmctx *ctx, int vcpu);
156366f6083SPeter Grehan int	vm_capability_name2type(const char *capname);
15713f4cf6cSNeel Natu const char *vm_capability_type2name(int type);
158366f6083SPeter Grehan int	vm_get_capability(struct vmctx *ctx, int vcpu, enum vm_cap_type cap,
159366f6083SPeter Grehan 			  int *retval);
160366f6083SPeter Grehan int	vm_set_capability(struct vmctx *ctx, int vcpu, enum vm_cap_type cap,
161366f6083SPeter Grehan 			  int val);
162366f6083SPeter Grehan int	vm_assign_pptdev(struct vmctx *ctx, int bus, int slot, int func);
163366f6083SPeter Grehan int	vm_unassign_pptdev(struct vmctx *ctx, int bus, int slot, int func);
164366f6083SPeter Grehan int	vm_map_pptdev_mmio(struct vmctx *ctx, int bus, int slot, int func,
165366f6083SPeter Grehan 			   vm_paddr_t gpa, size_t len, vm_paddr_t hpa);
16655888cfaSNeel Natu int	vm_setup_pptdev_msi(struct vmctx *ctx, int vcpu, int bus, int slot,
16755888cfaSNeel Natu 	    int func, uint64_t addr, uint64_t msg, int numvec);
16855888cfaSNeel Natu int	vm_setup_pptdev_msix(struct vmctx *ctx, int vcpu, int bus, int slot,
16955888cfaSNeel Natu 	    int func, int idx, uint64_t addr, uint64_t msg,
17055888cfaSNeel Natu 	    uint32_t vector_control);
171366f6083SPeter Grehan 
172091d4532SNeel Natu int	vm_get_intinfo(struct vmctx *ctx, int vcpu, uint64_t *i1, uint64_t *i2);
173091d4532SNeel Natu int	vm_set_intinfo(struct vmctx *ctx, int vcpu, uint64_t exit_intinfo);
174091d4532SNeel Natu 
17500ef17beSBartek Rutkowski const cap_ioctl_t *vm_get_ioctls(size_t *len);
17600ef17beSBartek Rutkowski 
177366f6083SPeter Grehan /*
178366f6083SPeter Grehan  * Return a pointer to the statistics buffer. Note that this is not MT-safe.
179366f6083SPeter Grehan  */
180366f6083SPeter Grehan uint64_t *vm_get_stats(struct vmctx *ctx, int vcpu, struct timeval *ret_tv,
181366f6083SPeter Grehan 		       int *ret_entries);
182366f6083SPeter Grehan const char *vm_get_stat_desc(struct vmctx *ctx, int index);
183366f6083SPeter Grehan 
184e9027382SNeel Natu int	vm_get_x2apic_state(struct vmctx *ctx, int vcpu, enum x2apic_state *s);
185e9027382SNeel Natu int	vm_set_x2apic_state(struct vmctx *ctx, int vcpu, enum x2apic_state s);
186e9027382SNeel Natu 
18708e3ff32SNeel Natu int	vm_get_hpet_capabilities(struct vmctx *ctx, uint32_t *capabilities);
18808e3ff32SNeel Natu 
1896303b65dSNeel Natu /*
1906303b65dSNeel Natu  * Translate the GLA range [gla,gla+len) into GPA segments in 'iov'.
19175f46cf6SPedro F. Giffuni  * The 'iovcnt' should be big enough to accommodate all GPA segments.
1929c4d5478SNeel Natu  *
1939c4d5478SNeel Natu  * retval	fault		Interpretation
1949c4d5478SNeel Natu  *   0		  0		Success
1959c4d5478SNeel Natu  *   0		  1		An exception was injected into the guest
1969c4d5478SNeel Natu  * EFAULT	 N/A		Error
1976303b65dSNeel Natu  */
198d665d229SNeel Natu int	vm_copy_setup(struct vmctx *ctx, int vcpu, struct vm_guest_paging *pg,
1999c4d5478SNeel Natu 	    uint64_t gla, size_t len, int prot, struct iovec *iov, int iovcnt,
2009c4d5478SNeel Natu 	    int *fault);
2016303b65dSNeel Natu void	vm_copyin(struct vmctx *ctx, int vcpu, struct iovec *guest_iov,
2026303b65dSNeel Natu 	    void *host_dst, size_t len);
2036303b65dSNeel Natu void	vm_copyout(struct vmctx *ctx, int vcpu, const void *host_src,
2046303b65dSNeel Natu 	    struct iovec *guest_iov, size_t len);
205009e2acbSNeel Natu void	vm_copy_teardown(struct vmctx *ctx, int vcpu, struct iovec *iov,
206009e2acbSNeel Natu 	    int iovcnt);
207da11f4aaSNeel Natu 
2080dafa5cdSNeel Natu /* RTC */
2090dafa5cdSNeel Natu int	vm_rtc_write(struct vmctx *ctx, int offset, uint8_t value);
2100dafa5cdSNeel Natu int	vm_rtc_read(struct vmctx *ctx, int offset, uint8_t *retval);
2110dafa5cdSNeel Natu int	vm_rtc_settime(struct vmctx *ctx, time_t secs);
2120dafa5cdSNeel Natu int	vm_rtc_gettime(struct vmctx *ctx, time_t *secs);
2130dafa5cdSNeel Natu 
214366f6083SPeter Grehan /* Reset vcpu register state */
215366f6083SPeter Grehan int	vcpu_reset(struct vmctx *ctx, int vcpu);
216366f6083SPeter Grehan 
21795ebc360SNeel Natu int	vm_active_cpus(struct vmctx *ctx, cpuset_t *cpus);
21895ebc360SNeel Natu int	vm_suspended_cpus(struct vmctx *ctx, cpuset_t *cpus);
219fc276d92SJohn Baldwin int	vm_debug_cpus(struct vmctx *ctx, cpuset_t *cpus);
22095ebc360SNeel Natu int	vm_activate_cpu(struct vmctx *ctx, int vcpu);
221fc276d92SJohn Baldwin int	vm_suspend_cpu(struct vmctx *ctx, int vcpu);
222fc276d92SJohn Baldwin int	vm_resume_cpu(struct vmctx *ctx, int vcpu);
22395ebc360SNeel Natu 
224*01d822d3SRodney W. Grimes /* CPU topology */
225*01d822d3SRodney W. Grimes int	vm_set_topology(struct vmctx *ctx, uint16_t sockets, uint16_t cores,
226*01d822d3SRodney W. Grimes 	    uint16_t threads, uint16_t maxcpus);
227*01d822d3SRodney W. Grimes int	vm_get_topology(struct vmctx *ctx, uint16_t *sockets, uint16_t *cores,
228*01d822d3SRodney W. Grimes 	    uint16_t *threads, uint16_t *maxcpus);
229*01d822d3SRodney W. Grimes 
230366f6083SPeter Grehan /*
231366f6083SPeter Grehan  * FreeBSD specific APIs
232366f6083SPeter Grehan  */
233366f6083SPeter Grehan int	vm_setup_freebsd_registers(struct vmctx *ctx, int vcpu,
234366f6083SPeter Grehan 				uint64_t rip, uint64_t cr3, uint64_t gdtbase,
235366f6083SPeter Grehan 				uint64_t rsp);
23600f3efe1SJohn Baldwin int	vm_setup_freebsd_registers_i386(struct vmctx *vmctx, int vcpu,
23700f3efe1SJohn Baldwin 					uint32_t eip, uint32_t gdtbase,
23800f3efe1SJohn Baldwin 					uint32_t esp);
239366f6083SPeter Grehan void	vm_setup_freebsd_gdt(uint64_t *gdtr);
240366f6083SPeter Grehan #endif	/* _VMMAPI_H_ */
241