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@324 | 36 | Segment::Segment(offset_t base, offset_t size, l4re_rm_flags_t flags, |
paul@324 | 37 | offset_t file_offset, offset_t file_contents) |
paul@324 | 38 | : base(base), size(size), flags(flags), file_offset(file_offset), |
paul@324 | 39 | file_contents(file_contents) |
paul@324 | 40 | { |
paul@324 | 41 | region_base = trunc(base, L4_PAGESIZE); |
paul@324 | 42 | region_offset = base - region_base; |
paul@324 | 43 | region_size = round(size, L4_PAGESIZE); |
paul@324 | 44 | } |
paul@324 | 45 | |
paul@324 | 46 | /* Allocate a region for the segment. */ |
paul@324 | 47 | |
paul@324 | 48 | long Segment::allocate() |
paul@324 | 49 | { |
paul@324 | 50 | return ipc_allocate_align(size, L4RE_RM_F_SEARCH_ADDR | flags, |
paul@324 | 51 | L4_PAGESHIFT, (void **) &buf, &ds); |
paul@324 | 52 | } |
paul@324 | 53 | |
paul@324 | 54 | /* Fill a segment region with file content. */ |
paul@324 | 55 | |
paul@324 | 56 | long Segment::fill(file_t *file) |
paul@324 | 57 | { |
paul@324 | 58 | if (!file_contents) |
paul@324 | 59 | return L4_EOK; |
paul@324 | 60 | |
paul@324 | 61 | memset(buf, 0, region_size); |
paul@324 | 62 | client_seek(file, file_offset, SEEK_SET); |
paul@324 | 63 | offset_t nread = client_read(file, buf + region_offset, file_contents); |
paul@324 | 64 | |
paul@324 | 65 | if (nread < file_contents) |
paul@324 | 66 | return -L4_EIO; |
paul@324 | 67 | else |
paul@324 | 68 | return L4_EOK; |
paul@324 | 69 | } |
paul@324 | 70 | |
paul@324 | 71 | /* Define and return the mapped region for the segment. */ |
paul@324 | 72 | |
paul@324 | 73 | MappedRegion &Segment::region() |
paul@324 | 74 | { |
paul@324 | 75 | _region = MappedRegion((l4_addr_t) buf, page_order(region_size), flags, region_base); |
paul@324 | 76 | return _region; |
paul@324 | 77 | } |
paul@324 | 78 | |
paul@324 | 79 | /* Return the mapped address corresponding to the given address. */ |
paul@324 | 80 | |
paul@324 | 81 | l4_addr_t Segment::region_address(char *address) |
paul@324 | 82 | { |
paul@324 | 83 | return (l4_addr_t) ((address - buf) + (char *) region_base); |
paul@324 | 84 | } |
paul@324 | 85 | |
paul@324 | 86 | l4_addr_t Segment::region_address(l4_addr_t address) |
paul@324 | 87 | { |
paul@324 | 88 | return (address - (l4_addr_t) buf) + region_base; |
paul@324 | 89 | } |
paul@324 | 90 | |
paul@324 | 91 | /* vim: tabstop=2 expandtab shiftwidth=2 |
paul@324 | 92 | */ |