Skip to content

Commit

Permalink
greq: use addr_of!() / addr_of_mut() instead of "as" casting
Browse files Browse the repository at this point in the history
This makes code more explicit and clearer.

Signed-off-by: Carlos López <carlos.lopez@suse.com>
  • Loading branch information
00xc committed Nov 23, 2023
1 parent c9dfb80 commit 8a393cd
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
7 changes: 4 additions & 3 deletions src/greq/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
extern crate alloc;

use alloc::boxed::Box;
use core::ptr::addr_of_mut;
use core::{cell::OnceCell, mem::size_of};

use crate::{
Expand Down Expand Up @@ -158,9 +159,9 @@ impl SnpGuestRequestDriver {
fn send(&mut self, req_class: SnpGuestRequestClass) -> Result<(), SvsmReqError> {
self.response.clear();

let req_page = VirtAddr::from(&mut *self.request as *mut SnpGuestRequestMsg);
let resp_page = VirtAddr::from(&mut *self.response as *mut SnpGuestRequestMsg);
let data_pages = VirtAddr::from(&mut *self.ext_data as *mut SnpGuestRequestExtData);
let req_page = VirtAddr::from(addr_of_mut!(*self.request));
let resp_page = VirtAddr::from(addr_of_mut!(*self.response));
let data_pages = VirtAddr::from(addr_of_mut!(*self.ext_data));

if req_class == SnpGuestRequestClass::Extended {
let num_user_pages = (self.user_extdata_size >> PAGE_SHIFT) as u64;
Expand Down
12 changes: 6 additions & 6 deletions src/greq/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use alloc::{
};
use core::{
mem::size_of,
ptr::addr_of,
ptr::{addr_of, addr_of_mut},
slice::{from_raw_parts, from_raw_parts_mut, from_ref},
};

Expand Down Expand Up @@ -175,7 +175,7 @@ impl SnpGuestRequestMsgHdr {

/// Get [`SnpGuestRequestMsgHdr`] as a mutable slice reference
fn as_slice_mut(&mut self) -> &mut [u8] {
unsafe { from_raw_parts_mut(self as *mut _ as *mut u8, size_of::<Self>()) }
unsafe { from_raw_parts_mut(addr_of_mut!(*self).cast(), size_of::<Self>()) }
}
}

Expand Down Expand Up @@ -244,7 +244,7 @@ impl SnpGuestRequestMsg {
/// before the object is dropped. Shared pages should not be freed
/// (returned to the allocator)
pub fn set_shared(&mut self) -> Result<(), SvsmReqError> {
let vaddr = VirtAddr::from(self as *mut Self);
let vaddr = VirtAddr::from(addr_of!(*self));
this_cpu_mut()
.get_pgtable()
.set_shared_4k(vaddr)
Expand All @@ -264,7 +264,7 @@ impl SnpGuestRequestMsg {

/// Set the C-bit (memory encryption bit) for the Self page
pub fn set_encrypted(&mut self) -> Result<(), SvsmReqError> {
let vaddr = VirtAddr::from(self as *mut Self);
let vaddr = VirtAddr::from(addr_of!(*self));
this_cpu_mut()
.get_pgtable()
.set_encrypted_4k(vaddr)
Expand Down Expand Up @@ -505,14 +505,14 @@ impl SnpGuestRequestExtData {
/// before the object is dropped. Shared pages should not be freed
/// (returned to the allocator)
pub fn set_shared(&mut self) -> Result<(), SvsmReqError> {
let start = VirtAddr::from(self as *mut Self);
let start = VirtAddr::from(addr_of!(*self));
let end = start + size_of::<Self>();
set_shared_region_4k(start, end)
}

/// Set the C-bit (memory encryption bit) for the Self pages
pub fn set_encrypted(&mut self) -> Result<(), SvsmReqError> {
let start = VirtAddr::from(self as *mut Self);
let start = VirtAddr::from(addr_of!(*self));
let end = start + size_of::<Self>();
set_encrypted_region_4k(start, end)
}
Expand Down

0 comments on commit 8a393cd

Please sign in to comment.