i386_head.S 7.7 KB

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