2021-03-04 07:39:08 +00:00
|
|
|
///////////////////////////////////////////
|
2021-03-18 18:35:46 +00:00
|
|
|
// tlb.sv
|
2021-03-04 07:39:08 +00:00
|
|
|
//
|
|
|
|
// Written: jtorrey@hmc.edu 16 February 2021
|
2021-06-01 21:50:37 +00:00
|
|
|
// Modified: kmacsaigoren@hmc.edu 1 June 2021
|
|
|
|
// Implemented SV48 on top of SV39. This included adding the SvMode signal,
|
|
|
|
// and using it to decide the translate signal and get the virtual page number
|
2021-03-04 07:39:08 +00:00
|
|
|
//
|
2021-04-13 17:37:24 +00:00
|
|
|
// Purpose: Translation lookaside buffer
|
|
|
|
// Cache of virtural-to-physical address translations
|
2021-03-04 07:39:08 +00:00
|
|
|
//
|
|
|
|
// A component of the Wally configurable RISC-V project.
|
|
|
|
//
|
|
|
|
// Copyright (C) 2021 Harvey Mudd College & Oklahoma State University
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
|
|
|
|
// files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
|
|
|
|
// modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
|
|
|
|
// is furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
|
|
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
|
|
// BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
|
|
|
|
// OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
///////////////////////////////////////////
|
|
|
|
|
|
|
|
/**
|
2021-06-01 21:50:37 +00:00
|
|
|
* SV32 specs
|
2021-03-04 07:39:08 +00:00
|
|
|
* ----------
|
|
|
|
* Virtual address [31:0] (32 bits)
|
|
|
|
* [________________________________]
|
|
|
|
* |--VPN1--||--VPN0--||----OFF---|
|
|
|
|
* 10 10 12
|
|
|
|
*
|
|
|
|
* Physical address [33:0] (34 bits)
|
|
|
|
* [__________________________________]
|
|
|
|
* |---PPN1---||--PPN0--||----OFF---|
|
|
|
|
* 12 10 12
|
|
|
|
*
|
|
|
|
* Page Table Entry [31:0] (32 bits)
|
|
|
|
* [________________________________]
|
|
|
|
* |---PPN1---||--PPN0--|||DAGUXWRV
|
|
|
|
* 12 10 ^^
|
|
|
|
* RSW(2) -- for OS
|
|
|
|
*/
|
|
|
|
|
2021-04-13 16:27:12 +00:00
|
|
|
`include "wally-config.vh"
|
|
|
|
|
2021-03-04 07:39:08 +00:00
|
|
|
// The TLB will have 2**ENTRY_BITS total entries
|
2021-04-21 23:58:36 +00:00
|
|
|
module tlb #(parameter ENTRY_BITS = 3,
|
|
|
|
parameter ITLB = 0) (
|
2021-06-04 21:05:07 +00:00
|
|
|
input logic clk, reset,
|
2021-03-04 07:39:08 +00:00
|
|
|
|
|
|
|
// Current value of satp CSR (from privileged unit)
|
2021-06-04 21:05:07 +00:00
|
|
|
input logic [`XLEN-1:0] SATP_REGW,
|
|
|
|
input logic STATUS_MXR, STATUS_SUM,
|
2021-03-04 07:39:08 +00:00
|
|
|
|
2021-03-18 18:35:46 +00:00
|
|
|
// Current privilege level of the processeor
|
2021-06-04 21:05:07 +00:00
|
|
|
input logic [1:0] PrivilegeModeW,
|
2021-03-18 18:35:46 +00:00
|
|
|
|
2021-04-21 23:58:36 +00:00
|
|
|
// 00 - TLB is not being accessed
|
|
|
|
// 1x - TLB is accessed for a read (or an instruction)
|
|
|
|
// x1 - TLB is accessed for a write
|
|
|
|
// 11 - TLB is accessed for both read and write
|
2021-06-04 21:05:07 +00:00
|
|
|
input logic [1:0] TLBAccessType,
|
2021-06-23 21:43:22 +00:00
|
|
|
input logic DisableTranslation,
|
2021-03-31 02:19:27 +00:00
|
|
|
|
2021-03-04 07:39:08 +00:00
|
|
|
// Virtual address input
|
2021-06-04 21:05:07 +00:00
|
|
|
input logic [`XLEN-1:0] VirtualAddress,
|
2021-03-04 07:39:08 +00:00
|
|
|
|
|
|
|
// Controls for writing a new entry to the TLB
|
2021-06-08 17:39:32 +00:00
|
|
|
input logic [`XLEN-1:0] PTEWriteVal,
|
|
|
|
input logic [1:0] PageTypeWriteVal,
|
2021-06-04 21:05:07 +00:00
|
|
|
input logic TLBWrite,
|
2021-03-04 07:39:08 +00:00
|
|
|
|
|
|
|
// Invalidate all TLB entries
|
2021-06-04 21:05:07 +00:00
|
|
|
input logic TLBFlush,
|
2021-03-04 07:39:08 +00:00
|
|
|
|
|
|
|
// Physical address outputs
|
2021-06-18 13:11:31 +00:00
|
|
|
output logic [`PA_BITS-1:0] PhysicalAddress,
|
2021-06-04 21:05:07 +00:00
|
|
|
output logic TLBMiss,
|
|
|
|
output logic TLBHit,
|
2021-04-08 06:44:59 +00:00
|
|
|
|
|
|
|
// Faults
|
2021-06-04 21:05:07 +00:00
|
|
|
output logic TLBPageFault
|
2021-03-04 07:39:08 +00:00
|
|
|
);
|
|
|
|
|
2021-03-18 18:35:46 +00:00
|
|
|
logic Translate;
|
2021-04-21 23:58:36 +00:00
|
|
|
logic TLBAccess, ReadAccess, WriteAccess;
|
2021-03-04 07:39:08 +00:00
|
|
|
|
2021-06-01 21:50:37 +00:00
|
|
|
// Store current virtual memory mode (SV32, SV39, SV48, ect...)
|
|
|
|
logic [`SVMODE_BITS-1:0] SvMode;
|
2021-03-05 18:35:24 +00:00
|
|
|
|
2021-03-04 07:39:08 +00:00
|
|
|
// Index (currently random) to write the next TLB entry
|
|
|
|
logic [ENTRY_BITS-1:0] WriteIndex;
|
2021-06-08 17:59:03 +00:00
|
|
|
logic [(2**ENTRY_BITS)-1:0] WriteLines; // used as the one-hot encoding of WriteIndex
|
2021-03-04 07:39:08 +00:00
|
|
|
|
|
|
|
// Sections of the virtual and physical addresses
|
2021-03-05 18:35:24 +00:00
|
|
|
logic [`VPN_BITS-1:0] VirtualPageNumber;
|
2021-04-08 06:44:59 +00:00
|
|
|
logic [`PPN_BITS-1:0] PhysicalPageNumber, PhysicalPageNumberMixed;
|
2021-04-08 07:24:10 +00:00
|
|
|
logic [`PA_BITS-1:0] PhysicalAddressFull;
|
|
|
|
|
|
|
|
// Sections of the page table entry
|
2021-04-08 06:44:59 +00:00
|
|
|
logic [7:0] PTEAccessBits;
|
2021-03-05 18:35:24 +00:00
|
|
|
logic [11:0] PageOffset;
|
2021-03-04 07:39:08 +00:00
|
|
|
|
2021-04-21 23:58:36 +00:00
|
|
|
// Useful PTE Control Bits
|
|
|
|
logic PTE_U, PTE_X, PTE_W, PTE_R;
|
|
|
|
|
2021-04-08 06:44:59 +00:00
|
|
|
// Pattern location in the CAM and type of page hit
|
2021-03-04 07:39:08 +00:00
|
|
|
logic [ENTRY_BITS-1:0] VPNIndex;
|
2021-04-08 06:44:59 +00:00
|
|
|
logic [1:0] HitPageType;
|
2021-03-04 07:39:08 +00:00
|
|
|
|
2021-04-08 07:24:10 +00:00
|
|
|
// Whether the virtual address has a match in the CAM
|
|
|
|
logic CAMHit;
|
2021-03-31 02:19:27 +00:00
|
|
|
|
2021-06-01 21:50:37 +00:00
|
|
|
// Grab the sv mode from SATP
|
|
|
|
assign SvMode = SATP_REGW[`XLEN-1:`XLEN-`SVMODE_BITS];
|
|
|
|
|
2021-06-07 23:23:30 +00:00
|
|
|
// Decode the integer encoded WriteIndex into the one-hot encoded WriteLines
|
2021-06-08 17:59:03 +00:00
|
|
|
decoder #(ENTRY_BITS) writedecoder(WriteIndex, WriteLines);
|
2021-06-07 23:23:30 +00:00
|
|
|
|
2021-06-01 21:50:37 +00:00
|
|
|
// The bus width is always the largest it could be for that XLEN. For example, vpn will be 36 bits wide in rv64
|
|
|
|
// this, even though it could be 27 bits (SV39) or 36 bits (SV48) wide. When the value of VPN is narrower,
|
|
|
|
// is shorter, the extra bits are used as padded zeros on the left of the full value.
|
2021-04-21 23:58:36 +00:00
|
|
|
generate
|
|
|
|
if (`XLEN == 32) begin
|
2021-06-01 21:50:37 +00:00
|
|
|
assign VirtualPageNumber = VirtualAddress[`VPN_BITS+11:12];
|
2021-04-21 23:58:36 +00:00
|
|
|
end else begin
|
2021-06-01 21:50:37 +00:00
|
|
|
assign VirtualPageNumber = (SvMode == `SV48) ?
|
|
|
|
VirtualAddress[`VPN_BITS+11:12] :
|
|
|
|
{{`VPN_SEGMENT_BITS{1'b0}}, VirtualAddress[3*`VPN_SEGMENT_BITS+11:12]};
|
2021-04-21 23:58:36 +00:00
|
|
|
end
|
|
|
|
endgenerate
|
|
|
|
|
|
|
|
// Whether translation should occur
|
2021-06-28 22:26:11 +00:00
|
|
|
assign Translate = (SvMode != `NO_TRANSLATE) & (PrivilegeModeW != `M_MODE) & ~ DisableTranslation;
|
2021-04-21 23:58:36 +00:00
|
|
|
|
|
|
|
// Determine how the TLB is currently being used
|
2021-05-14 11:12:32 +00:00
|
|
|
// Note that we use ReadAccess for both loads and instruction fetches
|
2021-04-21 23:58:36 +00:00
|
|
|
assign ReadAccess = TLBAccessType[1];
|
|
|
|
assign WriteAccess = TLBAccessType[0];
|
|
|
|
assign TLBAccess = ReadAccess || WriteAccess;
|
|
|
|
|
2021-06-01 21:50:37 +00:00
|
|
|
|
2021-06-07 23:23:30 +00:00
|
|
|
assign PageOffset = VirtualAddress[11:0];
|
2021-03-04 07:39:08 +00:00
|
|
|
|
2021-04-21 23:58:36 +00:00
|
|
|
// TLB entries are evicted according to the LRU algorithm
|
2021-06-07 22:54:05 +00:00
|
|
|
tlblru #(ENTRY_BITS) lru(.*);
|
2021-03-04 07:39:08 +00:00
|
|
|
|
2021-06-07 22:54:05 +00:00
|
|
|
tlbram #(ENTRY_BITS) tlbram(.*);
|
|
|
|
tlbcam #(ENTRY_BITS, `VPN_BITS, `VPN_SEGMENT_BITS) tlbcam(.*);
|
2021-04-08 06:44:59 +00:00
|
|
|
|
2021-04-21 23:58:36 +00:00
|
|
|
// unswizzle useful PTE bits
|
|
|
|
assign PTE_U = PTEAccessBits[4];
|
|
|
|
assign PTE_X = PTEAccessBits[3];
|
|
|
|
assign PTE_W = PTEAccessBits[2];
|
|
|
|
assign PTE_R = PTEAccessBits[1];
|
|
|
|
|
|
|
|
// Check whether the access is allowed, page faulting if not.
|
|
|
|
// *** We might not have S mode.
|
|
|
|
generate
|
|
|
|
if (ITLB == 1) begin
|
|
|
|
logic ImproperPrivilege;
|
|
|
|
|
|
|
|
// User mode may only execute user mode pages, and supervisor mode may
|
|
|
|
// only execute non-user mode pages.
|
|
|
|
assign ImproperPrivilege = ((PrivilegeModeW == `U_MODE) && ~PTE_U) ||
|
|
|
|
((PrivilegeModeW == `S_MODE) && PTE_U);
|
|
|
|
assign TLBPageFault = Translate && TLBHit && (ImproperPrivilege || ~PTE_X);
|
|
|
|
end else begin
|
|
|
|
logic ImproperPrivilege, InvalidRead, InvalidWrite;
|
|
|
|
|
|
|
|
// User mode may only load/store from user mode pages, and supervisor mode
|
|
|
|
// may only access user mode pages when STATUS_SUM is low.
|
|
|
|
assign ImproperPrivilege = ((PrivilegeModeW == `U_MODE) && ~PTE_U) ||
|
|
|
|
((PrivilegeModeW == `S_MODE) && PTE_U && ~STATUS_SUM);
|
|
|
|
// Check for read error. Reads are invalid when the page is not readable
|
|
|
|
// (and executable pages are not readable) or when the page is neither
|
|
|
|
// readable nor executable (and executable pages are readable).
|
|
|
|
assign InvalidRead = ReadAccess &&
|
|
|
|
((~STATUS_MXR && ~PTE_R) || (STATUS_MXR && ~PTE_R && PTE_X));
|
|
|
|
// Check for write error. Writes are invalid when the page's write bit is
|
|
|
|
// low.
|
|
|
|
assign InvalidWrite = WriteAccess && ~PTE_W;
|
|
|
|
assign TLBPageFault = Translate && TLBHit &&
|
|
|
|
(ImproperPrivilege || InvalidRead || InvalidWrite);
|
|
|
|
end
|
|
|
|
endgenerate
|
2021-04-08 06:44:59 +00:00
|
|
|
|
2021-04-08 07:24:10 +00:00
|
|
|
// Replace segments of the virtual page number with segments of the physical
|
|
|
|
// page number. For 4 KB pages, the entire virtual page number is replaced.
|
|
|
|
// For superpages, some segments are considered offsets into a larger page.
|
2021-06-07 22:32:34 +00:00
|
|
|
physicalpagemask PageNumberMixer(VirtualPageNumber, PhysicalPageNumber, HitPageType, PhysicalPageNumberMixed);
|
2021-04-08 06:44:59 +00:00
|
|
|
|
2021-04-08 07:24:10 +00:00
|
|
|
// Provide physical address only on TLBHits to cause catastrophic errors if
|
|
|
|
// garbage address is used.
|
2021-04-08 06:44:59 +00:00
|
|
|
assign PhysicalAddressFull = (TLBHit) ?
|
|
|
|
{PhysicalPageNumberMixed, PageOffset} : '0;
|
2021-03-04 07:39:08 +00:00
|
|
|
|
2021-04-08 07:24:10 +00:00
|
|
|
// Output the hit physical address if translation is currently on.
|
2021-03-04 07:39:08 +00:00
|
|
|
generate
|
|
|
|
if (`XLEN == 32) begin
|
2021-06-18 13:11:31 +00:00
|
|
|
mux2 #(`PA_BITS) addressmux({2'b0, VirtualAddress}, PhysicalAddressFull, Translate, PhysicalAddress);
|
2021-03-04 07:39:08 +00:00
|
|
|
end else begin
|
2021-06-18 13:11:31 +00:00
|
|
|
mux2 #(`PA_BITS) addressmux(VirtualAddress[`PA_BITS-1:0], PhysicalAddressFull, Translate, PhysicalAddress);
|
2021-03-04 07:39:08 +00:00
|
|
|
end
|
|
|
|
endgenerate
|
|
|
|
|
2021-03-31 02:19:27 +00:00
|
|
|
assign TLBHit = CAMHit & TLBAccess;
|
|
|
|
assign TLBMiss = ~TLBHit & ~TLBFlush & Translate & TLBAccess;
|
2021-03-04 07:39:08 +00:00
|
|
|
endmodule
|