啟動程序流程
LINUX啟動程序時設定了CS,ESP等各個暫存器的值,這是相應的巨集:
void
start_thread(struct pt_regs *regs, unsigned long new_ip, unsigned long new_sp)
{
loadsegment(fs, 0);
loadsegment(es, 0);
loadsegment(ds, 0);
load_gs_index(0);
regs->ip = new_ip;
regs->sp = new_sp;
percpu_write(old_rsp, new_sp);
regs->cs = __USER_CS;
regs->ss = __USER_DS;
regs->flags = 0x200;
set_fs(USER_DS);
/*
* Free the old FP and other extended state
*/
free_thread_xstate(current);
}
EXPORT_SYMBOL_GPL(start_thread);
這是__USER_CS在X386架構的定義:
#define GDT_ENTRY_DEFAULT_USER_CS 6
#define __USER_CS (GDT_ENTRY_DEFAULT_USER_CS* 8 + 3)
現在看一下保護模式下的段暫存器的具體定義:
15 32 1 0
index T RPL
RPL:Request Privilege Level
T:0=GDT 1=LDT
這裡__USER_CS等於:
__USER_CS = 6 * 8 + 3 = 48 + 3 = 51
00110011 RPL= 11 T = 0 (GDT) INDEX=110(6)