6565top-level paging structure as it is written out last.
6666"""
6767
68- import sys
69- import array
7068import argparse
69+ import array
7170import ctypes
7271import os
73- import struct
7472import re
73+ import struct
74+ import sys
7575import textwrap
7676
77- from packaging import version
78-
7977import elftools
8078from elftools .elf .elffile import ELFFile
8179from elftools .elf .sections import SymbolTableSection
80+ from packaging import version
8281
8382if version .parse (elftools .__version__ ) < version .parse ('0.24' ):
8483 sys .exit ("pyelftools is out of date, need version 0.24 or later" )
@@ -134,9 +133,9 @@ def error(text):
134133def align_check (base , size , scope = 4096 ):
135134 """Make sure base and size are page-aligned"""
136135 if (base % scope ) != 0 :
137- error ("unaligned base address %x" % base )
136+ error (f "unaligned base address { base :x } " )
138137 if (size % scope ) != 0 :
139- error ("Unaligned region size 0x%x for base %x" % ( size , base ) )
138+ error (f "Unaligned region size 0x{ size :x } for base { base :x } " )
140139
141140
142141def dump_flags (flags ):
@@ -184,7 +183,7 @@ def round_down(val, align):
184183# access or set caching properties at leaf levels.
185184INT_FLAGS = FLAG_P | FLAG_RW | FLAG_US
186185
187- class MMUTable () :
186+ class MMUTable :
188187 """Represents a particular table in a set of page tables, at any level"""
189188
190189 def __init__ (self ):
@@ -261,9 +260,9 @@ def map(self, virt_addr, phys_addr, entry_flags):
261260 this is the physical address of the next level table"""
262261 index = self .entry_index (virt_addr )
263262
264- verbose ("%s: mapping 0x%x to 0x%x : %s" %
265- ( self . __class__ . __name__ ,
266- phys_addr , virt_addr , dump_flags (entry_flags )) )
263+ verbose (f" { self . __class__ . __name__ :s } : "
264+ f"mapping 0x { phys_addr :x } to 0x { virt_addr :x } : "
265+ f" { dump_flags (entry_flags ):s } " )
267266
268267 self .entries [index ] = ((phys_addr & self .addr_mask ) |
269268 (entry_flags & self .supported_flags ))
@@ -274,9 +273,9 @@ def set_perms(self, virt_addr, entry_flags):
274273 Unsupported flags will be filtered out."""
275274 index = self .entry_index (virt_addr )
276275
277- verbose ("%s: changing perm at 0x%x : %s" %
278- ( self . __class__ . __name__ ,
279- virt_addr , dump_flags (entry_flags )) )
276+ verbose (f" { self . __class__ . __name__ :s } : "
277+ f"changing perm at 0x { virt_addr :x } : "
278+ f" { dump_flags (entry_flags ):s } " )
280279
281280 self .entries [index ] = ((self .entries [index ] & self .addr_mask ) |
282281 (entry_flags & self .supported_flags ))
@@ -336,7 +335,7 @@ class PtXd(Pt):
336335 FLAG_D | FLAG_IGNORED0 | FLAG_IGNORED1 | FLAG_IGNORED2 )
337336
338337
339- class PtableSet () :
338+ class PtableSet :
340339 """Represents a complete set of page tables for any paging mode"""
341340
342341 def __init__ (self , pages_start ):
@@ -345,8 +344,8 @@ def __init__(self, pages_start):
345344 self .toplevel = self .levels [0 ]()
346345 self .page_pos = pages_start
347346
348- debug ("%s starting at physical address 0x%x" %
349- ( self . __class__ . __name__ , self .page_pos ) )
347+ debug (f" { self . __class__ . __name__ :s } "
348+ f"starting at physical address 0x { self .page_pos :x } " )
350349
351350 # Database of page table pages. Maps physical memory address to
352351 # MMUTable objects, excluding the top-level table which is tracked
@@ -408,8 +407,7 @@ def new_child_table(self, table, virt_addr, depth):
408407 """Create a new child table"""
409408 new_table_addr = self .get_new_mmutable_addr ()
410409 new_table = self .levels [depth ]()
411- debug ("new %s at physical addr 0x%x"
412- % (self .levels [depth ].__name__ , new_table_addr ))
410+ debug (f"new { self .levels [depth ].__name__ :s} at physical addr 0x{ new_table_addr :x} " )
413411 self .tables [new_table_addr ] = new_table
414412 table .map (virt_addr , new_table_addr , INT_FLAGS )
415413
@@ -436,17 +434,16 @@ def map_page(self, virt_addr, phys_addr, flags, reserve, level=PT_LEVEL):
436434
437435 def reserve (self , virt_base , size , to_level = PT_LEVEL ):
438436 """Reserve page table space with already aligned virt_base and size"""
439- debug ("Reserving paging structures for 0x%x (0x%x)" %
440- (virt_base , size ))
437+ debug (f"Reserving paging structures for 0x{ virt_base :x} (0x{ size :x} )" )
441438
442439 align_check (virt_base , size )
443440
444441 # How much memory is covered by leaf page table
445442 scope = 1 << self .levels [PD_LEVEL ].addr_shift
446443
447444 if virt_base % scope != 0 :
448- error ("misaligned virtual address space, 0x%x not a multiple of 0x%x" %
449- ( virt_base , scope ) )
445+ error (f "misaligned virtual address space, 0x{ virt_base :x } "
446+ f"not a multiple of 0x { scope :x } " )
450447
451448 for addr in range (virt_base , virt_base + size , scope ):
452449 self .map_page (addr , 0 , 0 , True , to_level )
@@ -473,8 +470,7 @@ def map(self, phys_base, virt_base, size, flags, level=PT_LEVEL):
473470
474471 scope = 1 << self .levels [level ].addr_shift
475472
476- debug ("Mapping 0x%x (0x%x) to 0x%x: %s" %
477- (phys_base , size , virt_base , dump_flags (flags )))
473+ debug (f"Mapping 0x{ phys_base :x} (0x{ size :x} ) to 0x{ virt_base :x} : { dump_flags (flags ):s} " )
478474
479475 align_check (phys_base , size , scope )
480476 align_check (virt_base , size , scope )
@@ -537,8 +533,7 @@ def set_region_perms(self, name, flags, level=PT_LEVEL):
537533 if size == 0 :
538534 return
539535
540- debug ("change flags for %s at 0x%x (0x%x): %s" %
541- (name , base , size , dump_flags (flags )))
536+ debug (f"change flags for { name :s} at 0x{ base :x} (0x{ size :x} ): { dump_flags (flags ):s} " )
542537
543538 num_levels = len (self .levels ) + level + 1
544539 scope = 1 << self .levels [level ].addr_shift
@@ -556,8 +551,7 @@ def set_region_perms(self, name, flags, level=PT_LEVEL):
556551 table = self .tables [table .lookup (addr )]
557552 table .set_perms (addr , flags )
558553 except KeyError :
559- error ("no mapping for %s region 0x%x (size 0x%x)" %
560- (name , base , size ))
554+ error (f"no mapping for { name :s} region 0x{ base :x} (size 0x{ size :x} )" )
561555
562556 def write_output (self , filename ):
563557 """Write the page tables to the output file in binary format"""
@@ -574,9 +568,8 @@ def write_output(self, filename):
574568 # in PAE, the top-level PDPT has only 4 entries and is not a
575569 # full page in size. We do not put it in the tables dictionary
576570 # and treat it as a special case.
577- debug ("top-level %s at physical addr 0x%x" %
578- (self .toplevel .__class__ .__name__ ,
579- self .get_new_mmutable_addr ()))
571+ debug (f"top-level { self .toplevel .__class__ .__name__ :s} at "
572+ f"physical addr 0x{ self .get_new_mmutable_addr ():x} " )
580573 top_level_bin = self .toplevel .get_binary ()
581574 output_fp .write (top_level_bin )
582575 written_size += len (top_level_bin )
@@ -666,7 +659,7 @@ def map_extra_regions(pt):
666659 elements = entry .split (',' )
667660
668661 if len (elements ) < 2 :
669- error ("Not enough arguments for --map %s" % entry )
662+ error (f "Not enough arguments for --map { entry :s } " )
670663
671664 one_map = {}
672665
@@ -681,7 +674,7 @@ def map_extra_regions(pt):
681674
682675 # Check for allowed flags
683676 if not bool (re .match ('^[LUWXD]*$' , map_flags )):
684- error ("Unrecognized flags: %s" % map_flags )
677+ error (f "Unrecognized flags: { map_flags :s } " )
685678
686679 flags = FLAG_P | ENTRY_XD
687680 if 'W' in map_flags :
@@ -716,8 +709,8 @@ def map_extra_regions(pt):
716709 # Check if addresses have already been mapped.
717710 # Error out if so as they could override kernel mappings.
718711 if pt .is_region_mapped (virt , size , level ):
719- error (( "Region 0x%x (%d ) already been mapped "
720- "for --map %s" % ( virt , size , one_map ['cmdline' ])) )
712+ error (f "Region 0x{ virt :x } ( { size :d } ) already been mapped "
713+ f "for --map { one_map ['cmdline' ]:x } " )
721714
722715 # Reserve space in page table, and map the region
723716 pt .reserve_unaligned (virt , size , level )
@@ -746,7 +739,7 @@ def main():
746739 else :
747740 pclass = Ptables32bit
748741
749- debug ("building %s" % pclass .__name__ )
742+ debug (f "building { pclass .__name__ :s } " )
750743
751744 vm_base = syms ["CONFIG_KERNEL_VM_BASE" ]
752745 vm_size = syms ["CONFIG_KERNEL_VM_SIZE" ]
@@ -778,23 +771,22 @@ def main():
778771
779772 ptables_phys = syms ["z_x86_pagetables_start" ] + virt_to_phys_offset
780773
781- debug ("Address space: 0x%x - 0x%x size 0x%x" %
782- (vm_base , vm_base + vm_size - 1 , vm_size ))
774+ debug (f"Address space: 0x{ vm_base :x} - 0x{ vm_base + vm_size - 1 :x} size 0x{ vm_size :x} " )
783775
784- debug ("Zephyr image: 0x%x - 0x%x size 0x%x" %
785- ( image_base , image_base + image_size - 1 , image_size ) )
776+ debug (f "Zephyr image: 0x{ image_base :x } - 0x{ image_base + image_size - 1 :x } "
777+ f"size 0x { image_size :x } " )
786778
787779 if virt_to_phys_offset != 0 :
788- debug ("Physical address space: 0x%x - 0x%x size 0x%x" %
789- ( sram_base , sram_base + sram_size - 1 , sram_size ) )
780+ debug (f "Physical address space: 0x{ sram_base :x } - 0x{ sram_base + sram_size - 1 :x } "
781+ f"size 0x { sram_size :x } " )
790782
791783 is_perm_regions = isdef ("CONFIG_SRAM_REGION_PERMISSIONS" )
792784
793785 # Are pages in non-boot, non-pinned sections present at boot.
794786 is_generic_section_present = isdef ("CONFIG_LINKER_GENERIC_SECTIONS_PRESENT_AT_BOOT" )
795787
796788 if image_size >= vm_size :
797- error ("VM size is too small (have 0x%x need more than 0x%x)" % ( vm_size , image_size ) )
789+ error (f "VM size is too small (have 0x{ vm_size :x } need more than 0x{ image_size :x } )" )
798790
799791 map_flags = 0
800792
@@ -835,8 +827,7 @@ def main():
835827 # from real mode
836828 locore_base = syms ["_locore_start" ]
837829 locore_size = syms ["_lodata_end" ] - locore_base
838- debug ("Base addresses: physical 0x%x size 0x%x" % (locore_base ,
839- locore_size ))
830+ debug (f"Base addresses: physical 0x{ locore_base :x} size 0x{ locore_size :x} " )
840831 pt .map (locore_base , None , locore_size , map_flags | FLAG_P | ENTRY_RW )
841832
842833 if isdef ("CONFIG_XIP" ):
@@ -917,7 +908,7 @@ def main():
917908 pt .set_region_perms ("__x86shadowstack" , FLAG_P | FLAG_D | ENTRY_XD )
918909
919910 written_size = pt .write_output (args .output )
920- debug ("Written %d bytes to %s" % ( written_size , args .output ) )
911+ debug (f "Written { written_size :d } bytes to { args .output :s } " )
921912
922913 # Warn if reserved page table is not of correct size
923914 if reserved_pt_size and written_size != reserved_pt_size :
@@ -935,9 +926,8 @@ def main():
935926
936927 reason = "big" if reserved_pt_size > written_size else "small"
937928
938- error (("Reserved space for page table is too %s."
939- " Set CONFIG_X86_EXTRA_PAGE_TABLE_PAGES=%d" ) %
940- (reason , extra_pages_needed ))
929+ error (f"Reserved space for page table is too { reason :s} ."
930+ f" Set CONFIG_X86_EXTRA_PAGE_TABLE_PAGES={ extra_pages_needed :d} " )
941931
942932
943933if __name__ == "__main__" :
0 commit comments