Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

librm.h 5.7KB

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