kexec.c 8.7 KB

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