kernel.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 "version.h"
  10. #include "vga.h"
  11. static inline void outb(unsigned short port, unsigned char v)
  12. {
  13. __asm__ volatile ("outb %0, %1" : : "a"(v), "Nd"(port));
  14. }
  15. volatile unsigned int ticks;
  16. struct idt_entry {
  17. unsigned short offset_lo;
  18. unsigned short selector;
  19. unsigned char zero;
  20. unsigned char type;
  21. unsigned short offset_hi;
  22. } __attribute__((packed));
  23. struct idtr_desc {
  24. unsigned short limit;
  25. unsigned int base;
  26. } __attribute__((packed));
  27. static struct idt_entry idt[256];
  28. static struct idtr_desc idtr;
  29. extern void pit_irq_stub(void);
  30. extern void kbd_irq_stub(void);
  31. extern void fdc_irq_stub(void);
  32. extern void default_irq_stub(void);
  33. extern unsigned int exc_stub_table[];
  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. extern unsigned char __bss_start[], __bss_end[];
  105. void kernel_main(void);
  106. void kernel_main(void)
  107. {
  108. unsigned char *p = __bss_start;
  109. while (p < __bss_end)
  110. *p++ = 0;
  111. serial_init(SERIAL_COM1);
  112. serial_ring_init();
  113. serial_puts(SERIAL_COM1, "\n" OS_NAME " v" OS_VERSION " booted\n");
  114. io_init();
  115. idle_init();
  116. timer_init();
  117. mouse_init();
  118. process_init();
  119. vga_shadow_init();
  120. vga_fb_flush();
  121. terminal_run();
  122. }