home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / caway349.zip / MISC / RING0_32.ASM < prev    next >
Assembly Source File  |  1998-11-10  |  3KB  |  133 lines

  1. ; RING0_32.ASM
  2. ; Demonstration of how to switch to ring 0 with CauseWay for those who
  3. ;  may find it useful, 32-bit non-flat version.
  4. ; Remember that no application can run under ring 0 under DPMI, even with
  5. ;  another DOS extender that might normally run applications at ring 0
  6. ;  for whatever strange reason it thinks it appropriate.
  7. ;
  8. ; Type: WL32 ring0_32 to create RING0_32.EXE
  9. ;
  10. .386P                ; 32-bit code segment
  11. ;.MODEL FLAT
  12. .MODEL SMALL
  13. .STACK 400h
  14.  
  15. ; CauseWay specific equates
  16. GDTData            EQU    0e0h+3
  17. KernalPL3_2_PL0    EQU 48H+3    ; PL3 to PL0 call gate.
  18. KernalZero        EQU    70h+3    ; Kernal 0-4G data reference.
  19.  
  20. .DATA
  21. HiText    DB    'Hello from 32-bit Ring 3 (if still in Ring 0 this DOS call would crash).',13,10
  22. NoZero    DB    'Cannot go to Ring 0.',13,10
  23.  
  24. .CODE
  25. ;    DB    64*1024    DUP (?)        ; just to show use with nonzero high word 32-bit offsets
  26. start:
  27.     mov    ax,0ff00h    ; Info
  28.     int    31h
  29.     jc    neverdid
  30.     test    edi,8    ; see if running under DPMI
  31.     jne    neverdid    ; yes, can't switch to ring 0
  32.  
  33. ; setup for ring 3 to ring 0 switch
  34.     mov    bx,cs
  35.     mov    ax,0ff06h    ; AliasSel
  36.     int    31h
  37.     jc    neverdid
  38.     mov    bx,ax
  39.     mov    cl,10011011b    ; access rights
  40.     mov    ch,01000000b    ; extended access rights bits, 32-bit default, byte granular
  41.     mov    ax,9        ; Set Descriptor Access Rights
  42.     int    31h
  43.     jc    neverdid
  44.     push    bx        ; save aliased PL0 CS
  45.  
  46.     mov    bx,GDTData
  47.     mov    ax,0ff08h    ; GetSelDet32
  48.     int    31h
  49.     jc    neverdid
  50.  
  51.     cli                ; no interrupt processing allowed under ring 0 or setup
  52.  
  53. ; edx holds linear base of GDT
  54.     add    edx,KernalPL3_2_PL0    ; add in ring 3 to 0 call gate address
  55.     and    edx,NOT 7    ; zap offset bytes
  56.     mov    esi,edx        ; save GDT offset to call gate
  57.     mov    ax,KernalZero
  58.     mov    es,ax
  59.     mov    edi,es:[edx]    ; save original call gate CS:EIP
  60.     mov    bx,es:[edx+6]    ; save high word of EIP
  61.  
  62.     mov    ecx,OFFSET In0
  63.     mov    es:[edx],cx
  64.     shr    ecx,16
  65.     mov    WORD PTR es:[edx+6],cx
  66.     pop    WORD PTR es:[edx+2]    ; set aliased PL0 CS
  67.  
  68.     mov    edx,esp        ; save SS:ESP in [E]CX:EDX
  69.     mov    cx,ss
  70.     movzx    ecx,cx
  71.     mov    bp,cs        ; save non-aliased PL3 CS
  72.  
  73. ; do the ring 0 to ring 3 switch
  74.     db    9ah            ; absolute 32-bit call to clear pre-fetch and load CS
  75.     dd    OFFSET In0    ; In0 is a dummy place-holder here
  76.     dw    KernalPL3_2_PL0
  77.  
  78. ; do ring 0-only stuff to show that it works;
  79. ;  these instructions would all fail under ring 3.
  80. ;  Just do benign reads so things remain stable.
  81. ; CauseWay emulates MOV EAX,CR0; MOV CR0,EAX; MOV EAX,CR3; MOV CR3,EAX
  82. ;  instructions in the normal ring 3 GPF-handler, so don't use those.
  83. In0:
  84.     mov    eax,DR0        ; just for show
  85.     mov    eax,DR6
  86.     mov    eax,CR2
  87.  
  88. ; do the ring 0 to ring 3 switch
  89.     mov    es:[esi],edi    ; restore original CS:EIP in call gate
  90.     mov    es:[esi+6],bx
  91.     push    ecx        ; SS
  92.     push    edx        ; ESP
  93.     pushfd
  94.     pop    eax            ; EFlags in eax
  95.     and    ax,1000111111111111b    ; clear NT and IOPL flag bits
  96.     or    ax,0011000000000000b    ; force IOPL to 3
  97.     push    eax
  98.     popfd
  99.     push    eax        ; flags
  100.     xor    eax,eax
  101.     mov    ax,bp
  102.     push    eax        ; CS, PL3
  103.     mov    eax,OFFSET In3
  104.     push    eax        ; EIP
  105.     iretd            ; return control at ring 3 to In3
  106.  
  107. In3:
  108.     sti                ; re-enable interrupts
  109.  
  110.     mov    ax,_DATA
  111.     mov    ds,ax
  112.     mov    edx,OFFSET HiText
  113.     mov    cx,SIZEOF HiText
  114.     mov    bx,1
  115.     mov    ah,40h
  116.     int    21h
  117.  
  118.     mov    ax,4c00h
  119.     int    21h
  120.  
  121. neverdid:
  122.     mov    ax,_DATA
  123.     mov    ds,ax
  124.     mov    edx,OFFSET NoZero
  125.     mov    cx,SIZEOF NoZero
  126.     mov    bx,1
  127.     mov    ah,40h
  128.     int    21h
  129.     mov    ax,4c01h
  130.     int    21h
  131.  
  132. END    start
  133.