Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
154 changes: 154 additions & 0 deletions submissions/latifbhatti/explain_pmp_config.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
\documentclass{article}
\usepackage{array}
\usepackage{geometry}
\geometry{ margin=1in}
\usepackage{longtable}
\usepackage[longtable]{xcolor}
\usepackage{booktabs} % For better table lines
\usepackage[table]{xcolor} % Enable coloring for tables
\usepackage{multirow} % For multi-row cells

\begin{document}

\section*{PMP Configuration Table}
This table describes the PMP configuration values, their binary representation, and their corresponding status and examples.

\scriptsize % Reduce font size for better fit
\begin{longtable}{|>{\ttfamily}c|>{\ttfamily}c|c|c|c|c|c|c|p{5cm}|p{4cm}|}
\hline
\rowcolor{lightgray} % Shade header row
\textbf{Hex} & \textbf{Binary} & \textbf{L} & \textbf{Reserved} & \textbf{A} & \textbf{X} & \textbf{W} & \textbf{R} & \textbf{Status} & \textbf{Example} \\
\hline
\endhead % Ensures the header repeats on every page

0x0 & 00000000 & 0 & 000 & 00 & 0 & 0 & 0 & Disabled, no perms & Unused entry \\
\hline
0x1 & 00000001 & 0 & 000 & 00 & 0 & 0 & 1 & Disabled, read-only & Read-only mem \\
\hline
0x2 & 00000010 & 0 & 000 & 00 & 0 & 1 & 0 & Disabled, write-only & Write-only mem \\
\hline
0x3 & 00000011 & 0 & 000 & 00 & 0 & 1 & 1 & Disabled, read-write & Read-write mem \\
\hline
0x4 & 00000100 & 0 & 000 & 00 & 1 & 0 & 0 & Disabled, exec-only & Exec-only code \\
\hline
0x5 & 00000101 & 0 & 000 & 00 & 1 & 0 & 1 & Disabled, read-exec & Read-exec code \\
\hline
0x6 & 00000110 & 0 & 000 & 00 & 1 & 1 & 0 & Disabled, write-exec & Write-exec code \\
\hline
0x7 & 00000111 & 0 & 000 & 00 & 1 & 1 & 1 & Disabled, r-w-x & R-W-X code \\
\hline
0x8 & 00001000 & 0 & 000 & 01 & 0 & 0 & 0 & TOR, no perms & Unused TOR \\
\hline
0x9 & 00001001 & 0 & 000 & 01 & 0 & 0 & 1 & TOR, read-only & Read-only TOR \\
\hline
0xA & 00001010 & 0 & 000 & 01 & 0 & 1 & 0 & TOR, write-only & Write-only TOR \\
\hline
0xB & 00001011 & 0 & 000 & 01 & 0 & 1 & 1 & TOR, read-write & Read-write TOR \\
\hline
0xC & 00001100 & 0 & 000 & 01 & 1 & 0 & 0 & TOR, exec-only & Exec-only TOR \\
\hline
0xD & 00001101 & 0 & 000 & 01 & 1 & 0 & 1 & TOR, read-exec & Read-exec TOR \\
\hline
0xE & 00001110 & 0 & 000 & 01 & 1 & 1 & 0 & TOR, write-exec & Write-exec TOR \\
\hline
0xF & 00001111 & 0 & 000 & 01 & 1 & 1 & 1 & TOR, r-w-x & R-W-X TOR \\
\hline
0x10 & 00010000 & 0 & 000 & 10 & 0 & 0 & 0 & NA4, no perms & Unused NA4 \\
\hline
0x11 & 00010001 & 0 & 000 & 10 & 0 & 0 & 1 & NA4, read-only & Read-only NA4 \\
\hline
0x12 & 00010010 & 0 & 000 & 10 & 0 & 1 & 0 & NA4, write-only & Write-only NA4 \\
\hline
0x13 & 00010011 & 0 & 000 & 10 & 0 & 1 & 1 & NA4, read-write & Read-write NA4 \\
\hline
0x14 & 00010100 & 0 & 000 & 10 & 1 & 0 & 0 & NA4, exec-only & Exec-only NA4 \\
\hline
0x15 & 00010101 & 0 & 000 & 10 & 1 & 0 & 1 & NA4, read-exec & Read-exec NA4 \\
\hline
0x16 & 00010110 & 0 & 000 & 10 & 1 & 1 & 0 & NA4, write-exec & Write-exec NA4 \\
\hline
0x17 & 00010111 & 0 & 000 & 10 & 1 & 1 & 1 & NA4, r-w-x & R-W-X NA4 \\
\hline
0x18 & 00011000 & 0 & 000 & 11 & 0 & 0 & 0 & NAPOT, no perms & Unused NAPOT \\
\hline
0x19 & 00011001 & 0 & 000 & 11 & 0 & 0 & 1 & NAPOT, read-only & Read-only NAPOT \\
\hline
0x1A & 00011010 & 0 & 000 & 11 & 0 & 1 & 0 & NAPOT, write-only & Write-only NAPOT \\
\hline
0x1B & 00011011 & 0 & 000 & 11 & 0 & 1 & 1 & NAPOT, read-write & Read-write NAPOT \\
\hline
0x1C & 00011100 & 0 & 000 & 11 & 1 & 0 & 0 & NAPOT, exec-only & Exec-only NAPOT \\
\hline
0x1D & 00011101 & 0 & 000 & 11 & 1 & 0 & 1 & NAPOT, read-exec & Read-exec NAPOT \\
\hline
0x1E & 00011110 & 0 & 000 & 11 & 1 & 1 & 0 & NAPOT, write-exec & Write-exec NAPOT \\
\hline
0x1F & 00011111 & 0 & 000 & 11 & 1 & 1 & 1 & NAPOT, r-w-x & R-W-X NAPOT \\
\hline
0x20 & 00100000 & 0 & 001 & 00 & 0 & 0 & 0 & Disabled, no perms & Unused entry \\
\hline
0x21 & 00100001 & 0 & 001 & 00 & 0 & 0 & 1 & Disabled, read-only & Read-only mem \\
\hline
0x22 & 00100010 & 0 & 001 & 00 & 0 & 1 & 0 & Disabled, write-only & Write-only mem \\
\hline
0x23 & 00100011 & 0 & 001 & 00 & 0 & 1 & 1 & Disabled, read-write & Read-write mem \\
\hline
0x24 & 00100100 & 0 & 001 & 00 & 1 & 0 & 0 & Disabled, exec-only & Exec-only code \\
\hline
0x25 & 00100101 & 0 & 001 & 00 & 1 & 0 & 1 & Disabled, read-exec & Read-exec code \\
\hline
0x26 & 00100110 & 0 & 001 & 00 & 1 & 1 & 0 & Disabled, write-exec & Write-exec code \\
\hline
0x27 & 00100111 & 0 & 001 & 00 & 1 & 1 & 1 & Disabled, r-w-x & R-W-X code \\
\hline
0x28 & 00101000 & 0 & 001 & 01 & 0 & 0 & 0 & TOR, no perms & Unused TOR \\
\hline
0x29 & 00101001 & 0 & 001 & 01 & 0 & 0 & 1 & TOR, read-only & Read-only TOR \\
\hline
0x2A & 00101010 & 0 & 001 & 01 & 0 & 1 & 0 & TOR, write-only & Write-only TOR \\
\hline
0x2B & 00101011 & 0 & 001 & 01 & 0 & 1 & 1 & TOR, read-write & Read-write TOR \\
\hline
0x2C & 00101100 & 0 & 001 & 01 & 1 & 0 & 0 & TOR, exec-only & Exec-only TOR \\
\hline
0x2D & 00101101 & 0 & 001 & 01 & 1 & 0 & 1 & TOR, read-exec & Read-exec TOR \\
\hline
0x2E & 00101110 & 0 & 001 & 01 & 1 & 1 & 0 & TOR, write-exec & Write-exec TOR \\
\hline
0x2F & 00101111 & 0 & 001 & 01 & 1 & 1 & 1 & TOR, r-w-x & R-W-X TOR \\
\hline
0x30 & 00110000 & 0 & 001 & 10 & 0 & 0 & 0 & NA4, no perms & Unused NA4 \\
\hline
0x31 & 00110001 & 0 & 001 & 10 & 0 & 0 & 1 & NA4, read-only & Read-only NA4 \\
\hline
0x32 & 00110010 & 0 & 001 & 10 & 0 & 1 & 0 & NA4, write-only & Write-only NA4 \\
\hline
0x33 & 00110011 & 0 & 001 & 10 & 0 & 1 & 1 & NA4, read-write & Read-write NA4 \\
\hline
0x34 & 00110100 & 0 & 001 & 10 & 1 & 0 & 0 & NA4, exec-only & Exec-only NA4 \\
\hline
0x35 & 00110101 & 0 & 001 & 10 & 1 & 0 & 1 & NA4, read-exec & Read-exec NA4 \\
\hline
0x36 & 00110110 & 0 & 001 & 10 & 1 & 1 & 0 & NA4, write-exec & Write-exec NA4 \\
\hline
0x37 & 00110111 & 0 & 001 & 10 & 1 & 1 & 1 & NA4, r-w-x & R-W-X NA4 \\
\hline
0x38 & 00111000 & 0 & 001 & 11 & 0 & 0 & 0 & NAPOT, no perms & Unused NAPOT \\
\hline
0x39 & 00111001 & 0 & 001 & 11 & 0 & 0 & 1 & NAPOT, read-only & Read-only NAPOT \\
\hline
0x3A & 00111010 & 0 & 001 & 11 & 0 & 1 & 0 & NAPOT, write-only & Write-only NAPOT \\
\hline
0x3B & 00111011 & 0 & 001 & 11 & 0 & 1 & 1 & NAPOT, read-write & Read-write NAPOT \\
\hline
0x3C & 00111100 & 0 & 001 & 11 & 1 & 0 & 0 & NAPOT, exec-only & Exec-only NAPOT \\
\hline
0x3D & 00111101 & 0 & 001 & 11 & 1 & 0 & 1 & NAPOT, read-exec & Read-exec NAPOT \\
\hline
0x3E & 00111110 & 0 & 001 & 11 & 1 & 1 & 0 & NAPOT, write-exec & Write-exec NAPOT \\
\hline
0x3F & 00111111 & 0 & 001 & 11 & 1 & 1 & 1 & NAPOT, r-w-x & R-W-X NAPOT \\
\hline
\end{longtable}

\end{document}
98 changes: 98 additions & 0 deletions submissions/latifbhatti/pmp_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import sys
import re

def parse_pmp_config(file_path):
with open(file_path, 'r') as file:
lines = file.readlines()
pmpcfg = [int(line.strip(), 16) & 0xFF for line in lines[:64]]
pmpaddr = [int(line.strip(), 16) for line in lines[64:128]]
return pmpcfg, pmpaddr

def get_pmp_region(base, a, pmpaddr_value, pmpaddr):
if a == 1:
if base == 0:
prev_base = 0
else:
prev_base = (pmpaddr[base - 1])
region_start, region_end = prev_base, (pmpaddr_value)
elif a == 2:
region_start = (pmpaddr_value)
region_end = region_start + 3
elif a == 3:
n = 0
mask = 1
while (pmpaddr_value & mask) == mask:
n += 1
mask = (mask << 1) | 1
size = 1 << (n + 2)
region_start = (pmpaddr_value << 2) & ~(size - 1)
region_end = region_start + size - 1
else:
return None
return region_start, region_end

def check_pmp_access(pmpcfg, pmpaddr, addr, mode, operation):
addr = int(addr, 16)
matching_index = -1
for i in range(0, 64, 1):
cfg = pmpcfg[i]
a = (cfg >> 3) & 0x3
if a == 0:
continue
region = get_pmp_region(i, a, pmpaddr[i], pmpaddr)
if not region:
continue
region_start, region_end = region
if region_start <= addr <= region_end:
matching_index = i
break
cfg = pmpcfg[matching_index]
l = (cfg >> 7) & 0x1
r = (cfg >> 0) & 0x1
w = (cfg >> 1) & 0x1
x = (cfg >> 2) & 0x1
print("value of pmpcfg = " , hex(cfg) ,"\noperation R = " , r,"\noperation W = " , w,"\noperation X = " , x)
if matching_index == -1:
if mode in ['S', 'U']:
return False
else:
return True

if mode == 'M':
if l:
return True
elif mode in ['S', 'U']:
if l:
return False
if operation == 'R' and not r:
return False
if operation == 'W' and not w:
return False
if operation == 'X' and not x:
return False
return True

def validate_hex_address(addr):
return re.match(r'^0x[0-9a-fA-F]+$', addr) is not None

def main():
if len(sys.argv) != 5:
print("Usage: python pmp_check.py <pmp_config_file> <physical_address> <privilege_mode> <operation>")
sys.exit(1)
if not validate_hex_address(sys.argv[2]):
print("Invalid physical address format. Must be in hexadecimal with a leading '0x'.")
sys.exit(1)
pmpcfg, pmpaddr = parse_pmp_config(sys.argv[1])
addr = sys.argv[2]
mode = sys.argv[3].upper()
operation = sys.argv[4].upper()
if mode not in ['M', 'S', 'U'] or operation not in ['R', 'W', 'X']:
print("Invalid privilege mode or operation.")
sys.exit(1)
if check_pmp_access(pmpcfg, pmpaddr, addr, mode, operation):
print("Access allowed.")
else:
print("Access fault.")

if __name__ == "__main__":
main()
128 changes: 128 additions & 0 deletions submissions/latifbhatti/pmp_config.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
0x0
0x1
0x2
0x3
0x4
0x5
0x6
0x7
0x8
0x9
0xA
0xB
0xC
0xD
0xE
0xF
0x10
0x11
0x12
0x13
0x14
0x15
0x16
0x17
0x18
0x19
0x1A
0x1B
0x1C
0x1D
0x1E
0x1F
0x20
0x21
0x22
0x23
0x24
0x25
0x26
0x27
0x28
0x29
0x2A
0x2B
0x2C
0x2D
0x2E
0x2F
0x30
0x31
0x32
0x33
0x34
0x35
0x36
0x37
0x38
0x39
0x3A
0x3B
0x3C
0x3D
0x3E
0x3F
0x100
0x1000
0x2000
0x5000
0x80000000
0x10000000
0x20000000
0x70000000
0x80000000
0x90000000
0xA0000000
0xB0000000
0xC0000000
0xD0000000
0xE0000000
0xF0000000
0x100000000
0x110000000
0x120000000
0x130000000
0x140000000
0x150000000
0x160000000
0x170000000
0x180000000
0x190000000
0x1A0000000
0x1B0000000
0x1C0000000
0x1D0000000
0x1E0000000
0x1F0000000
0x200000000
0x210000000
0x220000000
0x230000000
0x240000000
0x250000000
0x260000000
0x270000000
0x280000000
0x290000000
0x2A0000000
0x2B0000000
0x2C0000000
0x2D0000000
0x2E0000000
0x2F0000000
0x300000000
0x310000000
0x320000000
0x330000000
0x340000000
0x350000000
0x360000000
0x370000000
0x380000000
0x390000000
0x3A0000000
0x3B0000000
0x3C0000000
0x3D0000000
0x3E0000000
0x3F0000000