通常在进入保护模式时要关闭所有的中断,把IDTR的界限设置为0,CPU自动关闭所有中断,包括NMI,返回实模式后恢复IDTR并开中断。 另外A20地址线的控制对于正确访问整个内存也很重要,在进入保护模式前要让8042打开A20地址线。 在这个例子里FS段寄存器设成可访问4GB内存的基址和界限,由于在DOS中很少有程序会用到GS、FS这两个386增加的段寄存器,当要读写4GB范围中的任一个地方都可通过FS段来达到,直到FS在实模式下被重装入冲掉为止。 这个例子在386SX、386DX、486上都运行通过。例子里加有十分详细的注释,由于这一程序是用BC 3.1编译连接的,而其连接器不能为DOS程序处理32位寄存器,所以直接在代码中加入操作码前缀0x66和地址前缀0x67,以便让DOS实模式下的16位程序可用32位寄存器和地址。程序的右边以注释形式给出等效的32位指令。要注意16位的指令中mov al, byte ptr [BX]的指令码正好是32位的指令mov al, byte ptr[EDI]。 读者可用这个程序验证BIOS是否同时在两个区域出现。如果有线性定址能力的VESA显示卡(如TVGA9440)还可进一步验证线性显示缓冲区在1MB之上的工作情况。 #include unsigned long GDT-Table[]= {0,0, //NULL - 00H 0x0000FFFF,0x00CF9A00, //Code32 - 08h Base=0 Limit=4G-1 Size=4G 0x0000FFFF,0x00CF9200 //Data32 - 10h Base=0 Limit=4G-1 Size=4G }; unsigned char OldIDT [6]={0}; //Save The IDTR before Enter Protect Mode. unsigned char pdescr-tmp [6]={0}; //NULL The IDTR s Limit=0 CPU will // disable all Interrupts, include NMI. #define KeyWait() {while(inportb(0x64) &2);} void A20Enable(void) { keyWait (); outportb(0x64,0xD1); KeyWait(); outportb(0x60,0xDF); //Enable A20 with 8042. KeyWait(); outportb(0x64,0xFF); KeyWait (); } void LoadFSLimit4G(void) { A20Enable (); //Enable A20 //*** Disable ints & Null IDT //*** asm { CLI //Disable inerrupts SIDT OldIDT //Save OLD IDTR LIDT pdescr-tmp //Set up empty IDT.Disable any interrupts, } // Include NMI. //*** Lodd GDTR //*** asm{ // The right Code is Real, But BC++ s Linker NOT // Work with 32bits Code. db 0x66 //32 bit Operation Prefix in 16 Bit DOS. MOV CX,DS //MOV ECX,DS db 0x66 //Get Data segment physical Address SHL CX,4 //SHL ECX,4 MOV word ptr pdescr-tmp [0],(3*8-1) //MOV word ptr pdescr-tmp [0], (3*8-1) db 0x66 XOR AX,AX //XOR EAX,EAX MOV AX,offset GDT-Table // MOV AX,offset GDT-Table db 0x66 ADD AX,CX //ADD EAX,ECX MOV word ptr pdescr-tmp [2], AX //GDTR Base low16 bits db 0x66 SHR AX,16 //SHR EAX,16 MOV word ptr pdescr-tmp [4],AX //GDTR Base high16 bits LGDT pdescr-tmp //Load GDTR } //**** //* Enter 32 bit Flat Protected Mode //**** asm{ mov DX,0x10 // The Data32 Selector db 0x66,0x0F,0x20,0xC0 // MOV EAX,CR0 db 0x66 MOV BX,AX // MOV EBX,EAX OR AX,1 db 0x66,0x0F,0x22,0xC0 //MOV CRO,EAX // Set Protection enable bit JMP Flsuh } //Clear machine perform cache. flush: // Now In Flat Mode, But The CS is Real Mode Value. asm { //And it s attrib is 16Bit Code Segment. db 0x66 MOV AX,BX //MOV EAX,EBX db 0x8E,0xE2 //MOV FS,DX //Load FS Base=0 Size=4G now db 0x66,0x0F,0x22,0xC0 //MOV CRO,EAX //Return Real Mode. LIDT OldIDT //LIDT OldIDT //Restore IDTR STI // STI //Enable INTR } } unsigned char ReadByte (unsigned long Address) { asm db 0x66 asm mov di,word ptr Address // MOV EDI, Address asm db 0x67 //32 bit Address Prefix asm db 0x64 //FS: asm mov al,byte ptr [BX] // =MOV AL, FS: [EDI] return -AL; } unsigned char WriteByte(unsigned Long Address) { asm db 0x66 asm mov di,word ptr Address //MOV EDI, Address asm db 0x67 //32 bit Address Prefix asm db 0x64 //FS: asm mov byte ptr [BX],al //=MOV FS: [EDI],AL return -AL; } //////// Don t Touch Above Code /// # include void Dump4G (unsigned long Address) { int i; int j; for (i=0; i
|