-
Notifications
You must be signed in to change notification settings - Fork 65
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
Add methods to Transport to read and write config space, rather than returning a pointer. #163
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,9 +3,10 @@ | |
use crate::hal::Hal; | ||
use crate::queue::VirtQueue; | ||
use crate::transport::Transport; | ||
use crate::volatile::{volread, Volatile}; | ||
use crate::volatile::Volatile; | ||
use crate::{Error, Result}; | ||
use bitflags::bitflags; | ||
use core::mem::offset_of; | ||
use log::info; | ||
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout}; | ||
|
||
|
@@ -55,12 +56,10 @@ impl<H: Hal, T: Transport> VirtIOBlk<H, T> { | |
let negotiated_features = transport.begin_init(SUPPORTED_FEATURES); | ||
|
||
// Read configuration space. | ||
let config = transport.config_space::<BlkConfig>()?; | ||
info!("config: {:?}", config); | ||
// Safe because config is a valid pointer to the device configuration space. | ||
let capacity = unsafe { | ||
volread!(config, capacity_low) as u64 | (volread!(config, capacity_high) as u64) << 32 | ||
}; | ||
let capacity = transport.read_config_space::<u32>(offset_of!(BlkConfig, capacity_low))? | ||
as u64 | ||
| (transport.read_config_space::<u32>(offset_of!(BlkConfig, capacity_high))? as u64) | ||
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. not related to this PR, but technically you should use the config generation field when doing multiple part reads like this: https://docs.oasis-open.org/virtio/virtio/v1.2/csd01/virtio-v1.2-csd01.html#x1-230001 (FWIW, the crosvm VMM currently always has 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. Good point, I missed that. Done in #169. |
||
<< 32; | ||
info!("found a block device of size {}KB", capacity / 2); | ||
|
||
let queue = VirtQueue::new( | ||
|
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.
In the new code, the you have to be careful about keeping the template param and the field types in sync at every call point. It looks easy to mess up. I wonder if we could have a macro like
read_config_space!(transport, BlkConfig, capacity_low)
that handles the offset and makes sure the types line up properly.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.
I had the same thought, but can't find any way to do so as there doesn't seem to be any macro to get the type of a struct field from its name.
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.
Here is a trick
Seems to optimize out https://godbolt.org/z/hzWGjqxY1
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.
I don't think getting the size helps, the issue is to get the type. But I think I have found a way around that, I'll send another PR in a bit.
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.
Example at #171
Whoops, it seems I only half read your comment, didn't see that you were going to send a PR until after I sent that one just now.