memory management system

Preface

Blog records the operation of the experiment in Chapter 8 of "Operating System Truth Restore"~

Experimental environment: ubuntu18.04+VMware, download and install Bochs

Experiment content:

  1. Implement assert assertion.
  2. Implement string manipulation functions. (memset, memcpy, strcpy, strlen, strchr, etc.)
  3. Implement bitmaps for memory management.
  4. Implements the basic parts of the memory management system.
  5. Implement simple memory allocation based on Experiment 4.

prerequisite knowledge

makefile

Makefile understanding

Makefile definition: This file describes the relationship between the various files in the program and provides update commands for each file.

When executing the make command, a makefile is required to tell the make command how to compile and link the program.

basic grammar

The basic syntax of makefile consists of three parts

目标文件:依赖文件
[Tab]命令
  1. The target file refers to the file you want to generate in this rule. It can be a target file ending with .o, an executable file, or a pseudo target.
  2. Dependent files refer to which files are required to generate the target files in this rule.
  3. The command refers to the action to be performed in this rule. [Must start with Tab at the beginning of the line]

[Supplement 1] makeHow does the program determine that files need to be updated?

In Linux, files are divided into two parts: attributes and data. Each file has three times ( statviewable with commands). The make program obtains the mtime (modify time) of the dependent file and the target file respectively, compares whether the mtime of the dependent file is newer than the mtime of the target file, and determines whether to execute the command in the rule.

makefileIs the file name of [Supplement 2] fixed?

makefileThe file name is not fixed and can be specified with the -f parameter when executing make. If -f is not specified, by default, make will first look for a file named GNUmakefile. If the file does not exist, then it will look for a file named makefile. If makefile does not exist, it will finally look for a file named Makefile. document.

(For detailed makefile operations, please refer to the original book or Baidu official documentation)

bitmap

Experiment 3 used this knowledge.

Bitmap: Bitmap uses 1 bit in a byte to map resources of other unit sizes. The essence is mapping.

The OS can use bitmaps for memory management (as shown in the figure below). If a bitmap is used to manage memory, each bit in the bitmap will represent 4KB in the actual physical memory, which is one page. That is, one bit in the bitmap corresponds to one page in the physical memory. If a bit is 0, Indicates that the page corresponding to this bit is not allocated and available. Otherwise, it is temporarily not redistributable.

Insert image description here

Memory pool planning

The memory space occupied by user programs is allocated by the operating system.

In protected mode, the program address becomes a virtual address, and the physical address corresponding to the virtual address is mapped by the paging mechanism.

Since there are virtual addresses and physical addresses under the paging mechanism, management is easier, so we created a virtual memory address pool and a physical memory address pool .

First, let’s describe the planning of the physical memory pool (the schematic diagram is shown below).

Division of physical memory pool:

  • Part of it is only used to run the kernel, that is, the physical memory in this memory pool is only used by the operating system. This part is called the kernel physical memory pool.
  • The other part is only used to run user processes, that is, the physical memory in this memory pool is only allocated to user processes. This part is called the user physical memory pool.

The memory in the memory pool is obtained in units of pages, with one page being 4KB.

Insert image description here
That is, the physical memory is divided into two memory pools.

Virtual memory address pool (schematic diagram below)

  • Kernel: Here, we let the kernel apply for memory through the memory management system. To do this, it has a pool of virtual addresses. When it applies for memory, it allocates a virtual address from the kernel's own virtual address pool, then allocates physical memory from the kernel's physical memory pool (kernel-specific), and finally establishes a mapping relationship between the two addresses in the kernel's own page table.
  • User process: For a user process, when it applies for memory from the memory management system, that is, the operating system, the operating system first allocates free virtual addresses from the user process's own virtual address pool, and then allocates free virtual addresses from the user physical memory pool (all users Allocate free physical memory in the process shared), and then establish a mapping relationship between the two addresses in the user process's own page table.

Insert image description here

Experimental operation

experiment one

This experiment implements assert assertion

(base) user@ubuntu:/home/cooiboi/bochs/kernel$ sudo vim debug.c
(base) user@ubuntu:/home/cooiboi/bochs/kernel$ sudo vim debug.h
(base) user@ubuntu:/home/cooiboi/bochs/kernel$ sudo vim interrupt.c
(base) user@ubuntu:/home/cooiboi/bochs/kernel$ sudo vim interrupt.h
(base) user@ubuntu:/home/cooiboi/bochs/kernel$ sudo vim  main.c

Write a makefile (note the path of your computer file)

(base) user@ubuntu:/home/cooiboi/bochs$ sudo vim makefile
BUILD_DIR = ./build
ENTRY_POINT = 0xc0001500
AS = nasm
CC = gcc
LD = ld
LIB = -I lib/ -I lib/kernel/ -I lib/user/ -I kernel/ -I device/ 
ASFLAGS = -f elf
CFLAGS = -Wall -m32 -fno-stack-protector $(LIB) -c -fno-builtin -W -Wstrict-prototypes -Wmissing-prototypes
LDFLAGS =  -m elf_i386 -Ttext $(ENTRY_POINT) -e main -Map $(BUILD_DIR)/kernel.map
OBJS = $(BUILD_DIR)/main.o $(BUILD_DIR)/init.o $(BUILD_DIR)/interrupt.o \
      $(BUILD_DIR)/timer.o $(BUILD_DIR)/kernel.o $(BUILD_DIR)/print.o \
      $(BUILD_DIR)/debug.o 

##############     c代码编译     			###############
##############     后面的代码以后照本宣科即可		###############
$(BUILD_DIR)/main.o: kernel/main.c lib/kernel/print.h \
        lib/stdint.h kernel/init.h
	$(CC) $(CFLAGS) $< -o $@

$(BUILD_DIR)/init.o: kernel/init.c kernel/init.h lib/kernel/print.h \
        lib/stdint.h kernel/interrupt.h device/timer.h
	$(CC) $(CFLAGS) $< -o $@

$(BUILD_DIR)/interrupt.o: kernel/interrupt.c kernel/interrupt.h \
        lib/stdint.h kernel/global.h lib/kernel/io.h lib/kernel/print.h
	$(CC) $(CFLAGS) $< -o $@

$(BUILD_DIR)/timer.o: device/timer.c device/timer.h lib/stdint.h\
        lib/kernel/io.h lib/kernel/print.h
	$(CC) $(CFLAGS) $< -o $@

$(BUILD_DIR)/debug.o: kernel/debug.c kernel/debug.h \
        lib/kernel/print.h lib/stdint.h kernel/interrupt.h
	$(CC) $(CFLAGS) $< -o $@


##############    汇编代码编译    ###############
$(BUILD_DIR)/kernel.o: kernel/kernel.S
	$(AS) $(ASFLAGS) $< -o $@

$(BUILD_DIR)/print.o: lib/kernel/print.S
	$(AS) $(ASFLAGS) $< -o $@

##############    链接所有目标文件    #############
$(BUILD_DIR)/kernel.bin: $(OBJS)
	$(LD) $(LDFLAGS) $^ -o $@

.PHONY : mk_dir hd clean all

mk_dir:
	if [ ! -d $(BUILD_DIR) ]; then mkdir $(BUILD_DIR); fi

hd:
	dd if=$(BUILD_DIR)/kernel.bin \
           of=/home/cooiboi/bochs/boot/hd60M.img \
           bs=512 count=200 seek=9 conv=notrunc

clean:
	cd $(BUILD_DIR) && rm -f  ./*

build: $(BUILD_DIR)/kernel.bin

all: mk_dir build hd

Enter root mode first, otherwise it will appearPermission denied

(base) user@ubuntu:/home/cooiboi/bochs$ su
Password: 

execute make allcommand

root@ubuntu:/home/cooiboi/bochs# make all
if [ ! -d ./build ]; then mkdir ./build; fi
gcc -Wall -m32 -fno-stack-protector -I lib/ -I lib/kernel/ -I lib/user/ -I kernel/ -I device/  -c -fno-builtin -W -Wstrict-prototypes -Wmissing-prototypes kernel/init.c -o build/init.o
gcc -Wall -m32 -fno-stack-protector -I lib/ -I lib/kernel/ -I lib/user/ -I kernel/ -I device/  -c -fno-builtin -W -Wstrict-prototypes -Wmissing-prototypes kernel/interrupt.c -o build/interrupt.o
gcc -Wall -m32 -fno-stack-protector -I lib/ -I lib/kernel/ -I lib/user/ -I kernel/ -I device/  -c -fno-builtin -W -Wstrict-prototypes -Wmissing-prototypes kernel/debug.c -o build/debug.o
ld -m elf_i386 -Ttext 0xc0001500 -e main -Map ./build/kernel.map build/main.o build/init.o build/interrupt.o build/timer.o build/kernel.o build/print.o build/debug.o -o build/kernel.bin
dd if=./build/kernel.bin \
           of=/home/cooiboi/bochs/boot/hd60M.img \
           bs=512 count=200 seek=9 conv=notrunc
22+1 records in
22+1 records out
11732 bytes (12 kB, 11 KiB) copied, 0.000227052 s, 51.7 MB/s

Start Bochs

sudo bin/bochs -f boot/bochsrc.disk

Insert image description here

Experiment 2

This experiment implements functions related to strings

For example memset, memcpy, strcpy, strlen, strchr, etc.

Create string.c and string.h under lib

root@ubuntu:/home/cooiboi/bochs/lib# sudo vim string.c
root@ubuntu:/home/cooiboi/bochs/lib# sudo vim string.h
root@ubuntu:/home/cooiboi/bochs/lib# ls
kernel  stdint.h  string.c  string.h  user

Modify main.c to output Hello OSthe length and comparison assertion operation

root@ubuntu:/home/cooiboi/bochs/kernel# sudo vim  main.c
#include "print.h"
#include "init.h"
#include "debug.h"
#include "string.h"

int main(void) {
    
    
   put_str("I am kernel\n");
   init_all();
   put_str("length:");
   put_int(strlen("Hello OS"));
   ASSERT(strcmp("bbb","bbb"));
   while(1);
}

Modify the makefile file, a total of 3 modifications

OBJS = $(BUILD_DIR)/main.o $(BUILD_DIR)/init.o $(BUILD_DIR)/interrupt.o \
      $(BUILD_DIR)/timer.o $(BUILD_DIR)/kernel.o $(BUILD_DIR)/print.o \
      $(BUILD_DIR)/debug.o $(BUILD_DIR)/string.o 
$(BUILD_DIR)/main.o: kernel/main.c lib/kernel/print.h \
        lib/stdint.h kernel/init.h lib/string.h
	$(CC) $(CFLAGS) $< -o $@     
$(BUILD_DIR)/string.o: lib/string.c lib/string.h \
        lib/stdint.h  kernel/debug.h  lib/string.h kernel/global.h
	$(CC) $(CFLAGS) $< -o $@

Complete makefile

BUILD_DIR = ./build
ENTRY_POINT = 0xc0001500
AS = nasm
CC = gcc
LD = ld
LIB = -I lib/ -I lib/kernel/ -I lib/user/ -I kernel/ -I device/ 
ASFLAGS = -f elf
CFLAGS = -Wall -m32 -fno-stack-protector $(LIB) -c -fno-builtin -W -Wstrict-prototypes -Wmissing-prototypes
LDFLAGS =  -m elf_i386 -Ttext $(ENTRY_POINT) -e main -Map $(BUILD_DIR)/kernel.map
OBJS = $(BUILD_DIR)/main.o $(BUILD_DIR)/init.o $(BUILD_DIR)/interrupt.o \
      $(BUILD_DIR)/timer.o $(BUILD_DIR)/kernel.o $(BUILD_DIR)/print.o \
      $(BUILD_DIR)/debug.o $(BUILD_DIR)/string.o 

##############     c代码编译     			###############
##############     后面的代码以后照本宣科即可		###############
$(BUILD_DIR)/main.o: kernel/main.c lib/kernel/print.h \
        lib/stdint.h kernel/init.h lib/string.h
	$(CC) $(CFLAGS) $< -o $@

$(BUILD_DIR)/init.o: kernel/init.c kernel/init.h lib/kernel/print.h \
        lib/stdint.h kernel/interrupt.h device/timer.h
	$(CC) $(CFLAGS) $< -o $@

$(BUILD_DIR)/interrupt.o: kernel/interrupt.c kernel/interrupt.h \
        lib/stdint.h kernel/global.h lib/kernel/io.h lib/kernel/print.h
	$(CC) $(CFLAGS) $< -o $@

$(BUILD_DIR)/timer.o: device/timer.c device/timer.h lib/stdint.h\
        lib/kernel/io.h lib/kernel/print.h
	$(CC) $(CFLAGS) $< -o $@

$(BUILD_DIR)/debug.o: kernel/debug.c kernel/debug.h \
        lib/kernel/print.h lib/stdint.h kernel/interrupt.h
	$(CC) $(CFLAGS) $< -o $@
	
$(BUILD_DIR)/string.o: lib/string.c lib/string.h \
        lib/stdint.h  kernel/debug.h  lib/string.h kernel/global.h
	$(CC) $(CFLAGS) $< -o $@

##############    汇编代码编译    ###############
$(BUILD_DIR)/kernel.o: kernel/kernel.S
	$(AS) $(ASFLAGS) $< -o $@

$(BUILD_DIR)/print.o: lib/kernel/print.S
	$(AS) $(ASFLAGS) $< -o $@

##############    链接所有目标文件    #############
$(BUILD_DIR)/kernel.bin: $(OBJS)
	$(LD) $(LDFLAGS) $^ -o $@

.PHONY : mk_dir hd clean all

mk_dir:
	if [ ! -d $(BUILD_DIR) ]; then mkdir $(BUILD_DIR); fi

hd:
	dd if=$(BUILD_DIR)/kernel.bin \
           of=/home/cooiboi/bochs/boot/hd60M.img \
           bs=512 count=200 seek=9 conv=notrunc

clean:
	cd $(BUILD_DIR) && rm -f  ./*

build: $(BUILD_DIR)/kernel.bin

all: mk_dir build hd
make all
root@ubuntu:/home/cooiboi/bochs# make all
if [ ! -d ./build ]; then mkdir ./build; fi
gcc -Wall -m32 -fno-stack-protector -I lib/ -I lib/kernel/ -I lib/user/ -I kernel/ -I device/  -c -fno-builtin -W -Wstrict-prototypes -Wmissing-prototypes kernel/main.c -o build/main.o
gcc -Wall -m32 -fno-stack-protector -I lib/ -I lib/kernel/ -I lib/user/ -I kernel/ -I device/  -c -fno-builtin -W -Wstrict-prototypes -Wmissing-prototypes lib/string.c -o build/string.o
ld -m elf_i386 -Ttext 0xc0001500 -e main -Map ./build/kernel.map build/main.o build/init.o build/interrupt.o build/timer.o build/kernel.o build/print.o build/debug.o build/string.o -o build/kernel.bin
dd if=./build/kernel.bin \
           of=/home/cooiboi/bochs/boot/hd60M.img \
           bs=512 count=200 seek=9 conv=notrunc
23+1 records in
23+1 records out
12284 bytes (12 kB, 12 KiB) copied, 0.000235485 s, 52.2 MB/s

BUG: NULL is not defined

root@ubuntu:/home/cooiboi/bochs# sudo make all
if [ ! -d ./build ]; then mkdir ./build; fi
gcc -Wall -m32 -fno-stack-protector -I lib/ -I lib/kernel/ -I lib/user/ -I kernel/ -I device/  -c -fno-builtin -W -Wstrict-prototypes -Wmissing-prototypes lib/string.c -o build/string.o
In file included from lib/string.c:3:0:
lib/string.c: In function ‘memset’:
lib/string.c:7:19: error: ‘NULL’ undeclared (first use in this function)
    ASSERT(dst_ != NULL);
                   ^
kernel/debug.h:16:11: note: in definition of macro ‘ASSERT’
       if (CONDITION) {
    
    } else {
    
                                        \
           ^
lib/string.c:7:19: note: each undeclared identifier is reported only once for each function it appears in
    ASSERT(dst_ != NULL);
                   ^
kernel/debug.h:16:11: note: in definition of macro ‘ASSERT’
       if (CONDITION) {
    
    } else {
    
                                        \
           ^
lib/string.c: In function ‘memcpy’:
lib/string.c:15:19: error: ‘NULL’ undeclared (first use in this function)
    ASSERT(dst_ != NULL && src_ != NULL);
                   ^
kernel/debug.h:16:11: note: in definition of macro ‘ASSERT’
       if (CONDITION) {
    
    } else {
    
                                        \
           ^
lib/string.c: In function ‘memcmp’:
lib/string.c:26:16: error: ‘NULL’ undeclared (first use in this function)
    ASSERT(a != NULL || b != NULL);
                ^
kernel/debug.h:16:11: note: in definition of macro ‘ASSERT’
       if (CONDITION) {
    
    } else {
    
                                        \
           ^
lib/string.c: In function ‘strcpy’:
lib/string.c:39:19: error: ‘NULL’ undeclared (first use in this function)
    ASSERT(dst_ != NULL && src_ != NULL);
                   ^
kernel/debug.h:16:11: note: in definition of macro ‘ASSERT’
       if (CONDITION) {
    
    } else {
    
                                        \
           ^
lib/string.c: In function ‘strlen’:
lib/string.c:47:18: error: ‘NULL’ undeclared (first use in this function)
    ASSERT(str != NULL);
                  ^
kernel/debug.h:16:11: note: in definition of macro ‘ASSERT’
       if (CONDITION) {
    
    } else {
    
                                        \
           ^
lib/string.c: In function ‘strcmp’:
lib/string.c:55:16: error: ‘NULL’ undeclared (first use in this function)
    ASSERT(a != NULL && b != NULL);
                ^
kernel/debug.h:16:11: note: in definition of macro ‘ASSERT’
       if (CONDITION) {
    
    } else {
    
                                        \
           ^
lib/string.c: In function ‘strchr’:
lib/string.c:67:18: error: ‘NULL’ undeclared (first use in this function)
    ASSERT(str != NULL);
                  ^
kernel/debug.h:16:11: note: in definition of macro ‘ASSERT’
       if (CONDITION) {
    
    } else {
    
                                        \
           ^
lib/string.c: In function ‘strrchr’:
lib/string.c:79:18: error: ‘NULL’ undeclared (first use in this function)
    ASSERT(str != NULL);
                  ^
kernel/debug.h:16:11: note: in definition of macro ‘ASSERT’
       if (CONDITION) {
    
    } else {
    
                                        \
           ^
lib/string.c: In function ‘strcat’:
lib/string.c:93:19: error: ‘NULL’ undeclared (first use in this function)
    ASSERT(dst_ != NULL && src_ != NULL);
                   ^
kernel/debug.h:16:11: note: in definition of macro ‘ASSERT’
       if (CONDITION) {
    
    } else {
    
                                        \
           ^
lib/string.c: In function ‘strchrs’:
lib/string.c:103:18: error: ‘NULL’ undeclared (first use in this function)
    ASSERT(str != NULL);
                  ^
kernel/debug.h:16:11: note: in definition of macro ‘ASSERT’
       if (CONDITION) {
    
    } else {
    
                                        \
           ^
lib/string.c: In function ‘strchr’:
lib/string.c:75:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
makefile:38: recipe for target 'build/string.o' failed
make: *** [build/string.o] Error 1

Solution :

Add in lib/string.h file#define NULL 0

Insert image description here


Start Bochs

sudo bin/bochs -f boot/bochsrc.disk
root@ubuntu:/home/cooiboi/bochs# sudo bin/bochs -f boot/bochsrc.disk

Insert image description here

Experiment 3

Implement bitmaps for memory management.

Create bitmap.c and bitmap.h files in the lib/kernel directory.

root@ubuntu:/home/cooiboi/bochs/lib/kernel# sudo vim bitmap.c
root@ubuntu:/home/cooiboi/bochs/lib/kernel# sudo vim bitmap.h

lib/kernel/bitmap.h

Insert image description here
struct bitmap defines two member variables: bitmap pointer bits and bitmap byte length btmp_bytes_len.

Experiment 4

Implement the basic parts of the memory management system

(There is a problem with this part of the code, you can skip it directly and go to the next experiment)

Create memory.c and memory.h files in the kernel directory.

root@ubuntu:/home/cooiboi/bochs/kernel# vim memory.c
root@ubuntu:/home/cooiboi/bochs/kernel# vim memory.h

Added a #include "memory.h" and a mem_init(); under /kernel/init.c.

root@ubuntu:/home/cooiboi/bochs/kernel# vim init.c

Modify the makefile and make 3 changes

OBJS = $(BUILD_DIR)/main.o $(BUILD_DIR)/init.o $(BUILD_DIR)/interrupt.o \
      $(BUILD_DIR)/timer.o $(BUILD_DIR)/kernel.o $(BUILD_DIR)/print.o \
      $(BUILD_DIR)/debug.o $(BUILD_DIR)/string.o $(BUILD_DIR)/memory.o
$(BUILD_DIR)/main.o: kernel/main.c lib/kernel/print.h \
        lib/stdint.h kernel/init.h lib/string.h kernel/memory.h
        $(CC) $(CFLAGS) $< -o $@
$(BUILD_DIR)/memory.o: kernel/memory.c kernel/memory.h lib/stdint.h lib/kernel/bitmap.h \
   	kernel/global.h kernel/global.h kernel/debug.h lib/kernel/print.h \
	lib/kernel/io.h kernel/interrupt.h lib/string.h lib/stdint.h
	$(CC) $(CFLAGS) $< -o $@

bitmap.h

Need to add

typedef int bool;
root@ubuntu:/home/cooiboi/bochs# make all
if [ ! -d ./build ]; then mkdir ./build; fi
gcc -Wall -m32 -fno-stack-protector -I lib/ -I lib/kernel/ -I lib/user/ -I kernel/ -I device/  -c -fno-builtin -W -Wstrict-prototypes -Wmissing-prototypes kernel/main.c -o build/main.o
gcc -Wall -m32 -fno-stack-protector -I lib/ -I lib/kernel/ -I lib/user/ -I kernel/ -I device/  -c -fno-builtin -W -Wstrict-prototypes -Wmissing-prototypes kernel/init.c -o build/init.o
In file included from kernel/memory.h:4:0,
                 from kernel/init.c:5:
lib/kernel/bitmap.h:12:1: error: unknown type name ‘bool’
 bool bitmap_scan_test(struct bitmap* btmp, uint32_t bit_idx);
 ^
makefile:22: recipe for target 'build/init.o' failed
make: *** [build/init.o] Error 1

Experiment 5

Implement simple memory allocation based on Experiment 4.

Create memory.c and memory.h files in the kernel directory.

root@ubuntu:/home/cooiboi/bochs/kernel# vim memory.c
root@ubuntu:/home/cooiboi/bochs/kernel# vim memory.h

Added a #include "memory.h" and a mem_init(); under /kernel/init.c.

root@ubuntu:/home/cooiboi/bochs/kernel# vim init.c

Added to /lib/kernel/bitmap.htypedef int bool;

Insert image description here

Modify the makefile (note: you need to modify it according to your actual directory)

BUILD_DIR = ./build
ENTRY_POINT = 0xc0001500
AS = nasm
CC = gcc
LD = ld
LIB = -I lib/ -I lib/kernel/ -I lib/user/ -I kernel/ -I device/ 
ASFLAGS = -f elf
CFLAGS = -Wall -m32 -fno-stack-protector $(LIB) -c -fno-builtin -W -Wstrict-prototypes -Wmissing-prototypes
LDFLAGS =  -m elf_i386 -Ttext $(ENTRY_POINT) -e main -Map $(BUILD_DIR)/kernel.map
OBJS = $(BUILD_DIR)/main.o $(BUILD_DIR)/init.o $(BUILD_DIR)/interrupt.o \
      $(BUILD_DIR)/timer.o $(BUILD_DIR)/kernel.o $(BUILD_DIR)/print.o \
      $(BUILD_DIR)/debug.o $(BUILD_DIR)/string.o $(BUILD_DIR)/memory.o \
      $(BUILD_DIR)/bitmap.o
##############     c代码编译     ###############
$(BUILD_DIR)/main.o: kernel/main.c lib/kernel/print.h \
        lib/stdint.h kernel/init.h lib/string.h kernel/memory.h
	$(CC) $(CFLAGS) $< -o $@

$(BUILD_DIR)/init.o: kernel/init.c kernel/init.h lib/kernel/print.h \
        lib/stdint.h kernel/interrupt.h device/timer.h kernel/memory.h
	$(CC) $(CFLAGS) $< -o $@

$(BUILD_DIR)/interrupt.o: kernel/interrupt.c kernel/interrupt.h \
        lib/stdint.h kernel/global.h lib/kernel/io.h lib/kernel/print.h
	$(CC) $(CFLAGS) $< -o $@

$(BUILD_DIR)/timer.o: device/timer.c device/timer.h lib/stdint.h\
        lib/kernel/io.h lib/kernel/print.h
	$(CC) $(CFLAGS) $< -o $@

$(BUILD_DIR)/debug.o: kernel/debug.c kernel/debug.h \
        lib/kernel/print.h lib/stdint.h kernel/interrupt.h
	$(CC) $(CFLAGS) $< -o $@
	
$(BUILD_DIR)/string.o: lib/string.c lib/string.h \
	kernel/debug.h kernel/global.h
	$(CC) $(CFLAGS) $< -o $@
	
$(BUILD_DIR)/memory.o: kernel/memory.c kernel/memory.h \
	lib/stdint.h lib/kernel/bitmap.h kernel/debug.h lib/string.h
	$(CC) $(CFLAGS) $< -o $@
	
$(BUILD_DIR)/bitmap.o: lib/kernel/bitmap.c lib/kernel/bitmap.h \
	lib/string.h kernel/interrupt.h lib/kernel/print.h kernel/debug.h
	$(CC) $(CFLAGS) $< -o $@

##############    汇编代码编译    ###############
$(BUILD_DIR)/kernel.o: kernel/kernel.S
	$(AS) $(ASFLAGS) $< -o $@

$(BUILD_DIR)/print.o: lib/kernel/print.S
	$(AS) $(ASFLAGS) $< -o $@

##############    链接所有目标文件    #############
$(BUILD_DIR)/kernel.bin: $(OBJS)
	$(LD) $(LDFLAGS) $^ -o $@

.PHONY : mk_dir hd clean all

mk_dir:
	if [ ! -d $(BUILD_DIR) ]; then mkdir $(BUILD_DIR); fi

hd:
	dd if=$(BUILD_DIR)/kernel.bin \
           of=/home/cooiboi/bochs/boot/hd60M.img \
           bs=512 count=200 seek=9 conv=notrunc

clean:
	cd $(BUILD_DIR) && rm -f  ./*

build: $(BUILD_DIR)/kernel.bin

all: mk_dir build hd

Start Bochs~

Insert image description here

Use to info tabview the mapping relationship between virtual addresses and physical addresses in the page table.

The left side is the range of virtual addresses, and the right side is the mapped physical address.

The virtual address range is 0x00100000~0x00104fff, and the mapped physical address range is 0x200000~0x204fff.
Insert image description here
We can also page 虚拟地址get the physical address using .

Insert image description here

References

おすすめ

転載: blog.csdn.net/weixin_42888638/article/details/128627195