kernel.c 3.2 KB

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