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/>.
22 #include "qemu/osdep.h"
24 #include "qapi/error.h"
27 #include "exec/exec-all.h"
29 static void pmp_write_cfg(CPURISCVState
*env
, uint32_t addr_index
,
31 static uint8_t pmp_read_cfg(CPURISCVState
*env
, uint32_t addr_index
);
32 static void pmp_update_rule(CPURISCVState
*env
, uint32_t pmp_index
);
35 * Accessor method to extract address matching type 'a field' from cfg reg
37 static inline uint8_t pmp_get_a_field(uint8_t cfg
)
44 * Check whether a PMP is locked or not.
46 static inline int pmp_is_locked(CPURISCVState
*env
, uint32_t pmp_index
)
49 if (env
->pmp_state
.pmp
[pmp_index
].cfg_reg
& PMP_LOCK
) {
53 /* Top PMP has no 'next' to check */
54 if ((pmp_index
+ 1u) >= MAX_RISCV_PMPS
) {
62 * Count the number of active rules.
64 uint32_t pmp_get_num_rules(CPURISCVState
*env
)
66 return env
->pmp_state
.num_rules
;
70 * Accessor to get the cfg reg for a specific PMP/HART
72 static inline uint8_t pmp_read_cfg(CPURISCVState
*env
, uint32_t pmp_index
)
74 if (pmp_index
< MAX_RISCV_PMPS
) {
75 return env
->pmp_state
.pmp
[pmp_index
].cfg_reg
;
83 * Accessor to set the cfg reg for a specific PMP/HART
84 * Bounds checks and relevant lock bit.
86 static void pmp_write_cfg(CPURISCVState
*env
, uint32_t pmp_index
, uint8_t val
)
88 if (pmp_index
< MAX_RISCV_PMPS
) {
91 if (riscv_cpu_cfg(env
)->epmp
) {
92 /* mseccfg.RLB is set */
93 if (MSECCFG_RLB_ISSET(env
)) {
97 /* mseccfg.MML is not set */
98 if (!MSECCFG_MML_ISSET(env
) && !pmp_is_locked(env
, pmp_index
)) {
102 /* mseccfg.MML is set */
103 if (MSECCFG_MML_ISSET(env
)) {
104 /* not adding execute bit */
105 if ((val
& PMP_LOCK
) != 0 && (val
& PMP_EXEC
) != PMP_EXEC
) {
108 /* shared region and not adding X bit */
109 if ((val
& PMP_LOCK
) != PMP_LOCK
&&
110 (val
& 0x7) != (PMP_WRITE
| PMP_EXEC
)) {
115 if (!pmp_is_locked(env
, pmp_index
)) {
121 qemu_log_mask(LOG_GUEST_ERROR
, "ignoring pmpcfg write - locked\n");
123 env
->pmp_state
.pmp
[pmp_index
].cfg_reg
= val
;
124 pmp_update_rule(env
, pmp_index
);
127 qemu_log_mask(LOG_GUEST_ERROR
,
128 "ignoring pmpcfg write - out of bounds\n");
132 static void pmp_decode_napot(target_ulong a
, target_ulong
*sa
,
136 * aaaa...aaa0 8-byte NAPOT range
137 * aaaa...aa01 16-byte NAPOT range
138 * aaaa...a011 32-byte NAPOT range
140 * aa01...1111 2^XLEN-byte NAPOT range
141 * a011...1111 2^(XLEN+1)-byte NAPOT range
142 * 0111...1111 2^(XLEN+2)-byte NAPOT range
143 * 1111...1111 Reserved
150 void pmp_update_rule_addr(CPURISCVState
*env
, uint32_t pmp_index
)
152 uint8_t this_cfg
= env
->pmp_state
.pmp
[pmp_index
].cfg_reg
;
153 target_ulong this_addr
= env
->pmp_state
.pmp
[pmp_index
].addr_reg
;
154 target_ulong prev_addr
= 0u;
155 target_ulong sa
= 0u;
156 target_ulong ea
= 0u;
158 if (pmp_index
>= 1u) {
159 prev_addr
= env
->pmp_state
.pmp
[pmp_index
- 1].addr_reg
;
162 switch (pmp_get_a_field(this_cfg
)) {
169 sa
= prev_addr
<< 2; /* shift up from [xx:0] to [xx+2:2] */
170 ea
= (this_addr
<< 2) - 1u;
177 sa
= this_addr
<< 2; /* shift up from [xx:0] to [xx+2:2] */
181 case PMP_AMATCH_NAPOT
:
182 pmp_decode_napot(this_addr
, &sa
, &ea
);
191 env
->pmp_state
.addr
[pmp_index
].sa
= sa
;
192 env
->pmp_state
.addr
[pmp_index
].ea
= ea
;
195 void pmp_update_rule_nums(CPURISCVState
*env
)
199 env
->pmp_state
.num_rules
= 0;
200 for (i
= 0; i
< MAX_RISCV_PMPS
; i
++) {
201 const uint8_t a_field
=
202 pmp_get_a_field(env
->pmp_state
.pmp
[i
].cfg_reg
);
203 if (PMP_AMATCH_OFF
!= a_field
) {
204 env
->pmp_state
.num_rules
++;
210 * Convert cfg/addr reg values here into simple 'sa' --> start address and 'ea'
211 * end address values.
212 * This function is called relatively infrequently whereas the check that
213 * an address is within a pmp rule is called often, so optimise that one
215 static void pmp_update_rule(CPURISCVState
*env
, uint32_t pmp_index
)
217 pmp_update_rule_addr(env
, pmp_index
);
218 pmp_update_rule_nums(env
);
221 static int pmp_is_in_range(CPURISCVState
*env
, int pmp_index
,
226 if ((addr
>= env
->pmp_state
.addr
[pmp_index
].sa
) &&
227 (addr
<= env
->pmp_state
.addr
[pmp_index
].ea
)) {
237 * Check if the address has required RWX privs when no PMP entry is matched.
239 static bool pmp_hart_has_privs_default(CPURISCVState
*env
, target_ulong addr
,
240 target_ulong size
, pmp_priv_t privs
,
241 pmp_priv_t
*allowed_privs
,
246 if (riscv_cpu_cfg(env
)->epmp
) {
247 if (MSECCFG_MMWP_ISSET(env
)) {
249 * The Machine Mode Whitelist Policy (mseccfg.MMWP) is set
250 * so we default to deny all, even for M-mode.
254 } else if (MSECCFG_MML_ISSET(env
)) {
256 * The Machine Mode Lockdown (mseccfg.MML) bit is set
257 * so we can only execute code in M-mode with an applicable
258 * rule. Other modes are disabled.
260 if (mode
== PRV_M
&& !(privs
& PMP_EXEC
)) {
262 *allowed_privs
= PMP_READ
| PMP_WRITE
;
272 if (!riscv_cpu_cfg(env
)->pmp
|| (mode
== PRV_M
)) {
274 * Privileged spec v1.10 states if HW doesn't implement any PMP entry
275 * or no PMP entry matches an M-Mode access, the access succeeds.
278 *allowed_privs
= PMP_READ
| PMP_WRITE
| PMP_EXEC
;
281 * Other modes are not allowed to succeed if they don't * match a rule,
282 * but there are rules. We've checked for no rule earlier in this
298 * Check if the address has required RWX privs to complete desired operation
299 * Return PMP rule index if a pmp rule match
300 * Return MAX_RISCV_PMPS if default match
301 * Return negtive value if no match
303 int pmp_hart_has_privs(CPURISCVState
*env
, target_ulong addr
,
304 target_ulong size
, pmp_priv_t privs
,
305 pmp_priv_t
*allowed_privs
, target_ulong mode
)
313 /* Short cut if no rules */
314 if (0 == pmp_get_num_rules(env
)) {
315 if (pmp_hart_has_privs_default(env
, addr
, size
, privs
,
316 allowed_privs
, mode
)) {
317 ret
= MAX_RISCV_PMPS
;
322 if (riscv_cpu_cfg(env
)->mmu
) {
324 * If size is unknown (0), assume that all bytes
325 * from addr to the end of the page will be accessed.
327 pmp_size
= -(addr
| TARGET_PAGE_MASK
);
329 pmp_size
= sizeof(target_ulong
);
336 * 1.10 draft priv spec states there is an implicit order
339 for (i
= 0; i
< MAX_RISCV_PMPS
; i
++) {
340 s
= pmp_is_in_range(env
, i
, addr
);
341 e
= pmp_is_in_range(env
, i
, addr
+ pmp_size
- 1);
343 /* partially inside */
345 qemu_log_mask(LOG_GUEST_ERROR
,
346 "pmp violation - access is partially inside\n");
352 const uint8_t a_field
=
353 pmp_get_a_field(env
->pmp_state
.pmp
[i
].cfg_reg
);
356 * Convert the PMP permissions to match the truth table in the
359 const uint8_t epmp_operation
=
360 ((env
->pmp_state
.pmp
[i
].cfg_reg
& PMP_LOCK
) >> 4) |
361 ((env
->pmp_state
.pmp
[i
].cfg_reg
& PMP_READ
) << 2) |
362 (env
->pmp_state
.pmp
[i
].cfg_reg
& PMP_WRITE
) |
363 ((env
->pmp_state
.pmp
[i
].cfg_reg
& PMP_EXEC
) >> 2);
365 if (((s
+ e
) == 2) && (PMP_AMATCH_OFF
!= a_field
)) {
367 * If the PMP entry is not off and the address is in range,
370 if (!MSECCFG_MML_ISSET(env
)) {
372 * If mseccfg.MML Bit is not set, do pmp priv check
373 * This will always apply to regular PMP.
375 *allowed_privs
= PMP_READ
| PMP_WRITE
| PMP_EXEC
;
376 if ((mode
!= PRV_M
) || pmp_is_locked(env
, i
)) {
377 *allowed_privs
&= env
->pmp_state
.pmp
[i
].cfg_reg
;
381 * If mseccfg.MML Bit set, do the enhanced pmp priv check
384 switch (epmp_operation
) {
397 *allowed_privs
= PMP_READ
| PMP_WRITE
;
401 *allowed_privs
= PMP_EXEC
;
405 *allowed_privs
= PMP_READ
| PMP_EXEC
;
409 *allowed_privs
= PMP_READ
;
412 g_assert_not_reached();
415 switch (epmp_operation
) {
427 *allowed_privs
= PMP_EXEC
;
432 *allowed_privs
= PMP_READ
;
436 *allowed_privs
= PMP_READ
| PMP_WRITE
;
439 *allowed_privs
= PMP_READ
| PMP_EXEC
;
442 *allowed_privs
= PMP_READ
| PMP_WRITE
| PMP_EXEC
;
445 g_assert_not_reached();
451 * If matching address range was found, the protection bits
452 * defined with PMP must be used. We shouldn't fallback on
453 * finding default privileges.
460 /* No rule matched */
462 if (pmp_hart_has_privs_default(env
, addr
, size
, privs
,
463 allowed_privs
, mode
)) {
464 ret
= MAX_RISCV_PMPS
;
472 * Handle a write to a pmpcfg CSR
474 void pmpcfg_csr_write(CPURISCVState
*env
, uint32_t reg_index
,
479 int pmpcfg_nums
= 2 << riscv_cpu_mxl(env
);
481 trace_pmpcfg_csr_write(env
->mhartid
, reg_index
, val
);
483 for (i
= 0; i
< pmpcfg_nums
; i
++) {
484 cfg_val
= (val
>> 8 * i
) & 0xff;
485 pmp_write_cfg(env
, (reg_index
* 4) + i
, cfg_val
);
488 /* If PMP permission of any addr has been changed, flush TLB pages. */
489 tlb_flush(env_cpu(env
));
494 * Handle a read from a pmpcfg CSR
496 target_ulong
pmpcfg_csr_read(CPURISCVState
*env
, uint32_t reg_index
)
499 target_ulong cfg_val
= 0;
500 target_ulong val
= 0;
501 int pmpcfg_nums
= 2 << riscv_cpu_mxl(env
);
503 for (i
= 0; i
< pmpcfg_nums
; i
++) {
504 val
= pmp_read_cfg(env
, (reg_index
* 4) + i
);
505 cfg_val
|= (val
<< (i
* 8));
507 trace_pmpcfg_csr_read(env
->mhartid
, reg_index
, cfg_val
);
514 * Handle a write to a pmpaddr CSR
516 void pmpaddr_csr_write(CPURISCVState
*env
, uint32_t addr_index
,
519 trace_pmpaddr_csr_write(env
->mhartid
, addr_index
, val
);
521 if (addr_index
< MAX_RISCV_PMPS
) {
523 * In TOR mode, need to check the lock bit of the next pmp
524 * (if there is a next).
526 if (addr_index
+ 1 < MAX_RISCV_PMPS
) {
527 uint8_t pmp_cfg
= env
->pmp_state
.pmp
[addr_index
+ 1].cfg_reg
;
529 if (pmp_cfg
& PMP_LOCK
&&
530 PMP_AMATCH_TOR
== pmp_get_a_field(pmp_cfg
)) {
531 qemu_log_mask(LOG_GUEST_ERROR
,
532 "ignoring pmpaddr write - pmpcfg + 1 locked\n");
537 if (!pmp_is_locked(env
, addr_index
)) {
538 env
->pmp_state
.pmp
[addr_index
].addr_reg
= val
;
539 pmp_update_rule(env
, addr_index
);
541 qemu_log_mask(LOG_GUEST_ERROR
,
542 "ignoring pmpaddr write - locked\n");
545 qemu_log_mask(LOG_GUEST_ERROR
,
546 "ignoring pmpaddr write - out of bounds\n");
552 * Handle a read from a pmpaddr CSR
554 target_ulong
pmpaddr_csr_read(CPURISCVState
*env
, uint32_t addr_index
)
556 target_ulong val
= 0;
558 if (addr_index
< MAX_RISCV_PMPS
) {
559 val
= env
->pmp_state
.pmp
[addr_index
].addr_reg
;
560 trace_pmpaddr_csr_read(env
->mhartid
, addr_index
, val
);
562 qemu_log_mask(LOG_GUEST_ERROR
,
563 "ignoring pmpaddr read - out of bounds\n");
570 * Handle a write to a mseccfg CSR
572 void mseccfg_csr_write(CPURISCVState
*env
, target_ulong val
)
576 trace_mseccfg_csr_write(env
->mhartid
, val
);
578 /* RLB cannot be enabled if it's already 0 and if any regions are locked */
579 if (!MSECCFG_RLB_ISSET(env
)) {
580 for (i
= 0; i
< MAX_RISCV_PMPS
; i
++) {
581 if (pmp_is_locked(env
, i
)) {
589 val
|= (env
->mseccfg
& (MSECCFG_MMWP
| MSECCFG_MML
));
595 * Handle a read from a mseccfg CSR
597 target_ulong
mseccfg_csr_read(CPURISCVState
*env
)
599 trace_mseccfg_csr_read(env
->mhartid
, env
->mseccfg
);
604 * Calculate the TLB size if the start address or the end address of
605 * PMP entry is presented in the TLB page.
607 target_ulong
pmp_get_tlb_size(CPURISCVState
*env
, int pmp_index
,
608 target_ulong tlb_sa
, target_ulong tlb_ea
)
610 target_ulong pmp_sa
= env
->pmp_state
.addr
[pmp_index
].sa
;
611 target_ulong pmp_ea
= env
->pmp_state
.addr
[pmp_index
].ea
;
613 if (pmp_sa
<= tlb_sa
&& pmp_ea
>= tlb_ea
) {
614 return TARGET_PAGE_SIZE
;
617 * At this point we have a tlb_size that is the smallest possible size
618 * That fits within a TARGET_PAGE_SIZE and the PMP region.
620 * If the size is less then TARGET_PAGE_SIZE we drop the size to 1.
621 * This means the result isn't cached in the TLB and is only used for
622 * a single translation.
629 * Convert PMP privilege to TLB page privilege.
631 int pmp_priv_to_page_prot(pmp_priv_t pmp_priv
)
635 if (pmp_priv
& PMP_READ
) {
638 if (pmp_priv
& PMP_WRITE
) {
641 if (pmp_priv
& PMP_EXEC
) {