~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/scripts/gdb/linux/mm.py

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /scripts/gdb/linux/mm.py (Version linux-6.12-rc7) and /scripts/gdb/linux/mm.py (Version ccs-tools-1.8.12)


  1 # SPDX-License-Identifier: GPL-2.0                
  2 #                                                 
  3 # Copyright (c) 2023 MediaTek Inc.                
  4 #                                                 
  5 # Authors:                                        
  6 #  Kuan-Ying Lee <Kuan-Ying.Lee@mediatek.com>      
  7 #                                                 
  8                                                   
  9 import gdb                                        
 10 import math                                       
 11 from linux import utils, constants                
 12                                                   
 13 def DIV_ROUND_UP(n,d):                            
 14     return ((n) + (d) - 1) // (d)                 
 15                                                   
 16 def test_bit(nr, addr):                           
 17     if addr.dereference() & (0x1 << nr):          
 18         return True                               
 19     else:                                         
 20         return False                              
 21                                                   
 22 class page_ops():                                 
 23     ops = None                                    
 24     def __init__(self):                           
 25         if not constants.LX_CONFIG_SPARSEMEM_V    
 26             raise gdb.GdbError('Only support C    
 27         if constants.LX_CONFIG_ARM64 and utils    
 28             self.ops = aarch64_page_ops()         
 29         else:                                     
 30             raise gdb.GdbError('Only support a    
 31                                                   
 32 class aarch64_page_ops():                         
 33     def __init__(self):                           
 34         self.SUBSECTION_SHIFT = 21                
 35         self.SEBSECTION_SIZE = 1 << self.SUBSE    
 36         self.MODULES_VSIZE = 2 * 1024 * 1024 *    
 37                                                   
 38         if constants.LX_CONFIG_ARM64_64K_PAGES    
 39             self.SECTION_SIZE_BITS = 29           
 40         else:                                     
 41             self.SECTION_SIZE_BITS = 27           
 42         self.MAX_PHYSMEM_BITS = constants.LX_C    
 43                                                   
 44         self.PAGE_SHIFT = constants.LX_CONFIG_    
 45         self.PAGE_SIZE = 1 << self.PAGE_SHIFT     
 46         self.PAGE_MASK = (~(self.PAGE_SIZE - 1    
 47                                                   
 48         self.VA_BITS = constants.LX_CONFIG_ARM    
 49         if self.VA_BITS > 48:                     
 50             if constants.LX_CONFIG_ARM64_16K_P    
 51                 self.VA_BITS_MIN = 47             
 52             else:                                 
 53                 self.VA_BITS_MIN = 48             
 54             tcr_el1 = gdb.execute("info regist    
 55             tcr_el1 = int(tcr_el1.split()[1],     
 56             self.vabits_actual = 64 - ((tcr_el    
 57         else:                                     
 58             self.VA_BITS_MIN = self.VA_BITS       
 59             self.vabits_actual = self.VA_BITS     
 60         self.kimage_voffset = gdb.parse_and_ev    
 61                                                   
 62         self.SECTIONS_SHIFT = self.MAX_PHYSMEM    
 63                                                   
 64         if str(constants.LX_CONFIG_ARCH_FORCE_    
 65             self.MAX_ORDER = constants.LX_CONF    
 66         else:                                     
 67             self.MAX_ORDER = 10                   
 68                                                   
 69         self.MAX_ORDER_NR_PAGES = 1 << (self.M    
 70         self.PFN_SECTION_SHIFT = self.SECTION_    
 71         self.NR_MEM_SECTIONS = 1 << self.SECTI    
 72         self.PAGES_PER_SECTION = 1 << self.PFN    
 73         self.PAGE_SECTION_MASK = (~(self.PAGES    
 74                                                   
 75         if constants.LX_CONFIG_SPARSEMEM_EXTRE    
 76             self.SECTIONS_PER_ROOT = self.PAGE    
 77         else:                                     
 78             self.SECTIONS_PER_ROOT = 1            
 79                                                   
 80         self.NR_SECTION_ROOTS = DIV_ROUND_UP(s    
 81         self.SECTION_ROOT_MASK = self.SECTIONS    
 82         self.SUBSECTION_SHIFT = 21                
 83         self.SEBSECTION_SIZE = 1 << self.SUBSE    
 84         self.PFN_SUBSECTION_SHIFT = self.SUBSE    
 85         self.PAGES_PER_SUBSECTION = 1 << self.    
 86                                                   
 87         self.SECTION_HAS_MEM_MAP = 1 << int(gd    
 88         self.SECTION_IS_EARLY = 1 << int(gdb.p    
 89                                                   
 90         self.struct_page_size = utils.get_page    
 91         self.STRUCT_PAGE_MAX_SHIFT = (int)(mat    
 92                                                   
 93         self.PAGE_OFFSET = self._PAGE_OFFSET(s    
 94         self.MODULES_VADDR = self._PAGE_END(se    
 95         self.MODULES_END = self.MODULES_VADDR     
 96                                                   
 97         self.VMEMMAP_RANGE = self._PAGE_END(se    
 98         self.VMEMMAP_SIZE = (self.VMEMMAP_RANG    
 99         self.VMEMMAP_END = (-(1 * 1024 * 1024     
100         self.VMEMMAP_START = self.VMEMMAP_END     
101                                                   
102         self.VMALLOC_START = self.MODULES_END     
103         self.VMALLOC_END = self.VMEMMAP_START     
104                                                   
105         self.memstart_addr = gdb.parse_and_eva    
106         self.PHYS_OFFSET = self.memstart_addr     
107         self.vmemmap = gdb.Value(self.VMEMMAP_    
108                                                   
109         self.KERNEL_START = gdb.parse_and_eval    
110         self.KERNEL_END = gdb.parse_and_eval("    
111                                                   
112         if constants.LX_CONFIG_KASAN_GENERIC o    
113             if constants.LX_CONFIG_KASAN_GENER    
114                 self.KASAN_SHADOW_SCALE_SHIFT     
115             else:                                 
116                 self.KASAN_SHADOW_SCALE_SHIFT     
117             self.KASAN_SHADOW_OFFSET = constan    
118             self.KASAN_SHADOW_END = (1 << (64     
119             self.PAGE_END = self.KASAN_SHADOW_    
120         else:                                     
121             self.PAGE_END = self._PAGE_END(sel    
122                                                   
123         if constants.LX_CONFIG_NUMA and consta    
124             self.NODE_SHIFT = constants.LX_CON    
125         else:                                     
126             self.NODE_SHIFT = 0                   
127                                                   
128         self.MAX_NUMNODES = 1 << self.NODE_SHI    
129                                                   
130     def SECTION_NR_TO_ROOT(self, sec):            
131         return sec // self.SECTIONS_PER_ROOT      
132                                                   
133     def __nr_to_section(self, nr):                
134         root = self.SECTION_NR_TO_ROOT(nr)        
135         mem_section = gdb.parse_and_eval("mem_    
136         return mem_section[root][nr & self.SEC    
137                                                   
138     def pfn_to_section_nr(self, pfn):             
139         return pfn >> self.PFN_SECTION_SHIFT      
140                                                   
141     def section_nr_to_pfn(self, sec):             
142         return sec << self.PFN_SECTION_SHIFT      
143                                                   
144     def __pfn_to_section(self, pfn):              
145         return self.__nr_to_section(self.pfn_t    
146                                                   
147     def pfn_to_section(self, pfn):                
148         return self.__pfn_to_section(pfn)         
149                                                   
150     def subsection_map_index(self, pfn):          
151         return (pfn & ~(self.PAGE_SECTION_MASK    
152                                                   
153     def pfn_section_valid(self, ms, pfn):         
154         if constants.LX_CONFIG_SPARSEMEM_VMEMM    
155             idx = self.subsection_map_index(pf    
156             return test_bit(idx, ms['usage']['    
157         else:                                     
158             return True                           
159                                                   
160     def valid_section(self, mem_section):         
161         if mem_section != None and (mem_sectio    
162             return True                           
163         return False                              
164                                                   
165     def early_section(self, mem_section):         
166         if mem_section != None and (mem_sectio    
167             return True                           
168         return False                              
169                                                   
170     def pfn_valid(self, pfn):                     
171         ms = None                                 
172         if self.PHYS_PFN(self.PFN_PHYS(pfn)) !    
173             return False                          
174         if self.pfn_to_section_nr(pfn) >= self    
175             return False                          
176         ms = self.__pfn_to_section(pfn)           
177                                                   
178         if not self.valid_section(ms):            
179             return False                          
180         return self.early_section(ms) or self.    
181                                                   
182     def _PAGE_OFFSET(self, va):                   
183         return (-(1 << (va))) & 0xffffffffffff    
184                                                   
185     def _PAGE_END(self, va):                      
186         return (-(1 << (va - 1))) & 0xffffffff    
187                                                   
188     def kasan_reset_tag(self, addr):              
189         if constants.LX_CONFIG_KASAN_SW_TAGS o    
190             return int(addr) | (0xff << 56)       
191         else:                                     
192             return addr                           
193                                                   
194     def __is_lm_address(self, addr):              
195         if (addr - self.PAGE_OFFSET) < (self.P    
196             return True                           
197         else:                                     
198             return False                          
199     def __lm_to_phys(self, addr):                 
200         return addr - self.PAGE_OFFSET + self.    
201                                                   
202     def __kimg_to_phys(self, addr):               
203         return addr - self.kimage_voffset         
204                                                   
205     def __virt_to_phys_nodebug(self, va):         
206         untagged_va = self.kasan_reset_tag(va)    
207         if self.__is_lm_address(untagged_va):     
208             return self.__lm_to_phys(untagged_    
209         else:                                     
210             return self.__kimg_to_phys(untagge    
211                                                   
212     def __virt_to_phys(self, va):                 
213         if constants.LX_CONFIG_DEBUG_VIRTUAL:     
214             if not self.__is_lm_address(self.k    
215                 raise gdb.GdbError("Warning: v    
216         return self.__virt_to_phys_nodebug(va)    
217                                                   
218     def virt_to_phys(self, va):                   
219         return self.__virt_to_phys(va)            
220                                                   
221     def PFN_PHYS(self, pfn):                      
222         return pfn << self.PAGE_SHIFT             
223                                                   
224     def PHYS_PFN(self, phys):                     
225         return phys >> self.PAGE_SHIFT            
226                                                   
227     def __phys_to_virt(self, pa):                 
228         return (pa - self.PHYS_OFFSET) | self.    
229                                                   
230     def __phys_to_pfn(self, pa):                  
231         return self.PHYS_PFN(pa)                  
232                                                   
233     def __pfn_to_phys(self, pfn):                 
234         return self.PFN_PHYS(pfn)                 
235                                                   
236     def __pa_symbol_nodebug(self, x):             
237         return self.__kimg_to_phys(x)             
238                                                   
239     def __phys_addr_symbol(self, x):              
240         if constants.LX_CONFIG_DEBUG_VIRTUAL:     
241             if x < self.KERNEL_START or x > se    
242                 raise gdb.GdbError("0x%x excee    
243         return self.__pa_symbol_nodebug(x)        
244                                                   
245     def __pa_symbol(self, x):                     
246         return self.__phys_addr_symbol(x)         
247                                                   
248     def __va(self, pa):                           
249         return self.__phys_to_virt(pa)            
250                                                   
251     def pfn_to_kaddr(self, pfn):                  
252         return self.__va(pfn << self.PAGE_SHIF    
253                                                   
254     def virt_to_pfn(self, va):                    
255         return self.__phys_to_pfn(self.__virt_    
256                                                   
257     def sym_to_pfn(self, x):                      
258         return self.__phys_to_pfn(self.__pa_sy    
259                                                   
260     def page_to_pfn(self, page):                  
261         return int(page.cast(utils.get_page_ty    
262                                                   
263     def page_to_phys(self, page):                 
264         return self.__pfn_to_phys(self.page_to    
265                                                   
266     def pfn_to_page(self, pfn):                   
267         return (self.vmemmap + pfn).cast(utils    
268                                                   
269     def page_to_virt(self, page):                 
270         if constants.LX_CONFIG_DEBUG_VIRTUAL:     
271             return self.__va(self.page_to_phys    
272         else:                                     
273             __idx = int((page.cast(gdb.lookup_    
274             return self.PAGE_OFFSET + (__idx *    
275                                                   
276     def virt_to_page(self, va):                   
277         if constants.LX_CONFIG_DEBUG_VIRTUAL:     
278             return self.pfn_to_page(self.virt_    
279         else:                                     
280             __idx = int(self.kasan_reset_tag(v    
281             addr = self.VMEMMAP_START + (__idx    
282             return gdb.Value(addr).cast(utils.    
283                                                   
284     def page_address(self, page):                 
285         return self.page_to_virt(page)            
286                                                   
287     def folio_address(self, folio):               
288         return self.page_address(folio['page']    
289                                                   
290 class LxPFN2Page(gdb.Command):                    
291     """PFN to struct page"""                      
292                                                   
293     def __init__(self):                           
294         super(LxPFN2Page, self).__init__("lx-p    
295                                                   
296     def invoke(self, arg, from_tty):              
297         argv = gdb.string_to_argv(arg)            
298         pfn = int(argv[0])                        
299         page = page_ops().ops.pfn_to_page(pfn)    
300         gdb.write("pfn_to_page(0x%x) = 0x%x\n"    
301                                                   
302 LxPFN2Page()                                      
303                                                   
304 class LxPage2PFN(gdb.Command):                    
305     """struct page to PFN"""                      
306                                                   
307     def __init__(self):                           
308         super(LxPage2PFN, self).__init__("lx-p    
309                                                   
310     def invoke(self, arg, from_tty):              
311         argv = gdb.string_to_argv(arg)            
312         struct_page_addr = int(argv[0], 16)       
313         page = gdb.Value(struct_page_addr).cas    
314         pfn = page_ops().ops.page_to_pfn(page)    
315         gdb.write("page_to_pfn(0x%x) = 0x%x\n"    
316                                                   
317 LxPage2PFN()                                      
318                                                   
319 class LxPageAddress(gdb.Command):                 
320     """struct page to linear mapping address""    
321                                                   
322     def __init__(self):                           
323         super(LxPageAddress, self).__init__("l    
324                                                   
325     def invoke(self, arg, from_tty):              
326         argv = gdb.string_to_argv(arg)            
327         struct_page_addr = int(argv[0], 16)       
328         page = gdb.Value(struct_page_addr).cas    
329         addr = page_ops().ops.page_address(pag    
330         gdb.write("page_address(0x%x) = 0x%x\n    
331                                                   
332 LxPageAddress()                                   
333                                                   
334 class LxPage2Phys(gdb.Command):                   
335     """struct page to physical address"""         
336                                                   
337     def __init__(self):                           
338         super(LxPage2Phys, self).__init__("lx-    
339                                                   
340     def invoke(self, arg, from_tty):              
341         argv = gdb.string_to_argv(arg)            
342         struct_page_addr = int(argv[0], 16)       
343         page = gdb.Value(struct_page_addr).cas    
344         phys_addr = page_ops().ops.page_to_phy    
345         gdb.write("page_to_phys(0x%x) = 0x%x\n    
346                                                   
347 LxPage2Phys()                                     
348                                                   
349 class LxVirt2Phys(gdb.Command):                   
350     """virtual address to physical address"""     
351                                                   
352     def __init__(self):                           
353         super(LxVirt2Phys, self).__init__("lx-    
354                                                   
355     def invoke(self, arg, from_tty):              
356         argv = gdb.string_to_argv(arg)            
357         linear_addr = int(argv[0], 16)            
358         phys_addr = page_ops().ops.virt_to_phy    
359         gdb.write("virt_to_phys(0x%x) = 0x%x\n    
360                                                   
361 LxVirt2Phys()                                     
362                                                   
363 class LxVirt2Page(gdb.Command):                   
364     """virtual address to struct page"""          
365                                                   
366     def __init__(self):                           
367         super(LxVirt2Page, self).__init__("lx-    
368                                                   
369     def invoke(self, arg, from_tty):              
370         argv = gdb.string_to_argv(arg)            
371         linear_addr = int(argv[0], 16)            
372         page = page_ops().ops.virt_to_page(lin    
373         gdb.write("virt_to_page(0x%x) = 0x%x\n    
374                                                   
375 LxVirt2Page()                                     
376                                                   
377 class LxSym2PFN(gdb.Command):                     
378     """symbol address to PFN"""                   
379                                                   
380     def __init__(self):                           
381         super(LxSym2PFN, self).__init__("lx-sy    
382                                                   
383     def invoke(self, arg, from_tty):              
384         argv = gdb.string_to_argv(arg)            
385         sym_addr = int(argv[0], 16)               
386         pfn = page_ops().ops.sym_to_pfn(sym_ad    
387         gdb.write("sym_to_pfn(0x%x) = %d\n" %     
388                                                   
389 LxSym2PFN()                                       
390                                                   
391 class LxPFN2Kaddr(gdb.Command):                   
392     """PFN to kernel address"""                   
393                                                   
394     def __init__(self):                           
395         super(LxPFN2Kaddr, self).__init__("lx-    
396                                                   
397     def invoke(self, arg, from_tty):              
398         argv = gdb.string_to_argv(arg)            
399         pfn = int(argv[0])                        
400         kaddr = page_ops().ops.pfn_to_kaddr(pf    
401         gdb.write("pfn_to_kaddr(%d) = 0x%x\n"     
402                                                   
403 LxPFN2Kaddr()                                     
                                                      

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php