2

I'm not an expert, but just a hobbyist. I was playing with 68000 architecture in the past and I've been always thinking of its TRAP instruction. This instruction is always described as a "bridge" to an OS (in some systems however it's not used in this regard, but that's a different story). How this is achieved? TRAP itself is a privileged instruction, so how this OS invoking mechanism works in user mode? My guess is that the privilege violation exception is triggered and the exception handler checks what particular instruction has caused the exception. If it's a TRAP instruction then the instruction is simply executed (maybe TRAP's operand i.e. TRAP vector number is checked as well), of course now in the supervisor mode. Am I right?

5
  • Yes, you're pretty much correct - this was the exact mechanism that the original Mac OS (back in 1984) used for toolbox calls.
    – Paul R
    Mar 17, 2014 at 13:20
  • So why the designers made this instruction privileged? Wouldn't it be simpler just to make it unprivileged? Mar 17, 2014 at 13:33
  • I don't know - my guess would be that this allows for different behaviour when TRAP is executed from user space or kernel space. Otherwise there would be no differentiation in behaviour. Since this was designed > 30 years ago I imagine the original rationale might be hard to find out now.
    – Paul R
    Mar 17, 2014 at 13:52
  • @Paul R, it makes sense, thank you for your explanations. Mar 17, 2014 at 14:01
  • @PaulR: The Mac OS used opcodes $A000-$AFFF, which all invoke the same handler. That handler then fetches the stacked program-counter value, fetches a word there, and uses that to fetch a vector from a look-up table.
    – supercat
    Nov 30, 2015 at 21:54

1 Answer 1

6

The TRAP instruction is not privileged, you can call it from either user mode or supervisor mode.

It's the TRAP instruction itself that will force the CPU to supervisor mode, and then depending of the #xx number you used will jump to any of the 16 possible callbacks from the memory area $80 to $BC.

TRAP also pushes to the stack the PC and SR values, so when the last function call returns it goes back to whatever mode was setup before you called TRAP.

1
  • Right. I was misled by some book I read about 20 years ago. By the way, in that book some other instructions were made (wrong) privileged, e.g. chk. That simply didn't make much sense to me. Now suddenly it stabbed my mind to clarify this. I've checked the Motorola online docs and of course you're right. Thank you. Apr 19, 2014 at 11:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.