Files
lk/app/tests/mem_tests.c
2022-04-18 00:45:07 -07:00

226 lines
5.7 KiB
C

/*
* Copyright (c) 2014 Travis Geiselbrecht
*
* Use of this source code is governed by a MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <lk/err.h>
#include <arch.h>
#include <arch/ops.h>
#include <lk/console_cmd.h>
#include <platform.h>
#include <lk/debug.h>
#if WITH_KERNEL_VM
#include <kernel/vm.h>
#endif
static void mem_test_fail(void *ptr, uint32_t should, uint32_t is) {
printf("ERROR at %p: should be 0x%x, is 0x%x\n", ptr, should, is);
ptr = (void *)ROUNDDOWN((uintptr_t)ptr, 64);
hexdump(ptr, 128);
}
static status_t do_pattern_test(void *ptr, size_t len, uint32_t pat) {
volatile uint32_t *vbuf32 = ptr;
size_t i;
printf("\tpattern 0x%08x\n", pat);
for (i = 0; i < len / 4; i++) {
vbuf32[i] = pat;
}
for (i = 0; i < len / 4; i++) {
if (vbuf32[i] != pat) {
mem_test_fail((void *)&vbuf32[i], pat, vbuf32[i]);
return ERR_GENERIC;
}
}
return NO_ERROR;
}
static status_t do_moving_inversion_test(void *ptr, size_t len, uint32_t pat) {
volatile uint32_t *vbuf32 = ptr;
size_t i;
printf("\tpattern 0x%08x\n", pat);
/* fill memory */
for (i = 0; i < len / 4; i++) {
vbuf32[i] = pat;
}
/* from the bottom, walk through each cell, inverting the value */
//printf("\t\tbottom up invert\n");
for (i = 0; i < len / 4; i++) {
if (vbuf32[i] != pat) {
mem_test_fail((void *)&vbuf32[i], pat, vbuf32[i]);
return ERR_GENERIC;
}
vbuf32[i] = ~pat;
}
/* repeat, walking from top down */
//printf("\t\ttop down invert\n");
for (i = len / 4; i > 0; i--) {
if (vbuf32[i-1] != ~pat) {
mem_test_fail((void *)&vbuf32[i-1], ~pat, vbuf32[i-1]);
return ERR_GENERIC;
}
vbuf32[i-1] = pat;
}
/* verify that we have the original pattern */
//printf("\t\tfinal test\n");
for (i = 0; i < len / 4; i++) {
if (vbuf32[i] != pat) {
mem_test_fail((void *)&vbuf32[i], pat, vbuf32[i]);
return ERR_GENERIC;
}
}
return NO_ERROR;
}
static void do_mem_tests(void *ptr, size_t len) {
size_t i;
printf("running memory tests on address range [%p..%p]\n",
ptr, (uint8_t *)ptr + len - 1);
/* test 1: simple write address to memory, read back */
printf("test 1: simple address write, read back\n");
volatile uint32_t *vbuf32 = ptr;
for (i = 0; i < len / 4; i++) {
vbuf32[i] = i;
}
for (i = 0; i < len / 4; i++) {
if (vbuf32[i] != i) {
mem_test_fail((void *)&vbuf32[i], i, vbuf32[i]);
goto out;
}
}
/* test 2: write various patterns, read back */
printf("test 2: write patterns, read back\n");
static const uint32_t pat[] = {
0x0, 0xffffffff,
0xaaaaaaaa, 0x55555555,
};
for (size_t p = 0; p < countof(pat); p++) {
if (do_pattern_test(ptr, len, pat[p]) < 0)
goto out;
}
// shift bits through 32bit word
for (uint32_t p = 1; p != 0; p <<= 1) {
if (do_pattern_test(ptr, len, p) < 0)
goto out;
}
// shift bits through 16bit word, invert top of 32bit
for (uint16_t p = 1; p != 0; p <<= 1) {
if (do_pattern_test(ptr, len, ((~p) << 16) | p) < 0)
goto out;
}
/* test 3: moving inversion, patterns */
printf("test 3: moving inversions with patterns\n");
for (size_t p = 0; p < countof(pat); p++) {
if (do_moving_inversion_test(ptr, len, pat[p]) < 0)
goto out;
}
// shift bits through 32bit word
for (uint32_t p = 1; p != 0; p <<= 1) {
if (do_moving_inversion_test(ptr, len, p) < 0)
goto out;
}
// shift bits through 16bit word, invert top of 32bit
for (uint16_t p = 1; p != 0; p <<= 1) {
if (do_moving_inversion_test(ptr, len, ((~p) << 16) | p) < 0)
goto out;
}
out:
printf("done with tests\n");
}
static int mem_test(int argc, const console_cmd_args *argv) {
if (argc < 2) {
printf("not enough arguments\n");
usage:
printf("usage: %s <length>\n", argv[0].str);
printf("usage: %s <base> <length>\n", argv[0].str);
return -1;
}
if (argc == 2) {
void *ptr;
size_t len = argv[1].u;
#if WITH_KERNEL_VM
/* rounding up len to the next page */
len = PAGE_ALIGN(len);
if (len == 0) {
printf("invalid length\n");
return -1;
}
/* allocate a region to test in */
status_t err = vmm_alloc_contiguous(vmm_get_kernel_aspace(), "memtest", len, &ptr, 0, 0, ARCH_MMU_FLAG_UNCACHED);
if (err < 0) {
printf("error %d allocating test region\n", err);
return -1;
}
paddr_t pa;
pa = vaddr_to_paddr(ptr);
printf("physical address 0x%lx\n", pa);
#else
/* allocate from the heap */
ptr = malloc(len);
if (!ptr ) {
printf("error allocating test area from heap\n");
return -1;
}
#endif
printf("got buffer at %p of length %#zx\n", ptr, len);
/* run the tests */
do_mem_tests(ptr, len);
#if WITH_KERNEL_VM
// XXX free memory region here
printf("NOTE: leaked memory\n");
#else
free(ptr);
#endif
} else if (argc == 3) {
void *ptr = argv[1].p;
size_t len = argv[2].u;
/* run the tests */
do_mem_tests(ptr, len);
} else {
goto usage;
}
return 0;
}
STATIC_COMMAND_START
STATIC_COMMAND("mem_test", "test memory", &mem_test)
STATIC_COMMAND_END(mem_tests);