defining.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include <stdint.h>
  2. #include <stddef.h>
  3. #include "stivale2.h"
  4. #ifndef PROCESS_H
  5. #define PROCESS_H
  6. typedef uint8_t u8;
  7. typedef uint16_t u16;
  8. typedef uint32_t u32;
  9. typedef uint64_t u64;
  10. //defining print function(printing integers function)
  11. void (*write)(const char *string, size_t length);
  12. void print(int val) {
  13. char buf[30];
  14. buf[29] = 0; // Null terminator
  15. int written = 0;
  16. do {
  17. char c = '0' + (val % 10);
  18. val /= 10;
  19. buf[28 - written] = c;
  20. written++;
  21. } while (val != 0);
  22. char *string = buf + 29 - written;
  23. write(string, written);
  24. }
  25. //defining print function(printing integers function)
  26. //defining idt_init
  27. struct idt_descriptor {
  28. u16 offset_lowest; // MAX_UNSIGNED_SHORT
  29. u16 selector; // must point to gdt_descriptor
  30. u8 ist;
  31. u8 type_attr;
  32. u16 offset_medium;
  33. u32 offset_highest;
  34. u32 zero;
  35. }
  36. idtdes;
  37. void idt_init(){
  38. idtdes.offset_lowest = 0xFFFF;
  39. idtdes.selector = 0x08;
  40. idtdes.ist = 0;
  41. idtdes.type_attr = 0x8e;
  42. idtdes.offset_medium = 0xFFFF;
  43. idtdes.offset_highest = 0xFFFFFFFF;
  44. }
  45. //defining idt_init
  46. //defining gdt_load
  47. struct gdt_descriptor { //defining gdt_descriptor
  48. u32 _0;
  49. u8 _1, access, granularity, _2;
  50. } __attribute__((packed));
  51. struct gdt_pointer { // pointer where all values are gonna be stored and pointed
  52. u16 size; u64 addr;
  53. } __attribute__((packed));
  54. const u8 access_flagsGDT = 0b10010010; // Present, ring 0 only, readable cs and writable data
  55. const u8 gdt_is_code_segment = 1 << 3, gdt_longmode_cs = 1 << 5;
  56. static struct gdt_descriptor gdt[] = {
  57. {},
  58. {.access = access_flagsGDT | gdt_is_code_segment, .granularity = gdt_longmode_cs}, // kern cs
  59. {.access = access_flagsGDT, .granularity = 0} // kern ds
  60. };
  61. void gdt_load() {
  62. struct gdt_pointer gdtr = {.size = sizeof(gdt) - 1, .addr = (u64)&gdt};
  63. asm volatile(
  64. "cli"
  65. );
  66. asm volatile(
  67. "lgdt %0\n\t"
  68. : : "m"(gdtr)
  69. );
  70. asm volatile(
  71. "leaq 1f(%%rip), %%rax\n"
  72. "pushq $0x08\n"
  73. "pushq %%rax\n"
  74. "lretq\n"
  75. "1:\n": :
  76. );
  77. asm volatile(
  78. "mov %0,%%ds\nmov %0,%%es\nmov %0,%%gs\nmov %0,%%fs\nmov %0,%%ss\n"
  79. : : "a"((u16)0x10)
  80. );
  81. }
  82. // defining gdt_load
  83. #endif