I am trying to do some simulation. In the real case, an x86-based Industrial PC connects with an network board using PC/104. The network board is treated as an I/O device. The code running on IPC uses assembly instructions like IN, OUT to access the I/O ports. I want to simulate the whole thing on my laptop.
The c++ program running on IPC can be ported to my laptop since they are both based on x86-architecture. But for IN/OUT instructions, I ran into troubles since no real network board is connected to my laptop. I simulated the logics of the network board using SystemC. I am wondering if there’s a way to intercept the I/O port access(IN/OUT) to real hardware and let the logics of the virtualized hardware take over.
I appreciate any help or advice from you.
If your code with IN/OUT instructions is in a user program, you need to find out how to intercept and handle these instructions either in this same program (perhaps, by means of signals?) or in the kernel. IN and OUT are privileged instructions and by default they cause an exception (#GP) in user-mode code and the kernel then handles this exception. You need to have your own handler for this case. In Windows this can be done using so-called Structured Exception Handling (AKA SEH).
If the code is in the kernel already, it’s tricky because IN/OUT in there don’t cause #GP.
Couldn’t you just replace the routines doing IN and OUT with something else, with the calls to the device emulation code? You’re emulating it anyway, nobody cares if it’s IN/OUT or something else.
See how this can be done with SEH.