Added the Global Descriptor Table.

This commit is contained in:
2020-06-19 12:08:17 -05:00
parent b409b3e9d2
commit 84f64badaf
6 changed files with 102 additions and 5 deletions
+50
View File
@@ -0,0 +1,50 @@
#include "../includes/gdt.h"
/* Code taken from http://www.osdever.net/bkerndev/Docs/gdt.htm with some minor tweaks. */
/* 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 load_gdt() in our assembler file in order
* to tell the processor where the new GDT is and update the
* new segment registers */
void gdt_install()
{
struct gdt_entry gdt_entries[3];
struct gdt gdt_ptr;
/* Setup the GDT pointer and the size of the table */
gdt_ptr.size = sizeof(struct gdt_entry) * 3 - 1;
gdt_ptr.address = (unsigned int) &gdt_entries;
/* Our NULL descriptor */
gdt_set_gate(0, 0, 0, 0, &gdt_entries[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 */
gdt_set_gate(0, 0xFFFFFFFF, 0x9A, 0xCF, &gdt_entries[1]);
/* 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 */
gdt_set_gate(0, 0xFFFFFFFF, 0x92, 0xCF, &gdt_entries[2]);
/* Flush out the old GDT and install the new changes! */
load_gdt(&gdt_ptr);
}
void gdt_set_gate(unsigned long base, unsigned long limit, unsigned char access, unsigned char gran, struct gdt_entry* gdt_entry)
{
/* Setup the descriptor base address */
gdt_entry->base_low = (base & 0xFFFF);
gdt_entry->base_middle = (base >> 16) & 0xFF;
gdt_entry->base_high = (base >> 24) & 0xFF;
/* Setup the descriptor limits */
gdt_entry->limit_low = (limit & 0xFFFF);
gdt_entry->granularity = ((limit >> 16) & 0x0F);
/* Finally, set up the granularity and access flags */
gdt_entry->granularity |= (gran & 0xF0);
gdt_entry->access = access;
}
+20 -1
View File
@@ -16,6 +16,25 @@ global inb
; stack: [esp + 4] The address of the I/O port
; [esp ] The return address
inb:
mov dx, [esp + 4] ; Move the address of the I/O port to the dx register.
mov dx, [esp + 4] ; Move the address of the I/O port to the dx register
in al, dx ; Read a byte from the I/O port and store it in the al register
ret
global load_gdt
; load_gdt - Installs a new GDT
; stack: [esp + 4] The address of the GDT
; [esp ] The return addess
load_gdt:
mov eax, [esp + 4]
lgdt [eax]
mov ax, 0x10
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
mov ss, ax
jmp 0x08:return ; Perform a "far jump" to load 0x08 in the CS register
return:
ret ; Now we've changed CS to 0x08
+8 -1
View File
@@ -1,4 +1,5 @@
#include "../includes/io.h"
#include "../includes/gdt.h"
#include "../includes/serial.h"
#include "../includes/screen.h"
@@ -26,7 +27,7 @@ void kmain()
move_cursor(5, 20);
draw_string("Cursor moved over 5 cells and down 4 cells.", 0xF, 0x0);
draw_string("And the cursor moves to the end of the string", 0x1, 0xF);
draw_string("Now this string should have a newline now:\nAnd now a new line is here.", 0xF, 0x0);
draw_string("Now this string should have a newline now:\nAnd now a new line is here.\n", 0xF, 0x0);
//draw_rect(20, 5, 50, 7, 0x4, 0xF);
//draw_rect(20, 5, 50, 7, 0x4, 0xF);
//draw_rect(20, 5, 50, 7, 0x4, 0xF);
@@ -34,5 +35,11 @@ void kmain()
//row = 2;
///column = 80;
//draw_s("And 2nd Row. Now the 3rd row.", 2, 9);
move_cursor(0, 3);
draw_string("Installing GDT...\n", 0xF, 0x0);
gdt_install();
move_cursor(0, 4);
draw_string("Installed GDT\n", 0xf, 0x0);
asm("hlt");
}