I have a multi-threaded linux program where I want to intercept reads and writes in a particular memory area, possibly performing actions determined by the location read/written providing values to reads and responding to values in writes.
I have set up the area as protected memory and get a sigsev when it is accessed. In the sigsev I map the area as read/write, perhaps call a processor function for a read, set the "TRAP" processor control flag (which limits the next action to a single machine instruction) and return. The accessing instruction executes and then a sigtrap is triggered. In the sigtrap handler, I perhaps call a processor function for the write, reset the trap flag, remap the memory to protected and return.
This works fine, most of the time. Just occasionally, however, the threads switch while the execution is in the trap handler before the memory access is removed. Another thread then accesses the memory but doesn't cause a sigsev, because the memory is mapped read/write.
I want (need) the sigsev - single instruction - sigtrap to be atomic, so is there any way to prevent a pthreads task switch inside a signal handler?
Unfortunately, paranoid corporate policies mean I can't provide code...