aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarvin Borner2020-04-25 23:53:19 +0200
committerMarvin Borner2020-04-25 23:53:19 +0200
commit69d08dcb4580f8e544464bf54432ab07a7f58fc6 (patch)
tree878d35b5d3e3fac8f1c8e2a043519658d440f350
parentf2175ab40a977d15526e7ebc3ad327e3ef825069 (diff)
Small cleanup and formatting
-rwxr-xr-xrun8
-rw-r--r--src/kernel/boot.asm115
-rw-r--r--src/kernel/kernel.c10
-rw-r--r--src/kernel/linker.ld20
-rw-r--r--src/kernel/tasks/process.asm48
-rw-r--r--src/kernel/tasks/task.c177
-rw-r--r--src/kernel/tasks/task.h30
-rw-r--r--src/kernel/timer/timer.c1
8 files changed, 62 insertions, 347 deletions
diff --git a/run b/run
index c0efa1b..b181689 100755
--- a/run
+++ b/run
@@ -105,6 +105,14 @@ make_build() {
cp ./src/bootloader/grub.cfg ./iso/boot/grub/
grub-mkrescue -o ./build/melvix.iso ./iso/
+ # Check ISO
+ if grub-file --is-x86-multiboot2 ./build/kernel.bin; then
+ echo "Multiboot confirmed"
+ else
+ echo "Kernel is corrupted!"
+ exit 1
+ fi
+
# Create disk image
make_genext2fs
mkdir ./mnt/
diff --git a/src/kernel/boot.asm b/src/kernel/boot.asm
index d15bce4..363f76a 100644
--- a/src/kernel/boot.asm
+++ b/src/kernel/boot.asm
@@ -1,85 +1,52 @@
section .multiboot
- header_start:
- dd 0xe85250d6
- dd 0
- dd header_end - header_start
- dd 0x100000000 - (0xe85250d6 + 0 + (header_end - header_start))
-
- ; Information tag
- align 8
- dw 1
- dw 1
- dd 24
- dd 2 ; bootloader name
- dd 4 ; meminfo
- dd 6 ; mmap
- dd 13 ; smbios
-
- ; Empty tag
- align 8
- dw 0
- dw 0
- dd 8
- header_end:
+ header_start:
+ dd 0xe85250d6
+ dd 0
+ dd header_end - header_start
+ dd 0x100000000 - (0xe85250d6 + 0 + (header_end - header_start))
+
+ ; Information tag
+ align 8
+ dw 1
+ dw 1
+ dd 24
+ dd 2 ; bootloader name
+ dd 4 ; meminfo
+ dd 6 ; mmap
+ dd 13 ; smbios
+
+ ; Empty tag
+ align 8
+ dw 0
+ dw 0
+ dd 8
+ header_end:
section .start_section
- dd _start
+ dd _start
; Initialize stack
section .bss
- align 16
- global STACK_BOTTOM
- global STACK_TOP
+ align 16
+ global STACK_BOTTOM
+ global STACK_TOP
- STACK_BOTTOM:
- resb 0x4000
- STACK_TOP:
+ STACK_BOTTOM:
+ resb 0x4000
+ STACK_TOP:
section .text
- global _start
- extern kernel_main
- _start:
- mov esp, STACK_TOP
- push ebx
- push eax
- cli
- call kernel_main
- cli
- jmp $
-
- global jump_userspace
- jump_userspace:
- cli
- push ebp
- mov ebp, esp
- mov edx, dword [esp + 0xC]
- mov esp, edx
- push 0xABCDEF
-
- mov ax, 0x23
- mov ds, ax
- mov es, ax
- mov fs, ax
- mov gs, ax
-
- mov eax, esp
- push 0x23
- push eax
- pushf
-
- ; Enable interrupts
- pop eax
- or eax, 0x200
- push eax
-
- push 0x1B
- push dword [ebp + 8]
- iret
-
- pop ebp
- ret
+ global _start
+ extern kernel_main
+ _start:
+ mov esp, STACK_TOP
+ push ebx
+ push eax
+ cli
+ call kernel_main
+ ; cli
+ jmp $
section .end_section
- global ASM_KERNEL_END
- ASM_KERNEL_END:
- ; Kernel size detection \ No newline at end of file
+ global ASM_KERNEL_END
+ ASM_KERNEL_END: \ No newline at end of file
diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c
index 441f188..b0850b7 100644
--- a/src/kernel/kernel.c
+++ b/src/kernel/kernel.c
@@ -11,7 +11,6 @@
#include <kernel/syscall/syscall.h>
#include <kernel/pci/pci.h>
#include <kernel/net/network.h>
-#include <kernel/tasks/task.h>
#include <kernel/fs/load.h>
#include <kernel/fs/elf.h>
#include <kernel/lib/stdio.h>
@@ -66,18 +65,9 @@ void kernel_main(uint32_t magic, uint32_t multiboot_address)
elf_load("/bin/user");
- // tasking_install();
-
#ifdef INSTALL_MELVIX
panic("Installation isn't supported right now!");
#endif
- // load_elf((char *)userspace);
-
- // syscalls_install();
- // exec(userspace);
-
- // panic("This should NOT happen!");
-
// asm ("div %0" :: "r"(0)); // Exception testing x/0
} \ No newline at end of file
diff --git a/src/kernel/linker.ld b/src/kernel/linker.ld
index ff04c17..eda4b85 100644
--- a/src/kernel/linker.ld
+++ b/src/kernel/linker.ld
@@ -1,28 +1,34 @@
ENTRY(_start)
-SECTIONS {
+SECTIONS
+{
. = 1M;
- .text BLOCK(4K) : ALIGN(4K) {
+ .text BLOCK(4K) : ALIGN(4K)
+ {
*(.multiboot)
*(.start_section)
*(.text)
}
- .rodata BLOCK(4K) : ALIGN(4K) {
+ .rodata BLOCK(4K) : ALIGN(4K)
+ {
*(.rodata)
}
- .data BLOCK(4K) : ALIGN(4K) {
+ .data BLOCK(4K) : ALIGN(4K)
+ {
*(.data)
}
- .bss BLOCK(4K) : ALIGN(4K) {
- *(COMMON)
+ .bss BLOCK(4K) : ALIGN(4K)
+ {
+ *(.COMMON)
*(.bss)
}
- .end_section BLOCK(4K) : ALIGN(4K) {
+ .end_section BLOCK(4K) : ALIGN(4K)
+ {
*(.end_section)
}
} \ No newline at end of file
diff --git a/src/kernel/tasks/process.asm b/src/kernel/tasks/process.asm
deleted file mode 100644
index a46ac3d..0000000
--- a/src/kernel/tasks/process.asm
+++ /dev/null
@@ -1,48 +0,0 @@
-[GLOBAL read_eip]
-read_eip:
- pop eax
- jmp eax
-
-[GLOBAL copy_page_physical]
-copy_page_physical:
- push ebx
- pushf
-
- cli
-
- mov ebx, [esp+12]
- mov ecx, [esp+16]
-
- mov edx, cr0
- and edx, 0x7fffffff
- mov cr0, edx
-
- mov edx, 1024
-
-.loop:
- mov eax, [ebx]
- mov [ecx], eax
- add ebx, 4
- add ecx, 4
- dec edx
- jnz .loop
-
- mov edx, cr0
- or edx, 0x80000000
- mov cr0, edx
-
- popf
- pop ebx
- ret
-
-[GLOBAL perform_task_switch]
-perform_task_switch:
- cli
- mov ecx, [esp+4]
- mov eax, [esp+8]
- mov ebp, [esp+12]
- mov esp, [esp+16]
- mov cr3, eax
- mov eax, 0x12345
- sti
- jmp ecx \ No newline at end of file
diff --git a/src/kernel/tasks/task.c b/src/kernel/tasks/task.c
deleted file mode 100644
index 036c5d4..0000000
--- a/src/kernel/tasks/task.c
+++ /dev/null
@@ -1,177 +0,0 @@
-#include <kernel/memory/paging.h>
-#include <kernel/tasks/task.h>
-#include <kernel/memory/alloc.h>
-#include <kernel/lib/lib.h>
-#include <kernel/io/io.h>
-#include <kernel/gdt/gdt.h>
-#include <kernel/system.h>
-
-task_t *current_task;
-task_t *ready_queue;
-
-extern uint32_t read_eip();
-
-uint32_t next_pid = 1;
-
-void tasking_install()
-{
- cli();
- move_stack((void *)0xE0000000, 0x2000);
-
- current_task = ready_queue = (task_t *)kmalloc(sizeof(task_t));
- current_task->id = (int)next_pid++;
- current_task->esp = 0;
- current_task->ebp = 0;
- current_task->eip = 0;
- current_task->page_directory = current_page_directory;
- current_task->next = 0;
- current_task->kernel_stack = kmalloc(KERNEL_STACK_SIZE);
-
- vga_log("Installed Tasking");
- sti();
-}
-
-void move_stack(void *new_stack_start, uint32_t size)
-{
- for (uint32_t i = (uint32_t)new_stack_start; i >= ((uint32_t)new_stack_start - size);
- i -= 0x1000) {
- // paging_alloc_frame(paging_get_page(i, 1, current_page_directory), 0, 1);
- }
-
- uint32_t pd_addr;
- asm volatile("mov %%cr3, %0" : "=r"(pd_addr));
- asm volatile("mov %0, %%cr3" : : "r"(pd_addr));
-
- uint32_t old_stack_pointer;
- asm volatile("mov %%esp, %0" : "=r"(old_stack_pointer));
- uint32_t old_base_pointer;
- asm volatile("mov %%ebp, %0" : "=r"(old_base_pointer));
-
- uint32_t offset = (uint32_t)new_stack_start - initial_esp;
-
- uint32_t new_stack_pointer = old_stack_pointer + offset;
- uint32_t new_base_pointer = old_base_pointer + offset;
-
- memcpy((void *)new_stack_pointer, (void *)old_stack_pointer,
- initial_esp - old_stack_pointer);
-
- for (uint32_t i = (uint32_t)new_stack_start; i > (uint32_t)new_stack_start - size; i -= 4) {
- uint32_t tmp = *(uint32_t *)i;
- if ((old_stack_pointer < tmp) && (tmp < initial_esp)) {
- tmp = tmp + offset;
- uint32_t *tmp2 = (uint32_t *)i;
- *tmp2 = tmp;
- }
- }
-
- asm volatile("mov %0, %%esp" : : "r"(new_stack_pointer));
- asm volatile("mov %0, %%ebp" : : "r"(new_base_pointer));
-}
-
-extern void perform_task_switch(uint32_t, uint32_t, uint32_t, uint32_t);
-
-void switch_task()
-{
- if (!current_task)
- return;
-
- uint32_t esp, ebp, eip;
- asm volatile("mov %%esp, %0" : "=r"(esp));
- asm volatile("mov %%ebp, %0" : "=r"(ebp));
-
- eip = read_eip();
-
- if (eip == 0x12345)
- return;
-
- current_task->eip = eip;
- current_task->esp = esp;
- current_task->ebp = ebp;
-
- current_task = current_task->next;
- if (!current_task)
- current_task = ready_queue;
-
- eip = current_task->eip;
- esp = current_task->esp;
- ebp = current_task->ebp;
-
- current_page_directory = current_task->page_directory;
-
- set_kernel_stack((uintptr_t)(current_task->kernel_stack + KERNEL_STACK_SIZE));
-
- paging_switch_directory((int)current_page_directory);
- perform_task_switch(eip, (uint32_t)current_page_directory, ebp, esp);
-}
-
-int fork()
-{
- cli();
-
- task_t *parent_task = (task_t *)current_task;
-
- uint32_t *directory = 0; //paging_clone_directory(current_page_directory);
-
- task_t *new_task = (task_t *)kmalloc(sizeof(task_t));
- new_task->id = (int)next_pid++;
- new_task->esp = new_task->ebp = 0;
- new_task->eip = 0;
- new_task->page_directory = directory;
- current_task->kernel_stack = kmalloc(KERNEL_STACK_SIZE);
- new_task->next = 0;
-
- task_t *tmp_task = (task_t *)ready_queue;
- while (tmp_task->next)
- tmp_task = tmp_task->next;
- tmp_task->next = new_task;
-
- uint32_t eip = read_eip();
-
- if (current_task == parent_task) {
- uint32_t esp;
- asm volatile("mov %%esp, %0" : "=r"(esp));
- uint32_t ebp;
- asm volatile("mov %%ebp, %0" : "=r"(ebp));
- new_task->esp = esp;
- new_task->ebp = ebp;
- new_task->eip = eip;
- sti();
-
- return new_task->id;
- } else {
- return 0;
- }
-}
-
-int getpid()
-{
- return current_task->id;
-}
-
-void exec(uint32_t binary)
-{
- set_kernel_stack((uintptr_t)(current_task->kernel_stack + KERNEL_STACK_SIZE));
-
- info("Switching to user mode...");
-
- asm volatile("\
- cli; \
- mov $0x23, %%ax; \
- mov %%ax, %%ds; \
- mov %%ax, %%es; \
- mov %%ax, %%fs; \
- mov %%ax, %%gs; \
- mov %%esp, %%eax; \
- pushl $0x23; \
- pushl %%esp; \
- pushf; \
- pushl $0x1B; \
- push $1f; \
- iret; \
- 1: \
- "
- :
- : "r"(binary));
-
- // syscall_write("test");
-} \ No newline at end of file
diff --git a/src/kernel/tasks/task.h b/src/kernel/tasks/task.h
deleted file mode 100644
index 8652e44..0000000
--- a/src/kernel/tasks/task.h
+++ /dev/null
@@ -1,30 +0,0 @@
-#ifndef MELVIX_TASK_H
-#define MELVIX_TASK_H
-
-#include <stdint.h>
-#include <kernel/memory/paging.h>
-
-#define KERNEL_STACK_SIZE 2048
-
-typedef struct task {
- int id;
- uint32_t esp, ebp;
- uint32_t eip;
- uint32_t *page_directory;
- uint32_t *kernel_stack;
- struct task *next;
-} task_t;
-
-void tasking_install();
-
-void switch_task();
-
-int fork();
-
-void move_stack(void *new_stack_start, uint32_t size);
-
-int getpid();
-
-void exec(uint32_t binary);
-
-#endif \ No newline at end of file
diff --git a/src/kernel/timer/timer.c b/src/kernel/timer/timer.c
index 1bbf41b..f1a296a 100644
--- a/src/kernel/timer/timer.c
+++ b/src/kernel/timer/timer.c
@@ -1,7 +1,6 @@
#include <kernel/interrupts/interrupts.h>
#include <kernel/io/io.h>
#include <kernel/system.h>
-#include <kernel/tasks/task.h>
unsigned long timer_ticks = 0;