LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Hardware > Linux - Embedded & Single-board computer
User Name
Password
Linux - Embedded & Single-board computer This forum is for the discussion of Linux on both embedded devices and single-board computers (such as the Raspberry Pi, BeagleBoard and PandaBoard). Discussions involving Arduino, plug computers and other micro-controller like devices are also welcome.

Notices

Reply
 
Search this Thread
Old 10-03-2011, 04:56 PM   #1
brandony
LQ Newbie
 
Registered: Oct 2011
Posts: 6

Rep: Reputation: Disabled
How to access physical memory from user space


Hi all,

I'm a bit new to embedded linux. I'm trying to figure out the best way to directly write to two physical registers in my system. Writing a kernel driver for this is not really an option right now.

What's the best way to map the physical addresses into my user space, if this is even possible?
 
Old 10-03-2011, 10:57 PM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,758

Rep: Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643
I'm confused whether you're talking about memory or cpu as you mentioned registers.. if you really mean registers then use inline asm (assuming your language supports it). Access to physical memory is not really an option under any modern OS as the virtual memory system will control what processes see.
It is possible to read physical memory but I don't think you'll be able to map which process is in which location, you'd need to take into account paging as well.
 
Old 10-04-2011, 07:07 AM   #3
brandony
LQ Newbie
 
Registered: Oct 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
Sorry I should have said physical addresses instead of physical memory. It looks like using mmap with /dev/mem to map the registers into virtual user space may be my best option. Does this sound right?
 
Old 10-04-2011, 04:59 PM   #4
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,758

Rep: Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643
Sorry, I'm still confused by your terminology, its a little difficult to follow when you switch between "physical addresses", "physical memory" and "registers" - you want to be able to read physical memory from user space, correct? ...
 
Old 10-04-2011, 05:14 PM   #5
brandony
LQ Newbie
 
Registered: Oct 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
I apologize for the confusing terminology, as I said I'm new to embedded linux. My background is with embedded software on devices running lower level RTOS where virtual addressing is not a consideration. I've solved my problem (I believe) but I'll try to clarify a bit for the sake of anyone else looking for the same answer.

First, forget I ever said "Physical memory." What I am trying to do is gain read/write access to a hardware register (not a processor register) from my user-mode application. Initially I was confused how to access physical addresses when my process is only using virtual addresses. Using mmap with /dev/mem I was able to map the section of physical address space that addresses the registers into virtual addresses in my process address space. From there reading and writing was trivial.

Last edited by brandony; 10-04-2011 at 05:17 PM.
 
1 members found this post helpful.
Old 10-04-2011, 05:22 PM   #6
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,758

Rep: Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643
Ah, I see - the term we were looking for here is "memory-mapped I/O", glad it's resolved
 
Old 10-27-2011, 03:42 PM   #7
brandony
LQ Newbie
 
Registered: Oct 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
Rather than start a new thread I'll just follow up here. I have a memory mapped IO address that I'm unable to write to from user space. I can read to it. And I can read/write it from a kernel module or from uboot. I'm just unable to write to it from my user process.

What would cause such behavior? Is there some global write protections that's set up at startup?
 
Old 11-13-2011, 05:29 PM   #8
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
You can use mmap + /dev/mem to access memory-mapped IO from userspace, but you must have root privileges.
--- rod.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
LXer: User space memory access from the Linux kernel LXer Syndicated Linux News 0 08-13-2010 06:40 AM
Division of Logical Memory Space in to User Space and Kernel Space shreshtha Linux - Newbie 2 01-14-2010 09:59 AM
Map kernel physical space to user space zvivered Linux - Embedded & Single-board computer 2 12-11-2009 06:18 AM
Swap space did not function when physical memory ( RAM ) is full rabbit00 Linux - Newbie 19 07-06-2009 09:18 PM
How to access physical memory in C? cyu021 Programming 6 03-27-2005 04:16 AM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration