1843e1988Sjohnlev /******************************************************************************
2843e1988Sjohnlev  * xen-x86_64.h
3843e1988Sjohnlev  *
4843e1988Sjohnlev  * Guest OS interface to x86 64-bit Xen.
5843e1988Sjohnlev  *
6843e1988Sjohnlev  * Permission is hereby granted, free of charge, to any person obtaining a copy
7843e1988Sjohnlev  * of this software and associated documentation files (the "Software"), to
8843e1988Sjohnlev  * deal in the Software without restriction, including without limitation the
9843e1988Sjohnlev  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10843e1988Sjohnlev  * sell copies of the Software, and to permit persons to whom the Software is
11843e1988Sjohnlev  * furnished to do so, subject to the following conditions:
12843e1988Sjohnlev  *
13843e1988Sjohnlev  * The above copyright notice and this permission notice shall be included in
14843e1988Sjohnlev  * all copies or substantial portions of the Software.
15843e1988Sjohnlev  *
16843e1988Sjohnlev  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17843e1988Sjohnlev  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18843e1988Sjohnlev  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19843e1988Sjohnlev  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20843e1988Sjohnlev  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21843e1988Sjohnlev  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22843e1988Sjohnlev  * DEALINGS IN THE SOFTWARE.
23843e1988Sjohnlev  *
24843e1988Sjohnlev  * Copyright (c) 2004-2006, K A Fraser
25843e1988Sjohnlev  */
26843e1988Sjohnlev 
27843e1988Sjohnlev #ifndef __XEN_PUBLIC_ARCH_X86_XEN_X86_64_H__
28843e1988Sjohnlev #define __XEN_PUBLIC_ARCH_X86_XEN_X86_64_H__
29843e1988Sjohnlev 
30843e1988Sjohnlev /*
31843e1988Sjohnlev  * Hypercall interface:
32843e1988Sjohnlev  *  Input:  %rdi, %rsi, %rdx, %r10, %r8 (arguments 1-5)
33843e1988Sjohnlev  *  Output: %rax
34843e1988Sjohnlev  * Access is via hypercall page (set up by guest loader or via a Xen MSR):
35843e1988Sjohnlev  *  call hypercall_page + hypercall-number * 32
36843e1988Sjohnlev  * Clobbered: argument registers (e.g., 2-arg hypercall clobbers %rdi,%rsi)
37843e1988Sjohnlev  */
38843e1988Sjohnlev 
39843e1988Sjohnlev /*
40*349b53ddSStuart Maybee  * Direct hypercall interface:
41843e1988Sjohnlev  * As above, except the entry sequence to the hypervisor is:
42843e1988Sjohnlev  *  mov $hypercall-number*32,%eax ; syscall
43843e1988Sjohnlev  * Clobbered: %rcx, %r11, argument registers (as above)
44843e1988Sjohnlev  */
45843e1988Sjohnlev #if !defined(_ASM)
46843e1988Sjohnlev #define TRAP_INSTR "syscall"
47843e1988Sjohnlev #else
48843e1988Sjohnlev #define TRAP_INSTR syscall
49843e1988Sjohnlev #endif
50a576ab5bSrab 
51843e1988Sjohnlev /*
52843e1988Sjohnlev  * 64-bit segment selectors
53843e1988Sjohnlev  * These flat segments are in the Xen-private section of every GDT. Since these
54843e1988Sjohnlev  * are also present in the initial GDT, many OSes will be able to avoid
55843e1988Sjohnlev  * installing their own GDT.
56843e1988Sjohnlev  */
57843e1988Sjohnlev 
58843e1988Sjohnlev #define FLAT_RING3_CS32 0xe023  /* GDT index 260 */
59843e1988Sjohnlev #define FLAT_RING3_CS64 0xe033  /* GDT index 261 */
60843e1988Sjohnlev #define FLAT_RING3_DS32 0xe02b  /* GDT index 262 */
61843e1988Sjohnlev #define FLAT_RING3_DS64 0x0000  /* NULL selector */
62843e1988Sjohnlev #define FLAT_RING3_SS32 0xe02b  /* GDT index 262 */
63843e1988Sjohnlev #define FLAT_RING3_SS64 0xe02b  /* GDT index 262 */
64843e1988Sjohnlev 
65843e1988Sjohnlev #define FLAT_KERNEL_DS64 FLAT_RING3_DS64
66843e1988Sjohnlev #define FLAT_KERNEL_DS32 FLAT_RING3_DS32
67843e1988Sjohnlev #define FLAT_KERNEL_DS   FLAT_KERNEL_DS64
68843e1988Sjohnlev #define FLAT_KERNEL_CS64 FLAT_RING3_CS64
69843e1988Sjohnlev #define FLAT_KERNEL_CS32 FLAT_RING3_CS32
70843e1988Sjohnlev #define FLAT_KERNEL_CS   FLAT_KERNEL_CS64
71843e1988Sjohnlev #define FLAT_KERNEL_SS64 FLAT_RING3_SS64
72843e1988Sjohnlev #define FLAT_KERNEL_SS32 FLAT_RING3_SS32
73843e1988Sjohnlev #define FLAT_KERNEL_SS   FLAT_KERNEL_SS64
74843e1988Sjohnlev 
75843e1988Sjohnlev #define FLAT_USER_DS64 FLAT_RING3_DS64
76843e1988Sjohnlev #define FLAT_USER_DS32 FLAT_RING3_DS32
77843e1988Sjohnlev #define FLAT_USER_DS   FLAT_USER_DS64
78843e1988Sjohnlev #define FLAT_USER_CS64 FLAT_RING3_CS64
79843e1988Sjohnlev #define FLAT_USER_CS32 FLAT_RING3_CS32
80843e1988Sjohnlev #define FLAT_USER_CS   FLAT_USER_CS64
81843e1988Sjohnlev #define FLAT_USER_SS64 FLAT_RING3_SS64
82843e1988Sjohnlev #define FLAT_USER_SS32 FLAT_RING3_SS32
83843e1988Sjohnlev #define FLAT_USER_SS   FLAT_USER_SS64
84843e1988Sjohnlev 
85843e1988Sjohnlev #define __HYPERVISOR_VIRT_START 0xFFFF800000000000
86843e1988Sjohnlev #define __HYPERVISOR_VIRT_END   0xFFFF880000000000
87843e1988Sjohnlev #define __MACH2PHYS_VIRT_START  0xFFFF800000000000
88843e1988Sjohnlev #define __MACH2PHYS_VIRT_END    0xFFFF804000000000
89843e1988Sjohnlev 
90843e1988Sjohnlev #ifndef HYPERVISOR_VIRT_START
91843e1988Sjohnlev #define HYPERVISOR_VIRT_START mk_unsigned_long(__HYPERVISOR_VIRT_START)
92843e1988Sjohnlev #define HYPERVISOR_VIRT_END   mk_unsigned_long(__HYPERVISOR_VIRT_END)
93843e1988Sjohnlev #endif
94843e1988Sjohnlev 
95843e1988Sjohnlev #define MACH2PHYS_VIRT_START  mk_unsigned_long(__MACH2PHYS_VIRT_START)
96843e1988Sjohnlev #define MACH2PHYS_VIRT_END    mk_unsigned_long(__MACH2PHYS_VIRT_END)
97843e1988Sjohnlev #define MACH2PHYS_NR_ENTRIES  ((MACH2PHYS_VIRT_END-MACH2PHYS_VIRT_START)>>3)
98843e1988Sjohnlev #ifndef machine_to_phys_mapping
99843e1988Sjohnlev #define machine_to_phys_mapping ((unsigned long *)HYPERVISOR_VIRT_START)
100843e1988Sjohnlev #endif
101843e1988Sjohnlev 
102843e1988Sjohnlev /*
103843e1988Sjohnlev  * int HYPERVISOR_set_segment_base(unsigned int which, unsigned long base)
104843e1988Sjohnlev  *  @which == SEGBASE_*  ;  @base == 64-bit base address
105843e1988Sjohnlev  * Returns 0 on success.
106843e1988Sjohnlev  */
107843e1988Sjohnlev #define SEGBASE_FS          0
108843e1988Sjohnlev #define SEGBASE_GS_USER     1
109843e1988Sjohnlev #define SEGBASE_GS_KERNEL   2
110843e1988Sjohnlev #define SEGBASE_GS_USER_SEL 3 /* Set user %gs specified in base[15:0] */
111843e1988Sjohnlev 
112843e1988Sjohnlev /*
113843e1988Sjohnlev  * int HYPERVISOR_iret(void)
114843e1988Sjohnlev  * All arguments are on the kernel stack, in the following format.
115843e1988Sjohnlev  * Never returns if successful. Current kernel context is lost.
116843e1988Sjohnlev  * The saved CS is mapped as follows:
117843e1988Sjohnlev  *   RING0 -> RING3 kernel mode.
118843e1988Sjohnlev  *   RING1 -> RING3 kernel mode.
119843e1988Sjohnlev  *   RING2 -> RING3 kernel mode.
120843e1988Sjohnlev  *   RING3 -> RING3 user mode.
121843e1988Sjohnlev  * However RING0 indicates that the guest kernel should return to iteself
122843e1988Sjohnlev  * directly with
123843e1988Sjohnlev  *      orb   $3,1*8(%rsp)
124843e1988Sjohnlev  *      iretq
125843e1988Sjohnlev  * If flags contains VGCF_in_syscall:
126843e1988Sjohnlev  *   Restore RAX, RIP, RFLAGS, RSP.
127843e1988Sjohnlev  *   Discard R11, RCX, CS, SS.
128843e1988Sjohnlev  * Otherwise:
129843e1988Sjohnlev  *   Restore RAX, R11, RCX, CS:RIP, RFLAGS, SS:RSP.
130843e1988Sjohnlev  * All other registers are saved on hypercall entry and restored to user.
131843e1988Sjohnlev  */
132843e1988Sjohnlev /* Guest exited in SYSCALL context? Return to guest with SYSRET? */
133843e1988Sjohnlev #define _VGCF_in_syscall 8
134843e1988Sjohnlev #define VGCF_in_syscall  (1<<_VGCF_in_syscall)
135843e1988Sjohnlev #define VGCF_IN_SYSCALL  VGCF_in_syscall
136*349b53ddSStuart Maybee 
137*349b53ddSStuart Maybee #ifndef __ASSEMBLY__
138*349b53ddSStuart Maybee 
139843e1988Sjohnlev struct iret_context {
140843e1988Sjohnlev     /* Top of stack (%rsp at point of hypercall). */
141843e1988Sjohnlev     uint64_t rax, r11, rcx, flags, rip, cs, rflags, rsp, ss;
142843e1988Sjohnlev     /* Bottom of iret stack frame. */
143843e1988Sjohnlev };
144843e1988Sjohnlev 
145*349b53ddSStuart Maybee #if defined(__GNUC__) && !defined(__STRICT_ANSI__)
146843e1988Sjohnlev /* Anonymous union includes both 32- and 64-bit names (e.g., eax/rax). */
147a576ab5bSrab #define __DECL_REG(name) union { \
148a576ab5bSrab     uint64_t r ## name, e ## name; \
149a576ab5bSrab     uint32_t _e ## name; \
150a576ab5bSrab }
151843e1988Sjohnlev #else
152843e1988Sjohnlev /* Non-gcc sources must always use the proper 64-bit name (e.g., rax). */
153843e1988Sjohnlev #define __DECL_REG(name) uint64_t r ## name
154843e1988Sjohnlev #endif
155843e1988Sjohnlev 
156843e1988Sjohnlev struct cpu_user_regs {
157843e1988Sjohnlev     uint64_t r15;
158843e1988Sjohnlev     uint64_t r14;
159843e1988Sjohnlev     uint64_t r13;
160843e1988Sjohnlev     uint64_t r12;
161843e1988Sjohnlev     __DECL_REG(bp);
162843e1988Sjohnlev     __DECL_REG(bx);
163843e1988Sjohnlev     uint64_t r11;
164843e1988Sjohnlev     uint64_t r10;
165843e1988Sjohnlev     uint64_t r9;
166843e1988Sjohnlev     uint64_t r8;
167843e1988Sjohnlev     __DECL_REG(ax);
168843e1988Sjohnlev     __DECL_REG(cx);
169843e1988Sjohnlev     __DECL_REG(dx);
170843e1988Sjohnlev     __DECL_REG(si);
171843e1988Sjohnlev     __DECL_REG(di);
172843e1988Sjohnlev     uint32_t error_code;    /* private */
173843e1988Sjohnlev     uint32_t entry_vector;  /* private */
174843e1988Sjohnlev     __DECL_REG(ip);
175843e1988Sjohnlev     uint16_t cs, _pad0[1];
176843e1988Sjohnlev     uint8_t  saved_upcall_mask;
177843e1988Sjohnlev     uint8_t  _pad1[3];
178843e1988Sjohnlev     __DECL_REG(flags);      /* rflags.IF == !saved_upcall_mask */
179843e1988Sjohnlev     __DECL_REG(sp);
180843e1988Sjohnlev     uint16_t ss, _pad2[3];
181843e1988Sjohnlev     uint16_t es, _pad3[3];
182843e1988Sjohnlev     uint16_t ds, _pad4[3];
183843e1988Sjohnlev     uint16_t fs, _pad5[3]; /* Non-zero => takes precedence over fs_base.     */
184843e1988Sjohnlev     uint16_t gs, _pad6[3]; /* Non-zero => takes precedence over gs_base_usr. */
185843e1988Sjohnlev };
186843e1988Sjohnlev typedef struct cpu_user_regs cpu_user_regs_t;
187843e1988Sjohnlev DEFINE_XEN_GUEST_HANDLE(cpu_user_regs_t);
188843e1988Sjohnlev 
189843e1988Sjohnlev #undef __DECL_REG
190843e1988Sjohnlev 
191843e1988Sjohnlev #define xen_pfn_to_cr3(pfn) ((unsigned long)(pfn) << 12)
192843e1988Sjohnlev #define xen_cr3_to_pfn(cr3) ((unsigned long)(cr3) >> 12)
193843e1988Sjohnlev 
194843e1988Sjohnlev struct arch_vcpu_info {
195843e1988Sjohnlev     unsigned long cr2;
196843e1988Sjohnlev     unsigned long pad; /* sizeof(vcpu_info_t) == 64 */
197843e1988Sjohnlev };
198843e1988Sjohnlev typedef struct arch_vcpu_info arch_vcpu_info_t;
199843e1988Sjohnlev 
200843e1988Sjohnlev typedef unsigned long xen_callback_t;
201843e1988Sjohnlev 
202843e1988Sjohnlev /*
203843e1988Sjohnlev  * Structure used to capture the register state at panic time.  This struct
204843e1988Sjohnlev  * is built to mimic a similar structure in Solaris.  If there is interest
205843e1988Sjohnlev  * in making this panic implementation an official part of Xen, this should
206843e1988Sjohnlev  * be made more platform-neutral.
207843e1988Sjohnlev  */
208843e1988Sjohnlev struct panic_regs {
209843e1988Sjohnlev 	unsigned long pad1;
210843e1988Sjohnlev 	unsigned long pad2;
211843e1988Sjohnlev 	unsigned long rdi;
212843e1988Sjohnlev 	unsigned long rsi;
213843e1988Sjohnlev 	unsigned long rdx;
214843e1988Sjohnlev 	unsigned long rcx;
215843e1988Sjohnlev 	unsigned long r8;
216843e1988Sjohnlev 	unsigned long r9;
217843e1988Sjohnlev 	unsigned long rax;
218843e1988Sjohnlev 	unsigned long rbx;
219843e1988Sjohnlev 	unsigned long rbp;
220843e1988Sjohnlev 	unsigned long r10;
221843e1988Sjohnlev 	unsigned long r11;
222843e1988Sjohnlev 	unsigned long r12;
223843e1988Sjohnlev 	unsigned long r13;
224843e1988Sjohnlev 	unsigned long r14;
225843e1988Sjohnlev 	unsigned long r15;
226843e1988Sjohnlev 	unsigned long pad3;
227843e1988Sjohnlev 	unsigned long pad4;
228843e1988Sjohnlev 	unsigned long ds;
229843e1988Sjohnlev 	unsigned long es;
230843e1988Sjohnlev 	unsigned long fs;
231843e1988Sjohnlev 	unsigned long gs;
232843e1988Sjohnlev 	unsigned long pad5;
233843e1988Sjohnlev 	unsigned long pad6;
234843e1988Sjohnlev 	unsigned long rip;
235843e1988Sjohnlev 	unsigned long cs;
236843e1988Sjohnlev 	unsigned long rfl;
237843e1988Sjohnlev 	unsigned long rsp;
238843e1988Sjohnlev 	unsigned long ss;
239843e1988Sjohnlev };
240843e1988Sjohnlev 
241843e1988Sjohnlev #endif /* !__ASSEMBLY__ */
242843e1988Sjohnlev 
243843e1988Sjohnlev /* Offsets of each field in the xen_panic_regs structure.  */
244843e1988Sjohnlev #define PANIC_REG_PAD1		0
245843e1988Sjohnlev #define PANIC_REG_PAD2		8
246843e1988Sjohnlev #define PANIC_REG_RDI		16
247843e1988Sjohnlev #define PANIC_REG_RSI		24
248843e1988Sjohnlev #define PANIC_REG_RDX		32
249843e1988Sjohnlev #define PANIC_REG_RCX		40
250843e1988Sjohnlev #define PANIC_REG_R8		48
251843e1988Sjohnlev #define PANIC_REG_R9		56
252843e1988Sjohnlev #define PANIC_REG_RAX		64
253843e1988Sjohnlev #define PANIC_REG_RBX		72
254843e1988Sjohnlev #define PANIC_REG_RBP		80
255843e1988Sjohnlev #define PANIC_REG_R10		88
256843e1988Sjohnlev #define PANIC_REG_R11		96
257843e1988Sjohnlev #define PANIC_REG_R12		104
258843e1988Sjohnlev #define PANIC_REG_R13		112
259843e1988Sjohnlev #define PANIC_REG_R14		120
260843e1988Sjohnlev #define PANIC_REG_R15		128
261843e1988Sjohnlev #define PANIC_REG_PAD3		136
262843e1988Sjohnlev #define PANIC_REG_PAD4		144
263843e1988Sjohnlev #define PANIC_REG_DS		152
264843e1988Sjohnlev #define PANIC_REG_ES		160
265843e1988Sjohnlev #define PANIC_REG_FS		168
266843e1988Sjohnlev #define PANIC_REG_GS		176
267843e1988Sjohnlev #define PANIC_REG_PAD5		184
268843e1988Sjohnlev #define PANIC_REG_PAD6		192
269843e1988Sjohnlev #define PANIC_REG_RIP		200
270843e1988Sjohnlev #define PANIC_REG_CS		208
271843e1988Sjohnlev #define PANIC_REG_RFL		216
272843e1988Sjohnlev #define PANIC_REG_RSP		224
273843e1988Sjohnlev #define PANIC_REG_SS		232
274843e1988Sjohnlev #define PANIC_REG_STRUCT_SIZE	240
275843e1988Sjohnlev 
276843e1988Sjohnlev #endif /* __XEN_PUBLIC_ARCH_X86_XEN_X86_64_H__ */
277843e1988Sjohnlev 
278843e1988Sjohnlev /*
279843e1988Sjohnlev  * Local variables:
280843e1988Sjohnlev  * mode: C
281843e1988Sjohnlev  * c-set-style: "BSD"
282843e1988Sjohnlev  * c-basic-offset: 4
283843e1988Sjohnlev  * tab-width: 4
284843e1988Sjohnlev  * indent-tabs-mode: nil
285843e1988Sjohnlev  * End:
286843e1988Sjohnlev  */
287