1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/conf/dstest_file_mapping.cfg Sun Jun 05 21:30:59 2022 +0200
1.3 @@ -0,0 +1,23 @@
1.4 +-- vim:set ft=lua:
1.5 +
1.6 +local L4 = require("L4");
1.7 +
1.8 +local l = L4.default_loader;
1.9 +
1.10 +local server = l:new_channel();
1.11 +
1.12 +l:startv({
1.13 + caps = {
1.14 + server = server:svr(),
1.15 + },
1.16 + log = { "server", "r" },
1.17 + },
1.18 + "rom/dstest_host_server", "10");
1.19 +
1.20 +l:startv({
1.21 + caps = {
1.22 + server = server,
1.23 + },
1.24 + log = { "client", "g" },
1.25 + },
1.26 + "rom/dstest_file_mapping", "rom/dstest_file_mapping.cfg", 1024, 1024);
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/conf/dstest_file_mapping.list Sun Jun 05 21:30:59 2022 +0200
2.3 @@ -0,0 +1,25 @@
2.4 +entry dstest_file_mapping
2.5 +roottask moe rom/dstest_file_mapping.cfg
2.6 +module dstest_file_mapping.cfg
2.7 +module l4re
2.8 +module ned
2.9 +module dstest_file_mapping
2.10 +module dstest_host_server
2.11 +module lib4re-c.so
2.12 +module lib4re-c-util.so
2.13 +module lib4re.so
2.14 +module lib4re-util.so
2.15 +module libc_be_l4refile.so
2.16 +module libc_be_l4re.so
2.17 +module libc_be_socket_noop.so
2.18 +module libc_support_misc.so
2.19 +module libdl.so
2.20 +module libipc.so
2.21 +module libl4sys-direct.so
2.22 +module libl4sys.so
2.23 +module libl4util.so
2.24 +module libld-l4.so
2.25 +module libpthread.so
2.26 +module libstdc++.so
2.27 +module libsupc++.so
2.28 +module libuc_c.so
3.1 --- a/tests/Makefile Sun Jun 05 18:50:47 2022 +0200
3.2 +++ b/tests/Makefile Sun Jun 05 21:30:59 2022 +0200
3.3 @@ -14,7 +14,8 @@
3.4 dstest_pipe_client \
3.5 dstest_test_client \
3.6 dstest_map_test \
3.7 - dstest_exec
3.8 + dstest_exec \
3.9 + dstest_file_mapping
3.10
3.11 MODE = static
3.12
3.13 @@ -36,10 +37,14 @@
3.14
3.15 # Individual interfaces.
3.16
3.17 +CLIENT_INTERFACES_CC = dataspace mapped_file
3.18 +
3.19 SERVER_INTERFACES_CC = $(call common_interfaces,$(COMP_INTERFACES_CC))
3.20
3.21 # Generated and plain source files.
3.22
3.23 +CLIENT_INTERFACES_SRC_CC = $(call interfaces_to_client_cc,$(CLIENT_INTERFACES_CC))
3.24 +
3.25 SERVER_INTERFACES_SRC_CC = $(call interfaces_to_server_cc,$(SERVER_INTERFACES_CC) $(COMP_INTERFACES_CC))
3.26
3.27 # Normal source files.
3.28 @@ -71,10 +76,15 @@
3.29 PLAIN_SRC_CC_dstest_exec = dstest_exec.cc
3.30 SRC_CC_dstest_exec = $(PLAIN_SRC_CC_dstest_exec) $(SERVER_INTERFACES_SRC_CC)
3.31
3.32 +PLAIN_SRC_CC_dstest_file_mapping = dstest_file_mapping.cc
3.33 +SRC_CC_dstest_file_mapping = $(PLAIN_SRC_CC_dstest_file_mapping) $(CLIENT_INTERFACES_SRC_CC)
3.34 +
3.35 REQUIRES_LIBS = l4re_c-util libexec libfsclient libmem libipc libstdc++ libsystypes libe2access_blockserver
3.36 PRIVATE_INCDIR = $(IDL_BUILD_DIR) $(IDL_EXPORT_DIR)
3.37
3.38 include $(L4DIR)/mk/prog.mk
3.39 include $(IDL_MK_DIR)/interface_rules.mk
3.40
3.41 +$(PLAIN_SRC_CC_dstest_file_mapping): $(CLIENT_INTERFACES_SRC_CC)
3.42 +
3.43 $(PLAIN_SRC_CC_dstest_exec): $(SERVER_INTERFACES_SRC_CC)
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
4.2 +++ b/tests/dstest_file_mapping.cc Sun Jun 05 21:30:59 2022 +0200
4.3 @@ -0,0 +1,143 @@
4.4 +/*
4.5 + * Test dataspace operations.
4.6 + *
4.7 + * Copyright (C) 2020, 2021, 2022 Paul Boddie <paul@boddie.org.uk>
4.8 + *
4.9 + * This program is free software; you can redistribute it and/or
4.10 + * modify it under the terms of the GNU General Public License as
4.11 + * published by the Free Software Foundation; either version 2 of
4.12 + * the License, or (at your option) any later version.
4.13 + *
4.14 + * This program is distributed in the hope that it will be useful,
4.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4.17 + * GNU General Public License for more details.
4.18 + *
4.19 + * You should have received a copy of the GNU General Public License
4.20 + * along with this program; if not, write to the Free Software
4.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
4.22 + * Boston, MA 02110-1301, USA
4.23 + */
4.24 +
4.25 +#include <l4/re/env.h>
4.26 +#include <l4/sys/err.h>
4.27 +
4.28 +#include <systypes/fcntl.h>
4.29 +
4.30 +#include <stdio.h>
4.31 +#include <string.h>
4.32 +#include <stdlib.h>
4.33 +
4.34 +#include <fsclient/file.h>
4.35 +#include <mem/memory_utils.h>
4.36 +
4.37 +#include "dataspace_client.h"
4.38 +#include "mapped_file_client.h"
4.39 +
4.40 +
4.41 +
4.42 +/* NOTE: Copied from libfsclient. */
4.43 +
4.44 +static void _update_extent(file_t *file)
4.45 +{
4.46 + /* Handle files ending after or within the region. */
4.47 +
4.48 + if (file->size > file->start_pos)
4.49 + {
4.50 + if (file->size > file->end_pos)
4.51 + file->data_end = file->end_pos - file->start_pos;
4.52 + else
4.53 + file->data_end = file->size - file->start_pos;
4.54 + }
4.55 +
4.56 + /* Handle files ending before the region. */
4.57 +
4.58 + else
4.59 + file->data_end = 0;
4.60 +}
4.61 +
4.62 +
4.63 +
4.64 +int main(int argc, char *argv[])
4.65 +{
4.66 + if (argc < 4)
4.67 + {
4.68 + printf("Need filename, step and sample size.\n");
4.69 + return 1;
4.70 + }
4.71 +
4.72 + /* Obtain filename and access parameters. */
4.73 +
4.74 + char *filename = argv[1];
4.75 + unsigned long step = atoi(argv[2]);
4.76 + unsigned long sample = atoi(argv[3]);
4.77 +
4.78 + /* Allocate a buffer for sampling from the file. */
4.79 +
4.80 + char buf[sample + 1];
4.81 +
4.82 + /* Obtain access to the filesystem. */
4.83 +
4.84 + l4_cap_idx_t server = l4re_env_get_cap("server");
4.85 +
4.86 + /* Invoke the open method to receive the file reference. */
4.87 +
4.88 + file_t file;
4.89 + long err = file_open(&file, filename, O_RDWR, server);
4.90 +
4.91 + if (err)
4.92 + {
4.93 + printf("Could not obtain file: %s\n", l4sys_errtostr(err));
4.94 + return 1;
4.95 + }
4.96 +
4.97 + /* A region of the file is mapped. */
4.98 +
4.99 + client_MappedFile mapped_file(file.ref);
4.100 +
4.101 + err = mapped_file.mmap(0, page(10), 0, 0, &file.start_pos, &file.end_pos, &file.size);
4.102 +
4.103 + if (err)
4.104 + {
4.105 + printf("Could not map file region: %s\n", l4sys_errtostr(err));
4.106 + return 1;
4.107 + }
4.108 +
4.109 + /* Fix up the file data structure manually. */
4.110 +
4.111 + file.memory = (char *) 0x2000000;
4.112 + _update_extent(&file);
4.113 +
4.114 + /* Explicitly map the file into this address space, without region mapper
4.115 + usage. The receive window base must be a multiple of its size. */
4.116 +
4.117 + client_Dataspace dataspace(file.ref);
4.118 + l4_snd_fpage_t region = {0, l4_fpage((l4_addr_t) file.memory, L4_PAGESHIFT + 4, 0)};
4.119 +
4.120 + printf("region = {%lx, {%lx, %d}}\n", region.snd_base, l4_fpage_memaddr(region.fpage), l4_fpage_size(region.fpage));
4.121 +
4.122 + err = dataspace.map(0, 0, L4_FPAGE_RO, ®ion);
4.123 +
4.124 + if (err)
4.125 + {
4.126 + printf("Could not invoke map successfully: %s\n", l4sys_errtostr(err));
4.127 + return 1;
4.128 + }
4.129 +
4.130 + for (unsigned long offset = 0; offset < file_populated_span(&file); offset += step)
4.131 + {
4.132 + unsigned long remaining = file_populated_span(&file) - offset;
4.133 + unsigned long sample_remaining = remaining < sample ? remaining : sample;
4.134 +
4.135 + printf("%ld bytes from %p...\n", sample_remaining, (file.memory + offset));
4.136 + strncpy(buf, (file.memory + offset), sample_remaining);
4.137 + buf[sample_remaining] = '\0';
4.138 + printf("%s\n", buf);
4.139 + }
4.140 +
4.141 + printf("File shown.\n");
4.142 +
4.143 + return 0;
4.144 +}
4.145 +
4.146 +// vim: tabstop=2 expandtab shiftwidth=2