-
Notifications
You must be signed in to change notification settings - Fork 861
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ADD: a basic BTM N-trace spec compliant trace encoder model #1824
Open
iansseijelly
wants to merge
19
commits into
riscv-software-src:master
Choose a base branch
from
iansseijelly:n_trace
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 13 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
f6e9ffa
ADD: working n-trace encoder
iansseijelly ffb81a9
ADD: formatting and debug utilities
iansseijelly 0ed1088
FIX: correct icnt
iansseijelly b49d258
FIX: memory leak and incorrect encoding for indirect branches
iansseijelly 810d618
FORMAT: indentation fix
iansseijelly 200dee9
FIX: adding compressed instruction check
iansseijelly 369dd65
ADD: golden reference trace for debugging
iansseijelly 62fc95b
FIX: fully correctly decode
iansseijelly 5818fd5
FIX: major flattening
iansseijelly 6e2b5b4
FORMAT: imports
iansseijelly b3d8471
ADD: --trace command line option
iansseijelly ca7b2b7
FIX: removing excess prints
iansseijelly 1c8e63a
FIX: remove unecessary constructor
iansseijelly 9a9b572
FORMAT: cosmetics
iansseijelly 4124ecc
FORMAT: remove unused indirection
iansseijelly 08e4542
FORMAT: more cosmetics
iansseijelly b5bfcec
ADD: redo inferable jump for spec compliance
iansseijelly dac6f34
ADD: add timestamps
iansseijelly 8a60abc
FIX: use delta compressed timestamp
iansseijelly File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
#include "trace_encoder_n.h" | ||
|
||
iansseijelly marked this conversation as resolved.
Show resolved
Hide resolved
|
||
void trace_encoder_n::reset() { | ||
this->state = TRACE_ENCODER_N_IDLE; | ||
this->icnt = 0; | ||
this->packet_0 = hart_to_encoder_ingress_t(); // create empty packet | ||
this->packet_1 = hart_to_encoder_ingress_t(); // create empty packet | ||
this->enabled = false; | ||
this->active = true; | ||
} | ||
|
||
void trace_encoder_n::set_enable(bool enabled) { | ||
bool was_enabled = this->enabled; | ||
this->enabled = enabled; | ||
if (!was_enabled && enabled) { | ||
this->state = TRACE_ENCODER_N_IDLE; | ||
} | ||
} | ||
|
||
void trace_encoder_n::trace_encoder_push_commit(hart_to_encoder_ingress_t packet) { | ||
this->packet_1 = this->packet_0; | ||
this->packet_0 = packet; | ||
if (this->enabled) { | ||
fprintf(this->debug_reference, "%lx\n", packet.i_addr); | ||
if (this->state == TRACE_ENCODER_N_IDLE) { | ||
trace_encoder_generate_packet(TCODE_PROG_TRACE_SYNC); | ||
this->state = TRACE_ENCODER_N_DATA; | ||
this->icnt += _get_packet_0()->ilastsize; | ||
} else if (this->state == TRACE_ENCODER_N_DATA) { | ||
this->icnt += _get_packet_0()->ilastsize; | ||
if (_get_packet_1()->i_type == I_BRANCH_TAKEN) { | ||
trace_encoder_generate_packet(TCODE_DBR); | ||
this->icnt = _get_packet_0()->ilastsize; | ||
} else if (_get_packet_1()->i_type == I_JUMP_INFERABLE || _get_packet_1()->i_type == I_JUMP_UNINFERABLE) { | ||
trace_encoder_generate_packet(TCODE_IBR); | ||
this->icnt = _get_packet_0()->ilastsize; | ||
} | ||
this->state = this->icnt >= MAX_ICNT ? TRACE_ENCODER_N_FULL : TRACE_ENCODER_N_DATA; | ||
} else if (this->state == TRACE_ENCODER_N_FULL) { | ||
trace_encoder_generate_packet(TCODE_FULL); | ||
this->state = TRACE_ENCODER_N_DATA; | ||
this->icnt = 0; | ||
} | ||
} | ||
} | ||
|
||
void print_packet(trace_encoder_n_packet_t* packet) { | ||
printf("[trace_encoder_n] printing packet: tcode: %lx, src: %lx, icnt: %lx, f_addr: %lx, u_addr: %lx, b_type: %lx\n", packet->tcode, packet->src, packet->icnt, packet->f_addr, packet->u_addr, packet->b_type); | ||
} | ||
|
||
void print_encoded_packet(uint8_t* buffer, int num_bytes) { | ||
printf("[trace_encoder_n] encoded packet: "); | ||
for (int i = 0; i < num_bytes; i++) { | ||
printf("%lx ", buffer[i]); | ||
} | ||
printf("\n"); | ||
} | ||
|
||
void trace_encoder_n::trace_encoder_generate_packet(tcode_t tcode) { | ||
trace_encoder_n_packet_t packet; | ||
int num_bytes; | ||
switch (tcode) { | ||
case TCODE_PROG_TRACE_SYNC: | ||
_set_program_trace_sync_packet(&packet); | ||
// print_packet(&packet); | ||
num_bytes = packet_to_buffer(&packet); | ||
fwrite(this->buffer, 1, num_bytes, this->trace_sink); | ||
// print_encoded_packet(this->buffer, num_bytes); | ||
break; | ||
case TCODE_DBR: | ||
_set_direct_branch_packet(&packet); | ||
// print_packet(&packet); | ||
num_bytes = packet_to_buffer(&packet); | ||
fwrite(this->buffer, 1, num_bytes, this->trace_sink); | ||
// print_encoded_packet(this->buffer, num_bytes); | ||
break; | ||
case TCODE_IBR: | ||
_set_indirect_branch_packet(&packet); | ||
// print_packet(&packet); | ||
num_bytes = packet_to_buffer(&packet); | ||
fwrite(this->buffer, 1, num_bytes, this->trace_sink); | ||
// print_encoded_packet(this->buffer, num_bytes); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
|
||
void trace_encoder_n::_set_program_trace_sync_packet(trace_encoder_n_packet_t* packet){ | ||
packet->tcode = TCODE_PROG_TRACE_SYNC; | ||
packet->src = this->src; | ||
packet->sync = SYNC_TRACE_EN; | ||
packet->icnt = 0; | ||
packet->f_addr = _get_packet_0()->i_addr >> 1; | ||
this->prev_addr = packet->f_addr; | ||
} | ||
|
||
void trace_encoder_n::_set_direct_branch_packet(trace_encoder_n_packet_t* packet){ | ||
packet->tcode = TCODE_DBR; | ||
packet->src = this->src; | ||
packet->icnt = this->icnt - _get_packet_0()->ilastsize; | ||
} | ||
|
||
void trace_encoder_n::_set_indirect_branch_packet(trace_encoder_n_packet_t* packet){ | ||
packet->tcode = TCODE_IBR; | ||
packet->src = this->src; | ||
packet->b_type = B_INDIRECT; | ||
packet->icnt = this->icnt - _get_packet_0()->ilastsize; | ||
uint64_t e_addr = _get_packet_0()->i_addr >> 1; | ||
packet->u_addr = e_addr ^ this->prev_addr; | ||
this->prev_addr = e_addr; | ||
} | ||
|
||
// returns the number of bytes written to the buffer | ||
int trace_encoder_n::packet_to_buffer(trace_encoder_n_packet_t* packet){ | ||
switch (packet->tcode) { | ||
case TCODE_PROG_TRACE_SYNC: | ||
return _packet_to_buffer_program_trace_sync(packet); | ||
case TCODE_DBR: | ||
return _packet_to_buffer_direct_branch_packet(packet); | ||
case TCODE_IBR: | ||
return _packet_to_buffer_indirect_branch_packet(packet); | ||
default: | ||
break; | ||
} | ||
} | ||
|
||
int trace_encoder_n::_packet_to_buffer_program_trace_sync(trace_encoder_n_packet_t* packet) { | ||
int msb = find_msb(packet->f_addr); | ||
this->buffer[0] = packet->tcode << MDO_OFFSET | MSEO_IDLE; | ||
this->buffer[1] = packet->sync << MDO_OFFSET | MSEO_IDLE; | ||
this->buffer[1] |= (packet->f_addr & 0b11) << 6; | ||
int num_bytes = 0; | ||
if (msb < 2) { | ||
this->buffer[1] |= MSEO_LAST; | ||
} else { | ||
packet->f_addr >>= 2; | ||
msb -= 2; | ||
num_bytes = ceil_div(msb, 6); | ||
for (int iter = 0; iter < num_bytes; iter++) { | ||
this->buffer[2 + iter] = ((packet->f_addr & 0x3F) << MDO_OFFSET) | MSEO_IDLE; | ||
packet->f_addr >>= 6; | ||
} | ||
this->buffer[2 + num_bytes - 1] |= MSEO_LAST; | ||
} | ||
return 2 + num_bytes; | ||
} | ||
|
||
int trace_encoder_n::_packet_to_buffer_direct_branch_packet(trace_encoder_n_packet_t* packet) { | ||
this->buffer[0] = packet->tcode << MDO_OFFSET | MSEO_IDLE; | ||
int msb = find_msb(packet->icnt); | ||
int num_bytes = ceil_div(msb, 6); | ||
for (int iter = 0; iter < num_bytes; iter++) { | ||
this->buffer[1 + iter] = ((packet->icnt & 0x3F) << MDO_OFFSET) | MSEO_IDLE; | ||
packet->icnt >>= 6; | ||
} | ||
this->buffer[1 + num_bytes - 1] |= MSEO_LAST; | ||
return 1 + num_bytes; | ||
} | ||
|
||
int trace_encoder_n::_packet_to_buffer_indirect_branch_packet(trace_encoder_n_packet_t* packet) { | ||
this->buffer[0] = packet->tcode << MDO_OFFSET | MSEO_IDLE; | ||
this->buffer[1] = packet->b_type << MDO_OFFSET | MSEO_IDLE; | ||
// icnt | ||
int icnt_msb = find_msb(packet->icnt); | ||
int icnt_num_bytes = 0; | ||
this->buffer[1] |= (packet->icnt & 0xF) << 4; | ||
if (icnt_msb < 4) { | ||
this->buffer[1] |= MSEO_EOF; | ||
} else { | ||
packet->icnt >>= 4; | ||
icnt_msb -= 4; | ||
icnt_num_bytes = ceil_div(icnt_msb, 6); | ||
for (int iter = 0; iter < icnt_num_bytes; iter++) { | ||
this->buffer[2 + iter] = ((packet->icnt & 0x3F) << MDO_OFFSET) | MSEO_IDLE; | ||
packet->icnt >>= 6; | ||
} | ||
this->buffer[2 + icnt_num_bytes - 1] |= MSEO_EOF; | ||
} | ||
// u_addr | ||
int u_addr_start = 2 + icnt_num_bytes; | ||
int u_addr_msb = find_msb(packet->u_addr); | ||
int u_addr_num_bytes = ceil_div(u_addr_msb, 6); | ||
for (int iter = 0; iter < u_addr_num_bytes; iter++) { | ||
this->buffer[u_addr_start + iter] = ((packet->u_addr & 0x3F) << MDO_OFFSET) | MSEO_IDLE; | ||
packet->u_addr >>= 6; | ||
} | ||
this->buffer[u_addr_start + u_addr_num_bytes - 1] |= MSEO_LAST; | ||
return u_addr_start + u_addr_num_bytes; | ||
} | ||
|
||
// returns the 0-index of the most significant bit | ||
int find_msb(uint64_t x) { | ||
if (x == 0) return -1; | ||
return 63 - __builtin_clzll(x); | ||
} | ||
|
||
// returns the ceiling of the division of a 0-indexed a by b | ||
int ceil_div(int a, int b) { | ||
return a / b + 1; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This
priv
should be fixed.Should this
trace_encoder_push_commit
also be added to thetake_trap
andtake_interrupt
methods?