kernel.c 3.2 KB

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