|
| 1 | +/* |
| 2 | + * Copyright (c) 2006-2022, RT-Thread Development Team |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + * |
| 6 | + * Change Logs: |
| 7 | + * Date Author Notes |
| 8 | + */ |
| 9 | +#include <rtthread.h> |
| 10 | +#include <rthw.h> |
| 11 | +#include <string.h> |
| 12 | +#include <stdlib.h> |
| 13 | + |
| 14 | +#if defined(RT_USING_UTEST) && defined(ENABLE_VECTOR) |
| 15 | +#include <utest.h> |
| 16 | +#include <ext_context.h> |
| 17 | + |
| 18 | +void rt_hw_vector_ctx_restore(void *buf); |
| 19 | +void rt_hw_vector_ctx_save(void *buf); |
| 20 | + |
| 21 | +/** |
| 22 | + * ============================================================== |
| 23 | + * TEST FEATURE |
| 24 | + * Use libc `memcpy` which employing V extension codes |
| 25 | + * to test V extension features |
| 26 | + * ============================================================== |
| 27 | + */ |
| 28 | +static char *constant = "hello,it's a nice day and i'm happy to see you\n"; |
| 29 | +#define ARR_SIZE 4096 |
| 30 | +static char array[ARR_SIZE]; |
| 31 | + |
| 32 | +static void test_feature(void) |
| 33 | +{ |
| 34 | + memcpy(array, constant, sizeof array); |
| 35 | + char *src = constant; |
| 36 | + char *dst = array; |
| 37 | + int error = 0; |
| 38 | + |
| 39 | + for (size_t i = 0; i < ARR_SIZE; i++) |
| 40 | + { |
| 41 | + if (src[i] != dst[i]) |
| 42 | + { |
| 43 | + error = 1; |
| 44 | + break; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + uassert_false(error); |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * ============================================================== |
| 53 | + * TEST CONTEXT SAVING |
| 54 | + * Create 2 threads employing V extension, verify V states are |
| 55 | + * not modified by each other |
| 56 | + * ============================================================== |
| 57 | + */ |
| 58 | +#define TEST_THREAD 2 |
| 59 | +#define VECTOR_CTX_BYTES (CTX_VECTOR_REG_NR * REGBYTES) |
| 60 | +void *ctx_vector[TEST_THREAD * 2]; |
| 61 | + |
| 62 | +static rt_sem_t sem; |
| 63 | + |
| 64 | +void dump_frame(void *frame) |
| 65 | +{ |
| 66 | + uint64_t *content = frame; |
| 67 | + for (size_t i = 0; i < VECTOR_CTX_BYTES / 8; i++) |
| 68 | + { |
| 69 | + rt_kprintf("%x ", content[i]); |
| 70 | + } |
| 71 | + rt_kprintf("\n"); |
| 72 | +} |
| 73 | + |
| 74 | +static void vector_child(void *param) |
| 75 | +{ |
| 76 | + void **ctx = param; |
| 77 | + uint64_t *reg = ctx[0]; |
| 78 | + uint64_t vtype; |
| 79 | + uint64_t vl; |
| 80 | + |
| 81 | + rt_sem_release(sem); |
| 82 | + |
| 83 | + rt_hw_vector_ctx_restore(ctx[0]); |
| 84 | + |
| 85 | + /* STAGE 2, save t2 context */ |
| 86 | + test_feature(); |
| 87 | + |
| 88 | + /** |
| 89 | + * @brief vtype & vl will be modified after context saving, |
| 90 | + * it's ok because it will be recover after context restoring |
| 91 | + * We restore these states manually here. |
| 92 | + */ |
| 93 | + asm volatile("csrr %0, vtype":"=r"(vtype)); |
| 94 | + asm volatile("csrr %0, vl":"=r"(vl)); |
| 95 | + rt_hw_vector_ctx_save(ctx[0]); |
| 96 | + |
| 97 | + rt_memcpy(ctx[1], ctx[0], VECTOR_CTX_BYTES); |
| 98 | + |
| 99 | + rt_thread_yield(); |
| 100 | + |
| 101 | + asm volatile("vsetvl x0, %0, %1"::"r"(vl), "r"(vtype)); |
| 102 | + rt_hw_vector_ctx_save(ctx[0]); |
| 103 | + |
| 104 | + uassert_false(rt_memcmp(ctx[1], ctx[0], VECTOR_CTX_BYTES)); |
| 105 | +} |
| 106 | + |
| 107 | +/** |
| 108 | + * @brief Test if context save/restore codes work properly |
| 109 | + */ |
| 110 | +static void test_context() |
| 111 | +{ |
| 112 | + rt_thread_t child; |
| 113 | + uint64_t vtype; |
| 114 | + uint64_t vl; |
| 115 | + |
| 116 | + for (size_t i = 0; i < TEST_THREAD; i++) |
| 117 | + { |
| 118 | + ctx_vector[i * 2] = calloc(VECTOR_CTX_BYTES, 1); |
| 119 | + ctx_vector[i * 2 + 1] = calloc(VECTOR_CTX_BYTES, 1); |
| 120 | + } |
| 121 | + rt_hw_vector_ctx_restore(ctx_vector[0]); |
| 122 | + |
| 123 | + child = rt_thread_create("test_vector_child", vector_child, &ctx_vector[2], 4096, 10, 20); |
| 124 | + |
| 125 | + /* STAGE 1, save t1 context */ |
| 126 | + /* assuming that rt libc memcpy do not use vector instruction */ |
| 127 | + asm volatile("csrr %0, vtype":"=r"(vtype)); |
| 128 | + asm volatile("csrr %0, vl":"=r"(vl)); |
| 129 | + rt_hw_vector_ctx_save(ctx_vector[0]); |
| 130 | + |
| 131 | + rt_memcpy(ctx_vector[1], ctx_vector[0], VECTOR_CTX_BYTES); |
| 132 | + |
| 133 | + rt_thread_startup(child); |
| 134 | + rt_sem_take(sem, 0); |
| 135 | + |
| 136 | + /* STAGE 3, verify t1 context */ |
| 137 | + asm volatile("vsetvl x0, %0, %1"::"r"(vl), "r"(vtype)); |
| 138 | + rt_hw_vector_ctx_save(ctx_vector[0]); |
| 139 | + uassert_false(rt_memcmp(ctx_vector[1], ctx_vector[0], VECTOR_CTX_BYTES)); |
| 140 | + |
| 141 | + rt_thread_yield(); |
| 142 | +} |
| 143 | + |
| 144 | +/** |
| 145 | + * ============================================================== |
| 146 | + * TEST NO VECTOR raise error and recover |
| 147 | + * ============================================================== |
| 148 | + */ |
| 149 | + |
| 150 | +static void test_no_vector() |
| 151 | +{ |
| 152 | + asm volatile ("li t0, 0x600\n" |
| 153 | + "csrc sstatus, t0"); |
| 154 | + test_feature(); |
| 155 | + uassert_true(1); |
| 156 | +} |
| 157 | + |
| 158 | +static rt_err_t utest_tc_init(void) |
| 159 | +{ |
| 160 | + sem = rt_sem_create("test_ctx", 0, RT_IPC_FLAG_FIFO); |
| 161 | + return RT_EOK; |
| 162 | +} |
| 163 | + |
| 164 | +static rt_err_t utest_tc_cleanup(void) |
| 165 | +{ |
| 166 | + rt_sem_delete(sem); |
| 167 | + return RT_EOK; |
| 168 | +} |
| 169 | + |
| 170 | +static void testcase(void) |
| 171 | +{ |
| 172 | + UTEST_UNIT_RUN(test_feature); |
| 173 | + UTEST_UNIT_RUN(test_context); |
| 174 | + UTEST_UNIT_RUN(test_no_vector); |
| 175 | +} |
| 176 | + |
| 177 | +UTEST_TC_EXPORT(testcase, "testcases.libcpu.vector", utest_tc_init, utest_tc_cleanup, 10); |
| 178 | +#endif /* RT_USING_UTEST && ENABLE_VECTOR */ |
0 commit comments