1.1 --- a/Makefile Sun Mar 14 17:17:09 2021 +0100
1.2 +++ b/Makefile Sun Mar 14 21:41:49 2021 +0100
1.3 @@ -2,8 +2,8 @@
1.4 L4DIR ?= $(PKGDIR)/../..
1.5
1.6 TARGET = \
1.7 - dstest_host_client dstest_test_client \
1.8 - dstest_host_server dstest_test_server
1.9 + dstest_block_client dstest_host_client dstest_test_client \
1.10 + dstest_block_server dstest_host_server dstest_test_server
1.11
1.12 MODE = static
1.13
1.14 @@ -24,77 +24,98 @@
1.15 opener_context_object_NAME = OpenerContextObject
1.16 opener_context_object_INTERFACES = dataspace opener_context
1.17
1.18 -COMP_INTERFACES_CC = mapped_file_object opener_context_object
1.19 +COMP_INTERFACES_CC = mapped_file_object opener_context_object
1.20
1.21 # Individual interfaces.
1.22
1.23 -CLIENT_INTERFACES_CC = dataspace mapped_file opener opener_context
1.24 +CLIENT_INTERFACES_CC = dataspace mapped_file opener opener_context
1.25
1.26 -SERVER_INTERFACES_CC = opener $(call common_interfaces,$(COMP_INTERFACES_CC))
1.27 +SERVER_INTERFACES_CC = opener $(call common_interfaces,$(COMP_INTERFACES_CC))
1.28
1.29 # Generated and plain source files.
1.30
1.31 -CLIENT_INTERFACES_SRC_CC = $(call interfaces_to_client_cc,$(CLIENT_INTERFACES_CC))
1.32 +CLIENT_INTERFACES_SRC_CC = $(call interfaces_to_client_cc,$(CLIENT_INTERFACES_CC))
1.33
1.34 -SERVER_INTERFACES_SRC_CC = $(call interfaces_to_server_cc,$(SERVER_INTERFACES_CC) $(COMP_INTERFACES_CC))
1.35 +SERVER_INTERFACES_SRC_CC = $(call interfaces_to_server_cc,$(SERVER_INTERFACES_CC) $(COMP_INTERFACES_CC))
1.36 +
1.37 +COMMON_SRC_CC = memory_utils.cc
1.38
1.39 -COMMON_SRC_CC = memory_utils.cc
1.40 +PLAIN_SRC_CC_dstest_block_client = dstest_block_client.cc file.cc
1.41
1.42 -PLAIN_SRC_CC_dstest_host_client = dstest_host_client.cc file.cc
1.43 +PLAIN_SRC_CC_dstest_host_client = dstest_host_client.cc file.cc
1.44
1.45 -PLAIN_SRC_CC_dstest_test_client = dstest_test_client.cc file.cc
1.46 +PLAIN_SRC_CC_dstest_test_client = dstest_test_client.cc file.cc
1.47
1.48 -PLAIN_SRC_CC_dstest_host_server = \
1.49 - access_map.cc accessing.cc accessor.cc \
1.50 - dstest_host_server.cc flexpage.cc file_pager.cc \
1.51 - ipc.cc memory.cc \
1.52 - opener_resource.cc opener_context_resource.cc \
1.53 - page_mapper.cc page_queue.cc pager.cc pages.cc paging.cc \
1.54 - region.cc resource_server.cc simple_pager.cc \
1.55 - files/host_file_accessor.cc files/host_file_opener.cc
1.56 +PLAIN_SRC_CC_common_server = \
1.57 + access_map.cc accessing.cc accessor.cc \
1.58 + flexpage.cc file_pager.cc ipc.cc memory.cc \
1.59 + opener_resource.cc opener_context_resource.cc \
1.60 + page_mapper.cc page_queue.cc pager.cc pages.cc paging.cc \
1.61 + region.cc resource_server.cc simple_pager.cc
1.62
1.63 -PLAIN_SRC_CC_dstest_test_server = \
1.64 - access_map.cc accessing.cc accessor.cc \
1.65 - dstest_test_server.cc flexpage.cc file_pager.cc \
1.66 - ipc.cc memory.cc \
1.67 - opener_resource.cc opener_context_resource.cc \
1.68 - page_mapper.cc page_queue.cc pager.cc pages.cc paging.cc \
1.69 - region.cc resource_server.cc simple_pager.cc \
1.70 - files/test_file_accessor.cc files/test_file_opener.cc
1.71 +PLAIN_SRC_CC_dstest_block_server = \
1.72 + $(PLAIN_SRC_CC_common_server) \
1.73 + dstest_block_server.cc \
1.74 + files/block_file_accessor.cc files/block_file_opener.cc \
1.75 + files/host_file_accessor.cc files/host_file_opener.cc
1.76 +
1.77 +PLAIN_SRC_CC_dstest_host_server = \
1.78 + $(PLAIN_SRC_CC_common_server) \
1.79 + dstest_host_server.cc \
1.80 + files/host_file_accessor.cc files/host_file_opener.cc
1.81 +
1.82 +PLAIN_SRC_CC_dstest_test_server = \
1.83 + $(PLAIN_SRC_CC_common_server) \
1.84 + dstest_test_server.cc \
1.85 + files/test_file_accessor.cc files/test_file_opener.cc
1.86
1.87 # Normal definitions.
1.88
1.89 -SRC_CC_dstest_host_client = \
1.90 - $(CLIENT_INTERFACES_SRC_CC) \
1.91 - $(PLAIN_SRC_CC_dstest_host_client) \
1.92 - $(COMMON_SRC_CC)
1.93 +SRC_CC_dstest_block_client = \
1.94 + $(CLIENT_INTERFACES_SRC_CC) \
1.95 + $(PLAIN_SRC_CC_dstest_block_client) \
1.96 + $(COMMON_SRC_CC)
1.97
1.98 -SRC_CC_dstest_test_client = \
1.99 - $(CLIENT_INTERFACES_SRC_CC) \
1.100 - $(PLAIN_SRC_CC_dstest_test_client) \
1.101 - $(COMMON_SRC_CC)
1.102 +SRC_CC_dstest_host_client = \
1.103 + $(CLIENT_INTERFACES_SRC_CC) \
1.104 + $(PLAIN_SRC_CC_dstest_host_client) \
1.105 + $(COMMON_SRC_CC)
1.106 +
1.107 +SRC_CC_dstest_test_client = \
1.108 + $(CLIENT_INTERFACES_SRC_CC) \
1.109 + $(PLAIN_SRC_CC_dstest_test_client) \
1.110 + $(COMMON_SRC_CC)
1.111
1.112 -SRC_CC_dstest_host_server = \
1.113 - $(SERVER_INTERFACES_SRC_CC) \
1.114 - $(PLAIN_SRC_CC_dstest_host_server) \
1.115 - $(COMMON_SRC_CC)
1.116 +SRC_CC_dstest_block_server = \
1.117 + $(SERVER_INTERFACES_SRC_CC) \
1.118 + $(PLAIN_SRC_CC_dstest_block_server) \
1.119 + $(COMMON_SRC_CC)
1.120
1.121 -SRC_CC_dstest_test_server = \
1.122 - $(SERVER_INTERFACES_SRC_CC) \
1.123 - $(PLAIN_SRC_CC_dstest_test_server) \
1.124 - $(COMMON_SRC_CC)
1.125 +SRC_CC_dstest_host_server = \
1.126 + $(SERVER_INTERFACES_SRC_CC) \
1.127 + $(PLAIN_SRC_CC_dstest_host_server) \
1.128 + $(COMMON_SRC_CC)
1.129
1.130 -REQUIRES_LIBS = l4re_c-util libipc libstdc++ libsystypes
1.131 +SRC_CC_dstest_test_server = \
1.132 + $(SERVER_INTERFACES_SRC_CC) \
1.133 + $(PLAIN_SRC_CC_dstest_test_server) \
1.134 + $(COMMON_SRC_CC)
1.135
1.136 -PRIVATE_INCDIR = $(PKGDIR) $(PKGDIR)/files $(IDL_BUILD_DIR) $(IDL_EXPORT_DIR)
1.137 +REQUIRES_LIBS = l4re_c-util libipc libstdc++ libsystypes
1.138 +
1.139 +PRIVATE_INCDIR = $(PKGDIR) $(PKGDIR)/files $(IDL_BUILD_DIR) $(IDL_EXPORT_DIR)
1.140
1.141 include $(L4DIR)/mk/prog.mk
1.142 include $(IDL_MK_DIR)/interface_rules.mk
1.143
1.144 +$(PLAIN_SRC_CC_dstest_block_client): $(CLIENT_INTERFACES_SRC_CC)
1.145 +
1.146 $(PLAIN_SRC_CC_dstest_host_client): $(CLIENT_INTERFACES_SRC_CC)
1.147
1.148 $(PLAIN_SRC_CC_dstest_test_client): $(CLIENT_INTERFACES_SRC_CC)
1.149
1.150 +$(PLAIN_SRC_CC_dstest_block_server): $(SERVER_INTERFACES_SRC_CC)
1.151 +
1.152 $(PLAIN_SRC_CC_dstest_host_server): $(SERVER_INTERFACES_SRC_CC)
1.153
1.154 $(PLAIN_SRC_CC_dstest_test_server): $(SERVER_INTERFACES_SRC_CC)
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/conf/dstest_block.cfg Sun Mar 14 21:41:49 2021 +0100
2.3 @@ -0,0 +1,23 @@
2.4 +-- vim:set ft=lua:
2.5 +
2.6 +local L4 = require("L4");
2.7 +
2.8 +local l = L4.default_loader;
2.9 +
2.10 +local server = l:new_channel();
2.11 +
2.12 +l:start({
2.13 + caps = {
2.14 + server = server:svr(),
2.15 + },
2.16 + log = { "server", "r" },
2.17 + },
2.18 + "rom/dstest_block_server");
2.19 +
2.20 +l:startv({
2.21 + caps = {
2.22 + server = server,
2.23 + },
2.24 + log = { "client", "g" },
2.25 + },
2.26 + "rom/dstest_block_client", "rom/dstest_block.cfg", 1024, 1024);
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/conf/dstest_block.list Sun Mar 14 21:41:49 2021 +0100
3.3 @@ -0,0 +1,25 @@
3.4 +entry dstest_block
3.5 +roottask moe rom/dstest_block.cfg
3.6 +module dstest_block.cfg
3.7 +module l4re
3.8 +module ned
3.9 +module dstest_block_client
3.10 +module dstest_block_server
3.11 +module lib4re-c.so
3.12 +module lib4re-c-util.so
3.13 +module lib4re.so
3.14 +module lib4re-util.so
3.15 +module libc_be_l4refile.so
3.16 +module libc_be_l4re.so
3.17 +module libc_be_socket_noop.so
3.18 +module libc_support_misc.so
3.19 +module libdl.so
3.20 +module libipc.so
3.21 +module libl4sys-direct.so
3.22 +module libl4sys.so
3.23 +module libl4util.so
3.24 +module libld-l4.so
3.25 +module libpthread.so
3.26 +module libstdc++.so
3.27 +module libsupc++.so
3.28 +module libuc_c.so
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
4.2 +++ b/dstest_block_client.cc Sun Mar 14 21:41:49 2021 +0100
4.3 @@ -0,0 +1,93 @@
4.4 +/*
4.5 + * Test dataspace operations.
4.6 + *
4.7 + * Copyright (C) 2020, 2021 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 <stdio.h>
4.29 +#include <string.h>
4.30 +#include <stdlib.h>
4.31 +
4.32 +#include <ipc/thread.h>
4.33 +
4.34 +#include "file.h"
4.35 +#include "memory_utils.h"
4.36 +
4.37 +
4.38 +
4.39 +int main(int argc, char *argv[])
4.40 +{
4.41 + if (argc < 4)
4.42 + {
4.43 + printf("Need filename, step and sample size.\n");
4.44 + return 1;
4.45 + }
4.46 +
4.47 + /* Obtain filename and access parameters. */
4.48 +
4.49 + char *filename = argv[1];
4.50 + unsigned long step = atoi(argv[2]);
4.51 + unsigned long sample = atoi(argv[3]);
4.52 +
4.53 + /* Allocate a buffer for sampling from the file. */
4.54 +
4.55 + char buf[sample + 1];
4.56 +
4.57 + /* Obtain access to the filesystem. */
4.58 +
4.59 + l4_cap_idx_t server = l4re_env_get_cap("server");
4.60 +
4.61 + /* Invoke the open method to receive the file reference. */
4.62 +
4.63 + file_t file;
4.64 + long err = file_open(&file, filename, server);
4.65 +
4.66 + if (err)
4.67 + {
4.68 + printf("Could not obtain file: %s\n", l4sys_errtostr(err));
4.69 + return 1;
4.70 + }
4.71 +
4.72 + /* A region of the file is mapped. */
4.73 +
4.74 + err = file_mmap(&file, 0, page(10));
4.75 +
4.76 + if (err)
4.77 + {
4.78 + printf("Could not map file region: %s\n", l4sys_errtostr(err));
4.79 + return 1;
4.80 + }
4.81 +
4.82 + for (unsigned long offset = 0; offset < file_populated_span(&file); offset += step)
4.83 + {
4.84 + unsigned long remaining = file_populated_span(&file) - offset;
4.85 + unsigned long sample_remaining = remaining < sample ? remaining : sample;
4.86 +
4.87 + printf("%ld bytes from %p...\n", sample_remaining, (file.memory + offset));
4.88 + strncpy(buf, (file.memory + offset), sample_remaining);
4.89 + buf[sample_remaining] = '\0';
4.90 + printf("%s\n", buf);
4.91 + }
4.92 +
4.93 + return 0;
4.94 +}
4.95 +
4.96 +// vim: tabstop=2 expandtab shiftwidth=2
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
5.2 +++ b/dstest_block_server.cc Sun Mar 14 21:41:49 2021 +0100
5.3 @@ -0,0 +1,65 @@
5.4 +/*
5.5 + * Test dataspace operations.
5.6 + *
5.7 + * Copyright (C) 2020, 2021 Paul Boddie <paul@boddie.org.uk>
5.8 + *
5.9 + * This program is free software; you can redistribute it and/or
5.10 + * modify it under the terms of the GNU General Public License as
5.11 + * published by the Free Software Foundation; either version 2 of
5.12 + * the License, or (at your option) any later version.
5.13 + *
5.14 + * This program is distributed in the hope that it will be useful,
5.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
5.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5.17 + * GNU General Public License for more details.
5.18 + *
5.19 + * You should have received a copy of the GNU General Public License
5.20 + * along with this program; if not, write to the Free Software
5.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
5.22 + * Boston, MA 02110-1301, USA
5.23 + */
5.24 +
5.25 +#include <l4/re/env.h>
5.26 +#include <l4/sys/err.h>
5.27 +
5.28 +#include <stdio.h>
5.29 +#include <string.h>
5.30 +#include <stdlib.h>
5.31 +
5.32 +#include "accessing.h"
5.33 +#include "memory.h"
5.34 +#include "memory_utils.h"
5.35 +#include "pages.h"
5.36 +#include "paging.h"
5.37 +#include "resource_server.h"
5.38 +#include "block_file_opener.h"
5.39 +
5.40 +
5.41 +
5.42 +const unsigned int MEMORY_PAGES = 10;
5.43 +
5.44 +int main(void)
5.45 +{
5.46 + /* Some memory plus infrastructure. */
5.47 +
5.48 + Memory mem(MEMORY_PAGES);
5.49 + Accessing accessing;
5.50 + Paging paging;
5.51 + Pages pages(&mem);
5.52 + BlockFileOpener opener(&accessing, &paging, &pages);
5.53 +
5.54 + /* Register a server associating it with the given object. */
5.55 +
5.56 + ResourceServer server(&opener);
5.57 + long err = server.bind("server");
5.58 +
5.59 + if (err)
5.60 + {
5.61 + printf("Could not bind server: %s\n", l4sys_errtostr(err));
5.62 + return 1;
5.63 + }
5.64 +
5.65 + printf("Starting server...\n");
5.66 + server.start();
5.67 + return 0;
5.68 +}
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
6.2 +++ b/files/block_file_accessor.cc Sun Mar 14 21:41:49 2021 +0100
6.3 @@ -0,0 +1,85 @@
6.4 +#include <stdio.h>
6.5 +#include <string.h>
6.6 +#include <sys/types.h>
6.7 +#include <sys/stat.h>
6.8 +
6.9 +#include <algorithm>
6.10 +
6.11 +#include "block_file_accessor.h"
6.12 +
6.13 +
6.14 +
6.15 +BlockFileAccessor::BlockFileAccessor(const char *path, fileid_t fileid)
6.16 +: Accessor(fileid)
6.17 +{
6.18 + /* Obtain the size of the file. */
6.19 +
6.20 + struct stat buf;
6.21 +
6.22 + if (stat(path, &buf))
6.23 + {
6.24 + _size = 0;
6.25 + return;
6.26 + }
6.27 +
6.28 + /* Allocate memory. */
6.29 +
6.30 + _size = buf.st_size;
6.31 + _data = (char *) malloc(_size);
6.32 +
6.33 + if (_data == NULL)
6.34 + return;
6.35 +
6.36 + /* Load the file into memory and initialise the size. */
6.37 +
6.38 + FILE *fp = fopen(path, "r");
6.39 +
6.40 + if (fp == NULL)
6.41 + {
6.42 + free(_data);
6.43 + return;
6.44 + }
6.45 +
6.46 + _size = fread(_data, sizeof(char), _size, fp);
6.47 +
6.48 + fclose(fp);
6.49 +}
6.50 +
6.51 +/* Data transfer methods. */
6.52 +
6.53 +void BlockFileAccessor::fill(Flexpage *flexpage)
6.54 +{
6.55 + offset_t filepos = flexpage->base_offset;
6.56 + offset_t addr = flexpage->base_addr;
6.57 + offset_t populated_size = std::min(flexpage->size, _size - filepos);
6.58 +
6.59 + /* Tag the region with file state. */
6.60 +
6.61 + flexpage->region->fill(fileid, filepos);
6.62 +
6.63 + /* Fill the region with file content. */
6.64 +
6.65 + memcpy((void *) addr, _data + filepos, populated_size);
6.66 +
6.67 + /* Pad the flexpage with zero. */
6.68 +
6.69 + if (populated_size < flexpage->size)
6.70 + memset((void *) (addr + populated_size), 0, flexpage->size - populated_size);
6.71 +}
6.72 +
6.73 +void BlockFileAccessor::flush(Flexpage *flexpage)
6.74 +{
6.75 + offset_t addr = flexpage->base_addr;
6.76 + offset_t filepos = flexpage->base_offset;
6.77 + offset_t populated_size = std::min(flexpage->size, _size - filepos);
6.78 +
6.79 + /* Remove the file state tag from the region. */
6.80 +
6.81 + flexpage->region->flush();
6.82 +
6.83 + /* Copy the populated region to the block memory. */
6.84 +
6.85 + memcpy((void *) (_data + filepos), (const void *) addr, populated_size);
6.86 +}
6.87 +
6.88 +// vim: tabstop=4 expandtab shiftwidth=4
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
7.2 +++ b/files/block_file_accessor.h Sun Mar 14 21:41:49 2021 +0100
7.3 @@ -0,0 +1,23 @@
7.4 +#pragma once
7.5 +
7.6 +#include "accessor.h"
7.7 +
7.8 +/* A block file accessor, providing flexpages corresponding to the regions of
7.9 + loaded files. */
7.10 +
7.11 +class BlockFileAccessor : public Accessor
7.12 +{
7.13 +protected:
7.14 + char *_data;
7.15 +
7.16 +public:
7.17 + explicit BlockFileAccessor(const char *path, fileid_t fileid);
7.18 +
7.19 + /* Data transfer methods. */
7.20 +
7.21 + virtual void fill(Flexpage *flexpage);
7.22 +
7.23 + virtual void flush(Flexpage *flexpage);
7.24 +};
7.25 +
7.26 +// vim: tabstop=4 expandtab shiftwidth=4
8.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
8.2 +++ b/files/block_file_opener.cc Sun Mar 14 21:41:49 2021 +0100
8.3 @@ -0,0 +1,16 @@
8.4 +#include "block_file_accessor.h"
8.5 +#include "block_file_opener.h"
8.6 +
8.7 +/* Return a new accessor for 'fileid'. */
8.8 +
8.9 +Accessor *BlockFileOpener::make_accessor(fileid_t fileid)
8.10 +{
8.11 + FilePaths::iterator found = _paths.find(fileid);
8.12 +
8.13 + if (found != _paths.end())
8.14 + return new BlockFileAccessor(found->second, fileid);
8.15 + else
8.16 + return NULL;
8.17 +}
8.18 +
8.19 +// vim: tabstop=4 expandtab shiftwidth=4
9.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
9.2 +++ b/files/block_file_opener.h Sun Mar 14 21:41:49 2021 +0100
9.3 @@ -0,0 +1,23 @@
9.4 +#pragma once
9.5 +
9.6 +#include "host_file_opener.h"
9.7 +
9.8 +
9.9 +
9.10 +/* Support for providing access to files. */
9.11 +
9.12 +class BlockFileOpener : public HostFileOpener
9.13 +{
9.14 +protected:
9.15 + /* Configurable methods. */
9.16 +
9.17 + virtual Accessor *make_accessor(fileid_t fileid);
9.18 +
9.19 +public:
9.20 + explicit BlockFileOpener(Accessing *accessing, Paging *paging, Pages *pages)
9.21 + : HostFileOpener(accessing, paging, pages)
9.22 + {
9.23 + }
9.24 +};
9.25 +
9.26 +// vim: tabstop=4 expandtab shiftwidth=4