1 /* 2 * Program memory segment support. 3 * 4 * Copyright (C) 2022 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation; either version 2 of 9 * the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * Boston, MA 02110-1301, USA 20 */ 21 22 #include <l4/sys/err.h> 23 24 #include <ipc/mem_ipc.h> 25 #include <mem/memory_utils.h> 26 27 #include <stdio.h> 28 #include <string.h> 29 30 #include "segment.h" 31 32 33 34 /* Initialise a memory segment. */ 35 36 Segment::Segment(l4_addr_t base, offset_t size, l4re_rm_flags_t flags, 37 offset_t file_offset, offset_t file_contents) 38 39 : _base(base), _size(size), _flags(flags), _file_offset(file_offset), 40 _file_contents(file_contents) 41 { 42 _region_base = trunc(_base, L4_PAGESIZE); 43 _region_offset = _base - _region_base; 44 45 /* Expand the region size. */ 46 47 _region_size = round(_size + _region_offset, L4_PAGESIZE); 48 } 49 50 /* Return the address of allocated memory. */ 51 52 char *Segment::address() 53 { 54 return _buf; 55 } 56 57 /* Return the size of the allocated memory. */ 58 59 offset_t Segment::size() 60 { 61 return _region_size; 62 } 63 64 /* Allocate a writable region for the segment. */ 65 66 long Segment::allocate() 67 { 68 return ipc_allocate_align(_region_size, L4RE_RM_F_SEARCH_ADDR | L4RE_RM_F_RW, 69 L4_PAGESHIFT, (void **) &_buf, &_ds); 70 } 71 72 /* Fill a segment region with file content. */ 73 74 long Segment::fill(file_t *file) 75 { 76 /* NOTE: This should be replaced by copy-on-write dataspace usage. */ 77 78 if (_flags & L4RE_RM_F_W) 79 { 80 long err = allocate(); 81 82 if (err) 83 return err; 84 85 client_seek(file, _file_offset - _region_offset, SEEK_SET); 86 offset_t nread = client_read(file, _buf, _region_size); 87 88 memset(_buf, 0, _region_offset); 89 memset(_buf + _region_offset + _file_contents, 0, _region_size - _region_offset - _file_contents); 90 91 if (nread < _file_contents) 92 return -L4_EIO; 93 else 94 return L4_EOK; 95 } 96 97 /* Provide the exposed file contents in a masked memory mapped region. */ 98 99 _buf = (char *) client_mmap(file, _file_offset - _region_offset, _region_size, 0, 0, _flags); 100 101 if (_buf == NULL) 102 return -L4_EIO; 103 else 104 return L4_EOK; 105 } 106 107 /* Define and return the mapped region for the segment. */ 108 109 MappedRegion &Segment::region() 110 { 111 _region = MappedRegion((l4_addr_t) _buf, _region_size, _flags, _region_base); 112 return _region; 113 } 114 115 /* Return the mapped address corresponding to the given address. */ 116 117 l4_addr_t Segment::region_address(char *address) 118 { 119 return (l4_addr_t) ((address - _buf) + (char *) _region_base); 120 } 121 122 l4_addr_t Segment::region_address(l4_addr_t address) 123 { 124 return (address - (l4_addr_t) _buf) + _region_base; 125 } 126 127 /* Return the amount of file content loaded into the segment. */ 128 129 offset_t Segment::file_contents() 130 { 131 return _file_contents; 132 } 133 134 /* Return the region flags for the segment. */ 135 136 l4re_rm_flags_t Segment::region_flags() 137 { 138 return _flags; 139 } 140 141 /* vim: tabstop=2 expandtab shiftwidth=2 142 */