[OS64][025]源碼閱讀:程序4-11:運行結(jié)果灵汪,數(shù)據(jù)結(jié)構(gòu)败许,第一個進程init_task_union

學(xué)習(xí)筆記

使用教材(配書源碼以及使用方法)
《一個64位操作系統(tǒng)的設(shè)計與實現(xiàn)》
http://www.ituring.com.cn/book/2450
http://www.reibang.com/p/28f9713a9171

源碼結(jié)構(gòu)

  • 配書代碼包 :第4章 \ 程序 \ 程序4-11

程序4-11 運行結(jié)果

[anno@localhost bootloader]$ make
nasm boot.asm -o boot.bin
nasm loader.asm -o loader.bin

[anno@localhost kernel]$ make
gcc -E  head.S > head.s
as --64 -o head.o head.s
gcc -E  entry.S > entry.s
as --64 -o entry.o entry.s
gcc  -mcmodel=large -fno-builtin -m64 -c main.c
gcc  -mcmodel=large -fno-builtin -m64 -c printk.c
gcc  -mcmodel=large -fno-builtin -m64 -c trap.c
gcc  -mcmodel=large -fno-builtin -m64 -c memory.c
gcc  -mcmodel=large -fno-builtin -m64 -c interrupt.c
gcc  -mcmodel=large -fno-builtin -m64 -c task.c 
ld -b elf64-x86-64 -z muldefs -o system head.o entry.o main.o printk.o trap.o memory.o interrupt.o task.o -T Kernel.lds 
objcopy -I elf64-x86-64 -S -R ".eh_frame" -R ".comment" -O binary system kernel.bin

[anno@localhost 4-11]$ sudo mount boot.img media -t vfat -o loop
[anno@localhost 4-11]$ sudo cp bootloader/loader.bin media
[anno@localhost 4-11]$ sync
[anno@localhost 4-11]$ sudo cp bootloader/boot.bin media
[anno@localhost 4-11]$ sync
[anno@localhost 4-11]$ sudo cp kernel/kernel.bin media
[anno@localhost 4-11]$ sync

[anno@localhost 4-11]$ bochs -f ./bochsrc
程序4-11

程序4-11 數(shù)據(jù)結(jié)構(gòu)

各結(jié)構(gòu)體大小

各結(jié)構(gòu)體大小
strcut mm_struct : 80 Bytes
struct thread_struct : 64 Bytes
struct task_struct : 88 Bytes
union task_union : 32768 Bytes

程序控制結(jié)構(gòu)體 PCB(Process Control Block): task_struct (task.h)

struct task_struct
{
    struct List list;
    volatile long state;
    unsigned long flags;

    struct mm_struct *mm;
    struct thread_struct *thread;

    unsigned long addr_limit;   
/*0x0000,0000,0000,0000 - 0x0000,7fff,ffff,ffff user*/
/*0xffff,8000,0000,0000 - 0xffff,ffff,ffff,ffff kernel*/

    long pid;

    long counter;

    long signal;

    long priority;
};

List 雙向鏈表 用于連接各個PCB (lib.h)

struct List
{
    struct List * prev;
    struct List * next;
};

內(nèi)存空間分布結(jié)構(gòu)體: mm_struct (task.h)

struct mm_struct
{
    pml4t_t *pgd;   //page table point
    
    unsigned long start_code,end_code;
    unsigned long start_data,end_data;
    unsigned long start_rodata,end_rodata;
    unsigned long start_brk,end_brk;
    unsigned long start_stack;  
};
  • pml4t_t *pgd; : CR3

pml4t_t (memory.h)

typedef struct {unsigned long pml4t;} pml4t_t;

thread_struct 用于保存執(zhí)行現(xiàn)場的寄存器值 (task.h)

struct thread_struct
{
    unsigned long rsp0; //in tss

    unsigned long rip;
    unsigned long rsp;  

    unsigned long fs;
    unsigned long gs;

    unsigned long cr2;
    unsigned long trap_nr;
    unsigned long error_code;
};

第一個進程 init_task_union

代碼布局

1st進程init_task_union 初始化 : init_thread垒酬、init_mm.png

聯(lián)合體 task_union (task.h)

// 32KB
#define STACK_SIZE 32768

union task_union
{
    struct task_struct task;
    unsigned long stack[STACK_SIZE / sizeof(unsigned long)];
}__attribute__((aligned (8)));  //8Bytes align


union task_union init_task_union 
__attribute__((__section__ (".data.init_task"))) 
= {INIT_TASK(init_task_union.task)};

  • 由聯(lián)合體 task_union的定義可見砰嘁,是一個task_struct結(jié)構(gòu)體加一個數(shù)組 ,之后聲明創(chuàng)建了全局變量 init_task_union 作為操作系統(tǒng)的第一個進程

  • 聯(lián)合體內(nèi)的數(shù)組空間勘究,就作為這個進程init_task_union棸妫空間來使用

  • task.h是內(nèi)核的一部分,更具體的說應(yīng)該是內(nèi)核代碼段的一部分口糕,這里聯(lián)合里定義了一個數(shù)組僅僅只是定義缅阳,并沒有在原地就開辟了一段空間出來給這個數(shù)組

  • union task_union init_task_union __attribute__((__section__ (".data.init_task"))) = {INIT_TASK(init_task_union.task)};才是真正的開辟空間,而修飾__attribute__((__section__ (".data.init_task")))意思就是把init_task_union這個聯(lián)合體放到名為.data.init_task的段里

  • 結(jié)合程序4-11運行結(jié)果圖景描,可以看到其検欤基地址是0x120000,因為這個聯(lián)合體的大小是32K= 32678=0x8000超棺,那么就可以計算出.data.init_task的段的起始地址0x1200000 - 0x8000 = 0x118000

  • 同時看截圖可以知道代碼段的結(jié)束地址0x10b5e0向族,很明顯聯(lián)合體init_task_union不在代碼段內(nèi)

  • 詳細布局參見配書代碼包Kernel.lds文件;

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末棠绘,一起剝皮案震驚了整個濱河市件相,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌氧苍,老刑警劉巖夜矗,帶你破解...
    沈念sama閱讀 218,122評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異让虐,居然都是意外死亡紊撕,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,070評論 3 395
  • 文/潘曉璐 我一進店門赡突,熙熙樓的掌柜王于貴愁眉苦臉地迎上來对扶,“玉大人,你說我怎么就攤上這事麸俘”缁” “怎么了?”我有些...
    開封第一講書人閱讀 164,491評論 0 354
  • 文/不壞的土叔 我叫張陵从媚,是天一觀的道長逞泄。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么喷众? 我笑而不...
    開封第一講書人閱讀 58,636評論 1 293
  • 正文 為了忘掉前任各谚,我火速辦了婚禮,結(jié)果婚禮上到千,老公的妹妹穿的比我還像新娘昌渤。我一直安慰自己,他們只是感情好憔四,可當(dāng)我...
    茶點故事閱讀 67,676評論 6 392
  • 文/花漫 我一把揭開白布膀息。 她就那樣靜靜地躺著,像睡著了一般了赵。 火紅的嫁衣襯著肌膚如雪潜支。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,541評論 1 305
  • 那天柿汛,我揣著相機與錄音冗酿,去河邊找鬼。 笑死络断,一個胖子當(dāng)著我的面吹牛裁替,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播貌笨,決...
    沈念sama閱讀 40,292評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼弱判,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了锥惋?” 一聲冷哼從身側(cè)響起裕循,我...
    開封第一講書人閱讀 39,211評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎净刮,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體硅则,經(jīng)...
    沈念sama閱讀 45,655評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡淹父,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,846評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了怎虫。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片暑认。...
    茶點故事閱讀 39,965評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖大审,靈堂內(nèi)的尸體忽然破棺而出蘸际,到底是詐尸還是另有隱情,我是刑警寧澤徒扶,帶...
    沈念sama閱讀 35,684評論 5 347
  • 正文 年R本政府宣布粮彤,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏导坟。R本人自食惡果不足惜屿良,卻給世界環(huán)境...
    茶點故事閱讀 41,295評論 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望惫周。 院中可真熱鬧尘惧,春花似錦、人聲如沸递递。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,894評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽登舞。三九已至贰逾,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間逊躁,已是汗流浹背似踱。 一陣腳步聲響...
    開封第一講書人閱讀 33,012評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留稽煤,地道東北人核芽。 一個月前我還...
    沈念sama閱讀 48,126評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像酵熙,于是被迫代替她去往敵國和親轧简。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,914評論 2 355