Single-stepping
Quoted 8 lines
Quoted 7 lines
Note that this is the short explanation. It is not as simple as it sounds. One major complication is that branches with links don't work properly with the instruction queue magic, so the link register has to be updated in the taken branch trap handler. Also branch externals won't update the space of the space queue tail properly (again, that has to be fixed in the taken branch handler). I can provide more details if the recovery counter method doesn't work out.
Quoted 4 lines
I'm a little intrigued about these "complications". How can the link register or space _not_ be updated properly? As far as I can see, the only really tricky instruction to single-step is RFI - which shouldn't ever occur in userspace, and which we'd just emulate if it was important.
The problem is that the link register is set to IAOQ_Back + 4. and in the case of ble, sr0 is set to IASQ_Back. Since we've played games with the queues, IAOQ_Back and IASQ_Back are pointing at the break page, not at the instruction following the branch.
The additional complication is that the taken branch trap traps at the branch destination, not at the branch, so at the point of the trap you don't know where you came from in order to fix the problem easily. So, what HP-UX does is check each instruction before it executes it to see if it is a branch, and if so, what the link register is (and that is all that needs to be parsed, since we are not emulating the instruction). It then stores the branch location, and also sets some branch state flags (e.g. UBE for a branch external, and UBL for a branch with a link, both flags being set for a ble instruction). Then in the taken branch handler you have all the information you need to fix the queue. You also need to check this saved state if a signal handler is invoked while single stepping, so that the proper pc queue values can be saved in the signal context.
John Marvin
[email protected]
Quoted 9 lines · John Marvin
Quoted 4 lines
I'm a little intrigued about these "complications". How can the link register or space _not_ be updated properly? As far as I can see, the only really tricky instruction to single-step is RFI - which shouldn't ever occur in userspace, and which we'd just emulate if it was important.
The problem is that the link register is set to
IAOQ_Back+ 4. and in the case of ble, sr0 is set toIASQ_Back. Since we've played games with the queues,IAOQ_BackandIASQ_Backare pointing at the break page, not at the instruction following the branch.
Ah. That is a little nasty, especially given the effect on signal handlers you mention below. Maybe using the recovery counter isn't such a bad idea after all, especially since the added syscall and task switch overhead can be quite small if the kernel only supports single-step by one instruction.
Quoted 5 lines
The additional complication is that the taken branch trap traps at the branch destination, not at the branch, so at the point of the trap you don't know where you came from in order to fix the problem easily. So, what HP-UX does is check each instruction before it executes it to see if it is a branch, and if so, what the link register is (and that is all that
Quoted 8 lines
needs to be parsed, since we are not emulating the instruction). It then stores the branch location, and also sets some branch state flags (e.g. UBE for a branch external, and UBL for a branch with a link, both flags being set for a ble instruction). Then in the taken branch handler you have all the information you need to fix the queue. You also need to check this saved state if a signal handler is invoked while single stepping, so that the proper pc queue values can be saved in the signal context.
Another question for you and/or the list in general:
Why does struct pt_regs have an ipsw field? Seems like it currently is unused.
Regards, Alan Modra
Signature · 1 line
Linuxcare. Support for the Revolution.
Re:
Quoted 1 line
handlers you mention below. Maybe using the recovery counter isn't such a
quite true.
Quoted 3 lines
bad idea after all, especially since the added syscall and task switch overhead can be quite small if the kernel only supports single-step by one instruction.
why the limit? We've used multi-instruction "single step" (oxymoron :) for about 15 years on PA-RISC...no problems, efficient, and *very* useful!
Signature · 2 lines
Stan Sieler [email protected] www.allegro.com/sieler/wanted/index.html www.sieler.com
Quoted 7 lines · Stan Sieler
Quoted 3 lines
bad idea after all, especially since the added syscall and task switch overhead can be quite small if the kernel only supports single-step by one instruction.
why the limit? We've used multi-instruction "single step" (oxymoron :) for about 15 years on PA-RISC...no problems, efficient, and *very* useful!
Because you would then need to save and restore cr0 on task switches (or only allow one task to be single-stepped at a time). That's four instructions and two extra memory accesses per task switch. Which might not seem very much, but at some point somebody will no doubt start caring about pa-linux performance. For a single-step by one, you can simply set cr0 to zero on a task switch, and possibly avoid touching cr0 on a task switch at all with careful attention to various trap handlers.
Here's the idea. The tail of syscall_restore (64-bit stuff pruned) will look like the following, with the first three instructions being the added code to support single-step (and also wide/narrow switching for 64-bit)
ldi 3,%r20 mtctl $r20,%cr0 /* recovery counter, ptrace single-step */ LDREG TASK_PT_PSW(%r1),%r20 mtctl %r1,%cr30 /* intrhandler okay. */ mfsp %sr3,%r1 /* Get users space id */ mtsp %r1,%sr4 /* Restore sr4 */ mtsp %r1,%sr5 /* Restore sr5 */ mtsp %r1,%sr6 /* Restore sr6 */ depi 3,31,2,%r31 /* ensure return to user mode. */ mtsm %r20 /* restore irq state */ mfctl %cr27,%r20 be 0(%sr3,%r31) /* return to user space */ mtsp %r1,%sr7 /* Restore sr7 */
ptrace will fiddle with TASK_PT_PSW, setting the R bit and clearing the I bit to enable the recovery counter - which will start counting down at the mtsm instruction above, and reach zero on the user-space instruction, so we'll trap after executing one user-space instruction. The task-switch nonsense is to handle the case where we page-fault on the instruction and switch to another task also doing single-stepping. You want to ensure cr0 is zero when we finally get back to the original task.
Now it might turn out that having extra instructions in the syscall path is worse than extra code and memory accesses on task switch. If that turns out to be true, then you'll probably get your multi-step ptrace. :-)
Alan Modra
Signature · 1 line
Linuxcare. Support for the Revolution.
Re:
Quoted 5 lines
Because you would then need to save and restore cr0 on task switches (or only allow one task to be single-stepped at a time). That's four instructions and two extra memory accesses per task switch. Which might not seem very much, but at some point somebody will no doubt start caring about pa-linux performance.
And it still won't seem like much, then!
Non-memory-access instructions are cheap. An extra memory reference (from something probably already in cache) and two extra instructions would probably cost less than an hour per CPU over the next 10 *years*, assuming 10 years of 1000 task switches per second on a slow 100 MHz CPU.
Of course, at the cost of an extra non-memory-referencing instruction or so, you could say "at switch-to-task time: if PSW R-bit set, then load the saved CR0 from memory and move it to CR0", saving one memory reference 99.99999% of the time, resuling in an average of only one memory reference per task switch normally.
I haven't look at interrupt handling / system calls closely, but I hope there aren't other false savings. (E.g., failure to save/restore the PID check flag ... sure, user processes *now* probably never have pid checking disabled, but that's a very useful feature to have available (with proper security controls, of course).)
(Yes, I'm one of the very few who use that feature on MPE/iX ... carefully, of course :)
Signature · 2 lines
Stan Sieler [email protected] www.allegro.com/sieler/wanted/index.html www.sieler.com