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 _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 /* Allocate writable memory to populate. */ 77 78 long err = ipc_allocate_align(_region_size, 79 L4RE_RM_F_SEARCH_ADDR | _flags | L4RE_RM_F_W, 80 L4_PAGESHIFT, (void **) &_buf, &_ds); 81 82 if (err) 83 return err; 84 85 /* Align the exposed file contents to the memory mapped region. 86 Since the region is page-aligned, the region offset is introduced to map 87 from an earlier part of the file, if appropriate. */ 88 89 memset(_buf, 0, _region_size); 90 91 client_seek(file, _file_offset, SEEK_SET); 92 offset_t nread = client_read(file, _buf + _region_offset, _file_contents); 93 94 if (nread < _file_contents) 95 return -L4_EIO; 96 else 97 return L4_EOK; 98 } 99 100 /* Define and return the mapped region for the segment. */ 101 102 MappedRegion &Segment::region() 103 { 104 _region = MappedRegion((l4_addr_t) _buf, _region_size, _flags, _region_base); 105 return _region; 106 } 107 108 /* Return the mapped address corresponding to the given address. */ 109 110 l4_addr_t Segment::region_address(char *address) 111 { 112 return (l4_addr_t) ((address - _buf) + (char *) _region_base); 113 } 114 115 l4_addr_t Segment::region_address(l4_addr_t address) 116 { 117 return (address - (l4_addr_t) _buf) + _region_base; 118 } 119 120 /* Return the amount of file content loaded into the segment. */ 121 122 offset_t Segment::file_contents() 123 { 124 return _file_contents; 125 } 126 127 /* Return the region flags for the segment. */ 128 129 l4re_rm_flags_t Segment::region_flags() 130 { 131 return _flags; 132 } 133 134 /* vim: tabstop=2 expandtab shiftwidth=2 135 */