Skip to content
Merged
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 .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/gen/cpp_hart_gen/rv64_Debug/build/iss",
"args": ["-m", "rv64", "-c", "${workspaceFolder}/cfgs/rv64-riscv-tests.yaml", "${workspaceFolder}/ext/riscv-tests/isa/rv64ui-p-addi"],
"args": ["-m", "rv64", "-c", "${workspaceFolder}/cfgs/rv64-riscv-tests.yaml", "--mm", "${workspaceFolder}/cfgs/memmap.json", "${workspaceFolder}/ext/riscv-tests/isa/rv64ui-p-add"],
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"environment": [],
Expand Down
46 changes: 26 additions & 20 deletions backends/cpp_hart_gen/cpp/include/udb/elf_reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <limits>
#include <string>
#include <utility>
#include <algorithm>

#include "udb/soc_model.hpp"

Expand Down Expand Up @@ -64,32 +65,37 @@ namespace udb {

template <unsigned char CLASS>
std::pair<uint64_t, uint64_t> ElfReader::_mem_range() {
Elf_Scn* pscn;
std::conditional_t<CLASS == ELFCLASS32, Elf32_Phdr, Elf64_Phdr>* phdr;
std::conditional_t<CLASS == ELFCLASS32, Elf32_Shdr, Elf64_Shdr>* shdr;
size_t n;
uint64_t smallest_addr = std::numeric_limits<uint64_t>::max();
uint64_t largest_addr = std::numeric_limits<uint64_t>::min();

if (elf_getphdrnum(m_elf, &n) != 0) {
throw ElfException("Could not find number of Program Headers");
}

if constexpr (CLASS == ELFCLASS32) {
phdr = elf32_getphdr(m_elf);
} else {
phdr = elf64_getphdr(m_elf);
}
for (size_t i = 0; i < n; i++) {
if (phdr[i].p_type == PT_LOAD) {
if (phdr[i].p_vaddr < smallest_addr) {
smallest_addr = phdr[i].p_vaddr;
}
if (phdr[i].p_vaddr + phdr[i].p_memsz > largest_addr) {
largest_addr = phdr[i].p_vaddr + phdr[i].p_memsz;
uint64_t addr_lo = std::numeric_limits<uint64_t>::max();
uint64_t addr_hi = std::numeric_limits<uint64_t>::min();

//Make room for any section allocating memory
if(elf_getshdrnum(m_elf, &n) == 0 && n > 0) {
for (size_t i = 0; i < n; i++) {
pscn = elf_getscn(m_elf, i);
if(pscn != NULL) {
if constexpr (CLASS == ELFCLASS32) {
shdr = elf32_getshdr(pscn);
} else {
shdr = elf64_getshdr(pscn);
}
if (shdr->sh_flags & SHF_ALLOC) {
addr_lo = std::min(addr_lo, (uint64_t)shdr->sh_addr);
addr_hi = std::max(addr_hi, (uint64_t)(shdr->sh_addr + shdr->sh_size));
}
}
}
}

return std::make_pair(smallest_addr, largest_addr);
//No memory to be allocated
if(addr_lo > addr_hi) {
addr_lo = addr_hi = 0;
}

return std::make_pair(addr_lo, addr_hi);
}

template <unsigned char CLASS, SocModel SocType>
Expand Down
49 changes: 39 additions & 10 deletions backends/cpp_hart_gen/cpp/src/iss.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@

#include <CLI/CLI.hpp>
#include <string>
#include <fstream>
#include <nlohmann/json.hpp>

#include "udb/defines.hpp"
#include "udb/elf_reader.hpp"
#include "udb/hart_factory.hxx"
#include "udb/inst.hpp"
#include "udb/iss_soc_model.hpp"

using json = nlohmann::json;

struct Options {
std::string config_name;
std::filesystem::path config_path;
std::filesystem::path memory_map_path;
bool show_configs;
std::string elf_file_path;

Expand All @@ -24,6 +29,7 @@ int parse_cmdline(int argc, char **argv, Options &options) {
CLI::App app("Bare-bones ISS");
app.add_option("-m,--model", options.config_name, "Hart model");
app.add_option("-c,--cfg", options.config_path, "Hart configuration file");
app.add_option("--mm, --memory-map", options.memory_map_path, "Memory map file");
app.add_flag("-l,--list-configs", options.show_configs,
"List available configurations");

Expand All @@ -33,6 +39,35 @@ int parse_cmdline(int argc, char **argv, Options &options) {
return PARSE_OK;
}

static const std::pair<uint64_t, uint64_t> get_memory_range(std::filesystem::path memmap, std::filesystem::path elf_file_path) {
json regions;
uint64_t memsz;

if(!memmap.empty()) {
std::ifstream f(memmap);
json data = json::parse(f);
regions = data["regions"];

for (const auto& region : regions) {
auto type = region["type"];
if (type == "ram") {
std::string base = region["base"]["value"];
std::string size = region["size"]["value"];
return std::make_pair(std::stoul(base, nullptr, 0), std::stoul(size, nullptr, 0));
}
}
}

udb::ElfReader elf_reader(elf_file_path.c_str());
auto range = elf_reader.mem_range();
memsz = range.second - range.first;
// round up to a page for good measure
memsz = (memsz + 0xfff) & ~0xfffull;

return std::make_pair(range.first, memsz);
}


int main(int argc, char **argv) {
Options opts;
int ret;
Expand All @@ -52,22 +87,16 @@ int main(int argc, char **argv) {
return 1;
}

udb::ElfReader elf_reader(opts.elf_file_path.c_str());

// how much memory do we need?
auto range = elf_reader.mem_range();
uint64_t memsz = range.second - range.first + 1;

// round up to a page for good measure
memsz = (memsz & ~0xfffull) + 0x1000;

udb::IssSocModel soc(memsz, range.first);
auto range = get_memory_range(opts.memory_map_path, opts.elf_file_path);
udb::IssSocModel soc(range.second, range.first);

auto hart = udb::HartFactory::create<udb::IssSocModel>(opts.config_name, 0,
opts.config_path, soc);
auto tracer = udb::HartFactory::create_tracer<udb::IssSocModel>(
"riscv-tests", opts.config_name, hart);
hart->attach_tracer(tracer);

udb::ElfReader elf_reader(opts.elf_file_path.c_str());
auto entry_pc = elf_reader.loadLoadableSegments(soc);
hart->reset(entry_pc);

Expand Down
76 changes: 76 additions & 0 deletions cfgs/memmap.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
{
"regions": [
{
"type": "rom",
"base": {
"len": 64,
"value": "0x1000"
},
"size": {
"len": 64,
"value": "0x1000"
},
"attributes": {
"cacheable": true,
"coherent": true,
"executable": false,
"readable": true,
"writable": false,
"read_idempotent": true,
"write_idempotent": true,
"misaligned_fault": "NoFault",
"reservability": "RsrvNone",
"supports_cbo_zero": false
},
"include_in_device_tree": false
},
{
"type": "mmio",
"base": {
"len": 64,
"value": "0x2000000"
},
"size": {
"len": 64,
"value": "0x2000000"
},
"attributes": {
"cacheable": false,
"coherent": true,
"executable": false,
"readable": true,
"writable": true,
"read_idempotent": false,
"write_idempotent": false,
"misaligned_fault": "AlignmentFault",
"reservability": "RsrvNone",
"supports_cbo_zero": false
},
"include_in_device_tree": false
},
{
"type": "ram",
"base": {
"len": 64,
"value": "0x80000000"
},
"size": {
"len": 64,
"value": "0x80000000"
},
"attributes": {
"cacheable": true,
"coherent": true,
"executable": true,
"readable": true,
"writable": true,
"read_idempotent": true,
"write_idempotent": true,
"misaligned_fault": "NoFault",
"reservability": "RsrvEventual",
"supports_cbo_zero": true
},
"include_in_device_tree": true
}
]
}
Loading