1 /*
2  * Copyright (c) 2019 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #ifndef ZEPHYR_INCLUDE_APP_MEMORY_APP_MEMDOMAIN_H_
7 #define ZEPHYR_INCLUDE_APP_MEMORY_APP_MEMDOMAIN_H_
8 
9 #include <linker/linker-defs.h>
10 #include <sys/dlist.h>
11 #include <kernel.h>
12 
13 #ifdef CONFIG_USERSPACE
14 
15 /**
16  * @brief Name of the data section for a particular partition
17  *
18  * Useful for defining memory pools, or any other macro that takes a
19  * section name as a parameter.
20  *
21  * @param id Partition name
22  */
23 #define K_APP_DMEM_SECTION(id) data_smem_##id##_data
24 
25 /**
26  * @brief Name of the bss section for a particular partition
27  *
28  * Useful for defining memory pools, or any other macro that takes a
29  * section name as a parameter.
30  *
31  * @param id Partition name
32  */
33 #define K_APP_BMEM_SECTION(id) data_smem_##id##_bss
34 
35 /**
36  * @brief Place data in a partition's data section
37  *
38  * Globals tagged with this will end up in the data section for the
39  * specified memory partition. This data should be initialized to some
40  * desired value.
41  *
42  * @param id Name of the memory partition to associate this data
43  */
44 #define K_APP_DMEM(id) Z_GENERIC_SECTION(K_APP_DMEM_SECTION(id))
45 
46 /**
47  * @brief Place data in a partition's bss section
48  *
49  * Globals tagged with this will end up in the bss section for the
50  * specified memory partition. This data will be zeroed at boot.
51  *
52  * @param id Name of the memory partition to associate this data
53  */
54 #define K_APP_BMEM(id) Z_GENERIC_SECTION(K_APP_BMEM_SECTION(id))
55 
56 struct z_app_region {
57 	void *bss_start;
58 	size_t bss_size;
59 };
60 
61 #define Z_APP_START(id) z_data_smem_##id##_part_start
62 #define Z_APP_SIZE(id) z_data_smem_##id##_part_size
63 #define Z_APP_BSS_START(id) z_data_smem_##id##_bss_start
64 #define Z_APP_BSS_SIZE(id) z_data_smem_##id##_bss_size
65 
66 /* If a partition is declared with K_APPMEM_PARTITION, but never has any
67  * data assigned to its contents, then no symbols with its prefix will end
68  * up in the symbol table. This prevents gen_app_partitions.py from detecting
69  * that the partition exists, and the linker symbols which specify partition
70  * bounds will not be generated, resulting in build errors.
71  *
72  * What this inline assembly code does is define a symbol with no data.
73  * This should work for all arches that produce ELF binaries, see
74  * https://sourceware.org/binutils/docs/as/Section.html
75  *
76  * We don't know what active flags/type of the pushed section were, so we are
77  * specific: "aw" indicates section is allocatable and writable,
78  * and "@progbits" indicates the section has data.
79  */
80 #if defined(CONFIG_ARM) || defined(CONFIG_ARM64)
81 /* ARM has a quirk in that '@' denotes a comment, so we have to send
82  * %progbits to the assembler instead.
83  */
84 #define Z_PROGBITS_SYM	"\%"
85 #else
86 #define Z_PROGBITS_SYM "@"
87 #endif
88 
89 #if defined(CONFIG_ARC) && defined(__CCAC__)
90 /* ARC MWDT assembler has slightly different pushsection/popsection directives
91  * names.
92  */
93 #define Z_PUSHSECTION_DIRECTIV		".pushsect"
94 #define Z_POPSECTION_DIRECTIVE		".popsect"
95 #else
96 #define Z_PUSHSECTION_DIRECTIV		".pushsection"
97 #define Z_POPSECTION_DIRECTIVE		".popsection"
98 #endif
99 
100 #define Z_APPMEM_PLACEHOLDER(name) \
101 	__asm__ ( \
102 		Z_PUSHSECTION_DIRECTIV " " STRINGIFY(K_APP_DMEM_SECTION(name)) \
103 			",\"aw\"," Z_PROGBITS_SYM "progbits\n\t" \
104 		".global " STRINGIFY(name) "_placeholder\n\t" \
105 		STRINGIFY(name) "_placeholder:\n\t" \
106 		Z_POPSECTION_DIRECTIVE "\n\t")
107 
108 /**
109  * @brief Define an application memory partition with linker support
110  *
111  * Defines a k_mem_paritition with the provided name.
112  * This name may be used with the K_APP_DMEM and K_APP_BMEM macros to
113  * place globals automatically in this partition.
114  *
115  * NOTE: placeholder char variable is defined here to prevent build errors
116  * if a partition is defined but nothing ever placed in it.
117  *
118  * @param name Name of the k_mem_partition to declare
119  */
120 #define K_APPMEM_PARTITION_DEFINE(name) \
121 	extern char Z_APP_START(name)[]; \
122 	extern char Z_APP_SIZE(name)[]; \
123 	struct k_mem_partition name = { \
124 		.start = (uintptr_t) &Z_APP_START(name), \
125 		.size = (size_t) &Z_APP_SIZE(name), \
126 		.attr = K_MEM_PARTITION_P_RW_U_RW \
127 	}; \
128 	extern char Z_APP_BSS_START(name)[]; \
129 	extern char Z_APP_BSS_SIZE(name)[]; \
130 	Z_GENERIC_SECTION(.app_regions.name) \
131 	const struct z_app_region name##_region = { \
132 		.bss_start = &Z_APP_BSS_START(name), \
133 		.bss_size = (size_t) &Z_APP_BSS_SIZE(name) \
134 	}; \
135 	Z_APPMEM_PLACEHOLDER(name)
136 #else
137 
138 #define K_APP_BMEM(ptn)
139 #define K_APP_DMEM(ptn)
140 #define K_APP_DMEM_SECTION(ptn) .data
141 #define K_APP_BMEM_SECTION(ptn) .bss
142 #define K_APPMEM_PARTITION_DEFINE(name)
143 
144 #endif /* CONFIG_USERSPACE */
145 #endif /* ZEPHYR_INCLUDE_APP_MEMORY_APP_MEMDOMAIN_H_ */
146