kexec.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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_entry,
  6. * which re-inits IDT/PIC/PIT/mouse/process/VGA-shadow/terminal from scratch.
  7. *
  8. * Because this code lives inside the kernel region we overwrite, the final
  9. * "copy image to 0x7E00 and jump" is done by a small position-independent
  10. * 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_entry 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 "mm.h"
  26. #include "serial.h"
  27. #define KEXEC_MAGIC 0x4345584B /* "KXEC" little-endian */
  28. /* Staging buffer for the received image: allocated from the frame allocator
  29. * when the header arrives, so no fixed low-memory region is needed. It sits
  30. * above the VGA shadow reserve (>= 0x900000), far from the 0x7E00 load
  31. * address, so the trampoline's forward copy can never overlap itself. */
  32. unsigned int kexec_stage;
  33. static unsigned int kexec_stage_pages;
  34. int kexec_stage_alloc(unsigned int size)
  35. {
  36. unsigned int pages;
  37. if (size == 0 || size > KEXEC_MAX)
  38. return 0;
  39. pages = (size + 0xFFF) >> 12;
  40. kexec_stage = mm_alloc_pages(pages);
  41. if (!kexec_stage)
  42. return 0;
  43. kexec_stage_pages = pages;
  44. return 1;
  45. }
  46. void kexec_stage_free(void)
  47. {
  48. unsigned int i;
  49. for (i = 0; i < kexec_stage_pages; i++)
  50. mm_free_page(kexec_stage + i * 0x1000);
  51. kexec_stage_pages = 0;
  52. kexec_stage = 0;
  53. }
  54. #define KEXEC_TRAMP 0x800 /* relocated trampoline (below the kernel) */
  55. volatile int kexec_busy;
  56. volatile unsigned int kexec_progress;
  57. volatile unsigned int kexec_total;
  58. extern unsigned char kexec_trampoline[];
  59. extern unsigned char kexec_trampoline_end[];
  60. int kexec_pending(void)
  61. {
  62. unsigned char b, x, e, c;
  63. /* Scan for the "KXEC" magic, draining any leading bytes that can't begin
  64. * it. This self-heals after a stray/partial/overflowed transfer, so stale
  65. * bytes in the ring buffer can never permanently block detection. */
  66. for (;;) {
  67. if (!serial_ring_peek(0, &b))
  68. return 0; /* buffer empty */
  69. if (b != 'K') {
  70. (void)serial_ring_getc(); /* stale byte, drop it */
  71. continue;
  72. }
  73. /* First byte is 'K'. Report pending only once all 4 bytes are here. */
  74. if (serial_ring_peek(1, &x) && serial_ring_peek(2, &e) &&
  75. serial_ring_peek(3, &c)) {
  76. if (x == 'X' && e == 'E' && c == 'C')
  77. return 1; /* "KXEC" found */
  78. (void)serial_ring_getc(); /* false 'K' start, drop it */
  79. continue;
  80. }
  81. return 0; /* 'K' seen but magic incomplete; wait */
  82. }
  83. }
  84. /* Drain whatever partial data remains in the ring buffer after a failure so a
  85. * subsequent attempt starts clean. */
  86. static void kexec_drain(void)
  87. {
  88. while (serial_has_data(SERIAL_COM1))
  89. (void)serial_ring_getc();
  90. }
  91. static int kexec_fail(const char *msg)
  92. {
  93. if (msg)
  94. io_putstr(msg);
  95. kexec_drain();
  96. return -1;
  97. }
  98. static int kexec_recv(unsigned int *entry, unsigned int *size)
  99. {
  100. unsigned int i, magic = 0, n;
  101. unsigned char c, sum = 0, chk;
  102. volatile unsigned char *dst;
  103. for (i = 0; i < 4; i++) {
  104. if (!serial_ring_wait(5000, &c))
  105. return kexec_fail("kexec: timeout (magic)\n");
  106. magic |= (unsigned int)c << (8 * i);
  107. }
  108. if (magic != KEXEC_MAGIC)
  109. return kexec_fail("kexec: bad magic\n");
  110. *entry = 0;
  111. for (i = 0; i < 4; i++) {
  112. if (!serial_ring_wait(5000, &c))
  113. return kexec_fail("kexec: timeout (entry)\n");
  114. *entry |= (unsigned int)c << (8 * i);
  115. }
  116. *size = 0;
  117. for (i = 0; i < 4; i++) {
  118. if (!serial_ring_wait(5000, &c))
  119. return kexec_fail("kexec: timeout (size)\n");
  120. *size |= (unsigned int)c << (8 * i);
  121. }
  122. if (*size == 0 || *size > KEXEC_MAX)
  123. return kexec_fail("kexec: bad size\n");
  124. if (!kexec_stage_alloc(*size))
  125. return kexec_fail("kexec: out of memory (staging)\n");
  126. dst = (volatile unsigned char *)kexec_stage;
  127. kexec_total = *size;
  128. kexec_progress = 0;
  129. for (n = 0; n < *size; n++) {
  130. if (!serial_ring_wait(5000, &c)) {
  131. kexec_stage_free();
  132. return kexec_fail("kexec: timeout (data)\n");
  133. }
  134. dst[n] = c;
  135. sum += c;
  136. kexec_progress = n + 1;
  137. if ((n & 0xFFF) == 0xFFF)
  138. io_putchar('.');
  139. }
  140. io_putchar('\n');
  141. if (!serial_ring_wait(5000, &chk)) {
  142. kexec_stage_free();
  143. return kexec_fail("kexec: timeout (checksum)\n");
  144. }
  145. if (chk != sum) {
  146. kexec_stage_free();
  147. return kexec_fail("kexec: checksum mismatch\n");
  148. }
  149. return 0;
  150. }
  151. void program_kexec_main(void)
  152. {
  153. unsigned int entry = 0, size = 0;
  154. io_putstr("kexec: waiting for kernel image over serial...\n");
  155. if (kexec_recv(&entry, &size) != 0) {
  156. io_putstr("kexec: aborted\n");
  157. kexec_busy = 0;
  158. return;
  159. }
  160. io_putstr("kexec: received ");
  161. io_puthex(size);
  162. io_putstr(" bytes, entry ");
  163. io_puthex(entry);
  164. io_putstr("\n");
  165. kexec_jump(entry, size);
  166. }
  167. /* Relocate the trampoline to 0x800 (it survives the kernel overwrite) and
  168. * jump to it with the staging/load/size/entry arguments. Never returns. */
  169. void kexec_jump(unsigned int entry, unsigned int size)
  170. {
  171. /* Relocate the trampoline to 0x800 so it survives the kernel overwrite. */
  172. {
  173. const void *s = (const void *)kexec_trampoline;
  174. void *d = (void *)KEXEC_TRAMP;
  175. unsigned int n = (unsigned int)(kexec_trampoline_end - kexec_trampoline);
  176. __asm__ volatile (
  177. "cld\n\t"
  178. "rep movsb"
  179. : "+S"(s), "+D"(d), "+c"(n)
  180. :
  181. : "memory");
  182. }
  183. /* Serialize so the freshly-written trampoline is visible to fetch. */
  184. __asm__ volatile ("cpuid" ::: "eax", "ebx", "ecx", "edx");
  185. /* Jump to the trampoline with args in eax/ebx/ecx/edx. Never returns. */
  186. __asm__ volatile (
  187. "jmp *%4"
  188. :
  189. : "a"(kexec_stage), "b"(KEXEC_LOAD), "c"(size), "d"(entry), "r"(KEXEC_TRAMP)
  190. : "memory");
  191. }