Privileged and Non-Privileged Instructions in Operating System
Introduction
It is highly likely that a novice programmer will occasionally execute sensitive code, which could cause the operating system to malfunction and fail. Therefore, we must be able to distinguish between operating system code execution and user system code execution in order to guarantee the operating system's proper operation. The operating system has Dual mode for this purpose. Let's take a quick look at dual-mode in OS before proceeding to the privileged and non-privileged instructions.
Dual Mode in OS
Dual mode is a concept in operating systems that involves the use of two distinct modes of operation or privilege levels: user mode and kernel mode.
- User mode is a mode of operation where the operating system restricts the access of running programs to system resources such as memory, processor, and I/O devices. In user mode, programs can only access resources in a controlled and limited way. This is done to prevent user programs from accidentally or intentionally damaging the system or other running programs. User mode is also known as unprivileged mode.
- Kernel mode, on the other hand, is a mode of operation where the operating system has full control and unrestricted access to system resources. This mode is used to execute privileged instructions and to manage system resources, such as memory allocation and I/O operations. Kernel mode is also known as privileged mode.
The dual-mode concept is used to ensure the safety and security of the operating system and the programs running on it. By running in user mode, programs are prevented from interfering with the operating system or other running programs, and by running in kernel mode, the operating system can manage system resources and perform privileged operations. When a program needs to perform a privileged operation or access a system resource that is restricted in user mode, it must request the operating system to switch to kernel mode temporarily to execute the operation. This is done through a system call, which is a mechanism provided by the operating system to allow user programs to request privileged services.
Life Cycle of Instruction
The life cycle of an instruction in an operating system can be broken down into four main stages: fetch, decode, execute, and store. These stages are performed in both user mode and kernel mode, depending on the type of instruction being executed.
- Fetch: The first stage is fetching the instruction from memory. The processor reads the instruction from memory and stores it in a special register called the instruction register (IR). The location of the instruction is determined by the program counter (PC), which holds the memory address of the next instruction to be executed.
- Decode: In this stage, the processor decodes the instruction in the IR to determine what operation needs to be performed. This involves interpreting the opcode and any operands that are included in the instruction.
- Execute: The execute stage is where the operation specified by the instruction is performed. This can involve performing arithmetic or logical operations on data stored in memory or registers, or it can involve accessing I/O devices or interacting with the operating system through system calls.
- Store: The final stage is storing the result of the operation back to memory or registers. This allows subsequent instructions to access the result of the operation.
When a user application is started, the control is switched to user mode, and instructions are executed in this mode. However, when a system call, interrupt, or trap occurs, the control is switched to kernel mode to allow the operating system to perform privileged operations, such as accessing hardware or modifying system settings. Once the privileged operation is complete, control is returned to the instruction following the system call, and instructions continue to be executed in user mode. This process of switching between user mode and kernel mode during the execution of instructions allows for the safe and efficient operation of the operating system.
Types of instructions
An instruction is a command or order that an application gives to the system. There are two types of instructions in the operating system: privileged instructions and non-privileged instructions. Prior to that, we'll talk about privileged instructions.
What are Privileged Instructions?
The instructions that can only be executed in kernel mode are referred to as privileged instructions.
A privileged instruction will be ignored and treated as an illegal instruction if it is attempted to be executed in user mode. The hardware keeps it in the operating system.
Before transferring control to any user application, the operating system is responsible for ensuring that the Timer is set to interrupt. Consequently, in the event that the timer is interrupted, the operating system can regain control.
To guarantee proper operation, the operating system makes use of privileged instruction.
- I/O instructions
- Context switching
- Clear memory
- Set the CPU's timer
- Halt instructions
- Interrupt management
- Modify entries in the Device-status table
What are Non-Privileged Instructions?
Instructions that are only carried out in user mode are referred to as non-privileged instructions.
Examples of non-privileged instructions include the trap instruction, reading system time, reading processor status, sending output to the printer, and performing arithmetic operations. The layered organization of the operating system will now be depicted in the figure.
There are two parts to the computer system as a whole: software and hardware. Using the instruction set, the software and hardware interact with one another.
There are two types of the instruction set:
Special instructions: Because of the potential for misuse, these instructions must be followed with care.
Non-advantaged guidelines: These are standard directions.
Based on the instructions, there are two categories for the software component:
- Kernel: only able to carry out privileged instructions.
- Application: can carry out instructions, both privileged and non-privileged.
Differences between Privileged and Non-Privileged instructions in an Operating System:
Feature | Privileged Instructions | Non-Privileged Instructions |
Execution | Can only be executed in kernel or supervisor mode | Can be executed in user mode |
Access | Have access to all system resources, including hardware | Limited access to system resources, with some restrictions |
Purpose | Used to perform privileged operations, such as memory management and I/O control. | Used to perform basic operations, such as arithmetic and logical operations. |
Security | Can potentially compromise system security if executed incorrectly or maliciously | Generally pose a lower security risk than privileged instructions |
Examples | Interrupt and trap handling instructions, memory management instructions, I/O instructions | Arithmetic instructions, logical instructions, data transfer instructions, control instructions |