i386_head.S 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. #include <linux/linkage.h>
  2. #include <linux/lguest.h>
  3. #include <asm/lguest_hcall.h>
  4. #include <asm/asm-offsets.h>
  5. #include <asm/thread_info.h>
  6. #include <asm/processor-flags.h>
  7. #include <asm/pgtable.h>
  8. /*G:020
  9. * Our story starts with the kernel booting into startup_32 in
  10. * arch/x86/kernel/head_32.S. It expects a boot header, which is created by
  11. * the bootloader (the Launcher in our case).
  12. *
  13. * The startup_32 function does very little: it clears the uninitialized global
  14. * C variables which we expect to be zero (ie. BSS) and then copies the boot
  15. * header and kernel command line somewhere safe. Finally it checks the
  16. * 'hardware_subarch' field. This was introduced in 2.6.24 for lguest and Xen:
  17. * if it's set to '1' (lguest's assigned number), then it calls us here.
  18. *
  19. * WARNING: be very careful here! We're running at addresses equal to physical
  20. * addesses (around 0), not above PAGE_OFFSET as most code expectes
  21. * (eg. 0xC0000000). Jumps are relative, so they're OK, but we can't touch any
  22. * data without remembering to subtract __PAGE_OFFSET!
  23. *
  24. * The .section line puts this code in .init.text so it will be discarded after
  25. * boot.
  26. */
  27. .section .init.text, "ax", @progbits
  28. ENTRY(lguest_entry)
  29. /*
  30. * We make the "initialization" hypercall now to tell the Host about
  31. * us, and also find out where it put our page tables.
  32. */
  33. movl $LHCALL_LGUEST_INIT, %eax
  34. movl $lguest_data - __PAGE_OFFSET, %ebx
  35. int $LGUEST_TRAP_ENTRY
  36. /* Set up the initial stack so we can run C code. */
  37. movl $(init_thread_union+THREAD_SIZE),%esp
  38. call init_pagetables
  39. /* Jumps are relative: we're running __PAGE_OFFSET too low. */
  40. jmp lguest_init+__PAGE_OFFSET
  41. /*
  42. * Initialize page tables. This creates a PDE and a set of page
  43. * tables, which are located immediately beyond __brk_base. The variable
  44. * _brk_end is set up to point to the first "safe" location.
  45. * Mappings are created both at virtual address 0 (identity mapping)
  46. * and PAGE_OFFSET for up to _end.
  47. *
  48. * FIXME: This code is taken verbatim from arch/x86/kernel/head_32.S: they
  49. * don't have a stack at this point, so we can't just use call and ret.
  50. */
  51. init_pagetables:
  52. #if PTRS_PER_PMD > 1
  53. #define PAGE_TABLE_SIZE(pages) (((pages) / PTRS_PER_PMD) + PTRS_PER_PGD)
  54. #else
  55. #define PAGE_TABLE_SIZE(pages) ((pages) / PTRS_PER_PGD)
  56. #endif
  57. #define pa(X) ((X) - __PAGE_OFFSET)
  58. /* Enough space to fit pagetables for the low memory linear map */
  59. MAPPING_BEYOND_END = \
  60. PAGE_TABLE_SIZE(((1<<32) - __PAGE_OFFSET) >> PAGE_SHIFT) << PAGE_SHIFT
  61. #ifdef CONFIG_X86_PAE
  62. /*
  63. * In PAE mode initial_page_table is statically defined to contain
  64. * enough entries to cover the VMSPLIT option (that is the top 1, 2 or 3
  65. * entries). The identity mapping is handled by pointing two PGD entries
  66. * to the first kernel PMD.
  67. *
  68. * Note the upper half of each PMD or PTE are always zero at this stage.
  69. */
  70. #define KPMDS (((-__PAGE_OFFSET) >> 30) & 3) /* Number of kernel PMDs */
  71. xorl %ebx,%ebx /* %ebx is kept at zero */
  72. movl $pa(__brk_base), %edi
  73. movl $pa(initial_pg_pmd), %edx
  74. movl $PTE_IDENT_ATTR, %eax
  75. 10:
  76. leal PDE_IDENT_ATTR(%edi),%ecx /* Create PMD entry */
  77. movl %ecx,(%edx) /* Store PMD entry */
  78. /* Upper half already zero */
  79. addl $8,%edx
  80. movl $512,%ecx
  81. 11:
  82. stosl
  83. xchgl %eax,%ebx
  84. stosl
  85. xchgl %eax,%ebx
  86. addl $0x1000,%eax
  87. loop 11b
  88. /*
  89. * End condition: we must map up to the end + MAPPING_BEYOND_END.
  90. */
  91. movl $pa(_end) + MAPPING_BEYOND_END + PTE_IDENT_ATTR, %ebp
  92. cmpl %ebp,%eax
  93. jb 10b
  94. 1:
  95. addl $__PAGE_OFFSET, %edi
  96. movl %edi, pa(_brk_end)
  97. shrl $12, %eax
  98. movl %eax, pa(max_pfn_mapped)
  99. /* Do early initialization of the fixmap area */
  100. movl $pa(initial_pg_fixmap)+PDE_IDENT_ATTR,%eax
  101. movl %eax,pa(initial_pg_pmd+0x1000*KPMDS-8)
  102. #else /* Not PAE */
  103. page_pde_offset = (__PAGE_OFFSET >> 20);
  104. movl $pa(__brk_base), %edi
  105. movl $pa(initial_page_table), %edx
  106. movl $PTE_IDENT_ATTR, %eax
  107. 10:
  108. leal PDE_IDENT_ATTR(%edi),%ecx /* Create PDE entry */
  109. movl %ecx,(%edx) /* Store identity PDE entry */
  110. movl %ecx,page_pde_offset(%edx) /* Store kernel PDE entry */
  111. addl $4,%edx
  112. movl $1024, %ecx
  113. 11:
  114. stosl
  115. addl $0x1000,%eax
  116. loop 11b
  117. /*
  118. * End condition: we must map up to the end + MAPPING_BEYOND_END.
  119. */
  120. movl $pa(_end) + MAPPING_BEYOND_END + PTE_IDENT_ATTR, %ebp
  121. cmpl %ebp,%eax
  122. jb 10b
  123. addl $__PAGE_OFFSET, %edi
  124. movl %edi, pa(_brk_end)
  125. shrl $12, %eax
  126. movl %eax, pa(max_pfn_mapped)
  127. /* Do early initialization of the fixmap area */
  128. movl $pa(initial_pg_fixmap)+PDE_IDENT_ATTR,%eax
  129. movl %eax,pa(initial_page_table+0xffc)
  130. #endif
  131. ret
  132. /*G:055
  133. * We create a macro which puts the assembler code between lgstart_ and lgend_
  134. * markers. These templates are put in the .text section: they can't be
  135. * discarded after boot as we may need to patch modules, too.
  136. */
  137. .text
  138. #define LGUEST_PATCH(name, insns...) \
  139. lgstart_##name: insns; lgend_##name:; \
  140. .globl lgstart_##name; .globl lgend_##name
  141. LGUEST_PATCH(cli, movl $0, lguest_data+LGUEST_DATA_irq_enabled)
  142. LGUEST_PATCH(pushf, movl lguest_data+LGUEST_DATA_irq_enabled, %eax)
  143. /*G:033
  144. * But using those wrappers is inefficient (we'll see why that doesn't matter
  145. * for save_fl and irq_disable later). If we write our routines carefully in
  146. * assembler, we can avoid clobbering any registers and avoid jumping through
  147. * the wrapper functions.
  148. *
  149. * I skipped over our first piece of assembler, but this one is worth studying
  150. * in a bit more detail so I'll describe in easy stages. First, the routine to
  151. * enable interrupts:
  152. */
  153. ENTRY(lg_irq_enable)
  154. /*
  155. * The reverse of irq_disable, this sets lguest_data.irq_enabled to
  156. * X86_EFLAGS_IF (ie. "Interrupts enabled").
  157. */
  158. movl $X86_EFLAGS_IF, lguest_data+LGUEST_DATA_irq_enabled
  159. /*
  160. * But now we need to check if the Host wants to know: there might have
  161. * been interrupts waiting to be delivered, in which case it will have
  162. * set lguest_data.irq_pending to X86_EFLAGS_IF. If it's not zero, we
  163. * jump to send_interrupts, otherwise we're done.
  164. */
  165. testl $0, lguest_data+LGUEST_DATA_irq_pending
  166. jnz send_interrupts
  167. /*
  168. * One cool thing about x86 is that you can do many things without using
  169. * a register. In this case, the normal path hasn't needed to save or
  170. * restore any registers at all!
  171. */
  172. ret
  173. send_interrupts:
  174. /*
  175. * OK, now we need a register: eax is used for the hypercall number,
  176. * which is LHCALL_SEND_INTERRUPTS.
  177. *
  178. * We used not to bother with this pending detection at all, which was
  179. * much simpler. Sooner or later the Host would realize it had to
  180. * send us an interrupt. But that turns out to make performance 7
  181. * times worse on a simple tcp benchmark. So now we do this the hard
  182. * way.
  183. */
  184. pushl %eax
  185. movl $LHCALL_SEND_INTERRUPTS, %eax
  186. /*
  187. * This is a vmcall instruction (same thing that KVM uses). Older
  188. * assembler versions might not know the "vmcall" instruction, so we
  189. * create one manually here.
  190. */
  191. .byte 0x0f,0x01,0xc1 /* KVM_HYPERCALL */
  192. /* Put eax back the way we found it. */
  193. popl %eax
  194. ret
  195. /*
  196. * Finally, the "popf" or "restore flags" routine. The %eax register holds the
  197. * flags (in practice, either X86_EFLAGS_IF or 0): if it's X86_EFLAGS_IF we're
  198. * enabling interrupts again, if it's 0 we're leaving them off.
  199. */
  200. ENTRY(lg_restore_fl)
  201. /* This is just "lguest_data.irq_enabled = flags;" */
  202. movl %eax, lguest_data+LGUEST_DATA_irq_enabled
  203. /*
  204. * Now, if the %eax value has enabled interrupts and
  205. * lguest_data.irq_pending is set, we want to tell the Host so it can
  206. * deliver any outstanding interrupts. Fortunately, both values will
  207. * be X86_EFLAGS_IF (ie. 512) in that case, and the "testl"
  208. * instruction will AND them together for us. If both are set, we
  209. * jump to send_interrupts.
  210. */
  211. testl lguest_data+LGUEST_DATA_irq_pending, %eax
  212. jnz send_interrupts
  213. /* Again, the normal path has used no extra registers. Clever, huh? */
  214. ret
  215. /*:*/
  216. /* These demark the EIP range where host should never deliver interrupts. */
  217. .global lguest_noirq_start
  218. .global lguest_noirq_end
  219. /*M:004
  220. * When the Host reflects a trap or injects an interrupt into the Guest, it
  221. * sets the eflags interrupt bit on the stack based on lguest_data.irq_enabled,
  222. * so the Guest iret logic does the right thing when restoring it. However,
  223. * when the Host sets the Guest up for direct traps, such as system calls, the
  224. * processor is the one to push eflags onto the stack, and the interrupt bit
  225. * will be 1 (in reality, interrupts are always enabled in the Guest).
  226. *
  227. * This turns out to be harmless: the only trap which should happen under Linux
  228. * with interrupts disabled is Page Fault (due to our lazy mapping of vmalloc
  229. * regions), which has to be reflected through the Host anyway. If another
  230. * trap *does* go off when interrupts are disabled, the Guest will panic, and
  231. * we'll never get to this iret!
  232. :*/
  233. /*G:045
  234. * There is one final paravirt_op that the Guest implements, and glancing at it
  235. * you can see why I left it to last. It's *cool*! It's in *assembler*!
  236. *
  237. * The "iret" instruction is used to return from an interrupt or trap. The
  238. * stack looks like this:
  239. * old address
  240. * old code segment & privilege level
  241. * old processor flags ("eflags")
  242. *
  243. * The "iret" instruction pops those values off the stack and restores them all
  244. * at once. The only problem is that eflags includes the Interrupt Flag which
  245. * the Guest can't change: the CPU will simply ignore it when we do an "iret".
  246. * So we have to copy eflags from the stack to lguest_data.irq_enabled before
  247. * we do the "iret".
  248. *
  249. * There are two problems with this: firstly, we need to use a register to do
  250. * the copy and secondly, the whole thing needs to be atomic. The first
  251. * problem is easy to solve: push %eax on the stack so we can use it, and then
  252. * restore it at the end just before the real "iret".
  253. *
  254. * The second is harder: copying eflags to lguest_data.irq_enabled will turn
  255. * interrupts on before we're finished, so we could be interrupted before we
  256. * return to userspace or wherever. Our solution to this is to surround the
  257. * code with lguest_noirq_start: and lguest_noirq_end: labels. We tell the
  258. * Host that it is *never* to interrupt us there, even if interrupts seem to be
  259. * enabled.
  260. */
  261. ENTRY(lguest_iret)
  262. pushl %eax
  263. movl 12(%esp), %eax
  264. lguest_noirq_start:
  265. /*
  266. * Note the %ss: segment prefix here. Normal data accesses use the
  267. * "ds" segment, but that will have already been restored for whatever
  268. * we're returning to (such as userspace): we can't trust it. The %ss:
  269. * prefix makes sure we use the stack segment, which is still valid.
  270. */
  271. movl %eax,%ss:lguest_data+LGUEST_DATA_irq_enabled
  272. popl %eax
  273. iret
  274. lguest_noirq_end: