1 /* 2 * Resource server functionality. 3 * 4 * Copyright (C) 2018, 2019, 2020 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/c/util/cap_alloc.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) _resource, &_config->server); 39 } 40 41 /* Start in the same thread with no deletion notifications or finalisation. */ 42 43 long ResourceServer::start() 44 { 45 resource_init_config(_config, _resource); 46 _config->thread = pthread_l4_cap(pthread_self()); 47 return resource_start_config(_config, _resource); 48 } 49 50 /* Start a new thread with deletion notifications and finalisation. */ 51 52 long ResourceServer::start_thread() 53 { 54 pthread_t thread; 55 pthread_attr_t attr; 56 57 pthread_attr_init(&attr); 58 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); 59 60 resource_init_config(_config, _resource); 61 pthread_create(&thread, &attr, ipc_server_start_mainloop, _config); 62 resource_set_config_threaded(_config, pthread_l4_cap(thread), 1); 63 64 return resource_start_config(_config, _resource); 65 } 66 67 68 69 /* Initialise a server configuration for a resource. */ 70 71 void resource_init_config(ipc_server_config_type *config, Resource *resource) 72 { 73 config->handler_obj = resource->interface(); 74 config->finaliser_obj = resource; 75 config->expected_items = resource->expected_items(); 76 config->handler = resource->handler(); 77 } 78 79 /* Set a configuration to be threaded. */ 80 81 void resource_set_config_threaded(ipc_server_config_type *config, 82 l4_cap_idx_t thread, int new_thread) 83 { 84 config->finaliser = resource_thread_finaliser; 85 config->new_thread = new_thread; 86 config->thread = thread; 87 config->notifications = 1; 88 } 89 90 /* Activate a resource and start a server for it. */ 91 92 long resource_start_config(ipc_server_config_type *config, Resource *resource) 93 { 94 resource->activate(); 95 return ipc_server_start_config(config); 96 } 97 98 99 100 /* A finaliser for exposed resources. */ 101 102 void resource_thread_finaliser(ipc_server_config_type *config) 103 { 104 Resource *resource = reinterpret_cast<Resource *>(config->finaliser_obj); 105 106 /* Close and delete the resource. */ 107 108 resource->close(); 109 delete resource; 110 111 /* Release the capabilities. */ 112 113 ipc_server_finalise_config(config); 114 delete config; 115 }