1 /* SPDX-License-Identifier: MIT */
2 /******************************************************************************
3  * xen-x86_64.h
4  *
5  * Guest OS interface to x86 64-bit Xen.
6  *
7  * Copyright (c) 2004-2006, K A Fraser
8  */
9 
10 #ifndef __XEN_PUBLIC_ARCH_X86_XEN_X86_64_H__
11 #define __XEN_PUBLIC_ARCH_X86_XEN_X86_64_H__
12 
13 /*
14  * Hypercall interface:
15  *  Input:  %rdi, %rsi, %rdx, %r10, %r8, %r9 (arguments 1-6)
16  *  Output: %rax
17  * Access is via hypercall page (set up by guest loader or via a Xen MSR):
18  *  call hypercall_page + hypercall-number * 32
19  * Clobbered: argument registers (e.g., 2-arg hypercall clobbers %rdi,%rsi)
20  */
21 
22 /*
23  * 64-bit segment selectors
24  * These flat segments are in the Xen-private section of every GDT. Since these
25  * are also present in the initial GDT, many OSes will be able to avoid
26  * installing their own GDT.
27  */
28 
29 #define FLAT_RING3_CS32 0xe023  /* GDT index 260 */
30 #define FLAT_RING3_CS64 0xe033  /* GDT index 262 */
31 #define FLAT_RING3_DS32 0xe02b  /* GDT index 261 */
32 #define FLAT_RING3_DS64 0x0000  /* NULL selector */
33 #define FLAT_RING3_SS32 0xe02b  /* GDT index 261 */
34 #define FLAT_RING3_SS64 0xe02b  /* GDT index 261 */
35 
36 #define FLAT_KERNEL_DS64 FLAT_RING3_DS64
37 #define FLAT_KERNEL_DS32 FLAT_RING3_DS32
38 #define FLAT_KERNEL_DS   FLAT_KERNEL_DS64
39 #define FLAT_KERNEL_CS64 FLAT_RING3_CS64
40 #define FLAT_KERNEL_CS32 FLAT_RING3_CS32
41 #define FLAT_KERNEL_CS   FLAT_KERNEL_CS64
42 #define FLAT_KERNEL_SS64 FLAT_RING3_SS64
43 #define FLAT_KERNEL_SS32 FLAT_RING3_SS32
44 #define FLAT_KERNEL_SS   FLAT_KERNEL_SS64
45 
46 #define FLAT_USER_DS64 FLAT_RING3_DS64
47 #define FLAT_USER_DS32 FLAT_RING3_DS32
48 #define FLAT_USER_DS   FLAT_USER_DS64
49 #define FLAT_USER_CS64 FLAT_RING3_CS64
50 #define FLAT_USER_CS32 FLAT_RING3_CS32
51 #define FLAT_USER_CS   FLAT_USER_CS64
52 #define FLAT_USER_SS64 FLAT_RING3_SS64
53 #define FLAT_USER_SS32 FLAT_RING3_SS32
54 #define FLAT_USER_SS   FLAT_USER_SS64
55 
56 #define __HYPERVISOR_VIRT_START 0xFFFF800000000000
57 #define __HYPERVISOR_VIRT_END   0xFFFF880000000000
58 #define __MACH2PHYS_VIRT_START  0xFFFF800000000000
59 #define __MACH2PHYS_VIRT_END    0xFFFF804000000000
60 
61 #ifndef HYPERVISOR_VIRT_START
62 #define HYPERVISOR_VIRT_START xen_mk_ulong(__HYPERVISOR_VIRT_START)
63 #define HYPERVISOR_VIRT_END   xen_mk_ulong(__HYPERVISOR_VIRT_END)
64 #endif
65 
66 #define MACH2PHYS_VIRT_START  xen_mk_ulong(__MACH2PHYS_VIRT_START)
67 #define MACH2PHYS_VIRT_END    xen_mk_ulong(__MACH2PHYS_VIRT_END)
68 #define MACH2PHYS_NR_ENTRIES  ((MACH2PHYS_VIRT_END-MACH2PHYS_VIRT_START)>>3)
69 #ifndef machine_to_phys_mapping
70 #define machine_to_phys_mapping ((unsigned long *)HYPERVISOR_VIRT_START)
71 #endif
72 
73 /*
74  * int HYPERVISOR_set_segment_base(unsigned int which, unsigned long base)
75  *  @which == SEGBASE_*  ;  @base == 64-bit base address
76  * Returns 0 on success.
77  */
78 #define SEGBASE_FS          0
79 #define SEGBASE_GS_USER     1
80 #define SEGBASE_GS_KERNEL   2
81 #define SEGBASE_GS_USER_SEL 3 /* Set user %gs specified in base[15:0] */
82 
83 /*
84  * int HYPERVISOR_iret(void)
85  * All arguments are on the kernel stack, in the following format.
86  * Never returns if successful. Current kernel context is lost.
87  * The saved CS is mapped as follows:
88  *   RING0 -> RING3 kernel mode.
89  *   RING1 -> RING3 kernel mode.
90  *   RING2 -> RING3 kernel mode.
91  *   RING3 -> RING3 user mode.
92  * However RING0 indicates that the guest kernel should return to iteself
93  * directly with
94  *      orb   $3,1*8(%rsp)
95  *      iretq
96  * If flags contains VGCF_in_syscall:
97  *   Restore RAX, RIP, RFLAGS, RSP.
98  *   Discard R11, RCX, CS, SS.
99  * Otherwise:
100  *   Restore RAX, R11, RCX, CS:RIP, RFLAGS, SS:RSP.
101  * All other registers are saved on hypercall entry and restored to user.
102  */
103 /* Guest exited in SYSCALL context? Return to guest with SYSRET? */
104 #define _VGCF_in_syscall 8
105 #define VGCF_in_syscall  (1<<_VGCF_in_syscall)
106 #define VGCF_IN_SYSCALL  VGCF_in_syscall
107 
108 #ifndef __ASSEMBLY__
109 
110 struct iret_context {
111     /* Top of stack (%rsp at point of hypercall). */
112     uint64_t rax, r11, rcx, flags, rip, cs, rflags, rsp, ss;
113     /* Bottom of iret stack frame. */
114 };
115 
116 #if defined(__XEN__) || defined(__XEN_TOOLS__)
117 /* Anonymous unions include all permissible names (e.g., al/ah/ax/eax/rax). */
118 #define __DECL_REG_LOHI(which) union { \
119     uint64_t r ## which ## x; \
120     uint32_t e ## which ## x; \
121     uint16_t which ## x; \
122     struct { \
123         uint8_t which ## l; \
124         uint8_t which ## h; \
125     }; \
126 }
127 #define __DECL_REG_LO8(name) union { \
128     uint64_t r ## name; \
129     uint32_t e ## name; \
130     uint16_t name; \
131     uint8_t name ## l; \
132 }
133 #define __DECL_REG_LO16(name) union { \
134     uint64_t r ## name; \
135     uint32_t e ## name; \
136     uint16_t name; \
137 }
138 #define __DECL_REG_HI(num) union { \
139     uint64_t r ## num; \
140     uint32_t r ## num ## d; \
141     uint16_t r ## num ## w; \
142     uint8_t r ## num ## b; \
143 }
144 #elif defined(__GNUC__) && !defined(__STRICT_ANSI__)
145 /* Anonymous union includes both 32- and 64-bit names (e.g., eax/rax). */
146 #define __DECL_REG(name) union { \
147     uint64_t r ## name, e ## name; \
148     uint32_t _e ## name; \
149 }
150 #else
151 /* Non-gcc sources must always use the proper 64-bit name (e.g., rax). */
152 #define __DECL_REG(name) uint64_t r ## name
153 #endif
154 
155 #ifndef __DECL_REG_LOHI
156 #define __DECL_REG_LOHI(name) __DECL_REG(name ## x)
157 #define __DECL_REG_LO8        __DECL_REG
158 #define __DECL_REG_LO16       __DECL_REG
159 #define __DECL_REG_HI(num)    uint64_t r ## num
160 #endif
161 
162 struct cpu_user_regs {
163     __DECL_REG_HI(15);
164     __DECL_REG_HI(14);
165     __DECL_REG_HI(13);
166     __DECL_REG_HI(12);
167     __DECL_REG_LO8(bp);
168     __DECL_REG_LOHI(b);
169     __DECL_REG_HI(11);
170     __DECL_REG_HI(10);
171     __DECL_REG_HI(9);
172     __DECL_REG_HI(8);
173     __DECL_REG_LOHI(a);
174     __DECL_REG_LOHI(c);
175     __DECL_REG_LOHI(d);
176     __DECL_REG_LO8(si);
177     __DECL_REG_LO8(di);
178     uint32_t error_code;    /* private */
179     uint32_t entry_vector;  /* private */
180     __DECL_REG_LO16(ip);
181     uint16_t cs, _pad0[1];
182     uint8_t  saved_upcall_mask;
183     uint8_t  _pad1[3];
184     __DECL_REG_LO16(flags); /* rflags.IF == !saved_upcall_mask */
185     __DECL_REG_LO8(sp);
186     uint16_t ss, _pad2[3];
187     uint16_t es, _pad3[3];
188     uint16_t ds, _pad4[3];
189     uint16_t fs, _pad5[3];
190     uint16_t gs, _pad6[3];
191 };
192 typedef struct cpu_user_regs cpu_user_regs_t;
193 DEFINE_XEN_GUEST_HANDLE(cpu_user_regs_t);
194 
195 #undef __DECL_REG
196 #undef __DECL_REG_LOHI
197 #undef __DECL_REG_LO8
198 #undef __DECL_REG_LO16
199 #undef __DECL_REG_HI
200 
201 #define xen_pfn_to_cr3(pfn) ((unsigned long)(pfn) << 12)
202 #define xen_cr3_to_pfn(cr3) ((unsigned long)(cr3) >> 12)
203 
204 struct arch_vcpu_info {
205     unsigned long cr2;
206     unsigned long pad; /* sizeof(vcpu_info_t) == 64 */
207 };
208 typedef struct arch_vcpu_info arch_vcpu_info_t;
209 
210 typedef unsigned long xen_callback_t;
211 
212 #endif /* !__ASSEMBLY__ */
213 
214 #endif /* __XEN_PUBLIC_ARCH_X86_XEN_X86_64_H__ */
215 
216 /*
217  * Local variables:
218  * mode: C
219  * c-file-style: "BSD"
220  * c-basic-offset: 4
221  * tab-width: 4
222  * indent-tabs-mode: nil
223  * End:
224  */
225