Overview
A single line of code that prints a log message never reaches the display directly: the CPU itself prevents it. This video explains the boundary between user mode and kernel mode, what enforces it in hardware, and how system calls let programs cross it.
Key Points
- Without separate modes, one stray pointer could overwrite other apps or the OS. A user-mode crash ends only that process; a kernel-mode failure stops the whole system (a Blue Screen or kernel panic).
- The boundary is a single mode bit in the CPU: privileged instructions pass only when it is 1. Only an interrupt sets it, jumping to handlers that the OS installs and protects at boot.
- A system call is an interrupt a program raises on itself.
openis a standard-library wrapper: it puts the call number (2 for open) inraxand executesint, and the kernel switches to a kernel stack, does the work and returns. CPUs now have dedicatedsyscallandsysretinstructions. - Reading a file in one line makes four system calls:
openat,fstat,readandclose. The kernel returns a file descriptor, a plain integer indexing a per-process table; sockets and pipes work the same way. - Practical angles:
man 2covers system calls; mode switches cost less than context switches but far more than function calls, so system calls in loops are slow; timer interrupts enable preemptive scheduling; and drivers and anti-cheat software run in kernel mode, so a broken update like CrowdStrike’s could stop millions of machines.
Chapters
- 0:00 Introduction
- 1:22 Chapter 1: Why separate the modes?
- 3:32 Chapter 2: A single bit inside the CPU
- 7:47 Chapter 3: The moment a system call happens
- 10:50 Chapter 4: The chain set off by one line of code
- 13:56 Chapter 5: Three practical perspectives
- 19:29 Summary
概要
ログを出力する1行のコードも、ディスプレイに直接は届きません。CPU自身がそれを許さないからです。この動画では、ユーザーモードとカーネルモードの境界線と、それをハードウェアで守る仕組み、そしてプログラムがその境界を越えるためのシステムコールを解説します。
ポイント
- モードが分かれていなければ、ポインターが1つずれるだけで他のアプリやOS本体のメモリを書き換えられてしまいます。ユーザーモードのアプリが落ちてもOSはそのプロセスだけを終了させれば済みますが、カーネルモードで問題が起きるとブルースクリーンやカーネルパニックでシステム全体が止まります。
- 境界線の正体はCPUの中のたった1ビット、モードビットです。特権命令はこのビットが1のときだけ回路を通ります。ビットを1にできるのは割り込みだけで、飛び先のハンドラーはOSが起動時に配置して守っています。
- システムコールは、プログラムが自分自身にかける割り込みです。
openは標準ライブラリーが用意した包み紙で、システムコール番号(open は2)をraxレジスターに入れてからint命令を実行します。カーネルはカーネルスタックに切り替えて処理を行い、元に戻ります。現在のCPUには専用のsyscallとsysret命令があります。 - ファイルを読む1行のコードの裏では、
openat、fstat、read、closeの4回のシステムコールが呼ばれます。カーネルが返すファイルディスクリプタは、プロセスごとの表を指すただの整数で、ソケットやパイプも同じ仕組みで扱えます。 - 実務につながる視点として、
man 2のページがシステムコールを説明していること、モードスイッチはコンテキストスイッチより軽いものの関数呼び出しよりずっと重く、ループの中でシステムコールを呼ぶと遅くなること、タイマー割り込みがプリエンプティブなスケジューリングを支えていることを紹介します。さらに、ドライバーやゲームのアンチチートはカーネルモードで動くため、CrowdStrikeの障害のように壊れた更新が何百万台もの端末を止めることがあります。