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: 32251767Sgibbs * Input: %rdi, %rsi, %rdx, %r10, %r8, %r9 (arguments 1-6) 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/* 40181624Skmacy * 64-bit segment selectors 41181624Skmacy * These flat segments are in the Xen-private section of every GDT. Since these 42181624Skmacy * are also present in the initial GDT, many OSes will be able to avoid 43181624Skmacy * installing their own GDT. 44181624Skmacy */ 45181624Skmacy 46181624Skmacy#define FLAT_RING3_CS32 0xe023 /* GDT index 260 */ 47181624Skmacy#define FLAT_RING3_CS64 0xe033 /* GDT index 261 */ 48181624Skmacy#define FLAT_RING3_DS32 0xe02b /* GDT index 262 */ 49181624Skmacy#define FLAT_RING3_DS64 0x0000 /* NULL selector */ 50181624Skmacy#define FLAT_RING3_SS32 0xe02b /* GDT index 262 */ 51181624Skmacy#define FLAT_RING3_SS64 0xe02b /* GDT index 262 */ 52181624Skmacy 53181624Skmacy#define FLAT_KERNEL_DS64 FLAT_RING3_DS64 54181624Skmacy#define FLAT_KERNEL_DS32 FLAT_RING3_DS32 55181624Skmacy#define FLAT_KERNEL_DS FLAT_KERNEL_DS64 56181624Skmacy#define FLAT_KERNEL_CS64 FLAT_RING3_CS64 57181624Skmacy#define FLAT_KERNEL_CS32 FLAT_RING3_CS32 58181624Skmacy#define FLAT_KERNEL_CS FLAT_KERNEL_CS64 59181624Skmacy#define FLAT_KERNEL_SS64 FLAT_RING3_SS64 60181624Skmacy#define FLAT_KERNEL_SS32 FLAT_RING3_SS32 61181624Skmacy#define FLAT_KERNEL_SS FLAT_KERNEL_SS64 62181624Skmacy 63181624Skmacy#define FLAT_USER_DS64 FLAT_RING3_DS64 64181624Skmacy#define FLAT_USER_DS32 FLAT_RING3_DS32 65181624Skmacy#define FLAT_USER_DS FLAT_USER_DS64 66181624Skmacy#define FLAT_USER_CS64 FLAT_RING3_CS64 67181624Skmacy#define FLAT_USER_CS32 FLAT_RING3_CS32 68181624Skmacy#define FLAT_USER_CS FLAT_USER_CS64 69181624Skmacy#define FLAT_USER_SS64 FLAT_RING3_SS64 70181624Skmacy#define FLAT_USER_SS32 FLAT_RING3_SS32 71181624Skmacy#define FLAT_USER_SS FLAT_USER_SS64 72181624Skmacy 73181624Skmacy#define __HYPERVISOR_VIRT_START 0xFFFF800000000000 74181624Skmacy#define __HYPERVISOR_VIRT_END 0xFFFF880000000000 75181624Skmacy#define __MACH2PHYS_VIRT_START 0xFFFF800000000000 76181624Skmacy#define __MACH2PHYS_VIRT_END 0xFFFF804000000000 77181624Skmacy 78181624Skmacy#ifndef HYPERVISOR_VIRT_START 79181624Skmacy#define HYPERVISOR_VIRT_START mk_unsigned_long(__HYPERVISOR_VIRT_START) 80181624Skmacy#define HYPERVISOR_VIRT_END mk_unsigned_long(__HYPERVISOR_VIRT_END) 81181624Skmacy#endif 82181624Skmacy 83181624Skmacy#define MACH2PHYS_VIRT_START mk_unsigned_long(__MACH2PHYS_VIRT_START) 84181624Skmacy#define MACH2PHYS_VIRT_END mk_unsigned_long(__MACH2PHYS_VIRT_END) 85181624Skmacy#define MACH2PHYS_NR_ENTRIES ((MACH2PHYS_VIRT_END-MACH2PHYS_VIRT_START)>>3) 86181624Skmacy#ifndef machine_to_phys_mapping 87181624Skmacy#define machine_to_phys_mapping ((unsigned long *)HYPERVISOR_VIRT_START) 88181624Skmacy#endif 89181624Skmacy 90181624Skmacy/* 91181624Skmacy * int HYPERVISOR_set_segment_base(unsigned int which, unsigned long base) 92181624Skmacy * @which == SEGBASE_* ; @base == 64-bit base address 93181624Skmacy * Returns 0 on success. 94181624Skmacy */ 95181624Skmacy#define SEGBASE_FS 0 96181624Skmacy#define SEGBASE_GS_USER 1 97181624Skmacy#define SEGBASE_GS_KERNEL 2 98181624Skmacy#define SEGBASE_GS_USER_SEL 3 /* Set user %gs specified in base[15:0] */ 99181624Skmacy 100181624Skmacy/* 101181624Skmacy * int HYPERVISOR_iret(void) 102181624Skmacy * All arguments are on the kernel stack, in the following format. 103181624Skmacy * Never returns if successful. Current kernel context is lost. 104181624Skmacy * The saved CS is mapped as follows: 105181624Skmacy * RING0 -> RING3 kernel mode. 106181624Skmacy * RING1 -> RING3 kernel mode. 107181624Skmacy * RING2 -> RING3 kernel mode. 108181624Skmacy * RING3 -> RING3 user mode. 109181624Skmacy * However RING0 indicates that the guest kernel should return to iteself 110181624Skmacy * directly with 111181624Skmacy * orb $3,1*8(%rsp) 112181624Skmacy * iretq 113181624Skmacy * If flags contains VGCF_in_syscall: 114181624Skmacy * Restore RAX, RIP, RFLAGS, RSP. 115181624Skmacy * Discard R11, RCX, CS, SS. 116181624Skmacy * Otherwise: 117181624Skmacy * Restore RAX, R11, RCX, CS:RIP, RFLAGS, SS:RSP. 118181624Skmacy * All other registers are saved on hypercall entry and restored to user. 119181624Skmacy */ 120181624Skmacy/* Guest exited in SYSCALL context? Return to guest with SYSRET? */ 121181624Skmacy#define _VGCF_in_syscall 8 122181624Skmacy#define VGCF_in_syscall (1<<_VGCF_in_syscall) 123181624Skmacy#define VGCF_IN_SYSCALL VGCF_in_syscall 124183340Skmacy 125183340Skmacy#ifndef __ASSEMBLY__ 126183340Skmacy 127181624Skmacystruct iret_context { 128181624Skmacy /* Top of stack (%rsp at point of hypercall). */ 129181624Skmacy uint64_t rax, r11, rcx, flags, rip, cs, rflags, rsp, ss; 130181624Skmacy /* Bottom of iret stack frame. */ 131181624Skmacy}; 132181624Skmacy 133183340Skmacy#if defined(__GNUC__) && !defined(__STRICT_ANSI__) 134181624Skmacy/* Anonymous union includes both 32- and 64-bit names (e.g., eax/rax). */ 135181624Skmacy#define __DECL_REG(name) union { \ 136181624Skmacy uint64_t r ## name, e ## name; \ 137181624Skmacy uint32_t _e ## name; \ 138181624Skmacy} 139181624Skmacy#else 140181624Skmacy/* Non-gcc sources must always use the proper 64-bit name (e.g., rax). */ 141181624Skmacy#define __DECL_REG(name) uint64_t r ## name 142181624Skmacy#endif 143181624Skmacy 144181624Skmacystruct cpu_user_regs { 145181624Skmacy uint64_t r15; 146181624Skmacy uint64_t r14; 147181624Skmacy uint64_t r13; 148181624Skmacy uint64_t r12; 149181624Skmacy __DECL_REG(bp); 150181624Skmacy __DECL_REG(bx); 151181624Skmacy uint64_t r11; 152181624Skmacy uint64_t r10; 153181624Skmacy uint64_t r9; 154181624Skmacy uint64_t r8; 155181624Skmacy __DECL_REG(ax); 156181624Skmacy __DECL_REG(cx); 157181624Skmacy __DECL_REG(dx); 158181624Skmacy __DECL_REG(si); 159181624Skmacy __DECL_REG(di); 160181624Skmacy uint32_t error_code; /* private */ 161181624Skmacy uint32_t entry_vector; /* private */ 162181624Skmacy __DECL_REG(ip); 163181624Skmacy uint16_t cs, _pad0[1]; 164181624Skmacy uint8_t saved_upcall_mask; 165181624Skmacy uint8_t _pad1[3]; 166181624Skmacy __DECL_REG(flags); /* rflags.IF == !saved_upcall_mask */ 167181624Skmacy __DECL_REG(sp); 168181624Skmacy uint16_t ss, _pad2[3]; 169181624Skmacy uint16_t es, _pad3[3]; 170181624Skmacy uint16_t ds, _pad4[3]; 171181624Skmacy uint16_t fs, _pad5[3]; /* Non-zero => takes precedence over fs_base. */ 172181624Skmacy uint16_t gs, _pad6[3]; /* Non-zero => takes precedence over gs_base_usr. */ 173181624Skmacy}; 174181624Skmacytypedef struct cpu_user_regs cpu_user_regs_t; 175181624SkmacyDEFINE_XEN_GUEST_HANDLE(cpu_user_regs_t); 176181624Skmacy 177181624Skmacy#undef __DECL_REG 178181624Skmacy 179181624Skmacy#define xen_pfn_to_cr3(pfn) ((unsigned long)(pfn) << 12) 180181624Skmacy#define xen_cr3_to_pfn(cr3) ((unsigned long)(cr3) >> 12) 181181624Skmacy 182181624Skmacystruct arch_vcpu_info { 183181624Skmacy unsigned long cr2; 184181624Skmacy unsigned long pad; /* sizeof(vcpu_info_t) == 64 */ 185181624Skmacy}; 186181624Skmacytypedef struct arch_vcpu_info arch_vcpu_info_t; 187181624Skmacy 188181624Skmacytypedef unsigned long xen_callback_t; 189181624Skmacy 190181624Skmacy#endif /* !__ASSEMBLY__ */ 191181624Skmacy 192181624Skmacy#endif /* __XEN_PUBLIC_ARCH_X86_XEN_X86_64_H__ */ 193181624Skmacy 194181624Skmacy/* 195181624Skmacy * Local variables: 196181624Skmacy * mode: C 197181624Skmacy * c-set-style: "BSD" 198181624Skmacy * c-basic-offset: 4 199181624Skmacy * tab-width: 4 200181624Skmacy * indent-tabs-mode: nil 201181624Skmacy * End: 202181624Skmacy */ 203