vmmapi.h revision 277359
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: head/lib/libvmmapi/vmmapi.h 277359 2015-01-19 06:51:04Z neel $
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 */
39277310Sneel#define	VMMAPI_VERSION	0101	/* 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
55265951Sneel#define	VM_MEM_F_INCORE	0x01	/* include guest memory in core file */
56265951Sneel
57221828Sgrehanint	vm_create(const char *name);
58221828Sgrehanstruct vmctx *vm_open(const char *name);
59221828Sgrehanvoid	vm_destroy(struct vmctx *ctx);
60256176Sneelint	vm_parse_memsize(const char *optarg, size_t *memsize);
61256072Sneelint	vm_get_memory_seg(struct vmctx *ctx, vm_paddr_t gpa, size_t *ret_len,
62256072Sneel			  int *wired);
63248477Sneelint	vm_setup_memory(struct vmctx *ctx, size_t len, enum vm_mmap_style s);
64248477Sneelvoid	*vm_map_gpa(struct vmctx *ctx, vm_paddr_t gaddr, size_t len);
65256072Sneelint	vm_get_gpa_pmap(struct vmctx *, uint64_t gpa, uint64_t *pte, int *num);
66248477Sneeluint32_t vm_get_lowmem_limit(struct vmctx *ctx);
67248477Sneelvoid	vm_set_lowmem_limit(struct vmctx *ctx, uint32_t limit);
68265951Sneelvoid	vm_set_memflags(struct vmctx *ctx, int flags);
69267811Sneelsize_t	vm_get_lowmem_size(struct vmctx *ctx);
70267811Sneelsize_t	vm_get_highmem_size(struct vmctx *ctx);
71221828Sgrehanint	vm_set_desc(struct vmctx *ctx, int vcpu, int reg,
72221828Sgrehan		    uint64_t base, uint32_t limit, uint32_t access);
73221828Sgrehanint	vm_get_desc(struct vmctx *ctx, int vcpu, int reg,
74221828Sgrehan		    uint64_t *base, uint32_t *limit, uint32_t *access);
75269008Sneelint	vm_get_seg_desc(struct vmctx *ctx, int vcpu, int reg,
76269008Sneel			struct seg_desc *seg_desc);
77221828Sgrehanint	vm_set_register(struct vmctx *ctx, int vcpu, int reg, uint64_t val);
78221828Sgrehanint	vm_get_register(struct vmctx *ctx, int vcpu, int reg, uint64_t *retval);
79277310Sneelint	vm_run(struct vmctx *ctx, int vcpu, struct vm_exit *ret_vmexit);
80265062Sneelint	vm_suspend(struct vmctx *ctx, enum vm_suspend_how how);
81267216Sneelint	vm_reinit(struct vmctx *ctx);
82239026Sneelint	vm_apicid2vcpu(struct vmctx *ctx, int apicid);
83277310Sneelint	vm_inject_exception(struct vmctx *ctx, int vcpu, int vector,
84277310Sneel    int errcode_valid, uint32_t errcode, int restart_instruction);
85221828Sgrehanint	vm_lapic_irq(struct vmctx *ctx, int vcpu, int vector);
86259779Sjhbint	vm_lapic_local_irq(struct vmctx *ctx, int vcpu, int vector);
87259482Sneelint	vm_lapic_msi(struct vmctx *ctx, uint64_t addr, uint64_t msg);
88258075Sneelint	vm_ioapic_assert_irq(struct vmctx *ctx, int irq);
89258075Sneelint	vm_ioapic_deassert_irq(struct vmctx *ctx, int irq);
90258494Sneelint	vm_ioapic_pulse_irq(struct vmctx *ctx, int irq);
91261268Sjhbint	vm_ioapic_pincount(struct vmctx *ctx, int *pincount);
92264468Stychonint	vm_isa_assert_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq);
93263035Stychonint	vm_isa_deassert_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq);
94263035Stychonint	vm_isa_pulse_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq);
95266125Sjhbint	vm_isa_set_irq_trigger(struct vmctx *ctx, int atpic_irq,
96266125Sjhb	    enum vm_intr_trigger trigger);
97221828Sgrehanint	vm_inject_nmi(struct vmctx *ctx, int vcpu);
98221828Sgrehanint	vm_capability_name2type(const char *capname);
99241486Sneelconst char *vm_capability_type2name(int type);
100221828Sgrehanint	vm_get_capability(struct vmctx *ctx, int vcpu, enum vm_cap_type cap,
101221828Sgrehan			  int *retval);
102221828Sgrehanint	vm_set_capability(struct vmctx *ctx, int vcpu, enum vm_cap_type cap,
103221828Sgrehan			  int val);
104221828Sgrehanint	vm_assign_pptdev(struct vmctx *ctx, int bus, int slot, int func);
105221828Sgrehanint	vm_unassign_pptdev(struct vmctx *ctx, int bus, int slot, int func);
106221828Sgrehanint	vm_map_pptdev_mmio(struct vmctx *ctx, int bus, int slot, int func,
107221828Sgrehan			   vm_paddr_t gpa, size_t len, vm_paddr_t hpa);
108259537Sneelint	vm_setup_pptdev_msi(struct vmctx *ctx, int vcpu, int bus, int slot,
109259537Sneel	    int func, uint64_t addr, uint64_t msg, int numvec);
110259537Sneelint	vm_setup_pptdev_msix(struct vmctx *ctx, int vcpu, int bus, int slot,
111259537Sneel	    int func, int idx, uint64_t addr, uint64_t msg,
112259537Sneel	    uint32_t vector_control);
113221828Sgrehan
114268889Sneelint	vm_get_intinfo(struct vmctx *ctx, int vcpu, uint64_t *i1, uint64_t *i2);
115268889Sneelint	vm_set_intinfo(struct vmctx *ctx, int vcpu, uint64_t exit_intinfo);
116268889Sneel
117221828Sgrehan/*
118221828Sgrehan * Return a pointer to the statistics buffer. Note that this is not MT-safe.
119221828Sgrehan */
120221828Sgrehanuint64_t *vm_get_stats(struct vmctx *ctx, int vcpu, struct timeval *ret_tv,
121221828Sgrehan		       int *ret_entries);
122221828Sgrehanconst char *vm_get_stat_desc(struct vmctx *ctx, int index);
123221828Sgrehan
124240922Sneelint	vm_get_x2apic_state(struct vmctx *ctx, int vcpu, enum x2apic_state *s);
125240922Sneelint	vm_set_x2apic_state(struct vmctx *ctx, int vcpu, enum x2apic_state s);
126240922Sneel
127258579Sneelint	vm_get_hpet_capabilities(struct vmctx *ctx, uint32_t *capabilities);
128258579Sneel
129266708Sneel/*
130266708Sneel * Translate the GLA range [gla,gla+len) into GPA segments in 'iov'.
131266708Sneel * The 'iovcnt' should be big enough to accomodate all GPA segments.
132266708Sneel * Returns 0 on success, 1 on a guest fault condition and -1 otherwise.
133266708Sneel */
134269008Sneelint	vm_copy_setup(struct vmctx *ctx, int vcpu, struct vm_guest_paging *pg,
135266708Sneel	    uint64_t gla, size_t len, int prot, struct iovec *iov, int iovcnt);
136266708Sneelvoid	vm_copyin(struct vmctx *ctx, int vcpu, struct iovec *guest_iov,
137266708Sneel	    void *host_dst, size_t len);
138266708Sneelvoid	vm_copyout(struct vmctx *ctx, int vcpu, const void *host_src,
139266708Sneel	    struct iovec *guest_iov, size_t len);
140277359Sneelvoid	vm_copy_teardown(struct vmctx *ctx, int vcpu, struct iovec *iov,
141277359Sneel	    int iovcnt);
142266633Sneel
143276428Sneel/* RTC */
144276428Sneelint	vm_rtc_write(struct vmctx *ctx, int offset, uint8_t value);
145276428Sneelint	vm_rtc_read(struct vmctx *ctx, int offset, uint8_t *retval);
146276428Sneelint	vm_rtc_settime(struct vmctx *ctx, time_t secs);
147276428Sneelint	vm_rtc_gettime(struct vmctx *ctx, time_t *secs);
148276428Sneel
149221828Sgrehan/* Reset vcpu register state */
150221828Sgrehanint	vcpu_reset(struct vmctx *ctx, int vcpu);
151221828Sgrehan
152266933Sneelint	vm_active_cpus(struct vmctx *ctx, cpuset_t *cpus);
153266933Sneelint	vm_suspended_cpus(struct vmctx *ctx, cpuset_t *cpus);
154266933Sneelint	vm_activate_cpu(struct vmctx *ctx, int vcpu);
155266933Sneel
156221828Sgrehan/*
157221828Sgrehan * FreeBSD specific APIs
158221828Sgrehan */
159221828Sgrehanint	vm_setup_freebsd_registers(struct vmctx *ctx, int vcpu,
160221828Sgrehan				uint64_t rip, uint64_t cr3, uint64_t gdtbase,
161221828Sgrehan				uint64_t rsp);
162261504Sjhbint	vm_setup_freebsd_registers_i386(struct vmctx *vmctx, int vcpu,
163261504Sjhb					uint32_t eip, uint32_t gdtbase,
164261504Sjhb					uint32_t esp);
165221828Sgrehanvoid	vm_setup_freebsd_gdt(uint64_t *gdtr);
166221828Sgrehan#endif	/* _VMMAPI_H_ */
167