xref: /illumos-gate/usr/src/uts/common/xen/public/arch-x86/xen-x86_64.h (revision c26dc428cee0f025b14a5ad03a7722f2a52f8383)
1 /******************************************************************************
2  * xen-x86_64.h
3  *
4  * Guest OS interface to x86 64-bit Xen.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  *
24  * Copyright (c) 2004-2006, K A Fraser
25  */
26 
27 #ifndef __XEN_PUBLIC_ARCH_X86_XEN_X86_64_H__
28 #define __XEN_PUBLIC_ARCH_X86_XEN_X86_64_H__
29 
30 /*
31  * Hypercall interface:
32  *  Input:  %rdi, %rsi, %rdx, %r10, %r8 (arguments 1-5)
33  *  Output: %rax
34  * Access is via hypercall page (set up by guest loader or via a Xen MSR):
35  *  call hypercall_page + hypercall-number * 32
36  * Clobbered: argument registers (e.g., 2-arg hypercall clobbers %rdi,%rsi)
37  */
38 
39 /*
40  * XXPV HACK, we don't support the hypercall page yet.
41  * #if __XEN_INTERFACE_VERSION__ < 0x00030203
42  */
43 /*
44  * Legacy hypercall interface:
45  * As above, except the entry sequence to the hypervisor is:
46  *  mov $hypercall-number*32,%eax ; syscall
47  * Clobbered: %rcx, %r11, argument registers (as above)
48  */
49 #if !defined(_ASM)
50 #define TRAP_INSTR "syscall"
51 #else
52 #define TRAP_INSTR syscall
53 #endif
54 /*
55  * XXPV HACK, we don't support the hypercall page yet.
56  * #endif
57  */
58 
59 /*
60  * 64-bit segment selectors
61  * These flat segments are in the Xen-private section of every GDT. Since these
62  * are also present in the initial GDT, many OSes will be able to avoid
63  * installing their own GDT.
64  */
65 
66 #define FLAT_RING3_CS32 0xe023  /* GDT index 260 */
67 #define FLAT_RING3_CS64 0xe033  /* GDT index 261 */
68 #define FLAT_RING3_DS32 0xe02b  /* GDT index 262 */
69 #define FLAT_RING3_DS64 0x0000  /* NULL selector */
70 #define FLAT_RING3_SS32 0xe02b  /* GDT index 262 */
71 #define FLAT_RING3_SS64 0xe02b  /* GDT index 262 */
72 
73 #define FLAT_KERNEL_DS64 FLAT_RING3_DS64
74 #define FLAT_KERNEL_DS32 FLAT_RING3_DS32
75 #define FLAT_KERNEL_DS   FLAT_KERNEL_DS64
76 #define FLAT_KERNEL_CS64 FLAT_RING3_CS64
77 #define FLAT_KERNEL_CS32 FLAT_RING3_CS32
78 #define FLAT_KERNEL_CS   FLAT_KERNEL_CS64
79 #define FLAT_KERNEL_SS64 FLAT_RING3_SS64
80 #define FLAT_KERNEL_SS32 FLAT_RING3_SS32
81 #define FLAT_KERNEL_SS   FLAT_KERNEL_SS64
82 
83 #define FLAT_USER_DS64 FLAT_RING3_DS64
84 #define FLAT_USER_DS32 FLAT_RING3_DS32
85 #define FLAT_USER_DS   FLAT_USER_DS64
86 #define FLAT_USER_CS64 FLAT_RING3_CS64
87 #define FLAT_USER_CS32 FLAT_RING3_CS32
88 #define FLAT_USER_CS   FLAT_USER_CS64
89 #define FLAT_USER_SS64 FLAT_RING3_SS64
90 #define FLAT_USER_SS32 FLAT_RING3_SS32
91 #define FLAT_USER_SS   FLAT_USER_SS64
92 
93 #define __HYPERVISOR_VIRT_START 0xFFFF800000000000
94 #define __HYPERVISOR_VIRT_END   0xFFFF880000000000
95 #define __MACH2PHYS_VIRT_START  0xFFFF800000000000
96 #define __MACH2PHYS_VIRT_END    0xFFFF804000000000
97 
98 #ifndef HYPERVISOR_VIRT_START
99 #define HYPERVISOR_VIRT_START mk_unsigned_long(__HYPERVISOR_VIRT_START)
100 #define HYPERVISOR_VIRT_END   mk_unsigned_long(__HYPERVISOR_VIRT_END)
101 #endif
102 
103 #define MACH2PHYS_VIRT_START  mk_unsigned_long(__MACH2PHYS_VIRT_START)
104 #define MACH2PHYS_VIRT_END    mk_unsigned_long(__MACH2PHYS_VIRT_END)
105 #define MACH2PHYS_NR_ENTRIES  ((MACH2PHYS_VIRT_END-MACH2PHYS_VIRT_START)>>3)
106 #ifndef machine_to_phys_mapping
107 #define machine_to_phys_mapping ((unsigned long *)HYPERVISOR_VIRT_START)
108 #endif
109 
110 #ifndef __ASSEMBLY__
111 
112 /*
113  * int HYPERVISOR_set_segment_base(unsigned int which, unsigned long base)
114  *  @which == SEGBASE_*  ;  @base == 64-bit base address
115  * Returns 0 on success.
116  */
117 #define SEGBASE_FS          0
118 #define SEGBASE_GS_USER     1
119 #define SEGBASE_GS_KERNEL   2
120 #define SEGBASE_GS_USER_SEL 3 /* Set user %gs specified in base[15:0] */
121 
122 /*
123  * int HYPERVISOR_iret(void)
124  * All arguments are on the kernel stack, in the following format.
125  * Never returns if successful. Current kernel context is lost.
126  * The saved CS is mapped as follows:
127  *   RING0 -> RING3 kernel mode.
128  *   RING1 -> RING3 kernel mode.
129  *   RING2 -> RING3 kernel mode.
130  *   RING3 -> RING3 user mode.
131  * However RING0 indicates that the guest kernel should return to iteself
132  * directly with
133  *      orb   $3,1*8(%rsp)
134  *      iretq
135  * If flags contains VGCF_in_syscall:
136  *   Restore RAX, RIP, RFLAGS, RSP.
137  *   Discard R11, RCX, CS, SS.
138  * Otherwise:
139  *   Restore RAX, R11, RCX, CS:RIP, RFLAGS, SS:RSP.
140  * All other registers are saved on hypercall entry and restored to user.
141  */
142 /* Guest exited in SYSCALL context? Return to guest with SYSRET? */
143 #define _VGCF_in_syscall 8
144 #define VGCF_in_syscall  (1<<_VGCF_in_syscall)
145 #define VGCF_IN_SYSCALL  VGCF_in_syscall
146 struct iret_context {
147     /* Top of stack (%rsp at point of hypercall). */
148     uint64_t rax, r11, rcx, flags, rip, cs, rflags, rsp, ss;
149     /* Bottom of iret stack frame. */
150 };
151 
152 #ifdef __GNUC__
153 /* Anonymous union includes both 32- and 64-bit names (e.g., eax/rax). */
154 #define __DECL_REG(name) union { \
155     uint64_t r ## name, e ## name; \
156     uint32_t _e ## name; \
157 }
158 #else
159 /* Non-gcc sources must always use the proper 64-bit name (e.g., rax). */
160 #define __DECL_REG(name) uint64_t r ## name
161 #endif
162 
163 struct cpu_user_regs {
164     uint64_t r15;
165     uint64_t r14;
166     uint64_t r13;
167     uint64_t r12;
168     __DECL_REG(bp);
169     __DECL_REG(bx);
170     uint64_t r11;
171     uint64_t r10;
172     uint64_t r9;
173     uint64_t r8;
174     __DECL_REG(ax);
175     __DECL_REG(cx);
176     __DECL_REG(dx);
177     __DECL_REG(si);
178     __DECL_REG(di);
179     uint32_t error_code;    /* private */
180     uint32_t entry_vector;  /* private */
181     __DECL_REG(ip);
182     uint16_t cs, _pad0[1];
183     uint8_t  saved_upcall_mask;
184     uint8_t  _pad1[3];
185     __DECL_REG(flags);      /* rflags.IF == !saved_upcall_mask */
186     __DECL_REG(sp);
187     uint16_t ss, _pad2[3];
188     uint16_t es, _pad3[3];
189     uint16_t ds, _pad4[3];
190     uint16_t fs, _pad5[3]; /* Non-zero => takes precedence over fs_base.     */
191     uint16_t gs, _pad6[3]; /* Non-zero => takes precedence over gs_base_usr. */
192 };
193 typedef struct cpu_user_regs cpu_user_regs_t;
194 DEFINE_XEN_GUEST_HANDLE(cpu_user_regs_t);
195 
196 #undef __DECL_REG
197 
198 #define xen_pfn_to_cr3(pfn) ((unsigned long)(pfn) << 12)
199 #define xen_cr3_to_pfn(cr3) ((unsigned long)(cr3) >> 12)
200 
201 struct arch_vcpu_info {
202     unsigned long cr2;
203     unsigned long pad; /* sizeof(vcpu_info_t) == 64 */
204 };
205 typedef struct arch_vcpu_info arch_vcpu_info_t;
206 
207 typedef unsigned long xen_callback_t;
208 
209 /*
210  * Structure used to capture the register state at panic time.  This struct
211  * is built to mimic a similar structure in Solaris.  If there is interest
212  * in making this panic implementation an official part of Xen, this should
213  * be made more platform-neutral.
214  */
215 struct panic_regs {
216 	unsigned long pad1;
217 	unsigned long pad2;
218 	unsigned long rdi;
219 	unsigned long rsi;
220 	unsigned long rdx;
221 	unsigned long rcx;
222 	unsigned long r8;
223 	unsigned long r9;
224 	unsigned long rax;
225 	unsigned long rbx;
226 	unsigned long rbp;
227 	unsigned long r10;
228 	unsigned long r11;
229 	unsigned long r12;
230 	unsigned long r13;
231 	unsigned long r14;
232 	unsigned long r15;
233 	unsigned long pad3;
234 	unsigned long pad4;
235 	unsigned long ds;
236 	unsigned long es;
237 	unsigned long fs;
238 	unsigned long gs;
239 	unsigned long pad5;
240 	unsigned long pad6;
241 	unsigned long rip;
242 	unsigned long cs;
243 	unsigned long rfl;
244 	unsigned long rsp;
245 	unsigned long ss;
246 };
247 
248 #endif /* !__ASSEMBLY__ */
249 
250 /* Offsets of each field in the xen_panic_regs structure.  */
251 #define PANIC_REG_PAD1		0
252 #define PANIC_REG_PAD2		8
253 #define PANIC_REG_RDI		16
254 #define PANIC_REG_RSI		24
255 #define PANIC_REG_RDX		32
256 #define PANIC_REG_RCX		40
257 #define PANIC_REG_R8		48
258 #define PANIC_REG_R9		56
259 #define PANIC_REG_RAX		64
260 #define PANIC_REG_RBX		72
261 #define PANIC_REG_RBP		80
262 #define PANIC_REG_R10		88
263 #define PANIC_REG_R11		96
264 #define PANIC_REG_R12		104
265 #define PANIC_REG_R13		112
266 #define PANIC_REG_R14		120
267 #define PANIC_REG_R15		128
268 #define PANIC_REG_PAD3		136
269 #define PANIC_REG_PAD4		144
270 #define PANIC_REG_DS		152
271 #define PANIC_REG_ES		160
272 #define PANIC_REG_FS		168
273 #define PANIC_REG_GS		176
274 #define PANIC_REG_PAD5		184
275 #define PANIC_REG_PAD6		192
276 #define PANIC_REG_RIP		200
277 #define PANIC_REG_CS		208
278 #define PANIC_REG_RFL		216
279 #define PANIC_REG_RSP		224
280 #define PANIC_REG_SS		232
281 #define PANIC_REG_STRUCT_SIZE	240
282 
283 #endif /* __XEN_PUBLIC_ARCH_X86_XEN_X86_64_H__ */
284 
285 /*
286  * Local variables:
287  * mode: C
288  * c-set-style: "BSD"
289  * c-basic-offset: 4
290  * tab-width: 4
291  * indent-tabs-mode: nil
292  * End:
293  */
294