1/******************************************************************************
2 * arch-x86/xen.h
3 *
4 * Guest OS interface to x86 Xen.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Copyright (c) 2004-2006, K A Fraser
25 */
26
27#include "../xen.h"
28
29#ifndef __XEN_PUBLIC_ARCH_X86_XEN_H__
30#define __XEN_PUBLIC_ARCH_X86_XEN_H__
31
32/* Structural guest handles introduced in 0x00030201. */
33#if __XEN_INTERFACE_VERSION__ >= 0x00030201
34#define ___DEFINE_XEN_GUEST_HANDLE(name, type) \
35    typedef struct { type *p; } __guest_handle_ ## name
36#else
37#define ___DEFINE_XEN_GUEST_HANDLE(name, type) \
38    typedef type * __guest_handle_ ## name
39#endif
40
41/*
42 * XEN_GUEST_HANDLE represents a guest pointer, when passed as a field
43 * in a struct in memory.
44 * XEN_GUEST_HANDLE_PARAM represent a guest pointer, when passed as an
45 * hypercall argument.
46 * XEN_GUEST_HANDLE_PARAM and XEN_GUEST_HANDLE are the same on X86 but
47 * they might not be on other architectures.
48 */
49#define __DEFINE_XEN_GUEST_HANDLE(name, type) \
50    ___DEFINE_XEN_GUEST_HANDLE(name, type);   \
51    ___DEFINE_XEN_GUEST_HANDLE(const_##name, const type)
52#define DEFINE_XEN_GUEST_HANDLE(name)   __DEFINE_XEN_GUEST_HANDLE(name, name)
53#define __XEN_GUEST_HANDLE(name)        __guest_handle_ ## name
54#define XEN_GUEST_HANDLE(name)          __XEN_GUEST_HANDLE(name)
55#define XEN_GUEST_HANDLE_PARAM(name)    XEN_GUEST_HANDLE(name)
56#define set_xen_guest_handle_raw(hnd, val)  do { (hnd).p = val; } while (0)
57#ifdef __XEN_TOOLS__
58#define get_xen_guest_handle(val, hnd)  do { val = (hnd).p; } while (0)
59#endif
60#define set_xen_guest_handle(hnd, val) set_xen_guest_handle_raw(hnd, val)
61
62#if defined(__i386__)
63#include "xen-x86_32.h"
64#elif defined(__x86_64__)
65#include "xen-x86_64.h"
66#endif
67
68#ifndef __ASSEMBLY__
69typedef unsigned long xen_pfn_t;
70#define PRI_xen_pfn "lx"
71#endif
72
73#define XEN_HAVE_PV_GUEST_ENTRY 1
74
75#define XEN_HAVE_PV_UPCALL_MASK 1
76
77/*
78 * `incontents 200 segdesc Segment Descriptor Tables
79 */
80/*
81 * ` enum neg_errnoval
82 * ` HYPERVISOR_set_gdt(const xen_pfn_t frames[], unsigned int entries);
83 * `
84 */
85/*
86 * A number of GDT entries are reserved by Xen. These are not situated at the
87 * start of the GDT because some stupid OSes export hard-coded selector values
88 * in their ABI. These hard-coded values are always near the start of the GDT,
89 * so Xen places itself out of the way, at the far end of the GDT.
90 *
91 * NB The LDT is set using the MMUEXT_SET_LDT op of HYPERVISOR_mmuext_op
92 */
93#define FIRST_RESERVED_GDT_PAGE  14
94#define FIRST_RESERVED_GDT_BYTE  (FIRST_RESERVED_GDT_PAGE * 4096)
95#define FIRST_RESERVED_GDT_ENTRY (FIRST_RESERVED_GDT_BYTE / 8)
96
97
98/*
99 * ` enum neg_errnoval
100 * ` HYPERVISOR_update_descriptor(u64 pa, u64 desc);
101 * `
102 * ` @pa   The machine physical address of the descriptor to
103 * `       update. Must be either a descriptor page or writable.
104 * ` @desc The descriptor value to update, in the same format as a
105 * `       native descriptor table entry.
106 */
107
108/* Maximum number of virtual CPUs in legacy multi-processor guests. */
109#define XEN_LEGACY_MAX_VCPUS 32
110
111#ifndef __ASSEMBLY__
112
113typedef unsigned long xen_ulong_t;
114#define PRI_xen_ulong "lx"
115
116/*
117 * ` enum neg_errnoval
118 * ` HYPERVISOR_stack_switch(unsigned long ss, unsigned long esp);
119 * `
120 * Sets the stack segment and pointer for the current vcpu.
121 */
122
123/*
124 * ` enum neg_errnoval
125 * ` HYPERVISOR_set_trap_table(const struct trap_info traps[]);
126 * `
127 */
128/*
129 * Send an array of these to HYPERVISOR_set_trap_table().
130 * Terminate the array with a sentinel entry, with traps[].address==0.
131 * The privilege level specifies which modes may enter a trap via a software
132 * interrupt. On x86/64, since rings 1 and 2 are unavailable, we allocate
133 * privilege levels as follows:
134 *  Level == 0: Noone may enter
135 *  Level == 1: Kernel may enter
136 *  Level == 2: Kernel may enter
137 *  Level == 3: Everyone may enter
138 */
139#define TI_GET_DPL(_ti)      ((_ti)->flags & 3)
140#define TI_GET_IF(_ti)       ((_ti)->flags & 4)
141#define TI_SET_DPL(_ti,_dpl) ((_ti)->flags |= (_dpl))
142#define TI_SET_IF(_ti,_if)   ((_ti)->flags |= ((!!(_if))<<2))
143struct trap_info {
144    uint8_t       vector;  /* exception vector                              */
145    uint8_t       flags;   /* 0-3: privilege level; 4: clear event enable?  */
146    uint16_t      cs;      /* code selector                                 */
147    unsigned long address; /* code offset                                   */
148};
149typedef struct trap_info trap_info_t;
150DEFINE_XEN_GUEST_HANDLE(trap_info_t);
151
152typedef uint64_t tsc_timestamp_t; /* RDTSC timestamp */
153
154/*
155 * The following is all CPU context. Note that the fpu_ctxt block is filled
156 * in by FXSAVE if the CPU has feature FXSR; otherwise FSAVE is used.
157 *
158 * Also note that when calling DOMCTL_setvcpucontext and VCPU_initialise
159 * for HVM and PVH guests, not all information in this structure is updated:
160 *
161 * - For HVM guests, the structures read include: fpu_ctxt (if
162 * VGCT_I387_VALID is set), flags, user_regs, debugreg[*]
163 *
164 * - PVH guests are the same as HVM guests, but additionally use ctrlreg[3] to
165 * set cr3. All other fields not used should be set to 0.
166 */
167struct vcpu_guest_context {
168    /* FPU registers come first so they can be aligned for FXSAVE/FXRSTOR. */
169    struct { char x[512]; } fpu_ctxt;       /* User-level FPU registers     */
170#define VGCF_I387_VALID                (1<<0)
171#define VGCF_IN_KERNEL                 (1<<2)
172#define _VGCF_i387_valid               0
173#define VGCF_i387_valid                (1<<_VGCF_i387_valid)
174#define _VGCF_in_kernel                2
175#define VGCF_in_kernel                 (1<<_VGCF_in_kernel)
176#define _VGCF_failsafe_disables_events 3
177#define VGCF_failsafe_disables_events  (1<<_VGCF_failsafe_disables_events)
178#define _VGCF_syscall_disables_events  4
179#define VGCF_syscall_disables_events   (1<<_VGCF_syscall_disables_events)
180#define _VGCF_online                   5
181#define VGCF_online                    (1<<_VGCF_online)
182    unsigned long flags;                    /* VGCF_* flags                 */
183    struct cpu_user_regs user_regs;         /* User-level CPU registers     */
184    struct trap_info trap_ctxt[256];        /* Virtual IDT                  */
185    unsigned long ldt_base, ldt_ents;       /* LDT (linear address, # ents) */
186    unsigned long gdt_frames[16], gdt_ents; /* GDT (machine frames, # ents) */
187    unsigned long kernel_ss, kernel_sp;     /* Virtual TSS (only SS1/SP1)   */
188    /* NB. User pagetable on x86/64 is placed in ctrlreg[1]. */
189    unsigned long ctrlreg[8];               /* CR0-CR7 (control registers)  */
190    unsigned long debugreg[8];              /* DB0-DB7 (debug registers)    */
191#ifdef __i386__
192    unsigned long event_callback_cs;        /* CS:EIP of event callback     */
193    unsigned long event_callback_eip;
194    unsigned long failsafe_callback_cs;     /* CS:EIP of failsafe callback  */
195    unsigned long failsafe_callback_eip;
196#else
197    unsigned long event_callback_eip;
198    unsigned long failsafe_callback_eip;
199#ifdef __XEN__
200    union {
201        unsigned long syscall_callback_eip;
202        struct {
203            unsigned int event_callback_cs;    /* compat CS of event cb     */
204            unsigned int failsafe_callback_cs; /* compat CS of failsafe cb  */
205        };
206    };
207#else
208    unsigned long syscall_callback_eip;
209#endif
210#endif
211    unsigned long vm_assist;                /* VMASST_TYPE_* bitmap */
212#ifdef __x86_64__
213    /* Segment base addresses. */
214    uint64_t      fs_base;
215    uint64_t      gs_base_kernel;
216    uint64_t      gs_base_user;
217#endif
218};
219typedef struct vcpu_guest_context vcpu_guest_context_t;
220DEFINE_XEN_GUEST_HANDLE(vcpu_guest_context_t);
221
222struct arch_shared_info {
223    /*
224     * Number of valid entries in the p2m table(s) anchored at
225     * pfn_to_mfn_frame_list_list and/or p2m_vaddr.
226     */
227    unsigned long max_pfn;
228    /*
229     * Frame containing list of mfns containing list of mfns containing p2m.
230     * A value of 0 indicates it has not yet been set up, ~0 indicates it has
231     * been set to invalid e.g. due to the p2m being too large for the 3-level
232     * p2m tree. In this case the linear mapper p2m list anchored at p2m_vaddr
233     * is to be used.
234     */
235    xen_pfn_t     pfn_to_mfn_frame_list_list;
236    unsigned long nmi_reason;
237    /*
238     * Following three fields are valid if p2m_cr3 contains a value different
239     * from 0.
240     * p2m_cr3 is the root of the address space where p2m_vaddr is valid.
241     * p2m_cr3 is in the same format as a cr3 value in the vcpu register state
242     * and holds the folded machine frame number (via xen_pfn_to_cr3) of a
243     * L3 or L4 page table.
244     * p2m_vaddr holds the virtual address of the linear p2m list. All entries
245     * in the range [0...max_pfn[ are accessible via this pointer.
246     * p2m_generation will be incremented by the guest before and after each
247     * change of the mappings of the p2m list. p2m_generation starts at 0 and
248     * a value with the least significant bit set indicates that a mapping
249     * update is in progress. This allows guest external software (e.g. in Dom0)
250     * to verify that read mappings are consistent and whether they have changed
251     * since the last check.
252     * Modifying a p2m element in the linear p2m list is allowed via an atomic
253     * write only.
254     */
255    unsigned long p2m_cr3;         /* cr3 value of the p2m address space */
256    unsigned long p2m_vaddr;       /* virtual address of the p2m list */
257    unsigned long p2m_generation;  /* generation count of p2m mapping */
258#ifdef __i386__
259    /* There's no room for this field in the generic structure. */
260    uint32_t wc_sec_hi;
261#endif
262};
263typedef struct arch_shared_info arch_shared_info_t;
264
265#if defined(__XEN__) || defined(__XEN_TOOLS__)
266/*
267 * struct xen_arch_domainconfig's ABI is covered by
268 * XEN_DOMCTL_INTERFACE_VERSION.
269 */
270struct xen_arch_domainconfig {
271    char dummy;
272};
273#endif
274
275#endif /* !__ASSEMBLY__ */
276
277/*
278 * ` enum neg_errnoval
279 * ` HYPERVISOR_fpu_taskswitch(int set);
280 * `
281 * Sets (if set!=0) or clears (if set==0) CR0.TS.
282 */
283
284/*
285 * ` enum neg_errnoval
286 * ` HYPERVISOR_set_debugreg(int regno, unsigned long value);
287 *
288 * ` unsigned long
289 * ` HYPERVISOR_get_debugreg(int regno);
290 * For 0<=reg<=7, returns the debug register value.
291 * For other values of reg, returns ((unsigned long)-EINVAL).
292 * (Unfortunately, this interface is defective.)
293 */
294
295/*
296 * Prefix forces emulation of some non-trapping instructions.
297 * Currently only CPUID.
298 */
299#ifdef __ASSEMBLY__
300#define XEN_EMULATE_PREFIX .byte 0x0f,0x0b,0x78,0x65,0x6e ;
301#define XEN_CPUID          XEN_EMULATE_PREFIX cpuid
302#else
303#define XEN_EMULATE_PREFIX ".byte 0x0f,0x0b,0x78,0x65,0x6e ; "
304#define XEN_CPUID          XEN_EMULATE_PREFIX "cpuid"
305#endif
306
307#endif /* __XEN_PUBLIC_ARCH_X86_XEN_H__ */
308
309/*
310 * Local variables:
311 * mode: C
312 * c-file-style: "BSD"
313 * c-basic-offset: 4
314 * tab-width: 4
315 * indent-tabs-mode: nil
316 * End:
317 */
318