diff --git a/tests/gdt.c b/tests/gdt.c new file mode 100644 index 0000000..36bc254 --- /dev/null +++ b/tests/gdt.c @@ -0,0 +1,69 @@ +/* $Id$ */ +/* Copyright (c) 2025 Pierre Pronchery */ +/* This file is part of DeforaOS uKernel */ + + + +#include +#ifndef vaddr_t +# define vaddr_t unsigned long +#endif +#include "../src/loader/gdt.c" + + +#if defined(__amd64__) || defined(__i386__) +/* __arch_setgdt */ +void __arch_setgdt(GDTEntry const * entries, size_t count) +{ + size_t i; + GDTEntry const * entry; + + printf("sizeof(*entries) => %zu\n", sizeof(*entries)); + for(i = 0; i < count; i++) + { + entry = &entries[i]; + printf("base=0x%02x%02x%02x%02x ", + entry->base3, entry->base2, + entry->base1, entry->base0); + printf("limit=0x%x%02x%02x ", + entry->limit2, entry->limit1, entry->limit0); + printf("access=0x%02x ", entry->access); + printf("flags=0x%0x\n", entry->flags); + } + printf("count=0x%zx => size=0x%zx\n", count, (count << 3) - 1); +} +#endif + + +/* main */ +int main(int argc, char * argv[]) +{ +#if defined(__amd64__) || defined(__i386__) + GDT * gdt; + (void) argc; + (void) argv; + + printf("sizeof(GDTEntry) = %zu (expected: %u)\n\n", + sizeof(GDTEntry), 8); + + /* flat 4 GB */ + printf("Flat 4 GB:\n"); + gdt = gdt_init(); + gdt_append(gdt, 0x00000000, 0xffffffff, PROT_READ | PROT_EXEC); + gdt_append(gdt, 0x00000000, 0xffffffff, PROT_READ | PROT_WRITE); + gdt_apply(gdt); + + /* 4 MB code + 4 MB data (read-write) + 4 MB data (read-only) */ + printf("\n4 MB (rx) + 4 MB (rw) + 4 MB (ro):\n"); + gdt = gdt_init(); + gdt_append(gdt, 0x00400000, 0x00400000, PROT_READ | PROT_EXEC); + gdt_append(gdt, 0x00800000, 0x00400000, PROT_READ | PROT_WRITE); + gdt_append(gdt, 0x00c00000, 0x00400000, PROT_READ); + gdt_apply(gdt); + +#endif + (void) argc; + (void) argv; + + return 0; +} diff --git a/tests/project.conf b/tests/project.conf index 0f985b9..8bd17b7 100644 --- a/tests/project.conf +++ b/tests/project.conf @@ -1,4 +1,6 @@ -targets=clint.log,distcheck.log,fixme.log,grub.log +targets=clint.log,distcheck.log,fixme.log,gdt,grub.log +cppflags_force=-I../src +cflags_force=-W -Wall -g -O2 dist=Makefile,clint.sh,distcheck.sh,fixme.sh,grub.sh #targets @@ -20,8 +22,16 @@ script=./fixme.sh depends=fixme.sh enabled=0 +[gdt] +type=binary +sources=gdt.c + [grub.log] type=script script=./grub.sh depends=$(OBJDIR)../src/kernel/uKernel.bin,grub.sh enabled=0 + +#sources +[gdt.c] +depends=../src/loader/gdt.c,../src/arch/amd64/gdt.h,../src/arch/i386/gdt.h