Study Notes
Textbooks (source and methods of use with the book)
"A 64-bit Operating System Design and Implementation"
http://www.ituring.com.cn/book/2450
https://www.jianshu.com/p/28f9713a9171
Source structure
- Code package with the book: Chapter 4 \ program \ Program 4-6
- Code package with the book: Chapter 4 \ program \ Program 4-7
Run the debugger
[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
Obtaining information on the physical program memory 4-6
-
Each physical address space occupied by information 20B
Available memory
Type=0x0000 0001
(9f000h
+7fef0000h
) B =7ff8f000h
B ≈2047.55
MB about2
GB
4-7 calculation program available physical memory pages
- Physical page size is 2MB , the number of available physical page is 1022 (decimal)
- 4-7 of the program
e820
structure of the array of available physical memory segments 2MB physical page alignment boundary
- Align effect
#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;
}
C language compiler compile_c_online online
Reference material
-
INT 15h AX=e820h
Save the physical address space information to physical memory addresses0x7E00h
at
[OS64 bit] [014] Read Source: Listing 3-18 - 3-22 to read kernel memory kernel.bin 0x100000
https://www.jianshu.com/p/e9ed9f10bad6