123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- #ifndef LIBKIR_H
- #define LIBKIR_H
-
- #include "realmode.h"
-
- #ifndef ASSEMBLY
-
- /*
- * Full API documentation for these functions is in realmode.h.
- *
- */
-
- /* Copy to/from base memory */
-
- static inline void copy_to_real_libkir ( uint16_t dest_seg, uint16_t dest_off,
- void *src, size_t n ) {
- __asm__ ( "movw %4, %%es\n\t"
- "cld\n\t"
- "rep movsb\n\t"
- "pushw %%ds\n\t" /* restore %es */
- "popw %%es\n\t"
- : "=S" ( src ), "=D" ( dest_off ), "=c" ( n ) /* clobbered */
- : "S" ( src ), "r" ( dest_seg ), "D" ( dest_off ), "c" ( n )
- : "memory"
- );
- }
-
- static inline void copy_from_real_libkir ( void *dest,
- uint16_t src_seg, uint16_t src_off,
- size_t n ) {
- __asm__ ( "movw %%ax, %%ds\n\t"
- "cld\n\t"
- "rep movsb\n\t"
- "pushw %%es\n\t" /* restore %ds */
- "popw %%ds\n\t"
- : "=S" ( src_off ), "=D" ( dest ), "=c" ( n ) /* clobbered */
- : "a" ( src_seg ), "S" ( src_off ), "D" ( dest ), "c" ( n )
- : "memory"
- );
- }
- #define copy_to_real copy_to_real_libkir
- #define copy_from_real copy_from_real_libkir
-
- /*
- * Transfer individual values to/from base memory. There may well be
- * a neater way to do this. We have two versions: one for constant
- * offsets (where the mov instruction must be of the form "mov
- * %es:123, %xx") and one for non-constant offsets (where the mov
- * instruction must be of the form "mov %es:(%xx), %yx". If it's
- * possible to incorporate both forms into one __asm__ instruction, I
- * don't know how to do it.
- *
- * Ideally, the mov instruction should be "mov%z0"; the "%z0" is meant
- * to expand to either "b", "w" or "l" depending on the size of
- * operand 0. This would remove the (minor) ambiguity in the mov
- * instruction. However, gcc on at least my system barfs with an
- * "internal compiler error" when confronted with %z0.
- *
- */
-
- #define put_real_kir_const_off( var, seg, off ) \
- __asm__ ( "movw %w1, %%es\n\t" \
- "mov %0, %%es:%c2\n\t" \
- "pushw %%ds\n\t" /* restore %es */ \
- "popw %%es\n\t" \
- : \
- : "r,r" ( var ), "rm,rm" ( seg ), "i,!r" ( off ) \
- )
-
- #define put_real_kir_nonconst_off( var, seg, off ) \
- __asm__ ( "movw %w1, %%es\n\t" \
- "mov %0, %%es:(%2)\n\t" \
- "pushw %%ds\n\t" /* restore %es */ \
- "popw %%es\n\t" \
- : \
- : "r" ( var ), "rm" ( seg ), "r" ( off ) \
- )
-
- #define put_real_kir( var, seg, off ) \
- do { \
- if ( __builtin_constant_p ( off ) ) \
- put_real_kir_const_off ( var, seg, off ); \
- else \
- put_real_kir_nonconst_off ( var, seg, off ); \
- } while ( 0 )
-
- #define get_real_kir_const_off( var, seg, off ) \
- __asm__ ( "movw %w1, %%es\n\t" \
- "mov %%es:%c2, %0\n\t" \
- "pushw %%ds\n\t" /* restore %es */ \
- "popw %%es\n\t" \
- : "=r,r" ( var ) \
- : "rm,rm" ( seg ), "i,!r" ( off ) \
- )
-
- #define get_real_kir_nonconst_off( var, seg, off ) \
- __asm__ ( "movw %w1, %%es\n\t" \
- "mov %%es:(%2), %0\n\t" \
- "pushw %%ds\n\t" /* restore %es */ \
- "popw %%es\n\t" \
- : "=r" ( var ) \
- : "rm" ( seg ), "r" ( off ) \
- )
-
- #define get_real_kir( var, seg, off ) \
- do { \
- if ( __builtin_constant_p ( off ) ) \
- get_real_kir_const_off ( var, seg, off ); \
- else \
- get_real_kir_nonconst_off ( var, seg, off ); \
- } while ( 0 )
-
- #define put_real put_real_kir
- #define get_real get_real_kir
-
- /* Place/remove parameter on real-mode stack in a way that's
- * compatible with libkir
- */
- #define BASEMEM_PARAMETER_INIT_LIBKIR( param ) \
- ( ( uint16_t ) ( ( uint32_t ) & ( param ) ) )
- #define BASEMEM_PARAMETER_DONE_LIBKIR( param )
- #define BASEMEM_PARAMETER_INIT BASEMEM_PARAMETER_INIT_LIBKIR
- #define BASEMEM_PARAMETER_DONE BASEMEM_PARAMETER_DONE_LIBKIR
-
- /* REAL_CALL: call an external real-mode routine */
- #define OUT_CONSTRAINTS(...) __VA_ARGS__
- #define IN_CONSTRAINTS(...) "m" ( __routine ), ## __VA_ARGS__
- #define CLOBBER(...) __VA_ARGS__
- #define REAL_CALL( routine, num_out_constraints, out_constraints, \
- in_constraints, clobber ) \
- do { \
- segoff_t __routine = routine; \
- __asm__ __volatile__ ( \
- "pushl %" #num_out_constraints "\n\t" \
- ".code16\n\t" \
- "pushw %%gs\n\t" /* preserve segs */ \
- "pushw %%fs\n\t" \
- "pushw %%es\n\t" \
- "pushw %%ds\n\t" \
- "pushw %%cs\n\t" /* lcall to routine */ \
- "call 1f\n\t" \
- "jmp 2f\n\t" \
- "\n1:\n\t" \
- "addr32 pushl 12(%%esp)\n\t" \
- "lret\n\t" \
- "\n2:\n\t" \
- "popw %%ds\n\t" /* restore segs */ \
- "popw %%es\n\t" \
- "popw %%fs\n\t" \
- "popw %%gs\n\t" \
- "addw $4, %%sp\n\t" \
- ".code16gcc\n\t" \
- : out_constraints : in_constraints : clobber \
- ); \
- } while ( 0 )
-
- /* REAL_EXEC: execute some inline assembly code in a way that matches
- * the interface of librm
- */
-
- #define IN_CONSTRAINTS_NO_ROUTINE( routine, ... ) __VA_ARGS__
- #define REAL_EXEC( name, asm_code_str, num_out_constraints, out_constraints, \
- in_constraints, clobber ) \
- __asm__ __volatile__ ( \
- ".code16\n\t" \
- "pushw %%gs\n\t" \
- "pushw %%fs\n\t" \
- "pushw %%es\n\t" \
- "pushw %%ds\n\t" \
- "\n" #name ":\n\t" \
- asm_code_str \
- "popw %%ds\n\t" \
- "popw %%es\n\t" \
- "popw %%fs\n\t" \
- "popw %%gs\n\t" \
- ".code16gcc\n\t" \
- : out_constraints \
- : IN_CONSTRAINTS_NO_ROUTINE ( in_constraints ) \
- : clobber \
- );
-
- #endif /* ASSEMBLY */
-
- #endif /* LIBKIR_H */
|