kernel.c 3.1 KB

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