Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Big endianness Support added #751

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
3 changes: 3 additions & 0 deletions model/prelude.sail
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,6 @@ type max_mem_access : Int = 4096

// Type used for memory access widths. Zero byte accesses are not allowed.
type mem_access_width = range(1, max_mem_access)

// Function to reverse endianness.
val reverse_endianness = pure {c: " reverse_endianness"} : forall 'n . (bits('n * 8)) -> bits('n * 8)
28 changes: 23 additions & 5 deletions model/riscv_mem.sail
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ function write_kind_of_flags (aq : bool, rl : bool, con : bool) -> write_kind =
(true, false, true) => throw(Error_not_implemented("sc.aq"))
}

function is_big_endian(typ : AccessType(ext_access_type)) -> bool = {
match effectivePrivilege(typ, mstatus, cur_privilege) {
Machine => bits_to_bool(mstatus[MBE]),
Supervisor => bits_to_bool(mstatus[SBE]),
User => bits_to_bool(mstatus[UBE]),
}
}

// only used for actual memory regions, to avoid MMIO effects
function phys_mem_read forall 'n, 0 < 'n <= max_mem_access . (t : AccessType(ext_access_type), paddr : physaddr, width : int('n), aq : bool, rl: bool, res : bool, meta : bool) -> MemoryOpResult((bits(8 * 'n), mem_meta)) = {
let result = (match read_kind_of_flags(aq, rl, res) {
Expand All @@ -73,9 +81,15 @@ function phys_mem_read forall 'n, 0 < 'n <= max_mem_access . (t : AccessType(ext
(Execute(), None()) => Err(E_Fetch_Access_Fault()),
(Read(Data), None()) => Err(E_Load_Access_Fault()),
(_, None()) => Err(E_SAMO_Access_Fault()),
(_, Some(v, m)) => { if get_config_print_mem()
then print_mem("mem[" ^ to_str(t) ^ "," ^ BitStr(physaddr_bits(paddr)) ^ "] -> " ^ BitStr(v));
Ok(v, m) }
(_, Some(v, m)) => {
let temp_var: bits('n * 8) = match (t, is_big_endian(t)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use a more specific/significant name than temp_var. Especially as we move to include Sail code in the ISA manual, we want to make sure the meaning of everything is clear.

(Execute(), _) => (v),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not move the check for an execute access into is_big_endian? It already needs to get AccessType to determine effective privilege, and if it is an execute type it will always be little endian.

(_, true) => reverse_endianness(v),
(_, false) => (v),
};
if get_config_print_mem()
then print_mem("mem[" ^ to_str(t) ^ "," ^ BitStr(physaddr_bits(paddr)) ^ "] -> " ^ BitStr(temp_var));
Ok(temp_var, m) }
}
}

Expand Down Expand Up @@ -196,9 +210,13 @@ $endif

// only used for actual memory regions, to avoid MMIO effects
function phys_mem_write forall 'n, 0 < 'n <= max_mem_access . (wk : write_kind, paddr : physaddr, width : int('n), data : bits(8 * 'n), meta : mem_meta) -> MemoryOpResult(bool) = {
let result = write_ram(wk, paddr, width, data, meta);
let temp_var: bits('n * 8) = match (is_big_endian(Write())) {
false => data,
true => reverse_endianness(data),
};
let result = write_ram(wk, paddr, width, temp_var, meta);
if get_config_print_mem()
then print_mem("mem[" ^ BitStr(physaddr_bits(paddr)) ^ "] <- " ^ BitStr(data));
then print_mem("mem[" ^ BitStr(physaddr_bits(paddr)) ^ "] <- " ^ BitStr(temp_var));
Ok(result)
}

Expand Down
5 changes: 1 addition & 4 deletions model/riscv_sys_control.sail
Original file line number Diff line number Diff line change
Expand Up @@ -341,10 +341,7 @@ function reset_sys() -> unit = {

// "If little-endian memory accesses are supported, the mstatus/mstatush field
// MBE is reset to 0."
// TODO: The handling of mstatush is a bit awkward currently, but the model
// currently only supports little endian so MBE is always 0.
// See https://github.com/riscv/sail-riscv/issues/639
// mstatus[MBE] = 0b0;
mstatus[MBE] = 0b0;

// "The misa register is reset to enable the maximal set of supported extensions"
reset_misa();
Expand Down
10 changes: 8 additions & 2 deletions model/riscv_sys_regs.sail
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ bitfield Mstatus : bits(64) = {
SPP : 8,

MPIE : 7,
UBE : 6,
SPIE : 5,

MIE : 3,
Expand Down Expand Up @@ -244,8 +245,8 @@ function legalize_mstatus(o : Mstatus, v : bits(64)) -> Mstatus = {
// MPV = v[MPV],
// GVA = v[GVA],
/* We don't currently support changing MBE and SBE. */
// MBE = v[MBE],
// SBE = v[SBE],
MBE = v[MBE],
SBE = if extensionEnabled(Ext_S) then v[SBE] else 0b0,
/* We don't support dynamic changes to SXL and UXL. */
// SXL = if xlen == 64 then v[SXL] else o[SXL],
// UXL = if xlen == 64 then v[UXL] else o[UXL],
Expand All @@ -269,6 +270,7 @@ function legalize_mstatus(o : Mstatus, v : bits(64)) -> Mstatus = {
SPP = if extensionEnabled(Ext_S) then v[SPP] else 0b0,
VS = v[VS],
MPIE = v[MPIE],
UBE = if extensionEnabled(Ext_U) then v[UBE] else 0b0,
SPIE = v[SPIE],
MIE = v[MIE],
SIE = v[SIE],
Expand All @@ -294,6 +296,7 @@ register mstatus : Mstatus = {
}

mapping clause csr_name_map = 0x300 <-> "mstatus"
mapping clause csr_name_map = 0x310 <-> "mstatush"

function clause is_CSR_defined(0x300) = true // mstatus
function clause is_CSR_defined(0x310) = xlen == 32 // mstatush
Expand Down Expand Up @@ -703,6 +706,7 @@ bitfield Sstatus : bits(64) = {
FS : 14 .. 13,
VS : 10 .. 9,
SPP : 8,
UBE : 6,
SPIE : 5,
SIE : 1,
}
Expand All @@ -722,6 +726,7 @@ function lower_mstatus(m : Mstatus) -> Sstatus = {
FS = m[FS],
VS = m[VS],
SPP = m[SPP],
UBE = m[UBE],
SPIE = m[SPIE],
SIE = m[SIE],
]
Expand All @@ -742,6 +747,7 @@ function lift_sstatus(m : Mstatus, s : Sstatus) -> Mstatus = {
FS = s[FS],
VS = s[VS],
SPP = s[SPP],
UBE = s[UBE],
SPIE = s[SPIE],
SIE = s[SIE],
]
Expand Down
Loading