LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-11-2013, 07:26 AM   #1
mrthaggar
LQ Newbie
 
Registered: Jan 2013
Posts: 2

Rep: Reputation: Disabled
Reading from a physical memory address


Hi all, first post here so I'll try to be as clear as I can

Essentially I need to read an int that is stored in memory, and I have it's physical address. This address was obtained from a running kernel module, and I can confirm 100% that it is a physical address, not a virtual one.

I need to either create a kernel module, or a userspace app that will allow me to read the int from the physical address. Read only access is required, I don't need/want to write anything to memory at this time.

From what I've been reading, I think it might be possible to do what I want using the mmap function, which should (I might be wrong here) map a physical memory address into a process space so that it can be read from.

If anyone could provide some information on accessing physical memory, or mapping a physical address to a virtual address I would greatly appreciate it.

Thanks in advance!
 
Old 01-11-2013, 01:23 PM   #2
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Nope.

Kernel physical addresses are useless outside the kernel. mmap will not map kernel memory.

This was why the sysfs virtual filesystem was created. Kernel modules that need to import/export data in odd units define entries that allow the sysfs filesystem to have the appropriate directory tree, and can import/export whatever they need.

The problem with physical addresses is that they can change... And updating a physical address does not guarantee that what is there is always there. It depends on how the module is loaded, what other modules may be present. So "peeking and poking" the kernel is not supported.

as a quickie followup, what are you trying to do?

Last edited by jpollard; 01-11-2013 at 01:24 PM.
 
Old 01-14-2013, 05:27 AM   #3
mrthaggar
LQ Newbie
 
Registered: Jan 2013
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thanks jpollard.

Basically, I've got a loaded kernel module that as part of its init routine creates a 4KB struct in memory.

What I need to do is share this struct so that at a later date, a user space process can have read only access to it.

I've done a bit more reading and I think I need to either use mmap, or remap_pfn_range/remap_pfn_pages.

If you could provide any further information, that would be great.

Thanks
 
Old 01-14-2013, 08:28 AM   #4
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
As I said, mmap cannot map kernel memory. NONE of the user mode mappings can do so.

What you have to do is add a sysfs entry for your module. The user process can then read the entry in the normal way.

Anything else can crash your system.

Last edited by jpollard; 01-14-2013 at 08:31 AM.
 
Old 01-14-2013, 09:43 AM   #5
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,138

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
There used to be a device, /dev/kmem, for accessing kernel virtual memory; but it's been eliminated in recent kernels as a security hole. Privileged processes can access physical addresses from user space using /dev/mem. However using this to look at a kernel struct is like using a cannon to turn off your lights. Use a sysfs entry as jpollard suggests.
 
Old 01-14-2013, 10:10 AM   #6
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
And kmem is kernel memory... not necessarily the same as physical memory. It does get remapped as pages move around.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Memory map or physical address of some memory allocated by malloc demon007 Programming 1 02-04-2012 06:17 AM
Can we allocate physical memory starting from a given address? bvg_1 Linux - Newbie 4 09-19-2011 02:52 AM
virtual memory address to physical address _Linux_Learner Linux - Newbie 1 04-14-2010 01:34 AM
virtual address to physical memory access pupillo Linux - General 2 02-22-2010 10:07 AM
Memory Mapping (same physical address = virtual address) skd.tech Linux - Kernel 7 07-24-2006 11:13 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:10 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration