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 "memory_incremental.h" 23 #include "pipe_opener_resource.h" 24 #include "pipe_opener_server.h" 25 #include "pipe_pager.h" 26 #include "resource_server.h" 27 28 29 30 /* Support for providing access to pipes. */ 31 32 PipeOpenerResource::PipeOpenerResource(Memory *memory) 33 : _memory(memory) 34 { 35 } 36 37 int PipeOpenerResource::expected_items() 38 { 39 return PipeOpener_expected_items; 40 } 41 42 ipc_server_handler_type PipeOpenerResource::handler() 43 { 44 return (ipc_server_handler_type) handle_PipeOpener; 45 } 46 47 48 49 /* Pipe opener interface methods. */ 50 51 long PipeOpenerResource::pipe(offset_t size, l4_cap_idx_t *reader, l4_cap_idx_t *writer) 52 { 53 /* Both endpoints will employ a common paging coordinator. */ 54 55 PipePaging *paging = new PipePaging(_memory, size); 56 57 /* Each endpoint will have its own pager. */ 58 59 /* NOTE: Failure to open an endpoint should invalidate both, plus the 60 paging object. Also, any active server thread would need to be 61 cancelled. */ 62 63 return open_endpoint(paging, false, reader) || open_endpoint(paging, true, writer); 64 } 65 66 long PipeOpenerResource::open_endpoint(PipePaging *paging, bool writing, l4_cap_idx_t *endpoint) 67 { 68 PipePager *pager = new PipePager(paging, writing); 69 70 /* Start the endpoint server in a new thread. 71 If the thread does not start, the resource should be finalised. */ 72 73 ResourceServer server(pager); 74 long err = server.start_thread(); 75 76 /* Return the server capability to the caller. */ 77 78 if (!err) 79 *endpoint = server.config()->server; 80 81 return err; 82 } 83 84 // vim: tabstop=4 expandtab shiftwidth=4