kexec.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* kexec: receive a new kernel.bin over serial and jump into it.
  2. *
  3. * C-light reload: we do NOT return to real mode or re-run the VESA stage.
  4. * Instead we reuse the current framebuffer (the boot FB info at 0x600 and the
  5. * LFB are still valid) and jump straight to the new kernel's kernel_main,
  6. * which re-inits IDT/PIC/PIT/mouse/process/VGA-shadow/terminal from scratch.
  7. *
  8. * Because this code lives at 0x7E00..0x20000 (inside the region we overwrite),
  9. * the final "copy image to 0x7E00 and jump" is done by a small
  10. * position-independent trampoline relocated to 0x800 (below the kernel).
  11. *
  12. * The terminal polls kexec_pending() on its idle loop, so `make dev` on the
  13. * host alone triggers the reload (no typing required). The manual `kexec`
  14. * command is kept as a fallback that waits for the same image.
  15. *
  16. * Wire protocol (host -> us):
  17. * 4 bytes: magic "KXEC" (LE)
  18. * 4 bytes: entry address (LE) -- new kernel_main VMA
  19. * 4 bytes: size (LE) -- kernel.bin byte count
  20. * N bytes: kernel.bin data
  21. * 1 byte: checksum (sum of data bytes mod 256)
  22. */
  23. #include "kexec.h"
  24. #include "io.h"
  25. #include "serial.h"
  26. #define KEXEC_MAGIC 0x4345584B /* "KXEC" little-endian */
  27. #define KEXEC_STAGE 0x30000 /* staging buffer for the received image */
  28. #define KEXEC_LOAD 0x7E00 /* where the kernel must be placed */
  29. #define KEXEC_TRAMP 0x800 /* relocated trampoline (below the kernel) */
  30. #define KEXEC_MAX 0x10000 /* 64 KB cap (fits the staging region) */
  31. volatile int kexec_busy;
  32. extern unsigned char kexec_trampoline[];
  33. extern unsigned char kexec_trampoline_end[];
  34. int kexec_pending(void)
  35. {
  36. unsigned char b0 = 0, b1 = 0, b2 = 0, b3 = 0;
  37. if (serial_ring_peek(0, &b0) && serial_ring_peek(1, &b1) &&
  38. serial_ring_peek(2, &b2) && serial_ring_peek(3, &b3))
  39. return (b0 == 'K' && b1 == 'X' && b2 == 'E' && b3 == 'C');
  40. return 0;
  41. }
  42. /* Drain whatever partial data remains in the ring buffer after a failure so a
  43. * subsequent attempt starts clean. */
  44. static void kexec_drain(void)
  45. {
  46. while (serial_has_data(SERIAL_COM1))
  47. (void)serial_ring_getc();
  48. }
  49. static int kexec_fail(const char *msg)
  50. {
  51. if (msg)
  52. io_putstr(msg);
  53. kexec_drain();
  54. return -1;
  55. }
  56. static int kexec_recv(unsigned int *entry, unsigned int *size)
  57. {
  58. volatile unsigned char *dst = (volatile unsigned char *)KEXEC_STAGE;
  59. unsigned int i, magic = 0, n;
  60. unsigned char c, sum = 0, chk;
  61. for (i = 0; i < 4; i++) {
  62. if (!serial_ring_wait(5000, &c))
  63. return kexec_fail("kexec: timeout (magic)\n");
  64. magic |= (unsigned int)c << (8 * i);
  65. }
  66. if (magic != KEXEC_MAGIC)
  67. return kexec_fail("kexec: bad magic\n");
  68. *entry = 0;
  69. for (i = 0; i < 4; i++) {
  70. if (!serial_ring_wait(5000, &c))
  71. return kexec_fail("kexec: timeout (entry)\n");
  72. *entry |= (unsigned int)c << (8 * i);
  73. }
  74. *size = 0;
  75. for (i = 0; i < 4; i++) {
  76. if (!serial_ring_wait(5000, &c))
  77. return kexec_fail("kexec: timeout (size)\n");
  78. *size |= (unsigned int)c << (8 * i);
  79. }
  80. if (*size == 0 || *size > KEXEC_MAX)
  81. return kexec_fail("kexec: bad size\n");
  82. for (n = 0; n < *size; n++) {
  83. if (!serial_ring_wait(5000, &c))
  84. return kexec_fail("kexec: timeout (data)\n");
  85. dst[n] = c;
  86. sum += c;
  87. if ((n & 0xFFF) == 0xFFF)
  88. io_putchar('.');
  89. }
  90. io_putchar('\n');
  91. if (!serial_ring_wait(5000, &chk))
  92. return kexec_fail("kexec: timeout (checksum)\n");
  93. if (chk != sum)
  94. return kexec_fail("kexec: checksum mismatch\n");
  95. return 0;
  96. }
  97. void program_kexec_main(void)
  98. {
  99. unsigned int entry = 0, size = 0;
  100. io_putstr("kexec: waiting for kernel image over serial...\n");
  101. if (kexec_recv(&entry, &size) != 0) {
  102. io_putstr("kexec: aborted\n");
  103. kexec_busy = 0;
  104. return;
  105. }
  106. io_putstr("kexec: received ");
  107. io_puthex(size);
  108. io_putstr(" bytes, entry ");
  109. io_puthex(entry);
  110. io_putstr("\n");
  111. /* Relocate the trampoline to 0x800 so it survives the kernel overwrite. */
  112. {
  113. const void *s = (const void *)kexec_trampoline;
  114. void *d = (void *)KEXEC_TRAMP;
  115. unsigned int n = (unsigned int)(kexec_trampoline_end - kexec_trampoline);
  116. __asm__ volatile (
  117. "cld\n\t"
  118. "rep movsb"
  119. : "+S"(s), "+D"(d), "+c"(n)
  120. :
  121. : "memory");
  122. }
  123. /* Serialize so the freshly-written trampoline is visible to fetch. */
  124. __asm__ volatile ("cpuid" ::: "eax", "ebx", "ecx", "edx");
  125. /* Jump to the trampoline with args in eax/ebx/ecx/edx. Never returns. */
  126. __asm__ volatile (
  127. "jmp *%4"
  128. :
  129. : "a"(KEXEC_STAGE), "b"(KEXEC_LOAD), "c"(size), "d"(entry), "r"(KEXEC_TRAMP)
  130. : "memory");
  131. }