1 /************************************************************************* 2 > File Name: boot.s 3 > Author: Linpeng1577 4 > mail:[email protected] or [email protected] 5 > Created Time: Sun 02 Nov 2014 11:42:53 pm pst 6 ************************************************************************/ 1 ! boot.s ! code from Zhaobo's "Linux kernel full anatomy: Based on 0.12 cores", ! here is the author's reading comprehension 2 bootseg = 0x07c0 ! here is actually 0x07c00, in real mode, 80386 for compatibility with 8086, !80386 address will automatically move left 4-bit 3 SYSSEG = 0x1000 ! here is actually 0x10000, the reason ibid 4 SYSLEN = 17 ! system Sector 17 x 5 entry start ! Entrance Tags 6 start: ! ditto 7 jmpi go, #BOOTSEG ! start, in real mode, JMPI will CS:EIP automatically assigned value, CS segment point to Bootseg 8 go: mov ax,cs ! AX works as temp because there is no direct assignment between segment registers 9 mov ds,ax !ds segment point to Bootseg 10 mov ss,ax &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;!SS segment point to bootseg 11 &NBSP;&NBSP;&NBSP;MOV&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SP, #400 ! set a temporary stack pointer with a value greater than the end of the program and a certain amount of space 12! loading systemUnified Code 13 load_system: 14 mov &NBSP;&NBSP;&NBSP;DX, #0x0000 15 mov &NBSP;&NBSP;&NBSP;CX, #0x0002 16 mov ax, #SYSSEG 17 mov es,ax 18 xor bx,bx 19 mov ax, #0x200 +syslen 20 int 0x13 21 jnc ok_load 22 die: jmp die 23! Move the kernel code to memory 0 at the beginning 24 ok_load: 25 cli ! 26 mov ax, #SYSSEG ! 27 mov ds,ax ! 28 xor ax,ax ! 29 mov es,ax ! 30 mov cs, #0x1000 ! 31 sub si,si ! 32 sub di ,di ! 33 rep movw ! 18 xor bx,bx ! 19 mov ax, #0x200 +syslen! 20 int 0x13 ! 21 jnc ok_load ! 22 die: jmp die ! 23 24 ok_load: 25 cli 26 mov ax, #SYSSEG 27 mov ds,ax 28 xor ax,ax 29 mov es,ax 30 mov cs, #0x1000 31 sub si,si 32 sub di,di 33 rep movw 34 35 mov ax, #BOOTSEG 36 mov ds,ax 37 lidt idt_48 ! Loading IDT Table 38 lgdt gdt_48 ! Loading GDT Table 39 40 mov ax, #0x0001 41 lmsw ax ! The LMSW directive sets the CR0 protection flag bit PE to 0X0001, which is entered in the protected mode by the real mode 42 jmpi 0,8 ! history Jump, here end real mode, turn on protection mode, 8 is segment selector, !0 is the offset address, The binary of !8 is 1000, ! Description is the first segment of the GDT 43 44 gdt: .word 0,0,0,0 ! Segment Descriptor 0 No 45 46 .word 0x07ff ! Segment Descriptor 1,x86 is a small-end mode CPU, the first word is a low address, indicating a segment limit length 47 .word 0x0000 ! bit 0.. 15,16..23,24..31 are 0, so the base of the segment is 0x00000 48 .word 0x9a00 ! Code Snippets 49 .word 0X00C0 50 51 .word 0x07FF ! Segment Descriptor 2 52 .word 0x0000 ! segment base is 0x00000, same as code snippet &NBSP;53&NBSP;&NBSP;&Nbsp; .word 0x9a00 ! Data Segment 54 .word 0x00C0 55 56 idt_48: .word 0 !idt table length is 0 57 .word 0,0 The &NBSP;&NBSP;!LIDT instruction will load 6 words of information, 2 words of length, 4 words of the address value 58 gdt_48: .word 0x7ff !gdt table length 0x7ff, decimal is 2048, each descriptor 8 bytes, there are 256 descriptors 59 .word 0x7c00+gdt,0 ! Before loading is real mode, so the base address is the segment base 0x7c00 (Note not 0x07c0, ! address and value do not confuse) +gdt label offset (the assembly label is actually the offset address in the paragraph, ! compiler automatically assigns values) 60 .org 510 ! a sector has 512 bytes, in the last byte, the assignment value 0xaa55, which indicates that this is a valid sector 61 . word 0xaa55 ! Boot Sector valid flag <linux/ test_boot/boot.s [format=unix] [type=asm] [pos=18,21][30%] 07/11/14 - 22:58
This article is from the "mountain Ask the Boy" blog, please be sure to keep this source http://linpeng.blog.51cto.com/9779987/1596726
Linux kernel 0.00-boot.s Source code Analysis