kexec_tramp.S 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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_main)
  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. pushl %eax
  21. pushl %ebx
  22. pushl %ecx
  23. pushl %edx
  24. /* Disable paging so the copy below can overwrite the old kernel's page
  25. * tables (which live inside the kernel region we overwrite) without
  26. * faulting. We are identity-mapped, so the fetch after clearing PG is
  27. * safe; the jump flushes the prefetch queue. */
  28. movl %cr0, %eax
  29. andl $0x7FFFFFFF, %eax /* clear CR0.PG */
  30. movl %eax, %cr0
  31. jmp 1f
  32. 1:
  33. /* Mask all IRQs. */
  34. movb $0xFF, %al
  35. outb %al, $0x21
  36. outb %al, $0xA1
  37. /* Build flat GDT (base 0 / 4 GB) at 0x900 and load it. */
  38. movl $0x900, %edi
  39. movl $0, 0(%edi) /* null */
  40. movl $0, 4(%edi)
  41. movl $0x0000FFFF, 8(%edi) /* code: limit 0xFFFF, base low 0 */
  42. movl $0x00CF9A00, 12(%edi)/* code: base mid 0, 0x9A, 0xCF, base hi 0 */
  43. movl $0x0000FFFF, 16(%edi)/* data: limit 0xFFFF, base low 0 */
  44. movl $0x00CF9200, 20(%edi)/* data: base mid 0, 0x92, 0xCF, base hi 0 */
  45. movw $0x17, 24(%edi) /* gdtr limit = 23 */
  46. movl $0x900, 26(%edi) /* gdtr base = 0x900 */
  47. lgdt 24(%edi)
  48. popl %edx
  49. popl %ecx
  50. popl %ebx
  51. popl %eax
  52. /* Copy new kernel: src=eax -> dst=ebx, count=ecx. */
  53. movl %eax, %esi
  54. movl %ebx, %edi
  55. cld
  56. rep movsb
  57. /* Flush the caches so the freshly-copied kernel is coherent. The Pentium
  58. * (P5) split I/D caches are not coherent for self-modifying code; without
  59. * this the jump below can execute stale instructions. */
  60. wbinvd
  61. movl $0x40800, %esp
  62. jmp *%edx
  63. .globl kexec_trampoline_end
  64. kexec_trampoline_end:
  65. nop