1 /*
2  * Copyright (c) 2019 Nordic Semiconductor ASA.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/ztest.h>
8 #include <zephyr/linker/linker-defs.h>
9 #include <zephyr/internal/syscall_handler.h>
10 
11 static volatile int test_flag;
12 
ram_function(void)13 __ramfunc static void ram_function(void)
14 {
15 	test_flag = 1;
16 }
17 
ZTEST(ramfunc,test_ramfunc)18 ZTEST(ramfunc, test_ramfunc)
19 {
20 	int init_flag, post_flag;
21 
22 	init_flag = test_flag;
23 	zassert_true(init_flag == 0, "Test flag not initialized to zero");
24 
25 	/* Verify that the .ramfunc section is not empty, it is located
26 	 * inside SRAM, and that ram_function(.) is located inside
27 	 * the .ramfunc section.
28 	 */
29 	zassert_true((uint32_t)&__ramfunc_size != 0,
30 		".ramfunc linker section is empty");
31 	zassert_true(((uint32_t)&__ramfunc_start >= (uint32_t)&_image_ram_start)
32 			&& ((uint32_t)&__ramfunc_end < (uint32_t)&_image_ram_end),
33 			".ramfunc linker section not in RAM");
34 	zassert_true(
35 		(((uint32_t)&__ramfunc_start) <= (uint32_t)ram_function) &&
36 		(((uint32_t)&__ramfunc_end) > (uint32_t)ram_function),
37 		"ram_function not loaded into .ramfunc");
38 
39 	/* If we build with User Mode support, verify that the
40 	 * ram_function(.) is user (read) accessible.
41 	 */
42 #if defined(CONFIG_USERSPACE)
43 	zassert_true(arch_buffer_validate((void *)&__ramfunc_start,
44 			(size_t)&__ramfunc_size, 0) == 0 /* Success */,
45 		".ramfunc section not user accessible");
46 #endif /* CONFIG_USERSPACE */
47 
48 	/* Execute the function from SRAM. */
49 	ram_function();
50 
51 	/* Verify that the function is executed successfully. */
52 	post_flag = test_flag;
53 	zassert_true(post_flag == 1,
54 		"ram_function() execution failed.");
55 }
56 /**
57  * @}
58  */
59