1 /* 2 * Program memory segment support. 3 * 4 * Copyright (C) 2022, 2023, 2024 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 #pragma once 23 24 #include <exec/common.h> 25 #include <exec/mapped_region.h> 26 #include <fsclient/client.h> 27 #include <systypes/base.h> 28 29 30 31 /* Program segment abstractions. */ 32 33 class Segment 34 { 35 protected: 36 MappedRegion _region; 37 struct exec_region _exec_region; 38 39 /* Allocated memory. */ 40 41 char *_buf; 42 l4_cap_idx_t _ds; 43 file_t *_file = NULL; 44 bool _allocated = false, _attached = false; 45 46 /* Segment region base. */ 47 48 l4_addr_t _region_base; 49 50 /* Segment region size. */ 51 52 offset_t _region_allocated_size; 53 54 /* Offset of segment content within the region. */ 55 56 offset_t _region_content_offset; 57 58 /* Common initialisation. */ 59 60 void init(); 61 62 public: 63 virtual ~Segment(); 64 65 l4_cap_idx_t cap(); 66 67 char *address(); 68 69 offset_t size(); 70 71 /* Segment population methods. */ 72 73 long allocate(bool attach); 74 75 long fill(file_t *file, bool attach); 76 77 /* Mapped region methods. */ 78 79 MappedRegion ®ion(); 80 81 l4_addr_t region_address(char *address); 82 83 l4_addr_t region_address(l4_addr_t address); 84 85 /* Generic property access. */ 86 87 struct exec_region &exec_region(); 88 89 virtual bool loadable() = 0; 90 virtual offset_t file_contents() = 0; 91 virtual offset_t file_offset() = 0; 92 virtual l4_addr_t region_address() = 0; 93 virtual offset_t region_size() = 0; 94 virtual rm_flags_t region_flags() = 0; 95 }; 96 97 98 99 /* An explicitly created segment independent of any particular payload. */ 100 101 class ExplicitSegment : public Segment 102 { 103 protected: 104 /* Segment base. */ 105 106 l4_addr_t _base; 107 108 /* Segment size. */ 109 110 offset_t _size; 111 112 /* Access flags. */ 113 114 rm_flags_t _flags; 115 116 /* File access details. */ 117 118 offset_t _file_offset, _file_contents; 119 120 public: 121 explicit ExplicitSegment(l4_addr_t base, offset_t size, rm_flags_t flags, 122 offset_t file_offset = 0, offset_t file_contents = 0); 123 124 /* Generic property access. */ 125 126 bool loadable(); 127 offset_t file_contents(); 128 offset_t file_offset(); 129 l4_addr_t region_address(); 130 offset_t region_size(); 131 rm_flags_t region_flags(); 132 }; 133 134 /* vim: tabstop=2 expandtab shiftwidth=2 135 */