iPXE
xen.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: MIT */
2 /******************************************************************************
3  * arch-x86/xen.h
4  *
5  * Guest OS interface to x86 Xen.
6  *
7  * Copyright (c) 2004-2006, K A Fraser
8  */
9 
10 #include "../xen.h"
11 
12 #ifndef __XEN_PUBLIC_ARCH_X86_XEN_H__
13 #define __XEN_PUBLIC_ARCH_X86_XEN_H__
14 
15 FILE_LICENCE ( MIT );
16 
17 /* Structural guest handles introduced in 0x00030201. */
18 #if __XEN_INTERFACE_VERSION__ >= 0x00030201
19 #define ___DEFINE_XEN_GUEST_HANDLE(name, type) \
20  typedef struct { type *p; } __guest_handle_ ## name
21 #else
22 #define ___DEFINE_XEN_GUEST_HANDLE(name, type) \
23  typedef type * __guest_handle_ ## name
24 #endif
25 
26 /*
27  * XEN_GUEST_HANDLE represents a guest pointer, when passed as a field
28  * in a struct in memory.
29  * XEN_GUEST_HANDLE_PARAM represent a guest pointer, when passed as an
30  * hypercall argument.
31  * XEN_GUEST_HANDLE_PARAM and XEN_GUEST_HANDLE are the same on X86 but
32  * they might not be on other architectures.
33  */
34 #define __DEFINE_XEN_GUEST_HANDLE(name, type) \
35  ___DEFINE_XEN_GUEST_HANDLE(name, type); \
36  ___DEFINE_XEN_GUEST_HANDLE(const_##name, const type)
37 #define DEFINE_XEN_GUEST_HANDLE(name) __DEFINE_XEN_GUEST_HANDLE(name, name)
38 #define __XEN_GUEST_HANDLE(name) __guest_handle_ ## name
39 #define XEN_GUEST_HANDLE(name) __XEN_GUEST_HANDLE(name)
40 #define XEN_GUEST_HANDLE_PARAM(name) XEN_GUEST_HANDLE(name)
41 #define set_xen_guest_handle_raw(hnd, val) do { (hnd).p = val; } while (0)
42 #define set_xen_guest_handle(hnd, val) set_xen_guest_handle_raw(hnd, val)
43 
44 #if defined(__i386__)
45 # ifdef __XEN__
46 __DeFiNe__ __DECL_REG_LO8(which) uint32_t e ## which ## x
47 __DeFiNe__ __DECL_REG_LO16(name) union { uint32_t e ## name; }
48 # endif
49 #include "xen-x86_32.h"
50 # ifdef __XEN__
51 __UnDeF__ __DECL_REG_LO8
52 __UnDeF__ __DECL_REG_LO16
53 __DeFiNe__ __DECL_REG_LO8(which) e ## which ## x
54 __DeFiNe__ __DECL_REG_LO16(name) e ## name
55 # endif
56 #elif defined(__x86_64__)
57 #include "xen-x86_64.h"
58 #endif
59 
60 #ifndef __ASSEMBLY__
61 typedef unsigned long xen_pfn_t;
62 #define PRI_xen_pfn "lx"
63 #define PRIu_xen_pfn "lu"
64 #endif
65 
66 #define XEN_HAVE_PV_GUEST_ENTRY 1
67 
68 #define XEN_HAVE_PV_UPCALL_MASK 1
69 
70 /*
71  * `incontents 200 segdesc Segment Descriptor Tables
72  */
73 /*
74  * ` enum neg_errnoval
75  * ` HYPERVISOR_set_gdt(const xen_pfn_t frames[], unsigned int entries);
76  * `
77  */
78 /*
79  * A number of GDT entries are reserved by Xen. These are not situated at the
80  * start of the GDT because some stupid OSes export hard-coded selector values
81  * in their ABI. These hard-coded values are always near the start of the GDT,
82  * so Xen places itself out of the way, at the far end of the GDT.
83  *
84  * NB The LDT is set using the MMUEXT_SET_LDT op of HYPERVISOR_mmuext_op
85  */
86 #define FIRST_RESERVED_GDT_PAGE 14
87 #define FIRST_RESERVED_GDT_BYTE (FIRST_RESERVED_GDT_PAGE * 4096)
88 #define FIRST_RESERVED_GDT_ENTRY (FIRST_RESERVED_GDT_BYTE / 8)
89 
90 
91 /*
92  * ` enum neg_errnoval
93  * ` HYPERVISOR_update_descriptor(u64 pa, u64 desc);
94  * `
95  * ` @pa The machine physical address of the descriptor to
96  * ` update. Must be either a descriptor page or writable.
97  * ` @desc The descriptor value to update, in the same format as a
98  * ` native descriptor table entry.
99  */
100 
101 /* Maximum number of virtual CPUs in legacy multi-processor guests. */
102 #define XEN_LEGACY_MAX_VCPUS 32
103 
104 #ifndef __ASSEMBLY__
105 
106 typedef unsigned long xen_ulong_t;
107 #define PRI_xen_ulong "lx"
108 
109 /*
110  * ` enum neg_errnoval
111  * ` HYPERVISOR_stack_switch(unsigned long ss, unsigned long esp);
112  * `
113  * Sets the stack segment and pointer for the current vcpu.
114  */
115 
116 /*
117  * ` enum neg_errnoval
118  * ` HYPERVISOR_set_trap_table(const struct trap_info traps[]);
119  * `
120  */
121 /*
122  * Send an array of these to HYPERVISOR_set_trap_table().
123  * Terminate the array with a sentinel entry, with traps[].address==0.
124  * The privilege level specifies which modes may enter a trap via a software
125  * interrupt. On x86/64, since rings 1 and 2 are unavailable, we allocate
126  * privilege levels as follows:
127  * Level == 0: Noone may enter
128  * Level == 1: Kernel may enter
129  * Level == 2: Kernel may enter
130  * Level == 3: Everyone may enter
131  *
132  * Note: For compatibility with kernels not setting up exception handlers
133  * early enough, Xen will avoid trying to inject #GP (and hence crash
134  * the domain) when an RDMSR would require this, but no handler was
135  * set yet. The precise conditions are implementation specific, and
136  * new code may not rely on such behavior anyway.
137  */
138 #define TI_GET_DPL(_ti) ((_ti)->flags & 3)
139 #define TI_GET_IF(_ti) ((_ti)->flags & 4)
140 #define TI_SET_DPL(_ti,_dpl) ((_ti)->flags |= (_dpl))
141 #define TI_SET_IF(_ti,_if) ((_ti)->flags |= ((!!(_if))<<2))
142 struct trap_info {
143  uint8_t vector; /* exception vector */
144  uint8_t flags; /* 0-3: privilege level; 4: clear event enable? */
145  uint16_t cs; /* code selector */
146  unsigned long address; /* code offset */
147 };
148 typedef struct trap_info trap_info_t;
150 
151 typedef uint64_t tsc_timestamp_t; /* RDTSC timestamp */
152 
153 /*
154  * The following is all CPU context. Note that the fpu_ctxt block is filled
155  * in by FXSAVE if the CPU has feature FXSR; otherwise FSAVE is used.
156  *
157  * Also note that when calling DOMCTL_setvcpucontext for HVM guests, not all
158  * information in this structure is updated, the fields read include: fpu_ctxt
159  * (if VGCT_I387_VALID is set), flags, user_regs and debugreg[*].
160  *
161  * Note: VCPUOP_initialise for HVM guests is non-symetric with
162  * DOMCTL_setvcpucontext, and uses struct vcpu_hvm_context from hvm/hvm_vcpu.h
163  */
165  /* FPU registers come first so they can be aligned for FXSAVE/FXRSTOR. */
166  struct { char x[512]; } fpu_ctxt; /* User-level FPU registers */
167 #define VGCF_I387_VALID (1<<0)
168 #define VGCF_IN_KERNEL (1<<2)
169 #define _VGCF_i387_valid 0
170 #define VGCF_i387_valid (1<<_VGCF_i387_valid)
171 #define _VGCF_in_kernel 2
172 #define VGCF_in_kernel (1<<_VGCF_in_kernel)
173 #define _VGCF_failsafe_disables_events 3
174 #define VGCF_failsafe_disables_events (1<<_VGCF_failsafe_disables_events)
175 #define _VGCF_syscall_disables_events 4
176 #define VGCF_syscall_disables_events (1<<_VGCF_syscall_disables_events)
177 #define _VGCF_online 5
178 #define VGCF_online (1<<_VGCF_online)
179  unsigned long flags; /* VGCF_* flags */
180  struct cpu_user_regs user_regs; /* User-level CPU registers */
181  struct trap_info trap_ctxt[256]; /* Virtual IDT */
182  unsigned long ldt_base, ldt_ents; /* LDT (linear address, # ents) */
183  unsigned long gdt_frames[16], gdt_ents; /* GDT (machine frames, # ents) */
184  unsigned long kernel_ss, kernel_sp; /* Virtual TSS (only SS1/SP1) */
185  /* NB. User pagetable on x86/64 is placed in ctrlreg[1]. */
186  unsigned long ctrlreg[8]; /* CR0-CR7 (control registers) */
187  unsigned long debugreg[8]; /* DB0-DB7 (debug registers) */
188 #ifdef __i386__
189  unsigned long event_callback_cs; /* CS:EIP of event callback */
190  unsigned long event_callback_eip;
191  unsigned long failsafe_callback_cs; /* CS:EIP of failsafe callback */
192  unsigned long failsafe_callback_eip;
193 #else
194  unsigned long event_callback_eip;
195  unsigned long failsafe_callback_eip;
196 #ifdef __XEN__
197  union {
198  unsigned long syscall_callback_eip;
199  struct {
200  unsigned int event_callback_cs; /* compat CS of event cb */
201  unsigned int failsafe_callback_cs; /* compat CS of failsafe cb */
202  };
203  };
204 #else
205  unsigned long syscall_callback_eip;
206 #endif
207 #endif
208  unsigned long vm_assist; /* VMASST_TYPE_* bitmap */
209 #ifdef __x86_64__
210  /* Segment base addresses. */
211  uint64_t fs_base;
212  uint64_t gs_base_kernel;
213  uint64_t gs_base_user;
214 #endif
215 };
218 
219 struct arch_shared_info {
220  /*
221  * Number of valid entries in the p2m table(s) anchored at
222  * pfn_to_mfn_frame_list_list and/or p2m_vaddr.
223  */
224  unsigned long max_pfn;
225  /*
226  * Frame containing list of mfns containing list of mfns containing p2m.
227  * A value of 0 indicates it has not yet been set up, ~0 indicates it has
228  * been set to invalid e.g. due to the p2m being too large for the 3-level
229  * p2m tree. In this case the linear mapper p2m list anchored at p2m_vaddr
230  * is to be used.
231  */
233  unsigned long nmi_reason;
234  /*
235  * Following three fields are valid if p2m_cr3 contains a value different
236  * from 0.
237  * p2m_cr3 is the root of the address space where p2m_vaddr is valid.
238  * p2m_cr3 is in the same format as a cr3 value in the vcpu register state
239  * and holds the folded machine frame number (via xen_pfn_to_cr3) of a
240  * L3 or L4 page table.
241  * p2m_vaddr holds the virtual address of the linear p2m list. All entries
242  * in the range [0...max_pfn[ are accessible via this pointer.
243  * p2m_generation will be incremented by the guest before and after each
244  * change of the mappings of the p2m list. p2m_generation starts at 0 and
245  * a value with the least significant bit set indicates that a mapping
246  * update is in progress. This allows guest external software (e.g. in Dom0)
247  * to verify that read mappings are consistent and whether they have changed
248  * since the last check.
249  * Modifying a p2m element in the linear p2m list is allowed via an atomic
250  * write only.
251  */
252  unsigned long p2m_cr3; /* cr3 value of the p2m address space */
253  unsigned long p2m_vaddr; /* virtual address of the p2m list */
254  unsigned long p2m_generation; /* generation count of p2m mapping */
255 #ifdef __i386__
256  /* There's no room for this field in the generic structure. */
257  uint32_t wc_sec_hi;
258 #endif
259 };
261 
262 #if defined(__XEN__) || defined(__XEN_TOOLS__)
263 /*
264  * struct xen_arch_domainconfig's ABI is covered by
265  * XEN_DOMCTL_INTERFACE_VERSION.
266  */
267 struct xen_arch_domainconfig {
268 #define _XEN_X86_EMU_LAPIC 0
269 #define XEN_X86_EMU_LAPIC (1U<<_XEN_X86_EMU_LAPIC)
270 #define _XEN_X86_EMU_HPET 1
271 #define XEN_X86_EMU_HPET (1U<<_XEN_X86_EMU_HPET)
272 #define _XEN_X86_EMU_PM 2
273 #define XEN_X86_EMU_PM (1U<<_XEN_X86_EMU_PM)
274 #define _XEN_X86_EMU_RTC 3
275 #define XEN_X86_EMU_RTC (1U<<_XEN_X86_EMU_RTC)
276 #define _XEN_X86_EMU_IOAPIC 4
277 #define XEN_X86_EMU_IOAPIC (1U<<_XEN_X86_EMU_IOAPIC)
278 #define _XEN_X86_EMU_PIC 5
279 #define XEN_X86_EMU_PIC (1U<<_XEN_X86_EMU_PIC)
280 #define _XEN_X86_EMU_VGA 6
281 #define XEN_X86_EMU_VGA (1U<<_XEN_X86_EMU_VGA)
282 #define _XEN_X86_EMU_IOMMU 7
283 #define XEN_X86_EMU_IOMMU (1U<<_XEN_X86_EMU_IOMMU)
284 #define _XEN_X86_EMU_PIT 8
285 #define XEN_X86_EMU_PIT (1U<<_XEN_X86_EMU_PIT)
286 #define _XEN_X86_EMU_USE_PIRQ 9
287 #define XEN_X86_EMU_USE_PIRQ (1U<<_XEN_X86_EMU_USE_PIRQ)
288 #define _XEN_X86_EMU_VPCI 10
289 #define XEN_X86_EMU_VPCI (1U<<_XEN_X86_EMU_VPCI)
290 
291 #define XEN_X86_EMU_ALL (XEN_X86_EMU_LAPIC | XEN_X86_EMU_HPET | \
292  XEN_X86_EMU_PM | XEN_X86_EMU_RTC | \
293  XEN_X86_EMU_IOAPIC | XEN_X86_EMU_PIC | \
294  XEN_X86_EMU_VGA | XEN_X86_EMU_IOMMU | \
295  XEN_X86_EMU_PIT | XEN_X86_EMU_USE_PIRQ |\
296  XEN_X86_EMU_VPCI)
297  uint32_t emulation_flags;
298 
299 /*
300  * Select whether to use a relaxed behavior for accesses to MSRs not explicitly
301  * handled by Xen instead of injecting a #GP to the guest. Note this option
302  * doesn't allow the guest to read or write to the underlying MSR.
303  */
304 #define XEN_X86_MSR_RELAXED (1u << 0)
305  uint32_t misc_flags;
306 };
307 
308 /* Max XEN_X86_* constant. Used for ABI checking. */
309 #define XEN_X86_MISC_FLAGS_MAX XEN_X86_MSR_RELAXED
310 
311 #endif
312 
313 /*
314  * Representations of architectural CPUID and MSR information. Used as the
315  * serialised version of Xen's internal representation.
316  */
317 typedef struct xen_cpuid_leaf {
318 #define XEN_CPUID_NO_SUBLEAF 0xffffffffu
320  uint32_t a, b, c, d;
323 
324 typedef struct xen_msr_entry {
326  uint32_t flags; /* Reserved MBZ. */
330 
331 #endif /* !__ASSEMBLY__ */
332 
333 /*
334  * ` enum neg_errnoval
335  * ` HYPERVISOR_fpu_taskswitch(int set);
336  * `
337  * Sets (if set!=0) or clears (if set==0) CR0.TS.
338  */
339 
340 /*
341  * ` enum neg_errnoval
342  * ` HYPERVISOR_set_debugreg(int regno, unsigned long value);
343  *
344  * ` unsigned long
345  * ` HYPERVISOR_get_debugreg(int regno);
346  * For 0<=reg<=7, returns the debug register value.
347  * For other values of reg, returns ((unsigned long)-EINVAL).
348  * (Unfortunately, this interface is defective.)
349  */
350 
351 /*
352  * Prefix forces emulation of some non-trapping instructions.
353  * Currently only CPUID.
354  */
355 #ifdef __ASSEMBLY__
356 #define XEN_EMULATE_PREFIX .byte 0x0f,0x0b,0x78,0x65,0x6e ;
357 #define XEN_CPUID XEN_EMULATE_PREFIX cpuid
358 #else
359 #define XEN_EMULATE_PREFIX ".byte 0x0f,0x0b,0x78,0x65,0x6e ; "
360 #define XEN_CPUID XEN_EMULATE_PREFIX "cpuid"
361 #endif
362 
363 /*
364  * Debug console IO port, also called "port E9 hack". Each character written
365  * to this IO port will be printed on the hypervisor console, subject to log
366  * level restrictions.
367  */
368 #define XEN_HVM_DEBUGCONS_IOPORT 0xe9
369 
370 #endif /* __XEN_PUBLIC_ARCH_X86_XEN_H__ */
371 
372 /*
373  * Local variables:
374  * mode: C
375  * c-file-style: "BSD"
376  * c-basic-offset: 4
377  * tab-width: 4
378  * indent-tabs-mode: nil
379  * End:
380  */
struct xen_cpuid_leaf xen_cpuid_leaf_t
unsigned long p2m_generation
Definition: xen.h:254
unsigned long address
Definition: xen.h:146
const char * name
Definition: ath9k_hw.c:1984
unsigned short uint16_t
Definition: stdint.h:11
#define __DECL_REG_LO16(name)
Definition: xen-x86_32.h:116
uint32_t b
Definition: xen.h:320
Definition: xen.h:142
unsigned long long uint64_t
Definition: stdint.h:13
unsigned long failsafe_callback_eip
Definition: xen.h:195
unsigned long ldt_ents
Definition: xen.h:182
#define __DECL_REG_LO8(which)
Definition: xen-x86_32.h:115
uint32_t subleaf
Definition: xen.h:319
uint32_t idx
Definition: xen.h:325
unsigned long ldt_base
Definition: xen.h:182
uint64_t tsc_timestamp_t
Definition: xen.h:151
unsigned long xen_ulong_t
Definition: xen.h:106
unsigned long event_callback_eip
Definition: xen.h:194
uint32_t e
Definition: sha1.c:32
struct xen_msr_entry xen_msr_entry_t
unsigned long syscall_callback_eip
Definition: xen.h:205
unsigned long flags
Definition: xen.h:179
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
uint32_t flags
Definition: xen.h:326
unsigned long max_pfn
Definition: xen.h:224
unsigned long kernel_sp
Definition: xen.h:184
uint8_t vector
Definition: xen.h:143
unsigned long nmi_reason
Definition: xen.h:233
unsigned long p2m_vaddr
Definition: xen.h:253
unsigned long xen_pfn_t
Definition: nonxen.h:25
char x[512]
Definition: xen.h:166
unsigned char uint8_t
Definition: stdint.h:10
struct cpu_user_regs user_regs
Definition: xen.h:180
uint32_t leaf
Definition: xen.h:319
uint32_t c
Definition: xen.h:320
unsigned int uint32_t
Definition: stdint.h:12
unsigned long kernel_ss
Definition: xen.h:184
unsigned long p2m_cr3
Definition: xen.h:252
xen_pfn_t pfn_to_mfn_frame_list_list
Definition: xen.h:232
uint64_t val
Definition: xen.h:327
uint16_t cs
Definition: xen.h:145
#define DEFINE_XEN_GUEST_HANDLE(name)
Definition: xen.h:37
uint32_t a
Definition: xen.h:320
unsigned long debugreg[8]
Definition: xen.h:187
struct trap_info trap_ctxt[256]
Definition: xen.h:181
uint8_t flags
Definition: xen.h:144
uint32_t d
Definition: xen.h:320
unsigned long gdt_ents
Definition: xen.h:183
unsigned long gdt_frames[16]
Definition: xen.h:183
unsigned long ctrlreg[8]
Definition: xen.h:186
unsigned long xen_pfn_t
Definition: xen.h:61
Definition: xen.h:324
unsigned long vm_assist
Definition: xen.h:208
struct vcpu_guest_context::@602 fpu_ctxt