0

I read that RFE (return from exception) is a privileged instruction which helps to transition from kernel to user mode.

As it is a privileged instruction, a software interrupt(trap) will be generated during it's execution.

I want to know whether all other interrupts or interrupt enable flag is disabled during it's execution so that kernel doesn't get interrupted in between. Are interrupts disabled during software interrupt?

Zephyr
  • 183

2 Answers2

2

Disclaimer: I have only ever dealt with OS-level code on a Motorola 68000. This answer is written from the perspective of a similar, two-ring style system, but the concepts should translate fairly well to modern multi-ring systems.


On a system with two privilege levels, call them user mode and system mode, application program would run in user mode, while the kernel runs in system mode. If a user-mode application tries to run a privileged instruction, a trap occurs so the kernel can decide what to do about the situation.

On the other hand, the kernel already has the privileges to execute whatever instructions it wants to, so no trap is executed.

On the MC680x0 specifically, any trap handler is run in privileged mode by default. So any trap handler can use "return from exception" with no repercussions, and without worrying about generating another trap. And code in user mode shouldn't use such an instruction, as there is no exception from which to return.

In short, the question arises from a false premise; no trap is generated when using "return from exception" to switch to user mode.

Fox
  • 8,193
  • 1
    This is valid for (most, if not all) other architectures too: return instructions (whether generic return, return from interrupt, or more specific instructions like SYSEXIT on x86) can only return to code running with the same privilege or lower. (Well, I imagine if you tried returning to higher privilege code by tweaking the stack in a segmented architecture, you’d cause a trap, but the general “return to user mode” case doesn’t.) – Stephen Kitt Dec 16 '17 at 22:15
  • Come to think of it, you could trap on return by returning to an address in a page not present, but that’s not specific to returning from kernel mode, and not particularly relevant here. – Stephen Kitt Dec 16 '17 at 22:26
  • Thanks for the answer. In general are other interrupts disabled when kernel runs or processes a trap or a interrupt? – Zephyr Dec 17 '17 at 04:47
1

How transitions between processor modes work depends on the processor architecture. However, you can indeed expect that there is an instruction to switch from kernel mode to user mode; call it RFE if you like, but its essence is more jump-and-change-mode than return-and-change-mode. It's also a misleading to think of this as a “software interrupt” since it won't execute an interrupt handle.

It doesn't really make sense to ask whether interrupts are disabled because this is a single instruction. An interrupt at that point wouldn't cause the kernel to “get interrupted” since the kernel has already decided to stop running. The kernel would always enable interrupts before jumping back to user mode since user mode should never run with interrupts disabled — that would be a denial of service. If the processor needs to spend multiple cycles to execute the RFE instruction, it may be the case that the processor won't check interrupts during some of these cycles, but that's a detail of the processor architecture that the software can't observe (except through fine timing measurements).

  • Suppose kernel is executing a system call like write system call. Can interrupts from any other devices or any other software interrupt, interrupt the kernel during the execution of write system call? – Zephyr Dec 17 '17 at 04:52