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@632 | 23 | #include <l4/re/c/rm.h> |
paul@623 | 24 | |
paul@72 | 25 | #include "memory_incremental.h" |
paul@1 | 26 | |
paul@331 | 27 | #include <ipc/mem_ipc.h> |
paul@331 | 28 | |
paul@14 | 29 | #include <stdlib.h> |
paul@1 | 30 | |
paul@110 | 31 | |
paul@110 | 32 | |
paul@1 | 33 | /* Initialise the memory pool with an optional 'limit' in pages. */ |
paul@1 | 34 | |
paul@623 | 35 | MemoryIncremental::MemoryIncremental(unsigned int limit, offset_t region_size, |
paul@623 | 36 | l4_cap_idx_t dma) |
paul@623 | 37 | : _limit(limit), _region_size(region_size), _dma(dma) |
paul@1 | 38 | { |
paul@1 | 39 | _limited = true; |
paul@1 | 40 | } |
paul@1 | 41 | |
paul@623 | 42 | MemoryIncremental::MemoryIncremental(l4_cap_idx_t dma) |
paul@623 | 43 | : _region_size(PAGE_SIZE), _dma(dma) |
paul@1 | 44 | { |
paul@1 | 45 | _limited = false; |
paul@1 | 46 | } |
paul@1 | 47 | |
paul@14 | 48 | /* Allocate a block of the given 'size'. */ |
paul@14 | 49 | |
paul@72 | 50 | Region *MemoryIncremental::allocate(offset_t size) |
paul@14 | 51 | { |
paul@14 | 52 | /* Attempt to allocate aligned memory. */ |
paul@14 | 53 | |
paul@331 | 54 | void *current = NULL; |
paul@640 | 55 | l4_cap_idx_t ds; |
paul@14 | 56 | |
paul@14 | 57 | /* Make the size appropriate for the invocation. */ |
paul@14 | 58 | |
paul@14 | 59 | size = round_multiple(size, PAGE_SIZE); |
paul@14 | 60 | |
paul@331 | 61 | /* Use allocation permitting executable mapping of the memory. |
paul@341 | 62 | NOTE: Here, it might be beneficial to employ an allocator that obtains |
paul@341 | 63 | dataspaces and provides multiple blocks from each dataspace. */ |
paul@331 | 64 | |
paul@623 | 65 | const l4_size_t attach_flags = L4RE_RM_F_SEARCH_ADDR | L4RE_RM_F_RWX; |
paul@623 | 66 | const l4_size_t alloc_flags = l4_is_valid_cap(_dma) ? |
paul@623 | 67 | L4RE_MA_CONTINUOUS | L4RE_MA_PINNED : 0; |
paul@623 | 68 | |
paul@623 | 69 | long err = ipc_new_dataspace(size, alloc_flags, page_order(size), &ds); |
paul@623 | 70 | |
paul@623 | 71 | if (err) |
paul@623 | 72 | return NULL; |
paul@623 | 73 | |
paul@623 | 74 | err = ipc_attach_dataspace_align(ds, size, attach_flags, page_order(size), |
paul@623 | 75 | (void **) ¤t); |
paul@623 | 76 | |
paul@623 | 77 | if (err) |
paul@14 | 78 | return NULL; |
paul@14 | 79 | |
paul@623 | 80 | /* Obtain physical addresses if a DMA space capability is available. */ |
paul@623 | 81 | |
paul@623 | 82 | l4re_dma_space_dma_addr_t paddr = 0; |
paul@623 | 83 | l4_size_t size_out = size; |
paul@623 | 84 | |
paul@623 | 85 | if (l4_is_valid_cap(_dma)) |
paul@623 | 86 | { |
paul@623 | 87 | long err = l4re_dma_space_map(_dma, ds | L4_CAP_FPAGE_RW, 0, &size_out, |
paul@623 | 88 | 0, L4RE_DMA_SPACE_BIDIRECTIONAL, &paddr); |
paul@623 | 89 | |
paul@623 | 90 | if (err) |
paul@623 | 91 | return NULL; |
paul@623 | 92 | |
paul@623 | 93 | if (size_out < size) |
paul@623 | 94 | return NULL; |
paul@623 | 95 | } |
paul@623 | 96 | |
paul@623 | 97 | return new Region((offset_t) current, (offset_t) current + size, paddr); |
paul@14 | 98 | } |
paul@14 | 99 | |
paul@331 | 100 | /* Deallocate the given region. */ |
paul@331 | 101 | |
paul@331 | 102 | void MemoryIncremental::deallocate(Region *region) |
paul@331 | 103 | { |
paul@331 | 104 | ipc_detach_dataspace((void *) region->start); |
paul@331 | 105 | delete region; |
paul@331 | 106 | } |
paul@331 | 107 | |
paul@1 | 108 | /* Allocate a new region of the given 'size' rounded to the nearest page. */ |
paul@1 | 109 | |
paul@72 | 110 | Region *MemoryIncremental::region(offset_t size) |
paul@1 | 111 | { |
paul@1 | 112 | std::lock_guard<std::mutex> guard(_lock); |
paul@1 | 113 | |
paul@1 | 114 | offset_t rounded = round(size, PAGE_SIZE); |
paul@1 | 115 | offset_t pages = rounded / PAGE_SIZE; |
paul@1 | 116 | |
paul@1 | 117 | /* Check for sufficient pages. */ |
paul@1 | 118 | |
paul@1 | 119 | if (!_limited || (_limit >= pages)) |
paul@1 | 120 | { |
paul@1 | 121 | /* Attempt to allocate aligned memory. */ |
paul@1 | 122 | |
paul@14 | 123 | Region *region = allocate(rounded); |
paul@1 | 124 | |
paul@1 | 125 | if (region == NULL) |
paul@1 | 126 | return NULL; |
paul@1 | 127 | |
paul@1 | 128 | if (_limited) |
paul@1 | 129 | _limit -= pages; |
paul@1 | 130 | |
paul@1 | 131 | return region; |
paul@1 | 132 | } |
paul@1 | 133 | |
paul@1 | 134 | /* Return no region without sufficient pages. */ |
paul@1 | 135 | |
paul@1 | 136 | else |
paul@1 | 137 | return NULL; |
paul@1 | 138 | } |
paul@1 | 139 | |
paul@110 | 140 | /* Allocate a new region having the default region size rounded to the nearest |
paul@110 | 141 | page. */ |
paul@110 | 142 | |
paul@72 | 143 | Region *MemoryIncremental::region() |
paul@1 | 144 | { |
paul@1 | 145 | return region(_region_size); |
paul@1 | 146 | } |
paul@1 | 147 | |
paul@1 | 148 | /* Release the allocated 'region'. */ |
paul@1 | 149 | |
paul@72 | 150 | void MemoryIncremental::release(Region *region) |
paul@1 | 151 | { |
paul@1 | 152 | std::lock_guard<std::mutex> guard(_lock); |
paul@1 | 153 | |
paul@1 | 154 | offset_t rounded = round(region->size(), PAGE_SIZE); |
paul@1 | 155 | offset_t pages = rounded / PAGE_SIZE; |
paul@1 | 156 | |
paul@1 | 157 | if (_limited) |
paul@1 | 158 | _limit += pages; |
paul@1 | 159 | |
paul@331 | 160 | deallocate(region); |
paul@1 | 161 | } |
paul@1 | 162 | |
paul@1 | 163 | // vim: tabstop=4 expandtab shiftwidth=4 |