kernel.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. void pit_irq(void);
  25. void pit_irq(void)
  26. {
  27. ticks++;
  28. outb(0x20, 0x20);
  29. }
  30. unsigned int get_ticks_ms(void)
  31. {
  32. return ticks;
  33. }
  34. static void timer_init(void)
  35. {
  36. unsigned int stub = (unsigned int)pit_irq_stub;
  37. outb(0x20, 0x11);
  38. outb(0xA0, 0x11);
  39. outb(0x21, 0x20);
  40. outb(0xA1, 0x28);
  41. outb(0x21, 0x04);
  42. outb(0xA1, 0x02);
  43. outb(0x21, 0x01);
  44. outb(0xA1, 0x01);
  45. outb(0x21, 0xFE);
  46. outb(0xA1, 0xFF);
  47. idt[0x20].offset_lo = (unsigned short)(stub & 0xFFFF);
  48. idt[0x20].selector = 0x08;
  49. idt[0x20].zero = 0;
  50. idt[0x20].type = 0x8E;
  51. idt[0x20].offset_hi = (unsigned short)((stub >> 16) & 0xFFFF);
  52. idtr.limit = (unsigned short)(sizeof(idt) - 1);
  53. idtr.base = (unsigned int)idt;
  54. __asm__ volatile ("lidt %0" : : "m"(idtr));
  55. outb(0x43, 0x36);
  56. outb(0x40, (unsigned char)(1193 & 0xFF));
  57. outb(0x40, (unsigned char)((1193 >> 8) & 0xFF));
  58. __asm__ volatile ("sti");
  59. }
  60. void kernel_main(void) __attribute__((section(".text.entry")));
  61. void kernel_main(void)
  62. {
  63. io_init();
  64. timer_init();
  65. terminal_run();
  66. }