source: mainline/kernel/generic/src/ipc/event.c@ 5d3ed34

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 5d3ed34 was cfaa35a, checked in by Jakub Jermar <jakub@…>, 13 years ago

Rename call_t's link to ab_link as this link is exclusively used for
linking the call into one of the answerbox lists.

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