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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. /**
  141. * Convert segment:offset address to user buffer
  142. *
  143. * @v segment Real-mode segment
  144. * @v offset Real-mode offset
  145. * @ret buffer User buffer
  146. */
  147. static inline __always_inline userptr_t
  148. real_to_user ( unsigned int segment, unsigned int offset ) {
  149. return ( phys_to_user ( ( segment << 4 ) + offset ) );
  150. }
  151. extern uint16_t copy_user_to_rm_stack ( userptr_t data, size_t size );
  152. extern void remove_user_from_rm_stack ( userptr_t data, size_t size );
  153. /* TEXT16_CODE: declare a fragment of code that resides in .text16 */
  154. #define TEXT16_CODE( asm_code_str ) \
  155. ".section \".text16\", \"ax\", @progbits\n\t" \
  156. ".code16\n\t" \
  157. asm_code_str "\n\t" \
  158. ".code32\n\t" \
  159. ".previous\n\t"
  160. /* REAL_CODE: declare a fragment of code that executes in real mode */
  161. #define REAL_CODE( asm_code_str ) \
  162. "pushl $1f\n\t" \
  163. "call real_call\n\t" \
  164. "addl $4, %%esp\n\t" \
  165. TEXT16_CODE ( "\n1:\n\t" \
  166. asm_code_str \
  167. "\n\t" \
  168. "ret\n\t" )
  169. /* PHYS_CODE: declare a fragment of code that executes in flat physical mode */
  170. #define PHYS_CODE( asm_code_str ) \
  171. "call _virt_to_phys\n\t" \
  172. asm_code_str \
  173. "call _phys_to_virt\n\t"
  174. /** Number of interrupts */
  175. #define NUM_INT 256
  176. /** An interrupt descriptor table register */
  177. struct idtr {
  178. /** Limit */
  179. uint16_t limit;
  180. /** Base */
  181. uint32_t base;
  182. } __attribute__ (( packed ));
  183. /** An interrupt descriptor table entry */
  184. struct interrupt_descriptor {
  185. /** Low 16 bits of address */
  186. uint16_t low;
  187. /** Code segment */
  188. uint16_t segment;
  189. /** Unused */
  190. uint8_t unused;
  191. /** Type and attributes */
  192. uint8_t attr;
  193. /** High 16 bits of address */
  194. uint16_t high;
  195. } __attribute__ (( packed ));
  196. /** Interrupt descriptor is present */
  197. #define IDTE_PRESENT 0x80
  198. /** Interrupt descriptor 32-bit interrupt gate type */
  199. #define IDTE_TYPE_IRQ32 0x0e
  200. /** An interrupt vector
  201. *
  202. * Each interrupt vector comprises an eight-byte fragment of code:
  203. *
  204. * 60 pushal
  205. * b0 xx movb $INT, %al
  206. * e9 xx xx xx xx jmp interrupt_wrapper
  207. */
  208. struct interrupt_vector {
  209. /** "pushal" instruction */
  210. uint8_t pushal;
  211. /** "movb" instruction */
  212. uint8_t movb;
  213. /** Interrupt number */
  214. uint8_t intr;
  215. /** "jmp" instruction */
  216. uint8_t jmp;
  217. /** Interrupt wrapper address offset */
  218. uint32_t offset;
  219. /** Next instruction after jump */
  220. uint8_t next[0];
  221. } __attribute__ (( packed ));
  222. /** "pushal" instruction */
  223. #define PUSHAL_INSN 0x60
  224. /** "movb" instruction */
  225. #define MOVB_INSN 0xb0
  226. /** "jmp" instruction */
  227. #define JMP_INSN 0xe9
  228. extern void set_interrupt_vector ( unsigned int intr, void *vector );
  229. #endif /* ASSEMBLY */
  230. #endif /* LIBRM_H */