paul@93 | 1 | /* |
paul@93 | 2 | * A memory pool allocating a region at a time from the system. |
paul@93 | 3 | * |
paul@623 | 4 | * Copyright (C) 2021, 2022, 2024 Paul Boddie <paul@boddie.org.uk> |
paul@93 | 5 | * |
paul@93 | 6 | * This program is free software; you can redistribute it and/or |
paul@93 | 7 | * modify it under the terms of the GNU General Public License as |
paul@93 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@93 | 9 | * the License, or (at your option) any later version. |
paul@93 | 10 | * |
paul@93 | 11 | * This program is distributed in the hope that it will be useful, |
paul@93 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@93 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@93 | 14 | * GNU General Public License for more details. |
paul@93 | 15 | * |
paul@93 | 16 | * You should have received a copy of the GNU General Public License |
paul@93 | 17 | * along with this program; if not, write to the Free Software |
paul@93 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@93 | 19 | * Boston, MA 02110-1301, USA |
paul@93 | 20 | */ |
paul@93 | 21 | |
paul@623 | 22 | #include <l4/re/c/mem_alloc.h> |
paul@623 | 23 | |
paul@72 | 24 | #include "memory_incremental.h" |
paul@1 | 25 | |
paul@331 | 26 | #include <ipc/mem_ipc.h> |
paul@331 | 27 | |
paul@14 | 28 | #include <stdlib.h> |
paul@1 | 29 | |
paul@110 | 30 | |
paul@110 | 31 | |
paul@1 | 32 | /* Initialise the memory pool with an optional 'limit' in pages. */ |
paul@1 | 33 | |
paul@623 | 34 | MemoryIncremental::MemoryIncremental(unsigned int limit, offset_t region_size, |
paul@623 | 35 | l4_cap_idx_t dma) |
paul@623 | 36 | : _limit(limit), _region_size(region_size), _dma(dma) |
paul@1 | 37 | { |
paul@1 | 38 | _limited = true; |
paul@1 | 39 | } |
paul@1 | 40 | |
paul@623 | 41 | MemoryIncremental::MemoryIncremental(l4_cap_idx_t dma) |
paul@623 | 42 | : _region_size(PAGE_SIZE), _dma(dma) |
paul@1 | 43 | { |
paul@1 | 44 | _limited = false; |
paul@1 | 45 | } |
paul@1 | 46 | |
paul@14 | 47 | /* Allocate a block of the given 'size'. */ |
paul@14 | 48 | |
paul@72 | 49 | Region *MemoryIncremental::allocate(offset_t size) |
paul@14 | 50 | { |
paul@14 | 51 | /* Attempt to allocate aligned memory. */ |
paul@14 | 52 | |
paul@331 | 53 | void *current = NULL; |
paul@331 | 54 | l4re_ds_t ds; |
paul@14 | 55 | |
paul@14 | 56 | /* Make the size appropriate for the invocation. */ |
paul@14 | 57 | |
paul@14 | 58 | size = round_multiple(size, PAGE_SIZE); |
paul@14 | 59 | |
paul@331 | 60 | /* Use allocation permitting executable mapping of the memory. |
paul@341 | 61 | NOTE: Here, it might be beneficial to employ an allocator that obtains |
paul@341 | 62 | dataspaces and provides multiple blocks from each dataspace. */ |
paul@331 | 63 | |
paul@623 | 64 | const l4_size_t attach_flags = L4RE_RM_F_SEARCH_ADDR | L4RE_RM_F_RWX; |
paul@623 | 65 | const l4_size_t alloc_flags = l4_is_valid_cap(_dma) ? |
paul@623 | 66 | L4RE_MA_CONTINUOUS | L4RE_MA_PINNED : 0; |
paul@623 | 67 | |
paul@623 | 68 | long err = ipc_new_dataspace(size, alloc_flags, page_order(size), &ds); |
paul@623 | 69 | |
paul@623 | 70 | if (err) |
paul@623 | 71 | return NULL; |
paul@623 | 72 | |
paul@623 | 73 | err = ipc_attach_dataspace_align(ds, size, attach_flags, page_order(size), |
paul@623 | 74 | (void **) ¤t); |
paul@623 | 75 | |
paul@623 | 76 | if (err) |
paul@14 | 77 | return NULL; |
paul@14 | 78 | |
paul@623 | 79 | /* Obtain physical addresses if a DMA space capability is available. */ |
paul@623 | 80 | |
paul@623 | 81 | l4re_dma_space_dma_addr_t paddr = 0; |
paul@623 | 82 | l4_size_t size_out = size; |
paul@623 | 83 | |
paul@623 | 84 | if (l4_is_valid_cap(_dma)) |
paul@623 | 85 | { |
paul@623 | 86 | long err = l4re_dma_space_map(_dma, ds | L4_CAP_FPAGE_RW, 0, &size_out, |
paul@623 | 87 | 0, L4RE_DMA_SPACE_BIDIRECTIONAL, &paddr); |
paul@623 | 88 | |
paul@623 | 89 | if (err) |
paul@623 | 90 | return NULL; |
paul@623 | 91 | |
paul@623 | 92 | if (size_out < size) |
paul@623 | 93 | return NULL; |
paul@623 | 94 | } |
paul@623 | 95 | |
paul@623 | 96 | return new Region((offset_t) current, (offset_t) current + size, paddr); |
paul@14 | 97 | } |
paul@14 | 98 | |
paul@331 | 99 | /* Deallocate the given region. */ |
paul@331 | 100 | |
paul@331 | 101 | void MemoryIncremental::deallocate(Region *region) |
paul@331 | 102 | { |
paul@331 | 103 | ipc_detach_dataspace((void *) region->start); |
paul@331 | 104 | delete region; |
paul@331 | 105 | } |
paul@331 | 106 | |
paul@1 | 107 | /* Allocate a new region of the given 'size' rounded to the nearest page. */ |
paul@1 | 108 | |
paul@72 | 109 | Region *MemoryIncremental::region(offset_t size) |
paul@1 | 110 | { |
paul@1 | 111 | std::lock_guard<std::mutex> guard(_lock); |
paul@1 | 112 | |
paul@1 | 113 | offset_t rounded = round(size, PAGE_SIZE); |
paul@1 | 114 | offset_t pages = rounded / PAGE_SIZE; |
paul@1 | 115 | |
paul@1 | 116 | /* Check for sufficient pages. */ |
paul@1 | 117 | |
paul@1 | 118 | if (!_limited || (_limit >= pages)) |
paul@1 | 119 | { |
paul@1 | 120 | /* Attempt to allocate aligned memory. */ |
paul@1 | 121 | |
paul@14 | 122 | Region *region = allocate(rounded); |
paul@1 | 123 | |
paul@1 | 124 | if (region == NULL) |
paul@1 | 125 | return NULL; |
paul@1 | 126 | |
paul@1 | 127 | if (_limited) |
paul@1 | 128 | _limit -= pages; |
paul@1 | 129 | |
paul@1 | 130 | return region; |
paul@1 | 131 | } |
paul@1 | 132 | |
paul@1 | 133 | /* Return no region without sufficient pages. */ |
paul@1 | 134 | |
paul@1 | 135 | else |
paul@1 | 136 | return NULL; |
paul@1 | 137 | } |
paul@1 | 138 | |
paul@110 | 139 | /* Allocate a new region having the default region size rounded to the nearest |
paul@110 | 140 | page. */ |
paul@110 | 141 | |
paul@72 | 142 | Region *MemoryIncremental::region() |
paul@1 | 143 | { |
paul@1 | 144 | return region(_region_size); |
paul@1 | 145 | } |
paul@1 | 146 | |
paul@1 | 147 | /* Release the allocated 'region'. */ |
paul@1 | 148 | |
paul@72 | 149 | void MemoryIncremental::release(Region *region) |
paul@1 | 150 | { |
paul@1 | 151 | std::lock_guard<std::mutex> guard(_lock); |
paul@1 | 152 | |
paul@1 | 153 | offset_t rounded = round(region->size(), PAGE_SIZE); |
paul@1 | 154 | offset_t pages = rounded / PAGE_SIZE; |
paul@1 | 155 | |
paul@1 | 156 | if (_limited) |
paul@1 | 157 | _limit += pages; |
paul@1 | 158 | |
paul@331 | 159 | deallocate(region); |
paul@1 | 160 | } |
paul@1 | 161 | |
paul@1 | 162 | // vim: tabstop=4 expandtab shiftwidth=4 |