Skip to content

Commit 025c6c8

Browse files
authored
Merge pull request #205 from rcore-os/physaddr
Use u64 rather than usize for PhysAddr
2 parents e817bee + 94e9f28 commit 025c6c8

File tree

14 files changed

+61
-56
lines changed

14 files changed

+61
-56
lines changed

examples/aarch64/src/hal.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,5 @@ unsafe impl Hal for HalImpl {
4848
}
4949

5050
fn virt_to_phys(vaddr: usize) -> PhysAddr {
51-
vaddr
51+
vaddr as _
5252
}

examples/riscv/src/virtio_impl.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use core::{
22
ptr::NonNull,
3-
sync::atomic::{AtomicUsize, Ordering},
3+
sync::atomic::{AtomicU64, Ordering},
44
};
55
use lazy_static::lazy_static;
66
use log::trace;
@@ -11,14 +11,14 @@ extern "C" {
1111
}
1212

1313
lazy_static! {
14-
static ref DMA_PADDR: AtomicUsize = AtomicUsize::new(end as usize);
14+
static ref DMA_PADDR: AtomicU64 = AtomicU64::new(end as u64);
1515
}
1616

1717
pub struct HalImpl;
1818

1919
unsafe impl Hal for HalImpl {
2020
fn dma_alloc(pages: usize, _direction: BufferDirection) -> (PhysAddr, NonNull<u8>) {
21-
let paddr = DMA_PADDR.fetch_add(PAGE_SIZE * pages, Ordering::SeqCst);
21+
let paddr = DMA_PADDR.fetch_add((PAGE_SIZE * pages) as u64, Ordering::SeqCst);
2222
trace!("alloc DMA: paddr={:#x}, pages={}", paddr, pages);
2323
let vaddr = NonNull::new(paddr as _).unwrap();
2424
(paddr, vaddr)
@@ -46,5 +46,5 @@ unsafe impl Hal for HalImpl {
4646
}
4747

4848
fn virt_to_phys(vaddr: usize) -> PhysAddr {
49-
vaddr
49+
vaddr as _
5050
}

examples/x86_64/src/hal.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use core::{
22
ptr::NonNull,
3-
sync::atomic::{AtomicUsize, Ordering},
3+
sync::atomic::{AtomicU64, Ordering},
44
};
55
use lazy_static::lazy_static;
66
use log::trace;
@@ -11,15 +11,14 @@ extern "C" {
1111
}
1212

1313
lazy_static! {
14-
static ref DMA_PADDR: AtomicUsize =
15-
AtomicUsize::new(unsafe { &dma_region as *const u8 as usize });
14+
static ref DMA_PADDR: AtomicU64 = AtomicU64::new(unsafe { &dma_region as *const u8 as u64 });
1615
}
1716

1817
pub struct HalImpl;
1918

2019
unsafe impl Hal for HalImpl {
2120
fn dma_alloc(pages: usize, _direction: BufferDirection) -> (PhysAddr, NonNull<u8>) {
22-
let paddr = DMA_PADDR.fetch_add(PAGE_SIZE * pages, Ordering::SeqCst);
21+
let paddr = DMA_PADDR.fetch_add((PAGE_SIZE * pages) as u64, Ordering::SeqCst);
2322
trace!("alloc DMA: paddr={:#x}, pages={}", paddr, pages);
2423
let vaddr = NonNull::new(paddr as _).unwrap();
2524
(paddr, vaddr)
@@ -47,5 +46,5 @@ unsafe impl Hal for HalImpl {
4746
}
4847

4948
fn virt_to_phys(vaddr: usize) -> PhysAddr {
50-
vaddr
49+
vaddr as _
5150
}

examples/x86_64/src/main.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#![no_std]
22
#![no_main]
3-
#![feature(asm_const)]
43
#![feature(abi_x86_interrupt)]
54

65
#[macro_use]

src/hal.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ use crate::{Error, Result, PAGE_SIZE};
55
use core::{marker::PhantomData, ptr::NonNull};
66

77
/// A physical address as used for virtio.
8-
pub type PhysAddr = usize;
8+
pub type PhysAddr = u64;
99

1010
/// A region of contiguous physical memory used for DMA.
1111
#[derive(Debug)]
1212
pub struct Dma<H: Hal> {
13-
paddr: usize,
13+
paddr: PhysAddr,
1414
vaddr: NonNull<u8>,
1515
pages: usize,
1616
_hal: PhantomData<H>,
@@ -42,7 +42,7 @@ impl<H: Hal> Dma<H> {
4242
}
4343

4444
/// Returns the physical address of the start of the DMA region, as seen by devices.
45-
pub fn paddr(&self) -> usize {
45+
pub fn paddr(&self) -> PhysAddr {
4646
self.paddr
4747
}
4848

src/hal/fake.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ unsafe impl Hal for FakeHal {
8282
}
8383

8484
fn virt_to_phys(vaddr: usize) -> PhysAddr {
85-
vaddr
85+
vaddr as _
8686
}
8787

8888
fn phys_to_virt(paddr: PhysAddr) -> usize {
89-
paddr
89+
paddr as _
9090
}

src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ pub use safe_mmio::UniqueMmioPointer;
6666
/// The page size in bytes supported by the library (4 KiB).
6767
pub const PAGE_SIZE: usize = 0x1000;
6868

69+
const PAGE_SIZE_PHYS: PhysAddr = PAGE_SIZE as PhysAddr;
70+
6971
/// The type returned by driver methods.
7072
pub type Result<T = ()> = core::result::Result<T, Error>;
7173

@@ -119,6 +121,11 @@ fn align_up(size: usize) -> usize {
119121
(size + PAGE_SIZE) & !(PAGE_SIZE - 1)
120122
}
121123

124+
/// Align `size` up to a page.
125+
fn align_up_phys(size: PhysAddr) -> PhysAddr {
126+
(size + PAGE_SIZE_PHYS) & !(PAGE_SIZE_PHYS - 1)
127+
}
128+
122129
/// The number of pages required to store `size` bytes, rounded up to a whole number of pages.
123130
fn pages(size: usize) -> usize {
124131
size.div_ceil(PAGE_SIZE)

src/queue.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ impl<H: Hal, const SIZE: usize> VirtQueue<H, SIZE> {
443443
// `indirect_list` is owned by this function and is not accessed from any other threads.
444444
unsafe {
445445
H::unshare(
446-
paddr as usize,
446+
paddr,
447447
indirect_list.as_mut_bytes().into(),
448448
BufferDirection::DriverToDevice,
449449
);
@@ -459,7 +459,7 @@ impl<H: Hal, const SIZE: usize> VirtQueue<H, SIZE> {
459459
unsafe {
460460
// Unshare the buffer (and perhaps copy its contents back to the original
461461
// buffer).
462-
H::unshare(indirect_list[i].addr as usize, buffer, direction);
462+
H::unshare(indirect_list[i].addr, buffer, direction);
463463
}
464464
}
465465
drop(indirect_list);
@@ -487,7 +487,7 @@ impl<H: Hal, const SIZE: usize> VirtQueue<H, SIZE> {
487487
// from which we got `paddr`.
488488
unsafe {
489489
// Unshare the buffer (and perhaps copy its contents back to the original buffer).
490-
H::unshare(paddr as usize, buffer, direction);
490+
H::unshare(paddr, buffer, direction);
491491
}
492492
}
493493

@@ -640,12 +640,12 @@ impl<H: Hal> VirtQueueLayout<H> {
640640
match self {
641641
Self::Legacy {
642642
dma, avail_offset, ..
643-
} => dma.paddr() + avail_offset,
643+
} => dma.paddr() + *avail_offset as u64,
644644
Self::Modern {
645645
driver_to_device_dma,
646646
avail_offset,
647647
..
648-
} => driver_to_device_dma.paddr() + avail_offset,
648+
} => driver_to_device_dma.paddr() + *avail_offset as u64,
649649
}
650650
}
651651

@@ -668,7 +668,7 @@ impl<H: Hal> VirtQueueLayout<H> {
668668
match self {
669669
Self::Legacy {
670670
used_offset, dma, ..
671-
} => dma.paddr() + used_offset,
671+
} => dma.paddr() + *used_offset as u64,
672672
Self::Modern {
673673
device_to_driver_dma,
674674
..
@@ -699,7 +699,7 @@ fn queue_part_sizes(queue_size: u16) -> (usize, usize, usize) {
699699
queue_size.is_power_of_two(),
700700
"queue size should be a power of 2"
701701
);
702-
let queue_size = queue_size as usize;
702+
let queue_size = usize::from(queue_size);
703703
let desc = size_of::<Descriptor>() * queue_size;
704704
let avail = size_of::<u16>() * (3 + queue_size);
705705
let used = size_of::<u16>() * 3 + size_of::<UsedElem>() * queue_size;
@@ -729,7 +729,7 @@ impl Descriptor {
729729
) {
730730
// SAFETY: Our caller promises that the buffer is valid.
731731
unsafe {
732-
self.addr = H::share(buf, direction) as u64;
732+
self.addr = H::share(buf, direction);
733733
}
734734
self.len = buf.len().try_into().unwrap();
735735
self.flags = extra_flags

src/transport/mmio.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
//! MMIO transport for VirtIO.
22
33
use super::{DeviceStatus, DeviceType, DeviceTypeError, Transport};
4-
use crate::{align_up, queue::Descriptor, transport::InterruptStatus, Error, PhysAddr, PAGE_SIZE};
4+
use crate::{
5+
align_up_phys, queue::Descriptor, transport::InterruptStatus, Error, PhysAddr, PAGE_SIZE,
6+
PAGE_SIZE_PHYS,
7+
};
58
use core::{
69
convert::{TryFrom, TryInto},
710
mem::{align_of, size_of},
@@ -403,18 +406,18 @@ impl Transport for MmioTransport<'_> {
403406
MmioVersion::Legacy => {
404407
assert_eq!(
405408
driver_area - descriptors,
406-
size_of::<Descriptor>() * size as usize
409+
size_of::<Descriptor>() as u64 * u64::from(size)
407410
);
408411
assert_eq!(
409412
device_area - descriptors,
410-
align_up(
411-
size_of::<Descriptor>() * size as usize
412-
+ size_of::<u16>() * (size as usize + 3)
413+
align_up_phys(
414+
size_of::<Descriptor>() as u64 * u64::from(size)
415+
+ size_of::<u16>() as u64 * (u64::from(size) + 3)
413416
)
414417
);
415418
let align = PAGE_SIZE as u32;
416-
let pfn = (descriptors / PAGE_SIZE) as u32;
417-
assert_eq!(pfn as usize * PAGE_SIZE, descriptors);
419+
let pfn = (descriptors / PAGE_SIZE_PHYS).try_into().unwrap();
420+
assert_eq!(u64::from(pfn) * PAGE_SIZE_PHYS, descriptors);
418421
field!(self.header, queue_sel).write(queue.into());
419422
field!(self.header, queue_num).write(size);
420423
field!(self.header, legacy_queue_align).write(align);

src/transport/pci.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -283,9 +283,9 @@ impl Transport for PciTransport {
283283
) {
284284
field!(self.common_cfg, queue_select).write(queue);
285285
field!(self.common_cfg, queue_size).write(size as u16);
286-
field!(self.common_cfg, queue_desc).write(descriptors as u64);
287-
field!(self.common_cfg, queue_driver).write(driver_area as u64);
288-
field!(self.common_cfg, queue_device).write(device_area as u64);
286+
field!(self.common_cfg, queue_desc).write(descriptors);
287+
field!(self.common_cfg, queue_driver).write(driver_area);
288+
field!(self.common_cfg, queue_device).write(device_area);
289289
field!(self.common_cfg, queue_enable).write(1);
290290
}
291291

@@ -506,12 +506,6 @@ impl From<PciError> for VirtioPciError {
506506
}
507507
}
508508

509-
// SAFETY: The `vaddr` field of `VirtioPciError::Misaligned` is only used for debug output.
510-
unsafe impl Send for VirtioPciError {}
511-
512-
// SAFETY: The `vaddr` field of `VirtioPciError::Misaligned` is only used for debug output.
513-
unsafe impl Sync for VirtioPciError {}
514-
515509
#[cfg(test)]
516510
mod tests {
517511
use super::*;

0 commit comments

Comments
 (0)