1 /* SPDX-License-Identifier: MIT */
2 /******************************************************************************
3  * xen-x86_32.h
4  *
5  * Guest OS interface to x86 32-bit Xen.
6  *
7  * Copyright (c) 2004-2007, K A Fraser
8  */
9 
10 #ifndef __XEN_PUBLIC_ARCH_X86_XEN_X86_32_H__
11 #define __XEN_PUBLIC_ARCH_X86_XEN_X86_32_H__
12 
13 /*
14  * Hypercall interface:
15  *  Input:  %ebx, %ecx, %edx, %esi, %edi, %ebp (arguments 1-6)
16  *  Output: %eax
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 %ebx,%ecx)
20  */
21 
22 /*
23  * These flat segments are in the Xen-private section of every GDT. Since these
24  * are also present in the initial GDT, many OSes will be able to avoid
25  * installing their own GDT.
26  */
27 #define FLAT_RING1_CS 0xe019    /* GDT index 259 */
28 #define FLAT_RING1_DS 0xe021    /* GDT index 260 */
29 #define FLAT_RING1_SS 0xe021    /* GDT index 260 */
30 #define FLAT_RING3_CS 0xe02b    /* GDT index 261 */
31 #define FLAT_RING3_DS 0xe033    /* GDT index 262 */
32 #define FLAT_RING3_SS 0xe033    /* GDT index 262 */
33 
34 #define FLAT_KERNEL_CS FLAT_RING1_CS
35 #define FLAT_KERNEL_DS FLAT_RING1_DS
36 #define FLAT_KERNEL_SS FLAT_RING1_SS
37 #define FLAT_USER_CS    FLAT_RING3_CS
38 #define FLAT_USER_DS    FLAT_RING3_DS
39 #define FLAT_USER_SS    FLAT_RING3_SS
40 
41 #define __HYPERVISOR_VIRT_START_PAE    0xF5800000
42 #define __MACH2PHYS_VIRT_START_PAE     0xF5800000
43 #define __MACH2PHYS_VIRT_END_PAE       0xF6800000
44 #define HYPERVISOR_VIRT_START_PAE      xen_mk_ulong(__HYPERVISOR_VIRT_START_PAE)
45 #define MACH2PHYS_VIRT_START_PAE       xen_mk_ulong(__MACH2PHYS_VIRT_START_PAE)
46 #define MACH2PHYS_VIRT_END_PAE         xen_mk_ulong(__MACH2PHYS_VIRT_END_PAE)
47 
48 /* Non-PAE bounds are obsolete. */
49 #define __HYPERVISOR_VIRT_START_NONPAE 0xFC000000
50 #define __MACH2PHYS_VIRT_START_NONPAE  0xFC000000
51 #define __MACH2PHYS_VIRT_END_NONPAE    0xFC400000
52 #define HYPERVISOR_VIRT_START_NONPAE   \
53     xen_mk_ulong(__HYPERVISOR_VIRT_START_NONPAE)
54 #define MACH2PHYS_VIRT_START_NONPAE    \
55     xen_mk_ulong(__MACH2PHYS_VIRT_START_NONPAE)
56 #define MACH2PHYS_VIRT_END_NONPAE      \
57     xen_mk_ulong(__MACH2PHYS_VIRT_END_NONPAE)
58 
59 #define __HYPERVISOR_VIRT_START __HYPERVISOR_VIRT_START_PAE
60 #define __MACH2PHYS_VIRT_START  __MACH2PHYS_VIRT_START_PAE
61 #define __MACH2PHYS_VIRT_END    __MACH2PHYS_VIRT_END_PAE
62 
63 #ifndef HYPERVISOR_VIRT_START
64 #define HYPERVISOR_VIRT_START xen_mk_ulong(__HYPERVISOR_VIRT_START)
65 #endif
66 
67 #define MACH2PHYS_VIRT_START  xen_mk_ulong(__MACH2PHYS_VIRT_START)
68 #define MACH2PHYS_VIRT_END    xen_mk_ulong(__MACH2PHYS_VIRT_END)
69 #define MACH2PHYS_NR_ENTRIES  ((MACH2PHYS_VIRT_END-MACH2PHYS_VIRT_START)>>2)
70 #ifndef machine_to_phys_mapping
71 #define machine_to_phys_mapping ((unsigned long *)MACH2PHYS_VIRT_START)
72 #endif
73 
74 /* 32-/64-bit invariability for control interfaces (domctl/sysctl). */
75 #if defined(__XEN__) || defined(__XEN_TOOLS__)
76 #undef ___DEFINE_XEN_GUEST_HANDLE
77 #define ___DEFINE_XEN_GUEST_HANDLE(name, type)                  \
78     typedef struct { type *p; }                                 \
79         __guest_handle_ ## name;                                \
80     typedef struct { union { type *p; uint64_aligned_t q; }; }  \
81         __guest_handle_64_ ## name
82 #undef set_xen_guest_handle_raw
83 #define set_xen_guest_handle_raw(hnd, val)                  \
84     do { if ( sizeof(hnd) == 8 ) *(uint64_t *)&(hnd) = 0;   \
85          (hnd).p = val;                                     \
86     } while ( 0 )
87 #define  int64_aligned_t  int64_t __attribute__((aligned(8)))
88 #define uint64_aligned_t uint64_t __attribute__((aligned(8)))
89 #define __XEN_GUEST_HANDLE_64(name) __guest_handle_64_ ## name
90 #define XEN_GUEST_HANDLE_64(name) __XEN_GUEST_HANDLE_64(name)
91 #endif
92 
93 #ifndef __ASSEMBLY__
94 
95 #if defined(XEN_GENERATING_COMPAT_HEADERS)
96 /* nothing */
97 #elif defined(__XEN__) || defined(__XEN_TOOLS__)
98 /* Anonymous unions include all permissible names (e.g., al/ah/ax/eax). */
99 #define __DECL_REG_LO8(which) union { \
100     uint32_t e ## which ## x; \
101     uint16_t which ## x; \
102     struct { \
103         uint8_t which ## l; \
104         uint8_t which ## h; \
105     }; \
106 }
107 #define __DECL_REG_LO16(name) union { \
108     uint32_t e ## name, _e ## name; \
109     uint16_t name; \
110 }
111 #else
112 /* Other sources must always use the proper 32-bit name (e.g., eax). */
113 #define __DECL_REG_LO8(which) uint32_t e ## which ## x
114 #define __DECL_REG_LO16(name) uint32_t e ## name
115 #endif
116 
117 struct cpu_user_regs {
118     __DECL_REG_LO8(b);
119     __DECL_REG_LO8(c);
120     __DECL_REG_LO8(d);
121     __DECL_REG_LO16(si);
122     __DECL_REG_LO16(di);
123     __DECL_REG_LO16(bp);
124     __DECL_REG_LO8(a);
125     uint16_t error_code;    /* private */
126     uint16_t entry_vector;  /* private */
127     __DECL_REG_LO16(ip);
128     uint16_t cs;
129     uint8_t  saved_upcall_mask;
130     uint8_t  _pad0;
131     __DECL_REG_LO16(flags); /* eflags.IF == !saved_upcall_mask */
132     __DECL_REG_LO16(sp);
133     uint16_t ss, _pad1;
134     uint16_t es, _pad2;
135     uint16_t ds, _pad3;
136     uint16_t fs, _pad4;
137     uint16_t gs, _pad5;
138 };
139 typedef struct cpu_user_regs cpu_user_regs_t;
140 DEFINE_XEN_GUEST_HANDLE(cpu_user_regs_t);
141 
142 #undef __DECL_REG_LO8
143 #undef __DECL_REG_LO16
144 
145 /*
146  * Page-directory addresses above 4GB do not fit into architectural %cr3.
147  * When accessing %cr3, or equivalent field in vcpu_guest_context, guests
148  * must use the following accessor macros to pack/unpack valid MFNs.
149  */
150 #define xen_pfn_to_cr3(pfn) (((unsigned)(pfn) << 12) | ((unsigned)(pfn) >> 20))
151 #define xen_cr3_to_pfn(cr3) (((unsigned)(cr3) >> 12) | ((unsigned)(cr3) << 20))
152 
153 struct arch_vcpu_info {
154     unsigned long cr2;
155     unsigned long pad[5]; /* sizeof(vcpu_info_t) == 64 */
156 };
157 typedef struct arch_vcpu_info arch_vcpu_info_t;
158 
159 struct xen_callback {
160     unsigned long cs;
161     unsigned long eip;
162 };
163 typedef struct xen_callback xen_callback_t;
164 
165 #endif /* !__ASSEMBLY__ */
166 
167 #endif /* __XEN_PUBLIC_ARCH_X86_XEN_X86_32_H__ */
168 
169 /*
170  * Local variables:
171  * mode: C
172  * c-file-style: "BSD"
173  * c-basic-offset: 4
174  * tab-width: 4
175  * indent-tabs-mode: nil
176  * End:
177  */
178