Emulating memory mapped registers via mmap, mprotect and sigaction?
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.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Emulating memory mapped registers via mmap, mprotect and sigaction?
I'm working with an embedded device that has memory mapped registers to various bits of hardware.
So I'm trying to emulate it on a desktop by mmap'ing the same address range and then mprotecting it and the setting a sigaction handler to be called if the code attempts to read or write to the addresses in that range.
So I can get as far as that...
..but I don't know how to know detect whether it's a read or a write and find out what value was being written and or return the correct value to a read and then allow the program to proceed.
I suspect the ucontext parameter may help but I'm not sure how.
It almost seems to me that you might have to look at the instruction that was executing to see if it was a read-type or write-type instruction, by looking at the parameters that are handed to your handler. I believe that the trap will go off based on the memory address that was accessed, no matter how or why. You might well have to figure out what kind of access it was. Messy.
SIGILL, SIGFPE, SIGSEGV, SIGBUS, and SIGTRAP fill in si_addr with the address of the fault.
But I don't see anything indicating whether it was read or write. So I agree with sundialscvs, you would have to pull out the code address and decode the instruction. Messy indeed.
I don't think so: the hardware hands the operating-system a page-fault interrupt that simply informs it that the memory access failed. The OS can determine both what address was referenced and, based on the interrupt-return call-stack entry, where in the world that it was. But you should find, in the source-code of something like QEmu or VirtualBox, code which determines what to do by analyzing the instruction.
If you can't find something that already exists, you could write logic which uses single-byte decision tables to analyze the possibly multi-byte sequences which make up a CPU instruction, to classify it as being either a read or a write ... but there still could be further flies in the ointment: an instruction such as MOVE is both at the same time.
However, since you are apparently trying to create an emulator for a particular case that is well-known to you, perhaps you can suffice with a less-than fully-generalized solution ... one that is good enough for you and that need not be more.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.