V4.7.3 The Interrupt That Never Came

MediumDebug20 minap_soc v1.5 · recorded bootReview the topic

An interrupt test on a chip under bring-up enables two sources and asks the board for a GPIO edge. The edge comes, and the interrupt never does. Find the first transfer the chip got wrong.

This chip is not the one the other problems use: it is a build under bring-up, and one block of it misbehaves. The firmware is the kit's usual pattern. It ended with fail code 2: no GPIO interrupt within its limit of 3,000 polls.

The firmware of the recorded run (main.c).
#include "drivers/uart.h"
#include "drivers/gpio.h"
#include "irq.h"
#include "tb.h"

static volatile unsigned got;
static void on_uart(void) { (void)reg_read32(UART0_RXDATA); got |= 1; }
static void on_gpio(void) { gpio_ack(1u << 8); got |= 2; }

static int wait_for(unsigned bit)
{
    for (unsigned t = 0; t < 3000; t++) if (got & bit) return 1;
    return 0;
}

int main(void)
{
    reg_write32(SYSCTL_PINMUX, 2u << 2);
    irq_enable(UART0_IRQ, on_uart);                /* ap_plic source 1 */
    irq_enable(GPIO_IRQ, on_gpio);                 /* ap_plic source 2 */
    uart_enable_rx_irq();
    gpio_rising_irq(1u << 8);
    tb_event(5);                                   /* the board pulses IO8 */
    if (!wait_for(2)) return 2;
    tb_event(3);                                   /* the board sends a byte */
    if (!wait_for(1)) return 1;
    tb_puts("both interrupts\n");
    return 0;
}

The kit's irq_enable(id, handler) writes the handler into its table and 1 into PLIC_PRIORITY(id). Then lw a3, 0(a4) reads PLIC_ENABLE, sll and or set bit id, and sw a5, 0(a4) writes it back. The full specification holds its listing.

The trace below shows 23 of 802 retired loads and stores, cycles 191 to 354. 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
irq_enable in the listing of this image (riscv64-unknown-elf-gcc (14.2.0+19) 14.2.0): address and instruction.
80000124 <irq_enable>:
80000124:  addi  a4,a0,-1
80000128:  li  a5,15
8000012c:  bltu  a5,a4,80000134 <irq_enable+0x10>
80000130:  bnez  a1,80000148 <irq_enable+0x24>
80000134:  lui  a0,0x10
80000138:  addi  sp,sp,-16
8000013c:  addi  a0,a0,-3
80000140:  sw  ra,12(sp)
80000144:  jal  80000270 <tb_fail>
80000148:  lui  a5,0x80000
8000014c:  slli  a4,a0,0x2
80000150:  addi  a5,a5,1008
80000154:  add  a5,a5,a4
80000158:  lui  a4,0x3000
8000015c:  add  a4,a0,a4
80000160:  sw  a1,0(a5)
80000164:  slli  a4,a4,0x2
80000168:  li  a5,1
8000016c:  sw  a5,0(a4)
80000170:  lui  a4,0xc002
80000174:  lw  a3,0(a4)
80000178:  sll  a5,a5,a0
8000017c:  or  a5,a5,a3
80000180:  sw  a5,0(a4)
80000184:  lui  a5,0x1
80000188:  addi  a5,a5,-2048
8000018c:  csrs  mie,a5
80000190:  csrsi  mstatus,8
80000194:  ret

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

EVENT 311: 00000005
verdict=fail
reason=status_fail
status=bad00002
cycles=27347
traps=0
interrupts=0
mismatches=0

Retired Memory Transfers

CyclePC (hex)OperationAddress (hex)Data (hex)
1918000007cstore1000402000000002
196800002f0store8000fffc80000084
201800002fcstore8000fff800000000
20480000304store1000401000000008
22580000160store800003f4800002b8
2308000016cstore0c00000400000001
23280000174load0c00200000000000
23580000180store0c00200000000002
26780000160store800003f8800002d0
2728000016cstore0c00000800000001
27480000174load0c00200000000004
27780000180store0c00200000000004
2948000032cstore1000000800000002
29780000334store1000101c00000000
30180000340store1000102000000100
30380000344store1000102400000100
30580000348store1000101800000100
312800002b0store1000402800000005
3188000035cload8000043400000000
3278000035cload8000043400000000
3368000035cload8000043400000000
3458000035cload8000043400000000
3548000035cload8000043400000000
Step 1 · The first bad cycle and signal
Step 2 · The cause

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

irq.h
#ifndef AP_IRQ_H
#define AP_IRQ_H
typedef void (*irq_handler)(void);
unsigned plic_claim(void);
void plic_complete(unsigned id);
void irq_enable(unsigned id, irq_handler handler);
unsigned long long mtime_read(void);
void mtime_set_compare(unsigned long long deadline);
void msip_enable(irq_handler handler);
void mtime_enable(irq_handler handler);
#endif

drivers/gpio.h
#ifndef AP_GPIO_H
#define AP_GPIO_H
#include "hal.h"

static inline void gpio_drive(unsigned outputs, unsigned value)
{
    reg_write32(GPIO_OUT, value);
    reg_write32(GPIO_DIR, outputs);
}

static inline unsigned gpio_read(void)
{
    return reg_read32(GPIO_IN);
}

/* Select the rising-edge inputs for this test and discard stale edges. */
static inline void gpio_rising_irq(unsigned pins)
{
    reg_write32(GPIO_LEVEL_MODE, 0);
    reg_write32(GPIO_ACTIVE_HIGH, pins);
    reg_write32(GPIO_STATUS, pins);
    reg_write32(GPIO_IRQ_ENABLE, pins);
}

static inline void gpio_ack(unsigned pins)
{
    reg_write32(GPIO_STATUS, pins);
}
#endif

ap_plic · register manual
OffsetRegisterBitsFieldAccessResetHeld byMeaning
0x004 + 4 × (n − 1), n = 1 to 16PRIORITY2:0levelrw0x0blockPriority of source n, 0 to 7. 0 never competes. Priority.
0x001000PENDING16:1sourcesroblockPending bits; bit number = source ID. One bit for each source.
0x002000ENABLE16:1sourcesrw0x0000blockEnable bits; bit number = source ID. One bit for each source.
0x200000THRESHOLD2:0levelrw0x0blockThe core sees a request only above this priority. Threshold.
0x200004CLAIM4:0idrw0x00blockRead: claim, returns the winner's ID and clears its pending bit. Write: complete, names the ID that is done. Source ID.