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@326 | 44 | _region_size = round(_size, L4_PAGESIZE); |
paul@326 | 45 | } |
paul@326 | 46 | |
paul@326 | 47 | /* Return the address of allocated memory. */ |
paul@326 | 48 | |
paul@326 | 49 | char *Segment::address() |
paul@326 | 50 | { |
paul@326 | 51 | return _buf; |
paul@326 | 52 | } |
paul@326 | 53 | |
paul@326 | 54 | /* Return the size of the allocated memory. */ |
paul@326 | 55 | |
paul@326 | 56 | offset_t Segment::size() |
paul@326 | 57 | { |
paul@326 | 58 | return _size; |
paul@324 | 59 | } |
paul@324 | 60 | |
paul@324 | 61 | /* Allocate a region for the segment. */ |
paul@324 | 62 | |
paul@324 | 63 | long Segment::allocate() |
paul@324 | 64 | { |
paul@325 | 65 | /* Make regions writable if they need to be filled. */ |
paul@325 | 66 | |
paul@326 | 67 | l4re_rm_flags_t allocation_flags = _flags; |
paul@325 | 68 | |
paul@326 | 69 | if (_file_contents) |
paul@325 | 70 | allocation_flags |= L4RE_RM_F_W; |
paul@325 | 71 | |
paul@326 | 72 | return ipc_allocate_align(_size, L4RE_RM_F_SEARCH_ADDR | allocation_flags, |
paul@326 | 73 | L4_PAGESHIFT, (void **) &_buf, &_ds); |
paul@324 | 74 | } |
paul@324 | 75 | |
paul@324 | 76 | /* Fill a segment region with file content. */ |
paul@324 | 77 | |
paul@324 | 78 | long Segment::fill(file_t *file) |
paul@324 | 79 | { |
paul@326 | 80 | if (!_file_contents) |
paul@324 | 81 | return L4_EOK; |
paul@324 | 82 | |
paul@326 | 83 | memset(_buf, 0, _region_size); |
paul@326 | 84 | client_seek(file, _file_offset, SEEK_SET); |
paul@326 | 85 | offset_t nread = client_read(file, _buf + _region_offset, _file_contents); |
paul@324 | 86 | |
paul@326 | 87 | if (nread < _file_contents) |
paul@324 | 88 | return -L4_EIO; |
paul@324 | 89 | else |
paul@324 | 90 | return L4_EOK; |
paul@324 | 91 | } |
paul@324 | 92 | |
paul@324 | 93 | /* Define and return the mapped region for the segment. */ |
paul@324 | 94 | |
paul@324 | 95 | MappedRegion &Segment::region() |
paul@324 | 96 | { |
paul@326 | 97 | _region = MappedRegion((l4_addr_t) _buf, page_order(_region_size), _flags, _region_base); |
paul@324 | 98 | return _region; |
paul@324 | 99 | } |
paul@324 | 100 | |
paul@324 | 101 | /* Return the mapped address corresponding to the given address. */ |
paul@324 | 102 | |
paul@324 | 103 | l4_addr_t Segment::region_address(char *address) |
paul@324 | 104 | { |
paul@326 | 105 | return (l4_addr_t) ((address - _buf) + (char *) _region_base); |
paul@324 | 106 | } |
paul@324 | 107 | |
paul@324 | 108 | l4_addr_t Segment::region_address(l4_addr_t address) |
paul@324 | 109 | { |
paul@326 | 110 | return (address - (l4_addr_t) _buf) + _region_base; |
paul@324 | 111 | } |
paul@324 | 112 | |
paul@324 | 113 | /* vim: tabstop=2 expandtab shiftwidth=2 |
paul@324 | 114 | */ |