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