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

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