kexec_tramp.S 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /* Position-independent kexec trampoline. Copied to 0x800 at runtime (see
  2. * kexec.c) because the final copy overwrites the kernel code we're executing.
  3. *
  4. * Args in registers (set by kexec.c):
  5. * eax = source (staging buffer)
  6. * ebx = dest (0x7E00)
  7. * ecx = byte count
  8. * edx = entry (new kernel_entry, see entry.S)
  9. *
  10. * Also rebuilds a flat GDT at 0x900 and loads it, so the new kernel's segment
  11. * reloads (iret on the first context switch) don't read the old GDT that lives
  12. * inside the kernel region we overwrite. 0x900 is below 0x7E00 and is never
  13. * overwritten, so the GDT survives. Clobbers esi/edi. Never returns.
  14. */
  15. .code32
  16. .text
  17. .globl kexec_trampoline
  18. kexec_trampoline:
  19. cli
  20. /* Private stack: the copy below overwrites the kernel image, including
  21. * whatever stack we were called on. Use the free gap below the E820
  22. * map at 0x1000 (trampoline at 0x800, flat GDT at 0x900 -- the stack
  23. * only ever dips a few words). entry.S re-points ESP once the new
  24. * image is in place. */
  25. movl $0x1000, %esp
  26. pushl %eax
  27. pushl %ebx
  28. pushl %ecx
  29. pushl %edx
  30. /* Disable paging so the copy below can overwrite the old kernel's page
  31. * tables (which live inside the kernel region we overwrite) without
  32. * faulting. We are identity-mapped, so the fetch after clearing PG is
  33. * safe; the jump flushes the prefetch queue. */
  34. movl %cr0, %eax
  35. andl $0x7FFFFFFF, %eax /* clear CR0.PG */
  36. movl %eax, %cr0
  37. jmp 1f
  38. 1:
  39. /* Mask all IRQs. */
  40. movb $0xFF, %al
  41. outb %al, $0x21
  42. outb %al, $0xA1
  43. /* Build flat GDT (base 0 / 4 GB) at 0x900 and load it. */
  44. movl $0x900, %edi
  45. movl $0, 0(%edi) /* null */
  46. movl $0, 4(%edi)
  47. movl $0x0000FFFF, 8(%edi) /* code: limit 0xFFFF, base low 0 */
  48. movl $0x00CF9A00, 12(%edi)/* code: base mid 0, 0x9A, 0xCF, base hi 0 */
  49. movl $0x0000FFFF, 16(%edi)/* data: limit 0xFFFF, base low 0 */
  50. movl $0x00CF9200, 20(%edi)/* data: base mid 0, 0x92, 0xCF, base hi 0 */
  51. movw $0x17, 24(%edi) /* gdtr limit = 23 */
  52. movl $0x900, 26(%edi) /* gdtr base = 0x900 */
  53. lgdt 24(%edi)
  54. popl %edx
  55. popl %ecx
  56. popl %ebx
  57. popl %eax
  58. /* Copy new kernel: src=eax -> dst=ebx, count=ecx. */
  59. movl %eax, %esi
  60. movl %ebx, %edi
  61. cld
  62. rep movsb
  63. /* Flush the caches so the freshly-copied kernel is coherent. The Pentium
  64. * (P5) split I/D caches are not coherent for self-modifying code; without
  65. * this the jump below can execute stale instructions. */
  66. wbinvd
  67. jmp *%edx
  68. .globl kexec_trampoline_end
  69. kexec_trampoline_end:
  70. nop