Skip to content

Commit

Permalink
refactor!: rename ROM to DTB
Browse files Browse the repository at this point in the history
  • Loading branch information
edubart authored and diegonehab committed Oct 3, 2023
1 parent e4542ff commit f17ab80
Show file tree
Hide file tree
Showing 31 changed files with 204 additions and 267 deletions.
2 changes: 1 addition & 1 deletion lib/grpc-interfaces
2 changes: 1 addition & 1 deletion lib/machine-emulator-defines
6 changes: 3 additions & 3 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ LIBCARTESI_OBJS:= \
pma-driver.o \
clint.o \
clint-factory.o \
rom.o \
dtb.o \
tty.o \
htif.o \
htif-factory.o \
Expand Down Expand Up @@ -568,7 +568,7 @@ REMOTE_CARTESI_MACHINE_OBJS:= \
pma-driver.o \
clint.o \
clint-factory.o \
rom.o \
dtb.o \
tty.o \
htif.o \
htif-factory.o \
Expand Down Expand Up @@ -598,7 +598,7 @@ JSONRPC_REMOTE_CARTESI_MACHINE_OBJS:= \
pma-driver.o \
clint.o \
clint-factory.o \
rom.o \
dtb.o \
tty.o \
htif.o \
htif-factory.o \
Expand Down
4 changes: 2 additions & 2 deletions src/cartesi-machine-tests.lua
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ local function print_machine(test_name, expected_cycles)
"./cartesi-machine.lua \
--ram-length=32Mi\
--ram-image='%s'\
--no-rom-bootargs\
--no-dtb-bootargs\
--max-mcycle=%d ",
test_path .. "/" .. test_name,
2 * expected_cycles
Expand All @@ -679,7 +679,7 @@ local function print_machine(test_name, expected_cycles)
"./cartesi-machine.lua \
--ram-length=32Mi\
--ram-image='%s'\
--no-rom-bootargs\
--no-dtb-bootargs\
--uarch-ram-length=%d\
--uarch-ram-image=%s\
--max-mcycle=%d ",
Expand Down
55 changes: 27 additions & 28 deletions src/cartesi-machine.lua
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ where options are:
--ram-length=<number>
set RAM length.
--rom-image=<filename>
name of file containing ROM image (default: auto generated flattened device tree).
--dtb-image=<filename>
name of file containing DTB image (default: auto generated flattened device tree).
--no-rom-bootargs
--no-dtb-bootargs
clear default bootargs.
--append-rom-bootargs=<string>
--append-dtb-bootargs=<string>
append <string> to bootargs.
--no-root-flash-drive
Expand Down Expand Up @@ -352,7 +352,6 @@ where options are:
elf (optional)
the binary elf file with symbols and debugging information to be debugged, such as:
- vmlinux (for kernel debugging)
- ROM elf (for debugging the ROM)
- BBL elf (for debugging the BBL boot loader)
- a test elf (for debugging tests)
Expand Down Expand Up @@ -401,8 +400,8 @@ local flash_length = {}
local memory_range_replace = {}
local ram_image_filename = images_path .. "linux.bin"
local ram_length = 64 << 20
local rom_image_filename = nil
local rom_bootargs = "console=hvc0 rootfstype=ext2 root=/dev/mtdblock0 rw quiet \z
local dtb_image_filename = nil
local dtb_bootargs = "console=hvc0 rootfstype=ext2 root=/dev/mtdblock0 rw quiet \z
swiotlb=noforce init=/opt/cartesi/bin/init random.trust_bootloader=on"
local rollup
local uarch
Expand All @@ -411,7 +410,7 @@ local rollup_inspect
local concurrency_update_merkle_tree = 0
local skip_root_hash_check = false
local skip_version_check = false
local append_rom_bootargs = ""
local append_dtb_bootargs = ""
local htif_no_console_putchar = false
local htif_console_getchar = false
local htif_yield_automatic = false
Expand Down Expand Up @@ -508,7 +507,7 @@ local options = {
local ok, stdlib = pcall(require, "posix.stdlib")
if ok and stdlib then
-- use realpath to get images real filenames,
-- tools could use this information to detect rom/linux/rootfs versions
-- tools could use this information to detect linux/rootfs versions
local ram_image = stdlib.realpath(images_path .. "linux.bin")
local rootfs_image = stdlib.realpath(images_path .. "rootfs.ext2")
if ram_image then print(string.format(' "default_ram_image": "%s",', ram_image)) end
Expand All @@ -524,26 +523,26 @@ local options = {
end,
},
{
"^%-%-rom%-image%=(.*)$",
"^%-%-dtb%-image%=(.*)$",
function(o)
if not o or #o < 1 then return false end
rom_image_filename = o
dtb_image_filename = o
return true
end,
},
{
"^%-%-no%-rom%-bootargs$",
"^%-%-no%-dtb%-bootargs$",
function(all)
if not all then return false end
rom_bootargs = ""
dtb_bootargs = ""
return true
end,
},
{
"^%-%-append%-rom%-bootargs%=(.*)$",
"^%-%-append%-dtb%-bootargs%=(.*)$",
function(o)
if not o or #o < 1 then return false end
append_rom_bootargs = o
append_dtb_bootargs = o
return true
end,
},
Expand Down Expand Up @@ -825,7 +824,7 @@ local options = {
flash_length.root = nil
flash_shared.root = nil
table.remove(flash_label_order, 1)
rom_bootargs = "console=hvc0"
dtb_bootargs = "console=hvc0"
return true
end,
},
Expand Down Expand Up @@ -1163,12 +1162,12 @@ local function store_machine_config(config, output)
output(" image_filename = %q,", ram.image_filename or def.ram.image_filename)
comment_default(ram.image_filename, def.ram.image_filename)
output(" },\n")
local rom = config.rom or {}
output(" rom = {\n")
output(" image_filename = %q,", rom.image_filename or def.rom.image_filename)
comment_default(rom.image_filename, def.rom.image_filename)
output(" bootargs = %q,", rom.bootargs or def.rom.bootargs)
comment_default(rom.bootargs, def.rom.bootargs)
local dtb = config.dtb or {}
output(" dtb = {\n")
output(" image_filename = %q,", dtb.image_filename or def.dtb.image_filename)
comment_default(dtb.image_filename, def.dtb.image_filename)
output(" bootargs = %q,", dtb.bootargs or def.dtb.bootargs)
comment_default(dtb.bootargs, def.dtb.bootargs)
output(" },\n")
local tlb = config.tlb or {}
output(" tlb = {\n")
Expand Down Expand Up @@ -1342,9 +1341,9 @@ else
marchid = -1,
mvendorid = -1,
},
rom = {
image_filename = rom_image_filename,
bootargs = rom_bootargs,
dtb = {
image_filename = dtb_image_filename,
bootargs = dtb_bootargs,
},
ram = {
image_filename = ram_image_filename,
Expand All @@ -1370,13 +1369,13 @@ else
mtdparts[#mtdparts + 1] = string.format("flash.%d:-(%s)", i - 1, label)
end
if #mtdparts > 0 then
config.rom.bootargs = append(config.rom.bootargs, "mtdparts=" .. table.concat(mtdparts, ";"))
config.dtb.bootargs = append(config.dtb.bootargs, "mtdparts=" .. table.concat(mtdparts, ";"))
end

config.rom.bootargs = append(append(config.rom.bootargs, append_rom_bootargs), quiet and " splash=no" or "")
config.dtb.bootargs = append(append(config.dtb.bootargs, append_dtb_bootargs), quiet and " splash=no" or "")

if #exec_arguments > 0 then
config.rom.bootargs = append(config.rom.bootargs, "-- " .. table.concat(exec_arguments, " "))
config.dtb.bootargs = append(config.dtb.bootargs, "-- " .. table.concat(exec_arguments, " "))
end

if load_config then
Expand Down
18 changes: 9 additions & 9 deletions src/clua-machine-util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -770,10 +770,10 @@ static void push_cm_ram_config(lua_State *L, const cm_ram_config *r) {
}
}

/// \brief Pushes a cm_rom_config to the Lua stack
/// \brief Pushes a cm_dtb_config to the Lua stack
/// \param L Lua state.
/// \param r Ram configuration to be pushed.
static void push_cm_rom_config(lua_State *L, const cm_rom_config *r) {
static void push_cm_dtb_config(lua_State *L, const cm_dtb_config *r) {
lua_newtable(L);
if (r->bootargs != nullptr) {
clua_setstringfield(L, r->bootargs, "bootargs", -1);
Expand Down Expand Up @@ -905,8 +905,8 @@ void clua_push_cm_machine_config(lua_State *L, const cm_machine_config *c) {
lua_setfield(L, -2, "flash_drive"); // config
push_cm_ram_config(L, &c->ram); // config ram
lua_setfield(L, -2, "ram"); // config
push_cm_rom_config(L, &c->rom); // config rom
lua_setfield(L, -2, "rom"); // config
push_cm_dtb_config(L, &c->dtb); // config dtb
lua_setfield(L, -2, "dtb"); // config
push_cm_uarch_config(L, &c->uarch); // uarch
lua_setfield(L, -2, "uarch"); // config
if (c->rollup.has_value) {
Expand Down Expand Up @@ -942,12 +942,12 @@ static void check_cm_ram_config(lua_State *L, int tabidx, cm_ram_config *r) {
lua_pop(L, 1);
}

/// \brief Loads ROM config from Lua to cm_rom_config
/// \brief Loads DTB config from Lua to cm_dtb_config
/// \param L Lua state
/// \param tabidx Config stack index
/// \param r C api ROM config structure to receive results
static void check_cm_rom_config(lua_State *L, int tabidx, cm_rom_config *r) {
if (!opt_table_field(L, tabidx, "rom")) {
/// \param r C api DTB config structure to receive results
static void check_cm_dtb_config(lua_State *L, int tabidx, cm_dtb_config *r) {
if (!opt_table_field(L, tabidx, "dtb")) {
return;
}
r->image_filename = opt_copy_string_field(L, -1, "image_filename");
Expand Down Expand Up @@ -1206,7 +1206,7 @@ cm_machine_config *clua_check_cm_machine_config(lua_State *L, int tabidx, int ct
config->processor = get_default_processor_config(L);
check_cm_processor_config(L, tabidx, &config->processor, &config->processor);
check_cm_ram_config(L, tabidx, &config->ram);
check_cm_rom_config(L, tabidx, &config->rom);
check_cm_dtb_config(L, tabidx, &config->dtb);
check_cm_tlb_config(L, tabidx, &config->tlb);
check_cm_htif_config(L, tabidx, &config->htif);
check_cm_clint_config(L, tabidx, &config->clint);
Expand Down
4 changes: 2 additions & 2 deletions src/clua-machine-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ void cm_delete(cm_machine_runtime_config *ptr);
template <>
void cm_delete(cm_ram_config *p);

/// \brief Deleter for C api rom config
/// \brief Deleter for C api dtb config
template <>
void cm_delete(cm_rom_config *p);
void cm_delete(cm_dtb_config *p);

/// \brief Deleter for C api access log
template <>
Expand Down
28 changes: 7 additions & 21 deletions src/rom.cpp → src/dtb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
#include <string>

/// \file
/// \brief Bootstrap and device tree in ROM
/// \brief Device Tree Blob

#include "dtb.h"
#include "fdt-builder.h"
#include "machine-c-version.h"
#include "pma-constants.h"
#include "riscv-constants.h"
#include "rng-seed.h"
#include "rom.h"
#include "rtc.h"

namespace cartesi {
Expand All @@ -43,29 +43,15 @@ static std::string misa_to_isa_string(uint64_t misa) {
return ss.str();
}

void rom_init(const machine_config &c, unsigned char *rom_start, uint64_t length) {
if (length < PMA_ROM_EXTRASPACE_LENGTH_DEF) {
throw std::runtime_error{"not enough space on ROM for bootargs"};
}

// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
char *bootargs = reinterpret_cast<char *>(rom_start + length - PMA_ROM_EXTRASPACE_LENGTH_DEF);

if (!c.rom.bootargs.empty()) {
strncpy(bootargs, c.rom.bootargs.c_str(), PMA_BOOTARGS_LENGTH_DEF);
bootargs[PMA_BOOTARGS_LENGTH_DEF - 1] = '\0';
}
}

void rom_init_device_tree(const machine_config &c, unsigned char *rom_start, uint64_t length) {
void dtb_init(const machine_config &c, unsigned char *dtb_start, uint64_t dtb_length) {
using namespace std::string_literals;
constexpr uint32_t INTC_PHANDLE = 1;
constexpr uint32_t X_HOST = 13;
constexpr uint32_t BOOTARGS_MAX_LEN = 4096;

// Check if bootargs length is not too large
if (c.rom.bootargs.length() > BOOTARGS_MAX_LEN) {
throw std::runtime_error{"ROM bootargs is is above maximum length of 4096"};
if (c.dtb.bootargs.length() > BOOTARGS_MAX_LEN) {
throw std::runtime_error{"DTB bootargs is is above maximum length of 4096"};
}

FDTBuilder fdt;
Expand All @@ -80,7 +66,7 @@ void rom_init_device_tree(const machine_config &c, unsigned char *rom_start, uin

{ // chosen
fdt.begin_node("chosen");
fdt.prop_string("bootargs", c.rom.bootargs);
fdt.prop_string("bootargs", c.dtb.bootargs);
// ??(edubart): make this configurable in machine config?
fdt.prop("rng-seed", FDT_RNG_SEED, sizeof(FDT_RNG_SEED));
fdt.end_node();
Expand Down Expand Up @@ -217,7 +203,7 @@ void rom_init_device_tree(const machine_config &c, unsigned char *rom_start, uin
}

fdt.end();
fdt.finish(rom_start, length);
fdt.finish(dtb_start, dtb_length);
}

} // namespace cartesi
20 changes: 7 additions & 13 deletions src/rom.h → src/dtb.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
// with this program (see COPYING). If not, see <https://www.gnu.org/licenses/>.
//

#ifndef ROM_H
#define ROM_H
#ifndef DTB_H
#define DTB_H

/// \file
/// \brief Bootstrap and device tree in ROM
/// \brief Device Tree Blob

#include <cstdint>

Expand All @@ -29,17 +29,11 @@ namespace cartesi {
// Forward declarations
struct machine_config;

/// \brief Initializes PMA extension metadata on ROM
/// \brief Initializes flattened device tree from machine config on DTB
/// \param c Machine configuration.
/// \param rom_start Pointer to start of ROM contiguous range in host memory
/// \param length Maximum amount of ROM to use from start.
void rom_init(const machine_config &c, unsigned char *rom_start, uint64_t length);

/// \brief Initializes FDT metadata on ROM
/// \param c Machine configuration.
/// \param rom_start Pointer to start of ROM contiguous range in host memory
/// \param length Maximum amount of ROM to use from start.
void rom_init_device_tree(const machine_config &c, unsigned char *rom_start, uint64_t length);
/// \param dtb_start Pointer to start of DTB contiguous range in host memory
/// \param dtb_length Maximum amount of DTB to use from start.
void dtb_init(const machine_config &c, unsigned char *dtb_start, uint64_t dtb_length);

} // namespace cartesi

Expand Down
4 changes: 2 additions & 2 deletions src/i-virtual-machine.h
Original file line number Diff line number Diff line change
Expand Up @@ -631,8 +631,8 @@ class i_virtual_machine {
return do_reset_uarch_state();
}

/// \brief Reads the value of the microarchitecture ROM length
/// \returns The value of microarchitecture ROM length
/// \brief Reads the value of the microarchitecture RAM length
/// \returns The value of microarchitecture RAM length
uint64_t read_uarch_ram_length(void) const {
return do_read_uarch_ram_length();
}
Expand Down
2 changes: 1 addition & 1 deletion src/interpret.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5412,7 +5412,7 @@ static FORCE_INLINE fetch_status fetch_translate_pc_slow(STATE_ACCESS &a, uint64
}
// Walk memory map to find the range that contains the physical address
auto &pma = a.template find_pma_entry<uint16_t>(paddr);
// We only execute directly from RAM (as in "random access memory", which includes ROM)
// We only execute directly from RAM (as in "random access memory")
// If the range is not memory or not executable, this as a PMA violation
if (unlikely(!pma.get_istart_M() || !pma.get_istart_X())) {
pc = raise_exception(a, pc, MCAUSE_INSN_ACCESS_FAULT, vaddr);
Expand Down
Loading

0 comments on commit f17ab80

Please sign in to comment.