Skip to content

Commit 36a79c4

Browse files
committed
* Implemented GDT
* Incremented kernel version * Updated README.md * Updated Makefile
1 parent 7b0c201 commit 36a79c4

File tree

8 files changed

+101
-3
lines changed

8 files changed

+101
-3
lines changed

Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ AS=$(TOOLDIR)/i686-elf-as
1919
ASFLAGS=
2020

2121
NASM=nasm
22-
NASMFLAGS=
22+
NASMFLAGS=-f elf32 -O0 -w+zeroing
2323

2424
QEMU=qemu-system-i386
2525
QEMUFLAGS=
@@ -32,6 +32,8 @@ ISOFILE=$(PROJECT).iso
3232
OBJECTS= $(SRCDIR)/boot/boot.o \
3333
$(SRCDIR)/drivers/io/ports.o \
3434
$(SRCDIR)/drivers/power/reboot.o \
35+
$(SRCDIR)/drivers/cpu/gdt.o \
36+
$(SRCDIR)/drivers/cpu/gdt_helper.o \
3537
$(SRCDIR)/common/libs/string.o \
3638
$(SRCDIR)/common/libs/printf.o \
3739
$(SRCDIR)/drivers/video/vga_text.o \

docs/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
A new rewrite of SectorOS_RE2.
44

5-
The kernel is currently v0.23.03.4ALPR
5+
The kernel is currently v0.23.03.5ALPR
66

77
## Build
88

docs/changelog.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,10 @@
4040
* Updated README.md
4141
* Updated system.h
4242
* Updated string.h
43+
* Updated Makefile
44+
45+
27-03-2023: 08:11 PM IST v0.23.03.5ALPR
46+
* Implemented GDT
47+
* Incremented kernel version
48+
* Updated README.md
4349
* Updated Makefile

src/drivers/cpu/gdt.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include "gdt.h"
2+
3+
gdt_entry_t entries[GDT_MAX_ENTRIES];
4+
gdt_ptr_t ptr;
5+
6+
void init_gdt()
7+
{
8+
printf("[GDT] Configuring GDT pointer...\n");
9+
ptr.limit = (sizeof(gdt_entry_t) * GDT_MAX_ENTRIES) - 1;
10+
ptr.base = (uint32_t)&entries;
11+
12+
printf("[GDT] Adding entries...\n");
13+
14+
gdt_setGate(0, 0, 0, 0, 0);
15+
gdt_setGate(1, 0, 0xFFFFFFFF, 0x9A, 0xCF);
16+
gdt_setGate(2, 0, 0xFFFFFFFF, 0x92, 0xCF);
17+
gdt_setGate(3, 0, 0xFFFFFFFF, 0xFA, 0xCF);
18+
gdt_setGate(4, 0, 0xFFFFFFFF, 0xF2, 0xCF);
19+
20+
printf("[GDT] Flushing GDT entries...\n");
21+
22+
flush_gdt((uint32_t)&ptr);
23+
24+
printf("[GDT] Initialized successfully\n");
25+
}
26+
27+
void gdt_setGate(int32_t index, uint32_t base, uint32_t limit, uint8_t access, uint8_t gran)
28+
{
29+
entries[index].base_low = (base & 0xFFFF);
30+
entries[index].base_middle = (base >> 16) & 0xFF;
31+
entries[index].base_high = (base >> 24) & 0xFF;
32+
entries[index].limit_low = (limit & 0xFFFF);
33+
entries[index].granularity = (limit >> 16) & 0x0F;
34+
entries[index].granularity |= gran & 0xF0;
35+
entries[index].access = access;
36+
printf("[GDT] New entry %d. Base: 0x%06x, Limit: 0x%06x, Access: 0x%02x, Gran: 0x%02x\n", index, base, limit, access, gran);
37+
}

src/drivers/cpu/gdt_helper.asm

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[GLOBAL flush_gdt]
2+
3+
flush_gdt:
4+
mov eax, [esp+4]
5+
lgdt [eax]
6+
7+
mov ax, 0x10
8+
mov ds, ax
9+
mov es, ax
10+
mov fs, ax
11+
mov gs, ax
12+
mov ss, ax
13+
jmp 0x08:.flush
14+
.flush:
15+
ret

src/include/gdt.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#ifndef __GDT_H__
2+
#define __GDT_H__
3+
4+
#include "system.h"
5+
#include "printf.h"
6+
#include "string.h"
7+
8+
extern void flush_gdt(uint32_t gdt_ptr);
9+
10+
typedef struct gdt_entry
11+
{
12+
uint16_t limit_low;
13+
uint16_t base_low;
14+
uint8_t base_middle;
15+
uint8_t access;
16+
uint8_t granularity;
17+
uint8_t base_high;
18+
}__attribute__((packed))gdt_entry_t;
19+
20+
typedef struct gdt_ptr
21+
{
22+
uint16_t limit;
23+
uint32_t base;
24+
}__attribute__((packed))gdt_ptr_t;
25+
26+
#define GDT_MAX_ENTRIES 5
27+
#define GDT_GET_USER_SELECTOR(idx) (idx | 0x03)
28+
29+
extern gdt_entry_t entries[GDT_MAX_ENTRIES];
30+
31+
void init_gdt();
32+
33+
void gdt_setGate(int32_t num, uint32_t base, uint32_t limit, uint8_t access, uint8_t gran);
34+
35+
#endif /*__GDT_H__*/

src/include/system.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ void kernel_panic(char* message);
2626
* RELN: index of current release in the current month
2727
* STATUS: [PR]:Prerelease, [AL]:alpha, [NR]:Normal release
2828
*/
29-
#define KERNEL_VERSION "0.23.03.4ALPR"
29+
#define KERNEL_VERSION "0.23.03.5ALPR"
3030

3131
#define KB 1024
3232
#define MB (1024*KB)

src/kernel/kernel.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "serial.h"
44
#include "vga_text.h"
55
#include "printf.h"
6+
#include "gdt.h"
67

78
void kernelmain(const multiboot_info_t* info, uint32_t multiboot_magic)
89
{
@@ -12,6 +13,8 @@ void kernelmain(const multiboot_info_t* info, uint32_t multiboot_magic)
1213
text_chcolor(VGA_LIGHT_GREEN, VGA_BLACK);
1314
text_clear();
1415

16+
init_gdt();
17+
1518
if(multiboot_magic != 0x2BADB002)
1619
{
1720
serial_puts("multiboot bootloader magic is invalid\n");

0 commit comments

Comments
 (0)