aboutsummaryrefslogtreecommitdiff
path: root/kernel/include
diff options
context:
space:
mode:
authorRaghuram Subramani <raghus2247@gmail.com>2025-01-01 12:32:14 -0500
committerRaghuram Subramani <raghus2247@gmail.com>2025-01-01 12:32:14 -0500
commit47ba8e54f0aa6efffc2f0aa9d5d6eefe6b696fae (patch)
tree8f757d27a419c2a864e0f96851f2b7d197edbd97 /kernel/include
parentaed2678d037042065d29fc1df9484220ece2474f (diff)
kernel: GDT: Create GDT
The GDT has been implemented. It is yet to be loaded and used.
Diffstat (limited to 'kernel/include')
-rw-r--r--kernel/include/boot/gdt.h98
1 files changed, 98 insertions, 0 deletions
diff --git a/kernel/include/boot/gdt.h b/kernel/include/boot/gdt.h
new file mode 100644
index 0000000..b07d64d
--- /dev/null
+++ b/kernel/include/boot/gdt.h
@@ -0,0 +1,98 @@
+/*
+ * CMOS
+ * Copyright (C) 2024-2025 Raghuram Subramani <raghus2247@gmail.com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __boot_gdt_h
+#define __boot_gdt_h
+
+/* Access Flags:
+ * 7 PRESENT
+ * 6 PRIVILEGE
+ * 5 PRIVILEGE
+ * 4 TYPE
+ * 3 EXECUTABLE
+ * 2 DIRECTION/CONFORMING
+ * 1 READABLE/WRITABLE
+ * 0 ACCESSED
+ */
+
+#define PRESENT(x) (x << 7)
+#define PRIVILEGE(x) (x << 5)
+#define TYPE(x) (x << 4)
+#define EXECUTABLE(x) (x << 3)
+#define DIRECTION_CONFORMING(x) (x << 2)
+#define READABLE_WRITABLE(x) (x << 1)
+#define ACCESSED(x) x
+
+#define KERNEL_CODE_SEGMENT_ACCESS_FLAGS \
+ PRESENT(1) | PRIVILEGE(0) | TYPE(1) | EXECUTABLE(1) \
+ | DIRECTION_CONFORMING(0) | READABLE_WRITABLE(1) | ACCESSED(0)
+
+#define KERNEL_DATA_SEGMENT_ACCESS_FLAGS \
+ PRESENT(1) | PRIVILEGE(0) | TYPE(1) | EXECUTABLE(0) \
+ | DIRECTION_CONFORMING(0) | READABLE_WRITABLE(1) | ACCESSED(0)
+
+/* Other Flags:
+ * 3 GRANULARITY
+ * 2 SIZE
+ * 1 LONGMODE
+ * 0 RESERVED
+ */
+
+#define GRANULARITY (1 << 3)
+#define SIZE (1 << 2)
+#define LONGMODE (0 << 1)
+#define RESERVED 0
+
+#define FLAGS (GRANULARITY | SIZE | LONGMODE | RESERVED)
+
+#define FLAGS_LIMIT_HIGH(flags, limit_high) ((flags << 4) | limit_high)
+
+/* GDT Entry:
+ * BASE FLAGS LIMIT ACCESS_FLAGS BASE BASE LIMIT
+ * 8bit 4bit 4bit 8bit 8bit 16bit 16bit
+ */
+
+#define GDT_ENTRY(base, limit, access_flags, flags) \
+ { \
+ (limit & 0xffff), /* limit_low */ \
+ (base & 0xffff), /* base_low */ \
+ ((base >> 16) & 0xff), /* base_mid */ \
+ access_flags, /* access_flags */ \
+ FLAGS_LIMIT_HIGH(flags, \
+ ((limit >> 16) & 0xff)), /* flags_limit_high */ \
+ ((base >> 24) & 0xff) /* base_high */ \
+ }
+
+typedef struct {
+ uint16_t limit_low;
+ uint16_t base_low;
+ uint8_t base_mid;
+ uint8_t access_flags;
+ uint8_t flags_limit_high;
+ uint8_t base_high;
+} __attribute__((packed)) GDT_entry;
+
+typedef struct {
+ uint16_t limit; /* sizeof(GDT) - 1 */
+ GDT_entry *ptr; /* Address of GDT */
+} __attribute__((packed)) GDT_descriptor;
+
+/* TODO: Implement GDT_load() */
+void __attribute((cdecl)) GDT_load(void);
+
+#endif