Some vague attempts at GDT stuff

This commit is contained in:
Gitea 2020-12-01 20:59:57 -06:00
parent e04489a8c3
commit f15f5f4fc4
3 changed files with 76 additions and 0 deletions

34
arch/i386/gdt.S Normal file
View file

@ -0,0 +1,34 @@
.globl set_gdt
.type set_gdt,%function
gdtr:
.word 0
.int 0
set_gdt:
.fnstart
mov 4(%esp), %eax
mov %eax, 2($gdtr)
mov 8(%esp), %ax
mov %ax, $gdtr
lgdt $gdtr
ret
.fnend
.globl reload_segments
.type reload_segments,%function
reload_segments:
.fnstart
jmp 0x08:reload_CS
.fnend
reload_CS:
mov 0x10, %ax
mov %ax, %ds
mov %ax, %es
mov %ax, %fs
mov %ax, %gs
mov %ax, %ss
ret

28
arch/i386/gdt.c Normal file
View file

@ -0,0 +1,28 @@
void encode_gdt_entry(unsigned short int * target, struct GDT source) {
if((source.limit > 65536) && ((source.limit & 0xFFF) != 0xFFF)) {
/* This needs to error out. */
return;
}
if(source.limit > 65536) {
/* Granularity adjustments */
source.limit = source.limit >> 12;
target[6] = 0xC0;
}
else {
target[6] = 0x40;
}
/* Encode limit */
target[0] = source.limit & 0xFF;
target[1] = (source.limit >> 8) & 0xFF;
target[6] |= (source.limit >> 16) & 0xF;
/* Encode base */
target[2] = source.base & 0xFF;
target[3] = (source.base >> 8) & 0xFF;
target[4] = (source.base >> 16) & 0xFF;
target[7] = (source.base >> 24) & 0xFF;
/* Encode type */
target[5] = source.type;
}

14
arch/i386/gdt.h Normal file
View file

@ -0,0 +1,14 @@
#ifndef _ARCH_I386_GDT_H
#define _ARCH_I386_GDT_H
struct GDT {
unsigned int limit;
unsigned long int base;
unsigned short int type;
};
void encode_gdt_entry(unsigned short int *, struct GDT);
extern void set_gdt(void);
extern void reload_segments(void);
#endif