source: mainline/kernel/generic/src/ipc/event.c@ 4e1a2f5

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 4e1a2f5 was f9061b4, checked in by Martin Decky <martin@…>, 15 years ago

add kernel event notification masking (currently used only for EVENT_KLOG)
improve kernel event notification API

  • Property mode set to 100644
File size: 6.4 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 <ipc/event_types.h>
39#include <mm/slab.h>
40#include <typedefs.h>
41#include <synch/spinlock.h>
42#include <console/console.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
50/** Initialize kernel events.
51 *
52 */
53void event_init(void)
54{
55 for (unsigned int i = 0; i < EVENT_END; i++) {
56 spinlock_initialize(&events[i].lock, "event.lock");
57 events[i].answerbox = NULL;
58 events[i].counter = 0;
59 events[i].imethod = 0;
60 events[i].masked = false;
61 }
62}
63
64/** Unsubscribe kernel events associated with an answerbox
65 *
66 * @param answerbox Answerbox to be unsubscribed.
67 *
68 */
69void 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/** Send kernel notification event
86 *
87 * @param evno Event type.
88 * @param mask Mask further notifications after a successful
89 * sending.
90 * @param a1 First argument.
91 * @param a2 Second argument.
92 * @param a3 Third argument.
93 * @param a4 Fourth argument.
94 * @param a5 Fifth argument.
95 *
96 * @return EOK if notification was successfully sent.
97 * @return ENOMEM if the notification IPC message failed to allocate.
98 * @return EBUSY if the notifications of the given type are
99 * currently masked.
100 * @return ENOENT if the notifications of the given type are
101 * currently not subscribed.
102 *
103 */
104int event_notify(event_type_t evno, bool mask, sysarg_t a1, sysarg_t a2,
105 sysarg_t a3, sysarg_t a4, sysarg_t a5)
106{
107 ASSERT(evno < EVENT_END);
108
109 spinlock_lock(&events[evno].lock);
110
111 int ret;
112
113 if (events[evno].answerbox != NULL) {
114 if (!events[evno].masked) {
115 call_t *call = ipc_call_alloc(FRAME_ATOMIC);
116
117 if (call) {
118 call->flags |= IPC_CALL_NOTIF;
119 call->priv = ++events[evno].counter;
120
121 IPC_SET_IMETHOD(call->data, events[evno].imethod);
122 IPC_SET_ARG1(call->data, a1);
123 IPC_SET_ARG2(call->data, a2);
124 IPC_SET_ARG3(call->data, a3);
125 IPC_SET_ARG4(call->data, a4);
126 IPC_SET_ARG5(call->data, a5);
127
128 irq_spinlock_lock(&events[evno].answerbox->irq_lock, true);
129 list_append(&call->link, &events[evno].answerbox->irq_notifs);
130 irq_spinlock_unlock(&events[evno].answerbox->irq_lock, true);
131
132 waitq_wakeup(&events[evno].answerbox->wq, WAKEUP_FIRST);
133
134 if (mask)
135 events[evno].masked = true;
136
137 ret = EOK;
138 } else
139 ret = ENOMEM;
140 } else
141 ret = EBUSY;
142 } else
143 ret = ENOENT;
144
145 spinlock_unlock(&events[evno].lock);
146
147 return ret;
148}
149
150/** Subscribe event notifications
151 *
152 * @param evno Event type.
153 * @param imethod IPC interface and method to be used for
154 * the notifications.
155 * @param answerbox Answerbox to send the notifications to.
156 *
157 * @return EOK if the subscription was successful.
158 * @return EEXISTS if the notifications of the given type are
159 * already subscribed.
160 *
161 */
162static int event_subscribe(event_type_t evno, sysarg_t imethod,
163 answerbox_t *answerbox)
164{
165 ASSERT(evno < EVENT_END);
166
167 spinlock_lock(&events[evno].lock);
168
169 int res;
170
171 if (events[evno].answerbox == NULL) {
172 events[evno].answerbox = answerbox;
173 events[evno].imethod = imethod;
174 events[evno].counter = 0;
175 events[evno].masked = false;
176 res = EOK;
177 } else
178 res = EEXISTS;
179
180 spinlock_unlock(&events[evno].lock);
181
182 return res;
183}
184
185/** Unmask event notifications
186 *
187 * @param evno Event type to unmask.
188 *
189 */
190static void event_unmask(event_type_t evno)
191{
192 ASSERT(evno < EVENT_END);
193
194 spinlock_lock(&events[evno].lock);
195 events[evno].masked = false;
196 spinlock_unlock(&events[evno].lock);
197}
198
199/** Event notification syscall wrapper
200 *
201 * @param evno Event type to subscribe.
202 * @param imethod IPC interface and method to be used for
203 * the notifications.
204 *
205 * @return EOK on success.
206 * @return ELIMIT on unknown event type.
207 * @return EEXISTS if the notifications of the given type are
208 * already subscribed.
209 *
210 */
211sysarg_t sys_event_subscribe(sysarg_t evno, sysarg_t imethod)
212{
213 if (evno >= EVENT_END)
214 return ELIMIT;
215
216 return (sysarg_t) event_subscribe((event_type_t) evno, (sysarg_t)
217 imethod, &TASK->answerbox);
218}
219
220/** Event notification unmask syscall wrapper
221 *
222 * Note that currently no tests are performed whether the calling
223 * task is entitled to unmask the notifications. However, thanks
224 * to the fact that notification masking is only a performance
225 * optimization, this has probably no security implications.
226 *
227 * @param evno Event type to unmask.
228 *
229 * @return EOK on success.
230 * @return ELIMIT on unknown event type.
231 *
232 */
233sysarg_t sys_event_unmask(sysarg_t evno)
234{
235 if (evno >= EVENT_END)
236 return ELIMIT;
237
238 event_unmask((event_type_t) evno);
239 return EOK;
240}
241
242/** @}
243 */
Note: See TracBrowser for help on using the repository browser.