1 /*
2  * Copyright (c) 2020 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include <zephyr/init.h>
9 #include <zephyr/linker/linker-defs.h>
10 #include <zephyr/sys/iterable_sections.h>
11 /* private kernel APIs */
12 #include <ksched.h>
13 #include <wait_q.h>
14 
k_heap_array_get(struct k_heap ** heap)15 int k_heap_array_get(struct k_heap **heap)
16 {
17 	int num;
18 
19 	/* Pointer to the start of the heap array */
20 	STRUCT_SECTION_GET(k_heap, 0, heap);
21 	/* Number of statically defined heaps */
22 	STRUCT_SECTION_COUNT(k_heap, &num);
23 	return num;
24 }
25 
k_heap_init(struct k_heap * heap,void * mem,size_t bytes)26 void k_heap_init(struct k_heap *heap, void *mem, size_t bytes)
27 {
28 	z_waitq_init(&heap->wait_q);
29 	heap->lock = (struct k_spinlock) {};
30 	sys_heap_init(&heap->heap, mem, bytes);
31 
32 	SYS_PORT_TRACING_OBJ_INIT(k_heap, heap);
33 }
34 
statics_init(void)35 static int statics_init(void)
36 {
37 	STRUCT_SECTION_FOREACH(k_heap, heap) {
38 #if defined(CONFIG_DEMAND_PAGING) && !defined(CONFIG_LINKER_GENERIC_SECTIONS_PRESENT_AT_BOOT)
39 		/* Some heaps may not present at boot, so we need to wait for
40 		 * paging mechanism to be initialized before we can initialize
41 		 * each heap.
42 		 */
43 		extern bool z_sys_post_kernel;
44 		bool do_clear = z_sys_post_kernel;
45 
46 		/* During pre-kernel init, z_sys_post_kernel == false,
47 		 * initialize if within pinned region. Otherwise skip.
48 		 * In post-kernel init, z_sys_post_kernel == true, skip those in
49 		 * pinned region as they have already been initialized and
50 		 * possibly already in use. Otherwise initialize.
51 		 */
52 		if (lnkr_is_pinned((uint8_t *)heap) &&
53 		    lnkr_is_pinned((uint8_t *)&heap->wait_q) &&
54 		    lnkr_is_region_pinned((uint8_t *)heap->heap.init_mem,
55 					  heap->heap.init_bytes)) {
56 			do_clear = !do_clear;
57 		}
58 
59 		if (do_clear)
60 #endif /* CONFIG_DEMAND_PAGING && !CONFIG_LINKER_GENERIC_SECTIONS_PRESENT_AT_BOOT */
61 		{
62 			k_heap_init(heap, heap->heap.init_mem, heap->heap.init_bytes);
63 		}
64 	}
65 	return 0;
66 }
67 
68 SYS_INIT_NAMED(statics_init_pre, statics_init, PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_OBJECTS);
69 
70 #if defined(CONFIG_DEMAND_PAGING) && !defined(CONFIG_LINKER_GENERIC_SECTIONS_PRESENT_AT_BOOT)
71 /* Need to wait for paging mechanism to be initialized before
72  * heaps that are not in pinned sections can be initialized.
73  */
74 SYS_INIT_NAMED(statics_init_post, statics_init, POST_KERNEL, 0);
75 #endif /* CONFIG_DEMAND_PAGING && !CONFIG_LINKER_GENERIC_SECTIONS_PRESENT_AT_BOOT */
76 
77 typedef void * (sys_heap_allocator_t)(struct sys_heap *heap, size_t align, size_t bytes);
78 
z_heap_alloc_helper(struct k_heap * heap,size_t align,size_t bytes,k_timeout_t timeout,sys_heap_allocator_t * sys_heap_allocator)79 static void *z_heap_alloc_helper(struct k_heap *heap, size_t align, size_t bytes,
80 				 k_timeout_t timeout,
81 				 sys_heap_allocator_t *sys_heap_allocator)
82 {
83 	k_timepoint_t end = sys_timepoint_calc(timeout);
84 	void *ret = NULL;
85 
86 	k_spinlock_key_t key = k_spin_lock(&heap->lock);
87 
88 	__ASSERT(!arch_is_in_isr() || K_TIMEOUT_EQ(timeout, K_NO_WAIT), "");
89 
90 	bool blocked_alloc = false;
91 
92 	while (ret == NULL) {
93 		ret = sys_heap_allocator(&heap->heap, align, bytes);
94 
95 		if (!IS_ENABLED(CONFIG_MULTITHREADING) ||
96 		    (ret != NULL) || K_TIMEOUT_EQ(timeout, K_NO_WAIT)) {
97 			break;
98 		}
99 
100 		if (!blocked_alloc) {
101 			blocked_alloc = true;
102 
103 			SYS_PORT_TRACING_OBJ_FUNC_BLOCKING(k_heap, alloc_helper, heap, timeout);
104 		} else {
105 			/**
106 			 * @todo	Trace attempt to avoid empty trace segments
107 			 */
108 		}
109 
110 		timeout = sys_timepoint_timeout(end);
111 		(void) z_pend_curr(&heap->lock, key, &heap->wait_q, timeout);
112 		key = k_spin_lock(&heap->lock);
113 	}
114 
115 	k_spin_unlock(&heap->lock, key);
116 	return ret;
117 }
118 
k_heap_alloc(struct k_heap * heap,size_t bytes,k_timeout_t timeout)119 void *k_heap_alloc(struct k_heap *heap, size_t bytes, k_timeout_t timeout)
120 {
121 	SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_heap, alloc, heap, timeout);
122 
123 	void *ret = z_heap_alloc_helper(heap, 0, bytes, timeout,
124 					sys_heap_noalign_alloc);
125 
126 	SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_heap, alloc, heap, timeout, ret);
127 
128 	return ret;
129 }
130 
k_heap_aligned_alloc(struct k_heap * heap,size_t align,size_t bytes,k_timeout_t timeout)131 void *k_heap_aligned_alloc(struct k_heap *heap, size_t align, size_t bytes,
132 			k_timeout_t timeout)
133 {
134 	SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_heap, aligned_alloc, heap, timeout);
135 
136 	/* A power of 2 as well as 0 is OK */
137 	__ASSERT((align & (align - 1)) == 0,
138 		 "align must be a power of 2");
139 
140 	void *ret = z_heap_alloc_helper(heap, align, bytes, timeout,
141 					sys_heap_aligned_alloc);
142 
143 	/*
144 	 * modules/debug/percepio/TraceRecorder/kernelports/Zephyr/include/tracing_tracerecorder.h
145 	 * contains a concealed non-parameterized direct reference to a local
146 	 * variable through the SYS_PORT_TRACING_OBJ_FUNC_EXIT macro below
147 	 * that is no longer in scope. Provide a dummy stub for compilation
148 	 * to still succeed until that module's layering violation is fixed.
149 	 */
150 	bool blocked_alloc = false; ARG_UNUSED(blocked_alloc);
151 
152 	SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_heap, aligned_alloc, heap, timeout, ret);
153 
154 	return ret;
155 }
156 
k_heap_calloc(struct k_heap * heap,size_t num,size_t size,k_timeout_t timeout)157 void *k_heap_calloc(struct k_heap *heap, size_t num, size_t size, k_timeout_t timeout)
158 {
159 	SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_heap, calloc, heap, timeout);
160 
161 	void *ret = NULL;
162 	size_t bounds = 0U;
163 
164 	if (!size_mul_overflow(num, size, &bounds)) {
165 		ret = k_heap_alloc(heap, bounds, timeout);
166 	}
167 	if (ret != NULL) {
168 		(void)memset(ret, 0, bounds);
169 	}
170 
171 	SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_heap, calloc, heap, timeout, ret);
172 
173 	return ret;
174 }
175 
k_heap_realloc(struct k_heap * heap,void * ptr,size_t bytes,k_timeout_t timeout)176 void *k_heap_realloc(struct k_heap *heap, void *ptr, size_t bytes, k_timeout_t timeout)
177 {
178 	k_timepoint_t end = sys_timepoint_calc(timeout);
179 	void *ret = NULL;
180 
181 	k_spinlock_key_t key = k_spin_lock(&heap->lock);
182 
183 	SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_heap, realloc, heap, ptr, bytes, timeout);
184 
185 	__ASSERT(!arch_is_in_isr() || K_TIMEOUT_EQ(timeout, K_NO_WAIT), "");
186 
187 	while (ret == NULL) {
188 		ret = sys_heap_realloc(&heap->heap, ptr, bytes);
189 
190 		if (!IS_ENABLED(CONFIG_MULTITHREADING) ||
191 		    (ret != NULL) || K_TIMEOUT_EQ(timeout, K_NO_WAIT)) {
192 			break;
193 		}
194 
195 		timeout = sys_timepoint_timeout(end);
196 		(void) z_pend_curr(&heap->lock, key, &heap->wait_q, timeout);
197 		key = k_spin_lock(&heap->lock);
198 	}
199 
200 	SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_heap, realloc, heap, ptr, bytes, timeout, ret);
201 
202 	k_spin_unlock(&heap->lock, key);
203 	return ret;
204 }
205 
k_heap_free(struct k_heap * heap,void * mem)206 void k_heap_free(struct k_heap *heap, void *mem)
207 {
208 	k_spinlock_key_t key = k_spin_lock(&heap->lock);
209 
210 	sys_heap_free(&heap->heap, mem);
211 
212 	SYS_PORT_TRACING_OBJ_FUNC(k_heap, free, heap);
213 	if (IS_ENABLED(CONFIG_MULTITHREADING) && (z_unpend_all(&heap->wait_q) != 0)) {
214 		z_reschedule(&heap->lock, key);
215 	} else {
216 		k_spin_unlock(&heap->lock, key);
217 	}
218 }
219