-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathdmllib.h
1950 lines (1749 loc) · 66.2 KB
/
dmllib.h
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
© 2010-2022 Intel Corporation
SPDX-License-Identifier: 0BSD
*/
/* DML runtime utilities needed by the C code generated by dmlc */
#ifndef SIMICS_DMLLIB_H
#define SIMICS_DMLLIB_H
#include <stddef.h>
#include <simics/base/types.h>
#include <simics/base/log.h>
#include <simics/base/conf-object.h>
#include <simics/base/version.h>
#include <simics/util/swabber.h>
#include <simics/model-iface/bank-instrumentation.h>
#include <simics/simulator/conf-object.h>
#include <simics/util/alloc.h>
#include <simics/util/hashtab.h>
#include <simics/util/help-macros.h>
#include <simics/util/strbuf.h>
#include <simics/util/swabber.h>
// Copy bits from y given by mask into corresponding bits in x and return the
// result
static inline uint64 DML_combine_bits(uint64 x, uint64 y, uint64 mask)
{
return (x & ~mask) | (y & mask);
}
#define DML_ASSERT(filename, lineno, expr) do { \
if (unlikely(!(expr))) \
assert_error((lineno), (filename), "", NULL); \
} while (0)
static inline uint64 DML_shlu(uint64 a, uint64 b)
{
return b > 63 ? 0 : a << b;
}
UNUSED static void
_DML_fault(const char *filename, int lineno, const char *msg)
{
// Need SIM_get_api_function indirection to work with old 6 base
// packages.
// TODO: use VT_critical_error directly in simics 7
void (*critical_error)(const char *, const char *)
= (void (*)(const char *, const char *))SIM_get_api_function(
"VT_critical_error");
if (critical_error) {
char long_msg[256];
snprintf(long_msg, 256, "%s:%d: %s", filename, lineno, msg);
critical_error(msg, long_msg);
} else {
assert_error(lineno, filename, "", msg);
}
}
static inline int64 DML_shl(int64 a, int64 b, const char *filename, int lineno)
{
if (unlikely(b < 0)) {
_DML_fault(filename, lineno, "negative shift count");
return 0;
}
return b > 63 ? 0 : (uint64)a << b;
}
static inline uint64 DML_shru(uint64 a, uint64 b)
{
return b > 63 ? 0 : a >> b;
}
static inline int64 DML_shr(int64 a, int64 b, const char *filename, int lineno)
{
if (unlikely(b < 0)) {
_DML_fault(filename, lineno, "negative shift count");
return 0;
}
return b > 63 ? (a > 0 ? 0 : -1) : a >> b;
}
static inline uint64 DML_divu(uint64 a, uint64 b,
const char *filename, int lineno)
{
if (unlikely(b == 0)) {
_DML_fault(filename, lineno, "integer division by zero");
return 0;
}
return a / b;
}
static inline int64 DML_div(int64 a, int64 b, const char *filename, int lineno)
{
if (unlikely(b == 0)) {
_DML_fault(filename, lineno, "integer division by zero");
return 0;
}
return a / b;
}
static inline uint64 DML_modu(uint64 a, uint64 b,
const char *filename, int lineno)
{
if (unlikely(b == 0)) {
_DML_fault(filename, lineno, "integer modulo by zero");
return 0;
}
return a % b;
}
static inline int64 DML_mod(int64 a, int64 b, const char *filename, int lineno)
{
if (unlikely(b == 0)) {
_DML_fault(filename, lineno, "integer modulo by zero");
return 0;
}
return a % b;
}
static inline bool
DML_lt_iu(int64 a, uint64 b)
{
return (uint64)a < b || ((a | b) >> 63);
}
static inline bool
DML_lt_ui(uint64 a, int64 b)
{
return a < (uint64)b && !((a | b) >> 63);
}
static inline bool
DML_leq_iu(int64 a, uint64 b)
{
return (uint64)a <= b || ((a | b) >> 63);
}
static inline bool
DML_leq_ui(uint64 a, int64 b)
{
return a <= (uint64)b && !((a | b) >> 63);
}
// compare signed and unsigned: return true if bits are equal and neither sign
// bit is set
static inline bool
DML_eq(uint64 a, uint64 b)
{
return ((a ^ b) | ((b | a) >> 63)) == 0;
}
// Upcast a trait reference expression expr of type 'from' to a corresponding
// trait reference of an ancestor trait, given that the vtable of the new trait
// is found as from.ancestry. E.g.: If a trait y extends z, and x extends y,
// and xyz is a reference to trait x, then UPCAST(xyz, x, y.z) yields a trait
// reference to the z trait.
#define UPCAST(expr, from, ancestry) \
({_traitref_t __tref = expr; \
__tref.trait = (char *)(__tref.trait) \
+ offsetof(struct _ ## from, ancestry); \
__tref;})
// Downcast a trait reference expression expr to a corresponding trait
// reference with vtable type of trait 'to',
// given that expr's vtable type is found as to.ancestry. E.g.: If a trait y
// extends z, and x extends y, and xyz is a reference to trait x upcast to
// trait z, then DOWNCAST(xyz, x, y.z) yields a trait reference to the x trait,
// and UPCAST(DOWNCAST(xyz, x, y.z), x, y.z) == xyz.
// This is similar to the container_of macro in Linux.
#define DOWNCAST(expr, to, ancestry) \
({_traitref_t __tref = expr; \
__tref.trait = (char *)(__tref.trait) \
- offsetof(struct _ ## to, ancestry); \
__tref;})
#define CALL_TRAIT_METHOD(type, method, dev, traitref, ...) \
({_traitref_t __tref = traitref; \
((struct _ ## type *) __tref.trait)->method(dev, __tref, \
__VA_ARGS__);})
#define CALL_TRAIT_METHOD0(type, method, dev, traitref) \
({_traitref_t __tref = traitref; \
((struct _ ## type *) __tref.trait)->method(dev, __tref);}) \
// A parameter is represented in the vtable using a pointer that points to the
// parameter value. A parameter whose value varies across indices is stored as
// an array of all values, indexed by encoded_index. This is marked by setting
// bit 0 of the vtable's pointer to 1. A parameter that stays constant across
// indices is represented as a single value, this is marked by setting bit 0 of
// the pointer to 0. Parameter values are always stored with an alignment of at
// least 2.
#define VTABLE_PARAM(traitref, vtable_type, member) \
({_traitref_t __tref = traitref; \
uintptr_t __member \
= (uintptr_t)((vtable_type *)__tref.trait)->member; \
(__member & 1) \
? ((typeof(((vtable_type*)NULL)->member))(__member - 1))[ \
__tref.id.encoded_index] \
: *(typeof(((vtable_type*)NULL)->member))__member; })
#define _raw_load_uint8_be_t UNALIGNED_LOAD_BE8
#define _raw_load_uint16_be_t UNALIGNED_LOAD_BE16
#define _raw_load_uint32_be_t UNALIGNED_LOAD_BE32
#define _raw_load_uint64_be_t UNALIGNED_LOAD_BE64
#define _raw_load_uint8_le_t UNALIGNED_LOAD_LE8
#define _raw_load_uint16_le_t UNALIGNED_LOAD_LE16
#define _raw_load_uint32_le_t UNALIGNED_LOAD_LE32
#define _raw_load_uint64_le_t UNALIGNED_LOAD_LE64
#define _raw_store_uint8_be_t UNALIGNED_STORE_BE8
#define _raw_store_uint16_be_t UNALIGNED_STORE_BE16
#define _raw_store_uint32_be_t UNALIGNED_STORE_BE32
#define _raw_store_uint64_be_t UNALIGNED_STORE_BE64
#define _raw_store_uint8_le_t UNALIGNED_STORE_LE8
#define _raw_store_uint16_le_t UNALIGNED_STORE_LE16
#define _raw_store_uint32_le_t UNALIGNED_STORE_LE32
#define _raw_store_uint64_le_t UNALIGNED_STORE_LE64
static inline int8
_raw_load_int8_be_t(const void *addr)
{
return (int8)_raw_load_uint8_be_t(addr);
}
static inline int16
_raw_load_int16_be_t(const void *addr)
{
return (int16)_raw_load_uint16_be_t(addr);
}
static inline int32
_raw_load_int32_be_t(const void *addr)
{
return (int32)_raw_load_uint32_be_t(addr);
}
static inline int64
_raw_load_int64_be_t(const void *addr)
{
return (int64)_raw_load_uint64_be_t(addr);
}
static inline int8
_raw_load_int8_le_t(const void *addr)
{
return (int8)_raw_load_uint8_le_t(addr);
}
static inline int16
_raw_load_int16_le_t(const void *addr)
{
return (int16)_raw_load_uint16_le_t(addr);
}
static inline int32
_raw_load_int32_le_t(const void *addr)
{
return (int32)_raw_load_uint32_le_t(addr);
}
static inline int64
_raw_load_int64_le_t(const void *addr)
{
return (int64)_raw_load_uint64_le_t(addr);
}
static inline uint32
_raw_load_uint24_be_t(const void *addr)
{
return _raw_load_uint8_be_t(addr) << 16 |
_raw_load_uint16_be_t((char *)addr + 1);
}
static inline int32
_raw_load_int24_be_t(const void *addr)
{
return ((int32)_raw_load_uint24_be_t(addr) << 8) >> 8;
}
static inline void
_raw_store_uint24_be_t(void *addr, uint32 v)
{
_raw_store_uint8_be_t(addr, v >> 16);
_raw_store_uint16_be_t((char *)addr + 1, v & 0xffff);
}
static inline uint64
_raw_load_uint40_be_t(const void *addr)
{
return (uint64)_raw_load_uint8_be_t(addr) << 32
| _raw_load_uint32_be_t((char *)addr + 1);
}
static inline int64
_raw_load_int40_be_t(const void *addr)
{
return ((int64)_raw_load_uint40_be_t(addr) << 24) >> 24;
}
static inline void
_raw_store_uint40_be_t(void *addr, uint64 v)
{
_raw_store_uint8_be_t(addr, v >> 32);
_raw_store_uint32_be_t((char *)addr + 1, v & 0xffffffff);
}
static inline uint64
_raw_load_uint48_be_t(const void *addr)
{
return (uint64)_raw_load_uint16_be_t(addr) << 32
| _raw_load_uint32_be_t((char *)addr + 2);
}
static inline int64
_raw_load_int48_be_t(const void *addr)
{ return ((int64)_raw_load_uint48_be_t(addr) << 16) >> 16; }
static inline void
_raw_store_uint48_be_t(void *addr, uint64 v)
{
_raw_store_uint16_be_t(addr, v >> 32);
_raw_store_uint32_be_t((char *)addr + 2, v & 0xffffffff);
}
static inline uint64
_raw_load_uint56_be_t(const void *addr)
{
return (uint64)_raw_load_uint24_be_t(addr) << 32
| _raw_load_uint32_be_t((char *)addr + 3);
}
static inline int64
_raw_load_int56_be_t(const void *addr)
{ return ((int64)_raw_load_uint56_be_t(addr) << 8) >> 8; }
static inline void
_raw_store_uint56_be_t(void *addr, uint64 v)
{
_raw_store_uint24_be_t(addr, v >> 32);
_raw_store_uint32_be_t((char *)addr + 3, v & 0xffffffff);
}
static inline uint32
_raw_load_uint24_le_t(const void *addr)
{
return _raw_load_uint8_le_t((char *)addr + 2) << 16 |
_raw_load_uint16_le_t(addr);
}
static inline int32
_raw_load_int24_le_t(const void *addr)
{
return ((int32)_raw_load_uint24_le_t(addr) << 8) >> 8;
}
static inline void
_raw_store_uint24_le_t(void *addr, uint32 v)
{
_raw_store_uint8_le_t((char *)addr + 2, v >> 16);
_raw_store_uint16_le_t(addr, v & 0xffff);
}
static inline uint64
_raw_load_uint40_le_t(const void *addr)
{
return (uint64)_raw_load_uint8_le_t((char *)addr + 4) << 32
| _raw_load_uint32_le_t(addr);
}
static inline int64
_raw_load_int40_le_t(const void *addr)
{
return ((int64)_raw_load_uint40_le_t(addr) << 24) >> 24;
}
static inline void
_raw_store_uint40_le_t(void *addr, uint64 v)
{
_raw_store_uint8_le_t((char *)addr + 4, v >> 32);
_raw_store_uint32_le_t(addr, v & 0xffffffff);
}
static inline uint64
_raw_load_uint48_le_t(const void *addr)
{
return (uint64)_raw_load_uint16_le_t((char *)addr + 4) << 32
| _raw_load_uint32_le_t(addr);
}
static inline int64
_raw_load_int48_le_t(const void *addr)
{
return ((int64)_raw_load_uint48_le_t(addr) << 16) >> 16;
}
static inline void
_raw_store_uint48_le_t(void *addr, uint64 v)
{
_raw_store_uint16_le_t((char *)addr + 4, v >> 32);
_raw_store_uint32_le_t(addr, v & 0xffffffff);
}
static inline uint64
_raw_load_uint56_le_t(const void *addr)
{
return (uint64)_raw_load_uint24_le_t((char *)addr + 4) << 32
| _raw_load_uint32_le_t(addr);
}
static inline int64
_raw_load_int56_le_t(const void *addr)
{
return ((int64)_raw_load_uint56_le_t(addr) << 8) >> 8;
}
static inline void
_raw_store_uint56_le_t(void *addr, uint64 v)
{
_raw_store_uint24_le_t((char *)addr + 4, v >> 32);
_raw_store_uint32_le_t(addr, v & 0xffffffff);
}
// Define signed aliases for raw store, used in macros later
#define _raw_store_int8_be_t _raw_store_uint8_be_t
#define _raw_store_int16_be_t _raw_store_uint16_be_t
#define _raw_store_int24_be_t _raw_store_uint24_be_t
#define _raw_store_int32_be_t _raw_store_uint32_be_t
#define _raw_store_int40_be_t _raw_store_uint40_be_t
#define _raw_store_int48_be_t _raw_store_uint48_be_t
#define _raw_store_int56_be_t _raw_store_uint56_be_t
#define _raw_store_int64_be_t _raw_store_uint64_be_t
#define _raw_store_int8_le_t _raw_store_uint8_le_t
#define _raw_store_int16_le_t _raw_store_uint16_le_t
#define _raw_store_int24_le_t _raw_store_uint24_le_t
#define _raw_store_int32_le_t _raw_store_uint32_le_t
#define _raw_store_int40_le_t _raw_store_uint40_le_t
#define _raw_store_int48_le_t _raw_store_uint48_le_t
#define _raw_store_int56_le_t _raw_store_uint56_le_t
#define _raw_store_int64_le_t _raw_store_uint64_le_t
// macro definitions for endian types
#define FOR_ALL_BITSIZES(x, s, ed) \
x(8, s, ed) x(16, s, ed) \
x(24, s, ed) x(32, s, ed) \
x(40, s, ed) x(48, s, ed) \
x(56, s, ed) x(64, s, ed)
#define FOR_ENDIAN_VARIANTS(x) \
FOR_ALL_BITSIZES(x, u, be) FOR_ALL_BITSIZES(x, u, le) \
FOR_ALL_BITSIZES(x, , be) FOR_ALL_BITSIZES(x, , le)
// declare endian types
#define DECLARE_ENDIAN_TYPE(size, sign, endian) \
typedef struct { uint8 bytes[size/8]; } \
sign ## int ## size ## _ ## endian ## _ ## t ;
FOR_ENDIAN_VARIANTS(DECLARE_ENDIAN_TYPE);
#undef DECLARE_ENDIAN_TYPE
#undef FOR_ALL_BITSIZES
#undef FOR_ENDIAN_VARIANTS
#define FOR_ALL_BITSIZES(x, sign, endian) \
x(int ## 64, sign ## int ## 8 ## _ ## endian ## _ ## t) \
x(int ## 64, sign ## int ## 16 ## _ ## endian ## _ ## t) \
x(int ## 64, sign ## int ## 24 ## _ ## endian ## _ ## t) \
x(int ## 64, sign ## int ## 32 ## _ ## endian ## _ ## t) \
x(int ## 64, sign ## int ## 40 ## _ ## endian ## _ ## t) \
x(int ## 64, sign ## int ## 48 ## _ ## endian ## _ ## t) \
x(int ## 64, sign ## int ## 56 ## _ ## endian ## _ ## t)
#define FOR_ENDIAN_VARIANTS(x) \
FOR_ALL_BITSIZES(x, , be) FOR_ALL_BITSIZES(x, , le) \
FOR_ALL_BITSIZES(x, u, be) FOR_ALL_BITSIZES(x, u, le) \
x(uint ## 64, uint ## 64_le_t) x(uint ## 64, uint ## 64_be_t) \
x(int ## 64, int ## 64_le_t) x(int ## 64, int ## 64_be_t)
#define DEFINE_ENDIAN_LOAD(inttype, endiantype) \
static inline inttype \
dml_load_ ## endiantype (const endiantype eint) \
{ \
return _raw_load_ ## endiantype \
((void *)&(eint.bytes)); \
};
FOR_ENDIAN_VARIANTS(DEFINE_ENDIAN_LOAD)
#undef DEFINE_ENDIAN_LOAD
#define DEFINE_ENDIAN_STORE(inttype, endiantype) \
static inline endiantype \
dml_store_ ## endiantype ( inttype val ) \
{ \
endiantype toret; \
_raw_store_ ## endiantype ((void *)&(toret.bytes), val); \
return toret; \
}
FOR_ENDIAN_VARIANTS(DEFINE_ENDIAN_STORE);
#undef DEFINE_ENDIAN_LOAD
// x = y is translated to dml_copy_T(&x, y) for endian types
#define DEFINE_ENDIAN_COPY(inttype, endiantype) \
static inline endiantype \
dml_copy_ ## endiantype ( endiantype *dest, endiantype val ) \
{ memcpy(dest, &val, sizeof(endiantype)); return val; }
FOR_ENDIAN_VARIANTS(DEFINE_ENDIAN_COPY);
#undef DEFINE_ENDIAN_COPY
#define DEFINE_PRECHANGE(inttype, endiantype) \
static inline inttype \
dml_prechange_ ## endiantype (endiantype *addr, int change, \
bool post) { \
uint64 orig = dml_load_ ## endiantype (*addr); \
*addr = dml_store_ ## endiantype(orig + change); \
return post ? orig : dml_load_ ## endiantype (*addr); \
}
FOR_ENDIAN_VARIANTS(DEFINE_PRECHANGE);
#undef DEFINE_PRECHANGE
#undef FOR_ALL_BITSIZES
#undef FOR_ENDIAN_VARIANTS
typedef struct {
uint32 id;
uint32 encoded_index;
} _identity_t;
typedef struct {
const char *logname;
const uint32 *dimsizes;
uint32 dimensions;
uint32 id;
} _id_info_t;
typedef struct {
void *trait;
_identity_t id;
} _traitref_t;
UNUSED static attr_value_t
_serialize_identity(const _id_info_t *id_info_array, const _identity_t id) {
_id_info_t info = id_info_array[id.id];
attr_value_t inner = SIM_alloc_attr_list(info.dimensions);
uint32 index = id.encoded_index;
for (int i = info.dimensions - 1; i >= 0; --i) {
SIM_attr_list_set_item(&inner, i,
SIM_make_attr_uint64(index % info.dimsizes[i]));
index /= info.dimsizes[i];
}
return SIM_make_attr_list(2, SIM_make_attr_string(info.logname), inner);
}
UNUSED static set_error_t
_deserialize_identity(ht_str_table_t *id_info_ht, attr_value_t val,
_identity_t *out_id) {
if (unlikely(!SIM_attr_is_list(val) || SIM_attr_list_size(val) != 2)) {
goto bad_repr;
}
attr_value_t logname_attr = SIM_attr_list_item(val, 0);
attr_value_t indices_attr = SIM_attr_list_item(val, 1);
if (unlikely(!SIM_attr_is_string(logname_attr)
|| !SIM_attr_is_list(indices_attr))) {
goto bad_repr;
}
const char *logname = SIM_attr_string(logname_attr);
const _id_info_t *info = ht_lookup_str(id_info_ht, logname);
if (unlikely(!info)) {
SIM_c_attribute_error("Failed to look up object node '%s' when "
"deserializing _identity_t", logname);
return Sim_Set_Illegal_Value;
}
if (unlikely(SIM_attr_list_size(indices_attr) != info->dimensions)) {
SIM_c_attribute_error(
"Invalid number of indices for when attempting "
"to deserialize _identity_t for object node %s. "
"Expected %u indices, got %u.",
logname, info->dimensions, SIM_attr_list_size(indices_attr));
return Sim_Set_Illegal_Value;
}
uint32 flat_index = 0;
attr_value_t *indices = SIM_attr_list(indices_attr);
for (uint32 i = 0; i < info->dimensions; ++i) {
if (unlikely(!SIM_attr_is_integer(indices[i]))) {
goto bad_repr;
}
uint32 index = SIM_attr_integer(indices[i]);
if (unlikely(index >= info->dimsizes[i])) {
SIM_c_attribute_error(
"Encountered invalid index when attempting to deserialize "
"_identity_t for object node %s. The size of dimension %u is "
"%u, but deserialized index for that dimension is %u.",
logname, i, info->dimsizes[i], index);
return Sim_Set_Illegal_Value;
}
flat_index = flat_index * info->dimsizes[i] + index;
}
*out_id = (_identity_t) { .id = info->id, .encoded_index = flat_index };
return Sim_Set_Ok;
bad_repr:
SIM_attribute_error("invalid serialized representation of _identity_t");
return Sim_Set_Illegal_Type;
}
UNUSED __attribute__((const)) static inline bool
_identity_eq(const _identity_t a, const _identity_t b) {
return a.id == b.id && a.encoded_index == b.encoded_index;
}
// List of vtables of a specific trait in one specific object
typedef struct {
// trait vtable instance
void *vtable;
// total number of elements (product of object's dimsizes)
uint64 num;
// The unique object id
uint32 id;
} _vtable_list_t;
typedef struct {
const _vtable_list_t *base;
// iteration interval in list of vtable_list_t. Interval includes start
// point but not endpoint. starti is currently always 0.
uint32 starti;
uint32 endi;
// When iterating inside an array, the vtable_list_t instances show
// number of elements globally. If one or more outer object array index
// is fixed in the 'in each' expression, then these members show which
// subset to iterate through from each vtable_list_t: Iteration in a
// vtable_array_t starts at array_idx * num/array_size, and we iterate
// through num/array_size elements.
uint32 array_idx;
uint32 array_size;
} _each_in_t;
UNUSED static uint64 _count_eachin(_each_in_t each_in) {
uint64 count = 0;
for (int i = each_in.starti; i < each_in.endi; ++i) {
count += each_in.base[i].num / each_in.array_size;
}
return count;
}
UNUSED static void
_DML_register_subobj_connect(conf_class_t *cls,
const char *classname, const char *port,
const char *desc)
{
conf_class_t *port_cls = SIM_get_class(classname);
if (port_cls == NULL) {
char short_buf[256];
snprintf(short_buf, 256, "Class '%s' not found", classname);
char long_buf[256];
snprintf(long_buf, 256,
"Failed to look up class '%s'"
" when registering port '%s'"
" in class '%s'", classname, port,
SIM_get_class_name(cls));
VT_critical_error(short_buf, long_buf);
return;
}
SIM_register_port(cls, port, port_cls, desc);
}
typedef struct {
conf_object_t obj;
conf_object_t *dev;
int ndims;
const uint32 *indices;
} _port_object_t;
UNUSED static conf_object_t *
_port_class_alloc_object(lang_void *data)
{
return (conf_object_t *)MM_ZALLOC(1, _port_object_t);
}
UNUSED static lang_void *
_port_class_init_object(conf_object_t *obj, lang_void *data)
{
_port_object_t *po = (_port_object_t *)obj;
if (po->dev == NULL) {
// can happen when restoring a checkpoint after reducing an
// object array size
SIM_LOG_ERROR(obj, 0, "Port object not recognized by parent");
return NULL;
}
return obj;
}
UNUSED static int
_port_class_delete_instance(conf_object_t *obj)
{
MM_FREE(obj);
return 0;
}
UNUSED static conf_class_t *
_register_port_class(const char *name, const char *desc, const char *doc) {
class_data_t cd = {
.alloc_object = _port_class_alloc_object,
.init_object = _port_class_init_object,
.delete_instance = _port_class_delete_instance,
.description = doc,
.class_desc = desc,
};
return SIM_register_class(name, &cd);
}
UNUSED static conf_object_t *
_init_port_object(conf_object_t *dev, const char *portname, int ndims,
const uint32 *indices)
{
_port_object_t *po = (_port_object_t *)SIM_object_descendant(
dev, portname);
ASSERT(po != NULL);
po->dev = dev;
po->ndims = ndims;
po->indices = indices;
return &po->obj;
}
/*
Before Simics 6, there were no port objects, so attributes in ports and banks
were registered on the device object using the name <portname>_<attrname>. In
Simics 6, attributes are instead registered on the port object, but with a
proxy pseudo attribute registered on the device object for compatibility.
There are two functions for registering an attribute on a port class, with a
proxy on the device class: _register_port_attr registers an attribute on a
single port, and _register_port_array_attr registers an attribute on a
one-dimensional port array. Multi-dimensional port arrays don't exist in 5 and
thus don't need proxy attributes.
*/
typedef struct {
ptrdiff_t port_obj_offset;
const char *attrname;
get_attr_t get;
set_attr_t set;
} _port_attr_t;
static attr_value_t
_getattr_from_portobj(lang_void *ptr, conf_object_t *obj, attr_value_t *idx)
{
ASSERT(SIM_attr_is_nil(*idx));
_port_attr_t *port = (_port_attr_t *)ptr;
conf_object_t *portobj = *(conf_object_t **)(
(uintptr_t)obj + port->port_obj_offset);
return port->get(NULL, portobj, NULL);
}
static set_error_t
_setattr_from_portobj(lang_void *ptr, conf_object_t *obj, attr_value_t *val,
attr_value_t *idx)
{
ASSERT(SIM_attr_is_nil(*idx));
_port_attr_t *port = (_port_attr_t *)ptr;
conf_object_t *portobj = *(conf_object_t **)(
(uintptr_t)obj + port->port_obj_offset);
if (port->attrname == NULL || SIM_object_is_configured(portobj)) {
return port->set(NULL, portobj, val, NULL);
} else {
// port attribute is registered as required; need to propagate
// value through API call to fulfil requirement
return SIM_set_attribute_default(portobj, port->attrname, *val);
}
}
UNUSED static void
_register_port_attr_no_aux(conf_class_t *portcls, const char *attrname,
get_attr_t getter, set_attr_t setter,
attr_attr_t attr, const char *type, const char *desc)
{
SIM_register_typed_attribute(
portcls, attrname, getter, NULL, setter, NULL,
attr, type, NULL, desc);
}
// port_obj_offset is the offset within the device struct of a pointer to the
// port object.
UNUSED static void
_register_port_attr(conf_class_t *devcls, conf_class_t *portcls,
ptrdiff_t port_obj_offset, bool is_bank,
const char *portname, const char *attrname,
get_attr_t getter, set_attr_t setter,
attr_attr_t attr, const char *type, const char *desc)
{
_register_port_attr_no_aux(portcls, attrname,
getter, setter, attr, type, desc);
_port_attr_t *data = MM_MALLOC(1, _port_attr_t);
data->port_obj_offset = port_obj_offset;
if ((attr & Sim_Attr_Flag_Mask) == Sim_Attr_Required) {
ASSERT(setter);
data->attrname = MM_STRDUP(attrname);
} else {
data->attrname = NULL;
}
data->get = getter;
data->set = setter;
char name[strlen(portname) + strlen(attrname) + 2];
sprintf(name, "%s_%s", portname, attrname);
strbuf_t proxy_desc = sb_newf(
"Proxy attribute for %s.%s.%s",
is_bank ? "bank" : "port", portname, attrname);
SIM_register_typed_attribute(
devcls, name,
getter ? _getattr_from_portobj : NULL, data,
setter ? _setattr_from_portobj : NULL, data,
(attr_attr_t)(Sim_Attr_Pseudo | Sim_Attr_Internal),
type, NULL, sb_str(&proxy_desc));
sb_free(&proxy_desc);
}
typedef struct {
ptrdiff_t port_obj_base_offset;
get_attr_t get;
set_attr_t set;
uint32 array_size;
} _port_array_attr_t;
static attr_value_t
_getattr_from_portobj_array(lang_void *ptr, conf_object_t *obj,
attr_value_t *idx)
{
ASSERT(SIM_attr_is_nil(*idx));
_port_array_attr_t *port = (_port_array_attr_t *)ptr;
conf_object_t **port_obj_ptr_base = (conf_object_t **)(
(uintptr_t)obj + port->port_obj_base_offset);
attr_value_t vals = SIM_alloc_attr_list(port->array_size);
for (uint32 i = 0; i < port->array_size; i++) {
conf_object_t *port_obj = port_obj_ptr_base[i];
attr_value_t val = port->get(NULL, port_obj, NULL);
if (SIM_attr_is_invalid(val)) {
SIM_attr_free(&vals);
return val;
}
SIM_attr_list_set_item(&vals, i, val);
}
return vals;
}
static set_error_t
_setattr_from_portobj_array(lang_void *ptr, conf_object_t *obj,
attr_value_t *vals, attr_value_t *idx)
{
ASSERT(SIM_attr_is_nil(*idx));
_port_array_attr_t *port = (_port_array_attr_t *)ptr;
conf_object_t **port_obj_ptr_base = (conf_object_t **)(
(uintptr_t)obj + port->port_obj_base_offset);
for (uint32 i = 0; i < port->array_size; i++) {
conf_object_t *port_obj = port_obj_ptr_base[i];
attr_value_t val = SIM_attr_list_item(*vals, i);
set_error_t err = port->set(NULL, port_obj, &val, NULL);
if (err != Sim_Set_Ok) {
return err;
}
}
return Sim_Set_Ok;
}
// port_obj_offset is the offset within the device struct of the first pointer
// to a port object. Remaining port objects are stored consecutively in memory.
UNUSED static void
_register_port_array_attr(conf_class_t *devcls, conf_class_t *portcls,
ptrdiff_t port_obj_offset,
uint32 array_size, bool is_bank,
const char *portname, const char *attrname,
get_attr_t getter, set_attr_t setter,
attr_attr_t attr, const char *type, const char *desc)
{
_register_port_attr_no_aux(portcls, attrname,
getter, setter, attr, type, desc);
_port_array_attr_t *data = MM_MALLOC(1, _port_array_attr_t);
data->port_obj_base_offset = port_obj_offset;
data->get = getter;
data->set = setter;
data->array_size = array_size;
char name[strlen(portname) + strlen(attrname) + 2];
sprintf(name, "%s_%s", portname, attrname);
strbuf_t proxy_desc = sb_newf("Proxy attribute for %s.%s[].%s",
is_bank ? "bank" : "port",
portname, attrname);
strbuf_t proxy_type = sb_newf("[%s{%d}]", type, array_size);
SIM_register_typed_attribute(
devcls, name,
getter ? _getattr_from_portobj_array : NULL, data,
setter ? _setattr_from_portobj_array : NULL, data,
(attr_attr_t)(Sim_Attr_Pseudo | Sim_Attr_Internal),
sb_str(&proxy_type), NULL, sb_str(&proxy_desc));
sb_free(&proxy_type);
sb_free(&proxy_desc);
}
typedef struct {
char *bufs[4];
unsigned char i;
} dml_qname_cache_t;
#include <stdarg.h>
UNUSED PRINTF_FORMAT(2, 3) static const char *
__qname(dml_qname_cache_t *cache, const char *fmt, ...)
{
va_list va;
char *s;
/* rotate between four buffers */
if (!cache->bufs[cache->i])
cache->bufs[cache->i] = MM_MALLOC(256, char);
s = cache->bufs[cache->i];
cache->i = (cache->i + 1) % 4;
va_start(va, fmt);
vsnprintf(s, 256, fmt, va);
va_end(va);
return (const char *)s;
}
static inline int
DML_pointer_eq(lang_void *data, lang_void *match_data)
{ return data == match_data; }
typedef bool (*_dml_reg_read_t)(void *dev, const uint16 *idx,
uint64 *result);
typedef bool (*_dml_reg_write_t)(void *dev, const uint16 *idx, uint64 val);
typedef struct {
const char *name;
unsigned dim;
_dml_reg_read_t read;
_dml_reg_write_t write;
} _dml_reg_t;
typedef struct {
int64 num;
uint32 reg;
const uint16 *idx;
} _dml_reg_number_t;
static inline const _dml_reg_number_t *
_DML_find_regnum(const _dml_reg_number_t *map, uint32 maplen, uint32 regnum)
{
uint32 i;
for (i = 0; i < maplen; i++) {
if (map[i].num == regnum)
return &map[i];
}
return NULL;
}
/* returns a static string */
static const char *
_DML_regname_unindexed(const _dml_reg_t *regs, const _dml_reg_number_t *map)
{
const _dml_reg_t *reg = ®s[map->reg];
if (reg->dim == 0)
return reg->name;
return NULL;
}
/* returns an allocated string to be freed by the caller */
static inline char *
_DML_regname_indexed(const _dml_reg_t *regs, const _dml_reg_number_t *map)
{
const _dml_reg_t *reg = ®s[map->reg];
strbuf_t name = SB_INIT;
const char *src = reg->name;
const uint16 *idxp = map->idx;
for (int dim = reg->dim; dim > 0; dim--) {
while (*src != '[')
sb_addc(&name, *src++);
src++;
sb_addfmt(&name, "[%u", *idxp++);
}
sb_addstr(&name, src);
return sb_detach(&name);
}
static inline const char *
_DML_make_regname(const _dml_reg_t *regs, const _dml_reg_number_t *map)
{
if (!map)
return NULL;
const char *name = _DML_regname_unindexed(regs, map);
if (name)
return name; /* statically allocated */
else {
/* Here we leak memory! We should probably put it in a
per-instance hash table instead. See bug 16901. */
return _DML_regname_indexed(regs, map);
}
}
static inline const _dml_reg_number_t *
_DML_find_regname(const _dml_reg_number_t *map, int maplen,
const char *name, const _dml_reg_t *regs)