A test of nested calls runs for seventy thousand cycles and dies in a trap, with no line printed. Somewhere the stack and the program's variables met. Find the first transfer that went where it should not.
TB_ARG was 3000 for this run. The retire trace has one trap: at pc 800002d4, mcause 2. The stack starts at 0x8001_0000 and grows down. This image's variables run from 0x80000398 (limit) to the end of .bss. external is the kit's table of 17 interrupt handlers; deepest is the last variable.
#include "tb.h"
volatile unsigned limit = 8; /* the depth the test means to reach */
volatile unsigned deepest;
/* Count down through nested calls, keeping a little state in each frame. */
static unsigned nest(unsigned n)
{
volatile unsigned frame[4];
frame[0] = n; frame[1] = n + 1; frame[2] = n + 2; frame[3] = n + 3;
if (n == 0) return frame[0];
if (n > deepest) deepest = n;
return nest(n - 1) + frame[1] - frame[0] - 1;
}
int main(void)
{
unsigned depth = tb_arg(0); /* the bench asks for a depth */
if (nest(depth) != 0) return 1;
tb_puts("deepest ");
tb_put_hex(deepest);
tb_putc('\n');
return deepest == depth && depth <= limit ? 0 : 2;
}
nest starts with addi sp, sp, -32 and sw a0, 0(sp). Its frame is 32 bytes; its first store is frame[0] at the new sp. It saves ra at 28(sp) and writes deepest with sw a0, -1972(gp). The full specification holds the listing of nest.
The trace below shows 51 of 12194 retired loads and stores, cycles 190 to 72945. Name the first transfer that went wrong: its cycle, the wrong part, its address and its register or variable. Then choose the cause.
Full specification
8000029c <nest>:
8000029c: addi sp,sp,-32
800002a0: sw a0,0(sp)
800002a4: addi a5,a0,1
800002a8: sw a5,4(sp)
800002ac: addi a5,a0,2
800002b0: sw a5,8(sp)
800002b4: addi a5,a0,3
800002b8: sw ra,28(sp)
800002bc: sw a5,12(sp)
800002c0: bnez a0,800002d4 <nest+0x38>
800002c4: lw a0,0(sp)
800002c8: lw ra,28(sp)
800002cc: addi sp,sp,32
800002d0: ret
800002d4: lw a4,-1972(gp)
800002d8: bgeu a4,a0,800002e0 <nest+0x44>
800002dc: sw a0,-1972(gp)
800002e0: addi a0,a0,-1
800002e4: jal 8000029c <nest>
800002e8: lw a5,4(sp)
800002ec: lw a4,0(sp)
800002f0: addi a5,a5,-1
800002f4: sub a5,a5,a4
800002f8: add a0,a5,a0
800002fc: j 800002c8 <nest+0x2c>
The register list names every address the answer may use. Addresses are in hex without 0x in the trace; write them with or without 0x in the form.
Data in the trace is the word as the bus carried it. A load's data is what came back; a store's is what was written.
Diagnose the Failing Run
Recorded Console and Result
ARG 1: 00000bb8 verdict=fail reason=status_fail status=bad0ffff cycles=73232 traps=1 interrupts=0 mismatches=0
Retired Memory Transfers
| Cycle | PC (hex) | Operation | Address (hex) | Data (hex) |
|---|---|---|---|---|
| 190 | 80000078 | store | 10004020 | 00000002 |
| 196 | 80000308 | store | 8000fffc | 80000080 |
| 199 | 8000030c | store | 8000fff8 | 00000000 |
| 202 | 80000310 | store | 8000fff4 | 00000000 |
| 215 | 80000294 | load | 1000402c | 00000bb8 |
| 224 | 800002a0 | store | 8000ffd0 | 00000bb8 |
| 227 | 800002a8 | store | 8000ffd4 | 00000bb9 |
| 234 | 800002b0 | store | 8000ffd8 | 00000bba |
| 237 | 800002b8 | store | 8000ffec | 80000320 |
| 243 | 800002bc | store | 8000ffdc | 00000bbb |
| 72703 | 800002b0 | store | 80000438 | 000003dd |
| 72706 | 800002b8 | store | 8000044c | 800002e8 |
| 72712 | 800002bc | store | 8000043c | 000003de |
| 72718 | 800002d4 | load | 800003e4 | 00000bb8 |
| 72729 | 800002a0 | store | 80000410 | 000003da |
| 72732 | 800002a8 | store | 80000414 | 000003db |
| 72739 | 800002b0 | store | 80000418 | 000003dc |
| 72742 | 800002b8 | store | 8000042c | 800002e8 |
| 72748 | 800002bc | store | 8000041c | 000003dd |
| 72754 | 800002d4 | load | 800003e4 | 00000bb8 |
| 72765 | 800002a0 | store | 800003f0 | 000003d9 |
| 72768 | 800002a8 | store | 800003f4 | 000003da |
| 72775 | 800002b0 | store | 800003f8 | 000003db |
| 72778 | 800002b8 | store | 8000040c | 800002e8 |
| 72784 | 800002bc | store | 800003fc | 000003dc |
| 72790 | 800002d4 | load | 800003e4 | 00000bb8 |
| 72801 | 800002a0 | store | 800003d0 | 000003d8 |
| 72804 | 800002a8 | store | 800003d4 | 000003d9 |
| 72811 | 800002b0 | store | 800003d8 | 000003da |
| 72814 | 800002b8 | store | 800003ec | 800002e8 |
| 72820 | 800002bc | store | 800003dc | 000003db |
| 72826 | 800002d4 | load | 800003e4 | 00000bb8 |
| 72837 | 800002a0 | store | 800003b0 | 000003d7 |
| 72840 | 800002a8 | store | 800003b4 | 000003d8 |
| 72847 | 800002b0 | store | 800003b8 | 000003d9 |
| 72850 | 800002b8 | store | 800003cc | 800002e8 |
| 72856 | 800002bc | store | 800003bc | 000003da |
| 72862 | 800002d4 | load | 800003e4 | 00000bb8 |
| 72873 | 800002a0 | store | 80000390 | 000003d6 |
| 72876 | 800002a8 | store | 80000394 | 000003d7 |
| 72883 | 800002b0 | store | 80000398 | 000003d8 |
| 72886 | 800002b8 | store | 800003ac | 800002e8 |
| 72892 | 800002bc | store | 8000039c | 000003d9 |
| 72898 | 800002d4 | load | 800003e4 | 00000bb8 |
| 72909 | 800002a0 | store | 80000370 | 000003d5 |
| 72912 | 800002a8 | store | 80000374 | 000003d6 |
| 72919 | 800002b0 | store | 80000378 | 000003d7 |
| 72922 | 800002b8 | store | 8000038c | 800002e8 |
| 72928 | 800002bc | store | 8000037c | 000003d8 |
| 72934 | 800002d4 | load | 800003e4 | 00000bb8 |
| 72945 | 800002a0 | store | 80000350 | 000003d4 |
Given Files
Use the register map to identify the transfer in the recording.
tb.h
#ifndef AP_TB_H
#define AP_TB_H
_Noreturn void tb_pass(void);
_Noreturn void tb_fail(unsigned code);
void tb_putc(char byte);
void tb_puts(const char *text);
void tb_put_hex(unsigned value);
void tb_event(unsigned id);
/* Slot zero is the single bench-to-firmware argument word. */
unsigned tb_arg(unsigned n);
void tb_wait_event(unsigned id);
#endif
hal.h
#ifndef AP_HAL_H
#define AP_HAL_H
#include "ap_soc_regs.h"
/* V13.4 supplies these two functions through DPI in a host-native build. */
#ifdef AP_HOST_DPI
unsigned reg_read32(unsigned address);
void reg_write32(unsigned address, unsigned value);
#else
static inline unsigned reg_read32(unsigned address)
{
return *(volatile unsigned *)address;
}
static inline void reg_write32(unsigned address, unsigned value)
{
*(volatile unsigned *)address = value;
}
#endif
#define FIELD_GET(field, word) (((word) & field##_MASK) >> field##_SHIFT)
#define FIELD_PREP(field, value) (((unsigned)(value) << field##_SHIFT) & field##_MASK)
#endif