xref: /freebsd/lib/libvmmapi/vmmapi.h (revision 5e53a4f90f82c4345f277dd87cc9292f26e04a29)
1366f6083SPeter Grehan /*-
2*5e53a4f9SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*5e53a4f9SPedro 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);
116b060ba50SNeel Natu uint32_t vm_get_lowmem_limit(struct vmctx *ctx);
117b060ba50SNeel Natu void	vm_set_lowmem_limit(struct vmctx *ctx, uint32_t limit);
1180dd10c00SNeel Natu void	vm_set_memflags(struct vmctx *ctx, int flags);
1199b1aa8d6SNeel Natu int	vm_get_memflags(struct vmctx *ctx);
120be679db4SNeel Natu size_t	vm_get_lowmem_size(struct vmctx *ctx);
121be679db4SNeel Natu size_t	vm_get_highmem_size(struct vmctx *ctx);
122366f6083SPeter Grehan int	vm_set_desc(struct vmctx *ctx, int vcpu, int reg,
123366f6083SPeter Grehan 		    uint64_t base, uint32_t limit, uint32_t access);
124366f6083SPeter Grehan int	vm_get_desc(struct vmctx *ctx, int vcpu, int reg,
125366f6083SPeter Grehan 		    uint64_t *base, uint32_t *limit, uint32_t *access);
126d665d229SNeel Natu int	vm_get_seg_desc(struct vmctx *ctx, int vcpu, int reg,
127d665d229SNeel Natu 			struct seg_desc *seg_desc);
128366f6083SPeter Grehan int	vm_set_register(struct vmctx *ctx, int vcpu, int reg, uint64_t val);
129366f6083SPeter Grehan int	vm_get_register(struct vmctx *ctx, int vcpu, int reg, uint64_t *retval);
130d087a399SNeel Natu int	vm_run(struct vmctx *ctx, int vcpu, struct vm_exit *ret_vmexit);
131f0fdcfe2SNeel Natu int	vm_suspend(struct vmctx *ctx, enum vm_suspend_how how);
1325fcf252fSNeel Natu int	vm_reinit(struct vmctx *ctx);
13390d4b48fSNeel Natu int	vm_apicid2vcpu(struct vmctx *ctx, int apicid);
134d087a399SNeel Natu int	vm_inject_exception(struct vmctx *ctx, int vcpu, int vector,
135d087a399SNeel Natu     int errcode_valid, uint32_t errcode, int restart_instruction);
136366f6083SPeter Grehan int	vm_lapic_irq(struct vmctx *ctx, int vcpu, int vector);
137330baf58SJohn Baldwin int	vm_lapic_local_irq(struct vmctx *ctx, int vcpu, int vector);
1384f8be175SNeel Natu int	vm_lapic_msi(struct vmctx *ctx, uint64_t addr, uint64_t msg);
139565bbb86SNeel Natu int	vm_ioapic_assert_irq(struct vmctx *ctx, int irq);
140565bbb86SNeel Natu int	vm_ioapic_deassert_irq(struct vmctx *ctx, int irq);
141ac7304a7SNeel Natu int	vm_ioapic_pulse_irq(struct vmctx *ctx, int irq);
1423cbf3585SJohn Baldwin int	vm_ioapic_pincount(struct vmctx *ctx, int *pincount);
143b96be57aSTycho Nightingale int	vm_isa_assert_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq);
144762fd208STycho Nightingale int	vm_isa_deassert_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq);
145762fd208STycho Nightingale int	vm_isa_pulse_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq);
146b3e9732aSJohn Baldwin int	vm_isa_set_irq_trigger(struct vmctx *ctx, int atpic_irq,
147b3e9732aSJohn Baldwin 	    enum vm_intr_trigger trigger);
148366f6083SPeter Grehan int	vm_inject_nmi(struct vmctx *ctx, int vcpu);
149366f6083SPeter Grehan int	vm_capability_name2type(const char *capname);
15013f4cf6cSNeel Natu const char *vm_capability_type2name(int type);
151366f6083SPeter Grehan int	vm_get_capability(struct vmctx *ctx, int vcpu, enum vm_cap_type cap,
152366f6083SPeter Grehan 			  int *retval);
153366f6083SPeter Grehan int	vm_set_capability(struct vmctx *ctx, int vcpu, enum vm_cap_type cap,
154366f6083SPeter Grehan 			  int val);
155366f6083SPeter Grehan int	vm_assign_pptdev(struct vmctx *ctx, int bus, int slot, int func);
156366f6083SPeter Grehan int	vm_unassign_pptdev(struct vmctx *ctx, int bus, int slot, int func);
157366f6083SPeter Grehan int	vm_map_pptdev_mmio(struct vmctx *ctx, int bus, int slot, int func,
158366f6083SPeter Grehan 			   vm_paddr_t gpa, size_t len, vm_paddr_t hpa);
15955888cfaSNeel Natu int	vm_setup_pptdev_msi(struct vmctx *ctx, int vcpu, int bus, int slot,
16055888cfaSNeel Natu 	    int func, uint64_t addr, uint64_t msg, int numvec);
16155888cfaSNeel Natu int	vm_setup_pptdev_msix(struct vmctx *ctx, int vcpu, int bus, int slot,
16255888cfaSNeel Natu 	    int func, int idx, uint64_t addr, uint64_t msg,
16355888cfaSNeel Natu 	    uint32_t vector_control);
164366f6083SPeter Grehan 
165091d4532SNeel Natu int	vm_get_intinfo(struct vmctx *ctx, int vcpu, uint64_t *i1, uint64_t *i2);
166091d4532SNeel Natu int	vm_set_intinfo(struct vmctx *ctx, int vcpu, uint64_t exit_intinfo);
167091d4532SNeel Natu 
16800ef17beSBartek Rutkowski const cap_ioctl_t *vm_get_ioctls(size_t *len);
16900ef17beSBartek Rutkowski 
170366f6083SPeter Grehan /*
171366f6083SPeter Grehan  * Return a pointer to the statistics buffer. Note that this is not MT-safe.
172366f6083SPeter Grehan  */
173366f6083SPeter Grehan uint64_t *vm_get_stats(struct vmctx *ctx, int vcpu, struct timeval *ret_tv,
174366f6083SPeter Grehan 		       int *ret_entries);
175366f6083SPeter Grehan const char *vm_get_stat_desc(struct vmctx *ctx, int index);
176366f6083SPeter Grehan 
177e9027382SNeel Natu int	vm_get_x2apic_state(struct vmctx *ctx, int vcpu, enum x2apic_state *s);
178e9027382SNeel Natu int	vm_set_x2apic_state(struct vmctx *ctx, int vcpu, enum x2apic_state s);
179e9027382SNeel Natu 
18008e3ff32SNeel Natu int	vm_get_hpet_capabilities(struct vmctx *ctx, uint32_t *capabilities);
18108e3ff32SNeel Natu 
1826303b65dSNeel Natu /*
1836303b65dSNeel Natu  * Translate the GLA range [gla,gla+len) into GPA segments in 'iov'.
18475f46cf6SPedro F. Giffuni  * The 'iovcnt' should be big enough to accommodate all GPA segments.
1859c4d5478SNeel Natu  *
1869c4d5478SNeel Natu  * retval	fault		Interpretation
1879c4d5478SNeel Natu  *   0		  0		Success
1889c4d5478SNeel Natu  *   0		  1		An exception was injected into the guest
1899c4d5478SNeel Natu  * EFAULT	 N/A		Error
1906303b65dSNeel Natu  */
191d665d229SNeel Natu int	vm_copy_setup(struct vmctx *ctx, int vcpu, struct vm_guest_paging *pg,
1929c4d5478SNeel Natu 	    uint64_t gla, size_t len, int prot, struct iovec *iov, int iovcnt,
1939c4d5478SNeel Natu 	    int *fault);
1946303b65dSNeel Natu void	vm_copyin(struct vmctx *ctx, int vcpu, struct iovec *guest_iov,
1956303b65dSNeel Natu 	    void *host_dst, size_t len);
1966303b65dSNeel Natu void	vm_copyout(struct vmctx *ctx, int vcpu, const void *host_src,
1976303b65dSNeel Natu 	    struct iovec *guest_iov, size_t len);
198009e2acbSNeel Natu void	vm_copy_teardown(struct vmctx *ctx, int vcpu, struct iovec *iov,
199009e2acbSNeel Natu 	    int iovcnt);
200da11f4aaSNeel Natu 
2010dafa5cdSNeel Natu /* RTC */
2020dafa5cdSNeel Natu int	vm_rtc_write(struct vmctx *ctx, int offset, uint8_t value);
2030dafa5cdSNeel Natu int	vm_rtc_read(struct vmctx *ctx, int offset, uint8_t *retval);
2040dafa5cdSNeel Natu int	vm_rtc_settime(struct vmctx *ctx, time_t secs);
2050dafa5cdSNeel Natu int	vm_rtc_gettime(struct vmctx *ctx, time_t *secs);
2060dafa5cdSNeel Natu 
207366f6083SPeter Grehan /* Reset vcpu register state */
208366f6083SPeter Grehan int	vcpu_reset(struct vmctx *ctx, int vcpu);
209366f6083SPeter Grehan 
21095ebc360SNeel Natu int	vm_active_cpus(struct vmctx *ctx, cpuset_t *cpus);
21195ebc360SNeel Natu int	vm_suspended_cpus(struct vmctx *ctx, cpuset_t *cpus);
21295ebc360SNeel Natu int	vm_activate_cpu(struct vmctx *ctx, int vcpu);
21395ebc360SNeel Natu 
214366f6083SPeter Grehan /*
215366f6083SPeter Grehan  * FreeBSD specific APIs
216366f6083SPeter Grehan  */
217366f6083SPeter Grehan int	vm_setup_freebsd_registers(struct vmctx *ctx, int vcpu,
218366f6083SPeter Grehan 				uint64_t rip, uint64_t cr3, uint64_t gdtbase,
219366f6083SPeter Grehan 				uint64_t rsp);
22000f3efe1SJohn Baldwin int	vm_setup_freebsd_registers_i386(struct vmctx *vmctx, int vcpu,
22100f3efe1SJohn Baldwin 					uint32_t eip, uint32_t gdtbase,
22200f3efe1SJohn Baldwin 					uint32_t esp);
223366f6083SPeter Grehan void	vm_setup_freebsd_gdt(uint64_t *gdtr);
224366f6083SPeter Grehan #endif	/* _VMMAPI_H_ */
225