1366f6083SPeter Grehan /*- 24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 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 29366f6083SPeter Grehan #ifndef _VMMAPI_H_ 30366f6083SPeter Grehan #define _VMMAPI_H_ 31366f6083SPeter Grehan 3295ebc360SNeel Natu #include <sys/param.h> 3395ebc360SNeel Natu #include <sys/cpuset.h> 34f0880ab7SVitaliy Gusev #include <machine/vmm.h> 35483d953aSJohn Baldwin #include <machine/vmm_dev.h> 3695ebc360SNeel Natu 378a68ae80SConrad Meyer #include <stdbool.h> 388a68ae80SConrad Meyer 39d087a399SNeel Natu /* 40d087a399SNeel Natu * API version for out-of-tree consumers like grub-bhyve for making compile 41d087a399SNeel Natu * time decisions. 42d087a399SNeel Natu */ 437d9ef309SJohn Baldwin #define VMMAPI_VERSION 0200 /* 2 digit major followed by 2 digit minor */ 44d087a399SNeel Natu 456303b65dSNeel Natu struct iovec; 467d9ef309SJohn Baldwin struct vcpu; 47366f6083SPeter Grehan struct vmctx; 48483d953aSJohn Baldwin struct vm_snapshot_meta; 49e9027382SNeel Natu enum x2apic_state; 50366f6083SPeter Grehan 51b060ba50SNeel Natu /* 52b060ba50SNeel Natu * Different styles of mapping the memory assigned to a VM into the address 53b060ba50SNeel Natu * space of the controlling process. 54b060ba50SNeel Natu */ 55b060ba50SNeel Natu enum vm_mmap_style { 56b060ba50SNeel Natu VM_MMAP_NONE, /* no mapping */ 57b060ba50SNeel Natu VM_MMAP_ALL, /* fully and statically mapped */ 58b060ba50SNeel Natu VM_MMAP_SPARSE, /* mappings created on-demand */ 59b060ba50SNeel Natu }; 60b060ba50SNeel Natu 619b1aa8d6SNeel Natu /* 629b1aa8d6SNeel Natu * 'flags' value passed to 'vm_set_memflags()'. 639b1aa8d6SNeel Natu */ 640dd10c00SNeel Natu #define VM_MEM_F_INCORE 0x01 /* include guest memory in core file */ 659b1aa8d6SNeel Natu #define VM_MEM_F_WIRED 0x02 /* guest memory is wired */ 669b1aa8d6SNeel Natu 679b1aa8d6SNeel Natu /* 689b1aa8d6SNeel Natu * Identifiers for memory segments: 699b1aa8d6SNeel Natu * - vm_setup_memory() uses VM_SYSMEM for the system memory segment. 709b1aa8d6SNeel Natu * - the remaining identifiers can be used to create devmem segments. 719b1aa8d6SNeel Natu */ 729b1aa8d6SNeel Natu enum { 739b1aa8d6SNeel Natu VM_SYSMEM, 749b1aa8d6SNeel Natu VM_BOOTROM, 759b1aa8d6SNeel Natu VM_FRAMEBUFFER, 76e47fe318SCorvin Köhne VM_PCIROM, 779b1aa8d6SNeel Natu }; 789b1aa8d6SNeel Natu 791228a047SVitaliy Gusev __BEGIN_DECLS 809b1aa8d6SNeel Natu /* 819b1aa8d6SNeel Natu * Get the length and name of the memory segment identified by 'segid'. 829b1aa8d6SNeel Natu * Note that system memory segments are identified with a nul name. 839b1aa8d6SNeel Natu * 849b1aa8d6SNeel Natu * Returns 0 on success and non-zero otherwise. 859b1aa8d6SNeel Natu */ 869b1aa8d6SNeel Natu int vm_get_memseg(struct vmctx *ctx, int ident, size_t *lenp, char *name, 879b1aa8d6SNeel Natu size_t namesiz); 889b1aa8d6SNeel Natu 899b1aa8d6SNeel Natu /* 909b1aa8d6SNeel Natu * Iterate over the guest address space. This function finds an address range 919b1aa8d6SNeel Natu * that starts at an address >= *gpa. 929b1aa8d6SNeel Natu * 939b1aa8d6SNeel Natu * Returns 0 if the next address range was found and non-zero otherwise. 949b1aa8d6SNeel Natu */ 959b1aa8d6SNeel Natu int vm_mmap_getnext(struct vmctx *ctx, vm_paddr_t *gpa, int *segid, 969b1aa8d6SNeel Natu vm_ooffset_t *segoff, size_t *len, int *prot, int *flags); 97483d953aSJohn Baldwin 98483d953aSJohn Baldwin int vm_get_guestmem_from_ctx(struct vmctx *ctx, char **guest_baseaddr, 99483d953aSJohn Baldwin size_t *lowmem_size, size_t *highmem_size); 100483d953aSJohn Baldwin 1019b1aa8d6SNeel Natu /* 1029b1aa8d6SNeel Natu * Create a device memory segment identified by 'segid'. 1039b1aa8d6SNeel Natu * 1049b1aa8d6SNeel Natu * Returns a pointer to the memory segment on success and MAP_FAILED otherwise. 1059b1aa8d6SNeel Natu */ 1069b1aa8d6SNeel Natu void *vm_create_devmem(struct vmctx *ctx, int segid, const char *name, 1079b1aa8d6SNeel Natu size_t len); 1089b1aa8d6SNeel Natu 1099b1aa8d6SNeel Natu /* 1109b1aa8d6SNeel Natu * Map the memory segment identified by 'segid' into the guest address space 1119b1aa8d6SNeel Natu * at [gpa,gpa+len) with protection 'prot'. 1129b1aa8d6SNeel Natu */ 1139b1aa8d6SNeel Natu int vm_mmap_memseg(struct vmctx *ctx, vm_paddr_t gpa, int segid, 1149b1aa8d6SNeel Natu vm_ooffset_t segoff, size_t len, int prot); 1150dd10c00SNeel Natu 116f8a6ec2dSD Scott Phillips int vm_munmap_memseg(struct vmctx *ctx, vm_paddr_t gpa, size_t len); 117f8a6ec2dSD Scott Phillips 118366f6083SPeter Grehan int vm_create(const char *name); 119366f6083SPeter Grehan struct vmctx *vm_open(const char *name); 120f0880ab7SVitaliy Gusev void vm_close(struct vmctx *ctx); 121366f6083SPeter Grehan void vm_destroy(struct vmctx *ctx); 1223e9b4532SMark Johnston int vm_limit_rights(struct vmctx *ctx); 1237d9ef309SJohn Baldwin struct vcpu *vm_vcpu_open(struct vmctx *ctx, int vcpuid); 1247d9ef309SJohn Baldwin void vm_vcpu_close(struct vcpu *vcpu); 1257d9ef309SJohn Baldwin int vcpu_id(struct vcpu *vcpu); 126200758f1SNeel Natu int vm_parse_memsize(const char *optarg, size_t *memsize); 127b060ba50SNeel Natu int vm_setup_memory(struct vmctx *ctx, size_t len, enum vm_mmap_style s); 128b060ba50SNeel Natu void *vm_map_gpa(struct vmctx *ctx, vm_paddr_t gaddr, size_t len); 129483d953aSJohn Baldwin /* inverse operation to vm_map_gpa - extract guest address from host pointer */ 130483d953aSJohn Baldwin vm_paddr_t vm_rev_map_gpa(struct vmctx *ctx, void *addr); 131318224bbSNeel Natu int vm_get_gpa_pmap(struct vmctx *, uint64_t gpa, uint64_t *pte, int *num); 1327d9ef309SJohn Baldwin int vm_gla2gpa(struct vcpu *vcpu, struct vm_guest_paging *paging, 1339c4d5478SNeel Natu uint64_t gla, int prot, uint64_t *gpa, int *fault); 1347d9ef309SJohn Baldwin int vm_gla2gpa_nofault(struct vcpu *vcpu, 1355f8754c0SJohn Baldwin struct vm_guest_paging *paging, uint64_t gla, int prot, 1365f8754c0SJohn Baldwin uint64_t *gpa, int *fault); 137b060ba50SNeel Natu uint32_t vm_get_lowmem_limit(struct vmctx *ctx); 138b060ba50SNeel Natu void vm_set_lowmem_limit(struct vmctx *ctx, uint32_t limit); 1390dd10c00SNeel Natu void vm_set_memflags(struct vmctx *ctx, int flags); 1409b1aa8d6SNeel Natu int vm_get_memflags(struct vmctx *ctx); 1413efc45f3SRobert Wing const char *vm_get_name(struct vmctx *ctx); 142be679db4SNeel Natu size_t vm_get_lowmem_size(struct vmctx *ctx); 143be679db4SNeel Natu size_t vm_get_highmem_size(struct vmctx *ctx); 144*e4656e10SMark Johnston #ifdef __amd64__ 1457d9ef309SJohn Baldwin int vm_set_desc(struct vcpu *vcpu, int reg, 146366f6083SPeter Grehan uint64_t base, uint32_t limit, uint32_t access); 1477d9ef309SJohn Baldwin int vm_get_desc(struct vcpu *vcpu, int reg, 148366f6083SPeter Grehan uint64_t *base, uint32_t *limit, uint32_t *access); 1497d9ef309SJohn Baldwin int vm_get_seg_desc(struct vcpu *vcpu, int reg, struct seg_desc *seg_desc); 150*e4656e10SMark Johnston #endif 1517d9ef309SJohn Baldwin int vm_set_register(struct vcpu *vcpu, int reg, uint64_t val); 1527d9ef309SJohn Baldwin int vm_get_register(struct vcpu *vcpu, int reg, uint64_t *retval); 1537d9ef309SJohn Baldwin int vm_set_register_set(struct vcpu *vcpu, unsigned int count, 1544f866698SJohn Baldwin const int *regnums, uint64_t *regvals); 1557d9ef309SJohn Baldwin int vm_get_register_set(struct vcpu *vcpu, unsigned int count, 1564f866698SJohn Baldwin const int *regnums, uint64_t *regvals); 157e17eca32SMark Johnston int vm_run(struct vcpu *vcpu, struct vm_run *vmrun); 158f0fdcfe2SNeel Natu int vm_suspend(struct vmctx *ctx, enum vm_suspend_how how); 1595fcf252fSNeel Natu int vm_reinit(struct vmctx *ctx); 16090d4b48fSNeel Natu int vm_apicid2vcpu(struct vmctx *ctx, int apicid); 1617d9ef309SJohn Baldwin int vm_inject_exception(struct vcpu *vcpu, int vector, 162d087a399SNeel Natu int errcode_valid, uint32_t errcode, int restart_instruction); 163*e4656e10SMark Johnston #ifdef __amd64__ 1647d9ef309SJohn Baldwin int vm_lapic_irq(struct vcpu *vcpu, int vector); 1657d9ef309SJohn Baldwin int vm_lapic_local_irq(struct vcpu *vcpu, int vector); 1664f8be175SNeel Natu int vm_lapic_msi(struct vmctx *ctx, uint64_t addr, uint64_t msg); 167565bbb86SNeel Natu int vm_ioapic_assert_irq(struct vmctx *ctx, int irq); 168565bbb86SNeel Natu int vm_ioapic_deassert_irq(struct vmctx *ctx, int irq); 169ac7304a7SNeel Natu int vm_ioapic_pulse_irq(struct vmctx *ctx, int irq); 1703cbf3585SJohn Baldwin int vm_ioapic_pincount(struct vmctx *ctx, int *pincount); 171b96be57aSTycho Nightingale int vm_isa_assert_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq); 172762fd208STycho Nightingale int vm_isa_deassert_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq); 173762fd208STycho Nightingale int vm_isa_pulse_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq); 174b3e9732aSJohn Baldwin int vm_isa_set_irq_trigger(struct vmctx *ctx, int atpic_irq, 175b3e9732aSJohn Baldwin enum vm_intr_trigger trigger); 1767d9ef309SJohn Baldwin int vm_inject_nmi(struct vcpu *vcpu); 177*e4656e10SMark Johnston #endif 178*e4656e10SMark Johnston int vm_readwrite_kernemu_device(struct vcpu *vcpu, 179*e4656e10SMark Johnston vm_paddr_t gpa, bool write, int size, uint64_t *value); 180366f6083SPeter Grehan int vm_capability_name2type(const char *capname); 18113f4cf6cSNeel Natu const char *vm_capability_type2name(int type); 1827d9ef309SJohn Baldwin int vm_get_capability(struct vcpu *vcpu, enum vm_cap_type cap, 183366f6083SPeter Grehan int *retval); 1847d9ef309SJohn Baldwin int vm_set_capability(struct vcpu *vcpu, enum vm_cap_type cap, 185366f6083SPeter Grehan int val); 186366f6083SPeter Grehan int vm_assign_pptdev(struct vmctx *ctx, int bus, int slot, int func); 187366f6083SPeter Grehan int vm_unassign_pptdev(struct vmctx *ctx, int bus, int slot, int func); 188366f6083SPeter Grehan int vm_map_pptdev_mmio(struct vmctx *ctx, int bus, int slot, int func, 189366f6083SPeter Grehan vm_paddr_t gpa, size_t len, vm_paddr_t hpa); 190f8a6ec2dSD Scott Phillips int vm_unmap_pptdev_mmio(struct vmctx *ctx, int bus, int slot, int func, 191f8a6ec2dSD Scott Phillips vm_paddr_t gpa, size_t len); 1927d9ef309SJohn Baldwin int vm_setup_pptdev_msi(struct vmctx *ctx, int bus, int slot, 19355888cfaSNeel Natu int func, uint64_t addr, uint64_t msg, int numvec); 1947d9ef309SJohn Baldwin int vm_setup_pptdev_msix(struct vmctx *ctx, int bus, int slot, 19555888cfaSNeel Natu int func, int idx, uint64_t addr, uint64_t msg, 19655888cfaSNeel Natu uint32_t vector_control); 1971925586eSJohn Baldwin int vm_disable_pptdev_msix(struct vmctx *ctx, int bus, int slot, int func); 198366f6083SPeter Grehan 1997d9ef309SJohn Baldwin int vm_get_intinfo(struct vcpu *vcpu, uint64_t *i1, uint64_t *i2); 2007d9ef309SJohn Baldwin int vm_set_intinfo(struct vcpu *vcpu, uint64_t exit_intinfo); 201091d4532SNeel Natu 202366f6083SPeter Grehan /* 203366f6083SPeter Grehan * Return a pointer to the statistics buffer. Note that this is not MT-safe. 204366f6083SPeter Grehan */ 2057d9ef309SJohn Baldwin uint64_t *vm_get_stats(struct vcpu *vcpu, struct timeval *ret_tv, 206366f6083SPeter Grehan int *ret_entries); 207366f6083SPeter Grehan const char *vm_get_stat_desc(struct vmctx *ctx, int index); 208366f6083SPeter Grehan 2097d9ef309SJohn Baldwin int vm_get_x2apic_state(struct vcpu *vcpu, enum x2apic_state *s); 2107d9ef309SJohn Baldwin int vm_set_x2apic_state(struct vcpu *vcpu, enum x2apic_state s); 211e9027382SNeel Natu 21208e3ff32SNeel Natu int vm_get_hpet_capabilities(struct vmctx *ctx, uint32_t *capabilities); 21308e3ff32SNeel Natu 2146303b65dSNeel Natu /* 2156303b65dSNeel Natu * Translate the GLA range [gla,gla+len) into GPA segments in 'iov'. 21675f46cf6SPedro F. Giffuni * The 'iovcnt' should be big enough to accommodate all GPA segments. 2179c4d5478SNeel Natu * 2189c4d5478SNeel Natu * retval fault Interpretation 2199c4d5478SNeel Natu * 0 0 Success 2209c4d5478SNeel Natu * 0 1 An exception was injected into the guest 2219c4d5478SNeel Natu * EFAULT N/A Error 2226303b65dSNeel Natu */ 2237d9ef309SJohn Baldwin int vm_copy_setup(struct vcpu *vcpu, struct vm_guest_paging *pg, 2249c4d5478SNeel Natu uint64_t gla, size_t len, int prot, struct iovec *iov, int iovcnt, 2259c4d5478SNeel Natu int *fault); 2262b4fe856SJohn Baldwin void vm_copyin(struct iovec *guest_iov, void *host_dst, size_t len); 2272b4fe856SJohn Baldwin void vm_copyout(const void *host_src, struct iovec *guest_iov, size_t len); 2282b4fe856SJohn Baldwin void vm_copy_teardown(struct iovec *iov, int iovcnt); 229da11f4aaSNeel Natu 2300dafa5cdSNeel Natu /* RTC */ 2310dafa5cdSNeel Natu int vm_rtc_write(struct vmctx *ctx, int offset, uint8_t value); 2320dafa5cdSNeel Natu int vm_rtc_read(struct vmctx *ctx, int offset, uint8_t *retval); 2330dafa5cdSNeel Natu int vm_rtc_settime(struct vmctx *ctx, time_t secs); 2340dafa5cdSNeel Natu int vm_rtc_gettime(struct vmctx *ctx, time_t *secs); 2350dafa5cdSNeel Natu 236366f6083SPeter Grehan /* Reset vcpu register state */ 2377d9ef309SJohn Baldwin int vcpu_reset(struct vcpu *vcpu); 238366f6083SPeter Grehan 23995ebc360SNeel Natu int vm_active_cpus(struct vmctx *ctx, cpuset_t *cpus); 24095ebc360SNeel Natu int vm_suspended_cpus(struct vmctx *ctx, cpuset_t *cpus); 241fc276d92SJohn Baldwin int vm_debug_cpus(struct vmctx *ctx, cpuset_t *cpus); 2427d9ef309SJohn Baldwin int vm_activate_cpu(struct vcpu *vcpu); 2437d9ef309SJohn Baldwin int vm_suspend_all_cpus(struct vmctx *ctx); 2447d9ef309SJohn Baldwin int vm_suspend_cpu(struct vcpu *vcpu); 2457d9ef309SJohn Baldwin int vm_resume_all_cpus(struct vmctx *ctx); 2467d9ef309SJohn Baldwin int vm_resume_cpu(struct vcpu *vcpu); 2477d9ef309SJohn Baldwin int vm_restart_instruction(struct vcpu *vcpu); 24895ebc360SNeel Natu 24901d822d3SRodney W. Grimes /* CPU topology */ 25001d822d3SRodney W. Grimes int vm_set_topology(struct vmctx *ctx, uint16_t sockets, uint16_t cores, 25101d822d3SRodney W. Grimes uint16_t threads, uint16_t maxcpus); 25201d822d3SRodney W. Grimes int vm_get_topology(struct vmctx *ctx, uint16_t *sockets, uint16_t *cores, 25301d822d3SRodney W. Grimes uint16_t *threads, uint16_t *maxcpus); 25401d822d3SRodney W. Grimes 255366f6083SPeter Grehan /* 256366f6083SPeter Grehan * FreeBSD specific APIs 257366f6083SPeter Grehan */ 2587d9ef309SJohn Baldwin int vm_setup_freebsd_registers(struct vcpu *vcpu, 259366f6083SPeter Grehan uint64_t rip, uint64_t cr3, uint64_t gdtbase, 260366f6083SPeter Grehan uint64_t rsp); 2617d9ef309SJohn Baldwin int vm_setup_freebsd_registers_i386(struct vcpu *vcpu, 26200f3efe1SJohn Baldwin uint32_t eip, uint32_t gdtbase, 26300f3efe1SJohn Baldwin uint32_t esp); 264366f6083SPeter Grehan void vm_setup_freebsd_gdt(uint64_t *gdtr); 265483d953aSJohn Baldwin 266483d953aSJohn Baldwin /* 267483d953aSJohn Baldwin * Save and restore 268483d953aSJohn Baldwin */ 2690f735657SJohn Baldwin int vm_snapshot_req(struct vmctx *ctx, struct vm_snapshot_meta *meta); 270483d953aSJohn Baldwin int vm_restore_time(struct vmctx *ctx); 2713e9b4532SMark Johnston 2723e9b4532SMark Johnston /* 2733e9b4532SMark Johnston * Deprecated interfaces, do not use them in new code. 2743e9b4532SMark Johnston */ 2753e9b4532SMark Johnston int vm_get_device_fd(struct vmctx *ctx); 2763e9b4532SMark Johnston const cap_ioctl_t *vm_get_ioctls(size_t *len); 2773e9b4532SMark Johnston 2781228a047SVitaliy Gusev __END_DECLS 279483d953aSJohn Baldwin 280366f6083SPeter Grehan #endif /* _VMMAPI_H_ */ 281