5 Star 6 Fork 5

anolis/ras-tools

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
proc_pagemap.c 1.21 KB
一键复制 编辑 原始数据 按行查看 历史
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) 2015 Intel Corporation
* Author: Tony Luck
*
* This software may be redistributed and/or modified under the terms of
* the GNU General Public License ("GPL") version 2 only as published by the
* Free Software Foundation.
*/
/*
* Convert a user mode virtual address belonging to the
* current process to physical.
* Does not handle huge pages.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
/*
* get information about address from /proc/{pid}/pagemap
*/
unsigned long long vtop(unsigned long long addr, pid_t pid)
{
static int pagesize;
unsigned long long pinfo;
long offset;
int fd;
char pagemapname[64];
if (pagesize == 0)
pagesize = getpagesize();
offset = addr / pagesize * (sizeof pinfo);
sprintf(pagemapname, "/proc/%d/pagemap", pid);
fd = open(pagemapname, O_RDONLY);
if (fd == -1) {
perror(pagemapname);
exit(1);
}
if (pread(fd, &pinfo, sizeof pinfo, offset) != sizeof pinfo) {
perror(pagemapname);
close(fd);
exit(1);
}
close(fd);
if ((pinfo & (1ull << 63)) == 0) {
printf("page not present\n");
return ~0ull;
}
return ((pinfo & 0x007fffffffffffffull) * pagesize) + (addr & (pagesize - 1));
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/anolis/ras-tools.git
git@gitee.com:anolis/ras-tools.git
anolis
ras-tools
ras-tools
master

搜索帮助

23e8dbc6 1850385 7e0993f3 1850385