1 /******************************************************************************
4 * Guest OS interface to x86 64-bit Xen.
6 * Copyright (c) 2004, K A Fraser
9 #ifndef __XEN_PUBLIC_ARCH_X86_64_H__
10 #define __XEN_PUBLIC_ARCH_X86_64_H__
13 * Hypercall interface:
14 * Input: %rdi, %rsi, %rdx, %r10, %r8 (arguments 1-5)
16 * Access is via hypercall page (set up by guest loader or via a Xen MSR):
17 * call hypercall_page + hypercall-number * 32
18 * Clobbered: argument registers (e.g., 2-arg hypercall clobbers %rdi,%rsi)
21 #if __XEN_INTERFACE_VERSION__ < 0x00030203
23 * Legacy hypercall interface:
24 * As above, except the entry sequence to the hypervisor is:
25 * mov $hypercall-number*32,%eax ; syscall
26 * Clobbered: %rcx, %r11, argument registers (as above)
28 #define TRAP_INSTR "syscall"
31 /* Structural guest handles introduced in 0x00030201. */
32 #if __XEN_INTERFACE_VERSION__ >= 0x00030201
33 #define __DEFINE_XEN_GUEST_HANDLE(name, type) \
34 typedef struct { type *p; } __guest_handle_ ## name
36 #define __DEFINE_XEN_GUEST_HANDLE(name, type) \
37 typedef type * __guest_handle_ ## name
40 #define DEFINE_XEN_GUEST_HANDLE(name) __DEFINE_XEN_GUEST_HANDLE(name, name)
41 #define XEN_GUEST_HANDLE(name) __guest_handle_ ## name
42 #define set_xen_guest_handle(hnd, val) do { (hnd).p = val; } while (0)
44 #define get_xen_guest_handle(val, hnd) do { val = (hnd).p; } while (0)
48 /* Guest handles for primitive C types. */
49 __DEFINE_XEN_GUEST_HANDLE(uchar, unsigned char);
50 __DEFINE_XEN_GUEST_HANDLE(uint, unsigned int);
51 __DEFINE_XEN_GUEST_HANDLE(ulong, unsigned long);
52 DEFINE_XEN_GUEST_HANDLE(char);
53 DEFINE_XEN_GUEST_HANDLE(int);
54 DEFINE_XEN_GUEST_HANDLE(long);
55 DEFINE_XEN_GUEST_HANDLE(void);
57 typedef unsigned long xen_pfn_t;
58 DEFINE_XEN_GUEST_HANDLE(xen_pfn_t);
62 * SEGMENT DESCRIPTOR TABLES
65 * A number of GDT entries are reserved by Xen. These are not situated at the
66 * start of the GDT because some stupid OSes export hard-coded selector values
67 * in their ABI. These hard-coded values are always near the start of the GDT,
68 * so Xen places itself out of the way, at the far end of the GDT.
70 #define FIRST_RESERVED_GDT_PAGE 14
71 #define FIRST_RESERVED_GDT_BYTE (FIRST_RESERVED_GDT_PAGE * 4096)
72 #define FIRST_RESERVED_GDT_ENTRY (FIRST_RESERVED_GDT_BYTE / 8)
75 * 64-bit segment selectors
76 * These flat segments are in the Xen-private section of every GDT. Since these
77 * are also present in the initial GDT, many OSes will be able to avoid
78 * installing their own GDT.
81 #define FLAT_RING3_CS32 0xe023 /* GDT index 260 */
82 #define FLAT_RING3_CS64 0xe033 /* GDT index 261 */
83 #define FLAT_RING3_DS32 0xe02b /* GDT index 262 */
84 #define FLAT_RING3_DS64 0x0000 /* NULL selector */
85 #define FLAT_RING3_SS32 0xe02b /* GDT index 262 */
86 #define FLAT_RING3_SS64 0xe02b /* GDT index 262 */
88 #define FLAT_KERNEL_DS64 FLAT_RING3_DS64
89 #define FLAT_KERNEL_DS32 FLAT_RING3_DS32
90 #define FLAT_KERNEL_DS FLAT_KERNEL_DS64
91 #define FLAT_KERNEL_CS64 FLAT_RING3_CS64
92 #define FLAT_KERNEL_CS32 FLAT_RING3_CS32
93 #define FLAT_KERNEL_CS FLAT_KERNEL_CS64
94 #define FLAT_KERNEL_SS64 FLAT_RING3_SS64
95 #define FLAT_KERNEL_SS32 FLAT_RING3_SS32
96 #define FLAT_KERNEL_SS FLAT_KERNEL_SS64
98 #define FLAT_USER_DS64 FLAT_RING3_DS64
99 #define FLAT_USER_DS32 FLAT_RING3_DS32
100 #define FLAT_USER_DS FLAT_USER_DS64
101 #define FLAT_USER_CS64 FLAT_RING3_CS64
102 #define FLAT_USER_CS32 FLAT_RING3_CS32
103 #define FLAT_USER_CS FLAT_USER_CS64
104 #define FLAT_USER_SS64 FLAT_RING3_SS64
105 #define FLAT_USER_SS32 FLAT_RING3_SS32
106 #define FLAT_USER_SS FLAT_USER_SS64
108 #define __HYPERVISOR_VIRT_START 0xFFFF800000000000
109 #define __HYPERVISOR_VIRT_END 0xFFFF880000000000
110 #define __MACH2PHYS_VIRT_START 0xFFFF800000000000
111 #define __MACH2PHYS_VIRT_END 0xFFFF804000000000
113 #ifndef HYPERVISOR_VIRT_START
114 #define HYPERVISOR_VIRT_START mk_unsigned_long(__HYPERVISOR_VIRT_START)
115 #define HYPERVISOR_VIRT_END mk_unsigned_long(__HYPERVISOR_VIRT_END)
118 #define MACH2PHYS_VIRT_START mk_unsigned_long(__MACH2PHYS_VIRT_START)
119 #define MACH2PHYS_VIRT_END mk_unsigned_long(__MACH2PHYS_VIRT_END)
120 #define MACH2PHYS_NR_ENTRIES ((MACH2PHYS_VIRT_END-MACH2PHYS_VIRT_START)>>3)
121 #ifndef machine_to_phys_mapping
122 #define machine_to_phys_mapping ((unsigned long *)HYPERVISOR_VIRT_START)
125 /* Maximum number of virtual CPUs in multi-processor guests. */
126 #define MAX_VIRT_CPUS 32
130 typedef unsigned long xen_ulong_t;
133 * int HYPERVISOR_set_segment_base(unsigned int which, unsigned long base)
134 * @which == SEGBASE_* ; @base == 64-bit base address
135 * Returns 0 on success.
138 #define SEGBASE_GS_USER 1
139 #define SEGBASE_GS_KERNEL 2
140 #define SEGBASE_GS_USER_SEL 3 /* Set user %gs specified in base[15:0] */
143 * int HYPERVISOR_iret(void)
144 * All arguments are on the kernel stack, in the following format.
145 * Never returns if successful. Current kernel context is lost.
146 * The saved CS is mapped as follows:
147 * RING0 -> RING3 kernel mode.
148 * RING1 -> RING3 kernel mode.
149 * RING2 -> RING3 kernel mode.
150 * RING3 -> RING3 user mode.
151 * However RING0 indicates that the guest kernel should return to iteself
155 * If flags contains VGCF_IN_SYSCALL:
156 * Restore RAX, RIP, RFLAGS, RSP.
157 * Discard R11, RCX, CS, SS.
159 * Restore RAX, R11, RCX, CS:RIP, RFLAGS, SS:RSP.
160 * All other registers are saved on hypercall entry and restored to user.
162 /* Guest exited in SYSCALL context? Return to guest with SYSRET? */
163 #define VGCF_IN_SYSCALL (1<<8)
164 struct iret_context {
165 /* Top of stack (%rsp at point of hypercall). */
166 uint64_t rax, r11, rcx, flags, rip, cs, rflags, rsp, ss;
167 /* Bottom of iret stack frame. */
171 * Send an array of these to HYPERVISOR_set_trap_table().
172 * N.B. As in x86/32 mode, the privilege level specifies which modes may enter
173 * a trap via a software interrupt. Since rings 1 and 2 are unavailable, we
174 * allocate privilege levels as follows:
175 * Level == 0: Noone may enter
176 * Level == 1: Kernel may enter
177 * Level == 2: Kernel may enter
178 * Level == 3: Everyone may enter
180 #define TI_GET_DPL(_ti) ((_ti)->flags & 3)
181 #define TI_GET_IF(_ti) ((_ti)->flags & 4)
182 #define TI_SET_DPL(_ti,_dpl) ((_ti)->flags |= (_dpl))
183 #define TI_SET_IF(_ti,_if) ((_ti)->flags |= ((!!(_if))<<2))
185 uint8_t vector; /* exception vector */
186 uint8_t flags; /* 0-3: privilege level; 4: clear event enable? */
187 uint16_t cs; /* code selector */
188 unsigned long address; /* code offset */
190 typedef struct trap_info trap_info_t;
191 DEFINE_XEN_GUEST_HANDLE(trap_info_t);
194 /* Anonymous union includes both 32- and 64-bit names (e.g., eax/rax). */
195 #define __DECL_REG(name) union { uint64_t r ## name, e ## name; }
197 /* Non-gcc sources must always use the proper 64-bit name (e.g., rax). */
198 #define __DECL_REG(name) uint64_t r ## name
201 struct cpu_user_regs {
217 uint32_t error_code; /* private */
218 uint32_t entry_vector; /* private */
220 uint16_t cs, _pad0[1];
221 uint8_t saved_upcall_mask;
223 __DECL_REG(flags); /* rflags.IF == !saved_upcall_mask */
225 uint16_t ss, _pad2[3];
226 uint16_t es, _pad3[3];
227 uint16_t ds, _pad4[3];
228 uint16_t fs, _pad5[3]; /* Non-zero => takes precedence over fs_base. */
229 uint16_t gs, _pad6[3]; /* Non-zero => takes precedence over gs_base_usr. */
231 typedef struct cpu_user_regs cpu_user_regs_t;
232 DEFINE_XEN_GUEST_HANDLE(cpu_user_regs_t);
236 typedef uint64_t tsc_timestamp_t; /* RDTSC timestamp */
239 * The following is all CPU context. Note that the fpu_ctxt block is filled
240 * in by FXSAVE if the CPU has feature FXSR; otherwise FSAVE is used.
242 struct vcpu_guest_context {
243 /* FPU registers come first so they can be aligned for FXSAVE/FXRSTOR. */
244 struct { char x[512]; } fpu_ctxt; /* User-level FPU registers */
245 #define VGCF_I387_VALID (1<<0)
246 #define VGCF_HVM_GUEST (1<<1)
247 #define VGCF_IN_KERNEL (1<<2)
248 #define _VGCF_i387_valid 0
249 #define VGCF_i387_valid (1<<_VGCF_i387_valid)
250 #define _VGCF_hvm_guest 1
251 #define VGCF_hvm_guest (1<<_VGCF_hvm_guest)
252 #define _VGCF_in_kernel 2
253 #define VGCF_in_kernel (1<<_VGCF_in_kernel)
254 #define _VGCF_failsafe_disables_events 3
255 #define VGCF_failsafe_disables_events (1<<_VGCF_failsafe_disables_events)
256 #define _VGCF_syscall_disables_events 4
257 #define VGCF_syscall_disables_events (1<<_VGCF_syscall_disables_events)
258 unsigned long flags; /* VGCF_* flags */
259 struct cpu_user_regs user_regs; /* User-level CPU registers */
260 struct trap_info trap_ctxt[256]; /* Virtual IDT */
261 unsigned long ldt_base, ldt_ents; /* LDT (linear address, # ents) */
262 unsigned long gdt_frames[16], gdt_ents; /* GDT (machine frames, # ents) */
263 unsigned long kernel_ss, kernel_sp; /* Virtual TSS (only SS1/SP1) */
264 unsigned long ctrlreg[8]; /* CR0-CR7 (control registers) */
265 unsigned long debugreg[8]; /* DB0-DB7 (debug registers) */
266 unsigned long event_callback_eip;
267 unsigned long failsafe_callback_eip;
268 unsigned long syscall_callback_eip;
269 unsigned long vm_assist; /* VMASST_TYPE_* bitmap */
270 /* Segment base addresses. */
272 uint64_t gs_base_kernel;
273 uint64_t gs_base_user;
275 typedef struct vcpu_guest_context vcpu_guest_context_t;
276 DEFINE_XEN_GUEST_HANDLE(vcpu_guest_context_t);
278 #define xen_pfn_to_cr3(pfn) ((unsigned long)(pfn) << 12)
279 #define xen_cr3_to_pfn(cr3) ((unsigned long)(cr3) >> 12)
281 struct arch_shared_info {
282 unsigned long max_pfn; /* max pfn that appears in table */
283 /* Frame containing list of mfns containing list of mfns containing p2m. */
284 xen_pfn_t pfn_to_mfn_frame_list_list;
285 unsigned long nmi_reason;
288 typedef struct arch_shared_info arch_shared_info_t;
290 struct arch_vcpu_info {
292 unsigned long pad; /* sizeof(vcpu_info_t) == 64 */
294 typedef struct arch_vcpu_info arch_vcpu_info_t;
296 typedef unsigned long xen_callback_t;
298 #endif /* !__ASSEMBLY__ */
301 * Prefix forces emulation of some non-trapping instructions.
302 * Currently only CPUID.
305 #define XEN_EMULATE_PREFIX .byte 0x0f,0x0b,0x78,0x65,0x6e ;
306 #define XEN_CPUID XEN_EMULATE_PREFIX cpuid
308 #define XEN_EMULATE_PREFIX ".byte 0x0f,0x0b,0x78,0x65,0x6e ; "
309 #define XEN_CPUID XEN_EMULATE_PREFIX "cpuid"
320 * indent-tabs-mode: nil