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