1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/libexec/lib/src/segment.cc Tue May 10 22:39:15 2022 +0200
1.3 @@ -0,0 +1,92 @@
1.4 +/*
1.5 + * Program memory segment support.
1.6 + *
1.7 + * Copyright (C) 2022 Paul Boddie <paul@boddie.org.uk>
1.8 + *
1.9 + * This program is free software; you can redistribute it and/or
1.10 + * modify it under the terms of the GNU General Public License as
1.11 + * published by the Free Software Foundation; either version 2 of
1.12 + * the License, or (at your option) any later version.
1.13 + *
1.14 + * This program is distributed in the hope that it will be useful,
1.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.17 + * GNU General Public License for more details.
1.18 + *
1.19 + * You should have received a copy of the GNU General Public License
1.20 + * along with this program; if not, write to the Free Software
1.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
1.22 + * Boston, MA 02110-1301, USA
1.23 + */
1.24 +
1.25 +#include <l4/sys/err.h>
1.26 +
1.27 +#include <ipc/mem_ipc.h>
1.28 +#include <mem/memory_utils.h>
1.29 +
1.30 +#include <stdio.h>
1.31 +#include <string.h>
1.32 +
1.33 +#include "segment.h"
1.34 +
1.35 +
1.36 +
1.37 +/* Initialise a memory segment. */
1.38 +
1.39 +Segment::Segment(offset_t base, offset_t size, l4re_rm_flags_t flags,
1.40 + offset_t file_offset, offset_t file_contents)
1.41 +: base(base), size(size), flags(flags), file_offset(file_offset),
1.42 + file_contents(file_contents)
1.43 +{
1.44 + region_base = trunc(base, L4_PAGESIZE);
1.45 + region_offset = base - region_base;
1.46 + region_size = round(size, L4_PAGESIZE);
1.47 +}
1.48 +
1.49 +/* Allocate a region for the segment. */
1.50 +
1.51 +long Segment::allocate()
1.52 +{
1.53 + return ipc_allocate_align(size, L4RE_RM_F_SEARCH_ADDR | flags,
1.54 + L4_PAGESHIFT, (void **) &buf, &ds);
1.55 +}
1.56 +
1.57 +/* Fill a segment region with file content. */
1.58 +
1.59 +long Segment::fill(file_t *file)
1.60 +{
1.61 + if (!file_contents)
1.62 + return L4_EOK;
1.63 +
1.64 + memset(buf, 0, region_size);
1.65 + client_seek(file, file_offset, SEEK_SET);
1.66 + offset_t nread = client_read(file, buf + region_offset, file_contents);
1.67 +
1.68 + if (nread < file_contents)
1.69 + return -L4_EIO;
1.70 + else
1.71 + return L4_EOK;
1.72 +}
1.73 +
1.74 +/* Define and return the mapped region for the segment. */
1.75 +
1.76 +MappedRegion &Segment::region()
1.77 +{
1.78 + _region = MappedRegion((l4_addr_t) buf, page_order(region_size), flags, region_base);
1.79 + return _region;
1.80 +}
1.81 +
1.82 +/* Return the mapped address corresponding to the given address. */
1.83 +
1.84 +l4_addr_t Segment::region_address(char *address)
1.85 +{
1.86 + return (l4_addr_t) ((address - buf) + (char *) region_base);
1.87 +}
1.88 +
1.89 +l4_addr_t Segment::region_address(l4_addr_t address)
1.90 +{
1.91 + return (address - (l4_addr_t) buf) + region_base;
1.92 +}
1.93 +
1.94 +/* vim: tabstop=2 expandtab shiftwidth=2
1.95 +*/