kernel.c 3.5 KB

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