Implemented GDT initialisation

This commit is contained in:
2022-12-24 10:28:41 +00:00
parent 0c0830cca7
commit 26853c32c8
10 changed files with 129 additions and 3 deletions

14
arch/x86_64/cpu.c Normal file
View File

@@ -0,0 +1,14 @@
#include <socks/machine/cpu.h>
#include <socks/machine/_gdt.h>
int ml_cpu_block_init(ml_cpu_block *p)
{
gdt_init(&p->c_gdt, &p->c_gdt_ptr);
return 0;
}
int ml_cpu_block_use(ml_cpu_block *p)
{
gdt_load(&p->c_gdt_ptr);
return 0;
}

27
arch/x86_64/gdt.c Normal file
View File

@@ -0,0 +1,27 @@
#include <socks/libc/string.h>
#include <socks/machine/_gdt.h>
static void init_entry(struct gdt_entry *entry, int access, int flags)
{
entry->ge_limit_low = 0xFFFF;
entry->ge_base_low = 0x00;
entry->ge_base_mid = 0x00;
entry->ge_access = access;
entry->ge_gran = flags;
entry->ge_base_hi = 0x00;
}
int gdt_init(struct gdt *gdt, struct gdt_ptr *gdtp)
{
memset(&gdt->g_entries[0], 0x0, sizeof gdt->g_entries[0]);
init_entry(&gdt->g_entries[1], GDT_A_PRESENT | GDT_A_CODEREAD | GDT_A_CODE, GDT_F_64BIT);
init_entry(&gdt->g_entries[2], GDT_A_PRESENT | GDT_A_DATAWRITE | GDT_A_DATA, GDT_F_64BIT);
init_entry(&gdt->g_entries[3], GDT_A_PRESENT | GDT_A_USER | GDT_A_CODEREAD | GDT_A_CODE, GDT_F_64BIT);
init_entry(&gdt->g_entries[4], GDT_A_PRESENT | GDT_A_USER | GDT_A_DATAWRITE | GDT_A_DATA, GDT_F_64BIT);
gdtp->g_ptr = (uint64_t)gdt;
gdtp->g_limit = sizeof(*gdt) - 1;
return 0;
}

28
arch/x86_64/gdt_load.S Normal file
View File

@@ -0,0 +1,28 @@
.section .text
.global gdt_load
.type gdt_load, @function
gdt_load:
pushq %rbp
movq %rsp, %rbp
lgdt (%rdi)
mov $0x10, %ax
mov %ax, %ss
mov %ax, %ds
mov %ax, %es
mov %ax, %fs
mov %ax, %gs
movabsq $(gdt_load2), %rax
push $0x08
push %rax
lretq
gdt_load2:
popq %rbp
ret

0
arch/x86_64/idt.c Normal file
View File

View File

@@ -0,0 +1,42 @@
#ifndef SOCKS_X86_64__GDT_H_
#define SOCKS_X86_64__GDT_H_
#include <socks/compiler.h>
#include <stdint.h>
#define NR_GDT_ENTRIES 5
#define GDT_A_PRESENT (1 << 7)
#define GDT_A_USER (3 << 5)
#define GDT_A_CODE (3 << 3)
#define GDT_A_DATA (8 << 1)
#define GDT_A_RPLK (1 << 5)
#define GDT_A_CODEREAD (1 << 1)
#define GDT_A_DATAWRITE (1 << 1)
#define GDT_A_GROWUP (1 << 5)
#define GDT_F_64BIT (0x2F)
#define GDT_F_32BIT (0x4F)
#define GDT_F_16BIT (0xF)
struct gdt_entry {
uint16_t ge_limit_low;
uint16_t ge_base_low;
uint8_t ge_base_mid;
uint8_t ge_access;
uint8_t ge_gran;
uint8_t ge_base_hi;
} __packed;
struct gdt {
struct gdt_entry g_entries[NR_GDT_ENTRIES];
} __packed;
struct gdt_ptr {
uint16_t g_limit;
uint64_t g_ptr;
} __packed;
extern int gdt_init(struct gdt *gdt, struct gdt_ptr *gdtp);
extern int gdt_load(struct gdt_ptr *gdtp);
#endif

View File

View File

@@ -1,10 +1,15 @@
#ifndef SOCKS_X86_64_CPU_H_ #ifndef SOCKS_X86_64_CPU_H_
#define SOCKS_X86_64_CPU_H_ #define SOCKS_X86_64_CPU_H_
typedef struct ml_cpu_block { #include <socks/machine/_gdt.h>
typedef struct ml_cpu_block {
struct gdt c_gdt;
struct gdt_ptr c_gdt_ptr;
} ml_cpu_block; } ml_cpu_block;
extern int ml_init_bootcpu(void);
extern int ml_cpu_block_init(ml_cpu_block *p); extern int ml_cpu_block_init(ml_cpu_block *p);
extern int ml_cpu_block_use(ml_cpu_block *p); extern int ml_cpu_block_use(ml_cpu_block *p);

0
arch/x86_64/tss.c Normal file
View File

View File

@@ -2,7 +2,7 @@
#define SOCKS_COMPILER_H_ #define SOCKS_COMPILER_H_
#define __used __attribute__((used)) #define __used __attribute__((used))
#define __packed __attribute__((packed))
#define __section(name) __attribute__((section(name))) #define __section(name) __attribute__((section(name)))
#endif #endif

View File

@@ -3,12 +3,22 @@
#include <socks/init.h> #include <socks/init.h>
#include <socks/console.h> #include <socks/console.h>
#include <socks/machine/init.h> #include <socks/machine/init.h>
#include <socks/machine/cpu.h>
static ml_cpu_block g_bootstrap_cpu = {};
void bootstrap_cpu_init(void)
{
ml_cpu_block_init(&g_bootstrap_cpu);
ml_cpu_block_use(&g_bootstrap_cpu);
}
void kernel_init(uintptr_t arg) void kernel_init(uintptr_t arg)
{ {
bootstrap_cpu_init();
console_init(); console_init();
for (int i = 0; i < 20; i++) { for (int i = 0; i < 8; i++) {
printf("Line %d\n", i); printf("Line %d\n", i);
} }
} }