kernel.c 3.6 KB

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