kexec.c 4.9 KB

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