-
-
Notifications
You must be signed in to change notification settings - Fork 54
Support Host I/O operations #66
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
Merged
Merged
Changes from 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
4a814af
Support Host I/O operations
bet4it 6823d00
Fix argument types
bet4it 774e780
Add missing Host I/O operations
bet4it 1e750db
Some fixes
bet4it 2465d6d
Change return type to HostIoResult
bet4it 41751fe
Improve handle_hostio_result macro
bet4it dc1f56e
Implement real filesystem access in example
bet4it 72b9ce2
Supply example with a complete real filesystem access implementation
bet4it 85a6acc
Store files in Vec
bet4it e20d15d
Simplify code
bet4it 7c03939
Allow non-ASCII characters in packet
bet4it 641eb0c
Optimize away the bounds checks in decode_bin_buf
bet4it 2ba3129
Fix style
bet4it 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,224 @@ | ||
use std::io::{Read, Seek, Write}; | ||
|
||
use gdbstub::target; | ||
use gdbstub::target::ext::host_io::{ | ||
FsKind, HostIoErrno, HostIoError, HostIoOpenFlags, HostIoOpenMode, HostIoOutput, HostIoResult, | ||
HostIoStat, HostIoToken, | ||
}; | ||
|
||
use crate::emu::Emu; | ||
|
||
impl target::ext::host_io::HostIo for Emu { | ||
#[inline(always)] | ||
fn enable_open(&mut self) -> Option<target::ext::host_io::HostIoOpenOps<Self>> { | ||
Some(self) | ||
} | ||
|
||
#[inline(always)] | ||
fn enable_close(&mut self) -> Option<target::ext::host_io::HostIoCloseOps<Self>> { | ||
Some(self) | ||
} | ||
|
||
#[inline(always)] | ||
fn enable_pread(&mut self) -> Option<target::ext::host_io::HostIoPreadOps<Self>> { | ||
Some(self) | ||
} | ||
|
||
#[inline(always)] | ||
fn enable_pwrite(&mut self) -> Option<target::ext::host_io::HostIoPwriteOps<Self>> { | ||
Some(self) | ||
} | ||
|
||
#[inline(always)] | ||
fn enable_fstat(&mut self) -> Option<target::ext::host_io::HostIoFstatOps<Self>> { | ||
Some(self) | ||
} | ||
|
||
#[inline(always)] | ||
fn enable_unlink(&mut self) -> Option<target::ext::host_io::HostIoUnlinkOps<Self>> { | ||
Some(self) | ||
} | ||
|
||
#[inline(always)] | ||
fn enable_readlink(&mut self) -> Option<target::ext::host_io::HostIoReadlinkOps<Self>> { | ||
Some(self) | ||
} | ||
|
||
#[inline(always)] | ||
fn enable_setfs(&mut self) -> Option<target::ext::host_io::HostIoSetfsOps<Self>> { | ||
Some(self) | ||
} | ||
} | ||
|
||
impl target::ext::host_io::HostIoOpen for Emu { | ||
fn open( | ||
&mut self, | ||
filename: &[u8], | ||
flags: HostIoOpenFlags, | ||
_mode: HostIoOpenMode, | ||
) -> HostIoResult<u32, Self> { | ||
if filename.starts_with(b"/proc") { | ||
return Err(HostIoError::Errno(HostIoErrno::ENOENT)); | ||
} | ||
|
||
let path = | ||
std::str::from_utf8(filename).map_err(|_| HostIoError::Errno(HostIoErrno::ENOENT))?; | ||
|
||
let mut read = false; | ||
let mut write = false; | ||
if flags.contains(HostIoOpenFlags::O_RDWR) { | ||
read = true; | ||
write = true; | ||
} else if flags.contains(HostIoOpenFlags::O_WRONLY) { | ||
write = true; | ||
} else { | ||
read = true; | ||
} | ||
|
||
let file = std::fs::OpenOptions::new() | ||
.read(read) | ||
.write(write) | ||
.append(flags.contains(HostIoOpenFlags::O_APPEND)) | ||
.create(flags.contains(HostIoOpenFlags::O_CREAT)) | ||
.truncate(flags.contains(HostIoOpenFlags::O_TRUNC)) | ||
.create_new(flags.contains(HostIoOpenFlags::O_EXCL)) | ||
.open(path)?; | ||
|
||
let n = match self.files.iter_mut().enumerate().find(|(_, f)| f.is_none()) { | ||
Some((n, free_file)) => { | ||
*free_file = Some(file); | ||
n | ||
} | ||
None => { | ||
self.files.push(Some(file)); | ||
self.files.len() - 1 | ||
} | ||
}; | ||
|
||
Ok(n as u32) | ||
} | ||
} | ||
|
||
impl target::ext::host_io::HostIoClose for Emu { | ||
fn close(&mut self, fd: u32) -> HostIoResult<(), Self> { | ||
if let Some(file) = self.files.get_mut(fd as usize) { | ||
bet4it marked this conversation as resolved.
Show resolved
Hide resolved
|
||
file.take().ok_or(HostIoError::Errno(HostIoErrno::EBADF))?; | ||
while let Some(None) = self.files.last() { | ||
self.files.pop(); | ||
} | ||
Ok(()) | ||
} else { | ||
Err(HostIoError::Errno(HostIoErrno::EBADF)) | ||
} | ||
} | ||
} | ||
|
||
impl target::ext::host_io::HostIoPread for Emu { | ||
fn pread<'a>( | ||
&mut self, | ||
fd: u32, | ||
count: u32, | ||
offset: u32, | ||
output: HostIoOutput<'a>, | ||
) -> HostIoResult<HostIoToken<'a>, Self> { | ||
if let Some(Some(file)) = self.files.get_mut(fd as usize) { | ||
let mut buffer = vec![0; count as usize]; | ||
file.seek(std::io::SeekFrom::Start(offset as u64))?; | ||
let n = file.read(&mut buffer)?; | ||
Ok(output.write(&buffer[..n])) | ||
} else { | ||
Err(HostIoError::Errno(HostIoErrno::EBADF)) | ||
} | ||
} | ||
} | ||
|
||
impl target::ext::host_io::HostIoPwrite for Emu { | ||
fn pwrite(&mut self, fd: u32, offset: u32, data: &[u8]) -> HostIoResult<u32, Self> { | ||
if let Some(Some(file)) = self.files.get_mut(fd as usize) { | ||
file.seek(std::io::SeekFrom::Start(offset as u64))?; | ||
let n = file.write(data)?; | ||
Ok(n as u32) | ||
} else { | ||
Err(HostIoError::Errno(HostIoErrno::EBADF)) | ||
} | ||
} | ||
} | ||
|
||
impl target::ext::host_io::HostIoFstat for Emu { | ||
fn fstat(&mut self, fd: u32) -> HostIoResult<HostIoStat, Self> { | ||
if let Some(Some(file)) = self.files.get(fd as usize) { | ||
let metadata = file.metadata()?; | ||
macro_rules! time_to_secs { | ||
($time:expr) => { | ||
$time | ||
.map_err(|_| HostIoError::Errno(HostIoErrno::EACCES))? | ||
.duration_since(std::time::SystemTime::UNIX_EPOCH) | ||
.map_err(|_| HostIoError::Errno(HostIoErrno::EACCES))? | ||
.as_secs() as u32 | ||
}; | ||
} | ||
let atime = time_to_secs!(metadata.accessed()); | ||
let mtime = time_to_secs!(metadata.modified()); | ||
let ctime = time_to_secs!(metadata.created()); | ||
Ok(HostIoStat { | ||
st_dev: 0, | ||
st_ino: 0, | ||
st_mode: HostIoOpenMode::empty(), | ||
st_nlink: 0, | ||
st_uid: 0, | ||
st_gid: 0, | ||
st_rdev: 0, | ||
st_size: metadata.len(), | ||
st_blksize: 0, | ||
st_blocks: 0, | ||
st_atime: atime, | ||
st_mtime: mtime, | ||
st_ctime: ctime, | ||
}) | ||
} else { | ||
Err(HostIoError::Errno(HostIoErrno::EBADF)) | ||
} | ||
} | ||
} | ||
|
||
impl target::ext::host_io::HostIoUnlink for Emu { | ||
fn unlink(&mut self, filename: &[u8]) -> HostIoResult<(), Self> { | ||
let path = | ||
std::str::from_utf8(filename).map_err(|_| HostIoError::Errno(HostIoErrno::ENOENT))?; | ||
std::fs::remove_file(path)?; | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl target::ext::host_io::HostIoReadlink for Emu { | ||
fn readlink<'a>( | ||
&mut self, | ||
filename: &[u8], | ||
output: HostIoOutput<'a>, | ||
) -> HostIoResult<HostIoToken<'a>, Self> { | ||
if filename == b"/proc/1/exe" { | ||
// Support `info proc exe` command | ||
return Ok(output.write(b"/test.elf")); | ||
} else if filename == b"/proc/1/cwd" { | ||
// Support `info proc cwd` command | ||
return Ok(output.write(b"/")); | ||
} else if filename.starts_with(b"/proc") { | ||
return Err(HostIoError::Errno(HostIoErrno::ENOENT)); | ||
} | ||
|
||
let path = | ||
std::str::from_utf8(filename).map_err(|_| HostIoError::Errno(HostIoErrno::ENOENT))?; | ||
Ok(output.write( | ||
std::fs::read_link(path)? | ||
.to_str() | ||
.ok_or(HostIoError::Errno(HostIoErrno::ENOENT))? | ||
.as_bytes(), | ||
)) | ||
} | ||
} | ||
|
||
impl target::ext::host_io::HostIoSetfs for Emu { | ||
fn setfs(&mut self, _fs: FsKind) -> HostIoResult<(), Self> { | ||
Ok(()) | ||
} | ||
} |
This file contains hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.