ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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!
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).
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.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.