02 - Architectural Support for Operating Systems

Class: CSCE-313


Notes:

Place of the Operative System

Pasted image 20260121134852.png|500

Function of an Operative System

A process (simplified)

A process is an instance of a running program

State of a process

From program to process

Pasted image 20260121135539.png|500

The Process, Refined

An executing program with restricted rights

Pasted image 20260121140124.png|500

User vs Kernel

Application/User Code (Untrusted)

Kernel Code (Trusted)

But run on the same machine!

Notes:

Hardware must support

  1. Privileged Instructions
    • Unsafe instructions cannot be executed in user mode
    • There are some instructions that you can only do when running in privileged mode
    • To do this we need to have in our processor a notion of what can be done and what cannot be node
      • For example a process cannot change its own address space, it has to ask the OS to change it
  2. Memory Isolation
    • Memory accesses outside a process’s address space prohibited
      • A process cannot break out of its virtual addresses container
  3. Interrupts
    • Ensure kernel can regain control from running process
    • We need some way to interrupt the execution of the CPU, because if not, the sharing of resources would become unfair
  4. Safe Transfers
    • Correctly transfer control from user-mode to kernel-mode and back
    • We need some way to correctly and safely transfer an unprivileged work to a privileged work and vice-versa.

1. Privilege levels differentiate instruction sets

Pasted image 20260121141157.png|500

2. VM can give the illusion of an entire contiguous address space

3. Interrupts wrest control from applications

  1. Hardware Interrupts. All interrupts are guaranteed to be taken on an instruction boundary. [Vol 3A, 6-6]
    1. Asynchronous
    2. External devices such as timers
  2. Software Interrupts (INT 0x80, syscall)
    1. Synchronous—explicit instruction
    2. System calls
  3. Exceptions—div/0,…In some cases, %rip points to fault insn.
    1. Protection violations, Page faults,…

Notes:

Interrupts and exceptions result in control change

An interrupt/exception results in the transfer of control to the OS in response to some event

Pasted image 20260121143344.png|251
Pasted image 20260121143401.png|500
Pasted image 20260121143418.png|500

Interrupt & Exception handlers

An interrupt handler is code that services an interrupt, then resumes the
interrupted program.

Notes:

Synchronous "interrupts"

Synchronous exceptions can be triggered by executing an instruction.

From within the user application there are 3 types of synchronous exceptions:

  1. Faults,
    • Usually occurs before the instruction completes and is restartable.
    • Page Fault
  2. Aborts
    • A severe, unrecoverable exception (e.g., hardware failure or double fault), not restartable. ECC checksum failure.
  3. Traps (INT 3) or System Calls (SYSCALL)

Examples: Exception in Intel Processors

See https://wiki.osdev.org/exceptions

Pasted image 20260121143947.png|500

Example 1: page fault

int a[1000];
main () {
	a[500] = 13;
}

Pasted image 20260123140511.png|450

Notes:

Example 2: illegal memory reference

Illegal Memory Reference

int a[1000];
main () {
	a[500] = 13;
}

Pasted image 20260123140809.png|450

Notes:

Example 3: Abort

Aborts are severe and unrecoverable errors.

Pasted image 20260123140914.png|450

...

Traps or system calls

Pasted image 20260123141019.png|500

System calls: example

#include <unistd.h>
int main(void) {
	write(1, "Hello, world\n", 13);
	return 0;
}
WRITE(2) Linux Programmer's Manual WRITE(2)

NAME
	write - write to a file descriptor

SYNOPSIS
	#include <unistd.h>
	ssize_t write(int fd, const void *buf, size_t count);

DESCRIPTION
	write() writes up to count bytes from the buffer starting at buf to the file referred to by the file descriptor fd.

stdin, stdout, stderr

A process usually has three file descriptors

secho
stdin> Howdy!
stdout: Howdy!
stderr: Howdy!
secho > /dev/null
stdin> Howdy!
stderr: Howdy!
secho > /dev/null 2>&1
Howdy!

Notes:

System calls: an example in assembly

main:
	pushq %rbp
	movq %rsp, %rbp
	movl $13, %edx
	leaq .LC0(%rip), %rax
	movq %rax, %rsi
	movl $1, %edi
	call write@PLT
	movl $0, %eax
	popq %rbp
	ret

glibc (GNU C Library) is the fundamental C standard library for GNU/Linux and other Unix-like systems, providing essential functions for nearly all applications, such as memory allocation, file I/O, string handling, and system calls, acting as a vital bridge between user programs and the operating system kernel, implementing standards like POSIX and ISO C

System call invocation

Pasted image 20260123143317.png|300

Control Flow in System Calls

Example: file open

Pasted image 20260123143422.png|500

Notes:

System calls

...

Types of System Calls

Process Control File Management Device Management
• load
• execute
• end, abort
• create process
• terminate process
• get/set process attributes
• wait for time, wait event, signal event
• allocate, free memory
• create file, delete file
• open, close
• read, write, reposition
• get/set file attributes
• request device, release device
• read, write, reposition
• get/set device attributes
• logically attach or detach devices
Information Maintenance Communication
• get/set time or date
• get/set system data
• get/set process, file, or device attributes
• create, delete communication connection
• send, receive messages
• transfer status information
• attach or detach remote devices