Merged mcb30-realmode-redesign back to HEAD
[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__ ( "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 ), "=c" ( n ) /* clobbered */
23                   : "S" ( src ), "r" ( dest_seg ), "D" ( dest_off ), "c" ( n )
24                   : "memory"
25                   );
26 }
27
28 static inline void copy_from_real_libkir ( void *dest,
29                                            uint16_t src_seg, uint16_t src_off,
30                                            size_t n ) {
31         __asm__ ( "movw %%ax, %%ds\n\t"
32                   "cld\n\t"
33                   "rep movsb\n\t"
34                   "pushw %%es\n\t" /* restore %ds */
35                   "popw %%ds\n\t"
36                   : "=S" ( src_off ), "=D" ( dest ), "=c" ( n ) /* clobbered */
37                   : "a" ( src_seg ), "S" ( src_off ), "D" ( dest ), "c" ( n )
38                   : "memory"
39                   );
40 }
41 #define copy_to_real copy_to_real_libkir
42 #define copy_from_real copy_from_real_libkir
43
44 /*
45  * Transfer individual values to/from base memory.  There may well be
46  * a neater way to do this.  We have two versions: one for constant
47  * offsets (where the mov instruction must be of the form "mov
48  * %es:123, %xx") and one for non-constant offsets (where the mov
49  * instruction must be of the form "mov %es:(%xx), %yx".  If it's
50  * possible to incorporate both forms into one __asm__ instruction, I
51  * don't know how to do it.
52  *
53  * Ideally, the mov instruction should be "mov%z0"; the "%z0" is meant
54  * to expand to either "b", "w" or "l" depending on the size of
55  * operand 0.  This would remove the (minor) ambiguity in the mov
56  * instruction.  However, gcc on at least my system barfs with an
57  * "internal compiler error" when confronted with %z0.
58  *
59  */
60
61 #define put_real_kir_const_off( var, seg, off )                              \
62         __asm__ ( "movw %w1, %%es\n\t"                                       \
63                   "mov %0, %%es:%c2\n\t"                                     \
64                   "pushw %%ds\n\t" /* restore %es */                         \
65                   "popw %%es\n\t"                                            \
66                   :                                                          \
67                   : "r,r" ( var ), "rm,rm" ( seg ), "i,!r" ( off )           \
68                   )
69
70 #define put_real_kir_nonconst_off( var, seg, off )                           \
71         __asm__ ( "movw %w1, %%es\n\t"                                       \
72                   "mov %0, %%es:(%2)\n\t"                                    \
73                   "pushw %%ds\n\t" /* restore %es */                         \
74                   "popw %%es\n\t"                                            \
75                   :                                                          \
76                   : "r" ( var ), "rm" ( seg ), "r" ( off )                   \
77                   )
78
79 #define put_real_kir( var, seg, off )                                        \
80         do {                                                                 \
81           if ( __builtin_constant_p ( off ) )                                \
82                   put_real_kir_const_off ( var, seg, off );                  \
83           else                                                               \
84                   put_real_kir_nonconst_off ( var, seg, off );               \
85         } while ( 0 )
86
87 #define get_real_kir_const_off( var, seg, off )                              \
88         __asm__ ( "movw %w1, %%es\n\t"                                       \
89                   "mov %%es:%c2, %0\n\t"                                     \
90                   "pushw %%ds\n\t" /* restore %es */                         \
91                   "popw %%es\n\t"                                            \
92                   : "=r,r" ( var )                                           \
93                   : "rm,rm" ( seg ), "i,!r" ( off )                          \
94                   )
95
96 #define get_real_kir_nonconst_off( var, seg, off )                           \
97         __asm__ ( "movw %w1, %%es\n\t"                                       \
98                   "mov %%es:(%2), %0\n\t"                                    \
99                   "pushw %%ds\n\t" /* restore %es */                         \
100                   "popw %%es\n\t"                                            \
101                   : "=r" ( var )                                             \
102                   : "rm" ( seg ), "r" ( off )                                \
103                   )
104
105 #define get_real_kir( var, seg, off )                                        \
106         do {                                                                 \
107           if ( __builtin_constant_p ( off ) )                                \
108                   get_real_kir_const_off ( var, seg, off );                  \
109           else                                                               \
110                   get_real_kir_nonconst_off ( var, seg, off );               \
111         } while ( 0 )
112
113 #define put_real put_real_kir
114 #define get_real get_real_kir
115
116 /* Place/remove parameter on real-mode stack in a way that's
117  * compatible with libkir
118  */
119 #define BASEMEM_PARAMETER_INIT_LIBKIR( param ) \
120         ( ( uint16_t ) ( ( uint32_t ) & ( param ) ) )
121 #define BASEMEM_PARAMETER_DONE_LIBKIR( param )
122 #define BASEMEM_PARAMETER_INIT BASEMEM_PARAMETER_INIT_LIBKIR
123 #define BASEMEM_PARAMETER_DONE BASEMEM_PARAMETER_DONE_LIBKIR
124
125 /* REAL_CALL: call an external real-mode routine */
126 #define OUT_CONSTRAINTS(...) __VA_ARGS__
127 #define IN_CONSTRAINTS(...) "m" ( __routine ), ## __VA_ARGS__
128 #define CLOBBER(...) __VA_ARGS__
129 #define REAL_CALL( routine, num_out_constraints, out_constraints,            \
130                    in_constraints, clobber )                                 \
131         do {                                                                 \
132                 segoff_t __routine = routine;                                \
133                 __asm__ __volatile__ (                                       \
134                         "pushl %" #num_out_constraints "\n\t"                \
135                         ".code16\n\t"                                        \
136                         "pushw %%gs\n\t"        /* preserve segs */          \
137                         "pushw %%fs\n\t"                                     \
138                         "pushw %%es\n\t"                                     \
139                         "pushw %%ds\n\t"                                     \
140                         "pushw %%cs\n\t"        /* lcall to routine */       \
141                         "call 1f\n\t"                                        \
142                         "jmp 2f\n\t"                                         \
143                         "\n1:\n\t"                                           \
144                         "addr32 pushl 12(%%esp)\n\t"                         \
145                         "lret\n\t"                                           \
146                         "\n2:\n\t"                                           \
147                         "popw %%ds\n\t"         /* restore segs */           \
148                         "popw %%es\n\t"                                      \
149                         "popw %%fs\n\t"                                      \
150                         "popw %%gs\n\t"                                      \
151                         "addw $4, %%sp\n\t"                                  \
152                         ".code16gcc\n\t"                                     \
153                         : out_constraints : in_constraints : clobber         \
154                 );                                                           \
155         } while ( 0 )
156
157 /* REAL_EXEC: execute some inline assembly code in a way that matches
158  * the interface of librm
159  */
160
161 #define IN_CONSTRAINTS_NO_ROUTINE( routine, ... ) __VA_ARGS__
162 #define REAL_EXEC( name, asm_code_str, num_out_constraints, out_constraints, \
163                    in_constraints, clobber )                                 \
164         __asm__ __volatile__ (                                               \
165                 ".code16\n\t"                                                \
166                 "pushw %%gs\n\t"                                             \
167                 "pushw %%fs\n\t"                                             \
168                 "pushw %%es\n\t"                                             \
169                 "pushw %%ds\n\t"                                             \
170                 "\n" #name ":\n\t"                                           \
171                 asm_code_str                                                 \
172                 "popw %%ds\n\t"                                              \
173                 "popw %%es\n\t"                                              \
174                 "popw %%fs\n\t"                                              \
175                 "popw %%gs\n\t"                                              \
176                 ".code16gcc\n\t"                                             \
177                 : out_constraints                                            \
178                 : IN_CONSTRAINTS_NO_ROUTINE ( in_constraints )               \
179                 : clobber                                                    \
180                 );
181
182 #endif /* ASSEMBLY */
183
184 #endif /* LIBKIR_H */