LinuxQuestions.org
Support LQ: Use code LQCO20 and save 20% on CrossOver Office
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 06-18-2005, 06:38 AM   #1
NCC-1701&NCC-1701-D
Member
 
Registered: May 2005
Distribution: Debian Woody,Knoppix
Posts: 88

Rep: Reputation: 15
Question Direct Memory Access with C


Hi all,

I'm just wondering if and how direct memory access with C is possible. As I was examining the MINIX/LINUX source, I found some lines like
#define MEM_LOC 0x<memory address>

Well, can I directly access the memory by giving the hex location and how?
Thanks!
 
Old 06-18-2005, 07:41 AM   #2
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 46
Quote:
As I was examining the MINIX/LINUX source
Uh?

I know there's a kernel named MINIX, and a kernel named LINUX, but I haven't heard of one named MINIX/LINUX--and I wouldn't think they're diff equal for any non-random number of lines.

--

But anyways. You don't get segfaults in kernel mode

And that's basically why you can access any part of the memory. If you're looking for the code to do it, try:
Code:
void* p = (void*) 0xDeadBeef;
/* and then you can do stuff like */
memcpy(p, &foostruct, sizeof(foostruct));
But watch out for endian-ness if you do stuff like that

(like we haven't already chugged any hope of portability).

hth --Jonas
 
Old 06-18-2005, 07:48 AM   #3
NCC-1701&NCC-1701-D
Member
 
Registered: May 2005
Distribution: Debian Woody,Knoppix
Posts: 88

Original Poster
Rep: Reputation: 15
Thanks! I meant Linux and Minix, sorry!
 
Old 06-18-2005, 08:36 AM   #4
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Ubuntu, OEL
Posts: 9,165

Rep: Reputation: 243Reputation: 243Reputation: 243
Beware that the operating system (minix, linux, unix, ...) is running the kernel and all processes in virtual memory, so if by "direct memory access" you mean physical RAM access, pointers are of no use.
/dev/mem on linux or solaris is giving you access to the physical RAM content and /dev/kmem the virtual memory used by the kernel.
 
Old 06-18-2005, 08:39 AM   #5
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 46
Quote:
...pointers are of no use.
Good point!
 
Old 06-18-2005, 09:40 AM   #6
lowpro2k3
Member
 
Registered: Oct 2003
Location: Canada
Distribution: Slackware
Posts: 340

Rep: Reputation: 30
Quote:
Originally posted by jlliagre
...so if by "direct memory access" you mean physical RAM access, pointers are of no use.
Can someone explain this?
 
Old 06-18-2005, 10:07 AM   #7
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 46
Quote:
Can someone explain this?
I'll try.

The kernel does virtual memory.

Values of pointers are virtual addresses, which the kernel will translate into physical addresses for you.

So, when you read from address 0xDeadBeef, the kernel looks up in a table that the virtual address 0xDeadBeef is either:
1) Not allocated (gives you a segfault)
2) Allocated at physical address such-and-such; the kernel then reads from that physical address and gives you the value.

In practice it's a little bit more complicated--the memory is allocated in blocks called pages. The kernel has to look up the page number and calculate the offset into the page.

Capice?

--Jonas
 
Old 06-19-2005, 06:14 AM   #8
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Ubuntu, OEL
Posts: 9,165

Rep: Reputation: 243Reputation: 243Reputation: 243
There's a third case:
3) allocated, but not mapped to physical memory, a page-fault is generated and the memory is retrieved from the swap space (paging space actually) by the kernel. After that, the process is resumed.

Also a comment on 2), that's not really the kernel that returns a virtual address content, but the mmu.
 
Old 06-19-2005, 12:40 PM   #9
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 46
(3) is a good point--you probably know much more about `kernel stuff' than I do

Quote:
Not really the kernel .. but the mmu.
...If there is an mmu.

I rest my case with the conclusion that `for various reasons, pointer != phys mem'
 
Old 06-19-2005, 05:01 PM   #10
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Ubuntu, OEL
Posts: 9,165

Rep: Reputation: 243Reputation: 243Reputation: 243
Quote:
...If there is an mmu.
There's always one when virtual memory is supported.
Last time I had a Unix (in that case unix like) without an mmu, that was an Atari 520ST under Minix about 20 years ago.
The problem was there was neither virtual memory, so the system was pretty unreliable (no memory protection between processes).
I guess ucLinux is providing the same kind of features for embbeded systems with no mmu nowadays.
 
Old 06-19-2005, 05:06 PM   #11
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 46
Quote:
There's always one when virtual memory is supported.
... but it might be implemented in software as opposed to hardware?
(that was what I meant--sorry to make it unclear).

--Jonas
 
Old 06-19-2005, 07:27 PM   #12
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Ubuntu, OEL
Posts: 9,165

Rep: Reputation: 243Reputation: 243Reputation: 243
There's no such thing as a software MMU unless the CPU is also implemented in software, like with QEMU for example.
 
Old 06-20-2005, 12:26 AM   #13
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 46
Oh...
/me reads some more homework
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
SUSE 9.3 and the PC's DMA (Direct Memory Access) langbein Suse/Novell 26 11-22-2005 12:06 PM
Direct memory access through C/C++ NCC-1701&NCC-1701-D Programming 3 09-14-2005 03:20 PM
Direct access to sound device not possible bootsy Linux - Hardware 1 08-06-2005 06:02 AM
Jack or alsa issue - direct access to soundcard not possible bootsy Linux - Software 11 01-24-2005 08:04 PM
RH 8.0 Mapping Virtual Memory to get access to VMIC Reflective Memory PCI card. Merlin53 Linux - Hardware 0 05-05-2003 12:50 PM


All times are GMT -5. The time now is 04:44 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
Open Source Consulting | Domain Registration