.githooks/
riscv/
test/
.clang-format
570 B
.gitignore
5 B
.gitsigners
112 B
AGENTS.md
227 B
LICENSE
1.1 KiB
Makefile
3.5 KiB
README
12.5 KiB
STYLE
781 B
abi_host.c
12.2 KiB
abi_linux.c
3.7 KiB
bitmanip.h
5.8 KiB
color.h
567 B
debug_map.c
5.1 KiB
debug_map.h
1.3 KiB
emulator.c
87.3 KiB
host.h
5.5 KiB
host_posix.c
11.2 KiB
hosted.h
5.7 KiB
image_native.c
4.6 KiB
image_rad0.c
2.7 KiB
image_raw.c
2.6 KiB
io.c
1.1 KiB
io.h
444 B
jit.c
99.2 KiB
jit.h
7.8 KiB
machine.c
4.4 KiB
machine.h
2.1 KiB
machine_cpu.c
13.6 KiB
machine_csr.c
4.6 KiB
machine_devices.c
10.8 KiB
machine_fdt.c
8.7 KiB
machine_internal.h
7.4 KiB
muldiv.h
1.0 KiB
profile.c
17.5 KiB
profile.h
1.4 KiB
riscv.c
755 B
riscv.h
4.3 KiB
types.h
1019 B
vector.c
54.0 KiB
vector.h
1.3 KiB
image_native.c
raw
| 1 | /* Native image format of current compilers. |
| 2 | * |
| 3 | * A 48-byte little-endian header, the magic "RADIANCE" followed by an address |
| 4 | * and size for the code and read-only data and an address, size and |
| 5 | * zero-filled tail for read-write data, then the bytes of the three segments. |
| 6 | * Execution starts at the code address. */ |
| 7 | #include <stdio.h> |
| 8 | |
| 9 | #include "hosted.h" |
| 10 | #include "io.h" |
| 11 | #include "machine.h" |
| 12 | #include "types.h" |
| 13 | |
| 14 | /* Native image magic: the ASCII bytes "RADIANCE" as a little-endian u64. */ |
| 15 | #define NATIVE_IMAGE_MAGIC 0x45434E4149444152ULL |
| 16 | |
| 17 | /* A native image segment: its address, the bytes stored in the file, and the |
| 18 | * zero-filled bytes that follow them in memory. */ |
| 19 | struct image_segment { |
| 20 | u64 base; |
| 21 | u32 size; |
| 22 | u32 tail; |
| 23 | }; |
| 24 | |
| 25 | /* A decoded native image: the code, read-only data and read-write data |
| 26 | * segments, in that order. Execution starts at the code address. */ |
| 27 | struct image { |
| 28 | struct image_segment segments[3]; |
| 29 | }; |
| 30 | |
| 31 | /* The address range [low, high) that each segment must lie in. */ |
| 32 | struct image_range { |
| 33 | u64 low[3]; |
| 34 | u64 high[3]; |
| 35 | }; |
| 36 | |
| 37 | bool image_native_match(const u8 *header, size_t len) { |
| 38 | return len >= sizeof(u64) && decode_u64_le(header) == NATIVE_IMAGE_MAGIC; |
| 39 | } |
| 40 | |
| 41 | /* Decode and check a native image of file_size bytes, leaving the file |
| 42 | * positioned at the payload. The caller has already checked the magic. */ |
| 43 | static struct image parse_image( |
| 44 | FILE *file, long file_size, const struct image_range *range |
| 45 | ) { |
| 46 | enum { HEADER_BYTES = 48 }; |
| 47 | u8 header[HEADER_BYTES]; |
| 48 | rewind(file); |
| 49 | if (fread(header, 1, sizeof(header), file) != sizeof(header)) |
| 50 | bail("truncated image header"); |
| 51 | |
| 52 | struct image image; |
| 53 | u64 expected = HEADER_BYTES; |
| 54 | for (u32 i = 0; i < 3; i++) { |
| 55 | const u8 *record = header + 8 + i * 12; |
| 56 | struct image_segment *seg = &image.segments[i]; |
| 57 | seg->base = decode_u64_le(record); |
| 58 | seg->size = decode_u32_le(record + 8); |
| 59 | seg->tail = i == 2 ? decode_u32_le(record + 12) : 0; |
| 60 | expected += seg->size; |
| 61 | u64 extent = (u64)seg->size + seg->tail; |
| 62 | if (extent && |
| 63 | (seg->base < range->low[i] || seg->base >= range->high[i] || |
| 64 | extent > range->high[i] - seg->base)) |
| 65 | bail("image segment outside memory"); |
| 66 | } |
| 67 | if (expected != (u64)file_size) |
| 68 | bail("image size does not match header"); |
| 69 | return image; |
| 70 | } |
| 71 | |
| 72 | /* Read the stored bytes of each segment into memory, with ram_base the address |
| 73 | * that memory offset 0 stands for. The zero-filled tails need no clearing: |
| 74 | * guest memory is static and the image loads once. */ |
| 75 | static void load_image_segments( |
| 76 | FILE *file, const struct image *image, u64 ram_base |
| 77 | ) { |
| 78 | for (u32 i = 0; i < 3; i++) { |
| 79 | const struct image_segment *seg = &image->segments[i]; |
| 80 | image_read(file, (u32)(seg->base - ram_base), seg->size, "native"); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | void image_native_load_machine( |
| 85 | FILE *file, long file_size, u32 memory_size, struct machine_config *config |
| 86 | ) { |
| 87 | u64 end = MACHINE_RAM_BASE + memory_size; |
| 88 | struct image_range range = { |
| 89 | .low = { MACHINE_RAM_BASE, MACHINE_RAM_BASE, MACHINE_RAM_BASE }, |
| 90 | .high = { end, end, end }, |
| 91 | }; |
| 92 | struct image image = parse_image(file, file_size, &range); |
| 93 | load_image_segments(file, &image, MACHINE_RAM_BASE); |
| 94 | |
| 95 | config->entry = image.segments[0].base; |
| 96 | struct machine_section *sections[] = { &config->text, |
| 97 | &config->rodata, |
| 98 | &config->rwdata }; |
| 99 | for (u32 i = 0; i < 3; i++) { |
| 100 | sections[i]->base = image.segments[i].base; |
| 101 | sections[i]->size = image.segments[i].size + image.segments[i].tail; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | struct hosted_image image_native_load( |
| 106 | FILE *file, long file_size, u32 data_limit |
| 107 | ) { |
| 108 | struct image_range range = { |
| 109 | .low = { DATA_RO_OFFSET, DATA_RO_OFFSET, DATA_MEMORY_START }, |
| 110 | .high = { DATA_MEMORY_START, DATA_MEMORY_START, data_limit }, |
| 111 | }; |
| 112 | struct image image = parse_image(file, file_size, &range); |
| 113 | load_image_segments(file, &image, 0); |
| 114 | |
| 115 | const struct image_segment *code = &image.segments[0]; |
| 116 | const struct image_segment *rwdata = &image.segments[2]; |
| 117 | u32 extent = rwdata->size + rwdata->tail; |
| 118 | /* The environment block must lie beyond the end of read-write data. */ |
| 119 | return (struct hosted_image){ |
| 120 | .code_base = (u32)code->base, |
| 121 | .code_bytes = code->size, |
| 122 | .data_bytes = |
| 123 | extent ? (u32)(rwdata->base - DATA_MEMORY_START) + extent : 0, |
| 124 | }; |
| 125 | } |