kernel.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* Kernel: entry point at 0x7E00, timer (PIT 1 kHz), process init, terminal. */
  2. #include "idle.h"
  3. #include "io.h"
  4. #include "mouse.h"
  5. #include "process.h"
  6. #include "terminal.h"
  7. #include "timer.h"
  8. #include "serial.h"
  9. #include "vga.h"
  10. static inline void outb(unsigned short port, unsigned char v)
  11. {
  12. __asm__ volatile ("outb %0, %1" : : "a"(v), "Nd"(port));
  13. }
  14. volatile unsigned int ticks;
  15. struct idt_entry {
  16. unsigned short offset_lo;
  17. unsigned short selector;
  18. unsigned char zero;
  19. unsigned char type;
  20. unsigned short offset_hi;
  21. } __attribute__((packed));
  22. struct idtr_desc {
  23. unsigned short limit;
  24. unsigned int base;
  25. } __attribute__((packed));
  26. static struct idt_entry idt[256];
  27. static struct idtr_desc idtr;
  28. extern void pit_irq_stub(void);
  29. extern void kbd_irq_stub(void);
  30. extern void fdc_irq_stub(void);
  31. extern void default_irq_stub(void);
  32. extern unsigned int exc_stub_table[];
  33. unsigned int get_ticks_ms(void)
  34. {
  35. return ticks;
  36. }
  37. void exc_handler(unsigned int vector);
  38. void exc_handler(unsigned int vector)
  39. {
  40. static const char * const names[] = {
  41. "#DE Divide Error", "#DB Debug", "NMI", "#BP Breakpoint",
  42. "#OF Overflow", "#BR Bound Range", "#UD Invalid Opcode",
  43. "#NM No FPU", "#DF Double Fault", "Coprocessor Overrun",
  44. "#TS Invalid TSS", "#NP Seg Not Present", "#SS Stack Fault",
  45. "#GP General Protection", "#PF Page Fault", "Reserved",
  46. "#MF FPU Error", "#AC Alignment Check", "#MC Machine Check",
  47. "#XM SIMD Error",
  48. };
  49. io_putstr("\n*** EXCEPTION: ");
  50. if (vector < 20)
  51. io_putstr(names[vector]);
  52. else {
  53. io_putstr("vector ");
  54. if (vector >= 10)
  55. io_putchar((char)('0' + vector / 10));
  56. io_putchar((char)('0' + vector % 10));
  57. }
  58. io_putstr(" ***\nSystem halted.\n");
  59. for (;;)
  60. __asm__ volatile ("cli; hlt");
  61. }
  62. static void set_idt_gate(int vec, unsigned int handler)
  63. {
  64. idt[vec].offset_lo = (unsigned short)(handler & 0xFFFF);
  65. idt[vec].selector = 0x08;
  66. idt[vec].zero = 0;
  67. idt[vec].type = 0x8E;
  68. idt[vec].offset_hi = (unsigned short)((handler >> 16) & 0xFFFF);
  69. }
  70. static void timer_init(void)
  71. {
  72. int i;
  73. /* Remap PICs: master at 0x20, slave at 0x28. */
  74. outb(0x20, 0x11);
  75. outb(0xA0, 0x11);
  76. outb(0x21, 0x20);
  77. outb(0xA1, 0x28);
  78. outb(0x21, 0x04);
  79. outb(0xA1, 0x02);
  80. outb(0x21, 0x01);
  81. outb(0xA1, 0x01);
  82. outb(0x21, 0xFC);
  83. outb(0xA1, 0xFF);
  84. /* Default handler for all 256 vectors: absorbs any unexpected
  85. interrupt (including spurious IRQ 7) with an EOI + iret. */
  86. for (i = 0; i < 256; i++)
  87. set_idt_gate(i, (unsigned int)default_irq_stub);
  88. /* Exception handlers 0-31: print the fault name and halt. */
  89. for (i = 0; i < 32; i++)
  90. set_idt_gate(i, exc_stub_table[i]);
  91. /* Hardware IRQ handlers. */
  92. set_idt_gate(0x20, (unsigned int)pit_irq_stub);
  93. set_idt_gate(0x21, (unsigned int)kbd_irq_stub);
  94. set_idt_gate(0x26, (unsigned int)fdc_irq_stub);
  95. idtr.limit = (unsigned short)(sizeof(idt) - 1);
  96. idtr.base = (unsigned int)idt;
  97. __asm__ volatile ("lidt %0" : : "m"(idtr));
  98. outb(0x43, 0x36);
  99. outb(0x40, (unsigned char)(1193 & 0xFF));
  100. outb(0x40, (unsigned char)((1193 >> 8) & 0xFF));
  101. __asm__ volatile ("sti");
  102. }
  103. extern unsigned char __bss_start[], __bss_end[];
  104. void kernel_main(void);
  105. void kernel_main(void)
  106. {
  107. unsigned char *p = __bss_start;
  108. while (p < __bss_end)
  109. *p++ = 0;
  110. serial_init(SERIAL_COM1);
  111. serial_ring_init();
  112. io_init();
  113. idle_init();
  114. timer_init();
  115. mouse_init();
  116. process_init();
  117. vga_shadow_init();
  118. vga_fb_flush();
  119. terminal_run();
  120. }