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