Add: GDT, entry initializing for code and data segments
This commit is contained in:
48
gdt.c
Normal file
48
gdt.c
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
#include "gdt.h"
|
||||||
|
|
||||||
|
struct gdt_entry
|
||||||
|
{
|
||||||
|
unsigned short limit_low;
|
||||||
|
unsigned short base_low;
|
||||||
|
unsigned char base_middle;
|
||||||
|
unsigned char access;
|
||||||
|
unsigned char granularity;
|
||||||
|
unsigned char base_high;
|
||||||
|
} __attribute__((packed));
|
||||||
|
|
||||||
|
struct gdt_ptr
|
||||||
|
{
|
||||||
|
unsigned short limit;
|
||||||
|
unsigned int base;
|
||||||
|
} __attribute__((packed));
|
||||||
|
|
||||||
|
struct gdt_entry gdt[3];
|
||||||
|
struct gdt_ptr gp;
|
||||||
|
|
||||||
|
extern void gdt_flush();
|
||||||
|
|
||||||
|
void gdt_set_gate(int num, unsigned long base, unsigned long limit, unsigned char access, unsigned char gran)
|
||||||
|
{
|
||||||
|
gdt[num].base_low = (base & 0xFFFF);
|
||||||
|
gdt[num].base_middle = (base >> 16) & 0xFF;
|
||||||
|
gdt[num].base_high = (base >> 24) & 0xFF;
|
||||||
|
|
||||||
|
gdt[num].limit_low = (limit & 0xFFFF);
|
||||||
|
gdt[num].granularity = ((limit >> 16) & 0x0F);
|
||||||
|
|
||||||
|
gdt[num].granularity |= (gran & 0xF0);
|
||||||
|
gdt[num].access = access;
|
||||||
|
}
|
||||||
|
|
||||||
|
void gdt_install()
|
||||||
|
{
|
||||||
|
gp.limit = (sizeof(struct gdt_entry)*3) - 1;
|
||||||
|
gp.base = (unsigned int)&gdt;
|
||||||
|
|
||||||
|
gdt_set_gate(0, 0, 0, 0, 0);
|
||||||
|
|
||||||
|
gdt_set_gate(1, 0, 0xFFFFFFFF, 0x9A, 0xCF);
|
||||||
|
gdt_set_gate(2, 0, 0xFFFFFFFF, 0x92, 0xCF);
|
||||||
|
|
||||||
|
gdt_flush();
|
||||||
|
}
|
||||||
8
gdt.h
Normal file
8
gdt.h
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
#ifndef GDT_H
|
||||||
|
#define GDT_H
|
||||||
|
|
||||||
|
void gdt_set_gate(int num, unsigned long base, unsigned long limit, unsigned char access, unsigned char gran);
|
||||||
|
|
||||||
|
void gdt_install();
|
||||||
|
|
||||||
|
#endif
|
||||||
Binary file not shown.
BIN
kernel.elf
BIN
kernel.elf
Binary file not shown.
7
kmain.c
7
kmain.c
@@ -1,14 +1,19 @@
|
|||||||
#include "stdio.h"
|
#include "stdio.h"
|
||||||
#include "serial.h"
|
#include "serial.h"
|
||||||
|
#include "gdt.h"
|
||||||
|
|
||||||
int kmain(int retvalue)
|
int kmain(int retvalue)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
gdt_install();
|
||||||
|
|
||||||
// serial testing
|
// serial testing
|
||||||
|
|
||||||
init_serial();
|
init_serial();
|
||||||
|
|
||||||
log("serial connection established", 3);
|
log("serial connection established", 3);
|
||||||
log("Kernel started", 2);
|
log("initialized GDT entries", 2);
|
||||||
|
log("kernel started", 2);
|
||||||
|
|
||||||
clear();
|
clear();
|
||||||
|
|
||||||
|
|||||||
17
loader.s
17
loader.s
@@ -14,6 +14,7 @@ align 4
|
|||||||
extern kmain
|
extern kmain
|
||||||
|
|
||||||
loader:
|
loader:
|
||||||
|
cli
|
||||||
; mov eax, 0xCAFEBABE
|
; mov eax, 0xCAFEBABE
|
||||||
push dword 42
|
push dword 42
|
||||||
call kmain
|
call kmain
|
||||||
@@ -21,6 +22,22 @@ loader:
|
|||||||
.loop:
|
.loop:
|
||||||
jmp .loop
|
jmp .loop
|
||||||
|
|
||||||
|
global gdt_flush
|
||||||
|
extern gp
|
||||||
|
|
||||||
|
gdt_flush:
|
||||||
|
lgdt [gp]
|
||||||
|
mov ax, 0x10
|
||||||
|
mov ds, ax
|
||||||
|
mov es, ax
|
||||||
|
mov fs, ax
|
||||||
|
mov gs, ax
|
||||||
|
mov ss, ax
|
||||||
|
jmp 0x08:flush2
|
||||||
|
|
||||||
|
flush2:
|
||||||
|
ret
|
||||||
|
|
||||||
section .bss
|
section .bss
|
||||||
align 4
|
align 4
|
||||||
kernel_stack:
|
kernel_stack:
|
||||||
|
|||||||
2
makefile
2
makefile
@@ -1,4 +1,4 @@
|
|||||||
OBJECTS = loader.o kmain.o stdio.o io.o string.o serial.o
|
OBJECTS = loader.o kmain.o stdio.o io.o string.o serial.o gdt.o
|
||||||
CC = gcc
|
CC = gcc
|
||||||
CFLAGS = -m32 -nostdlib -nostdinc -fno-builtin -fno-stack-protector -nostartfiles -nodefaultlibs -Wall -Wextra -c
|
CFLAGS = -m32 -nostdlib -nostdinc -fno-builtin -fno-stack-protector -nostartfiles -nodefaultlibs -Wall -Wextra -c
|
||||||
LDFLAGS = -T link.ld -melf_i386
|
LDFLAGS = -T link.ld -melf_i386
|
||||||
|
|||||||
Reference in New Issue
Block a user