paul@324 | 1 | /* |
paul@324 | 2 | * Program memory segment support. |
paul@324 | 3 | * |
paul@324 | 4 | * Copyright (C) 2022 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@324 | 24 | #include <ipc/mem_ipc.h> |
paul@324 | 25 | #include <mem/memory_utils.h> |
paul@324 | 26 | |
paul@324 | 27 | #include <stdio.h> |
paul@324 | 28 | #include <string.h> |
paul@324 | 29 | |
paul@324 | 30 | #include "segment.h" |
paul@324 | 31 | |
paul@324 | 32 | |
paul@324 | 33 | |
paul@324 | 34 | /* Initialise a memory segment. */ |
paul@324 | 35 | |
paul@325 | 36 | Segment::Segment(l4_addr_t base, offset_t size, l4re_rm_flags_t flags, |
paul@324 | 37 | offset_t file_offset, offset_t file_contents) |
paul@326 | 38 | |
paul@326 | 39 | : _base(base), _size(size), _flags(flags), _file_offset(file_offset), |
paul@326 | 40 | _file_contents(file_contents) |
paul@324 | 41 | { |
paul@326 | 42 | _region_base = trunc(_base, L4_PAGESIZE); |
paul@326 | 43 | _region_offset = _base - _region_base; |
paul@329 | 44 | |
paul@329 | 45 | /* Expand the region size. */ |
paul@329 | 46 | |
paul@329 | 47 | _region_size = round(_size + _region_offset, L4_PAGESIZE); |
paul@326 | 48 | } |
paul@326 | 49 | |
paul@326 | 50 | /* Return the address of allocated memory. */ |
paul@326 | 51 | |
paul@326 | 52 | char *Segment::address() |
paul@326 | 53 | { |
paul@326 | 54 | return _buf; |
paul@326 | 55 | } |
paul@326 | 56 | |
paul@326 | 57 | /* Return the size of the allocated memory. */ |
paul@326 | 58 | |
paul@326 | 59 | offset_t Segment::size() |
paul@326 | 60 | { |
paul@338 | 61 | return _region_size; |
paul@324 | 62 | } |
paul@324 | 63 | |
paul@329 | 64 | /* Allocate a writable region for the segment. */ |
paul@324 | 65 | |
paul@324 | 66 | long Segment::allocate() |
paul@324 | 67 | { |
paul@329 | 68 | return ipc_allocate_align(_region_size, L4RE_RM_F_SEARCH_ADDR | L4RE_RM_F_RW, |
paul@326 | 69 | L4_PAGESHIFT, (void **) &_buf, &_ds); |
paul@324 | 70 | } |
paul@324 | 71 | |
paul@324 | 72 | /* Fill a segment region with file content. */ |
paul@324 | 73 | |
paul@324 | 74 | long Segment::fill(file_t *file) |
paul@324 | 75 | { |
paul@338 | 76 | /* NOTE: This should be replaced by copy-on-write dataspace usage. */ |
paul@338 | 77 | |
paul@338 | 78 | if (_flags & L4RE_RM_F_W) |
paul@338 | 79 | { |
paul@338 | 80 | long err = allocate(); |
paul@329 | 81 | |
paul@338 | 82 | if (err) |
paul@338 | 83 | return err; |
paul@329 | 84 | |
paul@338 | 85 | client_seek(file, _file_offset - _region_offset, SEEK_SET); |
paul@338 | 86 | offset_t nread = client_read(file, _buf, _region_size); |
paul@329 | 87 | |
paul@338 | 88 | memset(_buf, 0, _region_offset); |
paul@338 | 89 | memset(_buf + _region_offset + _file_contents, 0, _region_size - _region_offset - _file_contents); |
paul@324 | 90 | |
paul@338 | 91 | if (nread < _file_contents) |
paul@338 | 92 | return -L4_EIO; |
paul@338 | 93 | else |
paul@338 | 94 | return L4_EOK; |
paul@338 | 95 | } |
paul@329 | 96 | |
paul@338 | 97 | /* Provide the exposed file contents in a masked memory mapped region. */ |
paul@324 | 98 | |
paul@339 | 99 | _buf = (char *) client_mmap(file, _file_offset - _region_offset, _region_size, 0, 0, _flags); |
paul@338 | 100 | |
paul@338 | 101 | if (_buf == NULL) |
paul@324 | 102 | return -L4_EIO; |
paul@324 | 103 | else |
paul@324 | 104 | return L4_EOK; |
paul@324 | 105 | } |
paul@324 | 106 | |
paul@324 | 107 | /* Define and return the mapped region for the segment. */ |
paul@324 | 108 | |
paul@324 | 109 | MappedRegion &Segment::region() |
paul@324 | 110 | { |
paul@329 | 111 | _region = MappedRegion((l4_addr_t) _buf, _region_size, _flags, _region_base); |
paul@324 | 112 | return _region; |
paul@324 | 113 | } |
paul@324 | 114 | |
paul@324 | 115 | /* Return the mapped address corresponding to the given address. */ |
paul@324 | 116 | |
paul@324 | 117 | l4_addr_t Segment::region_address(char *address) |
paul@324 | 118 | { |
paul@326 | 119 | return (l4_addr_t) ((address - _buf) + (char *) _region_base); |
paul@324 | 120 | } |
paul@324 | 121 | |
paul@324 | 122 | l4_addr_t Segment::region_address(l4_addr_t address) |
paul@324 | 123 | { |
paul@326 | 124 | return (address - (l4_addr_t) _buf) + _region_base; |
paul@324 | 125 | } |
paul@324 | 126 | |
paul@329 | 127 | /* Return the amount of file content loaded into the segment. */ |
paul@329 | 128 | |
paul@329 | 129 | offset_t Segment::file_contents() |
paul@329 | 130 | { |
paul@329 | 131 | return _file_contents; |
paul@329 | 132 | } |
paul@329 | 133 | |
paul@329 | 134 | /* Return the region flags for the segment. */ |
paul@329 | 135 | |
paul@329 | 136 | l4re_rm_flags_t Segment::region_flags() |
paul@329 | 137 | { |
paul@329 | 138 | return _flags; |
paul@329 | 139 | } |
paul@329 | 140 | |
paul@324 | 141 | /* vim: tabstop=2 expandtab shiftwidth=2 |
paul@324 | 142 | */ |