1 /* 2 * Resource server functionality. 3 * 4 * Copyright (C) 2018, 2019, 2020, 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 <l4/re/env.h> 23 #include <l4/sys/types.h> 24 25 #include <pthread-l4.h> 26 #include <pthread.h> 27 28 #include "resource_server.h" 29 30 31 32 /* Convenience server methods. */ 33 34 /* Bind to a named IPC gate capability. */ 35 36 long ResourceServer::bind(const char *name) 37 { 38 return ipc_server_bind(name, (l4_umword_t) &_config->endpoint, &_config->server); 39 } 40 41 /* Start in the same thread indicating whether deletion notifications and 42 finalisation are to be used. */ 43 44 long ResourceServer::start(bool finalisation) 45 { 46 resource_init_config(_config, _resource); 47 _config->thread = pthread_l4_cap(pthread_self()); 48 49 /* NOTE: On MIPS32, at least, in a payload started by libexec, the main thread 50 is not necessarily returned correctly. */ 51 52 if (l4_is_invalid_cap(_config->thread)) 53 _config->thread = l4re_env()->main_thread; 54 55 if (finalisation) 56 { 57 _config->finaliser = resource_same_thread_finaliser; 58 _config->irq_endpoint.config = _config; 59 } 60 61 return resource_start_config(_config, _resource); 62 } 63 64 /* Start a new thread with deletion notifications and finalisation. */ 65 66 long ResourceServer::start_thread() 67 { 68 pthread_t thread; 69 pthread_attr_t attr; 70 long err; 71 72 pthread_attr_init(&attr); 73 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); 74 75 resource_init_config(_config, _resource); 76 77 err = pthread_create(&thread, &attr, ipc_server_start_mainloop, _config); 78 if (err) 79 return err; 80 81 resource_set_config_threaded(_config, pthread_l4_cap(thread), 1); 82 83 return resource_start_config(_config, _resource); 84 } 85 86 /* A convenience method starting a thread and returning the server capability 87 employed via the given parameter. */ 88 89 long ResourceServer::start_thread(l4_cap_idx_t *server) 90 { 91 long err = start_thread(); 92 93 if (!err) 94 *server = _config->server; 95 96 return err; 97 } 98 99 100 101 /* Initialise a server configuration for a resource. */ 102 103 void resource_init_config(ipc_server_config_type *config, Resource *resource) 104 { 105 ipc_server_default_config_type default_config = resource->config(); 106 107 config->handler_obj = resource->interface(); 108 config->finaliser_obj = resource; 109 config->expected_items = default_config.expected_items; 110 config->handler = default_config.handler; 111 } 112 113 /* Set a configuration to be threaded. */ 114 115 void resource_set_config_threaded(ipc_server_config_type *config, 116 l4_cap_idx_t thread, int new_thread) 117 { 118 config->finaliser = resource_thread_finaliser; 119 config->config_thread = new_thread; 120 config->thread = thread; 121 config->irq_endpoint.config = config; 122 } 123 124 /* Activate a resource and start a server for it. */ 125 126 long resource_start_config(ipc_server_config_type *config, Resource *resource) 127 { 128 resource->activate(); 129 long err = ipc_server_start_config(config); 130 131 /* Discard any server resources if starting it failed. */ 132 133 if (err) 134 { 135 ipc_server_finalise_config(config); 136 ipc_server_discard_thread(config); 137 } 138 139 return err; 140 } 141 142 143 144 /* A finaliser for exposed resources in the same thread. */ 145 146 void resource_same_thread_finaliser(ipc_server_config_type *config) 147 { 148 Resource *resource = reinterpret_cast<Resource *>(config->finaliser_obj); 149 150 /* Close but do not delete the resource since it is assumed that it is being 151 managed by the thread. */ 152 153 resource->close(); 154 155 /* Release the capabilities. */ 156 157 ipc_server_finalise_config(config); 158 delete config; 159 } 160 161 /* A finaliser for exposed resources in a different thread. */ 162 163 void resource_thread_finaliser(ipc_server_config_type *config) 164 { 165 Resource *resource = reinterpret_cast<Resource *>(config->finaliser_obj); 166 167 /* Close and delete the resource. */ 168 169 resource->close(); 170 delete resource; 171 172 /* Release the capabilities. */ 173 174 ipc_server_finalise_config(config); 175 delete config; 176 } 177 178 // vim: tabstop=2 expandtab shiftwidth=2