---
# System prepended metadata

title: Chapter 09：User Process

---

Chapter 09：User Process
===

:::info 

作業系統的終極任務就是向 User Process 提供服務，所以本章的目標就是成功跳轉 User Process。本章的背景知識之前在 ==[Chapter 03-1：Privilege Level Switching](https://hackmd.io/@srhuang/BJO73NE5le)== 有詳細的範例實作，如果對於如何切換權限 (Ring0/Ring3) 還不熟悉的人可以先補充這部分的知識。
&emsp; 1. Memory mgmt. for user process
&emsp; 2. Load User Process
&emsp; 3. Jump to User Process
:::
>[time=Sat, Nov 29, 2025 2:46 AM]
---
https://youtu.be/er73Fo5T-dQ
<iframe width="560" height="315" src="https://www.youtube.com/embed/er73Fo5T-dQ?si=Xgi1xiXKt3X7d9tl" 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>

# Memory mgmt. for user process

>[!Note] Support user virtual address bitmap
>![截圖 2025-12-09 下午4.27.05](https://hackmd.io/_uploads/SJbtT8rfZg.png)

>[!Note] Overview of memory mgmt.
>![截圖 2025-12-10 下午6.34.20](https://hackmd.io/_uploads/H1P0nTIzbg.png)
    
>[!Note] Overview of memory bitmap
>![截圖 2025-12-17 下午4.03.40](https://hackmd.io/_uploads/HJx6MEkl7Zg.png)


## Source Code
https://github.com/srhuang/a-os/commit/c7be2124ddbfc485325e08ac81158bf0b131fb48

## Compile
```
make all
```

## Put on hard disk
```
sh gen.sh
```

## Checkpoint
![截圖 2025-12-10 凌晨4.42.20](https://hackmd.io/_uploads/BkR6YWIf-x.png)















# Load User Process

![截圖 2025-12-17 下午6.33.20](https://hackmd.io/_uploads/B1VzPWgXZg.png)

>[!Note] hard disk layout
>![截圖 2025-12-10 清晨5.08.30](https://hackmd.io/_uploads/H1qkeMUMbx.png)


>[!Note] ELF
>![截圖 2025-12-05 凌晨2.36.07](https://hackmd.io/_uploads/ByL24LkfWl.png)

>[!Note] Section & Segment
>Segment = loader 使用的單位（程式執行需要的 memory 區塊）
>Section = linker 使用的單位（編譯/連結時的邏輯區塊）
>Segment 是由多個 section 合併形成的。
>Section 不一定會出現在任何 segment 裡（很多純工具用的 section）。
>![截圖 2025-12-05 凌晨2.33.00](https://hackmd.io/_uploads/HkUG4UJG-x.png)



## Source Code
https://github.com/srhuang/a-os/commit/dadee4c58be66de81054e4b215cb04ff0dbe0558

## Compile
```
make all
```

## Put on hard disk
==gen.sh==
```sh=
dd if=../code/out/mbr.bin of=./60mb.img bs=512 count=1 conv=notrunc
dd if=../code/out/loader.bin of=./60mb.img bs=512 count=4 seek=1 conv=notrunc
dd if=../code/out/kernel.bin of=./60mb.img bs=512 count=200 seek=5 conv=notrunc
dd if=../code/out/usr.bin of=./60mb.img bs=512 count=100 seek=205 conv=notrunc

```
```
sh gen.sh
```

## Checkpoint
![截圖 2025-12-17 凌晨4.10.04](https://hackmd.io/_uploads/BJIThV1mWl.png)

```
ll out/usr.bin
```
![截圖 2025-12-17 晚上8.35.08](https://hackmd.io/_uploads/Sk69QXgmZg.png)

```
readelf -h -l out/usr.bin
```
![截圖 2025-12-17 晚上8.34.09](https://hackmd.io/_uploads/S1_P77xmZx.png)















# Jump to User Process

>[!Note] TSS data structure
>![upload_c591a86ffab683ff2f7ba1b256bf1697](https://hackmd.io/_uploads/By-EXSgQWx.png)

>[!Note] TSS Descriptor
>![upload_0b25cd6215d6a01eb4cd321beeb3e9bc](https://hackmd.io/_uploads/SkvrXSx7Wl.png)

>[!Note] User Process Stack
>![截圖 2025-12-17 晚上10.51.29](https://hackmd.io/_uploads/HkgqQrlXWg.png)

>[!Note] User Process Kernel Stack (Ring 0 --> Ring 3)
>![截圖 2025-12-17 晚上10.57.34](https://hackmd.io/_uploads/SJR-rHe7Zg.png)

>[!Note] Interrupt Triggered Kernel Stack (Ring 3 --> Ring 0)
>![截圖 2025-12-17 晚上11.49.05](https://hackmd.io/_uploads/BJgf-8l7We.png)

>[!Note] System Call Kernel Stack (Ring 3 --> Ring 0)
>![截圖 2025-12-17 晚上11.48.00](https://hackmd.io/_uploads/rJSAe8lX-e.png)

>[!Warning] mmap_min_addr
>The default value for mmap_min_addr varies by Linux distribution and kernel version, but it's commonly set to 65536 (64 KiB) for security to prevent NULL-pointer dereference exploits
>`cat /proc/sys/vm/mmap_min_addr`
>我們設定最低的 user space virtual address is `0x10000`


## Source Code
https://github.com/srhuang/a-os/commit/3f5c978caa14576bef935050a6221ed889b6e870

## Compile
```
make all
```

## Put on hard disk
```
sh gen.sh
```

## Checkpoint
![截圖 2025-12-17 凌晨4.13.01](https://hackmd.io/_uploads/SJpwT4kQZg.png)

```
vb 0x2b:0x80480a0
c
info gdt
info tab
```
![截圖 2025-12-17 晚上11.07.26](https://hackmd.io/_uploads/HkghPrl7Wx.png)


