import RT-Thread@9217865c without bsp, libcpu and components/net
This commit is contained in:
commit
e2376a3709
1414 changed files with 390370 additions and 0 deletions
17
examples/utest/testcases/mm/Kconfig
Normal file
17
examples/utest/testcases/mm/Kconfig
Normal file
|
@ -0,0 +1,17 @@
|
|||
menu "Memory Management Subsytem Testcase"
|
||||
|
||||
config UTEST_MM_API_TC
|
||||
bool "Enable Utest for MM API"
|
||||
default n
|
||||
help
|
||||
The test covers the Memory Management APIs under the
|
||||
`components/mm` and `libcpu/[mmu.*|tlb.*|cache.*]`
|
||||
|
||||
config UTEST_MM_LWP_TC
|
||||
bool "Enable Utest for MM API in lwp"
|
||||
default n
|
||||
help
|
||||
The test covers the Memory Management APIs under the
|
||||
`components/lwp`.
|
||||
|
||||
endmenu
|
16
examples/utest/testcases/mm/SConscript
Normal file
16
examples/utest/testcases/mm/SConscript
Normal file
|
@ -0,0 +1,16 @@
|
|||
Import('rtconfig')
|
||||
from building import *
|
||||
|
||||
cwd = GetCurrentDir()
|
||||
src = []
|
||||
CPPPATH = [cwd]
|
||||
|
||||
if GetDepend(['UTEST_MM_API_TC']):
|
||||
src += ['mm_api_tc.c', 'mm_libcpu_tc.c']
|
||||
|
||||
if GetDepend(['UTEST_MM_LWP_TC']):
|
||||
src += ['mm_lwp_tc.c']
|
||||
|
||||
group = DefineGroup('utestcases', src, depend = ['RT_USING_UTESTCASES'], CPPPATH = CPPPATH)
|
||||
|
||||
Return('group')
|
65
examples/utest/testcases/mm/common.h
Normal file
65
examples/utest/testcases/mm/common.h
Normal file
|
@ -0,0 +1,65 @@
|
|||
/*
|
||||
* Copyright (c) 2006-2023, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2023-03-20 WangXiaoyao Complete testcase for mm_aspace.c
|
||||
*/
|
||||
#ifndef __TEST_MM_COMMON_H__
|
||||
#define __TEST_MM_COMMON_H__
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <utest.h>
|
||||
|
||||
#include <board.h>
|
||||
#include <rtthread.h>
|
||||
#include <rthw.h>
|
||||
#include <lwp_arch.h>
|
||||
#include <mmu.h>
|
||||
#include <tlb.h>
|
||||
|
||||
#include <ioremap.h>
|
||||
#include <mm_aspace.h>
|
||||
#include <mm_flag.h>
|
||||
#include <mm_page.h>
|
||||
#include <mm_private.h>
|
||||
|
||||
extern rt_base_t rt_heap_lock(void);
|
||||
extern void rt_heap_unlock(rt_base_t level);
|
||||
|
||||
/**
|
||||
* @brief During the operations, is heap still the same;
|
||||
*/
|
||||
#define CONSIST_HEAP(statement) do { \
|
||||
rt_size_t total, used, max_used; \
|
||||
rt_size_t totala, useda, max_useda; \
|
||||
rt_ubase_t level = rt_heap_lock(); \
|
||||
rt_memory_info(&total, &used, &max_used); \
|
||||
statement; \
|
||||
rt_memory_info(&totala, &useda, &max_useda); \
|
||||
rt_heap_unlock(level); \
|
||||
uassert_true(total == totala); \
|
||||
uassert_true(used == useda); \
|
||||
uassert_true(max_used == max_useda); \
|
||||
} while (0)
|
||||
|
||||
rt_inline int memtest(volatile char *buf, int value, size_t buf_sz)
|
||||
{
|
||||
int ret = 0;
|
||||
for (size_t i = 0; i < buf_sz; i++)
|
||||
{
|
||||
if (buf[i] != value)
|
||||
{
|
||||
ret = -1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
#endif /* __TEST_MM_COMMON_H__ */
|
113
examples/utest/testcases/mm/mm_api_tc.c
Normal file
113
examples/utest/testcases/mm/mm_api_tc.c
Normal file
|
@ -0,0 +1,113 @@
|
|||
/*
|
||||
* Copyright (c) 2006-2023, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2022-12-14 WangXiaoyao the first version
|
||||
* 2023-03-20 WangXiaoyao Format & add more testcases for API under mm_aspace.h
|
||||
*/
|
||||
#include "common.h"
|
||||
|
||||
/**
|
||||
* @brief Testing all APIs under components/mm
|
||||
*/
|
||||
|
||||
void ioremap_tc(void);
|
||||
void flag_tc(void);
|
||||
|
||||
#ifdef STANDALONE_TC
|
||||
#define TC_ASSERT(expr) \
|
||||
((expr) \
|
||||
? 0 \
|
||||
: rt_kprintf("AssertFault(%d): %s\n", __LINE__, RT_STRINGIFY(expr)))
|
||||
#else
|
||||
#define TC_ASSERT(expr) uassert_true(expr)
|
||||
#endif
|
||||
|
||||
static rt_err_t utest_tc_init(void)
|
||||
{
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static rt_err_t utest_tc_cleanup(void)
|
||||
{
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
#include "test_aspace_api.h"
|
||||
|
||||
static void testcase(void)
|
||||
{
|
||||
UTEST_UNIT_RUN(aspace_tc);
|
||||
UTEST_UNIT_RUN(ioremap_tc);
|
||||
UTEST_UNIT_RUN(flag_tc);
|
||||
}
|
||||
UTEST_TC_EXPORT(testcase, "testcases.mm.api_tc", utest_tc_init, utest_tc_cleanup, 20);
|
||||
|
||||
void ioremap_tc(void)
|
||||
{
|
||||
const size_t bufsz = 0x1000;
|
||||
void *paddr = (void *)rt_pages_alloc(rt_page_bits(bufsz)) + PV_OFFSET;
|
||||
int *vaddr;
|
||||
vaddr = rt_ioremap_cached(paddr, bufsz);
|
||||
if (vaddr)
|
||||
{
|
||||
TC_ASSERT(*vaddr == *(int *)(paddr - PV_OFFSET));
|
||||
|
||||
rt_iounmap(vaddr);
|
||||
rt_pages_free(paddr - PV_OFFSET, 0);
|
||||
}
|
||||
}
|
||||
|
||||
void flag_tc(void)
|
||||
{
|
||||
size_t flags;
|
||||
|
||||
flags = MMF_CREATE(MMF_MAP_FIXED, 0x4000);
|
||||
TC_ASSERT(MMF_GET_CNTL(flags) == (MMF_MAP_FIXED | MMF_REQUEST_ALIGN));
|
||||
TC_ASSERT((1 << MMF_GET_ALIGN(flags)) == 0x4000);
|
||||
|
||||
flags = MMF_CREATE(MMF_MAP_FIXED, 0);
|
||||
TC_ASSERT(MMF_GET_CNTL(flags) == MMF_MAP_FIXED);
|
||||
TC_ASSERT(MMF_GET_ALIGN(flags) == 0);
|
||||
}
|
||||
|
||||
#if 0
|
||||
|
||||
#define BUF_SIZE (4ul << 20)
|
||||
static char ALIGN(BUF_SIZE) buf[BUF_SIZE];
|
||||
|
||||
void buddy_tc(void)
|
||||
{
|
||||
size_t total, free;
|
||||
rt_page_get_info(&total, &free);
|
||||
|
||||
rt_region_t region = {
|
||||
.start = (size_t)buf,
|
||||
.end = (size_t)buf + BUF_SIZE,
|
||||
};
|
||||
|
||||
size_t new_total, new_free;
|
||||
rt_page_install(region);
|
||||
rt_page_get_info(&new_total, &new_free);
|
||||
TC_ASSERT(new_total - total == (BUF_SIZE >> ARCH_PAGE_SHIFT));
|
||||
TC_ASSERT(new_free > free);
|
||||
}
|
||||
|
||||
void mmu_page_tc()
|
||||
{
|
||||
mm_aspace_t aspace = ASPACE_NEW();
|
||||
size_t total, free;
|
||||
rt_page_get_info(&total, &free);
|
||||
rt_hw_mmu_map(aspace, (void *)0x3fffffffff, 0, ARCH_PAGE_SIZE,
|
||||
MMU_MAP_K_RWCB);
|
||||
rt_hw_mmu_unmap(aspace, (void *)0x3fffffffff, ARCH_PAGE_SIZE);
|
||||
|
||||
size_t new_total, new_free;
|
||||
rt_page_get_info(&new_total, &new_free);
|
||||
TC_ASSERT(new_free == free);
|
||||
mm_aspace_delete(aspace);
|
||||
}
|
||||
#endif
|
16
examples/utest/testcases/mm/mm_libcpu_tc.c
Normal file
16
examples/utest/testcases/mm/mm_libcpu_tc.c
Normal file
|
@ -0,0 +1,16 @@
|
|||
/*
|
||||
* Copyright (c) 2006-2023, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2023-03-17 WangXiaoyao cache API unit test
|
||||
*/
|
||||
#include <rtthread.h>
|
||||
|
||||
#ifdef ARCH_RISCV64
|
||||
#include "test_cache_rv64.h"
|
||||
#elif defined(ARCH_ARMV8)
|
||||
#include "test_cache_aarch64.h"
|
||||
#endif
|
116
examples/utest/testcases/mm/mm_lwp_tc.c
Normal file
116
examples/utest/testcases/mm/mm_lwp_tc.c
Normal file
|
@ -0,0 +1,116 @@
|
|||
/*
|
||||
* Copyright (c) 2006-2023, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2023-03-27 WangXiaoyao testcase for lwp
|
||||
*/
|
||||
|
||||
#include "common.h"
|
||||
#include <lwp.h>
|
||||
#include "lwp_arch.h"
|
||||
#include "lwp_user_mm.h"
|
||||
#include "mm_aspace.h"
|
||||
#include "mmu.h"
|
||||
|
||||
/**
|
||||
* @brief user map API
|
||||
* rt_varea_t lwp_map_user_varea(struct rt_lwp *lwp, void *map_va, size_t map_size);
|
||||
* rt_varea_t lwp_map_user_varea_ext(struct rt_lwp *lwp, void *map_va, size_t map_size, size_t flags);
|
||||
*/
|
||||
#if 1 /* make it clear to identify the block :) */
|
||||
/* for testing on _aspace_traverse */
|
||||
static void *_prev_end;
|
||||
static size_t _count;
|
||||
static int _test_increase(rt_varea_t varea, void *param)
|
||||
{
|
||||
uassert_true(varea->start >= _prev_end);
|
||||
_prev_end = varea->start + varea->size;
|
||||
_count += 1;
|
||||
return 0;
|
||||
}
|
||||
#define TEST_VAREA_INSERT(statement, aspace) do {\
|
||||
size_t _prev_count; \
|
||||
_count = 0; \
|
||||
_prev_end = 0; \
|
||||
rt_aspace_traversal((aspace), _test_increase, NULL);\
|
||||
_prev_count = _count; \
|
||||
statement; \
|
||||
_count = 0; \
|
||||
_prev_end = 0; \
|
||||
rt_aspace_traversal((aspace), _test_increase, NULL);\
|
||||
uassert_true(_prev_count + 1 == _count); \
|
||||
} while (0)
|
||||
#endif
|
||||
|
||||
static void test_user_map_varea(void)
|
||||
{
|
||||
const size_t buf_sz = ARCH_PAGE_SIZE * 4;
|
||||
struct rt_lwp *lwp;
|
||||
rt_varea_t varea;
|
||||
lwp = lwp_new();
|
||||
|
||||
uassert_true(!!lwp);
|
||||
uassert_true(!lwp_user_space_init(lwp, 0));
|
||||
|
||||
TEST_VAREA_INSERT(
|
||||
varea = lwp_map_user_varea(lwp, 0, buf_sz),
|
||||
lwp->aspace);
|
||||
uassert_true(!!varea);
|
||||
uassert_true(varea->attr == (MMU_MAP_U_RWCB));
|
||||
uassert_true(varea->size == buf_sz);
|
||||
uassert_true(varea->aspace == lwp->aspace);
|
||||
uassert_true(varea->flag == 0);
|
||||
uassert_true(varea->start != 0);
|
||||
uassert_true(varea->start >= (void *)USER_VADDR_START && varea->start < (void *)USER_VADDR_TOP);
|
||||
|
||||
uassert_true(!lwp_ref_dec(lwp));
|
||||
}
|
||||
|
||||
static void test_user_map_varea_ext(void)
|
||||
{
|
||||
const size_t buf_sz = ARCH_PAGE_SIZE * 4;
|
||||
struct rt_lwp *lwp;
|
||||
rt_varea_t varea;
|
||||
lwp = lwp_new();
|
||||
|
||||
uassert_true(!!lwp);
|
||||
uassert_true(!lwp_user_space_init(lwp, 0));
|
||||
|
||||
TEST_VAREA_INSERT(
|
||||
varea = lwp_map_user_varea_ext(lwp, 0, buf_sz, LWP_MAP_FLAG_NOCACHE),
|
||||
lwp->aspace);
|
||||
uassert_true(!!varea);
|
||||
uassert_true(varea->attr == (MMU_MAP_U_RW));
|
||||
uassert_true(varea->size == buf_sz);
|
||||
uassert_true(varea->aspace == lwp->aspace);
|
||||
uassert_true(varea->flag == 0);
|
||||
uassert_true(varea->start != 0);
|
||||
uassert_true(varea->start >= (void *)USER_VADDR_START && varea->start < (void *)USER_VADDR_TOP);
|
||||
|
||||
uassert_true(!lwp_ref_dec(lwp));
|
||||
}
|
||||
|
||||
static void user_map_varea_tc(void)
|
||||
{
|
||||
CONSIST_HEAP(test_user_map_varea());
|
||||
CONSIST_HEAP(test_user_map_varea_ext());
|
||||
}
|
||||
|
||||
static rt_err_t utest_tc_init(void)
|
||||
{
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static rt_err_t utest_tc_cleanup(void)
|
||||
{
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static void testcase(void)
|
||||
{
|
||||
UTEST_UNIT_RUN(user_map_varea_tc);
|
||||
}
|
||||
UTEST_TC_EXPORT(testcase, "testcases.lwp.mm_tc", utest_tc_init, utest_tc_cleanup, 20);
|
37
examples/utest/testcases/mm/semaphore.h
Normal file
37
examples/utest/testcases/mm/semaphore.h
Normal file
|
@ -0,0 +1,37 @@
|
|||
/*
|
||||
* Copyright (c) 2006-2023, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2023-03-24 WangXiaoyao Complete testcase for synchronization
|
||||
*/
|
||||
#ifndef __SEMAPHORE_H__
|
||||
#define __SEMAPHORE_H__
|
||||
|
||||
#include <stdatomic.h>
|
||||
|
||||
typedef struct {
|
||||
atomic_int count;
|
||||
} semaphore_t;
|
||||
|
||||
void semaphore_init(semaphore_t *sem, int count)
|
||||
{
|
||||
atomic_init(&sem->count, count);
|
||||
}
|
||||
|
||||
void semaphore_wait(semaphore_t *sem)
|
||||
{
|
||||
int count;
|
||||
do {
|
||||
count = atomic_load(&sem->count);
|
||||
} while (count == 0 || !atomic_compare_exchange_weak(&sem->count, &count, count - 1));
|
||||
}
|
||||
|
||||
void semaphore_signal(semaphore_t *sem)
|
||||
{
|
||||
atomic_fetch_add(&sem->count, 1);
|
||||
}
|
||||
|
||||
#endif /* __SEMAPHORE_H__ */
|
287
examples/utest/testcases/mm/test_aspace_api.h
Normal file
287
examples/utest/testcases/mm/test_aspace_api.h
Normal file
|
@ -0,0 +1,287 @@
|
|||
/*
|
||||
* Copyright (c) 2006-2023, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2023-03-20 WangXiaoyao Complete testcase for mm_aspace.c
|
||||
*/
|
||||
#ifndef __TEST_ASPACE_API_H__
|
||||
#define __TEST_ASPACE_API_H__
|
||||
|
||||
#include "common.h"
|
||||
#include "lwp_arch.h"
|
||||
#include "test_aspace_api_internal.h"
|
||||
#include "test_synchronization.h"
|
||||
|
||||
/**
|
||||
* @brief API for aspace create/destroy
|
||||
*
|
||||
* rt_aspace_t rt_aspace_create(void *start, rt_size_t length, void *pgtbl);
|
||||
* rt_aspace_t rt_aspace_init(rt_aspace_t aspace, void *start, rt_size_t length, void *pgtbl);
|
||||
* void rt_aspace_delete(rt_aspace_t aspace);
|
||||
* void rt_aspace_detach(rt_aspace_t aspace);
|
||||
*
|
||||
* the init & detach is covered by create & detach
|
||||
*/
|
||||
|
||||
static void aspace_create_tc(void)
|
||||
{
|
||||
/* test robustness, detect failure and recover status of overall system */
|
||||
rt_aspace_t aspace;
|
||||
|
||||
CONSIST_HEAP(aspace = rt_aspace_create((void *)(0 - 0x1000), 0x1000, NULL));
|
||||
uassert_true(!aspace);
|
||||
}
|
||||
|
||||
#if 1 /* make it clear to identify the block :) */
|
||||
/* for testing on _aspace_traverse */
|
||||
static void *_prev_end;
|
||||
static size_t _count;
|
||||
static int _test_increase(rt_varea_t varea, void *param)
|
||||
{
|
||||
uassert_true(varea->start >= _prev_end);
|
||||
_prev_end = varea->start + varea->size;
|
||||
_count += 1;
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
static void aspace_delete_tc(void)
|
||||
{
|
||||
/**
|
||||
* @brief Requirements: delete should recycle all types of vareas properly inside
|
||||
* and release the resource allocated for it
|
||||
*/
|
||||
rt_aspace_t aspace;
|
||||
struct rt_mm_va_hint hint = {.flags = 0,
|
||||
.map_size = 0x1000,
|
||||
.prefer = 0};
|
||||
struct rt_varea varea_phy;
|
||||
struct rt_varea varea_mobj;
|
||||
void *pgtbl;
|
||||
void *vaddr;
|
||||
|
||||
/* compatible to armv7a */
|
||||
pgtbl = rt_pages_alloc(2);
|
||||
uassert_true(!!pgtbl); /* page must be usable */
|
||||
rt_memset(pgtbl, 0, ARCH_PAGE_SIZE);
|
||||
|
||||
CONSIST_HEAP({
|
||||
aspace = rt_aspace_create((void *)USER_VADDR_START, USER_VADDR_TOP - USER_VADDR_START, pgtbl);
|
||||
uassert_true(!!aspace);
|
||||
|
||||
/* insert 4 types of vareas into this aspace */
|
||||
hint.limit_start = aspace->start;
|
||||
hint.limit_range_size = aspace->size;
|
||||
uassert_true(!rt_aspace_map_phy(aspace, &hint, MMU_MAP_K_RWCB, 0, &vaddr));
|
||||
uassert_true(!rt_aspace_map_phy_static(aspace, &varea_phy, &hint, MMU_MAP_K_RWCB, 0, &vaddr));
|
||||
uassert_true(!rt_aspace_map(aspace, &vaddr, 0x1000, MMU_MAP_K_RWCB, 0, &rt_mm_dummy_mapper, 0));
|
||||
uassert_true(!rt_aspace_map_static(aspace, &varea_mobj, &vaddr, 0x1000, MMU_MAP_K_RWCB, 0, &rt_mm_dummy_mapper, 0));
|
||||
|
||||
/* for testing on _aspace_traverse */
|
||||
_count = 0;
|
||||
_prev_end = 0;
|
||||
uassert_true(!rt_aspace_traversal(aspace, _test_increase, 0));
|
||||
/* ensure the mapping is done */
|
||||
uassert_true(_count == 4);
|
||||
|
||||
rt_aspace_delete(aspace);
|
||||
|
||||
uassert_true(rt_pages_free(pgtbl, 2) == 1); /* page free must success */
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Memory Map on Virtual Address Space to Mappable Object
|
||||
* int rt_aspace_map(rt_aspace_t aspace, void **addr, rt_size_t length, rt_size_t attr,
|
||||
* mm_flag_t flags, rt_mem_obj_t mem_obj, rt_size_t offset);
|
||||
* int rt_aspace_map_static(rt_aspace_t aspace, rt_varea_t varea, void **addr,
|
||||
* rt_size_t length, rt_size_t attr, mm_flag_t flags,
|
||||
* rt_mem_obj_t mem_obj, rt_size_t offset);
|
||||
*/
|
||||
static void aspace_map_tc(void)
|
||||
{
|
||||
/**
|
||||
* @brief Requirement:
|
||||
* Robustness, filter out invalid input
|
||||
*/
|
||||
void *vaddr = RT_NULL;
|
||||
uassert_true(rt_aspace_map(0, &vaddr, 0x1000, MMU_MAP_K_RWCB, 0, &rt_mm_dummy_mapper, 0));
|
||||
uassert_true(vaddr == RT_NULL);
|
||||
|
||||
vaddr = (void *)USER_VADDR_START;
|
||||
uassert_true(rt_aspace_map(&rt_kernel_space, &vaddr, 0x1000, MMU_MAP_K_RWCB, 0, &rt_mm_dummy_mapper, 0));
|
||||
uassert_true(vaddr == RT_NULL);
|
||||
|
||||
uassert_true(rt_aspace_map(&rt_kernel_space, &vaddr, 0x1000, MMU_MAP_K_RWCB, -1, &rt_mm_dummy_mapper, 0));
|
||||
uassert_true(vaddr == RT_NULL);
|
||||
|
||||
/**
|
||||
* @brief Requirement:
|
||||
* in _rt_aspace_map:_varea_install
|
||||
* not covering an existed varea if a named mapping is mandatory
|
||||
*/
|
||||
vaddr = (void *)((rt_ubase_t)aspace_map_tc & ~ARCH_PAGE_MASK);
|
||||
CONSIST_HEAP(
|
||||
uassert_true(
|
||||
rt_aspace_map(&rt_kernel_space, &vaddr, 0x1000, MMU_MAP_K_RWCB, MMF_MAP_FIXED, &rt_mm_dummy_mapper, 0)));
|
||||
uassert_true(vaddr == RT_NULL);
|
||||
|
||||
/**
|
||||
* @brief Requirement:
|
||||
* in _rt_aspace_map:_varea_install:_find_free
|
||||
* verify that this routine can choose a free region with specified size
|
||||
* and specified alignment requirement
|
||||
*/
|
||||
#define ALIGN_REQ (0x04000000)
|
||||
CONSIST_HEAP({
|
||||
uassert_true(!rt_aspace_map(&rt_kernel_space, &vaddr, 0x1000, MMU_MAP_K_RWCB, MMF_CREATE(0, ALIGN_REQ), &rt_mm_dummy_mapper, 0));
|
||||
uassert_true(!((rt_ubase_t)vaddr & (ALIGN_REQ - 1)));
|
||||
rt_aspace_unmap(&rt_kernel_space, vaddr);
|
||||
});
|
||||
|
||||
/* test internal APIs */
|
||||
test_find_free();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Page frames mapping to varea
|
||||
* complete the page table on specified varea, and handle tlb maintenance
|
||||
* There are 2 variants of this API
|
||||
*
|
||||
* int rt_varea_map_page(rt_varea_t varea, void *vaddr, void *page);
|
||||
* int rt_varea_map_range(rt_varea_t varea, void *vaddr, void *paddr, rt_size_t length);
|
||||
*/
|
||||
|
||||
static rt_varea_t _create_varea(const size_t size)
|
||||
{
|
||||
rt_varea_t varea;
|
||||
void *vaddr = rt_ioremap_start;
|
||||
|
||||
varea = rt_malloc(sizeof(*varea));
|
||||
uassert_true(!!varea);
|
||||
uassert_true(!rt_aspace_map_static(&rt_kernel_space, varea, &vaddr, size, MMU_MAP_K_RWCB, 0, &rt_mm_dummy_mapper, 0));
|
||||
varea->flag &= ~MMF_STATIC_ALLOC;
|
||||
uassert_true(!!vaddr);
|
||||
return varea;
|
||||
}
|
||||
|
||||
static void test_varea_map_page(void)
|
||||
{
|
||||
/**
|
||||
* @brief rt_varea_map_page
|
||||
* Requirements: complete the page table entry
|
||||
*/
|
||||
const size_t buf_sz = 4 * ARCH_PAGE_SIZE;
|
||||
rt_varea_t varea = _create_varea(buf_sz);
|
||||
for (size_t i = 0; i < buf_sz; i += ARCH_PAGE_SIZE)
|
||||
{
|
||||
void *page = rt_pages_alloc(0);
|
||||
uassert_true(!!page);
|
||||
uassert_true(!rt_varea_map_page(varea, varea->start + i, page));
|
||||
|
||||
/* let page manager handle the free of page */
|
||||
rt_varea_pgmgr_insert(varea, page);
|
||||
uassert_true(rt_kmem_v2p(varea->start + i) == (page + PV_OFFSET));
|
||||
}
|
||||
|
||||
uassert_true(!rt_aspace_unmap(&rt_kernel_space, varea->start));
|
||||
}
|
||||
|
||||
static void test_varea_map_range(void)
|
||||
{
|
||||
/**
|
||||
* @brief rt_varea_map_range
|
||||
* Requirements: complete the page table entry
|
||||
*/
|
||||
const size_t buf_sz = 4 * ARCH_PAGE_SIZE;
|
||||
rt_varea_t varea = _create_varea(buf_sz);
|
||||
void *page = rt_pages_alloc(rt_page_bits(buf_sz));
|
||||
uassert_true(!!page);
|
||||
uassert_true(!rt_varea_map_range(varea, varea->start, page + PV_OFFSET, buf_sz));
|
||||
for (size_t i = 0; i < buf_sz; i += ARCH_PAGE_SIZE)
|
||||
{
|
||||
uassert_true(rt_kmem_v2p(varea->start + i) == (page + i + PV_OFFSET));
|
||||
}
|
||||
|
||||
uassert_true(rt_pages_free(page, rt_page_bits(buf_sz)));
|
||||
uassert_true(!rt_aspace_unmap(&rt_kernel_space, varea->start));
|
||||
}
|
||||
|
||||
static void varea_map_tc(void)
|
||||
{
|
||||
CONSIST_HEAP(test_varea_map_page());
|
||||
CONSIST_HEAP(test_varea_map_range());
|
||||
}
|
||||
|
||||
static void aspace_traversal_tc(void)
|
||||
{
|
||||
/**
|
||||
* @brief Requirement
|
||||
* Iterate over each varea in the kernel space
|
||||
*/
|
||||
CONSIST_HEAP(aspace_delete_tc());
|
||||
uassert_true(4 == _count);
|
||||
}
|
||||
|
||||
#ifdef ARCH_ARMV8
|
||||
static void aspace_control_tc(void)
|
||||
{
|
||||
/* this case is designed only for one page size */
|
||||
const size_t buf_sz = ARCH_PAGE_SIZE;
|
||||
void *vaddr = RT_NULL;
|
||||
volatile char *remap_nocache;
|
||||
int platform_cache_probe;
|
||||
uassert_true(!rt_aspace_map(&rt_kernel_space, &vaddr, 0x1000, MMU_MAP_K_RWCB, MMF_PREFETCH, &rt_mm_dummy_mapper, 0));
|
||||
uassert_true(!!vaddr);
|
||||
|
||||
/* map non-cacheable region to verify cache */
|
||||
remap_nocache = rt_ioremap(rt_kmem_v2p(vaddr), buf_sz);
|
||||
uassert_true(!!remap_nocache);
|
||||
|
||||
/* pre probing */
|
||||
rt_memset(vaddr, 0xba, buf_sz);
|
||||
/* no need to sync transaction on same core */
|
||||
platform_cache_probe = memtest(remap_nocache, 0xab, buf_sz);
|
||||
|
||||
if (!platform_cache_probe)
|
||||
{
|
||||
LOG_I("Cannot distinguish cache attribution on current platform");
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_I("Ready to verify attribution of cached & non-cacheable");
|
||||
}
|
||||
|
||||
/* verify cache */
|
||||
uassert_true(!rt_aspace_control(&rt_kernel_space, vaddr, MMU_CNTL_NONCACHE));
|
||||
rt_memset(vaddr, 0, buf_sz);
|
||||
uassert_true(!memtest(remap_nocache, 0, buf_sz));
|
||||
|
||||
/* another option as MMU_CNTL_CACHE */
|
||||
uassert_true(!rt_aspace_control(&rt_kernel_space, vaddr, MMU_CNTL_CACHE));
|
||||
|
||||
rt_iounmap(remap_nocache);
|
||||
uassert_true(!rt_aspace_unmap(&rt_kernel_space, vaddr));
|
||||
}
|
||||
#endif
|
||||
|
||||
static void aspace_tc(void)
|
||||
{
|
||||
UTEST_UNIT_RUN(aspace_create_tc);
|
||||
UTEST_UNIT_RUN(aspace_delete_tc);
|
||||
UTEST_UNIT_RUN(aspace_map_tc);
|
||||
UTEST_UNIT_RUN(aspace_traversal_tc);
|
||||
#ifdef ARCH_ARMV8
|
||||
UTEST_UNIT_RUN(aspace_control_tc);
|
||||
#endif
|
||||
UTEST_UNIT_RUN(varea_map_tc);
|
||||
|
||||
/* functionality */
|
||||
UTEST_UNIT_RUN(synchronization_tc);
|
||||
return ;
|
||||
}
|
||||
|
||||
#endif /* __TEST_ASPACE_API_H__ */
|
80
examples/utest/testcases/mm/test_aspace_api_internal.h
Normal file
80
examples/utest/testcases/mm/test_aspace_api_internal.h
Normal file
|
@ -0,0 +1,80 @@
|
|||
/*
|
||||
* Copyright (c) 2006-2023, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2023-03-23 WangXiaoyao Complete testcase for internal APIs
|
||||
*/
|
||||
#ifndef __TEST_ASPACE_API_INTERNAL_H__
|
||||
#define __TEST_ASPACE_API_INTERNAL_H__
|
||||
|
||||
#include "common.h"
|
||||
#include "mmu.h"
|
||||
#include "test_bst_adpt.h"
|
||||
#include <stddef.h>
|
||||
|
||||
/**
|
||||
* @brief 3 cases for find free:
|
||||
* with prefer & MAP_FIXED
|
||||
* with prefer
|
||||
* without prefer
|
||||
*
|
||||
* the requirement of find free:
|
||||
* it will return a subset in address space that is free
|
||||
* the subset contains `length` contiguous elements
|
||||
* the alignment is satisfied
|
||||
*/
|
||||
static void test_find_free(void)
|
||||
{
|
||||
void *top_page = rt_kernel_space.start + rt_kernel_space.size - 0x1000;
|
||||
void *vaddr = top_page;
|
||||
|
||||
CONSIST_HEAP({
|
||||
/* type 1, on success */
|
||||
uassert_true(!rt_aspace_map(&rt_kernel_space, &vaddr, 0x1000, MMU_MAP_K_RWCB, MMF_MAP_FIXED, &rt_mm_dummy_mapper, 0));
|
||||
uassert_true(vaddr == top_page);
|
||||
/* type 1, on failure */
|
||||
uassert_true(rt_aspace_map(&rt_kernel_space, &vaddr, 0x1000, MMU_MAP_K_RWCB, MMF_MAP_FIXED, &rt_mm_dummy_mapper, 0));
|
||||
uassert_true(!vaddr);
|
||||
|
||||
/* type 2, on success */
|
||||
vaddr = top_page;
|
||||
uassert_true(!rt_aspace_map(&rt_kernel_space, &vaddr, 0x1000, MMU_MAP_K_RWCB, 0, &rt_mm_dummy_mapper, 0));
|
||||
uassert_true(vaddr < top_page);
|
||||
uassert_true(!!vaddr);
|
||||
rt_aspace_unmap(&rt_kernel_space, vaddr);
|
||||
/* type 2, on failure */
|
||||
vaddr = rt_kernel_space.start;
|
||||
uassert_true(-RT_ENOSPC == rt_aspace_map(&rt_kernel_space, &vaddr, rt_kernel_space.size - 0x08000000, MMU_MAP_K_RWCB, 0, &rt_mm_dummy_mapper, 0));
|
||||
uassert_true(!vaddr);
|
||||
|
||||
/* type 3, on success is covered by ioremap */
|
||||
/* type 3, on failure */
|
||||
size_t map_size = ARCH_PAGE_SIZE;
|
||||
while (1)
|
||||
{
|
||||
void *va = rt_ioremap(0, map_size);
|
||||
if (va)
|
||||
{
|
||||
uassert_true(1);
|
||||
rt_iounmap(va);
|
||||
map_size <<= 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
uassert_true(1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* free top page */
|
||||
rt_aspace_unmap(&rt_kernel_space, top_page);
|
||||
});
|
||||
|
||||
/* test mm_private.h */
|
||||
CONSIST_HEAP(test_bst_adpt());
|
||||
}
|
||||
|
||||
#endif /* __TEST_ASPACE_API_INTERNAL_H__ */
|
107
examples/utest/testcases/mm/test_bst_adpt.h
Normal file
107
examples/utest/testcases/mm/test_bst_adpt.h
Normal file
|
@ -0,0 +1,107 @@
|
|||
/*
|
||||
* Copyright (c) 2006-2023, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2023-03-23 WangXiaoyao Complete testcase for internal APIs
|
||||
*/
|
||||
#ifndef __TEST_BST_ADPT_H__
|
||||
#define __TEST_BST_ADPT_H__
|
||||
|
||||
#include "common.h"
|
||||
#include "lwp_arch.h"
|
||||
|
||||
#ifdef RT_USING_SMART
|
||||
#include "lwp_user_mm.h"
|
||||
#include "mm_aspace.h"
|
||||
#include "mm_flag.h"
|
||||
#include <mm_private.h>
|
||||
#include <lwp_pid.h>
|
||||
|
||||
void test_bst_adpt(void)
|
||||
{
|
||||
size_t flags = MMF_MAP_FIXED;
|
||||
void *target_va = (void *)USER_VADDR_START + 0x3000;
|
||||
size_t map_size = 0x1000;
|
||||
void *prev_va = target_va - map_size;
|
||||
void *next_va = target_va + map_size + 1;
|
||||
struct rt_lwp *lwp;
|
||||
rt_aspace_t aspace;
|
||||
rt_mem_obj_t mem_obj;
|
||||
|
||||
/* create aspace by lwp */
|
||||
lwp = lwp_new();
|
||||
uassert_true(!!lwp);
|
||||
uassert_true(!lwp_user_space_init(lwp, 0));
|
||||
aspace = lwp->aspace;
|
||||
mem_obj = &lwp->lwp_obj->mem_obj;
|
||||
uassert_true(!!aspace);
|
||||
uassert_true(!!mem_obj);
|
||||
|
||||
/* _aspace_bst_search not cover */
|
||||
uassert_true(!_aspace_bst_search(aspace, target_va)); // ret == NULL
|
||||
|
||||
uassert_true(
|
||||
!rt_aspace_map(aspace, &target_va, map_size, MMU_MAP_K_RWCB, flags, mem_obj, 0));
|
||||
/* 2 wrappers */
|
||||
uassert_true(
|
||||
!rt_aspace_map(aspace, &prev_va, map_size - 1, MMU_MAP_K_RWCB, flags, mem_obj, 0));
|
||||
uassert_true(
|
||||
!rt_aspace_map(aspace, &next_va, map_size - 1, MMU_MAP_K_RWCB, flags, mem_obj, 0));
|
||||
|
||||
/* _aspace_bst_search */
|
||||
uassert_true(!!_aspace_bst_search(aspace, target_va));
|
||||
uassert_true(!_aspace_bst_search(aspace, target_va + map_size));
|
||||
uassert_true(!_aspace_bst_search(aspace, target_va - 1));
|
||||
|
||||
/**
|
||||
* @brief _aspace_bst_search_exceed
|
||||
* for given map [start, end]
|
||||
*/
|
||||
rt_varea_t find;
|
||||
find = _aspace_bst_search_exceed(aspace, target_va);
|
||||
uassert_true(!!find);
|
||||
uassert_true(find->start == target_va);
|
||||
|
||||
rt_varea_t last = ASPACE_VAREA_LAST(aspace);
|
||||
find = _aspace_bst_search_exceed(aspace, last->start + 1);
|
||||
uassert_true(!find);
|
||||
|
||||
/**
|
||||
* @brief _aspace_bst_search_overlap
|
||||
* for given map [start, end], five types of overlapping
|
||||
*/
|
||||
/* 1. all below */
|
||||
struct _mm_range range = {.start = prev_va - 2, .end = prev_va - 1};
|
||||
find = _aspace_bst_search_overlap(aspace, range);
|
||||
uassert_true(!find);
|
||||
/* 2. start below */
|
||||
range.end = prev_va;
|
||||
find = _aspace_bst_search_overlap(aspace, range);
|
||||
uassert_true(!!find);
|
||||
uassert_true(find->start == prev_va);
|
||||
/* 3. all wrapped */
|
||||
range.start = prev_va;
|
||||
range.end = prev_va + 1;
|
||||
find = _aspace_bst_search_overlap(aspace, range);
|
||||
uassert_true(!!find);
|
||||
uassert_true(find->start == prev_va);
|
||||
/* 4. end exceed */
|
||||
range.start = next_va;
|
||||
range.end = next_va + map_size + 1;
|
||||
find = _aspace_bst_search_overlap(aspace, range);
|
||||
uassert_true(!!find);
|
||||
uassert_true(find->start == next_va);
|
||||
/* 5. all exceed */
|
||||
range.start = next_va + map_size;
|
||||
find = _aspace_bst_search_overlap(aspace, range);
|
||||
uassert_true(!find);
|
||||
|
||||
lwp_ref_dec(lwp);
|
||||
}
|
||||
|
||||
#endif /* RT_USING_SMART */
|
||||
|
||||
#endif /* __TEST_BST_ADPT_H__ */
|
115
examples/utest/testcases/mm/test_cache_aarch64.h
Normal file
115
examples/utest/testcases/mm/test_cache_aarch64.h
Normal file
|
@ -0,0 +1,115 @@
|
|||
/*
|
||||
* Copyright (c) 2006-2023, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2023-03-17 WangXiaoyao cache API unit test
|
||||
*/
|
||||
#ifndef __TEST_CACHE_AARCH64_H__
|
||||
#define __TEST_CACHE_AARCH64_H__
|
||||
|
||||
#include "common.h"
|
||||
#include <cache.h>
|
||||
|
||||
const char *platform_cache_not_guarantee = "Cannot guarantee cache operation works";
|
||||
|
||||
/**
|
||||
* ==============================================================
|
||||
* TEST FEATURE
|
||||
* API under cache.h
|
||||
*
|
||||
* void rt_hw_icache_invalidate_range(unsigned long start_addr, int size);
|
||||
* void rt_hw_cpu_icache_invalidate(void *addr, rt_size_t size);
|
||||
* void rt_hw_cpu_dcache_clean_and_invalidate(void *addr, rt_size_t size);
|
||||
* ==============================================================
|
||||
*/
|
||||
|
||||
static int _get1_const(void)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int _get1(void)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int _get2(void)
|
||||
{
|
||||
return 2;
|
||||
}
|
||||
|
||||
/* hot patching codes and test if the value can be seen by icache */
|
||||
static void _test_icache_invalidate_range(void)
|
||||
{
|
||||
/* reset _get1 */
|
||||
rt_memcpy(_get1, _get1_const, _get2 - _get1);
|
||||
rt_hw_cpu_dcache_clean(_get1, _get2 - _get1);
|
||||
rt_hw_cpu_icache_invalidate(_get1, _get2 - _get1);
|
||||
uassert_true(1 == _get1());
|
||||
|
||||
/* now copy _get2 to _get1 */
|
||||
rt_memcpy(_get1, _get2, _get2 - _get1);
|
||||
if (1 != _get1())
|
||||
LOG_W(platform_cache_not_guarantee);
|
||||
|
||||
rt_hw_cpu_dcache_clean(_get1, _get2 - _get1);
|
||||
rt_hw_cpu_icache_invalidate(_get1, _get2 - _get1);
|
||||
__asm__ volatile("isb");
|
||||
uassert_true(2 == _get1());
|
||||
LOG_I("%s ok", __func__);
|
||||
}
|
||||
|
||||
/* due to hardware feature of cortex-a, we should done this on 2 separated cpu */
|
||||
static void _test_dcache_clean_and_invalidate(void)
|
||||
{
|
||||
const size_t padding = 1024 * 2;
|
||||
const size_t buf_sz = ARCH_PAGE_SIZE * 2;
|
||||
volatile char *remap_nocache;
|
||||
char *page = rt_pages_alloc(rt_page_bits(buf_sz));
|
||||
uassert_true(!!page);
|
||||
|
||||
rt_memset(page, 0xab, buf_sz);
|
||||
rt_hw_cpu_dcache_invalidate(page, buf_sz);
|
||||
|
||||
int _outdate_flag = 0;
|
||||
if (memtest(page, 0xab, buf_sz))
|
||||
_outdate_flag = 1;
|
||||
|
||||
/* after ioremap, we can access system memory to verify outcome */
|
||||
remap_nocache = rt_ioremap(page + PV_OFFSET, buf_sz);
|
||||
|
||||
rt_hw_cpu_dcache_clean(page + padding, ARCH_PAGE_SIZE);
|
||||
memtest(remap_nocache + padding, 0xab, ARCH_PAGE_SIZE);
|
||||
|
||||
if (!_outdate_flag)
|
||||
LOG_W(platform_cache_not_guarantee);
|
||||
else
|
||||
LOG_I("%s ok", __func__);
|
||||
|
||||
rt_pages_free(page, 0);
|
||||
rt_iounmap(remap_nocache);
|
||||
}
|
||||
|
||||
static rt_err_t utest_tc_init(void)
|
||||
{
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static rt_err_t utest_tc_cleanup(void)
|
||||
{
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static void testcase(void)
|
||||
{
|
||||
/* todo: format API under cache.h first */
|
||||
UTEST_UNIT_RUN(_test_icache_invalidate_range);
|
||||
UTEST_UNIT_RUN(_test_dcache_clean_and_invalidate);
|
||||
}
|
||||
|
||||
UTEST_TC_EXPORT(testcase, "testcases.libcpu.cache", utest_tc_init, utest_tc_cleanup, 10);
|
||||
|
||||
#endif /* __TEST_CACHE_AARCH64_H__ */
|
194
examples/utest/testcases/mm/test_cache_rv64.h
Normal file
194
examples/utest/testcases/mm/test_cache_rv64.h
Normal file
|
@ -0,0 +1,194 @@
|
|||
/*
|
||||
* Copyright (c) 2006-2023, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2023-03-17 WangXiaoyao cache API unit test
|
||||
*/
|
||||
|
||||
#ifndef __TEST_CACHE_RV64_H
|
||||
#define __TEST_CACHE_RV64_H
|
||||
|
||||
#ifdef ARCH_RISCV64
|
||||
#include "riscv_mmu.h"
|
||||
#include <utest.h>
|
||||
#include <cache.h>
|
||||
#include <page.h>
|
||||
#include <mmu.h>
|
||||
#include <ioremap.h>
|
||||
|
||||
/**
|
||||
* ==============================================================
|
||||
* TEST FEATURE
|
||||
* API under cache.h
|
||||
* rt_hw_sync_cache_local
|
||||
*
|
||||
* rt_hw_cpu_dcache_clean
|
||||
* rt_hw_cpu_dcache_invalidate
|
||||
* rt_hw_cpu_dcache_clean_invalidate
|
||||
* rt_hw_cpu_dcache_clean_all
|
||||
* rt_hw_cpu_dcache_invalidate_all // meaningless
|
||||
* rt_hw_cpu_dcache_clean_invalidate_all
|
||||
* rt_hw_cpu_icache_invalidate
|
||||
* rt_hw_cpu_icache_invalidate_all
|
||||
* ==============================================================
|
||||
*/
|
||||
|
||||
/* Ensure the ISA is valid for target ARCHITECTURE */
|
||||
static void _illegal_instr(void)
|
||||
{
|
||||
rt_hw_sync_cache_local(_illegal_instr, 64);
|
||||
rt_hw_cpu_dcache_clean(_illegal_instr, 64);
|
||||
rt_hw_cpu_dcache_invalidate(_illegal_instr, 64);
|
||||
// rt_hw_cpu_dcache_clean_invalidate(_illegal_instr, 64); // C908 ONLY
|
||||
rt_hw_cpu_dcache_clean_all();
|
||||
rt_hw_cpu_dcache_invalidate_all(); // !CAREFUL must be inline
|
||||
// rt_hw_cpu_dcache_clean_invalidate_all(); // C908 ONLY
|
||||
rt_hw_cpu_icache_invalidate(_illegal_instr, 64);
|
||||
rt_hw_cpu_icache_invalidate_all();
|
||||
uassert_true(1);
|
||||
LOG_I("All ok!");
|
||||
}
|
||||
|
||||
static int _get1(void)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int _get2(void)
|
||||
{
|
||||
return 2;
|
||||
}
|
||||
|
||||
/* hot patching codes and test if the value can be seen by icache */
|
||||
static void _test_cache_sync(void)
|
||||
{
|
||||
uassert_true(1 == _get1());
|
||||
rt_memcpy(_get1, _get2, _get2 - _get1);
|
||||
uassert_true(1 == _get1());
|
||||
rt_hw_sync_cache_local(_get1, _get2 - _get1);
|
||||
uassert_true(2 == _get1());
|
||||
LOG_I("%s ok", __func__);
|
||||
}
|
||||
|
||||
/* test clean operation should do and only effect the range specified by writing to a page */
|
||||
static void _test_dcache_clean(void)
|
||||
{
|
||||
const size_t padding = 1024 * 3;
|
||||
const size_t buf_sz = ARCH_PAGE_SIZE * 2;
|
||||
|
||||
char *page = rt_pages_alloc(rt_page_bits(buf_sz));
|
||||
uassert_true(!!page);
|
||||
|
||||
/* after ioremap, we can access system memory to verify outcome */
|
||||
volatile char *remap_nocache = rt_ioremap(page + PV_OFFSET, buf_sz);
|
||||
rt_memset(page, 0xab, buf_sz);
|
||||
rt_hw_cpu_sync();
|
||||
|
||||
int _outdate_flag = 0;
|
||||
for (size_t i = padding; i < ARCH_PAGE_SIZE; i++)
|
||||
{
|
||||
if (remap_nocache[i] != 0xab)
|
||||
{
|
||||
_outdate_flag = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
page[padding - 1] = 0xac;
|
||||
page[padding + ARCH_PAGE_SIZE] = 0xac;
|
||||
rt_hw_cpu_dcache_clean(page + padding, ARCH_PAGE_SIZE);
|
||||
|
||||
/* free some space in dcache to avoid padding data being written back */
|
||||
rt_hw_cpu_dcache_invalidate(page + padding, ARCH_PAGE_SIZE);
|
||||
uassert_true(remap_nocache[padding - 1] != 0xac);
|
||||
uassert_true(remap_nocache[padding + ARCH_PAGE_SIZE] != 0xac);
|
||||
|
||||
int _test_ok = 1;
|
||||
for (size_t i = padding; i < ARCH_PAGE_SIZE; i++)
|
||||
{
|
||||
if (remap_nocache[i] != 0xab)
|
||||
{
|
||||
_test_ok = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
uassert_true(_test_ok);
|
||||
|
||||
if (!_outdate_flag)
|
||||
LOG_W("Cannot guarantee clean works");
|
||||
else
|
||||
LOG_I("%s ok", __func__);
|
||||
|
||||
rt_pages_free(page, 0);
|
||||
rt_iounmap(remap_nocache);
|
||||
}
|
||||
|
||||
/* test clean op should do and only effect the range */
|
||||
static void _test_dcache_invalidate(void)
|
||||
{
|
||||
const size_t padding = 1024 * 3;
|
||||
const size_t buf_sz = ARCH_PAGE_SIZE * 2;
|
||||
|
||||
/* prepare */
|
||||
char *page = rt_pages_alloc(rt_page_bits(buf_sz));
|
||||
uassert_true(!!page);
|
||||
|
||||
volatile char *remap_nocache = rt_ioremap(page + PV_OFFSET, buf_sz);
|
||||
rt_memset(page, 0x0, buf_sz);
|
||||
rt_hw_cpu_sync();
|
||||
|
||||
int _outdate_flag = 0;
|
||||
for (size_t i = padding; i < ARCH_PAGE_SIZE; i++)
|
||||
{
|
||||
remap_nocache[i] = 0xab;
|
||||
rt_hw_cpu_dcache_invalidate((void *)&remap_nocache[i], 1);
|
||||
}
|
||||
|
||||
rt_hw_cpu_dcache_clean_all();
|
||||
|
||||
int _test_ok = 1;
|
||||
for (size_t i = padding; i < ARCH_PAGE_SIZE; i++)
|
||||
{
|
||||
if (remap_nocache[i] == 0xab)
|
||||
{
|
||||
_test_ok = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
uassert_true(_test_ok);
|
||||
|
||||
LOG_I("%s ok", __func__);
|
||||
|
||||
rt_pages_free(page, 0);
|
||||
rt_iounmap(remap_nocache);
|
||||
}
|
||||
|
||||
static rt_err_t utest_tc_init(void)
|
||||
{
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static rt_err_t utest_tc_cleanup(void)
|
||||
{
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static void testcase(void)
|
||||
{
|
||||
UTEST_UNIT_RUN(_illegal_instr);
|
||||
#ifdef BOARD_allwinnerd1s
|
||||
/* thead ISA extension */
|
||||
UTEST_UNIT_RUN(_test_cache_sync);
|
||||
/* part of it is hard to test on simulation machine */
|
||||
UTEST_UNIT_RUN(_test_dcache_clean);
|
||||
UTEST_UNIT_RUN(_test_dcache_invalidate);
|
||||
#endif
|
||||
}
|
||||
|
||||
UTEST_TC_EXPORT(testcase, "testcases.libcpu.cache", utest_tc_init, utest_tc_cleanup, 10);
|
||||
|
||||
#endif /* ARCH_RISCV64 */
|
||||
#endif /* __TEST_CACHE_RV64_H */
|
165
examples/utest/testcases/mm/test_synchronization.h
Normal file
165
examples/utest/testcases/mm/test_synchronization.h
Normal file
|
@ -0,0 +1,165 @@
|
|||
/*
|
||||
* Copyright (c) 2006-2023, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2023-03-24 WangXiaoyao Complete testcase for synchronization
|
||||
*/
|
||||
#ifndef __TEST_SYNCHRONIZATION_H__
|
||||
#define __TEST_SYNCHRONIZATION_H__
|
||||
|
||||
#include "common.h"
|
||||
#include "semaphore.h"
|
||||
|
||||
#ifdef RT_USING_SMP
|
||||
|
||||
#define THREAD_CNT RT_CPUS_NR
|
||||
#define TEST_TIMES 2000
|
||||
#define PRIO (UTEST_THR_PRIORITY + 1)
|
||||
/* size of mapping buffer */
|
||||
#define BUF_SIZE (64ul << 10)
|
||||
|
||||
/* engage with sibling */
|
||||
struct rt_semaphore done;
|
||||
static semaphore_t sem1[THREAD_CNT / 2];
|
||||
static semaphore_t sem2[THREAD_CNT / 2];
|
||||
|
||||
static void *map(void)
|
||||
{
|
||||
int err;
|
||||
int flags = MMF_PREFETCH;
|
||||
size_t attr = MMU_MAP_K_RWCB;
|
||||
void *vaddr = 0;
|
||||
err =
|
||||
rt_aspace_map(&rt_kernel_space, &vaddr, BUF_SIZE, attr, flags, &rt_mm_dummy_mapper, 0);
|
||||
if (err)
|
||||
uassert_true(0);
|
||||
return vaddr;
|
||||
}
|
||||
|
||||
static void unmap(void *buf)
|
||||
{
|
||||
int err;
|
||||
err =
|
||||
rt_aspace_unmap(&rt_kernel_space, buf);
|
||||
if (err)
|
||||
uassert_true(0);
|
||||
return ;
|
||||
}
|
||||
|
||||
static void group1_entry(void *param)
|
||||
{
|
||||
const size_t id = (size_t)param;
|
||||
size_t test_times = TEST_TIMES;
|
||||
size_t alive = test_times / 10;
|
||||
void *buf;
|
||||
|
||||
while (test_times--)
|
||||
{
|
||||
if (test_times % alive == 0)
|
||||
uassert_true(1);
|
||||
|
||||
buf = map();
|
||||
|
||||
memset(buf, 'A' + id, BUF_SIZE);
|
||||
/* if other core write to our cache, force the changes to be visible to us */
|
||||
rt_hw_dmb();
|
||||
|
||||
if (memtest(buf, 'A' + id, BUF_SIZE))
|
||||
uassert_true(0);
|
||||
|
||||
semaphore_signal(&sem1[id]);
|
||||
semaphore_wait(&sem2[id]);
|
||||
unmap(buf);
|
||||
}
|
||||
|
||||
rt_sem_release(&done);
|
||||
return;
|
||||
}
|
||||
|
||||
static void group2_entry(void *param)
|
||||
{
|
||||
const size_t id = (size_t)param;
|
||||
size_t test_times = TEST_TIMES;
|
||||
size_t alive = test_times / 10;
|
||||
void *buf;
|
||||
|
||||
while (test_times--)
|
||||
{
|
||||
if (test_times % alive == 0)
|
||||
uassert_true(1);
|
||||
|
||||
semaphore_signal(&sem2[id]);
|
||||
semaphore_wait(&sem1[id]);
|
||||
buf = map();
|
||||
|
||||
memset(buf, 'a' + id, BUF_SIZE);
|
||||
/* if other core write to our cache, force the changes to be visible to us */
|
||||
rt_hw_dmb();
|
||||
|
||||
if (memtest(buf, 'a' + id, BUF_SIZE))
|
||||
uassert_true(0);
|
||||
|
||||
unmap(buf);
|
||||
}
|
||||
|
||||
rt_sem_release(&done);
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief On a smp system, we create at least 4 threads
|
||||
* 2 doing map, 2 doing unmapping at the same moment
|
||||
*/
|
||||
|
||||
static void synchronization_tc(void)
|
||||
{
|
||||
rt_thread_t group1[THREAD_CNT / 2];
|
||||
rt_thread_t group2[THREAD_CNT / 2];
|
||||
|
||||
rt_sem_init(&done, __func__, 0, RT_IPC_FLAG_FIFO);
|
||||
|
||||
for (size_t i = 0; i < THREAD_CNT / 2; i++)
|
||||
{
|
||||
char name[RT_NAME_MAX];
|
||||
rt_sprintf(name, "grp1_%d", i);
|
||||
group1[i] =
|
||||
rt_thread_create(name, group1_entry, (void *)i, ARCH_PAGE_SIZE, PRIO, 10);
|
||||
uassert_true(!!group1[i]);
|
||||
semaphore_init(&sem1[i], 0);
|
||||
|
||||
uassert_true(!rt_thread_startup(group1[i]));
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < THREAD_CNT / 2; i++)
|
||||
{
|
||||
char name[RT_NAME_MAX];
|
||||
rt_sprintf(name, "grp2_%d", i);
|
||||
group2[i] =
|
||||
rt_thread_create(name, group2_entry, (void *)i, ARCH_PAGE_SIZE, PRIO, 10);
|
||||
uassert_true(!!group2[i]);
|
||||
semaphore_init(&sem2[i], 0);
|
||||
|
||||
uassert_true(!rt_thread_startup(group2[i]));
|
||||
}
|
||||
|
||||
/* wait all thread exit */
|
||||
for (size_t i = 0; i < (THREAD_CNT / 2 * 2); i++)
|
||||
{
|
||||
rt_sem_take(&done, RT_WAITING_FOREVER);
|
||||
}
|
||||
LOG_I("all threads exit");
|
||||
rt_sem_detach(&done);
|
||||
}
|
||||
|
||||
#else /* RT_USING_SMP */
|
||||
|
||||
static void synchronization_tc(void)
|
||||
{
|
||||
uassert_true(1);
|
||||
}
|
||||
#endif /* RT_USING_SMP */
|
||||
|
||||
#endif /* __TEST_SYNCHRONIZATION_H__ */
|
Loading…
Add table
Add a link
Reference in a new issue