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 extern void function_not_relocated(int32_t value);
27 void function_in_custom_section(void);
28
29 #define HAS_SRAM2_DATA_SECTION (CONFIG_ARM)
30
ZTEST(code_relocation,test_function_in_sram2)31 ZTEST(code_relocation, test_function_in_sram2)
32 {
33 extern uintptr_t __ram_text_reloc_start;
34 extern uintptr_t __ram_text_reloc_end;
35 extern uintptr_t __sram2_text_reloc_start;
36 extern uintptr_t __sram2_text_reloc_end;
37 extern uintptr_t __sram2_data_reloc_start;
38 extern uintptr_t __sram2_data_reloc_end;
39 extern uintptr_t __sram2_bss_reloc_start;
40 extern uintptr_t __sram2_bss_reloc_end;
41 extern uintptr_t __sram2_rodata_reloc_start;
42 extern uintptr_t __sram2_rodata_reloc_end;
43 extern uintptr_t __custom_section_start;
44 extern uintptr_t __custom_section_end;
45
46 /* Print values from sram2 */
47 printk("Address of var_sram2_data %p\n", &var_sram2_data);
48 printk("Address of k_sem_give %p\n", &k_sem_give);
49 printk("Address of var_sram2_rodata %p\n", &var_sram2_rodata);
50 printk("Address of var_sram2_bss %p\n\n", &var_sram2_bss);
51
52 zassert_between_inclusive((uintptr_t)&var_sram2_data,
53 (uintptr_t)&__sram2_data_reloc_start,
54 (uintptr_t)&__sram2_data_reloc_end,
55 "var_sram2_data not in sram2 region");
56 zassert_between_inclusive((uintptr_t)&k_sem_give,
57 (uintptr_t)&__sram2_text_reloc_start,
58 (uintptr_t)&__sram2_text_reloc_end,
59 "k_sem_give not in sram_text region");
60 zassert_between_inclusive((uintptr_t)&var_sram2_rodata,
61 (uintptr_t)&__sram2_rodata_reloc_start,
62 (uintptr_t)&__sram2_rodata_reloc_end,
63 "var_sram2_rodata not in sram2_rodata region");
64 zassert_between_inclusive((uintptr_t)&var_sram2_bss,
65 (uintptr_t)&__sram2_bss_reloc_start,
66 (uintptr_t)&__sram2_bss_reloc_end,
67 "var_sram2_bss not in sram2_bss region");
68
69 /* Print values from sram */
70 printk("Address of function_in_sram %p\n", &function_in_sram);
71 zassert_between_inclusive((uintptr_t)&function_in_sram,
72 (uintptr_t)&__ram_text_reloc_start,
73 (uintptr_t)&__ram_text_reloc_end,
74 "function_in_sram is not in ram region");
75 function_in_sram(var_sram2_data);
76
77 /* Print values from non-relocated function */
78 printk("Address of function_not_relocated %p\n", &function_not_relocated);
79 zassert_between_inclusive((uintptr_t)&function_not_relocated,
80 (uintptr_t)&__text_region_start,
81 (uintptr_t)&__text_region_end,
82 "function_not_relocated is not in flash region");
83 function_not_relocated(var_sram2_data);
84 /* Call library function */
85 relocated_library();
86
87 /* Print values which were placed using attributes */
88 printk("Address of custom_section, func placed using attributes %p\n",
89 &function_in_custom_section);
90 printk("Address of custom_section data placed using attributes %p\n\n",
91 &var_custom_data);
92 zassert_between_inclusive((uintptr_t)&function_in_custom_section,
93 (uintptr_t)&__custom_section_start,
94 (uintptr_t)&__custom_section_end,
95 "function_in_custom_section not in custom_section region");
96 zassert_between_inclusive((uintptr_t)&var_custom_data,
97 (uintptr_t)&__custom_section_start,
98 (uintptr_t)&__custom_section_end,
99 "var_custom_data not in custom_section region");
100
101 k_sem_give(&test);
102 }
103
__in_section(custom_section,static,fun)104 __in_section(custom_section, static, fun) void function_in_custom_section(void)
105 {
106 }
107