source: mainline/kernel/generic/src/ipc/event.c@ 44a7ee5

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 44a7ee5 was 44a7ee5, checked in by Jiri Svoboda <jiri@…>, 8 years ago

memxxx functions should be provided in the kernel via the same header as in userspace (mem.h).

  • Property mode set to 100644
File size: 9.9 KB
Line 
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 <proc/task.h>
43#include <errno.h>
44#include <arch.h>
45
46/** The events array. */
47static event_t events[EVENT_END];
48
49static void event_initialize(event_t *event)
50{
51 spinlock_initialize(&event->lock, "event.lock");
52 event->answerbox = NULL;
53 event->counter = 0;
54 event->imethod = 0;
55 event->masked = false;
56 event->unmask_callback = NULL;
57}
58
59static event_t *evno2event(int evno, task_t *task)
60{
61 ASSERT(evno < EVENT_TASK_END);
62
63 event_t *event;
64
65 if (evno < EVENT_END)
66 event = &events[(event_type_t) evno];
67 else
68 event = &task->events[(event_task_type_t) evno - EVENT_END];
69
70 return event;
71}
72
73/** Initialize kernel events.
74 *
75 */
76void event_init(void)
77{
78 for (unsigned int i = 0; i < EVENT_END; i++)
79 event_initialize(evno2event(i, NULL));
80}
81
82void event_task_init(task_t *task)
83{
84 for (unsigned int i = EVENT_END; i < EVENT_TASK_END; i++)
85 event_initialize(evno2event(i, task));
86}
87
88/** Unsubscribe kernel events associated with an answerbox
89 *
90 * @param answerbox Answerbox to be unsubscribed.
91 *
92 */
93void event_cleanup_answerbox(answerbox_t *answerbox)
94{
95 for (unsigned int i = 0; i < EVENT_END; i++) {
96 spinlock_lock(&events[i].lock);
97
98 if (events[i].answerbox == answerbox) {
99 events[i].answerbox = NULL;
100 events[i].counter = 0;
101 events[i].imethod = 0;
102 events[i].masked = false;
103 }
104
105 spinlock_unlock(&events[i].lock);
106 }
107}
108
109static void _event_set_unmask_callback(event_t *event, event_callback_t callback)
110{
111 spinlock_lock(&event->lock);
112 event->unmask_callback = callback;
113 spinlock_unlock(&event->lock);
114}
115
116/** Define a callback function for the event unmask event.
117 *
118 * @param evno Event type.
119 * @param callback Callback function to be called when
120 * the event is unmasked.
121 *
122 */
123void event_set_unmask_callback(event_type_t evno, event_callback_t callback)
124{
125 ASSERT(evno < EVENT_END);
126
127 _event_set_unmask_callback(evno2event(evno, NULL), callback);
128}
129
130void event_task_set_unmask_callback(task_t *task, event_task_type_t evno,
131 event_callback_t callback)
132{
133 ASSERT(evno >= (int) EVENT_END);
134 ASSERT(evno < EVENT_TASK_END);
135
136 _event_set_unmask_callback(evno2event(evno, task), callback);
137}
138
139static int event_enqueue(event_t *event, bool mask, sysarg_t a1, sysarg_t a2,
140 sysarg_t a3, sysarg_t a4, sysarg_t a5)
141{
142 int res;
143
144 spinlock_lock(&event->lock);
145
146 if (event->answerbox != NULL) {
147 if (!event->masked) {
148 call_t *call = ipc_call_alloc(FRAME_ATOMIC);
149
150 if (call) {
151 call->flags |= IPC_CALL_NOTIF;
152 call->priv = ++event->counter;
153
154 IPC_SET_IMETHOD(call->data, event->imethod);
155 IPC_SET_ARG1(call->data, a1);
156 IPC_SET_ARG2(call->data, a2);
157 IPC_SET_ARG3(call->data, a3);
158 IPC_SET_ARG4(call->data, a4);
159 IPC_SET_ARG5(call->data, a5);
160
161 call->data.task_id = TASK ? TASK->taskid : 0;
162
163 irq_spinlock_lock(&event->answerbox->irq_lock,
164 true);
165 list_append(&call->ab_link,
166 &event->answerbox->irq_notifs);
167 irq_spinlock_unlock(&event->answerbox->irq_lock,
168 true);
169
170 waitq_wakeup(&event->answerbox->wq,
171 WAKEUP_FIRST);
172
173 if (mask)
174 event->masked = true;
175
176 res = EOK;
177 } else
178 res = ENOMEM;
179 } else
180 res = EBUSY;
181 } else
182 res = ENOENT;
183
184 spinlock_unlock(&event->lock);
185 return res;
186}
187
188/** Send kernel notification event
189 *
190 * @param evno Event type.
191 * @param mask Mask further notifications after a successful
192 * sending.
193 * @param a1 First argument.
194 * @param a2 Second argument.
195 * @param a3 Third argument.
196 * @param a4 Fourth argument.
197 * @param a5 Fifth argument.
198 *
199 * @return EOK if notification was successfully sent.
200 * @return ENOMEM if the notification IPC message failed to allocate.
201 * @return EBUSY if the notifications of the given type are
202 * currently masked.
203 * @return ENOENT if the notifications of the given type are
204 * currently not subscribed.
205 *
206 */
207int event_notify(event_type_t evno, bool mask, sysarg_t a1, sysarg_t a2,
208 sysarg_t a3, sysarg_t a4, sysarg_t a5)
209{
210 ASSERT(evno < EVENT_END);
211
212 return event_enqueue(evno2event(evno, NULL), mask, a1, a2, a3, a4, a5);
213}
214
215/** Send per-task kernel notification event
216 *
217 * @param task Destination task.
218 * @param evno Event type.
219 * @param mask Mask further notifications after a successful
220 * sending.
221 * @param a1 First argument.
222 * @param a2 Second argument.
223 * @param a3 Third argument.
224 * @param a4 Fourth argument.
225 * @param a5 Fifth argument.
226 *
227 * @return EOK if notification was successfully sent.
228 * @return ENOMEM if the notification IPC message failed to allocate.
229 * @return EBUSY if the notifications of the given type are
230 * currently masked.
231 * @return ENOENT if the notifications of the given type are
232 * currently not subscribed.
233 *
234 */
235int event_task_notify(task_t *task, event_task_type_t evno, bool mask,
236 sysarg_t a1, sysarg_t a2, sysarg_t a3, sysarg_t a4, sysarg_t a5)
237{
238 ASSERT(evno >= (int) EVENT_END);
239 ASSERT(evno < EVENT_TASK_END);
240
241 return event_enqueue(evno2event(evno, task), mask, a1, a2, a3, a4, a5);
242}
243
244/** Subscribe event notifications
245 *
246 * @param evno Event type.
247 * @param imethod IPC interface and method to be used for
248 * the notifications.
249 * @param answerbox Answerbox to send the notifications to.
250 *
251 * @return EOK if the subscription was successful.
252 * @return EEXIST if the notifications of the given type are
253 * already subscribed.
254 *
255 */
256static int event_subscribe(event_t *event, sysarg_t imethod,
257 answerbox_t *answerbox)
258{
259 int res;
260
261 spinlock_lock(&event->lock);
262
263 if (event->answerbox == NULL) {
264 event->answerbox = answerbox;
265 event->imethod = imethod;
266 event->counter = 0;
267 event->masked = false;
268 res = EOK;
269 } else
270 res = EEXIST;
271
272 spinlock_unlock(&event->lock);
273
274 return res;
275}
276
277/** Unsubscribe event notifications
278 *
279 * @param evno Event type.
280 * @param answerbox Answerbox used to send the notifications to.
281 *
282 * @return EOK if the subscription was successful.
283 * @return EEXIST if the notifications of the given type are
284 * already subscribed.
285 *
286 */
287static int event_unsubscribe(event_t *event, answerbox_t *answerbox)
288{
289 int res;
290
291 spinlock_lock(&event->lock);
292
293 if (event->answerbox == answerbox) {
294 event->answerbox = NULL;
295 event->counter = 0;
296 event->imethod = 0;
297 event->masked = false;
298 res = EOK;
299 } else
300 res = ENOENT;
301
302 spinlock_unlock(&event->lock);
303
304 return res;
305}
306
307/** Unmask event notifications
308 *
309 * @param evno Event type to unmask.
310 *
311 */
312static void event_unmask(event_t *event)
313{
314 spinlock_lock(&event->lock);
315 event->masked = false;
316 event_callback_t callback = event->unmask_callback;
317 spinlock_unlock(&event->lock);
318
319 /*
320 * Check if there is an unmask callback
321 * function defined for this event.
322 */
323 if (callback != NULL)
324 callback(event);
325}
326
327/** Event notification subscription syscall wrapper
328 *
329 * @param evno Event type to subscribe.
330 * @param imethod IPC interface and method to be used for
331 * the notifications.
332 *
333 * @return EOK on success.
334 * @return ELIMIT on unknown event type.
335 * @return EEXIST if the notifications of the given type are
336 * already subscribed.
337 *
338 */
339sysarg_t sys_ipc_event_subscribe(sysarg_t evno, sysarg_t imethod)
340{
341 if (evno >= EVENT_TASK_END)
342 return ELIMIT;
343
344 return (sysarg_t) event_subscribe(evno2event(evno, TASK),
345 (sysarg_t) imethod, &TASK->answerbox);
346}
347
348/** Event notification unsubscription syscall wrapper
349 *
350 * @param evno Event type to unsubscribe.
351 *
352 * @return EOK on success.
353 * @return ELIMIT on unknown event type.
354 * @return ENOENT if the notification of the given type is not
355 subscribed.
356 *
357 */
358sysarg_t sys_ipc_event_unsubscribe(sysarg_t evno)
359{
360 if (evno >= EVENT_TASK_END)
361 return ELIMIT;
362
363 return (sysarg_t) event_unsubscribe(evno2event(evno, TASK),
364 &TASK->answerbox);
365}
366
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 */
380sysarg_t sys_ipc_event_unmask(sysarg_t evno)
381{
382 if (evno >= EVENT_TASK_END)
383 return ELIMIT;
384
385 event_unmask(evno2event(evno, TASK));
386
387 return EOK;
388}
389
390/** @}
391 */
Note: See TracBrowser for help on using the repository browser.