I am reading Operating Systems book by Galvin. Galvin explains, what are kernel & user modes, instruction privileges given for both modes & also about mode-bit. But I am interested to know how mode changes from one to another. Basically I want to solve following question :
A CPU has 2 modes, privileged and non-privileged. In order to change the mode from previledged to non-previledged
a) A hardware interrupt is needed
b) A software interrupt is needed.
c) A privileged instruction is needed.
d) A non-privileged instruction is need.
From what I understand,
from user mode to kernel mode - Hardware Interrupt is needed [like in Disk I/O]. Now, in case user program tires to access an memory which is beyond its permissible range, a trap occurs, which is basically a software interrupt which will be handled by OS. Now, in user mode we cannot execute any privileged instructions. So, a non-privileged instruction such as I/O request can change user to kernel mode. So I think, to change
from non-privileged(user) to privileged(kernel) - H/W Interrupt, S/W Interrupt & non-Privileged instruction will do.
Now coming to, kernel to user mode. The OS can change kernel to user mode. So, it will simply run a privileged instruction to change from kernel to user mode. It does not need to generate either H/w or S/w interrupt. So I conclude, to change
from previledged to non-previledged - a privileged instruction will do.
Am I right ?
Also when executing in kernel mode, all interrupts will be disabled right ? So answer cannot be (a) or (b). Also, since OS is basically a software, it cannot generate H/W interrupts.
Also, since OS itself handles interrupts, it does not make sense to me why has to generate a interrupt (& service it) to change from kernel to user mode.
Please let me know if I am wrong anywhere. Any help regarding this is appreciated.