machine_internal.h 7.4 KiB raw
1
/* State and interfaces shared by the machine implementation files. */
2
#ifndef MACHINE_INTERNAL_H
3
#define MACHINE_INTERNAL_H
4
5
#include "machine.h"
6
#include "vector.h"
7
8
/* Fixed physical addresses, also published in the FDT. */
9
#define MACHINE_FDT_BASE    (MACHINE_RAM_BASE + 0x00800000U)
10
#define MACHINE_FDT_SIZE    0x00010000U
11
#define MACHINE_CLINT_BASE  0x02000000U
12
#define MACHINE_CLINT_SIZE  0x00010000U
13
#define MACHINE_PLIC_BASE   0x0c000000U
14
#define MACHINE_PLIC_SIZE   0x04000000U
15
#define MACHINE_UART_BASE   0x10000000U
16
#define MACHINE_UART_SIZE   0x00001000U
17
#define MACHINE_FINISH_BASE 0x10001000U
18
#define MACHINE_TIMEBASE    10000000U
19
20
/* Implemented mstatus fields. */
21
#define MSTATUS_MIE  (1ULL << 3)
22
#define MSTATUS_MPIE (1ULL << 7)
23
#define MSTATUS_MPP  (3ULL << 11)
24
25
/* Interrupt bits of mip and mie. The bit index is the interrupt cause. */
26
#define MIP_MSIP (1ULL << 3)
27
#define MIP_MTIP (1ULL << 7)
28
#define MIP_MEIP (1ULL << 11)
29
#define MIP_ALL  (MIP_MSIP | MIP_MTIP | MIP_MEIP)
30
31
/* The mcause bit that marks an interrupt. */
32
#define MCAUSE_INTERRUPT (1ULL << 63)
33
34
/* Synchronous exception causes. */
35
enum machine_cause {
36
    CAUSE_FETCH_MISALIGNED = 0,
37
    CAUSE_FETCH_FAULT      = 1,
38
    CAUSE_ILLEGAL          = 2,
39
    CAUSE_BREAKPOINT       = 3,
40
    CAUSE_LOAD_MISALIGNED  = 4,
41
    CAUSE_LOAD_FAULT       = 5,
42
    CAUSE_STORE_MISALIGNED = 6,
43
    CAUSE_STORE_FAULT      = 7,
44
    CAUSE_ECALL_U          = 8,
45
    CAUSE_ECALL_M          = 11,
46
};
47
48
/* Bus access flags. A plain load has none. */
49
enum machine_access {
50
    ACCESS_STORE  = 1,
51
    ACCESS_FETCH  = 2,
52
    ACCESS_ATOMIC = 4, /* Part of an LR, SC or AMO, which only reach RAM. */
53
};
54
55
/* Independent architectural state of one RV64IMA hart. */
56
struct machine_hart {
57
    u64  regs[32];         /* Integer registers; x0 is never written. */
58
    u64  pc;               /* Full-width physical instruction address. */
59
    u64  mstatus;          /* Implemented M/U status fields. */
60
    u64  mie;              /* MSIE, MTIE and MEIE enable bits. */
61
    u64  mtvec;            /* Aligned trap base and direct/vectored mode. */
62
    u64  mscratch;         /* Software-owned trap scratch register. */
63
    u64  mepc;             /* Aligned exception return address. */
64
    u64  mcause;           /* Last architectural trap cause. */
65
    u64  mtval;            /* Fault address or illegal instruction. */
66
    u64  minstret;         /* Retired instructions. */
67
    u32  id;               /* Stable zero-based hardware hart ID. */
68
    u8   privilege;        /* Architectural privilege: U=0, M=3. */
69
    bool waiting;          /* WFI sleep until an enabled interrupt pends. */
70
    bool reserved;         /* Whether the LR reservation is live. */
71
    u64  reservation;      /* Exact physical address of the last LR. */
72
    u32  reservation_size; /* Width of the last LR, 4 or 8 bytes. */
73
    bool wrote_instret;    /* The current instruction wrote minstret. */
74
75
    /* Integer vector registers and CSRs. */
76
    struct vector_state vector;
77
};
78
79
/* Shared CLINT state. mtime is writable, so it is kept apart from the
80
 * scheduler clock. */
81
struct machine_clint {
82
    u64 time;                        /* Writable mtime counter. */
83
    u64 compare[MACHINE_MAX_HARTS];  /* Per-hart mtimecmp, reset to all ones. */
84
    u32 software[MACHINE_MAX_HARTS]; /* Per-hart MSIP bit. */
85
};
86
87
/* PLIC gateway state with one M context per enabled hart. */
88
struct machine_plic {
89
    u32 priority[MACHINE_IRQ_COUNT + 1]; /* Source priority, WARL 0..7. */
90
    u32 pending;                         /* Latched requests not yet claimed. */
91
    u32 level;                           /* Asserted level-sensitive sources. */
92
    u32 active; /* Sources between claim and completion. */
93
    u8  owner[MACHINE_IRQ_COUNT + 1]; /* Context that owns an active source. */
94
    u32 enable[MACHINE_MAX_HARTS];    /* Per-context source enable bits. */
95
    u32 threshold[MACHINE_MAX_HARTS]; /* Per-context threshold, WARL 0..7. */
96
};
97
98
/* Transmit-only 16550 subset; transmission is synchronous. */
99
struct machine_uart {
100
    u8   ier;    /* Interrupt enable; only THRE (value 2) is modeled. */
101
    bool tx_irq; /* THRE interrupt latch: set when the holding register
102
                    empties or THRE is enabled, cleared by IIR reads. */
103
};
104
105
/* Whole-machine state, never shared with the hosted interpreter or JIT. */
106
struct machine {
107
    const struct machine_config *config; /* Borrowed immutable options. */
108
    struct machine_hart  harts[MACHINE_MAX_HARTS]; /* Independent live harts. */
109
    struct machine_clint clint;         /* Shared local interrupts. */
110
    struct machine_plic  plic;          /* Shared external interrupts. */
111
    struct machine_uart  uart;          /* Shared console device. */
112
    bool                 injected_irq;  /* One-shot edge has occurred. */
113
    u64                  ticks;         /* Monotonic scheduling time. */
114
    u64                  retired;       /* Total retired instructions. */
115
    u32                  next_hart;     /* Round-robin selection. */
116
    u64  stack_base[MACHINE_MAX_HARTS]; /* Reserved initial stack bases. */
117
    bool finished;                      /* Explicit platform termination. */
118
    int  result;                        /* Host process exit result. */
119
};
120
121
/* Return whether two finite physical intervals intersect. */
122
bool machine_overlap(u64 a, u64 asize, u64 b, u64 bsize);
123
/* Checked, naturally aligned bus access of 1, 2, 4 or 8 bytes. `access`
124
 * holds machine_access flags; fetches and atomics reach RAM only. On failure,
125
 * cause receives the exception code. No access uses a host pointer before
126
 * range validation. */
127
bool machine_bus(
128
    struct machine      *m,
129
    struct machine_hart *h,
130
    u64                  address,
131
    u32                  width,
132
    u32                  access,
133
    u64                 *value,
134
    u64                 *cause
135
);
136
/* Validate an atomic RAM store without performing it, including failed SC. */
137
bool machine_store_probe(
138
    const struct machine      *m,
139
    const struct machine_hart *h,
140
    u64                        address,
141
    u32                        width,
142
    u64                       *cause
143
);
144
/* Refresh the one-shot edge and level-sensitive device interrupt state. */
145
void machine_devices_update(struct machine *m);
146
/* Return the next future timer/edge wake tick, bounded by the run deadline. */
147
u64 machine_next_event(const struct machine *m);
148
/* Whether the PLIC has a notification above threshold for a context. */
149
bool machine_plic_notify(const struct machine *m, u32 context);
150
151
/* Derive a hart's hardware mip bits from CLINT and PLIC state. */
152
static inline u64 machine_pending(
153
    const struct machine *m, const struct machine_hart *h
154
) {
155
    u64 pending = 0;
156
    if (m->clint.software[h->id] & 1)
157
        pending |= MIP_MSIP;
158
    if (m->clint.time >= m->clint.compare[h->id])
159
        pending |= MIP_MTIP;
160
    if (machine_plic_notify(m, h->id))
161
        pending |= MIP_MEIP;
162
    return pending;
163
}
164
165
/* Deliver an exception or interrupt; no instruction retires. */
166
void machine_trap(
167
    struct machine *m, struct machine_hart *h, u64 cause, u64 value
168
);
169
/* Read/modify/write a CSR instruction; false means illegal instruction. */
170
bool machine_csr(struct machine *m, struct machine_hart *h, u32 instruction);
171
/* Execute at most one instruction, or take an interrupt. */
172
void machine_step(struct machine *m, struct machine_hart *h);
173
/* Write a bounded flattened device tree into the reserved RAM interval. */
174
bool machine_fdt(struct machine *m);
175
176
#endif /* MACHINE_INTERNAL_H */