[OS64 Bit] [020] Read Source: 4-7 calculation program available physical memory pages

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

    10191360-819318f314477545.png
    Physical address space of the structure

  • Available memory Type=0x0000 0001( 9f000h+ 7fef0000h) B = 7ff8f000h B2047.55 MB about 2 GB

10191360-4b8e5926f42ab26a.png
4-6 program information to obtain the physical memory available memory (9f000h + 7fef0000h) B = 7ff8f000h B ≈ 2047.55 MB about 2 GB

10191360-0d59f9dfc410d3a8.png
bochsrc profile settings of virtual machine memory to 2GB

4-7 calculation program available physical memory pages

  • Physical page size is 2MB , the number of available physical page is 1022 (decimal)
10191360-1e5e348a48522bac.png
4-7 calculation program available physical memory pages

10191360-11a7afc9f8c9a382.png
Program structure 4-7 extern struct Global_Memory_Descriptor memory_management_struct.png
  • 4-7 of the program e820structure of the array of available physical memory segments 2MB physical page alignment boundary
10191360-11a03292613bf216.png
Program 4-7 e820 structural array available physical memory segments 2MB physical page boundary alignment .png
  • 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

10191360-2d4841816035dcdb.PNG
Align effect .PNG

Reference material

  • INT 15h AX=e820hSave the physical address space information to physical memory addresses 0x7E00hat

[OS64 bit] [014] Read Source: Listing 3-18 - 3-22 to read kernel memory kernel.bin 0x100000
https://www.jianshu.com/p/e9ed9f10bad6

Guess you like

Origin blog.csdn.net/weixin_33674437/article/details/90968827