These changes are the raw update to qemu-2.6.
[kvmfornfv.git] / qemu / roms / ipxe / src / arch / i386 / include / librm.h
1 #ifndef LIBRM_H
2 #define LIBRM_H
3
4 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
5
6 /* Segment selectors as used in our protected-mode GDTs.
7  *
8  * Don't change these unless you really know what you're doing.
9  */
10
11 #define VIRTUAL_CS 0x08
12 #define VIRTUAL_DS 0x10
13 #define PHYSICAL_CS 0x18
14 #define PHYSICAL_DS 0x20
15 #define REAL_CS 0x28
16 #define REAL_DS 0x30
17 #if 0
18 #define LONG_CS 0x38
19 #define LONG_DS 0x40
20 #endif
21
22 #ifndef ASSEMBLY
23
24 #ifdef UACCESS_LIBRM
25 #define UACCESS_PREFIX_librm
26 #else
27 #define UACCESS_PREFIX_librm __librm_
28 #endif
29
30 /* Variables in librm.S */
31 extern unsigned long virt_offset;
32
33 /**
34  * Convert physical address to user pointer
35  *
36  * @v phys_addr         Physical address
37  * @ret userptr         User pointer
38  */
39 static inline __always_inline userptr_t
40 UACCESS_INLINE ( librm, phys_to_user ) ( unsigned long phys_addr ) {
41         return ( phys_addr - virt_offset );
42 }
43
44 /**
45  * Convert user buffer to physical address
46  *
47  * @v userptr           User pointer
48  * @v offset            Offset from user pointer
49  * @ret phys_addr       Physical address
50  */
51 static inline __always_inline unsigned long
52 UACCESS_INLINE ( librm, user_to_phys ) ( userptr_t userptr, off_t offset ) {
53         return ( userptr + offset + virt_offset );
54 }
55
56 static inline __always_inline userptr_t
57 UACCESS_INLINE ( librm, virt_to_user ) ( volatile const void *addr ) {
58         return trivial_virt_to_user ( addr );
59 }
60
61 static inline __always_inline void *
62 UACCESS_INLINE ( librm, user_to_virt ) ( userptr_t userptr, off_t offset ) {
63         return trivial_user_to_virt ( userptr, offset );
64 }
65
66 static inline __always_inline userptr_t
67 UACCESS_INLINE ( librm, userptr_add ) ( userptr_t userptr, off_t offset ) {
68         return trivial_userptr_add ( userptr, offset );
69 }
70
71 static inline __always_inline off_t
72 UACCESS_INLINE ( librm, userptr_sub ) ( userptr_t userptr,
73                                         userptr_t subtrahend ) {
74         return trivial_userptr_sub ( userptr, subtrahend );
75 }
76
77 static inline __always_inline void
78 UACCESS_INLINE ( librm, memcpy_user ) ( userptr_t dest, off_t dest_off,
79                                         userptr_t src, off_t src_off,
80                                         size_t len ) {
81         trivial_memcpy_user ( dest, dest_off, src, src_off, len );
82 }
83
84 static inline __always_inline void
85 UACCESS_INLINE ( librm, memmove_user ) ( userptr_t dest, off_t dest_off,
86                                          userptr_t src, off_t src_off,
87                                          size_t len ) {
88         trivial_memmove_user ( dest, dest_off, src, src_off, len );
89 }
90
91 static inline __always_inline int
92 UACCESS_INLINE ( librm, memcmp_user ) ( userptr_t first, off_t first_off,
93                                         userptr_t second, off_t second_off,
94                                         size_t len ) {
95         return trivial_memcmp_user ( first, first_off, second, second_off, len);
96 }
97
98 static inline __always_inline void
99 UACCESS_INLINE ( librm, memset_user ) ( userptr_t buffer, off_t offset,
100                                         int c, size_t len ) {
101         trivial_memset_user ( buffer, offset, c, len );
102 }
103
104 static inline __always_inline size_t
105 UACCESS_INLINE ( librm, strlen_user ) ( userptr_t buffer, off_t offset ) {
106         return trivial_strlen_user ( buffer, offset );
107 }
108
109 static inline __always_inline off_t
110 UACCESS_INLINE ( librm, memchr_user ) ( userptr_t buffer, off_t offset,
111                                         int c, size_t len ) {
112         return trivial_memchr_user ( buffer, offset, c, len );
113 }
114
115
116 /******************************************************************************
117  *
118  * Access to variables in .data16 and .text16
119  *
120  */
121
122 extern char *data16;
123 extern char *text16;
124
125 #define __data16( variable )                                            \
126         __attribute__ (( section ( ".data16" ) ))                       \
127         _data16_ ## variable __asm__ ( #variable )
128
129 #define __data16_array( variable, array )                               \
130         __attribute__ (( section ( ".data16" ) ))                       \
131         _data16_ ## variable array __asm__ ( #variable )
132
133 #define __bss16( variable )                                             \
134         __attribute__ (( section ( ".bss16" ) ))                        \
135         _data16_ ## variable __asm__ ( #variable )
136
137 #define __bss16_array( variable, array )                                \
138         __attribute__ (( section ( ".bss16" ) ))                        \
139         _data16_ ## variable array __asm__ ( #variable )
140
141 #define __text16( variable )                                            \
142         __attribute__ (( section ( ".text16.data" ) ))                  \
143         _text16_ ## variable __asm__ ( #variable )
144
145 #define __text16_array( variable, array )                               \
146         __attribute__ (( section ( ".text16.data" ) ))                  \
147         _text16_ ## variable array __asm__ ( #variable )
148
149 #define __use_data16( variable )                                        \
150         ( * ( ( typeof ( _data16_ ## variable ) * )                     \
151               & ( data16 [ ( size_t ) & ( _data16_ ## variable ) ] ) ) )
152
153 #define __use_text16( variable )                                        \
154         ( * ( ( typeof ( _text16_ ## variable ) * )                     \
155               & ( text16 [ ( size_t ) & ( _text16_ ## variable ) ] ) ) )
156
157 #define __from_data16( pointer )                                        \
158         ( ( unsigned int )                                              \
159           ( ( ( void * ) (pointer) ) - ( ( void * ) data16 ) ) )
160
161 #define __from_text16( pointer )                                        \
162         ( ( unsigned int )                                              \
163           ( ( ( void * ) (pointer) ) - ( ( void * ) text16 ) ) )
164
165 /* Variables in librm.S, present in the normal data segment */
166 extern uint16_t rm_sp;
167 extern uint16_t rm_ss;
168 extern uint16_t __text16 ( rm_cs );
169 #define rm_cs __use_text16 ( rm_cs )
170 extern uint16_t __text16 ( rm_ds );
171 #define rm_ds __use_text16 ( rm_ds )
172
173 extern uint16_t copy_user_to_rm_stack ( userptr_t data, size_t size );
174 extern void remove_user_from_rm_stack ( userptr_t data, size_t size );
175
176 /* TEXT16_CODE: declare a fragment of code that resides in .text16 */
177 #define TEXT16_CODE( asm_code_str )                     \
178         ".section \".text16\", \"ax\", @progbits\n\t"   \
179         ".code16\n\t"                                   \
180         asm_code_str "\n\t"                             \
181         ".code32\n\t"                                   \
182         ".previous\n\t"
183
184 /* REAL_CODE: declare a fragment of code that executes in real mode */
185 #define REAL_CODE( asm_code_str )                       \
186         "pushl $1f\n\t"                                 \
187         "call real_call\n\t"                            \
188         "addl $4, %%esp\n\t"                            \
189         TEXT16_CODE ( "\n1:\n\t"                        \
190                       asm_code_str                      \
191                       "\n\t"                            \
192                       "ret\n\t" )
193
194 /* PHYS_CODE: declare a fragment of code that executes in flat physical mode */
195 #define PHYS_CODE( asm_code_str )                       \
196         "call _virt_to_phys\n\t"                        \
197         asm_code_str                                    \
198         "call _phys_to_virt\n\t"
199
200 /** Number of interrupts */
201 #define NUM_INT 256
202
203 /** An interrupt descriptor table register */
204 struct idtr {
205         /** Limit */
206         uint16_t limit;
207         /** Base */
208         uint32_t base;
209 } __attribute__ (( packed ));
210
211 /** An interrupt descriptor table entry */
212 struct interrupt_descriptor {
213         /** Low 16 bits of address */
214         uint16_t low;
215         /** Code segment */
216         uint16_t segment;
217         /** Unused */
218         uint8_t unused;
219         /** Type and attributes */
220         uint8_t attr;
221         /** High 16 bits of address */
222         uint16_t high;
223 } __attribute__ (( packed ));
224
225 /** Interrupt descriptor is present */
226 #define IDTE_PRESENT 0x80
227
228 /** Interrupt descriptor 32-bit interrupt gate type */
229 #define IDTE_TYPE_IRQ32 0x0e
230
231 /** An interrupt vector
232  *
233  * Each interrupt vector comprises an eight-byte fragment of code:
234  *
235  *   60                 pushal
236  *   b0 xx              movb $INT, %al
237  *   e9 xx xx xx xx     jmp interrupt_wrapper
238  */
239 struct interrupt_vector {
240         /** "pushal" instruction */
241         uint8_t pushal;
242         /** "movb" instruction */
243         uint8_t movb;
244         /** Interrupt number */
245         uint8_t intr;
246         /** "jmp" instruction */
247         uint8_t jmp;
248         /** Interrupt wrapper address offset */
249         uint32_t offset;
250         /** Next instruction after jump */
251         uint8_t next[0];
252 } __attribute__ (( packed ));
253
254 /** "pushal" instruction */
255 #define PUSHAL_INSN 0x60
256
257 /** "movb" instruction */
258 #define MOVB_INSN 0xb0
259
260 /** "jmp" instruction */
261 #define JMP_INSN 0xe9
262
263 extern void set_interrupt_vector ( unsigned int intr, void *vector );
264
265 #endif /* ASSEMBLY */
266
267 #endif /* LIBRM_H */