Skip to content
KeePromise edited this page Apr 22, 2021 · 1 revision

Loader design and implementation

1.design

The main function of loader is to detect computer hardware information, and then load the operating system to the specified physical memory 0x200000. Because the KePOS operating system has no file system function yet, the application program is simply saved to the physical memory address 0xd0000000. To save the application program to physical memory, the program needs to read the application program bin file (mymain.bin) from the floppy disk FAT12 file system, and then save it to the physical address 0xd0000000.

2.function

  • Detect computer SVGA information and set SVGA.
  • Find kernel.bin on the floppy disk and load it to physical address 0x200000.
  • Find mymain.bin on the floppy disk and load it to physical address 0xd0000000.
  • Set the segment table, page table, and enter the 64-bit long mode.
  • Jump to the kernel code for execution.

3.Part of the code

1.Address parameter :

;kernel.bin被加载到的内存地址
BaseOfKernelFile	equ	0x00
OffsetOfKernelFile	equ	0x200000	
;mymain.bin被加载到的内存地址
OffsetofuserFile    equ 0xd0000000

2.Segment table :

[SECTION gdt64]

LABEL_GDT64:		dq	0x0000000000000000
LABEL_DESC_CODE64:	dq	0x0020980000000000
LABEL_DESC_DATA64:	dq	0x0000920000000000

GdtLen64	equ	$ - LABEL_GDT64
;保存到GDTR寄存器的值
GdtPtr64	dw	GdtLen64 - 1
		dd	LABEL_GDT64

3.Page table :

;每条页表项占8B
	;PML4页表,基址是90000H
	mov	dword	[0x90000],	0x91007
	mov	dword	[0x90004],	0x00000

	mov	dword	[0x90800],	0x91007
	mov	dword	[0x90804],	0x00000

	;PDPT页表
	mov	dword	[0x91000],	0x92007
	mov	dword	[0x91004],	0x00000

	;PDT页表,最终的页表。
	;本次的页大小的2M,不是4k
	mov	dword	[0x92000],	0x000083
	mov	dword	[0x92004],	0x000000

	mov	dword	[0x92008],	0x200083
	mov	dword	[0x9200c],	0x000000

	mov	dword	[0x92010],	0x400083
	mov	dword	[0x92014],	0x000000

	mov	dword	[0x92018],	0x600083
	mov	dword	[0x9201c],	0x000000

	mov	dword	[0x92020],	0x800083
	mov	dword	[0x92024],	0x000000

	mov	dword	[0x92028],	0xa00083
	mov	dword	[0x9202c],	0x000000

4.Suggest

Recommended reading :《一个64位操作系统的设计与实现》(第 3 章 BootLoader引导启动程序)

Clone this wiki locally