學(xué)習(xí)筆記
使用教材(配書源碼以及使用方法)
《一個(gè)64位操作系統(tǒng)的設(shè)計(jì)與實(shí)現(xiàn)》
http://www.ituring.com.cn/book/2450
http://www.itdecent.cn/p/28f9713a9171
源碼結(jié)構(gòu)
- 配書代碼包 :第5章 \ 程序 \ 程序5-2
程序5-2 運(yùn)行
ls
cd bootloader
make clean
make
cd ../
cd kernel
make clean
make
cd ../
sudo mount boot.img media -t vfat -o loop
sudo cp bootloader/loader.bin media
sync
sudo cp bootloader/boot.bin media
sync
sudo cp kernel/kernel.bin media
sync
bochs -f ./bochsrc

程序5-2運(yùn)行結(jié)果
程序5-2 執(zhí)行過(guò)程
一、到 user_level_function()為止的流程參考
[OS64][028]源碼閱讀:程序5-1 從內(nèi)核層(0特權(quán)級(jí))到用戶層(3特權(quán)級(jí))
http://www.itdecent.cn/p/37f2f96db932
- 為了執(zhí)行
sysenter指令而在init_task()函數(shù)中新增的代碼
init_task() {
. . .
wrmsr(0x174,KERNEL_CS); // 程序5-1已有
wrmsr(0x175,current->thread->rsp0); // new
wrmsr(0x176,(unsigned long)system_call); // new
. . .
}
init_task() 毫無(wú)疑問(wèn)運(yùn)行在 內(nèi)核層,
current是一個(gè)宏,目的是獲取當(dāng)前的進(jìn)程結(jié)構(gòu)體,
那么,很明顯,此處的current獲取到的就是 屬于內(nèi)核層的進(jìn)程結(jié)構(gòu)體
自然,current->thread->rsp0 就是內(nèi)核層的?;刂?內(nèi)核層的棧空間本質(zhì)上就是全局變量聯(lián)合體union task_union init_task_union其中的數(shù)組空間
參考 [OS64][025]源碼閱讀:程序4-11:運(yùn)行結(jié)果,數(shù)據(jù)結(jié)構(gòu),第一個(gè)進(jìn)程init_task_union
[http://www.itdecent.cn/p/b30fb97a1dc4](http://www.itdecent.cn/p/b30fb97a1dc4)
二、進(jìn)入位于用戶層user_level_function()的函數(shù)
void user_level_function()
{
long ret = 0;
color_printk(RED,BLACK,"user_level_function task is running\n");
__asm__ __volatile__ ( "leaq sysexit_return_address(%%rip), %%rdx \n\t"
"movq %%rsp, %%rcx \n\t"
"sysenter \n\t"
"sysexit_return_address: \n\t"
:"=a"(ret):"0"(15):"memory");
color_printk(RED,BLACK,"user_level_function task called sysenter,ret:%ld\n",ret);
while(1);
}
:"=a"(ret):"0"(15):相當(dāng)于輸入是RAX=15、輸出時(shí)有ret=RAXleaq sysexit_return_address(%%rip), %%rdx把應(yīng)用程序的返回地址(即返回到標(biāo)號(hào)處)填入RDX(等下回來(lái)就是回來(lái)到標(biāo)號(hào)sysexit_return_address:處)movq %%rsp, %%rcx將當(dāng)前的CPU 寄存器RSP填入CPU寄存器RCX,當(dāng)執(zhí)行到函數(shù)user_level_function()時(shí),已經(jīng)是位于用戶層了,因此當(dāng)前的RSP就是指向用戶層的?;刂?,毫無(wú)疑問(wèn)這里的值應(yīng)該就是硬編碼的0xa00000(等下回來(lái)時(shí)候要用)sysenter,兩個(gè)需要的參數(shù)是上面的wrmsr提供的,接著會(huì)跳轉(zhuǎn)到system_call
三、實(shí)現(xiàn) 3特權(quán)級(jí) 到 0特權(quán)級(jí): sysenter
- 1、
system_call最后是一句callq system_call_function
sysenter 會(huì)導(dǎo)致 EFLAGS 的 if 標(biāo)志位復(fù)位 即不允許中斷
這里手動(dòng)打開(kāi),允許中斷 ,使用指令 sti
ENTRY(system_call)
sti
subq $0x38, %rsp
cld;
pushq %rax;
. . .
pushq %r15;
movq $0x10, %rdx;
movq %rdx, %ds;
movq %rdx, %es;
movq %rsp, %rdi
callq system_call_function ////////
ENTRY(ret_system_call)
movq %rax, 0x80(%rsp)
popq %r15
. . .
popq %rax
addq $0x38, %rsp
.byte 0x48
sysexit
這兩個(gè)代碼段,在源碼文件entry.S就是連在一起的,
下面是反匯編后它們的機(jī)器碼以及線性地址:
ffff800000104027 <system_call>:
ffff800000104027: fb sti
. . .
ffff80000010405c: 48 89 e7 mov %rsp,%rdi
ffff80000010405f: e8 da 72 00 00 callq ffff80000010b33e <system_call_function>
ffff800000104064 <ret_system_call>:
ffff800000104064: 48 89 84 24 80 00 00 mov %rax,0x80(%rsp)
. . .
ffff80000010407d: 59 pop %rcx
ffff80000010407e: 5a pop %rdx
. . .
ffff80000010408f: 48 0f 35 rex.W sysexit
- 2、
system_call_function帶著從no_system_call來(lái)的返回值RAX=-1是返回到哪里呢?
unsigned long system_call_function(struct pt_regs * regs)
{
return system_call_table[regs->rax](regs);
}
#define MAX_SYSTEM_CALL_NR 128
函數(shù)指針類型
返回值是 unsigned long
傳入?yún)?shù)是 結(jié)構(gòu)體pt_regs指針
typedef unsigned long (* system_call_t)(struct pt_regs * regs);
unsigned long no_system_call(struct pt_regs * regs)
{
color_printk(RED,BLACK,"no_system_call is calling,NR:%#04x\n",regs->rax);
return -1;
}
目前全部的 系統(tǒng)調(diào)用處理函數(shù) 都設(shè)置成 no_system_call
system_call_t system_call_table[MAX_SYSTEM_CALL_NR] =
{
[0 ... MAX_SYSTEM_CALL_NR-1] = no_system_call
};
- 3、由于
callq與retq是一一對(duì)應(yīng)的,那么其實(shí)就是返回到callq system_call_function后一條指令處,即ret_system_call的入口地址
四、實(shí)現(xiàn) 0特權(quán)級(jí) 回到 3特權(quán)級(jí) : sysexit
ffff800000104064 <ret_system_call>:
ffff800000104064: 48 89 84 24 80 00 00 mov %rax,0x80(%rsp)
. . .
ffff80000010407d: 59 pop %rcx
ffff80000010407e: 5a pop %rdx
. . .
ffff80000010408a: 58 pop %rax
ffff80000010408b: 48 83 c4 38 add $0x38,%rsp
ffff80000010408f: 48 0f 35 rex.W sysexit
兩條popq語(yǔ)句,填好了
RDX = 用戶層 user_level_function() 的標(biāo)號(hào)sysexit_return_address: 處
RCX = 用戶層的?;刂?
五、回到 用戶層 user_level_function() 的標(biāo)號(hào) sysexit_return_address: 處
void user_level_function()
{
long ret = 0;
color_printk(RED,BLACK,"user_level_function task is running\n");
__asm__ __volatile__ ( . . .
"sysexit_return_address: \n\t"
:"=a"(ret):"0"(15):"memory");
color_printk(RED,BLACK,"user_level_function task called sysenter,ret:%ld\n",ret);
while(1);
}
- 回來(lái)了,還帶著返回值
RAX=-1,繼續(xù)執(zhí)行,輸出字符串黑底紅字的user_level_function task called sysenter,ret:-1
程序5-2 調(diào)試過(guò)程
- 驗(yàn)證:
return system_call_table[regs->rax](regs);是不是返回到了<ret_system_call>:處
<bochs:1> b 0x10b36d
<bochs:2> c
(0) Breakpoint 1, 0xffff80000010b36d in ?? ()
Next at t=63456943
(0) [0x00000010b36d] 0008:ffff80000010b36d (unk. ctxt): ret ; c3
<bochs:3> print-stack
Stack address size 8
| STACK 0xffff80000011ff38 [0xffff8000:0x00104064]
<bochs:4> s
Next at t=63456944
(0) [0x000000104064] 0008:ffff800000104064 (unk. ctxt): mov qword ptr ss:[rsp+128], rax ; 4889842480000000
直接斷點(diǎn)設(shè)置到system_call_function的return語(yǔ)句:
ffff80000010b33e <system_call_function>:
. . .
ffff80000010b36d: c3 retq
0x 104064 處就是 ret_system_call的入口地址:
ffff800000104064 <ret_system_call>: