feat: introduced tasking, added paging (no vm), moved malloc, added
syscalls, other stuff
This commit is contained in:
145
kernel/kernel.c
145
kernel/kernel.c
@@ -1,6 +1,4 @@
|
||||
#include <drivers/ports.h>
|
||||
#include <drivers/vgascreen.h>
|
||||
#include <libc/libc.h>
|
||||
#include <cpu/isr.h>
|
||||
#include <cpu/timer.h>
|
||||
#include <drivers/keyboard.h>
|
||||
@@ -8,8 +6,6 @@
|
||||
#include <multiboot.h>
|
||||
#include <drivers/serial.h>
|
||||
#include <kprint.h>
|
||||
#include <libc/readline.h>
|
||||
#include <libc/string.h>
|
||||
#include <libk.h>
|
||||
#include <drivers/pci.h>
|
||||
#include <drivers/ide.h>
|
||||
@@ -18,118 +14,22 @@
|
||||
#include <fs/blockdev.h>
|
||||
#include <fs/fat.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#define BOOTLOADER_NAME_MAX_LENGTH 64
|
||||
#define CMDLINE_MAX_LENGTH 256
|
||||
#include <cpu/cpuidx.h>
|
||||
#include <mem/malloc.h>
|
||||
#include <tasks/task.h>
|
||||
#include <syscall.h>
|
||||
#include <command.h>
|
||||
#include <attributes.h>
|
||||
|
||||
const int version_major = 0,
|
||||
version_minor = 0,
|
||||
version_patch = 1;
|
||||
|
||||
char bootloader_name[BOOTLOADER_NAME_MAX_LENGTH];
|
||||
char cmdline[CMDLINE_MAX_LENGTH];
|
||||
|
||||
typedef void (*cmd_handler_func)(const char *);
|
||||
|
||||
typedef struct {
|
||||
char *cmd;
|
||||
cmd_handler_func handler;
|
||||
} cmd_handler;
|
||||
|
||||
void print(const char *arg);
|
||||
|
||||
void ide(const char *arg);
|
||||
|
||||
void echo(const char *arg);
|
||||
|
||||
void help(const char *arg);
|
||||
|
||||
cmd_handler cmd_handlers[] = {
|
||||
{"help", help},
|
||||
{"echo", echo},
|
||||
{"print", print},
|
||||
{"ide", ide},
|
||||
{NULL, NULL},
|
||||
};
|
||||
|
||||
void print_bootinfo() {
|
||||
printf("Bootloader name: %s\n"
|
||||
"cmdline: %s\n",
|
||||
(bootloader_name[0] == 0 ? "NULL" : bootloader_name),
|
||||
(cmdline[0] == 0 ? "NULL" : cmdline));
|
||||
}
|
||||
|
||||
void help(const char *arg) {
|
||||
kprint("Available commands:\n");
|
||||
int index = 0;
|
||||
while (true) {
|
||||
if (cmd_handlers[index].cmd == NULL) {
|
||||
break;
|
||||
}
|
||||
printf("%s\n", cmd_handlers[index].cmd);
|
||||
index += 1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void echo(const char *arg) {
|
||||
printf("%s\n", arg);
|
||||
}
|
||||
|
||||
void print(const char *arg) {
|
||||
if (strcmp(arg, "mmap") == 0) {
|
||||
print_mmap_info();
|
||||
} else if (strcmp(arg, "malloc") == 0) {
|
||||
print_malloc_info();
|
||||
} else if (strcmp(arg, "tick") == 0) {
|
||||
print_current_tick();
|
||||
} else if (strcmp(arg, "bootinfo") == 0) {
|
||||
print_bootinfo();
|
||||
} else if (strcmp(arg, "pci") == 0) {
|
||||
pci_print_info();
|
||||
} else if (strcmp(arg, "ide") == 0) {
|
||||
ide_print_devices();
|
||||
} else {
|
||||
printf("Unknown print %s\n", arg);
|
||||
}
|
||||
}
|
||||
|
||||
void ide(const char *arg) {
|
||||
if (strcmp(arg, "block_devices") == 0) {
|
||||
block_dev_print_info();
|
||||
} else if (strcmp(arg, "ide_devices") == 0) {
|
||||
ide_print_devices();
|
||||
} else if (strcmp(arg, "mbr") == 0) {
|
||||
mbr_read_from_ide(0);
|
||||
} else {
|
||||
printf("Unknown print %s\n", arg);
|
||||
}
|
||||
}
|
||||
|
||||
void main_loop();
|
||||
|
||||
void store_bootloader_info(multiboot_info_t *multiboot_info) {
|
||||
// get bootloader and cmdline
|
||||
if (multiboot_info->flags & MULTIBOOT_INFO_CMDLINE) {
|
||||
int cmdline_length = strlen((const char *) multiboot_info->cmdline);
|
||||
if (cmdline_length > CMDLINE_MAX_LENGTH) {
|
||||
k_panics("cmdline to long!\n");
|
||||
}
|
||||
memcpy(cmdline, (char *) multiboot_info->cmdline, cmdline_length);
|
||||
}
|
||||
if (multiboot_info->flags & MULTIBOOT_INFO_BOOT_LOADER_NAME) {
|
||||
int bootloader_length = strlen((const char *) multiboot_info->boot_loader_name);
|
||||
if (bootloader_length > BOOTLOADER_NAME_MAX_LENGTH) {
|
||||
k_panics("bootloader name to long!\n");
|
||||
}
|
||||
memcpy(bootloader_name, (char *) multiboot_info->boot_loader_name, bootloader_length);
|
||||
}
|
||||
}
|
||||
|
||||
void init_mmap(multiboot_info_t *multiboot_info) {
|
||||
if (multiboot_info->flags & (1 << 6)) {
|
||||
init_mmap_multiboot((struct multiboot_mmap_entry *) multiboot_info->mmap_addr,
|
||||
mmap_init_multiboot((struct multiboot_mmap_entry *) multiboot_info->mmap_addr,
|
||||
multiboot_info->mmap_length / sizeof(struct multiboot_mmap_entry));
|
||||
malloc_init();
|
||||
// todo fallback on other mechanisms?
|
||||
} else {
|
||||
k_panics("mmap invalid!\n");
|
||||
@@ -156,7 +56,7 @@ void init_block_devices() {
|
||||
block_dev_scan_repeat();
|
||||
}
|
||||
|
||||
void kmain(multiboot_info_t *multiboot_info) {
|
||||
void noreturn kmain(multiboot_info_t *multiboot_info) {
|
||||
isr_install();
|
||||
vga_clear_screen();
|
||||
vga_clear_screen(' ', VGA_WHITE | (VGA_GRAY << VGA_SHIFT_BG));
|
||||
@@ -164,6 +64,8 @@ void kmain(multiboot_info_t *multiboot_info) {
|
||||
serial_init();
|
||||
kprint_register(serial_kprint);
|
||||
|
||||
cpuidx_print_info();
|
||||
|
||||
store_bootloader_info(multiboot_info);
|
||||
init_mmap(multiboot_info);
|
||||
|
||||
@@ -176,27 +78,6 @@ void kmain(multiboot_info_t *multiboot_info) {
|
||||
|
||||
printf("Booted successfully v%d.%d.%d\n", version_major, version_minor, version_patch);
|
||||
|
||||
// enter main loop
|
||||
while (true) {
|
||||
main_loop();
|
||||
}
|
||||
task_spawn(main_loop, NULL);
|
||||
syscall_start_scheduler();
|
||||
}
|
||||
|
||||
void main_loop() {
|
||||
char *msg = readline(NULL);
|
||||
char *args = strchr(msg, ' ') + 1;
|
||||
args[-1] = 0;
|
||||
int test_index = 0;
|
||||
while (true) {
|
||||
if (cmd_handlers[test_index].cmd == 0) {
|
||||
printf("Unknown command: %s\n", msg);
|
||||
break;
|
||||
}
|
||||
if (strcmp(cmd_handlers[test_index].cmd, msg) == 0) {
|
||||
cmd_handlers[test_index].handler(args);
|
||||
break;
|
||||
}
|
||||
test_index++;
|
||||
}
|
||||
free(msg);
|
||||
}
|
||||
Reference in New Issue
Block a user