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 committed Jul 25, 2023
1 parent 147a7bd commit 444606c
Show file tree
Hide file tree
Showing 30 changed files with 228 additions and 290 deletions.
6 changes: 3 additions & 3 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ CARTESI_OBJS:= \
pma-driver.o \
clint.o \
clint-factory.o \
rom.o \
dtb.o \
tty.o \
htif.o \
htif-factory.o \
Expand Down Expand Up @@ -559,7 +559,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 @@ -588,7 +588,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
61 changes: 30 additions & 31 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-bootargs
clear default bootargs.
--append-rom-bootargs=<string>
--append-bootargs=<string>
append <string> to bootargs.
--no-root-flash-drive
Expand Down Expand Up @@ -359,7 +359,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 @@ -428,10 +427,11 @@ 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/pmem0 rw quiet \z
local dtb_image_filename = nil
local bootargs = "console=hvc0 rootfstype=ext2 root=/dev/pmem0 rw quiet \z
swiotlb=noforce random.trust_bootloader=on"
local init_splash = true
local append_bootargs = ""
local append_init = ""
local append_entrypoint = ""
local rollup
Expand All @@ -441,7 +441,6 @@ 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 htif_no_console_putchar = false
local htif_console_getchar = false
local htif_yield_automatic = false
Expand Down Expand Up @@ -537,7 +536,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 @@ -553,26 +552,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%-bootargs$",
function(all)
if not all then return false end
rom_bootargs = ""
bootargs = ""
return true
end,
},
{
"^%-%-append%-rom%-bootargs%=(.*)$",
"^%-%-append%-bootargs%=(.*)$",
function(o)
if not o or #o < 1 then return false end
append_rom_bootargs = o
append_bootargs = o
return true
end,
},
Expand Down Expand Up @@ -866,7 +865,7 @@ local options = {
flash_length.root = nil
flash_shared.root = nil
table.remove(flash_label_order, 1)
rom_bootargs = "console=hvc0"
bootargs = "console=hvc0"
return true
end,
},
Expand Down Expand Up @@ -1250,12 +1249,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 @@ -1421,9 +1420,9 @@ else
marchid = -1,
mvendorid = -1,
},
rom = {
image_filename = rom_image_filename,
bootargs = rom_bootargs,
dtb = {
image_filename = dtb_image_filename,
bootargs = bootargs,
init = "",
entrypoint = "",
},
Expand All @@ -1443,7 +1442,7 @@ else

-- show splash on init
if init_splash then
config.rom.init = config.rom.init
config.dtb.init = config.dtb.init
.. ([[
echo "
.
Expand All @@ -1468,20 +1467,20 @@ echo "
length = flash_length[label] or -1,
}
if label ~= "root" and flash_mount[label] then
config.rom.init = config.rom.init
config.dtb.init = config.dtb.init
.. ([[
busybox mkdir "/mnt/LABEL" && busybox mount "/dev/DEVNAME" "/mnt/LABEL"
]]):gsub("LABEL", label):gsub("DEVNAME", devname)
end
end

if #append_init > 0 then config.rom.init = config.rom.init .. append_init end
if #append_init > 0 then config.dtb.init = config.dtb.init .. append_init end

if append_rom_bootargs then config.rom.bootargs = config.rom.bootargs .. " " .. append_rom_bootargs end
if #append_bootargs > 0 then config.dtb.bootargs = config.dtb.bootargs .. " " .. append_bootargs end

if #append_entrypoint > 0 then config.rom.entrypoint = config.rom.entrypoint .. append_entrypoint end
if #append_entrypoint > 0 then config.dtb.entrypoint = config.dtb.entrypoint .. append_entrypoint end

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

if load_config then
local env = {}
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 @@ -768,10 +768,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 @@ -909,8 +909,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 @@ -946,12 +946,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 @@ -1212,7 +1212,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
32 changes: 9 additions & 23 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 All @@ -90,8 +76,8 @@ void rom_init_device_tree(const machine_config &c, unsigned char *rom_start, uin
fdt.begin_node("cartesi");
// We add emulator version, so can inspect it from inside the machine by reading the FDT
fdt.prop_string_data("version", CM_VERSION);
fdt.prop_string_data("init", c.rom.init);
fdt.prop_string_data("entrypoint", c.rom.entrypoint);
fdt.prop_string_data("init", c.dtb.init);
fdt.prop_string_data("entrypoint", c.dtb.entrypoint);
fdt.end_node();
}

Expand Down Expand Up @@ -216,7 +202,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 @@
// along with the machine-emulator. If not, see http://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
Loading

0 comments on commit 444606c

Please sign in to comment.