pretty-print GDT entries

This commit is contained in:
Quinten Kock 2020-12-07 22:48:28 +01:00
parent 22d75a4d43
commit b0e433fca2
5 changed files with 45 additions and 11 deletions

32
kernel/src/arch/x86/gdt.c Normal file
View File

@ -0,0 +1,32 @@
#include <print/print.h>
#include "gdt.h"
void print_gdt_ent(uint64_t* ent) {
uint64_t e = *ent;
uint64_t base = ((e >> 16) & 0xFFFFFF) | ((e >> 32) & 0xFF000000);
uint64_t limit = (e & 0xFFFF) | ((e >> 32) & 0xF0000);
uint64_t flags = ((e >> 52) & 0xF);
uint64_t access = ((e >> 40) & 0xFF);
uint64_t code = (access & 0x8);
int bits = (flags&0x2) ? 64 : ((flags&0x4) ? 32 : 16);
uint64_t length = limit * ((flags & 0x8) ? 4096 : 1);
printf("%X (%h): \t", base, length);
if(e == 0) {
puts("NULL");
return;
}
if(access & 0x80) putchar('P');
printf("%d", (access >> 5) & 0x3);
if(access & 0x10) putchar('S');
if(code) printf(" Code%d\t", bits); else puts(" Data\t");
if(access & 0x4) putchar(code ? 'C' : 'D');
if(access & 0x2) putchar(code ? 'R' : 'W');
if(access & 0x1) putchar('A');
if(flags & 0x8) putchar('G');
if(flags & 0x1) putchar('z');
}

View File

@ -0,0 +1,3 @@
#include <stdint.h>
void print_gdt_ent(uint64_t* ent);

View File

@ -3,11 +3,12 @@
#include <stddef.h>
#include <arch/x86/stivale2-parse.h>
#include <arch/x86/gdt.h>
#include <arch/x86_64/gdt.h>
#include <print/print.h>
#include <main.h>
#include "gdt.h"
static uint8_t stack[4096] = {0};
void stivale2_main(struct stivale2_struct *info);
@ -46,7 +47,7 @@ void stivale2_main(struct stivale2_struct *info) {
printf("GDT: %x (%d)\n", pGDT.base, pGDT.limit);
for(int i = 0; i < (pGDT.limit+1)/8; i++) {
uint64_t *gdt_ent = pGDT.base + 8*i;
printf("GDTent(%d at %x): %x\n", i, gdt_ent, *gdt_ent);
printf("GDT %d: ", i); print_gdt_ent(gdt_ent); putchar('\n');
}
char *argv[3] = {"stivale2", info->bootloader_brand, info->bootloader_version};

View File

@ -6,4 +6,4 @@ struct gdt_ptr
uint64_t base;
} __attribute__((packed));
void loadgdt();
void loadgdt();

View File

@ -1,5 +1,6 @@
#include <stddef.h>
#include <stdarg.h>
#include <stdbool.h>
#include "print.h"
@ -16,15 +17,10 @@ void print(const char* c) {
static const char CONVERSION_TABLE[] = "0123456789abcdef";
static const char UNITS[] = "bKM";
static void printhex(size_t num) {
static void printhex(size_t num, bool pad) {
int i;
char buf[17];
if (!num) {
puts("0x0");
return;
}
buf[16] = 0;
for (i = 15; num; i--) {
@ -33,7 +29,7 @@ static void printhex(size_t num) {
}
puts("0x");
for(int j = i; j >= 0; j--) {
for(int j = i; pad && j >= 0; j--) {
putchar('0');
}
@ -79,7 +75,9 @@ void printf(const char *format, ...) {
if (*format == '%') {
format++;
if (*format == 'x') {
printhex(va_arg(argp, size_t));
printhex(va_arg(argp, size_t), false);
} else if (*format == 'X') {
printhex(va_arg(argp, size_t), true);
} else if (*format == 'd') {
printdec(va_arg(argp, size_t));
} else if (*format == 's') {