libnexus-rv
trace-decoder.h
Go to the documentation of this file.
1 // SPDX-License-Identifier: Apache 2.0
2 /*
3  * trace-decoder.h - NexusRV Trace decoder functions and declarations
4  *
5  * Copyright (C) 2025, Bo Gan <ganboing@gmail.com>
6  */
7 
8 #ifndef LIBNEXUS_RV_TRACE_DECODER_H
9 #define LIBNEXUS_RV_TRACE_DECODER_H
10 
11 #include "msg-decoder.h"
12 #include "hist-array.h"
13 #include "return-stack.h"
14 
94 typedef struct nexusrv_trace_indirect {
95  uint64_t target;
96  uint64_t context;
98  struct {
99  uint8_t interrupt : 1;
100  uint8_t exception : 1;
101  uint8_t ownership : 1;
102  uint8_t ownership_fmt : 2;
103  uint8_t ownership_priv : 2;
104  uint8_t ownership_v : 1;
105  };
107 
110 typedef struct nexusrv_trace_sync {
111  uint64_t addr;
112  struct {
113  uint8_t sync : 4;
114  };
116 
119 typedef struct nexusrv_trace_error {
120  uint32_t ecode;
121  struct {
122  uint8_t etype : 4;
123  };
125 
128 typedef struct nexusrv_trace_stop {
129  uint8_t evcode : 4;
131 
135  NEXUSRV_Trace_Event_None,
136  NEXUSRV_Trace_Event_Direct,
137  NEXUSRV_Trace_Event_DirectSync,
138  NEXUSRV_Trace_Event_Trap,
139  NEXUSRV_Trace_Event_Indirect,
140  NEXUSRV_Trace_Event_IndirectSync,
141  NEXUSRV_Trace_Event_Sync,
142  NEXUSRV_Trace_Event_Stop,
143  NEXUSRV_Trace_Event_Error,
144 };
145 
146 static inline const char
147 *str_nexusrv_trace_event(unsigned event) {
148  switch (event) {
149  case NEXUSRV_Trace_Event_None:
150  return "none";
151  case NEXUSRV_Trace_Event_Direct:
152  return "direct";
153  case NEXUSRV_Trace_Event_DirectSync:
154  return "direct-sync";
155  case NEXUSRV_Trace_Event_Trap:
156  return "trap";
157  case NEXUSRV_Trace_Event_Indirect:
158  return "indirect";
159  case NEXUSRV_Trace_Event_IndirectSync:
160  return "indirect-sync";
161  case NEXUSRV_Trace_Event_Sync:
162  return "sync";
163  case NEXUSRV_Trace_Event_Stop:
164  return "stop";
165  case NEXUSRV_Trace_Event_Error:
166  return "error";
167  default:
168  return "unknown";
169  }
170 }
171 
172 
179 typedef struct nexusrv_trace_decoder {
182  struct nexusrv_hist_array* res_hists;
184  uint32_t res_icnt;
185  uint32_t res_tnts;
186  uint32_t consumed_icnt;
187  uint8_t consumed_tnts;
188  bool synced;
189  bool msg_present;
191  uint64_t full_addr;
192  uint64_t timestamp;
195 
209  nexusrv_msg_decoder *msg_decoder);
210 
218 
229 static inline uint64_t nexusrv_trace_time(nexusrv_trace_decoder* decoder) {
230  const nexusrv_hw_cfg *hwcfg = decoder->msg_decoder->hw_cfg;
231  uint64_t time = decoder->timestamp;
232  if (hwcfg->ts_bits < 64)
233  time &= (((uint64_t)1 << hwcfg->ts_bits) - 1);
234  if (hwcfg->timer_freq) {
235  unsigned __int128 normalized = time;
236  normalized *= 1000UL * 1000 * 1000;
237  time = normalized / hwcfg->timer_freq;
238  }
239  return time;
240 }
241 
248 
284  uint32_t icnt,
285  unsigned *event);
286 
303  nexusrv_trace_sync *sync);
304 
313 
314 int nexusrv_trace_push_call(nexusrv_trace_decoder* decoder,
315  uint64_t callsite);
316 
317 int nexusrv_trace_pop_ret(nexusrv_trace_decoder* decoder,
318  uint64_t *callsite);
319 
320 unsigned nexusrv_trace_callstack_used(nexusrv_trace_decoder* decoder);
321 
330  nexusrv_trace_indirect *indirect);
331 
340  nexusrv_trace_sync *sync);
341 
350  nexusrv_trace_error *error);
351 
360  nexusrv_trace_stop *stop);
361 
372  uint64_t timestamp);
373 
374 #endif
NexusRV Message decoder configuration.
Definition: msg-decoder.h:23
unsigned ts_bits
Definition: msg-decoder.h:25
uint64_t timer_freq
Definition: msg-decoder.h:28
NexusRV Message decoder context.
Definition: msg-decoder.h:39
const nexusrv_hw_cfg * hw_cfg
Definition: msg-decoder.h:40
Decoded NexusRV Message.
Definition: msg-types.h:102
Definition: return-stack.h:17
NexusRV Trace decoder context.
Definition: trace-decoder.h:179
uint64_t full_addr
Definition: trace-decoder.h:191
struct nexusrv_hist_array * res_hists
Definition: trace-decoder.h:182
nexusrv_msg msg
Definition: trace-decoder.h:190
uint32_t res_icnt
Definition: trace-decoder.h:184
uint8_t consumed_tnts
Definition: trace-decoder.h:187
nexusrv_return_stack return_stack
Definition: trace-decoder.h:193
uint32_t consumed_icnt
Definition: trace-decoder.h:186
bool msg_present
Definition: trace-decoder.h:189
uint64_t timestamp
Definition: trace-decoder.h:192
uint32_t res_tnts
Definition: trace-decoder.h:185
nexusrv_msg_decoder * msg_decoder
Definition: trace-decoder.h:180
bool synced
Definition: trace-decoder.h:188
NexusRV Trace Error Event.
Definition: trace-decoder.h:119
uint32_t ecode
Definition: trace-decoder.h:120
uint8_t etype
Definition: trace-decoder.h:122
NexusRV Trace Indirect Branch Event.
Definition: trace-decoder.h:94
uint8_t interrupt
Definition: trace-decoder.h:99
uint8_t ownership_priv
Definition: trace-decoder.h:103
uint8_t ownership_v
Definition: trace-decoder.h:104
uint8_t ownership
Definition: trace-decoder.h:100
uint64_t context
Definition: trace-decoder.h:96
uint8_t ownership_fmt
Definition: trace-decoder.h:102
uint64_t target
Definition: trace-decoder.h:95
NexusRV Stop Event.
Definition: trace-decoder.h:128
uint8_t evcode
Definition: trace-decoder.h:129
NexusRV Trace Sync Event.
Definition: trace-decoder.h:110
uint64_t addr
Definition: trace-decoder.h:111
uint8_t sync
Definition: trace-decoder.h:113
int nexusrv_trace_decoder_init(nexusrv_trace_decoder *decoder, nexusrv_msg_decoder *msg_decoder)
Initialize the trace decoder.
int32_t nexusrv_trace_try_retire(nexusrv_trace_decoder *decoder, uint32_t icnt, unsigned *event)
Try to retire icnt from trace.
int nexusrv_trace_next_tnt(nexusrv_trace_decoder *decoder)
Get the next taken-not-taken.
uint32_t nexusrv_trace_available_icnt(nexusrv_trace_decoder *decoder)
Get the available I-CNT before needing to fetch new messages.
int nexusrv_trace_sync_reset(nexusrv_trace_decoder *decoder, nexusrv_trace_sync *sync)
Synchronize the trace decoder.
struct nexusrv_trace_sync nexusrv_trace_sync
NexusRV Trace Sync Event.
nexusrv_trace_events
NexusRV trace events.
Definition: trace-decoder.h:134
void nexusrv_trace_add_timestamp(nexusrv_trace_decoder *decoder, uint64_t timestamp)
Add(retire) the timestamp to decoder.
void nexusrv_trace_decoder_fini(nexusrv_trace_decoder *decoder)
Finialize the trace decoder.
struct nexusrv_trace_error nexusrv_trace_error
NexusRV Trace Error Event.
int nexusrv_trace_next_stop(nexusrv_trace_decoder *decoder, nexusrv_trace_stop *stop)
Get the next Stop event.
struct nexusrv_trace_decoder nexusrv_trace_decoder
NexusRV Trace decoder context.
int nexusrv_trace_next_error(nexusrv_trace_decoder *decoder, nexusrv_trace_error *error)
Get the next Error event.
struct nexusrv_trace_stop nexusrv_trace_stop
NexusRV Stop Event.
struct nexusrv_trace_indirect nexusrv_trace_indirect
NexusRV Trace Indirect Branch Event.
int nexusrv_trace_next_indirect(nexusrv_trace_decoder *decoder, nexusrv_trace_indirect *indirect)
Get the next indirect branch.
int nexusrv_trace_next_sync(nexusrv_trace_decoder *decoder, nexusrv_trace_sync *sync)
Get the next Sync event.