1 /* 2 * A pipe opener resource. 3 * 4 * Copyright (C) 2021 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation; either version 2 of 9 * the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * Boston, MA 02110-1301, USA 20 */ 21 22 #include <mem/memory_incremental.h> 23 24 #include "pipe_opener_resource.h" 25 #include "pipe_opener_server.h" 26 #include "pipe_pager.h" 27 #include "resource_server.h" 28 29 30 31 /* Support for providing access to pipes. */ 32 33 PipeOpenerResource::PipeOpenerResource(Memory *memory) 34 : _memory(memory) 35 { 36 } 37 38 int PipeOpenerResource::expected_items() 39 { 40 return PipeOpener_expected_items; 41 } 42 43 ipc_server_handler_type PipeOpenerResource::handler() 44 { 45 return (ipc_server_handler_type) handle_PipeOpener; 46 } 47 48 49 50 /* Pipe opener interface methods. */ 51 52 long PipeOpenerResource::pipe(offset_t size, l4_cap_idx_t *reader, l4_cap_idx_t *writer) 53 { 54 /* Both endpoints will employ a common paging coordinator. */ 55 56 PipePaging *paging = new PipePaging(_memory, size); 57 58 /* Each endpoint will have its own pager. */ 59 60 /* NOTE: Failure to open an endpoint should invalidate both, plus the 61 paging object. Also, any active server thread would need to be 62 cancelled. */ 63 64 return open_endpoint(paging, false, reader) || open_endpoint(paging, true, writer); 65 } 66 67 long PipeOpenerResource::open_endpoint(PipePaging *paging, bool writing, l4_cap_idx_t *endpoint) 68 { 69 PipePager *pager = new PipePager(paging, writing); 70 71 /* Start the endpoint server in a new thread. 72 If the thread does not start, the resource should be finalised. */ 73 74 ResourceServer server(pager); 75 long err = server.start_thread(); 76 77 /* Return the server capability to the caller. */ 78 79 if (!err) 80 *endpoint = server.config()->server; 81 82 return err; 83 } 84 85 // vim: tabstop=4 expandtab shiftwidth=4