2 * QEMU RISC-V PMP (Physical Memory Protection)
4 * Author: Daire McNamara, daire.mcnamara@emdalo.com
5 * Ivan Griffin, ivan.griffin@emdalo.com
7 * This provides a RISC-V Physical Memory Protection implementation
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms and conditions of the GNU General Public License,
11 * version 2 or later, as published by the Free Software Foundation.
13 * This program is distributed in the hope it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 * You should have received a copy of the GNU General Public License along with
19 * this program. If not, see <http://www.gnu.org/licenses/>.
23 * PMP (Physical Memory Protection) is as-of-yet unused and needs testing.
26 #include "qemu/osdep.h"
28 #include "qapi/error.h"
32 static void pmp_write_cfg(CPURISCVState
*env
, uint32_t addr_index
,
34 static uint8_t pmp_read_cfg(CPURISCVState
*env
, uint32_t addr_index
);
35 static void pmp_update_rule(CPURISCVState
*env
, uint32_t pmp_index
);
38 * Accessor method to extract address matching type 'a field' from cfg reg
40 static inline uint8_t pmp_get_a_field(uint8_t cfg
)
47 * Check whether a PMP is locked or not.
49 static inline int pmp_is_locked(CPURISCVState
*env
, uint32_t pmp_index
)
52 if (env
->pmp_state
.pmp
[pmp_index
].cfg_reg
& PMP_LOCK
) {
56 /* Top PMP has no 'next' to check */
57 if ((pmp_index
+ 1u) >= MAX_RISCV_PMPS
) {
61 /* In TOR mode, need to check the lock bit of the next pmp
62 * (if there is a next)
64 const uint8_t a_field
=
65 pmp_get_a_field(env
->pmp_state
.pmp
[pmp_index
+ 1].cfg_reg
);
66 if ((env
->pmp_state
.pmp
[pmp_index
+ 1u].cfg_reg
& PMP_LOCK
) &&
67 (PMP_AMATCH_TOR
== a_field
)) {
75 * Count the number of active rules.
77 static inline uint32_t pmp_get_num_rules(CPURISCVState
*env
)
79 return env
->pmp_state
.num_rules
;
83 * Accessor to get the cfg reg for a specific PMP/HART
85 static inline uint8_t pmp_read_cfg(CPURISCVState
*env
, uint32_t pmp_index
)
87 if (pmp_index
< MAX_RISCV_PMPS
) {
88 return env
->pmp_state
.pmp
[pmp_index
].cfg_reg
;
96 * Accessor to set the cfg reg for a specific PMP/HART
97 * Bounds checks and relevant lock bit.
99 static void pmp_write_cfg(CPURISCVState
*env
, uint32_t pmp_index
, uint8_t val
)
101 if (pmp_index
< MAX_RISCV_PMPS
) {
102 if (!pmp_is_locked(env
, pmp_index
)) {
103 env
->pmp_state
.pmp
[pmp_index
].cfg_reg
= val
;
104 pmp_update_rule(env
, pmp_index
);
106 qemu_log_mask(LOG_GUEST_ERROR
, "ignoring pmpcfg write - locked\n");
109 qemu_log_mask(LOG_GUEST_ERROR
,
110 "ignoring pmpcfg write - out of bounds\n");
114 static void pmp_decode_napot(target_ulong a
, target_ulong
*sa
, target_ulong
*ea
)
117 aaaa...aaa0 8-byte NAPOT range
118 aaaa...aa01 16-byte NAPOT range
119 aaaa...a011 32-byte NAPOT range
121 aa01...1111 2^XLEN-byte NAPOT range
122 a011...1111 2^(XLEN+1)-byte NAPOT range
123 0111...1111 2^(XLEN+2)-byte NAPOT range
131 target_ulong t1
= ctz64(~a
);
132 target_ulong base
= (a
& ~(((target_ulong
)1 << t1
) - 1)) << 2;
133 target_ulong range
= ((target_ulong
)1 << (t1
+ 3)) - 1;
140 /* Convert cfg/addr reg values here into simple 'sa' --> start address and 'ea'
141 * end address values.
142 * This function is called relatively infrequently whereas the check that
143 * an address is within a pmp rule is called often, so optimise that one
145 static void pmp_update_rule(CPURISCVState
*env
, uint32_t pmp_index
)
149 env
->pmp_state
.num_rules
= 0;
151 uint8_t this_cfg
= env
->pmp_state
.pmp
[pmp_index
].cfg_reg
;
152 target_ulong this_addr
= env
->pmp_state
.pmp
[pmp_index
].addr_reg
;
153 target_ulong prev_addr
= 0u;
154 target_ulong sa
= 0u;
155 target_ulong ea
= 0u;
157 if (pmp_index
>= 1u) {
158 prev_addr
= env
->pmp_state
.pmp
[pmp_index
- 1].addr_reg
;
161 switch (pmp_get_a_field(this_cfg
)) {
168 sa
= prev_addr
<< 2; /* shift up from [xx:0] to [xx+2:2] */
169 ea
= (this_addr
<< 2) - 1u;
173 sa
= this_addr
<< 2; /* shift up from [xx:0] to [xx+2:2] */
174 ea
= (this_addr
+ 4u) - 1u;
177 case PMP_AMATCH_NAPOT
:
178 pmp_decode_napot(this_addr
, &sa
, &ea
);
187 env
->pmp_state
.addr
[pmp_index
].sa
= sa
;
188 env
->pmp_state
.addr
[pmp_index
].ea
= ea
;
190 for (i
= 0; i
< MAX_RISCV_PMPS
; i
++) {
191 const uint8_t a_field
=
192 pmp_get_a_field(env
->pmp_state
.pmp
[i
].cfg_reg
);
193 if (PMP_AMATCH_OFF
!= a_field
) {
194 env
->pmp_state
.num_rules
++;
199 static int pmp_is_in_range(CPURISCVState
*env
, int pmp_index
, target_ulong addr
)
203 if ((addr
>= env
->pmp_state
.addr
[pmp_index
].sa
)
204 && (addr
<= env
->pmp_state
.addr
[pmp_index
].ea
)) {
219 * Check if the address has required RWX privs to complete desired operation
221 bool pmp_hart_has_privs(CPURISCVState
*env
, target_ulong addr
,
222 target_ulong size
, pmp_priv_t privs
, target_ulong mode
)
229 pmp_priv_t allowed_privs
= 0;
231 /* Short cut if no rules */
232 if (0 == pmp_get_num_rules(env
)) {
237 * if size is unknown (0), assume that all bytes
238 * from addr to the end of the page will be accessed.
241 pmp_size
= -(addr
| TARGET_PAGE_MASK
);
246 /* 1.10 draft priv spec states there is an implicit order
248 for (i
= 0; i
< MAX_RISCV_PMPS
; i
++) {
249 s
= pmp_is_in_range(env
, i
, addr
);
250 e
= pmp_is_in_range(env
, i
, addr
+ pmp_size
- 1);
252 /* partially inside */
254 qemu_log_mask(LOG_GUEST_ERROR
,
255 "pmp violation - access is partially inside\n");
261 const uint8_t a_field
=
262 pmp_get_a_field(env
->pmp_state
.pmp
[i
].cfg_reg
);
265 * If the PMP entry is not off and the address is in range, do the priv
268 if (((s
+ e
) == 2) && (PMP_AMATCH_OFF
!= a_field
)) {
269 allowed_privs
= PMP_READ
| PMP_WRITE
| PMP_EXEC
;
270 if ((mode
!= PRV_M
) || pmp_is_locked(env
, i
)) {
271 allowed_privs
&= env
->pmp_state
.pmp
[i
].cfg_reg
;
274 if ((privs
& allowed_privs
) == privs
) {
284 /* No rule matched */
287 ret
= 1; /* Privileged spec v1.10 states if no PMP entry matches an
288 * M-Mode access, the access succeeds */
290 ret
= 0; /* Other modes are not allowed to succeed if they don't
291 * match a rule, but there are rules. We've checked for
292 * no rule earlier in this function. */
296 return ret
== 1 ? true : false;
301 * Handle a write to a pmpcfg CSP
303 void pmpcfg_csr_write(CPURISCVState
*env
, uint32_t reg_index
,
309 trace_pmpcfg_csr_write(env
->mhartid
, reg_index
, val
);
311 if ((reg_index
& 1) && (sizeof(target_ulong
) == 8)) {
312 qemu_log_mask(LOG_GUEST_ERROR
,
313 "ignoring pmpcfg write - incorrect address\n");
317 for (i
= 0; i
< sizeof(target_ulong
); i
++) {
318 cfg_val
= (val
>> 8 * i
) & 0xff;
319 pmp_write_cfg(env
, (reg_index
* sizeof(target_ulong
)) + i
,
326 * Handle a read from a pmpcfg CSP
328 target_ulong
pmpcfg_csr_read(CPURISCVState
*env
, uint32_t reg_index
)
331 target_ulong cfg_val
= 0;
332 target_ulong val
= 0;
334 for (i
= 0; i
< sizeof(target_ulong
); i
++) {
335 val
= pmp_read_cfg(env
, (reg_index
* sizeof(target_ulong
)) + i
);
336 cfg_val
|= (val
<< (i
* 8));
338 trace_pmpcfg_csr_read(env
->mhartid
, reg_index
, cfg_val
);
345 * Handle a write to a pmpaddr CSP
347 void pmpaddr_csr_write(CPURISCVState
*env
, uint32_t addr_index
,
350 trace_pmpaddr_csr_write(env
->mhartid
, addr_index
, val
);
351 if (addr_index
< MAX_RISCV_PMPS
) {
352 if (!pmp_is_locked(env
, addr_index
)) {
353 env
->pmp_state
.pmp
[addr_index
].addr_reg
= val
;
354 pmp_update_rule(env
, addr_index
);
356 qemu_log_mask(LOG_GUEST_ERROR
,
357 "ignoring pmpaddr write - locked\n");
360 qemu_log_mask(LOG_GUEST_ERROR
,
361 "ignoring pmpaddr write - out of bounds\n");
367 * Handle a read from a pmpaddr CSP
369 target_ulong
pmpaddr_csr_read(CPURISCVState
*env
, uint32_t addr_index
)
371 target_ulong val
= 0;
373 if (addr_index
< MAX_RISCV_PMPS
) {
374 val
= env
->pmp_state
.pmp
[addr_index
].addr_reg
;
375 trace_pmpaddr_csr_read(env
->mhartid
, addr_index
, val
);
377 qemu_log_mask(LOG_GUEST_ERROR
,
378 "ignoring pmpaddr read - out of bounds\n");