forked from qemu/qemu
-
Notifications
You must be signed in to change notification settings - Fork 23
/
csky-trace.c
619 lines (577 loc) · 18.4 KB
/
csky-trace.c
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
/*
* CSKY trace
*
* Copyright (c) 2011-2019 C-SKY Limited. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#include "qemu/osdep.h"
#include "cpu.h"
#include "exec/tracestub.h"
#include "exec/helper-proto.h"
extern bool is_gdbserver_start;
static uint32_t csky_trace_insn_seg;
static uint32_t csky_trace_data_seg;
#ifdef TARGET_RISCV
bool csky_trace_elf_start;
#endif
inline bool gen_mem_trace(void)
{
if (tfilter.enable) {
#ifdef TARGET_RISCV
if (!csky_trace_elf_start) {
return false;
}
#endif
if (tfilter.event & TRACE_EVENT_DATA) {
return true;
}
}
return false;
}
inline bool gen_tb_trace(void)
{
if (tfilter.enable) {
if (tfilter.event & TRACE_EVENT_INSN) {
return true;
}
}
return false;
}
inline bool gen_x_vf_trace(void)
{
if (tfilter.enable) {
if (tfilter.event & TRACE_EVENT_X_VF) {
return true;
}
}
return false;
}
inline bool gen_x_lmul_trace(void)
{
if (tfilter.enable) {
if (tfilter.event & TRACE_EVENT_X_LMUL) {
return true;
}
}
return false;
}
static inline uint32_t csky_trace_get_addr_base(target_ulong addr)
{
if (sizeof(target_ulong) == 8) {
return (uint64_t)addr >> 32;
}
return 0;
}
static inline uint32_t csky_trace_get_addr_offset(target_ulong addr)
{
if (sizeof(target_ulong) == 8) {
return extract64(addr, 0, 32);
}
return addr;
}
static inline void csky_trace_send_base(uint32_t *base, uint8_t type,
target_ulong addr)
{
int packlen = 0;
uint32_t addr_base;
packlen = 2 * sizeof(uint8_t) + sizeof(uint32_t);
addr_base = csky_trace_get_addr_base(addr);
if (addr_base != *base) {
*base = addr_base;
write_trace_8_8(type, packlen, sizeof(uint8_t), addr_base);
}
return;
}
static bool stsp_range_match(target_ulong pc, target_ulong smask)
{
struct trace_range *tr = NULL;
int i;
if (tfilter.stsp_num == 0) {
if (smask == 0) {
return true;
} else {
return false;
}
}
for (i = 0; i < tfilter.stsp_num; i++) {
tr = &tfilter.stsp_range[i];
if (smask != 0) {
if ((tr->start & smask ) == pc) {
tr->start = pc;
return true;
} else if ((tr->end & smask) ==pc) {
tr->end = pc;
return true;
} else {
return false;
}
}
if (tfilter.sstsp == STSP_EXIT) {
if (tr->start == pc) {
tfilter.sstsp = STSP_START;
return true;
}
return false;
} else if (tfilter.sstsp == STSP_START) {
if (tr->end == pc) {
tfilter.sstsp = STSP_EXIT;
return false;
}
return true;
} else {
return false;
}
}
return false;
}
static bool addr_range_match(target_ulong pc, target_ulong smask)
{
struct trace_range *tr = NULL;
int i;
if (tfilter.addr_num == 0) {
if (smask == 0) {
return true;
} else {
return false;
}
}
for (i = 0; i < tfilter.addr_num; i++) {
tr = &tfilter.addr_range[i];
if (smask != 0) {
if ((tr->start & smask ) == pc) {
tr->start = pc;
return true;
} else if ((tr->end & smask) == pc) {
tr->end = pc;
return true;
} else {
return false;
}
}
if ((tr->start <= pc) && (tr->end > pc)) {
return true;
}
}
return false;
}
#ifdef TARGET_CSKY
static bool data_range_match(target_ulong pc, target_ulong value)
{
struct trace_data *td = NULL;
struct trace_range *tr = NULL;
int i;
if (tfilter.data_num == 0) {
return true;
}
for (i = 0; i < tfilter.data_num; i++) {
td = &tfilter.data_range[i];
tr = &td->data_addr;
if ((tr->start <= pc) && (tr->end > pc)) {
if (td->mode == TRACE_VALUE_SINGLE) {
if (td->min == value) {
return true;
}
} else if (td->mode == TRACE_VALUE_RANGE) {
if ((td->min <= value) && (td->max > value)) {
return true;
}
}
}
}
return false;
}
#endif
bool trace_range_test(void *cpu, uint32_t pc, uint32_t smask)
{
bool result = false;
result = stsp_range_match(pc, smask);
result |= addr_range_match(pc, smask);
return result;
}
static int addr_trace_filter(CPUArchState *env, target_ulong pc)
{
int result = 0;
#ifdef TARGET_CSKY
if (tfilter.enable) {
if (tfilter.asid & TRACE_ASID_ENABLE_MASK) {
if (ENV_GET_MMU(env)) {
if (tfilter.asid != ENV_GET_ASID(env)) {
return result;
}
}
}
if (stsp_range_match(pc, 0)) {
result |= STSP_RANGE_MATCH;
if (addr_range_match(pc, 0)) {
result |= ADDR_RANGE_MATCH;
}
}
}
#elif TARGET_RISCV
if (tfilter.enable) {
if (stsp_range_match(pc, 0)) {
result |= STSP_RANGE_MATCH;
}
result |= ADDR_RANGE_MATCH;
}
#endif
return result;
}
static int data_trace_filter(CPUArchState *env,
target_ulong pc, target_ulong addr, target_ulong value)
{
int result = 0;
#ifdef TARGET_CSKY
if (tfilter.enable) {
if (tfilter.asid & TRACE_ASID_ENABLE_MASK) {
if (ENV_GET_MMU(env)) {
if (tfilter.asid != ENV_GET_ASID(env)) {
return result;
}
}
}
if (stsp_range_match(pc, 0)) {
result |= STSP_RANGE_MATCH;
if (addr_range_match(addr, 0)) {
result |= ADDR_RANGE_MATCH;
}
if (data_range_match(addr, value)) {
result |= DATA_RANGE_MATCH;
}
}
}
#else
result |= ADDR_RANGE_MATCH | DATA_RANGE_MATCH;
#endif
return result;
}
//#define CSKY_TRACE_DEBUG
#ifdef CSKY_TRACE_DEBUG
static int sync_trace_count;
#endif
void helper_csky_trace_icount(CPUArchState *env, target_ulong tb_pc, uint32_t icount)
{
static long long total_csky_trace_icount;
static long long last_csky_trace_icount;
total_csky_trace_icount += icount;
if ((total_csky_trace_icount - last_csky_trace_icount) > INSN_PER_PACKET) {
#ifdef CSKY_TRACE_DEBUG
if (sync_trace_count % 100 == 0) {
fprintf(stderr, ".");
fflush(stderr);
}
sync_trace_count++;
#endif
trace_send();
last_csky_trace_icount = total_csky_trace_icount;
}
if (tfilter.enable) {
if (tfilter.event & TRACE_EVENT_INSN) {
int result = addr_trace_filter(env, tb_pc);
if (result & STSP_RANGE_MATCH) {
csky_trace_icount += icount;
}
}
}
}
void helper_trace_tb_start(CPUArchState *env, target_ulong tb_pc)
{
static int8_t lastbase;
int8_t base = (tb_pc >> 24) & 0xff;
int32_t offset = tb_pc & 0xffffff;
int result = addr_trace_filter(env, tb_pc);
#ifdef TARGET_RISCV
if (!csky_trace_elf_start) {
if (tb_pc == env->elf_start) {
csky_trace_elf_start = true;
}
}
if (!csky_trace_elf_start) {
return;
}
#endif
if (result & STSP_RANGE_MATCH) {
csky_trace_send_base(&csky_trace_insn_seg, INST_SEG, tb_pc);
env->last_pc = tb_pc;
if (base != lastbase) {
write_trace_8(INST_BASE, 2 * sizeof(uint8_t), base);
lastbase = base;
}
write_trace_8(INST_OFFSET, sizeof(uint32_t), offset);
if (result & ADDR_RANGE_MATCH) {
//write_trace_8(ADDR_CMPR_MATCH, sizeof(uint32_t), offset);
}
}
}
void helper_trace_tb_exit(uint32_t subtype, uint32_t offset)
{
#ifdef TARGET_RISCV
if (!csky_trace_elf_start) {
return;
}
#endif
write_trace_8_8(INST_EXIT, sizeof(uint32_t), subtype, offset);
if (is_gdbserver_start) {
trace_send_immediately();
}
}
void extern_helper_trace_tb_exit(uint32_t subtype, uint32_t offset)
{
helper_trace_tb_exit(subtype, offset);
}
static void helper_trace_ldst(CPUArchState *env, target_ulong pc,
target_ulong rz, target_ulong addr, int type)
{
int packlen = 0;
int result = data_trace_filter(env, pc, addr, rz);
packlen = 2 * sizeof(uint8_t) + sizeof(uint32_t);
if (result & STSP_RANGE_MATCH) {
if (tfilter.proxy) {
write_trace_8(DATA_INST_OFFSET, sizeof(uint32_t), pc - env->last_pc);
}
csky_trace_send_base(&csky_trace_data_seg, DATA_SEG, addr);
addr = csky_trace_get_addr_offset(addr);
switch (type) {
case LD8U: case LD8S:
write_trace_8_8(DATA_RADDR, packlen, sizeof(uint8_t), addr);
write_trace_8_8(DATA_VALUE, packlen, 0, (uint32_t)rz);
raddr_num++;
break;
case LD16U: case LD16S:
write_trace_8_8(DATA_RADDR, packlen, sizeof(uint16_t), addr);
write_trace_8_8(DATA_VALUE, packlen, 0, (uint32_t)rz);
raddr_num++;
break;
case LD32U: case LD32S:
write_trace_8_8(DATA_RADDR, packlen, sizeof(uint32_t), addr);
write_trace_8_8(DATA_VALUE, packlen, 0, (uint32_t)rz);
raddr_num++;
break;
case LD64U: case LD64S:
write_trace_8_8(DATA_RADDR, packlen, sizeof(uint64_t), addr);
//write_trace_8_seq(DATA_VALUE, sizeof(uint64_t) + 1, (uint8_t *)&rz);
write_trace_8_8(DATA_VALUE, packlen, 0, extract64(rz, 0, 32));
write_trace_8_8(DATA_VALUE, packlen, 32, extract64(rz, 32, 32));
raddr_num++;
break;
case ST8:
write_trace_8_8(DATA_WADDR, packlen, sizeof(uint8_t), addr);
write_trace_8_8(DATA_VALUE, packlen, 0, (uint32_t)rz);
waddr_num++;
break;
case ST16:
write_trace_8_8(DATA_WADDR, packlen, sizeof(uint16_t), addr);
write_trace_8_8(DATA_VALUE, packlen, 0, (uint32_t)rz);
waddr_num++;
break;
case ST32:
write_trace_8_8(DATA_WADDR, packlen, sizeof(uint32_t), addr);
write_trace_8_8(DATA_VALUE, packlen, 0, (uint32_t)rz);
waddr_num++;
break;
case ST64:
write_trace_8_8(DATA_WADDR, packlen, sizeof(uint64_t), addr);
//write_trace_8_seq(DATA_VALUE, sizeof(uint64_t) + 1, (uint8_t*)&rz);
write_trace_8_8(DATA_VALUE, packlen, 0, extract64(rz, 0, 32));
write_trace_8_8(DATA_VALUE, packlen, 32, extract64(rz, 32, 32));
waddr_num++;
break;
default:
break;
}
if (result & ADDR_RANGE_MATCH) {
}
if (result & DATA_RANGE_MATCH) {
}
}
}
void helper_trace_ld8u(CPUArchState *env, target_ulong pc,
target_ulong rz, target_ulong addr)
{
helper_trace_ldst(env, pc, rz, addr, LD8U);
}
void helper_trace_ld16u(CPUArchState *env, target_ulong pc,
target_ulong rz, target_ulong addr)
{
helper_trace_ldst(env, pc, rz, addr, LD16U);
}
void helper_trace_ld32u(CPUArchState *env, target_ulong pc,
target_ulong rz, target_ulong addr)
{
helper_trace_ldst(env, pc, rz, addr, LD32U);
}
void helper_trace_ld64u(CPUArchState *env, target_ulong pc,
target_ulong rz, target_ulong addr)
{
helper_trace_ldst(env, pc, rz, addr, LD64U);
}
void helper_trace_ld8s(CPUArchState *env, target_ulong pc,
target_ulong rz, target_ulong addr)
{
helper_trace_ldst(env, pc, rz, addr, LD8S);
}
void helper_trace_ld16s(CPUArchState *env, target_ulong pc,
target_ulong rz, target_ulong addr)
{
helper_trace_ldst(env, pc, rz, addr, LD16S);
}
void helper_trace_ld32s(CPUArchState *env, target_ulong pc,
target_ulong rz, target_ulong addr)
{
helper_trace_ldst(env, pc, rz, addr, LD32S);
}
void helper_trace_ld64s(CPUArchState *env, target_ulong pc,
target_ulong rz, target_ulong addr)
{
helper_trace_ldst(env, pc, rz, addr, LD64S);
}
void helper_trace_st8(CPUArchState *env, target_ulong pc,
target_ulong rz, target_ulong addr)
{
helper_trace_ldst(env, pc, rz, addr, ST8);
}
void helper_trace_st16(CPUArchState *env, target_ulong pc,
target_ulong rz, target_ulong addr)
{
helper_trace_ldst(env, pc, rz, addr, ST16);
}
void helper_trace_st32(CPUArchState *env, target_ulong pc,
target_ulong rz, target_ulong addr)
{
helper_trace_ldst(env, pc, rz, addr, ST32);
}
void helper_trace_st64(CPUArchState *env, target_ulong pc,
target_ulong rz, target_ulong addr)
{
helper_trace_ldst(env, pc, rz, addr, ST64);
}
void helper_trace_m_addr(CPUArchState *env, target_ulong pc,
target_ulong addr, target_ulong num, uint32_t type)
{
int packlen = 0;
int result = addr_trace_filter(env, pc);
packlen = 2 * sizeof(uint8_t) + sizeof(uint32_t);
if (result & STSP_RANGE_MATCH) {
csky_trace_send_base(&csky_trace_data_seg, DATA_SEG, addr);
addr = csky_trace_get_addr_offset(addr);
write_trace_8_8(type, packlen, num * sizeof(uint32_t), addr);
if (type == 0x41) {
waddr_num++;
} else if (type == 0x40) {
raddr_num++;
}
if (result & ADDR_RANGE_MATCH) {
}
}
}
void helper_trace_m_value(CPUArchState *env, target_ulong pc,
target_ulong addr, target_ulong value)
{
int packlen = 0;
int result = data_trace_filter(env, pc, addr, value);
packlen = 2 * sizeof(uint8_t) + sizeof(uint32_t);
if (result & STSP_RANGE_MATCH) {
//write_trace_8_seq(DATA_VALUE, packlen, &value);
if (sizeof(target_ulong) == 64) {
write_trace_8_8(DATA_VALUE, packlen, 0, extract64(value, 0, 32));
write_trace_8_8(DATA_VALUE, packlen, 0, extract64(value, 32, 32));
} else {
write_trace_8_8(DATA_VALUE, packlen, 0, value);
}
if (result & ADDR_RANGE_MATCH) {
}
if (result & DATA_RANGE_MATCH) {
}
}
}
void helper_trace_update_tcr(CPUArchState *env)
{
int mode = -1;
int enable = 0;
uint32_t *addr_index = &tfilter.addr_num;
uint32_t *data_index = &tfilter.data_num;
uint32_t *stsp_index = &tfilter.stsp_num;
int value_index, value_mode, i;
CPUState *cpu = env_cpu(env);
/* TRCEn enable */
if ((env->cp13.tcr & 0x01) && (cpu->csky_trace_features & CSKY_TRACE)) {
tfilter.enable = true;
tfilter.event = env->cp13.ter; /* get all trace event */
/* first send trace header */
write_trace_header(env->cp13.ter);
if (!(cpu->csky_trace_features & MEM_TRACE)) {
tfilter.event &= ~(0x02);
}
if (!(cpu->csky_trace_features & TB_TRACE)) {
tfilter.event &= ~(0x01);
}
tfilter.asid = env->cp13.asid;
/* fill all range */
for (i = 0; i < MAX_ADDR_CMPR_NUM - 1; i++) {
mode = (env->cp13.addr_cmpr_config[i] & ADDR_CMPR_MODE_MASK)
>> ADDR_CMPR_MODE_START;
enable = env->cp13.addr_cmpr_config[i] & ADDR_CMPR_ENABLE_MASK;
if (enable) {
switch (mode) {
case INSN_ADDR_RANGE_CMPR:
case DATA_ADDR_RANGE_CMPR: /* addr range match */
tfilter.addr_range[*addr_index].start
= env->cp13.addr_cmpr[i];
tfilter.addr_range[*addr_index].end
= env->cp13.addr_cmpr[++i];
(*addr_index)++;
break;
case ASSOCIATE_VALUE_CMPR: /* associate value match */
value_index = env->cp13.addr_cmpr_config[i]
& ADDR_CMPR_DATA_MASK;
value_mode = env->cp13.data_cmpr_config[value_index]
& DATA_CMPR_MODE_MASK;
if (TRACE_VALUE_SINGLE == value_mode) {
tfilter.data_range[*data_index].mode
= value_mode;
tfilter.data_range[*data_index].min
= env->cp13.data_cmpr[value_index];
} else if (TRACE_VALUE_RANGE == value_mode) {
tfilter.data_range[*data_index].mode
= value_mode;
tfilter.data_range[*data_index].min
= env->cp13.data_cmpr[value_index];
tfilter.data_range[*data_index].max
= env->cp13.data_cmpr[value_index + 1];
} else {
return;
}
tfilter.data_range[*data_index].data_addr.start
= env->cp13.addr_cmpr[i];
tfilter.data_range[*data_index].data_addr.end
= env->cp13.addr_cmpr[++i];
(*data_index)++;
break;
case STSP_RANGE_CMPR: /* stsp range match */
tfilter.stsp_range[*stsp_index].start
= env->cp13.addr_cmpr[i];
tfilter.stsp_range[*stsp_index].end
= env->cp13.addr_cmpr[++i];
(*stsp_index)++;
break;
default:
break;
}
}
}
} else {
memset(&tfilter, 0, sizeof(tfilter));
}
}