mirror of
https://github.com/asterinas/asterinas.git
synced 2025-06-24 01:43:22 +00:00
Remove ostd/src/mm/offset.rs
This commit is contained in:
committed by
Tate, Hongliang Tian
parent
1a1d9bfb60
commit
a1f81df263
@ -18,7 +18,6 @@ use log::{debug, info};
|
||||
use ostd::{
|
||||
io::IoMem,
|
||||
mm::{DmaDirection, DmaStream, FrameAllocOptions, HasDaddr, VmIo, PAGE_SIZE},
|
||||
offset_of,
|
||||
sync::{LocalIrqDisabled, RwLock, SpinLock},
|
||||
trap::TrapFrame,
|
||||
};
|
||||
|
@ -6,7 +6,7 @@ use core::{fmt::Debug, hint::spin_loop, mem::size_of};
|
||||
use aster_network::{RxBuffer, TxBuffer};
|
||||
use aster_util::{field_ptr, slot_vec::SlotVec};
|
||||
use log::debug;
|
||||
use ostd::{mm::VmWriter, offset_of, sync::SpinLock, trap::TrapFrame, Pod};
|
||||
use ostd::{mm::VmWriter, sync::SpinLock, trap::TrapFrame, Pod};
|
||||
|
||||
use super::{
|
||||
config::{VirtioVsockConfig, VsockFeatures},
|
||||
|
@ -4,7 +4,7 @@
|
||||
|
||||
use alloc::vec::Vec;
|
||||
use core::{
|
||||
mem::size_of,
|
||||
mem::{offset_of, size_of},
|
||||
sync::atomic::{fence, Ordering},
|
||||
};
|
||||
|
||||
@ -14,7 +14,7 @@ use bitflags::bitflags;
|
||||
use log::debug;
|
||||
use ostd::{
|
||||
mm::{DmaCoherent, FrameAllocOptions, PodOnce},
|
||||
offset_of, Pod,
|
||||
Pod,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
@ -309,7 +309,7 @@ impl VirtQueue {
|
||||
let last_used_slot = self.last_used_idx & (self.queue_size - 1);
|
||||
let element_ptr = {
|
||||
let mut ptr = self.used.borrow_vm();
|
||||
ptr.byte_add(offset_of!(UsedRing, ring) as usize + last_used_slot as usize * 8);
|
||||
ptr.byte_add(offset_of!(UsedRing, ring) + last_used_slot as usize * 8);
|
||||
ptr.cast::<UsedElem>()
|
||||
};
|
||||
let index = field_ptr!(&element_ptr, UsedElem, id).read_once().unwrap();
|
||||
@ -333,7 +333,7 @@ impl VirtQueue {
|
||||
let last_used_slot = self.last_used_idx & (self.queue_size - 1);
|
||||
let element_ptr = {
|
||||
let mut ptr = self.used.borrow_vm();
|
||||
ptr.byte_add(offset_of!(UsedRing, ring) as usize + last_used_slot as usize * 8);
|
||||
ptr.byte_add(offset_of!(UsedRing, ring) + last_used_slot as usize * 8);
|
||||
ptr.cast::<UsedElem>()
|
||||
};
|
||||
let index = field_ptr!(&element_ptr, UsedElem, id).read_once().unwrap();
|
||||
|
@ -1,7 +1,7 @@
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
use alloc::{boxed::Box, sync::Arc};
|
||||
use core::mem::size_of;
|
||||
use core::mem::{offset_of, size_of};
|
||||
|
||||
use aster_rights::{ReadOp, WriteOp};
|
||||
use aster_util::{field_ptr, safe_ptr::SafePtr};
|
||||
@ -16,7 +16,6 @@ use ostd::{
|
||||
},
|
||||
io::IoMem,
|
||||
mm::{DmaCoherent, PAGE_SIZE},
|
||||
offset_of,
|
||||
sync::RwLock,
|
||||
trap::IrqCallbackFunction,
|
||||
};
|
||||
@ -66,9 +65,9 @@ impl VirtioMmioTransport {
|
||||
let interrupt_ack_offset = offset_of!(VirtioMmioLayout, interrupt_ack);
|
||||
let interrupt_status_offset = offset_of!(VirtioMmioLayout, interrupt_status);
|
||||
let mut interrupt_ack = layout.clone();
|
||||
interrupt_ack.byte_add(interrupt_ack_offset as usize);
|
||||
interrupt_ack.byte_add(interrupt_ack_offset);
|
||||
let mut interrupt_status = layout.clone();
|
||||
interrupt_status.byte_add(interrupt_status_offset as usize);
|
||||
interrupt_status.byte_add(interrupt_status_offset);
|
||||
(
|
||||
interrupt_ack.cast::<u32>().restrict::<WriteOp>(),
|
||||
interrupt_status.cast::<u32>().restrict::<ReadOp>(),
|
||||
@ -174,7 +173,7 @@ impl VirtioTransport for VirtioMmioTransport {
|
||||
}
|
||||
|
||||
fn notify_config(&self, _idx: usize) -> ConfigManager<u32> {
|
||||
let offset = offset_of!(VirtioMmioLayout, queue_notify) as usize;
|
||||
let offset = offset_of!(VirtioMmioLayout, queue_notify);
|
||||
let safe_ptr = Some(SafePtr::new(self.common_device.io_mem().clone(), offset));
|
||||
|
||||
ConfigManager::new(safe_ptr, None)
|
||||
|
@ -15,7 +15,6 @@ use ostd::{
|
||||
},
|
||||
io::IoMem,
|
||||
mm::DmaCoherent,
|
||||
offset_of,
|
||||
trap::IrqCallbackFunction,
|
||||
};
|
||||
|
||||
|
@ -410,21 +410,27 @@ impl<T, M: Debug, R> Debug for SafePtr<T, M, R> {
|
||||
#[macro_export]
|
||||
macro_rules! field_ptr {
|
||||
($ptr:expr, $type:ty, $($field:tt)+) => {{
|
||||
use ostd::offset_of;
|
||||
use aster_util::safe_ptr::SafePtr;
|
||||
|
||||
#[inline]
|
||||
fn new_field_ptr<T, M, R: Clone, U>(
|
||||
container_ptr: &SafePtr<T, M, R>,
|
||||
field_offset: *const U
|
||||
field_offset: usize,
|
||||
_type_infer: *const U,
|
||||
) -> SafePtr<U, &M, R>
|
||||
{
|
||||
let mut ptr = container_ptr.borrow_vm();
|
||||
ptr.byte_add(field_offset as usize);
|
||||
ptr.byte_add(field_offset);
|
||||
ptr.cast()
|
||||
}
|
||||
|
||||
let field_offset = offset_of!($type, $($field)*);
|
||||
new_field_ptr($ptr, field_offset)
|
||||
let field_offset = core::mem::offset_of!($type, $($field)*);
|
||||
let type_infer = ostd::ptr_null_of!({
|
||||
let ptr: *const $type = core::ptr::null();
|
||||
// This is not sound, but the code won't be executed.
|
||||
&raw const (*ptr).$($field)*
|
||||
});
|
||||
|
||||
new_field_ptr($ptr, field_offset, type_infer)
|
||||
}}
|
||||
}
|
||||
|
@ -1,44 +1,39 @@
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
use core::marker::PhantomData;
|
||||
|
||||
use ostd::Pod;
|
||||
|
||||
/// This ptr is designed to read union field safely.
|
||||
/// Write to union field is safe operation. While reading union field is UB.
|
||||
/// However, if this field is Pod, we can safely read that field.
|
||||
pub struct UnionReadPtr<'a, T: Pod> {
|
||||
/// A reader to read `Pod` fields from a `Pod` type.
|
||||
pub struct Reader<'a> {
|
||||
bytes: &'a [u8],
|
||||
marker: PhantomData<&'a mut T>,
|
||||
}
|
||||
|
||||
impl<'a, T: Pod> UnionReadPtr<'a, T> {
|
||||
pub fn new(object: &'a T) -> Self {
|
||||
let bytes = object.as_bytes();
|
||||
impl<'a> Reader<'a> {
|
||||
pub fn new<T: Pod>(object: &'a T) -> Self {
|
||||
Self {
|
||||
bytes,
|
||||
marker: PhantomData,
|
||||
bytes: object.as_bytes(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn read_at<F: Pod>(&self, offset: *const F) -> F {
|
||||
let offset = offset as usize;
|
||||
F::from_bytes(&self.bytes[offset..])
|
||||
pub fn read_at<F: Pod>(&self, field_offset: usize, _type_infer: *const F) -> F {
|
||||
F::from_bytes(&self.bytes[field_offset..])
|
||||
}
|
||||
}
|
||||
|
||||
/// FIXME: This macro requires the first argument to be a `reference` of a Pod type.
|
||||
/// This is because the value_offset macro requires
|
||||
#[macro_export]
|
||||
macro_rules! read_union_fields {
|
||||
($container:ident) => ({
|
||||
let offset = value_offset!($container);
|
||||
let union_read_ptr = UnionReadPtr::new(&*$container);
|
||||
union_read_ptr.read_at(offset)
|
||||
});
|
||||
($container:ident.$($field:ident).*) => ({
|
||||
let field_offset = ostd::value_offset!($container.$($field).*);
|
||||
let union_read_ptr = UnionReadPtr::new(&*$container);
|
||||
union_read_ptr.read_at(field_offset)
|
||||
});
|
||||
macro_rules! read_union_field {
|
||||
($container:expr, $type:ty, $($field:tt)+) => {{
|
||||
use $crate::union_read_ptr::Reader;
|
||||
|
||||
// Perform type checking first.
|
||||
let container: &$type = $container;
|
||||
let reader = Reader::new(container);
|
||||
|
||||
let field_offset = core::mem::offset_of!($type, $($field)*);
|
||||
let type_infer = ostd::ptr_null_of!({
|
||||
// This is not safe, but the code won't be executed.
|
||||
&raw const container.$($field)*
|
||||
});
|
||||
|
||||
reader.read_at(field_offset, type_infer)
|
||||
}}
|
||||
}
|
||||
|
@ -5,7 +5,7 @@
|
||||
|
||||
use core::mem::{self, size_of};
|
||||
|
||||
use aster_util::{read_union_fields, union_read_ptr::UnionReadPtr};
|
||||
use aster_util::read_union_field;
|
||||
|
||||
use super::sig_num::SigNum;
|
||||
use crate::{
|
||||
@ -55,8 +55,7 @@ impl siginfo_t {
|
||||
}
|
||||
|
||||
pub fn si_addr(&self) -> Vaddr {
|
||||
// let siginfo = *self;
|
||||
read_union_fields!(self.siginfo_fields.sigfault.addr)
|
||||
read_union_field!(self, Self, siginfo_fields.sigfault.addr)
|
||||
}
|
||||
}
|
||||
|
||||
@ -120,11 +119,11 @@ pub union sigval_t {
|
||||
|
||||
impl sigval_t {
|
||||
pub fn read_int(&self) -> i32 {
|
||||
read_union_fields!(self.sigval_int)
|
||||
read_union_field!(self, Self, sigval_int)
|
||||
}
|
||||
|
||||
pub fn read_ptr(&self) -> Vaddr {
|
||||
read_union_fields!(self.sigval_ptr)
|
||||
read_union_field!(self, Self, sigval_ptr)
|
||||
}
|
||||
}
|
||||
|
||||
@ -233,15 +232,15 @@ pub union _sigev_un {
|
||||
|
||||
impl _sigev_un {
|
||||
pub fn read_tid(&self) -> i32 {
|
||||
read_union_fields!(self._tid)
|
||||
read_union_field!(self, Self, _tid)
|
||||
}
|
||||
|
||||
pub fn read_function(&self) -> Vaddr {
|
||||
read_union_fields!(self._sigev_thread.function)
|
||||
read_union_field!(self, Self, _sigev_thread.function)
|
||||
}
|
||||
|
||||
pub fn read_attribute(&self) -> Vaddr {
|
||||
read_union_fields!(self._sigev_thread.attribute)
|
||||
read_union_field!(self, Self, _sigev_thread.attribute)
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user