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