From 0fd506ff7155de63d01a6d777ba322a6e173ae4c Mon Sep 17 00:00:00 2001 From: zhaoyanbai Date: Sat, 3 Oct 2026 12:35:17 +0800 Subject: [PATCH] =?utf8?q?=E5=9C=A8ring3=E4=B8=AD=E8=A7=A6=E5=8F=91#PF?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- kernel/ring3.S | 5 +++++ kernel/task_user.c | 7 +++++++ scripts/link.ld | 9 +++++++++ 3 files changed, 21 insertions(+) diff --git a/kernel/ring3.S b/kernel/ring3.S index 5774685..9ec9176 100644 --- a/kernel/ring3.S +++ b/kernel/ring3.S @@ -10,6 +10,8 @@ #define ASM #include +.extern ring3_c_text + # a allocatable 可分配,表示这个节在程序运行时需要加载到内存中 # x executable 可执行,表示这个节包含可执行的代码 # w writable 可写,通常用于数据节 @@ -58,6 +60,9 @@ ring3_entry: addl $0x18, %esp + call ring3_c_text + + # 纯消耗CPU时间 movl $0xFF0F0F0F, %ecx 1: diff --git a/kernel/task_user.c b/kernel/task_user.c index 31ee601..73fdf66 100644 --- a/kernel/task_user.c +++ b/kernel/task_user.c @@ -61,3 +61,10 @@ void user_task_entry() { asm volatile("sysexit;" ::"d"(ring3_page_vaddr), "c"(ring3_stack_top_vaddr)); } + +// 用于触发#PF以验证COW +__attribute__((__section__(".ring3.data"))) uint32_t ring3_x = 0; +__attribute__((__section__(".ring3.text"))) void ring3_c_text() { + asm("nop;nop;nop;nop;nop;nop;nop;nop;"); + ring3_x += 1; +} diff --git a/scripts/link.ld b/scripts/link.ld index d80f375..d91f95a 100644 --- a/scripts/link.ld +++ b/scripts/link.ld @@ -64,6 +64,7 @@ SECTIONS { ring3_page_begin = .; *(.ring3.entry) + *(.ring3.text) . = ALIGN(0x100); ring3_text_size = SIZEOF(.ring3); ring3_page_end = .; @@ -72,6 +73,14 @@ SECTIONS /* 带绝对 VMA 的节写完后 . 不会自动接上,而是变成 该节VMA+SIZEOF(该节名) */ . = ring3_lma + SIZEOF(.ring3); + . = ALIGN(0x1000); + ring3_data_lma = .; + .ring3_data 0x10000000 : AT(ring3_data_lma) + { + *(.ring3.data) + } + . = ring3_data_lma + SIZEOF(.ring3_data); + . = ALIGN(0x1000); /* . = kernel_begin; */ . += kernel_virtual_addr_start; -- 2.55.0