1 /*
2  * Copyright (c) 2012-2014 Wind River Systems, Inc.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include <zephyr/sys/printk.h>
9 #include <zephyr/ztest.h>
10 
11 #include "test_lib.h"
12 
13 /*
14  * These values will typically be placed in the appropriate sections, but may be moved around
15  * by the compiler; for instance var_sram2_data might end up in .rodata if the compiler can prove
16  * that it's never modified. To prevent that, we explicitly specify sections.
17  */
18 __in_section(data, sram2, var) uint32_t var_sram2_data = 10U;
19 __in_section(bss, sram2, var) uint32_t var_sram2_bss;
20 K_SEM_DEFINE(test, 0, 1);
21 __in_section(rodata, sram2, var) const uint32_t var_sram2_rodata = 100U;
22 
23 __in_section(custom_section, static, var) uint32_t var_custom_data = 1U;
24 
25 extern void function_in_sram(int32_t value);
26 void function_in_custom_section(void);
27 
28 #define HAS_SRAM2_DATA_SECTION (CONFIG_ARM)
29 
ZTEST(code_relocation,test_function_in_sram2)30 ZTEST(code_relocation, test_function_in_sram2)
31 {
32 	extern uintptr_t __sram2_text_reloc_start;
33 	extern uintptr_t __sram2_text_reloc_end;
34 	extern uintptr_t __sram2_data_reloc_start;
35 	extern uintptr_t __sram2_data_reloc_end;
36 	extern uintptr_t __sram2_bss_reloc_start;
37 	extern uintptr_t __sram2_bss_reloc_end;
38 	extern uintptr_t __sram2_rodata_reloc_start;
39 	extern uintptr_t __sram2_rodata_reloc_end;
40 	extern uintptr_t __custom_section_start;
41 	extern uintptr_t __custom_section_end;
42 
43 	/* Print values from sram2 */
44 	printk("Address of var_sram2_data %p\n", &var_sram2_data);
45 	printk("Address of k_sem_give %p\n", &k_sem_give);
46 	printk("Address of var_sram2_rodata %p\n", &var_sram2_rodata);
47 	printk("Address of var_sram2_bss %p\n\n", &var_sram2_bss);
48 
49 	zassert_between_inclusive((uintptr_t)&var_sram2_data,
50 		(uintptr_t)&__sram2_data_reloc_start,
51 		(uintptr_t)&__sram2_data_reloc_end,
52 		"var_sram2_data not in sram2 region");
53 	zassert_between_inclusive((uintptr_t)&k_sem_give,
54 		(uintptr_t)&__sram2_text_reloc_start,
55 		(uintptr_t)&__sram2_text_reloc_end,
56 		"k_sem_give not in sram_text region");
57 	zassert_between_inclusive((uintptr_t)&var_sram2_rodata,
58 		(uintptr_t)&__sram2_rodata_reloc_start,
59 		(uintptr_t)&__sram2_rodata_reloc_end,
60 		"var_sram2_rodata not in sram2_rodata region");
61 	zassert_between_inclusive((uintptr_t)&var_sram2_bss,
62 		(uintptr_t)&__sram2_bss_reloc_start,
63 		(uintptr_t)&__sram2_bss_reloc_end,
64 		"var_sram2_bss not in sram2_bss region");
65 
66 	/* Print values from sram */
67 	function_in_sram(var_sram2_data);
68 	/* Call library function */
69 	relocated_library();
70 
71 	/* Print values which were placed using attributes */
72 	printk("Address of custom_section, func placed using attributes %p\n",
73 	       &function_in_custom_section);
74 	printk("Address of custom_section data placed using attributes %p\n\n",
75 	       &var_custom_data);
76 	zassert_between_inclusive((uintptr_t)&function_in_custom_section,
77 		(uintptr_t)&__custom_section_start,
78 		(uintptr_t)&__custom_section_end,
79 		"function_in_custom_section not in custom_section region");
80 	zassert_between_inclusive((uintptr_t)&var_custom_data,
81 		(uintptr_t)&__custom_section_start,
82 		(uintptr_t)&__custom_section_end,
83 		"var_custom_data not in custom_section region");
84 
85 	k_sem_give(&test);
86 }
87 
__in_section(custom_section,static,fun)88 __in_section(custom_section, static, fun) void function_in_custom_section(void)
89 {
90 }
91