| 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 |
|
|---|
| 29 | /** @addtogroup generic
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /**
|
|---|
| 33 | * @file
|
|---|
| 34 | * @brief Kernel event notifications.
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 | #include <ipc/event.h>
|
|---|
| 38 | #include <mm/slab.h>
|
|---|
| 39 | #include <typedefs.h>
|
|---|
| 40 | #include <synch/spinlock.h>
|
|---|
| 41 | #include <console/console.h>
|
|---|
| 42 | #include <memstr.h>
|
|---|
| 43 | #include <errno.h>
|
|---|
| 44 | #include <arch.h>
|
|---|
| 45 |
|
|---|
| 46 | /** The events array. */
|
|---|
| 47 | static event_t events[EVENT_END];
|
|---|
| 48 |
|
|---|
| 49 | /** Initialize kernel events.
|
|---|
| 50 | *
|
|---|
| 51 | */
|
|---|
| 52 | void event_init(void)
|
|---|
| 53 | {
|
|---|
| 54 | for (unsigned int i = 0; i < EVENT_END; i++) {
|
|---|
| 55 | spinlock_initialize(&events[i].lock, "event.lock");
|
|---|
| 56 | events[i].answerbox = NULL;
|
|---|
| 57 | events[i].counter = 0;
|
|---|
| 58 | events[i].imethod = 0;
|
|---|
| 59 | events[i].masked = false;
|
|---|
| 60 | events[i].unmask_callback = NULL;
|
|---|
| 61 | }
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | /** Unsubscribe kernel events associated with an answerbox
|
|---|
| 65 | *
|
|---|
| 66 | * @param answerbox Answerbox to be unsubscribed.
|
|---|
| 67 | *
|
|---|
| 68 | */
|
|---|
| 69 | void event_cleanup_answerbox(answerbox_t *answerbox)
|
|---|
| 70 | {
|
|---|
| 71 | for (unsigned int i = 0; i < EVENT_END; i++) {
|
|---|
| 72 | spinlock_lock(&events[i].lock);
|
|---|
| 73 |
|
|---|
| 74 | if (events[i].answerbox == answerbox) {
|
|---|
| 75 | events[i].answerbox = NULL;
|
|---|
| 76 | events[i].counter = 0;
|
|---|
| 77 | events[i].imethod = 0;
|
|---|
| 78 | events[i].masked = false;
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | spinlock_unlock(&events[i].lock);
|
|---|
| 82 | }
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | /** Define a callback function for the event unmask event.
|
|---|
| 86 | *
|
|---|
| 87 | * @param evno Event type.
|
|---|
| 88 | * @param callback Callback function to be called when
|
|---|
| 89 | * the event is unmasked.
|
|---|
| 90 | *
|
|---|
| 91 | */
|
|---|
| 92 | void event_set_unmask_callback(event_type_t evno, event_callback_t callback)
|
|---|
| 93 | {
|
|---|
| 94 | ASSERT(evno < EVENT_END);
|
|---|
| 95 |
|
|---|
| 96 | spinlock_lock(&events[evno].lock);
|
|---|
| 97 | events[evno].unmask_callback = callback;
|
|---|
| 98 | spinlock_unlock(&events[evno].lock);
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | /** Send kernel notification event
|
|---|
| 102 | *
|
|---|
| 103 | * @param evno Event type.
|
|---|
| 104 | * @param mask Mask further notifications after a successful
|
|---|
| 105 | * sending.
|
|---|
| 106 | * @param a1 First argument.
|
|---|
| 107 | * @param a2 Second argument.
|
|---|
| 108 | * @param a3 Third argument.
|
|---|
| 109 | * @param a4 Fourth argument.
|
|---|
| 110 | * @param a5 Fifth argument.
|
|---|
| 111 | *
|
|---|
| 112 | * @return EOK if notification was successfully sent.
|
|---|
| 113 | * @return ENOMEM if the notification IPC message failed to allocate.
|
|---|
| 114 | * @return EBUSY if the notifications of the given type are
|
|---|
| 115 | * currently masked.
|
|---|
| 116 | * @return ENOENT if the notifications of the given type are
|
|---|
| 117 | * currently not subscribed.
|
|---|
| 118 | *
|
|---|
| 119 | */
|
|---|
| 120 | int event_notify(event_type_t evno, bool mask, sysarg_t a1, sysarg_t a2,
|
|---|
| 121 | sysarg_t a3, sysarg_t a4, sysarg_t a5)
|
|---|
| 122 | {
|
|---|
| 123 | ASSERT(evno < EVENT_END);
|
|---|
| 124 |
|
|---|
| 125 | spinlock_lock(&events[evno].lock);
|
|---|
| 126 |
|
|---|
| 127 | int ret;
|
|---|
| 128 |
|
|---|
| 129 | if (events[evno].answerbox != NULL) {
|
|---|
| 130 | if (!events[evno].masked) {
|
|---|
| 131 | call_t *call = ipc_call_alloc(FRAME_ATOMIC);
|
|---|
| 132 |
|
|---|
| 133 | if (call) {
|
|---|
| 134 | call->flags |= IPC_CALL_NOTIF;
|
|---|
| 135 | call->priv = ++events[evno].counter;
|
|---|
| 136 |
|
|---|
| 137 | IPC_SET_IMETHOD(call->data, events[evno].imethod);
|
|---|
| 138 | IPC_SET_ARG1(call->data, a1);
|
|---|
| 139 | IPC_SET_ARG2(call->data, a2);
|
|---|
| 140 | IPC_SET_ARG3(call->data, a3);
|
|---|
| 141 | IPC_SET_ARG4(call->data, a4);
|
|---|
| 142 | IPC_SET_ARG5(call->data, a5);
|
|---|
| 143 |
|
|---|
| 144 | irq_spinlock_lock(&events[evno].answerbox->irq_lock, true);
|
|---|
| 145 | list_append(&call->link, &events[evno].answerbox->irq_notifs);
|
|---|
| 146 | irq_spinlock_unlock(&events[evno].answerbox->irq_lock, true);
|
|---|
| 147 |
|
|---|
| 148 | waitq_wakeup(&events[evno].answerbox->wq, WAKEUP_FIRST);
|
|---|
| 149 |
|
|---|
| 150 | if (mask)
|
|---|
| 151 | events[evno].masked = true;
|
|---|
| 152 |
|
|---|
| 153 | ret = EOK;
|
|---|
| 154 | } else
|
|---|
| 155 | ret = ENOMEM;
|
|---|
| 156 | } else
|
|---|
| 157 | ret = EBUSY;
|
|---|
| 158 | } else
|
|---|
| 159 | ret = ENOENT;
|
|---|
| 160 |
|
|---|
| 161 | spinlock_unlock(&events[evno].lock);
|
|---|
| 162 |
|
|---|
| 163 | return ret;
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | /** Subscribe event notifications
|
|---|
| 167 | *
|
|---|
| 168 | * @param evno Event type.
|
|---|
| 169 | * @param imethod IPC interface and method to be used for
|
|---|
| 170 | * the notifications.
|
|---|
| 171 | * @param answerbox Answerbox to send the notifications to.
|
|---|
| 172 | *
|
|---|
| 173 | * @return EOK if the subscription was successful.
|
|---|
| 174 | * @return EEXISTS if the notifications of the given type are
|
|---|
| 175 | * already subscribed.
|
|---|
| 176 | *
|
|---|
| 177 | */
|
|---|
| 178 | static int event_subscribe(event_type_t evno, sysarg_t imethod,
|
|---|
| 179 | answerbox_t *answerbox)
|
|---|
| 180 | {
|
|---|
| 181 | ASSERT(evno < EVENT_END);
|
|---|
| 182 |
|
|---|
| 183 | spinlock_lock(&events[evno].lock);
|
|---|
| 184 |
|
|---|
| 185 | int res;
|
|---|
| 186 |
|
|---|
| 187 | if (events[evno].answerbox == NULL) {
|
|---|
| 188 | events[evno].answerbox = answerbox;
|
|---|
| 189 | events[evno].imethod = imethod;
|
|---|
| 190 | events[evno].counter = 0;
|
|---|
| 191 | events[evno].masked = false;
|
|---|
| 192 | res = EOK;
|
|---|
| 193 | } else
|
|---|
| 194 | res = EEXISTS;
|
|---|
| 195 |
|
|---|
| 196 | spinlock_unlock(&events[evno].lock);
|
|---|
| 197 |
|
|---|
| 198 | return res;
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | /** Unmask event notifications
|
|---|
| 202 | *
|
|---|
| 203 | * @param evno Event type to unmask.
|
|---|
| 204 | *
|
|---|
| 205 | */
|
|---|
| 206 | static void event_unmask(event_type_t evno)
|
|---|
| 207 | {
|
|---|
| 208 | ASSERT(evno < EVENT_END);
|
|---|
| 209 |
|
|---|
| 210 | spinlock_lock(&events[evno].lock);
|
|---|
| 211 | events[evno].masked = false;
|
|---|
| 212 | event_callback_t callback = events[evno].unmask_callback;
|
|---|
| 213 | spinlock_unlock(&events[evno].lock);
|
|---|
| 214 |
|
|---|
| 215 | /*
|
|---|
| 216 | * Check if there is an unmask callback
|
|---|
| 217 | * function defined for this event.
|
|---|
| 218 | */
|
|---|
| 219 | if (callback != NULL)
|
|---|
| 220 | callback();
|
|---|
| 221 | }
|
|---|
| 222 |
|
|---|
| 223 | /** Event notification syscall wrapper
|
|---|
| 224 | *
|
|---|
| 225 | * @param evno Event type to subscribe.
|
|---|
| 226 | * @param imethod IPC interface and method to be used for
|
|---|
| 227 | * the notifications.
|
|---|
| 228 | *
|
|---|
| 229 | * @return EOK on success.
|
|---|
| 230 | * @return ELIMIT on unknown event type.
|
|---|
| 231 | * @return EEXISTS if the notifications of the given type are
|
|---|
| 232 | * already subscribed.
|
|---|
| 233 | *
|
|---|
| 234 | */
|
|---|
| 235 | sysarg_t sys_event_subscribe(sysarg_t evno, sysarg_t imethod)
|
|---|
| 236 | {
|
|---|
| 237 | if (evno >= EVENT_END)
|
|---|
| 238 | return ELIMIT;
|
|---|
| 239 |
|
|---|
| 240 | return (sysarg_t) event_subscribe((event_type_t) evno, (sysarg_t)
|
|---|
| 241 | imethod, &TASK->answerbox);
|
|---|
| 242 | }
|
|---|
| 243 |
|
|---|
| 244 | /** Event notification unmask syscall wrapper
|
|---|
| 245 | *
|
|---|
| 246 | * Note that currently no tests are performed whether the calling
|
|---|
| 247 | * task is entitled to unmask the notifications. However, thanks
|
|---|
| 248 | * to the fact that notification masking is only a performance
|
|---|
| 249 | * optimization, this has probably no security implications.
|
|---|
| 250 | *
|
|---|
| 251 | * @param evno Event type to unmask.
|
|---|
| 252 | *
|
|---|
| 253 | * @return EOK on success.
|
|---|
| 254 | * @return ELIMIT on unknown event type.
|
|---|
| 255 | *
|
|---|
| 256 | */
|
|---|
| 257 | sysarg_t sys_event_unmask(sysarg_t evno)
|
|---|
| 258 | {
|
|---|
| 259 | if (evno >= EVENT_END)
|
|---|
| 260 | return ELIMIT;
|
|---|
| 261 |
|
|---|
| 262 | event_unmask((event_type_t) evno);
|
|---|
| 263 | return EOK;
|
|---|
| 264 | }
|
|---|
| 265 |
|
|---|
| 266 | /** @}
|
|---|
| 267 | */
|
|---|