У меня есть PROTECTED.INC для FASM. Кому интиресно:
Код:
; This program is free software. It comes without any warranty, to
; the extent permitted by applicable law. You can redistribute it
; and/or modify it under the terms of the Do What The Fuck You Want
; To Public License, Version 2, as published by Sam Hocevar. See
; http://sam.zoy.org/wtfpl/COPYING for more details.
;--------------------------------------------------------------
;-----------------------README---------------------------------
;
;Use GDT_descr (base, limit, access, options) to create a GDT descriptor.
;
;Use GDT_descr (base, limit, access options, options) to create a IDT descriptor (gate).
;
;Use EnterPM (GDT,IDT,entery segment:entery point) to enter Protected Mode.
;
;Use EnterURM to get an unreal segment inside fs.
;
;(C) 418ImATeapot
;Sorry for my English!
;--------------------------------------------------------------
if ~(defined INC_protected)
INC_protected equ
include 'PIC.inc'
;protected.inc
;------------------------------------------------
;Privilegion levels
PL_CORE equ 00000000b
PL_DRV0 equ 00100000b
PL_DRV1 equ 01000000b
PL_USER equ 01100000b
;------------------------------------------------
;macro for a usual GDT element
macro GDT_descr base,limit,access,options
{
dw limit and 000FFFFh ;First 16 bits of the limit
dw base and 0000FFFFh ;First 16 bits of the base
db (base and 00FF0000h) /10000h ;Bits 16:23 of the base
db access ;access bits
db (limit and 00F0000h)/10000h + options ;Bites 16:23 of the limit + options
db (base and 0FF000000h)/1000000h ;Bits 23:31 of the base
}
;GDT segment options
GDT_OPT_AVL equ 10h;This option is at your option :-)
GDT_OPT_I_WANT_TO_HANG_MY_COMPUTER equ 20h; Bit 5 is reserved and must be 0 in IA-32.
GDT_OPT_32 equ 40h;If this option is on, the segment will use 32-bit code as default.
GDT_OPT_PG equ 80h;Page granularity.
;GDT access options
GDT_A_PRESENTED equ 80h
;Flags for nonsystem segments
GDT_A_NONSYSTEM equ 10h;Must be on!!!
GDT_ANS_AC equ 01h;This flag may be changed CPU as "durty" sign. Just don't set it.
GDT_ANS_RW equ 02h;In code segments this flag will enabled reading, in data segments this flag will enabled writing.
GTD_ANS_DC equ 04h;In code segments this flag will let low-privilegged code run it, in data segments this flag will change grow direction.
GDT_ANS_EXEC equ 08h;Code segment.
;Types of system segments
GDT_SYS_FREETSS16 equ 01
GDT_SYS_LDT equ 02
GDT_SYS_BISSYTSS16 equ 03
GDT_SYS_CALLGATE16 equ 04
GDT_SYS_TASKGATE equ 05
GDT_SYS_INTGATE16 equ 06
GDT_SYS_TRAPGATE16 equ 07
GDT_SYS_FREETSS32 equ 09
GDT_SYS_BISSYTSS32 equ 11
GDT_SYS_CALLGATE32 equ 12
GDT_SYS_INTGATE32 equ 14
GDT_SYS_TRAPGATE32 equ 15
;------------------------------------------------
;macro for a IDT element
;IDT descriptor options
IDT_PRESENTED equ 10000000b
IDT_STORAGE_SEGMENT equ 00010000b;(Me.RTFM)
;IDT TYPES
IDT_TASK equ 0x5
IDT_INTG16 equ 0x6
IDT_TRAP16 equ 0x7
IDT_INTG32 equ 0xE
IDT_TRAPG32 equ 0xF
macro IDT_descr segment,offset,options
{
dw offset and 000FFFFh ;First 16 bits of offset
dw segment ;Segment
db 0
db options ;Options
dw (offset and 0FFFF0000h) /10000h
}
;------------------------------------------------
;protected mode entery
macro EnterPM GDT,IDT, start_seg, PMstart;Entering fully protected mode.
{
lgdt [GDT];Loading GDT
lidt [IDT]
;Setup bit 0 in cr0 (X-TIME!!!)
mov eax,cr0
or al,1
mov cr0,eax
;Jumping to the PM-code segment.
db 66h
use32
jmp start_seg:PMstart
}
macro EnterURM;Entering UnReal mode.
{
MaskNMI;Masking NMI
jmp @f
;URM GDT
.gdtr: dw 16
dd .gdt
.gdt:
dd 0,0;Z-segment
GDT_descr 0,0FFFFFFh,GDT_A_PRESENTED or GDT_A_NONSYSTEM or GDT_ANS_RW , GDT_OPT_PG;Data segment
@@:
;Computing GDT adress
push cs
pop ds
xor eax,eax
mov ax,cs
shl eax,4
add dword [.gdtr+2],eax
;X-TIME
lgdt [.gdtr]
mov eax,cr0
or al,1
mov cr0,eax
;Loading URM segment
mov ax,8
mov fs,ax
;Going back to real mode, but with a change of dark of FS segment.
mov eax,cr0
and al,0FFh
mov cr0,eax
;Unmasking NMI
UMaskNMI
; hlt
}
end if