paul@10 | 1 | /* |
paul@10 | 2 | * Resource server functionality. |
paul@10 | 3 | * |
paul@621 | 4 | * Copyright (C) 2018-2022, 2024 Paul Boddie <paul@boddie.org.uk> |
paul@10 | 5 | * |
paul@10 | 6 | * This program is free software; you can redistribute it and/or |
paul@10 | 7 | * modify it under the terms of the GNU General Public License as |
paul@10 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@10 | 9 | * the License, or (at your option) any later version. |
paul@10 | 10 | * |
paul@10 | 11 | * This program is distributed in the hope that it will be useful, |
paul@10 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@10 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@10 | 14 | * GNU General Public License for more details. |
paul@10 | 15 | * |
paul@10 | 16 | * You should have received a copy of the GNU General Public License |
paul@10 | 17 | * along with this program; if not, write to the Free Software |
paul@10 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@10 | 19 | * Boston, MA 02110-1301, USA |
paul@10 | 20 | */ |
paul@10 | 21 | |
paul@517 | 22 | #include <l4/re/env.h> |
paul@10 | 23 | #include <l4/sys/types.h> |
paul@10 | 24 | |
paul@10 | 25 | #include <pthread-l4.h> |
paul@10 | 26 | #include <pthread.h> |
paul@10 | 27 | |
paul@10 | 28 | #include "resource_server.h" |
paul@10 | 29 | |
paul@10 | 30 | |
paul@10 | 31 | |
paul@10 | 32 | /* Convenience server methods. */ |
paul@10 | 33 | |
paul@10 | 34 | /* Bind to a named IPC gate capability. */ |
paul@10 | 35 | |
paul@10 | 36 | long ResourceServer::bind(const char *name) |
paul@10 | 37 | { |
paul@621 | 38 | _config->server = l4re_env_get_cap(name); |
paul@621 | 39 | return L4_EOK; |
paul@10 | 40 | } |
paul@10 | 41 | |
paul@538 | 42 | /* Start in the same thread indicating whether the server can be finalised. If |
paul@538 | 43 | so, deletion notifications will be used. */ |
paul@10 | 44 | |
paul@493 | 45 | long ResourceServer::start(bool finalisation) |
paul@10 | 46 | { |
paul@10 | 47 | resource_init_config(_config, _resource); |
paul@10 | 48 | _config->thread = pthread_l4_cap(pthread_self()); |
paul@493 | 49 | |
paul@517 | 50 | /* NOTE: On MIPS32, at least, in a payload started by libexec, the main thread |
paul@517 | 51 | is not necessarily returned correctly. */ |
paul@517 | 52 | |
paul@517 | 53 | if (l4_is_invalid_cap(_config->thread)) |
paul@517 | 54 | _config->thread = l4re_env()->main_thread; |
paul@517 | 55 | |
paul@493 | 56 | if (finalisation) |
paul@493 | 57 | { |
paul@493 | 58 | _config->finaliser = resource_same_thread_finaliser; |
paul@493 | 59 | _config->notifications = 1; |
paul@538 | 60 | _config->auto_deletion = 1; |
paul@493 | 61 | } |
paul@493 | 62 | |
paul@10 | 63 | return resource_start_config(_config, _resource); |
paul@10 | 64 | } |
paul@10 | 65 | |
paul@538 | 66 | /* Start serving a resource in an existing thread. The resource will not be |
paul@538 | 67 | finalised. */ |
paul@533 | 68 | |
paul@538 | 69 | long ResourceServer::start_in_thread(l4_cap_idx_t thread) |
paul@533 | 70 | { |
paul@533 | 71 | resource_init_config(_config, _resource); |
paul@538 | 72 | resource_set_config_threaded(_config, thread, 1, 0, 0); |
paul@10 | 73 | |
paul@533 | 74 | return resource_start_config(_config, _resource); |
paul@533 | 75 | } |
paul@533 | 76 | |
paul@533 | 77 | /* Start serving a resource in a new thread. */ |
paul@533 | 78 | |
paul@538 | 79 | long ResourceServer::start_thread(bool finalisation, bool auto_deletion) |
paul@10 | 80 | { |
paul@10 | 81 | pthread_t thread; |
paul@10 | 82 | pthread_attr_t attr; |
paul@43 | 83 | long err; |
paul@10 | 84 | |
paul@10 | 85 | pthread_attr_init(&attr); |
paul@10 | 86 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
paul@10 | 87 | |
paul@10 | 88 | resource_init_config(_config, _resource); |
paul@43 | 89 | |
paul@43 | 90 | err = pthread_create(&thread, &attr, ipc_server_start_mainloop, _config); |
paul@43 | 91 | if (err) |
paul@43 | 92 | return err; |
paul@43 | 93 | |
paul@538 | 94 | resource_set_config_threaded(_config, pthread_l4_cap(thread), 1, finalisation, |
paul@538 | 95 | auto_deletion); |
paul@10 | 96 | |
paul@10 | 97 | return resource_start_config(_config, _resource); |
paul@10 | 98 | } |
paul@10 | 99 | |
paul@385 | 100 | /* A convenience method starting a thread and returning the server capability |
paul@385 | 101 | employed via the given parameter. */ |
paul@120 | 102 | |
paul@538 | 103 | long ResourceServer::start_thread(l4_cap_idx_t *server, bool finalisation, |
paul@538 | 104 | bool auto_deletion) |
paul@120 | 105 | { |
paul@538 | 106 | long err = start_thread(finalisation, auto_deletion); |
paul@120 | 107 | |
paul@120 | 108 | if (!err) |
paul@120 | 109 | *server = _config->server; |
paul@120 | 110 | |
paul@120 | 111 | return err; |
paul@120 | 112 | } |
paul@120 | 113 | |
paul@10 | 114 | |
paul@10 | 115 | |
paul@10 | 116 | /* Initialise a server configuration for a resource. */ |
paul@10 | 117 | |
paul@10 | 118 | void resource_init_config(ipc_server_config_type *config, Resource *resource) |
paul@10 | 119 | { |
paul@459 | 120 | ipc_server_default_config_type default_config = resource->config(); |
paul@459 | 121 | |
paul@10 | 122 | config->handler_obj = resource->interface(); |
paul@10 | 123 | config->finaliser_obj = resource; |
paul@459 | 124 | config->expected_items = default_config.expected_items; |
paul@459 | 125 | config->handler = default_config.handler; |
paul@10 | 126 | } |
paul@10 | 127 | |
paul@10 | 128 | /* Set a configuration to be threaded. */ |
paul@10 | 129 | |
paul@10 | 130 | void resource_set_config_threaded(ipc_server_config_type *config, |
paul@538 | 131 | l4_cap_idx_t thread, int separate_thread, |
paul@538 | 132 | int finalisation, int auto_deletion) |
paul@10 | 133 | { |
paul@539 | 134 | /* Only invoke the finaliser where auto-deletion is employed. This assumes |
paul@539 | 135 | that a single configuration is in use and thus a single resource that can |
paul@539 | 136 | be managed in the server finalisation process. Where multiple |
paul@539 | 137 | configurations are involved with an IPC gate, server finalisation does not |
paul@539 | 138 | attempt to finalise all these configurations and associated resources, and |
paul@539 | 139 | it therefore makes more sense to perform such finalisation elsewhere. */ |
paul@539 | 140 | |
paul@539 | 141 | if (auto_deletion) |
paul@538 | 142 | config->finaliser = resource_thread_finaliser; |
paul@538 | 143 | |
paul@538 | 144 | config->config_thread = separate_thread; |
paul@10 | 145 | config->thread = thread; |
paul@533 | 146 | config->notifications = finalisation; |
paul@538 | 147 | config->auto_deletion = auto_deletion; |
paul@10 | 148 | } |
paul@10 | 149 | |
paul@10 | 150 | /* Activate a resource and start a server for it. */ |
paul@10 | 151 | |
paul@10 | 152 | long resource_start_config(ipc_server_config_type *config, Resource *resource) |
paul@10 | 153 | { |
paul@10 | 154 | resource->activate(); |
paul@20 | 155 | long err = ipc_server_start_config(config); |
paul@20 | 156 | |
paul@20 | 157 | /* Discard any server resources if starting it failed. */ |
paul@20 | 158 | |
paul@20 | 159 | if (err) |
paul@20 | 160 | { |
paul@20 | 161 | ipc_server_finalise_config(config); |
paul@20 | 162 | ipc_server_discard_thread(config); |
paul@20 | 163 | } |
paul@20 | 164 | |
paul@20 | 165 | return err; |
paul@10 | 166 | } |
paul@10 | 167 | |
paul@10 | 168 | |
paul@10 | 169 | |
paul@493 | 170 | /* A finaliser for exposed resources in the same thread. */ |
paul@493 | 171 | |
paul@493 | 172 | void resource_same_thread_finaliser(ipc_server_config_type *config) |
paul@493 | 173 | { |
paul@493 | 174 | Resource *resource = reinterpret_cast<Resource *>(config->finaliser_obj); |
paul@493 | 175 | |
paul@493 | 176 | /* Close but do not delete the resource since it is assumed that it is being |
paul@493 | 177 | managed by the thread. */ |
paul@493 | 178 | |
paul@493 | 179 | resource->close(); |
paul@493 | 180 | |
paul@493 | 181 | /* Release the capabilities. */ |
paul@493 | 182 | |
paul@493 | 183 | ipc_server_finalise_config(config); |
paul@493 | 184 | delete config; |
paul@493 | 185 | } |
paul@493 | 186 | |
paul@493 | 187 | /* A finaliser for exposed resources in a different thread. */ |
paul@10 | 188 | |
paul@10 | 189 | void resource_thread_finaliser(ipc_server_config_type *config) |
paul@10 | 190 | { |
paul@10 | 191 | Resource *resource = reinterpret_cast<Resource *>(config->finaliser_obj); |
paul@10 | 192 | |
paul@10 | 193 | /* Close and delete the resource. */ |
paul@10 | 194 | |
paul@10 | 195 | resource->close(); |
paul@10 | 196 | delete resource; |
paul@10 | 197 | |
paul@10 | 198 | /* Release the capabilities. */ |
paul@10 | 199 | |
paul@10 | 200 | ipc_server_finalise_config(config); |
paul@10 | 201 | delete config; |
paul@10 | 202 | } |
paul@43 | 203 | |
paul@43 | 204 | // vim: tabstop=2 expandtab shiftwidth=2 |