|
| 1 | +#include "riscv_callbacks.h" |
| 2 | +#include "riscv_config.h" |
| 3 | +#include "riscv_platform_impl.h" |
| 4 | +#include <stdlib.h> |
| 5 | +#include <vector> |
| 6 | +#include <inttypes.h> |
| 7 | + |
| 8 | +bool config_enable_rvfi = true; // Only used if RVFI_DII is defined |
| 9 | + |
| 10 | +void print_lbits_hex(lbits val, int length = 0) |
| 11 | +{ |
| 12 | + if (length == 0) { |
| 13 | + length = val.len; |
| 14 | + } |
| 15 | + std::vector<uint8_t> data(length); |
| 16 | + mpz_export(data.data(), nullptr, -1, 1, 0, 0, *val.bits); |
| 17 | + for (int i = length - 1; i >= 0; --i) { |
| 18 | + fprintf(trace_log, "%02" PRIX8, data[i]); |
| 19 | + } |
| 20 | + fprintf(trace_log, "\n"); |
| 21 | +} |
| 22 | + |
| 23 | +// Implementations of default callbacks for trace printing and RVFI. |
| 24 | +// The model assumes that these functions do not change the state of the model. |
| 25 | +unit mem_write_callback(uint64_t paddr, uint64_t width, lbits value) |
| 26 | +{ |
| 27 | + if (config_print_mem_access) { |
| 28 | + fprintf(trace_log, "mem[0x%016" PRIX64 "] <- 0x", addr); |
| 29 | + print_lbits_hex(value, width); |
| 30 | + } |
| 31 | +#ifdef RVFI_DII |
| 32 | + if (config_enable_rvfi) { |
| 33 | + zrvfi_write(addr, width, value); |
| 34 | + } |
| 35 | +#endif |
| 36 | + return UNIT; |
| 37 | +} |
| 38 | + |
| 39 | +unit mem_read_callback(const char *type, uint64_t paddr, uint64_t width, |
| 40 | + lbits value) |
| 41 | +{ |
| 42 | + if (config_print_mem_access) { |
| 43 | + fprintf(trace_log, "mem[%s,0x%016" PRIX64 "] -> 0x", type, addr); |
| 44 | + print_lbits_hex(value, width); |
| 45 | + } |
| 46 | +#ifdef RVFI_DII |
| 47 | + if (config_enable_rvfi) { |
| 48 | + sail_int len; |
| 49 | + CREATE(sail_int)(&len); |
| 50 | + CONVERT_OF(sail_int, mach_int)(&len, width); |
| 51 | + zrvfi_read(addr, len, value); |
| 52 | + KILL(sail_int)(&len); |
| 53 | + } |
| 54 | +#endif |
| 55 | + return UNIT; |
| 56 | +} |
| 57 | + |
| 58 | +unit mem_exception_callback(uint64_t paddr, uint64_t num_of_exception) |
| 59 | +{ |
| 60 | + (void)num_of_exception; |
| 61 | +#ifdef RVFI_DII |
| 62 | + if (config_enable_rvfi) { |
| 63 | + zrvfi_mem_exception(addr); |
| 64 | + } |
| 65 | +#else |
| 66 | + (void)addr; |
| 67 | +#endif |
| 68 | + return UNIT; |
| 69 | +} |
| 70 | + |
| 71 | +unit xreg_write_callback(unsigned reg, uint64_t value) |
| 72 | +{ |
| 73 | + if (config_print_reg) { |
| 74 | + fprintf(trace_log, "x%d <- 0x%016" PRIX64 "\n", reg, value); |
| 75 | + } |
| 76 | +#ifdef RVFI_DII |
| 77 | + if (config_enable_rvfi) { |
| 78 | + zrvfi_wX(reg, value); |
| 79 | + } |
| 80 | +#endif |
| 81 | + return UNIT; |
| 82 | +} |
| 83 | + |
| 84 | +unit freg_write_callback(unsigned reg, uint64_t value) |
| 85 | +{ |
| 86 | + // TODO: will only print bits; should we print in floating point format? |
| 87 | + if (config_print_reg) { |
| 88 | + fprintf(trace_log, "f%d <- 0x%016" PRIX64 "\n", reg, value); |
| 89 | + } |
| 90 | + return UNIT; |
| 91 | +} |
| 92 | + |
| 93 | +unit csr_full_write_callback(const_sail_string csr_name, unsigned reg, |
| 94 | + uint64_t value) |
| 95 | +{ |
| 96 | + if (config_print_reg) { |
| 97 | + fprintf(trace_log, "CSR %s (0x%03X) <- 0x%016" PRIX64 "\n", csr_name, reg, |
| 98 | + value); |
| 99 | + } |
| 100 | + return UNIT; |
| 101 | +} |
| 102 | + |
| 103 | +unit csr_full_read_callback(const_sail_string csr_name, unsigned reg, |
| 104 | + uint64_t value) |
| 105 | +{ |
| 106 | + if (config_print_reg) { |
| 107 | + fprintf(trace_log, "CSR %s (0x%03X) -> 0x%016" PRIX64 "\n", csr_name, reg, |
| 108 | + value); |
| 109 | + } |
| 110 | + return UNIT; |
| 111 | +} |
| 112 | + |
| 113 | +unit vreg_write_callback(unsigned reg, lbits value) |
| 114 | +{ |
| 115 | + if (config_print_reg) { |
| 116 | + fprintf(trace_log, "v%d <- ", reg); |
| 117 | + print_lbits_hex(value); |
| 118 | + } |
| 119 | + return UNIT; |
| 120 | +} |
| 121 | + |
| 122 | +unit pc_write_callback(uint64_t value) |
| 123 | +{ |
| 124 | + (void)value; |
| 125 | + return UNIT; |
| 126 | +} |
| 127 | + |
| 128 | +unit trap_callback(unit) |
| 129 | +{ |
| 130 | +#ifdef RVFI_DII |
| 131 | + if (config_enable_rvfi) { |
| 132 | + zrvfi_trap(); |
| 133 | + } |
| 134 | +#endif |
| 135 | + return UNIT; |
| 136 | +} |
0 commit comments