head_32.S 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. /*G:020
  8. * Our story starts with the bzImage: booting starts at startup_32 in
  9. * arch/x86/boot/compressed/head_32.S. This merely uncompresses the real
  10. * kernel in place and then jumps into it: startup_32 in
  11. * arch/x86/kernel/head_32.S. Both routines expects a boot header in the %esi
  12. * register, which is created by the bootloader (the Launcher in our case).
  13. *
  14. * The startup_32 function does very little: it clears the uninitialized global
  15. * C variables which we expect to be zero (ie. BSS) and then copies the boot
  16. * header and kernel command line somewhere safe, and populates some initial
  17. * page tables. Finally it checks the 'hardware_subarch' field. This was
  18. * introduced in 2.6.24 for lguest and Xen: if it's set to '1' (lguest's
  19. * assigned number), then it calls us here.
  20. *
  21. * WARNING: be very careful here! We're running at addresses equal to physical
  22. * addresses (around 0), not above PAGE_OFFSET as most code expects
  23. * (eg. 0xC0000000). Jumps are relative, so they're OK, but we can't touch any
  24. * data without remembering to subtract __PAGE_OFFSET!
  25. *
  26. * The .section line puts this code in .init.text so it will be discarded after
  27. * boot.
  28. */
  29. .section .init.text, "ax", @progbits
  30. ENTRY(lguest_entry)
  31. /*
  32. * We make the "initialization" hypercall now to tell the Host where
  33. * our lguest_data struct is.
  34. */
  35. movl $LHCALL_LGUEST_INIT, %eax
  36. movl $lguest_data - __PAGE_OFFSET, %ebx
  37. int $LGUEST_TRAP_ENTRY
  38. /* Now turn our pagetables on; setup by arch/x86/kernel/head_32.S. */
  39. movl $LHCALL_NEW_PGTABLE, %eax
  40. movl $(initial_page_table - __PAGE_OFFSET), %ebx
  41. int $LGUEST_TRAP_ENTRY
  42. /* Set up the initial stack so we can run C code. */
  43. movl $(init_thread_union+THREAD_SIZE),%esp
  44. /* Jumps are relative: we're running __PAGE_OFFSET too low. */
  45. jmp lguest_init+__PAGE_OFFSET
  46. /*G:055
  47. * We create a macro which puts the assembler code between lgstart_ and lgend_
  48. * markers. These templates are put in the .text section: they can't be
  49. * discarded after boot as we may need to patch modules, too.
  50. */
  51. .text
  52. #define LGUEST_PATCH(name, insns...) \
  53. lgstart_##name: insns; lgend_##name:; \
  54. .globl lgstart_##name; .globl lgend_##name
  55. LGUEST_PATCH(cli, movl $0, lguest_data+LGUEST_DATA_irq_enabled)
  56. LGUEST_PATCH(pushf, movl lguest_data+LGUEST_DATA_irq_enabled, %eax)
  57. /*G:033
  58. * But using those wrappers is inefficient (we'll see why that doesn't matter
  59. * for save_fl and irq_disable later). If we write our routines carefully in
  60. * assembler, we can avoid clobbering any registers and avoid jumping through
  61. * the wrapper functions.
  62. *
  63. * I skipped over our first piece of assembler, but this one is worth studying
  64. * in a bit more detail so I'll describe in easy stages. First, the routine to
  65. * enable interrupts:
  66. */
  67. ENTRY(lg_irq_enable)
  68. /*
  69. * The reverse of irq_disable, this sets lguest_data.irq_enabled to
  70. * X86_EFLAGS_IF (ie. "Interrupts enabled").
  71. */
  72. movl $X86_EFLAGS_IF, lguest_data+LGUEST_DATA_irq_enabled
  73. /*
  74. * But now we need to check if the Host wants to know: there might have
  75. * been interrupts waiting to be delivered, in which case it will have
  76. * set lguest_data.irq_pending to X86_EFLAGS_IF. If it's not zero, we
  77. * jump to send_interrupts, otherwise we're done.
  78. */
  79. cmpl $0, lguest_data+LGUEST_DATA_irq_pending
  80. jnz send_interrupts
  81. /*
  82. * One cool thing about x86 is that you can do many things without using
  83. * a register. In this case, the normal path hasn't needed to save or
  84. * restore any registers at all!
  85. */
  86. ret
  87. send_interrupts:
  88. /*
  89. * OK, now we need a register: eax is used for the hypercall number,
  90. * which is LHCALL_SEND_INTERRUPTS.
  91. *
  92. * We used not to bother with this pending detection at all, which was
  93. * much simpler. Sooner or later the Host would realize it had to
  94. * send us an interrupt. But that turns out to make performance 7
  95. * times worse on a simple tcp benchmark. So now we do this the hard
  96. * way.
  97. */
  98. pushl %eax
  99. movl $LHCALL_SEND_INTERRUPTS, %eax
  100. /* This is the actual hypercall trap. */
  101. int $LGUEST_TRAP_ENTRY
  102. /* Put eax back the way we found it. */
  103. popl %eax
  104. ret
  105. /*
  106. * Finally, the "popf" or "restore flags" routine. The %eax register holds the
  107. * flags (in practice, either X86_EFLAGS_IF or 0): if it's X86_EFLAGS_IF we're
  108. * enabling interrupts again, if it's 0 we're leaving them off.
  109. */
  110. ENTRY(lg_restore_fl)
  111. /* This is just "lguest_data.irq_enabled = flags;" */
  112. movl %eax, lguest_data+LGUEST_DATA_irq_enabled
  113. /*
  114. * Now, if the %eax value has enabled interrupts and
  115. * lguest_data.irq_pending is set, we want to tell the Host so it can
  116. * deliver any outstanding interrupts. Fortunately, both values will
  117. * be X86_EFLAGS_IF (ie. 512) in that case, and the "testl"
  118. * instruction will AND them together for us. If both are set, we
  119. * jump to send_interrupts.
  120. */
  121. testl lguest_data+LGUEST_DATA_irq_pending, %eax
  122. jnz send_interrupts
  123. /* Again, the normal path has used no extra registers. Clever, huh? */
  124. ret
  125. /*:*/
  126. /* These demark the EIP where host should never deliver interrupts. */
  127. .global lguest_noirq_iret
  128. /*M:004
  129. * When the Host reflects a trap or injects an interrupt into the Guest, it
  130. * sets the eflags interrupt bit on the stack based on lguest_data.irq_enabled,
  131. * so the Guest iret logic does the right thing when restoring it. However,
  132. * when the Host sets the Guest up for direct traps, such as system calls, the
  133. * processor is the one to push eflags onto the stack, and the interrupt bit
  134. * will be 1 (in reality, interrupts are always enabled in the Guest).
  135. *
  136. * This turns out to be harmless: the only trap which should happen under Linux
  137. * with interrupts disabled is Page Fault (due to our lazy mapping of vmalloc
  138. * regions), which has to be reflected through the Host anyway. If another
  139. * trap *does* go off when interrupts are disabled, the Guest will panic, and
  140. * we'll never get to this iret!
  141. :*/
  142. /*G:045
  143. * There is one final paravirt_op that the Guest implements, and glancing at it
  144. * you can see why I left it to last. It's *cool*! It's in *assembler*!
  145. *
  146. * The "iret" instruction is used to return from an interrupt or trap. The
  147. * stack looks like this:
  148. * old address
  149. * old code segment & privilege level
  150. * old processor flags ("eflags")
  151. *
  152. * The "iret" instruction pops those values off the stack and restores them all
  153. * at once. The only problem is that eflags includes the Interrupt Flag which
  154. * the Guest can't change: the CPU will simply ignore it when we do an "iret".
  155. * So we have to copy eflags from the stack to lguest_data.irq_enabled before
  156. * we do the "iret".
  157. *
  158. * There are two problems with this: firstly, we can't clobber any registers
  159. * and secondly, the whole thing needs to be atomic. The first problem
  160. * is solved by using "push memory"/"pop memory" instruction pair for copying.
  161. *
  162. * The second is harder: copying eflags to lguest_data.irq_enabled will turn
  163. * interrupts on before we're finished, so we could be interrupted before we
  164. * return to userspace or wherever. Our solution to this is to tell the
  165. * Host that it is *never* to interrupt us there, even if interrupts seem to be
  166. * enabled. (It's not necessary to protect pop instruction, since
  167. * data gets updated only after it completes, so we only need to protect
  168. * one instruction, iret).
  169. */
  170. ENTRY(lguest_iret)
  171. pushl 2*4(%esp)
  172. /*
  173. * Note the %ss: segment prefix here. Normal data accesses use the
  174. * "ds" segment, but that will have already been restored for whatever
  175. * we're returning to (such as userspace): we can't trust it. The %ss:
  176. * prefix makes sure we use the stack segment, which is still valid.
  177. */
  178. popl %ss:lguest_data+LGUEST_DATA_irq_enabled
  179. lguest_noirq_iret:
  180. iret