Add __{text,data}16_array, since there's no way I can see to make the
[people/xl0/gpxe.git] / src / arch / i386 / include / libkir.h
1 #ifndef LIBKIR_H
2 #define LIBKIR_H
3
4 #include "realmode.h"
5
6 #ifndef ASSEMBLY
7
8 /*
9  * Full API documentation for these functions is in realmode.h.
10  *
11  */
12
13 /* Access to variables in .data16 and .text16 in a way compatible with librm */
14 #define __data16( variable ) variable
15 #define __data16_array( variable, array ) variable array
16 #define __text16( variable ) variable
17 #define __text16_array( variable,array ) variable array
18 #define __use_data16( variable ) variable
19 #define __use_text16( variable ) variable
20 #define __from_data16( variable ) variable
21 #define __from_text16( variable ) variable
22
23 /* Copy to/from base memory */
24
25 static inline void copy_to_real_libkir ( uint16_t dest_seg, uint16_t dest_off,
26                                          void *src, size_t n ) {
27         __asm__ __volatile__ ( "movw %4, %%es\n\t"
28                                "cld\n\t"
29                                "rep movsb\n\t"
30                                "pushw %%ds\n\t" /* restore %es */
31                                "popw %%es\n\t"
32                                : "=S" ( src ), "=D" ( dest_off ),
33                                  "=c" ( n ) /* clobbered */
34                                : "S" ( src ), "r" ( dest_seg ),
35                                  "D" ( dest_off ), "c" ( n )
36                                : "memory" );
37 }
38
39 static inline void copy_from_real_libkir ( void *dest,
40                                            uint16_t src_seg, uint16_t src_off,
41                                            size_t n ) {
42         __asm__ __volatile__ ( "movw %%ax, %%ds\n\t"
43                                "cld\n\t"
44                                "rep movsb\n\t"
45                                "pushw %%es\n\t" /* restore %ds */
46                                "popw %%ds\n\t"
47                                : "=S" ( src_off ), "=D" ( dest ),
48                                  "=c" ( n ) /* clobbered */
49                                : "a" ( src_seg ), "S" ( src_off ),
50                                  "D" ( dest ), "c" ( n )
51                                : "memory" );
52 }
53 #define copy_to_real copy_to_real_libkir
54 #define copy_from_real copy_from_real_libkir
55
56 /*
57  * Transfer individual values to/from base memory.  There may well be
58  * a neater way to do this.  We have two versions: one for constant
59  * offsets (where the mov instruction must be of the form "mov
60  * %es:123, %xx") and one for non-constant offsets (where the mov
61  * instruction must be of the form "mov %es:(%xx), %yx".  If it's
62  * possible to incorporate both forms into one __asm__ instruction, I
63  * don't know how to do it.
64  *
65  * Ideally, the mov instruction should be "mov%z0"; the "%z0" is meant
66  * to expand to either "b", "w" or "l" depending on the size of
67  * operand 0.  This would remove the (minor) ambiguity in the mov
68  * instruction.  However, gcc on at least my system barfs with an
69  * "internal compiler error" when confronted with %z0.
70  *
71  */
72
73 #define put_real_kir_const_off( var, seg, off )                              \
74         __asm__ ( "movw %w1, %%es\n\t"                                       \
75                   "mov %0, %%es:%c2\n\t"                                     \
76                   "pushw %%ds\n\t" /* restore %es */                         \
77                   "popw %%es\n\t"                                            \
78                   :                                                          \
79                   : "r,r" ( var ), "rm,rm" ( seg ), "i,!r" ( off )           \
80                   )
81
82 #define put_real_kir_nonconst_off( var, seg, off )                           \
83         __asm__ ( "movw %w1, %%es\n\t"                                       \
84                   "mov %0, %%es:(%2)\n\t"                                    \
85                   "pushw %%ds\n\t" /* restore %es */                         \
86                   "popw %%es\n\t"                                            \
87                   :                                                          \
88                   : "r" ( var ), "rm" ( seg ), "r" ( off )                   \
89                   )
90
91 #define put_real_kir( var, seg, off )                                        \
92         do {                                                                 \
93           if ( __builtin_constant_p ( off ) )                                \
94                   put_real_kir_const_off ( var, seg, off );                  \
95           else                                                               \
96                   put_real_kir_nonconst_off ( var, seg, off );               \
97         } while ( 0 )
98
99 #define get_real_kir_const_off( var, seg, off )                              \
100         __asm__ ( "movw %w1, %%es\n\t"                                       \
101                   "mov %%es:%c2, %0\n\t"                                     \
102                   "pushw %%ds\n\t" /* restore %es */                         \
103                   "popw %%es\n\t"                                            \
104                   : "=r,r" ( var )                                           \
105                   : "rm,rm" ( seg ), "i,!r" ( off )                          \
106                   )
107
108 #define get_real_kir_nonconst_off( var, seg, off )                           \
109         __asm__ ( "movw %w1, %%es\n\t"                                       \
110                   "mov %%es:(%2), %0\n\t"                                    \
111                   "pushw %%ds\n\t" /* restore %es */                         \
112                   "popw %%es\n\t"                                            \
113                   : "=r" ( var )                                             \
114                   : "rm" ( seg ), "r" ( off )                                \
115                   )
116
117 #define get_real_kir( var, seg, off )                                        \
118         do {                                                                 \
119           if ( __builtin_constant_p ( off ) )                                \
120                   get_real_kir_const_off ( var, seg, off );                  \
121           else                                                               \
122                   get_real_kir_nonconst_off ( var, seg, off );               \
123         } while ( 0 )
124
125 #define put_real put_real_kir
126 #define get_real get_real_kir
127
128 /* Place/remove parameter on real-mode stack in a way that's
129  * compatible with libkir
130  */
131 #define BASEMEM_PARAMETER_INIT_LIBKIR( param ) \
132         ( ( uint16_t ) ( ( uint32_t ) & ( param ) ) )
133 #define BASEMEM_PARAMETER_DONE_LIBKIR( param )
134 #define BASEMEM_PARAMETER_INIT BASEMEM_PARAMETER_INIT_LIBKIR
135 #define BASEMEM_PARAMETER_DONE BASEMEM_PARAMETER_DONE_LIBKIR
136
137 /* REAL_FRAGMENT: Declare and define a real-mode code fragment in
138  * .text16.  We don't need this for REAL_EXEC, since we can just
139  * execute our real-mode code inline, but it's handy in case someone
140  * genuinely wants to create a block of code that can be copied to a
141  * specific location and then executed.
142  *
143  * Note that we put the code in the data segment, since otherwise we
144  * can't (easily) access it in order to copy it to its target
145  * location.  We should never be calling any REAL_FRAGMENT routines
146  * directly anyway.
147  */
148 #define REAL_FRAGMENT( name, asm_code_str )                             \
149         extern void name ( void );                                      \
150         extern char name ## _size[];                                    \
151         __asm__ __volatile__ (                                          \
152                 ".section \".data.text16\"\n\t"                         \
153                 ".code16\n\t"                                           \
154                 ".arch i386\n\t"                                        \
155                 #name ":\n\t"                                           \
156                 asm_code_str "\n\t"                                     \
157                 "lret\n\t"                                              \
158                 #name "_end:\n\t"                                       \
159                 ".equ " #name "_size, " #name "_end - " #name "\n\t"    \
160                 ".code16gcc\n\t"                                        \
161                 ".previous\n\t"                                         \
162                 : :                                                     \
163         )
164 #define FRAGMENT_SIZE( fragment ) ( (size_t) fragment ## _size )
165
166 /* REAL_CALL: call an external real-mode routine */
167 #define OUT_CONSTRAINTS(...) __VA_ARGS__
168 #define IN_CONSTRAINTS(...) "m" ( __routine ), ## __VA_ARGS__
169 #define CLOBBER(...) __VA_ARGS__
170 #define REAL_CALL( routine, num_out_constraints, out_constraints,            \
171                    in_constraints, clobber )                                 \
172         do {                                                                 \
173                 segoff_t __routine = routine;                                \
174                 __asm__ __volatile__ (                                       \
175                         "pushl %" #num_out_constraints "\n\t"                \
176                         ".code16\n\t"                                        \
177                         "pushw %%gs\n\t"        /* preserve segs */          \
178                         "pushw %%fs\n\t"                                     \
179                         "pushw %%es\n\t"                                     \
180                         "pushw %%ds\n\t"                                     \
181                         "pushw %%cs\n\t"        /* lcall to routine */       \
182                         "call 1f\n\t"                                        \
183                         "jmp 2f\n\t"                                         \
184                         "\n1:\n\t"                                           \
185                         "addr32 pushl 12(%%esp)\n\t"                         \
186                         "lret\n\t"                                           \
187                         "\n2:\n\t"                                           \
188                         "popw %%ds\n\t"         /* restore segs */           \
189                         "popw %%es\n\t"                                      \
190                         "popw %%fs\n\t"                                      \
191                         "popw %%gs\n\t"                                      \
192                         "addw $4, %%sp\n\t"                                  \
193                         ".code16gcc\n\t"                                     \
194                         : out_constraints : in_constraints : clobber         \
195                 );                                                           \
196         } while ( 0 )
197
198 /* REAL_EXEC: execute some inline assembly code in a way that matches
199  * the interface of librm
200  */
201
202 #define IN_CONSTRAINTS_NO_ROUTINE( routine, ... ) __VA_ARGS__
203 #define REAL_EXEC( name, asm_code_str, num_out_constraints, out_constraints, \
204                    in_constraints, clobber )                                 \
205         __asm__ __volatile__ (                                               \
206                 ".code16\n\t"                                                \
207                 "pushw %%gs\n\t"                                             \
208                 "pushw %%fs\n\t"                                             \
209                 "pushw %%es\n\t"                                             \
210                 "pushw %%ds\n\t"                                             \
211                 "\n" #name ":\n\t"                                           \
212                 asm_code_str                                                 \
213                 "popw %%ds\n\t"                                              \
214                 "popw %%es\n\t"                                              \
215                 "popw %%fs\n\t"                                              \
216                 "popw %%gs\n\t"                                              \
217                 ".code16gcc\n\t"                                             \
218                 : out_constraints                                            \
219                 : IN_CONSTRAINTS_NO_ROUTINE ( in_constraints )               \
220                 : clobber                                                    \
221                 );
222
223 #endif /* ASSEMBLY */
224
225 #endif /* LIBKIR_H */