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