1.1 --- a/Makefile Sun Jan 24 19:29:07 2021 +0100
1.2 +++ b/Makefile Mon Feb 01 00:10:21 2021 +0100
1.3 @@ -15,16 +15,19 @@
1.4
1.5 # Compound interfaces.
1.6
1.7 -mapped_file_object_NAME = MappedFileObject
1.8 -mapped_file_object_INTERFACES = dataspace mapped_file
1.9 +mapped_file_object_NAME = MappedFileObject
1.10 +mapped_file_object_INTERFACES = dataspace mapped_file
1.11
1.12 -COMP_INTERFACES_CC = mapped_file_object
1.13 +opener_context_object_NAME = OpenerContextObject
1.14 +opener_context_object_INTERFACES = dataspace opener_context
1.15 +
1.16 +COMP_INTERFACES_CC = mapped_file_object opener_context_object
1.17
1.18 # Individual interfaces.
1.19
1.20 -CLIENT_INTERFACES_CC = mapped_file
1.21 +CLIENT_INTERFACES_CC = dataspace mapped_file opener opener_context
1.22
1.23 -SERVER_INTERFACES_CC = $(call common_interfaces,$(COMP_INTERFACES_CC))
1.24 +SERVER_INTERFACES_CC = opener $(call common_interfaces,$(COMP_INTERFACES_CC))
1.25
1.26 # Generated and plain source files.
1.27
1.28 @@ -32,27 +35,30 @@
1.29
1.30 SERVER_INTERFACES_SRC_CC = $(call interfaces_to_server_cc,$(SERVER_INTERFACES_CC) $(COMP_INTERFACES_CC))
1.31
1.32 -PLAIN_SRC_CC_dstest_client = \
1.33 - dstest_client.cc memory_utils.cc
1.34 +COMMON_SRC_CC = memory_utils.cc
1.35 +
1.36 +PLAIN_SRC_CC_dstest_client = dstest_client.cc
1.37
1.38 PLAIN_SRC_CC_dstest_server = \
1.39 access_map.cc accessing.cc accessor.cc \
1.40 dstest_server.cc flexpage.cc file_pager.cc \
1.41 - ipc.cc memory.cc memory_map.cc memory_utils.cc \
1.42 - opener.cc opener_context.cc \
1.43 + ipc.cc memory.cc memory_map.cc \
1.44 + opener_resource.cc opener_context_resource.cc \
1.45 page_mapper.cc page_queue.cc pager.cc pages.cc paging.cc \
1.46 - region.cc resource.cc simple_pager.cc \
1.47 - test_file_accessor.cc test_file_opener.cc
1.48 + region.cc resource_server.cc simple_pager.cc \
1.49 + test_file_accessor.cc test_file_opener.cc \
1.50
1.51 # Normal definitions.
1.52
1.53 SRC_CC_dstest_client = \
1.54 $(CLIENT_INTERFACES_SRC_CC) \
1.55 - $(PLAIN_SRC_CC_dstest_client)
1.56 + $(PLAIN_SRC_CC_dstest_client) \
1.57 + $(COMMON_SRC_CC)
1.58
1.59 SRC_CC_dstest_server = \
1.60 $(SERVER_INTERFACES_SRC_CC) \
1.61 - $(PLAIN_SRC_CC_dstest_server)
1.62 + $(PLAIN_SRC_CC_dstest_server) \
1.63 + $(COMMON_SRC_CC)
1.64
1.65 REQUIRES_LIBS = l4re_c-util libipc libstdc++
1.66
2.1 --- a/dstest_client.cc Sun Jan 24 19:29:07 2021 +0100
2.2 +++ b/dstest_client.cc Mon Feb 01 00:10:21 2021 +0100
2.3 @@ -28,6 +28,9 @@
2.4
2.5 #include <ipc/mem_ipc.h>
2.6
2.7 +#include "dataspace_client.h"
2.8 +#include "opener_client.h"
2.9 +#include "opener_context_client.h"
2.10 #include "mapped_file_client.h"
2.11 #include "memory_utils.h"
2.12
2.13 @@ -36,8 +39,62 @@
2.14 int main(void)
2.15 {
2.16 l4_cap_idx_t server = l4re_env_get_cap("server");
2.17 + l4_cap_idx_t context_ref;
2.18 + client_Opener opener(server);
2.19
2.20 - client_MappedFile obj(server);
2.21 + long err = opener.context(&context_ref);
2.22 +
2.23 + if (err)
2.24 + {
2.25 + printf("Could not obtain context: %s\n", l4sys_errtostr(err));
2.26 + return 1;
2.27 + }
2.28 +
2.29 + client_OpenerContext context(context_ref);
2.30 + client_Dataspace context_ds(context_ref);
2.31 + unsigned long size, flags;
2.32 +
2.33 + err = context_ds.info(&size, &flags);
2.34 +
2.35 + if (err)
2.36 + {
2.37 + printf("Could not obtain context info: %s\n", l4sys_errtostr(err));
2.38 + return 1;
2.39 + }
2.40 +
2.41 + /* Map context memory to write the filename. */
2.42 +
2.43 + char *memory;
2.44 +
2.45 + printf("Attach region of size %ld...\n", size);
2.46 +
2.47 + err = ipc_attach_dataspace(context_ref, size, (void **) &memory);
2.48 +
2.49 + if (err)
2.50 + {
2.51 + printf("Could not map memory: %s\n", l4sys_errtostr(err));
2.52 + return 1;
2.53 + }
2.54 +
2.55 + printf("Mapped memory at %p\n", memory);
2.56 +
2.57 + /* Write the filename. */
2.58 +
2.59 + strcpy(memory, "123");
2.60 +
2.61 + /* Invoke the open method to receive the file reference. */
2.62 +
2.63 + l4_cap_idx_t file;
2.64 +
2.65 + err = context.open(L4_FPAGE_RW, &size, &file);
2.66 +
2.67 + if (err)
2.68 + {
2.69 + printf("Could not obtain file: %s\n", l4sys_errtostr(err));
2.70 + return 1;
2.71 + }
2.72 +
2.73 + client_MappedFile obj(file);
2.74
2.75 /* Some memory to be mapped. */
2.76
2.77 @@ -45,7 +102,7 @@
2.78
2.79 printf("Map region from %ld to %ld...\n", 0L, page(10));
2.80
2.81 - long err = obj.mmap(0, page(10), &start_pos, &end_pos, &data_end);
2.82 + err = obj.mmap(0, page(10), &start_pos, &end_pos, &data_end);
2.83
2.84 if (err)
2.85 {
2.86 @@ -53,12 +110,11 @@
2.87 return 1;
2.88 }
2.89
2.90 - char *memory;
2.91 - unsigned long size = end_pos - start_pos;
2.92 + size = end_pos - start_pos;
2.93
2.94 printf("Attach region of size %ld...\n", size);
2.95
2.96 - err = ipc_attach_dataspace(server, size, (void **) &memory);
2.97 + err = ipc_attach_dataspace(file, size, (void **) &memory);
2.98
2.99 if (err)
2.100 {
3.1 --- a/dstest_server.cc Sun Jan 24 19:29:07 2021 +0100
3.2 +++ b/dstest_server.cc Mon Feb 01 00:10:21 2021 +0100
3.3 @@ -26,14 +26,12 @@
3.4 #include <string.h>
3.5 #include <stdlib.h>
3.6
3.7 -#include <ipc/server.h>
3.8 -
3.9 #include "accessing.h"
3.10 -#include "mapped_file_object_server.h"
3.11 +#include "memory.h"
3.12 #include "memory_utils.h"
3.13 -#include "file_pager.h"
3.14 #include "pages.h"
3.15 #include "paging.h"
3.16 +#include "resource_server.h"
3.17 #include "test_file_opener.h"
3.18
3.19
3.20 @@ -50,15 +48,11 @@
3.21 Paging paging;
3.22 Pages pages(&mem);
3.23 TestFileOpener opener(&accessing, &paging, &pages, page(FILE_PAGES));
3.24 - FilePager *obj = opener.open("123");
3.25 -
3.26 - /* Server capability. */
3.27 -
3.28 - l4_cap_idx_t server;
3.29
3.30 /* Register a server associating it with the given object. */
3.31
3.32 - long err = ipc_server_bind("server", (l4_umword_t) obj, &server);
3.33 + ResourceServer server(&opener);
3.34 + long err = server.bind("server");
3.35
3.36 if (err)
3.37 {
3.38 @@ -66,9 +60,7 @@
3.39 return 1;
3.40 }
3.41
3.42 - /* Wait for messages, dispatching to the handler. */
3.43 -
3.44 - ipc_server_loop_for(MappedFileObject, obj);
3.45 -
3.46 + printf("Starting server...\n");
3.47 + server.start();
3.48 return 0;
3.49 }
4.1 --- a/file_pager.cc Sun Jan 24 19:29:07 2021 +0100
4.2 +++ b/file_pager.cc Mon Feb 01 00:10:21 2021 +0100
4.3 @@ -1,4 +1,5 @@
4.4 #include "file_pager.h"
4.5 +#include "mapped_file_object_server.h"
4.6
4.7 /* Initialise a pager for a file with a unique file identifier and shared page
4.8 mapper for moderating access to loaded pages. */
4.9 @@ -8,6 +9,16 @@
4.10 {
4.11 }
4.12
4.13 +int FilePager::expected_items()
4.14 +{
4.15 + return MappedFileObject_expected_items;
4.16 +}
4.17 +
4.18 +ipc_server_handler_type FilePager::handler()
4.19 +{
4.20 + return (ipc_server_handler_type) handle_MappedFileObject;
4.21 +}
4.22 +
4.23 long FilePager::mmap(size_t position, size_t length, size_t *start_pos, size_t *end_pos, size_t *data_end)
4.24 {
4.25 /* Set the limits of the paged region. */
5.1 --- a/file_pager.h Sun Jan 24 19:29:07 2021 +0100
5.2 +++ b/file_pager.h Mon Feb 01 00:10:21 2021 +0100
5.3 @@ -12,6 +12,17 @@
5.4
5.5 explicit FilePager(fileid_t fileid, PageMapper *mapper);
5.6
5.7 + /* Server details. */
5.8 +
5.9 + int expected_items();
5.10 +
5.11 + ipc_server_handler_type handler();
5.12 +
5.13 + void *interface()
5.14 + { return static_cast<MappedFileObject *>(this); }
5.15 +
5.16 + /* Pager and mapped file methods. */
5.17 +
5.18 virtual long map(unsigned long offset, l4_addr_t hot_spot, unsigned long flags, l4_snd_fpage_t *region)
5.19 { return Pager::map(offset, hot_spot, flags, region); }
5.20
6.1 --- a/ipc.cc Sun Jan 24 19:29:07 2021 +0100
6.2 +++ b/ipc.cc Mon Feb 01 00:10:21 2021 +0100
6.3 @@ -9,12 +9,10 @@
6.4
6.5 /* Make an L4 representation of the given flexpage. */
6.6
6.7 -static l4_fpage_t ipc_get_fpage(SendFlexpage *send_flexpage)
6.8 +static l4_fpage_t ipc_get_fpage(SendFlexpage *send_flexpage, unsigned long flags)
6.9 {
6.10 // NOTE: To introduce flags properly in the flexpage abstractions.
6.11
6.12 - unsigned long flags = L4_FPAGE_RW;
6.13 -
6.14 return l4_fpage(send_flexpage->base_addr, send_flexpage->order,
6.15 (flags & L4RE_DS_MAP_FLAG_RW) ? L4_FPAGE_RW : L4_FPAGE_RO);
6.16 }
6.17 @@ -30,7 +28,7 @@
6.18 if (!send_flexpage.order)
6.19 return -L4_ERANGE;
6.20
6.21 - region->fpage = ipc_get_fpage(&send_flexpage);
6.22 + region->fpage = ipc_get_fpage(&send_flexpage, flags);
6.23 region->snd_base = hot_spot;
6.24
6.25 return L4_EOK;
6.26 @@ -42,7 +40,9 @@
6.27 {
6.28 SendFlexpage send_flexpage = flexpage->to_unmap();
6.29
6.30 - l4_task_unmap(L4RE_THIS_TASK_CAP, ipc_get_fpage(&send_flexpage), L4_FP_OTHER_SPACES);
6.31 + // NOTE: To introduce flags properly in the flexpage abstractions.
6.32 +
6.33 + l4_task_unmap(L4RE_THIS_TASK_CAP, ipc_get_fpage(&send_flexpage, L4_FPAGE_RW), L4_FP_OTHER_SPACES);
6.34 }
6.35
6.36 // vim: tabstop=4 expandtab shiftwidth=4
7.1 --- a/opener.cc Sun Jan 24 19:29:07 2021 +0100
7.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
7.3 @@ -1,63 +0,0 @@
7.4 -#include "opener.h"
7.5 -
7.6 -/* Support for providing access to files. */
7.7 -
7.8 -Opener::Opener(Accessing *accessing, Paging *paging, Pages *pages)
7.9 -: _accessing(accessing), _paging(paging), _pages(pages)
7.10 -{
7.11 -}
7.12 -
7.13 -OpenerContext *Opener::context()
7.14 -{
7.15 - return new OpenerContext(this);
7.16 -}
7.17 -
7.18 -FilePager *Opener::open(const char *path)
7.19 -{
7.20 - fileid_t fileid = get_fileid(path);
7.21 -
7.22 - if (fileid == FILEID_INVALID)
7.23 - return NULL;
7.24 -
7.25 - Accessor *accessor = get_accessor(fileid);
7.26 - PageMapper *mapper = get_mapper(accessor);
7.27 - return new FilePager(fileid, mapper);
7.28 -}
7.29 -
7.30 -/* Obtain an accessor for the 'fileid' or register a new one in the accessing
7.31 - object. */
7.32 -
7.33 -Accessor *Opener::get_accessor(fileid_t fileid)
7.34 -{
7.35 - Accessor *accessor;
7.36 -
7.37 - if (_accessing->reserve(fileid))
7.38 - accessor = _accessing->get(fileid);
7.39 - else
7.40 - {
7.41 - accessor = make_accessor(fileid);
7.42 - _accessing->set(fileid, accessor);
7.43 - }
7.44 -
7.45 - return accessor;
7.46 -}
7.47 -
7.48 -/* Obtain a page mapper for the 'accessor' or register a new one in the
7.49 - paging object. */
7.50 -
7.51 -PageMapper *Opener::get_mapper(Accessor *accessor)
7.52 -{
7.53 - PageMapper *mapper;
7.54 -
7.55 - if (_paging->reserve(accessor))
7.56 - mapper = _paging->get(accessor);
7.57 - else
7.58 - {
7.59 - mapper = new PageMapper(accessor, _pages);
7.60 - _paging->set(accessor, mapper);
7.61 - }
7.62 -
7.63 - return mapper;
7.64 -}
7.65 -
7.66 -// vim: tabstop=4 expandtab shiftwidth=4
8.1 --- a/opener.h Sun Jan 24 19:29:07 2021 +0100
8.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
8.3 @@ -1,39 +0,0 @@
8.4 -#pragma once
8.5 -
8.6 -#include "accessing.h"
8.7 -#include "file_pager.h"
8.8 -#include "opener_context.h"
8.9 -#include "pager.h"
8.10 -#include "paging.h"
8.11 -#include "pages.h"
8.12 -
8.13 -/* Support for providing access to files. */
8.14 -
8.15 -class Opener
8.16 -{
8.17 -protected:
8.18 - Accessing *_accessing;
8.19 - Paging *_paging;
8.20 - Pages *_pages;
8.21 -
8.22 - /* Convenience methods. */
8.23 -
8.24 - Accessor *get_accessor(fileid_t fileid);
8.25 -
8.26 - PageMapper *get_mapper(Accessor *accessor);
8.27 -
8.28 - /* Configurable methods. */
8.29 -
8.30 - virtual fileid_t get_fileid(const char *path) = 0;
8.31 -
8.32 - virtual Accessor *make_accessor(fileid_t fileid) = 0;
8.33 -
8.34 -public:
8.35 - explicit Opener(Accessing *accessing, Paging *paging, Pages *pages);
8.36 -
8.37 - OpenerContext *context();
8.38 -
8.39 - FilePager *open(const char *path);
8.40 -};
8.41 -
8.42 -// vim: tabstop=4 expandtab shiftwidth=4
9.1 --- a/opener_context.cc Sun Jan 24 19:29:07 2021 +0100
9.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
9.3 @@ -1,21 +0,0 @@
9.4 -#include "opener.h"
9.5 -#include "opener_context.h"
9.6 -
9.7 -/* Support for providing access to files. */
9.8 -
9.9 -OpenerContext::OpenerContext(Opener *opener, Memory *memory)
9.10 -: SimplePager(memory), _opener(opener)
9.11 -{
9.12 -}
9.13 -
9.14 -FilePager *OpenerContext::open()
9.15 -{
9.16 - return _opener->open(get_path());
9.17 -}
9.18 -
9.19 -char *OpenerContext::get_path()
9.20 -{
9.21 - return _region->read();
9.22 -}
9.23 -
9.24 -// vim: tabstop=4 expandtab shiftwidth=4
11.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
11.2 +++ b/opener_context_resource.cc Mon Feb 01 00:10:21 2021 +0100
11.3 @@ -0,0 +1,55 @@
11.4 +#include "opener_context_resource.h"
11.5 +#include "opener_context_object_server.h"
11.6 +#include "opener_resource.h"
11.7 +#include "resource_server.h"
11.8 +
11.9 +/* Support for providing access to files. */
11.10 +
11.11 +OpenerContextResource::OpenerContextResource(OpenerResource *opener, Memory *memory)
11.12 +: SimplePager(memory), _opener(opener)
11.13 +{
11.14 +}
11.15 +
11.16 +int OpenerContextResource::expected_items()
11.17 +{
11.18 + return OpenerContextObject_expected_items;
11.19 +}
11.20 +
11.21 +ipc_server_handler_type OpenerContextResource::handler()
11.22 +{
11.23 + return (ipc_server_handler_type) handle_OpenerContextObject;
11.24 +}
11.25 +
11.26 +
11.27 +
11.28 +/* Data access methods. */
11.29 +
11.30 +char *OpenerContextResource::get_path()
11.31 +{
11.32 + return _region->read();
11.33 +}
11.34 +
11.35 +
11.36 +
11.37 +/* Opener context interface methods. */
11.38 +
11.39 +long OpenerContextResource::open(int flags, size_t *size, l4_cap_idx_t *file)
11.40 +{
11.41 + /* NOTE: Support flags in the file pager method and obtain the file size. */
11.42 +
11.43 + FilePager *pager = _opener->open(get_path());
11.44 +
11.45 + /* Complete the initialisation and start a server in a new thread. */
11.46 +
11.47 + ResourceServer server(pager);
11.48 + long err = server.start_thread();
11.49 +
11.50 + /* Return the file size. */
11.51 + /* Return the server capability to the caller. */
11.52 +
11.53 + *size = pager->get_data_size();
11.54 + *file = server.config()->server;
11.55 + return err;
11.56 +}
11.57 +
11.58 +// vim: tabstop=4 expandtab shiftwidth=4
12.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
12.2 +++ b/opener_context_resource.h Mon Feb 01 00:10:21 2021 +0100
12.3 @@ -0,0 +1,51 @@
12.4 +#pragma once
12.5 +
12.6 +#include "opener_context_object_interface.h"
12.7 +#include "simple_pager.h"
12.8 +
12.9 +
12.10 +
12.11 +/* Forward declaration. */
12.12 +
12.13 +class OpenerResource;
12.14 +
12.15 +
12.16 +
12.17 +/* Support for indicating files to be opened. */
12.18 +
12.19 +class OpenerContextResource : public SimplePager, public OpenerContextObject
12.20 +{
12.21 +protected:
12.22 + OpenerResource *_opener;
12.23 +
12.24 +public:
12.25 + explicit OpenerContextResource(OpenerResource *opener, Memory *memory=NULL);
12.26 +
12.27 + /* Server details. */
12.28 +
12.29 + int expected_items();
12.30 +
12.31 + ipc_server_handler_type handler();
12.32 +
12.33 + void *interface()
12.34 + { return static_cast<OpenerContextObject *>(this); }
12.35 +
12.36 + /* Data access methods. */
12.37 +
12.38 + char *get_path();
12.39 +
12.40 + /* Opener context interface methods. */
12.41 +
12.42 + long open(int flags, size_t *size, l4_cap_idx_t *file);
12.43 +
12.44 + /* Pager/dataspace methods. */
12.45 +
12.46 + long map(unsigned long offset, l4_addr_t hot_spot, unsigned long flags,
12.47 + l4_snd_fpage_t *region)
12.48 + { return SimplePager::map(offset, hot_spot, flags, region); }
12.49 +
12.50 + long info(unsigned long *size, unsigned long *flags)
12.51 + { return SimplePager::info(size, flags); }
12.52 +};
12.53 +
12.54 +// vim: tabstop=4 expandtab shiftwidth=4
13.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
13.2 +++ b/opener_resource.cc Mon Feb 01 00:10:21 2021 +0100
13.3 @@ -0,0 +1,93 @@
13.4 +#include "opener_server.h"
13.5 +#include "opener_resource.h"
13.6 +#include "resource_server.h"
13.7 +
13.8 +/* Support for providing access to files. */
13.9 +
13.10 +OpenerResource::OpenerResource(Accessing *accessing, Paging *paging, Pages *pages)
13.11 +: _accessing(accessing), _paging(paging), _pages(pages)
13.12 +{
13.13 +}
13.14 +
13.15 +int OpenerResource::expected_items()
13.16 +{
13.17 + return Opener_expected_items;
13.18 +}
13.19 +
13.20 +ipc_server_handler_type OpenerResource::handler()
13.21 +{
13.22 + return (ipc_server_handler_type) handle_Opener;
13.23 +}
13.24 +
13.25 +
13.26 +
13.27 +/* Return a pager object for the given path. */
13.28 +
13.29 +FilePager *OpenerResource::open(const char *path)
13.30 +{
13.31 + fileid_t fileid = get_fileid(path);
13.32 +
13.33 + if (fileid == FILEID_INVALID)
13.34 + return NULL;
13.35 +
13.36 + Accessor *accessor = get_accessor(fileid);
13.37 + PageMapper *mapper = get_mapper(accessor);
13.38 + return new FilePager(fileid, mapper);
13.39 +}
13.40 +
13.41 +/* Obtain an accessor for the 'fileid' or register a new one in the accessing
13.42 + object. */
13.43 +
13.44 +Accessor *OpenerResource::get_accessor(fileid_t fileid)
13.45 +{
13.46 + Accessor *accessor;
13.47 +
13.48 + if (_accessing->reserve(fileid))
13.49 + accessor = _accessing->get(fileid);
13.50 + else
13.51 + {
13.52 + accessor = make_accessor(fileid);
13.53 + _accessing->set(fileid, accessor);
13.54 + }
13.55 +
13.56 + return accessor;
13.57 +}
13.58 +
13.59 +/* Obtain a page mapper for the 'accessor' or register a new one in the
13.60 + paging object. */
13.61 +
13.62 +PageMapper *OpenerResource::get_mapper(Accessor *accessor)
13.63 +{
13.64 + PageMapper *mapper;
13.65 +
13.66 + if (_paging->reserve(accessor))
13.67 + mapper = _paging->get(accessor);
13.68 + else
13.69 + {
13.70 + mapper = new PageMapper(accessor, _pages);
13.71 + _paging->set(accessor, mapper);
13.72 + }
13.73 +
13.74 + return mapper;
13.75 +}
13.76 +
13.77 +
13.78 +
13.79 +/* Opener interface methods. */
13.80 +
13.81 +long OpenerResource::context(l4_cap_idx_t *context)
13.82 +{
13.83 + OpenerContextResource *resource = new OpenerContextResource(this);
13.84 +
13.85 + /* Complete the initialisation and start a server in a new thread. */
13.86 +
13.87 + ResourceServer server(resource);
13.88 + long err = server.start_thread();
13.89 +
13.90 + /* Return the server capability to the caller. */
13.91 +
13.92 + *context = server.config()->server;
13.93 + return err;
13.94 +}
13.95 +
13.96 +// vim: tabstop=4 expandtab shiftwidth=4
14.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
14.2 +++ b/opener_resource.h Mon Feb 01 00:10:21 2021 +0100
14.3 @@ -0,0 +1,56 @@
14.4 +#pragma once
14.5 +
14.6 +#include <l4/sys/ipc.h>
14.7 +
14.8 +#include "accessing.h"
14.9 +#include "file_pager.h"
14.10 +#include "opener_context_resource.h"
14.11 +#include "opener_interface.h"
14.12 +#include "pager.h"
14.13 +#include "paging.h"
14.14 +#include "pages.h"
14.15 +#include "resource.h"
14.16 +
14.17 +/* Support for providing access to files. */
14.18 +
14.19 +class OpenerResource : public Resource, public Opener
14.20 +{
14.21 +protected:
14.22 + Accessing *_accessing;
14.23 + Paging *_paging;
14.24 + Pages *_pages;
14.25 +
14.26 + /* Convenience methods. */
14.27 +
14.28 + Accessor *get_accessor(fileid_t fileid);
14.29 +
14.30 + PageMapper *get_mapper(Accessor *accessor);
14.31 +
14.32 + /* Configurable methods. */
14.33 +
14.34 + virtual fileid_t get_fileid(const char *path) = 0;
14.35 +
14.36 + virtual Accessor *make_accessor(fileid_t fileid) = 0;
14.37 +
14.38 +public:
14.39 + explicit OpenerResource(Accessing *accessing, Paging *paging, Pages *pages);
14.40 +
14.41 + /* Server details. */
14.42 +
14.43 + int expected_items();
14.44 +
14.45 + ipc_server_handler_type handler();
14.46 +
14.47 + void *interface()
14.48 + { return static_cast<Opener *>(this); }
14.49 +
14.50 + /* Direct access methods. */
14.51 +
14.52 + FilePager *open(const char *path);
14.53 +
14.54 + /* Opener interface methods. */
14.55 +
14.56 + long context(l4_cap_idx_t *context);
14.57 +};
14.58 +
14.59 +// vim: tabstop=4 expandtab shiftwidth=4
16.1 --- a/resource.h Sun Jan 24 19:29:07 2021 +0100
16.2 +++ b/resource.h Mon Feb 01 00:10:21 2021 +0100
16.3 @@ -1,15 +1,56 @@
16.4 +/*
16.5 + * Common resource classes and functions.
16.6 + *
16.7 + * Copyright (C) 2018, 2019, 2020 Paul Boddie <paul@boddie.org.uk>
16.8 + *
16.9 + * This program is free software; you can redistribute it and/or
16.10 + * modify it under the terms of the GNU General Public License as
16.11 + * published by the Free Software Foundation; either version 2 of
16.12 + * the License, or (at your option) any later version.
16.13 + *
16.14 + * This program is distributed in the hope that it will be useful,
16.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16.17 + * GNU General Public License for more details.
16.18 + *
16.19 + * You should have received a copy of the GNU General Public License
16.20 + * along with this program; if not, write to the Free Software
16.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
16.22 + * Boston, MA 02110-1301, USA
16.23 + */
16.24 +
16.25 #pragma once
16.26
16.27 +#include <ipc/server.h>
16.28 +
16.29
16.30
16.31 -/* A resource interface. */
16.32 +/* A generic class for an object potentially needing to be closed after use. */
16.33
16.34 class Resource
16.35 {
16.36 public:
16.37 - virtual ~Resource();
16.38 + virtual ~Resource()
16.39 + {
16.40 + }
16.41 +
16.42 + /* Server details. */
16.43 +
16.44 + virtual int expected_items() = 0;
16.45 +
16.46 + virtual ipc_server_handler_type handler() = 0;
16.47 +
16.48 + virtual void *interface() = 0;
16.49
16.50 - virtual void close();
16.51 + /* Deallocation of resources. */
16.52 +
16.53 + virtual void close()
16.54 + {
16.55 + }
16.56 +
16.57 + /* Activation. */
16.58 +
16.59 + virtual void activate()
16.60 + {
16.61 + }
16.62 };
16.63 -
16.64 -// vim: tabstop=4 expandtab shiftwidth=4
17.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
17.2 +++ b/resource_server.cc Mon Feb 01 00:10:21 2021 +0100
17.3 @@ -0,0 +1,115 @@
17.4 +/*
17.5 + * Resource server functionality.
17.6 + *
17.7 + * Copyright (C) 2018, 2019, 2020 Paul Boddie <paul@boddie.org.uk>
17.8 + *
17.9 + * This program is free software; you can redistribute it and/or
17.10 + * modify it under the terms of the GNU General Public License as
17.11 + * published by the Free Software Foundation; either version 2 of
17.12 + * the License, or (at your option) any later version.
17.13 + *
17.14 + * This program is distributed in the hope that it will be useful,
17.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17.17 + * GNU General Public License for more details.
17.18 + *
17.19 + * You should have received a copy of the GNU General Public License
17.20 + * along with this program; if not, write to the Free Software
17.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17.22 + * Boston, MA 02110-1301, USA
17.23 + */
17.24 +
17.25 +#include <l4/re/c/util/cap_alloc.h>
17.26 +#include <l4/sys/types.h>
17.27 +
17.28 +#include <pthread-l4.h>
17.29 +#include <pthread.h>
17.30 +
17.31 +#include "resource_server.h"
17.32 +
17.33 +
17.34 +
17.35 +/* Convenience server methods. */
17.36 +
17.37 +/* Bind to a named IPC gate capability. */
17.38 +
17.39 +long ResourceServer::bind(const char *name)
17.40 +{
17.41 + return ipc_server_bind(name, (l4_umword_t) _resource, &_config->server);
17.42 +}
17.43 +
17.44 +/* Start in the same thread with no deletion notifications or finalisation. */
17.45 +
17.46 +long ResourceServer::start()
17.47 +{
17.48 + resource_init_config(_config, _resource);
17.49 + _config->thread = pthread_l4_cap(pthread_self());
17.50 + return resource_start_config(_config, _resource);
17.51 +}
17.52 +
17.53 +/* Start a new thread with deletion notifications and finalisation. */
17.54 +
17.55 +long ResourceServer::start_thread()
17.56 +{
17.57 + pthread_t thread;
17.58 + pthread_attr_t attr;
17.59 +
17.60 + pthread_attr_init(&attr);
17.61 + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
17.62 +
17.63 + resource_init_config(_config, _resource);
17.64 + pthread_create(&thread, &attr, ipc_server_start_mainloop, _config);
17.65 + resource_set_config_threaded(_config, pthread_l4_cap(thread), 1);
17.66 +
17.67 + return resource_start_config(_config, _resource);
17.68 +}
17.69 +
17.70 +
17.71 +
17.72 +/* Initialise a server configuration for a resource. */
17.73 +
17.74 +void resource_init_config(ipc_server_config_type *config, Resource *resource)
17.75 +{
17.76 + config->handler_obj = resource->interface();
17.77 + config->finaliser_obj = resource;
17.78 + config->expected_items = resource->expected_items();
17.79 + config->handler = resource->handler();
17.80 +}
17.81 +
17.82 +/* Set a configuration to be threaded. */
17.83 +
17.84 +void resource_set_config_threaded(ipc_server_config_type *config,
17.85 + l4_cap_idx_t thread, int new_thread)
17.86 +{
17.87 + config->finaliser = resource_thread_finaliser;
17.88 + config->new_thread = new_thread;
17.89 + config->thread = thread;
17.90 + config->notifications = 1;
17.91 +}
17.92 +
17.93 +/* Activate a resource and start a server for it. */
17.94 +
17.95 +long resource_start_config(ipc_server_config_type *config, Resource *resource)
17.96 +{
17.97 + resource->activate();
17.98 + return ipc_server_start_config(config);
17.99 +}
17.100 +
17.101 +
17.102 +
17.103 +/* A finaliser for exposed resources. */
17.104 +
17.105 +void resource_thread_finaliser(ipc_server_config_type *config)
17.106 +{
17.107 + Resource *resource = reinterpret_cast<Resource *>(config->finaliser_obj);
17.108 +
17.109 + /* Close and delete the resource. */
17.110 +
17.111 + resource->close();
17.112 + delete resource;
17.113 +
17.114 + /* Release the capabilities. */
17.115 +
17.116 + ipc_server_finalise_config(config);
17.117 + delete config;
17.118 +}
18.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
18.2 +++ b/resource_server.h Mon Feb 01 00:10:21 2021 +0100
18.3 @@ -0,0 +1,76 @@
18.4 +/*
18.5 + * Common resource server functions.
18.6 + *
18.7 + * Copyright (C) 2018, 2019, 2020 Paul Boddie <paul@boddie.org.uk>
18.8 + *
18.9 + * This program is free software; you can redistribute it and/or
18.10 + * modify it under the terms of the GNU General Public License as
18.11 + * published by the Free Software Foundation; either version 2 of
18.12 + * the License, or (at your option) any later version.
18.13 + *
18.14 + * This program is distributed in the hope that it will be useful,
18.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18.17 + * GNU General Public License for more details.
18.18 + *
18.19 + * You should have received a copy of the GNU General Public License
18.20 + * along with this program; if not, write to the Free Software
18.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
18.22 + * Boston, MA 02110-1301, USA
18.23 + */
18.24 +
18.25 +#pragma once
18.26 +
18.27 +#include "resource.h"
18.28 +#include <ipc/server.h>
18.29 +
18.30 +
18.31 +
18.32 +/* Convenience abstraction for blocking servers. */
18.33 +
18.34 +class ResourceServer
18.35 +{
18.36 +protected:
18.37 + Resource *_resource;
18.38 + ipc_server_config_type *_config;
18.39 +
18.40 +public:
18.41 + explicit ResourceServer(Resource *resource)
18.42 + : _resource(resource)
18.43 + {
18.44 + _config = new ipc_server_config_type;
18.45 + ipc_server_init_config(_config);
18.46 + }
18.47 +
18.48 + /* Access to configuration. */
18.49 +
18.50 + ipc_server_config_type *config()
18.51 + { return _config; }
18.52 +
18.53 + /* Server IPC gate allocation. */
18.54 +
18.55 + long bind(const char *name);
18.56 +
18.57 + /* Server initiation. */
18.58 +
18.59 + long start();
18.60 +
18.61 + long start_thread();
18.62 +};
18.63 +
18.64 +
18.65 +
18.66 +/* Server initialisation. */
18.67 +
18.68 +void resource_init_config(ipc_server_config_type *config, Resource *resource);
18.69 +
18.70 +void resource_set_config_threaded(ipc_server_config_type *config,
18.71 + l4_cap_idx_t thread, int new_thread);
18.72 +
18.73 +/* Server initiation. */
18.74 +
18.75 +long resource_start_config(ipc_server_config_type *config, Resource *resource);
18.76 +
18.77 +/* Server finalisation. */
18.78 +
18.79 +void resource_thread_finaliser(ipc_server_config_type *config);
19.1 --- a/simple_pager.cc Sun Jan 24 19:29:07 2021 +0100
19.2 +++ b/simple_pager.cc Mon Feb 01 00:10:21 2021 +0100
19.3 @@ -36,17 +36,25 @@
19.4
19.5 /* Send the flexpage explicitly. */
19.6
19.7 - long err = ipc_prepare_flexpage(&flexpage, offset, PAGE_SIZE, hot_spot, flags, region);
19.8 + long err = ipc_prepare_flexpage(&flexpage, offset, _region->size(), hot_spot, flags, region);
19.9
19.10 if (err)
19.11 - return err;
19.12 + return err;
19.13
19.14 err = complete_Dataspace_map(*region);
19.15
19.16 if (err)
19.17 - return err;
19.18 + return err;
19.19
19.20 return IPC_MESSAGE_SENT;
19.21 }
19.22
19.23 +long SimplePager::info(unsigned long *size, unsigned long *flags)
19.24 +{
19.25 + *size = _region->size();
19.26 + *flags = L4_FPAGE_RW;
19.27 +
19.28 + return L4_EOK;
19.29 +}
19.30 +
19.31 // vim: tabstop=4 expandtab shiftwidth=4
20.1 --- a/simple_pager.h Sun Jan 24 19:29:07 2021 +0100
20.2 +++ b/simple_pager.h Mon Feb 01 00:10:21 2021 +0100
20.3 @@ -23,6 +23,8 @@
20.4 /* Paging methods. */
20.5
20.6 long map(unsigned long offset, l4_addr_t hot_spot, unsigned long flags, l4_snd_fpage_t *region);
20.7 +
20.8 + long info(unsigned long *size, unsigned long *flags);
20.9 };
20.10
20.11 // vim: tabstop=4 expandtab shiftwidth=4
21.1 --- a/test_file_opener.cc Sun Jan 24 19:29:07 2021 +0100
21.2 +++ b/test_file_opener.cc Mon Feb 01 00:10:21 2021 +0100
21.3 @@ -6,8 +6,8 @@
21.4 /* Support for providing access to files. */
21.5
21.6 TestFileOpener::TestFileOpener(Accessing *accessing, Paging *paging, Pages *pages,
21.7 - offset_t file_size)
21.8 -: Opener(accessing, paging, pages), _file_size(file_size)
21.9 + offset_t file_size)
21.10 +: OpenerResource(accessing, paging, pages), _file_size(file_size)
21.11 {
21.12 }
21.13
22.1 --- a/test_file_opener.h Sun Jan 24 19:29:07 2021 +0100
22.2 +++ b/test_file_opener.h Mon Feb 01 00:10:21 2021 +0100
22.3 @@ -1,10 +1,10 @@
22.4 #pragma once
22.5
22.6 -#include "opener.h"
22.7 +#include "opener_resource.h"
22.8
22.9 /* Support for providing access to files. */
22.10
22.11 -class TestFileOpener : public Opener
22.12 +class TestFileOpener : public OpenerResource
22.13 {
22.14 protected:
22.15 offset_t _file_size;
22.16 @@ -17,7 +17,7 @@
22.17
22.18 public:
22.19 explicit TestFileOpener(Accessing *accessing, Paging *paging, Pages *pages,
22.20 - offset_t file_size=0);
22.21 + offset_t file_size=0);
22.22 };
22.23
22.24 // vim: tabstop=4 expandtab shiftwidth=4