---
# System prepended metadata

title: Chapter 03-2：CPU Multitasking

---

Chapter 03-2：CPU Multitasking
===

:::info 

雖然現代作業系統早已捨棄 x86 CPU 原生支援的 multitasking，但是我們仍然可以透過這個章節的範例，一窺當初 80386 設計的 CPU level multitasking。
&emsp; 1. LDT
&emsp; 2. Task Gate and TSS

:::
>[time=Sat, Sep 6, 2025 11:47 PM]
---

https://youtu.be/tACT3xxqcE4
<iframe width="560" height="315" src="https://www.youtube.com/embed/tACT3xxqcE4?si=nsvri2hA9n_kC2wR" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>

>[!Note] Overview of Segment Descriptors
![types-of-segment-descriptors-l](https://hackmd.io/_uploads/rJ0YxiHiel.jpg)


# LDT

>Reference: [使用Local Descriptor Table (LDT)](https://jasonblog.github.io/note/os/1415.html)

>[!Note] LDT segment descriptor
>![截圖 2025-09-15 晚上10.26.06](https://hackmd.io/_uploads/S1dfMirjlx.png)
>![截圖 2025-09-15 晚上10.24.40](https://hackmd.io/_uploads/HyVTZoroex.png)


>[!Note] LLDT: Load LDT
>![截圖 2025-09-06 下午5.06.56](https://hackmd.io/_uploads/H1aCFdK5ee.png)

>[!Note] Memory Layout
>![截圖 2025-09-07 凌晨2.27.28](https://hackmd.io/_uploads/ry046g9qle.png)

>[!Note] Code Flow
>![截圖 2025-09-07 凌晨2.28.17](https://hackmd.io/_uploads/S1dPTxqqge.png)

## Source Code

https://github.com/srhuang/a-os/commit/248e558b13d57bfdfdb98e9b017d9115723c2144

## Compile

```sh
nasm boot/loader.s -I boot/ -o out/loader.bin 
```

## Put on hard disk

```sh
dd if=../code/out/loader.bin of=./60mb.img bs=512 count=4 seek=1 conv=notrunc
```

## Checkpoint

![截圖 2025-09-07 凌晨2.33.01](https://hackmd.io/_uploads/HkaK0lcqel.png)

![截圖 2025-09-07 凌晨2.30.46](https://hackmd.io/_uploads/S1ZHRg95ex.png)

# Task Gate

>[!Note] Task State Segment (TSS)
>Reference: [Task State Segment](https://pdos.csail.mit.edu/6.828/2004/readings/i386/s07_01.htm)
>![截圖 2025-09-08 晚上9.35.38](https://hackmd.io/_uploads/r1wpjU3qge.png)

>[!Note] TSS Descriptor
>Reference: [Task Gate Descriptor](https://pdos.csail.mit.edu/6.828/2004/readings/i386/s07_04.htm)
>![截圖 2025-09-07 凌晨4.04.15](https://hackmd.io/_uploads/BJUJ4Gccel.png)

>[!Note] Task Gate Descriptor
>![截圖 2025-09-06 晚上9.42.22](https://hackmd.io/_uploads/H1PYq2Y9le.png)

>[!Note] call task gate steps
>Reference: [Task Switching](https://pdos.csail.mit.edu/6.828/2004/readings/i386/s07_05.htm)
>1. Get new TSS Selector from task gate.
>2. Get new TSS Descriptor from GDT.
>3. Check new TSS Descriptor: P should be `1`, B should be `0`.
>4. Get old TSS position from TR Register and backup all registers.
>5. Load new TSS position to TR Register and restore all registers.
>6. Set new TSS Descriptor: B = `1`.
>7. Set Register elfags: NT = `1`.
>8. Set new TSS: `BACK LINK TO PREVIOUS TSS`.
>9. Jump to new TSS.

>[!Note] iret steps
>1. Set Register eflags: NT = `0`.
>2. Set current TSS Descriptor: B = `0`.
>3. Get current TSS position from TR Register and backup all registers.
>4. Get `BACK LINK TO PREVIOUS TSS`, and load it to TR register, and restore all registers.
>5. Jump to previous TSS.

>[!Note] Memory Layout
>![截圖 2025-09-08 下午6.35.46](https://hackmd.io/_uploads/r1bsWNncee.png)

>[!Note] Code Flow
>![截圖 2025-09-08 晚上9.24.32](https://hackmd.io/_uploads/SJNNK8h9lg.png)

## Source Code

https://github.com/srhuang/a-os/commit/e8b9c9e13367e306fd4a011d4416145d82cbb571

## Compile

```sh
nasm boot/loader.s -I boot/ -o out/loader.bin 
```

## Put on hard disk

```sh
dd if=../code/out/loader.bin of=./60mb.img bs=512 count=4 seek=1 conv=notrunc
```

## Checkpoint

![截圖 2025-09-07 凌晨4.24.53](https://hackmd.io/_uploads/Bkd3uzq9le.png)

![截圖 2025-09-07 凌晨4.28.18](https://hackmd.io/_uploads/ryJ19zq5lx.png)


