feat: cleanup init code, small other refactors

This commit is contained in:
2021-09-01 21:43:21 +02:00
parent e693b12915
commit 073051c99e
21 changed files with 118 additions and 76 deletions

View File

@@ -5,10 +5,6 @@
#ifndef NEW_KERNEL_ACPI_H
#define NEW_KERNEL_ACPI_H
void acpi_parse();
void acpi_init();
void* acpi_find_table_by_name(const char* name, int skip);
#endif //NEW_KERNEL_ACPI_H

View File

@@ -9,6 +9,4 @@
void store_bootloader_info(multiboot_info_t *multiboot_info);
void main_loop(void *data);
#endif //NEW_KERNEL_COMMAND_H

View File

@@ -7,7 +7,7 @@
#include <sys/types.h>
int pit_init(uint32_t freq);
int pit_int_frequency(uint32_t freq);
void print_current_tick();

View File

@@ -19,8 +19,6 @@ typedef struct KeyEvent_t {
char getc();
void init_keyboard();
//const char *key_code_to_string(KeyCode key);
KeyEvent *get_next_event();

View File

@@ -1,12 +0,0 @@
//
// Created by rick on 28-01-21.
//
#ifndef NEW_KERNEL_SERIAL_H
#define NEW_KERNEL_SERIAL_H
int serial_init();
void serial_kprint(const char *msg);
#endif //NEW_KERNEL_SERIAL_H

View File

@@ -27,8 +27,4 @@
#define VGA_COL_MAX 80
#define VGA_ROW_MAX 25
void vga_clear_screen();
void vga_kprint(const char *msg);
#endif //MY_KERNEL_VGASCREEN_H

View File

@@ -18,5 +18,3 @@ void kprint(const char *msg);
void kprint_sync(const char *msg);
void kprint_init();
void kprint_start_task();

View File

@@ -8,13 +8,23 @@
#include <sys/types.h>
#include <myke/driver.h>
enum init_stage {
INIT_STAGE_EARLY_BOOT_0, // id mapped, high memory, no malloc
INIT_STAGE_EARLY_BOOT_1, // memory available, no tasking
INIT_STAGE_LATE_BOOT, // time source, memory, most basic hardware available
INIT_STAGE_PRE_TASKING, // just before tasking is ready
INIT_STAGE_AFTER_BOOT_PRE_INIT, // tasking just started
// todo define later stages
};
struct init {
const char *name;
enum init_stage stage;
void (*init)();
};
#define INIT_FUNCTION(order) GENERIC_DRIVER(init, order)
void init_execute_all();
void init_execute_all(enum init_stage stage);
#endif //NEW_KERNEL_INIT_H

View File

@@ -66,8 +66,6 @@ uint8_t block_dev_register(block_device_t *device);
void block_dev_free(block_device_t *device);
void block_dev_start_task();
void block_dev_print_info();
bool block_dev_mount(char *identifier, char *driver);

View File

@@ -13,6 +13,7 @@
#include <myke/acpi/structures.h>
#include <myke/cpu/lapic.h>
#include <myke/libk/libk.h>
#include <myke/util/init.h>
void acpi_handle_facp(const struct acpi_sdt_header *addr);
@@ -272,5 +273,12 @@ void acpi_parse() {
}
void acpi_init() {
acpi_parse();
lai_create_namespace();
}
INIT_FUNCTION(100) = {
.name = "acpi",
.stage = INIT_STAGE_EARLY_BOOT_1,
.init = acpi_init,
};

View File

@@ -25,6 +25,7 @@
#include <myke/debug/debug.h>
#include <myke/util/slingurl.h>
#include <myke/libk/syscall.h>
#include <myke/util/init.h>
#endif
@@ -223,3 +224,15 @@ void att_noreturn main_loop(void *data) {
free(msg);
}
}
#ifdef K_SHELL
void main_loop_start() {
task_spawn(main_loop, NULL, "main");
}
INIT_FUNCTION(100) = {
.name = "main-task",
.stage = INIT_STAGE_PRE_TASKING,
.init = main_loop_start,
};
#endif

View File

@@ -78,7 +78,7 @@ void print_current_tick() {
kprint("\n");
}
int pit_init(uint32_t freq) {
int pit_int_frequency(uint32_t freq) {
register_interrupt_handler(IRQ0, pit_callback);
uint32_t divisor = PIT_FREQUENCY / freq;

View File

@@ -8,6 +8,7 @@
#include <myke/libc/ringqueue.h>
#include <myke/libk/libk.h>
#include <myke/libk/syscall.h>
#include <myke/util/init.h>
const char scancode_map_lowercase[] = {
@@ -131,7 +132,7 @@ static void keyboard_callback(isr_registers_t *regs) {
publish_key_event(scancode);
}
void init_keyboard() {
void keyboard_init() {
register_interrupt_handler(IRQ1, keyboard_callback);
keyboard_state.shift = 0;
keyboard_state.ctrl = 0;
@@ -139,3 +140,9 @@ void init_keyboard() {
keyboard_state.extended = 0;
keyboard_event_buffer = create_buffer(256, sizeof(KeyEvent));
}
INIT_FUNCTION(100) = {
.name = "keyboard",
.stage = INIT_STAGE_LATE_BOOT,
.init = keyboard_init,
};

View File

@@ -2,9 +2,10 @@
// Created by rick on 28-01-21.
//
#include <myke/drivers/serial.h>
#include <sys/types.h>
#include <myke/drivers/ports.h>
#include <myke/libk/kprint.h>
#include <myke/util/init.h>
#define SERIAL_INTERRUPT_DATA_AVAILABLE (1 << 0)
#define SERIAL_INTERRUPT_TRANSMITTER_EMPTY (1 << 1)
@@ -51,7 +52,7 @@
#define MODEM_CONTROL_LOOPBACK_MODE (1 << 4)
#define MODEM_CONTROL_AUTOFLOW_CONTROL_ENABLED (1 << 5)
int serial_init() {
int serial_init_hw() {
port_byte_out(PORT_SERIAL_0 + PORT_SERIAL_INTERRUPT, 0); // Disable all interrupts
port_byte_out(PORT_SERIAL_0 + PORT_SERIAL_LINE_CONTROL,
LINE_CONTROL_DIVISOR); // Enable DLAB (set baud rate divisor)
@@ -115,3 +116,16 @@ void serial_kprint(const char *msg) {
i++;
}
}
void serial_init() {
if (serial_init_hw() != 0) {
return;
}
kprint_register(serial_kprint);
}
INIT_FUNCTION(100) = {
.name = "serial",
.stage = INIT_STAGE_EARLY_BOOT_0,
.init = serial_init,
};

View File

@@ -3,9 +3,11 @@
//
#include <string.h>
#include <myke/drivers/vgascreen.h>
#include <myke/drivers/ports.h>
#include <myke/drivers/vgascreen.h>
#include <myke/libk/kprint.h>
#include <myke/util/init.h>
char *_vga_character_memory = (char *) VGA_CHARACTER_MEMORY_LOCATION;
@@ -136,3 +138,14 @@ int get_offset_row(int offset) {
int get_offset_col(int offset) {
return (offset - (get_offset_row(offset) * 2 * VGA_COL_MAX)) / 2;
}
void vga_init() {
vga_clear_screen();
kprint_register(vga_kprint);
}
INIT_FUNCTION(100) = {
.name = "vga",
.stage = INIT_STAGE_EARLY_BOOT_0,
.init = vga_init,
};

View File

@@ -4,18 +4,12 @@
#define DEBUG_INIT
#include <myke/acpi/acpi.h>
#include <myke/command.h>
#include <myke/cpu/cpuidx.h>
#include <myke/cpu/gdt.h>
#include <myke/cpu/isr.h>
#include <myke/cpu/pit.h>
#include <myke/debug/debug.h>
#include <myke/drivers/keyboard.h>
#include <myke/drivers/pci/pci.h>
#include <myke/drivers/serial.h>
#include <myke/drivers/vgascreen.h>
#include <myke/vfs/blockdev.h>
#include <myke/libk/kprint.h>
#include <myke/libk/libk.h>
#include <myke/libk/syscall.h>
@@ -44,11 +38,8 @@ void init_pci_system() {
void att_noreturn att_used kmain(multiboot_info_t *multiboot_info, uint32_t mb_name) {
// early init
isr_install();
vga_clear_screen();
vga_clear_screen(' ', VGA_WHITE | (VGA_GRAY << VGA_SHIFT_BG));
kprint_register(vga_kprint);
serial_init();
kprint_register(serial_kprint);
// initialize early modules (kprint etc.)
init_execute_all(INIT_STAGE_EARLY_BOOT_0);
// parse multiboot
if (mb_name != MULTIBOOT_BOOTLOADER_MAGIC) {
@@ -59,41 +50,33 @@ void att_noreturn att_used kmain(multiboot_info_t *multiboot_info, uint32_t mb_n
// initialize memory management
init_mmap(multiboot_info);
// safe multiboot info for later use
debug_store_info(multiboot_info);
gdt_init();
// initialize kprint functionality
kprint_init();
// todo earlier in boot
acpi_parse();
acpi_init();
debug_store_info(multiboot_info);
// identify cpu
cpuidx_print_info();
// initialize early driver code (ACPI, etc.)
init_execute_all(INIT_STAGE_EARLY_BOOT_1);
// enable interrupts
__asm__ __volatile__("sti");
// start the timer
pit_init(1000);
// initialize devices
init_keyboard();
pit_int_frequency(1000);
// initialize drivers that are not discovered in any other way
init_execute_all(INIT_STAGE_LATE_BOOT);
// init PCI
init_pci_system();
printf("Booted successfully v%d.%d.%d\n", version_major, version_minor, version_patch);
// initializing modules
init_execute_all();
// initialize tasking
task_init();
kprint_start_task();
block_dev_start_task();
#ifdef K_SHELL
task_spawn(main_loop, NULL, "main");
#endif
// let other system provide tasks (command, kprint, blockdev)
init_execute_all(INIT_STAGE_PRE_TASKING);
// switch to tasking
syscall_start_scheduler();
}

View File

@@ -9,12 +9,13 @@
#include <myke/libk/kprint.h>
#include <myke/tasks/task.h>
#include <myke/util/stream.h>
#include <myke/util/init.h>
#define MAX_HANDLERS 8
#define STREAM_SIZE (32*1024)
#define PRINT_BUFFER_SIZE 64
stream_t *kprint_stream;
stream_t *kprint_stream = NULL;
kprint_handler handlers[MAX_HANDLERS] = {0};
@@ -29,8 +30,12 @@ void kprint_register(kprint_handler handler) {
}
void kprint(const char *msg) {
if (kprint_stream == NULL) {
kprint_sync(msg);
} else {
stream_write(kprint_stream, (const uint8_t *) msg, strlen(msg));
}
}
void kprint_internal(const char *msg) {
for (int i = 0; i < MAX_HANDLERS; ++i) {
@@ -66,3 +71,9 @@ void att_noreturn kprint_task(void *_) {
void kprint_start_task() {
task_spawn(kprint_task, NULL, "kprint");
}
INIT_FUNCTION(100) = {
.name = "kprint-task",
.stage = INIT_STAGE_PRE_TASKING,
.init = kprint_start_task,
};

View File

@@ -725,7 +725,7 @@ void free(void *ptr) {
void *calloc(size_t nobj, size_t size) {
int real_size;
size_t real_size;
void *p;
real_size = nobj * size;

View File

@@ -10,9 +10,12 @@ extern struct init __stop_init[];
#define NUM_DRIVERS ((size_t)(__stop_init - __start_init))
#define DRIVER(i) ((__start_init) + (i))
void init_execute_all() {
void init_execute_all(enum init_stage stage) {
for (size_t i = 0; i < NUM_DRIVERS; ++i) {
if (DRIVER(i)->stage != stage) continue;
if (stage > INIT_STAGE_EARLY_BOOT_0) {
printf("init %s\n", DRIVER(i)->name);
}
DRIVER(i)->init();
}
}

View File

@@ -11,6 +11,7 @@
#include <myke/libk/libk.h>
#include <myke/tasks/locking.h>
#include <myke/tasks/task.h>
#include <myke/util/init.h>
#define MAX_BLOCK_DEVS 64
@@ -143,3 +144,9 @@ void block_dev_print_info() {
block_devices[i].flags.driver_installed ? block_devices[i].driver->name : "n/a");
}
}
INIT_FUNCTION(100) = {
.name = "blockdev-task",
.stage = INIT_STAGE_PRE_TASKING,
.init = block_dev_start_task,
};

View File

@@ -9,5 +9,6 @@ void tmpfs_init() {
INIT_FUNCTION(100) = {
.name = "tmpfs",
.stage = INIT_STAGE_AFTER_BOOT_PRE_INIT,
.init = tmpfs_init,
};