Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

librm.h 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #ifndef LIBRM_H
  2. #define LIBRM_H
  3. FILE_LICENCE ( GPL2_OR_LATER );
  4. /* Segment selectors as used in our protected-mode GDTs.
  5. *
  6. * Don't change these unless you really know what you're doing.
  7. */
  8. #define VIRTUAL_CS 0x08
  9. #define VIRTUAL_DS 0x10
  10. #define PHYSICAL_CS 0x18
  11. #define PHYSICAL_DS 0x20
  12. #define REAL_CS 0x28
  13. #define REAL_DS 0x30
  14. #if 0
  15. #define LONG_CS 0x38
  16. #define LONG_DS 0x40
  17. #endif
  18. #ifndef ASSEMBLY
  19. #ifdef UACCESS_LIBRM
  20. #define UACCESS_PREFIX_librm
  21. #else
  22. #define UACCESS_PREFIX_librm __librm_
  23. #endif
  24. /* Variables in librm.S */
  25. extern unsigned long virt_offset;
  26. /**
  27. * Convert physical address to user pointer
  28. *
  29. * @v phys_addr Physical address
  30. * @ret userptr User pointer
  31. */
  32. static inline __always_inline userptr_t
  33. UACCESS_INLINE ( librm, phys_to_user ) ( unsigned long phys_addr ) {
  34. return ( phys_addr - virt_offset );
  35. }
  36. /**
  37. * Convert user buffer to physical address
  38. *
  39. * @v userptr User pointer
  40. * @v offset Offset from user pointer
  41. * @ret phys_addr Physical address
  42. */
  43. static inline __always_inline unsigned long
  44. UACCESS_INLINE ( librm, user_to_phys ) ( userptr_t userptr, off_t offset ) {
  45. return ( userptr + offset + virt_offset );
  46. }
  47. static inline __always_inline userptr_t
  48. UACCESS_INLINE ( librm, virt_to_user ) ( volatile const void *addr ) {
  49. return trivial_virt_to_user ( addr );
  50. }
  51. static inline __always_inline void *
  52. UACCESS_INLINE ( librm, user_to_virt ) ( userptr_t userptr, off_t offset ) {
  53. return trivial_user_to_virt ( userptr, offset );
  54. }
  55. static inline __always_inline userptr_t
  56. UACCESS_INLINE ( librm, userptr_add ) ( userptr_t userptr, off_t offset ) {
  57. return trivial_userptr_add ( userptr, offset );
  58. }
  59. static inline __always_inline void
  60. UACCESS_INLINE ( librm, memcpy_user ) ( userptr_t dest, off_t dest_off,
  61. userptr_t src, off_t src_off,
  62. size_t len ) {
  63. trivial_memcpy_user ( dest, dest_off, src, src_off, len );
  64. }
  65. static inline __always_inline void
  66. UACCESS_INLINE ( librm, memmove_user ) ( userptr_t dest, off_t dest_off,
  67. userptr_t src, off_t src_off,
  68. size_t len ) {
  69. trivial_memmove_user ( dest, dest_off, src, src_off, len );
  70. }
  71. static inline __always_inline void
  72. UACCESS_INLINE ( librm, memset_user ) ( userptr_t buffer, off_t offset,
  73. int c, size_t len ) {
  74. trivial_memset_user ( buffer, offset, c, len );
  75. }
  76. static inline __always_inline size_t
  77. UACCESS_INLINE ( librm, strlen_user ) ( userptr_t buffer, off_t offset ) {
  78. return trivial_strlen_user ( buffer, offset );
  79. }
  80. static inline __always_inline off_t
  81. UACCESS_INLINE ( librm, memchr_user ) ( userptr_t buffer, off_t offset,
  82. int c, size_t len ) {
  83. return trivial_memchr_user ( buffer, offset, c, len );
  84. }
  85. /******************************************************************************
  86. *
  87. * Access to variables in .data16 and .text16
  88. *
  89. */
  90. extern char *data16;
  91. extern char *text16;
  92. #define __data16( variable ) \
  93. __attribute__ (( section ( ".data16" ) )) \
  94. _data16_ ## variable __asm__ ( #variable )
  95. #define __data16_array( variable, array ) \
  96. __attribute__ (( section ( ".data16" ) )) \
  97. _data16_ ## variable array __asm__ ( #variable )
  98. #define __bss16( variable ) \
  99. __attribute__ (( section ( ".bss16" ) )) \
  100. _data16_ ## variable __asm__ ( #variable )
  101. #define __bss16_array( variable, array ) \
  102. __attribute__ (( section ( ".bss16" ) )) \
  103. _data16_ ## variable array __asm__ ( #variable )
  104. #define __text16( variable ) \
  105. __attribute__ (( section ( ".text16.data" ) )) \
  106. _text16_ ## variable __asm__ ( #variable )
  107. #define __text16_array( variable, array ) \
  108. __attribute__ (( section ( ".text16.data" ) )) \
  109. _text16_ ## variable array __asm__ ( #variable )
  110. #define __use_data16( variable ) \
  111. ( * ( ( typeof ( _data16_ ## variable ) * ) \
  112. & ( data16 [ ( size_t ) & ( _data16_ ## variable ) ] ) ) )
  113. #define __use_text16( variable ) \
  114. ( * ( ( typeof ( _text16_ ## variable ) * ) \
  115. & ( text16 [ ( size_t ) & ( _text16_ ## variable ) ] ) ) )
  116. #define __from_data16( pointer ) \
  117. ( ( unsigned int ) \
  118. ( ( ( void * ) (pointer) ) - ( ( void * ) data16 ) ) )
  119. #define __from_text16( pointer ) \
  120. ( ( unsigned int ) \
  121. ( ( ( void * ) (pointer) ) - ( ( void * ) text16 ) ) )
  122. /* Variables in librm.S, present in the normal data segment */
  123. extern uint16_t rm_sp;
  124. extern uint16_t rm_ss;
  125. extern uint16_t __data16 ( rm_cs );
  126. #define rm_cs __use_data16 ( rm_cs )
  127. extern uint16_t __text16 ( rm_ds );
  128. #define rm_ds __use_text16 ( rm_ds )
  129. /* Functions that librm expects to be able to link to. Included here
  130. * so that the compiler will catch prototype mismatches.
  131. */
  132. extern void gateA20_set ( void );
  133. /**
  134. * Convert segment:offset address to user buffer
  135. *
  136. * @v segment Real-mode segment
  137. * @v offset Real-mode offset
  138. * @ret buffer User buffer
  139. */
  140. static inline __always_inline userptr_t
  141. real_to_user ( unsigned int segment, unsigned int offset ) {
  142. return ( phys_to_user ( ( segment << 4 ) + offset ) );
  143. }
  144. extern uint16_t copy_user_to_rm_stack ( userptr_t data, size_t size );
  145. extern void remove_user_from_rm_stack ( userptr_t data, size_t size );
  146. /* TEXT16_CODE: declare a fragment of code that resides in .text16 */
  147. #define TEXT16_CODE( asm_code_str ) \
  148. ".section \".text16\", \"ax\", @progbits\n\t" \
  149. ".code16\n\t" \
  150. asm_code_str "\n\t" \
  151. ".code32\n\t" \
  152. ".previous\n\t"
  153. /* REAL_CODE: declare a fragment of code that executes in real mode */
  154. #define REAL_CODE( asm_code_str ) \
  155. "pushl $1f\n\t" \
  156. "call real_call\n\t" \
  157. "addl $4, %%esp\n\t" \
  158. TEXT16_CODE ( "\n1:\n\t" \
  159. asm_code_str \
  160. "\n\t" \
  161. "ret\n\t" )
  162. /* PHYS_CODE: declare a fragment of code that executes in flat physical mode */
  163. #define PHYS_CODE( asm_code_str ) \
  164. "call _virt_to_phys\n\t" \
  165. asm_code_str \
  166. "call _phys_to_virt\n\t"
  167. #endif /* ASSEMBLY */
  168. #endif /* LIBRM_H */