Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added additional unit tests + bug fixes #8

Merged
merged 5 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified efi/OVMF_VARS.fd
Binary file not shown.
8 changes: 8 additions & 0 deletions kernel/src/arch/x86/ap_startup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ void _prepareApStartupMemoryMappings() {
}

void initializeApCores() {
const uint32_t coreStartupMaxTimeout = 3; // seconds

auto& acpiController = AcpiController::get();
Madt* apicTable = acpiController.getApicTable();

Expand Down Expand Up @@ -122,6 +124,9 @@ void initializeApCores() {
// Let the AP cores continue on their own asynchronously
_releaseApStartupSpinlockFlag();
});

// Wait for all cores to fully start and finish initializing
sleep(coreStartupMaxTimeout);
}

void bootAndInitApCore(uint8_t apicid) {
Expand Down Expand Up @@ -170,6 +175,9 @@ void apStartupEntryC(int apicid) {
size_t usermodeStackSize = 8 * PAGE_SIZE;
size_t userStackTop = (uint64_t)(usermodeStack + usermodeStackSize);

// Set the userStackTop field of the CPU's swapper task
g_kernelSwapperTasks[apicid].userStackTop = userStackTop;

__call_lowered_entry(apStartupEntryLowered, (void*)userStackTop);
while (1);
}
Expand Down
38 changes: 5 additions & 33 deletions kernel/src/entry/kernel_entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,9 @@
#include <time/ktime.h>
#include <kprint.h>

#include "tests/kernel_unit_tests.h"
#include "tests/kernel_entry_tests.h"

// #define KE_TEST_MULTITHREADING
// #define KE_TEST_XHCI_INIT
// #define KE_TEST_AP_STARTUP
// #define KE_TEST_CPU_TEMP_READINGS
// #define KE_TEST_PRINT_CURRENT_TIME
// #define KE_TEST_GRAPHICS
#ifdef KRUN_UNIT_TESTS
#include "tests/kernel_unit_tests.h"
#endif

EXTERN_C __PRIVILEGED_CODE void _kentry(KernelEntryParams* params);
extern uint64_t __kern_phys_base;
Expand Down Expand Up @@ -75,6 +69,8 @@ __PRIVILEGED_CODE void _kentry(KernelEntryParams* params) {
g_kernelSwapperTasks[BSP_CPU_ID].pid = 1;
zeromem(&g_kernelSwapperTasks[BSP_CPU_ID].context, sizeof(CpuContext));
g_kernelSwapperTasks[BSP_CPU_ID].context.rflags |= 0x200;
g_kernelSwapperTasks[BSP_CPU_ID].userStackTop =
(uint64_t)(__usermodeKernelEntryStack + USERMODE_KERNEL_ENTRY_STACK_SIZE);

// Elevated flag must be 0 since we are going to lower ourselves in the next few calls.
// TO-DO: investigate further why setting elevated flag to 1 here causes a crash.
Expand Down Expand Up @@ -183,30 +179,6 @@ void _kuser_entry() {
// Bring up all available processor cores
initializeApCores();

#ifdef KE_TEST_MULTITHREADING
ke_test_multithreading();
#endif

#ifdef KE_TEST_XHCI_INIT
ke_test_xhci_init();
#endif

#ifdef KE_TEST_AP_STARTUP
ke_test_ap_startup();
#endif

#ifdef KE_TEST_CPU_TEMP_READINGS
ke_test_read_cpu_temps();
#endif

#ifdef KE_TEST_PRINT_CURRENT_TIME
ke_test_print_current_time();
#endif

#ifdef KE_TEST_GRAPHICS
ke_test_graphics();
#endif

#ifdef KRUN_UNIT_TESTS
// Run unit tests
executeUnitTests();
Expand Down
27 changes: 0 additions & 27 deletions kernel/src/entry/tests/kernel_entry_ap_startup_test.cpp

This file was deleted.

23 changes: 0 additions & 23 deletions kernel/src/entry/tests/kernel_entry_cpu_temps_test.cpp

This file was deleted.

26 changes: 0 additions & 26 deletions kernel/src/entry/tests/kernel_entry_graphics_test.cpp

This file was deleted.

63 changes: 0 additions & 63 deletions kernel/src/entry/tests/kernel_entry_multithreading_test.cpp

This file was deleted.

16 changes: 0 additions & 16 deletions kernel/src/entry/tests/kernel_entry_tests.h

This file was deleted.

16 changes: 0 additions & 16 deletions kernel/src/entry/tests/kernel_entry_time_print_test.cpp

This file was deleted.

1 change: 0 additions & 1 deletion kernel/src/entry/tests/kernel_entry_xhci_init_test.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include "kernel_entry_tests.h"
#include <core/kprint.h>
#include <kelevate/kelevate.h>
#include <acpi/acpi_controller.h>
Expand Down
66 changes: 63 additions & 3 deletions kernel/src/entry/tests/memory_allocation.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,16 @@ DECLARE_UNIT_TEST("Heap Allocate - Heavy", kheapHeavyAllocateUnitTest) {
// Print progress every 10% of iterations or every 100 MB
if ((i + 1) % reportInterval == 0 || totalAllocatedBytes >= nextMemoryMilestone) {
kuPrint(UNIT_TEST "Allocated %llu MB of memory after %llu iterations\n",
(unsigned long long)(totalAllocatedBytes / bytesPerMB),
(unsigned long long)(i + 1));
(uint64_t)(totalAllocatedBytes / bytesPerMB),
(uint64_t)(i + 1));

// Update the memory milestone for the next 100 MB
nextMemoryMilestone += 100 * bytesPerMB;
}
}

kuPrint(UNIT_TEST "Finished allocating %llu MB of memory in total\n",
(unsigned long long)(totalAllocatedBytes / bytesPerMB));
(uint64_t)(totalAllocatedBytes / bytesPerMB));

// Free all the allocated memory in this test
for (size_t i = 0; i < iterations; i++) {
Expand Down Expand Up @@ -204,3 +204,63 @@ DECLARE_UNIT_TEST("Paging - Allocate Multiple Zeroed Pages", pagingAllocateMulti

return UNIT_TEST_SUCCESS;
}

DECLARE_UNIT_TEST("Paging and Heap Allocation Combined Test", kheapWithPagingAllocationUnitTest) {
const size_t allocSize = 100000; // Allocation size in bytes
const size_t iterations = 8000; // Number of allocations
const size_t reportInterval = iterations / 10; // Report every 10% of iterations
const size_t bytesPerMB = 1024 * 1024;
size_t nextMemoryMilestone = 100 * bytesPerMB; // First memory milestone (100 MB)

void** savedHeapPointers = (void**)kmalloc(sizeof(void*) * iterations);
ASSERT_TRUE_CRITICAL(savedHeapPointers, "Failed to allocate a buffer for heap memory pointers");

void** savedPagePointers = (void**)kmalloc(sizeof(void*) * iterations);
ASSERT_TRUE_CRITICAL(savedPagePointers, "Failed to allocate a buffer for allocated page pointers");

size_t totalAllocatedBytes = 0; // Track total allocated memory in bytes

// Perform a heavy-weight tight loop allocation test
for (size_t i = 0; i < iterations; i++) {
void* ptr = kmalloc(allocSize);
ASSERT_TRUE_CRITICAL(ptr, "Allocated heap memory pointer was null");

void* page = zallocPage();
ASSERT_TRUE_CRITICAL(page, "Allocated virtual page pointer was null");

savedHeapPointers[i] = ptr;
savedPagePointers[i] = page;

totalAllocatedBytes += allocSize;

// Print progress every 10% of iterations or every 100 MB
if ((i + 1) % reportInterval == 0 || totalAllocatedBytes >= nextMemoryMilestone) {
kuPrint(UNIT_TEST "Allocated %llu MB of heap memory and %llu pages after %llu iterations\n",
(uint64_t)(totalAllocatedBytes / bytesPerMB),
(uint64_t)(i + 1),
(uint64_t)(i + 1));

// Update the memory milestone for the next 100 MB
nextMemoryMilestone += 100 * bytesPerMB;
}
}

kuPrint(UNIT_TEST "Finished allocating %llu MB of heap memory and %llu pages in total\n",
(uint64_t)(totalAllocatedBytes / bytesPerMB), iterations);

// Free all the allocated heap memory in this test
for (size_t i = 0; i < iterations; i++) {
kfree(savedHeapPointers[i]);
}

// Free all the allocated pages in this test
for (size_t i = 0; i < iterations; i++) {
freePage(savedPagePointers[i]);
}

// Free the pointer buffers
kfree(savedHeapPointers);
kfree(savedPagePointers);

return UNIT_TEST_SUCCESS;
}
Loading