kernel.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /* Kernel: entry point at 0x7E00, timer (PIT 1 kHz), process init, terminal. */
  2. #include "idle.h"
  3. #include "io.h"
  4. #include "kmalloc.h"
  5. #include "mm.h"
  6. #include "mouse.h"
  7. #include "paging.h"
  8. #include "process.h"
  9. #include "timer.h"
  10. #include "tss.h"
  11. #include "wm.h"
  12. #include "serial.h"
  13. #include "version.h"
  14. #include "vga.h"
  15. #include "vfs.h"
  16. static inline void outb(unsigned short port, unsigned char v)
  17. {
  18. __asm__ volatile ("outb %0, %1" : : "a"(v), "Nd"(port));
  19. }
  20. volatile unsigned int ticks;
  21. struct idt_entry {
  22. unsigned short offset_lo;
  23. unsigned short selector;
  24. unsigned char zero;
  25. unsigned char type;
  26. unsigned short offset_hi;
  27. } __attribute__((packed));
  28. struct idtr_desc {
  29. unsigned short limit;
  30. unsigned int base;
  31. } __attribute__((packed));
  32. static struct idt_entry idt[256];
  33. static struct idtr_desc idtr;
  34. extern void pit_irq_stub(void);
  35. extern void kbd_irq_stub(void);
  36. extern void fdc_irq_stub(void);
  37. extern void default_irq_stub(void);
  38. extern void syscall_stub(void);
  39. extern unsigned int exc_stub_table[];
  40. unsigned int get_ticks_ms(void)
  41. {
  42. return ticks;
  43. }
  44. void exc_handler(unsigned int eip, unsigned int vector);
  45. void exc_handler(unsigned int eip, unsigned int vector)
  46. {
  47. unsigned int cr2 = 0;
  48. static const char * const names[] = {
  49. "#DE Divide Error", "#DB Debug", "NMI", "#BP Breakpoint",
  50. "#OF Overflow", "#BR Bound Range", "#UD Invalid Opcode",
  51. "#NM No FPU", "#DF Double Fault", "Coprocessor Overrun",
  52. "#TS Invalid TSS", "#NP Seg Not Present", "#SS Stack Fault",
  53. "#GP General Protection", "#PF Page Fault", "Reserved",
  54. "#MF FPU Error", "#AC Alignment Check", "#MC Machine Check",
  55. "#XM SIMD Error",
  56. };
  57. __asm__ volatile ("mov %%cr2, %0" : "=r"(cr2));
  58. /* Print the fault to serial FIRST, using only minimal/no-stack code, so it
  59. * survives even if the screen buffer or kernel stack is the problem. */
  60. serial_puts(SERIAL_COM1, "\n*** EXC vec ");
  61. serial_puthex(SERIAL_COM1, vector);
  62. serial_puts(SERIAL_COM1, " eip=");
  63. serial_puthex(SERIAL_COM1, eip);
  64. serial_puts(SERIAL_COM1, " cr2=");
  65. serial_puthex(SERIAL_COM1, cr2);
  66. serial_puts(SERIAL_COM1, " ");
  67. if (vector < 20)
  68. serial_puts(SERIAL_COM1, names[vector]);
  69. serial_puts(SERIAL_COM1, " ***\n");
  70. /* Then attempt the screen output (may fault if the screen is broken). */
  71. io_putstr("\n*** EXCEPTION: ");
  72. if (vector < 20)
  73. io_putstr(names[vector]);
  74. else {
  75. io_putstr("vector ");
  76. if (vector >= 10)
  77. io_putchar((char)('0' + vector / 10));
  78. io_putchar((char)('0' + vector % 10));
  79. }
  80. io_putstr(" ***\nSystem halted.\n");
  81. for (;;)
  82. __asm__ volatile ("cli; hlt");
  83. }
  84. static void set_idt_gate(int vec, unsigned int handler)
  85. {
  86. idt[vec].offset_lo = (unsigned short)(handler & 0xFFFF);
  87. idt[vec].selector = 0x08;
  88. idt[vec].zero = 0;
  89. idt[vec].type = 0x8E;
  90. idt[vec].offset_hi = (unsigned short)((handler >> 16) & 0xFFFF);
  91. }
  92. /* Trap gate (DPL=3) so ring-3 can `int` into it; a trap gate does NOT clear
  93. * IF, so a blocking syscall (e.g. sleep) can still be preempted by the timer. */
  94. static void set_idt_gate_user(int vec, unsigned int handler)
  95. {
  96. idt[vec].offset_lo = (unsigned short)(handler & 0xFFFF);
  97. idt[vec].selector = 0x08;
  98. idt[vec].zero = 0;
  99. idt[vec].type = 0xEF;
  100. idt[vec].offset_hi = (unsigned short)((handler >> 16) & 0xFFFF);
  101. }
  102. static void timer_init(void)
  103. {
  104. int i;
  105. /* Remap PICs: master at 0x20, slave at 0x28. */
  106. outb(0x20, 0x11);
  107. outb(0xA0, 0x11);
  108. outb(0x21, 0x20);
  109. outb(0xA1, 0x28);
  110. outb(0x21, 0x04);
  111. outb(0xA1, 0x02);
  112. outb(0x21, 0x01);
  113. outb(0xA1, 0x01);
  114. outb(0x21, 0xFC);
  115. outb(0xA1, 0xFF);
  116. /* Default handler for all 256 vectors: absorbs any unexpected
  117. interrupt (including spurious IRQ 7) with an EOI + iret. */
  118. for (i = 0; i < 256; i++)
  119. set_idt_gate(i, (unsigned int)default_irq_stub);
  120. /* Exception handlers 0-31: print the fault name and halt. */
  121. for (i = 0; i < 32; i++)
  122. set_idt_gate(i, exc_stub_table[i]);
  123. /* Hardware IRQ handlers. */
  124. set_idt_gate(0x20, (unsigned int)pit_irq_stub);
  125. set_idt_gate(0x21, (unsigned int)kbd_irq_stub);
  126. set_idt_gate(0x26, (unsigned int)fdc_irq_stub);
  127. /* Syscall gate (ring-3 can int 0x80). */
  128. set_idt_gate_user(0x80, (unsigned int)syscall_stub);
  129. idtr.limit = (unsigned short)(sizeof(idt) - 1);
  130. idtr.base = (unsigned int)idt;
  131. __asm__ volatile ("lidt %0" : : "m"(idtr));
  132. outb(0x43, 0x36);
  133. outb(0x40, (unsigned char)(1193 & 0xFF));
  134. outb(0x40, (unsigned char)((1193 >> 8) & 0xFF));
  135. __asm__ volatile ("sti");
  136. }
  137. extern unsigned char __bss_start[], __bss_end[];
  138. void kernel_main(void);
  139. void kernel_main(void)
  140. {
  141. unsigned char *p = __bss_start;
  142. while (p < __bss_end)
  143. *p++ = 0;
  144. serial_init(SERIAL_COM1);
  145. serial_ring_init();
  146. serial_puts(SERIAL_COM1, "\n" OS_NAME " v" OS_VERSION " booted\n");
  147. mm_init();
  148. mm_dump();
  149. kmalloc_init();
  150. paging_init();
  151. io_init();
  152. idle_init();
  153. /* process_init + gdt_tss_init must run BEFORE timer_init: once the PIT is
  154. * started (sti in timer_init) the context-switch stub runs on every tick
  155. * and reads procs[].cr3 / procs[].kstack_top / the TSS, so those have to be
  156. * valid first (otherwise the first switch loads a garbage CR3 and faults). */
  157. process_init();
  158. gdt_tss_init();
  159. timer_init();
  160. mouse_init();
  161. vga_shadow_init();
  162. vga_fb_flush();
  163. vfs_mount_all();
  164. wm_run();
  165. }