1 /* 2 * Generic object event notification support. 3 * 4 * Copyright (C) 2021, 2022, 2023 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 <map> 23 #include <mutex> 24 25 #include <l4/sys/irq.h> 26 27 #include <ipc/cap_alloc.h> 28 #include <ipc/server.h> 29 #include <resource/resource_server.h> 30 31 #include "notification_client.h" 32 #include "notifier.h" 33 #include "notifier_server.h" 34 35 36 37 /* Null notification state. */ 38 39 static ObjectNotificationState _null_state; 40 41 42 43 /* Lock protecting per-task notifier access. */ 44 45 static std::mutex _lock; 46 47 /* Per-task storage for specific waiting operations. */ 48 49 static SpecificObjectNotifier *_notifier = NULL; 50 51 52 53 /* Return the per-task notifier for object-specific waiting operations. */ 54 55 SpecificObjectNotifier *notifier_get_task_notifier() 56 { 57 std::lock_guard<std::mutex> guard(_lock); 58 59 /* Start any new notifier. */ 60 61 if (_notifier == NULL) 62 { 63 _notifier = new SpecificObjectNotifier; 64 _notifier->start(); 65 } 66 67 return _notifier; 68 } 69 70 /* Return a local notifier for general object waiting operations. */ 71 72 GeneralObjectNotifier *notifier_get_local_notifier() 73 { 74 GeneralObjectNotifier *notifier = new GeneralObjectNotifier; 75 76 notifier->start(); 77 return notifier; 78 } 79 80 81 82 /* Virtual destructor required for base class instance reference deletion. */ 83 84 ObjectNotifier::~ObjectNotifier() 85 { 86 stop(); 87 88 ServerConfigs::iterator it; 89 90 for (it = _configs.begin(); it != _configs.end(); it++) 91 delete *it; 92 93 _configs.clear(); 94 95 /* Handle deletion of the special task notifier. */ 96 97 if (this == _notifier) 98 _notifier = NULL; 99 } 100 101 102 103 /* Start listening for notifications. */ 104 105 long ObjectNotifier::start() 106 { 107 if (_started) 108 return L4_EOK; 109 110 /* Create a new thread to serve a "null" resource. This resource is not used 111 for notifications but merely for control purposes. */ 112 113 NotifierResource *notifier = new NotifierResource; 114 ResourceServer server(notifier); 115 long err = server.start_thread(true, false); 116 117 if (err) 118 return err; 119 120 _configs.push_back(server.config()); 121 _started = true; 122 123 /* Retain the IRQ created for the server for control purposes. */ 124 125 _irq = server.config()->irq; 126 127 return L4_EOK; 128 } 129 130 131 132 /* Stop the notifier. */ 133 134 void ObjectNotifier::stop() 135 { 136 if (l4_is_valid_cap(_irq)) 137 { 138 l4_irq_trigger(_irq); 139 _irq = L4_INVALID_CAP; 140 } 141 } 142 143 144 145 /* Return notification state for the given object or null state if no record 146 existed for the object. */ 147 148 ObjectNotificationState &ObjectNotifier::object_state(notifiable_t *object, bool create) 149 { 150 ObjectNotificationStates::iterator it = _state.find(object); 151 152 if (it == _state.end()) 153 { 154 if (create) 155 return _state[object]; 156 else 157 return _null_state; 158 } 159 160 return it->second; 161 } 162 163 /* Subscribe to notification events on an object. */ 164 165 long ObjectNotifier::subscribe(notifiable_t *object, notify_flags_t flags) 166 { 167 /* Acquire the lock for state lookup. */ 168 169 std::unique_lock<std::mutex> state_guard(_state_lock); 170 171 /* Obtain potentially new state for the object. */ 172 173 ObjectNotificationState &state = object_state(object, true); 174 175 if (state.is_null()) 176 { 177 /* Serve the new object in the notifier thread. */ 178 179 NotifierResource *resource = new NotifierResource(this, object); 180 ResourceServer server(resource); 181 long err = server.start_in_thread(_configs.front()->thread); 182 183 if (err) 184 return err; 185 186 _configs.push_back(server.config()); 187 state.endpoint = server.config()->server; 188 } 189 190 /* Subscribe, sending the notification endpoint via the principal reference 191 for the object. */ 192 193 client_Notification notify(object->base->ref); 194 195 return notify.subscribe(state.endpoint, flags); 196 } 197 198 /* Unsubscribe from notification events on an object. */ 199 200 long ObjectNotifier::unsubscribe(notifiable_t *object) 201 { 202 /* Acquire the lock for state lookup. */ 203 204 std::unique_lock<std::mutex> state_guard(_state_lock); 205 206 ObjectNotificationState &state = object_state(object, false); 207 208 if (state.is_null()) 209 return -L4_ENOENT; 210 211 /* Unsubscribe via the notification interface. */ 212 213 client_Notification notify(object->base->ref); 214 215 notify.unsubscribe(state.endpoint); 216 217 return remove_endpoint(object, state.endpoint); 218 } 219 220 /* Remove a notification endpoint for an object. */ 221 222 long ObjectNotifier::remove_endpoint(notifiable_t *object, l4_cap_idx_t endpoint) 223 { 224 if (l4_is_invalid_cap(endpoint)) 225 return -L4_EINVAL; 226 227 ipc_cap_free_um(endpoint); 228 229 _state.erase(object); 230 231 /* Remove the lock for updating object state. */ 232 233 _object_locks.erase(object); 234 235 return L4_EOK; 236 } 237 238 239 240 /* Handle a notification event for an object. */ 241 242 void GeneralObjectNotifier::notify(notifiable_t *object, notify_flags_t flags, 243 notify_values_t values) 244 { 245 /* Enter critical section for the notifier (affecting all objects). */ 246 247 std::unique_lock<std::mutex> general_guard(_general_lock); 248 249 /* Acquire the lock for state lookup. */ 250 251 std::unique_lock<std::mutex> state_guard(_state_lock); 252 253 ObjectNotificationState &state = object_state(object, false); 254 255 if (state.is_null()) 256 return; 257 258 /* Acquire the lock for the object state itself. */ 259 260 std::unique_lock<std::mutex> object_guard(state.lock); 261 262 /* Record flags and note previous flags. */ 263 264 notify_flags_t recorded = state.pending_notifications; 265 266 state.pending_notifications |= flags; 267 state.pending_values = values; 268 269 /* Add an object queue entry for any objects without previous notifications. */ 270 271 if (!recorded) 272 _affected.push_back(object); 273 274 /* Notify any waiting caller. */ 275 276 _general_condition.notify_one(); 277 } 278 279 void SpecificObjectNotifier::notify(notifiable_t *object, notify_flags_t flags, 280 notify_values_t values) 281 { 282 /* Acquire the lock for state lookup. */ 283 284 std::unique_lock<std::mutex> state_guard(_state_lock); 285 286 ObjectNotificationState &state = object_state(object, false); 287 288 if (state.is_null()) 289 return; 290 291 /* Acquire the lock for the object state itself. */ 292 293 std::unique_lock<std::mutex> object_guard(state.lock); 294 295 state.pending_notifications |= flags; 296 state.pending_values = values; 297 298 /* Notify any waiting caller. */ 299 300 state.condition.notify_one(); 301 } 302 303 304 305 /* Transfer pending notifications to the given object. This must be called with 306 a lock acquired on the object notification state. */ 307 308 bool ObjectNotifier::_transfer(ObjectNotificationState &state, notifiable_t *object) 309 { 310 notify_flags_t recorded = state.pending_notifications; 311 312 if (recorded) 313 { 314 object->notifications = recorded; 315 object->values = state.pending_values; 316 state.pending_notifications = 0; 317 return true; 318 } 319 320 return false; 321 } 322 323 324 325 /* Obtain object state and transfer notifications. */ 326 327 bool GeneralObjectNotifier::_retrieve_for_object(notifiable_t *object) 328 { 329 /* Acquire the lock for state lookup. */ 330 331 std::unique_lock<std::mutex> state_guard(_state_lock); 332 333 ObjectNotificationState &state = object_state(object, false); 334 335 if (state.is_null()) 336 return false; 337 338 /* Acquire the lock for the object state itself, then release the state lock. */ 339 340 std::unique_lock<std::mutex> object_guard(state.lock); 341 342 state_guard.unlock(); 343 344 /* Call generic method to transfer notifications, if possible. */ 345 346 return _transfer(state, object); 347 } 348 349 /* Obtain queued objects until one is found that still has events recorded for 350 it. This must be called with the notifier's general lock acquired. */ 351 352 bool GeneralObjectNotifier::_retrieve(notifiable_t **object) 353 { 354 while (!_affected.empty()) 355 { 356 *object = _affected.front(); 357 _affected.pop_front(); 358 359 if (_retrieve_for_object(*object)) 360 return true; 361 } 362 363 return false; 364 } 365 366 367 368 /* Wait for notification events on objects. */ 369 370 long GeneralObjectNotifier::wait(notifiable_t **object) 371 { 372 std::unique_lock<std::mutex> general_guard(_general_lock); 373 374 while (1) 375 { 376 /* With pending notifications, update the first object and exit. */ 377 378 if (_retrieve(object)) 379 break; 380 381 /* Otherwise, wait for notifications. */ 382 383 _general_condition.wait(general_guard); 384 } 385 386 return L4_EOK; 387 } 388 389 /* Wait for notifications from a single object. */ 390 391 long SpecificObjectNotifier::wait_object(notifiable_t *object) 392 { 393 /* Acquire the lock for reading object state. */ 394 395 std::unique_lock<std::mutex> state_guard(_state_lock); 396 397 ObjectNotificationState &state = object_state(object, false); 398 399 if (state.is_null()) 400 return -L4_EINVAL; 401 402 /* Acquire the lock for the object state itself, then release the state lock. */ 403 404 std::unique_lock<std::mutex> object_guard(state.lock); 405 406 state_guard.unlock(); 407 408 while (1) 409 { 410 /* With pending notifications, update the object and exit. */ 411 412 if (_transfer(state, object)) 413 break; 414 415 /* Otherwise, wait for notifications. */ 416 417 state.condition.wait(object_guard); 418 } 419 420 return L4_EOK; 421 } 422 423 424 425 /* Object-specific resource methods. */ 426 427 ipc_server_default_config_type NotifierResource::config() 428 { 429 return config_Notifier; 430 } 431 432 /* Register a notification received by an object-specific resource. */ 433 434 long NotifierResource::notify(notify_flags_t flags, notify_values_t values) 435 { 436 if (_notifier != NULL) 437 _notifier->notify(_object, flags, values); 438 439 return L4_EOK; 440 } 441 442 // vim: tabstop=2 expandtab shiftwidth=2