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