Assembly Language & x86 Low-Level Systems Programming · บทเรียน

วงแหวนการป้องกันและสิทธิ์การใช้งาน

เรียนรู้ระดับสิทธิ์ของ x86 (วงแหวน) และวิธีที่ระดับเหล่านี้บังคับใช้การแยกส่วนและความปลอดภัยระหว่างระบบปฏิบัติการกับแอปพลิเคชันของผู้ใช้

บทเรียน 2 จาก 412 ขั้นตอน

วงแหวนการป้องกันและสิทธิ์การใช้งาน เป็นบทเรียน Assembly Language & x86 Low-Level Systems Programming ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Assembly Language & x86 Low-Level Systems Programming และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Assembly Language & x86 Low-Level Systems Programming มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Intro: Protection Rings

Ever wondered how your apps are kept separate from the operating system? Or how a malicious program can't just take over your computer?

The answer lies in privilege rings, a core security feature of x86 processors!

Why Rings? Security!

Privilege rings create a hierarchical structure for software execution. Think of them as security levels.

  • Isolation: Prevent user programs from crashing the OS.
  • Security: Protect critical system resources.
  • Stability: Ensure the system runs reliably.

Ring 0: The Kernel

Ring 0 is the most privileged level. It's where the operating system's kernel resides.

The kernel has direct access to all hardware, memory, and CPU features. It's the ultimate authority, managing everything without restrictions.

Ring 3: User Applications

Ring 3 is the least privileged level. This is where most of your everyday applications run.

User applications have limited access to hardware and memory. They must request services from the kernel (Ring 0) to perform privileged operations.

The x86 Ring Hierarchy

The x86 architecture defines 4 privilege levels: Ring 0, Ring 1, Ring 2, and Ring 3.

However, modern operating systems like Linux and Windows typically only use:

  • Ring 0: For the OS kernel.
  • Ring 3: For user applications.

Rings 1 and 2 are usually unused, simplifying the model for most systems.

CPL: Current Privilege

The CPU tracks the Current Privilege Level (CPL) of the currently executing code.

The CPL is stored in the Code Segment (CS) register, specifically in the lower two bits. It tells the CPU which ring the program is currently operating in.

DPL: Segment Privilege

Every segment descriptor (which defines memory segments) has a Descriptor Privilege Level (DPL).

The DPL specifies the minimum privilege level required to access that segment. For instance, a Ring 0 code segment would have a DPL of 0.

RPL: Requestor Privilege

The Requestor Privilege Level (RPL) indicates the privilege level of the code that requested access to a segment.

The CPU uses CPL, DPL, and RPL to enforce security rules. For example, a Ring 3 program cannot load a Ring 0 code segment, even if it tries to trick the system.

Ring Transitions: Syscalls

How does a Ring 3 application ask the Ring 0 kernel to do something privileged, like writing to a file?

It uses a controlled mechanism called a system call (or syscall). Syscalls are like "gates" that allow safe, limited transitions to a higher privilege level and back.

IOPL: I/O Port Control

Direct hardware I/O via IN and OUT instructions is highly privileged. A user-mode program (Ring 3) attempting this will typically cause a General Protection Fault.

The CPU checks the I/O Privilege Level (IOPL) in the EFLAGS register. If the Current Privilege Level (CPL) is numerically less than or equal to IOPL, direct I/O is allowed. Otherwise, the operation is blocked.

This example tries to read from an I/O port. On a modern OS, this will likely fail with a protection fault or be terminated. It demonstrates the restriction:

; This example illustrates a privileged I/O attempt.
; It is specific to Linux (using int 0x80 for syscalls).

section .data
  msg db "Attempting privileged I/O...", 0xA
  len equ $ - msg

section .text
  global _start

_start:
  ; Print message using a system call (Ring 3 -> Ring 0 transition)
  mov eax, 4      ; sys_write
  mov ebx, 1      ; stdout file descriptor
  mov ecx, msg    ; address of string
  mov edx, len    ; length of string
  int 0x80        ; Invoke Linux kernel (syscall)

  ; Attempt to read from I/O port 0x60 (e.g., keyboard data)
  ; This instruction requires sufficient privilege (IOPL <= CPL)
  ; In a typical Ring 3 user program, this will cause a fault.
  in al, 0x60     ; !!! PRIVILEGED INSTRUCTION !!!

  ; Exit program using a system call
  mov eax, 1      ; sys_exit
  xor ebx, ebx    ; exit code 0
  int 0x80        ; Invoke Linux kernel (syscall)

Privilege Ring Check

Let's test your understanding of privilege rings!

Recap: Rings & Security

Great job! You've learned about x86 privilege rings.

  • Ring 0: OS Kernel, highest privilege.
  • Ring 3: User applications, lowest privilege.
  • CPL, DPL, RPL: CPU mechanisms for privilege checking.
  • System Calls: Controlled transitions to higher privileges.
  • IOPL: Restricts direct I/O access.

These rings are fundamental for system security and stability by isolating different software components.

เริ่มต้นได้ฟรี

เรียนรู้ Assembly ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
12
บทเรียน
48

คำถามที่พบบ่อย

บทเรียน “วงแหวนการป้องกันและสิทธิ์การใช้งาน” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “วงแหวนการป้องกันและสิทธิ์การใช้งาน” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Assembly Language & x86 Low-Level Systems Programming ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Assembly Language & x86 Low-Level Systems Programming มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “วงแหวนการป้องกันและสิทธิ์การใช้งาน”

เรียนรู้ระดับสิทธิ์ของ x86 (วงแหวน) และวิธีที่ระดับเหล่านี้บังคับใช้การแยกส่วนและความปลอดภัยระหว่างระบบปฏิบัติการกับแอปพลิเคชันของผู้ใช้ คุณปฏิบัติ Assembly Language & x86 Low-Level Systems Programming ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Assembly Language & x86 Low-Level Systems Programming หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Assembly Language & x86 Low-Level Systems Programming บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “วงแหวนการป้องกันและสิทธิ์การใช้งาน” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Assembly Language & x86 Low-Level Systems Programming นี้ได้ไหม

ได้ บทเรียน Assembly Language & x86 Low-Level Systems Programming ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การจัดหน้าและหน่วยจัดการหน่วยความจำ (MMU)
  2. วงแหวนการป้องกันและสิทธิ์การใช้งาน
  3. พื้นฐานไฮเปอร์ไวเซอร์และการทำเสมือน
  4. การแบ่งเซกเมนต์และตารางตัวบอกส่วนกลาง (GDT)
← กลับไปที่ Assembly Language & x86 Low-Level Systems Programming