Skip to content

Commit f5159c3

Browse files
committed
* Implemented a function for switching to usermode
* Implemented task state segment * Added new functions to gdt.c and idt.c to reinitialize the gdt and idt * Fixed a bug in gdt.c * Added new functions in bit.c * Updated math.c * Updated kernel.c * Incremented kernel version * Updated Makefile * Updated README.md
1 parent 40e3beb commit f5159c3

File tree

18 files changed

+267
-11
lines changed

18 files changed

+267
-11
lines changed

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ OBJECTS= $(SRCDIR)/boot/multiboot.o \
4646
$(SRCDIR)/drivers/cpu/gdt_helper.o \
4747
$(SRCDIR)/drivers/cpu/idt.o \
4848
$(SRCDIR)/drivers/cpu/idt_helper.o \
49+
$(SRCDIR)/drivers/cpu/tss.o \
50+
$(SRCDIR)/drivers/cpu/tss_helper.o \
4951
$(SRCDIR)/drivers/cpu/pic.o \
5052
$(SRCDIR)/drivers/cpu/pit.o \
5153
$(SRCDIR)/drivers/cpu/rdtsc.o \
@@ -76,6 +78,7 @@ OBJECTS= $(SRCDIR)/boot/multiboot.o \
7678
$(SRCDIR)/memory/kheap.o \
7779
$(SRCDIR)/memory/paging.o \
7880
$(SRCDIR)/memory/pmm.o \
81+
$(SRCDIR)/process/usermode.o \
7982
$(SRCDIR)/kernel/shell.o \
8083
$(SRCDIR)/kernel/kernel.o
8184

@@ -134,6 +137,9 @@ run: $(ISOFILE)
134137
rund: $(ISOFILE)
135138
$(QEMU) $(QEMUFLAGS) $(QEMUDFLAGS)
136139

140+
runkvmd: $(ISOFILE)
141+
$(QEMU) $(QEMUFLAGS) $(QEMUDFLAGS) -enable-kvm -cpu host
142+
137143
stripd: $(EXECUTABLE)
138144
@$(TOOLDIR)$(OBJCOPY) --only-keep-debug $(EXECUTABLE) debug.sym
139145
@$(TOOLDIR)$(OBJCOPY) $(OBJCOPYFLAGS) $(EXECUTABLE)

docs/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Kernel Version
1414

1515

1616
## 🧱 Kernel
17-
The current kernel version for SectorOS-RW4 is v4.23.04.2NR.<br>
17+
The current kernel version for SectorOS-RW4 is v4.23.04.3NR.<br>
1818

1919
## ⚙️ Build Steps
2020

docs/changelog.txt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,16 @@
122122
* Implemented zalloc to allocate memory initialized with 0
123123
* Updated Makefile
124124
* Updated kernelmain.c
125-
* Incremented kernel version
125+
* Incremented kernel version
126+
127+
09-04-2023: 05:06 PM IST v4.23.04.3NR
128+
* Implemented a function for switching to usermode
129+
* Implemented task state segment
130+
* Added new functions to gdt.c and idt.c to reinitialize the gdt and idt
131+
* Fixed a bug in gdt.c
132+
* Added new functions in bit.c
133+
* Updated math.c
134+
* Updated kernel.c
135+
* Incremented kernel version
136+
* Updated Makefile
137+
* Updated README.md

src/common/libs/bit.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,19 @@ uint8_t lobyte(uint16_t data)
88
uint8_t hibyte(uint16_t data)
99
{
1010
return ((data >> 8) & 0xFF);
11+
}
12+
13+
void set_hibyte(uint16_t* data, uint8_t num)
14+
{
15+
data = (num << 8);
16+
}
17+
18+
void set_lobyte(uint16_t* data, uint8_t num)
19+
{
20+
data = num;
21+
}
22+
23+
int get_bit(uint32_t data, int num)
24+
{
25+
return (data >> num) & 1;
1126
}

src/common/libs/math.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ void init_rand()
5050
{
5151
printf("[RNG] No rdrand support, using PRNG.\n");
5252
seed(ticks);
53-
for(int i = 0; i < ticks; i++)
53+
uint32_t j = rand_range(0, 256);
54+
for(int i = 0; i < j; i++)
5455
rand();
5556
}
5657
}

src/drivers/cpu/gdt.c

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
11
#include "gdt.h"
2+
#include "bit.h"
3+
#include "isr.h"
24

35
gdt_entry_t entries[GDT_MAX_ENTRIES];
46
gdt_ptr_t ptr;
57

8+
void gdt_handler(registers_t* registers)
9+
{
10+
uint32_t ecode = registers->ecode;
11+
int External = get_bit(ecode, 0);
12+
int table = get_bit(ecode, 1);
13+
int index = (ecode >> 2) & 13;
14+
15+
printf("This fault was happened %s inside %s entry #%d.\n", External == 0 ? "internally" : "externally", (table == 0b00 ? "GDT" : (table == 0b01) ? "IDT" : (table == 0b10) ? "LDT" : "Unknown"), index);
16+
kernel_panic("GPF");
17+
}
18+
619
void init_gdt()
720
{
821
printf("[GDT] Configuring GDT pointer...\n");
9-
ptr.limit = (sizeof(gdt_entry_t) * GDT_MAX_ENTRIES) - 1;
10-
ptr.base = (uint32_t)&entries;
22+
ptr.limit = (sizeof(entries)) - 1;
23+
ptr.base = (uint32_t)entries;
1124

1225
printf("[GDT] Adding entries...\n");
1326

@@ -21,9 +34,25 @@ void init_gdt()
2134

2235
flush_gdt((uint32_t)&ptr);
2336

37+
register_interrupt_handler(15, gdt_handler);
38+
2439
printf("[GDT] Initialized successfully\n");
2540
}
2641

42+
void gdt_reinit()
43+
{
44+
ptr.limit = (sizeof(gdt_entry_t) * GDT_MAX_ENTRIES) - 1;
45+
ptr.base = (uint32_t)&entries;
46+
47+
gdt_setGate(0, 0, 0, 0, 0);
48+
gdt_setGate(1, 0, 0xFFFFFFFF, 0x9A, 0xCF);
49+
gdt_setGate(2, 0, 0xFFFFFFFF, 0x92, 0xCF);
50+
gdt_setGate(3, 0, 0xFFFFFFFF, 0xFA, 0xCF);
51+
gdt_setGate(4, 0, 0xFFFFFFFF, 0xF2, 0xCF);
52+
53+
flush_gdt((uint32_t)&ptr);
54+
}
55+
2756
void gdt_setGate(int32_t index, uint32_t base, uint32_t limit, uint8_t access, uint8_t gran)
2857
{
2958
entries[index].base_low = (base & 0xFFFF);

src/drivers/cpu/idt.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,68 @@ void init_idt()
7171
printf("[IDT] Initialized successfully\n");
7272
}
7373

74+
void idt_reinit()
75+
{
76+
idt_ptr.limit = sizeof(idt_entry_t) * 256 - 1;
77+
idt_ptr.base = (uint32_t)&idt_entries;
78+
79+
memset(&idt_entries, 0, sizeof(idt_entry_t) * 256);
80+
81+
set_idtGate(0, (uint32_t)exception0, 0x08, 0x8E);
82+
set_idtGate(1, (uint32_t)exception1, 0x08, 0x8E);
83+
set_idtGate(2, (uint32_t)exception2, 0x08, 0x8E);
84+
set_idtGate(3, (uint32_t)exception3, 0x08, 0x8E);
85+
set_idtGate(4, (uint32_t)exception4, 0x08, 0x8E);
86+
set_idtGate(5, (uint32_t)exception5, 0x08, 0x8E);
87+
set_idtGate(6, (uint32_t)exception6, 0x08, 0x8E);
88+
set_idtGate(7, (uint32_t)exception7, 0x08, 0x8E);
89+
set_idtGate(8, (uint32_t)exception8, 0x08, 0x8E);
90+
set_idtGate(9, (uint32_t)exception9, 0x08, 0x8E);
91+
set_idtGate(10, (uint32_t)exception10, 0x08, 0x8E);
92+
set_idtGate(11, (uint32_t)exception11, 0x08, 0x8E);
93+
set_idtGate(12, (uint32_t)exception12, 0x08, 0x8E);
94+
set_idtGate(13, (uint32_t)exception13, 0x08, 0x8E);
95+
set_idtGate(14, (uint32_t)exception14, 0x08, 0x8E);
96+
set_idtGate(15, (uint32_t)exception15, 0x08, 0x8E);
97+
set_idtGate(16, (uint32_t)exception16, 0x08, 0x8E);
98+
set_idtGate(17, (uint32_t)exception17, 0x08, 0x8E);
99+
set_idtGate(18, (uint32_t)exception18, 0x08, 0x8E);
100+
set_idtGate(19, (uint32_t)exception19, 0x08, 0x8E);
101+
set_idtGate(20, (uint32_t)exception20, 0x08, 0x8E);
102+
set_idtGate(21, (uint32_t)exception21, 0x08, 0x8E);
103+
set_idtGate(22, (uint32_t)exception22, 0x08, 0x8E);
104+
set_idtGate(23, (uint32_t)exception23, 0x08, 0x8E);
105+
set_idtGate(24, (uint32_t)exception24, 0x08, 0x8E);
106+
set_idtGate(25, (uint32_t)exception25, 0x08, 0x8E);
107+
set_idtGate(26, (uint32_t)exception26, 0x08, 0x8E);
108+
set_idtGate(27, (uint32_t)exception27, 0x08, 0x8E);
109+
set_idtGate(28, (uint32_t)exception28, 0x08, 0x8E);
110+
set_idtGate(29, (uint32_t)exception29, 0x08, 0x8E);
111+
set_idtGate(30, (uint32_t)exception30, 0x08, 0x8E);
112+
set_idtGate(31, (uint32_t)exception31, 0x08, 0x8E);
113+
114+
set_idtGate(32, (uint32_t)isr0, 0x08, 0x8E);
115+
set_idtGate(33, (uint32_t)isr1, 0x08, 0x8E);
116+
set_idtGate(34, (uint32_t)isr2, 0x08, 0x8E);
117+
set_idtGate(35, (uint32_t)isr3, 0x08, 0x8E);
118+
set_idtGate(36, (uint32_t)isr4, 0x08, 0x8E);
119+
set_idtGate(37, (uint32_t)isr5, 0x08, 0x8E);
120+
set_idtGate(38, (uint32_t)isr6, 0x08, 0x8E);
121+
set_idtGate(39, (uint32_t)isr7, 0x08, 0x8E);
122+
set_idtGate(40, (uint32_t)isr8, 0x08, 0x8E);
123+
set_idtGate(41, (uint32_t)isr9, 0x08, 0x8E);
124+
set_idtGate(42, (uint32_t)isr10, 0x08, 0x8E);
125+
set_idtGate(43, (uint32_t)isr11, 0x08, 0x8E);
126+
set_idtGate(44, (uint32_t)isr12, 0x08, 0x8E);
127+
set_idtGate(45, (uint32_t)isr13, 0x08, 0x8E);
128+
set_idtGate(46, (uint32_t)isr14, 0x08, 0x8E);
129+
set_idtGate(47, (uint32_t)isr15, 0x08, 0x8E);
130+
131+
set_idtGate(128, (uint32_t)exception128, 0x08, (0x8E | 0x60));
132+
133+
flush_idt((uint32_t)&idt_ptr);
134+
}
135+
74136
void set_idtGate(uint8_t num, uint32_t base, uint16_t selector, uint8_t flags)
75137
{
76138
idt_entries[num].offset_low = base & 0xFFFF;

src/drivers/cpu/tss.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include "tss.h"
2+
3+
static tss_entry_t kernel_tss;
4+
5+
void init_tss(uint32_t idx, uint32_t kss, uint32_t kesp)
6+
{
7+
serialprintf("[TSS] Adding TSS pointer to GDT #%d...\n", idx);
8+
uint32_t base = (uint32_t)&kernel_tss;
9+
uint32_t limit = base + sizeof(tss_entry_t);
10+
gdt_setGate(idx, base, limit, 0xE9, 0);
11+
12+
serialprintf("[TSS] Updating kernel tss entries...\n");
13+
memset(&kernel_tss, 0, sizeof(tss_entry_t));
14+
kernel_tss.ss0 = kss;
15+
kernel_tss.esp0 = kesp;
16+
17+
kernel_tss.cs = 0x0B;
18+
kernel_tss.ss = 0x13;
19+
kernel_tss.ds = 0x13;
20+
kernel_tss.es = 0x13;
21+
kernel_tss.fs = 0x13;
22+
kernel_tss.gs = 0x13;
23+
24+
uint32_t cr3;
25+
asm volatile("mov %%cr3, %0" : "=r" (cr3));
26+
kernel_tss.cr3 = cr3;
27+
28+
serialprintf("[TSS] Flushing TSS...\n");
29+
30+
flush_tss();
31+
32+
serialprintf("[TSS] Initialization completed...\n");
33+
}
34+
35+
void tss_set_kernel_stack(uint32_t kss, uint32_t kesp)
36+
{
37+
kernel_tss.ss0 = kss;
38+
kernel_tss.esp0 = kesp;
39+
}

src/drivers/cpu/tss_helper.s

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.global flush_tss
2+
3+
flush_tss:
4+
mov $0x2B, %ax
5+
ltr %ax
6+
ret

src/include/bit.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,9 @@
66
uint8_t lobyte(uint16_t data);
77
uint8_t hibyte(uint16_t data);
88

9+
void set_lobyte(uint16_t* data, uint8_t num);
10+
void set_hibyte(uint16_t* data, uint8_t num);
11+
12+
int get_bit(uint32_t data, int num);
13+
914
#endif /*__BIT_H__*/

0 commit comments

Comments
 (0)