1 /* 2 * A pipe opener resource. 3 * 4 * Copyright (C) 2021, 2022 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 <ipc/cap_alloc.h> 23 24 #include <l4/cxx/exceptions> 25 26 #include "pipe_opener_resource.h" 27 #include "pipe_opener_server.h" 28 #include "pipe_pager.h" 29 #include "resource_server.h" 30 31 32 33 /* Support for providing access to pipes. */ 34 35 PipeOpenerResource::PipeOpenerResource(Memory *memory) 36 : _memory(memory) 37 { 38 } 39 40 int PipeOpenerResource::expected_items() 41 { 42 return PipeOpener_expected_items; 43 } 44 45 ipc_server_handler_type PipeOpenerResource::handler() 46 { 47 return (ipc_server_handler_type) handle_PipeOpener; 48 } 49 50 51 52 /* Pipe opener interface methods. */ 53 54 long PipeOpenerResource::pipe(offset_t size, l4_cap_idx_t *reader, l4_cap_idx_t *writer) 55 { 56 /* Both endpoints will employ a common paging coordinator. */ 57 58 PipePaging *paging; 59 60 try 61 { 62 paging = new PipePaging(_memory, size); 63 } 64 catch (L4::Out_of_memory const &e) 65 { 66 return -L4_ENOMEM; 67 } 68 69 /* Each endpoint will have its own pager. */ 70 71 long err = open_endpoint(paging, true, writer); 72 73 if (err) 74 return err; 75 76 err = open_endpoint(paging, false, reader); 77 78 /* Failure to open an endpoint should invalidate both, plus the paging 79 object. Also, any active server thread would need to be cancelled. The 80 freeing of the writer capability attempts to do this. */ 81 82 if (err) 83 { 84 ipc_cap_free_um(*writer); 85 return err; 86 } 87 88 return L4_EOK; 89 } 90 91 /* Start the endpoint server in a new thread. If the thread does not start, the 92 resource (including pager) will be finalised. */ 93 94 long PipeOpenerResource::open_endpoint(PipePaging *paging, bool writing, l4_cap_idx_t *endpoint) 95 { 96 PipePager *pager = new PipePager(paging, writing); 97 98 return ResourceServer(pager).start_thread(endpoint); 99 } 100 101 // vim: tabstop=4 expandtab shiftwidth=4