i386_head.S 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 This is where we begin: we have a magic signature which the launcher
  8. * looks for. The plan is that the Linux boot protocol will be extended with a
  9. * "platform type" field which will guide us here from the normal entry point,
  10. * but for the moment this suffices. The normal boot code uses %esi for the
  11. * boot header, so we do too.
  12. *
  13. * WARNING: be very careful here! We're running at addresses equal to physical
  14. * addesses (around 0), not above PAGE_OFFSET as most code expectes
  15. * (eg. 0xC0000000). Jumps are relative, so they're OK, but we can't touch any
  16. * data.
  17. *
  18. * The .section line puts this code in .init.text so it will be discarded after
  19. * boot. */
  20. .section .init.text, "ax", @progbits
  21. .ascii "GenuineLguest"
  22. /* Make initial hypercall now, so we can set up the pagetables. */
  23. movl $LHCALL_LGUEST_INIT, %eax
  24. movl $lguest_data - __PAGE_OFFSET, %edx
  25. int $LGUEST_TRAP_ENTRY
  26. /* Set up boot information pointer to hand to lguest_init(): it wants
  27. * a virtual address. */
  28. movl %esi, %eax
  29. addl $__PAGE_OFFSET, %eax
  30. /* The Host put the toplevel pagetable in lguest_data.pgdir. The movsl
  31. * instruction uses %esi, so we needed to save it above. */
  32. movl lguest_data - __PAGE_OFFSET + LGUEST_DATA_pgdir, %esi
  33. /* Copy first 32 entries of page directory to __PAGE_OFFSET entries.
  34. * This means the first 128M of kernel memory will be mapped at
  35. * PAGE_OFFSET where the kernel expects to run. This will get it far
  36. * enough through boot to switch to its own pagetables. */
  37. movl $32, %ecx
  38. movl %esi, %edi
  39. addl $((__PAGE_OFFSET >> 22) * 4), %edi
  40. rep
  41. movsl
  42. /* Set up the initial stack so we can run C code. */
  43. movl $(init_thread_union+THREAD_SIZE),%esp
  44. /* Jumps are relative, and we're running __PAGE_OFFSET too low at the
  45. * moment. */
  46. jmp lguest_init+__PAGE_OFFSET
  47. /*G:055 We create a macro which puts the assembler code between lgstart_ and
  48. * lgend_ 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. .text
  51. #define LGUEST_PATCH(name, insns...) \
  52. lgstart_##name: insns; lgend_##name:; \
  53. .globl lgstart_##name; .globl lgend_##name
  54. LGUEST_PATCH(cli, movl $0, lguest_data+LGUEST_DATA_irq_enabled)
  55. LGUEST_PATCH(sti, movl $X86_EFLAGS_IF, lguest_data+LGUEST_DATA_irq_enabled)
  56. LGUEST_PATCH(popf, movl %eax, lguest_data+LGUEST_DATA_irq_enabled)
  57. LGUEST_PATCH(pushf, movl lguest_data+LGUEST_DATA_irq_enabled, %eax)
  58. /*:*/
  59. /* These demark the EIP range where host should never deliver interrupts. */
  60. .global lguest_noirq_start
  61. .global lguest_noirq_end
  62. /*M:004 When the Host reflects a trap or injects an interrupt into the Guest,
  63. * it sets the eflags interrupt bit on the stack based on
  64. * lguest_data.irq_enabled, so the Guest iret logic does the right thing when
  65. * restoring it. However, when the Host sets the Guest up for direct traps,
  66. * such as system calls, the processor is the one to push eflags onto the
  67. * stack, and the interrupt bit will be 1 (in reality, interrupts are always
  68. * enabled in the Guest).
  69. *
  70. * This turns out to be harmless: the only trap which should happen under Linux
  71. * with interrupts disabled is Page Fault (due to our lazy mapping of vmalloc
  72. * regions), which has to be reflected through the Host anyway. If another
  73. * trap *does* go off when interrupts are disabled, the Guest will panic, and
  74. * we'll never get to this iret! :*/
  75. /*G:045 There is one final paravirt_op that the Guest implements, and glancing
  76. * at it you can see why I left it to last. It's *cool*! It's in *assembler*!
  77. *
  78. * The "iret" instruction is used to return from an interrupt or trap. The
  79. * stack looks like this:
  80. * old address
  81. * old code segment & privilege level
  82. * old processor flags ("eflags")
  83. *
  84. * The "iret" instruction pops those values off the stack and restores them all
  85. * at once. The only problem is that eflags includes the Interrupt Flag which
  86. * the Guest can't change: the CPU will simply ignore it when we do an "iret".
  87. * So we have to copy eflags from the stack to lguest_data.irq_enabled before
  88. * we do the "iret".
  89. *
  90. * There are two problems with this: firstly, we need to use a register to do
  91. * the copy and secondly, the whole thing needs to be atomic. The first
  92. * problem is easy to solve: push %eax on the stack so we can use it, and then
  93. * restore it at the end just before the real "iret".
  94. *
  95. * The second is harder: copying eflags to lguest_data.irq_enabled will turn
  96. * interrupts on before we're finished, so we could be interrupted before we
  97. * return to userspace or wherever. Our solution to this is to surround the
  98. * code with lguest_noirq_start: and lguest_noirq_end: labels. We tell the
  99. * Host that it is *never* to interrupt us there, even if interrupts seem to be
  100. * enabled. */
  101. ENTRY(lguest_iret)
  102. pushl %eax
  103. movl 12(%esp), %eax
  104. lguest_noirq_start:
  105. /* Note the %ss: segment prefix here. Normal data accesses use the
  106. * "ds" segment, but that will have already been restored for whatever
  107. * we're returning to (such as userspace): we can't trust it. The %ss:
  108. * prefix makes sure we use the stack segment, which is still valid. */
  109. movl %eax,%ss:lguest_data+LGUEST_DATA_irq_enabled
  110. popl %eax
  111. iret
  112. lguest_noirq_end: