kexec.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /* kexec: receive a new kernel + rootfs pack 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 "KXE2" (LE) -- new magic: old kernels ignore it
  18. * 4 bytes: entry address (LE) -- new kernel_entry VMA
  19. * 4 bytes: kernel_size (LE) -- kernel.bin byte count
  20. * 4 bytes: pack_size (LE) -- rootfs gzip byte count (0 = keep running)
  21. * N bytes: kernel.bin data
  22. * M bytes: rootfs pack data
  23. * 1 byte: checksum (sum of both payloads mod 256)
  24. */
  25. #include "kexec.h"
  26. #include "bootinfo.h"
  27. #include "io.h"
  28. #include "mm.h"
  29. #include "ramfs.h"
  30. #include "serial.h"
  31. #include "smp.h"
  32. #define KEXEC_MAGIC 0x3245584B /* "KXE2" little-endian */
  33. /* Staging buffer for the received image: allocated from the frame allocator
  34. * when the header arrives, so no fixed low-memory region is needed. It sits
  35. * above the VGA shadow reserve (>= 0x900000), far from the 0x7E00 load
  36. * address, so the trampoline's forward copy can never overlap itself. */
  37. unsigned int kexec_stage;
  38. static unsigned int kexec_stage_pages;
  39. int kexec_stage_alloc(unsigned int size)
  40. {
  41. unsigned int pages;
  42. if (size == 0 || size > KEXEC_MAX)
  43. return 0;
  44. pages = (size + 0xFFF) >> 12;
  45. kexec_stage = mm_alloc_pages(pages);
  46. if (!kexec_stage)
  47. return 0;
  48. kexec_stage_pages = pages;
  49. return 1;
  50. }
  51. void kexec_stage_free(void)
  52. {
  53. unsigned int i;
  54. for (i = 0; i < kexec_stage_pages; i++)
  55. mm_free_page(kexec_stage + i * 0x1000);
  56. kexec_stage_pages = 0;
  57. kexec_stage = 0;
  58. }
  59. #define KEXEC_TRAMP 0x800 /* relocated trampoline (below the kernel) */
  60. volatile int kexec_busy;
  61. volatile unsigned int kexec_progress;
  62. volatile unsigned int kexec_total;
  63. extern unsigned char kexec_trampoline[];
  64. extern unsigned char kexec_trampoline_end[];
  65. int kexec_pending(void)
  66. {
  67. unsigned char b, x, e, c;
  68. /* Scan for the "KXE2" magic, draining any leading bytes that can't begin
  69. * it. This self-heals after a stray/partial/overflowed transfer, so stale
  70. * bytes in the ring buffer can never permanently block detection. */
  71. for (;;) {
  72. if (!serial_ring_peek(0, &b))
  73. return 0; /* buffer empty */
  74. if (b != 'K') {
  75. (void)serial_ring_getc(); /* stale byte, drop it */
  76. continue;
  77. }
  78. /* First byte is 'K'. Report pending only once all 4 bytes are here. */
  79. if (serial_ring_peek(1, &x) && serial_ring_peek(2, &e) &&
  80. serial_ring_peek(3, &c)) {
  81. if (x == 'X' && e == 'E' && c == '2')
  82. return 1; /* "KXE2" found */
  83. (void)serial_ring_getc(); /* false 'K' start, drop it */
  84. continue;
  85. }
  86. return 0; /* 'K' seen but magic incomplete; wait */
  87. }
  88. }
  89. /* Drain whatever partial data remains in the ring buffer after a failure so a
  90. * subsequent attempt starts clean. */
  91. static void kexec_drain(void)
  92. {
  93. while (serial_has_data(SERIAL_COM1))
  94. (void)serial_ring_getc();
  95. }
  96. static int kexec_fail(const char *msg)
  97. {
  98. if (msg)
  99. io_putstr(msg);
  100. kexec_drain();
  101. return -1;
  102. }
  103. /* Publish the boot-info block the new kernel reads. The pack source is the
  104. * staging tail for a full reload, or the running ramfs for a kernel-only one;
  105. * ramfs_init() takes its own copy and releases the region. */
  106. static void kexec_write_bootinfo(unsigned int pack_addr, unsigned int pack_size)
  107. {
  108. volatile struct boot_info *bi =
  109. (volatile struct boot_info *)BOOT_INFO_ADDR;
  110. unsigned int words[BOOT_INFO_WORDS];
  111. unsigned int rsdp = 0, flags = 0, sum = 0, i;
  112. if (bootinfo_valid()) {
  113. rsdp = bootinfo_rsdp();
  114. flags = bootinfo_flags();
  115. }
  116. for (i = 0; i < BOOT_INFO_WORDS; i++)
  117. words[i] = 0;
  118. words[0] = BOOT_INFO_MAGIC;
  119. words[1] = BOOT_INFO_VERSION;
  120. words[3] = rsdp;
  121. words[4] = flags;
  122. words[5] = pack_addr;
  123. words[6] = pack_size;
  124. for (i = 0; i < BOOT_INFO_WORDS; i++)
  125. sum += words[i];
  126. words[2] = 0u - sum;
  127. bi->magic = words[0];
  128. bi->version = words[1];
  129. bi->checksum = words[2];
  130. bi->rsdp = words[3];
  131. bi->flags = words[4];
  132. bi->pack_addr = words[5];
  133. bi->pack_size = words[6];
  134. for (i = 0; i < 5; i++)
  135. bi->reserved[i] = words[7 + i];
  136. }
  137. static int kexec_recv(unsigned int *entry, unsigned int *kernel_size,
  138. unsigned int *pack_size)
  139. {
  140. unsigned int i, magic = 0, n, total;
  141. unsigned char c, sum = 0, chk;
  142. volatile unsigned char *dst;
  143. for (i = 0; i < 4; i++) {
  144. if (!serial_ring_wait(5000, &c))
  145. return kexec_fail("kexec: timeout (magic)\n");
  146. magic |= (unsigned int)c << (8 * i);
  147. }
  148. if (magic != KEXEC_MAGIC)
  149. return kexec_fail("kexec: bad magic\n");
  150. *entry = 0;
  151. for (i = 0; i < 4; i++) {
  152. if (!serial_ring_wait(5000, &c))
  153. return kexec_fail("kexec: timeout (entry)\n");
  154. *entry |= (unsigned int)c << (8 * i);
  155. }
  156. *kernel_size = 0;
  157. for (i = 0; i < 4; i++) {
  158. if (!serial_ring_wait(5000, &c))
  159. return kexec_fail("kexec: timeout (kernel size)\n");
  160. *kernel_size |= (unsigned int)c << (8 * i);
  161. }
  162. *pack_size = 0;
  163. for (i = 0; i < 4; i++) {
  164. if (!serial_ring_wait(5000, &c))
  165. return kexec_fail("kexec: timeout (pack size)\n");
  166. *pack_size |= (unsigned int)c << (8 * i);
  167. }
  168. if (*kernel_size == 0 || *kernel_size > KEXEC_MAX ||
  169. *pack_size > KEXEC_MAX ||
  170. *kernel_size + *pack_size < *kernel_size ||
  171. *kernel_size + *pack_size > KEXEC_MAX)
  172. return kexec_fail("kexec: bad size\n");
  173. total = *kernel_size + *pack_size;
  174. if (!kexec_stage_alloc(total))
  175. return kexec_fail("kexec: out of memory (staging)\n");
  176. dst = (volatile unsigned char *)kexec_stage;
  177. kexec_total = total;
  178. kexec_progress = 0;
  179. for (n = 0; n < total; n++) {
  180. if (!serial_ring_wait(5000, &c)) {
  181. kexec_stage_free();
  182. return kexec_fail("kexec: timeout (data)\n");
  183. }
  184. dst[n] = c;
  185. sum += c;
  186. kexec_progress = n + 1;
  187. if ((n & 0xFFF) == 0xFFF)
  188. io_putchar('.');
  189. }
  190. io_putchar('\n');
  191. if (!serial_ring_wait(5000, &chk)) {
  192. kexec_stage_free();
  193. return kexec_fail("kexec: timeout (checksum)\n");
  194. }
  195. if (chk != sum) {
  196. kexec_stage_free();
  197. return kexec_fail("kexec: checksum mismatch\n");
  198. }
  199. return 0;
  200. }
  201. void program_kexec_main(void)
  202. {
  203. unsigned int entry = 0, kernel_size = 0, pack_size = 0;
  204. io_putstr("kexec: waiting for kernel/rootfs over serial...\n");
  205. if (kexec_recv(&entry, &kernel_size, &pack_size) != 0) {
  206. io_putstr("kexec: aborted\n");
  207. kexec_busy = 0;
  208. return;
  209. }
  210. io_putstr("kexec: received kernel ");
  211. io_puthex(kernel_size);
  212. io_putstr(" bytes, rootfs ");
  213. io_puthex(pack_size);
  214. io_putstr(" bytes, entry ");
  215. io_puthex(entry);
  216. io_putstr("\n");
  217. if (pack_size)
  218. kexec_jump(entry, kernel_size, kexec_stage + kernel_size,
  219. pack_size);
  220. else
  221. kexec_jump(entry, kernel_size, 0, 0);
  222. }
  223. /* Relocate the trampoline to 0x800 (it survives the kernel overwrite) and
  224. * jump to it with the staging/load/size/entry arguments. Never returns.
  225. * A zero pack_size keeps the running rootfs. */
  226. void kexec_jump(unsigned int entry, unsigned int kernel_size,
  227. unsigned int pack_addr, unsigned int pack_size)
  228. {
  229. if (pack_size == 0) {
  230. pack_addr = (unsigned int)ramfs_pack_base();
  231. pack_size = ramfs_pack_size();
  232. }
  233. /* Park the APs first: the new kernel image is about to overwrite the
  234. * kernel they are executing. INIT puts them back in real mode where
  235. * they wait for the new kernel's SIPI. */
  236. smp_stop_aps();
  237. kexec_write_bootinfo(pack_addr, pack_size);
  238. /* Relocate the trampoline to 0x800 so it survives the kernel overwrite. */
  239. {
  240. const void *s = (const void *)kexec_trampoline;
  241. void *d = (void *)KEXEC_TRAMP;
  242. unsigned int n = (unsigned int)(kexec_trampoline_end - kexec_trampoline);
  243. __asm__ volatile (
  244. "cld\n\t"
  245. "rep movsb"
  246. : "+S"(s), "+D"(d), "+c"(n)
  247. :
  248. : "memory");
  249. }
  250. /* Serialize so the freshly-written trampoline is visible to fetch. */
  251. __asm__ volatile ("cpuid" ::: "eax", "ebx", "ecx", "edx");
  252. /* Jump to the trampoline with args in eax/ebx/ecx/edx. Only the kernel
  253. * bytes are copied; the pack stays in the staging buffer. Never returns. */
  254. __asm__ volatile (
  255. "jmp *%4"
  256. :
  257. : "a"(kexec_stage), "b"(KEXEC_LOAD), "c"(kernel_size), "d"(entry), "r"(KEXEC_TRAMP)
  258. : "memory");
  259. }