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 { uint64_t r ## name, e ## name; }
155 #else
156 /* Non-gcc sources must always use the proper 64-bit name (e.g., rax). */
157 #define __DECL_REG(name) uint64_t r ## name
158 #endif
159 
160 struct cpu_user_regs {
161     uint64_t r15;
162     uint64_t r14;
163     uint64_t r13;
164     uint64_t r12;
165     __DECL_REG(bp);
166     __DECL_REG(bx);
167     uint64_t r11;
168     uint64_t r10;
169     uint64_t r9;
170     uint64_t r8;
171     __DECL_REG(ax);
172     __DECL_REG(cx);
173     __DECL_REG(dx);
174     __DECL_REG(si);
175     __DECL_REG(di);
176     uint32_t error_code;    /* private */
177     uint32_t entry_vector;  /* private */
178     __DECL_REG(ip);
179     uint16_t cs, _pad0[1];
180     uint8_t  saved_upcall_mask;
181     uint8_t  _pad1[3];
182     __DECL_REG(flags);      /* rflags.IF == !saved_upcall_mask */
183     __DECL_REG(sp);
184     uint16_t ss, _pad2[3];
185     uint16_t es, _pad3[3];
186     uint16_t ds, _pad4[3];
187     uint16_t fs, _pad5[3]; /* Non-zero => takes precedence over fs_base.     */
188     uint16_t gs, _pad6[3]; /* Non-zero => takes precedence over gs_base_usr. */
189 };
190 typedef struct cpu_user_regs cpu_user_regs_t;
191 DEFINE_XEN_GUEST_HANDLE(cpu_user_regs_t);
192 
193 #undef __DECL_REG
194 
195 #define xen_pfn_to_cr3(pfn) ((unsigned long)(pfn) << 12)
196 #define xen_cr3_to_pfn(cr3) ((unsigned long)(cr3) >> 12)
197 
198 struct arch_vcpu_info {
199     unsigned long cr2;
200     unsigned long pad; /* sizeof(vcpu_info_t) == 64 */
201 };
202 typedef struct arch_vcpu_info arch_vcpu_info_t;
203 
204 typedef unsigned long xen_callback_t;
205 
206 /*
207  * Structure used to capture the register state at panic time.  This struct
208  * is built to mimic a similar structure in Solaris.  If there is interest
209  * in making this panic implementation an official part of Xen, this should
210  * be made more platform-neutral.
211  */
212 struct panic_regs {
213 	unsigned long pad1;
214 	unsigned long pad2;
215 	unsigned long rdi;
216 	unsigned long rsi;
217 	unsigned long rdx;
218 	unsigned long rcx;
219 	unsigned long r8;
220 	unsigned long r9;
221 	unsigned long rax;
222 	unsigned long rbx;
223 	unsigned long rbp;
224 	unsigned long r10;
225 	unsigned long r11;
226 	unsigned long r12;
227 	unsigned long r13;
228 	unsigned long r14;
229 	unsigned long r15;
230 	unsigned long pad3;
231 	unsigned long pad4;
232 	unsigned long ds;
233 	unsigned long es;
234 	unsigned long fs;
235 	unsigned long gs;
236 	unsigned long pad5;
237 	unsigned long pad6;
238 	unsigned long rip;
239 	unsigned long cs;
240 	unsigned long rfl;
241 	unsigned long rsp;
242 	unsigned long ss;
243 };
244 
245 #endif /* !__ASSEMBLY__ */
246 
247 /* Offsets of each field in the xen_panic_regs structure.  */
248 #define PANIC_REG_PAD1		0
249 #define PANIC_REG_PAD2		8
250 #define PANIC_REG_RDI		16
251 #define PANIC_REG_RSI		24
252 #define PANIC_REG_RDX		32
253 #define PANIC_REG_RCX		40
254 #define PANIC_REG_R8		48
255 #define PANIC_REG_R9		56
256 #define PANIC_REG_RAX		64
257 #define PANIC_REG_RBX		72
258 #define PANIC_REG_RBP		80
259 #define PANIC_REG_R10		88
260 #define PANIC_REG_R11		96
261 #define PANIC_REG_R12		104
262 #define PANIC_REG_R13		112
263 #define PANIC_REG_R14		120
264 #define PANIC_REG_R15		128
265 #define PANIC_REG_PAD3		136
266 #define PANIC_REG_PAD4		144
267 #define PANIC_REG_DS		152
268 #define PANIC_REG_ES		160
269 #define PANIC_REG_FS		168
270 #define PANIC_REG_GS		176
271 #define PANIC_REG_PAD5		184
272 #define PANIC_REG_PAD6		192
273 #define PANIC_REG_RIP		200
274 #define PANIC_REG_CS		208
275 #define PANIC_REG_RFL		216
276 #define PANIC_REG_RSP		224
277 #define PANIC_REG_SS		232
278 #define PANIC_REG_STRUCT_SIZE	240
279 
280 #endif /* __XEN_PUBLIC_ARCH_X86_XEN_X86_64_H__ */
281 
282 /*
283  * Local variables:
284  * mode: C
285  * c-set-style: "BSD"
286  * c-basic-offset: 4
287  * tab-width: 4
288  * indent-tabs-mode: nil
289  * End:
290  */
291