paul@324 | 1 | /* |
paul@324 | 2 | * Program memory segment support. |
paul@324 | 3 | * |
paul@634 | 4 | * Copyright (C) 2022, 2023, 2024 Paul Boddie <paul@boddie.org.uk> |
paul@324 | 5 | * |
paul@324 | 6 | * This program is free software; you can redistribute it and/or |
paul@324 | 7 | * modify it under the terms of the GNU General Public License as |
paul@324 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@324 | 9 | * the License, or (at your option) any later version. |
paul@324 | 10 | * |
paul@324 | 11 | * This program is distributed in the hope that it will be useful, |
paul@324 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@324 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@324 | 14 | * GNU General Public License for more details. |
paul@324 | 15 | * |
paul@324 | 16 | * You should have received a copy of the GNU General Public License |
paul@324 | 17 | * along with this program; if not, write to the Free Software |
paul@324 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@324 | 19 | * Boston, MA 02110-1301, USA |
paul@324 | 20 | */ |
paul@324 | 21 | |
paul@324 | 22 | #include <l4/sys/err.h> |
paul@324 | 23 | |
paul@494 | 24 | #include <ipc/cap_alloc.h> |
paul@324 | 25 | #include <ipc/mem_ipc.h> |
paul@324 | 26 | #include <mem/memory_utils.h> |
paul@324 | 27 | |
paul@324 | 28 | #include <stdio.h> |
paul@324 | 29 | #include <string.h> |
paul@324 | 30 | |
paul@324 | 31 | #include "segment.h" |
paul@324 | 32 | |
paul@324 | 33 | |
paul@324 | 34 | |
paul@494 | 35 | /* Free the allocated resources. */ |
paul@324 | 36 | |
paul@349 | 37 | Segment::~Segment() |
paul@349 | 38 | { |
paul@494 | 39 | if (_allocated) |
paul@494 | 40 | { |
paul@494 | 41 | if (_attached) |
paul@494 | 42 | ipc_detach_dataspace(_buf); |
paul@494 | 43 | |
paul@494 | 44 | /* Also unmap the dataspace, not apparently done by the underlying |
paul@494 | 45 | functions. */ |
paul@494 | 46 | |
paul@494 | 47 | if (l4_is_valid_cap(_ds)) |
paul@494 | 48 | ipc_cap_free_um(_ds); |
paul@494 | 49 | } |
paul@494 | 50 | else if (client_opened(_file)) |
paul@494 | 51 | { |
paul@494 | 52 | client_close(_file); |
paul@520 | 53 | _file = NULL; |
paul@494 | 54 | } |
paul@349 | 55 | } |
paul@326 | 56 | |
paul@349 | 57 | /* Common initialisation. */ |
paul@349 | 58 | |
paul@349 | 59 | void Segment::init() |
paul@324 | 60 | { |
paul@349 | 61 | _region_base = trunc(region_address(), L4_PAGESIZE); |
paul@349 | 62 | _region_content_offset = region_address() - _region_base; |
paul@329 | 63 | |
paul@329 | 64 | /* Expand the region size. */ |
paul@329 | 65 | |
paul@349 | 66 | _region_allocated_size = round(region_size() + _region_content_offset, L4_PAGESIZE); |
paul@326 | 67 | } |
paul@326 | 68 | |
paul@365 | 69 | /* Return the capability of the dataspace providing the memory. */ |
paul@365 | 70 | |
paul@365 | 71 | l4_cap_idx_t Segment::cap() |
paul@365 | 72 | { |
paul@365 | 73 | return _ds; |
paul@365 | 74 | } |
paul@365 | 75 | |
paul@326 | 76 | /* Return the address of allocated memory. */ |
paul@326 | 77 | |
paul@326 | 78 | char *Segment::address() |
paul@326 | 79 | { |
paul@326 | 80 | return _buf; |
paul@326 | 81 | } |
paul@326 | 82 | |
paul@326 | 83 | /* Return the size of the allocated memory. */ |
paul@326 | 84 | |
paul@326 | 85 | offset_t Segment::size() |
paul@326 | 86 | { |
paul@349 | 87 | return _region_allocated_size; |
paul@324 | 88 | } |
paul@324 | 89 | |
paul@329 | 90 | /* Allocate a writable region for the segment. */ |
paul@324 | 91 | |
paul@365 | 92 | long Segment::allocate(bool attach) |
paul@324 | 93 | { |
paul@494 | 94 | _allocated = true; |
paul@494 | 95 | _attached = attach; |
paul@494 | 96 | |
paul@365 | 97 | if (attach) |
paul@365 | 98 | return ipc_allocate_align(_region_allocated_size, |
paul@365 | 99 | L4RE_RM_F_SEARCH_ADDR | L4RE_RM_F_RW, |
paul@365 | 100 | L4_PAGESHIFT, (void **) &_buf, &_ds); |
paul@365 | 101 | else |
paul@365 | 102 | return ipc_new_dataspace(_region_allocated_size, |
paul@365 | 103 | L4RE_RM_F_SEARCH_ADDR | L4RE_RM_F_RW, |
paul@365 | 104 | L4_PAGESHIFT, &_ds); |
paul@324 | 105 | } |
paul@324 | 106 | |
paul@324 | 107 | /* Fill a segment region with file content. */ |
paul@324 | 108 | |
paul@365 | 109 | long Segment::fill(file_t *file, bool attach) |
paul@324 | 110 | { |
paul@365 | 111 | /* Provide the exposed file contents in a masked memory mapped region, or if |
paul@365 | 112 | not attaching the region, merely request access via a dataspace. */ |
paul@324 | 113 | |
paul@365 | 114 | if (attach) |
paul@365 | 115 | { |
paul@365 | 116 | _buf = (char *) client_mmap(file, |
paul@365 | 117 | file_offset() - _region_content_offset, |
paul@365 | 118 | _region_allocated_size, |
paul@365 | 119 | file_offset(), |
paul@365 | 120 | file_offset() + file_contents(), |
paul@365 | 121 | region_flags()); |
paul@365 | 122 | if (_buf == NULL) |
paul@365 | 123 | return -L4_EIO; |
paul@365 | 124 | } |
paul@365 | 125 | else |
paul@365 | 126 | { |
paul@365 | 127 | long err = file_mmap_only(file, |
paul@349 | 128 | file_offset() - _region_content_offset, |
paul@349 | 129 | _region_allocated_size, |
paul@349 | 130 | file_offset(), |
paul@365 | 131 | file_offset() + file_contents()); |
paul@365 | 132 | if (err) |
paul@365 | 133 | return err; |
paul@365 | 134 | } |
paul@338 | 135 | |
paul@494 | 136 | _file = file; |
paul@365 | 137 | _ds = file->ref; |
paul@365 | 138 | return L4_EOK; |
paul@324 | 139 | } |
paul@324 | 140 | |
paul@324 | 141 | /* Define and return the mapped region for the segment. */ |
paul@324 | 142 | |
paul@324 | 143 | MappedRegion &Segment::region() |
paul@324 | 144 | { |
paul@515 | 145 | _region = MappedRegion(_region_base, _region_base + _region_allocated_size, |
paul@515 | 146 | region_flags(), _ds, (l4_addr_t) _buf); |
paul@324 | 147 | return _region; |
paul@324 | 148 | } |
paul@324 | 149 | |
paul@365 | 150 | /* Define and return the region dataspace details. */ |
paul@365 | 151 | |
paul@365 | 152 | struct exec_region &Segment::exec_region() |
paul@365 | 153 | { |
paul@365 | 154 | _exec_region = (struct exec_region) {_region_base, _region_allocated_size, region_flags(), _ds}; |
paul@365 | 155 | return _exec_region; |
paul@365 | 156 | } |
paul@365 | 157 | |
paul@324 | 158 | /* Return the mapped address corresponding to the given address. */ |
paul@324 | 159 | |
paul@324 | 160 | l4_addr_t Segment::region_address(char *address) |
paul@324 | 161 | { |
paul@326 | 162 | return (l4_addr_t) ((address - _buf) + (char *) _region_base); |
paul@324 | 163 | } |
paul@324 | 164 | |
paul@324 | 165 | l4_addr_t Segment::region_address(l4_addr_t address) |
paul@324 | 166 | { |
paul@326 | 167 | return (address - (l4_addr_t) _buf) + _region_base; |
paul@324 | 168 | } |
paul@324 | 169 | |
paul@349 | 170 | |
paul@349 | 171 | |
paul@349 | 172 | /* Initialise a memory segment using explicit parameters. */ |
paul@349 | 173 | |
paul@349 | 174 | ExplicitSegment::ExplicitSegment(l4_addr_t base, offset_t size, |
paul@634 | 175 | rm_flags_t flags, offset_t file_offset, |
paul@349 | 176 | offset_t file_contents) |
paul@349 | 177 | : _base(base), _size(size), _flags(flags), _file_offset(file_offset), |
paul@349 | 178 | _file_contents(file_contents) |
paul@349 | 179 | { |
paul@349 | 180 | init(); |
paul@349 | 181 | } |
paul@349 | 182 | |
paul@329 | 183 | /* Return the amount of file content loaded into the segment. */ |
paul@329 | 184 | |
paul@349 | 185 | bool ExplicitSegment::loadable() |
paul@349 | 186 | { |
paul@349 | 187 | return false; |
paul@349 | 188 | } |
paul@349 | 189 | |
paul@349 | 190 | offset_t ExplicitSegment::file_contents() |
paul@329 | 191 | { |
paul@329 | 192 | return _file_contents; |
paul@329 | 193 | } |
paul@329 | 194 | |
paul@349 | 195 | offset_t ExplicitSegment::file_offset() |
paul@349 | 196 | { |
paul@349 | 197 | return _file_offset; |
paul@349 | 198 | } |
paul@349 | 199 | |
paul@349 | 200 | l4_addr_t ExplicitSegment::region_address() |
paul@349 | 201 | { |
paul@349 | 202 | return _base; |
paul@349 | 203 | } |
paul@349 | 204 | |
paul@349 | 205 | offset_t ExplicitSegment::region_size() |
paul@349 | 206 | { |
paul@349 | 207 | return _size; |
paul@349 | 208 | } |
paul@349 | 209 | |
paul@329 | 210 | /* Return the region flags for the segment. */ |
paul@329 | 211 | |
paul@634 | 212 | rm_flags_t ExplicitSegment::region_flags() |
paul@329 | 213 | { |
paul@329 | 214 | return _flags; |
paul@329 | 215 | } |
paul@329 | 216 | |
paul@324 | 217 | /* vim: tabstop=2 expandtab shiftwidth=2 |
paul@324 | 218 | */ |