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
9import gdb
10import re
11from linux import lists, utils, stackdepot, constants, mm
12
13vmap_area_type = utils.CachedType('struct vmap_area')
14vmap_area_ptr_type = vmap_area_type.get_type().pointer()
15
16def is_vmalloc_addr(x):
17    pg_ops = mm.page_ops().ops
18    addr = pg_ops.kasan_reset_tag(x)
19    return addr >= pg_ops.VMALLOC_START and addr < pg_ops.VMALLOC_END
20
21class LxVmallocInfo(gdb.Command):
22    """Show vmallocinfo"""
23
24    def __init__(self):
25        super(LxVmallocInfo, self).__init__("lx-vmallocinfo", gdb.COMMAND_DATA)
26
27    def invoke(self, arg, from_tty):
28        vmap_area_list = gdb.parse_and_eval('vmap_area_list')
29        for vmap_area in lists.list_for_each_entry(vmap_area_list, vmap_area_ptr_type, "list"):
30            if not vmap_area['vm']:
31                gdb.write("0x%x-0x%x %10d vm_map_ram\n" % (vmap_area['va_start'], vmap_area['va_end'],
32                    vmap_area['va_end'] - vmap_area['va_start']))
33                continue
34            v = vmap_area['vm']
35            gdb.write("0x%x-0x%x %10d" % (v['addr'], v['addr'] + v['size'], v['size']))
36            if v['caller']:
37                gdb.write(" %s" % str(v['caller']).split(' ')[-1])
38            if v['nr_pages']:
39                gdb.write(" pages=%d" % v['nr_pages'])
40            if v['phys_addr']:
41                gdb.write(" phys=0x%x" % v['phys_addr'])
42            if v['flags'] & constants.LX_VM_IOREMAP:
43                gdb.write(" ioremap")
44            if v['flags'] & constants.LX_VM_ALLOC:
45                gdb.write(" vmalloc")
46            if v['flags'] & constants.LX_VM_MAP:
47                gdb.write(" vmap")
48            if v['flags'] & constants.LX_VM_USERMAP:
49                gdb.write(" user")
50            if v['flags'] & constants.LX_VM_DMA_COHERENT:
51                gdb.write(" dma-coherent")
52            if is_vmalloc_addr(v['pages']):
53                gdb.write(" vpages")
54            gdb.write("\n")
55
56LxVmallocInfo()
57