I read in a tutorial that some 15 instructions in x86 architecture is not allowed to be used in user mode.
I know there is something called code segment register which keeps track of current previlege level
My question is
a)does cpu, before executing every instructions has to check current previlege level it is running?
b)What actually happens if there is an instruction which cannot be used in user mode present in a user program?How CPU comes to know about this before executing?
The CPU does check CPL, RPL and things alike before executing certain instructions or certain parts of them (there are a number of instructions with very complex logic and the set of checks to perform depends on a number of conditions).
If an instruction is not allowed to execute, the CPU generates an exception event, which is then dispatched to its handler. Exception handlers are similar to interrupt handlers in nature and are defined by the OS. So, when the OS gets to handle an exception that it cannot anyhow correct, it terminates the program that's caused the exception.
An example of a "correctable" exception is page faults for virtual memory that's been offloaded to the disk. The OS loads the code/data that the application is trying to use back from the disk into the memory.