xref: /qemu/target/riscv/pmp.h (revision d6430c17d7113d3c38480dc34e59d00b0504e2f7)
1 /*
2  * QEMU RISC-V PMP (Physical Memory Protection)
3  *
4  * Author: Daire McNamara, daire.mcnamara@emdalo.com
5  *         Ivan Griffin, ivan.griffin@emdalo.com
6  *
7  * This provides a RISC-V Physical Memory Protection interface
8  *
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.
12  *
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
16  * more details.
17  *
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/>.
20  */
21 
22 #ifndef RISCV_PMP_H
23 #define RISCV_PMP_H
24 
25 #include "cpu.h"
26 
27 typedef enum {
28     PMP_READ  = 1 << 0,
29     PMP_WRITE = 1 << 1,
30     PMP_EXEC  = 1 << 2,
31     PMP_AMATCH = (3 << 3),
32     PMP_LOCK  = 1 << 7
33 } pmp_priv_t;
34 
35 typedef enum {
36     PMP_AMATCH_OFF,  /* Null (off)                            */
37     PMP_AMATCH_TOR,  /* Top of Range                          */
38     PMP_AMATCH_NA4,  /* Naturally aligned four-byte region    */
39     PMP_AMATCH_NAPOT /* Naturally aligned power-of-two region */
40 } pmp_am_t;
41 
42 typedef enum {
43     MSECCFG_MML   = 1 << 0,
44     MSECCFG_MMWP  = 1 << 1,
45     MSECCFG_RLB   = 1 << 2,
46     MSECCFG_USEED = 1 << 8,
47     MSECCFG_SSEED = 1 << 9,
48     MSECCFG_MLPE =  1 << 10,
49     MSECCFG_PMM = 3ULL << 32,
50 } mseccfg_field_t;
51 
52 typedef struct {
53     target_ulong addr_reg;
54     uint8_t  cfg_reg;
55 } pmp_entry_t;
56 
57 typedef struct {
58     hwaddr sa;
59     hwaddr ea;
60 } pmp_addr_t;
61 
62 typedef struct {
63     pmp_entry_t pmp[MAX_RISCV_PMPS];
64     pmp_addr_t  addr[MAX_RISCV_PMPS];
65     uint32_t num_rules;
66 } pmp_table_t;
67 
68 void pmpcfg_csr_write(CPURISCVState *env, uint32_t reg_index,
69                       target_ulong val);
70 target_ulong pmpcfg_csr_read(CPURISCVState *env, uint32_t reg_index);
71 
72 void mseccfg_csr_write(CPURISCVState *env, target_ulong val);
73 target_ulong mseccfg_csr_read(CPURISCVState *env);
74 
75 void pmpaddr_csr_write(CPURISCVState *env, uint32_t addr_index,
76                        target_ulong val);
77 target_ulong pmpaddr_csr_read(CPURISCVState *env, uint32_t addr_index);
78 bool pmp_hart_has_privs(CPURISCVState *env, hwaddr addr,
79                         target_ulong size, pmp_priv_t privs,
80                         pmp_priv_t *allowed_privs,
81                         target_ulong mode);
82 target_ulong pmp_get_tlb_size(CPURISCVState *env, hwaddr addr);
83 void pmp_update_rule_addr(CPURISCVState *env, uint32_t pmp_index);
84 void pmp_update_rule_nums(CPURISCVState *env);
85 uint32_t pmp_get_num_rules(CPURISCVState *env);
86 int pmp_priv_to_page_prot(pmp_priv_t pmp_priv);
87 void pmp_unlock_entries(CPURISCVState *env);
88 
89 #define MSECCFG_MML_ISSET(env) get_field(env->mseccfg, MSECCFG_MML)
90 #define MSECCFG_MMWP_ISSET(env) get_field(env->mseccfg, MSECCFG_MMWP)
91 #define MSECCFG_RLB_ISSET(env) get_field(env->mseccfg, MSECCFG_RLB)
92 
93 #endif
94