hosted.h 5.7 KiB raw
1
/* Hosted mode: state shared by the emulator core, the image loaders of each
2
 * image format and the system calls of each call interface.
3
 *
4
 * Programs from every Radiance release run unchanged. An image format is
5
 * recognized by its magic bytes, and the two call interfaces use disjoint
6
 * call numbers, so neither needs a version field in the program. */
7
#ifndef HOSTED_H
8
#define HOSTED_H
9
10
#include <stdio.h>
11
12
#include "machine.h"
13
#include "riscv.h"
14
#include "types.h"
15
#include "vector.h"
16
17
/* Maximum physical memory size (384MB). The runtime can choose any size up to
18
 * this value with `-memory-size=...`. */
19
#define MEMORY_SIZE       (384 * 1024 * 1024)
20
/* Read-only data offset. */
21
#define DATA_RO_OFFSET    0x10000
22
/* Writable data region base, where data memory starts. */
23
#define DATA_MEMORY_START 0xFFFFF0
24
/* Maximum open files in guest runtime. */
25
#define MAX_OPEN_FILES    32
26
/* First call number of the host ABI. Linux call numbers lie below it. */
27
#define ABI_HOST_BASE     0x400
28
29
/* CPU state. */
30
struct cpu {
31
    u64      regs[REGISTERS];
32
    u32      pc;               /* Program counter. */
33
    u32      programsize;      /* Size of loaded program. */
34
    instr_t *program;          /* Program instructions. */
35
    bool     running;          /* Execution status. */
36
    bool     faulted;          /* There was a fault in execution. */
37
    bool     ebreak;           /* Program terminated via EBREAK. */
38
    reg_t    modified;         /* Index of the last modified register. */
39
    bool     reserved;         /* An LR reservation is held. */
40
    u32      reservation;      /* Address of the reservation. */
41
    u32      reservation_size; /* Width of the reserved access in bytes. */
42
43
    /* Integer vector registers and CSRs. */
44
    struct vector_state vector;
45
};
46
47
/* Guest memory sizes chosen on the command line, in bytes. */
48
struct guest_sizes {
49
    u32 memory; /* Physical memory. */
50
    u32 data;   /* Data memory, from DATA_MEMORY_START. */
51
    u32 stack;  /* Stack, at the end of memory. */
52
};
53
54
/* Where a hosted image was placed in guest memory. */
55
struct hosted_image {
56
    u32 code_base;  /* Address of the first instruction; execution starts. */
57
    u32 code_bytes; /* Bytes of code. */
58
    u32 data_bytes; /* Bytes of read-write data from DATA_MEMORY_START,
59
                     * including any zero-filled tail. */
60
};
61
62
/* CPU memory. */
63
extern u8 memory[MEMORY_SIZE];
64
65
/* Decode a little-endian word without relying on host alignment. */
66
static inline u32 decode_u32_le(const u8 *bytes) {
67
    return (u32)bytes[0] | ((u32)bytes[1] << 8) | ((u32)bytes[2] << 16) |
68
           ((u32)bytes[3] << 24);
69
}
70
71
/* Decode a little-endian doubleword without relying on host alignment. */
72
static inline u64 decode_u64_le(const u8 *bytes) {
73
    return (u64)decode_u32_le(bytes) | ((u64)decode_u32_le(bytes + 4) << 32);
74
}
75
76
/* Store a little-endian integer of `size` bytes. */
77
static inline void encode_le(u8 *bytes, u64 value, u32 size) {
78
    for (u32 i = 0; i < size; i++)
79
        bytes[i] = (u8)(value >> (8 * i));
80
}
81
82
/* Return the guest memory sizes. */
83
struct guest_sizes guest_sizes(void);
84
85
/* Return true if [addr, addr + len) lies in guest memory. Otherwise report the
86
 * named call as out of bounds, fault the guest at `pc` and return false. */
87
bool guest_check(struct cpu *cpu, u32 pc, const char *call, u64 addr, u64 len);
88
89
/* Add a host file descriptor to the guest table. Returns the guest descriptor,
90
 * or -1 when the table is full. */
91
int guest_fd_table_add(int host_fd);
92
93
/* Get the host descriptor of a guest descriptor, or -1 if it is not open. The
94
 * standard streams map to themselves. */
95
int guest_fd_table_get(int guest_fd);
96
97
/* Remove a guest descriptor from the table. */
98
void guest_fd_table_remove(int guest_fd);
99
100
/* Read exactly `size` bytes from `file` into guest memory at `offset`. */
101
void image_read(FILE *file, u32 offset, u32 size, const char *label);
102
103
/* Image loaders, one per image format. Each takes the open image of
104
 * `file_size` bytes, positioned anywhere, and places code and read-only data
105
 * in [DATA_RO_OFFSET, DATA_MEMORY_START) and read-write data in
106
 * [DATA_MEMORY_START, data_limit). They exit through `bail` on a malformed
107
 * image. */
108
109
/* Load a native image, which starts with the magic "RADIANCE". See
110
 * image_native.c. */
111
struct hosted_image image_native_load(
112
    FILE *file, long file_size, u32 data_limit
113
);
114
115
/* Load a native image in machine mode, where every segment must lie in the
116
 * `memory_size` bytes of RAM at MACHINE_RAM_BASE, and record its entry and
117
 * sections in `config`. See image_native.c. */
118
void image_native_load_machine(
119
    FILE *file, long file_size, u32 memory_size, struct machine_config *config
120
);
121
122
/* Load a "RAD0" version 1 image. See image_rad0.c. */
123
struct hosted_image image_rad0_load(FILE *file, long file_size, u32 data_limit);
124
125
/* Load a headerless image at `path`, with its data in sidecar files. See
126
 * image_raw.c. */
127
struct hosted_image image_raw_load(
128
    FILE *file, long file_size, const char *path, u32 data_limit
129
);
130
131
/* Return true if `header`, the first `len` bytes of an image, starts with the
132
 * native magic. */
133
bool image_native_match(const u8 *header, size_t len);
134
135
/* Return true if `header`, the first `len` bytes of an image, starts with the
136
 * "RAD0" magic. */
137
bool image_rad0_match(const u8 *header, size_t len);
138
139
/* System calls, one entry point per call interface. Each reads the call
140
 * number from a7 and its arguments from a0 and up, and stores the result in
141
 * a0 unless the call faulted or stopped the guest. */
142
143
/* Run a host ABI call, numbered from ABI_HOST_BASE. See abi_host.c. */
144
void abi_host_call(struct cpu *cpu, u32 pc);
145
146
/* Run a Linux-numbered call of older programs. See abi_linux.c. */
147
void abi_linux_call(struct cpu *cpu, u32 pc);
148
149
#endif