[05641a9e] | 1 | /*
|
---|
| 2 | * Copyright (c) 2009 Jakub Jermar
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
[174156fd] | 29 | /** @addtogroup kernel_generic
|
---|
[05641a9e] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file
|
---|
[13a638d] | 34 | * @brief Kernel event notifications.
|
---|
[05641a9e] | 35 | */
|
---|
| 36 |
|
---|
[63e27ef] | 37 | #include <assert.h>
|
---|
[13a638d] | 38 | #include <ipc/event.h>
|
---|
[05641a9e] | 39 | #include <mm/slab.h>
|
---|
[d99c1d2] | 40 | #include <typedefs.h>
|
---|
[05641a9e] | 41 | #include <synch/spinlock.h>
|
---|
| 42 | #include <console/console.h>
|
---|
[5d0500c] | 43 | #include <proc/task.h>
|
---|
[05641a9e] | 44 | #include <errno.h>
|
---|
| 45 | #include <arch.h>
|
---|
| 46 |
|
---|
[e49b57b2] | 47 | /** The events array. */
|
---|
[13a638d] | 48 | static event_t events[EVENT_END];
|
---|
[05641a9e] | 49 |
|
---|
[5d0500c] | 50 | static void event_initialize(event_t *event)
|
---|
| 51 | {
|
---|
| 52 | spinlock_initialize(&event->lock, "event.lock");
|
---|
| 53 | event->answerbox = NULL;
|
---|
| 54 | event->counter = 0;
|
---|
| 55 | event->imethod = 0;
|
---|
| 56 | event->masked = false;
|
---|
| 57 | event->unmask_callback = NULL;
|
---|
| 58 | }
|
---|
| 59 |
|
---|
[8820544] | 60 | static event_t *evno2event(int evno, task_t *task)
|
---|
[5d0500c] | 61 | {
|
---|
[63e27ef] | 62 | assert(evno < EVENT_TASK_END);
|
---|
[a35b458] | 63 |
|
---|
[5d0500c] | 64 | event_t *event;
|
---|
[a35b458] | 65 |
|
---|
[5d0500c] | 66 | if (evno < EVENT_END)
|
---|
| 67 | event = &events[(event_type_t) evno];
|
---|
| 68 | else
|
---|
[8820544] | 69 | event = &task->events[(event_task_type_t) evno - EVENT_END];
|
---|
[a35b458] | 70 |
|
---|
[5d0500c] | 71 | return event;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[f9061b4] | 74 | /** Initialize kernel events.
|
---|
| 75 | *
|
---|
| 76 | */
|
---|
[05641a9e] | 77 | void event_init(void)
|
---|
| 78 | {
|
---|
[5d0500c] | 79 | for (unsigned int i = 0; i < EVENT_END; i++)
|
---|
| 80 | event_initialize(evno2event(i, NULL));
|
---|
[05641a9e] | 81 | }
|
---|
| 82 |
|
---|
[0fe52ef] | 83 | void event_task_init(task_t *task)
|
---|
[5d0500c] | 84 | {
|
---|
| 85 | for (unsigned int i = EVENT_END; i < EVENT_TASK_END; i++)
|
---|
[0fe52ef] | 86 | event_initialize(evno2event(i, task));
|
---|
[5d0500c] | 87 | }
|
---|
| 88 |
|
---|
[f9061b4] | 89 | /** Unsubscribe kernel events associated with an answerbox
|
---|
| 90 | *
|
---|
| 91 | * @param answerbox Answerbox to be unsubscribed.
|
---|
| 92 | *
|
---|
| 93 | */
|
---|
| 94 | void event_cleanup_answerbox(answerbox_t *answerbox)
|
---|
| 95 | {
|
---|
| 96 | for (unsigned int i = 0; i < EVENT_END; i++) {
|
---|
| 97 | spinlock_lock(&events[i].lock);
|
---|
[a35b458] | 98 |
|
---|
[f9061b4] | 99 | if (events[i].answerbox == answerbox) {
|
---|
| 100 | events[i].answerbox = NULL;
|
---|
| 101 | events[i].counter = 0;
|
---|
| 102 | events[i].imethod = 0;
|
---|
| 103 | events[i].masked = false;
|
---|
| 104 | }
|
---|
[a35b458] | 105 |
|
---|
[f9061b4] | 106 | spinlock_unlock(&events[i].lock);
|
---|
| 107 | }
|
---|
| 108 | }
|
---|
| 109 |
|
---|
[5d0500c] | 110 | static void _event_set_unmask_callback(event_t *event, event_callback_t callback)
|
---|
| 111 | {
|
---|
| 112 | spinlock_lock(&event->lock);
|
---|
| 113 | event->unmask_callback = callback;
|
---|
| 114 | spinlock_unlock(&event->lock);
|
---|
| 115 | }
|
---|
| 116 |
|
---|
[0496c17] | 117 | /** Define a callback function for the event unmask event.
|
---|
| 118 | *
|
---|
[97d42d5] | 119 | * @param evno Event type.
|
---|
| 120 | * @param callback Callback function to be called when
|
---|
| 121 | * the event is unmasked.
|
---|
[0496c17] | 122 | *
|
---|
| 123 | */
|
---|
[97d42d5] | 124 | void event_set_unmask_callback(event_type_t evno, event_callback_t callback)
|
---|
[0496c17] | 125 | {
|
---|
[63e27ef] | 126 | assert(evno < EVENT_END);
|
---|
[a35b458] | 127 |
|
---|
[5d0500c] | 128 | _event_set_unmask_callback(evno2event(evno, NULL), callback);
|
---|
| 129 | }
|
---|
| 130 |
|
---|
[0fe52ef] | 131 | void event_task_set_unmask_callback(task_t *task, event_task_type_t evno,
|
---|
[5d0500c] | 132 | event_callback_t callback)
|
---|
| 133 | {
|
---|
[63e27ef] | 134 | assert(evno >= (int) EVENT_END);
|
---|
| 135 | assert(evno < EVENT_TASK_END);
|
---|
[a35b458] | 136 |
|
---|
[0fe52ef] | 137 | _event_set_unmask_callback(evno2event(evno, task), callback);
|
---|
[5d0500c] | 138 | }
|
---|
| 139 |
|
---|
[b7fd2a0] | 140 | static errno_t event_enqueue(event_t *event, bool mask, sysarg_t a1, sysarg_t a2,
|
---|
[5d0500c] | 141 | sysarg_t a3, sysarg_t a4, sysarg_t a5)
|
---|
| 142 | {
|
---|
[b7fd2a0] | 143 | errno_t res;
|
---|
[5d0500c] | 144 |
|
---|
| 145 | spinlock_lock(&event->lock);
|
---|
[a35b458] | 146 |
|
---|
[5d0500c] | 147 | if (event->answerbox != NULL) {
|
---|
| 148 | if (!event->masked) {
|
---|
[90efa3b] | 149 | call_t *call = ipc_call_alloc();
|
---|
[a35b458] | 150 |
|
---|
[5d0500c] | 151 | if (call) {
|
---|
| 152 | call->flags |= IPC_CALL_NOTIF;
|
---|
| 153 | call->priv = ++event->counter;
|
---|
[a35b458] | 154 |
|
---|
[fafb8e5] | 155 | ipc_set_imethod(&call->data, event->imethod);
|
---|
| 156 | ipc_set_arg1(&call->data, a1);
|
---|
| 157 | ipc_set_arg2(&call->data, a2);
|
---|
| 158 | ipc_set_arg3(&call->data, a3);
|
---|
| 159 | ipc_set_arg4(&call->data, a4);
|
---|
| 160 | ipc_set_arg5(&call->data, a5);
|
---|
[a35b458] | 161 |
|
---|
[e2ab36f1] | 162 | call->data.task_id = TASK ? TASK->taskid : 0;
|
---|
[a35b458] | 163 |
|
---|
[cfaa35a] | 164 | irq_spinlock_lock(&event->answerbox->irq_lock,
|
---|
| 165 | true);
|
---|
| 166 | list_append(&call->ab_link,
|
---|
| 167 | &event->answerbox->irq_notifs);
|
---|
| 168 | irq_spinlock_unlock(&event->answerbox->irq_lock,
|
---|
| 169 | true);
|
---|
[a35b458] | 170 |
|
---|
[cfaa35a] | 171 | waitq_wakeup(&event->answerbox->wq,
|
---|
| 172 | WAKEUP_FIRST);
|
---|
[a35b458] | 173 |
|
---|
[5d0500c] | 174 | if (mask)
|
---|
| 175 | event->masked = true;
|
---|
[a35b458] | 176 |
|
---|
[5d0500c] | 177 | res = EOK;
|
---|
| 178 | } else
|
---|
| 179 | res = ENOMEM;
|
---|
| 180 | } else
|
---|
| 181 | res = EBUSY;
|
---|
| 182 | } else
|
---|
| 183 | res = ENOENT;
|
---|
[a35b458] | 184 |
|
---|
[5d0500c] | 185 | spinlock_unlock(&event->lock);
|
---|
| 186 | return res;
|
---|
[0496c17] | 187 | }
|
---|
| 188 |
|
---|
[f9061b4] | 189 | /** Send kernel notification event
|
---|
| 190 | *
|
---|
| 191 | * @param evno Event type.
|
---|
| 192 | * @param mask Mask further notifications after a successful
|
---|
| 193 | * sending.
|
---|
| 194 | * @param a1 First argument.
|
---|
| 195 | * @param a2 Second argument.
|
---|
| 196 | * @param a3 Third argument.
|
---|
| 197 | * @param a4 Fourth argument.
|
---|
| 198 | * @param a5 Fifth argument.
|
---|
| 199 | *
|
---|
| 200 | * @return EOK if notification was successfully sent.
|
---|
| 201 | * @return ENOMEM if the notification IPC message failed to allocate.
|
---|
| 202 | * @return EBUSY if the notifications of the given type are
|
---|
| 203 | * currently masked.
|
---|
| 204 | * @return ENOENT if the notifications of the given type are
|
---|
| 205 | * currently not subscribed.
|
---|
| 206 | *
|
---|
| 207 | */
|
---|
[b7fd2a0] | 208 | errno_t event_notify(event_type_t evno, bool mask, sysarg_t a1, sysarg_t a2,
|
---|
[f9061b4] | 209 | sysarg_t a3, sysarg_t a4, sysarg_t a5)
|
---|
| 210 | {
|
---|
[63e27ef] | 211 | assert(evno < EVENT_END);
|
---|
[a35b458] | 212 |
|
---|
[5d0500c] | 213 | return event_enqueue(evno2event(evno, NULL), mask, a1, a2, a3, a4, a5);
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 | /** Send per-task kernel notification event
|
---|
| 217 | *
|
---|
[0fe52ef] | 218 | * @param task Destination task.
|
---|
[5d0500c] | 219 | * @param evno Event type.
|
---|
| 220 | * @param mask Mask further notifications after a successful
|
---|
| 221 | * sending.
|
---|
| 222 | * @param a1 First argument.
|
---|
| 223 | * @param a2 Second argument.
|
---|
| 224 | * @param a3 Third argument.
|
---|
| 225 | * @param a4 Fourth argument.
|
---|
| 226 | * @param a5 Fifth argument.
|
---|
| 227 | *
|
---|
| 228 | * @return EOK if notification was successfully sent.
|
---|
| 229 | * @return ENOMEM if the notification IPC message failed to allocate.
|
---|
| 230 | * @return EBUSY if the notifications of the given type are
|
---|
| 231 | * currently masked.
|
---|
| 232 | * @return ENOENT if the notifications of the given type are
|
---|
| 233 | * currently not subscribed.
|
---|
| 234 | *
|
---|
| 235 | */
|
---|
[b7fd2a0] | 236 | errno_t event_task_notify(task_t *task, event_task_type_t evno, bool mask,
|
---|
[0fe52ef] | 237 | sysarg_t a1, sysarg_t a2, sysarg_t a3, sysarg_t a4, sysarg_t a5)
|
---|
[5d0500c] | 238 | {
|
---|
[63e27ef] | 239 | assert(evno >= (int) EVENT_END);
|
---|
| 240 | assert(evno < EVENT_TASK_END);
|
---|
[a35b458] | 241 |
|
---|
[0fe52ef] | 242 | return event_enqueue(evno2event(evno, task), mask, a1, a2, a3, a4, a5);
|
---|
[f9061b4] | 243 | }
|
---|
| 244 |
|
---|
| 245 | /** Subscribe event notifications
|
---|
| 246 | *
|
---|
| 247 | * @param evno Event type.
|
---|
| 248 | * @param imethod IPC interface and method to be used for
|
---|
| 249 | * the notifications.
|
---|
| 250 | * @param answerbox Answerbox to send the notifications to.
|
---|
| 251 | *
|
---|
| 252 | * @return EOK if the subscription was successful.
|
---|
[8a637a4] | 253 | * @return EEXIST if the notifications of the given type are
|
---|
[f9061b4] | 254 | * already subscribed.
|
---|
| 255 | *
|
---|
| 256 | */
|
---|
[b7fd2a0] | 257 | static errno_t event_subscribe(event_t *event, sysarg_t imethod,
|
---|
[98000fb] | 258 | answerbox_t *answerbox)
|
---|
[05641a9e] | 259 | {
|
---|
[b7fd2a0] | 260 | errno_t res;
|
---|
[a35b458] | 261 |
|
---|
[5d0500c] | 262 | spinlock_lock(&event->lock);
|
---|
[a35b458] | 263 |
|
---|
[5d0500c] | 264 | if (event->answerbox == NULL) {
|
---|
| 265 | event->answerbox = answerbox;
|
---|
| 266 | event->imethod = imethod;
|
---|
| 267 | event->counter = 0;
|
---|
| 268 | event->masked = false;
|
---|
[05641a9e] | 269 | res = EOK;
|
---|
[13a638d] | 270 | } else
|
---|
[8a637a4] | 271 | res = EEXIST;
|
---|
[a35b458] | 272 |
|
---|
[5d0500c] | 273 | spinlock_unlock(&event->lock);
|
---|
[a35b458] | 274 |
|
---|
[05641a9e] | 275 | return res;
|
---|
| 276 | }
|
---|
| 277 |
|
---|
[8820544] | 278 | /** Unsubscribe event notifications
|
---|
| 279 | *
|
---|
| 280 | * @param evno Event type.
|
---|
| 281 | * @param answerbox Answerbox used to send the notifications to.
|
---|
| 282 | *
|
---|
| 283 | * @return EOK if the subscription was successful.
|
---|
[8a637a4] | 284 | * @return EEXIST if the notifications of the given type are
|
---|
[8820544] | 285 | * already subscribed.
|
---|
| 286 | *
|
---|
| 287 | */
|
---|
[b7fd2a0] | 288 | static errno_t event_unsubscribe(event_t *event, answerbox_t *answerbox)
|
---|
[8820544] | 289 | {
|
---|
[b7fd2a0] | 290 | errno_t res;
|
---|
[a35b458] | 291 |
|
---|
[8820544] | 292 | spinlock_lock(&event->lock);
|
---|
[a35b458] | 293 |
|
---|
[8820544] | 294 | if (event->answerbox == answerbox) {
|
---|
| 295 | event->answerbox = NULL;
|
---|
| 296 | event->counter = 0;
|
---|
| 297 | event->imethod = 0;
|
---|
| 298 | event->masked = false;
|
---|
| 299 | res = EOK;
|
---|
| 300 | } else
|
---|
| 301 | res = ENOENT;
|
---|
[a35b458] | 302 |
|
---|
[8820544] | 303 | spinlock_unlock(&event->lock);
|
---|
[a35b458] | 304 |
|
---|
[8820544] | 305 | return res;
|
---|
| 306 | }
|
---|
| 307 |
|
---|
[f9061b4] | 308 | /** Unmask event notifications
|
---|
| 309 | *
|
---|
| 310 | * @param evno Event type to unmask.
|
---|
| 311 | *
|
---|
| 312 | */
|
---|
[5d0500c] | 313 | static void event_unmask(event_t *event)
|
---|
[05641a9e] | 314 | {
|
---|
[5d0500c] | 315 | spinlock_lock(&event->lock);
|
---|
| 316 | event->masked = false;
|
---|
| 317 | event_callback_t callback = event->unmask_callback;
|
---|
| 318 | spinlock_unlock(&event->lock);
|
---|
[a35b458] | 319 |
|
---|
[0496c17] | 320 | /*
|
---|
[97d42d5] | 321 | * Check if there is an unmask callback
|
---|
| 322 | * function defined for this event.
|
---|
[0496c17] | 323 | */
|
---|
[97d42d5] | 324 | if (callback != NULL)
|
---|
[5d0500c] | 325 | callback(event);
|
---|
[05641a9e] | 326 | }
|
---|
| 327 |
|
---|
[8820544] | 328 | /** Event notification subscription syscall wrapper
|
---|
[f9061b4] | 329 | *
|
---|
| 330 | * @param evno Event type to subscribe.
|
---|
| 331 | * @param imethod IPC interface and method to be used for
|
---|
| 332 | * the notifications.
|
---|
| 333 | *
|
---|
| 334 | * @return EOK on success.
|
---|
| 335 | * @return ELIMIT on unknown event type.
|
---|
[8a637a4] | 336 | * @return EEXIST if the notifications of the given type are
|
---|
[f9061b4] | 337 | * already subscribed.
|
---|
| 338 | *
|
---|
| 339 | */
|
---|
[b7fd2a0] | 340 | sys_errno_t sys_ipc_event_subscribe(sysarg_t evno, sysarg_t imethod)
|
---|
[05641a9e] | 341 | {
|
---|
[5d0500c] | 342 | if (evno >= EVENT_TASK_END)
|
---|
[f9061b4] | 343 | return ELIMIT;
|
---|
[a35b458] | 344 |
|
---|
[b7fd2a0] | 345 | return (sys_errno_t) event_subscribe(evno2event(evno, TASK),
|
---|
[5d0500c] | 346 | (sysarg_t) imethod, &TASK->answerbox);
|
---|
[05641a9e] | 347 | }
|
---|
| 348 |
|
---|
[8820544] | 349 | /** Event notification unsubscription syscall wrapper
|
---|
| 350 | *
|
---|
| 351 | * @param evno Event type to unsubscribe.
|
---|
| 352 | *
|
---|
| 353 | * @return EOK on success.
|
---|
| 354 | * @return ELIMIT on unknown event type.
|
---|
| 355 | * @return ENOENT if the notification of the given type is not
|
---|
[7c3fb9b] | 356 | * subscribed.
|
---|
[8820544] | 357 | */
|
---|
[b7fd2a0] | 358 | sys_errno_t sys_ipc_event_unsubscribe(sysarg_t evno)
|
---|
[8820544] | 359 | {
|
---|
| 360 | if (evno >= EVENT_TASK_END)
|
---|
| 361 | return ELIMIT;
|
---|
[a35b458] | 362 |
|
---|
[b7fd2a0] | 363 | return (sys_errno_t) event_unsubscribe(evno2event(evno, TASK),
|
---|
[8820544] | 364 | &TASK->answerbox);
|
---|
| 365 | }
|
---|
| 366 |
|
---|
[f9061b4] | 367 | /** Event notification unmask syscall wrapper
|
---|
| 368 | *
|
---|
| 369 | * Note that currently no tests are performed whether the calling
|
---|
| 370 | * task is entitled to unmask the notifications. However, thanks
|
---|
| 371 | * to the fact that notification masking is only a performance
|
---|
| 372 | * optimization, this has probably no security implications.
|
---|
| 373 | *
|
---|
| 374 | * @param evno Event type to unmask.
|
---|
| 375 | *
|
---|
| 376 | * @return EOK on success.
|
---|
| 377 | * @return ELIMIT on unknown event type.
|
---|
| 378 | *
|
---|
| 379 | */
|
---|
[b7fd2a0] | 380 | sys_errno_t sys_ipc_event_unmask(sysarg_t evno)
|
---|
[05641a9e] | 381 | {
|
---|
[5d0500c] | 382 | if (evno >= EVENT_TASK_END)
|
---|
[f9061b4] | 383 | return ELIMIT;
|
---|
[a35b458] | 384 |
|
---|
[5d0500c] | 385 | event_unmask(evno2event(evno, TASK));
|
---|
| 386 |
|
---|
[f9061b4] | 387 | return EOK;
|
---|
[05641a9e] | 388 | }
|
---|
| 389 |
|
---|
| 390 | /** @}
|
---|
| 391 | */
|
---|