1181624Skmacy/****************************************************************************** 2181624Skmacy * xen-x86_64.h 3181624Skmacy * 4181624Skmacy * Guest OS interface to x86 64-bit Xen. 5181624Skmacy * 6181624Skmacy * Permission is hereby granted, free of charge, to any person obtaining a copy 7181624Skmacy * of this software and associated documentation files (the "Software"), to 8181624Skmacy * deal in the Software without restriction, including without limitation the 9181624Skmacy * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 10181624Skmacy * sell copies of the Software, and to permit persons to whom the Software is 11181624Skmacy * furnished to do so, subject to the following conditions: 12181624Skmacy * 13181624Skmacy * The above copyright notice and this permission notice shall be included in 14181624Skmacy * all copies or substantial portions of the Software. 15181624Skmacy * 16181624Skmacy * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17181624Skmacy * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18181624Skmacy * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19181624Skmacy * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20181624Skmacy * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21181624Skmacy * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22181624Skmacy * DEALINGS IN THE SOFTWARE. 23181624Skmacy * 24181624Skmacy * Copyright (c) 2004-2006, K A Fraser 25181624Skmacy */ 26181624Skmacy 27181624Skmacy#ifndef __XEN_PUBLIC_ARCH_X86_XEN_X86_64_H__ 28181624Skmacy#define __XEN_PUBLIC_ARCH_X86_XEN_X86_64_H__ 29181624Skmacy 30181624Skmacy/* 31181624Skmacy * Hypercall interface: 32181624Skmacy * Input: %rdi, %rsi, %rdx, %r10, %r8 (arguments 1-5) 33181624Skmacy * Output: %rax 34181624Skmacy * Access is via hypercall page (set up by guest loader or via a Xen MSR): 35181624Skmacy * call hypercall_page + hypercall-number * 32 36181624Skmacy * Clobbered: argument registers (e.g., 2-arg hypercall clobbers %rdi,%rsi) 37181624Skmacy */ 38181624Skmacy 39181624Skmacy#if __XEN_INTERFACE_VERSION__ < 0x00030203 40181624Skmacy/* 41181624Skmacy * Legacy hypercall interface: 42181624Skmacy * As above, except the entry sequence to the hypervisor is: 43181624Skmacy * mov $hypercall-number*32,%eax ; syscall 44181624Skmacy * Clobbered: %rcx, %r11, argument registers (as above) 45181624Skmacy */ 46181624Skmacy#define TRAP_INSTR "syscall" 47181624Skmacy#endif 48181624Skmacy 49181624Skmacy/* 50181624Skmacy * 64-bit segment selectors 51181624Skmacy * These flat segments are in the Xen-private section of every GDT. Since these 52181624Skmacy * are also present in the initial GDT, many OSes will be able to avoid 53181624Skmacy * installing their own GDT. 54181624Skmacy */ 55181624Skmacy 56181624Skmacy#define FLAT_RING3_CS32 0xe023 /* GDT index 260 */ 57181624Skmacy#define FLAT_RING3_CS64 0xe033 /* GDT index 261 */ 58181624Skmacy#define FLAT_RING3_DS32 0xe02b /* GDT index 262 */ 59181624Skmacy#define FLAT_RING3_DS64 0x0000 /* NULL selector */ 60181624Skmacy#define FLAT_RING3_SS32 0xe02b /* GDT index 262 */ 61181624Skmacy#define FLAT_RING3_SS64 0xe02b /* GDT index 262 */ 62181624Skmacy 63181624Skmacy#define FLAT_KERNEL_DS64 FLAT_RING3_DS64 64181624Skmacy#define FLAT_KERNEL_DS32 FLAT_RING3_DS32 65181624Skmacy#define FLAT_KERNEL_DS FLAT_KERNEL_DS64 66181624Skmacy#define FLAT_KERNEL_CS64 FLAT_RING3_CS64 67181624Skmacy#define FLAT_KERNEL_CS32 FLAT_RING3_CS32 68181624Skmacy#define FLAT_KERNEL_CS FLAT_KERNEL_CS64 69181624Skmacy#define FLAT_KERNEL_SS64 FLAT_RING3_SS64 70181624Skmacy#define FLAT_KERNEL_SS32 FLAT_RING3_SS32 71181624Skmacy#define FLAT_KERNEL_SS FLAT_KERNEL_SS64 72181624Skmacy 73181624Skmacy#define FLAT_USER_DS64 FLAT_RING3_DS64 74181624Skmacy#define FLAT_USER_DS32 FLAT_RING3_DS32 75181624Skmacy#define FLAT_USER_DS FLAT_USER_DS64 76181624Skmacy#define FLAT_USER_CS64 FLAT_RING3_CS64 77181624Skmacy#define FLAT_USER_CS32 FLAT_RING3_CS32 78181624Skmacy#define FLAT_USER_CS FLAT_USER_CS64 79181624Skmacy#define FLAT_USER_SS64 FLAT_RING3_SS64 80181624Skmacy#define FLAT_USER_SS32 FLAT_RING3_SS32 81181624Skmacy#define FLAT_USER_SS FLAT_USER_SS64 82181624Skmacy 83181624Skmacy#define __HYPERVISOR_VIRT_START 0xFFFF800000000000 84181624Skmacy#define __HYPERVISOR_VIRT_END 0xFFFF880000000000 85181624Skmacy#define __MACH2PHYS_VIRT_START 0xFFFF800000000000 86181624Skmacy#define __MACH2PHYS_VIRT_END 0xFFFF804000000000 87181624Skmacy 88181624Skmacy#ifndef HYPERVISOR_VIRT_START 89181624Skmacy#define HYPERVISOR_VIRT_START mk_unsigned_long(__HYPERVISOR_VIRT_START) 90181624Skmacy#define HYPERVISOR_VIRT_END mk_unsigned_long(__HYPERVISOR_VIRT_END) 91181624Skmacy#endif 92181624Skmacy 93181624Skmacy#define MACH2PHYS_VIRT_START mk_unsigned_long(__MACH2PHYS_VIRT_START) 94181624Skmacy#define MACH2PHYS_VIRT_END mk_unsigned_long(__MACH2PHYS_VIRT_END) 95181624Skmacy#define MACH2PHYS_NR_ENTRIES ((MACH2PHYS_VIRT_END-MACH2PHYS_VIRT_START)>>3) 96181624Skmacy#ifndef machine_to_phys_mapping 97181624Skmacy#define machine_to_phys_mapping ((unsigned long *)HYPERVISOR_VIRT_START) 98181624Skmacy#endif 99181624Skmacy 100181624Skmacy/* 101181624Skmacy * int HYPERVISOR_set_segment_base(unsigned int which, unsigned long base) 102181624Skmacy * @which == SEGBASE_* ; @base == 64-bit base address 103181624Skmacy * Returns 0 on success. 104181624Skmacy */ 105181624Skmacy#define SEGBASE_FS 0 106181624Skmacy#define SEGBASE_GS_USER 1 107181624Skmacy#define SEGBASE_GS_KERNEL 2 108181624Skmacy#define SEGBASE_GS_USER_SEL 3 /* Set user %gs specified in base[15:0] */ 109181624Skmacy 110181624Skmacy/* 111181624Skmacy * int HYPERVISOR_iret(void) 112181624Skmacy * All arguments are on the kernel stack, in the following format. 113181624Skmacy * Never returns if successful. Current kernel context is lost. 114181624Skmacy * The saved CS is mapped as follows: 115181624Skmacy * RING0 -> RING3 kernel mode. 116181624Skmacy * RING1 -> RING3 kernel mode. 117181624Skmacy * RING2 -> RING3 kernel mode. 118181624Skmacy * RING3 -> RING3 user mode. 119181624Skmacy * However RING0 indicates that the guest kernel should return to iteself 120181624Skmacy * directly with 121181624Skmacy * orb $3,1*8(%rsp) 122181624Skmacy * iretq 123181624Skmacy * If flags contains VGCF_in_syscall: 124181624Skmacy * Restore RAX, RIP, RFLAGS, RSP. 125181624Skmacy * Discard R11, RCX, CS, SS. 126181624Skmacy * Otherwise: 127181624Skmacy * Restore RAX, R11, RCX, CS:RIP, RFLAGS, SS:RSP. 128181624Skmacy * All other registers are saved on hypercall entry and restored to user. 129181624Skmacy */ 130181624Skmacy/* Guest exited in SYSCALL context? Return to guest with SYSRET? */ 131181624Skmacy#define _VGCF_in_syscall 8 132181624Skmacy#define VGCF_in_syscall (1<<_VGCF_in_syscall) 133181624Skmacy#define VGCF_IN_SYSCALL VGCF_in_syscall 134183340Skmacy 135183340Skmacy#ifndef __ASSEMBLY__ 136183340Skmacy 137181624Skmacystruct iret_context { 138181624Skmacy /* Top of stack (%rsp at point of hypercall). */ 139181624Skmacy uint64_t rax, r11, rcx, flags, rip, cs, rflags, rsp, ss; 140181624Skmacy /* Bottom of iret stack frame. */ 141181624Skmacy}; 142181624Skmacy 143183340Skmacy#if defined(__GNUC__) && !defined(__STRICT_ANSI__) 144181624Skmacy/* Anonymous union includes both 32- and 64-bit names (e.g., eax/rax). */ 145181624Skmacy#define __DECL_REG(name) union { \ 146181624Skmacy uint64_t r ## name, e ## name; \ 147181624Skmacy uint32_t _e ## name; \ 148181624Skmacy} 149181624Skmacy#else 150181624Skmacy/* Non-gcc sources must always use the proper 64-bit name (e.g., rax). */ 151181624Skmacy#define __DECL_REG(name) uint64_t r ## name 152181624Skmacy#endif 153181624Skmacy 154181624Skmacystruct cpu_user_regs { 155181624Skmacy uint64_t r15; 156181624Skmacy uint64_t r14; 157181624Skmacy uint64_t r13; 158181624Skmacy uint64_t r12; 159181624Skmacy __DECL_REG(bp); 160181624Skmacy __DECL_REG(bx); 161181624Skmacy uint64_t r11; 162181624Skmacy uint64_t r10; 163181624Skmacy uint64_t r9; 164181624Skmacy uint64_t r8; 165181624Skmacy __DECL_REG(ax); 166181624Skmacy __DECL_REG(cx); 167181624Skmacy __DECL_REG(dx); 168181624Skmacy __DECL_REG(si); 169181624Skmacy __DECL_REG(di); 170181624Skmacy uint32_t error_code; /* private */ 171181624Skmacy uint32_t entry_vector; /* private */ 172181624Skmacy __DECL_REG(ip); 173181624Skmacy uint16_t cs, _pad0[1]; 174181624Skmacy uint8_t saved_upcall_mask; 175181624Skmacy uint8_t _pad1[3]; 176181624Skmacy __DECL_REG(flags); /* rflags.IF == !saved_upcall_mask */ 177181624Skmacy __DECL_REG(sp); 178181624Skmacy uint16_t ss, _pad2[3]; 179181624Skmacy uint16_t es, _pad3[3]; 180181624Skmacy uint16_t ds, _pad4[3]; 181181624Skmacy uint16_t fs, _pad5[3]; /* Non-zero => takes precedence over fs_base. */ 182181624Skmacy uint16_t gs, _pad6[3]; /* Non-zero => takes precedence over gs_base_usr. */ 183181624Skmacy}; 184181624Skmacytypedef struct cpu_user_regs cpu_user_regs_t; 185181624SkmacyDEFINE_XEN_GUEST_HANDLE(cpu_user_regs_t); 186181624Skmacy 187181624Skmacy#undef __DECL_REG 188181624Skmacy 189181624Skmacy#define xen_pfn_to_cr3(pfn) ((unsigned long)(pfn) << 12) 190181624Skmacy#define xen_cr3_to_pfn(cr3) ((unsigned long)(cr3) >> 12) 191181624Skmacy 192181624Skmacystruct arch_vcpu_info { 193181624Skmacy unsigned long cr2; 194181624Skmacy unsigned long pad; /* sizeof(vcpu_info_t) == 64 */ 195181624Skmacy}; 196181624Skmacytypedef struct arch_vcpu_info arch_vcpu_info_t; 197181624Skmacy 198181624Skmacytypedef unsigned long xen_callback_t; 199181624Skmacy 200181624Skmacy#endif /* !__ASSEMBLY__ */ 201181624Skmacy 202181624Skmacy#endif /* __XEN_PUBLIC_ARCH_X86_XEN_X86_64_H__ */ 203181624Skmacy 204181624Skmacy/* 205181624Skmacy * Local variables: 206181624Skmacy * mode: C 207181624Skmacy * c-set-style: "BSD" 208181624Skmacy * c-basic-offset: 4 209181624Skmacy * tab-width: 4 210181624Skmacy * indent-tabs-mode: nil 211181624Skmacy * End: 212181624Skmacy */ 213