Getting to Ring 3:进入 Ring 3 的内核开发指南
文章目录
编写内核的最终目标是进入 用户态,换句话说,就是从 ring 0 进入 ring 3。虽然人们可能认为 ring 3 的 GDT 条目就足够了,但实际情况更为复杂。必须完成以下所有任务:
- 添加(至少)两个新的 GDT 条目,配置为 ring 3。
- 这些条目用于用户的代码段和数据段(各一个)
- 设置一个最基本的 TSS,并带有一个 ESP0 栈。
- 当 CPU 处于用户模式时发生中断(无论是故障、IRQ 还是软件中断),CPU 需要知道内核栈的位置。该位置存储在 TSS 的 ESP0(0 表示 ring 0)条目中。
- 设置一个 IDT为 ring 3 系统调用中断设置一个 IDT 条目(可选)。
- 系统调用是用户代码请求内核执行 IO 和进程管理的方式。更多信息请参见 系统调用。
要求
- Ring 0 GDT 和 IDT
- IRQ 处理
- 带任务切换的多任务计划
GDT
以下是使用位域的 C 语言 GDT 条目结构示例:
struct gdt_entry_bits { unsigned int limit_low : 16; unsigned int base_low : 24; unsigned int accessed : 1; unsigned int read_write : 1; // readable for code, writable for data unsigned int conforming_expand_down : 1; // conforming for code, expand down for data unsigned int code : 1; // 1 for code, 0 for data unsigned int code_data_segment : 1; // should be 1 for everything but TSS and LDT unsigned int DPL : 2; // privilege level unsigned int present : 1; unsigned int limit_high : 4; unsigned int available : 1; // only used in software; has no effect on hardware unsigned int long_mode : 1; unsigned int big : 1; // 32-bit opcodes for code, uint32_t stack for data unsigned int gran : 1; // 1 to use 4k page addressing, 0 for byte addressing unsigned int base_high : 8; } __packed; // or `__attribute__((packed))` depending on compiler
使用此结构,可以添加两个 ring 3 段,基址均为 0,界限均为 0xFFFFFFFF,如下所示:
static gdt_entry_bits gdt[6]; // one null segment, two ring 0 segments, two ring 3 segments, TSS segment // (ring 0 segments) gdt_entry_bits *ring3_code = &gdt[3]; gdt_entry_bits *ring3_data = &gdt[4]; ring3_code->limit_low = 0xFFFF; ring3_code->base_low = 0; ring3_code->accessed = 0; ring3_code->read_write = 1; // since this is a code segment, specifies that the segment is readable ring3_code->conforming = 0; // does not matter for ring 3 as no lower privilege level exists ring3_code->code = 1; ring3_code->code_data_segment = 1; ring3_code->DPL = 3; // ring 3 ring3_code->present = 1; ring3_code->limit_high = 0xF; ring3_code->available = 1; ring3_code->long_mode = 0; ring3_code->big = 1; // it's 32 bits ring3_code->gran = 1; // 4KB page addressing ring3_code->base_high = 0; *ring3_data = *ring3_code; // contents are similar so save time by copying ring3_data->code = 0; // not code but data install_tss(&gdt[5]); // TSS segment will be the fifth flush_tss();
实际上,仅凭这两个段就可以将 CPU 置于用户模式。然而,无法返回 ring 0 来执行系统调用、处理故障甚至 IRQ。这正是 TSS 的作用所在。
TSS
TSS 可用于多任务处理,但建议使用软件多任务处理,原因如下:
- 软件任务切换更快(通常)
- 当你将操作系统移植到不同的 CPU 时,它可能没有 TSS,因此无论如何你都必须实现软件任务切换
- x86 64 位模式不允许你使用 TSS 进行任务切换(主要原因,尤其是如果你的目标是进入 64 位模式)
本指南将使用软件多任务处理。因此,32 位 TSS 将包含许多我们不需要的冗余内容。以下是 TSS 的结构:
struct tss_entry_struct { uint32_t prev_tss; // The previous TSS - with hardware task switching these form a kind of backward linked list. uint32_t esp0; // The stack pointer to load when changing to kernel mode. uint32_t ss0; // The stack segment to load when changing to kernel mode. // Everything below here is unused. uint32_t esp1; // esp and ss 1 and 2 would be used when switching to rings 1 or 2. uint32_t ss1; uint32_t esp2; uint32_t ss2; uint32_t cr3; uint32_t eip; uint32_t eflags; uint32_t eax; uint32_t ecx; uint32_t edx; uint32_t ebx; uint32_t esp; uint32_t ebp; uint32_t esi; uint32_t edi; uint32_t es; uint32_t cs; uint32_t ss; uint32_t ds; uint32_t fs; uint32_t gs; uint32_t ldt; uint16_t trap; uint16_t iomap_base; } __packed; typedef struct tss_entry_struct tss_entry_t;
要设置此 TSS 结构,请为其提供一个带有正确 ss0 段的初始 esp0 栈。
// Note: some of the GDT entry struct field names may not match perfectly to the TSS entries. tss_entry_t tss_entry; void write_tss(gdt_entry_bits *g) { // Compute the base and limit of the TSS for use in the GDT entry. uint32_t base = (uint32_t) &tss_entry; uint32_t limit = sizeof tss_entry; // Add a TSS descriptor to the GDT. g->limit_low = limit; g->base_low = base; g->accessed = 1; // With a system entry (`code_data_segment` = 0), 1 indicates TSS and 0 indicates LDT g->read_write = 0; // For a TSS, indicates busy (1) or not busy (0). g->conforming_expand_down = 0; // always 0 for TSS g->code = 1; // For a TSS, 1 indicates 32-bit (1) or 16-bit (0). g->code_data_segment=0; // indicates TSS/LDT (see also `accessed`) g->DPL = 0; // ring 0, see the comments below g->present = 1; g->limit_high = (limit & (0xf << 16)) >> 16; // isolate top nibble g->available = 0; // 0 for a TSS g->long_mode = 0; g->big = 0; // should leave zero according to manuals. g->gran = 0; // limit is in bytes, not pages g->base_high = (base & (0xff << 24)) >> 24; //isolate top byte // Ensure the TSS is initially zero'd. memset(&tss_entry, 0, sizeof tss_entry); tss_entry.ss0 = REPLACE_KERNEL_DATA_SEGMENT; // Set the kernel stack segment. tss_entry.esp0 = REPLACE_KERNEL_STACK_ADDRESS; // Set the kernel stack pointer. //note that CS is loaded from the IDT entry and should be the regular kernel code segment } void set_kernel_stack(uint32_t stack) { // Used when an interrupt occurs tss_entry.esp0 = stack; }
最后,flush_tss 函数的实现(Intel 语法):
; C declaration: void flush_tss(void); global flush_tss flush_tss: mov ax, (5 * 8) | 0 ; fifth 8-byte selector, symbolically OR-ed with 0 to set the RPL (requested privilege level). ltr ax ret
此时内核已准备好进入 ring 3。值得注意的是,GDT 中 TSS 描述符的 DPL 与任务运行的特权级毫无关系:后者取决于用于设置 CS 的代码段的 DPL。TSS 描述符的 DPL 决定了在哪个特权级可以 CALL 它,从而触发硬件上下文切换(仅限 32 位)。
摘自《Intel® 64 and IA-32 Architectures Software Developer’s Manual Volume 3》第 7.2.2 节(TSS Descriptor):
在大多数系统中,TSS 描述符的 DPL 被设置为小于 3 的值,以便只有特权软件才能执行任务切换。然而,在多任务应用程序中,某些 TSS 描述符的 DPL 可能被设置为 3,以允许在应用程序(或用户)特权级进行任务切换。
进入 Ring 3
x86 是一款棘手的 CPU。无论你如何尝试,都没有简单的方法进入用户模式。尽管如此,下面介绍三种进入用户模式的方法。
iret 方法
进入 ring 3 的方法之一是让处理器认为它一开始就处于 ring 3。这可以通过 iret 来实现。以下是这个技巧的简单示例:
global jump_usermode extern test_user_function jump_usermode: mov ax, (4 * 8) | 3 ; ring 3 data with bottom 2 bits set for ring 3 mov ds, ax mov es, ax mov fs, ax mov gs, ax ; SS is handled by iret ; set up the stack frame iret expects mov eax, esp push (4 * 8) | 3 ; data selector push eax ; current esp pushf ; eflags push (3 * 8) | 3 ; code selector (ring 3 code with bottom 2 bits set for ring 3) push test_user_function ; instruction address to return to iret
这将调用 test_user_function,它将在用户模式下运行!让 test_user_function 执行 cli 或其他特权指令,你将会看到一个 通用保护故障。
sysexit 方法
第二种方法是使用 sysexit 指令,如下所示:
global jump_usermode extern test_user_function jump_usermode: mov ax, (4 * 8) | 3 ; user data segment with RPL 3 mov ds, ax mov es, ax mov fs, ax mov gs, ax ; sysexit sets SS ; setup wrmsr inputs xor edx, edx ; not necessary; set to 0 mov eax, 0x8 ; the segments are computed as follows: CS=MSR+0x10 (0x8+0x10=0x18), SS=MSR+0x18 (0x8+0x18=0x20). mov ecx, 0x174 ; MSR specifier: IA32_SYSENTER_CS wrmsr ; set sysexit segments ; setup sysexit inputs mov edx, test_user_function ; to be loaded into EIP mov ecx, esp ; to be loaded into ESP sysexit
sysret 方法
另一种方法是使用 sysret 指令,如下所示:
; note: this code is for 64-bit long mode only. ; it is unknown if it works in protected mode. ; using intel assembly style global jump_usermode extern test_user_function jump_usermode: ;enable system call extensions that enables sysret and syscall mov rcx, 0xc0000082 wrmsr mov rcx, 0xc0000080 rdmsr or eax, 1 wrmsr mov rcx, 0xc0000081 rdmsr mov edx, 0x00180008 wrmsr mov ecx, test_user_function ; to be loaded into RIP mov r11, 0x202 ; to be loaded into EFLAGS sysretq ;use "o64 sysret" if you assemble with NASM
多任务处理注意事项
用户模式和任务切换有许多微妙之处。每当发生系统调用中断时,ESP0 会被加载到栈指针中,并且在进入中断处理程序之前会压入所有系统信息。对于两个 ring 3 任务,这可能会成为问题。想象一下:esp 当前设置为 ESP0 栈。当接收到中断时,esp 被设置为另一个任务的 esp,而该 esp 是同一个 ESP0 栈。为避免覆盖数据,处理程序必须在每次任务切换时更改 ESP0 栈(以及中断压入的 ESP 栈)。
来源与翻译说明
本文根据原文快照翻译,保留原有代码、数值、技术限定及历史背景。原文中的命令与运行结果不代表本站已实际执行验证。
原文:Getting to Ring 3;离线修订号:27736。

评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论