1 /*
2  * Copyright (c) 2020 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include "mem_protect.h"
9 
10 
11 
12 /* Add volatile to disable pre-calculation in compile stage in some
13  * toolchain, such as arcmwdt toolchain.
14  */
15 static volatile K_APP_DMEM(ztest_mem_partition) int var = 1356;
16 static volatile K_APP_BMEM(ztest_mem_partition) int zeroed_var = 20420;
K_APP_BMEM(ztest_mem_partition)17 static volatile K_APP_BMEM(ztest_mem_partition) int bss_var;
18 
19 /**
20  * @brief Test assigning global data and BSS variables to memory partitions
21  *
22  * @details Test that system supports application assigning global data and BSS
23  * variables using macros K_APP_BMEM() and K_APP_DMEM
24  *
25  * @ingroup kernel_memprotect_tests
26  */
27 ZTEST_USER(mem_protect_part, test_mem_part_assign_bss_vars_zero)
28 {
29 	/* The global variable var will be inside the bounds of
30 	 * ztest_mem_partition and be initialized with 1356 at boot.
31 	 */
32 	zassert_true(var == 1356);
33 
34 	/* The global variable zeroed_var will be inside the bounds of
35 	 * ztest_mem_partition and must be zeroed at boot size K_APP_BMEM() was
36 	 * used, indicating a BSS variable.
37 	 */
38 	zassert_true(zeroed_var == 0);
39 
40 	/* The global variable var will be inside the bounds of
41 	 * ztest_mem_partition and must be zeroed at boot size K_APP_BMEM() was
42 	 * used, indicating a BSS variable.
43 	 */
44 	zassert_true(bss_var == 0);
45 }
46 
47 K_APPMEM_PARTITION_DEFINE(part_arch);
48 K_APP_BMEM(part_arch) uint8_t __aligned(MEM_REGION_ALLOC)
49 	buf_arc[MEM_REGION_ALLOC];
50 
51 /**
52  * @brief Test partitions sized per the constraints of the MPU hardware
53  *
54  * @details
55  * - MEM_REGION_ALLOC is pre-sized to naturally fit in the target hardware's
56  *   memory management granularity. Show that the partition size matches.
57  * - Show that the base address of the partition is properly set, it should
58  *   match the base address of buf_arc.
59  *
60  * @ingroup kernel_memprotect_tests
61  */
ZTEST(mem_protect_part,test_mem_part_auto_determ_size)62 ZTEST(mem_protect_part, test_mem_part_auto_determ_size)
63 {
64 	zassert_true(part_arch.size == MEM_REGION_ALLOC);
65 	zassert_true(part_arch.start == (uintptr_t)buf_arc,
66 	   "Base address of memory partition not determined at build time");
67 }
68 
69 ZTEST_SUITE(mem_protect_part, NULL, NULL, NULL, NULL, NULL);
70