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 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #ifndef LIBRM_H
  2. #define LIBRM_H
  3. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  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 int
  77. UACCESS_INLINE ( librm, memcmp_user ) ( userptr_t first, off_t first_off,
  78. userptr_t second, off_t second_off,
  79. size_t len ) {
  80. return trivial_memcmp_user ( first, first_off, second, second_off, len);
  81. }
  82. static inline __always_inline void
  83. UACCESS_INLINE ( librm, memset_user ) ( userptr_t buffer, off_t offset,
  84. int c, size_t len ) {
  85. trivial_memset_user ( buffer, offset, c, len );
  86. }
  87. static inline __always_inline size_t
  88. UACCESS_INLINE ( librm, strlen_user ) ( userptr_t buffer, off_t offset ) {
  89. return trivial_strlen_user ( buffer, offset );
  90. }
  91. static inline __always_inline off_t
  92. UACCESS_INLINE ( librm, memchr_user ) ( userptr_t buffer, off_t offset,
  93. int c, size_t len ) {
  94. return trivial_memchr_user ( buffer, offset, c, len );
  95. }
  96. /******************************************************************************
  97. *
  98. * Access to variables in .data16 and .text16
  99. *
  100. */
  101. extern char *data16;
  102. extern char *text16;
  103. #define __data16( variable ) \
  104. __attribute__ (( section ( ".data16" ) )) \
  105. _data16_ ## variable __asm__ ( #variable )
  106. #define __data16_array( variable, array ) \
  107. __attribute__ (( section ( ".data16" ) )) \
  108. _data16_ ## variable array __asm__ ( #variable )
  109. #define __bss16( variable ) \
  110. __attribute__ (( section ( ".bss16" ) )) \
  111. _data16_ ## variable __asm__ ( #variable )
  112. #define __bss16_array( variable, array ) \
  113. __attribute__ (( section ( ".bss16" ) )) \
  114. _data16_ ## variable array __asm__ ( #variable )
  115. #define __text16( variable ) \
  116. __attribute__ (( section ( ".text16.data" ) )) \
  117. _text16_ ## variable __asm__ ( #variable )
  118. #define __text16_array( variable, array ) \
  119. __attribute__ (( section ( ".text16.data" ) )) \
  120. _text16_ ## variable array __asm__ ( #variable )
  121. #define __use_data16( variable ) \
  122. ( * ( ( typeof ( _data16_ ## variable ) * ) \
  123. & ( data16 [ ( size_t ) & ( _data16_ ## variable ) ] ) ) )
  124. #define __use_text16( variable ) \
  125. ( * ( ( typeof ( _text16_ ## variable ) * ) \
  126. & ( text16 [ ( size_t ) & ( _text16_ ## variable ) ] ) ) )
  127. #define __from_data16( pointer ) \
  128. ( ( unsigned int ) \
  129. ( ( ( void * ) (pointer) ) - ( ( void * ) data16 ) ) )
  130. #define __from_text16( pointer ) \
  131. ( ( unsigned int ) \
  132. ( ( ( void * ) (pointer) ) - ( ( void * ) text16 ) ) )
  133. /* Variables in librm.S, present in the normal data segment */
  134. extern uint16_t rm_sp;
  135. extern uint16_t rm_ss;
  136. extern uint16_t __text16 ( rm_cs );
  137. #define rm_cs __use_text16 ( rm_cs )
  138. extern uint16_t __text16 ( rm_ds );
  139. #define rm_ds __use_text16 ( rm_ds )
  140. extern uint16_t copy_user_to_rm_stack ( userptr_t data, size_t size );
  141. extern void remove_user_from_rm_stack ( userptr_t data, size_t size );
  142. /* CODE_DEFAULT: restore default .code32/.code64 directive */
  143. #ifdef __x86_64__
  144. #define CODE_DEFAULT ".code64"
  145. #else
  146. #define CODE_DEFAULT ".code32"
  147. #endif
  148. /* TEXT16_CODE: declare a fragment of code that resides in .text16 */
  149. #define TEXT16_CODE( asm_code_str ) \
  150. ".section \".text16\", \"ax\", @progbits\n\t" \
  151. ".code16\n\t" \
  152. asm_code_str "\n\t" \
  153. CODE_DEFAULT "\n\t" \
  154. ".previous\n\t"
  155. /* REAL_CODE: declare a fragment of code that executes in real mode */
  156. #define REAL_CODE( asm_code_str ) \
  157. "push $1f\n\t" \
  158. "call real_call\n\t" \
  159. "addl $4, %%esp\n\t" \
  160. TEXT16_CODE ( "\n1:\n\t" \
  161. asm_code_str \
  162. "\n\t" \
  163. "ret\n\t" )
  164. /* PHYS_CODE: declare a fragment of code that executes in flat physical mode */
  165. #define PHYS_CODE( asm_code_str ) \
  166. "call _virt_to_phys\n\t" \
  167. ".code32\n\t" \
  168. asm_code_str \
  169. "call _phys_to_virt\n\t" \
  170. CODE_DEFAULT "\n\t"
  171. /** Number of interrupts */
  172. #define NUM_INT 256
  173. /** An interrupt descriptor table register */
  174. struct idtr {
  175. /** Limit */
  176. uint16_t limit;
  177. /** Base */
  178. uint32_t base;
  179. } __attribute__ (( packed ));
  180. /** An interrupt descriptor table entry */
  181. struct interrupt_descriptor {
  182. /** Low 16 bits of address */
  183. uint16_t low;
  184. /** Code segment */
  185. uint16_t segment;
  186. /** Unused */
  187. uint8_t unused;
  188. /** Type and attributes */
  189. uint8_t attr;
  190. /** High 16 bits of address */
  191. uint16_t high;
  192. } __attribute__ (( packed ));
  193. /** Interrupt descriptor is present */
  194. #define IDTE_PRESENT 0x80
  195. /** Interrupt descriptor 32-bit interrupt gate type */
  196. #define IDTE_TYPE_IRQ32 0x0e
  197. /** An interrupt vector
  198. *
  199. * Each interrupt vector comprises an eight-byte fragment of code:
  200. *
  201. * 60 pushal
  202. * b0 xx movb $INT, %al
  203. * e9 xx xx xx xx jmp interrupt_wrapper
  204. */
  205. struct interrupt_vector {
  206. /** "pushal" instruction */
  207. uint8_t pushal;
  208. /** "movb" instruction */
  209. uint8_t movb;
  210. /** Interrupt number */
  211. uint8_t intr;
  212. /** "jmp" instruction */
  213. uint8_t jmp;
  214. /** Interrupt wrapper address offset */
  215. uint32_t offset;
  216. /** Next instruction after jump */
  217. uint8_t next[0];
  218. } __attribute__ (( packed ));
  219. /** "pushal" instruction */
  220. #define PUSHAL_INSN 0x60
  221. /** "movb" instruction */
  222. #define MOVB_INSN 0xb0
  223. /** "jmp" instruction */
  224. #define JMP_INSN 0xe9
  225. extern void set_interrupt_vector ( unsigned int intr, void *vector );
  226. #endif /* ASSEMBLY */
  227. #endif /* LIBRM_H */