@@ -24,9 +24,7 @@ use bitflags::bitflags;
2424use core:: cmp;
2525use core:: ops:: { Index , IndexMut } ;
2626use core:: ptr:: NonNull ;
27-
28- extern crate alloc;
29- use alloc:: boxed:: Box ;
27+ use zerocopy:: FromZeros ;
3028
3129/// Number of entries in a page table (4KB/8B).
3230const ENTRY_COUNT : usize = 512 ;
@@ -211,7 +209,7 @@ impl PagingMode {
211209
212210/// Represents a page table entry.
213211#[ repr( C ) ]
214- #[ derive( Copy , Clone , Debug , Default ) ]
212+ #[ derive( Copy , Clone , Debug , FromZeros ) ]
215213pub struct PTEntry ( PhysAddr ) ;
216214
217215impl PTEntry {
@@ -391,7 +389,7 @@ impl PTEntry {
391389
392390/// A pagetable page with multiple entries.
393391#[ repr( C ) ]
394- #[ derive( Debug ) ]
392+ #[ derive( Debug , FromZeros ) ]
395393pub struct PTPage {
396394 entries : [ PTEntry ; ENTRY_COUNT ] ,
397395}
@@ -404,7 +402,7 @@ impl PTPage {
404402 ///
405403 /// Returns [`SvsmError`] if the page cannot be allocated.
406404 fn alloc ( ) -> Result < ( & ' static mut Self , PhysAddr ) , SvsmError > {
407- let page = PageBox :: try_new ( PTPage :: default ( ) ) ?;
405+ let page: PageBox < Self > = PageBox :: try_new_zeroed ( ) ?;
408406 let paddr = virt_to_phys ( page. vaddr ( ) ) ;
409407 Ok ( ( PageBox :: leak ( page) , paddr) )
410408 }
@@ -438,13 +436,6 @@ impl PTPage {
438436 }
439437}
440438
441- impl Default for PTPage {
442- fn default ( ) -> Self {
443- let entries = [ PTEntry :: default ( ) ; ENTRY_COUNT ] ;
444- PTPage { entries }
445- }
446- }
447-
448439/// Can be used to access page table entries by index.
449440impl Index < usize > for PTPage {
450441 type Output = PTEntry ;
@@ -514,7 +505,7 @@ impl PageFrame {
514505
515506/// Page table structure containing a root page with multiple entries.
516507#[ repr( C ) ]
517- #[ derive( Default , Debug ) ]
508+ #[ derive( Debug , FromZeros ) ]
518509pub struct PageTable {
519510 root : PTPage ,
520511}
@@ -544,7 +535,7 @@ impl PageTable {
544535 /// # Errors
545536 /// Returns [`SvsmError`] if the page cannot be allocated.
546537 pub fn allocate_new ( ) -> Result < PageBox < Self > , SvsmError > {
547- let mut pgtable = PageBox :: try_new ( PageTable :: default ( ) ) ?;
538+ let mut pgtable: PageBox < Self > = PageBox :: try_new_zeroed ( ) ?;
548539 let paddr = virt_to_phys ( pgtable. vaddr ( ) ) ;
549540
550541 // Set the self-map entry.
@@ -1273,7 +1264,7 @@ impl PageTable {
12731264}
12741265
12751266/// Represents a sub-tree of a page-table which can be mapped at a top-level index
1276- #[ derive( Default , Debug ) ]
1267+ #[ derive( Debug , FromZeros ) ]
12771268struct RawPageTablePart {
12781269 page : PTPage ,
12791270}
@@ -1514,7 +1505,7 @@ impl Drop for RawPageTablePart {
15141505#[ derive( Debug ) ]
15151506pub struct PageTablePart {
15161507 /// The root of the page-table sub-tree
1517- raw : Option < Box < RawPageTablePart > > ,
1508+ raw : Option < PageBox < RawPageTablePart > > ,
15181509 /// The top-level index this PageTablePart is populated at
15191510 idx : usize ,
15201511}
@@ -1541,7 +1532,9 @@ impl PageTablePart {
15411532 }
15421533
15431534 fn get_or_init_mut ( & mut self ) -> & mut RawPageTablePart {
1544- self . raw . get_or_insert_with ( Box :: default)
1535+ self . raw . get_or_insert_with ( || {
1536+ PageBox :: try_new_zeroed ( ) . expect ( "Failed to allocate page table page" )
1537+ } )
15451538 }
15461539
15471540 fn get_mut ( & mut self ) -> Option < & mut RawPageTablePart > {
0 commit comments