kexec.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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..0x30000 (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 0x50000 /* 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 0x28200 /* max = kernel region 0x7E00..0x30000 */
  31. volatile int kexec_busy;
  32. volatile unsigned int kexec_progress;
  33. volatile unsigned int kexec_total;
  34. extern unsigned char kexec_trampoline[];
  35. extern unsigned char kexec_trampoline_end[];
  36. int kexec_pending(void)
  37. {
  38. unsigned char b, x, e, c;
  39. /* Scan for the "KXEC" magic, draining any leading bytes that can't begin
  40. * it. This self-heals after a stray/partial/overflowed transfer, so stale
  41. * bytes in the ring buffer can never permanently block detection. */
  42. for (;;) {
  43. if (!serial_ring_peek(0, &b))
  44. return 0; /* buffer empty */
  45. if (b != 'K') {
  46. (void)serial_ring_getc(); /* stale byte, drop it */
  47. continue;
  48. }
  49. /* First byte is 'K'. Report pending only once all 4 bytes are here. */
  50. if (serial_ring_peek(1, &x) && serial_ring_peek(2, &e) &&
  51. serial_ring_peek(3, &c)) {
  52. if (x == 'X' && e == 'E' && c == 'C')
  53. return 1; /* "KXEC" found */
  54. (void)serial_ring_getc(); /* false 'K' start, drop it */
  55. continue;
  56. }
  57. return 0; /* 'K' seen but magic incomplete; wait */
  58. }
  59. }
  60. /* Drain whatever partial data remains in the ring buffer after a failure so a
  61. * subsequent attempt starts clean. */
  62. static void kexec_drain(void)
  63. {
  64. while (serial_has_data(SERIAL_COM1))
  65. (void)serial_ring_getc();
  66. }
  67. static int kexec_fail(const char *msg)
  68. {
  69. if (msg)
  70. io_putstr(msg);
  71. kexec_drain();
  72. return -1;
  73. }
  74. static int kexec_recv(unsigned int *entry, unsigned int *size)
  75. {
  76. volatile unsigned char *dst = (volatile unsigned char *)KEXEC_STAGE;
  77. unsigned int i, magic = 0, n;
  78. unsigned char c, sum = 0, chk;
  79. for (i = 0; i < 4; i++) {
  80. if (!serial_ring_wait(5000, &c))
  81. return kexec_fail("kexec: timeout (magic)\n");
  82. magic |= (unsigned int)c << (8 * i);
  83. }
  84. if (magic != KEXEC_MAGIC)
  85. return kexec_fail("kexec: bad magic\n");
  86. *entry = 0;
  87. for (i = 0; i < 4; i++) {
  88. if (!serial_ring_wait(5000, &c))
  89. return kexec_fail("kexec: timeout (entry)\n");
  90. *entry |= (unsigned int)c << (8 * i);
  91. }
  92. *size = 0;
  93. for (i = 0; i < 4; i++) {
  94. if (!serial_ring_wait(5000, &c))
  95. return kexec_fail("kexec: timeout (size)\n");
  96. *size |= (unsigned int)c << (8 * i);
  97. }
  98. if (*size == 0 || *size > KEXEC_MAX)
  99. return kexec_fail("kexec: bad size\n");
  100. kexec_total = *size;
  101. kexec_progress = 0;
  102. for (n = 0; n < *size; n++) {
  103. if (!serial_ring_wait(5000, &c))
  104. return kexec_fail("kexec: timeout (data)\n");
  105. dst[n] = c;
  106. sum += c;
  107. kexec_progress = n + 1;
  108. if ((n & 0xFFF) == 0xFFF)
  109. io_putchar('.');
  110. }
  111. io_putchar('\n');
  112. if (!serial_ring_wait(5000, &chk))
  113. return kexec_fail("kexec: timeout (checksum)\n");
  114. if (chk != sum)
  115. return kexec_fail("kexec: checksum mismatch\n");
  116. return 0;
  117. }
  118. void program_kexec_main(void)
  119. {
  120. unsigned int entry = 0, size = 0;
  121. io_putstr("kexec: waiting for kernel image over serial...\n");
  122. if (kexec_recv(&entry, &size) != 0) {
  123. io_putstr("kexec: aborted\n");
  124. kexec_busy = 0;
  125. return;
  126. }
  127. io_putstr("kexec: received ");
  128. io_puthex(size);
  129. io_putstr(" bytes, entry ");
  130. io_puthex(entry);
  131. io_putstr("\n");
  132. /* Relocate the trampoline to 0x800 so it survives the kernel overwrite. */
  133. {
  134. const void *s = (const void *)kexec_trampoline;
  135. void *d = (void *)KEXEC_TRAMP;
  136. unsigned int n = (unsigned int)(kexec_trampoline_end - kexec_trampoline);
  137. __asm__ volatile (
  138. "cld\n\t"
  139. "rep movsb"
  140. : "+S"(s), "+D"(d), "+c"(n)
  141. :
  142. : "memory");
  143. }
  144. /* Serialize so the freshly-written trampoline is visible to fetch. */
  145. __asm__ volatile ("cpuid" ::: "eax", "ebx", "ecx", "edx");
  146. /* Jump to the trampoline with args in eax/ebx/ecx/edx. Never returns. */
  147. __asm__ volatile (
  148. "jmp *%4"
  149. :
  150. : "a"(KEXEC_STAGE), "b"(KEXEC_LOAD), "c"(size), "d"(entry), "r"(KEXEC_TRAMP)
  151. : "memory");
  152. }