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); |
| 18 | sys_heap_init(&heap->heap, mem, bytes); |
Torbjörn Leksell | 80cd9da | 2021-03-26 13:42:25 +0100 | [diff] [blame] | 19 | |
Anas Nashif | 477a04a | 2024-02-28 08:15:15 -0500 | [diff] [blame] | 20 | SYS_PORT_TRACING_OBJ_INIT(k_heap, heap); |
Andy Ross | 0dd83b8 | 2020-04-03 10:01:03 -0700 | [diff] [blame] | 21 | } |
| 22 | |
Gerard Marull-Paretas | a5fd0d1 | 2022-10-19 09:33:44 +0200 | [diff] [blame] | 23 | static int statics_init(void) |
Andy Ross | 0dd83b8 | 2020-04-03 10:01:03 -0700 | [diff] [blame] | 24 | { |
Anas Nashif | 477a04a | 2024-02-28 08:15:15 -0500 | [diff] [blame] | 25 | STRUCT_SECTION_FOREACH(k_heap, heap) { |
Daniel Leung | 5c4fff3 | 2021-08-03 13:59:36 -0700 | [diff] [blame] | 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 | */ |
Anas Nashif | 477a04a | 2024-02-28 08:15:15 -0500 | [diff] [blame] | 40 | if (lnkr_is_pinned((uint8_t *)heap) && |
| 41 | lnkr_is_pinned((uint8_t *)&heap->wait_q) && |
| 42 | lnkr_is_region_pinned((uint8_t *)heap->heap.init_mem, |
| 43 | heap->heap.init_bytes)) { |
Daniel Leung | 5c4fff3 | 2021-08-03 13:59:36 -0700 | [diff] [blame] | 44 | do_clear = !do_clear; |
| 45 | } |
| 46 | |
| 47 | if (do_clear) |
| 48 | #endif /* CONFIG_DEMAND_PAGING && !CONFIG_LINKER_GENERIC_SECTIONS_PRESENT_AT_BOOT */ |
| 49 | { |
Anas Nashif | 477a04a | 2024-02-28 08:15:15 -0500 | [diff] [blame] | 50 | k_heap_init(heap, heap->heap.init_mem, heap->heap.init_bytes); |
Daniel Leung | 5c4fff3 | 2021-08-03 13:59:36 -0700 | [diff] [blame] | 51 | } |
Andy Ross | 0dd83b8 | 2020-04-03 10:01:03 -0700 | [diff] [blame] | 52 | } |
| 53 | return 0; |
| 54 | } |
| 55 | |
Jordan Yates | 6f41d52 | 2022-07-02 12:06:55 +1000 | [diff] [blame] | 56 | 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] | 57 | |
Daniel Leung | 5c4fff3 | 2021-08-03 13:59:36 -0700 | [diff] [blame] | 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 | */ |
Jordan Yates | 6f41d52 | 2022-07-02 12:06:55 +1000 | [diff] [blame] | 62 | SYS_INIT_NAMED(statics_init_post, statics_init, POST_KERNEL, 0); |
Daniel Leung | 5c4fff3 | 2021-08-03 13:59:36 -0700 | [diff] [blame] | 63 | #endif /* CONFIG_DEMAND_PAGING && !CONFIG_LINKER_GENERIC_SECTIONS_PRESENT_AT_BOOT */ |
| 64 | |
Anas Nashif | 477a04a | 2024-02-28 08:15:15 -0500 | [diff] [blame] | 65 | 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] | 66 | k_timeout_t timeout) |
Andy Ross | 0dd83b8 | 2020-04-03 10:01:03 -0700 | [diff] [blame] | 67 | { |
Nicolas Pitre | 77b7eb1 | 2023-07-06 14:58:03 -0400 | [diff] [blame] | 68 | k_timepoint_t end = sys_timepoint_calc(timeout); |
Andy Ross | 0dd83b8 | 2020-04-03 10:01:03 -0700 | [diff] [blame] | 69 | void *ret = NULL; |
Jay Shoen | b4734d5 | 2022-09-28 17:59:45 -0600 | [diff] [blame] | 70 | |
Anas Nashif | 477a04a | 2024-02-28 08:15:15 -0500 | [diff] [blame] | 71 | k_spinlock_key_t key = k_spin_lock(&heap->lock); |
Andy Ross | 0dd83b8 | 2020-04-03 10:01:03 -0700 | [diff] [blame] | 72 | |
Anas Nashif | 477a04a | 2024-02-28 08:15:15 -0500 | [diff] [blame] | 73 | 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] | 74 | |
Andy Ross | 0dd83b8 | 2020-04-03 10:01:03 -0700 | [diff] [blame] | 75 | __ASSERT(!arch_is_in_isr() || K_TIMEOUT_EQ(timeout, K_NO_WAIT), ""); |
| 76 | |
Torbjörn Leksell | 80cd9da | 2021-03-26 13:42:25 +0100 | [diff] [blame] | 77 | bool blocked_alloc = false; |
| 78 | |
Andy Ross | 0dd83b8 | 2020-04-03 10:01:03 -0700 | [diff] [blame] | 79 | while (ret == NULL) { |
Anas Nashif | 477a04a | 2024-02-28 08:15:15 -0500 | [diff] [blame] | 80 | ret = sys_heap_aligned_alloc(&heap->heap, align, bytes); |
Andy Ross | 0dd83b8 | 2020-04-03 10:01:03 -0700 | [diff] [blame] | 81 | |
Krzysztof Chruscinski | c482a57 | 2021-04-19 10:52:34 +0200 | [diff] [blame] | 82 | if (!IS_ENABLED(CONFIG_MULTITHREADING) || |
Nicolas Pitre | 77b7eb1 | 2023-07-06 14:58:03 -0400 | [diff] [blame] | 83 | (ret != NULL) || K_TIMEOUT_EQ(timeout, K_NO_WAIT)) { |
Andy Ross | 0dd83b8 | 2020-04-03 10:01:03 -0700 | [diff] [blame] | 84 | break; |
| 85 | } |
| 86 | |
Torbjörn Leksell | 80cd9da | 2021-03-26 13:42:25 +0100 | [diff] [blame] | 87 | if (!blocked_alloc) { |
| 88 | blocked_alloc = true; |
| 89 | |
Anas Nashif | 477a04a | 2024-02-28 08:15:15 -0500 | [diff] [blame] | 90 | 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] | 91 | } else { |
| 92 | /** |
| 93 | * @todo Trace attempt to avoid empty trace segments |
| 94 | */ |
| 95 | } |
| 96 | |
Nicolas Pitre | 77b7eb1 | 2023-07-06 14:58:03 -0400 | [diff] [blame] | 97 | timeout = sys_timepoint_timeout(end); |
Anas Nashif | 477a04a | 2024-02-28 08:15:15 -0500 | [diff] [blame] | 98 | (void) z_pend_curr(&heap->lock, key, &heap->wait_q, timeout); |
| 99 | key = k_spin_lock(&heap->lock); |
Andy Ross | 0dd83b8 | 2020-04-03 10:01:03 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Anas Nashif | 477a04a | 2024-02-28 08:15:15 -0500 | [diff] [blame] | 102 | 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] | 103 | |
Anas Nashif | 477a04a | 2024-02-28 08:15:15 -0500 | [diff] [blame] | 104 | k_spin_unlock(&heap->lock, key); |
Andy Ross | 0dd83b8 | 2020-04-03 10:01:03 -0700 | [diff] [blame] | 105 | return ret; |
| 106 | } |
| 107 | |
Anas Nashif | 477a04a | 2024-02-28 08:15:15 -0500 | [diff] [blame] | 108 | 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] | 109 | { |
Anas Nashif | 477a04a | 2024-02-28 08:15:15 -0500 | [diff] [blame] | 110 | SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_heap, alloc, heap, timeout); |
Torbjörn Leksell | 80cd9da | 2021-03-26 13:42:25 +0100 | [diff] [blame] | 111 | |
Anas Nashif | 477a04a | 2024-02-28 08:15:15 -0500 | [diff] [blame] | 112 | void *ret = k_heap_aligned_alloc(heap, sizeof(void *), bytes, timeout); |
Torbjörn Leksell | 80cd9da | 2021-03-26 13:42:25 +0100 | [diff] [blame] | 113 | |
Anas Nashif | 477a04a | 2024-02-28 08:15:15 -0500 | [diff] [blame] | 114 | 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] | 115 | |
| 116 | return ret; |
| 117 | } |
| 118 | |
Anas Nashif | 477a04a | 2024-02-28 08:15:15 -0500 | [diff] [blame] | 119 | void k_heap_free(struct k_heap *heap, void *mem) |
Andy Ross | 0dd83b8 | 2020-04-03 10:01:03 -0700 | [diff] [blame] | 120 | { |
Anas Nashif | 477a04a | 2024-02-28 08:15:15 -0500 | [diff] [blame] | 121 | k_spinlock_key_t key = k_spin_lock(&heap->lock); |
Andy Ross | 0dd83b8 | 2020-04-03 10:01:03 -0700 | [diff] [blame] | 122 | |
Anas Nashif | 477a04a | 2024-02-28 08:15:15 -0500 | [diff] [blame] | 123 | sys_heap_free(&heap->heap, mem); |
Andy Ross | 8f0959c | 2020-04-03 15:39:25 -0700 | [diff] [blame] | 124 | |
Anas Nashif | 477a04a | 2024-02-28 08:15:15 -0500 | [diff] [blame] | 125 | SYS_PORT_TRACING_OBJ_FUNC(k_heap, free, heap); |
Hess Nathan | 6d417d5 | 2024-04-30 13:26:35 +0200 | [diff] [blame] | 126 | if (IS_ENABLED(CONFIG_MULTITHREADING) && (z_unpend_all(&heap->wait_q) != 0)) { |
Anas Nashif | 477a04a | 2024-02-28 08:15:15 -0500 | [diff] [blame] | 127 | z_reschedule(&heap->lock, key); |
Andy Ross | 8f0959c | 2020-04-03 15:39:25 -0700 | [diff] [blame] | 128 | } else { |
Anas Nashif | 477a04a | 2024-02-28 08:15:15 -0500 | [diff] [blame] | 129 | k_spin_unlock(&heap->lock, key); |
Andy Ross | 8f0959c | 2020-04-03 15:39:25 -0700 | [diff] [blame] | 130 | } |
Andy Ross | 0dd83b8 | 2020-04-03 10:01:03 -0700 | [diff] [blame] | 131 | } |