Where can I find the address of the Linux global m

2019-06-11 16:27发布

My distro (Ubuntu 14.04.3 LTS) doesn't seem to export this reference so I can't resolve the address at module load time. I'm looking for another way to determine the address without a kernel re-compile.

I'm running as a guest under VMware Fusion on a macbook pro. Kernel is 3.13.0-74-generic.

Thanks in advance

标签: linux kernel
2条回答
叛逆
2楼-- · 2019-06-11 17:10

Use kallsyms_lookup_name. It is defined in linux/kallsyms.h as

unsigned long kallsyms_lookup_name(const char *name);

Usage is trivial:

struct page *my_mem_map = (struct page*)kallsyms_lookup_name("mem_map");

kallsyms_lookup_name is exported for modules since kernel 2.6.33.

For earlier kernels, or for find several symbols at once, generic function kallsyms_on_each_symbol can be used. It iterates over all symbols and calls user-specified function for them.

查看更多
爷、活的狠高调
3楼-- · 2019-06-11 17:15

Maybe this answer is too late for you @owenh . I am answering it for people who are looking for the same question about mem_map array. Because so far I can not find any clear answer about why mem_map can not be found. After tracing into how pte_page works, I record whatever I learn here.

If you can not find mem_map array, it is possible because the kernel is using virtually contiguous mem_map or the sparse memory model is used, both of which do not have a physically contiguous mem_map array. Instead, it may have a vmemmap or mem_section as a starting point to find all the page struct. This is decided by these macros (CONFIG_FLATMEM/DISCONTIGMEM/CONFIG_SPARSEMEM_VMEMMAP/CONFIG_SPARSEMEM) in include/asm-generic/memory_model.h. You can check your kernel compilation config flags using something like

sudo cat /boot/config-`uname -r` |grep CONFIG_SPARSEMEM_VMEMMAP

In CONFIG_SPARSEMEM_VMEMMAP mode, the page struct array starts from vmemmap, which is at a fixed location 0xffffea0000000000.

#define VMEMMAP_START    _AC(0xffffea0000000000, UL)
#define vmemmap ((struct page *)VMEMMAP_START)

While, in CONFIG_SPARSEMEM mode, the page struct array is managed through an 2-dimensional array called mem_section.

In order to know how kernel uses it, we can learn from how kernel get a page struct of a page given a pte of a page. This macro is called pte_page(in arch/x86/include/asm/pgtable.h). It transform the pte into it pfn. Using that pfn, it can locate the the page struct either using vmemmap or mem_section. This is in include/asm-generic/memory_model.h.

查看更多
登录 后发表回答