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_init(struct k_heap * h,void * mem,size_t bytes)15 void k_heap_init(struct k_heap *h, void *mem, size_t bytes)
16 {
17 	z_waitq_init(&h->wait_q);
18 	sys_heap_init(&h->heap, mem, bytes);
19 
20 	SYS_PORT_TRACING_OBJ_INIT(k_heap, h);
21 }
22 
statics_init(void)23 static int statics_init(void)
24 {
25 	STRUCT_SECTION_FOREACH(k_heap, h) {
26 #if defined(CONFIG_DEMAND_PAGING) && !defined(CONFIG_LINKER_GENERIC_SECTIONS_PRESENT_AT_BOOT)
27 		/* Some heaps may not present at boot, so we need to wait for
28 		 * paging mechanism to be initialized before we can initialize
29 		 * each heap.
30 		 */
31 		extern bool z_sys_post_kernel;
32 		bool do_clear = z_sys_post_kernel;
33 
34 		/* During pre-kernel init, z_sys_post_kernel == false,
35 		 * initialize if within pinned region. Otherwise skip.
36 		 * In post-kernel init, z_sys_post_kernel == true, skip those in
37 		 * pinned region as they have already been initialized and
38 		 * possibly already in use. Otherwise initialize.
39 		 */
40 		if (lnkr_is_pinned((uint8_t *)h) &&
41 		    lnkr_is_pinned((uint8_t *)&h->wait_q) &&
42 		    lnkr_is_region_pinned((uint8_t *)h->heap.init_mem,
43 					  h->heap.init_bytes)) {
44 			do_clear = !do_clear;
45 		}
46 
47 		if (do_clear)
48 #endif /* CONFIG_DEMAND_PAGING && !CONFIG_LINKER_GENERIC_SECTIONS_PRESENT_AT_BOOT */
49 		{
50 			k_heap_init(h, h->heap.init_mem, h->heap.init_bytes);
51 		}
52 	}
53 	return 0;
54 }
55 
56 SYS_INIT_NAMED(statics_init_pre, statics_init, PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_OBJECTS);
57 
58 #if defined(CONFIG_DEMAND_PAGING) && !defined(CONFIG_LINKER_GENERIC_SECTIONS_PRESENT_AT_BOOT)
59 /* Need to wait for paging mechanism to be initialized before
60  * heaps that are not in pinned sections can be initialized.
61  */
62 SYS_INIT_NAMED(statics_init_post, statics_init, POST_KERNEL, 0);
63 #endif /* CONFIG_DEMAND_PAGING && !CONFIG_LINKER_GENERIC_SECTIONS_PRESENT_AT_BOOT */
64 
k_heap_aligned_alloc(struct k_heap * h,size_t align,size_t bytes,k_timeout_t timeout)65 void *k_heap_aligned_alloc(struct k_heap *h, size_t align, size_t bytes,
66 			k_timeout_t timeout)
67 {
68 	k_timepoint_t end = sys_timepoint_calc(timeout);
69 	void *ret = NULL;
70 
71 	k_spinlock_key_t key = k_spin_lock(&h->lock);
72 
73 	SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_heap, aligned_alloc, h, timeout);
74 
75 	__ASSERT(!arch_is_in_isr() || K_TIMEOUT_EQ(timeout, K_NO_WAIT), "");
76 
77 	bool blocked_alloc = false;
78 
79 	while (ret == NULL) {
80 		ret = sys_heap_aligned_alloc(&h->heap, align, bytes);
81 
82 		if (!IS_ENABLED(CONFIG_MULTITHREADING) ||
83 		    (ret != NULL) || K_TIMEOUT_EQ(timeout, K_NO_WAIT)) {
84 			break;
85 		}
86 
87 		if (!blocked_alloc) {
88 			blocked_alloc = true;
89 
90 			SYS_PORT_TRACING_OBJ_FUNC_BLOCKING(k_heap, aligned_alloc, h, timeout);
91 		} else {
92 			/**
93 			 * @todo	Trace attempt to avoid empty trace segments
94 			 */
95 		}
96 
97 		timeout = sys_timepoint_timeout(end);
98 		(void) z_pend_curr(&h->lock, key, &h->wait_q, timeout);
99 		key = k_spin_lock(&h->lock);
100 	}
101 
102 	SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_heap, aligned_alloc, h, timeout, ret);
103 
104 	k_spin_unlock(&h->lock, key);
105 	return ret;
106 }
107 
k_heap_alloc(struct k_heap * h,size_t bytes,k_timeout_t timeout)108 void *k_heap_alloc(struct k_heap *h, size_t bytes, k_timeout_t timeout)
109 {
110 	SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_heap, alloc, h, timeout);
111 
112 	void *ret = k_heap_aligned_alloc(h, sizeof(void *), bytes, timeout);
113 
114 	SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_heap, alloc, h, timeout, ret);
115 
116 	return ret;
117 }
118 
k_heap_free(struct k_heap * h,void * mem)119 void k_heap_free(struct k_heap *h, void *mem)
120 {
121 	k_spinlock_key_t key = k_spin_lock(&h->lock);
122 
123 	sys_heap_free(&h->heap, mem);
124 
125 	SYS_PORT_TRACING_OBJ_FUNC(k_heap, free, h);
126 	if (IS_ENABLED(CONFIG_MULTITHREADING) && z_unpend_all(&h->wait_q) != 0) {
127 		z_reschedule(&h->lock, key);
128 	} else {
129 		k_spin_unlock(&h->lock, key);
130 	}
131 }
132