打印内核的页表条目 [英] Print kernel's page table entries

查看:144
本文介绍了打印内核的页表条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有4个级别页表的虚拟内存映射:

Virtual memory map with 4 level page tables:

0000000000000000 - 00007fffffffffff (=47 bits) user space, different per mm
ffff800000000000 - ffff80ffffffffff (=40 bits) guard hole
ffff880000000000 - ffffc7ffffffffff (=64 TB) direct mapping of all phys. memory
ffffc80000000000 - ffffc8ffffffffff (=40 bits) hole
ffffc90000000000 - ffffe8ffffffffff (=45 bits) vmalloc/ioremap space
ffffe90000000000 - ffffe9ffffffffff (=40 bits) hole
ffffea0000000000 - ffffeaffffffffff (=40 bits) virtual memory map (1TB)
ffffffff80000000 - ffffffffa0000000 (=512 MB)  kernel text mapping, from phys 0
ffffffffa0000000 - fffffffffff00000 (=1536 MB) module mapping space

我知道内核会尝试将物理地址直接映射到从PAGE_OFFSET开始的用于直接映射区域的虚拟地址

I know that the kernel tries to map directly physical addresses to virtual addresses starting from PAGE_OFFSET for the direct mapping region

ffff880000000000 - ffffc7ffffffffff (=64 TB) direct mapping of all phys. memory

但是我不知道内核中的代码将页表保留在哪里来管理此直接映射区域,以及如何在此直接映射区域中打印出4级页表的所有页表项.你知道如何打印它们吗?

but I do not know where the code in kernel keeps the page tables to manage this direct mapping region and how to print out all the page table entries of 4 level page tables in this direct mapping region. Do you know how to print them?

推荐答案

火影忍者:

我也对打印Linux的页表感兴趣,我偶然发现了内核源代码中的漂亮工具

I was interested in printing Linux's page tables as well and I stumbled upon the nifty utility in the kernel source

arch/x86/mm/dump_pagetables.c

如果您构建具有CONFIG_X86_PTDUMP设置的内核,则可以完成

cat /sys/kernel/debug/kernel_page_tables

它将遍历内核的页表层次结构并将其打印出来.但是,输出有2个问题:

And it will walk the kernel's page table hierarchy and print it out. There are 2 issues with the output however:

  1. 它仅列出虚拟地址,而不列出其关联的物理地址.这让我很失望,因为我最感兴趣的是虚拟映射到非RAM系统地址(例如PCI空间等),在其他作品中,我想用/sys/直观地匹配在/proc/iomem中看到的内容内核/调试/kernel_page_tables.
  2. 它没有提供打印每个进程页表的方法.据我了解,每个linux进程和init_level_4_pgt(内核的原始(启动后)页面表)共享其虚拟地址空间的很大一部分:确切地说,从PAGE_OFFSET开始.我猜想是这样做的,所以在用户进程(通过系统调用)的上下文中运行的内核代码可以访问内核内存.无论如何,我认为将所有这些都付诸实践将是一件很整洁的事情.
  1. It only lists the virtual addresses and not their associated physical address. This disappointed me because I was mostly interested in the virtual mappings into non-RAM system adresses (like PCI-space, etc) -- in other works I wanted to visually match up what I was seeing in /proc/iomem with /sys/kernel/debug/kernel_page_tables.
  2. It doesn't provide a way to print out the per-process page tables. As I understand, each linux process and init_level_4_pgt (the kernel's original (post-bootup) page table) share a large portion of their virtual address spaces: from PAGE_OFFSET on wards to be exact. I guess this is done so kernel code running in the context of a user process (via a system call) can access kernel memory. At any rate I, thought it would be pretty neat to see all this in action.

这是链接,它与我已打补丁的3.13.11内核相关,已修复了这两个问题(已修复的分支为page_table_print).

Here is the link to my patched 3.13.11 kernel with these 2 issues fixed (the branch with the fix is page_table_print).

要使用,只需做

echo <pid_of_interest> > /proc/sys/debug/pgt_dump_process_id

其中pid_of_interest,如果要监视其页表的进程的pid.注意:这样做

where pid_of_interest if the pid of the process whose page table's you wish to monitor. NOTE: by doing

echo -1 > /proc/sys/debug/pgt_dump_process_id

这将转储内核的页表.这是内核页表的示例输出:

this will dump the kernel's page tables. Here is an example output for the kernel's page tables:

cat /sys/kernel/debug/kernel_page_tables  | more
CR3= 0x196b70000, va_CR3 = 0xffff880196b70000
Page tables for kernelPGT localtion in memory: phs = 0x1c0e000, virt  = 0xffffffff81c0e000
---[ User Space ]---
0x0000000000000000-0xffff800000000000, phy: 0x0000000000000000-0x0000800001c0e000 16777088T                           pgd
---[ Kernel Space ]---
0xffff800000000000-0xffff880000000000, phy: 0x0000800001c0e000-0x0000000001fe1000        8T                           pgd
---[ Low Kernel Mapping ]---
0xffff880000000000-0xffff880000096000, phy: 0x0000000001fe1000-0x0000000002077000      600K     RW             GLB NX pte
0xffff880000096000-0xffff880000097000, phy: 0x0000000002077000-0x0000000002078000        4K     ro             GLB NX pte
0xffff880000097000-0xffff880000098000, phy: 0x0000000002078000-0x0000000002079000        4K     ro             GLB x  pte
0xffff880000098000-0xffff880000200000, phy: 0x0000000002079000-0x00000000021e0000     1440K     RW             GLB NX pte
0xffff880000200000-0xffff880001000000, phy: 0x00000000021e0000-0x0000000002fe0000       14M     RW         PSE GLB NX pmd
0xffff880001000000-0xffff880001600000, phy: 0x0000000002fe0000-0x00000001b14a1000        6M     ro         PSE GLB NX pmd
0xffff880001600000-0xffff880001734000, phy: 0x00000001b14a1000-0x00000001b15d5000     1232K     ro             GLB NX pte
0xffff880001734000-0xffff880001800000, phy: 0x00000001b15d5000-0x00000000037e0000      816K     RW             GLB NX pte
0xffff880001800000-0xffff880001a00000, phy: 0x00000000037e0000-0x00000001b14a2000        2M     ro         PSE GLB NX pmd

这是完成后的输出

echo 1 > /proc/sys/debug/pgt_dump_process_id


cat /sys/kernel/debug/kernel_page_tables  | more
CR3= 0x17312f000, va_CR3 = 0xffff88017312f000
Page tables for process id = 1
PGT localtion in memory: phs = 0x3623f000, virt  = 0xffff88003623f000
---[ User Space ]---
0x0000000000000000-0x00007f8000000000, phy: 0x0000000000000000-0x0000000036277000   130560G                           pgd
0x00007f8000000000-0x00007ff200000000, phy: 0x0000000036277000-0x000000003627a000      456G                           pud
0x00007ff200000000-0x00007ff207800000, phy: 0x000000003627a000-0x0000000036097000      120M                           pmd
0x00007ff207800000-0x00007ff2079b5000, phy: 0x0000000036097000-0x000000003624c000     1748K                           pte
0x00007ff2079b5000-0x00007ff2079b8000, phy: 0x000000003624c000-0x000000003624f000       12K USR ro                 x  pte
0x00007ff2079b8000-0x00007ff207bbf000, phy: 0x000000003624f000-0x00000000363c3000     2076K                           pte
0x00007ff207bbf000-0x00007ff207bc1000, phy: 0x00000000363c3000-0x00000000363c5000        8K USR ro                 NX pte
0x00007ff207bc1000-0x00007ff207bc4000, phy: 0x00000000363c5000-0x00000000363c8000       12K USR ro                 x  pte
0x00007ff207bc4000-0x00007ff207bc6000, phy: 0x00000000363c8000-0x00000000363ca000        8K                           pte
0x00007ff207bc6000-0x00007ff207bc7000, phy: 0x00000000363ca000-0x00000000363cb000        4K USR ro                 x  pte
0x00007ff207bc7000-0x00007ff207dcb000, phy: 0x00000000363cb000-0x00000000363d0000     2064K                           pte
0x00007ff207dcb000-0x00007ff207dcd000, phy: 0x00000000363d0000-0x00000000363d2000        8K USR ro                 NX pte
0x00007ff207dcd000-0x00007ff207dd2000, phy: 0x00000000363d2000-0x00000000363d7000       20K USR ro                 x  pte
0x00007ff207dd2000-0x00007ff207fe3000, phy: 0x00000000363d7000-0x00000000363fe000     2116K                           pte
0x00007ff207fe3000-0x00007ff207fe5000, phy: 0x00000000363fe000-0x0000000036400000        8K USR ro                 NX pte
0x00007ff207fe5000-0x00007ff207fe7000, phy: 0x0000000036400000-0x0000000036402000        8K                           pte
0x00007ff207fe7000-0x00007ff207fec000, phy: 0x0000000036402000-0x0000000036407000       20K USR ro                 x  pte
0x00007ff207fec000-0x00007ff207fed000, phy: 0x0000000036407000-0x0000000036408000        4K                           pte
0x00007ff207fed000-0x00007ff207fef000, phy: 0x0000000036408000-0x000000003640a000        8K USR ro                 x  pte
0x00007ff207fef000-0x00007ff2081ef000, phy: 0x000000003640a000-0x0000000036214000        2M                           pte
0x00007ff2081ef000-0x00007ff2081f1000, phy: 0x0000000036214000-0x0000000036216000        8K USR ro                 NX pte

我认为这很整洁,希望其他人也这么认为. :)

I think this is pretty neat, I hope others think the same. :)

这篇关于打印内核的页表条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆