Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Saturn dromajo #72

Draft
wants to merge 15 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ VERSION=Dromajo-0.1
CXX=g++
CFLAGS_OPT=-Ofast
CXXFLAGS=$(CFLAGS_OPT) -Wall -std=c++11 -g -Wno-parentheses -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -MMD
CXXFLAGS+=-D_GNU_SOURCE -DCONFIG_VERSION=\"$(VERSION)\"
CXXFLAGS+=-D_GNU_SOURCE -DCONFIG_VERSION=\"$(VERSION)\" -D__STDC_FORMAT_MACROS
#CXXFLAGS+=-DLIVECACHE
LDFLAGS=

Expand Down
115 changes: 108 additions & 7 deletions src/dromajo_cosim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ static inline bool is_mmio_load(RISCVCPUState *s,
uint64_t va = riscv_get_reg_previous(s, reg) + offset;

if(!riscv_cpu_get_phys_addr(s, va, ACCESS_READ, &pa) &&
mmio_start <= pa && pa < mmio_end) {
(mmio_start <= pa && pa < mmio_end) ||(pa == 0x4000) ) {
return true;
}

Expand Down Expand Up @@ -155,11 +155,32 @@ static inline void handle_dut_overrides(RISCVCPUState *s,
* hpmoverflows, mip, and sip.
* If the destination register is x0 then it is actually a csr-write
*/
bool is_pmp = (csrno == CSR_PMPCFG(0)) ||
(csrno == CSR_PMPCFG(2)) ||
(csrno == CSR_PMPADDR(0)) ||
(csrno == CSR_PMPADDR(1)) ||
(csrno == CSR_PMPADDR(2)) ||
(csrno == CSR_PMPADDR(3)) ||
(csrno == CSR_PMPADDR(4)) ||
(csrno == CSR_PMPADDR(5)) ||
(csrno == CSR_PMPADDR(6)) ||
(csrno == CSR_PMPADDR(7)) ||
(csrno == CSR_PMPADDR(8)) ||
(csrno == CSR_PMPADDR(9)) ||
(csrno == CSR_PMPADDR(10)) ||
(csrno == CSR_PMPADDR(11)) ||
(csrno == CSR_PMPADDR(12)) ||
(csrno == CSR_PMPADDR(13)) ||
(csrno == CSR_PMPADDR(14)) ||
(csrno == CSR_PMPADDR(15));
if (opcode == 0x73 && rd != 0 &&
(0xB00 <= csrno && csrno < 0xB20 ||
0xC00 <= csrno && csrno < 0xC20 ||
(csrno == 0x344 /* mip */ ||
csrno == 0x144 /* sip */)))
csrno == 0x144 /* sip */||
csrno == 0x306 /* mcounteren */||
csrno == 0x106 /* scounteren */||
is_pmp)))
riscv_set_reg(s, rd, dut_wdata);

/* Catch loads and amo from MMIO space */
Expand Down Expand Up @@ -196,13 +217,15 @@ static inline void handle_dut_overrides(RISCVCPUState *s,
void dromajo_cosim_raise_trap(dromajo_cosim_state_t *state, int hartid, int64_t cause)
{
VirtMachine *m = (VirtMachine *)state;

fprintf(dromajo_stderr, "cause %lx\n", cause);
fprintf(dromajo_stderr, "m->pending_interrupt: %x \n", m->pending_interrupt);
if (cause < 0) {
assert(m->pending_interrupt == -1);
m->pending_interrupt = cause & 63;
fprintf(dromajo_stderr, "DUT raised interrupt %d\n", m->pending_interrupt);
fprintf(dromajo_stderr, "[DEBUG] DUT raised interrupt %d\n", m->pending_interrupt);
} else {
m->pending_exception = cause;
fprintf(dromajo_stderr, "[DEBUG] DUT raised exception %d\n", m->pending_exception);
}
}

Expand All @@ -221,6 +244,8 @@ void dromajo_cosim_raise_trap(dromajo_cosim_state_t *state, int hartid, int64_t
* time, and instret. For all these cases the model will override
* with the expected values.
*/

//look here
int dromajo_cosim_step(dromajo_cosim_state_t *state,
int hartid,
uint64_t dut_pc,
Expand Down Expand Up @@ -281,7 +306,7 @@ int dromajo_cosim_step(dromajo_cosim_state_t *state,
/* On the DUT, the interrupt can race the exception.
Let's try to match that behavior */

fprintf(dromajo_stderr, "DUT also raised exception %d\n", r->common.pending_exception);
fprintf(dromajo_stderr, "[DEBUG] DUT also raised exception %d\n", r->common.pending_exception);
riscv_cpu_interp64(s, 1); // Advance into the exception

int cause = s->priv == PRV_S ? s->scause : s->mcause;
Expand All @@ -302,12 +327,19 @@ int dromajo_cosim_step(dromajo_cosim_state_t *state,
}
}

if (r->common.pending_interrupt != -1)
if (r->common.pending_interrupt != -1) {
riscv_cpu_set_mip(s, riscv_cpu_get_mip(s) | 1 << r->common.pending_interrupt);
fprintf(dromajo_stderr, "[DEBUG] Interrupt: MIP <- %d: Now MIP = %x\n", r->common.pending_interrupt, riscv_cpu_get_mip(s));
}

if (riscv_cpu_interp64(s, 1) != 0) {
iregno = riscv_get_most_recently_written_reg(s);
fregno = riscv_get_most_recently_written_fp_reg(s);

//// ABE: I think this is the solution
//r->common.pending_interrupt = -1;
//r->common.pending_exception = -1;

break;
}

Expand Down Expand Up @@ -336,7 +368,8 @@ int dromajo_cosim_step(dromajo_cosim_state_t *state,
if (verbose)
fprintf(dromajo_stderr, "f%-2d 0x%016" PRIx64, fregno, emu_wdata);
} else
fprintf(dromajo_stderr, " ");
if (verbose)
fprintf(dromajo_stderr, " ");

if (verbose)
fprintf(dromajo_stderr, " DASM(0x%08x)\n", emu_insn);
Expand Down Expand Up @@ -376,3 +409,71 @@ int dromajo_cosim_step(dromajo_cosim_state_t *state,

return exit_code;
}

/*
* dromajo_cosim_override_mem --
*
* DUT sets Dromajo memory. Used so that other devices (i.e. block device, accelerators, can write to memory).
*/
int dromajo_cosim_override_mem(dromajo_cosim_state_t *state, int hartid, uint64_t dut_paddr, uint64_t dut_val, int size_log2)
{
RISCVMachine *r = (RISCVMachine *)state;
RISCVCPUState *s = r->cpu_state[hartid];

uint8_t *ptr;
target_ulong offset;
PhysMemoryRange *pr = get_phys_mem_range(s->mem_map, dut_paddr);

if (!pr) {
#ifdef DUMP_INVALID_MEM_ACCESS
fprintf(dromajo_stderr, "riscv_cpu_write_memory: invalid physical address 0x%016" PRIx64 "\n", dut_paddr);
#endif
return 1;
} else if (pr->is_ram) {
phys_mem_set_dirty_bit(pr, dut_paddr - pr->addr);
ptr = pr->phys_mem + (uintptr_t)(dut_paddr - pr->addr);
switch (size_log2) {
case 0:
*(uint8_t *)ptr = dut_val;
break;
case 1:
*(uint16_t *)ptr = dut_val;
break;
case 2:
*(uint32_t *)ptr = dut_val;
break;
#if MLEN >= 64
case 3:
*(uint64_t *)ptr = dut_val;
break;
#endif
#if MLEN >= 128
case 4:
*(uint128_t *)ptr = dut_val;
break;
#endif
default:
abort();
}
} else {
offset = dut_paddr - pr->addr;
if (((pr->devio_flags >> size_log2) & 1) != 0) {
pr->write_func(pr->opaque, offset, dut_val, size_log2);
}
#if MLEN >= 64
else if ((pr->devio_flags & DEVIO_SIZE32) && size_log2 == 3) {
/* emulate 64 bit access */
pr->write_func(pr->opaque, offset,
dut_val & 0xffffffff, 2);
pr->write_func(pr->opaque, offset + 4,
(dut_val >> 32) & 0xffffffff, 2);
}
#endif
else {
#ifdef DUMP_INVALID_MEM_ACCESS
fprintf(dromajo_stderr, "unsupported device write access: addr=0x%016" PRIx64 " width=%d bits\n", dut_paddr, 1 << (3 + size_log2));
#endif
}
}
return 0;
}
12 changes: 12 additions & 0 deletions src/dromajo_cosim.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ int dromajo_cosim_step(dromajo_cosim_state_t *state,
void dromajo_cosim_raise_trap(dromajo_cosim_state_t *state,
int hartid,
int64_t cause);

/*
* dromajo_cosim_override_mem --
*
* DUT sets Dromajo memory. Used so that other devices (i.e. block device, accelerators, can write to memory).
*/
int dromajo_cosim_override_mem(dromajo_cosim_state_t *state,
int hartid,
uint64_t dut_paddr,
uint64_t dut_val,
int size_log2);

#ifdef __cplusplus
} // extern C
#endif
Expand Down
37 changes: 28 additions & 9 deletions src/dromajo_main.cpp
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing the semantics of M like this would break existing usage. You could make it accept both 0x-prefixed hex and decimal otherwise.

Original file line number Diff line number Diff line change
Expand Up @@ -609,11 +609,12 @@ static void usage(const char *prog, const char *msg)
" --bootrom load in a bootrom img file (default is dromajo bootrom)\n"
" --dtb load in a dtb file (default is dromajo dtb)\n"
" --compact_bootrom have dtb be directly after bootrom (default 256B after boot base)\n"
" --reset_vector set reset vector (default 0x%lx)\n"
" --reset_vector set reset vector for all cores (default 0x%lx)\n"
" --mmio_range START:END [START,END) mmio range for cosim (overridden by config file)\n"
" --plic START:SIZE set PLIC start address and size (defaults to 0x%lx:0x%lx)\n"
" --clint START:SIZE set CLINT start address and size (defaults to 0x%lx:0x%lx)\n"
" --custom_extension add X extension to isa\n",
" --plic START:SIZE set PLIC start address and size in B (defaults to 0x%lx:0x%lx)\n"
" --clint START:SIZE set CLINT start address and size in B (defaults to 0x%lx:0x%lx)\n"
" --custom_extension add X extension to misa for all cores\n"
" --clear_ids clear mvendorid, marchid, mimpid for all cores\n",
msg,
prog,
(long)BOOT_BASE_ADDR, (long)RAM_BASE_ADDR,
Expand Down Expand Up @@ -671,6 +672,7 @@ RISCVMachine *virt_machine_main(int argc, char **argv)
uint64_t clint_base_addr_override = 0;
uint64_t clint_size_override = 0;
bool custom_extension = false;
bool clear_ids = false;

dromajo_stdout = stdout;
dromajo_stderr = stderr;
Expand Down Expand Up @@ -698,6 +700,7 @@ RISCVMachine *virt_machine_main(int argc, char **argv)
{"plic", required_argument, 0, 'p' }, // CFG
{"clint", required_argument, 0, 'C' }, // CFG
{"custom_extension", no_argument, 0, 'u' }, // CFG
{"clear_ids", no_argument, 0, 'L' }, // CFG
{0, 0, 0, 0 }
};

Expand Down Expand Up @@ -751,7 +754,9 @@ RISCVMachine *virt_machine_main(int argc, char **argv)
break;

case 'M':
memory_size_override = atoi(optarg);
if (optarg[0] != '0' || optarg[1] != 'x')
usage(prog, "--memory_size expects argument to start with 0x... ");
memory_size_override = strtoll(optarg + 2, NULL, 16);
break;

case 'A':
Expand Down Expand Up @@ -786,7 +791,8 @@ RISCVMachine *virt_machine_main(int argc, char **argv)
if (!strchr(optarg, ':'))
usage(prog, "--mmio_range expects an argument like START:END");

char *mmio_start = strtok(optarg, ":");
char *copy = strdup(optarg);
char *mmio_start = strtok(copy, ":");
char *mmio_end = strtok(NULL, ":");

if (mmio_start[0] != '0' || mmio_start[1] != 'x')
Expand All @@ -796,14 +802,17 @@ RISCVMachine *virt_machine_main(int argc, char **argv)
if (mmio_end[0] != '0' || mmio_end[1] != 'x')
usage(prog, "--mmio_range END address must begin with 0x...");
mmio_end_override = strtoll(mmio_end+2, NULL, 16);

free(copy);
}
break;

case 'p': {
if (!strchr(optarg, ':'))
usage(prog, "--plic expects an argument like START:SIZE");

char *plic_base_addr = strtok(optarg, ":");
char *copy = strdup(optarg);
char *plic_base_addr = strtok(copy, ":");
char *plic_size = strtok(NULL, ":");

if (plic_base_addr[0] != '0' || plic_base_addr[1] != 'x')
Expand All @@ -813,14 +822,17 @@ RISCVMachine *virt_machine_main(int argc, char **argv)
if (plic_size[0] != '0' || plic_size[1] != 'x')
usage(prog, "--plic SIZE must begin with 0x...");
plic_size_override = strtoll(plic_size+2, NULL, 16);

free(copy);
}
break;

case 'C': {
if (!strchr(optarg, ':'))
usage(prog, "--clint expects an argument like START:SIZE");

char *clint_base_addr = strtok(optarg, ":");
char *copy = strdup(optarg);
char *clint_base_addr = strtok(copy, ":");
char *clint_size = strtok(NULL, ":");

if (clint_base_addr[0] != '0' || clint_base_addr[1] != 'x')
Expand All @@ -830,13 +842,19 @@ RISCVMachine *virt_machine_main(int argc, char **argv)
if (clint_size[0] != '0' || clint_size[1] != 'x')
usage(prog, "--clint SIZE must begin with 0x...");
clint_size_override = strtoll(clint_size+2, NULL, 16);

free(copy);
}
break;

case 'u':
custom_extension = true;
break;

case 'L':
clear_ids = true;
break;

default:
usage(prog, "I'm not having this argument");
}
Expand Down Expand Up @@ -1000,8 +1018,9 @@ RISCVMachine *virt_machine_main(int argc, char **argv)
if (clint_size_override)
p->clint_size = clint_size_override;

// ISA modifications
// core modifications
p->custom_extension = custom_extension;
p->clear_ids = clear_ids;

RISCVMachine *s = virt_machine_init(p);
if (!s)
Expand Down
3 changes: 3 additions & 0 deletions src/machine.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ typedef struct {
/* Add to misa custom extensions */
bool custom_extension;

/* Clear mimpid, marchid, mvendorid */
bool clear_ids;

uint64_t physical_addr_len;

char *logfile; // If non-zero, all output goes here, stderr and stdout
Expand Down
Loading