[代码实例][Linux内核编程]内核开发:1、从实模式到保护模式
2017-03-16 19:29
531 查看
bootsect.s:
head.s:
Makefile:
bochsrc:
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
相关文章推荐
- Linux内核0.12——从一个简单的多任务内核实例学习保护模式
- java开发系统内核:使用LDT保护进程数据和代码
- 三层模式开发使用GridPanel(.net)控件代码实例详解(展示层代码)
- php 微信公众平台开发模式实现多客服的实例代码
- 自己动手写内核(第2课:保护模式)(原创)
- 亿美短信开发实例后台代码
- 实模式到保护模式再返回实模式的学习代码
- 实例演示CodeSmith与ECC开发模式结合---快速建站(二)
- SP 短信开发-PROVISION接口实现-正向订购实例代码
- 标准双接口开发文档及其代码实例
- 80x86保护模式系列教程(4)实模式与保护模式切换实例
- SP 短信开发-PROVISION接口实现-反向取消实例代码
- 理解0.11内核setup.s中实模式与保护模式的切换
- Windows内核模式开发笔记
- Sculpture 一个开源的优秀的代码生成工具(MDA开发模式)
- 内核编程:保护模式下读取磁盘绝对扇区
- 为Windows应用创建简单的异步调用模式(附实例代码)
- 操作系统入门(五) - 载入32位保护模式代码为c做准备
- 在用工厂模式做三层架构开发的时候 无法反射创建类实例的发现的错误
- [C#]一步一步开发自己的自动代码生成工具之四:简单三层代码模板实例Model层