9 * Full API documentation for these functions is in realmode.h.
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
19 /* Copy to/from base memory */
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"
26 "pushw %%ds\n\t" /* restore %es */
28 : "=S" ( src ), "=D" ( dest_off ),
29 "=c" ( n ) /* clobbered */
30 : "S" ( src ), "r" ( dest_seg ),
31 "D" ( dest_off ), "c" ( n )
35 static inline void copy_from_real_libkir ( void *dest,
36 uint16_t src_seg, uint16_t src_off,
38 __asm__ __volatile__ ( "movw %%ax, %%ds\n\t"
41 "pushw %%es\n\t" /* restore %ds */
43 : "=S" ( src_off ), "=D" ( dest ),
44 "=c" ( n ) /* clobbered */
45 : "a" ( src_seg ), "S" ( src_off ),
46 "D" ( dest ), "c" ( n )
49 #define copy_to_real copy_to_real_libkir
50 #define copy_from_real copy_from_real_libkir
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.
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.
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 */ \
75 : "r,r" ( var ), "rm,rm" ( seg ), "i,!r" ( off ) \
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 */ \
84 : "r" ( var ), "rm" ( seg ), "r" ( off ) \
87 #define put_real_kir( var, seg, off ) \
89 if ( __builtin_constant_p ( off ) ) \
90 put_real_kir_const_off ( var, seg, off ); \
92 put_real_kir_nonconst_off ( var, seg, off ); \
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 */ \
101 : "rm,rm" ( seg ), "i,!r" ( off ) \
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 */ \
110 : "rm" ( seg ), "r" ( off ) \
113 #define get_real_kir( var, seg, off ) \
115 if ( __builtin_constant_p ( off ) ) \
116 get_real_kir_const_off ( var, seg, off ); \
118 get_real_kir_nonconst_off ( var, seg, off ); \
121 #define put_real put_real_kir
122 #define get_real get_real_kir
124 /* Place/remove parameter on real-mode stack in a way that's
125 * compatible with libkir
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
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.
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
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" \
152 asm_code_str "\n\t" \
155 ".equ " #name "_size, " #name "_end - " #name "\n\t" \
160 #define FRAGMENT_SIZE( fragment ) ( (size_t) fragment ## _size )
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 ) \
169 segoff_t __routine = routine; \
170 __asm__ __volatile__ ( \
171 "pushl %" #num_out_constraints "\n\t" \
173 "pushw %%gs\n\t" /* preserve segs */ \
177 "pushw %%cs\n\t" /* lcall to routine */ \
181 "addr32 pushl 12(%%esp)\n\t" \
184 "popw %%ds\n\t" /* restore segs */ \
188 "addw $4, %%sp\n\t" \
190 : out_constraints : in_constraints : clobber \
194 /* REAL_EXEC: execute some inline assembly code in a way that matches
195 * the interface of librm
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__ ( \
215 : IN_CONSTRAINTS_NO_ROUTINE ( in_constraints ) \
219 #endif /* ASSEMBLY */
221 #endif /* LIBKIR_H */