Skip to content

Commit a62d45e

Browse files
syscalls + userland process with its own page directory pointer table - working protection
1 parent da3d6e8 commit a62d45e

15 files changed

Lines changed: 135 additions & 31 deletions

File tree

iso/kernel/kernel.sys

168 Bytes
Binary file not shown.

os.iso

0 Bytes
Binary file not shown.

src/Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ LDFLAGS:=-T linker.ld -o kernel.sys -m elf_i386
55
ASM:=nasm
66
ASMFLAGS:=-f elf
77

8-
all: kernel.o boot.o
9-
$(LD) $(LDFLAGS) boot.o kernel.o screen.o keyboard.o asmio.o asmisr.o gdt.o idt.o descriptors.o isr.o string.o memoryasm.o memory.o heap.o task.o syscall.o
8+
all: kernel.o boot.o testuserapp.o
9+
$(LD) $(LDFLAGS) boot.o kernel.o screen.o keyboard.o asmio.o asmisr.o gdt.o idt.o descriptors.o isr.o string.o memoryasm.o memory.o heap.o task.o syscall.o testuserapp.o
1010

1111
kernel.o: screen.o keyboard.o asmio.o descriptors.o memory.o heap.o
1212
$(CC) $(CFLAGS) kernel/kernel.c
@@ -59,6 +59,9 @@ task.o: syscall.o
5959
syscall.o:
6060
$(CC) $(CFLAGS) kernel/syscall.c
6161

62+
testuserapp.o:
63+
$(ASM) $(ASMFLAGS) -o testuserapp.o asm/testuserapp.asm
64+
6265
clean:
6366
$(shell rm -rf *.o kernel.sys)
6467
$(shell rm ../os.iso)

src/asm/asmio.asm

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ global in_byte
22
global out_byte
33
global in_word
44
global out_word
5+
global io_wait
56
global asmcli
67
global asmsti
78

@@ -44,6 +45,16 @@ out_word:
4445
pop ebp
4546
ret
4647

48+
io_wait:
49+
push ebp
50+
mov ebp, esp
51+
push eax
52+
xor al, al
53+
out 0x00, al
54+
pop eax
55+
pop ebp
56+
ret
57+
4758
asmcli:
4859
cli
4960
ret

src/asm/asmio.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ extern unsigned char in_byte ( unsigned short port );
55
extern void out_byte ( unsigned short port , unsigned char data );
66
extern unsigned short in_word ( unsigned short port );
77
extern void out_word ( unsigned short port , unsigned short data );
8+
extern void io_wait(void);
89
extern void asmcli(void);
910
extern void asmsti(void);
1011

src/asm/asmisr.asm

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ global temp
44
%macro ISR_NOERRCODE 1
55
global isr%1
66
isr%1:
7-
cli ; disable hardware interrupts
7+
; cli ; disable hardware interrupts
88
push byte 0 ; push 0 as err code
99
push %1 ; push int num
1010
jmp isr_common_stub
@@ -14,7 +14,7 @@ global temp
1414
%macro ISR_ERRCODE 1
1515
global isr%1
1616
isr%1:
17-
cli ; disable hardware interrupts
17+
; cli ; disable hardware interrupts
1818
push %1 ; push int num, int errcode was already pushed by the cpu
1919
jmp isr_common_stub
2020
%endmacro
@@ -311,5 +311,5 @@ isr_common_stub:
311311
popa ; restore registers
312312

313313
add esp, 8 ; pop int num and int err code from the stack
314-
sti ; enable hardware interrupts
314+
; sti ; enable hardware interrupts
315315
iret ; interrupt return

src/asm/memory.asm

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
global loadPageDirectoryAsm
22
global enablePagingAsm
33
global enablePaePagingAsm
4+
global disablePagingAsm
45
loadPageDirectoryAsm:
56
push ebp
67
mov ebp, esp
@@ -18,6 +19,15 @@ enablePagingAsm:
1819
pop ebp
1920
ret
2021

22+
disablePagingAsm:
23+
push ebp
24+
mov ebp, esp
25+
mov eax, cr0
26+
and eax, 0xF7FFFFFFF
27+
mov cr0, eax
28+
pop ebp
29+
ret
30+
2131
enablePaePagingAsm:
2232
push ebp
2333
mov ebp, esp

src/asm/testuserapp.asm

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
section .usrtext
2+
global usermain
3+
usermain:
4+
mov eax, msg
5+
int 0x83
6+
int 0x82
7+
ret
8+
msg db 'Hello from userland!', 0xA, 0

src/kernel/descriptors.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,9 @@ void idt_setup() {
182182
idt_set_gate(126,(unsigned int)isr126,0x08,0x8e);
183183
idt_set_gate(127,(unsigned int)isr127,0x08,0x8e);
184184
idt_set_gate(128,(unsigned int)isr128,0x08,0x8e);
185-
idt_set_gate(129,(unsigned int)isr129,0x08,0x8e);
185+
idt_set_gate(129,(unsigned int)isr129,0x08,0x8e);
186186
idt_set_gate(130,(unsigned int)isr130,0x08,0xef); // Context switch
187-
idt_set_gate(131,(unsigned int)isr131,0x08,0x8e);
187+
idt_set_gate(131,(unsigned int)isr131,0x08,0xef); // Puts
188188
idt_set_gate(132,(unsigned int)isr132,0x08,0x8e);
189189
idt_set_gate(133,(unsigned int)isr133,0x08,0x8e);
190190
idt_set_gate(134,(unsigned int)isr134,0x08,0x8e);
@@ -341,5 +341,5 @@ void write_tss(unsigned int num, unsigned short ss0, unsigned int esp0)
341341
// Sets the stack to use on traps
342342
void set_kernel_stack(unsigned int stack)
343343
{
344-
sys_tss.esp0 = stack;
344+
sys_tss.esp0 = stack;
345345
}

src/kernel/memory.c

Lines changed: 73 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#include "memory.h"
22
#include "multiboot.h"
3+
#include "heap.h"
34
#include "../drivers/screen.h"
5+
#include "../asm/asmio.h"
6+
#include "../libs/string.h"
7+
extern void usermain();
48

59
page_directory_pointer_table_entry_t page_dir_ptr_tab[4] __attribute__((aligned(0x20)));
610
page_directory_table_entry_t page_dir[512] __attribute__((aligned(0x1000)));
@@ -12,28 +16,37 @@ unsigned int pre_frame_map[20];
1216

1317
void init_memory(multiboot_info_t * mymbd) {
1418
mbd = mymbd;
15-
startframe = 0x300000;
19+
startframe = 0x400000;
1620
// Map a 2 mb single page using an entry in the page directory table (0-0x200000)
1721
page_dir_ptr_tab[0].present = 1;
1822
page_dir_ptr_tab[0].page_directory_table_address = (unsigned int)&page_dir>>12;
1923
page_dir[0].present = 1;
2024
page_dir[0].ro_rw = 1;
2125
page_dir[0].size = 1;
22-
page_dir[0].page_table_address =0;
26+
page_dir[0].page_table_address = 0;
2327
page_dir[0].kernel_user = 1;
24-
// Map 512 4kb pages (0x200000-0x400000)
25-
page_dir[1].present = 1;
26-
page_dir[1].ro_rw = 1;
27-
page_dir[1].page_table_address = (unsigned int)&page_tab>>12;
28-
unsigned int i, address = 0;
29-
for(i = 0; i < 512; i++)
30-
{
31-
page_tab[i].present = 1;
32-
page_tab[i].ro_rw = 1;
33-
page_tab[i].physical_page_address = address>>12;
34-
page_tab[i].kernel_user = 1;
35-
address = address + 0x1000;
36-
}
28+
// page_dir[1].present = 1;
29+
// page_dir[1].ro_rw = 1;
30+
// page_dir[1].size = 1;
31+
// page_dir[1].page_table_address = 0x200000>>12;
32+
// page_dir[1].kernel_user = 1;
33+
////////////////////////////////////////////////////////////////////////////
34+
// An unsuccessful attempt at making a Higher Half Kernel
35+
// unsigned int i, address = 0xB0000000;
36+
// puts("\n");
37+
// for(i = 388; i < 475; i++){
38+
// screen_print_int(address>>12, 16);
39+
// puts(" ");
40+
// page_dir[i].present = 1;
41+
// page_dir[i].ro_rw = 1;
42+
// page_dir[i].size = 1;
43+
// page_dir[i].page_table_address = (unsigned int)address>>12;
44+
// screen_print_int(page_dir[i].page_table_address, 16);
45+
// puts("\n");
46+
// getc();
47+
// page_dir[i].kernel_user = 0;
48+
// address = (unsigned int)((unsigned int)address + (unsigned int)0x200000);
49+
// }
3750
puts("Enabaling PAE paging...\n");
3851
enablePaePagingAsm();
3952
loadPageDirectoryAsm((unsigned int *)&page_dir_ptr_tab);
@@ -113,4 +126,49 @@ void kfree_frame(unsigned int page_frame_addr)
113126
page_frame_addr = (unsigned int)(page_frame_addr - startframe);
114127
// Divide by 4kb to get the index of the page frame in frame_map
115128
frame_map[((unsigned int)page_frame_addr)/0x1000] = 0;
129+
}
130+
131+
// Create a page directory pointer table for a userland process
132+
unsigned int create_pdpt() {
133+
unsigned int task_pdpt, task_dt, task_tab;
134+
// Get a page frame for each pdpt, dt, pt because their address
135+
// must be 0x1000 (4096) byte aligned.
136+
task_pdpt = kalloc_frame();
137+
task_dt = kalloc_frame();
138+
task_tab = kalloc_frame();
139+
// We are entering a critical section
140+
asmcli();
141+
// Temporarily disable paging so we can write to the physical
142+
// addresses of paging tables without problems
143+
disablePagingAsm();
144+
// Create the pointers
145+
page_directory_pointer_table_entry_t * temp_pdpt;
146+
temp_pdpt = (page_directory_pointer_table_entry_t *)task_pdpt;
147+
page_directory_table_entry_t * temp_dt;
148+
temp_dt = (page_directory_table_entry_t *)task_dt;
149+
page_table_entry_t * temp_tab;
150+
temp_tab = (page_table_entry_t *)task_tab;
151+
temp_pdpt[0].page_directory_table_address = (unsigned int)temp_dt>>12;
152+
temp_pdpt[0].present = 1;
153+
// Map the kernel space
154+
temp_dt[0].present = 1;
155+
temp_dt[0].ro_rw = 1;
156+
temp_dt[0].size = 1;
157+
temp_dt[0].page_table_address = 0;
158+
temp_dt[0].kernel_user = 0;
159+
// Map user space (0x300000-0x301000)
160+
temp_dt[1].present = 1;
161+
temp_dt[1].page_table_address = (unsigned int)temp_tab>>12;
162+
temp_dt[1].kernel_user = 1;
163+
temp_tab[256].present = 1;
164+
temp_tab[256].ro_rw = 1;
165+
temp_tab[256].physical_page_address = 0x300000>>12;
166+
temp_tab[256].kernel_user = 1;
167+
// Re-enable interrupts and paging
168+
/* Temporary loading of the user task to the appropriate location
169+
* in memory. will be removed. */
170+
memcpy(0x300000, &usermain, 0x1000); // temp
171+
enablePagingAsm();
172+
asmsti();
173+
return task_pdpt;
116174
}

0 commit comments

Comments
 (0)