machine_csr.c 4.6 KiB raw
1
/* Traps and the machine CSR file. */
2
#include <stdio.h>
3
4
#include "machine_internal.h"
5
6
/* Implemented CSR numbers. */
7
enum {
8
    CSR_MSTATUS  = 0x300,
9
    CSR_MIE      = 0x304,
10
    CSR_MTVEC    = 0x305,
11
    CSR_MSCRATCH = 0x340,
12
    CSR_MEPC     = 0x341,
13
    CSR_MCAUSE   = 0x342,
14
    CSR_MTVAL    = 0x343,
15
    CSR_MIP      = 0x344,
16
    CSR_MINSTRET = 0xb02,
17
    CSR_INSTRET  = 0xc02,
18
    CSR_MHARTID  = 0xf14,
19
};
20
21
void machine_trap(
22
    struct machine *m, struct machine_hart *h, u64 cause, u64 value
23
) {
24
    u64 old      = h->mstatus;
25
    h->mstatus   = (old & ~(MSTATUS_MPP | MSTATUS_MPIE | MSTATUS_MIE)) |
26
                   ((u64)h->privilege << 11) | ((old & MSTATUS_MIE) << 4);
27
    h->mepc      = h->pc & ~3ULL;
28
    h->mcause    = cause;
29
    h->mtval     = value;
30
    h->privilege = 3;
31
    h->waiting   = false;
32
    h->reserved  = false;
33
    if (m->config->trace)
34
        fprintf(
35
            stderr,
36
            "hart=%u trap pc=0x%llx cause=0x%llx value=0x%llx\n",
37
            h->id,
38
            h->pc,
39
            cause,
40
            value
41
        );
42
    h->pc = h->mtvec & ~3ULL;
43
    if ((cause & MCAUSE_INTERRUPT) && (h->mtvec & 3) == 1)
44
        h->pc += 4 * (cause & ~MCAUSE_INTERRUPT);
45
}
46
47
/* Keep the implemented mstatus fields; MPP holds only M or U. */
48
static u64 status_warl(u64 value) {
49
    u64 result =
50
        value & (MSTATUS_MIE | MSTATUS_MPIE | MSTATUS_MPP | (3ULL << 9));
51
    if ((result & MSTATUS_MPP) != MSTATUS_MPP)
52
        result &= ~MSTATUS_MPP;
53
    return result;
54
}
55
56
static bool csr_read(
57
    struct machine *m, struct machine_hart *h, u32 csr, u64 *value
58
) {
59
    if (h->privilege < ((csr >> 8) & 3))
60
        return false;
61
    if (csr == CSR_INSTRET && h->privilege == 0)
62
        return false; /* mcounteren is zero. */
63
    switch (csr) {
64
    case CSR_MSTATUS:
65
        *value =
66
            h->mstatus | (2ULL << 32) |
67
            (((h->mstatus >> 9) & 3) == 3 ? 1ULL << 63
68
                                          : 0); /* UXL: U mode is 64-bit. */
69
        break;
70
    case CSR_MIE:
71
        *value = h->mie;
72
        break;
73
    case CSR_MTVEC:
74
        *value = h->mtvec;
75
        break;
76
    case CSR_MSCRATCH:
77
        *value = h->mscratch;
78
        break;
79
    case CSR_MEPC:
80
        *value = h->mepc;
81
        break;
82
    case CSR_MCAUSE:
83
        *value = h->mcause;
84
        break;
85
    case CSR_MTVAL:
86
        *value = h->mtval;
87
        break;
88
    case CSR_MIP:
89
        *value = machine_pending(m, h);
90
        break;
91
    case CSR_MINSTRET:
92
    case CSR_INSTRET:
93
        *value = h->minstret;
94
        break;
95
    case CSR_MHARTID:
96
        *value = h->id;
97
        break;
98
    default:
99
        return false;
100
    }
101
    return true;
102
}
103
104
static void csr_write(struct machine_hart *h, u32 csr, u64 value) {
105
    switch (csr) {
106
    case CSR_MSTATUS:
107
        h->mstatus = status_warl(value);
108
        break;
109
    case CSR_MIE:
110
        h->mie = value & MIP_ALL;
111
        break;
112
    case CSR_MTVEC:
113
        h->mtvec = (value & ~3ULL) | ((value & 3) == 1 ? 1 : 0);
114
        break;
115
    case CSR_MSCRATCH:
116
        h->mscratch = value;
117
        break;
118
    case CSR_MEPC:
119
        h->mepc = value & ~3ULL;
120
        break;
121
    case CSR_MCAUSE:
122
        h->mcause = value;
123
        break;
124
    case CSR_MTVAL:
125
        h->mtval = value;
126
        break;
127
    case CSR_MIP:
128
        break; /* MSIP/MTIP/MEIP are device-owned read-only fields. */
129
    case CSR_MINSTRET:
130
        /* An explicit write suppresses this instruction's increment. */
131
        h->minstret      = value;
132
        h->wrote_instret = true;
133
        break;
134
    }
135
}
136
137
bool machine_csr(struct machine *m, struct machine_hart *h, u32 instruction) {
138
    u32 vector_address = instruction >> 20;
139
    if (vector_address == 8 || vector_address == 9 || vector_address == 10 ||
140
        vector_address == 15 ||
141
        (vector_address >= 0xc20 && vector_address <= 0xc22)) {
142
        if (!(h->mstatus & (3ULL << 9)))
143
            return false;
144
        if (!vector_csr(&h->vector, instruction, h->regs))
145
            return false;
146
        h->mstatus |= 3ULL << 9;
147
        return true;
148
    }
149
150
    u32 funct = (instruction >> 12) & 7;
151
    u32 csr   = instruction >> 20;
152
    u32 rd    = (instruction >> 7) & 31;
153
    u32 rs    = (instruction >> 15) & 31;
154
    if (funct == 0 || funct == 4)
155
        return false;
156
    bool writes = (funct & 3) == 1 || rs != 0;
157
    if (writes && (csr >> 10) == 3)
158
        return false;
159
    u64 old;
160
    if (!csr_read(m, h, csr, &old))
161
        return false;
162
    u64 source = (funct & 4) ? rs : h->regs[rs];
163
    if (writes) {
164
        u64 value = (funct & 3) == 1   ? source
165
                    : (funct & 3) == 2 ? old | source
166
                                       : old & ~source;
167
        csr_write(h, csr, value);
168
    }
169
    if (rd)
170
        h->regs[rd] = old;
171
    return true;
172
}