您的位置:首页 > 运维架构 > Linux

[代码实例][Linux内核编程]内核开发:1、从实模式到保护模式

2017-03-16 19:29 531 查看
bootsect.s:

BOOT_SEG = 0x07C0
SETUP_SEG=0x1000
SETUP_LEN=10

.code16
.section .text
.global _start
_start:
ljmp $BOOT_SEG, $go
go:
mov %cs, %ax        /* Init segment registers */
mov %ax, %ds
mov %ax, %es
mov %ax, %fs
mov %ax, %gs
mov %ax, %ss
mov $0x400, %sp

mov $0x0003, %ax   /* Init VGA mode */
int $0x10

mov $0x1301, %ax   /* Display message */
mov $0x0002, %bx
mov MsgLoadSetupLen, %cx
mov CurrentCursorRow, %dh
xor %dl, %dl
mov $MsgLoadSetup, %bp
int $0x10
incb CurrentCursorRow

push %es            /* Load setup program */
mov $SETUP_SEG, %ax
mov %ax, %es
xor %bx, %bx
mov $0x0200 + SETUP_LEN, %ax
mov $0x0002, %cx
mov $0x0, %dx
int $0x13
pop %es

mov $0x1301, %ax   /* Display message */
mov $0x0002, %bx
mov MsgInitSystemLen, %cx
mov CurrentCursorRow, %dh
xor %dl, %dl
mov $MsgInitSystem, %bp
int $0x10
incb CurrentCursorRow

cli         /* Now we begin to initialize system */

push %ds
push %es
mov $SETUP_SEG, %ax
mov %ax, %ds
xor %ax, %ax
mov %ax, %es
mov $SETUP_LEN, %cx
sal $8, %cx
xor %si, %si
xor %di, %di
rep movsw
pop %es
pop %ds

lidt LidtOpcode
lgdt LgdtOpcode
smsw %ax
or $1, %ax
lmsw %ax

ljmp $8, $0

CpuHalt:
hlt
jmp CpuHalt

MsgLoadSetup:
.ascii "[Bootsect] Loading setup program..."
MsgLoadSetupLen:
.word .-MsgLoadSetup

MsgInitSystem:
.ascii "[Bootsect] Initializing system..."
MsgInitSystemLen:
.word .-MsgInitSystem

CurrentCursorRow:
.byte 0

LidtOpcode:
.word           # IDT limit = 0
.word 0, 0      # IDT base = 0
LgdtOpcode:
.word 0x7FF
.word 0x7C00 + GdtInit, 0

GdtInit:
.word 0, 0, 0, 0

.word 0x07FF
.word 0x0000
.word 0x9A00
.word 0x00C0

.word 0x07FF
.word 0x0000
.word 0x9200
.word 0x00C0

.org 510
.word 0xAA55


head.s:

Selector_InitCode = 0x08
Selector_InitData = 0x10
Selector_VGA = 0x18
Selector_KernelCode = 0x20
Selector_KernelData = 0x28

.code32

.section .text
.global _start
_start:
mov $Selector_InitData, %eax
mov %ax, %ds
mov %ax, %es
mov %ax, %fs
mov %ax, %gs
lss InitStack, %esp

call SetupGdt
call SetupIdt

mov $Selector_InitData, %eax
mov %ax, %ds
mov %ax, %es
mov %ax, %fs
mov %ax, %gs
lss InitStack, %esp

ljmp $Selector_KernelCode, $go
go:
hlt

SetupGdt:
lgdt LgdtOpcode
ret

SetupIdt:
lea IgnoreInterrupt, %edx
mov $0x00080000, %eax
mov %dx, %ax
mov $0x8E00, %dx
lea Idt, %edi
mov $256, %ecx
rp_idt:
mov %eax, (%edi)
mov %edx, 4(%edi)
add $8, %edi
dec %ecx
jne rp_idt
lidt LidtOpcode
ret

.align 4
IgnoreInterrupt:
iret

.align 4
LidtOpcode:
.word 256 * 8 -1
.long Idt
LgdtOpcode:
.word 256 * 8 -1
.long Gdt

.align 8
Idt:
.fill 256, 8, 0
Gdt:
.quad 0x0000000000000000    # 0x00 - NULL descriptor
.quad 0x00C09A00000007FF    # 0x08 - Init code segment descriptor
.quad 0x00C09200000007FF    # 0x10 - Init data segment descriptor
.quad 0x00C0920B80000007    # 0x18 - VGA memory segment descriptor
.quad 0x00CB9A00000007FF    # 0x20 - Kernel code segment descriptor
.quad 0x00CB9200000007FF    # 0x28 - Kernel data segment descriptor
.fill 250, 8, 0
EndGdt:
.fill 128, 4, 0
InitStack:
.long InitStack
.word Selector_InitData


Makefile:

all: Image

Image: bootsect head
dd if=/dev/zero of=Image bs=1440K count=1
dd if=bootsect of=Image bs=512 conv=notrunc
dd if=head of=Image bs=512 count=10 seek=1 conv=notrunc

bootsect: bootsect.o
ld --oformat binary -Ttext=0x0 -o $@ $^
bootsect.o: bootsect.s
as -o $@ $^

head: head.o
ld --oformat binary -m elf_i386 -Ttext=0x0 -o $@ $^
head.o: head.s
as --32 -o $@ $^

clean:
rm -f bootsect.o bootsect head.o head Image


bochsrc:

# configuration file generated by Bochs
plugin_ctrl: unmapped=1, biosdev=1, speaker=1, extfpuirq=1, parallel=1, serial=1, iodebug=1
config_interface: textconfig
display_library: x
memory: host=32, guest=32
romimage: file="/usr/local/share/bochs/BIOS-bochs-latest", address=0x0, options=none
vgaromimage: file="/usr/local/share/bochs/VGABIOS-lgpl-latest"
boot: floppy
floppy_bootsig_check: disabled=0
floppya: type=1_44, 1_44="Image", status=inserted, write_protected=0
# no floppyb
ata0: enabled=1, ioaddr1=0x1f0, ioaddr2=0x3f0, irq=14
ata0-master: type=none
ata0-slave: type=none
ata1: enabled=1, ioaddr1=0x170, ioaddr2=0x370, irq=15
ata1-master: type=none
ata1-slave: type=none
ata2: enabled=0
ata3: enabled=0
optromimage1: file=none
optromimage2: file=none
optromimage3: file=none
optromimage4: file=none
optramimage1: file=none
optramimage2: file=none
optramimage3: file=none
optramimage4: file=none
pci: enabled=1, chipset=i440fx
vga: extension=vbe, update_freq=5, realtime=1
cpu: count=1, ips=4000000, model=bx_generic, reset_on_triple_fault=1, cpuid_limit_winnt=0, ignore_bad_msrs=1, mwait_is_nop=0
cpuid: level=6, stepping=3, model=3, family=6, vendor_string="GenuineIntel", brand_string="              Intel(R) Pentium(R) 4 CPU        "
cpuid: mmx=1, apic=xapic, simd=sse2, sse4a=0, misaligned_sse=0, sep=1, movbe=0, adx=0
cpuid: aes=0, sha=0, xsave=0, xsaveopt=0, x86_64=1, 1g_pages=0, pcid=0, fsgsbase=0
cpuid: smep=0, smap=0, mwait=1
print_timestamps: enabled=0
debugger_log: -
magic_break: enabled=0
port_e9_hack: enabled=0
private_colormap: enabled=0
clock: sync=none, time0=local, rtc_sync=0
# no cmosimage
# no loader
log: -
logprefix: %t%e%d
debug: action=ignore
info: action=report
error: action=report
panic: action=ask
keyboard: type=mf, serial_delay=250, paste_delay=100000, user_shortcut=none
mouse: type=ps2, enabled=0, toggle=ctrl+mbutton
speaker: enabled=1, mode=system
parport1: enabled=1, file=none
parport2: enabled=0
com1: enabled=1, mode=null
com2: enabled=0
com3: enabled=0
com4: enabled=0
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: