1
1
//! Physical and virtual addresses manipulation
2
2
3
+ #[ cfg( feature = "memory_encryption" ) ]
4
+ use crate :: structures:: mem_encrypt:: ENC_BIT_MASK ;
5
+ use crate :: structures:: paging:: page_table:: PageTableLevel ;
6
+ use crate :: structures:: paging:: { PageOffset , PageTableIndex } ;
7
+ use bit_field:: BitField ;
3
8
use core:: convert:: TryFrom ;
4
9
use core:: fmt;
5
10
#[ cfg( feature = "step_trait" ) ]
6
11
use core:: iter:: Step ;
7
12
use core:: ops:: { Add , AddAssign , Sub , SubAssign } ;
8
-
9
- use crate :: structures:: paging:: page_table:: PageTableLevel ;
10
- use crate :: structures:: paging:: { PageOffset , PageTableIndex } ;
11
- use bit_field:: BitField ;
13
+ #[ cfg( feature = "memory_encryption" ) ]
14
+ use core:: sync:: atomic:: Ordering ;
12
15
13
16
const ADDRESS_SPACE_SIZE : u64 = 0x1_0000_0000_0000 ;
14
17
@@ -439,6 +442,8 @@ impl PhysAddr {
439
442
/// ## Panics
440
443
///
441
444
/// This function panics if a bit in the range 52 to 64 is set.
445
+ /// If the `memory_encryption` feature is available and has been enabled, this function also
446
+ /// panics fails if the encryption bit is manually set in the address.
442
447
#[ inline]
443
448
pub const fn new ( addr : u64 ) -> Self {
444
449
// TODO: Replace with .ok().expect(msg) when that works on stable.
@@ -448,12 +453,6 @@ impl PhysAddr {
448
453
}
449
454
}
450
455
451
- /// Creates a new physical address, throwing bits 52..64 away.
452
- #[ inline]
453
- pub const fn new_truncate ( addr : u64 ) -> PhysAddr {
454
- PhysAddr ( addr % ( 1 << 52 ) )
455
- }
456
-
457
456
/// Creates a new physical address, without any checks.
458
457
///
459
458
/// ## Safety
@@ -467,6 +466,8 @@ impl PhysAddr {
467
466
/// Tries to create a new physical address.
468
467
///
469
468
/// Fails if any bits in the range 52 to 64 are set.
469
+ /// If the `memory_encryption` feature is available and has been enabled, this also fails if the
470
+ /// encryption bit is manually set in the address.
470
471
#[ inline]
471
472
pub const fn try_new ( addr : u64 ) -> Result < Self , PhysAddrNotValid > {
472
473
let p = Self :: new_truncate ( addr) ;
@@ -546,6 +547,24 @@ impl PhysAddr {
546
547
}
547
548
}
548
549
550
+ #[ cfg( feature = "memory_encryption" ) ]
551
+ impl PhysAddr {
552
+ /// Creates a new physical address, throwing bits 52..64 and the encryption bit away.
553
+ #[ inline]
554
+ pub fn new_truncate ( addr : u64 ) -> PhysAddr {
555
+ PhysAddr ( ( addr % ( 1 << 52 ) ) & !ENC_BIT_MASK . load ( Ordering :: Relaxed ) )
556
+ }
557
+ }
558
+
559
+ #[ cfg( not( feature = "memory_encryption" ) ) ]
560
+ impl PhysAddr {
561
+ /// Creates a new physical address, throwing bits 52..64 away.
562
+ #[ inline]
563
+ pub const fn new_truncate ( addr : u64 ) -> PhysAddr {
564
+ PhysAddr ( addr % ( 1 << 52 ) )
565
+ }
566
+ }
567
+
549
568
impl fmt:: Debug for PhysAddr {
550
569
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
551
570
f. debug_tuple ( "PhysAddr" )
0 commit comments