Skip to content

Commit 016d308

Browse files
committed
* Major update
* Implemented IDT * Implemented interrupt handler * Implemented exception handler * Implemented driver for PIC * Implemented driver for PIT * Implemented stack tracing * Moved kernel_panic and other debug functions to debug.c * Updated boot.s * Fixed the return value in serial.c * Updated system.h * Incremented kernel version * Updated Makefile * Updated README.md
1 parent 36a79c4 commit 016d308

File tree

24 files changed

+734
-17
lines changed

24 files changed

+734
-17
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@
44
.vscode/*
55
**/*.o
66
src/kernel/kernel.o
7+
*.sym
8+
*.log

Makefile

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ INCLUDEDIR=$(SRCDIR)/include/
44
BACKUPDIR=$(SRCDIR)/../../BACKUP/
55
GRUBDIR=
66

7-
87
CC=$(TOOLDIR)/i686-elf-gcc
9-
CFLAGS= -I$(INCLUDEDIR) -I/usr/include -nostdlib -lgcc -fno-builtin -fno-exceptions -fno-leading-underscore -Wno-write-strings -m32
8+
CFLAGS= -I$(INCLUDEDIR) -I/usr/include -nostdlib -lgcc -fno-builtin -fno-exceptions -fno-leading-underscore -Wno-write-strings -m32 -g
109

1110
CXX=$(TOOLDIR)/i686-elf-g++
1211
CXXFLAGS=
@@ -21,8 +20,14 @@ ASFLAGS=
2120
NASM=nasm
2221
NASMFLAGS=-f elf32 -O0 -w+zeroing
2322

23+
OBJCOPY = i686-elf-objcopy
24+
OBJDUMP = i686-elf-objdump
25+
26+
OBJCOPYFLAGS = --strip-debug --strip-unneeded
27+
2428
QEMU=qemu-system-i386
25-
QEMUFLAGS=
29+
QEMUFLAGS=-cdrom SEOS.iso
30+
QEMUDFLAGS=-serial file:serial.log -s -S -daemonize
2631

2732
PROJECT=SEOS
2833

@@ -34,8 +39,18 @@ OBJECTS= $(SRCDIR)/boot/boot.o \
3439
$(SRCDIR)/drivers/power/reboot.o \
3540
$(SRCDIR)/drivers/cpu/gdt.o \
3641
$(SRCDIR)/drivers/cpu/gdt_helper.o \
42+
$(SRCDIR)/drivers/cpu/idt.o \
43+
$(SRCDIR)/drivers/cpu/idt_helper.o \
44+
$(SRCDIR)/drivers/cpu/pic.o \
45+
$(SRCDIR)/drivers/cpu/pit.o \
46+
$(SRCDIR)/interrupts/interrupt.o \
47+
$(SRCDIR)/interrupts/interrupt_helper.o \
48+
$(SRCDIR)/interrupts/exception.o \
49+
$(SRCDIR)/interrupts/exception_helper.o \
50+
$(SRCDIR)/common/libs/debug.o \
3751
$(SRCDIR)/common/libs/string.o \
3852
$(SRCDIR)/common/libs/printf.o \
53+
$(SRCDIR)/common/libs/bit.o \
3954
$(SRCDIR)/drivers/video/vga_text.o \
4055
$(SRCDIR)/drivers/io/serial.o \
4156
$(SRCDIR)/kernel/kernel.o
@@ -77,6 +92,19 @@ $(ISOFILE): $(EXECUTABLE)
7792
@echo '[NASM] $@'
7893
@$(NASM) $(NASMFLAGS) -o $@ $<
7994

95+
run: iso
96+
$(QEMU) $(QEMUFLAGS)
97+
98+
rund: iso
99+
$(QEMU) $(QEMUFLAGS) $(QEMUDFLAGS)
100+
101+
stripd: $(EXECUTABLE)
102+
@$(TOOLDIR)$(OBJCOPY) --only-keep-debug $(EXECUTABLE) debug.sym
103+
@$(TOOLDIR)$(OBJCOPY) $(OBJCOPYFLAGS) $(EXECUTABLE)
104+
105+
forcerun: clean iso run
106+
forcerund: clean iso rund stripd
107+
80108
PHONY: clean
81109
clean:
82110
@echo 'Cleaning the source directory...'

docs/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
A new rewrite of SectorOS_RE2.
44

5-
The kernel is currently v0.23.03.5ALPR
5+
The kernel is currently v2.23.03.6NR
66

77
## Build
88

99
To build the project, execute `make` command in root directory of the project
1010

11+
To run the kernel, execute `make run` command in root directory of the project
12+
1113
## License
1214

1315
This project is licensed under the [GNU GPLv3](../COPYING). <img align="right" src="https://www.gnu.org/graphics/gplv3-with-text-136x68.png"></img>

docs/changelog.txt

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,20 @@
4646
* Implemented GDT
4747
* Incremented kernel version
4848
* Updated README.md
49-
* Updated Makefile
49+
* Updated Makefile
50+
51+
28-03-2023: 07:55 PM IST v1.23.03.6NR
52+
* Major update
53+
* Implemented IDT
54+
* Implemented interrupt handler
55+
* Implemented exception handler
56+
* Implemented driver for PIC
57+
* Implemented driver for PIT
58+
* Implemented stack tracing
59+
* Moved kernel_panic and other debug functions to debug.c
60+
* Updated boot.s
61+
* Fixed the return value in serial.c
62+
* Updated system.h
63+
* Incremented kernel version
64+
* Updated Makefile
65+
* Updated README.md

src/boot/boot.s

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
.set ALIGN, 1<<0
22
.set MEMINFO, 1<<1
3-
.set FLAGS, ALIGN|MEMINFO
3+
.set ELFSHDR, 1<<5
4+
.set FLAGS, (ALIGN|MEMINFO)
45
.set MAGIC, 0x1BADB002
56
.set CHECKSUM, -(MAGIC + FLAGS)
67

@@ -22,6 +23,7 @@ stack_top:
2223
.type _start, @function
2324
_start:
2425
mov $stack_top, %esp
26+
xor %ebp, %ebp
2527
push %eax
2628
push %ebx
2729
call kernelmain

src/common/libs/bit.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include "bit.h"
2+
3+
uint8_t lobyte(uint16_t data)
4+
{
5+
return (data & 0xFF);
6+
}
7+
8+
uint8_t hibyte(uint16_t data)
9+
{
10+
return ((data >> 8) & 0xFF);
11+
}

src/common/libs/debug.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include "debug.h"
2+
#include "elf.h"
3+
4+
void kernel_panic(const char* message)
5+
{
6+
text_chcolor(VGA_LIGHT_RED, VGA_BLACK);
7+
printf("kernel panic: - %s\n", message);
8+
while(1);
9+
}
10+
11+
void stack_trace(uint32_t maxframes)
12+
{
13+
stackframe_t* stk;
14+
asm("movl %%ebp,%0" : "=r"(stk) ::);
15+
printf("stack trace:\n");
16+
for(uint32_t frame = 0; stk && frame < maxframes; frame++)
17+
{
18+
printf("\t[0x%06x]: 0x%06x\n", ((uint32_t)stk), stk->eip);
19+
stk = stk->ebp;
20+
}
21+
}
22+
23+
void backtrace()
24+
{
25+
//! Not Implemented
26+
// Will be implemented once i implment malloc
27+
}

src/drivers/cpu/idt.c

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#include "idt.h"
2+
3+
extern void flush_idt(uint32_t);
4+
5+
idt_entry_t idt_entries[256];
6+
idt_ptr_t idt_ptr;
7+
8+
void init_idt()
9+
{
10+
printf("[IDT] Configuring IDT pointer...\n");
11+
idt_ptr.limit = sizeof(idt_entry_t) * 256 - 1;
12+
idt_ptr.base = (uint32_t)&idt_entries;
13+
14+
memset(&idt_entries, 0, sizeof(idt_entry_t) * 256);
15+
16+
printf("[IDT] Adding entries...\n");
17+
set_idtGate(0, (uint32_t)exception0, 0x08, 0x8E);
18+
set_idtGate(1, (uint32_t)exception1, 0x08, 0x8E);
19+
set_idtGate(2, (uint32_t)exception2, 0x08, 0x8E);
20+
set_idtGate(3, (uint32_t)exception3, 0x08, 0x8E);
21+
set_idtGate(4, (uint32_t)exception4, 0x08, 0x8E);
22+
set_idtGate(5, (uint32_t)exception5, 0x08, 0x8E);
23+
set_idtGate(6, (uint32_t)exception6, 0x08, 0x8E);
24+
set_idtGate(7, (uint32_t)exception7, 0x08, 0x8E);
25+
set_idtGate(8, (uint32_t)exception8, 0x08, 0x8E);
26+
set_idtGate(9, (uint32_t)exception9, 0x08, 0x8E);
27+
set_idtGate(10, (uint32_t)exception10, 0x08, 0x8E);
28+
set_idtGate(11, (uint32_t)exception11, 0x08, 0x8E);
29+
set_idtGate(12, (uint32_t)exception12, 0x08, 0x8E);
30+
set_idtGate(13, (uint32_t)exception13, 0x08, 0x8E);
31+
set_idtGate(14, (uint32_t)exception14, 0x08, 0x8E);
32+
set_idtGate(15, (uint32_t)exception15, 0x08, 0x8E);
33+
set_idtGate(16, (uint32_t)exception16, 0x08, 0x8E);
34+
set_idtGate(17, (uint32_t)exception17, 0x08, 0x8E);
35+
set_idtGate(18, (uint32_t)exception18, 0x08, 0x8E);
36+
set_idtGate(19, (uint32_t)exception19, 0x08, 0x8E);
37+
set_idtGate(20, (uint32_t)exception20, 0x08, 0x8E);
38+
set_idtGate(21, (uint32_t)exception21, 0x08, 0x8E);
39+
set_idtGate(22, (uint32_t)exception22, 0x08, 0x8E);
40+
set_idtGate(23, (uint32_t)exception23, 0x08, 0x8E);
41+
set_idtGate(24, (uint32_t)exception24, 0x08, 0x8E);
42+
set_idtGate(25, (uint32_t)exception25, 0x08, 0x8E);
43+
set_idtGate(26, (uint32_t)exception26, 0x08, 0x8E);
44+
set_idtGate(27, (uint32_t)exception27, 0x08, 0x8E);
45+
set_idtGate(28, (uint32_t)exception28, 0x08, 0x8E);
46+
set_idtGate(29, (uint32_t)exception29, 0x08, 0x8E);
47+
set_idtGate(30, (uint32_t)exception30, 0x08, 0x8E);
48+
set_idtGate(31, (uint32_t)exception31, 0x08, 0x8E);
49+
50+
set_idtGate(32, (uint32_t)isr0, 0x08, 0x8E);
51+
set_idtGate(33, (uint32_t)isr1, 0x08, 0x8E);
52+
set_idtGate(34, (uint32_t)isr2, 0x08, 0x8E);
53+
set_idtGate(35, (uint32_t)isr3, 0x08, 0x8E);
54+
set_idtGate(36, (uint32_t)isr4, 0x08, 0x8E);
55+
set_idtGate(37, (uint32_t)isr5, 0x08, 0x8E);
56+
set_idtGate(38, (uint32_t)isr6, 0x08, 0x8E);
57+
set_idtGate(39, (uint32_t)isr7, 0x08, 0x8E);
58+
set_idtGate(40, (uint32_t)isr8, 0x08, 0x8E);
59+
set_idtGate(41, (uint32_t)isr9, 0x08, 0x8E);
60+
set_idtGate(42, (uint32_t)isr10, 0x08, 0x8E);
61+
set_idtGate(43, (uint32_t)isr11, 0x08, 0x8E);
62+
set_idtGate(44, (uint32_t)isr12, 0x08, 0x8E);
63+
set_idtGate(45, (uint32_t)isr13, 0x08, 0x8E);
64+
set_idtGate(46, (uint32_t)isr14, 0x08, 0x8E);
65+
set_idtGate(47, (uint32_t)isr15, 0x08, 0x8E);
66+
67+
set_idtGate(128, (uint32_t)exception128, 0x08, (0x8E | 0x60));
68+
69+
printf("[IDT] Flushing IDT entries...\n");
70+
flush_idt((uint32_t)&idt_ptr);
71+
printf("[IDT] Initialized successfully\n");
72+
}
73+
74+
void set_idtGate(uint8_t num, uint32_t base, uint16_t selector, uint8_t flags)
75+
{
76+
idt_entries[num].offset_low = base & 0xFFFF;
77+
idt_entries[num].offset_high = (base >> 16) & 0xFFFF;
78+
79+
idt_entries[num].selector = selector;
80+
idt_entries[num].always0 = 0;
81+
82+
idt_entries[num].flags = flags;
83+
}

src/drivers/cpu/idt_helper.asm

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[global flush_idt]
2+
3+
flush_idt:
4+
mov eax, [esp + 4]
5+
lidt [eax]
6+
ret

src/drivers/cpu/pic.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include "pic.h"
2+
3+
void init_pic()
4+
{
5+
printf("[PIC] Initializing...\n");
6+
outb(PIC1_COMMAND, 0x11);
7+
outb(PIC2_COMMAND, 0x11);
8+
outb(PIC1_DATA, 0x20);
9+
outb(PIC2_DATA, 0x28);
10+
outb(PIC1_DATA, 0x04);
11+
outb(PIC2_DATA, 0x02);
12+
outb(PIC1_DATA, 0x01);
13+
outb(PIC2_DATA, 0x01);
14+
outb(PIC1_DATA, 0x00);
15+
outb(PIC2_DATA, 0x00);
16+
printf("[PIC] Initialized...\n");
17+
}
18+
19+
void pic_eoi(uint8_t irq)
20+
{
21+
if(irq >= 0x28)
22+
{
23+
outb(PIC2_COMMAND, PIC_EOI);
24+
}
25+
outb(PIC1_COMMAND, PIC_EOI);
26+
}

0 commit comments

Comments
 (0)