CTAOS v3
This commit is contained in:
19
SysCore/hal/gdt/compile.bat
Normal file
19
SysCore/hal/gdt/compile.bat
Normal file
@ -0,0 +1,19 @@
|
||||
@echo off
|
||||
rem The name of the loader assembly file (without extension, must be .asm):
|
||||
set loader_name=loader
|
||||
|
||||
rem NASM and DJGPP executable paths:
|
||||
set nasm_path=C:\nasm
|
||||
set djgpp_path=C:\DJGPP\bin
|
||||
set objpath=../../objects
|
||||
set incpath=../../include
|
||||
|
||||
@echo on
|
||||
%nasm_path%\nasm.exe -f aout -o %objpath%/gdt_asm.o gdt.asm
|
||||
%djgpp_path%\gcc.exe -Wall -O -fstrength-reduce -fomit-frame-pointer -nostdinc -fno-builtin -I%incpath% -c -o %objpath%/gdt.o gdt.c
|
||||
|
||||
@echo off
|
||||
@echo .
|
||||
@echo Done!
|
||||
|
||||
@pause
|
20
SysCore/hal/gdt/gdt.asm
Normal file
20
SysCore/hal/gdt/gdt.asm
Normal file
@ -0,0 +1,20 @@
|
||||
bits 32
|
||||
; !!! GDT !!!
|
||||
; This will set up our new segment registers. We need to do
|
||||
; something special in order to set CS. We do what is called a
|
||||
; far jump. A jump that includes a segment as well as an offset.
|
||||
; This is declared in C as 'extern void gdt_flush();'
|
||||
|
||||
global _i86_gdt_flush ; Allows the C code to link to this
|
||||
extern _gp ; Says that '_gp' is in another file
|
||||
_i86_gdt_flush:
|
||||
lgdt [_gp] ; Load the GDT with our '_gp' which is a special pointer
|
||||
mov ax, 0x10 ; 0x10 is the offset in the GDT to our data segment
|
||||
mov ds, ax
|
||||
mov es, ax
|
||||
mov fs, ax
|
||||
mov gs, ax
|
||||
mov ss, ax
|
||||
jmp 0x08:flush2 ; 0x08 is the offset to our code segment: Far jump!
|
||||
flush2:
|
||||
ret ; Returns back to the C code!
|
65
SysCore/hal/gdt/gdt.c
Normal file
65
SysCore/hal/gdt/gdt.c
Normal file
@ -0,0 +1,65 @@
|
||||
/******************************************************************
|
||||
* gdt.c - GLOBAL DESCRIPTOR TABLE *
|
||||
* Contains function prototypes for setting up the GDT *
|
||||
******************************************************************/
|
||||
#define MAX_DESCRIPTORS 3
|
||||
#include "gdt.h"
|
||||
|
||||
/* Our GDT, with 3 entries, and finally our special GDT pointer */
|
||||
struct gdt_entry gdt[3];
|
||||
struct gdt_ptr gp;
|
||||
|
||||
|
||||
/* Setup a descriptor in the Global Descriptor Table */
|
||||
void i86_gdt_set_gate(int num, unsigned long base, unsigned long limit, unsigned char access, unsigned char gran)
|
||||
{
|
||||
/* Setup the descriptor base address */
|
||||
gdt[num].base_low = (base & 0xFFFF);
|
||||
gdt[num].base_middle = (base >> 16) & 0xFF;
|
||||
gdt[num].base_high = (base >> 24) & 0xFF;
|
||||
|
||||
/* Setup the descriptor limits */
|
||||
gdt[num].limit_low = (limit & 0xFFFF);
|
||||
gdt[num].granularity = ((limit >> 16) & 0x0F);
|
||||
|
||||
/* Finally, set up the granularity and access flags */
|
||||
gdt[num].granularity |= (gran & 0xF0);
|
||||
gdt[num].access = access;
|
||||
}
|
||||
|
||||
struct gdt_entry* i86_gdt_get_gate(int num)
|
||||
{
|
||||
if (num>MAX_DESCRIPTORS) return 0;
|
||||
return &gdt[num];
|
||||
}
|
||||
|
||||
/* Should be called by main. This will setup the special GDT
|
||||
* pointer, set up the first 3 entries in our GDT, and then
|
||||
* finally call gdt_flush() in our assembler file in order
|
||||
* to tell the processor where the new GDT is and update the
|
||||
* new segment registers */
|
||||
void i86_gdt_install()
|
||||
{
|
||||
/* Setup the GDT pointer and limit */
|
||||
gp.limit = (sizeof(struct gdt_entry) * 3) - 1;
|
||||
gp.base = (unsigned int)&gdt;
|
||||
|
||||
/* Our NULL descriptor */
|
||||
i86_gdt_set_gate(0, 0, 0, 0, 0);
|
||||
|
||||
/* The second entry is our Code Segment. The base address
|
||||
* is 0, the limit is 4GBytes, it uses 4KByte granularity,
|
||||
* uses 32-bit opcodes, and is a Code Segment descriptor.
|
||||
* Please check the table above in the tutorial in order
|
||||
* to see exactly what each value means */
|
||||
i86_gdt_set_gate(1, 0, 0xFFFFFFFF, 0x9A, 0xCF);
|
||||
|
||||
/* The third entry is our Data Segment. It's EXACTLY the
|
||||
* same as our code segment, but the descriptor type in
|
||||
* this entry's access byte says it's a Data Segment */
|
||||
i86_gdt_set_gate(2, 0, 0xFFFFFFFF, 0x92, 0xCF);
|
||||
|
||||
/* Flush out the old GDT and install the new changes! */
|
||||
i86_gdt_flush();
|
||||
}
|
||||
|
39
SysCore/hal/gdt/gdt.h
Normal file
39
SysCore/hal/gdt/gdt.h
Normal file
@ -0,0 +1,39 @@
|
||||
/******************************************************************
|
||||
* gdt.h - GLOBAL DESCRIPTOR TABLE *
|
||||
* Contains structures and function declarations for GDT *
|
||||
******************************************************************/
|
||||
|
||||
#ifndef __GDT_H
|
||||
#define __GDT_H
|
||||
|
||||
/* Defines a GDT entry. We say packed, because it prevents the
|
||||
* compiler from doing things that it thinks is best: Prevent
|
||||
* compiler "optimization" by packing */
|
||||
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));
|
||||
|
||||
/* Special pointer which includes the limit: The max bytes
|
||||
* taken up by the GDT, minus 1. Again, this NEEDS to be packed */
|
||||
struct gdt_ptr
|
||||
{
|
||||
unsigned short limit;
|
||||
unsigned int base;
|
||||
} __attribute__((packed));
|
||||
|
||||
|
||||
/* This will be a function in start.asm. We use this to properly
|
||||
* reload the new segment registers */
|
||||
extern void i86_gdt_flush();
|
||||
extern void i86_gdt_set_gate(int num, unsigned long base, unsigned long limit, unsigned char access, unsigned char gran);
|
||||
extern struct gdt_entry* i86_gdt_get_gate(int num);
|
||||
|
||||
extern void i86_gdt_install();
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user