Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

librm.h 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. #ifdef ASSEMBLY
  19. /**
  20. * Call C function from real-mode code
  21. *
  22. * @v function C function
  23. */
  24. .macro virtcall function
  25. pushl $\function
  26. call prot_call
  27. .endm
  28. #else /* ASSEMBLY */
  29. #ifdef UACCESS_LIBRM
  30. #define UACCESS_PREFIX_librm
  31. #else
  32. #define UACCESS_PREFIX_librm __librm_
  33. #endif
  34. /**
  35. * Call C function from real-mode code
  36. *
  37. * @v function C function
  38. */
  39. #define VIRT_CALL( function ) \
  40. "pushl $( " #function " )\n\t" \
  41. "call prot_call\n\t"
  42. /* Variables in librm.S */
  43. extern unsigned long virt_offset;
  44. /**
  45. * Convert physical address to user pointer
  46. *
  47. * @v phys_addr Physical address
  48. * @ret userptr User pointer
  49. */
  50. static inline __always_inline userptr_t
  51. UACCESS_INLINE ( librm, phys_to_user ) ( unsigned long phys_addr ) {
  52. return ( phys_addr - virt_offset );
  53. }
  54. /**
  55. * Convert user buffer to physical address
  56. *
  57. * @v userptr User pointer
  58. * @v offset Offset from user pointer
  59. * @ret phys_addr Physical address
  60. */
  61. static inline __always_inline unsigned long
  62. UACCESS_INLINE ( librm, user_to_phys ) ( userptr_t userptr, off_t offset ) {
  63. return ( userptr + offset + virt_offset );
  64. }
  65. static inline __always_inline userptr_t
  66. UACCESS_INLINE ( librm, virt_to_user ) ( volatile const void *addr ) {
  67. return trivial_virt_to_user ( addr );
  68. }
  69. static inline __always_inline void *
  70. UACCESS_INLINE ( librm, user_to_virt ) ( userptr_t userptr, off_t offset ) {
  71. return trivial_user_to_virt ( userptr, offset );
  72. }
  73. static inline __always_inline userptr_t
  74. UACCESS_INLINE ( librm, userptr_add ) ( userptr_t userptr, off_t offset ) {
  75. return trivial_userptr_add ( userptr, offset );
  76. }
  77. static inline __always_inline off_t
  78. UACCESS_INLINE ( librm, userptr_sub ) ( userptr_t userptr,
  79. userptr_t subtrahend ) {
  80. return trivial_userptr_sub ( userptr, subtrahend );
  81. }
  82. static inline __always_inline void
  83. UACCESS_INLINE ( librm, memcpy_user ) ( userptr_t dest, off_t dest_off,
  84. userptr_t src, off_t src_off,
  85. size_t len ) {
  86. trivial_memcpy_user ( dest, dest_off, src, src_off, len );
  87. }
  88. static inline __always_inline void
  89. UACCESS_INLINE ( librm, memmove_user ) ( userptr_t dest, off_t dest_off,
  90. userptr_t src, off_t src_off,
  91. size_t len ) {
  92. trivial_memmove_user ( dest, dest_off, src, src_off, len );
  93. }
  94. static inline __always_inline int
  95. UACCESS_INLINE ( librm, memcmp_user ) ( userptr_t first, off_t first_off,
  96. userptr_t second, off_t second_off,
  97. size_t len ) {
  98. return trivial_memcmp_user ( first, first_off, second, second_off, len);
  99. }
  100. static inline __always_inline void
  101. UACCESS_INLINE ( librm, memset_user ) ( userptr_t buffer, off_t offset,
  102. int c, size_t len ) {
  103. trivial_memset_user ( buffer, offset, c, len );
  104. }
  105. static inline __always_inline size_t
  106. UACCESS_INLINE ( librm, strlen_user ) ( userptr_t buffer, off_t offset ) {
  107. return trivial_strlen_user ( buffer, offset );
  108. }
  109. static inline __always_inline off_t
  110. UACCESS_INLINE ( librm, memchr_user ) ( userptr_t buffer, off_t offset,
  111. int c, size_t len ) {
  112. return trivial_memchr_user ( buffer, offset, c, len );
  113. }
  114. /******************************************************************************
  115. *
  116. * Access to variables in .data16 and .text16
  117. *
  118. */
  119. extern char *data16;
  120. extern char *text16;
  121. #define __data16( variable ) \
  122. __attribute__ (( section ( ".data16" ) )) \
  123. _data16_ ## variable __asm__ ( #variable )
  124. #define __data16_array( variable, array ) \
  125. __attribute__ (( section ( ".data16" ) )) \
  126. _data16_ ## variable array __asm__ ( #variable )
  127. #define __bss16( variable ) \
  128. __attribute__ (( section ( ".bss16" ) )) \
  129. _data16_ ## variable __asm__ ( #variable )
  130. #define __bss16_array( variable, array ) \
  131. __attribute__ (( section ( ".bss16" ) )) \
  132. _data16_ ## variable array __asm__ ( #variable )
  133. #define __text16( variable ) \
  134. __attribute__ (( section ( ".text16.data" ) )) \
  135. _text16_ ## variable __asm__ ( #variable )
  136. #define __text16_array( variable, array ) \
  137. __attribute__ (( section ( ".text16.data" ) )) \
  138. _text16_ ## variable array __asm__ ( #variable )
  139. #define __use_data16( variable ) \
  140. ( * ( ( typeof ( _data16_ ## variable ) * ) \
  141. & ( data16 [ ( size_t ) & ( _data16_ ## variable ) ] ) ) )
  142. #define __use_text16( variable ) \
  143. ( * ( ( typeof ( _text16_ ## variable ) * ) \
  144. & ( text16 [ ( size_t ) & ( _text16_ ## variable ) ] ) ) )
  145. #define __from_data16( pointer ) \
  146. ( ( unsigned int ) \
  147. ( ( ( void * ) (pointer) ) - ( ( void * ) data16 ) ) )
  148. #define __from_text16( pointer ) \
  149. ( ( unsigned int ) \
  150. ( ( ( void * ) (pointer) ) - ( ( void * ) text16 ) ) )
  151. /* Variables in librm.S, present in the normal data segment */
  152. extern uint16_t rm_sp;
  153. extern uint16_t rm_ss;
  154. extern uint16_t __text16 ( rm_cs );
  155. #define rm_cs __use_text16 ( rm_cs )
  156. extern uint16_t __text16 ( rm_ds );
  157. #define rm_ds __use_text16 ( rm_ds )
  158. extern uint16_t copy_user_to_rm_stack ( userptr_t data, size_t size );
  159. extern void remove_user_from_rm_stack ( userptr_t data, size_t size );
  160. /* CODE_DEFAULT: restore default .code32/.code64 directive */
  161. #ifdef __x86_64__
  162. #define CODE_DEFAULT ".code64"
  163. #else
  164. #define CODE_DEFAULT ".code32"
  165. #endif
  166. /* TEXT16_CODE: declare a fragment of code that resides in .text16 */
  167. #define TEXT16_CODE( asm_code_str ) \
  168. ".section \".text16\", \"ax\", @progbits\n\t" \
  169. ".code16\n\t" \
  170. asm_code_str "\n\t" \
  171. CODE_DEFAULT "\n\t" \
  172. ".previous\n\t"
  173. /* REAL_CODE: declare a fragment of code that executes in real mode */
  174. #define REAL_CODE( asm_code_str ) \
  175. "push $1f\n\t" \
  176. "call real_call\n\t" \
  177. TEXT16_CODE ( "\n1:\n\t" \
  178. asm_code_str \
  179. "\n\t" \
  180. "ret\n\t" )
  181. /* PHYS_CODE: declare a fragment of code that executes in flat physical mode */
  182. #define PHYS_CODE( asm_code_str ) \
  183. "call _virt_to_phys\n\t" \
  184. ".code32\n\t" \
  185. asm_code_str \
  186. "call _phys_to_virt\n\t" \
  187. CODE_DEFAULT "\n\t"
  188. /** Number of interrupts */
  189. #define NUM_INT 256
  190. /** An interrupt descriptor table register */
  191. struct idtr {
  192. /** Limit */
  193. uint16_t limit;
  194. /** Base */
  195. uint32_t base;
  196. } __attribute__ (( packed ));
  197. /** An interrupt descriptor table entry */
  198. struct interrupt_descriptor {
  199. /** Low 16 bits of address */
  200. uint16_t low;
  201. /** Code segment */
  202. uint16_t segment;
  203. /** Unused */
  204. uint8_t unused;
  205. /** Type and attributes */
  206. uint8_t attr;
  207. /** High 16 bits of address */
  208. uint16_t high;
  209. } __attribute__ (( packed ));
  210. /** Interrupt descriptor is present */
  211. #define IDTE_PRESENT 0x80
  212. /** Interrupt descriptor 32-bit interrupt gate type */
  213. #define IDTE_TYPE_IRQ32 0x0e
  214. /** An interrupt vector
  215. *
  216. * Each interrupt vector comprises an eight-byte fragment of code:
  217. *
  218. * 60 pushal
  219. * b0 xx movb $INT, %al
  220. * e9 xx xx xx xx jmp interrupt_wrapper
  221. */
  222. struct interrupt_vector {
  223. /** "pushal" instruction */
  224. uint8_t pushal;
  225. /** "movb" instruction */
  226. uint8_t movb;
  227. /** Interrupt number */
  228. uint8_t intr;
  229. /** "jmp" instruction */
  230. uint8_t jmp;
  231. /** Interrupt wrapper address offset */
  232. uint32_t offset;
  233. /** Next instruction after jump */
  234. uint8_t next[0];
  235. } __attribute__ (( packed ));
  236. /** "pushal" instruction */
  237. #define PUSHAL_INSN 0x60
  238. /** "movb" instruction */
  239. #define MOVB_INSN 0xb0
  240. /** "jmp" instruction */
  241. #define JMP_INSN 0xe9
  242. extern void set_interrupt_vector ( unsigned int intr, void *vector );
  243. #endif /* ASSEMBLY */
  244. #endif /* LIBRM_H */