Andy Ross | 0dd83b8 | 2020-04-03 10:01:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2020 Intel Corporation |
| 3 | * |
| 4 | * SPDX-License-Identifier: Apache-2.0 |
| 5 | */ |
| 6 | |
Gerard Marull-Paretas | cffefc8 | 2022-05-06 11:04:23 +0200 | [diff] [blame] | 7 | #include <zephyr/kernel.h> |
Gerard Marull-Paretas | cffefc8 | 2022-05-06 11:04:23 +0200 | [diff] [blame] | 8 | #include <zephyr/init.h> |
| 9 | #include <zephyr/linker/linker-defs.h> |
Gerard Marull-Paretas | dacb3db | 2023-05-15 15:50:28 +0200 | [diff] [blame] | 10 | #include <zephyr/sys/iterable_sections.h> |
Anas Nashif | 8634c3b | 2023-08-29 17:03:12 +0000 | [diff] [blame] | 11 | /* private kernel APIs */ |
| 12 | #include <ksched.h> |
| 13 | #include <wait_q.h> |
Andy Ross | 0dd83b8 | 2020-04-03 10:01:03 -0700 | [diff] [blame] | 14 | |
Anas Nashif | 477a04a | 2024-02-28 08:15:15 -0500 | [diff] [blame] | 15 | void k_heap_init(struct k_heap *heap, void *mem, size_t bytes) |
Andy Ross | 0dd83b8 | 2020-04-03 10:01:03 -0700 | [diff] [blame] | 16 | { |
Anas Nashif | 477a04a | 2024-02-28 08:15:15 -0500 | [diff] [blame] | 17 | z_waitq_init(&heap->wait_q); |
Peter Mitsis | 11083fc | 2025-01-30 12:18:56 -0800 | [diff] [blame] | 18 | heap->lock = (struct k_spinlock) {}; |
Anas Nashif | 477a04a | 2024-02-28 08:15:15 -0500 | [diff] [blame] | 19 | sys_heap_init(&heap->heap, mem, bytes); |
Torbjörn Leksell | 80cd9da | 2021-03-26 13:42:25 +0100 | [diff] [blame] | 20 | |
Anas Nashif | 477a04a | 2024-02-28 08:15:15 -0500 | [diff] [blame] | 21 | SYS_PORT_TRACING_OBJ_INIT(k_heap, heap); |
Andy Ross | 0dd83b8 | 2020-04-03 10:01:03 -0700 | [diff] [blame] | 22 | } |
| 23 | |
Gerard Marull-Paretas | a5fd0d1 | 2022-10-19 09:33:44 +0200 | [diff] [blame] | 24 | static int statics_init(void) |
Andy Ross | 0dd83b8 | 2020-04-03 10:01:03 -0700 | [diff] [blame] | 25 | { |
Anas Nashif | 477a04a | 2024-02-28 08:15:15 -0500 | [diff] [blame] | 26 | STRUCT_SECTION_FOREACH(k_heap, heap) { |
Daniel Leung | 5c4fff3 | 2021-08-03 13:59:36 -0700 | [diff] [blame] | 27 | #if defined(CONFIG_DEMAND_PAGING) && !defined(CONFIG_LINKER_GENERIC_SECTIONS_PRESENT_AT_BOOT) |
| 28 | /* Some heaps may not present at boot, so we need to wait for |
| 29 | * paging mechanism to be initialized before we can initialize |
| 30 | * each heap. |
| 31 | */ |
| 32 | extern bool z_sys_post_kernel; |
| 33 | bool do_clear = z_sys_post_kernel; |
| 34 | |
| 35 | /* During pre-kernel init, z_sys_post_kernel == false, |
| 36 | * initialize if within pinned region. Otherwise skip. |
| 37 | * In post-kernel init, z_sys_post_kernel == true, skip those in |
| 38 | * pinned region as they have already been initialized and |
| 39 | * possibly already in use. Otherwise initialize. |
| 40 | */ |
Anas Nashif | 477a04a | 2024-02-28 08:15:15 -0500 | [diff] [blame] | 41 | if (lnkr_is_pinned((uint8_t *)heap) && |
| 42 | lnkr_is_pinned((uint8_t *)&heap->wait_q) && |
| 43 | lnkr_is_region_pinned((uint8_t *)heap->heap.init_mem, |
| 44 | heap->heap.init_bytes)) { |
Daniel Leung | 5c4fff3 | 2021-08-03 13:59:36 -0700 | [diff] [blame] | 45 | do_clear = !do_clear; |
| 46 | } |
| 47 | |
| 48 | if (do_clear) |
| 49 | #endif /* CONFIG_DEMAND_PAGING && !CONFIG_LINKER_GENERIC_SECTIONS_PRESENT_AT_BOOT */ |
| 50 | { |
Anas Nashif | 477a04a | 2024-02-28 08:15:15 -0500 | [diff] [blame] | 51 | k_heap_init(heap, heap->heap.init_mem, heap->heap.init_bytes); |
Daniel Leung | 5c4fff3 | 2021-08-03 13:59:36 -0700 | [diff] [blame] | 52 | } |
Andy Ross | 0dd83b8 | 2020-04-03 10:01:03 -0700 | [diff] [blame] | 53 | } |
| 54 | return 0; |
| 55 | } |
| 56 | |
Jordan Yates | 6f41d52 | 2022-07-02 12:06:55 +1000 | [diff] [blame] | 57 | SYS_INIT_NAMED(statics_init_pre, statics_init, PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_OBJECTS); |
Andy Ross | 0dd83b8 | 2020-04-03 10:01:03 -0700 | [diff] [blame] | 58 | |
Daniel Leung | 5c4fff3 | 2021-08-03 13:59:36 -0700 | [diff] [blame] | 59 | #if defined(CONFIG_DEMAND_PAGING) && !defined(CONFIG_LINKER_GENERIC_SECTIONS_PRESENT_AT_BOOT) |
| 60 | /* Need to wait for paging mechanism to be initialized before |
| 61 | * heaps that are not in pinned sections can be initialized. |
| 62 | */ |
Jordan Yates | 6f41d52 | 2022-07-02 12:06:55 +1000 | [diff] [blame] | 63 | SYS_INIT_NAMED(statics_init_post, statics_init, POST_KERNEL, 0); |
Daniel Leung | 5c4fff3 | 2021-08-03 13:59:36 -0700 | [diff] [blame] | 64 | #endif /* CONFIG_DEMAND_PAGING && !CONFIG_LINKER_GENERIC_SECTIONS_PRESENT_AT_BOOT */ |
| 65 | |
Anas Nashif | 477a04a | 2024-02-28 08:15:15 -0500 | [diff] [blame] | 66 | void *k_heap_aligned_alloc(struct k_heap *heap, size_t align, size_t bytes, |
Maximilian Bachmann | 34d7c78 | 2020-11-13 15:12:31 +0100 | [diff] [blame] | 67 | k_timeout_t timeout) |
Andy Ross | 0dd83b8 | 2020-04-03 10:01:03 -0700 | [diff] [blame] | 68 | { |
Nicolas Pitre | 77b7eb1 | 2023-07-06 14:58:03 -0400 | [diff] [blame] | 69 | k_timepoint_t end = sys_timepoint_calc(timeout); |
Andy Ross | 0dd83b8 | 2020-04-03 10:01:03 -0700 | [diff] [blame] | 70 | void *ret = NULL; |
Jay Shoen | b4734d5 | 2022-09-28 17:59:45 -0600 | [diff] [blame] | 71 | |
Anas Nashif | 477a04a | 2024-02-28 08:15:15 -0500 | [diff] [blame] | 72 | k_spinlock_key_t key = k_spin_lock(&heap->lock); |
Andy Ross | 0dd83b8 | 2020-04-03 10:01:03 -0700 | [diff] [blame] | 73 | |
Anas Nashif | 477a04a | 2024-02-28 08:15:15 -0500 | [diff] [blame] | 74 | SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_heap, aligned_alloc, heap, timeout); |
Torbjörn Leksell | 80cd9da | 2021-03-26 13:42:25 +0100 | [diff] [blame] | 75 | |
Andy Ross | 0dd83b8 | 2020-04-03 10:01:03 -0700 | [diff] [blame] | 76 | __ASSERT(!arch_is_in_isr() || K_TIMEOUT_EQ(timeout, K_NO_WAIT), ""); |
| 77 | |
Torbjörn Leksell | 80cd9da | 2021-03-26 13:42:25 +0100 | [diff] [blame] | 78 | bool blocked_alloc = false; |
| 79 | |
Andy Ross | 0dd83b8 | 2020-04-03 10:01:03 -0700 | [diff] [blame] | 80 | while (ret == NULL) { |
Anas Nashif | 477a04a | 2024-02-28 08:15:15 -0500 | [diff] [blame] | 81 | ret = sys_heap_aligned_alloc(&heap->heap, align, bytes); |
Andy Ross | 0dd83b8 | 2020-04-03 10:01:03 -0700 | [diff] [blame] | 82 | |
Krzysztof Chruscinski | c482a57 | 2021-04-19 10:52:34 +0200 | [diff] [blame] | 83 | if (!IS_ENABLED(CONFIG_MULTITHREADING) || |
Nicolas Pitre | 77b7eb1 | 2023-07-06 14:58:03 -0400 | [diff] [blame] | 84 | (ret != NULL) || K_TIMEOUT_EQ(timeout, K_NO_WAIT)) { |
Andy Ross | 0dd83b8 | 2020-04-03 10:01:03 -0700 | [diff] [blame] | 85 | break; |
| 86 | } |
| 87 | |
Torbjörn Leksell | 80cd9da | 2021-03-26 13:42:25 +0100 | [diff] [blame] | 88 | if (!blocked_alloc) { |
| 89 | blocked_alloc = true; |
| 90 | |
Anas Nashif | 477a04a | 2024-02-28 08:15:15 -0500 | [diff] [blame] | 91 | SYS_PORT_TRACING_OBJ_FUNC_BLOCKING(k_heap, aligned_alloc, heap, timeout); |
Torbjörn Leksell | 80cd9da | 2021-03-26 13:42:25 +0100 | [diff] [blame] | 92 | } else { |
| 93 | /** |
| 94 | * @todo Trace attempt to avoid empty trace segments |
| 95 | */ |
| 96 | } |
| 97 | |
Nicolas Pitre | 77b7eb1 | 2023-07-06 14:58:03 -0400 | [diff] [blame] | 98 | timeout = sys_timepoint_timeout(end); |
Anas Nashif | 477a04a | 2024-02-28 08:15:15 -0500 | [diff] [blame] | 99 | (void) z_pend_curr(&heap->lock, key, &heap->wait_q, timeout); |
| 100 | key = k_spin_lock(&heap->lock); |
Andy Ross | 0dd83b8 | 2020-04-03 10:01:03 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Anas Nashif | 477a04a | 2024-02-28 08:15:15 -0500 | [diff] [blame] | 103 | SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_heap, aligned_alloc, heap, timeout, ret); |
Torbjörn Leksell | 80cd9da | 2021-03-26 13:42:25 +0100 | [diff] [blame] | 104 | |
Anas Nashif | 477a04a | 2024-02-28 08:15:15 -0500 | [diff] [blame] | 105 | k_spin_unlock(&heap->lock, key); |
Andy Ross | 0dd83b8 | 2020-04-03 10:01:03 -0700 | [diff] [blame] | 106 | return ret; |
| 107 | } |
| 108 | |
Anas Nashif | 477a04a | 2024-02-28 08:15:15 -0500 | [diff] [blame] | 109 | void *k_heap_alloc(struct k_heap *heap, size_t bytes, k_timeout_t timeout) |
Torbjörn Leksell | 80cd9da | 2021-03-26 13:42:25 +0100 | [diff] [blame] | 110 | { |
Anas Nashif | 477a04a | 2024-02-28 08:15:15 -0500 | [diff] [blame] | 111 | SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_heap, alloc, heap, timeout); |
Torbjörn Leksell | 80cd9da | 2021-03-26 13:42:25 +0100 | [diff] [blame] | 112 | |
Anas Nashif | 477a04a | 2024-02-28 08:15:15 -0500 | [diff] [blame] | 113 | void *ret = k_heap_aligned_alloc(heap, sizeof(void *), bytes, timeout); |
Torbjörn Leksell | 80cd9da | 2021-03-26 13:42:25 +0100 | [diff] [blame] | 114 | |
Anas Nashif | 477a04a | 2024-02-28 08:15:15 -0500 | [diff] [blame] | 115 | SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_heap, alloc, heap, timeout, ret); |
Torbjörn Leksell | 80cd9da | 2021-03-26 13:42:25 +0100 | [diff] [blame] | 116 | |
| 117 | return ret; |
| 118 | } |
| 119 | |
Simone Orru | 37fd711 | 2024-12-18 11:38:05 +0100 | [diff] [blame] | 120 | void *k_heap_calloc(struct k_heap *heap, size_t num, size_t size, k_timeout_t timeout) |
| 121 | { |
| 122 | SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_heap, calloc, heap, timeout); |
| 123 | |
| 124 | void *ret = NULL; |
| 125 | size_t bounds = 0U; |
| 126 | |
| 127 | if (!size_mul_overflow(num, size, &bounds)) { |
| 128 | ret = k_heap_alloc(heap, bounds, timeout); |
| 129 | } |
| 130 | if (ret != NULL) { |
| 131 | (void)memset(ret, 0, bounds); |
| 132 | } |
| 133 | |
| 134 | SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_heap, calloc, heap, timeout, ret); |
| 135 | |
| 136 | return ret; |
| 137 | } |
| 138 | |
Fin Maaß | 09eaa87 | 2024-05-13 09:15:35 +0200 | [diff] [blame] | 139 | void *k_heap_realloc(struct k_heap *heap, void *ptr, size_t bytes, k_timeout_t timeout) |
| 140 | { |
| 141 | k_timepoint_t end = sys_timepoint_calc(timeout); |
| 142 | void *ret = NULL; |
| 143 | |
| 144 | k_spinlock_key_t key = k_spin_lock(&heap->lock); |
| 145 | |
Fin Maaß | 8c37f14 | 2024-05-13 09:17:24 +0200 | [diff] [blame] | 146 | SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_heap, realloc, heap, ptr, bytes, timeout); |
| 147 | |
Fin Maaß | 09eaa87 | 2024-05-13 09:15:35 +0200 | [diff] [blame] | 148 | __ASSERT(!arch_is_in_isr() || K_TIMEOUT_EQ(timeout, K_NO_WAIT), ""); |
| 149 | |
| 150 | while (ret == NULL) { |
| 151 | ret = sys_heap_aligned_realloc(&heap->heap, ptr, sizeof(void *), bytes); |
| 152 | |
| 153 | if (!IS_ENABLED(CONFIG_MULTITHREADING) || |
| 154 | (ret != NULL) || K_TIMEOUT_EQ(timeout, K_NO_WAIT)) { |
| 155 | break; |
| 156 | } |
| 157 | |
| 158 | timeout = sys_timepoint_timeout(end); |
| 159 | (void) z_pend_curr(&heap->lock, key, &heap->wait_q, timeout); |
| 160 | key = k_spin_lock(&heap->lock); |
| 161 | } |
| 162 | |
Fin Maaß | 8c37f14 | 2024-05-13 09:17:24 +0200 | [diff] [blame] | 163 | SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_heap, realloc, heap, ptr, bytes, timeout, ret); |
| 164 | |
Fin Maaß | 09eaa87 | 2024-05-13 09:15:35 +0200 | [diff] [blame] | 165 | k_spin_unlock(&heap->lock, key); |
| 166 | return ret; |
| 167 | } |
| 168 | |
Anas Nashif | 477a04a | 2024-02-28 08:15:15 -0500 | [diff] [blame] | 169 | void k_heap_free(struct k_heap *heap, void *mem) |
Andy Ross | 0dd83b8 | 2020-04-03 10:01:03 -0700 | [diff] [blame] | 170 | { |
Anas Nashif | 477a04a | 2024-02-28 08:15:15 -0500 | [diff] [blame] | 171 | k_spinlock_key_t key = k_spin_lock(&heap->lock); |
Andy Ross | 0dd83b8 | 2020-04-03 10:01:03 -0700 | [diff] [blame] | 172 | |
Anas Nashif | 477a04a | 2024-02-28 08:15:15 -0500 | [diff] [blame] | 173 | sys_heap_free(&heap->heap, mem); |
Andy Ross | 8f0959c | 2020-04-03 15:39:25 -0700 | [diff] [blame] | 174 | |
Anas Nashif | 477a04a | 2024-02-28 08:15:15 -0500 | [diff] [blame] | 175 | SYS_PORT_TRACING_OBJ_FUNC(k_heap, free, heap); |
Hess Nathan | 6d417d5 | 2024-04-30 13:26:35 +0200 | [diff] [blame] | 176 | if (IS_ENABLED(CONFIG_MULTITHREADING) && (z_unpend_all(&heap->wait_q) != 0)) { |
Anas Nashif | 477a04a | 2024-02-28 08:15:15 -0500 | [diff] [blame] | 177 | z_reschedule(&heap->lock, key); |
Andy Ross | 8f0959c | 2020-04-03 15:39:25 -0700 | [diff] [blame] | 178 | } else { |
Anas Nashif | 477a04a | 2024-02-28 08:15:15 -0500 | [diff] [blame] | 179 | k_spin_unlock(&heap->lock, key); |
Andy Ross | 8f0959c | 2020-04-03 15:39:25 -0700 | [diff] [blame] | 180 | } |
Andy Ross | 0dd83b8 | 2020-04-03 10:01:03 -0700 | [diff] [blame] | 181 | } |