kernel.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 vector);
  45. void exc_handler(unsigned int vector)
  46. {
  47. static const char * const names[] = {
  48. "#DE Divide Error", "#DB Debug", "NMI", "#BP Breakpoint",
  49. "#OF Overflow", "#BR Bound Range", "#UD Invalid Opcode",
  50. "#NM No FPU", "#DF Double Fault", "Coprocessor Overrun",
  51. "#TS Invalid TSS", "#NP Seg Not Present", "#SS Stack Fault",
  52. "#GP General Protection", "#PF Page Fault", "Reserved",
  53. "#MF FPU Error", "#AC Alignment Check", "#MC Machine Check",
  54. "#XM SIMD Error",
  55. };
  56. io_putstr("\n*** EXCEPTION: ");
  57. if (vector < 20)
  58. io_putstr(names[vector]);
  59. else {
  60. io_putstr("vector ");
  61. if (vector >= 10)
  62. io_putchar((char)('0' + vector / 10));
  63. io_putchar((char)('0' + vector % 10));
  64. }
  65. io_putstr(" ***\nSystem halted.\n");
  66. for (;;)
  67. __asm__ volatile ("cli; hlt");
  68. }
  69. static void set_idt_gate(int vec, unsigned int handler)
  70. {
  71. idt[vec].offset_lo = (unsigned short)(handler & 0xFFFF);
  72. idt[vec].selector = 0x08;
  73. idt[vec].zero = 0;
  74. idt[vec].type = 0x8E;
  75. idt[vec].offset_hi = (unsigned short)((handler >> 16) & 0xFFFF);
  76. }
  77. /* Trap gate (DPL=3) so ring-3 can `int` into it; a trap gate does NOT clear
  78. * IF, so a blocking syscall (e.g. sleep) can still be preempted by the timer. */
  79. static void set_idt_gate_user(int vec, unsigned int handler)
  80. {
  81. idt[vec].offset_lo = (unsigned short)(handler & 0xFFFF);
  82. idt[vec].selector = 0x08;
  83. idt[vec].zero = 0;
  84. idt[vec].type = 0xEF;
  85. idt[vec].offset_hi = (unsigned short)((handler >> 16) & 0xFFFF);
  86. }
  87. static void timer_init(void)
  88. {
  89. int i;
  90. /* Remap PICs: master at 0x20, slave at 0x28. */
  91. outb(0x20, 0x11);
  92. outb(0xA0, 0x11);
  93. outb(0x21, 0x20);
  94. outb(0xA1, 0x28);
  95. outb(0x21, 0x04);
  96. outb(0xA1, 0x02);
  97. outb(0x21, 0x01);
  98. outb(0xA1, 0x01);
  99. outb(0x21, 0xFC);
  100. outb(0xA1, 0xFF);
  101. /* Default handler for all 256 vectors: absorbs any unexpected
  102. interrupt (including spurious IRQ 7) with an EOI + iret. */
  103. for (i = 0; i < 256; i++)
  104. set_idt_gate(i, (unsigned int)default_irq_stub);
  105. /* Exception handlers 0-31: print the fault name and halt. */
  106. for (i = 0; i < 32; i++)
  107. set_idt_gate(i, exc_stub_table[i]);
  108. /* Hardware IRQ handlers. */
  109. set_idt_gate(0x20, (unsigned int)pit_irq_stub);
  110. set_idt_gate(0x21, (unsigned int)kbd_irq_stub);
  111. set_idt_gate(0x26, (unsigned int)fdc_irq_stub);
  112. /* Syscall gate (ring-3 can int 0x80). */
  113. set_idt_gate_user(0x80, (unsigned int)syscall_stub);
  114. idtr.limit = (unsigned short)(sizeof(idt) - 1);
  115. idtr.base = (unsigned int)idt;
  116. __asm__ volatile ("lidt %0" : : "m"(idtr));
  117. outb(0x43, 0x36);
  118. outb(0x40, (unsigned char)(1193 & 0xFF));
  119. outb(0x40, (unsigned char)((1193 >> 8) & 0xFF));
  120. __asm__ volatile ("sti");
  121. }
  122. extern unsigned char __bss_start[], __bss_end[];
  123. void kernel_main(void);
  124. void kernel_main(void)
  125. {
  126. unsigned char *p = __bss_start;
  127. while (p < __bss_end)
  128. *p++ = 0;
  129. serial_init(SERIAL_COM1);
  130. serial_ring_init();
  131. serial_puts(SERIAL_COM1, "\n" OS_NAME " v" OS_VERSION " booted\n");
  132. mm_init();
  133. mm_dump();
  134. kmalloc_init();
  135. paging_init();
  136. serial_puts(SERIAL_COM1, "dbg: paging done\n");
  137. io_init();
  138. serial_puts(SERIAL_COM1, "dbg: io done\n");
  139. idle_init();
  140. serial_puts(SERIAL_COM1, "dbg: idle done\n");
  141. timer_init();
  142. serial_puts(SERIAL_COM1, "dbg: timer done\n");
  143. mouse_init();
  144. serial_puts(SERIAL_COM1, "dbg: mouse done\n");
  145. process_init();
  146. serial_puts(SERIAL_COM1, "dbg: process done\n");
  147. gdt_tss_init();
  148. serial_puts(SERIAL_COM1, "dbg: gdt_tss done\n");
  149. vga_shadow_init();
  150. serial_puts(SERIAL_COM1, "dbg: vga_shadow done\n");
  151. vga_fb_flush();
  152. serial_puts(SERIAL_COM1, "dbg: fb_flush done\n");
  153. vfs_mount_all();
  154. serial_puts(SERIAL_COM1, "dbg: vfs done\n");
  155. wm_run();
  156. }