From 310f3621a204813071b30d2cf9db5da141c19c23 Mon Sep 17 00:00:00 2001 From: Rick Rongen Date: Fri, 5 Mar 2021 22:23:39 +0100 Subject: [PATCH] feat: fixed some bugs for running on real hardware --- kernel/cpu/isr.c | 5 +++-- kernel/drivers/pci.c | 11 ++++++++++- kernel/drivers/pci.h | 17 +++++++++++++++++ kernel/fs/fat.c | 5 +++++ kernel/libk/kprint.c | 15 +++------------ kernel/tasks/locking.c | 5 +++++ 6 files changed, 43 insertions(+), 15 deletions(-) diff --git a/kernel/cpu/isr.c b/kernel/cpu/isr.c index 0ee7022..b117828 100644 --- a/kernel/cpu/isr.c +++ b/kernel/cpu/isr.c @@ -125,12 +125,13 @@ char *exception_messages[] = { }; void isr_handler(isr_registers_t r) { + char msg[256]; if (r.int_no == 128) { syscall_handle(&r); return; } - printf("Received interrupt: %d - %s\n", r.int_no, exception_messages[r.int_no]); - k_panic(); + sprintf(msg, "Received interrupt: %d - %s\n", r.int_no, exception_messages[r.int_no]); + k_panics(msg); } void register_interrupt_handler(uint8_t n, isr_t handler) { diff --git a/kernel/drivers/pci.c b/kernel/drivers/pci.c index 23dae78..4120107 100644 --- a/kernel/drivers/pci.c +++ b/kernel/drivers/pci.c @@ -32,6 +32,8 @@ const pci_driver *pci_drivers[MAX_PCI_DRIVERS]; int last_pci_device_index = 0; pci_device pci_devices[MAX_PCI_DEVICES]; +void pci_check_bus(uint8_t bus); + uint32_t pci_register_driver(const pci_driver *pci_driver) { for (int i = 0; i < MAX_PCI_DRIVERS; ++i) { if (pci_drivers[i] != NULL) { @@ -137,9 +139,16 @@ void pci_check_function(uint8_t bus, uint8_t slot, uint8_t func) { pci_devices[last_pci_device_index].config_line_2 = pci_config_read_double_word(bus, slot, func, PCI_CONFIG_LINE_2); pci_devices[last_pci_device_index].config_line_3 = pci_config_read_double_word(bus, slot, func, PCI_CONFIG_LINE_3); pci_devices[last_pci_device_index].device_state.present = 1; - pci_pick_driver(&pci_devices[last_pci_device_index]); last_pci_device_index++; + if (pci_devices[last_pci_device_index-1].class == 0x06 && pci_devices[last_pci_device_index-1].subclass == 0x04) { + uint8_t secondary_bus = pci_config_read_byte(bus, slot, func, PCI_CONFIG_SECONDARY_BUS_NUMBER); + printf("Found secondary bus: %d\n", secondary_bus); + pci_check_bus(secondary_bus); + } else { + pci_pick_driver(&pci_devices[last_pci_device_index-1]); + } + // todo do something with the function } diff --git a/kernel/drivers/pci.h b/kernel/drivers/pci.h index 33705a2..4552ead 100644 --- a/kernel/drivers/pci.h +++ b/kernel/drivers/pci.h @@ -57,6 +57,23 @@ #define PCI_CONFIG_MAX_GRANT 0x3E #define PCI_CONFIG_MAX_LATENCY 0x3F +// header type 1 +#define PCI_CONFIG_PRIMARY_BUS_NUMBER 0x18 +#define PCI_CONFIG_SECONDARY_BUS_NUMBER 0x19 +#define PCI_CONFIG_SUBORDINATE_BUS_NUMBER 0x1A +#define PCI_CONFIG_SECONDARY_LATENCY_TIMER 0x1B +#define PCI_CONFIG_IO_BASE 0x1C +#define PCI_CONFIG_IO_LIMIT 0x1D +#define PCI_CONFIG_SECONDARY_STATUS 0x1E +#define PCI_CONFIG_MEMORY_BASE 0x20 +#define PCI_CONFIG_MEMORY_LIMIT 0x22 +#define PCI_CONFIG_MEMORY_BASE_UPPER 0x28 +#define PCI_CONFIG_MEMORY_LIMIT_UPPER 0x2C +#define PCI_CONFIG_IO_BASE_UPPER 0x30 +#define PCI_CONFIG_IO_LIMIT_UPPER 0x32 +#define PCI_CONFIG_EXPANSION_ROM 0x38 +#define PCI_CONFIG_BRIDGE_CONTROL 0x3E + #define PCI_INTERRUPT_LINE_DISABLED 0xff typedef struct pci_driver pci_driver; diff --git a/kernel/fs/fat.c b/kernel/fs/fat.c index b57034d..74bcd19 100644 --- a/kernel/fs/fat.c +++ b/kernel/fs/fat.c @@ -123,6 +123,11 @@ void print_chars(char *chars, int amount) { uint8_t fat_check_device(const block_device *device, uint8_t *first_sector) { fat_bpb bpb; memcpy((uint8_t *) &bpb, first_sector, sizeof(fat_bpb)); + if (bpb.bpb.sectors_per_fat == 0 || bpb.bpb.sectors_per_cluster == 0) { + printf("Definitely not FAT\n"); + return BLOCK_DEV_DRIVER_CHECK_NO_MATCH; + } + printf("OEM: "); print_chars(bpb.bpb.oem_identifier, 11); printf("\n"); diff --git a/kernel/libk/kprint.c b/kernel/libk/kprint.c index 3ab0d0d..ec88f6f 100644 --- a/kernel/libk/kprint.c +++ b/kernel/libk/kprint.c @@ -11,21 +11,12 @@ #include "kprint.h" #define MAX_HANDLERS 8 -#define STREAM_SIZE 512 +#define STREAM_SIZE (32*1024) #define PRINT_BUFFER_SIZE 64 stream_t *kprint_stream; -kprint_handler handlers[MAX_HANDLERS] = { - NULL, - NULL, - NULL, - NULL, - NULL, - NULL, - NULL, - NULL, -}; +kprint_handler handlers[MAX_HANDLERS] = {0}; void kprint_register(kprint_handler handler) { for (int i = 0; i < MAX_HANDLERS; ++i) { @@ -50,7 +41,7 @@ void kprint_internal(const char *msg) { } } -void kprint_sync(const char* msg) { +void kprint_sync(const char *msg) { kprint_internal(msg); } diff --git a/kernel/tasks/locking.c b/kernel/tasks/locking.c index 9b21811..edb6e6b 100644 --- a/kernel/tasks/locking.c +++ b/kernel/tasks/locking.c @@ -8,6 +8,7 @@ #include #include #include +#include typedef struct lock_fifo_entry { uint32_t tid; @@ -32,6 +33,7 @@ struct spinlock { semaphore_t *semaphore_create(int32_t start) { semaphore_t *semaphore = malloc(sizeof(semaphore_t)); + memset((uint8_t *) semaphore, 0, sizeof(semaphore_t)); semaphore->value = start; return semaphore; } @@ -43,6 +45,7 @@ void semaphore_wait(semaphore_t *semaphore) { task_lock_acquire(); lock_fifo_entry_t *lock = malloc(sizeof(lock_fifo_entry_t)); lock->tid = task_get_current_tid(); + lock->next = NULL; if (semaphore->first_wait == NULL) { semaphore->first_wait = lock; } else { @@ -77,6 +80,7 @@ void semaphore_free(semaphore_t *semaphore) { mutex_t *mutex_create() { mutex_t *mutex = malloc(sizeof(mutex_t)); + memset((uint8_t *) mutex, 0, sizeof(mutex_t)); mutex->value = 1; return mutex; } @@ -88,6 +92,7 @@ void mutex_acquire(mutex_t *mutex) { task_lock_acquire(); lock_fifo_entry_t *lock = malloc(sizeof(lock_fifo_entry_t)); lock->tid = task_get_current_tid(); + lock->next = NULL; if (mutex->first_wait == NULL) { mutex->first_wait = lock; } else {