diff --git a/course/08-Task_management.md b/course/08-Task_management.md index 7e835f0f762fd4cfca79ad53bb615b76b904f6fa..09d98c733581ba419ff0fa1b5a8a3a7ac4084fbd 100644 --- a/course/08-Task_management.md +++ b/course/08-Task_management.md @@ -144,7 +144,7 @@ Several possible ways to switch to a task: ## Switching to a task (2/2) - Here, we will describe how to switch to a nested task by calling the `call far` instruction -- Very simple: one simply executes the `call far` instruction with the TSS selector of the task to switch to in argument: +- Very simple: simply execute the `call far` instruction with the **TSS selector** of the task to switch to in argument: ```{.assembler .small} ; switch to the task referenced by @@ -152,56 +152,7 @@ Several possible ways to switch to a task: call far <TSS selector> ``` -[//]: # ---------------------------------------------------------------- -## Task switching: steps (simplified) - -\small - -The CPU automatically peforms these operations during a task switch to a \textcolor{mygreen}{new task}: - -1) Reads the `tr` register to obtain the \textcolor{myorange}{current task}'s TSS -1) Saves the current CPU context into the \textcolor{myorange}{current task}'s TSS -1) Using the \textcolor{mygreen}{new task}'s TSS selector, loads the `tr` register to point to the \textcolor{mygreen}{new task}'s TSS -1) Loads the \textcolor{mygreen}{new task}'s context (from its TSS) into the CPU - - includes `cr3` = \textcolor{mygreen}{new task}'s address space (= memory mapping) - - `cr3` points to the \textcolor{mygreen}{new task}'s page directory -1) Resumes the execution of the \textcolor{mygreen}{new task} (from the restored `eip`) - -[//]: # ---------------------------------------------------------------- -## Switching back to the previous task - -\footnotesize - -Switching back to the \textcolor{mypurple}{previous task} is trivial: - -- Execute the `iret` assembly instruction - -What happens (simplified) when the CPU executes `iret`? - -1) Reads the `tr` register to obtain the \textcolor{mygreen}{current task}'s TSS -1) Saves the current CPU context into the \textcolor{mygreen}{current task}'s TSS -1) Loads the `tr` register with the value of the "Previous Task Link" field of the \textcolor{mygreen}{current task}'s TSS -1) Loads the \textcolor{mypurple}{previous task}'s context (from its TSS) into the CPU - - includes `cr3` = \textcolor{mypurple}{previous task}'s address space -1) Resumes the execution of the \textcolor{mypurple}{previous task} - -[//]: # ---------------------------------------------------------------- -## Stack switching - -- A stack switch occurs during a task switch from a **lesser privilege level to a higher** privilege level - - E.g. \textcolor{mygreen}{ring 3} $\rightarrow$ \textcolor{myred}{ring 0}: `ss` and `esp` are loaded from the current task's `TSS.ss0` and `TSS.esp0` values -- Typically happens when: - - a task (user mode) performs a system call (\textcolor{mygreen}{ring 3} $\rightarrow$ \textcolor{myred}{ring 0}) - - user code (\textcolor{mygreen}{ring 3}) is interrupted by a hardware interrupt or a processor exception - -[//]: # ---------------------------------------------------------------- -## Back to task switching - -- As previously seen, the CPU can switch to a task using a TSS **selector**: - ```{.assembler .small} - call far <TSS selector> - ``` -- What is a TSS **selector**? +- What is a **\textcolor{myred}{TSS selector}**? [//]: # ---------------------------------------------------------------- ## Global Descriptor Table (GDT) @@ -257,7 +208,22 @@ Example of a GDT defining 3 tasks: [//]: # ---------------------------------------------------------------- -## Task switching revisited: in-depth steps +## Task switching: steps (simplified) + +\small + +The CPU automatically peforms these operations during a task switch to a \textcolor{mygreen}{new task}: + +1) Reads the `tr` register to obtain the \textcolor{myorange}{current task}'s TSS +1) Saves the current CPU context into the \textcolor{myorange}{current task}'s TSS +1) Using the \textcolor{mygreen}{new task}'s TSS selector, loads the `tr` register to point to the \textcolor{mygreen}{new task}'s TSS +1) Loads the \textcolor{mygreen}{new task}'s context (from its TSS) into the CPU + - includes `cr3` = \textcolor{mygreen}{new task}'s address space (= memory mapping) + - `cr3` points to the \textcolor{mygreen}{new task}'s page directory +1) Resumes the execution of the \textcolor{mygreen}{new task} (from the restored `eip`) + +[//]: # ---------------------------------------------------------------- +## Task switching: in-depth steps \footnotesize @@ -275,7 +241,7 @@ The CPU automatically peforms these operations during a task switch: 1) Resumes the execution of the new task (from the restored `eip`) [//]: # ---------------------------------------------------------------- -## Task switching: important notes +## Task register - The task register, `tr`, is loaded using the `ltr` instruction - requires a TSS selector as operand @@ -285,10 +251,35 @@ The CPU automatically peforms these operations during a task switch: - `ltr` requires a TSS selector which implies a properly initialized and loaded GDT [//]: # ---------------------------------------------------------------- -## Task initialization, static data, done once +## Switching back to the previous task \footnotesize +Switching back to the \textcolor{mypurple}{previous task} is trivial: + +- Execute the `iret` assembly instruction + +What happens (simplified) when the CPU executes `iret`? + +1) Reads the `tr` register to obtain the \textcolor{mygreen}{current task}'s TSS +1) Saves the current CPU context into the \textcolor{mygreen}{current task}'s TSS +1) Loads the `tr` register with the value of the "Previous Task Link" field of the \textcolor{mygreen}{current task}'s TSS +1) Loads the \textcolor{mypurple}{previous task}'s context (from its TSS) into the CPU + - includes `cr3` = \textcolor{mypurple}{previous task}'s address space +1) Resumes the execution of the \textcolor{mypurple}{previous task} + +[//]: # ---------------------------------------------------------------- +## Stack switching + +- A stack switch occurs during a task switch from a **lesser privilege level to a higher** privilege level + - E.g. \textcolor{mygreen}{ring 3} $\rightarrow$ \textcolor{myred}{ring 0}: `ss` and `esp` are loaded from the current task's `TSS.ss0` and `TSS.esp0` values +- Typically happens when: + - a task (user mode) performs a system call (\textcolor{mygreen}{ring 3} $\rightarrow$ \textcolor{myred}{ring 0}) + - user code (\textcolor{mygreen}{ring 3}) is interrupted by a hardware interrupt or a processor exception + +[//]: # ---------------------------------------------------------------- +## Task initialization, static data, done once + 1. Reserve a GDT entry (TSS descriptors) for the initial TSS - used to save the CPU context **before** switching to the first task - to be done **once** @@ -297,13 +288,10 @@ The CPU automatically peforms these operations during a task switch: 1. Loads the task register with the initial TSS selector 1. Reserve a GDT entry (TSS descriptors) for task T 1. Allocate a data structures for task T: - - \scriptsize page directory - - \scriptsize TSS structure and TSS selector - - \scriptsize kernel stack - - \scriptsize anything else that might be useful -1. Initialize T's page directory with desired memory mappings, typically: - - \scriptsize framebuffer (identity, user privileged) - - \scriptsize RAM (identity, kernel privileged) + - page directory + - TSS structure and TSS selector + - kernel stack + - anything else that might be useful [//]: # ---------------------------------------------------------------- ## Task load workflow (1/3) @@ -314,21 +302,23 @@ The CPU automatically peforms these operations during a task switch: [//]: # ---------------------------------------------------------------- ## Task load workflow (2/3) -\small - 1. Initialize task T's data structures: - - \footnotesize initialize T's TSS descriptor (so it points to T's TSS) - - \footnotesize compute T's TSS selector - - \footnotesize initialize T's context (TSS structure) -1. Initialize T's page directory with memory mapping where app (code + data) will be loaded: - - \footnotesize allocate frames to store app + stack - - \footnotesize contiguously map these frames at the desired virtual address + - initialize T's TSS descriptor (so it points to T's TSS) + - compute T's TSS selector + - initialize T's context (TSS structure) + +1. Initialize T's page directory with desired memory mappings, typically: + - framebuffer (identity, \textcolor{mygreen}{user privileged}) + - RAM (identity, \textcolor{myred}{kernel privileged}) + - memory area where app will be loaded (\textcolor{mygreen}{user privileged}): + - allocate frames to store app + **stack** + - contiguously map these frames at the desired virtual address 1. \textcolor{myorange}{Copy the app (multiboot module) into T's allocated content} [//]: # ---------------------------------------------------------------- ## Task load workflow (3/3) -\textcolor{myorange}{Copy the app (multiboot module) into T's allocated content} +**\textcolor{myorange}{Copy the app (multiboot module) into T's allocated content}** \footnotesize diff --git a/course/08-Task_management.pdf b/course/08-Task_management.pdf index 008062e6af4b7e03a7c47627e338bc6d0f18c2c4..b6caddb009e27cff7b79c5aa6f4c3ecb386587af 100644 Binary files a/course/08-Task_management.pdf and b/course/08-Task_management.pdf differ