1181624Skmacy/******************************************************************************
2181624Skmacy * arch-x86/xen.h
3181624Skmacy *
4181624Skmacy * Guest OS interface to x86 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
27251767Sgibbs#include "../xen.h"
28251767Sgibbs
29181624Skmacy#ifndef __XEN_PUBLIC_ARCH_X86_XEN_H__
30181624Skmacy#define __XEN_PUBLIC_ARCH_X86_XEN_H__
31181624Skmacy
32183340Skmacy/* Structural guest handles introduced in 0x00030201. */
33183340Skmacy#if __XEN_INTERFACE_VERSION__ >= 0x00030201
34183340Skmacy#define ___DEFINE_XEN_GUEST_HANDLE(name, type) \
35183340Skmacy    typedef struct { type *p; } __guest_handle_ ## name
36183340Skmacy#else
37251767Sgibbs#define ___DEFINE_XEN_GUEST_HANDLE(name, type) \
38183340Skmacy    typedef type * __guest_handle_ ## name
39183340Skmacy#endif
40183340Skmacy
41288917Sroyger/*
42288917Sroyger * XEN_GUEST_HANDLE represents a guest pointer, when passed as a field
43288917Sroyger * in a struct in memory.
44288917Sroyger * XEN_GUEST_HANDLE_PARAM represent a guest pointer, when passed as an
45288917Sroyger * hypercall argument.
46288917Sroyger * XEN_GUEST_HANDLE_PARAM and XEN_GUEST_HANDLE are the same on X86 but
47288917Sroyger * they might not be on other architectures.
48288917Sroyger */
49183340Skmacy#define __DEFINE_XEN_GUEST_HANDLE(name, type) \
50183340Skmacy    ___DEFINE_XEN_GUEST_HANDLE(name, type);   \
51183340Skmacy    ___DEFINE_XEN_GUEST_HANDLE(const_##name, const type)
52183340Skmacy#define DEFINE_XEN_GUEST_HANDLE(name)   __DEFINE_XEN_GUEST_HANDLE(name, name)
53183340Skmacy#define __XEN_GUEST_HANDLE(name)        __guest_handle_ ## name
54183340Skmacy#define XEN_GUEST_HANDLE(name)          __XEN_GUEST_HANDLE(name)
55288917Sroyger#define XEN_GUEST_HANDLE_PARAM(name)    XEN_GUEST_HANDLE(name)
56251767Sgibbs#define set_xen_guest_handle_raw(hnd, val)  do { (hnd).p = val; } while (0)
57183340Skmacy#ifdef __XEN_TOOLS__
58183340Skmacy#define get_xen_guest_handle(val, hnd)  do { val = (hnd).p; } while (0)
59183340Skmacy#endif
60251767Sgibbs#define set_xen_guest_handle(hnd, val) set_xen_guest_handle_raw(hnd, val)
61183340Skmacy
62181624Skmacy#if defined(__i386__)
63251767Sgibbs#include "xen-x86_32.h"
64181624Skmacy#elif defined(__x86_64__)
65251767Sgibbs#include "xen-x86_64.h"
66181624Skmacy#endif
67181624Skmacy
68181624Skmacy#ifndef __ASSEMBLY__
69181624Skmacytypedef unsigned long xen_pfn_t;
70181624Skmacy#define PRI_xen_pfn "lx"
71181624Skmacy#endif
72181624Skmacy
73288917Sroyger#define XEN_HAVE_PV_GUEST_ENTRY 1
74288917Sroyger
75288917Sroyger#define XEN_HAVE_PV_UPCALL_MASK 1
76288917Sroyger
77181624Skmacy/*
78288917Sroyger * `incontents 200 segdesc Segment Descriptor Tables
79181624Skmacy */
80181624Skmacy/*
81251767Sgibbs * ` enum neg_errnoval
82251767Sgibbs * ` HYPERVISOR_set_gdt(const xen_pfn_t frames[], unsigned int entries);
83251767Sgibbs * `
84251767Sgibbs */
85251767Sgibbs/*
86181624Skmacy * A number of GDT entries are reserved by Xen. These are not situated at the
87181624Skmacy * start of the GDT because some stupid OSes export hard-coded selector values
88181624Skmacy * in their ABI. These hard-coded values are always near the start of the GDT,
89181624Skmacy * so Xen places itself out of the way, at the far end of the GDT.
90288917Sroyger *
91288917Sroyger * NB The LDT is set using the MMUEXT_SET_LDT op of HYPERVISOR_mmuext_op
92181624Skmacy */
93181624Skmacy#define FIRST_RESERVED_GDT_PAGE  14
94181624Skmacy#define FIRST_RESERVED_GDT_BYTE  (FIRST_RESERVED_GDT_PAGE * 4096)
95181624Skmacy#define FIRST_RESERVED_GDT_ENTRY (FIRST_RESERVED_GDT_BYTE / 8)
96181624Skmacy
97288917Sroyger
98288917Sroyger/*
99288917Sroyger * ` enum neg_errnoval
100288917Sroyger * ` HYPERVISOR_update_descriptor(u64 pa, u64 desc);
101288917Sroyger * `
102288917Sroyger * ` @pa   The machine physical address of the descriptor to
103288917Sroyger * `       update. Must be either a descriptor page or writable.
104288917Sroyger * ` @desc The descriptor value to update, in the same format as a
105288917Sroyger * `       native descriptor table entry.
106288917Sroyger */
107288917Sroyger
108251767Sgibbs/* Maximum number of virtual CPUs in legacy multi-processor guests. */
109251767Sgibbs#define XEN_LEGACY_MAX_VCPUS 32
110181624Skmacy
111181624Skmacy#ifndef __ASSEMBLY__
112181624Skmacy
113181624Skmacytypedef unsigned long xen_ulong_t;
114288917Sroyger#define PRI_xen_ulong "lx"
115181624Skmacy
116181624Skmacy/*
117251767Sgibbs * ` enum neg_errnoval
118251767Sgibbs * ` HYPERVISOR_stack_switch(unsigned long ss, unsigned long esp);
119251767Sgibbs * `
120251767Sgibbs * Sets the stack segment and pointer for the current vcpu.
121251767Sgibbs */
122251767Sgibbs
123251767Sgibbs/*
124251767Sgibbs * ` enum neg_errnoval
125251767Sgibbs * ` HYPERVISOR_set_trap_table(const struct trap_info traps[]);
126251767Sgibbs * `
127251767Sgibbs */
128251767Sgibbs/*
129181624Skmacy * Send an array of these to HYPERVISOR_set_trap_table().
130251767Sgibbs * Terminate the array with a sentinel entry, with traps[].address==0.
131181624Skmacy * The privilege level specifies which modes may enter a trap via a software
132181624Skmacy * interrupt. On x86/64, since rings 1 and 2 are unavailable, we allocate
133181624Skmacy * privilege levels as follows:
134181624Skmacy *  Level == 0: Noone may enter
135181624Skmacy *  Level == 1: Kernel may enter
136181624Skmacy *  Level == 2: Kernel may enter
137181624Skmacy *  Level == 3: Everyone may enter
138181624Skmacy */
139181624Skmacy#define TI_GET_DPL(_ti)      ((_ti)->flags & 3)
140181624Skmacy#define TI_GET_IF(_ti)       ((_ti)->flags & 4)
141181624Skmacy#define TI_SET_DPL(_ti,_dpl) ((_ti)->flags |= (_dpl))
142181624Skmacy#define TI_SET_IF(_ti,_if)   ((_ti)->flags |= ((!!(_if))<<2))
143181624Skmacystruct trap_info {
144181624Skmacy    uint8_t       vector;  /* exception vector                              */
145181624Skmacy    uint8_t       flags;   /* 0-3: privilege level; 4: clear event enable?  */
146181624Skmacy    uint16_t      cs;      /* code selector                                 */
147181624Skmacy    unsigned long address; /* code offset                                   */
148181624Skmacy};
149181624Skmacytypedef struct trap_info trap_info_t;
150181624SkmacyDEFINE_XEN_GUEST_HANDLE(trap_info_t);
151181624Skmacy
152181624Skmacytypedef uint64_t tsc_timestamp_t; /* RDTSC timestamp */
153181624Skmacy
154181624Skmacy/*
155181624Skmacy * The following is all CPU context. Note that the fpu_ctxt block is filled
156181624Skmacy * in by FXSAVE if the CPU has feature FXSR; otherwise FSAVE is used.
157288917Sroyger *
158288917Sroyger * Also note that when calling DOMCTL_setvcpucontext and VCPU_initialise
159288917Sroyger * for HVM and PVH guests, not all information in this structure is updated:
160288917Sroyger *
161288917Sroyger * - For HVM guests, the structures read include: fpu_ctxt (if
162288917Sroyger * VGCT_I387_VALID is set), flags, user_regs, debugreg[*]
163288917Sroyger *
164288917Sroyger * - PVH guests are the same as HVM guests, but additionally use ctrlreg[3] to
165288917Sroyger * set cr3. All other fields not used should be set to 0.
166181624Skmacy */
167181624Skmacystruct vcpu_guest_context {
168181624Skmacy    /* FPU registers come first so they can be aligned for FXSAVE/FXRSTOR. */
169181624Skmacy    struct { char x[512]; } fpu_ctxt;       /* User-level FPU registers     */
170181624Skmacy#define VGCF_I387_VALID                (1<<0)
171181624Skmacy#define VGCF_IN_KERNEL                 (1<<2)
172181624Skmacy#define _VGCF_i387_valid               0
173181624Skmacy#define VGCF_i387_valid                (1<<_VGCF_i387_valid)
174181624Skmacy#define _VGCF_in_kernel                2
175181624Skmacy#define VGCF_in_kernel                 (1<<_VGCF_in_kernel)
176181624Skmacy#define _VGCF_failsafe_disables_events 3
177181624Skmacy#define VGCF_failsafe_disables_events  (1<<_VGCF_failsafe_disables_events)
178181624Skmacy#define _VGCF_syscall_disables_events  4
179181624Skmacy#define VGCF_syscall_disables_events   (1<<_VGCF_syscall_disables_events)
180181624Skmacy#define _VGCF_online                   5
181181624Skmacy#define VGCF_online                    (1<<_VGCF_online)
182181624Skmacy    unsigned long flags;                    /* VGCF_* flags                 */
183181624Skmacy    struct cpu_user_regs user_regs;         /* User-level CPU registers     */
184181624Skmacy    struct trap_info trap_ctxt[256];        /* Virtual IDT                  */
185181624Skmacy    unsigned long ldt_base, ldt_ents;       /* LDT (linear address, # ents) */
186181624Skmacy    unsigned long gdt_frames[16], gdt_ents; /* GDT (machine frames, # ents) */
187181624Skmacy    unsigned long kernel_ss, kernel_sp;     /* Virtual TSS (only SS1/SP1)   */
188181624Skmacy    /* NB. User pagetable on x86/64 is placed in ctrlreg[1]. */
189181624Skmacy    unsigned long ctrlreg[8];               /* CR0-CR7 (control registers)  */
190181624Skmacy    unsigned long debugreg[8];              /* DB0-DB7 (debug registers)    */
191181624Skmacy#ifdef __i386__
192181624Skmacy    unsigned long event_callback_cs;        /* CS:EIP of event callback     */
193181624Skmacy    unsigned long event_callback_eip;
194181624Skmacy    unsigned long failsafe_callback_cs;     /* CS:EIP of failsafe callback  */
195181624Skmacy    unsigned long failsafe_callback_eip;
196181624Skmacy#else
197181624Skmacy    unsigned long event_callback_eip;
198181624Skmacy    unsigned long failsafe_callback_eip;
199181624Skmacy#ifdef __XEN__
200181624Skmacy    union {
201181624Skmacy        unsigned long syscall_callback_eip;
202181624Skmacy        struct {
203181624Skmacy            unsigned int event_callback_cs;    /* compat CS of event cb     */
204181624Skmacy            unsigned int failsafe_callback_cs; /* compat CS of failsafe cb  */
205181624Skmacy        };
206251767Sgibbs    };
207181624Skmacy#else
208181624Skmacy    unsigned long syscall_callback_eip;
209181624Skmacy#endif
210181624Skmacy#endif
211181624Skmacy    unsigned long vm_assist;                /* VMASST_TYPE_* bitmap */
212181624Skmacy#ifdef __x86_64__
213181624Skmacy    /* Segment base addresses. */
214181624Skmacy    uint64_t      fs_base;
215181624Skmacy    uint64_t      gs_base_kernel;
216181624Skmacy    uint64_t      gs_base_user;
217181624Skmacy#endif
218181624Skmacy};
219181624Skmacytypedef struct vcpu_guest_context vcpu_guest_context_t;
220181624SkmacyDEFINE_XEN_GUEST_HANDLE(vcpu_guest_context_t);
221181624Skmacy
222181624Skmacystruct arch_shared_info {
223288917Sroyger    /*
224288917Sroyger     * Number of valid entries in the p2m table(s) anchored at
225288917Sroyger     * pfn_to_mfn_frame_list_list and/or p2m_vaddr.
226288917Sroyger     */
227288917Sroyger    unsigned long max_pfn;
228288917Sroyger    /*
229288917Sroyger     * Frame containing list of mfns containing list of mfns containing p2m.
230288917Sroyger     * A value of 0 indicates it has not yet been set up, ~0 indicates it has
231288917Sroyger     * been set to invalid e.g. due to the p2m being too large for the 3-level
232288917Sroyger     * p2m tree. In this case the linear mapper p2m list anchored at p2m_vaddr
233288917Sroyger     * is to be used.
234288917Sroyger     */
235181624Skmacy    xen_pfn_t     pfn_to_mfn_frame_list_list;
236181624Skmacy    unsigned long nmi_reason;
237288917Sroyger    /*
238288917Sroyger     * Following three fields are valid if p2m_cr3 contains a value different
239288917Sroyger     * from 0.
240288917Sroyger     * p2m_cr3 is the root of the address space where p2m_vaddr is valid.
241288917Sroyger     * p2m_cr3 is in the same format as a cr3 value in the vcpu register state
242288917Sroyger     * and holds the folded machine frame number (via xen_pfn_to_cr3) of a
243288917Sroyger     * L3 or L4 page table.
244288917Sroyger     * p2m_vaddr holds the virtual address of the linear p2m list. All entries
245288917Sroyger     * in the range [0...max_pfn[ are accessible via this pointer.
246288917Sroyger     * p2m_generation will be incremented by the guest before and after each
247288917Sroyger     * change of the mappings of the p2m list. p2m_generation starts at 0 and
248288917Sroyger     * a value with the least significant bit set indicates that a mapping
249288917Sroyger     * update is in progress. This allows guest external software (e.g. in Dom0)
250288917Sroyger     * to verify that read mappings are consistent and whether they have changed
251288917Sroyger     * since the last check.
252288917Sroyger     * Modifying a p2m element in the linear p2m list is allowed via an atomic
253288917Sroyger     * write only.
254288917Sroyger     */
255288917Sroyger    unsigned long p2m_cr3;         /* cr3 value of the p2m address space */
256288917Sroyger    unsigned long p2m_vaddr;       /* virtual address of the p2m list */
257288917Sroyger    unsigned long p2m_generation;  /* generation count of p2m mapping */
258288917Sroyger#ifdef __i386__
259288917Sroyger    /* There's no room for this field in the generic structure. */
260288917Sroyger    uint32_t wc_sec_hi;
261288917Sroyger#endif
262181624Skmacy};
263181624Skmacytypedef struct arch_shared_info arch_shared_info_t;
264181624Skmacy
265288917Sroyger#if defined(__XEN__) || defined(__XEN_TOOLS__)
266288917Sroyger/*
267288917Sroyger * struct xen_arch_domainconfig's ABI is covered by
268288917Sroyger * XEN_DOMCTL_INTERFACE_VERSION.
269288917Sroyger */
270288917Sroygerstruct xen_arch_domainconfig {
271288917Sroyger    char dummy;
272288917Sroyger};
273288917Sroyger#endif
274288917Sroyger
275181624Skmacy#endif /* !__ASSEMBLY__ */
276181624Skmacy
277181624Skmacy/*
278251767Sgibbs * ` enum neg_errnoval
279251767Sgibbs * ` HYPERVISOR_fpu_taskswitch(int set);
280251767Sgibbs * `
281251767Sgibbs * Sets (if set!=0) or clears (if set==0) CR0.TS.
282251767Sgibbs */
283251767Sgibbs
284251767Sgibbs/*
285251767Sgibbs * ` enum neg_errnoval
286251767Sgibbs * ` HYPERVISOR_set_debugreg(int regno, unsigned long value);
287251767Sgibbs *
288251767Sgibbs * ` unsigned long
289251767Sgibbs * ` HYPERVISOR_get_debugreg(int regno);
290251767Sgibbs * For 0<=reg<=7, returns the debug register value.
291251767Sgibbs * For other values of reg, returns ((unsigned long)-EINVAL).
292251767Sgibbs * (Unfortunately, this interface is defective.)
293251767Sgibbs */
294251767Sgibbs
295251767Sgibbs/*
296181624Skmacy * Prefix forces emulation of some non-trapping instructions.
297181624Skmacy * Currently only CPUID.
298181624Skmacy */
299181624Skmacy#ifdef __ASSEMBLY__
300181624Skmacy#define XEN_EMULATE_PREFIX .byte 0x0f,0x0b,0x78,0x65,0x6e ;
301181624Skmacy#define XEN_CPUID          XEN_EMULATE_PREFIX cpuid
302181624Skmacy#else
303181624Skmacy#define XEN_EMULATE_PREFIX ".byte 0x0f,0x0b,0x78,0x65,0x6e ; "
304181624Skmacy#define XEN_CPUID          XEN_EMULATE_PREFIX "cpuid"
305181624Skmacy#endif
306181624Skmacy
307181624Skmacy#endif /* __XEN_PUBLIC_ARCH_X86_XEN_H__ */
308181624Skmacy
309181624Skmacy/*
310181624Skmacy * Local variables:
311181624Skmacy * mode: C
312288917Sroyger * c-file-style: "BSD"
313181624Skmacy * c-basic-offset: 4
314181624Skmacy * tab-width: 4
315181624Skmacy * indent-tabs-mode: nil
316181624Skmacy * End:
317181624Skmacy */
318