-
Notifications
You must be signed in to change notification settings - Fork 69
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
Refactored flexible memory implementation #378
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
56e8d06
Refactor dymnaic memory
mohanson d2626d4
Refactor memory flags
mohanson 83e7b33
Refactor frames flags
mohanson e826dc6
Small optimization
mohanson ee29e53
Use less instructions for _CHECK_READ_FRAMES on aarch64
mohanson 89bc29a
Small optimization
mohanson 31ba523
Small optimization
mohanson 62480dc
Use alloc instead of vec
mohanson 2df5fc9
Add cast_ptr_to_slice and cast_ptr_to_slice_mut methods
mohanson 172d9f8
Fix clippy
mohanson f121d77
Remove memory size limit
mohanson ceebd24
Let AsmCoreMachine be responsible for memory allocation and release
mohanson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
use crate::{ | ||
instructions::Instruction, MEMORY_FRAMES, MEMORY_FRAMESIZE, MEMORY_FRAME_SHIFTS, | ||
RISCV_GENERAL_REGISTER_NUMBER, RISCV_MAX_MEMORY, RISCV_PAGES, RISCV_PAGESIZE, | ||
instructions::Instruction, DEFAULT_MEMORY_SIZE, MEMORY_FRAMESIZE, MEMORY_FRAME_SHIFTS, | ||
RISCV_GENERAL_REGISTER_NUMBER, RISCV_PAGESIZE, | ||
}; | ||
use std::alloc::{alloc, Layout}; | ||
use std::alloc::{alloc, alloc_zeroed, dealloc, Layout}; | ||
|
||
// The number of trace items to keep | ||
pub const TRACE_SIZE: usize = 8192; | ||
|
@@ -101,15 +101,25 @@ pub struct AsmCoreMachine { | |
pub last_read_frame: u64, | ||
pub last_write_page: u64, | ||
|
||
pub flags: [u8; RISCV_PAGES], | ||
pub frames: [u8; MEMORY_FRAMES], | ||
pub memory_ptr: u64, | ||
pub flags_ptr: u64, | ||
pub frames_ptr: u64, | ||
} | ||
|
||
pub memory: [u8; RISCV_MAX_MEMORY], | ||
impl Drop for AsmCoreMachine { | ||
fn drop(&mut self) { | ||
let memory_layout = Layout::array::<u8>(self.memory_size as usize).unwrap(); | ||
unsafe { dealloc(self.memory_ptr as *mut u8, memory_layout) }; | ||
let flags_layout = Layout::array::<u8>(self.flags_size as usize).unwrap(); | ||
unsafe { dealloc(self.flags_ptr as *mut u8, flags_layout) }; | ||
let frames_layout = Layout::array::<u8>(self.frames_size as usize).unwrap(); | ||
unsafe { dealloc(self.frames_ptr as *mut u8, frames_layout) }; | ||
} | ||
} | ||
|
||
impl AsmCoreMachine { | ||
pub fn new(isa: u8, version: u32, max_cycles: u64) -> Box<AsmCoreMachine> { | ||
Self::new_with_memory(isa, version, max_cycles, RISCV_MAX_MEMORY) | ||
Self::new_with_memory(isa, version, max_cycles, DEFAULT_MEMORY_SIZE) | ||
} | ||
|
||
pub fn new_with_memory( | ||
|
@@ -119,15 +129,10 @@ impl AsmCoreMachine { | |
memory_size: usize, | ||
) -> Box<AsmCoreMachine> { | ||
assert_ne!(memory_size, 0); | ||
assert!(memory_size <= RISCV_MAX_MEMORY); | ||
assert_eq!(memory_size % RISCV_PAGESIZE, 0); | ||
assert_eq!(memory_size % (1 << MEMORY_FRAME_SHIFTS), 0); | ||
|
||
let mut machine = unsafe { | ||
let machine_size = | ||
std::mem::size_of::<AsmCoreMachine>() - RISCV_MAX_MEMORY + memory_size; | ||
|
||
let layout = Layout::array::<u8>(machine_size).unwrap(); | ||
let layout = Layout::new::<AsmCoreMachine>(); | ||
let raw_allocation = alloc(layout) as *mut AsmCoreMachine; | ||
Box::from_raw(raw_allocation) | ||
}; | ||
|
@@ -146,8 +151,6 @@ impl AsmCoreMachine { | |
machine.reset_signal = 0; | ||
machine.version = version; | ||
machine.isa = isa; | ||
machine.flags = [0; RISCV_PAGES]; | ||
machine.frames = [0; MEMORY_FRAMES]; | ||
|
||
machine.memory_size = memory_size as u64; | ||
machine.frames_size = (memory_size / MEMORY_FRAMESIZE) as u64; | ||
|
@@ -156,10 +159,35 @@ impl AsmCoreMachine { | |
machine.last_read_frame = u64::max_value(); | ||
machine.last_write_page = u64::max_value(); | ||
|
||
let memory_layout = Layout::array::<u8>(machine.memory_size as usize).unwrap(); | ||
machine.memory_ptr = unsafe { alloc(memory_layout) } as u64; | ||
let flags_layout = Layout::array::<u8>(machine.flags_size as usize).unwrap(); | ||
machine.flags_ptr = unsafe { alloc_zeroed(flags_layout) } as u64; | ||
let frames_layout = Layout::array::<u8>(machine.frames_size as usize).unwrap(); | ||
machine.frames_ptr = unsafe { alloc_zeroed(frames_layout) } as u64; | ||
|
||
machine | ||
} | ||
|
||
pub fn set_max_cycles(&mut self, cycles: u64) { | ||
self.max_cycles = cycles; | ||
} | ||
} | ||
|
||
impl AsmCoreMachine { | ||
pub fn cast_ptr_to_slice(&self, ptr: u64, offset: usize, size: usize) -> &[u8] { | ||
unsafe { | ||
let ptr = ptr as *mut u8; | ||
let ptr = ptr.add(offset); | ||
std::slice::from_raw_parts(ptr, size) | ||
} | ||
} | ||
|
||
pub fn cast_ptr_to_slice_mut(&self, ptr: u64, offset: usize, size: usize) -> &mut [u8] { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
unsafe { | ||
let ptr = ptr as *mut u8; | ||
let ptr = ptr.add(offset); | ||
std::slice::from_raw_parts_mut(ptr, size) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
&self
is not needed.