首页 > 其他分享 >kernel page address

kernel page address

时间:2022-09-26 01:11:24浏览次数:60  
标签:kernel pgd virt lx address printk page 0x%

代码:

#include <linux/module.h>
#include <linux/init.h>
#include <linux/moduleparam.h>
#include <linux/gfp.h>
#include <linux/mm.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
#include <linux/mm_types.h>
#include <linux/pgtable.h>
#include <linux/moduleparam.h>
#include <linux/types.h>

MODULE_AUTHOR ("Kitiz");
MODULE_LICENSE ("GPL");

static unsigned long useraddr = 0;
module_param(useraddr, ulong, S_IRUGO);

static unsigned long userpgd = 0;
module_param(userpgd, ulong, S_IRUGO);

static struct mm_struct mm;
pgd_t *kernel_pgd;

static void
get_pgtable_macro (void)
{

printk (" PGDIR_SHIFT = %d\n", PGDIR_SHIFT);
printk (" P4D_SHIFT = %d\n", P4D_SHIFT);
printk (" PUD_SHIFT = %d\n", PUD_SHIFT);
printk (" PMD_SHIFT = %d\n", PMD_SHIFT);
printk (" PAGE_SHIFT = %d\n", PAGE_SHIFT);

printk (" PTRS_PER_PGD = %d\n", PTRS_PER_PGD);
printk (" PTRS_PER_P4D = %d\n", PTRS_PER_P4D);
printk (" PTRS_PER_PUD = %d\n", PTRS_PER_PUD);
printk (" PTRS_PER_PMD = %d\n", PTRS_PER_PMD);
printk (" PTRS_PER_PTE = %d\n", PTRS_PER_PTE);

printk (" PAGE_OFFSET = 0x%lx\n", PAGE_OFFSET);
printk ("PTE_ADDR_MASK = 0x%lx\n", PTE_ADDR_MASK);

printk (" PAGE_MASK = 0x%lx\n", PAGE_MASK);
}

static unsigned long
vaddr2paddr (unsigned long vaddr)
{
pgd_t *pgd;
p4d_t *p4d;
pud_t *pud;
pmd_t *pmd;
pte_t *pte;
unsigned long paddr = 0;
unsigned long page_addr = 0;
unsigned long page_offset = 0;
struct page *pagep = NULL;

pgd_t pgdk;
unsigned long ttbr1 = read_sysreg(ttbr1_el1);
pgdk.pgd = ttbr1;
kernel_pgd = (pgd_t *)phys_to_virt(__pgd_to_phys(pgdk));

mm.pgd = kernel_pgd;
printk ("ttbr1 0x%lx, kernel pgd 0x%lx, page 0x%lx, pgd 0x%lx, virt 0x%lx\n",
ttbr1, kernel_pgd, phys_to_page(__pgd_to_phys(pgdk)), __pgd_to_phys(pgdk), page_to_virt(phys_to_page(__pgd_to_phys(pgdk))));

if ((vaddr & PAGE_OFFSET) != 0) {
//pgd = pgd_offset (current->mm, vaddr);
//p &init_mm
//address init_mm
pgd = pgd_offset ((struct mm_struct*)0xffffffc0121da790, vaddr);
//pgd = pgd_offset_k (vaddr);
}
else {
mm.pgd = userpgd;
pgd = pgd_offset (&mm, vaddr);
}
printk ("pgd 0x%lx\n", pgd);
printk ("pgd_val 0x%lx, pgd_index %lu\n", pgd_val(*pgd), pgd_index(vaddr));
if (pgd_none (*pgd))
{
printk ("not mapped in pgd\n");
return -1;
}

p4d = p4d_offset (pgd, vaddr);
printk ("p4d_val 0x%lx\n", p4d_val(*p4d));
if (p4d_none (*p4d))
{
printk ("not mapped in pgd\n");
return -1;
}

pud = pud_offset (p4d, vaddr);
printk ("pud_val 0x%lx, pgd_index %lu\n", pud_val (*pud), pud_index(vaddr));
if (pud_none (*pud))
{
printk ("not mapped in pud\n");
return -1;
}

pmd = pmd_offset (pud, vaddr);
printk ("pmd_val 0x%lx, pmd_index %lu\n", pmd_val (*pmd), pmd_index (vaddr));
if (pmd_none (*pmd))
{
printk ("not mapped in pmd\n");
return -1;
}

//pte = pte_offset_map(pmd, addr);
pte = pte_offset_kernel (pmd, vaddr);
printk ("pte_val 0x%lx, pte_index %lu\n", pte_val (*pte), pte_index (vaddr));
if (pte_none (*pte))
{
printk ("not mapped in pte\n");
return -1;
}

if (pte_present(*pte)) {
pagep = pte_page(*pte);
printk ("virt: 0x%lx, page: 0x%lx, pfn: %ld\n",
vaddr, pagep, page_to_pfn (pagep));
}
/* Page frame physical address mechanism | offset */
page_addr = pte_val (*pte) & PAGE_MASK;
pte_unmap(pte);
page_offset = vaddr & ~PAGE_MASK;
paddr = page_addr | page_offset;
printk ("page_addr 0x%lx, page_offset 0x%lx\n", page_addr, page_offset);
printk ("vaddr 0x%lx, paddr 0x%lx\n", vaddr, paddr);

if ((vaddr & PAGE_OFFSET) != 0) {
page_addr = (unsigned long)vmalloc_to_page((void*)vaddr);
printk ("virt: 0x%lx, phys: 0x%lx, page: 0x%lx, pfn: %ld\n",
vaddr, page_to_phys((struct page*)page_addr), page_addr, page_to_pfn ((struct page *)page_addr));
}

return paddr;
}


static int __init
hello_init (void)
{
unsigned long virt, tmp;
void *value;

 

printk (KERN_ALERT "__get_free_pages:\n");
tmp = __get_free_pages (GFP_KERNEL, get_order (16 * 1024));
printk ("get free pages vaddr=0x%lx\n", tmp);
virt = tmp;
printk ("virt: 0x%lx, phys: 0x%lx, page: 0x%lx, pfn: %ld\n", virt,
(unsigned long) virt_to_phys ((void *) virt),
virt_to_page ((void *) virt),
page_to_pfn (virt_to_page ((void *) virt)));

virt = tmp + 4 * 1024;
printk ("virt: 0x%lx, phys: 0x%lx, page: 0x%lx, pfn: %ld\n", virt,
(unsigned long) virt_to_phys ((void *) virt),
virt_to_page ((void *) virt),
page_to_pfn (virt_to_page ((void *) virt)));

virt = tmp + 8 * 1024;
printk ("virt: 0x%lx, phys: 0x%lx, page: 0x%lx, pfn: %ld\n", virt,
(unsigned long) virt_to_phys ((void *) virt),
virt_to_page ((void *) virt),
page_to_pfn (virt_to_page ((void *) virt)));

printk ("\n");


printk ("pagetable macro:\n");
get_pgtable_macro ();
printk ("\n");

 

printk (KERN_ALERT "kmalloc:\n");
value = kmalloc (16 * 1024, GFP_KERNEL);
printk ("kmalloc vaddr 0x%lx\n", value);
virt = (unsigned long) value;
printk ("virt: 0x%lx, phys: 0x%lx, page: 0x%lx, pfn: %ld\n", virt,
(unsigned long) virt_to_phys ((void *) virt),
virt_to_page ((void *) virt),
page_to_pfn (virt_to_page ((void *) virt)));

virt = virt + 4 * 1024;
printk ("virt: 0x%lx, phys: 0x%lx, page: 0x%lx, pfn: %ld\n", virt,
(unsigned long) virt_to_phys ((void *) virt),
virt_to_page ((void *) virt),
page_to_pfn (virt_to_page ((void *) virt)));

virt = virt + 4 * 1024;
printk ("virt: 0x%lx, phys: 0x%lx, page: 0x%lx, pfn: %ld\n", virt,
(unsigned long) virt_to_phys ((void *) virt),
virt_to_page ((void *) virt),
page_to_pfn (virt_to_page ((void *) virt)));

printk ("vaddr to paddr:\n");
vaddr2paddr (value);

printk ("\n");

 

 

printk (KERN_ALERT "vmalloc:\n");

printk ("vaddr to paddr:\n");
unsigned long vaddr = vmalloc (16 * 1024);
printk ("vmalloc vaddr 0x%lx\n", vaddr);
vaddr2paddr (vaddr);

memset ((void*)vaddr, 0, 16 * 1024);
printk ("after memset:\n");
vaddr2paddr (vaddr);

printk ("virt: 0x%lx, phys: 0x%lx, page: 0x%lx, pfn: %ld\n", vaddr,
(unsigned long) virt_to_phys ((void *) vaddr),
virt_to_page ((void *) vaddr),
page_to_pfn (virt_to_page ((void *) vaddr)));
vaddr = vaddr + 4 * 1024;
printk ("virt: 0x%lx, phys: 0x%lx, page: 0x%lx, pfn: %ld\n", vaddr,
(unsigned long) virt_to_phys ((void *) vaddr),
virt_to_page ((void *) vaddr),
page_to_pfn (virt_to_page ((void *) vaddr)));

printk ("\n");

 

vaddr = __get_free_page (GFP_KERNEL);
if (vaddr == 0)
{
return 0;
}
printk ("get_page_vaddr 0x%lx\n", vaddr);
virt = vaddr;
printk ("virt: 0x%lx, phys: 0x%lx, page: 0x%lx, pfn: %ld\n", virt,
(unsigned long) virt_to_phys ((void *) virt),
virt_to_page ((void *) virt),
page_to_pfn (virt_to_page ((void *) virt)));

vaddr2paddr (vaddr);
printk ("\n");

 

if (0 != useraddr) {
printk ("user mode addr:\n");
printk ("useraddr 0x%lx\n", useraddr);
vaddr2paddr (useraddr);
}
#if 0
printk ("virt: 0x%lx, phys: 0x%lx, page: 0x%lx, pfn: %ld\n", virt,
(unsigned long) virt_to_phys ((void *) virt),
virt_to_page ((void *) virt),
page_to_pfn (virt_to_page ((void *) virt)));

virt = virt + 4 * 1024;
printk ("virt: 0x%lx, phys: 0x%lx, page: 0x%lx, pfn: %ld\n", virt,
(unsigned long) virt_to_phys ((void *) virt),
virt_to_page ((void *) virt),
page_to_pfn (virt_to_page ((void *) virt)));

virt = virt + 8 * 1024;
printk ("virt: 0x%lx, phys: 0x%lx, page: 0x%lx, pfn: %ld\n", virt,
(unsigned long) virt_to_phys ((void *) virt),
virt_to_page ((void *) virt),
page_to_pfn (virt_to_page ((void *) virt)));
#endif
printk ("\nkernel pgd:\n");

unsigned long page_addr;
vaddr = (unsigned long)((struct mm_struct*)0xffffffc0121da088)->pgd;
page_addr = (unsigned long)vmalloc_to_page((void*)vaddr);
printk ("virt: 0x%lx, phys: 0x%lx, page: 0x%lx, pfn: %ld\n",
vaddr, page_to_phys((struct page*)page_addr), page_addr, page_to_pfn ((struct page *)page_addr));


return 0;
}

static void __exit
hello_exit (void)
{
printk (KERN_ALERT "Exit mudule...\n");
}

module_init (hello_init);
module_exit (hello_exit);

MODULE_DESCRIPTION ("Page tables test");
MODULE_ALIAS ("A test module");

 

Makefile:

#makefile for the hello.c


obj-m := test.o
CURRENT_PATH := $(shell pwd)
LINUX_KERNEL := $(shell uname -r)
LINUX_KERNEL_PATH := /root/kernel-source/linux-5.10.66
all:
$(MAKE) -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) modules
clean:
rm -f *.ko *.o modules.* *.mod* Module.symvers *~

 

结果输出:

[ 9.104503] __get_free_pages:
[ 9.104812] get free pages vaddr=0xffffff8006220000
[ 9.105387] virt: 0xffffff8006220000, phys: 0x46220000, page: 0xfffffffefff88800, pfn: 287264
[ 9.106549] virt: 0xffffff8006221000, phys: 0x46221000, page: 0xfffffffefff88840, pfn: 287265
[ 9.107461] virt: 0xffffff8006222000, phys: 0x46222000, page: 0xfffffffefff88880, pfn: 287266
[ 9.107950]
[ 9.108174] pagetable macro:
[ 9.108333] PGDIR_SHIFT = 30
[ 9.108535] P4D_SHIFT = 30
[ 9.108712] PUD_SHIFT = 30
[ 9.108872] PMD_SHIFT = 21
[ 9.109100] PAGE_SHIFT = 12
[ 9.109283] PTRS_PER_PGD = 512
[ 9.109489] PTRS_PER_P4D = 1
[ 9.109676] PTRS_PER_PUD = 1
[ 9.110259] PTRS_PER_PMD = 512
[ 9.110583] PTRS_PER_PTE = 512
[ 9.110837] PAGE_OFFSET = 0xffffff8000000000
[ 9.111078] PTE_ADDR_MASK = 0xfffffffff000
[ 9.111292] PAGE_MASK = 0xfffffffffffff000
[ 9.111490]
[ 9.111624] kmalloc:
[ 9.111778] kmalloc vaddr 0xffffff8006264000
[ 9.112017] virt: 0xffffff8006264000, phys: 0x46264000, page: 0xfffffffefff89900, pfn: 287332
[ 9.112452] virt: 0xffffff8006265000, phys: 0x46265000, page: 0xfffffffefff89940, pfn: 287333
[ 9.112760] virt: 0xffffff8006266000, phys: 0x46266000, page: 0xfffffffefff89980, pfn: 287334
[ 9.113130] vaddr to paddr:
[ 9.113367] ttbr1 0x34000041a58000, kernel pgd 0xffffff8001a58000, page 0xfffffffeffe69600, pgd 0x41a58000, virt 0xffffff8001a58000
[ 9.114472] pgd 0xffffffc011858000
[ 9.114696] pgd_val 0x4bffa003, pgd_index 0
[ 9.114944] p4d_val 0x4bffa003
[ 9.115085] pud_val 0x4bffa003, pgd_index 0
[ 9.115393] pmd_val 0x4bfd4003, pmd_index 49
[ 9.115677] pte_val 0x68000046264707, pte_index 100
[ 9.115990] virt: 0xffffff8006264000, page: 0xfffffffefff89900, pfn: 287332
[ 9.116349] page_addr 0x68000046264000, page_offset 0x0
[ 9.116611] vaddr 0xffffff8006264000, paddr 0x68000046264000
[ 9.116913] virt: 0xffffff8006264000, phys: 0x46264000, page: 0xfffffffefff89900, pfn: 287332
[ 9.117318]
[ 9.117425] vmalloc:
[ 9.117539] vaddr to paddr:
[ 9.117916] vmalloc vaddr 0xffffffc012591000
[ 9.118162] ttbr1 0x34000041a58000, kernel pgd 0xffffff8001a58000, page 0xfffffffeffe69600, pgd 0x41a58000, virt 0xffffff8001a58000
[ 9.118698] pgd 0xffffffc011858800
[ 9.118875] pgd_val 0x4bfff003, pgd_index 256
[ 9.119213] p4d_val 0x4bfff003
[ 9.119391] pud_val 0x4bfff003, pgd_index 0
[ 9.119566] pmd_val 0x4bffb003, pmd_index 146
[ 9.119719] pte_val 0x6800004625a703, pte_index 401
[ 9.119925] virt: 0xffffffc012591000, page: 0xfffffffefff89680, pfn: 287322
[ 9.120191] page_addr 0x6800004625a000, page_offset 0x0
[ 9.120374] vaddr 0xffffffc012591000, paddr 0x6800004625a000
[ 9.120648] virt: 0xffffffc012591000, phys: 0x4625a000, page: 0xfffffffefff89680, pfn: 287322
[ 9.121302] after memset:
[ 9.121816] ttbr1 0x34000041a58000, kernel pgd 0xffffff8001a58000, page 0xfffffffeffe69600, pgd 0x41a58000, virt 0xffffff8001a58000
[ 9.122512] pgd 0xffffffc011858800
[ 9.122699] pgd_val 0x4bfff003, pgd_index 256
[ 9.122836] p4d_val 0x4bfff003
[ 9.122987] pud_val 0x4bfff003, pgd_index 0
[ 9.123171] pmd_val 0x4bffb003, pmd_index 146
[ 9.123329] pte_val 0x6800004625a703, pte_index 401
[ 9.123524] virt: 0xffffffc012591000, page: 0xfffffffefff89680, pfn: 287322
[ 9.123887] page_addr 0x6800004625a000, page_offset 0x0
[ 9.124121] vaddr 0xffffffc012591000, paddr 0x6800004625a000
[ 9.124514] virt: 0xffffffc012591000, phys: 0x4625a000, page: 0xfffffffefff89680, pfn: 287322
[ 9.124964] virt: 0xffffffc012591000, phys: 0x42791000, page: 0x296440, pfn: 67446161
[ 9.125328] virt: 0xffffffc012592000, phys: 0x42792000, page: 0x296480, pfn: 67446162
[ 9.125643]
[ 9.126048] get_page_vaddr 0xffffff8006256000
[ 9.126410] virt: 0xffffff8006256000, phys: 0x46256000, page: 0xfffffffefff89580, pfn: 287318
[ 9.126889] ttbr1 0x34000041a58000, kernel pgd 0xffffff8001a58000, page 0xfffffffeffe69600, pgd 0x41a58000, virt 0xffffff8001a58000
[ 9.127330] pgd 0xffffffc011858000
[ 9.127475] pgd_val 0x4bffa003, pgd_index 0
[ 9.127689] p4d_val 0x4bffa003
[ 9.127811] pud_val 0x4bffa003, pgd_index 0
[ 9.127998] pmd_val 0x4bfd4003, pmd_index 49
[ 9.128160] pte_val 0x68000046256707, pte_index 86
[ 9.128333] virt: 0xffffff8006256000, page: 0xfffffffefff89580, pfn: 287318
[ 9.128588] page_addr 0x68000046256000, page_offset 0x0
[ 9.128768] vaddr 0xffffff8006256000, paddr 0x68000046256000
[ 9.128958] virt: 0xffffff8006256000, phys: 0x46256000, page: 0xfffffffefff89580, pfn: 287318
[ 9.129387]
[ 9.129491]
[ 9.129491] kernel pgd:
[ 9.129946] virt: 0xffffffc0121da0f0, phys: 0x423da000, page: 0xfffffffeffe8f680, pfn: 271322

标签:kernel,pgd,virt,lx,address,printk,page,0x%
From: https://www.cnblogs.com/kitiz/p/16729538.html

相关文章

  • 大页内存(Huge Pages)
    简单来说就是通过增大操作系统页的大小来减小页表,从而避免快表缺失。在介绍之前需要强调一点,大页内存也有适用范围,程序耗费内存很小或者程序的访存局部性很好,大页内存很难......
  • WEB自动化-10-Page Object 模型
    10PageObject模型10.1概述  在针对一个WEB页面编写自动化测试用例时,需要引用页面中的元素(数据)才能进行操作(动作)并显示出页面内容。如果编写的测试用例是直接针对......
  • VirtualBox-kernel-driver-not-installed-error(rc=-1908)
    在debian中更新的软件后发现virtualbox无法启动虚拟机,抛出错误说内核驱动未安装,请重新运行vboxconfig,多次运行或重启后无法解决错误,检查/usr/src目录发现当前内......
  • <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs&
    https://www.cnblogs.com/ChineseMoonGod/archive/2013/06/07/3123907.html<%@PageLanguage="C#"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="_Defa......
  • mybatis-plus使用selectPage查询数据时,total为0 的解决方案
    1:问题描述项目迁移,原本正常的mp的分页坏了,经过调试发现是total参数没有  2:原因这是mp需要设置分页拦截器才能生效的,所以只要注入一下拦截器即可3:解决把这段代码放......
  • Adaptive Sampled Softmax with Kernel Based Sampling
    目录概符号说明Motivation本文的方法Kernel的选择BlancG.andRendleS.Adaptivesampledsoftmaxwithkernelbasedsampling.InInternationalConferenceonMac......
  • asp.net core Razor Page 分页
    1.使用Nuget下载LazZiya.TagHelpers2._ViewImports.cshtml中添加@addTagHelper*,LazZiya.TagHelpers3.前台页面使用<tableclass="tabletable-bordered">......
  • ViewPager2 应用和DiffUtil的解释
    目前ViewPager2对Fragment支持只能FragmentStateAdapter,FragmentStateAdapter在遇到预加载时,只会创建Fragment对象,不会把Fragment真正的加入到布局中,所以自带懒加载效果。......
  • Android使用ViewPager2实现页面滑动切换
    Android使用ViewPager2实现页面滑动切换 作者:QiShare转载地址:https://juejin.cn/post/70655660992233472131.引言在很多应用中,我们经常会看到多个页面之间滑动切换......
  • Mybatis使用PageHelper分页插件
    1<dependency>2<groupId>com.github.pagehelper</groupId>3<artifactId>pagehelper-spring-boot-starter</artifactId>4......