Debian Codegraph @

common/insn_lookup.c

raw
1 /*
2 * Cisco router simulation platform.
3 * Copyright (c) 2006 Christophe Fillot ([email protected])
4 *
5 * Instruction Lookup Tables.
6 */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <string.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <assert.h>
15
16 #include "utils.h"
17 #include "hash.h"
18 #include "insn_lookup.h"
19 #include "dynamips.h"
20
21 /* Hash function for a CBM */
22 static inline u_int cbm_hash_f(void *ccbm)
23 {
24 cbm_array_t *cbm = (cbm_array_t *)ccbm;
25 char *p,*s = (char *)(cbm->tab);
26 u_int h,g,i;
27
28 for(h=0,i=0,p=s;i<(cbm->nr_entries*sizeof(int));p+=1,i++)
29 {
30 h = (h << 4) + *p;
31 if ((g = h & 0xf0000000)) {
32 h = h ^ (g >> 24);
33 h = h ^ g;
34 }
35 }
36
37 return(h);
38 }
39
40 /* Comparison function for 2 CBM */
41 static inline int cbm_cmp_f(void *b1,void *b2)
42 {
43 cbm_array_t *cbm1 = (cbm_array_t *)b1;
44 cbm_array_t *cbm2 = (cbm_array_t *)b2;
45 int i;
46
47 for(i=0;i<cbm1->nr_entries;i++)
48 if (cbm1->tab[i] != cbm2->tab[i])
49 return(FALSE);
50
51 return(TRUE);
52 }
53
54 /* Set bit corresponding to a rule number in a CBM */
55 static inline void cbm_set_rule(cbm_array_t *cbm,int rule_id)
56 {
57 CBM_ARRAY(cbm,(rule_id >> CBM_SHIFT)) |= 1 << (rule_id & (CBM_SIZE-1));
58 }
59
60 /* Clear bit corresponding to a rule number in a CBM */
61 static inline void cbm_unset_rule(cbm_array_t *cbm,int rule_id)
62 {
63 CBM_ARRAY(cbm,(rule_id >> CBM_SHIFT)) &= ~(1 << (rule_id & (CBM_SIZE-1)));
64 }
65
66 /* Returns TRUE if bit corresponding to a rule number in a CBM is set */
67 static inline int cbm_check_rule(cbm_array_t *cbm,int rule_id)
68 {
69 return(CBM_ARRAY(cbm,(rule_id >> CBM_SHIFT)) &
70 (1 << (rule_id & (CBM_SIZE-1))));
71 }
72
73 /* Compute bitwise ANDing of two CBM */
74 static inline void
75 cbm_bitwise_and(cbm_array_t *result,cbm_array_t *a1,cbm_array_t *a2)
76 {
77 int i;
78
79 /* Compute bitwise ANDing */
80 for(i=0;i<a1->nr_entries;i++)
81 CBM_ARRAY(result,i) = CBM_ARRAY(a1,i) & CBM_ARRAY(a2,i);
82 }
83
84 /* Get first matching rule number */
85 static inline int cbm_first_match(insn_lookup_t *ilt,cbm_array_t *cbm)
86 {
87 int i;
88
89 for(i=0;i<ilt->nr_insn;i++)
90 if (cbm_check_rule(cbm,i))
91 return(i);
92
93 return(-1);
94 }
95
96 /* Create a class bitmap (CBM) */
97 static cbm_array_t *cbm_create(insn_lookup_t *ilt)
98 {
99 cbm_array_t *array;
100 int size;
101
102 size = CBM_CSIZE(ilt->cbm_size);
103
104 /* CBM are simply bit arrays */
105 array = malloc(size);
106 assert(array);
107
108 memset(array,0,size);
109 array->nr_entries = ilt->cbm_size;
110 return array;
111 }
112
113 /* Duplicate a class bitmap */
114 static cbm_array_t *cbm_duplicate(cbm_array_t *cbm)
115 {
116 int size = CBM_CSIZE(cbm->nr_entries);
117 cbm_array_t *array;
118
119 array = malloc(size);
120 assert(array);
121 memcpy(array,cbm,size);
122 return array;
123 }
124
125 /*
126 * Get equivalent class corresponding to a class bitmap. Create eqclass
127 * structure if needed (CBM not previously seen).
128 */
129 static rfc_eqclass_t *cbm_get_eqclass(rfc_array_t *rfct,cbm_array_t *cbm)
130 {
131 rfc_eqclass_t *eqcl;
132 cbm_array_t *bmp;
133
134 /* Lookup for CBM into hash table */
135 if ((eqcl = hash_table_lookup(rfct->cbm_hash,cbm)) == NULL)
136 {
137 /* Duplicate CBM */
138 bmp = cbm_duplicate(cbm);
139 assert(bmp);
140
141 /* CBM is not already known */
142 eqcl = malloc(sizeof(rfc_eqclass_t));
143 assert(eqcl);
144
145 assert(rfct->nr_eqid < rfct->nr_elements);
146
147 /* Get a new equivalent ID */
148 eqcl->eqID = rfct->nr_eqid++;
149 eqcl->cbm = bmp;
150 rfct->id2cbm[eqcl->eqID] = bmp;
151
152 /* Insert it in hash table */
153 if (hash_table_insert(rfct->cbm_hash,bmp,eqcl) == -1)
154 return NULL;
155 }
156
157 return eqcl;
158 }
159
160 /* Allocate an array for Recursive Flow Classification */
161 static rfc_array_t *rfc_alloc_array(int nr_elements)
162 {
163 rfc_array_t *array;
164 int total_size;
165
166 /* Compute size of memory chunk needed to store the array */
167 total_size = (nr_elements * sizeof(int)) + sizeof(rfc_array_t);
168 array = malloc(total_size);
169 assert(array);
170 memset(array,0,total_size);
171 array->nr_elements = nr_elements;
172
173 /* Initialize hash table for Class Bitmaps */
174 array->cbm_hash = hash_table_create(cbm_hash_f,cbm_cmp_f,CBM_HASH_SIZE);
175 assert(array->cbm_hash);
176
177 /* Initialize table for converting ID to CBM */
178 array->id2cbm = calloc(nr_elements,sizeof(cbm_array_t *));
179 assert(array->id2cbm);
180
181 return(array);
182 }
183
184 /* Free value of cbm_hash */
185 static void rfc_free_array_cbm_hash_value(void *key,void *value,void *opt_arg)
186 {
187 free(value); // rfc_eqclass_t *
188 }
189
190 /* Free an array for Recursive Flow Classification */
191 static void rfc_free_array(rfc_array_t *array)
192 {
193 int i;
194
195 assert(array);
196
197 /* Free hash table for Class Bitmaps */
198 if (array->cbm_hash) {
199 hash_table_foreach(array->cbm_hash, rfc_free_array_cbm_hash_value, array);
200 hash_table_delete(array->cbm_hash);
201 array->cbm_hash = NULL;
202 }
203
204 /* Free table for converting ID to CBM */
205 if (array->id2cbm) {
206 for (i = 0; i < array->nr_elements; i++) {
207 if (array->id2cbm[i])
208 free(array->id2cbm[i]);
209 }
210 free(array->id2cbm);
211 array->id2cbm = NULL;
212 }
213
214 /* Free array */
215 free(array);
216 }
217
218 /* Check an instruction with specified parameter */
219 static void rfc_check_insn(insn_lookup_t *ilt,cbm_array_t *cbm,
220 ilt_check_cbk_t pcheck,int value)
221 {
222 void *p;
223 int i;
224
225 for(i=0;i<ilt->nr_insn;i++) {
226 p = ilt->get_insn(i);
227
228 if (pcheck(p,value))
229 cbm_set_rule(cbm,i);
230 else
231 cbm_unset_rule(cbm,i);
232 }
233 }
234
235 /* RFC Chunk preprocessing: phase 0 */
236 static rfc_array_t *rfc_phase_0(insn_lookup_t *ilt,ilt_check_cbk_t pcheck)
237 {
238 rfc_eqclass_t *eqcl;
239 rfc_array_t *rfct;
240 cbm_array_t *bmp;
241 int i;
242
243 /* allocate a temporary class bitmap */
244 bmp = cbm_create(ilt);
245 assert(bmp);
246
247 /* Allocate a new RFC array of 16-bits entries */
248 rfct = rfc_alloc_array(RFC_ARRAY_MAXSIZE);
249 assert(rfct);
250
251 for(i=0;i<RFC_ARRAY_MAXSIZE;i++)
252 {
253 /* determine all instructions that match this value */
254 rfc_check_insn(ilt,bmp,pcheck,i);
255
256 /* get equivalent class for this bitmap */
257 eqcl = cbm_get_eqclass(rfct,bmp);
258 assert(eqcl);
259
260 /* fill the RFC table */
261 rfct->eqID[i] = eqcl->eqID;
262 }
263
264 free(bmp);
265 return rfct;
266 }
267
268 /* RFC Chunk preprocessing: phase j (j > 0) */
269 static rfc_array_t *rfc_phase_j(insn_lookup_t *ilt,rfc_array_t *p0,
270 rfc_array_t *p1)
271 {
272 rfc_eqclass_t *eqcl;
273 rfc_array_t *rfct;
274 cbm_array_t *bmp;
275 int nr_elements;
276 int index = 0;
277 int i,j;
278
279 /* allocate a temporary class bitmap */
280 bmp = cbm_create(ilt);
281 assert(bmp);
282
283 /* compute number of elements */
284 nr_elements = p0->nr_eqid * p1->nr_eqid;
285
286 /* allocate a new RFC array */
287 rfct = rfc_alloc_array(nr_elements);
288 assert(rfct);
289 rfct->parent0 = p0;
290 rfct->parent1 = p1;
291
292 /* make a cross product between p0 and p1 */
293 for(i=0;i<p0->nr_eqid;i++)
294 for(j=0;j<p1->nr_eqid;j++)
295 {
296 /* compute bitwise AND */
297 cbm_bitwise_and(bmp,p0->id2cbm[i],p1->id2cbm[j]);
298
299 /* get equivalent class for this bitmap */
300 eqcl = cbm_get_eqclass(rfct,bmp);
301 assert(eqcl);
302
303 /* fill RFC table */
304 rfct->eqID[index++] = eqcl->eqID;
305 }
306
307 free(bmp);
308 return rfct;
309 }
310
311 /* Compute RFC phase 0 */
312 static void ilt_phase_0(insn_lookup_t *ilt,int idx,ilt_check_cbk_t pcheck)
313 {
314 rfc_array_t *rfct;
315
316 rfct = rfc_phase_0(ilt,pcheck);
317 assert(rfct);
318 ilt->rfct[idx] = rfct;
319 }
320
321 /* Compute RFC phase j */
322 static void ilt_phase_j(insn_lookup_t *ilt,int p0,int p1,int res)
323 {
324 rfc_array_t *rfct;
325
326 rfct = rfc_phase_j(ilt,ilt->rfct[p0],ilt->rfct[p1]);
327 assert(rfct);
328 ilt->rfct[res] = rfct;
329 }
330
331 /* Postprocessing */
332 static void ilt_postprocessing(insn_lookup_t *ilt)
333 {
334 rfc_array_t *rfct = ilt->rfct[2];
335 int i;
336
337 for(i=0;i<rfct->nr_elements;i++)
338 rfct->eqID[i] = cbm_first_match(ilt,rfct->id2cbm[rfct->eqID[i]]);
339 }
340
341 /* Instruction lookup table compilation */
342 static void ilt_compile(insn_lookup_t *ilt)
343 {
344 ilt_phase_0(ilt,0,ilt->chk_hi);
345 ilt_phase_0(ilt,1,ilt->chk_lo);
346 ilt_phase_j(ilt,0,1,2);
347 ilt_postprocessing(ilt);
348 }
349
350 /* Dump an instruction lookup table */
351 _Unused static int ilt_dump(char *table_name,insn_lookup_t *ilt)
352 {
353 rfc_array_t *rfct;
354 char *filename;
355 FILE *fd;
356 int i,j;
357
358 filename = dyn_sprintf("ilt_dump_%s_%s.txt",sw_version_tag,table_name);
359 assert(filename != NULL);
360
361 fd = fopen(filename,"w");
362 assert(fd != NULL);
363
364 fprintf(fd,"ILT %p: nr_insn=%d, cbm_size=%d\n",
365 ilt,ilt->nr_insn,ilt->cbm_size);
366
367 for(i=0;i<RFC_ARRAY_NUMBER;i++) {
368 rfct = ilt->rfct[i];
369
370 fprintf(fd,"RFCT %d: nr_elements=%d, nr_eqid=%d\n",
371 i,rfct->nr_elements,rfct->nr_eqid);
372
373 for(j=0;j<rfct->nr_elements;j++)
374 fprintf(fd," (0x%4.4x,0x%4.4x) = 0x%4.4x\n",i,j,rfct->eqID[j]);
375 }
376
377 fclose(fd);
378 free(filename);
379 return(0);
380 }
381
382 /* Write the specified RFC array to disk */
383 static void ilt_store_rfct(FILE *fd,int id,rfc_array_t *rfct)
384 {
385 /* Store RFC array ID + number of elements */
386 fwrite(&id,sizeof(id),1,fd);
387 fwrite(&rfct->nr_elements,sizeof(rfct->nr_elements),1,fd);
388 fwrite(&rfct->nr_eqid,sizeof(rfct->nr_eqid),1,fd);
389
390 fwrite(rfct->eqID,sizeof(int),rfct->nr_elements,fd);
391 }
392
393 /* Write the full instruction lookup table */
394 static void ilt_store_table(FILE *fd,insn_lookup_t *ilt)
395 {
396 int i;
397
398 for(i=0;i<RFC_ARRAY_NUMBER;i++)
399 if (ilt->rfct[i] != NULL)
400 ilt_store_rfct(fd,i,ilt->rfct[i]);
401 }
402
403 /* Load an RFC array from disk */
404 static int ilt_load_rfct(FILE *fd,insn_lookup_t *ilt)
405 {
406 u_int id,nr_elements,nr_eqid;
407 rfc_array_t *rfct;
408 size_t len;
409
410 /* Read ID and number of elements */
411 if ((fread(&id,sizeof(id),1,fd) != 1) ||
412 (fread(&nr_elements,sizeof(nr_elements),1,fd) != 1) ||
413 (fread(&nr_eqid,sizeof(nr_eqid),1,fd) != 1))
414 return(-1);
415
416 if ((id >= RFC_ARRAY_NUMBER) || (nr_elements > RFC_ARRAY_MAXSIZE))
417 return(-1);
418
419 /* Allocate the RFC array with the eqID table */
420 len = sizeof(*rfct) + (nr_elements * sizeof(int));
421
422 if (!(rfct = malloc(len)))
423 return(-1);
424
425 memset(rfct,0,sizeof(*rfct));
426 rfct->nr_elements = nr_elements;
427 rfct->nr_eqid = nr_eqid;
428
429 /* Read the equivalent ID array */
430 if (fread(rfct->eqID,sizeof(int),nr_elements,fd) != nr_elements) {
431 free(rfct);
432 return(-1);
433 }
434
435 ilt->rfct[id] = rfct;
436 return(0);
437 }
438
439 /* Check an instruction table loaded from disk */
440 static int ilt_check_cached_table(insn_lookup_t *ilt)
441 {
442 int i;
443
444 /* All arrays must have been loaded */
445 for(i=0;i<RFC_ARRAY_NUMBER;i++)
446 if (!ilt->rfct[i])
447 return(-1);
448
449 return(0);
450 }
451
452 /* Load a full instruction table from disk */
453 static insn_lookup_t *ilt_load_table(FILE *fd)
454 {
455 insn_lookup_t *ilt;
456 int i;
457
458 if (!(ilt = malloc(sizeof(*ilt))))
459 return NULL;
460
461 memset(ilt,0,sizeof(*ilt));
462 fseek(fd,0,SEEK_SET);
463
464 for(i=0;i<RFC_ARRAY_NUMBER;i++) {
465 if (ilt_load_rfct(fd,ilt) == -1)
466 return NULL;
467 }
468
469 if (ilt_check_cached_table(ilt) == -1)
470 return NULL;
471
472 return ilt;
473 }
474
475 /* Build a filename for a cached ILT table on disk */
476 static char *ilt_build_filename(char *table_name)
477 {
478 return(dyn_sprintf("ilt_%s_%s",sw_version_tag,table_name));
479 }
480
481 /* Try to load a cached ILT table from disk */
482 static insn_lookup_t *ilt_cache_load(char *table_name)
483 {
484 insn_lookup_t *ilt;
485 char *filename;
486 FILE *fd;
487
488 if (!(filename = ilt_build_filename(table_name)))
489 return NULL;
490
491 if (!(fd = fopen(filename,"rb"))) {
492 free(filename);
493 return NULL;
494 }
495
496 ilt = ilt_load_table(fd);
497 fclose(fd);
498 free(filename);
499 return ilt;
500 }
501
502 /* Store the specified ILT table on disk for future use (cache) */
503 static int ilt_cache_store(char *table_name,insn_lookup_t *ilt)
504 {
505 char *filename;
506 FILE *fd;
507
508 if (!(filename = ilt_build_filename(table_name)))
509 return(-1);
510
511 if (!(fd = fopen(filename,"wb"))) {
512 free(filename);
513 return(-1);
514 }
515
516 ilt_store_table(fd,ilt);
517 fclose(fd);
518 free(filename);
519 return(0);
520 }
521
522 /* Create an instruction lookup table */
523 insn_lookup_t *ilt_create(char *table_name,
524 int nr_insn,ilt_get_insn_cbk_t get_insn,
525 ilt_check_cbk_t chk_lo,ilt_check_cbk_t chk_hi)
526 {
527 insn_lookup_t *ilt;
528
529 /* Try to load a cached table from disk */
530 if ((ilt = ilt_cache_load(table_name))) {
531 printf("ILT: loaded table \"%s\" from cache.\n",table_name);
532 return ilt;
533 }
534
535 /* We have to build the full table... */
536 ilt = malloc(sizeof(*ilt));
537 assert(ilt);
538 memset(ilt,0,sizeof(*ilt));
539
540 ilt->cbm_size = normalize_size(nr_insn,CBM_SIZE,CBM_SHIFT);
541 ilt->nr_insn = nr_insn;
542 ilt->get_insn = get_insn;
543 ilt->chk_lo = chk_lo;
544 ilt->chk_hi = chk_hi;
545
546 /* Compile the instruction opcodes */
547 ilt_compile(ilt);
548
549 /* Store the result on disk for future exec */
550 ilt_cache_store(table_name,ilt);
551 return(ilt);
552 }
553
554 /* Destroy an instruction lookup table */
555 void ilt_destroy(insn_lookup_t *ilt)
556 {
557 int i;
558
559 assert(ilt);
560
561 /* Free instruction opcodes */
562 for (i = 0; i < RFC_ARRAY_NUMBER; i++) {
563 if (ilt->rfct[i])
564 rfc_free_array(ilt->rfct[i]);
565 }
566
567 /* Free instruction lookup table */
568 free(ilt);
569 }