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. */
|
---|
48 | static event_t events[EVENT_END];
|
---|
49 |
|
---|
50 | /** Initialize kernel events.
|
---|
51 | *
|
---|
52 | */
|
---|
53 | void 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 | events[i].unmask_cb = NULL;
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | /** Unsubscribe kernel events associated with an answerbox
|
---|
66 | *
|
---|
67 | * @param answerbox Answerbox to be unsubscribed.
|
---|
68 | *
|
---|
69 | */
|
---|
70 | void event_cleanup_answerbox(answerbox_t *answerbox)
|
---|
71 | {
|
---|
72 | for (unsigned int i = 0; i < EVENT_END; i++) {
|
---|
73 | spinlock_lock(&events[i].lock);
|
---|
74 |
|
---|
75 | if (events[i].answerbox == answerbox) {
|
---|
76 | events[i].answerbox = NULL;
|
---|
77 | events[i].counter = 0;
|
---|
78 | events[i].imethod = 0;
|
---|
79 | events[i].masked = false;
|
---|
80 | }
|
---|
81 |
|
---|
82 | spinlock_unlock(&events[i].lock);
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | /** Define a callback function for the event unmask event.
|
---|
87 | *
|
---|
88 | * @param evno Event type.
|
---|
89 | * @param cb Callback function to be called when the event is unmasked.
|
---|
90 | *
|
---|
91 | */
|
---|
92 | void event_set_unmask_callback(event_type_t evno, void (*cb)(void))
|
---|
93 | {
|
---|
94 | ASSERT(evno < EVENT_END);
|
---|
95 |
|
---|
96 | spinlock_lock(&events[evno].lock);
|
---|
97 | events[evno].unmask_cb = cb;
|
---|
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 | void (*cb)(void);
|
---|
209 | ASSERT(evno < EVENT_END);
|
---|
210 |
|
---|
211 | spinlock_lock(&events[evno].lock);
|
---|
212 | events[evno].masked = false;
|
---|
213 | cb = events[evno].unmask_cb;
|
---|
214 | spinlock_unlock(&events[evno].lock);
|
---|
215 |
|
---|
216 | /*
|
---|
217 | * Check if there is an unmask callback function defined for this event.
|
---|
218 | */
|
---|
219 | if (cb)
|
---|
220 | cb();
|
---|
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 | */
|
---|