fixing GDT
This commit is contained in:
@@ -3,10 +3,10 @@ cmake_policy(VERSION 3.30)
|
|||||||
|
|
||||||
project(appa-os LANGUAGES C ASM)
|
project(appa-os LANGUAGES C ASM)
|
||||||
|
|
||||||
enable_language(ASM-ATT)
|
|
||||||
set(CMAKE_C_STANDARD 99)
|
set(CMAKE_C_STANDARD 99)
|
||||||
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||||
|
|
||||||
|
enable_language(ASM-ATT)
|
||||||
option(BUILD_DOCS "Build documentation" ON)
|
option(BUILD_DOCS "Build documentation" ON)
|
||||||
|
|
||||||
if(BUILD_DOCS)
|
if(BUILD_DOCS)
|
||||||
|
|||||||
@@ -2,9 +2,9 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#define GDT_SIZE 5
|
#define GDT_ENTRY_SIZE 5
|
||||||
|
|
||||||
static gdt_segment_descriptor_t gdt_descriptors[GDT_SIZE];
|
static gdt_segment_descriptor_t gdt_descriptors[GDT_ENTRY_SIZE];
|
||||||
static gdt_entry_t gdt;
|
static gdt_entry_t gdt;
|
||||||
|
|
||||||
// https://forum.osdev.org/viewtopic.php?t=37245
|
// https://forum.osdev.org/viewtopic.php?t=37245
|
||||||
@@ -24,40 +24,38 @@ void gdt_flush() {
|
|||||||
"movw %%ax, %%fs \n\t"
|
"movw %%ax, %%fs \n\t"
|
||||||
"movw %%ax, %%gs \n\t"
|
"movw %%ax, %%gs \n\t"
|
||||||
"movw %%ax, %%ss \n\t"
|
"movw %%ax, %%ss \n\t"
|
||||||
"ljmp $0x08,$boing%=\n\t"
|
"ljmp $0x0008,$.gdt_flush_end%=\n\t"
|
||||||
"boing%=:"
|
".gdt_flush_end%=:"
|
||||||
:
|
:
|
||||||
: "r"(ptr)
|
: "r"(ptr)
|
||||||
: "eax");
|
: "eax");
|
||||||
// asm volatile("lgdtl %0 \n\t"
|
|
||||||
// "ljmp $0x08,$.L%= \n\t"
|
|
||||||
// ".L%=: \n\t"
|
|
||||||
// "mov %1, %%ds \n\t"
|
|
||||||
// "mov %1, %%es \n\t"
|
|
||||||
// "mov %1, %%fs \n\t"
|
|
||||||
// "mov %1, %%gs \n\t"
|
|
||||||
// "mov %1, %%ss \n\t" ::"m"(gdt),
|
|
||||||
// "r"(0x10));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void gdt_init() {
|
void gdt_init() {
|
||||||
// This is setting up 4 large seguments overlapped to use paging
|
// This is setting up 4 large seguments overlapped to use paging
|
||||||
// https://stackoverflow.com/a/23979175
|
// https://stackoverflow.com/a/23979175
|
||||||
gdt.limit = sizeof(gdt_segment_descriptor_t) * GDT_SIZE - 1;
|
gdt.limit = (sizeof(struct gdt_segment_descriptor) * GDT_ENTRY_SIZE) - 1;
|
||||||
gdt.base = (uint32_t)&gdt_descriptors;
|
gdt.base = (uint32_t)gdt_descriptors;
|
||||||
|
|
||||||
// Null Segment
|
// Null Segment
|
||||||
gdt_descriptors[0] = gdt_create_segment_descriptor(0, 0, 0, 0);
|
gdt_descriptors[0] = gdt_create_segment_descriptor(0, 0, 0, 0);
|
||||||
|
|
||||||
// Kernel Mode Code Segment with 4 megabytes
|
// Kernel Mode Code Segment with 4 megabytes
|
||||||
gdt_descriptors[1] = gdt_create_segment_descriptor(0, 0xFFFFFFFF, (uint8_t)(SEG_GRAN(1) | SEG_SIZE(1)),
|
gdt_descriptors[1] =
|
||||||
SEG_PRES(1) | SEG_PRIV(0) | SEG_DESCTYPE(1) | SEG_CODE_EXRD);
|
gdt_create_segment_descriptor(0, 0xFFFFFFFF, (GDT_FLAG_GRANULARITY(1) | GDT_FLAG_SIZE(1)) << 4,
|
||||||
gdt_descriptors[2] = gdt_create_segment_descriptor(0, 0xFFFFFFFF, (uint8_t)(SEG_GRAN(1) | SEG_SIZE(1)),
|
GDT_ACCESS_PRESENT(1) | GDT_ACCESS_PRIVILEGE(0) | GDT_ACCESS_DESCRIPTOR_TYPE(1) | GDT_CODE_EXRD);
|
||||||
SEG_PRES(1) | SEG_PRIV(0) | SEG_DESCTYPE(1) | SEG_DATA_RDWR);
|
|
||||||
gdt_descriptors[3] = gdt_create_segment_descriptor(0, 0xFFFFFFFF, (uint8_t)(SEG_GRAN(1) | SEG_SIZE(1)),
|
gdt_descriptors[2] =
|
||||||
SEG_PRES(1) | SEG_PRIV(3) | SEG_DESCTYPE(1) | SEG_CODE_EXRD);
|
gdt_create_segment_descriptor(0, 0xFFFFFFFF, (GDT_FLAG_GRANULARITY(1) | GDT_FLAG_SIZE(1)) << 4,
|
||||||
gdt_descriptors[4] = gdt_create_segment_descriptor(0, 0xFFFFFFFF, (uint8_t)(SEG_GRAN(1) | SEG_SIZE(1)),
|
GDT_ACCESS_PRESENT(1) | GDT_ACCESS_PRIVILEGE(0) | GDT_ACCESS_DESCRIPTOR_TYPE(1) | GDT_DATA_RDWR);
|
||||||
SEG_PRES(1) | SEG_PRIV(3) | SEG_DESCTYPE(1) | SEG_DATA_RDWR);
|
|
||||||
|
gdt_descriptors[3] =
|
||||||
|
gdt_create_segment_descriptor(0, 0xFFFFFFFF, (GDT_FLAG_GRANULARITY(1) | GDT_FLAG_SIZE(1)) << 4,
|
||||||
|
GDT_ACCESS_PRESENT(1) | GDT_ACCESS_PRIVILEGE(3) | GDT_ACCESS_DESCRIPTOR_TYPE(1) | GDT_CODE_EXRD);
|
||||||
|
|
||||||
|
gdt_descriptors[4] =
|
||||||
|
gdt_create_segment_descriptor(0, 0xFFFFFFFF, (GDT_FLAG_GRANULARITY(1) | GDT_FLAG_SIZE(1)) << 4,
|
||||||
|
GDT_ACCESS_PRESENT(1) | GDT_ACCESS_PRIVILEGE(3) | GDT_ACCESS_DESCRIPTOR_TYPE(1) | GDT_DATA_RDWR);
|
||||||
// TODO: add Task State Segment
|
// TODO: add Task State Segment
|
||||||
// Load and flush the GDT
|
// Load and flush the GDT
|
||||||
gdt_flush();
|
gdt_flush();
|
||||||
|
|||||||
@@ -9,32 +9,40 @@
|
|||||||
// Refer to the intel documentation for a description of what each one does.
|
// Refer to the intel documentation for a description of what each one does.
|
||||||
|
|
||||||
// Descriptor type (0 for system, 1 for code/data)
|
// Descriptor type (0 for system, 1 for code/data)
|
||||||
#define SEG_DESCTYPE(x) ((x) << 0x04)
|
#define GDT_ACCESS_DESCRIPTOR_TYPE(x) ((x) << 0x04)
|
||||||
#define SEG_PRES(x) ((x) << 0x07) // Present
|
|
||||||
#define SEG_SAVL(x) ((x) << 0x0C) // Available for system use
|
|
||||||
#define SEG_LONG(x) ((x) << 0x0D) // Long mode
|
|
||||||
// Size (0 for 16-bit, 1 for 32)
|
|
||||||
#define SEG_SIZE(x) ((x) << 0x0E)
|
|
||||||
// Granularity (0 for 1B - 1MB, 1 for 4KB - 4GB)
|
|
||||||
#define SEG_GRAN(x) ((x) << 0x0F)
|
|
||||||
#define SEG_PRIV(x) (((x) & 0x03) << 0x05) // Set privilege level (0 - 3)
|
|
||||||
|
|
||||||
#define SEG_DATA_RD 0x00 // Read-Only
|
// Present
|
||||||
#define SEG_DATA_RDA 0x01 // Read-Only, accessed
|
#define GDT_ACCESS_PRESENT(x) ((x) << 0x07)
|
||||||
#define SEG_DATA_RDWR 0x02 // Read/Write
|
|
||||||
#define SEG_DATA_RDWRA 0x03 // Read/Write, accessed
|
// Set privilege level (0 - 3)
|
||||||
#define SEG_DATA_RDEXPD 0x04 // Read-Only, expand-down
|
#define GDT_ACCESS_PRIVILEGE(x) (((x) & 0x03) << 0x05)
|
||||||
#define SEG_DATA_RDEXPDA 0x05 // Read-Only, expand-down, accessed
|
|
||||||
#define SEG_DATA_RDWREXPD 0x06 // Read/Write, expand-down
|
// Long mode
|
||||||
#define SEG_DATA_RDWREXPDA 0x07 // Read/Write, expand-down, accessed
|
#define GDT_FLAG_LONG_MODE(x) ((x) << 0x01)
|
||||||
#define SEG_CODE_EX 0x08 // Execute-Only
|
|
||||||
#define SEG_CODE_EXA 0x09 // Execute-Only, accessed
|
// Size (0 for 16-bit, 1 for 32)
|
||||||
#define SEG_CODE_EXRD 0x0A // Execute/Read
|
#define GDT_FLAG_SIZE(x) ((x) << 0x02)
|
||||||
#define SEG_CODE_EXRDA 0x0B // Execute/Read, accessed
|
|
||||||
#define SEG_CODE_EXC 0x0C // Execute-Only, conforming
|
// Granularity (0 for 1B - 1MB, 1 for 4KB - 4GB)
|
||||||
#define SEG_CODE_EXCA 0x0D // Execute-Only, conforming, accessed
|
#define GDT_FLAG_GRANULARITY(x) ((x) << 0x03)
|
||||||
#define SEG_CODE_EXRDC 0x0E // Execute/Read, conforming
|
|
||||||
#define SEG_CODE_EXRDCA 0x0F // Execute/Read, conforming, accessed
|
|
||||||
|
#define GDT_DATA_RD 0x00 // Read-Only
|
||||||
|
#define GDT_DATA_RDA 0x01 // Read-Only, accessed
|
||||||
|
#define GDT_DATA_RDWR 0x02 // Read/Write
|
||||||
|
#define GDT_DATA_RDWRA 0x03 // Read/Write, accessed
|
||||||
|
#define GDT_DATA_RDEXPD 0x04 // Read-Only, expand-down
|
||||||
|
#define GDT_DATA_RDEXPDA 0x05 // Read-Only, expand-down, accessed
|
||||||
|
#define GDT_DATA_RDWREXPD 0x06 // Read/Write, expand-down
|
||||||
|
#define GDT_DATA_RDWREXPDA 0x07 // Read/Write, expand-down, accessed
|
||||||
|
#define GDT_CODE_EX 0x08 // Execute-Only
|
||||||
|
#define GDT_CODE_EXA 0x09 // Execute-Only, accessed
|
||||||
|
#define GDT_CODE_EXRD 0x0A // Execute/Read
|
||||||
|
#define GDT_CODE_EXRDA 0x0B // Execute/Read, accessed
|
||||||
|
#define GDT_CODE_EXC 0x0C // Execute-Only, conforming
|
||||||
|
#define GDT_CODE_EXCA 0x0D // Execute-Only, conforming, accessed
|
||||||
|
#define GDT_CODE_EXRDC 0x0E // Execute/Read, conforming
|
||||||
|
#define GDT_CODE_EXRDCA 0x0F // Execute/Read, conforming, accessed
|
||||||
|
|
||||||
struct gdt_segment_descriptor {
|
struct gdt_segment_descriptor {
|
||||||
uint16_t limit;
|
uint16_t limit;
|
||||||
@@ -43,7 +51,7 @@ struct gdt_segment_descriptor {
|
|||||||
uint8_t access_byte;
|
uint8_t access_byte;
|
||||||
uint8_t flags;
|
uint8_t flags;
|
||||||
uint8_t high_base;
|
uint8_t high_base;
|
||||||
} __attribute__((packed));
|
} __attribute__((packed, aligned(4)));
|
||||||
|
|
||||||
typedef struct gdt_segment_descriptor gdt_segment_descriptor_t;
|
typedef struct gdt_segment_descriptor gdt_segment_descriptor_t;
|
||||||
|
|
||||||
|
|||||||
@@ -8,9 +8,9 @@
|
|||||||
|
|
||||||
#define APPA_OS_VERSION_MAJOR 0
|
#define APPA_OS_VERSION_MAJOR 0
|
||||||
#define APPA_OS_VERSION_MINOR 0
|
#define APPA_OS_VERSION_MINOR 0
|
||||||
#define APPA_OS_VERSION_PATCH 132
|
#define APPA_OS_VERSION_PATCH 150
|
||||||
#define APPA_OS_COMMIT_HASH cd01d7d8d36862bc00ae88f7cd185c352a7b7eda
|
#define APPA_OS_COMMIT_HASH cd01d7d8d36862bc00ae88f7cd185c352a7b7eda
|
||||||
#define APPA_OS_BUILD_TIMESTAMP 1730655451
|
#define APPA_OS_BUILD_TIMESTAMP 1730673250
|
||||||
|
|
||||||
#endif // INCLUDE_APPA_OS_VERSION_H_
|
#endif // INCLUDE_APPA_OS_VERSION_H_
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user