V4.7.2 Catch the Interrupt Faults

HardVerify40 minap_soc v1.5 · firmware · serverReview the topic

Write the interrupt test of the chip: it passes on ap_soc and fails visibly on every faulty chip of a hidden set. The faults sit on the way from the devices to the core: a device's request, ap_plic, and the core's own interrupt wires.

Hardware context: Interrupts of ap_soc v1.5 from each device through ap_plic and the core's wires to a C handler. An interrupt test that catches hidden faults.

Write a test of the interrupt system that passes on ap_soc and fails on each of a set of hidden faulty chips. Each hidden chip has one fault between a device and a handler. It may be in a device's own request, in ap_plic itself, or on the wires into the core.

Your test runs once on each chip. The board helps on request: TB_EVENT 3 sends a byte to the UART receive pad, TB_EVENT 5 pulses IO8. Print interrupts ok and pass when every check of yours holds.

A faulty chip is caught only when your test fails visibly with a fail word (or a check of the bench). A run that waits for ever for an interrupt that never comes is not caught.

Full specification
SourceDeviceHow firmware raises it
1UART0RX interrupt enabled, TB_EVENT 3
2GPIORising edge on pin 8, TB_EVENT 5
3Timer and PWMtimer_start with a compare
4SPIA frame (SPI_DATA), DONE interrupt enabled
5DMAA copy, DONE interrupt enabled
MTIPap_clintMTIMECMP below MTIME, mie bit 7
ap_plicOffsetRule
PRIORITY(n)0x04 × n0 never interrupts; of two pending sources the higher priority is claimed first
PENDING0x1000One bit a source, read only
ENABLE0x2000One bit a source
THRESHOLD0x20_0000Only a priority above it interrupts
CLAIM0x20_0004Read: the winning source, 0 if none; write: complete

Given Files

Read-only files supplied by the bench.

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

drivers/timer.h
#ifndef AP_TIMER_H
#define AP_TIMER_H
#include "hal.h"

static inline void timer_stop(void)
{
    reg_write32(TIMER_RUN, 0);
    reg_write32(TIMER_IRQ_CLEAR, TIMER_IRQ_CLEAR_MATCH_MASK);
}

static inline void timer_start(unsigned top, unsigned duty, unsigned compare)
{
    timer_stop();
    reg_write32(TIMER_DIV_TOP, FIELD_PREP(TIMER_DIV_TOP_TOP, top));
    reg_write32(TIMER_DUTY, FIELD_PREP(TIMER_DUTY_SLOTS, duty));
    reg_write32(TIMER_COMPARE, FIELD_PREP(TIMER_COMPARE_SLOT, compare));
    reg_write32(TIMER_IRQ_ENABLE, TIMER_IRQ_ENABLE_MATCH_MASK);
    reg_write32(TIMER_RUN, TIMER_RUN_RUN_MASK);
}
#endif

drivers/spi.h
#ifndef AP_SPI_H
#define AP_SPI_H
#include "hal.h"

static inline unsigned spi_transfer(unsigned char command)
{
    while (reg_read32(SPI_STATUS) & SPI_STATUS_BUSY_MASK) { }
    reg_write32(SPI_STATUS, SPI_STATUS_DONE_MASK);
    reg_write32(SPI_DATA, command);
    while (!FIELD_GET(SPI_STATUS_DONE, reg_read32(SPI_STATUS))) { }
    return FIELD_GET(SPI_DATA_RECEIVED, reg_read32(SPI_DATA));
}
#endif

drivers/uart.h
#ifndef AP_UART_H
#define AP_UART_H
#include "hal.h"

static inline void uart_flush(void)
{
    while (!FIELD_GET(UART0_STATUS_ROOM, reg_read32(UART0_STATUS))) { }
}

static inline void uart_putc(unsigned char byte)
{
    uart_flush();
    reg_write32(UART0_TXDATA, byte);
}

static inline unsigned char uart_getc(void)
{
    while (!FIELD_GET(UART0_STATUS_RX_READY, reg_read32(UART0_STATUS))) { }
    return reg_read32(UART0_RXDATA);
}

static inline void uart_enable_rx_irq(void)
{
    reg_write32(UART0_IRQ_ENABLE, UART0_IRQ_ENABLE_RX_READY_MASK);
}
#endif

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

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.
ap_clint · register manual
OffsetRegisterBitsFieldAccessResetHeld byMeaning
0x000000MSIP0msiprw0x0blockMachine software interrupt request of hart 0. 1 raises the request.
0x004000MTIMECMP_LOW31:0valuerw0xFFFFFFFFblockLow word of mtimecmp. Compare value, low word.
0x004004MTIMECMP_HIGH31:0valuerw0xFFFFFFFFblockHigh word of mtimecmp. Compare value, high word.
0x00BFF8MTIME_LOW31:0valuerw0x00000000blockLow word of mtime. Time, low word.
0x00BFFCMTIME_HIGH31:0valuerw0x00000000blockHigh word of mtime. Time, high word.

Examples

Example 1

The Good Chip
Input
one run
Output
console "interrupts ok"; pass

Every source arrives under its own number, the priorities and the threshold behave, the timer ticks once.

Example 2

A Source Under the Wrong Number
Input
a hidden chip
Output
your fail word

An interrupt arrives, but another handler runs. Only a check of who ran catches it.

Example 3

A Source That Never Arrives
Input
a hidden chip
Output
your fail word

A time limit turns the wait into a report.

Constraints and Contract

  • C firmware with the kit, SRAM layout.
  • One case, run on the good chip and on every hidden chip.
  • You may mask interrupts in mstatus (bit 3) to claim sources by hand.

Hints

Hint 1

A handler that runs is not proof: whose handler ran?

Hint 2

How many separate wires bring interrupts into the core?

Hint 3

What does your test do while it waits for an interrupt that a faulty chip never raises?

What the grader checks: Your test runs once on the good chip and once on each hidden faulty chip. The good chip must pass; each faulty chip must end with a fail word or a bench check. The page says only whether a faulty chip passed. No hidden chip has a fault that only a check of priority order or of the threshold would find. So a test without those checks can be accepted here, though it would not be complete on a real chip.

1 checks on ap_soc v1.5 · firmware · server. Run the examples first, then check the full regression. Full checks use the server judge. Downloads contain your text only; the chip, the bench and the hidden checks stay on the server. A passing result establishes the tested cases, not a proof for all possible inputs.

See all 3 problems on this path

Report a problem with this page

Your code CSaved locally
Ln 1, Col 1Tab indents · Ctrl/⌘ + Enter runs samples
Not run yet

The Good Chip

Input
one run
Expected output
console "interrupts ok"; pass

Every source arrives under its own number, the priorities and the threshold behave, the timer ticks once.

Run samples executes all 1 public checks. Check all tests runs 1 checks on the server.

ap_soc v1.5 · firmware · server