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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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. #define P2R_DS 0x38
  15. #define LONG_CS 0x40
  16. /* Calculate symbol address within VIRTUAL_CS or VIRTUAL_DS
  17. *
  18. * In a 64-bit build, we set the bases of VIRTUAL_CS and VIRTUAL_DS
  19. * such that truncating a .textdata symbol value to 32 bits gives a
  20. * valid 32-bit virtual address.
  21. *
  22. * The C code is compiled with -mcmodel=kernel and so we must place
  23. * all .textdata symbols within the negative 2GB of the 64-bit address
  24. * space. Consequently, all .textdata symbols will have the MSB set
  25. * after truncation to 32 bits. This means that a straightforward
  26. * R_X86_64_32 relocation record for the symbol will fail, since the
  27. * truncated symbol value will not correctly zero-extend to the
  28. * original 64-bit value.
  29. *
  30. * Using an R_X86_64_32S relocation record would work, but there is no
  31. * (sensible) way to generate these relocation records within 32-bit
  32. * or 16-bit code.
  33. *
  34. * The simplest solution is to generate an R_X86_64_32 relocation
  35. * record with an addend of (-0xffffffff00000000). Since all
  36. * .textdata symbols are within the negative 2GB of the 64-bit address
  37. * space, this addend acts to effectively truncate the symbol to 32
  38. * bits, thereby matching the semantics of the R_X86_64_32 relocation
  39. * records generated for 32-bit and 16-bit code.
  40. *
  41. * In a 32-bit build, this problem does not exist, and we can just use
  42. * the .textdata symbol values directly.
  43. */
  44. #ifdef __x86_64__
  45. #define VIRTUAL(address) ( (address) - 0xffffffff00000000 )
  46. #else
  47. #define VIRTUAL(address) (address)
  48. #endif
  49. #ifdef ASSEMBLY
  50. /**
  51. * Call C function from real-mode code
  52. *
  53. * @v function C function
  54. */
  55. .macro virtcall function
  56. pushl $VIRTUAL(\function)
  57. call virt_call
  58. .endm
  59. #else /* ASSEMBLY */
  60. #ifdef UACCESS_LIBRM
  61. #define UACCESS_PREFIX_librm
  62. #else
  63. #define UACCESS_PREFIX_librm __librm_
  64. #endif
  65. /**
  66. * Call C function from real-mode code
  67. *
  68. * @v function C function
  69. */
  70. #define VIRT_CALL( function ) \
  71. "pushl $( " _S2 ( VIRTUAL ( function ) ) " )\n\t" \
  72. "call virt_call\n\t"
  73. /* Variables in librm.S */
  74. extern const unsigned long virt_offset;
  75. /**
  76. * Convert physical address to user pointer
  77. *
  78. * @v phys_addr Physical address
  79. * @ret userptr User pointer
  80. */
  81. static inline __always_inline userptr_t
  82. UACCESS_INLINE ( librm, phys_to_user ) ( unsigned long phys_addr ) {
  83. /* In a 64-bit build, any valid physical address is directly
  84. * usable as a virtual address, since the low 4GB is
  85. * identity-mapped.
  86. */
  87. if ( sizeof ( physaddr_t ) > sizeof ( uint32_t ) )
  88. return phys_addr;
  89. /* In a 32-bit build, subtract virt_offset */
  90. return ( phys_addr - virt_offset );
  91. }
  92. /**
  93. * Convert user buffer to physical address
  94. *
  95. * @v userptr User pointer
  96. * @v offset Offset from user pointer
  97. * @ret phys_addr Physical address
  98. */
  99. static inline __always_inline unsigned long
  100. UACCESS_INLINE ( librm, user_to_phys ) ( userptr_t userptr, off_t offset ) {
  101. unsigned long addr = ( userptr + offset );
  102. /* In a 64-bit build, any virtual address in the low 4GB is
  103. * directly usable as a physical address, since the low 4GB is
  104. * identity-mapped.
  105. */
  106. if ( ( sizeof ( physaddr_t ) > sizeof ( uint32_t ) ) &&
  107. ( addr <= 0xffffffffUL ) )
  108. return addr;
  109. /* In a 32-bit build or in a 64-bit build with a virtual
  110. * address above 4GB: add virt_offset
  111. */
  112. return ( addr + virt_offset );
  113. }
  114. static inline __always_inline userptr_t
  115. UACCESS_INLINE ( librm, virt_to_user ) ( volatile const void *addr ) {
  116. return trivial_virt_to_user ( addr );
  117. }
  118. static inline __always_inline void *
  119. UACCESS_INLINE ( librm, user_to_virt ) ( userptr_t userptr, off_t offset ) {
  120. return trivial_user_to_virt ( userptr, offset );
  121. }
  122. static inline __always_inline userptr_t
  123. UACCESS_INLINE ( librm, userptr_add ) ( userptr_t userptr, off_t offset ) {
  124. return trivial_userptr_add ( userptr, offset );
  125. }
  126. static inline __always_inline off_t
  127. UACCESS_INLINE ( librm, userptr_sub ) ( userptr_t userptr,
  128. userptr_t subtrahend ) {
  129. return trivial_userptr_sub ( userptr, subtrahend );
  130. }
  131. static inline __always_inline void
  132. UACCESS_INLINE ( librm, memcpy_user ) ( userptr_t dest, off_t dest_off,
  133. userptr_t src, off_t src_off,
  134. size_t len ) {
  135. trivial_memcpy_user ( dest, dest_off, src, src_off, len );
  136. }
  137. static inline __always_inline void
  138. UACCESS_INLINE ( librm, memmove_user ) ( userptr_t dest, off_t dest_off,
  139. userptr_t src, off_t src_off,
  140. size_t len ) {
  141. trivial_memmove_user ( dest, dest_off, src, src_off, len );
  142. }
  143. static inline __always_inline int
  144. UACCESS_INLINE ( librm, memcmp_user ) ( userptr_t first, off_t first_off,
  145. userptr_t second, off_t second_off,
  146. size_t len ) {
  147. return trivial_memcmp_user ( first, first_off, second, second_off, len);
  148. }
  149. static inline __always_inline void
  150. UACCESS_INLINE ( librm, memset_user ) ( userptr_t buffer, off_t offset,
  151. int c, size_t len ) {
  152. trivial_memset_user ( buffer, offset, c, len );
  153. }
  154. static inline __always_inline size_t
  155. UACCESS_INLINE ( librm, strlen_user ) ( userptr_t buffer, off_t offset ) {
  156. return trivial_strlen_user ( buffer, offset );
  157. }
  158. static inline __always_inline off_t
  159. UACCESS_INLINE ( librm, memchr_user ) ( userptr_t buffer, off_t offset,
  160. int c, size_t len ) {
  161. return trivial_memchr_user ( buffer, offset, c, len );
  162. }
  163. /******************************************************************************
  164. *
  165. * Access to variables in .data16 and .text16
  166. *
  167. */
  168. extern char * const data16;
  169. extern char * const text16;
  170. #define __data16( variable ) \
  171. __attribute__ (( section ( ".data16" ) )) \
  172. _data16_ ## variable __asm__ ( #variable )
  173. #define __data16_array( variable, array ) \
  174. __attribute__ (( section ( ".data16" ) )) \
  175. _data16_ ## variable array __asm__ ( #variable )
  176. #define __bss16( variable ) \
  177. __attribute__ (( section ( ".bss16" ) )) \
  178. _data16_ ## variable __asm__ ( #variable )
  179. #define __bss16_array( variable, array ) \
  180. __attribute__ (( section ( ".bss16" ) )) \
  181. _data16_ ## variable array __asm__ ( #variable )
  182. #define __text16( variable ) \
  183. __attribute__ (( section ( ".text16.data" ) )) \
  184. _text16_ ## variable __asm__ ( #variable )
  185. #define __text16_array( variable, array ) \
  186. __attribute__ (( section ( ".text16.data" ) )) \
  187. _text16_ ## variable array __asm__ ( #variable )
  188. #define __use_data16( variable ) \
  189. ( * ( ( typeof ( _data16_ ## variable ) * ) \
  190. & ( data16 [ ( size_t ) & ( _data16_ ## variable ) ] ) ) )
  191. #define __use_text16( variable ) \
  192. ( * ( ( typeof ( _text16_ ## variable ) * ) \
  193. & ( text16 [ ( size_t ) & ( _text16_ ## variable ) ] ) ) )
  194. #define __from_data16( pointer ) \
  195. ( ( unsigned int ) \
  196. ( ( ( void * ) (pointer) ) - ( ( void * ) data16 ) ) )
  197. #define __from_text16( pointer ) \
  198. ( ( unsigned int ) \
  199. ( ( ( void * ) (pointer) ) - ( ( void * ) text16 ) ) )
  200. /* Variables in librm.S, present in the normal data segment */
  201. extern uint16_t rm_sp;
  202. extern uint16_t rm_ss;
  203. extern const uint16_t __text16 ( rm_cs );
  204. #define rm_cs __use_text16 ( rm_cs )
  205. extern const uint16_t __text16 ( rm_ds );
  206. #define rm_ds __use_text16 ( rm_ds )
  207. extern uint16_t copy_user_to_rm_stack ( userptr_t data, size_t size );
  208. extern void remove_user_from_rm_stack ( userptr_t data, size_t size );
  209. /* CODE_DEFAULT: restore default .code32/.code64 directive */
  210. #ifdef __x86_64__
  211. #define CODE_DEFAULT ".code64"
  212. #else
  213. #define CODE_DEFAULT ".code32"
  214. #endif
  215. /* TEXT16_CODE: declare a fragment of code that resides in .text16 */
  216. #define TEXT16_CODE( asm_code_str ) \
  217. ".section \".text16\", \"ax\", @progbits\n\t" \
  218. ".code16\n\t" \
  219. asm_code_str "\n\t" \
  220. CODE_DEFAULT "\n\t" \
  221. ".previous\n\t"
  222. /* REAL_CODE: declare a fragment of code that executes in real mode */
  223. #define REAL_CODE( asm_code_str ) \
  224. "push $1f\n\t" \
  225. "call real_call\n\t" \
  226. TEXT16_CODE ( "\n1:\n\t" \
  227. asm_code_str \
  228. "\n\t" \
  229. "ret\n\t" )
  230. /* PHYS_CODE: declare a fragment of code that executes in flat physical mode */
  231. #define PHYS_CODE( asm_code_str ) \
  232. "push $1f\n\t" \
  233. "call phys_call\n\t" \
  234. ".section \".text.phys\", \"ax\", @progbits\n\t"\
  235. ".code32\n\t" \
  236. "\n1:\n\t" \
  237. asm_code_str \
  238. "\n\t" \
  239. "ret\n\t" \
  240. CODE_DEFAULT "\n\t" \
  241. ".previous\n\t"
  242. /** Number of interrupts */
  243. #define NUM_INT 256
  244. /** A 32-bit interrupt descriptor table register */
  245. struct idtr32 {
  246. /** Limit */
  247. uint16_t limit;
  248. /** Base */
  249. uint32_t base;
  250. } __attribute__ (( packed ));
  251. /** A 64-bit interrupt descriptor table register */
  252. struct idtr64 {
  253. /** Limit */
  254. uint16_t limit;
  255. /** Base */
  256. uint64_t base;
  257. } __attribute__ (( packed ));
  258. /** A 32-bit interrupt descriptor table entry */
  259. struct interrupt32_descriptor {
  260. /** Low 16 bits of address */
  261. uint16_t low;
  262. /** Code segment */
  263. uint16_t segment;
  264. /** Unused */
  265. uint8_t unused;
  266. /** Type and attributes */
  267. uint8_t attr;
  268. /** High 16 bits of address */
  269. uint16_t high;
  270. } __attribute__ (( packed ));
  271. /** A 64-bit interrupt descriptor table entry */
  272. struct interrupt64_descriptor {
  273. /** Low 16 bits of address */
  274. uint16_t low;
  275. /** Code segment */
  276. uint16_t segment;
  277. /** Unused */
  278. uint8_t unused;
  279. /** Type and attributes */
  280. uint8_t attr;
  281. /** Middle 16 bits of address */
  282. uint16_t mid;
  283. /** High 32 bits of address */
  284. uint32_t high;
  285. /** Reserved */
  286. uint32_t reserved;
  287. } __attribute__ (( packed ));
  288. /** Interrupt descriptor is present */
  289. #define IDTE_PRESENT 0x80
  290. /** Interrupt descriptor 32-bit interrupt gate type */
  291. #define IDTE_TYPE_IRQ32 0x0e
  292. /** Interrupt descriptor 64-bit interrupt gate type */
  293. #define IDTE_TYPE_IRQ64 0x0e
  294. /** An interrupt vector
  295. *
  296. * Each interrupt vector comprises an eight-byte fragment of code:
  297. *
  298. * 50 pushl %eax (or pushq %rax in long mode)
  299. * b0 xx movb $INT, %al
  300. * e9 xx xx xx xx jmp interrupt_wrapper
  301. */
  302. struct interrupt_vector {
  303. /** "push" instruction */
  304. uint8_t push;
  305. /** "movb" instruction */
  306. uint8_t movb;
  307. /** Interrupt number */
  308. uint8_t intr;
  309. /** "jmp" instruction */
  310. uint8_t jmp;
  311. /** Interrupt wrapper address offset */
  312. uint32_t offset;
  313. /** Next instruction after jump */
  314. uint8_t next[0];
  315. } __attribute__ (( packed ));
  316. /** "push %eax" instruction */
  317. #define PUSH_INSN 0x50
  318. /** "movb" instruction */
  319. #define MOVB_INSN 0xb0
  320. /** "jmp" instruction */
  321. #define JMP_INSN 0xe9
  322. /** 32-bit interrupt wrapper stack frame */
  323. struct interrupt_frame32 {
  324. uint32_t esp;
  325. uint32_t ss;
  326. uint32_t gs;
  327. uint32_t fs;
  328. uint32_t es;
  329. uint32_t ds;
  330. uint32_t ebp;
  331. uint32_t edi;
  332. uint32_t esi;
  333. uint32_t edx;
  334. uint32_t ecx;
  335. uint32_t ebx;
  336. uint32_t eax;
  337. uint32_t eip;
  338. uint32_t cs;
  339. uint32_t eflags;
  340. } __attribute__ (( packed ));
  341. /** 64-bit interrupt wrapper stack frame */
  342. struct interrupt_frame64 {
  343. uint64_t r15;
  344. uint64_t r14;
  345. uint64_t r13;
  346. uint64_t r12;
  347. uint64_t r11;
  348. uint64_t r10;
  349. uint64_t r9;
  350. uint64_t r8;
  351. uint64_t rbp;
  352. uint64_t rdi;
  353. uint64_t rsi;
  354. uint64_t rdx;
  355. uint64_t rcx;
  356. uint64_t rbx;
  357. uint64_t rax;
  358. uint64_t rip;
  359. uint64_t cs;
  360. uint64_t rflags;
  361. uint64_t rsp;
  362. uint64_t ss;
  363. } __attribute__ (( packed ));
  364. extern void set_interrupt_vector ( unsigned int intr, void *vector );
  365. /** A page table */
  366. struct page_table {
  367. /** Page address and flags */
  368. uint64_t page[512];
  369. };
  370. /** Page flags */
  371. enum page_flags {
  372. /** Page is present */
  373. PAGE_P = 0x01,
  374. /** Page is writable */
  375. PAGE_RW = 0x02,
  376. /** Page is accessible by user code */
  377. PAGE_US = 0x04,
  378. /** Page-level write-through */
  379. PAGE_PWT = 0x08,
  380. /** Page-level cache disable */
  381. PAGE_PCD = 0x10,
  382. /** Page is a large page */
  383. PAGE_PS = 0x80,
  384. /** Page is the last page in an allocation
  385. *
  386. * This bit is ignored by the hardware. We use it to track
  387. * the size of allocations made by ioremap().
  388. */
  389. PAGE_LAST = 0x800,
  390. };
  391. /** The I/O space page table */
  392. extern struct page_table io_pages;
  393. /** I/O page size
  394. *
  395. * We choose to use 2MB pages for I/O space, to minimise the number of
  396. * page table entries required.
  397. */
  398. #define IO_PAGE_SIZE 0x200000UL
  399. /** I/O page base address
  400. *
  401. * We choose to place I/O space immediately above the identity-mapped
  402. * 32-bit address space.
  403. */
  404. #define IO_BASE ( ( void * ) 0x100000000ULL )
  405. #endif /* ASSEMBLY */
  406. #endif /* LIBRM_H */