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