kexec.c 6.3 KB

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