/* Machine layout, boot and the round-robin hart scheduler. */ #include #include #include "machine_internal.h" /* Return whether [base, base + size) overlaps any of the first n sections. */ static bool overlaps_any( u64 base, u32 size, const struct machine_section *sections, u32 n ) { for (u32 i = 0; i < n; i++) { if (machine_overlap(base, size, sections[i].base, sections[i].size)) return true; } return false; } /* Place each hart's stack below the top of RAM. Reject a layout in which the * FDT reservation, a stack and a loaded section collide, before the FDT or a * stack can overwrite the image. The loader has already placed the sections * in RAM without overlapping each other. */ static bool layout_init(struct machine *m) { const struct machine_config *c = m->config; struct machine_section used[] = { { MACHINE_FDT_BASE, MACHINE_FDT_SIZE }, c->text, c->rodata, c->rwdata }; if ((c->stack_size & 15) || c->memory_size < MACHINE_FDT_BASE + MACHINE_FDT_SIZE - MACHINE_RAM_BASE) return false; for (u32 i = 1; i < 4; i++) { if (overlaps_any(used[i].base, used[i].size, used, 1)) return false; } u64 top = (MACHINE_RAM_BASE + c->memory_size) & ~15ULL; for (u32 id = 0; id < c->hart_count; id++) { if (top - MACHINE_RAM_BASE < c->stack_size) return false; u64 base = top - c->stack_size; if (overlaps_any(base, c->stack_size, used, 4)) return false; m->stack_base[id] = base; top = base; } return true; } /* Advance virtual time. */ static void advance(struct machine *m, u64 delta) { m->ticks += delta; m->clint.time += delta; } int machine_run(const struct machine_config *config) { struct machine m = { .config = config }; if (!layout_init(&m)) { fprintf( stderr, "invalid machine layout: sections, aligned stacks and the FDT " "reservation must fit in RAM without overlap\n" ); return 2; } if (!machine_fdt(&m)) { fprintf(stderr, "machine FDT exceeds its bounded reservation\n"); return 2; } m.uart.tx_irq = true; for (u32 id = 0; id < config->hart_count; id++) { struct machine_hart *h = &m.harts[id]; h->id = id; h->pc = config->entry; h->privilege = 3; h->vector.vtype = 1ULL << 63; h->regs[2] = m.stack_base[id] + config->stack_size; h->regs[10] = id; h->regs[11] = MACHINE_FDT_BASE; m.clint.compare[id] = UINT64_MAX; } while (!m.finished && m.ticks < config->max_steps) { machine_devices_update(&m); bool runnable = false; for (u32 checked = 0; checked < config->hart_count; checked++) { u32 id = m.next_hart; m.next_hart = id + 1 == config->hart_count ? 0 : id + 1; struct machine_hart *h = &m.harts[id]; if (h->waiting && !(machine_pending(&m, h) & h->mie)) continue; machine_step(&m, h); advance(&m, 1); runnable = true; break; } if (!runnable) { u64 next = machine_next_event(&m); advance(&m, next - m.ticks); } } if (config->count_instructions) fprintf( stderr, "Processed %llu instructions across %u harts in %llu ticks\n", m.retired, config->hart_count, m.ticks ); if (!m.finished) { if (config->timed_out) *config->timed_out = true; fprintf( stderr, "machine timed out after %llu ticks (%llu instructions)\n", m.ticks, m.retired ); for (u32 id = 0; id < config->hart_count; id++) { const struct machine_hart *h = &m.harts[id]; fprintf( stderr, "hart=%u pc=0x%llx mode=%c wfi=%u mcause=0x%llx mepc=0x%llx " "mtval=0x%llx\n", id, h->pc, h->privilege ? 'M' : 'U', (unsigned)h->waiting, h->mcause, h->mepc, h->mtval ); } return 2; } return m.result; }