Skip to content

Commit

Permalink
Added usermode support. Usermode code added in a seperate section nam…
Browse files Browse the repository at this point in the history
…ed "um" and copied into a page frame. The pagetables are built for usermode code and stack at 1GB. Interrupt, exception and system call handlers are using register frames now. Fixed GDT entry for based address. Adding support for kernel memory heap.
  • Loading branch information
shoily committed Jul 17, 2020
1 parent d2bdc4d commit 117016d
Show file tree
Hide file tree
Showing 15 changed files with 501 additions and 60 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ gcc -m32 -std=gnu99 -nostdlib -nostdinc -fno-builtin -fno-stack-protector -nosta
as --32 handlr32.S -o handlr32.o<br>
gcc -m32 -std=gnu99 -nostdlib -nostdinc -fno-builtin -fno-stack-protector -nostartfiles -nodefaultlibs -ffreestanding -fno-pic -Wall -Wextra -Werror -c setup32.c -o setup32.o<br>
gcc -m32 -std=gnu99 -nostdlib -nostdinc -fno-builtin -fno-stack-protector -nostartfiles -nodefaultlibs -ffreestanding -fno-pic -Wall -Wextra -Werror -c start.c -o start.o<br>
gcc -m32 -std=gnu99 -nostdlib -nostdinc -fno-builtin -fno-stack-protector -nostartfiles -nodefaultlibs -ffreestanding -fno-pic -Wall -Wextra -Werror -c memory.c -o memory.o<br>
gcc -m32 -std=gnu99 -nostdlib -nostdinc -fno-builtin -fno-stack-protector -nostartfiles -nodefaultlibs -ffreestanding -fno-pic -Wall -Wextra -Werror -c page32.c -o page32.o<br>
gcc -m32 -std=gnu99 -nostdlib -nostdinc -fno-builtin -fno-stack-protector -nostartfiles -nodefaultlibs -ffreestanding -fno-pic -Wall -Wextra -Werror -c usermode.c -o usermode.o<br>


ld -static -T kernel32.ld -m elf_i386 -nostdlib --nmagic boot32.o util.o system.o setup32.o handlr32.o start.o -o xiskernel.elf<br>
ld -static -T kernel32.ld -m elf_i386 -nostdlib --nmagic boot32.o util.o system.o setup32.o handlr32.o memory.o page32.o usermode.o start.o -o xiskernel.elf<br>
objcopy -O binary xiskernel.elf xiskernel.bin<br>

Instructions for building boot loader -<br>
Expand Down
79 changes: 72 additions & 7 deletions handlr32.S
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

.globl common_trap_handler

.globl system_call_handler_3
.globl sys_call_handler_128

.globl irq_handler_0
.globl irq_handler_1
Expand All @@ -38,9 +38,22 @@
.macro DEFINE_IRQ_HANDLER irqn

irq_handler_\irqn:

pushl $\irqn
pusha

push %ds
push %es
push %fs
push %gs

movl %esp, %ebp

movl $0x28, %eax
movl %eax, %ds
movl %eax, %es
movl %eax, %fs
movl %eax, %gs

movb $0x20,%al

movb $\irqn, %ah
Expand All @@ -52,12 +65,19 @@

outb %al,$0x20

push $\irqn
push %ebp
call common_interrupt_handler
addl $4, %esp

pop %gs
pop %fs
pop %es
pop %ds

popa

addl $4, %esp

iret

.endm
Expand All @@ -68,10 +88,35 @@

sys_call_handler_\syscallnr:
pushl $\syscallnr
pusha

push %ds
push %es
push %fs
push %gs

movl %esp, %ebp

movl $0x28, %eax
movl %eax, %ds
movl %eax, %es
movl %eax, %fs
movl %eax, %gs

push %ebp
call common_sys_call_handler
addl $4, %esp

pop %gs
pop %fs
pop %es
pop %ds

popa

addl $4, %esp

iret

.endm

Expand All @@ -95,14 +140,34 @@
DEFINE_IRQ_HANDLER 15

// define system call handlers
DEFINE_SYS_CALL_HANDLER 3
DEFINE_SYS_CALL_HANDLER 128


common_trap_handler:
pushl $0
pushl $0
pusha

push %ds
push %es
push %fs
push %gs

movl %esp, %ebp

movl $0x28, %eax
movl %eax, %ds
movl %eax, %es
movl %eax, %fs
movl %eax, %gs

push %ebp
call trap_handler
addl $4, %esp

pop %gs
pop %fs
pop %es
pop %ds

popa

addl $8,%esp
iret
6 changes: 5 additions & 1 deletion kernel32.ld
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ SECTIONS
.text : AT(ADDR(.text) - 0x80000000) {*(.text); }
.data : AT(ADDR(.data) - 0x80000000) {*(.data); }
.bss : AT(ADDR(.bss) - 0x80000000) {*(.bss); }
. = ALIGN(4096);
.um : AT(ADDR(.um) - 0x80000000) {*(.um);}
um_size = SIZEOF(um);
.kernel_data ALIGN(4096) : AT(ADDR(.kernel_data) - 0x80000000) {*(.kernel_data); }
. = ALIGN(4096);
_kernel_pg_dir = .;
_kernel_pg_table_0 = _kernel_pg_dir + 0x1000;
_kernel_stack_0 = _kernel_pg_table_0 + 0x1000;
_kernel_stack_0_start = _kernel_stack_0 + 0x2000;
_kernel_private_data = _kernel_stack_0_start;
_kernel_private_data = _kernel_stack_0_start + 8;
_kernel_heap_start = (_kernel_private_data + 0x1000) & ~(0xfff);
}
31 changes: 31 additions & 0 deletions memory.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*****************************************************************************/
/* File: memory.c */
/* */
/* Description: Source file for memory allocation code. */
/* */
/* Author: Shoily O Rahman <[email protected]> */
/* */
/* Date: July 9, 2020 */
/* */
/*****************************************************************************/

#include "memory.h"

extern int _kernel_heap_start;

char* heap_ptr;

void init_memory() {

heap_ptr = (char*)_kernel_heap_start;
}

void *alloc_mem(int size, int alignment) {

char *mem_start = (char*)heap_ptr;

mem_start = (char*)(((int)heap_ptr + (alignment-1)) & ~(alignment-1));
heap_ptr = mem_start + size;

return mem_start;
}
18 changes: 18 additions & 0 deletions memory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*****************************************************************************/
/* File: memory.h */
/* */
/* Description: Header file for memory allocation code. */
/* */
/* Author: Shoily O Rahman <[email protected]> */
/* */
/* Date: July 9, 2020 */
/* */
/*****************************************************************************/

#ifndef MEMORY_H
#define MEMORY_H

void init_memory();
void *alloc_mem(int size, int alignment);

#endif
47 changes: 47 additions & 0 deletions page32.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*****************************************************************************/
/* File: page32.c */
/* */
/* Description: Source file for 32 bit page table handling code. */
/* */
/* Author: Shoily O Rahman <[email protected]> */
/* */
/* Date: July 9, 2020 */
/* */
/*****************************************************************************/

#include "page32.h"
#include "system.h"

void build_pagetable(pgd_t *pgdir, pte_t **pgtable, int phys_addr, int start, int length, int pgd_flags, int pte_flags) {

int idx = 0;
pgd_t *pgd = pgdir + ((start >> 22) & 0x3ff);
pte_t *pte = pgtable[idx] + ((start >> 12) & 0x3ff);
pte_t *last_pte = (pte_t*)(((int)pte + PAGE_SIZE) & ~(PAGE_SIZE-1));
int end = start + length;

if (!*pgd) {
*pgd = ((int)pgtable[idx] - KERNEL_VIRT_ADDR) | pgd_flags;
}

while(start < end) {

if (pte == last_pte) {

idx++;
pte = pgtable[idx];
last_pte = pte + (PAGE_SIZE/sizeof(pte_t));
pgd++;

if (!*pgd) {
*pgd = (((int)pgtable[idx] - KERNEL_VIRT_ADDR) | pgd_flags);
}
}

*pte = (phys_addr | pte_flags);

pte++;
start += 0x1000;
phys_addr += 0x1000;
}
}
29 changes: 29 additions & 0 deletions page32.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* File: page32.h */
/* */
/* Description: Header file for 32 bit page table handling code. */
/* */
/* Author: Shoily O Rahman <[email protected]> */
/* */
/* Date: July 9, 2020 */
/* */
/*****************************************************************************/

#ifndef PAGE32_H
#define PAGE32_H

typedef int pte_t;
typedef int pgd_t;

#define PAGE_SIZE 4096

#define PGD_PRESENT 1
#define PGD_WRITE 2
#define PGD_USER 4

#define PTE_PRESENT 1
#define PTE_WRITE 2
#define PTE_USER 4

void build_pagetable(pgd_t *pgdir, pte_t **pgtable, int phys_addr, int start, int length, int pgd_flags, int pte_flags);

#endif
Loading

0 comments on commit 117016d

Please sign in to comment.