machine_devices.c 10.8 KiB raw
1
/* Physical bus, CLINT, PLIC, UART and finish device. */
2
#include <stdint.h>
3
#include <stdio.h>
4
5
#include "machine_internal.h"
6
7
bool machine_overlap(u64 a, u64 asize, u64 b, u64 bsize) {
8
    if (!asize || !bsize)
9
        return false;
10
    return a <= b ? b - a < asize : a - b < bsize;
11
}
12
13
/* Return whether [address, address + width) lies in RAM, which lies above
14
 * every device aperture. */
15
static bool ram_range(const struct machine *m, u64 address, u32 width) {
16
    return address >= MACHINE_RAM_BASE &&
17
           address - MACHINE_RAM_BASE < m->config->memory_size &&
18
           width <= m->config->memory_size - (address - MACHINE_RAM_BASE);
19
}
20
21
/* Invalidate all reservations intersected by a store to RAM. */
22
static void invalidate(struct machine *m, u64 address, u32 width) {
23
    for (u32 i = 0; i < m->config->hart_count; i++) {
24
        struct machine_hart *h = &m->harts[i];
25
        if (h->reserved &&
26
            machine_overlap(
27
                address, width, h->reservation, h->reservation_size
28
            ))
29
            h->reserved = false;
30
    }
31
}
32
33
/* Choose highest priority, then lowest source ID as required by the PLIC. */
34
static u32 plic_best(const struct machine *m, u32 context, bool notification) {
35
    u32 eligible = m->plic.pending & m->plic.enable[context];
36
    if (!eligible)
37
        return 0;
38
    u32 best = 0, priority = notification ? m->plic.threshold[context] : 0;
39
    for (u32 id = 1; id <= MACHINE_IRQ_COUNT; id++) {
40
        if ((eligible & (1U << id)) && m->plic.priority[id] > priority) {
41
            best     = id;
42
            priority = m->plic.priority[id];
43
        }
44
    }
45
    return best;
46
}
47
48
/* The UART interrupt line is asserted while an enabled THRE interrupt waits. */
49
static bool uart_interrupt(const struct machine *m) {
50
    return (m->uart.ier & 2) && m->uart.tx_irq;
51
}
52
53
void machine_devices_update(struct machine *m) {
54
    if (!m->injected_irq && m->config->irq_source &&
55
        m->ticks >= m->config->irq_at) {
56
        u32 bit = 1U << m->config->irq_source;
57
        if (!(m->plic.active & bit))
58
            m->plic.pending |= bit;
59
        m->injected_irq = true;
60
    }
61
    u32 bit = 1U << MACHINE_UART_IRQ;
62
    if (uart_interrupt(m))
63
        m->plic.level |= bit;
64
    else
65
        m->plic.level &= ~bit;
66
    m->plic.pending |= m->plic.level & ~m->plic.active;
67
}
68
69
bool machine_plic_notify(const struct machine *m, u32 context) {
70
    return plic_best(m, context, true) != 0;
71
}
72
73
/* Lower *next to candidate when candidate lies after now. */
74
static void earlier(u64 now, u64 candidate, u64 *next) {
75
    if (candidate > now && candidate < *next)
76
        *next = candidate;
77
}
78
79
u64 machine_next_event(const struct machine *m) {
80
    u64 next = m->config->max_steps;
81
    for (u32 id = 0; id < m->config->hart_count; id++) {
82
        u64 compare = m->clint.compare[id];
83
        if ((m->harts[id].mie & MIP_MTIP) && compare > m->clint.time) {
84
            u64 delta = compare - m->clint.time;
85
            if (delta <= UINT64_MAX - m->ticks)
86
                earlier(m->ticks, m->ticks + delta, &next);
87
        }
88
    }
89
    if (!m->injected_irq && m->config->irq_source)
90
        earlier(m->ticks, m->config->irq_at, &next);
91
    return next;
92
}
93
94
/* CLINT supports naturally aligned RV32 halves and RV64 timer accesses. */
95
static bool clint_access(
96
    struct machine *m, u32 offset, u32 width, bool store, u64 *value
97
) {
98
    if (offset < 4 * m->config->hart_count && width == 4) {
99
        u32 *word = &m->clint.software[offset / 4];
100
        if (store)
101
            *word = (u32)*value & 1;
102
        else
103
            *value = *word;
104
        return true;
105
    }
106
    u64 *reg;
107
    u32  half;
108
    if (offset >= 0x4000 && offset < 0x4000 + 8 * m->config->hart_count) {
109
        reg  = &m->clint.compare[(offset - 0x4000) / 8];
110
        half = offset & 7;
111
    } else if (offset >= 0xbff8 && offset < 0xc000) {
112
        reg  = &m->clint.time;
113
        half = offset - 0xbff8;
114
    } else {
115
        return false;
116
    }
117
    if (width == 8 && half == 0) {
118
        if (store)
119
            *reg = *value;
120
        else
121
            *value = *reg;
122
    } else if (width == 4 && (half == 0 || half == 4)) {
123
        u32 shift = half * 8;
124
        if (store)
125
            *reg = (*reg & ~(0xffffffffULL << shift)) |
126
                   ((u64)(u32)*value << shift);
127
        else
128
            *value = (u32)(*reg >> shift);
129
    } else {
130
        return false;
131
    }
132
    return true;
133
}
134
135
static bool plic_access(
136
    struct machine *m, u32 offset, u32 width, bool store, u64 *value
137
) {
138
    struct machine_plic *p = &m->plic;
139
    if (width != 4)
140
        return false;
141
    if (offset <= MACHINE_IRQ_COUNT * 4) {
142
        u32 id = offset / 4;
143
        if (store) {
144
            if (id)
145
                p->priority[id] = (u32)*value & 7;
146
        } else
147
            *value = p->priority[id];
148
        return true;
149
    }
150
    if (offset == 0x1000) {
151
        if (store)
152
            return false;
153
        *value = p->pending;
154
        return true;
155
    }
156
    if (offset >= 0x2000 && offset < 0x2000 + m->config->hart_count * 0x80 &&
157
        (offset & 0x7f) == 0) {
158
        u32 context = (offset - 0x2000) / 0x80;
159
        if (store)
160
            p->enable[context] = (u32)*value & ~1U;
161
        else
162
            *value = p->enable[context];
163
        return true;
164
    }
165
    if (offset >= 0x200000 &&
166
        offset < 0x200000 + m->config->hart_count * 0x1000) {
167
        u32 context = (offset - 0x200000) / 0x1000;
168
        u32 reg     = offset & 0xfff;
169
        if (reg == 0) {
170
            if (store)
171
                p->threshold[context] = (u32)*value & 7;
172
            else
173
                *value = p->threshold[context];
174
            return true;
175
        }
176
        if (reg == 4) {
177
            if (store) {
178
                u32 id = (u32)*value;
179
                if (id && id <= MACHINE_IRQ_COUNT && (p->active & (1U << id)) &&
180
                    p->owner[id] == context &&
181
                    (p->enable[context] & (1U << id))) {
182
                    p->active  &= ~(1U << id);
183
                    p->pending |= p->level & (1U << id);
184
                }
185
            } else {
186
                u32 id = plic_best(m, context, false);
187
                *value = id;
188
                if (id) {
189
                    p->pending   &= ~(1U << id);
190
                    p->active    |= 1U << id;
191
                    p->owner[id]  = (u8)context;
192
                }
193
            }
194
            return true;
195
        }
196
    }
197
    return false;
198
}
199
200
/* Registers: THR at 0, IER at 1, IIR at 2 (read) and LSR at 5 (read). */
201
static bool uart_access(
202
    struct machine *m, u32 offset, u32 width, bool store, u64 *value
203
) {
204
    struct machine_uart *u = &m->uart;
205
    if (width != 1)
206
        return false;
207
    if (store) {
208
        switch (offset) {
209
        case 0:
210
            if (fputc((u8)*value, stdout) == EOF || fflush(stdout) == EOF) {
211
                fprintf(stderr, "machine UART output failed\n");
212
                m->finished = true;
213
                m->result   = 2;
214
            }
215
            u->tx_irq = true;
216
            break;
217
        case 1:
218
            if (!(u->ier & 2) && ((u8)*value & 2))
219
                u->tx_irq = true;
220
            u->ier = (u8)*value & 2;
221
            break;
222
        default:
223
            return false;
224
        }
225
        return true;
226
    }
227
    switch (offset) {
228
    case 1:
229
        *value = u->ier;
230
        break;
231
    case 2:
232
        if (uart_interrupt(m)) {
233
            *value    = 2; /* THR empty. */
234
            u->tx_irq = false;
235
        } else
236
            *value = 1; /* No interrupt pending. */
237
        break;
238
    case 5:
239
        *value = 0x60;
240
        break;
241
    default:
242
        return false;
243
    }
244
    return true;
245
}
246
247
/* The finish register reads as zero and accepts the success value 0x5555 or
248
 * a failure value 0xNNNN3333, which stop the machine. */
249
static bool finish_access(
250
    struct machine *m, struct machine_hart *h, bool store, u64 *value
251
) {
252
    if (!store) {
253
        *value = 0;
254
        return true;
255
    }
256
    u32 code = (u32)*value;
257
    if (code != 0x5555 && (code & 0xffff) != 0x3333)
258
        return false;
259
    m->finished = true;
260
    m->result   = code == 0x5555 ? 0 : 1;
261
    if (m->result)
262
        fprintf(
263
            stderr,
264
            "machine guest failure: code=%u hart=%u pc=0x%llx\n",
265
            code >> 16,
266
            h->id,
267
            h->pc
268
        );
269
    return true;
270
}
271
272
bool machine_store_probe(
273
    const struct machine      *m,
274
    const struct machine_hart *h,
275
    u64                        address,
276
    u32                        width,
277
    u64                       *cause
278
) {
279
    bool misaligned = (address & (width - 1)) != 0;
280
    *cause          = misaligned ? CAUSE_STORE_MISALIGNED : CAUSE_STORE_FAULT;
281
    if (misaligned || !ram_range(m, address, width))
282
        return false;
283
    return h->privilege != 0 ||
284
           (!machine_overlap(
285
                address, width, m->config->text.base, m->config->text.size
286
            ) &&
287
            !machine_overlap(
288
                address, width, m->config->rodata.base, m->config->rodata.size
289
            ));
290
}
291
292
bool machine_bus(
293
    struct machine      *m,
294
    struct machine_hart *h,
295
    u64                  address,
296
    u32                  width,
297
    u32                  access,
298
    u64                 *value,
299
    u64                 *cause
300
) {
301
    bool store = (access & ACCESS_STORE) != 0;
302
    bool fetch = (access & ACCESS_FETCH) != 0;
303
    if (address & (width - 1)) {
304
        *cause = fetch   ? CAUSE_FETCH_MISALIGNED
305
                 : store ? CAUSE_STORE_MISALIGNED
306
                         : CAUSE_LOAD_MISALIGNED;
307
        return false;
308
    }
309
    *cause = fetch   ? CAUSE_FETCH_FAULT
310
             : store ? CAUSE_STORE_FAULT
311
                     : CAUSE_LOAD_FAULT;
312
    if (ram_range(m, address, width)) {
313
        u8 *ram = m->config->ram + (address - MACHINE_RAM_BASE);
314
        if (store) {
315
            if (!machine_store_probe(m, h, address, width, cause))
316
                return false;
317
            for (u32 i = 0; i < width; i++)
318
                ram[i] = (u8)(*value >> (i * 8));
319
            invalidate(m, address, width);
320
        } else {
321
            u64 v = 0;
322
            for (u32 i = 0; i < width; i++)
323
                v |= (u64)ram[i] << (i * 8);
324
            *value = v;
325
        }
326
        return true;
327
    }
328
    if (access & (ACCESS_FETCH | ACCESS_ATOMIC))
329
        return false;
330
    if (address >= MACHINE_CLINT_BASE &&
331
        address < MACHINE_CLINT_BASE + MACHINE_CLINT_SIZE)
332
        return clint_access(
333
            m, (u32)(address - MACHINE_CLINT_BASE), width, store, value
334
        );
335
    if (address >= MACHINE_PLIC_BASE &&
336
        address < MACHINE_PLIC_BASE + MACHINE_PLIC_SIZE)
337
        return plic_access(
338
            m, (u32)(address - MACHINE_PLIC_BASE), width, store, value
339
        );
340
    if (address >= MACHINE_UART_BASE &&
341
        address < MACHINE_UART_BASE + MACHINE_UART_SIZE)
342
        return uart_access(
343
            m, (u32)(address - MACHINE_UART_BASE), width, store, value
344
        );
345
    if (address == MACHINE_FINISH_BASE && width == 4)
346
        return finish_access(m, h, store, value);
347
    return false;
348
}