/* Physical bus, CLINT, PLIC, UART and finish device. */ #include #include #include "machine_internal.h" bool machine_overlap(u64 a, u64 asize, u64 b, u64 bsize) { if (!asize || !bsize) return false; return a <= b ? b - a < asize : a - b < bsize; } /* Return whether [address, address + width) lies in RAM, which lies above * every device aperture. */ static bool ram_range(const struct machine *m, u64 address, u32 width) { return address >= MACHINE_RAM_BASE && address - MACHINE_RAM_BASE < m->config->memory_size && width <= m->config->memory_size - (address - MACHINE_RAM_BASE); } /* Invalidate all reservations intersected by a store to RAM. */ static void invalidate(struct machine *m, u64 address, u32 width) { for (u32 i = 0; i < m->config->hart_count; i++) { struct machine_hart *h = &m->harts[i]; if (h->reserved && machine_overlap( address, width, h->reservation, h->reservation_size )) h->reserved = false; } } /* Choose highest priority, then lowest source ID as required by the PLIC. */ static u32 plic_best(const struct machine *m, u32 context, bool notification) { u32 eligible = m->plic.pending & m->plic.enable[context]; if (!eligible) return 0; u32 best = 0, priority = notification ? m->plic.threshold[context] : 0; for (u32 id = 1; id <= MACHINE_IRQ_COUNT; id++) { if ((eligible & (1U << id)) && m->plic.priority[id] > priority) { best = id; priority = m->plic.priority[id]; } } return best; } /* The UART interrupt line is asserted while an enabled THRE interrupt waits. */ static bool uart_interrupt(const struct machine *m) { return (m->uart.ier & 2) && m->uart.tx_irq; } void machine_devices_update(struct machine *m) { if (!m->injected_irq && m->config->irq_source && m->ticks >= m->config->irq_at) { u32 bit = 1U << m->config->irq_source; if (!(m->plic.active & bit)) m->plic.pending |= bit; m->injected_irq = true; } u32 bit = 1U << MACHINE_UART_IRQ; if (uart_interrupt(m)) m->plic.level |= bit; else m->plic.level &= ~bit; m->plic.pending |= m->plic.level & ~m->plic.active; } bool machine_plic_notify(const struct machine *m, u32 context) { return plic_best(m, context, true) != 0; } /* Lower *next to candidate when candidate lies after now. */ static void earlier(u64 now, u64 candidate, u64 *next) { if (candidate > now && candidate < *next) *next = candidate; } u64 machine_next_event(const struct machine *m) { u64 next = m->config->max_steps; for (u32 id = 0; id < m->config->hart_count; id++) { u64 compare = m->clint.compare[id]; if ((m->harts[id].mie & MIP_MTIP) && compare > m->clint.time) { u64 delta = compare - m->clint.time; if (delta <= UINT64_MAX - m->ticks) earlier(m->ticks, m->ticks + delta, &next); } } if (!m->injected_irq && m->config->irq_source) earlier(m->ticks, m->config->irq_at, &next); return next; } /* CLINT supports naturally aligned RV32 halves and RV64 timer accesses. */ static bool clint_access( struct machine *m, u32 offset, u32 width, bool store, u64 *value ) { if (offset < 4 * m->config->hart_count && width == 4) { u32 *word = &m->clint.software[offset / 4]; if (store) *word = (u32)*value & 1; else *value = *word; return true; } u64 *reg; u32 half; if (offset >= 0x4000 && offset < 0x4000 + 8 * m->config->hart_count) { reg = &m->clint.compare[(offset - 0x4000) / 8]; half = offset & 7; } else if (offset >= 0xbff8 && offset < 0xc000) { reg = &m->clint.time; half = offset - 0xbff8; } else { return false; } if (width == 8 && half == 0) { if (store) *reg = *value; else *value = *reg; } else if (width == 4 && (half == 0 || half == 4)) { u32 shift = half * 8; if (store) *reg = (*reg & ~(0xffffffffULL << shift)) | ((u64)(u32)*value << shift); else *value = (u32)(*reg >> shift); } else { return false; } return true; } static bool plic_access( struct machine *m, u32 offset, u32 width, bool store, u64 *value ) { struct machine_plic *p = &m->plic; if (width != 4) return false; if (offset <= MACHINE_IRQ_COUNT * 4) { u32 id = offset / 4; if (store) { if (id) p->priority[id] = (u32)*value & 7; } else *value = p->priority[id]; return true; } if (offset == 0x1000) { if (store) return false; *value = p->pending; return true; } if (offset >= 0x2000 && offset < 0x2000 + m->config->hart_count * 0x80 && (offset & 0x7f) == 0) { u32 context = (offset - 0x2000) / 0x80; if (store) p->enable[context] = (u32)*value & ~1U; else *value = p->enable[context]; return true; } if (offset >= 0x200000 && offset < 0x200000 + m->config->hart_count * 0x1000) { u32 context = (offset - 0x200000) / 0x1000; u32 reg = offset & 0xfff; if (reg == 0) { if (store) p->threshold[context] = (u32)*value & 7; else *value = p->threshold[context]; return true; } if (reg == 4) { if (store) { u32 id = (u32)*value; if (id && id <= MACHINE_IRQ_COUNT && (p->active & (1U << id)) && p->owner[id] == context && (p->enable[context] & (1U << id))) { p->active &= ~(1U << id); p->pending |= p->level & (1U << id); } } else { u32 id = plic_best(m, context, false); *value = id; if (id) { p->pending &= ~(1U << id); p->active |= 1U << id; p->owner[id] = (u8)context; } } return true; } } return false; } /* Registers: THR at 0, IER at 1, IIR at 2 (read) and LSR at 5 (read). */ static bool uart_access( struct machine *m, u32 offset, u32 width, bool store, u64 *value ) { struct machine_uart *u = &m->uart; if (width != 1) return false; if (store) { switch (offset) { case 0: if (fputc((u8)*value, stdout) == EOF || fflush(stdout) == EOF) { fprintf(stderr, "machine UART output failed\n"); m->finished = true; m->result = 2; } u->tx_irq = true; break; case 1: if (!(u->ier & 2) && ((u8)*value & 2)) u->tx_irq = true; u->ier = (u8)*value & 2; break; default: return false; } return true; } switch (offset) { case 1: *value = u->ier; break; case 2: if (uart_interrupt(m)) { *value = 2; /* THR empty. */ u->tx_irq = false; } else *value = 1; /* No interrupt pending. */ break; case 5: *value = 0x60; break; default: return false; } return true; } /* The finish register reads as zero and accepts the success value 0x5555 or * a failure value 0xNNNN3333, which stop the machine. */ static bool finish_access( struct machine *m, struct machine_hart *h, bool store, u64 *value ) { if (!store) { *value = 0; return true; } u32 code = (u32)*value; if (code != 0x5555 && (code & 0xffff) != 0x3333) return false; m->finished = true; m->result = code == 0x5555 ? 0 : 1; if (m->result) fprintf( stderr, "machine guest failure: code=%u hart=%u pc=0x%llx\n", code >> 16, h->id, h->pc ); return true; } bool machine_store_probe( const struct machine *m, const struct machine_hart *h, u64 address, u32 width, u64 *cause ) { bool misaligned = (address & (width - 1)) != 0; *cause = misaligned ? CAUSE_STORE_MISALIGNED : CAUSE_STORE_FAULT; if (misaligned || !ram_range(m, address, width)) return false; return h->privilege != 0 || (!machine_overlap( address, width, m->config->text.base, m->config->text.size ) && !machine_overlap( address, width, m->config->rodata.base, m->config->rodata.size )); } bool machine_bus( struct machine *m, struct machine_hart *h, u64 address, u32 width, u32 access, u64 *value, u64 *cause ) { bool store = (access & ACCESS_STORE) != 0; bool fetch = (access & ACCESS_FETCH) != 0; if (address & (width - 1)) { *cause = fetch ? CAUSE_FETCH_MISALIGNED : store ? CAUSE_STORE_MISALIGNED : CAUSE_LOAD_MISALIGNED; return false; } *cause = fetch ? CAUSE_FETCH_FAULT : store ? CAUSE_STORE_FAULT : CAUSE_LOAD_FAULT; if (ram_range(m, address, width)) { u8 *ram = m->config->ram + (address - MACHINE_RAM_BASE); if (store) { if (!machine_store_probe(m, h, address, width, cause)) return false; for (u32 i = 0; i < width; i++) ram[i] = (u8)(*value >> (i * 8)); invalidate(m, address, width); } else { u64 v = 0; for (u32 i = 0; i < width; i++) v |= (u64)ram[i] << (i * 8); *value = v; } return true; } if (access & (ACCESS_FETCH | ACCESS_ATOMIC)) return false; if (address >= MACHINE_CLINT_BASE && address < MACHINE_CLINT_BASE + MACHINE_CLINT_SIZE) return clint_access( m, (u32)(address - MACHINE_CLINT_BASE), width, store, value ); if (address >= MACHINE_PLIC_BASE && address < MACHINE_PLIC_BASE + MACHINE_PLIC_SIZE) return plic_access( m, (u32)(address - MACHINE_PLIC_BASE), width, store, value ); if (address >= MACHINE_UART_BASE && address < MACHINE_UART_BASE + MACHINE_UART_SIZE) return uart_access( m, (u32)(address - MACHINE_UART_BASE), width, store, value ); if (address == MACHINE_FINISH_BASE && width == 4) return finish_access(m, h, store, value); return false; }