You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

92 lines
2.4 KiB

  1. #include "exception.h"
  2. #include "gdt.h"
  3. #include "idt.h"
  4. #include "interrupt.h"
  5. #include "io.h"
  6. #include "irq.h"
  7. #include "klibc.h"
  8. #include "mem.h"
  9. #include "multiboot.h"
  10. #include "paging.h"
  11. #include "pit.h"
  12. #include "serial.h"
  13. #include "stdarg.h"
  14. #ifdef RUN_TEST
  15. #include "test.h"
  16. #endif
  17. #include "types.h"
  18. #include "vga.h"
  19. #define CHECK_FLAG(flags, bit) ((flags) & (1 << (bit)))
  20. void cpuid(int code, uint32_t *a, uint32_t *d)
  21. {
  22. asm volatile("cpuid" : "=a"(*a), "=d"(*d) : "0"(code) : "ebx", "ecx");
  23. }
  24. // Multiboot information available here :
  25. // https://www.gnu.org/software/grub/manual/multiboot/multiboot.html#kernel_002ec
  26. void kmain(unsigned long magic, unsigned long addr)
  27. {
  28. unsigned long upper_mem = 0;
  29. VGASetup(BLACK, GREEN);
  30. printf("Setting up Interruptions\n");
  31. gdtSetup();
  32. idtSetup();
  33. irqSetup();
  34. pitSetup(100);
  35. if (magic == MULTIBOOT_BOOTLOADER_MAGIC) { // Get loaded by Grub wuth mutliboot version 1
  36. multiboot_info_t *mbi = (multiboot_info_t *)addr;
  37. /* Are mem_* valid? */
  38. if (CHECK_FLAG(mbi->flags, 0)) {
  39. printf("mem_lower = %dKB mem_upper %dKB\n", mbi->mem_lower, mbi->mem_upper);
  40. upper_mem = mbi->mem_upper;
  41. }
  42. /* Is boot_device valid? */
  43. if (CHECK_FLAG(mbi->flags, 1)) {
  44. printf("boot_device = %d\n", mbi->boot_device);
  45. }
  46. /* Is the command line passed? */
  47. if (CHECK_FLAG(mbi->flags, 2)) {
  48. printf("cmdline = %s\n", (char *)mbi->cmdline);
  49. }
  50. }
  51. if (upper_mem == 0) {
  52. printf("Cannot get upper phy mem bound. Using default value 32MB\n");
  53. upper_mem = 32 * 1024;
  54. }
  55. printf("Setting up Pagination\n");
  56. paddr_t lastUserByMem;
  57. memSetup(upper_mem, &lastUserByMem);
  58. #ifdef RUN_TEST
  59. testPhymem();
  60. #endif
  61. pagingSetup(lastUserByMem);
  62. printf("Setting up IRQ handlers\n");
  63. irqSetRoutine(IRQ_KEYBOARD, keyboard_handler);
  64. printf("Enabling HW interrupts\n");
  65. exceptionSetRoutine(EXCEPTION_DOUBLE_FAULT, print_handler);
  66. exceptionSetRoutine(EXCEPTION_PAGE_FAULT, pagefault_handler);
  67. // Enabling the HW interrupts
  68. asm volatile("sti\n");
  69. printf("Setting up Serial link (115200)\n");
  70. serialSetup(115200);
  71. #ifdef RUN_TEST
  72. run_test();
  73. #endif
  74. int count = 0;
  75. while (1) {
  76. printIntDetails(count++, GREEN, BLACK, 0, VGA_HEIGHT - 1);
  77. }
  78. printf("exiting\n");
  79. }