| /* |
| * Copyright 2023 Google LLC |
| * Copyright lowRISC contributors |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| |
| #include <stddef.h> |
| #include <stdint.h> |
| |
| #include "hw/top_matcha/sw/autogen/top_matcha.h" |
| #include "sw/device/lib/arch/device.h" |
| #include "sw/device/lib/base/macros.h" |
| #include "sw/device/lib/base/stdasm.h" |
| #include "sw/device/lib/dif/dif_uart.h" |
| #include "sw/device/lib/runtime/hart.h" |
| #include "sw/device/lib/runtime/log.h" |
| #include "sw/device/lib/runtime/print.h" |
| #include "sw/device/lib/testing/test_framework/check.h" |
| #include "sw/device/lib/testing/test_framework/ottf_test_config.h" |
| #include "sw/device/lib/testing/test_framework/status.h" |
| #include "sw/device/lib/testing/test_framework/test_util.h" |
| #include "sw/device/silicon_creator/lib/manifest_def.h" |
| |
| OTTF_DEFINE_TEST_CONFIG(); |
| |
| // Symbols defined in `sw/device/lib/testing/test_framework/ottf.ld`, |
| // which we use to check that the CRT did what it was supposed to. |
| extern char _bss_start; |
| extern char _bss_end; |
| extern char _data_start; |
| extern char _data_end; |
| extern char _data_init_start; |
| |
| // The addresses of the values above. |
| static const uintptr_t bss_start_addr = (uintptr_t)&_bss_start; |
| static const uintptr_t bss_end_addr = (uintptr_t)&_bss_end; |
| static const uintptr_t data_start_addr = (uintptr_t)&_data_start; |
| static const uintptr_t data_end_addr = (uintptr_t)&_data_end; |
| static const uintptr_t data_init_start_addr = (uintptr_t)&_data_init_start; |
| |
| // Ensure that both .bss and .data are non-empty. The compiler will always keep |
| // these symbols, since they're volatile. |
| volatile char ensure_data_exists = 42; |
| volatile char ensure_bss_exists; |
| |
| static dif_uart_t uart0; |
| |
| /** |
| * Test that crt_section_clear correctly zeros word aligned sections. |
| * |
| * Sections are simulated using word aligned regions of various sizes within an |
| * array. |
| * |
| * Does not return if the test fails. |
| */ |
| static void test_crt_section_clear(void) { |
| // Function to test (symbol in the CRT assembly library). |
| extern void crt_section_clear(void *start, void *end); |
| |
| // Maximum end index of target section. |
| const size_t kLen = 32; |
| |
| // Section indices (start inclusive, end exclusive). |
| const struct { |
| size_t start; |
| size_t end; |
| } kTests[] = {{.start = 0, .end = 0}, {.start = 0, .end = 1}, |
| {.start = kLen - 1, .end = kLen}, {.start = 0, .end = kLen - 1}, |
| {.start = 1, .end = kLen}, {.start = 0, .end = kLen}}; |
| |
| for (size_t t = 0; t < ARRAYSIZE(kTests); ++t) { |
| // Set target array to non-zero values. |
| uint32_t section[kLen]; |
| const uint32_t kVal = ~0u; |
| for (size_t i = 0; i < kLen; ++i) { |
| section[i] = kVal; |
| } |
| |
| // Clear section of target array. |
| const size_t start = kTests[t].start; |
| const size_t end = kTests[t].end; |
| crt_section_clear(§ion[start], §ion[end]); |
| |
| // Check that section was cleared. |
| for (size_t i = 0; i < kLen; ++i) { |
| const uint32_t expect = i >= start && i < end ? 0 : kVal; |
| CHECK(section[i] == expect, |
| "%s case %u: section[%u] got 0x%08x, want 0x%08x", __func__, t, i, |
| section[i], expect); |
| } |
| } |
| } |
| |
| /** |
| * Test that crt_section_copy correctly copies data between word aligned |
| * sections. |
| * |
| * Sections are simulated using word aligned regions of various sizes within |
| * arrays. |
| * |
| * Does not return if the test fails. |
| */ |
| static void test_crt_section_copy(void) { |
| // Function to test (symbol in the CRT assembly library). |
| extern void crt_section_copy(void *start, void *end, void *source); |
| |
| // Maximum end index of target section. |
| const size_t kLen = 32; |
| |
| // Section indices (start inclusive, end exclusive) and source index |
| // (inclusive). |
| const struct { |
| size_t start; |
| size_t end; |
| size_t source; |
| } kTests[] = {{.start = 0, .end = 0, .source = 0}, |
| {.start = 0, .end = 1, .source = 1}, |
| {.start = kLen - 1, .end = kLen, .source = 2}, |
| {.start = 0, .end = kLen - 1, .source = 1}, |
| {.start = 1, .end = kLen, .source = 1}, |
| {.start = 0, .end = kLen, .source = 0}, |
| {.start = 0, .end = kLen, .source = 0}, |
| {.start = 1, .end = kLen, .source = 0}, |
| {.start = 2, .end = kLen, .source = 0}, |
| {.start = 3, .end = kLen, .source = 0}, |
| {.start = 0, .end = kLen / 2, .source = 0}, |
| {.start = 1, .end = kLen / 2, .source = 0}, |
| {.start = 2, .end = kLen / 2, .source = 0}, |
| {.start = 3, .end = kLen / 2, .source = 0}}; |
| |
| for (size_t t = 0; t < ARRAYSIZE(kTests); ++t) { |
| // Clear target array and setup source array with known values (index + 1). |
| uint32_t dst[kLen], src[kLen]; |
| for (size_t i = 0; i < kLen; ++i) { |
| src[i] = (uint32_t)(i) + 1; |
| dst[i] = 0; |
| } |
| |
| // Copy section from source to target array. |
| const size_t start = kTests[t].start; |
| const size_t end = kTests[t].end; |
| const size_t source = kTests[t].source; |
| crt_section_copy(&dst[start], &dst[end], &src[source]); |
| |
| // First expected value. |
| uint32_t val = (uint32_t)(source) + 1; |
| |
| // Check section was copied correctly. |
| for (size_t i = 0; i < kLen; ++i) { |
| const uint32_t expect = i >= start && i < end ? val++ : 0; |
| CHECK(dst[i] == expect, "%s case %u: dst[%u] got 0x%08x, want 0x%08x", |
| __func__, t, i, dst[i], expect); |
| } |
| } |
| } |
| |
| void _ottf_main(void) { |
| // NOTE: we cannot call any external functions until all checks of post-CRT |
| // state are complete; this is to ensure that our checks are not tainted by |
| // external functions. |
| // |
| // Among other things, this means we can't CHECK, since we can't initialize |
| // UART. Thus, any critical failures are handled by returning from main. |
| // To minimize the chance of things going wrong, we don't even bother placing |
| // the checks in their own function. |
| |
| // Test core assumptions above the five addresses above. The test code |
| // must be able to assume these all hold. |
| // |
| // Note that performing these comparisons on their addresses is UB, and will |
| // cause this entire function to get deleted by the compiler. |
| if (&_bss_start > &_bss_end || &_data_start > &_data_end) { |
| // Something has gone terribly wrong and we have no hope of continuing the |
| // test, so we're going to just abort. |
| abort(); |
| } |
| |
| // Ensure that .bss was *actually* zeroed at the start of execution. If it |
| // wasn't, we note the offset from _bss_start at which it wasn't. |
| char *bss = &_bss_start; |
| ptrdiff_t bss_len = &_bss_end - &_bss_start; |
| int bad_bss_index = -1; |
| for (int i = 0; i < bss_len; ++i) { |
| if (bss[i] != 0) { |
| bad_bss_index = i; |
| break; |
| } |
| } |
| |
| // Similarly, ensure that .data has the values in the init section. |
| char *data = &_data_start; |
| char *data_init = &_data_init_start; |
| ptrdiff_t data_len = &_data_end - &_data_start; |
| int bad_data_index = -1; |
| for (int i = 0; i < data_len; ++i) { |
| if (data[i] != data_init[i]) { |
| bad_data_index = i; |
| break; |
| } |
| } |
| |
| // End of post-CRT checks; begin actual assertions.. |
| test_status_set(kTestStatusInTest); |
| // Initialize the UART to enable logging for non-DV simulation platforms. |
| if (kDeviceType != kDeviceSimDV) { |
| init_uart(TOP_MATCHA_UART0_BASE_ADDR, &uart0); |
| } |
| |
| CHECK(bss_start_addr % sizeof(uint32_t) == 0, |
| "_bss_start not word-aligned: 0x%08x", bss_start_addr); |
| CHECK(bss_end_addr % sizeof(uint32_t) == 0, |
| "_bss_end not word-aligned: 0x%08x", bss_end_addr); |
| CHECK(data_start_addr % sizeof(uint32_t) == 0, |
| "_data_start not word-aligned: 0x%08x", data_start_addr); |
| CHECK(data_end_addr % sizeof(uint32_t) == 0, |
| "_data_end not word-aligned: 0x%08x", data_end_addr); |
| CHECK(data_init_start_addr % sizeof(uint32_t) == 0, |
| "_data_init_start not word-aligned: 0x%08x", data_init_start_addr); |
| |
| CHECK(bad_bss_index == -1, "found non-zero .bss byte at *0x%08x == 0x%02x", |
| bss_start_addr + bad_bss_index, (uint32_t)bss[bad_bss_index]); |
| CHECK(bad_data_index == -1, |
| "found bad .data byte at *0x%08x == 0x%02x, expected 0x%02x", |
| data_start_addr + bad_data_index, (uint32_t)data_init[bad_data_index]); |
| |
| // Unit test CRT utility functions. |
| test_crt_section_clear(); |
| test_crt_section_copy(); |
| |
| test_status_set(kTestStatusPassed); |
| } |