Merge pull request #3 from xamidev/gdt
GDT init (load + flush)
This commit was merged in pull request #3.
This commit is contained in:
4
Makefile
4
Makefile
@@ -1,6 +1,6 @@
|
||||
build:
|
||||
rm -f *.o
|
||||
x86_64-elf-gcc -g -c -I src src/io/serial.c src/io/term.c src/io/printf.c src/kmain.c -Wall -Wextra -std=gnu99 -nostdlib -ffreestanding -fno-stack-protector -fno-stack-check -fno-PIC -ffunction-sections -fdata-sections -mcmodel=kernel
|
||||
x86_64-elf-gcc -g -c -I src src/mem/utils.h src/mem/gdt.c src/io/serial.c src/io/term.c src/io/printf.c src/kmain.c -Wall -Wextra -std=gnu99 -nostdlib -ffreestanding -fno-stack-protector -fno-stack-check -fno-PIC -ffunction-sections -fdata-sections -mcmodel=kernel
|
||||
objcopy -O elf64-x86-64 -B i386 -I binary zap-light16.psf zap-light16.o
|
||||
x86_64-elf-ld -o pepperk -T linker.ld *.o
|
||||
|
||||
@@ -27,7 +27,7 @@ build-iso: limine/limine build
|
||||
./limine/limine bios-install pepper.iso
|
||||
|
||||
debug:
|
||||
qemu-system-x86_64 -drive file=pepper.iso -s -S &
|
||||
qemu-system-x86_64 -drive file=pepper.iso -s -S -d int -no-reboot &
|
||||
gdb pepperk --command=debug.gdb
|
||||
|
||||
run: build-iso
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
target remote localhost:1234
|
||||
target remote localhost:1234
|
||||
set disassembly-flavor intel
|
||||
75
src/kmain.c
75
src/kmain.c
@@ -4,6 +4,8 @@
|
||||
#include "io/term.h"
|
||||
#include "io/printf.h"
|
||||
#include "io/serial.h"
|
||||
#include "mem/gdt.h"
|
||||
#include "mem/utils.h"
|
||||
|
||||
// Limine version used
|
||||
__attribute__((used, section(".limine_requests")))
|
||||
@@ -24,74 +26,6 @@ static volatile LIMINE_REQUESTS_END_MARKER;
|
||||
|
||||
struct limine_framebuffer* framebuffer;
|
||||
|
||||
// We won't be linked to standard library, but still need the basic mem* functions
|
||||
// so everything goes allright with the compiler
|
||||
|
||||
// We use the "restrict" keyword on pointers so that the compiler knows it can
|
||||
// do more optimization on them (and as it's a much used function, it's good to
|
||||
// be able to do that)
|
||||
void* memcpy(void* restrict dest, const void* restrict src, size_t n)
|
||||
{
|
||||
uint8_t* restrict pdest = (uint8_t* restrict)dest;
|
||||
const uint8_t* restrict psrc = (const uint8_t* restrict)src;
|
||||
|
||||
for (size_t i=0; i<n; i++)
|
||||
{
|
||||
pdest[i] = psrc[i];
|
||||
}
|
||||
|
||||
return dest;
|
||||
}
|
||||
|
||||
void* memset(void* s, int c, size_t n)
|
||||
{
|
||||
uint8_t* p = (uint8_t*)s;
|
||||
|
||||
for (size_t i=0; i<n; i++)
|
||||
{
|
||||
p[i] = (uint8_t)c;
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
void* memmove(void *dest, const void* src, size_t n)
|
||||
{
|
||||
uint8_t* pdest = (uint8_t*)dest;
|
||||
const uint8_t* psrc = (uint8_t*)src;
|
||||
|
||||
if (src > dest)
|
||||
{
|
||||
for (size_t i=0; i<n; i++)
|
||||
{
|
||||
pdest[i] = psrc[i];
|
||||
}
|
||||
} else if (src < dest)
|
||||
{
|
||||
for (size_t i=n; i>0; i--)
|
||||
{
|
||||
pdest[i-1] = psrc[i-1];
|
||||
}
|
||||
}
|
||||
return dest;
|
||||
}
|
||||
|
||||
int memcmp(const void* s1, const void* s2, size_t n)
|
||||
{
|
||||
const uint8_t* p1 = (const uint8_t*)s1;
|
||||
const uint8_t* p2 = (const uint8_t*)s2;
|
||||
|
||||
for (size_t i=0; i<n; i++)
|
||||
{
|
||||
if (p1[i] != p2[i])
|
||||
{
|
||||
return p1[i] < p2[i] ? -1 : 1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Panic
|
||||
static void hcf()
|
||||
{
|
||||
@@ -114,7 +48,10 @@ void kmain()
|
||||
|
||||
if (serial_init()) kputs("kernel: serial: error: Cannot init serial communication!");
|
||||
|
||||
serial_kputs("\n\nkernel: serial: Hello, world from serial!");
|
||||
serial_kputs("\n\nkernel: serial: Hello, world from serial!\n");
|
||||
|
||||
gdt_init();
|
||||
serial_kputs("kernel: gdt: Initialized GDT!");
|
||||
|
||||
// Draw something
|
||||
printf("%s, %s!", "Hello", "world");
|
||||
|
||||
79
src/mem/gdt.c
Normal file
79
src/mem/gdt.c
Normal file
@@ -0,0 +1,79 @@
|
||||
#include "gdt.h"
|
||||
#include <stdint.h>
|
||||
|
||||
// Descriptors are 8-byte wide (64bits)
|
||||
// So the selectors will be (in bytes): 0x0, 0x8, 0x10, 0x18, etc..
|
||||
uint64_t gdt_entries[NUM_GDT_ENTRIES];
|
||||
struct GDTR gdtr;
|
||||
|
||||
static void gdt_load()
|
||||
{
|
||||
asm("lgdt %0" : : "m"(gdtr));
|
||||
}
|
||||
|
||||
static void gdt_flush()
|
||||
{
|
||||
// Here, 0x8 is the kernel code selector
|
||||
// and 0x10 is the kernel data selector
|
||||
asm volatile (
|
||||
"mov $0x10, %%ax \n" // Reload segments with kernel data selector
|
||||
"mov %%ax, %%ds \n"
|
||||
"mov %%ax, %%es \n"
|
||||
"mov %%ax, %%fs \n"
|
||||
"mov %%ax, %%gs \n"
|
||||
"mov %%ax, %%ss \n"
|
||||
|
||||
"pushq $0x8 \n" // CS reload
|
||||
"lea 1f(%%rip), %%rax \n"
|
||||
"push %%rax \n"
|
||||
"lretq \n"
|
||||
"1: \n" // Execution continues here after CS reload
|
||||
:
|
||||
:
|
||||
: "rax", "memory"
|
||||
);
|
||||
}
|
||||
|
||||
void gdt_init()
|
||||
{
|
||||
// Null descriptor (required)
|
||||
gdt_entries[0] = 0;
|
||||
|
||||
// Kernel code segment
|
||||
uint64_t kernel_code = 0;
|
||||
kernel_code |= 0b1101 << 8; // Selector type: accessed, read-enable, no conforming
|
||||
kernel_code |= 1 << 12; // not a system descriptor
|
||||
kernel_code |= 0 << 13; // DPL field = 0
|
||||
kernel_code |= 1 << 15; // Present
|
||||
kernel_code |= 1 << 21; // Long mode
|
||||
|
||||
// Left shift 32 bits so we place our stuff in the upper 32 bits of the descriptor.
|
||||
// The lower 32 bits contain limit and part of base and therefore are ignored in Long Mode
|
||||
// (because we'll use paging; segmentation is used only for legacy)
|
||||
gdt_entries[1] = kernel_code << 32;
|
||||
|
||||
uint64_t kernel_data = 0;
|
||||
kernel_data |= 0b0011 << 8;
|
||||
kernel_data |= 1 << 12;
|
||||
kernel_data |= 0 << 13;
|
||||
kernel_data |= 1 << 15;
|
||||
kernel_data |= 1 << 21;
|
||||
|
||||
gdt_entries[2] = kernel_data << 32;
|
||||
|
||||
// We re-use the kernel descriptors here, and just update their DPL fields
|
||||
// (Descriptor privilege level) from ring 0 -> to ring 3 (userspace)
|
||||
uint64_t user_code = kernel_code | (3 << 13);
|
||||
gdt_entries[3] = user_code;
|
||||
|
||||
uint64_t user_data = kernel_data | (3 << 13);
|
||||
gdt_entries[4] = user_data;
|
||||
|
||||
// The -1 subtraction is some wizardry explained in the OSDev wiki -> GDT
|
||||
gdtr.limit = NUM_GDT_ENTRIES * sizeof(uint64_t) - 1;
|
||||
gdtr.address = (uint64_t)gdt_entries;
|
||||
|
||||
// Load the GDT we created, flush the old one
|
||||
gdt_load();
|
||||
gdt_flush();
|
||||
}
|
||||
26
src/mem/gdt.h
Normal file
26
src/mem/gdt.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#ifndef GDT_H
|
||||
#define GDT_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
// We're using the GDT for segmentation, but as we want to target Long Mode,
|
||||
// we'll only use this as a requirement for paging, not more.
|
||||
// This means base 0 and no limit (whole address space)
|
||||
|
||||
#define NUM_GDT_ENTRIES 5
|
||||
|
||||
#define NULL_SELECTOR 0x00
|
||||
#define KERNEL_CODE_SEGMENT 0x08
|
||||
#define KERNEL_DATA_SEGMENT 0x10
|
||||
#define USER_CODE_SEGMENT 0x18
|
||||
#define USER_DATA_SEGMENT 0x20
|
||||
|
||||
struct GDTR
|
||||
{
|
||||
uint16_t limit;
|
||||
uint64_t address;
|
||||
} __attribute__((packed));
|
||||
|
||||
void gdt_init();
|
||||
|
||||
#endif
|
||||
69
src/mem/utils.c
Normal file
69
src/mem/utils.c
Normal file
@@ -0,0 +1,69 @@
|
||||
#include <stddef.h>
|
||||
|
||||
// We won't be linked to standard library, but still need the basic mem* functions
|
||||
// so everything goes allright with the compiler
|
||||
|
||||
// We use the "restrict" keyword on pointers so that the compiler knows it can
|
||||
// do more optimization on them (and as it's a much used function, it's good to
|
||||
// be able to do that)
|
||||
void* memcpy(void* restrict dest, const void* restrict src, size_t n)
|
||||
{
|
||||
uint8_t* restrict pdest = (uint8_t* restrict)dest;
|
||||
const uint8_t* restrict psrc = (const uint8_t* restrict)src;
|
||||
|
||||
for (size_t i=0; i<n; i++)
|
||||
{
|
||||
pdest[i] = psrc[i];
|
||||
}
|
||||
|
||||
return dest;
|
||||
}
|
||||
|
||||
void* memset(void* s, int c, size_t n)
|
||||
{
|
||||
uint8_t* p = (uint8_t*)s;
|
||||
|
||||
for (size_t i=0; i<n; i++)
|
||||
{
|
||||
p[i] = (uint8_t)c;
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
void* memmove(void *dest, const void* src, size_t n)
|
||||
{
|
||||
uint8_t* pdest = (uint8_t*)dest;
|
||||
const uint8_t* psrc = (uint8_t*)src;
|
||||
|
||||
if (src > dest)
|
||||
{
|
||||
for (size_t i=0; i<n; i++)
|
||||
{
|
||||
pdest[i] = psrc[i];
|
||||
}
|
||||
} else if (src < dest)
|
||||
{
|
||||
for (size_t i=n; i>0; i--)
|
||||
{
|
||||
pdest[i-1] = psrc[i-1];
|
||||
}
|
||||
}
|
||||
return dest;
|
||||
}
|
||||
|
||||
int memcmp(const void* s1, const void* s2, size_t n)
|
||||
{
|
||||
const uint8_t* p1 = (const uint8_t*)s1;
|
||||
const uint8_t* p2 = (const uint8_t*)s2;
|
||||
|
||||
for (size_t i=0; i<n; i++)
|
||||
{
|
||||
if (p1[i] != p2[i])
|
||||
{
|
||||
return p1[i] < p2[i] ? -1 : 1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
11
src/mem/utils.h
Normal file
11
src/mem/utils.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#ifndef MEM_UTILS_H
|
||||
#define MEM_UTILS_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
void* memcpy(void* restrict dest, const void* restrict src, size_t n);
|
||||
void* memset(void* s, int c, size_t n);
|
||||
void* memmove(void *dest, const void* src, size_t n);
|
||||
int memcmp(const void* s1, const void* s2, size_t n);
|
||||
|
||||
#endif
|
||||
BIN
src/mem/utils.h.gch
Normal file
BIN
src/mem/utils.h.gch
Normal file
Binary file not shown.
Reference in New Issue
Block a user