學(xué)習(xí)筆記
使用教材(配書源碼以及使用方法)
《一個64位操作系統(tǒng)的設(shè)計與實現(xiàn)》
http://www.ituring.com.cn/book/2450
http://www.itdecent.cn/p/28f9713a9171
源碼結(jié)構(gòu)
- 配書代碼包 :第4章 \ 程序 \ 程序4-6
- 配書代碼包 :第4章 \ 程序 \ 程序4-7
運行調(diào)試
[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
ld -b elf64-x86-64 -z muldefs -o system head.o entry.o main.o printk.o trap.o memory.o -T Kernel.lds
objcopy -I elf64-x86-64 -S -R ".eh_frame" -R ".comment" -O binary system kernel.bin
[anno@localhost 4-7]$ ls
bochsrc boot.img bootloader kernel media
[anno@localhost 4-7]$ sudo mount boot.img media -t vfat -o loop
[anno@localhost 4-7]$ sudo cp bootloader/loader.bin media
[anno@localhost 4-7]$ sync
[anno@localhost 4-7]$ sudo cp bootloader/boot.bin media
[anno@localhost 4-7]$ sync
[anno@localhost 4-7]$ sudo cp kernel/kernel.bin media
[anno@localhost 4-7]$ sync
[anno@localhost 4-7]$ bochs -f ./bochsrc
程序4-6 獲得物理內(nèi)存的信息
-
每條物理地址空間信息占用 20B
物理地址空間結(jié)構(gòu)體 可用內(nèi)存
Type=0x0000 0001(9f000h+7fef0000h)B =7ff8f000hB ≈2047.55MB 約2GB
程序4-6 獲得物理內(nèi)存信息 可用內(nèi)存(9f000h + 7fef0000h)B = 7ff8f000h B ≈ 2047.55 MB約2 GB
bochsrc 配置文件設(shè)置的虛擬機內(nèi)存為 2GB
程序4-7 計算可用的物理內(nèi)存頁數(shù)
- 物理頁大小為2MB,可用的物理頁個數(shù)為1022(十進制)
程序4-7 計算可用的物理內(nèi)存頁數(shù)
程序4-7 結(jié)構(gòu)體 extern struct Global_Memory_Descriptor memory_management_struct.png
- 程序4-7 對
e820結(jié)構(gòu)體數(shù)組中的可用物理內(nèi)存段進行2MB物理頁邊界對齊
程序4-7 對e820結(jié)構(gòu)體數(shù)組中的可用物理內(nèi)存段進行2MB物理頁邊界對齊.png
- 對齊的效果
#include <stdio.h>
#define PAGE_2M_SHIFT 21
#define PAGE_4K_SHIFT 12
#define PAGE_2M_SIZE (1UL << PAGE_2M_SHIFT)
#define PAGE_4K_SIZE (1UL << PAGE_4K_SHIFT)
#define PAGE_2M_MASK (~ (PAGE_2M_SIZE - 1))
#define PAGE_4K_MASK (~ (PAGE_4K_SIZE - 1))
#define PAGE_2M_ALIGN(addr) (((unsigned long)(addr) + PAGE_2M_SIZE - 1) & PAGE_2M_MASK)
#define PAGE_4K_ALIGN(addr) (((unsigned long)(addr) + PAGE_4K_SIZE - 1) & PAGE_4K_MASK)
int main()
{
printf("unsigned long : %d byte\n", sizeof(unsigned long));
unsigned long add___ = 0x10AB;
unsigned long add_4K = PAGE_4K_ALIGN(add___);
printf("add___:%lx\n", add___);
printf("add_4K:%lx\n", add_4K);
unsigned long add_2M = PAGE_2M_ALIGN(add___);
printf("add___:%lx\n", add___);
printf("add_2M:%lx\n", add_2M);
return 0;
}
對齊的效果.PNG
參考資料
-
INT 15h AX=e820h保存物理地址空間信息到內(nèi)存物理地址0x7E00h處
[OS64位][014]源碼閱讀:代碼清單3-18 ~ 3-22 將內(nèi)核kernel.bin讀至內(nèi)存0x100000
http://www.itdecent.cn/p/e9ed9f10bad6





