2021-04-22 19:34:02 +00:00
///////////////////////////////////////////
2021-04-29 06:20:39 +00:00
// pmpchecker.sv
2021-04-22 19:34:02 +00:00
//
2021-04-29 06:20:39 +00:00
// Written: tfleming@hmc.edu & jtorrey@hmc.edu 28 April 2021
2021-04-22 19:34:02 +00:00
// Modified:
//
2021-04-29 06:20:39 +00:00
// Purpose: Examines all physical memory accesses and checks them against the
// current values of the physical memory protection (PMP) registers.
// Can raise an access fault on illegal reads, writes, and instruction
// fetches.
2021-04-22 19:34:02 +00:00
//
2023-01-15 02:14:38 +00:00
// Documentation: RISC-V System on Chip Design Chapter 8
//
2023-01-11 23:15:08 +00:00
// A component of the CORE-V-WALLY configurable RISC-V project.
2021-04-22 19:34:02 +00:00
//
2023-01-10 19:35:20 +00:00
// Copyright (C) 2021-23 Harvey Mudd College & Oklahoma State University
2021-04-22 19:34:02 +00:00
//
2023-01-10 19:35:20 +00:00
// SPDX-License-Identifier: Apache-2.0 WITH SHL-2.1
2021-04-22 19:34:02 +00:00
//
2023-01-10 19:35:20 +00:00
// Licensed under the Solderpad Hardware License v 2.1 (the “License”); you may not use this file
// except in compliance with the License, or, at your option, the Apache License version 2.0. You
// may obtain a copy of the License at
2021-04-22 19:34:02 +00:00
//
2023-01-10 19:35:20 +00:00
// https://solderpad.org/licenses/SHL-2.1/
//
// Unless required by applicable law or agreed to in writing, any work distributed under the
// License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
// either express or implied. See the License for the specific language governing permissions
// and limitations under the License.
2022-01-07 12:58:40 +00:00
////////////////////////////////////////////////////////////////////////////////////////////////
2021-04-22 19:34:02 +00:00
`include " wally-config.vh "
2021-04-29 06:20:39 +00:00
module pmpchecker (
2021-06-24 23:59:29 +00:00
input logic [ `PA_BITS - 1 : 0 ] PhysicalAddress ,
2021-07-03 06:25:31 +00:00
input logic [ 1 : 0 ] PrivilegeModeW ,
2023-01-15 02:14:38 +00:00
// ModelSim has a switch -svinputport which controls whether input ports
2021-05-04 19:18:08 +00:00
// are nets (wires) or vars by default. The default setting of this switch is
// `relaxed`, which means that signals are nets if and only if they are
// scalars or one-dimensional vectors. Since this is a two-dimensional vector,
// this will be understood as a var. However, if we don't supply the `var`
// keyword, the compiler warns us that it's interpreting the signal as a var,
// which we might not intend.
2023-01-15 02:14:38 +00:00
input var logic [ 7 : 0 ] PMPCFG_ARRAY_REGW [ `PMP_ENTRIES - 1 : 0 ] ,
2023-03-22 11:33:14 +00:00
input var logic [ `PA_BITS - 3 : 0 ] PMPADDR_ARRAY_REGW [ `PMP_ENTRIES - 1 : 0 ] ,
2023-01-15 02:14:38 +00:00
input logic ExecuteAccessF , WriteAccessM , ReadAccessM ,
output logic PMPInstrAccessFaultF ,
output logic PMPLoadAccessFaultM ,
output logic PMPStoreAmoAccessFaultM
2021-04-22 19:34:02 +00:00
) ;
2023-02-19 23:31:00 +00:00
// Bit i is high when the address falls in PMP region i
2023-03-19 12:46:34 +00:00
logic EnforcePMP ; // should PMP be checked in this privilege level
logic [ `PMP_ENTRIES - 1 : 0 ] Match ; // physical address matches one of the pmp ranges
2023-02-19 23:31:00 +00:00
logic [ `PMP_ENTRIES - 1 : 0 ] FirstMatch ; // onehot encoding for the first pmpaddr to match the current address.
logic [ `PMP_ENTRIES - 1 : 0 ] Active ; // PMP register i is non-null
logic [ `PMP_ENTRIES - 1 : 0 ] L , X , W , R ; // PMP matches and has flag set
2023-03-19 12:46:34 +00:00
logic [ `PMP_ENTRIES - 1 : 0 ] PAgePMPAdr ; // for TOR PMP matching, PhysicalAddress > PMPAdr[i]
2021-05-03 21:37:42 +00:00
2023-03-19 12:46:34 +00:00
if ( `PMP_ENTRIES > 0 ) // prevent complaints about array of no elements when PMP_ENTRIES = 0
2023-02-20 00:08:23 +00:00
pmpadrdec pmpadrdecs [ `PMP_ENTRIES - 1 : 0 ] (
. PhysicalAddress ,
. PMPCfg ( PMPCFG_ARRAY_REGW ) ,
. PMPAdr ( PMPADDR_ARRAY_REGW ) ,
. PAgePMPAdrIn ( { PAgePMPAdr [ `PMP_ENTRIES - 2 : 0 ] , 1 'b1 } ) ,
. PAgePMPAdrOut ( PAgePMPAdr ) ,
. Match , . Active , . L , . X , . W , . R ) ;
2021-07-02 15:04:13 +00:00
2023-02-19 23:31:00 +00:00
priorityonehot # ( `PMP_ENTRIES ) pmppriority ( . a ( Match ) , . y ( FirstMatch ) ) ; // combine the match signal from all the adress decoders to find the first one that matches.
2021-05-04 05:56:05 +00:00
2023-03-19 12:46:34 +00:00
// Only enforce PMP checking for S and U modes or in Machine mode when L bit is set in selected region
2023-02-19 23:31:00 +00:00
assign EnforcePMP = ( PrivilegeModeW = = `M_MODE ) ? | ( L & FirstMatch ) : | Active ;
2023-03-19 12:46:34 +00:00
// assign EnforcePMP = (PrivilegeModeW != `M_MODE) | |(L & FirstMatch); // *** switch to this logic when PMP is initialized for non-machine mode
// *** remove unused Active lines from pmpadrdecs
2022-01-05 14:35:25 +00:00
2023-02-19 23:31:00 +00:00
assign PMPInstrAccessFaultF = EnforcePMP & ExecuteAccessF & ~ | ( X & FirstMatch ) ;
assign PMPStoreAmoAccessFaultM = EnforcePMP & WriteAccessM & ~ | ( W & FirstMatch ) ;
assign PMPLoadAccessFaultM = EnforcePMP & ReadAccessM & ~ | ( R & FirstMatch ) ;
2023-01-15 02:14:38 +00:00
endmodule