LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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
  Search this Thread
Old 01-17-2018, 10:34 PM   #1
cyent
Member
 
Registered: Aug 2001
Location: ChristChurch New Zealand
Distribution: Ubuntu
Posts: 398

Rep: Reputation: 87
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.

Any ideas?
 
Old 01-19-2018, 07:57 AM   #2
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,679
Blog Entries: 4

Rep: Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947
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.
 
Old 01-20-2018, 06:46 PM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,786

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
signaction(2) says:
Quote:
void
handler(int sig, siginfo_t *info, void *ucontext)
[...]
siginfo_t {
[...]
void *si_addr; /* Memory location which caused fault */
[...]

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.
 
Old 01-21-2018, 02:43 PM   #4
cyent
Member
 
Registered: Aug 2001
Location: ChristChurch New Zealand
Distribution: Ubuntu
Posts: 398

Original Poster
Rep: Reputation: 87
Sigh. It was nice dream for a brief moment......

.... I wonder if I could do it in Qemu instead. Hmmmm.
 
Old 01-22-2018, 08:32 AM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,679
Blog Entries: 4

Rep: Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947
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.
 
  


Reply

Tags
emulation, mmap, mprotect, sigaction, ucontext



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
mmap to read and write registers crashes markcgriffiths Programming 4 09-08-2014 12:24 AM
How to access UART registers which is memory mapped at address 0x2530c00 in Linux? girmaji Linux - Embedded & Single-board computer 0 01-14-2014 09:44 AM
[SOLVED] python: safe to mmap a tempfile (that may close while mapped)? BrianK Programming 1 10-18-2011 10:19 AM
Network device Memory mapped or Port mapped ?? Bignon Linux - Hardware 0 10-20-2009 08:36 AM
sigaction always registers sa_handler even if SA_SIGINFO is specified sarin Programming 5 12-27-2006 10:29 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:59 PM.

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