mkregtable.c revision 1.2 1 1.2 riastrad /* $NetBSD: mkregtable.c,v 1.2 2018/08/27 04:58:36 riastradh Exp $ */
2 1.2 riastrad
3 1.1 riastrad /* utility to create the register check tables
4 1.1 riastrad * this includes inlined list.h safe for userspace.
5 1.1 riastrad *
6 1.1 riastrad * Copyright 2009 Jerome Glisse
7 1.1 riastrad * Copyright 2009 Red Hat Inc.
8 1.1 riastrad *
9 1.1 riastrad * Authors:
10 1.1 riastrad * Jerome Glisse
11 1.1 riastrad * Dave Airlie
12 1.1 riastrad */
13 1.1 riastrad
14 1.2 riastrad #include <sys/cdefs.h>
15 1.2 riastrad __KERNEL_RCSID(0, "$NetBSD: mkregtable.c,v 1.2 2018/08/27 04:58:36 riastradh Exp $");
16 1.2 riastrad
17 1.1 riastrad #include <sys/types.h>
18 1.1 riastrad #include <stdlib.h>
19 1.1 riastrad #include <string.h>
20 1.1 riastrad #include <stdio.h>
21 1.1 riastrad #include <regex.h>
22 1.1 riastrad #include <libgen.h>
23 1.1 riastrad
24 1.1 riastrad #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
25 1.1 riastrad /**
26 1.1 riastrad * container_of - cast a member of a structure out to the containing structure
27 1.1 riastrad * @ptr: the pointer to the member.
28 1.1 riastrad * @type: the type of the container struct this is embedded in.
29 1.1 riastrad * @member: the name of the member within the struct.
30 1.1 riastrad *
31 1.1 riastrad */
32 1.1 riastrad #define container_of(ptr, type, member) ({ \
33 1.1 riastrad const typeof(((type *)0)->member)*__mptr = (ptr); \
34 1.1 riastrad (type *)((char *)__mptr - offsetof(type, member)); })
35 1.1 riastrad
36 1.1 riastrad /*
37 1.1 riastrad * Simple doubly linked list implementation.
38 1.1 riastrad *
39 1.1 riastrad * Some of the internal functions ("__xxx") are useful when
40 1.1 riastrad * manipulating whole lists rather than single entries, as
41 1.1 riastrad * sometimes we already know the next/prev entries and we can
42 1.1 riastrad * generate better code by using them directly rather than
43 1.1 riastrad * using the generic single-entry routines.
44 1.1 riastrad */
45 1.1 riastrad
46 1.1 riastrad struct list_head {
47 1.1 riastrad struct list_head *next, *prev;
48 1.1 riastrad };
49 1.1 riastrad
50 1.1 riastrad #define LIST_HEAD_INIT(name) { &(name), &(name) }
51 1.1 riastrad
52 1.1 riastrad #define LIST_HEAD(name) \
53 1.1 riastrad struct list_head name = LIST_HEAD_INIT(name)
54 1.1 riastrad
55 1.1 riastrad static inline void INIT_LIST_HEAD(struct list_head *list)
56 1.1 riastrad {
57 1.1 riastrad list->next = list;
58 1.1 riastrad list->prev = list;
59 1.1 riastrad }
60 1.1 riastrad
61 1.1 riastrad /*
62 1.1 riastrad * Insert a new entry between two known consecutive entries.
63 1.1 riastrad *
64 1.1 riastrad * This is only for internal list manipulation where we know
65 1.1 riastrad * the prev/next entries already!
66 1.1 riastrad */
67 1.1 riastrad #ifndef CONFIG_DEBUG_LIST
68 1.1 riastrad static inline void __list_add(struct list_head *new,
69 1.1 riastrad struct list_head *prev, struct list_head *next)
70 1.1 riastrad {
71 1.1 riastrad next->prev = new;
72 1.1 riastrad new->next = next;
73 1.1 riastrad new->prev = prev;
74 1.1 riastrad prev->next = new;
75 1.1 riastrad }
76 1.1 riastrad #else
77 1.1 riastrad extern void __list_add(struct list_head *new,
78 1.1 riastrad struct list_head *prev, struct list_head *next);
79 1.1 riastrad #endif
80 1.1 riastrad
81 1.1 riastrad /**
82 1.1 riastrad * list_add - add a new entry
83 1.1 riastrad * @new: new entry to be added
84 1.1 riastrad * @head: list head to add it after
85 1.1 riastrad *
86 1.1 riastrad * Insert a new entry after the specified head.
87 1.1 riastrad * This is good for implementing stacks.
88 1.1 riastrad */
89 1.1 riastrad static inline void list_add(struct list_head *new, struct list_head *head)
90 1.1 riastrad {
91 1.1 riastrad __list_add(new, head, head->next);
92 1.1 riastrad }
93 1.1 riastrad
94 1.1 riastrad /**
95 1.1 riastrad * list_add_tail - add a new entry
96 1.1 riastrad * @new: new entry to be added
97 1.1 riastrad * @head: list head to add it before
98 1.1 riastrad *
99 1.1 riastrad * Insert a new entry before the specified head.
100 1.1 riastrad * This is useful for implementing queues.
101 1.1 riastrad */
102 1.1 riastrad static inline void list_add_tail(struct list_head *new, struct list_head *head)
103 1.1 riastrad {
104 1.1 riastrad __list_add(new, head->prev, head);
105 1.1 riastrad }
106 1.1 riastrad
107 1.1 riastrad /*
108 1.1 riastrad * Delete a list entry by making the prev/next entries
109 1.1 riastrad * point to each other.
110 1.1 riastrad *
111 1.1 riastrad * This is only for internal list manipulation where we know
112 1.1 riastrad * the prev/next entries already!
113 1.1 riastrad */
114 1.1 riastrad static inline void __list_del(struct list_head *prev, struct list_head *next)
115 1.1 riastrad {
116 1.1 riastrad next->prev = prev;
117 1.1 riastrad prev->next = next;
118 1.1 riastrad }
119 1.1 riastrad
120 1.1 riastrad /**
121 1.1 riastrad * list_del - deletes entry from list.
122 1.1 riastrad * @entry: the element to delete from the list.
123 1.1 riastrad * Note: list_empty() on entry does not return true after this, the entry is
124 1.1 riastrad * in an undefined state.
125 1.1 riastrad */
126 1.1 riastrad #ifndef CONFIG_DEBUG_LIST
127 1.1 riastrad static inline void list_del(struct list_head *entry)
128 1.1 riastrad {
129 1.1 riastrad __list_del(entry->prev, entry->next);
130 1.1 riastrad entry->next = (void *)0xDEADBEEF;
131 1.1 riastrad entry->prev = (void *)0xBEEFDEAD;
132 1.1 riastrad }
133 1.1 riastrad #else
134 1.1 riastrad extern void list_del(struct list_head *entry);
135 1.1 riastrad #endif
136 1.1 riastrad
137 1.1 riastrad /**
138 1.1 riastrad * list_replace - replace old entry by new one
139 1.1 riastrad * @old : the element to be replaced
140 1.1 riastrad * @new : the new element to insert
141 1.1 riastrad *
142 1.1 riastrad * If @old was empty, it will be overwritten.
143 1.1 riastrad */
144 1.1 riastrad static inline void list_replace(struct list_head *old, struct list_head *new)
145 1.1 riastrad {
146 1.1 riastrad new->next = old->next;
147 1.1 riastrad new->next->prev = new;
148 1.1 riastrad new->prev = old->prev;
149 1.1 riastrad new->prev->next = new;
150 1.1 riastrad }
151 1.1 riastrad
152 1.1 riastrad static inline void list_replace_init(struct list_head *old,
153 1.1 riastrad struct list_head *new)
154 1.1 riastrad {
155 1.1 riastrad list_replace(old, new);
156 1.1 riastrad INIT_LIST_HEAD(old);
157 1.1 riastrad }
158 1.1 riastrad
159 1.1 riastrad /**
160 1.1 riastrad * list_del_init - deletes entry from list and reinitialize it.
161 1.1 riastrad * @entry: the element to delete from the list.
162 1.1 riastrad */
163 1.1 riastrad static inline void list_del_init(struct list_head *entry)
164 1.1 riastrad {
165 1.1 riastrad __list_del(entry->prev, entry->next);
166 1.1 riastrad INIT_LIST_HEAD(entry);
167 1.1 riastrad }
168 1.1 riastrad
169 1.1 riastrad /**
170 1.1 riastrad * list_move - delete from one list and add as another's head
171 1.1 riastrad * @list: the entry to move
172 1.1 riastrad * @head: the head that will precede our entry
173 1.1 riastrad */
174 1.1 riastrad static inline void list_move(struct list_head *list, struct list_head *head)
175 1.1 riastrad {
176 1.1 riastrad __list_del(list->prev, list->next);
177 1.1 riastrad list_add(list, head);
178 1.1 riastrad }
179 1.1 riastrad
180 1.1 riastrad /**
181 1.1 riastrad * list_move_tail - delete from one list and add as another's tail
182 1.1 riastrad * @list: the entry to move
183 1.1 riastrad * @head: the head that will follow our entry
184 1.1 riastrad */
185 1.1 riastrad static inline void list_move_tail(struct list_head *list,
186 1.1 riastrad struct list_head *head)
187 1.1 riastrad {
188 1.1 riastrad __list_del(list->prev, list->next);
189 1.1 riastrad list_add_tail(list, head);
190 1.1 riastrad }
191 1.1 riastrad
192 1.1 riastrad /**
193 1.1 riastrad * list_is_last - tests whether @list is the last entry in list @head
194 1.1 riastrad * @list: the entry to test
195 1.1 riastrad * @head: the head of the list
196 1.1 riastrad */
197 1.1 riastrad static inline int list_is_last(const struct list_head *list,
198 1.1 riastrad const struct list_head *head)
199 1.1 riastrad {
200 1.1 riastrad return list->next == head;
201 1.1 riastrad }
202 1.1 riastrad
203 1.1 riastrad /**
204 1.1 riastrad * list_empty - tests whether a list is empty
205 1.1 riastrad * @head: the list to test.
206 1.1 riastrad */
207 1.1 riastrad static inline int list_empty(const struct list_head *head)
208 1.1 riastrad {
209 1.1 riastrad return head->next == head;
210 1.1 riastrad }
211 1.1 riastrad
212 1.1 riastrad /**
213 1.1 riastrad * list_empty_careful - tests whether a list is empty and not being modified
214 1.1 riastrad * @head: the list to test
215 1.1 riastrad *
216 1.1 riastrad * Description:
217 1.1 riastrad * tests whether a list is empty _and_ checks that no other CPU might be
218 1.1 riastrad * in the process of modifying either member (next or prev)
219 1.1 riastrad *
220 1.1 riastrad * NOTE: using list_empty_careful() without synchronization
221 1.1 riastrad * can only be safe if the only activity that can happen
222 1.1 riastrad * to the list entry is list_del_init(). Eg. it cannot be used
223 1.1 riastrad * if another CPU could re-list_add() it.
224 1.1 riastrad */
225 1.1 riastrad static inline int list_empty_careful(const struct list_head *head)
226 1.1 riastrad {
227 1.1 riastrad struct list_head *next = head->next;
228 1.1 riastrad return (next == head) && (next == head->prev);
229 1.1 riastrad }
230 1.1 riastrad
231 1.1 riastrad /**
232 1.1 riastrad * list_is_singular - tests whether a list has just one entry.
233 1.1 riastrad * @head: the list to test.
234 1.1 riastrad */
235 1.1 riastrad static inline int list_is_singular(const struct list_head *head)
236 1.1 riastrad {
237 1.1 riastrad return !list_empty(head) && (head->next == head->prev);
238 1.1 riastrad }
239 1.1 riastrad
240 1.1 riastrad static inline void __list_cut_position(struct list_head *list,
241 1.1 riastrad struct list_head *head,
242 1.1 riastrad struct list_head *entry)
243 1.1 riastrad {
244 1.1 riastrad struct list_head *new_first = entry->next;
245 1.1 riastrad list->next = head->next;
246 1.1 riastrad list->next->prev = list;
247 1.1 riastrad list->prev = entry;
248 1.1 riastrad entry->next = list;
249 1.1 riastrad head->next = new_first;
250 1.1 riastrad new_first->prev = head;
251 1.1 riastrad }
252 1.1 riastrad
253 1.1 riastrad /**
254 1.1 riastrad * list_cut_position - cut a list into two
255 1.1 riastrad * @list: a new list to add all removed entries
256 1.1 riastrad * @head: a list with entries
257 1.1 riastrad * @entry: an entry within head, could be the head itself
258 1.1 riastrad * and if so we won't cut the list
259 1.1 riastrad *
260 1.1 riastrad * This helper moves the initial part of @head, up to and
261 1.1 riastrad * including @entry, from @head to @list. You should
262 1.1 riastrad * pass on @entry an element you know is on @head. @list
263 1.1 riastrad * should be an empty list or a list you do not care about
264 1.1 riastrad * losing its data.
265 1.1 riastrad *
266 1.1 riastrad */
267 1.1 riastrad static inline void list_cut_position(struct list_head *list,
268 1.1 riastrad struct list_head *head,
269 1.1 riastrad struct list_head *entry)
270 1.1 riastrad {
271 1.1 riastrad if (list_empty(head))
272 1.1 riastrad return;
273 1.1 riastrad if (list_is_singular(head) && (head->next != entry && head != entry))
274 1.1 riastrad return;
275 1.1 riastrad if (entry == head)
276 1.1 riastrad INIT_LIST_HEAD(list);
277 1.1 riastrad else
278 1.1 riastrad __list_cut_position(list, head, entry);
279 1.1 riastrad }
280 1.1 riastrad
281 1.1 riastrad static inline void __list_splice(const struct list_head *list,
282 1.1 riastrad struct list_head *prev, struct list_head *next)
283 1.1 riastrad {
284 1.1 riastrad struct list_head *first = list->next;
285 1.1 riastrad struct list_head *last = list->prev;
286 1.1 riastrad
287 1.1 riastrad first->prev = prev;
288 1.1 riastrad prev->next = first;
289 1.1 riastrad
290 1.1 riastrad last->next = next;
291 1.1 riastrad next->prev = last;
292 1.1 riastrad }
293 1.1 riastrad
294 1.1 riastrad /**
295 1.1 riastrad * list_splice - join two lists, this is designed for stacks
296 1.1 riastrad * @list: the new list to add.
297 1.1 riastrad * @head: the place to add it in the first list.
298 1.1 riastrad */
299 1.1 riastrad static inline void list_splice(const struct list_head *list,
300 1.1 riastrad struct list_head *head)
301 1.1 riastrad {
302 1.1 riastrad if (!list_empty(list))
303 1.1 riastrad __list_splice(list, head, head->next);
304 1.1 riastrad }
305 1.1 riastrad
306 1.1 riastrad /**
307 1.1 riastrad * list_splice_tail - join two lists, each list being a queue
308 1.1 riastrad * @list: the new list to add.
309 1.1 riastrad * @head: the place to add it in the first list.
310 1.1 riastrad */
311 1.1 riastrad static inline void list_splice_tail(struct list_head *list,
312 1.1 riastrad struct list_head *head)
313 1.1 riastrad {
314 1.1 riastrad if (!list_empty(list))
315 1.1 riastrad __list_splice(list, head->prev, head);
316 1.1 riastrad }
317 1.1 riastrad
318 1.1 riastrad /**
319 1.1 riastrad * list_splice_init - join two lists and reinitialise the emptied list.
320 1.1 riastrad * @list: the new list to add.
321 1.1 riastrad * @head: the place to add it in the first list.
322 1.1 riastrad *
323 1.1 riastrad * The list at @list is reinitialised
324 1.1 riastrad */
325 1.1 riastrad static inline void list_splice_init(struct list_head *list,
326 1.1 riastrad struct list_head *head)
327 1.1 riastrad {
328 1.1 riastrad if (!list_empty(list)) {
329 1.1 riastrad __list_splice(list, head, head->next);
330 1.1 riastrad INIT_LIST_HEAD(list);
331 1.1 riastrad }
332 1.1 riastrad }
333 1.1 riastrad
334 1.1 riastrad /**
335 1.1 riastrad * list_splice_tail_init - join two lists and reinitialise the emptied list
336 1.1 riastrad * @list: the new list to add.
337 1.1 riastrad * @head: the place to add it in the first list.
338 1.1 riastrad *
339 1.1 riastrad * Each of the lists is a queue.
340 1.1 riastrad * The list at @list is reinitialised
341 1.1 riastrad */
342 1.1 riastrad static inline void list_splice_tail_init(struct list_head *list,
343 1.1 riastrad struct list_head *head)
344 1.1 riastrad {
345 1.1 riastrad if (!list_empty(list)) {
346 1.1 riastrad __list_splice(list, head->prev, head);
347 1.1 riastrad INIT_LIST_HEAD(list);
348 1.1 riastrad }
349 1.1 riastrad }
350 1.1 riastrad
351 1.1 riastrad /**
352 1.1 riastrad * list_entry - get the struct for this entry
353 1.1 riastrad * @ptr: the &struct list_head pointer.
354 1.1 riastrad * @type: the type of the struct this is embedded in.
355 1.2 riastrad * @member: the name of the list_head within the struct.
356 1.1 riastrad */
357 1.1 riastrad #define list_entry(ptr, type, member) \
358 1.1 riastrad container_of(ptr, type, member)
359 1.1 riastrad
360 1.1 riastrad /**
361 1.1 riastrad * list_first_entry - get the first element from a list
362 1.1 riastrad * @ptr: the list head to take the element from.
363 1.1 riastrad * @type: the type of the struct this is embedded in.
364 1.2 riastrad * @member: the name of the list_head within the struct.
365 1.1 riastrad *
366 1.1 riastrad * Note, that list is expected to be not empty.
367 1.1 riastrad */
368 1.1 riastrad #define list_first_entry(ptr, type, member) \
369 1.1 riastrad list_entry((ptr)->next, type, member)
370 1.1 riastrad
371 1.1 riastrad /**
372 1.1 riastrad * list_for_each - iterate over a list
373 1.1 riastrad * @pos: the &struct list_head to use as a loop cursor.
374 1.1 riastrad * @head: the head for your list.
375 1.1 riastrad */
376 1.1 riastrad #define list_for_each(pos, head) \
377 1.1 riastrad for (pos = (head)->next; prefetch(pos->next), pos != (head); \
378 1.1 riastrad pos = pos->next)
379 1.1 riastrad
380 1.1 riastrad /**
381 1.1 riastrad * list_for_each_prev - iterate over a list backwards
382 1.1 riastrad * @pos: the &struct list_head to use as a loop cursor.
383 1.1 riastrad * @head: the head for your list.
384 1.1 riastrad */
385 1.1 riastrad #define list_for_each_prev(pos, head) \
386 1.1 riastrad for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
387 1.1 riastrad pos = pos->prev)
388 1.1 riastrad
389 1.1 riastrad /**
390 1.1 riastrad * list_for_each_safe - iterate over a list safe against removal of list entry
391 1.1 riastrad * @pos: the &struct list_head to use as a loop cursor.
392 1.1 riastrad * @n: another &struct list_head to use as temporary storage
393 1.1 riastrad * @head: the head for your list.
394 1.1 riastrad */
395 1.1 riastrad #define list_for_each_safe(pos, n, head) \
396 1.1 riastrad for (pos = (head)->next, n = pos->next; pos != (head); \
397 1.1 riastrad pos = n, n = pos->next)
398 1.1 riastrad
399 1.1 riastrad /**
400 1.1 riastrad * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
401 1.1 riastrad * @pos: the &struct list_head to use as a loop cursor.
402 1.1 riastrad * @n: another &struct list_head to use as temporary storage
403 1.1 riastrad * @head: the head for your list.
404 1.1 riastrad */
405 1.1 riastrad #define list_for_each_prev_safe(pos, n, head) \
406 1.1 riastrad for (pos = (head)->prev, n = pos->prev; \
407 1.1 riastrad prefetch(pos->prev), pos != (head); \
408 1.1 riastrad pos = n, n = pos->prev)
409 1.1 riastrad
410 1.1 riastrad /**
411 1.1 riastrad * list_for_each_entry - iterate over list of given type
412 1.1 riastrad * @pos: the type * to use as a loop cursor.
413 1.1 riastrad * @head: the head for your list.
414 1.2 riastrad * @member: the name of the list_head within the struct.
415 1.1 riastrad */
416 1.1 riastrad #define list_for_each_entry(pos, head, member) \
417 1.1 riastrad for (pos = list_entry((head)->next, typeof(*pos), member); \
418 1.1 riastrad &pos->member != (head); \
419 1.1 riastrad pos = list_entry(pos->member.next, typeof(*pos), member))
420 1.1 riastrad
421 1.1 riastrad /**
422 1.1 riastrad * list_for_each_entry_reverse - iterate backwards over list of given type.
423 1.1 riastrad * @pos: the type * to use as a loop cursor.
424 1.1 riastrad * @head: the head for your list.
425 1.2 riastrad * @member: the name of the list_head within the struct.
426 1.1 riastrad */
427 1.1 riastrad #define list_for_each_entry_reverse(pos, head, member) \
428 1.1 riastrad for (pos = list_entry((head)->prev, typeof(*pos), member); \
429 1.1 riastrad prefetch(pos->member.prev), &pos->member != (head); \
430 1.1 riastrad pos = list_entry(pos->member.prev, typeof(*pos), member))
431 1.1 riastrad
432 1.1 riastrad /**
433 1.1 riastrad * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
434 1.1 riastrad * @pos: the type * to use as a start point
435 1.1 riastrad * @head: the head of the list
436 1.2 riastrad * @member: the name of the list_head within the struct.
437 1.1 riastrad *
438 1.1 riastrad * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
439 1.1 riastrad */
440 1.1 riastrad #define list_prepare_entry(pos, head, member) \
441 1.1 riastrad ((pos) ? : list_entry(head, typeof(*pos), member))
442 1.1 riastrad
443 1.1 riastrad /**
444 1.1 riastrad * list_for_each_entry_continue - continue iteration over list of given type
445 1.1 riastrad * @pos: the type * to use as a loop cursor.
446 1.1 riastrad * @head: the head for your list.
447 1.2 riastrad * @member: the name of the list_head within the struct.
448 1.1 riastrad *
449 1.1 riastrad * Continue to iterate over list of given type, continuing after
450 1.1 riastrad * the current position.
451 1.1 riastrad */
452 1.1 riastrad #define list_for_each_entry_continue(pos, head, member) \
453 1.1 riastrad for (pos = list_entry(pos->member.next, typeof(*pos), member); \
454 1.1 riastrad prefetch(pos->member.next), &pos->member != (head); \
455 1.1 riastrad pos = list_entry(pos->member.next, typeof(*pos), member))
456 1.1 riastrad
457 1.1 riastrad /**
458 1.1 riastrad * list_for_each_entry_continue_reverse - iterate backwards from the given point
459 1.1 riastrad * @pos: the type * to use as a loop cursor.
460 1.1 riastrad * @head: the head for your list.
461 1.2 riastrad * @member: the name of the list_head within the struct.
462 1.1 riastrad *
463 1.1 riastrad * Start to iterate over list of given type backwards, continuing after
464 1.1 riastrad * the current position.
465 1.1 riastrad */
466 1.1 riastrad #define list_for_each_entry_continue_reverse(pos, head, member) \
467 1.1 riastrad for (pos = list_entry(pos->member.prev, typeof(*pos), member); \
468 1.1 riastrad prefetch(pos->member.prev), &pos->member != (head); \
469 1.1 riastrad pos = list_entry(pos->member.prev, typeof(*pos), member))
470 1.1 riastrad
471 1.1 riastrad /**
472 1.1 riastrad * list_for_each_entry_from - iterate over list of given type from the current point
473 1.1 riastrad * @pos: the type * to use as a loop cursor.
474 1.1 riastrad * @head: the head for your list.
475 1.2 riastrad * @member: the name of the list_head within the struct.
476 1.1 riastrad *
477 1.1 riastrad * Iterate over list of given type, continuing from current position.
478 1.1 riastrad */
479 1.1 riastrad #define list_for_each_entry_from(pos, head, member) \
480 1.1 riastrad for (; prefetch(pos->member.next), &pos->member != (head); \
481 1.1 riastrad pos = list_entry(pos->member.next, typeof(*pos), member))
482 1.1 riastrad
483 1.1 riastrad /**
484 1.1 riastrad * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
485 1.1 riastrad * @pos: the type * to use as a loop cursor.
486 1.1 riastrad * @n: another type * to use as temporary storage
487 1.1 riastrad * @head: the head for your list.
488 1.2 riastrad * @member: the name of the list_head within the struct.
489 1.1 riastrad */
490 1.1 riastrad #define list_for_each_entry_safe(pos, n, head, member) \
491 1.1 riastrad for (pos = list_entry((head)->next, typeof(*pos), member), \
492 1.1 riastrad n = list_entry(pos->member.next, typeof(*pos), member); \
493 1.1 riastrad &pos->member != (head); \
494 1.1 riastrad pos = n, n = list_entry(n->member.next, typeof(*n), member))
495 1.1 riastrad
496 1.1 riastrad /**
497 1.1 riastrad * list_for_each_entry_safe_continue
498 1.1 riastrad * @pos: the type * to use as a loop cursor.
499 1.1 riastrad * @n: another type * to use as temporary storage
500 1.1 riastrad * @head: the head for your list.
501 1.2 riastrad * @member: the name of the list_head within the struct.
502 1.1 riastrad *
503 1.1 riastrad * Iterate over list of given type, continuing after current point,
504 1.1 riastrad * safe against removal of list entry.
505 1.1 riastrad */
506 1.1 riastrad #define list_for_each_entry_safe_continue(pos, n, head, member) \
507 1.1 riastrad for (pos = list_entry(pos->member.next, typeof(*pos), member), \
508 1.1 riastrad n = list_entry(pos->member.next, typeof(*pos), member); \
509 1.1 riastrad &pos->member != (head); \
510 1.1 riastrad pos = n, n = list_entry(n->member.next, typeof(*n), member))
511 1.1 riastrad
512 1.1 riastrad /**
513 1.1 riastrad * list_for_each_entry_safe_from
514 1.1 riastrad * @pos: the type * to use as a loop cursor.
515 1.1 riastrad * @n: another type * to use as temporary storage
516 1.1 riastrad * @head: the head for your list.
517 1.2 riastrad * @member: the name of the list_head within the struct.
518 1.1 riastrad *
519 1.1 riastrad * Iterate over list of given type from current point, safe against
520 1.1 riastrad * removal of list entry.
521 1.1 riastrad */
522 1.1 riastrad #define list_for_each_entry_safe_from(pos, n, head, member) \
523 1.1 riastrad for (n = list_entry(pos->member.next, typeof(*pos), member); \
524 1.1 riastrad &pos->member != (head); \
525 1.1 riastrad pos = n, n = list_entry(n->member.next, typeof(*n), member))
526 1.1 riastrad
527 1.1 riastrad /**
528 1.1 riastrad * list_for_each_entry_safe_reverse
529 1.1 riastrad * @pos: the type * to use as a loop cursor.
530 1.1 riastrad * @n: another type * to use as temporary storage
531 1.1 riastrad * @head: the head for your list.
532 1.2 riastrad * @member: the name of the list_head within the struct.
533 1.1 riastrad *
534 1.1 riastrad * Iterate backwards over list of given type, safe against removal
535 1.1 riastrad * of list entry.
536 1.1 riastrad */
537 1.1 riastrad #define list_for_each_entry_safe_reverse(pos, n, head, member) \
538 1.1 riastrad for (pos = list_entry((head)->prev, typeof(*pos), member), \
539 1.1 riastrad n = list_entry(pos->member.prev, typeof(*pos), member); \
540 1.1 riastrad &pos->member != (head); \
541 1.1 riastrad pos = n, n = list_entry(n->member.prev, typeof(*n), member))
542 1.1 riastrad
543 1.1 riastrad struct offset {
544 1.1 riastrad struct list_head list;
545 1.1 riastrad unsigned offset;
546 1.1 riastrad };
547 1.1 riastrad
548 1.1 riastrad struct table {
549 1.1 riastrad struct list_head offsets;
550 1.1 riastrad unsigned offset_max;
551 1.1 riastrad unsigned nentry;
552 1.1 riastrad unsigned *table;
553 1.1 riastrad char *gpu_prefix;
554 1.1 riastrad };
555 1.1 riastrad
556 1.1 riastrad static struct offset *offset_new(unsigned o)
557 1.1 riastrad {
558 1.1 riastrad struct offset *offset;
559 1.1 riastrad
560 1.1 riastrad offset = (struct offset *)malloc(sizeof(struct offset));
561 1.1 riastrad if (offset) {
562 1.1 riastrad INIT_LIST_HEAD(&offset->list);
563 1.1 riastrad offset->offset = o;
564 1.1 riastrad }
565 1.1 riastrad return offset;
566 1.1 riastrad }
567 1.1 riastrad
568 1.1 riastrad static void table_offset_add(struct table *t, struct offset *offset)
569 1.1 riastrad {
570 1.1 riastrad list_add_tail(&offset->list, &t->offsets);
571 1.1 riastrad }
572 1.1 riastrad
573 1.1 riastrad static void table_init(struct table *t)
574 1.1 riastrad {
575 1.1 riastrad INIT_LIST_HEAD(&t->offsets);
576 1.1 riastrad t->offset_max = 0;
577 1.1 riastrad t->nentry = 0;
578 1.1 riastrad t->table = NULL;
579 1.1 riastrad }
580 1.1 riastrad
581 1.1 riastrad static void table_print(struct table *t)
582 1.1 riastrad {
583 1.1 riastrad unsigned nlloop, i, j, n, c, id;
584 1.1 riastrad
585 1.1 riastrad nlloop = (t->nentry + 3) / 4;
586 1.1 riastrad c = t->nentry;
587 1.1 riastrad printf("static const unsigned %s_reg_safe_bm[%d] = {\n", t->gpu_prefix,
588 1.1 riastrad t->nentry);
589 1.1 riastrad for (i = 0, id = 0; i < nlloop; i++) {
590 1.1 riastrad n = 4;
591 1.1 riastrad if (n > c)
592 1.1 riastrad n = c;
593 1.1 riastrad c -= n;
594 1.1 riastrad for (j = 0; j < n; j++) {
595 1.1 riastrad if (j == 0)
596 1.1 riastrad printf("\t");
597 1.1 riastrad else
598 1.1 riastrad printf(" ");
599 1.1 riastrad printf("0x%08X,", t->table[id++]);
600 1.1 riastrad }
601 1.1 riastrad printf("\n");
602 1.1 riastrad }
603 1.1 riastrad printf("};\n");
604 1.1 riastrad }
605 1.1 riastrad
606 1.1 riastrad static int table_build(struct table *t)
607 1.1 riastrad {
608 1.1 riastrad struct offset *offset;
609 1.1 riastrad unsigned i, m;
610 1.1 riastrad
611 1.1 riastrad t->nentry = ((t->offset_max >> 2) + 31) / 32;
612 1.1 riastrad t->table = (unsigned *)malloc(sizeof(unsigned) * t->nentry);
613 1.1 riastrad if (t->table == NULL)
614 1.1 riastrad return -1;
615 1.1 riastrad memset(t->table, 0xff, sizeof(unsigned) * t->nentry);
616 1.1 riastrad list_for_each_entry(offset, &t->offsets, list) {
617 1.1 riastrad i = (offset->offset >> 2) / 32;
618 1.1 riastrad m = (offset->offset >> 2) & 31;
619 1.1 riastrad m = 1 << m;
620 1.1 riastrad t->table[i] ^= m;
621 1.1 riastrad }
622 1.1 riastrad return 0;
623 1.1 riastrad }
624 1.1 riastrad
625 1.1 riastrad static char gpu_name[10];
626 1.1 riastrad static int parser_auth(struct table *t, const char *filename)
627 1.1 riastrad {
628 1.1 riastrad FILE *file;
629 1.1 riastrad regex_t mask_rex;
630 1.1 riastrad regmatch_t match[4];
631 1.1 riastrad char buf[1024];
632 1.1 riastrad size_t end;
633 1.1 riastrad int len;
634 1.1 riastrad int done = 0;
635 1.1 riastrad int r;
636 1.1 riastrad unsigned o;
637 1.1 riastrad struct offset *offset;
638 1.1 riastrad char last_reg_s[10];
639 1.1 riastrad int last_reg;
640 1.1 riastrad
641 1.1 riastrad if (regcomp
642 1.1 riastrad (&mask_rex, "(0x[0-9a-fA-F]*) *([_a-zA-Z0-9]*)", REG_EXTENDED)) {
643 1.1 riastrad fprintf(stderr, "Failed to compile regular expression\n");
644 1.1 riastrad return -1;
645 1.1 riastrad }
646 1.1 riastrad file = fopen(filename, "r");
647 1.1 riastrad if (file == NULL) {
648 1.1 riastrad fprintf(stderr, "Failed to open: %s\n", filename);
649 1.1 riastrad return -1;
650 1.1 riastrad }
651 1.1 riastrad fseek(file, 0, SEEK_END);
652 1.1 riastrad end = ftell(file);
653 1.1 riastrad fseek(file, 0, SEEK_SET);
654 1.1 riastrad
655 1.1 riastrad /* get header */
656 1.1 riastrad if (fgets(buf, 1024, file) == NULL) {
657 1.1 riastrad fclose(file);
658 1.1 riastrad return -1;
659 1.1 riastrad }
660 1.1 riastrad
661 1.1 riastrad /* first line will contain the last register
662 1.1 riastrad * and gpu name */
663 1.1 riastrad sscanf(buf, "%9s %9s", gpu_name, last_reg_s);
664 1.1 riastrad t->gpu_prefix = gpu_name;
665 1.1 riastrad last_reg = strtol(last_reg_s, NULL, 16);
666 1.1 riastrad
667 1.1 riastrad do {
668 1.1 riastrad if (fgets(buf, 1024, file) == NULL) {
669 1.1 riastrad fclose(file);
670 1.1 riastrad return -1;
671 1.1 riastrad }
672 1.1 riastrad len = strlen(buf);
673 1.1 riastrad if (ftell(file) == end)
674 1.1 riastrad done = 1;
675 1.1 riastrad if (len) {
676 1.1 riastrad r = regexec(&mask_rex, buf, 4, match, 0);
677 1.1 riastrad if (r == REG_NOMATCH) {
678 1.1 riastrad } else if (r) {
679 1.1 riastrad fprintf(stderr,
680 1.1 riastrad "Error matching regular expression %d in %s\n",
681 1.1 riastrad r, filename);
682 1.1 riastrad fclose(file);
683 1.1 riastrad return -1;
684 1.1 riastrad } else {
685 1.1 riastrad buf[match[0].rm_eo] = 0;
686 1.1 riastrad buf[match[1].rm_eo] = 0;
687 1.1 riastrad buf[match[2].rm_eo] = 0;
688 1.1 riastrad o = strtol(&buf[match[1].rm_so], NULL, 16);
689 1.1 riastrad offset = offset_new(o);
690 1.1 riastrad table_offset_add(t, offset);
691 1.1 riastrad if (o > t->offset_max)
692 1.1 riastrad t->offset_max = o;
693 1.1 riastrad }
694 1.1 riastrad }
695 1.1 riastrad } while (!done);
696 1.1 riastrad fclose(file);
697 1.1 riastrad if (t->offset_max < last_reg)
698 1.1 riastrad t->offset_max = last_reg;
699 1.1 riastrad return table_build(t);
700 1.1 riastrad }
701 1.1 riastrad
702 1.1 riastrad int main(int argc, char *argv[])
703 1.1 riastrad {
704 1.1 riastrad struct table t;
705 1.1 riastrad
706 1.1 riastrad if (argc != 2) {
707 1.1 riastrad fprintf(stderr, "Usage: %s <authfile>\n", argv[0]);
708 1.1 riastrad exit(1);
709 1.1 riastrad }
710 1.1 riastrad table_init(&t);
711 1.1 riastrad if (parser_auth(&t, argv[1])) {
712 1.1 riastrad fprintf(stderr, "Failed to parse file %s\n", argv[1]);
713 1.1 riastrad return -1;
714 1.1 riastrad }
715 1.1 riastrad table_print(&t);
716 1.1 riastrad return 0;
717 1.1 riastrad }
718