Operating systems
Mentoring Operating System (MentOS)
Fundamental Concepts
Lecture 3.1
Florenc Demrozi
[Link]@[Link]
University of Verona
Department of Computer Science
2021/2022
Operating systems Mentoring Operating System (MentOS) 2021/2022 1 / 27
Table of Contents
1 Mentoring Operating System (MentOS)
2 Fundamental concepts
Central Processing Unit (CPU)
Programmable Interrupt Controller (PIC)
Memory organization
3 Kernel doubly-linked list
Operating systems Mentoring Operating System (MentOS) 2021/2022 2 / 27
Mentoring Operating System (MentOS)
Operating systems Mentoring Operating System (MentOS) 2021/2022 3 / 27
MentOS
What...
MentOS (Mentoring Operating System) is an open source educational
operating system. MentOS can be freely downloaded from a public github
repository: [Link]/MentOS/
Goal...
The goal of MentOS is to provide a project environment that is realistic
enough to show how a real Operating System work, yet simple enough
that students can understand and modify it in significant ways.
Operating systems Mentoring Operating System (MentOS) 2021/2022 4 / 27
MentOS
Who?
Enrico Fraccaroli Alessandro Danese
2019
Mirco De Marchi Luigi Capogrosso
Bachelor in Comp. Science
2020
Andrea Cracco
Bachelor in Bioinformatics
Linda Sacchetto Marco Berti
Master in Embedded Systems
2021
Daniele Nicoletti Filippo Ziche
Bachelor in Comp. Science
Operating systems Mentoring Operating System (MentOS) 2021/2022 5 / 27
MentOS
Why...
There are so many operating systems, why did we write MentOS?
It is true, there are a lot of education operating system, BUT how many of
them follow the guideline defined by Linux?
MentOS aims to have the same Linux’s data structures and algorithms. It
has a well-documented source code, and you can compile it on your laptop
in a few seconds!
If you are a beginner in Operating-System developing, perhaps MentOS is
the right operating system to start with.
Operating systems Mentoring Operating System (MentOS) 2021/2022 6 / 27
Fundamental concepts
Operating systems Mentoring Operating System (MentOS) 2021/2022 7 / 27
The big picture
Central Processing Unit
Programmable Interrupt Controller
Random Access Memory
RAM
PIC
CPU
BUS (Address, Data, Control)
Operating systems Mentoring Operating System (MentOS) 2021/2022 8 / 27
Fundamental concepts
Central Processing Unit (CPU)
Operating systems Mentoring Operating System (MentOS) 2021/2022 9 / 27
CPU registers
There are three types of registers:
general-purpose data registers;
segment registers;
status control registers.
Segment registers
General-purpose registers Status and control registers
(flat memory model)
31 15 8 7 0 32-bit 16-bit 15 0 31 0
AH AL EAX AX CS EFLAGS
BH BL EBX BX DS EIP
CH CL EXC XC SS
DH DL EDX DX ES
ESI FS
EDI GS
EBP
ESP
Operating systems Mentoring Operating System (MentOS) 2021/2022 10 / 27
General-purpose registers
The eight 32-bit general-purpose registers are used to hold operands for
logical and arithmetic operations, operands for address calculations and
memory pointers. The following shows what they are used for:
EAX: Accumulator for operands and results data;
EBX: Pointer to data in the DS segment;
ECX: Counter for loop operations;
EDX: I/O pointer;
ESI: Pointer to data in the segment pointed to by the DS register;
EDI: Pointer to data in the segment pointed to by the ES register;
EBP: Pointer to data on the stack (in the SS segment);
ESP: Stack pointer (in the SS segment).
Operating systems Mentoring Operating System (MentOS) 2021/2022 11 / 27
Status and control registers
The two 32-bit status control registers are used for:
EIP: Instruction pointer (also known as “program counter”);
EFLAGS: Mantain group of status, control, system flags.
Table with some of the flags:
Bit Description Category Bit Description Category
0 Carry flag Status 11 Overflow flag Status
2 Parity flag Status 12-13 Privilege level System
4 Adjust flag Status 16 Resume flag System
6 Zero flag Status 17 Virtual 8086 mode System
7 Sign flag Status 18 Alignment check System
8 Trap flag Control 19 Virtual interrupt flag System
9 Interrupt enable flag Control 20 Virtual interrupt pending System
10 Direction flag Control 21 Able to use CPUID instruction System
Not listed bit are reserved. What is the privilege level of a CPU?
Operating systems Mentoring Operating System (MentOS) 2021/2022 12 / 27
Privilege levels
There are four privilege levels, numbered 0
Ring 3 (most privileged) to 3 (least privileged).
User Mode
Applications
At any given time, an x86 CPU is running in
Ring 0
Kernel Mode a specific privilege level, which determines
Operating System
what code can and cannot execute.
Which of the following operations can
process do when the CPU is in user mode?
1 open a file;
2 print on screen;
Most modern x86 kernels use 3 allocate memory.
only two privilege levels, 0
and 3.
Operating systems Mentoring Operating System (MentOS) 2021/2022 13 / 27
Context switch (Overview)
Every time CPU changes privilege level, a context switch occurs!
Example of events making CPU change execution mode:
A mouse click, type of a character on the keyboard, a system call...
User User User
User mode
Kernel mode
Excp Intr
Time
How many times does the CPU change execution mode when a user
presses a key of the keyboard?
Operating systems Mentoring Operating System (MentOS) 2021/2022 14 / 27
Fundamental concepts
Programmable Interrupt Controller (PIC)
Operating systems Mentoring Operating System (MentOS) 2021/2022 15 / 27
Programmable Interrupt Controller (PIC)
INTR IRQ_0 A programmable interrupt controller is a
CPU
IRQ#
IRQ_1 components combining several interrupt
ACK
requests onto one or more CPU lines.
8259
… Example of interrupt request:
PIC IRQ_12
a key on the keyboard is pressed
…
IRQ_14
PIC rises INTR line and presents IRQ 1
IRQ_15
to CPU
CPU jumps into Kernel mode to handle
16 IRQ lines, numbered
the interrupt request
from 0 (highest priority)
CPU reads from the keyboard the key
to 15 (lowest priority) pressed
Why do we have a timer in CPU sends back ACK to notify that
IRQ 0? IRQ 1 was handled
CPU jumps back to User mode
Operating systems Mentoring Operating System (MentOS) 2021/2022 16 / 27
IRQ 0, Timer!
The timer is a hardware component aside the CPU. At a fixed frequency,
the timer rises a signal connected to the IRQ 0 of PIC.
User User User User
User mode
Kernel mode
Kernel Kernel Kernel Kernel
Time
Linux fixes the timer frequency to 100 Hz. The CPU runs a user process
for maximum 10 milliseconds, afterwards Kernel has back the control of
CPU.
Operating systems Mentoring Operating System (MentOS) 2021/2022 17 / 27
Fundamental concepts
Memory organization
Operating systems Mentoring Operating System (MentOS) 2021/2022 18 / 27
Memory organization (32-bit system)
The Kernel applies Virtual Memory to maps
0XFFFFFFFFF
virtual addresses to physical addresses.
Kernel addresses RAM is virtually split in Kernel space (1GB)
and User space (3GB).
CONFIG_PAGE_OFFSET
(default 0xC0000000)
CPU in Ring 0 has visibility of the whole
RAM.
User addresses CPU in Ring 3 has visibility of User space
only.
0x00000000
Figure: Kernel and User space.
Operating systems Mentoring Operating System (MentOS) 2021/2022 19 / 27
Kernel doubly-linked list
Operating systems Mentoring Operating System (MentOS) 2021/2022 20 / 27
Circular, doubly-linked list (1/7)
Introduction
Operating system kernels, like many other programs, often need to
maintain lists of data structures. To reduce the amount of duplicated
code, the kernel developers have created a standard implementation of
circular, doubly-linked lists.
Pros:
Safer/quicker than own ad-hoc implementation.
Comes with several ready functions.
Cons:
Pointer manipulation can be tricky.
Operating systems Mentoring Operating System (MentOS) 2021/2022 21 / 27
Circular, doubly-linked list (2/7)
Definition
To use the list mechanism kernel developers defined the list head data
structure as follow:
typedef struct list_head {
struct list_head *next, *prev;
} list_head_t;
A list head represent a node of a list!
Operating systems Mentoring Operating System (MentOS) 2021/2022 22 / 27
Circular, doubly-linked list (3/7)
Usage
To use the Linux list facility, we need only embed a list head inside the
structures that make up the list.
struct mystruct {
//...
list_head_t list;
//...
};
The instances of mystruct can now be linked to create a doubly-linked list!
mystruct { mystruct { mystruct {
// … // … // …
list: list: list:
next next next
prev prev prev
// … // … // …
} } }
Operating systems Mentoring Operating System (MentOS) 2021/2022 23 / 27
Circular, doubly-linked list (4/7)
Mechanism in Detail
The head of the list must be a standalone list head t structure.
list_head_t list;
struct mystruct {
//...
list_head_t list;
//...
};
mystruct { mystruct { mystruct {
// … // … // …
list { list: list: list:
next next next next
prev prev prev prev
} // … // … // …
} } }
head list
The head is always present in a circular, doubly-linked list!
If a list is empty, then only its head exists!
Operating systems Mentoring Operating System (MentOS) 2021/2022 24 / 27
Circular, double-linked list (5/7)
Support functions (1/3)
Support functions to use with a circular, doubly-linked list.
list head empty(list head t *head):
Returns a nonzero value if the given list is empty.
list head add(list head t *new, list head t *listnode):
This function adds the new entry immediately after the listnode.
list head add tail(list head t *new, list head t *listnode):
This function adds the new entry immediately before the listnode.
list head del(list head t *entry):
The given entry is removed from the list.
Operating systems Mentoring Operating System (MentOS) 2021/2022 25 / 27
Circular, double-linked list (6/7)
Support functions (2/3)
list entry(list head t *ptr, type of struct, field name):
Returns the struct embedding a list head. In detail:
ptr is a pointer to a list head t;
type of struct is the type name of the struct embedding a
list head t;
field name is the name of the pointed list head t within the struct.
// Example showing how to get the first mystruct from a list
list_head_t *listptr = [Link];
struct mystruct *item = list_entry(listptr, struct mystruct, list);
*casts operators omitted
mystruct {
// … offset of the variable list
list { //… from the start of mystruct
next list: listptr
prev // …
} } Retuned address:
listptr + offsetof(struct mystruct, list)*;
head
Operating systems Mentoring Operating System (MentOS) 2021/2022 26 / 27
Circular, double-linked list (7/7)
Support functions (3/3)
list for each(list head t *ptr, list head t *head):
Iterates over each item of a doubly-linked list. In detail:
ptr is a free variable pointer of type list head t;
head is a pointer to a doubly-linked list’s head node.
Starting from the first list’s item, at each call ptr is filled with the
address of the next item in the list until its head is reached.
list_head_t *ptr;
struct mystruct *entry;
// Inter over each mystruct item in list
list_for_each(ptr, &head) {
entry = list_entry(ptr, struct mystruct, list);
// ...
}
Operating systems Mentoring Operating System (MentOS) 2021/2022 27 / 27