2018-03-02 15:31:11 +03:00
|
|
|
/*
|
|
|
|
* QEMU RISC-V PMP (Physical Memory Protection)
|
|
|
|
*
|
|
|
|
* Author: Daire McNamara, daire.mcnamara@emdalo.com
|
|
|
|
* Ivan Griffin, ivan.griffin@emdalo.com
|
|
|
|
*
|
|
|
|
* This provides a RISC-V Physical Memory Protection interface
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2 or later, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with
|
|
|
|
* this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2019-03-15 17:51:21 +03:00
|
|
|
#ifndef RISCV_PMP_H
|
|
|
|
#define RISCV_PMP_H
|
2018-03-02 15:31:11 +03:00
|
|
|
|
2022-02-07 14:44:46 +03:00
|
|
|
#include "cpu.h"
|
|
|
|
|
2018-03-02 15:31:11 +03:00
|
|
|
typedef enum {
|
|
|
|
PMP_READ = 1 << 0,
|
|
|
|
PMP_WRITE = 1 << 1,
|
|
|
|
PMP_EXEC = 1 << 2,
|
|
|
|
PMP_LOCK = 1 << 7
|
|
|
|
} pmp_priv_t;
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
PMP_AMATCH_OFF, /* Null (off) */
|
|
|
|
PMP_AMATCH_TOR, /* Top of Range */
|
|
|
|
PMP_AMATCH_NA4, /* Naturally aligned four-byte region */
|
|
|
|
PMP_AMATCH_NAPOT /* Naturally aligned power-of-two region */
|
|
|
|
} pmp_am_t;
|
|
|
|
|
2021-04-19 09:16:53 +03:00
|
|
|
typedef enum {
|
2022-04-23 05:35:08 +03:00
|
|
|
MSECCFG_MML = 1 << 0,
|
|
|
|
MSECCFG_MMWP = 1 << 1,
|
|
|
|
MSECCFG_RLB = 1 << 2,
|
|
|
|
MSECCFG_USEED = 1 << 8,
|
|
|
|
MSECCFG_SSEED = 1 << 9
|
2021-04-19 09:16:53 +03:00
|
|
|
} mseccfg_field_t;
|
|
|
|
|
2018-03-02 15:31:11 +03:00
|
|
|
typedef struct {
|
|
|
|
target_ulong addr_reg;
|
|
|
|
uint8_t cfg_reg;
|
|
|
|
} pmp_entry_t;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
target_ulong sa;
|
|
|
|
target_ulong ea;
|
|
|
|
} pmp_addr_t;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
pmp_entry_t pmp[MAX_RISCV_PMPS];
|
|
|
|
pmp_addr_t addr[MAX_RISCV_PMPS];
|
|
|
|
uint32_t num_rules;
|
|
|
|
} pmp_table_t;
|
|
|
|
|
|
|
|
void pmpcfg_csr_write(CPURISCVState *env, uint32_t reg_index,
|
2023-04-05 11:58:11 +03:00
|
|
|
target_ulong val);
|
2018-03-02 15:31:11 +03:00
|
|
|
target_ulong pmpcfg_csr_read(CPURISCVState *env, uint32_t reg_index);
|
2021-04-19 09:16:53 +03:00
|
|
|
|
|
|
|
void mseccfg_csr_write(CPURISCVState *env, target_ulong val);
|
|
|
|
target_ulong mseccfg_csr_read(CPURISCVState *env);
|
|
|
|
|
2018-03-02 15:31:11 +03:00
|
|
|
void pmpaddr_csr_write(CPURISCVState *env, uint32_t addr_index,
|
2023-04-05 11:58:11 +03:00
|
|
|
target_ulong val);
|
2018-03-02 15:31:11 +03:00
|
|
|
target_ulong pmpaddr_csr_read(CPURISCVState *env, uint32_t addr_index);
|
target/riscv: Fix PMP propagation for tlb
Only the pmp index that be checked by pmp_hart_has_privs can be used
by pmp_get_tlb_size to avoid an error pmp index.
Before modification, we may use an error pmp index. For example,
we check address 0x4fc, and the size 0x4 in pmp_hart_has_privs. If there
is an pmp rule, valid range is [0x4fc, 0x500), then pmp_hart_has_privs
will return true;
However, this checked pmp index is discarded as pmp_hart_has_privs
return bool value. In pmp_is_range_in_tlb, it will traverse all pmp
rules. The tlb_sa will be 0x0, and tlb_ea will be 0xfff. If there is
a pmp rule [0x10, 0x14), it will be misused as it is legal in
pmp_get_tlb_size.
As we have already known the correct pmp index, just remove the
remove the pmp_is_range_in_tlb and get tlb size directly from
pmp_get_tlb_size.
Signed-off-by: LIU Zhiwei <zhiwei_liu@linux.alibaba.com>
Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
Message-Id: <20221012060016.30856-1-zhiwei_liu@linux.alibaba.com>
Signed-off-by: Alistair Francis <alistair.francis@wdc.com>
2022-10-12 09:00:16 +03:00
|
|
|
int pmp_hart_has_privs(CPURISCVState *env, target_ulong addr,
|
2023-04-05 11:58:11 +03:00
|
|
|
target_ulong size, pmp_priv_t privs,
|
|
|
|
pmp_priv_t *allowed_privs,
|
|
|
|
target_ulong mode);
|
target/riscv: Fix PMP propagation for tlb
Only the pmp index that be checked by pmp_hart_has_privs can be used
by pmp_get_tlb_size to avoid an error pmp index.
Before modification, we may use an error pmp index. For example,
we check address 0x4fc, and the size 0x4 in pmp_hart_has_privs. If there
is an pmp rule, valid range is [0x4fc, 0x500), then pmp_hart_has_privs
will return true;
However, this checked pmp index is discarded as pmp_hart_has_privs
return bool value. In pmp_is_range_in_tlb, it will traverse all pmp
rules. The tlb_sa will be 0x0, and tlb_ea will be 0xfff. If there is
a pmp rule [0x10, 0x14), it will be misused as it is legal in
pmp_get_tlb_size.
As we have already known the correct pmp index, just remove the
remove the pmp_is_range_in_tlb and get tlb size directly from
pmp_get_tlb_size.
Signed-off-by: LIU Zhiwei <zhiwei_liu@linux.alibaba.com>
Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
Message-Id: <20221012060016.30856-1-zhiwei_liu@linux.alibaba.com>
Signed-off-by: Alistair Francis <alistair.francis@wdc.com>
2022-10-12 09:00:16 +03:00
|
|
|
target_ulong pmp_get_tlb_size(CPURISCVState *env, int pmp_index,
|
|
|
|
target_ulong tlb_sa, target_ulong tlb_ea);
|
2020-10-26 14:55:27 +03:00
|
|
|
void pmp_update_rule_addr(CPURISCVState *env, uint32_t pmp_index);
|
|
|
|
void pmp_update_rule_nums(CPURISCVState *env);
|
2020-12-23 22:25:53 +03:00
|
|
|
uint32_t pmp_get_num_rules(CPURISCVState *env);
|
2021-02-21 17:01:20 +03:00
|
|
|
int pmp_priv_to_page_prot(pmp_priv_t pmp_priv);
|
2018-03-02 15:31:11 +03:00
|
|
|
|
2021-04-19 09:16:53 +03:00
|
|
|
#define MSECCFG_MML_ISSET(env) get_field(env->mseccfg, MSECCFG_MML)
|
|
|
|
#define MSECCFG_MMWP_ISSET(env) get_field(env->mseccfg, MSECCFG_MMWP)
|
|
|
|
#define MSECCFG_RLB_ISSET(env) get_field(env->mseccfg, MSECCFG_RLB)
|
|
|
|
|
2018-03-02 15:31:11 +03:00
|
|
|
#endif
|