Skip to content

Commit a7ee4d6

Browse files
rwalkracmel
authored andcommitted
perf cs-etm: Support for ARM A32/T32 instruction sets in CoreSight trace
This patch adds support for generating instruction samples from trace of AArch32 programs using the A32 and T32 instruction sets. T32 has variable 2 or 4 byte instruction size, so the conversion between addresses and instruction counts requires extra information from the trace decoder, requiring version 0.10.0 of OpenCSD. A check for the OpenCSD library version has been added to the feature check for OpenCSD. Signed-off-by: Robert Walker <robert.walker@arm.com> Reviewed-by: Mathieu Poirier <mathieu.poirier@linaro.org> Tested-by: Leo Yan <leo.yan@linaro.org> Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com> Cc: Jiri Olsa <jolsa@redhat.com> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: coresight@lists.linaro.org Cc: linux-arm-kernel@lists.infradead.org Link: http://lkml.kernel.org/r/1543839526-30348-1-git-send-email-robert.walker@arm.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent 0087976 commit a7ee4d6

4 files changed

Lines changed: 78 additions & 39 deletions

File tree

‎tools/build/feature/test-libopencsd.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
// SPDX-License-Identifier: GPL-2.0
22
#include <opencsd/c_api/opencsd_c_api.h>
33

4+
/*
5+
* Check OpenCSD library version is sufficient to provide required features
6+
*/
7+
#define OCSD_MIN_VER ((0 << 16) | (10 << 8) | (0))
8+
#if !defined(OCSD_VER_NUM) || (OCSD_VER_NUM < OCSD_MIN_VER)
9+
#error "OpenCSD >= 0.10.0 is required"
10+
#endif
11+
412
int main(void)
513
{
614
(void)ocsd_get_version();

‎tools/perf/util/cs-etm-decoder/cs-etm-decoder.c‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,12 @@ static void cs_etm_decoder__clear_buffer(struct cs_etm_decoder *decoder)
263263
decoder->tail = 0;
264264
decoder->packet_count = 0;
265265
for (i = 0; i < MAX_BUFFER; i++) {
266+
decoder->packet_buffer[i].isa = CS_ETM_ISA_UNKNOWN;
266267
decoder->packet_buffer[i].start_addr = CS_ETM_INVAL_ADDR;
267268
decoder->packet_buffer[i].end_addr = CS_ETM_INVAL_ADDR;
269+
decoder->packet_buffer[i].instr_count = 0;
268270
decoder->packet_buffer[i].last_instr_taken_branch = false;
271+
decoder->packet_buffer[i].last_instr_size = 0;
269272
decoder->packet_buffer[i].exc = false;
270273
decoder->packet_buffer[i].exc_ret = false;
271274
decoder->packet_buffer[i].cpu = INT_MIN;
@@ -294,11 +297,15 @@ cs_etm_decoder__buffer_packet(struct cs_etm_decoder *decoder,
294297
decoder->packet_count++;
295298

296299
decoder->packet_buffer[et].sample_type = sample_type;
300+
decoder->packet_buffer[et].isa = CS_ETM_ISA_UNKNOWN;
297301
decoder->packet_buffer[et].exc = false;
298302
decoder->packet_buffer[et].exc_ret = false;
299303
decoder->packet_buffer[et].cpu = *((int *)inode->priv);
300304
decoder->packet_buffer[et].start_addr = CS_ETM_INVAL_ADDR;
301305
decoder->packet_buffer[et].end_addr = CS_ETM_INVAL_ADDR;
306+
decoder->packet_buffer[et].instr_count = 0;
307+
decoder->packet_buffer[et].last_instr_taken_branch = false;
308+
decoder->packet_buffer[et].last_instr_size = 0;
302309

303310
if (decoder->packet_count == MAX_BUFFER - 1)
304311
return OCSD_RESP_WAIT;
@@ -321,8 +328,28 @@ cs_etm_decoder__buffer_range(struct cs_etm_decoder *decoder,
321328

322329
packet = &decoder->packet_buffer[decoder->tail];
323330

331+
switch (elem->isa) {
332+
case ocsd_isa_aarch64:
333+
packet->isa = CS_ETM_ISA_A64;
334+
break;
335+
case ocsd_isa_arm:
336+
packet->isa = CS_ETM_ISA_A32;
337+
break;
338+
case ocsd_isa_thumb2:
339+
packet->isa = CS_ETM_ISA_T32;
340+
break;
341+
case ocsd_isa_tee:
342+
case ocsd_isa_jazelle:
343+
case ocsd_isa_custom:
344+
case ocsd_isa_unknown:
345+
default:
346+
packet->isa = CS_ETM_ISA_UNKNOWN;
347+
}
348+
324349
packet->start_addr = elem->st_addr;
325350
packet->end_addr = elem->en_addr;
351+
packet->instr_count = elem->num_instr_range;
352+
326353
switch (elem->last_i_type) {
327354
case OCSD_INSTR_BR:
328355
case OCSD_INSTR_BR_INDIRECT:
@@ -336,6 +363,8 @@ cs_etm_decoder__buffer_range(struct cs_etm_decoder *decoder,
336363
break;
337364
}
338365

366+
packet->last_instr_size = elem->last_instr_sz;
367+
339368
return ret;
340369
}
341370

‎tools/perf/util/cs-etm-decoder/cs-etm-decoder.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,21 @@ enum cs_etm_sample_type {
2828
CS_ETM_TRACE_ON = 1 << 1,
2929
};
3030

31+
enum cs_etm_isa {
32+
CS_ETM_ISA_UNKNOWN,
33+
CS_ETM_ISA_A64,
34+
CS_ETM_ISA_A32,
35+
CS_ETM_ISA_T32,
36+
};
37+
3138
struct cs_etm_packet {
3239
enum cs_etm_sample_type sample_type;
40+
enum cs_etm_isa isa;
3341
u64 start_addr;
3442
u64 end_addr;
43+
u32 instr_count;
3544
u8 last_instr_taken_branch;
45+
u8 last_instr_size;
3646
u8 exc;
3747
u8 exc_ret;
3848
int cpu;

‎tools/perf/util/cs-etm.c‎

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,6 @@
3131

3232
#define MAX_TIMESTAMP (~0ULL)
3333

34-
/*
35-
* A64 instructions are always 4 bytes
36-
*
37-
* Only A64 is supported, so can use this constant for converting between
38-
* addresses and instruction counts, calculting offsets etc
39-
*/
40-
#define A64_INSTR_SIZE 4
41-
4234
struct cs_etm_auxtrace {
4335
struct auxtrace auxtrace;
4436
struct auxtrace_queues queues;
@@ -510,21 +502,17 @@ static inline void cs_etm__reset_last_branch_rb(struct cs_etm_queue *etmq)
510502
etmq->last_branch_rb->nr = 0;
511503
}
512504

513-
static inline u64 cs_etm__last_executed_instr(struct cs_etm_packet *packet)
514-
{
515-
/* Returns 0 for the CS_ETM_TRACE_ON packet */
516-
if (packet->sample_type == CS_ETM_TRACE_ON)
517-
return 0;
505+
static inline int cs_etm__t32_instr_size(struct cs_etm_queue *etmq,
506+
u64 addr) {
507+
u8 instrBytes[2];
518508

509+
cs_etm__mem_access(etmq, addr, ARRAY_SIZE(instrBytes), instrBytes);
519510
/*
520-
* The packet records the execution range with an exclusive end address
521-
*
522-
* A64 instructions are constant size, so the last executed
523-
* instruction is A64_INSTR_SIZE before the end address
524-
* Will need to do instruction level decode for T32 instructions as
525-
* they can be variable size (not yet supported).
511+
* T32 instruction size is indicated by bits[15:11] of the first
512+
* 16-bit word of the instruction: 0b11101, 0b11110 and 0b11111
513+
* denote a 32-bit instruction.
526514
*/
527-
return packet->end_addr - A64_INSTR_SIZE;
515+
return ((instrBytes[1] & 0xF8) >= 0xE8) ? 4 : 2;
528516
}
529517

530518
static inline u64 cs_etm__first_executed_instr(struct cs_etm_packet *packet)
@@ -536,27 +524,32 @@ static inline u64 cs_etm__first_executed_instr(struct cs_etm_packet *packet)
536524
return packet->start_addr;
537525
}
538526

539-
static inline u64 cs_etm__instr_count(const struct cs_etm_packet *packet)
527+
static inline
528+
u64 cs_etm__last_executed_instr(const struct cs_etm_packet *packet)
540529
{
541-
/*
542-
* Only A64 instructions are currently supported, so can get
543-
* instruction count by dividing.
544-
* Will need to do instruction level decode for T32 instructions as
545-
* they can be variable size (not yet supported).
546-
*/
547-
return (packet->end_addr - packet->start_addr) / A64_INSTR_SIZE;
530+
/* Returns 0 for the CS_ETM_TRACE_ON packet */
531+
if (packet->sample_type == CS_ETM_TRACE_ON)
532+
return 0;
533+
534+
return packet->end_addr - packet->last_instr_size;
548535
}
549536

550-
static inline u64 cs_etm__instr_addr(const struct cs_etm_packet *packet,
537+
static inline u64 cs_etm__instr_addr(struct cs_etm_queue *etmq,
538+
const struct cs_etm_packet *packet,
551539
u64 offset)
552540
{
553-
/*
554-
* Only A64 instructions are currently supported, so can get
555-
* instruction address by muliplying.
556-
* Will need to do instruction level decode for T32 instructions as
557-
* they can be variable size (not yet supported).
558-
*/
559-
return packet->start_addr + offset * A64_INSTR_SIZE;
541+
if (packet->isa == CS_ETM_ISA_T32) {
542+
u64 addr = packet->start_addr;
543+
544+
while (offset > 0) {
545+
addr += cs_etm__t32_instr_size(etmq, addr);
546+
offset--;
547+
}
548+
return addr;
549+
}
550+
551+
/* Assume a 4 byte instruction size (A32/A64) */
552+
return packet->start_addr + offset * 4;
560553
}
561554

562555
static void cs_etm__update_last_branch_rb(struct cs_etm_queue *etmq)
@@ -888,9 +881,8 @@ static int cs_etm__sample(struct cs_etm_queue *etmq)
888881
struct cs_etm_auxtrace *etm = etmq->etm;
889882
struct cs_etm_packet *tmp;
890883
int ret;
891-
u64 instrs_executed;
884+
u64 instrs_executed = etmq->packet->instr_count;
892885

893-
instrs_executed = cs_etm__instr_count(etmq->packet);
894886
etmq->period_instructions += instrs_executed;
895887

896888
/*
@@ -920,7 +912,7 @@ static int cs_etm__sample(struct cs_etm_queue *etmq)
920912
* executed, but PC has not advanced to next instruction)
921913
*/
922914
u64 offset = (instrs_executed - instrs_over - 1);
923-
u64 addr = cs_etm__instr_addr(etmq->packet, offset);
915+
u64 addr = cs_etm__instr_addr(etmq, etmq->packet, offset);
924916

925917
ret = cs_etm__synth_instruction_sample(
926918
etmq, addr, etm->instructions_sample_period);

0 commit comments

Comments
 (0)