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 __data16_array( variable, array ) variable array
16 #define __text16( variable ) variable
17 #define __text16_array( variable,array ) variable array
18 #define __use_data16( variable ) variable
19 #define __use_text16( variable ) variable
20 #define __from_data16( variable ) variable
21 #define __from_text16( variable ) variable
23 /* Real-mode data and code segments */
24 static inline __attribute__ (( always_inline )) unsigned int _rm_cs ( void ) {
26 __asm__ __volatile__ ( "movw %%cs, %w0" : "=r" ( cs ) );
30 static inline __attribute__ (( always_inline )) unsigned int _rm_ds ( void ) {
32 __asm__ __volatile__ ( "movw %%ds, %w0" : "=r" ( ds ) );
36 #define rm_cs ( _rm_cs() )
37 #define rm_ds ( _rm_ds() )
39 /* Copy to/from base memory */
41 static inline void copy_to_real_libkir ( uint16_t dest_seg, uint16_t dest_off,
42 const void *src, size_t n ) {
43 __asm__ __volatile__ ( "movw %4, %%es\n\t"
46 "pushw %%ds\n\t" /* restore %es */
48 : "=S" ( src ), "=D" ( dest_off ),
49 "=c" ( n ) /* clobbered */
50 : "S" ( src ), "r" ( dest_seg ),
51 "D" ( dest_off ), "c" ( n )
55 static inline void copy_from_real_libkir ( void *dest,
56 uint16_t src_seg, uint16_t src_off,
58 __asm__ __volatile__ ( "movw %%ax, %%ds\n\t"
61 "pushw %%es\n\t" /* restore %ds */
63 : "=S" ( src_off ), "=D" ( dest ),
64 "=c" ( n ) /* clobbered */
65 : "a" ( src_seg ), "S" ( src_off ),
66 "D" ( dest ), "c" ( n )
69 #define copy_to_real copy_to_real_libkir
70 #define copy_from_real copy_from_real_libkir
73 * Transfer individual values to/from base memory. There may well be
74 * a neater way to do this. We have two versions: one for constant
75 * offsets (where the mov instruction must be of the form "mov
76 * %es:123, %xx") and one for non-constant offsets (where the mov
77 * instruction must be of the form "mov %es:(%xx), %yx". If it's
78 * possible to incorporate both forms into one __asm__ instruction, I
79 * don't know how to do it.
81 * Ideally, the mov instruction should be "mov%z0"; the "%z0" is meant
82 * to expand to either "b", "w" or "l" depending on the size of
83 * operand 0. This would remove the (minor) ambiguity in the mov
84 * instruction. However, gcc on at least my system barfs with an
85 * "internal compiler error" when confronted with %z0.
89 #define put_real_kir_const_off( var, seg, off ) \
90 __asm__ ( "movw %w1, %%es\n\t" \
91 "mov %0, %%es:%c2\n\t" \
92 "pushw %%ds\n\t" /* restore %es */ \
95 : "r,r" ( var ), "rm,rm" ( seg ), "i,!r" ( off ) \
98 #define put_real_kir_nonconst_off( var, seg, off ) \
99 __asm__ ( "movw %w1, %%es\n\t" \
100 "mov %0, %%es:(%2)\n\t" \
101 "pushw %%ds\n\t" /* restore %es */ \
104 : "r" ( var ), "rm" ( seg ), "r" ( off ) \
107 #define put_real_kir( var, seg, off ) \
109 if ( __builtin_constant_p ( off ) ) \
110 put_real_kir_const_off ( var, seg, off ); \
112 put_real_kir_nonconst_off ( var, seg, off ); \
115 #define get_real_kir_const_off( var, seg, off ) \
116 __asm__ ( "movw %w1, %%es\n\t" \
117 "mov %%es:%c2, %0\n\t" \
118 "pushw %%ds\n\t" /* restore %es */ \
121 : "rm,rm" ( seg ), "i,!r" ( off ) \
124 #define get_real_kir_nonconst_off( var, seg, off ) \
125 __asm__ ( "movw %w1, %%es\n\t" \
126 "mov %%es:(%2), %0\n\t" \
127 "pushw %%ds\n\t" /* restore %es */ \
130 : "rm" ( seg ), "r" ( off ) \
133 #define get_real_kir( var, seg, off ) \
135 if ( __builtin_constant_p ( off ) ) \
136 get_real_kir_const_off ( var, seg, off ); \
138 get_real_kir_nonconst_off ( var, seg, off ); \
141 #define put_real put_real_kir
142 #define get_real get_real_kir
145 * A pointer to a user buffer
147 * This is actually a struct segoff, but encoded as a uint32_t to
148 * ensure that gcc passes it around efficiently.
150 typedef uint32_t userptr_t;
153 * Copy data to user buffer
155 * @v buffer User buffer
156 * @v offset Offset within user buffer
160 static inline __attribute__ (( always_inline )) void
161 copy_to_user ( userptr_t buffer, off_t offset, const void *src, size_t len ) {
162 copy_to_real ( ( buffer >> 16 ), ( ( buffer & 0xffff ) + offset ),
167 * Copy data from user buffer
169 * @v dest Destination
170 * @v buffer User buffer
171 * @v offset Offset within user buffer
174 static inline __attribute__ (( always_inline )) void
175 copy_from_user ( void *dest, userptr_t buffer, off_t offset, size_t len ) {
176 copy_from_real ( dest, ( buffer >> 16 ),
177 ( ( buffer & 0xffff ) + offset ), len );
181 * Convert segment:offset address to user buffer
183 * @v segment Real-mode segment
184 * @v offset Real-mode offset
185 * @ret buffer User buffer
187 static inline __attribute__ (( always_inline )) userptr_t
188 real_to_user ( unsigned int segment, unsigned int offset ) {
189 return ( ( segment << 16 ) | offset );
193 * Convert virtual address to user buffer
195 * @v virtual Virtual address
196 * @ret buffer User buffer
198 * This constructs a user buffer from an ordinary pointer. Use it
199 * when you need to pass a pointer to an internal buffer to a function
200 * that expects a @c userptr_t.
202 static inline __attribute__ (( always_inline )) userptr_t
203 virt_to_user ( void * virtual ) {
204 return real_to_user ( rm_ds, ( intptr_t ) virtual );
207 /* Place/remove parameter on real-mode stack in a way that's
208 * compatible with libkir
210 #define BASEMEM_PARAMETER_INIT_LIBKIR( param ) \
211 ( ( uint16_t ) ( ( uint32_t ) & ( param ) ) )
212 #define BASEMEM_PARAMETER_DONE_LIBKIR( param )
213 #define BASEMEM_PARAMETER_INIT BASEMEM_PARAMETER_INIT_LIBKIR
214 #define BASEMEM_PARAMETER_DONE BASEMEM_PARAMETER_DONE_LIBKIR
216 /* REAL_CODE: declare a fragment of code that executes in real mode */
217 #define REAL_CODE( asm_code_str ) \
230 /* REAL_EXEC: execute some inline assembly code in a way that matches
231 * the interface of librm
233 #define OUT_CONSTRAINTS(...) __VA_ARGS__
234 #define IN_CONSTRAINTS(...) __VA_ARGS__
235 #define CLOBBER(...) __VA_ARGS__
236 #define REAL_EXEC( name, asm_code_str, num_out_constraints, \
237 out_constraints, in_constraints, clobber ) do { \
238 __asm__ __volatile__ ( \
239 REAL_CODE ( asm_code_str ) \
240 : out_constraints : in_constraints : clobber ); \
243 #endif /* ASSEMBLY */
245 #endif /* LIBKIR_H */