You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

librm.h 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 off_t
  60. UACCESS_INLINE ( librm, userptr_sub ) ( userptr_t userptr,
  61. userptr_t subtrahend ) {
  62. return trivial_userptr_sub ( userptr, subtrahend );
  63. }
  64. static inline __always_inline void
  65. UACCESS_INLINE ( librm, memcpy_user ) ( userptr_t dest, off_t dest_off,
  66. userptr_t src, off_t src_off,
  67. size_t len ) {
  68. trivial_memcpy_user ( dest, dest_off, src, src_off, len );
  69. }
  70. static inline __always_inline void
  71. UACCESS_INLINE ( librm, memmove_user ) ( userptr_t dest, off_t dest_off,
  72. userptr_t src, off_t src_off,
  73. size_t len ) {
  74. trivial_memmove_user ( dest, dest_off, src, src_off, len );
  75. }
  76. static inline __always_inline void
  77. UACCESS_INLINE ( librm, memset_user ) ( userptr_t buffer, off_t offset,
  78. int c, size_t len ) {
  79. trivial_memset_user ( buffer, offset, c, len );
  80. }
  81. static inline __always_inline size_t
  82. UACCESS_INLINE ( librm, strlen_user ) ( userptr_t buffer, off_t offset ) {
  83. return trivial_strlen_user ( buffer, offset );
  84. }
  85. static inline __always_inline off_t
  86. UACCESS_INLINE ( librm, memchr_user ) ( userptr_t buffer, off_t offset,
  87. int c, size_t len ) {
  88. return trivial_memchr_user ( buffer, offset, c, len );
  89. }
  90. /******************************************************************************
  91. *
  92. * Access to variables in .data16 and .text16
  93. *
  94. */
  95. extern char *data16;
  96. extern char *text16;
  97. #define __data16( variable ) \
  98. __attribute__ (( section ( ".data16" ) )) \
  99. _data16_ ## variable __asm__ ( #variable )
  100. #define __data16_array( variable, array ) \
  101. __attribute__ (( section ( ".data16" ) )) \
  102. _data16_ ## variable array __asm__ ( #variable )
  103. #define __bss16( variable ) \
  104. __attribute__ (( section ( ".bss16" ) )) \
  105. _data16_ ## variable __asm__ ( #variable )
  106. #define __bss16_array( variable, array ) \
  107. __attribute__ (( section ( ".bss16" ) )) \
  108. _data16_ ## variable array __asm__ ( #variable )
  109. #define __text16( variable ) \
  110. __attribute__ (( section ( ".text16.data" ) )) \
  111. _text16_ ## variable __asm__ ( #variable )
  112. #define __text16_array( variable, array ) \
  113. __attribute__ (( section ( ".text16.data" ) )) \
  114. _text16_ ## variable array __asm__ ( #variable )
  115. #define __use_data16( variable ) \
  116. ( * ( ( typeof ( _data16_ ## variable ) * ) \
  117. & ( data16 [ ( size_t ) & ( _data16_ ## variable ) ] ) ) )
  118. #define __use_text16( variable ) \
  119. ( * ( ( typeof ( _text16_ ## variable ) * ) \
  120. & ( text16 [ ( size_t ) & ( _text16_ ## variable ) ] ) ) )
  121. #define __from_data16( pointer ) \
  122. ( ( unsigned int ) \
  123. ( ( ( void * ) (pointer) ) - ( ( void * ) data16 ) ) )
  124. #define __from_text16( pointer ) \
  125. ( ( unsigned int ) \
  126. ( ( ( void * ) (pointer) ) - ( ( void * ) text16 ) ) )
  127. /* Variables in librm.S, present in the normal data segment */
  128. extern uint16_t rm_sp;
  129. extern uint16_t rm_ss;
  130. extern uint16_t __data16 ( rm_cs );
  131. #define rm_cs __use_data16 ( rm_cs )
  132. extern uint16_t __text16 ( rm_ds );
  133. #define rm_ds __use_text16 ( rm_ds )
  134. /**
  135. * Convert segment:offset address to user buffer
  136. *
  137. * @v segment Real-mode segment
  138. * @v offset Real-mode offset
  139. * @ret buffer User buffer
  140. */
  141. static inline __always_inline userptr_t
  142. real_to_user ( unsigned int segment, unsigned int offset ) {
  143. return ( phys_to_user ( ( segment << 4 ) + offset ) );
  144. }
  145. extern uint16_t copy_user_to_rm_stack ( userptr_t data, size_t size );
  146. extern void remove_user_from_rm_stack ( userptr_t data, size_t size );
  147. /* TEXT16_CODE: declare a fragment of code that resides in .text16 */
  148. #define TEXT16_CODE( asm_code_str ) \
  149. ".section \".text16\", \"ax\", @progbits\n\t" \
  150. ".code16\n\t" \
  151. asm_code_str "\n\t" \
  152. ".code32\n\t" \
  153. ".previous\n\t"
  154. /* REAL_CODE: declare a fragment of code that executes in real mode */
  155. #define REAL_CODE( asm_code_str ) \
  156. "pushl $1f\n\t" \
  157. "call real_call\n\t" \
  158. "addl $4, %%esp\n\t" \
  159. TEXT16_CODE ( "\n1:\n\t" \
  160. asm_code_str \
  161. "\n\t" \
  162. "ret\n\t" )
  163. /* PHYS_CODE: declare a fragment of code that executes in flat physical mode */
  164. #define PHYS_CODE( asm_code_str ) \
  165. "call _virt_to_phys\n\t" \
  166. asm_code_str \
  167. "call _phys_to_virt\n\t"
  168. #endif /* ASSEMBLY */
  169. #endif /* LIBRM_H */