/* Hosted mode: state shared by the emulator core, the image loaders of each * image format and the system calls of each call interface. * * Programs from every Radiance release run unchanged. An image format is * recognized by its magic bytes, and the two call interfaces use disjoint * call numbers, so neither needs a version field in the program. */ #ifndef HOSTED_H #define HOSTED_H #include #include "machine.h" #include "riscv.h" #include "types.h" #include "vector.h" /* Maximum physical memory size (384MB). The runtime can choose any size up to * this value with `-memory-size=...`. */ #define MEMORY_SIZE (384 * 1024 * 1024) /* Read-only data offset. */ #define DATA_RO_OFFSET 0x10000 /* Writable data region base, where data memory starts. */ #define DATA_MEMORY_START 0xFFFFF0 /* Maximum open files in guest runtime. */ #define MAX_OPEN_FILES 32 /* First call number of the host ABI. Linux call numbers lie below it. */ #define ABI_HOST_BASE 0x400 /* CPU state. */ struct cpu { u64 regs[REGISTERS]; u32 pc; /* Program counter. */ u32 programsize; /* Size of loaded program. */ instr_t *program; /* Program instructions. */ bool running; /* Execution status. */ bool faulted; /* There was a fault in execution. */ bool ebreak; /* Program terminated via EBREAK. */ reg_t modified; /* Index of the last modified register. */ bool reserved; /* An LR reservation is held. */ u32 reservation; /* Address of the reservation. */ u32 reservation_size; /* Width of the reserved access in bytes. */ /* Integer vector registers and CSRs. */ struct vector_state vector; }; /* Guest memory sizes chosen on the command line, in bytes. */ struct guest_sizes { u32 memory; /* Physical memory. */ u32 data; /* Data memory, from DATA_MEMORY_START. */ u32 stack; /* Stack, at the end of memory. */ }; /* Where a hosted image was placed in guest memory. */ struct hosted_image { u32 code_base; /* Address of the first instruction; execution starts. */ u32 code_bytes; /* Bytes of code. */ u32 data_bytes; /* Bytes of read-write data from DATA_MEMORY_START, * including any zero-filled tail. */ }; /* CPU memory. */ extern u8 memory[MEMORY_SIZE]; /* Decode a little-endian word without relying on host alignment. */ static inline u32 decode_u32_le(const u8 *bytes) { return (u32)bytes[0] | ((u32)bytes[1] << 8) | ((u32)bytes[2] << 16) | ((u32)bytes[3] << 24); } /* Decode a little-endian doubleword without relying on host alignment. */ static inline u64 decode_u64_le(const u8 *bytes) { return (u64)decode_u32_le(bytes) | ((u64)decode_u32_le(bytes + 4) << 32); } /* Store a little-endian integer of `size` bytes. */ static inline void encode_le(u8 *bytes, u64 value, u32 size) { for (u32 i = 0; i < size; i++) bytes[i] = (u8)(value >> (8 * i)); } /* Return the guest memory sizes. */ struct guest_sizes guest_sizes(void); /* Return true if [addr, addr + len) lies in guest memory. Otherwise report the * named call as out of bounds, fault the guest at `pc` and return false. */ bool guest_check(struct cpu *cpu, u32 pc, const char *call, u64 addr, u64 len); /* Add a host file descriptor to the guest table. Returns the guest descriptor, * or -1 when the table is full. */ int guest_fd_table_add(int host_fd); /* Get the host descriptor of a guest descriptor, or -1 if it is not open. The * standard streams map to themselves. */ int guest_fd_table_get(int guest_fd); /* Remove a guest descriptor from the table. */ void guest_fd_table_remove(int guest_fd); /* Read exactly `size` bytes from `file` into guest memory at `offset`. */ void image_read(FILE *file, u32 offset, u32 size, const char *label); /* Image loaders, one per image format. Each takes the open image of * `file_size` bytes, positioned anywhere, and places code and read-only data * in [DATA_RO_OFFSET, DATA_MEMORY_START) and read-write data in * [DATA_MEMORY_START, data_limit). They exit through `bail` on a malformed * image. */ /* Load a native image, which starts with the magic "RADIANCE". See * image_native.c. */ struct hosted_image image_native_load( FILE *file, long file_size, u32 data_limit ); /* Load a native image in machine mode, where every segment must lie in the * `memory_size` bytes of RAM at MACHINE_RAM_BASE, and record its entry and * sections in `config`. See image_native.c. */ void image_native_load_machine( FILE *file, long file_size, u32 memory_size, struct machine_config *config ); /* Load a "RAD0" version 1 image. See image_rad0.c. */ struct hosted_image image_rad0_load(FILE *file, long file_size, u32 data_limit); /* Load a headerless image at `path`, with its data in sidecar files. See * image_raw.c. */ struct hosted_image image_raw_load( FILE *file, long file_size, const char *path, u32 data_limit ); /* Return true if `header`, the first `len` bytes of an image, starts with the * native magic. */ bool image_native_match(const u8 *header, size_t len); /* Return true if `header`, the first `len` bytes of an image, starts with the * "RAD0" magic. */ bool image_rad0_match(const u8 *header, size_t len); /* System calls, one entry point per call interface. Each reads the call * number from a7 and its arguments from a0 and up, and stores the result in * a0 unless the call faulted or stopped the guest. */ /* Run a host ABI call, numbered from ABI_HOST_BASE. See abi_host.c. */ void abi_host_call(struct cpu *cpu, u32 pc); /* Run a Linux-numbered call of older programs. See abi_linux.c. */ void abi_linux_call(struct cpu *cpu, u32 pc); #endif