Page 1 of 1

The RPL class is higher than DPL,but codes couldn't run well

Posted: Fri Apr 22, 2011 7:23 am
by leetow2003
My pogram is very simple,there is only a code segment in GDT,
I set that the RPL value is 0 and DPL value is 1,I think the
RPL class s higher than DPL,the code segment can run,but when
I run the program,my PC always restart,why ?who could tell how
to correct?

Code: Select all

;JUMP Macro
jump macro selector,offsetv
      db 0eah    ;Opcode:JMP
      dw offsetv  
      dw selector 
    endm


;segment descriptor
descriptor struc
  limitl dw 0
  basel dw 0 
  basem db 0 
  attributes dw 0 
  baseh db 0
descriptor ends

;
pdesc struc
limit dw 0 
base dd 0 
pdesc ends

;Constant
atce = 0B8h  ;To DPL to 1

;
  .386P   
;--------------------------------------------------------

;GDT
dseg segment use16 
gdt label byte 
dummy descriptor <> 

code descriptor <0ffffh,,,atce,>
code_sel =code-gdt ;Set RPL to 0

gdtlen =$-gdt
;
vgdtr pdesc <gdtlen-1,>
;
dseg ends

;-------------------------------------------------------------
cseg segment use16 ; 
  assume cs:cseg, ds:dseg
start:
  mov ax,dseg
  mov ds,ax
  ;set gdtr
  mov bx,16
  mul bx 
  add ax,offset gdt 
  adc dx,0
  mov word ptr vgdtr.base,ax
  mov word ptr vgdtr.base+2,dx
  ;
  mov ax,cs
  mul bx
  mov code.basel,ax 
  mov code.basem,dl 
  mov code.baseh,dh
  ;Load GDTR
  lgdt fword ptr vgdtr
  ;
  cli 
  ;
  mov eax,cr0
  or eax,1
  mov cr0,eax
  ;
  jump <code_sel>,<offset virtual>
  ;
virtual: ;P-Mode
  mov ah,48h ;I write one instruct optionally 
  ;
  mov eax,cr0
  and eax,0fffffffeh
  mov cr0,eax
  ;
  jump <seg real>,<offset real>
  ;
real: ;R-Mode
  sti 
  mov ax,4c00h 
  int 21h
;
cseg ends
  end start

Re: The RPL class is higher than DPL,but codes couldn't run

Posted: Fri Apr 22, 2011 2:09 pm
by egos
"seg real" isn't 16-bit code selector. See Intel manuals to have known how to switch to real mode.

Re: The RPL class is higher than DPL,but codes couldn't run

Posted: Fri Apr 22, 2011 9:04 pm
by leetow2003
egos wrote:"seg real" isn't 16-bit code selector. See Intel manuals to have known how to switch to real mode.
You mean it must be in p-mode between two segments

Re: The RPL class is higher than DPL,but codes couldn't run

Posted: Sat Apr 23, 2011 3:51 am
by egos
I had wrong. It seemed to me that you use 32-bit code segment.

You cannot jump to code segment with DPL other than CPL directly. To decrease priveleges you should use "retf" instruction. To increase privileges you should use a gate. Some instructions as "mov cr0" can be executed successfully only with CPL=0.