阅读和通过的/ dev / MEM,文本段作品写进程的内存,但数据段不能,为什么? [英] Read and write process' memory through /dev/mem, text segment works but data segment can not, why?
问题描述
我想读,并从进程的内存通过的/ dev / MEM
写的。
I want to read to and write from process' memory through /dev/mem
.
首页后,我通过我自己一个Linux内核模块codeD获得进程的内存映射,输出是这样的:
First, I get process' memory map through a linux kernel module coded by myself, output is like this:
start_code_segment 4000000000000000
end_code_segment 4000000000019c38
start_data_segment 6000000000009c38
end_data_segment 600000000000b21d
start_brk 6000000000010000
brk 6000000000034000
start_stack 60000fffffde7b00
二,我可以转换的虚拟地址(VA),以PA彻底的Linux内核模块,例如,我可以转换 VA:0x4000000000000008
到 PA:0x100100c49f8008
Second, I can convert virtual address(VA) to PA thorough the linux kernel module, for example, I can convert VA:0x4000000000000008
to PA:0x100100c49f8008
第三,功能 read_phy_mem
可以在内存中的数据PA:0x100100c49f8008
,code。在最后的决赛。
Third, function read_phy_mem
can get memory data in PA:0x100100c49f8008
,code at the final.
问题:我的问题是,当我读文本段
PA记忆,一切都OK,但如果我读数据段
PA内存, *((*长)mapAddr)
排队243会导致系统下去。另外,我试过
Problem: My problem is when I read text segment
PA memory, everything is OK, but if I read data segment
PA memory, *((long *)mapAddr)
in line 243 will cause system to go down. Also, I tried
memcpy( &data, (void *)mapAddr, sizeof(long) )
但它仍使系统走下来。
but it still make the system go down.
其他信息:我的电脑是IA64,操作系统就是Linux 2.6.18,当系统关闭,我可以从这样的控制台获取输出信息,系统会重新启动
other info: my computer is IA64, OS is Linux 2.6.18, when system is down, I can get output Info from console like this, then system will restart.
Entered OS MCA handler. PSP=20010000fff21320 cpu=0 monarch=1
cpu 0, MCA occurred in user space, original stack not modified
All OS MCA slaves have reached rendezvous
MCA: global MCA
mlogbuf_finish: printing switched to urgent mode, MCA/INIT might be dodgy or fail.
Delaying for 5 seconds...
函数的 code read_phy_mem
code of function read_phy_mem
/*
* pa: physical address
* data: memory data in pa
*
* return int: success or failed
*/
188 int read_phy_mem(unsigned long pa,long *data)
189 {
190 int memfd;
191 int pageSize;
192 int shift;
193 int do_mlock;
194 void volatile *mapStart;
195 void volatile *mapAddr;
196 unsigned long pa_base;
197 unsigned long pa_offset;
198
199 memfd = open("/dev/mem", O_RDWR | O_SYNC);
200 if(memfd == -1)
201 {
202 perror("Failed to open /dev/mem");
203 return FAIL;
204 }
205
206 shift = 0;
207 pageSize = PAGE_SIZE; //#define PAGE_SIZE 16384
208 while(pageSize > 0)
209 {
210 pageSize = pageSize >> 1;
211 shift ++;
212 }
213 shift --;
214 pa_base = (pa >> shift) << shift;
215 pa_offset = pa - pa_base;
224 mapStart = (void volatile *)mmap(0, PAGE_SIZE, PROT_READ | PROT_WRITE,MAP_SHARED | MAP_LOCKED, memfd, pa_base);
226 if(mapStart == MAP_FAILED)
227 {
228 perror("Failed to mmap /dev/mem");
229 close(memfd);
230 return FAIL;
231 }
232 if(mlock((void *)mapStart, PAGE_SIZE) == -1)
233 {
234 perror("Failed to mlock mmaped space");
235 do_mlock = 0;
236 }
237 do_mlock = 1;
238
239 mapAddr = (void volatile *)((unsigned long)mapStart + pa_offset);
243 printf("mapAddr %p %d\n", mapAddr, *((long *)mapAddr));
256 if(munmap((void *)mapStart, PAGE_SIZE) != 0)
257 {
258 perror("Failed to munmap /dev/mem");
259 }
260 close(memfd);
269 return OK;
270 }
任何人都可以理解为什么文本片段效果很好,但数据段不?
Can anyone understand why text segment works well but data segment does not?
推荐答案
我想,它的发生,因为$ C $剖腹产保留在内存中,而进程执行(如果不是DLL code),而数据部分休假在&放大器;出不断。结果
尝试用堆栈段。而如果它的工作检查?结果
编写自己的测试程序,并动态地以KB分配内存,并保持一个循环内使用该内存。比你的code尝试读取测试程序的内存段。我认为它会工作。结果
我已经做了类似的工作在Windows中,从IVT取代BIOS的地址。结果
应该是root用户。
I guess, its happening because code-section remain in memory while process executes(if not a DLL code), Whereas data section leave in & out continuously.
Try with stack-Segment. And check if its working?
Write your own test program and allocate memory dynamically in KBs and keep that memory in use within a loop. Than try it with your code to read memory segments of test program. I think it will work.
I have done similar work in windows to replace BIOS address from IVT.
Should be root user.
这篇关于阅读和通过的/ dev / MEM,文本段作品写进程的内存,但数据段不能,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!