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