kexec.c 5.3 KB

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