kexec.c 9.4 KB

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