bootstrap.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 "KXEC" (LE)
  12. * 4 bytes: entry address (LE)
  13. * 4 bytes: size (LE)
  14. * N bytes: kernel.bin data
  15. * 1 byte: checksum (sum of data bytes mod 256)
  16. */
  17. #define UART_BASE 0x3F8
  18. #define STAGE_ADDR 0x58000
  19. #define KERNEL_LOAD_ADDR 0x7E00
  20. #define BOOT_STACK 0x49800
  21. #define MAGIC 0x4345584B /* "KXEC" little-endian */
  22. #define MAX_SIZE 0x3C200
  23. static inline void outb(unsigned short port, unsigned char v)
  24. {
  25. __asm__ volatile ("outb %0, %1" : : "a"(v), "Nd"(port));
  26. }
  27. static inline unsigned char inb(unsigned short port)
  28. {
  29. unsigned char v;
  30. __asm__ volatile ("inb %1, %0" : "=a"(v) : "Nd"(port));
  31. return v;
  32. }
  33. static void uart_115200(void)
  34. {
  35. outb(UART_BASE + 1, 0x00); /* IER off */
  36. outb(UART_BASE + 3, 0x80); /* DLAB */
  37. outb(UART_BASE + 0, 0x01); /* divisor low = 1 (115200) */
  38. outb(UART_BASE + 1, 0x00); /* divisor high = 0 */
  39. outb(UART_BASE + 3, 0x03); /* 8N1 */
  40. outb(UART_BASE + 2, 0xC7); /* FIFO on, clear */
  41. outb(UART_BASE + 4, 0x03); /* DTR|RTS */
  42. }
  43. static unsigned char read_byte(void)
  44. {
  45. while (!(inb(UART_BASE + 5) & 0x01))
  46. ;
  47. return inb(UART_BASE + 0);
  48. }
  49. static unsigned int read32le(void)
  50. {
  51. unsigned int v = 0;
  52. unsigned int i;
  53. for (i = 0; i < 4; i++)
  54. v |= (unsigned int)read_byte() << (8 * i);
  55. return v;
  56. }
  57. static void delay(void)
  58. {
  59. volatile unsigned int i;
  60. for (i = 0; i < 50000; i++)
  61. ;
  62. }
  63. static void halt(void)
  64. {
  65. for (;;)
  66. __asm__ volatile ("hlt");
  67. }
  68. /* Flat GDT (base 0 / 4 GB) + GDTR, written to 0x900 (below the kernel) so
  69. * the new kernel's segment reloads don't read the old GDT we overwrite.
  70. * Layout: 24-byte GDT at 0x900, 6-byte GDTR at 0x918 (limit 23, base 0x900). */
  71. static const unsigned char flat_gdt_blob[30] = {
  72. 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, /* null */
  73. 0xFF,0xFF,0x00,0x00, 0x00,0x9A,0xCF,0x00, /* code */
  74. 0xFF,0xFF,0x00,0x00, 0x00,0x92,0xCF,0x00, /* data */
  75. 0x17,0x00, /* gdtr limit = 23 */
  76. 0x00,0x09,0x00,0x00 /* gdtr base = 0x900 */
  77. };
  78. static void copy_to(unsigned int dst, const void *src, unsigned int n)
  79. {
  80. __asm__ volatile (
  81. "cld\n\t"
  82. "rep movsb"
  83. : "+S"(src), "+D"(dst), "+c"(n)
  84. :
  85. : "memory");
  86. }
  87. static void setup_flat_gdt(void)
  88. {
  89. copy_to(0x900, flat_gdt_blob, sizeof(flat_gdt_blob));
  90. __asm__ volatile ("lgdt (%0)" : : "r"(0x918));
  91. }
  92. void _start(void)
  93. {
  94. unsigned char c, sum = 0, chk;
  95. unsigned int magic, entry, size, i;
  96. /* Stop the PIT (and all IRQs) so the kernel's serial_poll stops
  97. * competing with us for UART bytes. */
  98. __asm__ volatile ("cli");
  99. outb(0x21, 0xFF);
  100. outb(0xA1, 0xFF);
  101. uart_115200();
  102. /* Drain stale bytes. */
  103. while (inb(UART_BASE + 5) & 0x01)
  104. (void)inb(UART_BASE + 0);
  105. /* Emit 'R' until the host starts sending the header. */
  106. for (;;) {
  107. outb(UART_BASE + 0, 'R');
  108. delay();
  109. if (inb(UART_BASE + 5) & 0x01)
  110. break;
  111. }
  112. magic = read32le();
  113. entry = read32le();
  114. size = read32le();
  115. if (magic != MAGIC || size == 0 || size > MAX_SIZE) {
  116. outb(UART_BASE + 0, 'E');
  117. halt();
  118. }
  119. for (i = 0; i < size; i++) {
  120. c = read_byte();
  121. ((volatile unsigned char *)STAGE_ADDR)[i] = c;
  122. sum += c;
  123. }
  124. chk = read_byte();
  125. if (chk != sum) {
  126. outb(UART_BASE + 0, 'E');
  127. halt();
  128. }
  129. /* Copy staged kernel over 0x7E00, then jump to its kernel_main.
  130. * We live at 0x4A000, outside the overwritten region, so this is safe. */
  131. setup_flat_gdt();
  132. for (i = 0; i < size; i++)
  133. ((volatile unsigned char *)KERNEL_LOAD_ADDR)[i] =
  134. ((volatile unsigned char *)STAGE_ADDR)[i];
  135. __asm__ volatile ("movl %0, %%esp; jmp *%1" : : "i"(BOOT_STACK), "r"(entry));
  136. }