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$
27221828Sgrehan */
28221828Sgrehan
29221828Sgrehan#ifndef _VMMAPI_H_
30221828Sgrehan#define	_VMMAPI_H_
31221828Sgrehan
32270070Sgrehan#include <sys/param.h>
33270070Sgrehan#include <sys/cpuset.h>
34270070Sgrehan
35284894Sneel/*
36284894Sneel * API version for out-of-tree consumers like grub-bhyve for making compile
37284894Sneel * time decisions.
38284894Sneel */
39295124Sgrehan#define	VMMAPI_VERSION	0102	/* 2 digit major followed by 2 digit minor */
40284894Sneel
41268976Sjhbstruct 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
55295124Sgrehan/*
56295124Sgrehan * 'flags' value passed to 'vm_set_memflags()'.
57295124Sgrehan */
58268953Sjhb#define	VM_MEM_F_INCORE	0x01	/* include guest memory in core file */
59295124Sgrehan#define	VM_MEM_F_WIRED	0x02	/* guest memory is wired */
60268953Sjhb
61295124Sgrehan/*
62295124Sgrehan * Identifiers for memory segments:
63295124Sgrehan * - vm_setup_memory() uses VM_SYSMEM for the system memory segment.
64295124Sgrehan * - the remaining identifiers can be used to create devmem segments.
65295124Sgrehan */
66295124Sgrehanenum {
67295124Sgrehan	VM_SYSMEM,
68295124Sgrehan	VM_BOOTROM,
69295124Sgrehan	VM_FRAMEBUFFER,
70295124Sgrehan};
71295124Sgrehan
72295124Sgrehan/*
73295124Sgrehan * Get the length and name of the memory segment identified by 'segid'.
74295124Sgrehan * Note that system memory segments are identified with a nul name.
75295124Sgrehan *
76295124Sgrehan * Returns 0 on success and non-zero otherwise.
77295124Sgrehan */
78295124Sgrehanint	vm_get_memseg(struct vmctx *ctx, int ident, size_t *lenp, char *name,
79295124Sgrehan	    size_t namesiz);
80295124Sgrehan
81295124Sgrehan/*
82295124Sgrehan * Iterate over the guest address space. This function finds an address range
83295124Sgrehan * that starts at an address >= *gpa.
84295124Sgrehan *
85295124Sgrehan * Returns 0 if the next address range was found and non-zero otherwise.
86295124Sgrehan */
87295124Sgrehanint	vm_mmap_getnext(struct vmctx *ctx, vm_paddr_t *gpa, int *segid,
88295124Sgrehan	    vm_ooffset_t *segoff, size_t *len, int *prot, int *flags);
89295124Sgrehan/*
90295124Sgrehan * Create a device memory segment identified by 'segid'.
91295124Sgrehan *
92295124Sgrehan * Returns a pointer to the memory segment on success and MAP_FAILED otherwise.
93295124Sgrehan */
94295124Sgrehanvoid	*vm_create_devmem(struct vmctx *ctx, int segid, const char *name,
95295124Sgrehan	    size_t len);
96295124Sgrehan
97295124Sgrehan/*
98295124Sgrehan * Map the memory segment identified by 'segid' into the guest address space
99295124Sgrehan * at [gpa,gpa+len) with protection 'prot'.
100295124Sgrehan */
101295124Sgrehanint	vm_mmap_memseg(struct vmctx *ctx, vm_paddr_t gpa, int segid,
102295124Sgrehan	    vm_ooffset_t segoff, size_t len, int prot);
103295124Sgrehan
104221828Sgrehanint	vm_create(const char *name);
105221828Sgrehanstruct vmctx *vm_open(const char *name);
106221828Sgrehanvoid	vm_destroy(struct vmctx *ctx);
107256176Sneelint	vm_parse_memsize(const char *optarg, size_t *memsize);
108248477Sneelint	vm_setup_memory(struct vmctx *ctx, size_t len, enum vm_mmap_style s);
109248477Sneelvoid	*vm_map_gpa(struct vmctx *ctx, vm_paddr_t gaddr, size_t len);
110256072Sneelint	vm_get_gpa_pmap(struct vmctx *, uint64_t gpa, uint64_t *pte, int *num);
111284899Sneelint	vm_gla2gpa(struct vmctx *, int vcpuid, struct vm_guest_paging *paging,
112284900Sneel		   uint64_t gla, int prot, uint64_t *gpa, int *fault);
113248477Sneeluint32_t vm_get_lowmem_limit(struct vmctx *ctx);
114248477Sneelvoid	vm_set_lowmem_limit(struct vmctx *ctx, uint32_t limit);
115268953Sjhbvoid	vm_set_memflags(struct vmctx *ctx, int flags);
116295124Sgrehanint	vm_get_memflags(struct vmctx *ctx);
117270074Sgrehansize_t	vm_get_lowmem_size(struct vmctx *ctx);
118270074Sgrehansize_t	vm_get_highmem_size(struct vmctx *ctx);
119221828Sgrehanint	vm_set_desc(struct vmctx *ctx, int vcpu, int reg,
120221828Sgrehan		    uint64_t base, uint32_t limit, uint32_t access);
121221828Sgrehanint	vm_get_desc(struct vmctx *ctx, int vcpu, int reg,
122221828Sgrehan		    uint64_t *base, uint32_t *limit, uint32_t *access);
123270159Sgrehanint	vm_get_seg_desc(struct vmctx *ctx, int vcpu, int reg,
124270159Sgrehan			struct seg_desc *seg_desc);
125221828Sgrehanint	vm_set_register(struct vmctx *ctx, int vcpu, int reg, uint64_t val);
126221828Sgrehanint	vm_get_register(struct vmctx *ctx, int vcpu, int reg, uint64_t *retval);
127284894Sneelint	vm_run(struct vmctx *ctx, int vcpu, struct vm_exit *ret_vmexit);
128268935Sjhbint	vm_suspend(struct vmctx *ctx, enum vm_suspend_how how);
129270071Sgrehanint	vm_reinit(struct vmctx *ctx);
130239026Sneelint	vm_apicid2vcpu(struct vmctx *ctx, int apicid);
131284894Sneelint	vm_inject_exception(struct vmctx *ctx, int vcpu, int vector,
132284894Sneel    int errcode_valid, uint32_t errcode, int restart_instruction);
133221828Sgrehanint	vm_lapic_irq(struct vmctx *ctx, int vcpu, int vector);
134262350Sjhbint	vm_lapic_local_irq(struct vmctx *ctx, int vcpu, int vector);
135262350Sjhbint	vm_lapic_msi(struct vmctx *ctx, uint64_t addr, uint64_t msg);
136261088Sjhbint	vm_ioapic_assert_irq(struct vmctx *ctx, int irq);
137261088Sjhbint	vm_ioapic_deassert_irq(struct vmctx *ctx, int irq);
138261088Sjhbint	vm_ioapic_pulse_irq(struct vmctx *ctx, int irq);
139267393Sjhbint	vm_ioapic_pincount(struct vmctx *ctx, int *pincount);
140268891Sjhbint	vm_isa_assert_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq);
141268891Sjhbint	vm_isa_deassert_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq);
142268891Sjhbint	vm_isa_pulse_irq(struct vmctx *ctx, int atpic_irq, int ioapic_irq);
143268972Sjhbint	vm_isa_set_irq_trigger(struct vmctx *ctx, int atpic_irq,
144268972Sjhb	    enum vm_intr_trigger trigger);
145221828Sgrehanint	vm_inject_nmi(struct vmctx *ctx, int vcpu);
146221828Sgrehanint	vm_capability_name2type(const char *capname);
147241486Sneelconst char *vm_capability_type2name(int type);
148221828Sgrehanint	vm_get_capability(struct vmctx *ctx, int vcpu, enum vm_cap_type cap,
149221828Sgrehan			  int *retval);
150221828Sgrehanint	vm_set_capability(struct vmctx *ctx, int vcpu, enum vm_cap_type cap,
151221828Sgrehan			  int val);
152221828Sgrehanint	vm_assign_pptdev(struct vmctx *ctx, int bus, int slot, int func);
153221828Sgrehanint	vm_unassign_pptdev(struct vmctx *ctx, int bus, int slot, int func);
154221828Sgrehanint	vm_map_pptdev_mmio(struct vmctx *ctx, int bus, int slot, int func,
155221828Sgrehan			   vm_paddr_t gpa, size_t len, vm_paddr_t hpa);
156262350Sjhbint	vm_setup_pptdev_msi(struct vmctx *ctx, int vcpu, int bus, int slot,
157262350Sjhb	    int func, uint64_t addr, uint64_t msg, int numvec);
158262350Sjhbint	vm_setup_pptdev_msix(struct vmctx *ctx, int vcpu, int bus, int slot,
159262350Sjhb	    int func, int idx, uint64_t addr, uint64_t msg,
160262350Sjhb	    uint32_t vector_control);
161221828Sgrehan
162270159Sgrehanint	vm_get_intinfo(struct vmctx *ctx, int vcpu, uint64_t *i1, uint64_t *i2);
163270159Sgrehanint	vm_set_intinfo(struct vmctx *ctx, int vcpu, uint64_t exit_intinfo);
164270159Sgrehan
165221828Sgrehan/*
166221828Sgrehan * Return a pointer to the statistics buffer. Note that this is not MT-safe.
167221828Sgrehan */
168221828Sgrehanuint64_t *vm_get_stats(struct vmctx *ctx, int vcpu, struct timeval *ret_tv,
169221828Sgrehan		       int *ret_entries);
170221828Sgrehanconst char *vm_get_stat_desc(struct vmctx *ctx, int index);
171221828Sgrehan
172240922Sneelint	vm_get_x2apic_state(struct vmctx *ctx, int vcpu, enum x2apic_state *s);
173240922Sneelint	vm_set_x2apic_state(struct vmctx *ctx, int vcpu, enum x2apic_state s);
174240922Sneel
175261088Sjhbint	vm_get_hpet_capabilities(struct vmctx *ctx, uint32_t *capabilities);
176261088Sjhb
177268976Sjhb/*
178268976Sjhb * Translate the GLA range [gla,gla+len) into GPA segments in 'iov'.
179268976Sjhb * The 'iovcnt' should be big enough to accomodate all GPA segments.
180284900Sneel *
181284900Sneel * retval	fault		Interpretation
182284900Sneel *   0		  0		Success
183284900Sneel *   0		  1		An exception was injected into the guest
184284900Sneel * EFAULT	 N/A		Error
185268976Sjhb */
186270159Sgrehanint	vm_copy_setup(struct vmctx *ctx, int vcpu, struct vm_guest_paging *pg,
187284900Sneel	    uint64_t gla, size_t len, int prot, struct iovec *iov, int iovcnt,
188284900Sneel	    int *fault);
189268976Sjhbvoid	vm_copyin(struct vmctx *ctx, int vcpu, struct iovec *guest_iov,
190268976Sjhb	    void *host_dst, size_t len);
191268976Sjhbvoid	vm_copyout(struct vmctx *ctx, int vcpu, const void *host_src,
192268976Sjhb	    struct iovec *guest_iov, size_t len);
193284894Sneelvoid	vm_copy_teardown(struct vmctx *ctx, int vcpu, struct iovec *iov,
194284894Sneel	    int iovcnt);
195268976Sjhb
196284894Sneel/* RTC */
197284894Sneelint	vm_rtc_write(struct vmctx *ctx, int offset, uint8_t value);
198284894Sneelint	vm_rtc_read(struct vmctx *ctx, int offset, uint8_t *retval);
199284894Sneelint	vm_rtc_settime(struct vmctx *ctx, time_t secs);
200284894Sneelint	vm_rtc_gettime(struct vmctx *ctx, time_t *secs);
201284894Sneel
202221828Sgrehan/* Reset vcpu register state */
203221828Sgrehanint	vcpu_reset(struct vmctx *ctx, int vcpu);
204221828Sgrehan
205270070Sgrehanint	vm_active_cpus(struct vmctx *ctx, cpuset_t *cpus);
206270070Sgrehanint	vm_suspended_cpus(struct vmctx *ctx, cpuset_t *cpus);
207270070Sgrehanint	vm_activate_cpu(struct vmctx *ctx, int vcpu);
208270070Sgrehan
209221828Sgrehan/*
210221828Sgrehan * FreeBSD specific APIs
211221828Sgrehan */
212221828Sgrehanint	vm_setup_freebsd_registers(struct vmctx *ctx, int vcpu,
213221828Sgrehan				uint64_t rip, uint64_t cr3, uint64_t gdtbase,
214221828Sgrehan				uint64_t rsp);
215267399Sjhbint	vm_setup_freebsd_registers_i386(struct vmctx *vmctx, int vcpu,
216267399Sjhb					uint32_t eip, uint32_t gdtbase,
217267399Sjhb					uint32_t esp);
218221828Sgrehanvoid	vm_setup_freebsd_gdt(uint64_t *gdtr);
219221828Sgrehan#endif	/* _VMMAPI_H_ */
220