The Retire Trace as a Monitor

The core of ap_soc reports every instruction it finishes as one record of 24 fields. That stream is the best monitor a chip bench has. This page teaches how to read a record, and the rules that make every record mean one thing.

You Will Be Able To

  • Read the 24 fields of a retire record and say what the instruction did.
  • Tell a retired instruction, an exception and an interrupt apart in the trace.
  • State the rules a trace must keep, and why a monitor depends on them.
  • Say what the trace cannot show.
Lesson
V5.1
Time
26 min
Runs on
Your browser
Check
5 questions

A test fails deep into its run. The waveform shows two buses and dozens of signals, and the pipeline holds five instructions at once. Which instruction did what? The retire trace answers in one line per instruction.

One Record, 24 FieldsStep through the six groups of fields of one retire record, the lbu of the lab below.
  • Processor
  • Memory
  • Data
One Record, 24 FieldsStep through the six groups of fields of one retire record, the lbu of the lab below. processor Which instruction order 11, pc8000002c, insn,next_pc, priv processor Did it trap trap, cause, tval processor Registers rs1, rs2 read; rd 11= 000000ee memory Memory addr 800000c5,rmask 1, rdata000000ee processor CSR written csr_write, csr_addr,csr_wdata processor State after mstatus after, mip itwas decided with thenorandandand
  1. processor Which instructionorder 11, pc 8000002c, insn, next_pc, priv
    • then → Did it trap
  2. processor Did it traptrap, cause, tval
    • or → Registers
  3. processor Registersrs1, rs2 read; rd 11 = 000000ee
    • and → Memory
  4. memory Memoryaddr 800000c5, rmask 1, rdata 000000ee
    • and → CSR written
  5. processor CSR writtencsr_write, csr_addr, csr_wdata
    • and → State after
  6. processor State aftermstatus after, mip it was decided with
  1. Which instruction

    Fields 1 to 5: the record number, the address, the instruction word, where the core went next, the privilege.

  2. Did it trap

    Fields 6 to 8. A trap record says the instruction did not happen; cause and tval say why.

  3. Registers

    Fields 9 to 14: what it read from rs1 and rs2, and what it wrote to rd.

  4. Memory

    Fields 15 to 19: the byte address, which bytes were read or written, and their data.

  5. CSR

    Fields 20 to 22: a CSR written, and the value it holds after.

  6. State

    Fields 23 and 24: mstatus after the record, and the interrupt lines it was decided with.

Read the diagram as text
  • Which instruction: order 11, pc 8000002c, insn, next_pc, priv
  • Did it trap: trap, cause, tval
  • Registers: rs1, rs2 read; rd 11 = 000000ee
  • Memory: addr 800000c5, rmask 1, rdata 000000ee
  • CSR written: csr_write, csr_addr, csr_wdata
  • State after: mstatus after, mip it was decided with
  • Which instruction → Did it trap: then
  • Did it trap → Registers: or
  • Registers → Memory: and
  • Memory → CSR written: and
  • CSR written → State after: and

ap_rv32's retire trace (blueprint 3.5). The values are those of the lbu record recorded below.

Before You Start

Read The Life of a C Test and Traps: Exceptions, Interrupts and the Machine CSRs first. The Core as an IP shows the trace port among the core's other ports.

Five questions this page answers
Question
1Why does a chip bench want a retire trace when it already sees the buses?
2What does one record say, and when does the core make one?
3How does a trap look different from an instruction that retired?
4Which rules must every record keep, and who depends on them?
5What can the trace not show?

The Idea: One Line per Finished Instruction

A pipelined core works on five instructions at once, and its buses carry fetches that may never count. Watching them tells you what the core tried, not what it did. The retire trace reports only what finished, in program order, one record each.

Each record is the instruction's whole effect. It says which registers it read and wrote, which bytes it touched and which CSR it changed. A trap gets a record too, saying the instruction did not happen and why. Nothing the program can see changes without a record that shows it.

That makes the trace the monitor every other check stands on. The reference model of the next lesson compares it, field by field. Coverage counts it. A debugger reads it. Each of them trusts that a record means one thing, so the trace has rules of its own.

New Words

New words on this page
WordMeaning
RetireAn instruction finishes: its effects become part of the program's state.
Retire recordOne line of the trace for one commit-point event: a retired instruction, an exception or an interrupt entry.
Commit pointThe pipeline stage where an instruction can no longer be cancelled. In ap_rv32 it is MEM; the record appears in WB.
Exception recordA record with trap = 1 and cause bit 31 clear: the instruction itself faulted.
Interrupt recordA record with trap = 1 and cause bit 31 set: an interrupt was taken before the instruction ran.
minstretThe CSR that counts retired instructions: records with trap = 0.
RVFIThe RISC-V Formal Interface: a similar record that riscv-formal reads. ap_rv32's fields follow it where it has one.

The Smallest Example: Three Records by Hand

Here are three records of the lab, with eight of their fields. Read each one before the explanation.

Three records of the tour program
orderinstructiontrapcausetvalrd = valuememory
11lbu a1, 1(s1)00000000000000000x11 = 000000eeread 1 at 800000c5: 000000ee
14ecall10000000b00000000nonenone
54lw t2, 0(t0)10000000510007000nonenone

Recorded on ap_soc v1.5.

The lbu read one byte (mask 1) at 800000c5 and wrote it, zero-extended, to x11. The ecall is a trap with cause 11: no register, no memory. The last load faulted: cause 5, its address in tval, and its memory fields stay 0, because the access never happened.

The Rules

  1. Rule 1: Records come in program order, numbered without gaps.

    order counts every record from 0; minstret counts those with trap = 0.

  2. Rule 2: A trap changes nothing by itself.

    On a trap record the register, memory and CSR fields are 0; mepc, mcause and mtval are implied.

  3. Rule 3: An interrupt names the instruction it stopped.

    Its pc and insn are those of the instruction that did not run; that instruction retires later, as its own record.

  4. Rule 4: A field with nothing to say is 0.

    No rd: rd_wdata is 0. No access: mem_addr and the data are 0. No CSR write: its address and value are 0.

  5. Rule 5: The fields follow what happened, not the instruction's name.

    csrrs with x0 writes no CSR, so csr_write is 0; lui reads no register, so rs1_addr is 0.

Sources for These Rules

The 24 fields and rules T1 to T10: docs/soc/ap_rv32-blueprint.md, section 3.5. The text form (165 characters a record) and its reader: tools/soc/rv32/trace.mjs.

The Zicsr write rule behind rule 5: the RISC-V Unprivileged specification (library key RV-UNPRIV), the Zicsr chapter. Trap entry and mepc: the Privileged specification (RV-PRIV), the machine trap handling section.

RVFI is the interface of riscv-formal (github.com/YosysHQ/riscv-formal). The blueprint lists where ap_rv32's fields differ from it on purpose. For example, an interrupt has a record of its own.

Now Run It: One Record of Every Kind

The lab program makes one record of every kind. It reads a bench register, stores, loads a byte, runs lui and a CSR read that writes nothing. Then it takes an ecall, a timer interrupt in a waiting loop, and a load from a hole of the map.

The first lines of the lab program after its set-up (v5-labs.mjs TOUR_S).
    lw   a0, TB_ARG(s0)            # a device read: the bench's word
    la   s1, buf
    sw   a0, 0(s1)                 # a store to SRAM
    lbu  a1, 1(s1)                 # one byte back
    lui  a2, 0x12345               # no source register
    csrrs a3, mstatus, x0          # reads mstatus, writes nothing
    ecall                          # an exception
Eight of the 67 records (Icarus build of soc_tb with a probe, Icarus Verilog version 12.0 (stable) ()).
10 80000028 sw a0, 0(s1)             trap=0 cause=00000000 tval=00000000 rd= 0 rd_wdata=00000000 mem=800000c4 r0 w15 csr_write=0 mip=00000000
11 8000002c lbu a1, 1(s1)            trap=0 cause=00000000 tval=00000000 rd=11 rd_wdata=000000ee mem=800000c5 r1 w0 csr_write=0 mip=00000000
13 80000034 csrrs a3, mstatus, zero  trap=0 cause=00000000 tval=00000000 rd=13 rd_wdata=00001800 mem=00000000 r0 w0 csr_write=0 mip=00000000
14 80000038 ecall                    trap=1 cause=0000000b tval=00000000 rd= 0 rd_wdata=00000000 mem=00000000 r0 w0 csr_write=0 mip=00000000
43 80000070 bne s4, zero, 0x8000007c trap=1 cause=80000007 tval=00000000 rd= 0 rd_wdata=00000000 mem=00000000 r0 w0 csr_write=0 mip=00000080
51 800000c0 mret                     trap=0 cause=00000000 tval=00000000 rd= 0 rd_wdata=00000000 mem=00000000 r0 w0 csr_write=0 mip=00000080
52 80000070 bne s4, zero, 0x8000007c trap=0 cause=00000000 tval=00000000 rd= 0 rd_wdata=00000000 mem=00000000 r0 w0 csr_write=0 mip=00000000
54 80000080 lw t2, 0(t0)             trap=1 cause=00000005 tval=10007000 rd= 0 rd_wdata=00000000 mem=00000000 r0 w0 csr_write=0 mip=00000000

Record 13 is the csrrs: csr_write = 0, rule 5 at work. Record 43 is the interrupt, at the pc of a bne that did not run. Record 52 is that bne, retiring after mret: rule 3. The load from the hole has no memory fields: rule 2.

The run made 67 records and 3 of them were traps, so minstret ended at 64. On the chip bench the same run compared all 67 records with the model, with 0 mismatches.

Why It Is Built This Way

A bench could watch the buses and the register file instead. It would have to rebuild what the pipeline did. Which fetch was cancelled, which write came from which instruction? That rebuilding is a second core, with its own bugs. The trace is the core's own statement, at the point where nothing can be undone.

The core makes the record in WB, from the stage where the instruction committed. So a record never shows a cancelled instruction. Unlike RVFI, an interrupt gets a record of its own here. So the model of the next lesson can be told exactly where it landed.

Know what the trace leaves out. It shows no bus timing, no wait states, and nothing another manager did. A DMA write to SRAM never appears in it. It shows what the core did, and the bench's other monitors show the rest.

The Trap: The Trace You Trusted

A trace port is RTL like any other, and it can be wrong while the core is right. A record that reports rd = 0 with a value, or a trap that also claims a write, sends every reader astray. The model compare then reports a core bug that is not there, and someone spends the afternoon on the wrong block.

So a bench checks the trace's rules on every record, beside the model. The checker is cheap: a record count, the last next_pc and one decode of the opcode. The practice problem builds it on the chip's own trace port. The first worksheet reads records by hand.

At Scale

ap_soc~12 blocks
Phone-class SoChundreds of blocks

This section looks ahead. ap_rv32 retires at most one instruction a cycle.

  1. Several retires a cycle

    A superscalar core retires two or more instructions a cycle; its trace has one channel each, in order.

  2. Compressed trace

    On silicon, a trace port sends only branches and exceptions, compressed; a decoder rebuilds the path from the program image.

  3. The same monitor, three uses

    The model compare, functional coverage and debug all read the same stream, so a bug in it costs three times.

Try It

Run the slice: it reads every record of the lab and prints the traps. It checks two rules: order with no gap, and the count of retired records.

Change one order number in the source. What does the last line say now?

The predict question below asks you to count before you run.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: order 14 pc 80000038: exception, cause 11 order 43 pc 80000070: interrupt, cause 7 order 54 pc 80000080: exception, cause 5 records 67, gaps in order 0, retired 64

Check Yourself

Predict the outputThe slice now reads the first 44 records, up to and including the interrupt. What is the last line it prints?
// Records of one run of ap_soc v1.5: order, pc, trap and cause. Two rules of the trace:
// order counts every record with no gap, and minstret counts the records that did not trap.
module tb;
  localparam int N = 44;
  longint order [N];
  bit [31:0] pc [N], cause [N];
  bit trap [N];
  int retired = 0, gaps = 0;
  initial begin
    order[0] = 0; pc[0] = 32'h80000000; trap[0] = 0; cause[0] = 32'h00000000;
    order[1] = 1; pc[1] = 32'h80000004; trap[1] = 0; cause[1] = 32'h00000000;
    order[2] = 2; pc[2] = 32'h80000008; trap[2] = 0; cause[2] = 32'h00000000;
    order[3] = 3; pc[3] = 32'h8000000c; trap[3] = 0; cause[3] = 32'h00000000;
    order[4] = 4; pc[4] = 32'h80000010; trap[4] = 0; cause[4] = 32'h00000000;
    order[5] = 5; pc[5] = 32'h80000014; trap[5] = 0; cause[5] = 32'h00000000;
    order[6] = 6; pc[6] = 32'h80000018; trap[6] = 0; cause[6] = 32'h00000000;
    order[7] = 7; pc[7] = 32'h8000001c; trap[7] = 0; cause[7] = 32'h00000000;
    order[8] = 8; pc[8] = 32'h80000020; trap[8] = 0; cause[8] = 32'h00000000;
    order[9] = 9; pc[9] = 32'h80000024; trap[9] = 0; cause[9] = 32'h00000000;
    order[10] = 10; pc[10] = 32'h80000028; trap[10] = 0; cause[10] = 32'h00000000;
    order[11] = 11; pc[11] = 32'h8000002c; trap[11] = 0; cause[11] = 32'h00000000;
    order[12] = 12; pc[12] = 32'h80000030; trap[12] = 0; cause[12] = 32'h00000000;
    order[13] = 13; pc[13] = 32'h80000034; trap[13] = 0; cause[13] = 32'h00000000;
    order[14] = 14; pc[14] = 32'h80000038; trap[14] = 1; cause[14] = 32'h0000000b;
    order[15] = 15; pc[15] = 32'h80000094; trap[15] = 0; cause[15] = 32'h00000000;
    order[16] = 16; pc[16] = 32'h80000098; trap[16] = 0; cause[16] = 32'h00000000;
    order[17] = 17; pc[17] = 32'h8000009c; trap[17] = 0; cause[17] = 32'h00000000;
    order[18] = 18; pc[18] = 32'h800000a0; trap[18] = 0; cause[18] = 32'h00000000;
    order[19] = 19; pc[19] = 32'h800000a4; trap[19] = 0; cause[19] = 32'h00000000;
    order[20] = 20; pc[20] = 32'h800000a8; trap[20] = 0; cause[20] = 32'h00000000;
    order[21] = 21; pc[21] = 32'h8000003c; trap[21] = 0; cause[21] = 32'h00000000;
    order[22] = 22; pc[22] = 32'h80000040; trap[22] = 0; cause[22] = 32'h00000000;
    order[23] = 23; pc[23] = 32'h80000044; trap[23] = 0; cause[23] = 32'h00000000;
    order[24] = 24; pc[24] = 32'h80000048; trap[24] = 0; cause[24] = 32'h00000000;
    order[25] = 25; pc[25] = 32'h8000004c; trap[25] = 0; cause[25] = 32'h00000000;
    order[26] = 26; pc[26] = 32'h80000050; trap[26] = 0; cause[26] = 32'h00000000;
    order[27] = 27; pc[27] = 32'h80000054; trap[27] = 0; cause[27] = 32'h00000000;
    order[28] = 28; pc[28] = 32'h80000058; trap[28] = 0; cause[28] = 32'h00000000;
    order[29] = 29; pc[29] = 32'h8000005c; trap[29] = 0; cause[29] = 32'h00000000;
    order[30] = 30; pc[30] = 32'h80000060; trap[30] = 0; cause[30] = 32'h00000000;
    order[31] = 31; pc[31] = 32'h80000064; trap[31] = 0; cause[31] = 32'h00000000;
    order[32] = 32; pc[32] = 32'h80000068; trap[32] = 0; cause[32] = 32'h00000000;
    order[33] = 33; pc[33] = 32'h8000006c; trap[33] = 0; cause[33] = 32'h00000000;
    order[34] = 34; pc[34] = 32'h80000070; trap[34] = 0; cause[34] = 32'h00000000;
    order[35] = 35; pc[35] = 32'h80000074; trap[35] = 0; cause[35] = 32'h00000000;
    order[36] = 36; pc[36] = 32'h80000078; trap[36] = 0; cause[36] = 32'h00000000;
    order[37] = 37; pc[37] = 32'h80000070; trap[37] = 0; cause[37] = 32'h00000000;
    order[38] = 38; pc[38] = 32'h80000074; trap[38] = 0; cause[38] = 32'h00000000;
    order[39] = 39; pc[39] = 32'h80000078; trap[39] = 0; cause[39] = 32'h00000000;
    order[40] = 40; pc[40] = 32'h80000070; trap[40] = 0; cause[40] = 32'h00000000;
    order[41] = 41; pc[41] = 32'h80000074; trap[41] = 0; cause[41] = 32'h00000000;
    order[42] = 42; pc[42] = 32'h80000078; trap[42] = 0; cause[42] = 32'h00000000;
    order[43] = 43; pc[43] = 32'h80000070; trap[43] = 1; cause[43] = 32'h80000007;
    for (int i = 0; i < N; i++) begin
      if (i > 0 && order[i] != order[i-1] + 1) gaps++;
      if (!trap[i]) retired++;
      if (trap[i]) $display("order %0d pc %h: %s, cause %0d", order[i], pc[i], cause[i][31] ? "interrupt" : "exception", cause[i][30:0]);
    end
    $display("records %0d, gaps in order %0d, retired %0d", N, gaps, retired);
  end
endmodule
ConceptWhy does a chip bench want a retire trace when it already sees the buses?
ConceptAn interrupt record shows the pc of an addi. What happens to that addi?
ConceptA trap record shows rd = 5 and a value. What is wrong?
ConceptWhat can the retire trace not show?

Common Mistakes

Wrong Read the buses to see what the core did.

Fetches can be cancelled and the buses show no register writes.

Fix Read the retire trace: one record per finished instruction.

Wrong Count records to get the instructions executed.

Trap records are records too, but their instruction did not run.

Fix Count records with trap = 0, as minstret does.

Wrong Take a record's values on trust.

The trace port is RTL; a fault there misleads the model, coverage and debug at once.

Fix Check the trace's own rules on every record.

Remember

  • One record per commit-point event: a retired instruction, an exception or an interrupt entry, in program order.
  • A trap record changes nothing by itself; an interrupt names the instruction it stopped, which runs later.
  • A field with nothing to say is 0, and fields follow what happened, not the instruction's name.
  • The trace is the core's own account; check its rules, and know it shows nothing other managers did.

Practice

Report a problem with this page