Skip to content

Commit 41645bb

Browse files
jcjgrafwipawel
authored andcommitted
Add unittest that stress-tests the vmm and pmm
Repeatedly allocate and map, deallocate and unmape a number of pages/frames. With the current mainline version, this test either causes an assertion in the PMM or locks-up (deadlock?). Signed-off-by: Jean-Claude Graf <[email protected]>
1 parent f2382b2 commit 41645bb

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

tests/unittests.c

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <console.h>
2727
#include <cpuid.h>
2828
#include <ktf.h>
29+
#include <lib.h>
2930
#include <mm/pmm.h>
3031
#include <pagetable.h>
3132
#include <real_mode.h>
@@ -72,6 +73,33 @@ static unsigned long test_kernel_task_func(void *arg) {
7273
return _ul(arg);
7374
}
7475

76+
static inline uint64_t rand47(void) {
77+
return (_ul(rand()) << 16) ^ rand();
78+
}
79+
80+
#define KT2_ROUNDS 100
81+
#define KT2_CHUNKS 10
82+
static unsigned long test_kernel_pmm_vmm_stress_test(void *arg) {
83+
srand(rdtsc());
84+
85+
uint64_t blocks[KT2_CHUNKS] = {0};
86+
87+
for (size_t i = 0; i < KT2_ROUNDS; i++) {
88+
for (size_t j = 0; j < KT2_CHUNKS; j++) {
89+
blocks[j] = rand47() & ~(PAGE_SIZE - 1);
90+
void *res = vmap_kern_4k(_ptr(blocks[j]), get_free_frame()->mfn, L1_PROT);
91+
ASSERT(res == _ptr(blocks[j]));
92+
}
93+
94+
for (int j = 0; j < KT2_CHUNKS; j++) {
95+
mfn_t mfn;
96+
ASSERT(!vunmap_kern(_ptr(blocks[j]), &mfn, PAGE_ORDER_4K));
97+
put_free_frame(mfn);
98+
}
99+
}
100+
return 0;
101+
}
102+
75103
static unsigned long __user_text test_user_task_func1(void *arg) {
76104

77105
printf(USTR("printf: %u %x %d\n"), 1234, 0x41414141, 9);
@@ -203,11 +231,12 @@ int unit_tests(void *_unused) {
203231
printk("PTE: 0x%lx\n", pte2->entry);
204232
unmap_pagetables_va(&cr3, unit_tests);
205233

206-
task_t *task1, *task2, *task_user1, *task_user1_se, *task_user1_int80, *task_user2,
207-
*task_user3, *task_user4;
234+
task_t *task1, *task2, *task3, *task_user1, *task_user1_se, *task_user1_int80,
235+
*task_user2, *task_user3, *task_user4;
208236

209237
task1 = new_kernel_task("test1", test_kernel_task_func, _ptr(98));
210238
task2 = new_kernel_task("test2", test_kernel_task_func, _ptr(-99));
239+
task3 = new_kernel_task("test3", test_kernel_pmm_vmm_stress_test, NULL);
211240
task_user1 = new_user_task("test1 user", test_user_task_func1, NULL);
212241
task_user1_se =
213242
new_user_task("test1 user sysenter", test_user_task_func1_sysenter, NULL);
@@ -230,6 +259,7 @@ int unit_tests(void *_unused) {
230259
set_task_repeat(task1, 10);
231260
schedule_task(task1, get_bsp_cpu());
232261
schedule_task(task2, get_cpu(1));
262+
schedule_task(task3, get_cpu(1));
233263
schedule_task(task_user1, get_bsp_cpu());
234264
schedule_task(task_user1_se, get_bsp_cpu());
235265
schedule_task(task_user1_int80, get_bsp_cpu());

0 commit comments

Comments
 (0)