bootstrap.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* One-time kexec bootstrap (loaded via the existing `serial recv` + `run serial`
  2. * path at 9600 baud). Because nebos runs everything in ring-0, this program has
  3. * full hardware access: it switches COM1 to 115200, receives a new kernel image,
  4. * copies it over 0x7E00, and jumps to its kernel_main.
  5. *
  6. * Self-contained: no kernel symbols, no run_api dependency. We live at 0x4A000
  7. * (outside the 0x7E00..0x49000 kernel region), so we can overwrite the kernel
  8. * directly from our own code without a relocated trampoline.
  9. *
  10. * Wire protocol (host -> us), after we emit 'R':
  11. * 4 bytes: magic "KXE2" (LE)
  12. * 4 bytes: entry address (LE)
  13. * 4 bytes: kernel_size (LE)
  14. * 4 bytes: pack_size (LE) -- consumed but not stored (see below)
  15. * N bytes: kernel.bin data
  16. * M bytes: rootfs pack data
  17. * 1 byte: checksum (sum of both payloads mod 256)
  18. *
  19. * The bootstrap only installs the kernel; the rootfs pack arrives with the
  20. * next normal `make dev`/`make net-dev` (or `make cmds-dev`).
  21. */
  22. #define UART_BASE 0x3F8
  23. #define STAGE_ADDR 0x58000
  24. #define KERNEL_LOAD_ADDR 0x7E00
  25. #define BOOT_STACK 0x49800
  26. #define MAGIC 0x3245584B /* "KXE2" little-endian */
  27. #define MAX_SIZE 0x3C200
  28. static inline void outb(unsigned short port, unsigned char v)
  29. {
  30. __asm__ volatile ("outb %0, %1" : : "a"(v), "Nd"(port));
  31. }
  32. static inline unsigned char inb(unsigned short port)
  33. {
  34. unsigned char v;
  35. __asm__ volatile ("inb %1, %0" : "=a"(v) : "Nd"(port));
  36. return v;
  37. }
  38. static void uart_115200(void)
  39. {
  40. outb(UART_BASE + 1, 0x00); /* IER off */
  41. outb(UART_BASE + 3, 0x80); /* DLAB */
  42. outb(UART_BASE + 0, 0x01); /* divisor low = 1 (115200) */
  43. outb(UART_BASE + 1, 0x00); /* divisor high = 0 */
  44. outb(UART_BASE + 3, 0x03); /* 8N1 */
  45. outb(UART_BASE + 2, 0xC7); /* FIFO on, clear */
  46. outb(UART_BASE + 4, 0x03); /* DTR|RTS */
  47. }
  48. static unsigned char read_byte(void)
  49. {
  50. while (!(inb(UART_BASE + 5) & 0x01))
  51. ;
  52. return inb(UART_BASE + 0);
  53. }
  54. static unsigned int read32le(void)
  55. {
  56. unsigned int v = 0;
  57. unsigned int i;
  58. for (i = 0; i < 4; i++)
  59. v |= (unsigned int)read_byte() << (8 * i);
  60. return v;
  61. }
  62. static void delay(void)
  63. {
  64. volatile unsigned int i;
  65. for (i = 0; i < 50000; i++)
  66. ;
  67. }
  68. static void halt(void)
  69. {
  70. for (;;)
  71. __asm__ volatile ("hlt");
  72. }
  73. /* Flat GDT (base 0 / 4 GB) + GDTR, written to 0x900 (below the kernel) so
  74. * the new kernel's segment reloads don't read the old GDT we overwrite.
  75. * Layout: 24-byte GDT at 0x900, 6-byte GDTR at 0x918 (limit 23, base 0x900). */
  76. static const unsigned char flat_gdt_blob[30] = {
  77. 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, /* null */
  78. 0xFF,0xFF,0x00,0x00, 0x00,0x9A,0xCF,0x00, /* code */
  79. 0xFF,0xFF,0x00,0x00, 0x00,0x92,0xCF,0x00, /* data */
  80. 0x17,0x00, /* gdtr limit = 23 */
  81. 0x00,0x09,0x00,0x00 /* gdtr base = 0x900 */
  82. };
  83. static void copy_to(unsigned int dst, const void *src, unsigned int n)
  84. {
  85. __asm__ volatile (
  86. "cld\n\t"
  87. "rep movsb"
  88. : "+S"(src), "+D"(dst), "+c"(n)
  89. :
  90. : "memory");
  91. }
  92. static void setup_flat_gdt(void)
  93. {
  94. copy_to(0x900, flat_gdt_blob, sizeof(flat_gdt_blob));
  95. __asm__ volatile ("lgdt (%0)" : : "r"(0x918));
  96. }
  97. void _start(void)
  98. {
  99. unsigned char c, sum = 0, chk;
  100. unsigned int magic, entry, size, pack_size, i;
  101. /* Stop the PIT (and all IRQs) so the kernel's serial_poll stops
  102. * competing with us for UART bytes. */
  103. __asm__ volatile ("cli");
  104. outb(0x21, 0xFF);
  105. outb(0xA1, 0xFF);
  106. uart_115200();
  107. /* Drain stale bytes. */
  108. while (inb(UART_BASE + 5) & 0x01)
  109. (void)inb(UART_BASE + 0);
  110. /* Emit 'R' until the host starts sending the header. */
  111. for (;;) {
  112. outb(UART_BASE + 0, 'R');
  113. delay();
  114. if (inb(UART_BASE + 5) & 0x01)
  115. break;
  116. }
  117. magic = read32le();
  118. entry = read32le();
  119. size = read32le();
  120. pack_size = read32le();
  121. if (magic != MAGIC || size == 0 || size > MAX_SIZE ||
  122. pack_size > 0x400000) {
  123. outb(UART_BASE + 0, 'E');
  124. halt();
  125. }
  126. for (i = 0; i < size; i++) {
  127. c = read_byte();
  128. ((volatile unsigned char *)STAGE_ADDR)[i] = c;
  129. sum += c;
  130. }
  131. /* Consume the rootfs bytes; the kernel gets them on the next reload. */
  132. for (i = 0; i < pack_size; i++)
  133. sum += read_byte();
  134. chk = read_byte();
  135. if (chk != sum) {
  136. outb(UART_BASE + 0, 'E');
  137. halt();
  138. }
  139. /* Copy staged kernel over 0x7E00, then jump to its kernel_main.
  140. * We live at 0x4A000, outside the overwritten region, so this is safe. */
  141. setup_flat_gdt();
  142. for (i = 0; i < size; i++)
  143. ((volatile unsigned char *)KERNEL_LOAD_ADDR)[i] =
  144. ((volatile unsigned char *)STAGE_ADDR)[i];
  145. __asm__ volatile ("movl %0, %%esp; jmp *%1" : : "i"(BOOT_STACK), "r"(entry));
  146. }