Runtime Environment
The Layered Structure of Computer Systems
Modern computer systems use a layered architecture. From the bottom up, the layers are:
What each layer does:
- Hardware layer: physical devices such as the CPU, memory, and peripherals
- Hardware abstraction layer: device drivers that hide hardware details
- Operating system layer: the kernel, which manages hardware resources
- Application layer: user programs that provide concrete functionality
The Concept of Privilege Levels
Modern processors usually support several privilege levels, which are used to distinguish the permissions of different programs:
1. Privilege Level Classification
Intel x86 architecture:
- Ring 0: kernel mode, the highest privilege
- Ring 1-2: system service rings (rarely used)
- Ring 3: user mode, the lowest privilege
ARM architecture:
- EL0: user mode
- EL1: kernel mode
- EL2: virtualization level
- EL3: secure monitor level
2. Kernel Mode
Characteristics:
- The highest privilege level
- Able to execute every instruction
- Able to access hardware directly
- Able to modify system state
Permissions:
- Access every memory address
- Execute privileged instructions
- Control interrupts and exceptions
- Manage hardware devices
Typical operations:
- Memory management
- Process scheduling
- Device drivers
- System call handling
3. User Mode
Characteristics:
- A lower privilege level
- Able to execute non-privileged instructions only
- Unable to access hardware directly
- Protected by the system
Restrictions:
- May access user-space memory only
- May not execute privileged instructions
- May not operate hardware directly
- Is monitored by the operating system
Typical operations:
- Application execution
- User data processing
- Calling system services
- File operations
The Mode Switching Mechanism
1. Switching from User Mode to Kernel Mode
Trigger conditions:
- System calls
- Interrupts
- Exceptions
- Privileged instructions
Switching process:
Detailed steps:
- Save the user-mode register state
- Switch to the kernel stack
- Set the kernel-mode flag
- Jump to the kernel code
2. Switching from Kernel Mode to User Mode
Trigger conditions:
- Returning from a system call
- Finishing interrupt handling
- Finishing exception handling
Switching process:
Detailed steps:
- Restore the user-mode register state
- Switch to the user stack
- Clear the kernel-mode flag
- Jump to the user code
The Memory Protection Mechanism
1. Address Space Isolation
User space:
- The memory region accessible to applications
- Protected by the operating system
- Private to each process
Kernel space:
- The memory region used by the operating system kernel
- Shared by all processes
- Protected by the hardware
2. The Memory Management Unit (MMU)
Functions:
- Translating virtual addresses into physical addresses
- Controlling memory access permissions
- Protecting memory
How it works:
Interrupts
1. The Concept of an Interrupt
Definition: a temporary suspension of the program execution flow caused by a hardware device or an external event.
Characteristics:
- Occur asynchronously
- Can be masked
- Have priority levels
- Can be nested
2. Classification of Interrupts
By source:
- Hardware interrupts: generated by hardware devices
- Software interrupts: generated by program instructions
By maskability:
- Maskable interrupts: the CPU may ignore them
- Non-maskable interrupts: the CPU must handle them immediately
By priority:
- High-priority interrupts: critical system events
- Low-priority interrupts: ordinary device events
3. Common Types of Interrupts
Timer interrupts:
- Generated by the system clock
- Used for time slice rotation
- Occur periodically
I/O interrupts:
- Generated by I/O devices
- Signal that an I/O operation has completed
- Provide asynchronous notification
Hardware fault interrupts:
- Generated by hardware errors
- Must be handled immediately
- Form part of the system protection mechanism
4. The Interrupt Handling Process
Detailed steps:
- Interrupt detection: the CPU notices the interrupt signal
- Context saving: the current program state is saved
- Mode switch: the CPU switches to kernel mode
- Interrupt handling: the matching interrupt service routine runs
- Context restoring: the state of the interrupted program is restored
- Program return: the interrupted program resumes execution
Exceptions
1. The Concept of an Exception
Definition: an interruption of the program execution flow caused by an error or a special situation during program execution.
Characteristics:
- Occur synchronously
- Cannot be masked
- Are related to program execution
- Must be handled immediately
2. Classification of Exceptions
By severity:
- Faults: recoverable errors
- Traps: deliberately raised exceptions
- Aborts: unrecoverable errors
By source:
- Program exceptions: caused by errors in the program
- System exceptions: caused by the state of the system
3. Common Types of Exceptions
Page faults:
- Accessing a memory page that does not exist
- Accessing a memory page without sufficient permission
- Trigger virtual memory management
Divide-by-zero errors:
- A division whose divisor is zero
- A program error
- Require exception handling
Illegal instructions:
- Executing an instruction that does not exist
- Executing a privileged instruction
- Program errors
Stack overflow:
- Running out of stack space
- Recursion that is too deep
- Buffer overflow
4. The Exception Handling Process
Possible outcomes:
- Resume execution: continue once the error is repaired
- Terminate the program: unrecoverable, so the program is killed
- Reboot the system: a severe error forces a restart
The Difference Between Interrupts and Exceptions
| Feature | Interrupt | Exception |
|---|---|---|
| When it occurs | Asynchronous | Synchronous |
| Maskability | Maskable | Not maskable |
| Source | Hardware device | Program execution |
| Handling | Interrupt service routine | Exception handler |
| Priority | Has priority levels | No priority levels |
The Concept of a System Call
1. Definition
A system call is the interface the operating system offers to applications. It lets a user program ask the operating system kernel for a service.
2. Purpose
- Resource management: allocate and release system resources
- Process control: create, terminate, and wait for processes
- File operations: create, read, write, and delete files
- Device control: open, close, and control devices
- Communication services: inter-process communication and network communication
3. Characteristics of System Calls
- Privileged operations: they require a switch to kernel mode
- Standardized interface: they offer one uniform calling convention
- Security: they are protected by the operating system
- Portability: the interface stays the same across platforms
How System Calls Are Implemented
1. The Call Sequence
Detailed steps:
- Argument preparation: the user program prepares the system call arguments
- Calling the interface: control enters the kernel through the system call interface
- Mode switch: the CPU switches from user mode to kernel mode
- Service execution: the kernel runs the matching service
- Result return: the result is handed back to the user program
- Mode restoration: the CPU switches from kernel mode back to user mode
2. System Call Numbers
Definition: every system call has a unique number that identifies it.
Example (Linux x86-64):
#define SYS_read 0
#define SYS_write 1
#define SYS_open 2
#define SYS_close 3
#define SYS_fork 57
#define SYS_execve 59
3. Parameter Passing
Register passing:
- The system call number goes into a designated register
- The arguments go into the other registers in order
- The return value goes into a designated register
Stack passing:
- The arguments are pushed onto the stack
- The system call number goes to the top of the stack
- The return value is placed on the stack
Common Categories of System Calls
1. Process Management
Process creation:
pid_t fork(void); // Create a child process
int execve(const char *pathname, char *const argv[], char *const envp[]); // Execute a program
Process control:
pid_t wait(int *status); // Wait for a child process
int exit(int status); // Terminate the process
Process information:
pid_t getpid(void); // Get the process ID
pid_t getppid(void); // Get the parent process ID
2. File Operations
File control:
int open(const char *pathname, int flags); // Open a file
int close(int fd); // Close a file
File reading and writing:
ssize_t read(int fd, void *buf, size_t count); // Read from a file
ssize_t write(int fd, const void *buf, size_t count); // Write to a file
File positioning:
off_t lseek(int fd, off_t offset, int whence); // Reposition the file offset
3. Memory Management
Memory allocation:
void *brk(const void *addr); // Resize the data segment
void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset); // Map memory
Memory protection:
int mprotect(void *addr, size_t len, int prot); // Set memory protection
4. Inter-Process Communication
Pipes:
int pipe(int pipefd[2]); // Create a pipe
Signals:
int kill(pid_t pid, int sig); // Send a signal
sighandler_t signal(int signum, sighandler_t handler); // Install a signal handler
Shared memory:
int shmget(key_t key, size_t size, int shmflg); // Create a shared memory segment
void *shmat(int shmid, const void *shmaddr, int shmflg); // Attach a shared memory segment
The Security of System Calls
1. Parameter Validation
Bounds checking:
- Check that the arguments are valid
- Verify that the memory addresses are legal
- Check that the permissions are sufficient
Example:
// Check that the file descriptor is valid
if (fd < 0 || fd >= MAX_FILES) {
return -EBADF;
}
// Check that the memory address lies in user space
if (!access_ok(VERIFY_READ, buf, count)) {
return -EFAULT;
}
2. Permission Checks
User permissions:
- Check whether the user holds the required permissions
- Verify file access permissions
- Check system resource limits
Example:
// Check the file access permissions
if (!may_access(path, mode)) {
return -EACCES;
}
3. Resource Limits
System resources:
- Check whether the system has enough resources
- Prevent resource exhaustion
- Enforce resource quotas
Example:
// Check the memory limit
if (current->mm->total_vm > rlimit(RLIMIT_AS)) {
return -ENOMEM;
}
| Term | Abbreviation | Explanation |
|---|---|---|
| Kernel Mode | - | Privileged execution mode |
| User Mode | - | Non-privileged execution mode |
| Privileged Instruction | - | An instruction that may only run in kernel mode |
| Non-Privileged Instruction | - | An instruction that user mode may execute |
| Interrupt | - | An event triggered by an external device |
| Exception | - | An exceptional event during program execution |
| Trap | - | A deliberately raised exception |
| Fault | - | A recoverable exception |
| Abort | - | An unrecoverable exception |
| Interrupt Vector Table | IVT | Stores the addresses of interrupt handlers |
| Interrupt Descriptor Table | IDT | The x86 interrupt vector table |
| System Call | - | A request from an application for a kernel service |
| System Call Number | - | The number that identifies a system call |
| System Call Table | - | Stores the system call handler functions |
| Software Interrupt | - | An interrupt triggered by software |
| Signal | - | The UNIX inter-process communication mechanism |
| Process Context | - | The execution environment of a process |
| Context Switch | - | Switching the context of a process |
| Mode Switch | - | A switch between kernel mode and user mode |
| Clock Interrupt | - | An interrupt raised by the timer |
| Interrupt Mask | - | The mechanism that forbids certain interrupts |
| Interrupt Priority | - | The ordering of interrupts by priority |
