aboutsummaryrefslogtreecommitdiff
path: root/kernel
diff options
context:
space:
mode:
authorMarvin Borner2021-04-10 15:25:27 +0200
committerMarvin Borner2021-04-10 15:25:27 +0200
commitf96b8ad1ee83dec08ae636e179cc48019ca50b12 (patch)
tree80ff605e1305673291ce6230941db7ed4229856d /kernel
parent9655593d80e23d2ea3c091e3187e8e47b278bc3d (diff)
Some changes here and there
Diffstat (limited to 'kernel')
-rw-r--r--kernel/drivers/interrupts.c2
-rw-r--r--kernel/features/fs.c1
-rw-r--r--kernel/features/load.c28
-rw-r--r--kernel/features/mm.c13
-rw-r--r--kernel/features/proc.c5
-rw-r--r--kernel/features/syscall.c4
-rw-r--r--kernel/inc/mm.h7
-rw-r--r--kernel/inc/proc.h7
8 files changed, 36 insertions, 31 deletions
diff --git a/kernel/drivers/interrupts.c b/kernel/drivers/interrupts.c
index 9898ef1..8ca6987 100644
--- a/kernel/drivers/interrupts.c
+++ b/kernel/drivers/interrupts.c
@@ -181,6 +181,8 @@ void isr_panic(struct regs *r)
if (proc) {
printf("\t-> Exception occurred in %s at addr 0x%x (offset 0x%x)\n", proc->name,
r->eip, r->eip - proc->entry);
+ printf("\t\t-> Process: [entry: %x, kstack: %x, ustack: %x]\n", proc->entry,
+ proc->stack.kernel, proc->stack.user);
proc_exit(proc, r, 1);
} else {
__asm__ volatile("cli\nhlt");
diff --git a/kernel/features/fs.c b/kernel/features/fs.c
index 76c230e..82d00b6 100644
--- a/kernel/features/fs.c
+++ b/kernel/features/fs.c
@@ -40,6 +40,7 @@ u8 vfs_mounted(struct device *dev, const char *path)
return 0;
}
+// TODO: Reduce allocations in VFS find
static struct mount_info *vfs_recursive_find(char *path)
{
struct node *iterator = mount_points->head;
diff --git a/kernel/features/load.c b/kernel/features/load.c
index 02c02ae..77bb680 100644
--- a/kernel/features/load.c
+++ b/kernel/features/load.c
@@ -157,33 +157,21 @@ res elf_load(const char *name, struct proc *proc)
memory_backup_dir(&prev);
memory_switch_dir(proc->page_dir);
- stac();
-
// Allocate user stack with readonly lower and upper page boundary
- u32 user_stack =
- PAGE_SIZE + (u32)memory_alloc(proc->page_dir, PROC_STACK_SIZE + 2 * PAGE_SIZE,
- MEMORY_USER | MEMORY_CLEAR);
- virtual_remap_readonly(proc->page_dir, memory_range(user_stack - PAGE_SIZE, PAGE_SIZE));
- virtual_remap_readonly(proc->page_dir,
- memory_range(user_stack + PROC_STACK_SIZE, PAGE_SIZE));
+ u32 user_stack = (u32)memory_alloc_with_boundary(proc->page_dir, PROC_STACK_SIZE,
+ MEMORY_CLEAR | MEMORY_USER);
// Allocate kernel stack with readonly lower and upper page boundary
u32 kernel_stack =
- PAGE_SIZE +
- (u32)memory_alloc(proc->page_dir, PROC_STACK_SIZE + 2 * PAGE_SIZE, MEMORY_CLEAR);
- virtual_remap_readonly(proc->page_dir, memory_range(kernel_stack - PAGE_SIZE, PAGE_SIZE));
- virtual_remap_readonly(proc->page_dir,
- memory_range(kernel_stack + PROC_STACK_SIZE, PAGE_SIZE));
-
- proc->user_stack = user_stack + PROC_STACK_SIZE;
- proc->kernel_stack = kernel_stack + PROC_STACK_SIZE;
- proc->regs.ebp = proc->user_stack;
- proc->regs.useresp = proc->user_stack;
+ (u32)memory_alloc_with_boundary(proc->page_dir, PROC_STACK_SIZE, MEMORY_CLEAR);
+
+ proc->stack.user = user_stack + PROC_STACK_SIZE;
+ proc->stack.kernel = kernel_stack + PROC_STACK_SIZE;
+ proc->regs.ebp = proc->stack.user;
+ proc->regs.useresp = proc->stack.user;
proc->regs.eip = header.entry + rand_off;
proc->entry = header.entry + rand_off;
- clac();
-
memory_switch_dir(prev);
return EOK;
}
diff --git a/kernel/features/mm.c b/kernel/features/mm.c
index 0972ab3..9a7a64e 100644
--- a/kernel/features/mm.c
+++ b/kernel/features/mm.c
@@ -82,14 +82,19 @@ void page_fault_handler(struct regs *r)
u32 paddr = virtual_to_physical(dir, vaddr);
// Print!
+
printf("%s process tried to %s a %s page at [vaddr=%x; paddr=%x]\n", super, operation, type,
vaddr, paddr);
+
if (proc && vaddr > proc->regs.ebp - PROC_STACK_SIZE - PAGE_SIZE &&
vaddr < proc->regs.ebp + PAGE_SIZE)
print("Probably a stack overflow\n");
+
printf("Sections: [vaddr_section=%s; paddr_section=%s; eip_section=%s]\n",
page_fault_section(vaddr), page_fault_section(paddr), page_fault_section(r->eip));
+ /* printf("%b\n", virtual_entry(dir, vaddr)->uint); */
+
isr_panic(r);
}
@@ -421,6 +426,14 @@ err:
return NULL;
}
+void *memory_alloc_with_boundary(struct page_dir *dir, u32 size, u32 flags)
+{
+ u32 mem = PAGE_SIZE + (u32)memory_alloc(dir, size + 2 * PAGE_SIZE, flags);
+ virtual_remap_readonly(dir, memory_range(mem - PAGE_SIZE, PAGE_SIZE));
+ virtual_remap_readonly(dir, memory_range(mem + size, PAGE_SIZE));
+ return (void *)mem;
+}
+
void *memory_alloc_identity(struct page_dir *dir, u32 flags)
{
for (u32 i = 1; i < PAGE_KERNEL_COUNT * PAGE_COUNT; i++) {
diff --git a/kernel/features/proc.c b/kernel/features/proc.c
index d5bf2e6..3a6b429 100644
--- a/kernel/features/proc.c
+++ b/kernel/features/proc.c
@@ -54,7 +54,7 @@ HOT FLATTEN void scheduler(struct regs *regs)
current = idle_proc;
}
- tss_set_stack(0x10, PROC(current)->kernel_stack);
+ tss_set_stack(GDT_SUPER_DATA_OFFSET, PROC(current)->stack.kernel);
memory_switch_dir(PROC(current)->page_dir);
memcpy(regs, &PROC(current)->regs, sizeof(*regs));
@@ -223,6 +223,7 @@ end:
proc_state(p, PROC_BLOCKED);
}
+// TODO: Rewrite block/unblock mechanisms
void proc_unblock(u32 id, enum proc_block_type type)
{
struct page_dir *dir_bak;
@@ -582,7 +583,7 @@ NORETURN void proc_init(void)
// We'll shortly jump to usermode. Clear and protect every secret!
memory_user_hook();
- tss_set_stack(0x10, init->kernel_stack);
+ tss_set_stack(GDT_SUPER_DATA_OFFSET, init->stack.kernel);
memory_switch_dir(init->page_dir);
printf("Jumping to userspace!\n");
diff --git a/kernel/features/syscall.c b/kernel/features/syscall.c
index 98a7ff9..65777d8 100644
--- a/kernel/features/syscall.c
+++ b/kernel/features/syscall.c
@@ -23,10 +23,6 @@ static void syscall_handler(struct regs *r)
/* printf("[SYSCALL] %d from %s\n", num, proc_current()->name); */
switch (num) {
- case SYS_LOOP: {
- panic("Loop is deprecated!\n");
- break;
- }
case SYS_ALLOC: {
r->eax = memory_sys_alloc(proc_current()->page_dir, r->ebx, (u32 *)r->ecx,
(u32 *)r->edx, (u8)r->esi);
diff --git a/kernel/inc/mm.h b/kernel/inc/mm.h
index a9524ad..0f1b4ec 100644
--- a/kernel/inc/mm.h
+++ b/kernel/inc/mm.h
@@ -32,9 +32,9 @@ void physical_free(struct memory_range range);
* Virtual
*/
-#define PAGE_SIZE 0x1000
-#define PAGE_COUNT 1024
-#define PAGE_KERNEL_COUNT 256
+#define PAGE_SIZE 0x1000u
+#define PAGE_COUNT 1024u
+#define PAGE_KERNEL_COUNT 256u
#define PAGE_ALIGN(x) ((x) + PAGE_SIZE - ((x) % PAGE_SIZE))
#define PAGE_ALIGNED(x) ((x) % PAGE_SIZE == 0)
#define PAGE_ALIGN_UP(x) (((x) % PAGE_SIZE == 0) ? (x) : (x) + PAGE_SIZE - ((x) % PAGE_SIZE))
@@ -122,6 +122,7 @@ struct memory_range memory_range_from(u32 base, u32 size);
struct memory_range memory_range_around(u32 base, u32 size);
void *memory_alloc(struct page_dir *dir, u32 size, u32 flags) NONNULL;
+void *memory_alloc_with_boundary(struct page_dir *dir, u32 size, u32 flags) NONNULL;
void *memory_alloc_identity(struct page_dir *dir, u32 flags) NONNULL;
void memory_free(struct page_dir *dir, struct memory_range vrange) NONNULL;
void memory_map_identity(struct page_dir *dir, struct memory_range prange, u32 flags) NONNULL;
diff --git a/kernel/inc/proc.h b/kernel/inc/proc.h
index f5dbf1a..b8912a5 100644
--- a/kernel/inc/proc.h
+++ b/kernel/inc/proc.h
@@ -53,8 +53,6 @@ struct stream {
struct proc {
u32 pid;
u32 entry;
- u32 user_stack;
- u32 kernel_stack;
char name[64];
char dir[64];
@@ -70,6 +68,11 @@ struct proc {
struct {
u32 user;
u32 kernel;
+ } stack;
+
+ struct {
+ u32 user;
+ u32 kernel;
} ticks;
struct {