-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path0x06_events.c
More file actions
286 lines (243 loc) · 9.21 KB
/
Copy path0x06_events.c
File metadata and controls
286 lines (243 loc) · 9.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include "npu.h"
/*
* events_init - Address: 0xe0dc
*
* Events state structure initialization function.
*/
void events_init() {
/* Initializes the total number of events to 0 */
g_events_state.nb_events = 0;
/* Initializes the list of events */
list_init(&g_events_state.event_list);
/* Initializes the waiting lists and the counts */
for (int i = 0; i <= MAX_EVENT_IDS; ++i) {
list_init(&g_events_state.event_waiting_lists[i]);
g_events_state.standby_cnts[i] = 0;
g_events_state.trigger_cnts[i] = 0;
}
/* Sets the magic value and resets the state for each event */
for (int i = 0; i < MAX_EVENTS; i++) {
g_events_state.events[i].magic = EVENT_MAGIC;
g_events_state.events[i].state = 0;
}
}
/*
* __alloc_event - Address: 0xd618
*
* Creates and returns a new event referenced by `id` in `g_events_state`.
*/
struct event *__alloc_event(u32 id, u32 rule) {
struct event *event = 0;
u32 available_event_id = 0;
if (id >= MAX_EVENT_IDS || rule >= 3)
return 0;
/* Finds an empty event structure in `g_events_state` */
while (g_events_state.events[available_event_id].state & 1) {
if (++available_event_id >= MAX_EVENTS)
abort();
}
event = &g_events_state.events[available_event_id];
if ( event->magic != EVENT_MAGIC )
abort();
u32 standby_cnt = g_events_state.standby_cnts[id];
if (standby_cnt != 0 && standby_cnt != rule)
return 0;
/* Initializes the event's workqueue */
int ret = init_wqueue(event->wq, 0, event);
if (ret)
return 0;
/* Updates `g_events_state` with the new event */
g_events_state.nb_events++;
g_events_state.standby_cnts[id]++;
g_events_state.event_flags[id] = rule;
list_add(&event->event_list_entry, &g_events_state.event_list);
/* Sets the id and state of the event */
event->id = id;
event->state = event->state & ~EVENT_WAITING | EVENT_READY;
return event;
}
/*
* __free_event - Address: 0xd8c4
*
* Creates and returns a new event referenced by `id` in `g_events_state`.
*/
int __free_event(struct event *event) {
int ret = 0;
if (!event || event->magic != EVENT_MAGIC)
abort();
if (!g_events_state.standby_cnts[event->id])
return -22;
ret = cleanup_wqueue(event->wq);
if (ret)
return ret;
ret = deinit_wqueue(event->wq);
if (ret)
return ret;
/* Removes the event from `g_events_state` */
g_events_state.nb_events--;
g_events_state.standby_cnts[event->id]--;
list_del(&event->event_list_entry);
/* Resets the id and state of the event */
event->id = 0;
event->state = event->state & ~EVENT_READY;
return ret;
}
/*
* __set_event_no_schedule - Address: 0xda60
*
* This function goes through the events in the waiting list associated to a
* given event and wakes up all tasks waiting on it.
*/
int __set_event_no_schedule(struct event* event) {
if (!event || event->magic != EVENT_MAGIC || event->id > MAX_EVENT_IDS)
abort();
u32 ret = 0;
u32 id = event->id;
struct list_head* waiting_list = &g_events_state.event_waiting_lists[id];
switch (g_events_state.event_flags[id]) {
case 0:
/* Gets the first element of the waiting list */
struct event* curr_event = container_of(
waiting_list->next, struct event, waiting_list_entry);
/* Gets the second element of the waiting list */
struct event* next_event = container_of(
waiting_list->next->next, struct event, waiting_list_entry);
/* Checks that the waiting list is not empty */
if (!is_list_empty(waiting_list)) {
/*
* This loop goes through all events in the list and wake them
* up. It stops once the current element is the list head.
*/
for (;;) {
/* Tries to wake up the current event */
ret = wakeup_wqueue(&curr_event->wq);
if (ret)
break;
/*
* If the current event was woken up, remove it from the
* waiting list.
*/
list_del(&curr_event->waiting_list_entry);
/* Resets the waiting state */
event->state = event->state & ~EVENT_WAITING;
/*
* Sets the next event as the current one to continue
* the process.
*/
curr_event = next_event;
/* Next element while iterating through the list */
next_event = container_of(
&next_event->waiting_list_entry.next,
struct event, waiting_list_entry);
/* Stops the iteration once we're back to the list head */
if (&curr_event->waiting_list_entry == waiting_list)
return ret;
}
}
break;
case 1:
/* Gets the first element of the waiting list */
struct event* curr_event = container_of(
waiting_list, struct event, waiting_list_entry);
/* Gets the second element of the waiting list */
struct event* next_event = container_of(
waiting_list->next, struct event, waiting_list_entry);
/* Checks that the waiting list is not empty */
if (!is_list_empty(waiting_list)) {
/*
* This loop goes through all events in the list and wake them
* up. It stops once the current element is the list head.
*/
for (;;) {
/* Tries to wake up the current event */
ret = wakeup_wqueue(&curr_event->wq);
if (ret)
break;
/*
* If the current event was woken up, remove it from the
* waiting list.
*/
list_del(&curr_event->waiting_list_entry);
/* Resets the waiting state */
event->state = event->state & ~EVENT_WAITING;
/*
* Sets the next event as the current one to continue
* the process.
*/
next_event = next_event;
/* Next element while iterating through the list */
next_event = container_of(
&next_event->waiting_list_entry.next,
struct event, waiting_list_entry);
/* Stops the iteration once we're back to the list head */
if (&curr_event->waiting_list_entry == waiting_list)
return ret;
}
} else {
/* Increases the trigger count */
g_events_state.trigger_cnts[id]++;
}
break;
case 2:
/* Gets the first element of the waiting list */
struct event* curr_event = container_of(
waiting_list, struct event, waiting_list_entry);
/* Checks that the waiting list is not empty */
if (!is_list_empty(waiting_list)) {
/* Tries to wake up the current event */
ret = wakeup_wqueue(&curr_event->wq);
if (ret)
return ret;
/*
* If the current event was woken up, remove it from the
* waiting list.
*/
list_del(&curr_event->waiting_list_entry);
/* Resets the waiting state */
event->state = event->state & ~EVENT_WAITING;
} else {
/* Increases the trigger count */
g_events_state.trigger_cnts[id]++;
}
break;
default:
return -22;
}
return 0;
}
/*
* __wait_event - Address: 0xdd38
*
* This function queues an event into its associated waiting list and pauses
* the current tasks, waiting for it to be woken back up.
*/
int __wait_event(struct event *event) {
u32 ret = 0;
u32 id = event->id;
if (!event || event->magic != EVENT_MAGIC)
abort();
/* Decreases the trigger count */
if (g_events_state.trigger_cnts[id]) {
g_events_state.trigger_cnts[id]--;
return 0;
}
/* Removes the current task from the ready list... */
ret = __del_from_ready_list(g_current_task);
if (ret)
return ret;
/* ... and adds it to the pending list of this event */
ret = __add_to_pending_list(g_current_task, &event->wq);
if (ret)
return ret;
/* Checks that the event is not EVENT_WAITING */
u32 state = (event->state >> 1) & 1;
if (state)
return ret;
/* Adds the event to the corresponding waiting list */
list_add(&event->waiting_list_entry,
&g_events_state.event_waiting_lists[id]);
event->state = event->state | EVENT_WAITING;
/* Schedlues a new task */
schedule();
return ret;
}