kexec.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. flags |= BOOTINFO_FLAG_NET;
  142. words[7] = pack_ip(ip);
  143. words[8] = pack_ip(mask);
  144. words[9] = pack_ip(gw);
  145. words[10] = pack_ip(dns);
  146. }
  147. for (i = 0; i < BOOT_INFO_WORDS; i++)
  148. sum += words[i];
  149. words[2] = 0u - sum;
  150. bi->magic = words[0];
  151. bi->version = words[1];
  152. bi->checksum = words[2];
  153. bi->rsdp = words[3];
  154. bi->flags = words[4];
  155. bi->pack_addr = words[5];
  156. bi->pack_size = words[6];
  157. for (i = 0; i < 5; i++)
  158. bi->reserved[i] = words[7 + i];
  159. }
  160. static int kexec_recv(unsigned int *entry, unsigned int *kernel_size,
  161. unsigned int *pack_size)
  162. {
  163. unsigned int i, magic = 0, n, total;
  164. unsigned char c, sum = 0, chk;
  165. volatile unsigned char *dst;
  166. for (i = 0; i < 4; i++) {
  167. if (!serial_ring_wait(5000, &c))
  168. return kexec_fail("kexec: timeout (magic)\n");
  169. magic |= (unsigned int)c << (8 * i);
  170. }
  171. if (magic != KEXEC_MAGIC)
  172. return kexec_fail("kexec: bad magic\n");
  173. *entry = 0;
  174. for (i = 0; i < 4; i++) {
  175. if (!serial_ring_wait(5000, &c))
  176. return kexec_fail("kexec: timeout (entry)\n");
  177. *entry |= (unsigned int)c << (8 * i);
  178. }
  179. *kernel_size = 0;
  180. for (i = 0; i < 4; i++) {
  181. if (!serial_ring_wait(5000, &c))
  182. return kexec_fail("kexec: timeout (kernel size)\n");
  183. *kernel_size |= (unsigned int)c << (8 * i);
  184. }
  185. *pack_size = 0;
  186. for (i = 0; i < 4; i++) {
  187. if (!serial_ring_wait(5000, &c))
  188. return kexec_fail("kexec: timeout (pack size)\n");
  189. *pack_size |= (unsigned int)c << (8 * i);
  190. }
  191. if (*kernel_size == 0 || *kernel_size > KEXEC_MAX ||
  192. *pack_size > KEXEC_MAX ||
  193. *kernel_size + *pack_size < *kernel_size ||
  194. *kernel_size + *pack_size > KEXEC_MAX)
  195. return kexec_fail("kexec: bad size\n");
  196. total = *kernel_size + *pack_size;
  197. if (!kexec_stage_alloc(total))
  198. return kexec_fail("kexec: out of memory (staging)\n");
  199. dst = (volatile unsigned char *)kexec_stage;
  200. kexec_total = total;
  201. kexec_progress = 0;
  202. for (n = 0; n < total; n++) {
  203. if (!serial_ring_wait(5000, &c)) {
  204. kexec_stage_free();
  205. return kexec_fail("kexec: timeout (data)\n");
  206. }
  207. dst[n] = c;
  208. sum += c;
  209. kexec_progress = n + 1;
  210. if ((n & 0xFFF) == 0xFFF)
  211. io_putchar('.');
  212. }
  213. io_putchar('\n');
  214. if (!serial_ring_wait(5000, &chk)) {
  215. kexec_stage_free();
  216. return kexec_fail("kexec: timeout (checksum)\n");
  217. }
  218. if (chk != sum) {
  219. kexec_stage_free();
  220. return kexec_fail("kexec: checksum mismatch\n");
  221. }
  222. return 0;
  223. }
  224. void program_kexec_main(void)
  225. {
  226. unsigned int entry = 0, kernel_size = 0, pack_size = 0;
  227. io_putstr("kexec: waiting for kernel/rootfs over serial...\n");
  228. if (kexec_recv(&entry, &kernel_size, &pack_size) != 0) {
  229. io_putstr("kexec: aborted\n");
  230. kexec_busy = 0;
  231. return;
  232. }
  233. io_putstr("kexec: received kernel ");
  234. io_puthex(kernel_size);
  235. io_putstr(" bytes, rootfs ");
  236. io_puthex(pack_size);
  237. io_putstr(" bytes, entry ");
  238. io_puthex(entry);
  239. io_putstr("\n");
  240. if (pack_size)
  241. kexec_jump(entry, kernel_size, kexec_stage + kernel_size,
  242. pack_size);
  243. else
  244. kexec_jump(entry, kernel_size, 0, 0);
  245. }
  246. /* Relocate the trampoline to 0x800 (it survives the kernel overwrite) and
  247. * jump to it with the staging/load/size/entry arguments. Never returns.
  248. * A zero pack_size keeps the running rootfs. */
  249. void kexec_jump(unsigned int entry, unsigned int kernel_size,
  250. unsigned int pack_addr, unsigned int pack_size)
  251. {
  252. if (pack_size == 0) {
  253. pack_addr = (unsigned int)ramfs_pack_base();
  254. pack_size = ramfs_pack_size();
  255. }
  256. /* Stop NIC DMA before overwriting the kernel: otherwise the old
  257. * receive ring keeps writing into pages the new kernel reuses. */
  258. netdev_shutdown();
  259. /* Park the APs first: the new kernel image is about to overwrite the
  260. * kernel they are executing. INIT puts them back in real mode where
  261. * they wait for the new kernel's SIPI. */
  262. smp_stop_aps();
  263. kexec_write_bootinfo(pack_addr, pack_size);
  264. /* Relocate the trampoline to 0x800 so it survives the kernel overwrite. */
  265. {
  266. const void *s = (const void *)kexec_trampoline;
  267. void *d = (void *)KEXEC_TRAMP;
  268. unsigned int n = (unsigned int)(kexec_trampoline_end - kexec_trampoline);
  269. __asm__ volatile (
  270. "cld\n\t"
  271. "rep movsb"
  272. : "+S"(s), "+D"(d), "+c"(n)
  273. :
  274. : "memory");
  275. }
  276. /* Serialize so the freshly-written trampoline is visible to fetch. */
  277. __asm__ volatile ("cpuid" ::: "eax", "ebx", "ecx", "edx");
  278. /* Jump to the trampoline with args in eax/ebx/ecx/edx. Only the kernel
  279. * bytes are copied; the pack stays in the staging buffer. Never returns. */
  280. __asm__ volatile (
  281. "jmp *%4"
  282. :
  283. : "a"(kexec_stage), "b"(KEXEC_LOAD), "c"(kernel_size), "d"(entry), "r"(KEXEC_TRAMP)
  284. : "memory");
  285. }