一.特权级转移中

Q:处理器通过什么规则判断请求或代码跳转是否合法?
CPL,RPL,DPL之间的关系

A.数据段的访问规则(数据段无可执行属性)

1.访问者权限(CPL)高于或等于数据段权限(DPL)
2.请求特权级(RPL)高于或等于数据段特权(DPL)
3.即(CPL小于或等于DPL)(RPL小于或等于DPL)
实验-三个不同的实验来验证,来判断访问是否合法

三种不同的访问方式
; CPL = 2, RPL = 1, DPL = 3, 访问是否合法?   ===> yes
; CPL = 0, RPL = 3, DPL = 2, 访问是否合法?   ===> no
; CPL = 0, RPL = 1, DPL = 2, 访问是否合法?   ===> yes

代码如下

%include "inc.asm"

org 0x9000

jmp ENTRY_SEGMENT

[section .gdt]
; GDT definition
;                                 段基址,       段界限,       段属性
GDT_ENTRY       :     Descriptor    0,            0,           0
CODE32_DESC     :     Descriptor    0,    Code32SegLen - 1,    DA_C + DA_32 + DA_DPL2
VIDEO_DESC      :     Descriptor 0xB8000,     0x07FFF,         DA_DRWA + DA_32 + DA_DPL3
DATA32_DESC     :     Descriptor    0,    Data32SegLen - 1,    DA_DR + DA_32 + DA_DPL3
STACK32_DESC    :     Descriptor    0,     TopOfStack32,       DA_DRW + DA_32 + DA_DPL2
; GDT end

GdtLen    equ   $ - GDT_ENTRY

GdtPtr:
          dw   GdtLen - 1
          dd   0

; GDT Selector

Code32Selector     equ (0x0001 << 3) + SA_TIG + SA_RPL2
VideoSelector      equ (0x0002 << 3) + SA_TIG + SA_RPL3
Data32Selector     equ (0x0003 << 3) + SA_TIG + SA_RPL1
Stack32Selector    equ (0x0004 << 3) + SA_TIG + SA_RPL2

; end of [section .gdt]

; CPL = 2, RPL = 1, DPL = 3, 访问是否合法?   ===> yes
; CPL = 0, RPL = 3, DPL = 2, 访问是否合法?   ===> no
; CPL = 0, RPL = 1, DPL = 2, 访问是否合法?   ===> yes

TopOfStack16    equ 0x7c00

[section .s16]
[bits 16]
ENTRY_SEGMENT:
    mov ax, cs
    mov ds, ax
    mov es, ax
    mov ss, ax
    mov sp, TopOfStack16

    ; initialize GDT for 32 bits code segment
    mov esi, CODE32_SEGMENT
    mov edi, CODE32_DESC

    call InitDescItem

    mov esi, DATA32_SEGMENT
    mov edi, DATA32_DESC

    call InitDescItem

    mov esi, STACK32_SEGMENT
    mov edi, STACK32_DESC

    call InitDescItem

    ; initialize GDT pointer struct
    mov eax, 0
    mov ax, ds
    shl eax, 4
    add eax, GDT_ENTRY
    mov dword [GdtPtr + 2], eax

    ; 1. load GDT
    lgdt [GdtPtr]

    ; 2. close interrupt
    cli 

    ; 3. open A20
    in al, 0x92
    or al, 00000010b
    out 0x92, al

    ; 4. enter protect mode
    mov eax, cr0
    or eax, 0x01
    mov cr0, eax

    ; 5. jump to 32 bits code
    ; push Stack32Selector   ; 目标栈段选择子
    ; push TopOfStack32      ; 栈顶指针位置
    ; push Code32Selector    ; 目标代码段选择子
    ; push 0                 ; 目标代码段偏移
    ; retf
    jmp word Code32Selector : 0

; esi    --> code segment label
; edi    --> descriptor label
InitDescItem:
    push eax

    mov eax, 0
    mov ax, cs
    shl eax, 4
    add eax, esi
    mov word [edi + 2], ax
    shr eax, 16
    mov byte [edi + 4], al
    mov byte [edi + 7], ah

    pop eax

    ret

[section .dat]
[bits 32]
DATA32_SEGMENT:
    DTOS               db  "D.T.OS!", 0
    DTOS_OFFSET        equ DTOS - $$

Data32SegLen equ $ - DATA32_SEGMENT

[section .s32]
[bits 32]
CODE32_SEGMENT:
    mov ax, VideoSelector
    mov gs, ax

    mov ax, Data32Selector
    mov ds, ax

    mov ax, Stack32Selector
    mov ss, ax

    mov eax, TopOfStack32
    mov esp, eax

    mov ebp, DTOS_OFFSET
    mov bx, 0x0C
    mov dh, 12
    mov dl, 33

    call PrintString

    jmp $

; ds:ebp    --> string address
; bx        --> attribute
; dx        --> dh : row, dl : col
PrintString:
    push ebp
    push eax
    push edi
    push cx
    push dx

print:
    mov cl, [ds:ebp]
    cmp cl, 0
    je end
    mov eax, 80
    mul dh
    add al, dl
    shl eax, 1
    mov edi, eax
    mov ah, bl
    mov al, cl
    mov [gs:edi], ax
    inc ebp
    inc dl
    jmp print

end:
    pop dx
    pop cx
    pop edi
    pop eax
    pop ebp

    ret

Code32SegLen    equ    $ - CODE32_SEGMENT

[section .gs]
[bits 32]
STACK32_SEGMENT:
    times 1024 * 4 db 0

Stack32SegLen equ $ - STACK32_SEGMENT
TopOfStack32  equ Stack32SegLen - 1

实验结果如下
将段描述符中的CPL,DPL,RPL按照实验要求进行设置所示
操作系统-深入特权级转移中
第一个实验要求结果如图所示
操作系统-深入特权级转移中
实验结论
选择子被段寄存器加载时,会进行保护模式的检查
1.检查选择子的下标是否合法,即段描述符的合法性
2.检查特权级是否合法(CPL RPL<=DPL)
3.检查特权级时CPL和RPL之间不会进行比较

B.代码段的分类

1.代码段的分类有两种:非系统段与系统段(与S位有关),在段描述符内存结构图中,在高12位中的s位为0时,为系统段(LDT,TSS,各种门结构);s位为1时,为非系统段(一致性代码段与非一致性代码段)
操作系统-深入特权级转移中
2.非系统段的分类(如图所示)
a.一致性代码段:X=1,C=1
b.非一致性代码段:X=1,C=0
操作系统-深入特权级转移中
两者代码段之间的跳转规则(不借助门描述符)
a.非一致性代码段,代码之间只能平级转移(CPL==DPL.RPL<=DPL)
b.一致性代码段,支持低特权级代码向高特权级代码的转移(CPL>=DLP),虽然可以成功转移高特权级代码段,但是当前特权级不变
需要注意的是,数据段只有一种,没有一致性和非一致性的区分,并且,数据段不允许被低特权级的代码段访问。
实验-代码段的直接跳转实验

%include "inc.asm"

org 0x9000

jmp ENTRY_SEGMENT

[section .gdt]

; GDT definition

;                                 段基址,       段界限,       段属性

GDT_ENTRY       :     Descriptor    0,            0,           0

CODE32_DESC     :     Descriptor    0,    Code32SegLen - 1,    DA_C + DA_32 + DA_DPL1

VIDEO_DESC      :     Descriptor 0xB8000,     0x07FFF,         DA_DRWA + DA_32 + DA_DPL2

DATA32_DESC     :     Descriptor    0,    Data32SegLen - 1,    DA_DR + DA_32 + DA_DPL2

STACK32_DESC    :     Descriptor    0,     TopOfStack32,       DA_DRW + DA_32 + DA_DPL1

FUNCTION_DESC   :     Descriptor    0,   FunctionSegLen - 1,   DA_C + DA_32 + DA_DPL1;函数段,定义打印函数,在屏幕打印字符串

; GDT end

GdtLen    equ   $ - GDT_ENTRY

GdtPtr:

          dw   GdtLen - 1

          dd   0

; GDT Selector

Code32Selector     equ (0x0001 << 3) + SA_TIG + SA_RPL1

VideoSelector      equ (0x0002 << 3) + SA_TIG + SA_RPL2

Data32Selector     equ (0x0003 << 3) + SA_TIG + SA_RPL2

Stack32Selector    equ (0x0004 << 3) + SA_TIG + SA_RPL1

FunctionSelector   equ (0x0005 << 3) + SA_TIG + SA_RPL1

; end of [section .gdt]

TopOfStack16    equ 0x7c00

[section .s16]

[bits 16]

ENTRY_SEGMENT:

    mov ax, cs

    mov ds, ax

    mov es, ax

    mov ss, ax

    mov sp, TopOfStack16

    ; initialize GDT for 32 bits code segment

    mov esi, CODE32_SEGMENT

    mov edi, CODE32_DESC

    call InitDescItem

    mov esi, DATA32_SEGMENT

    mov edi, DATA32_DESC

    call InitDescItem

    mov esi, STACK32_SEGMENT

    mov edi, STACK32_DESC

    call InitDescItem

    mov esi, FUNCTION_SEGMENT

    mov edi, FUNCTION_DESC

    call InitDescItem

    ; initialize GDT pointer struct

    mov eax, 0

    mov ax, ds

    shl eax, 4

    add eax, GDT_ENTRY

    mov dword [GdtPtr + 2], eax

    ; 1. load GDT

    lgdt [GdtPtr]

    ; 2. close interrupt

    cli 

    ; 3. open A20

    in al, 0x92

    or al, 00000010b

    out 0x92, al

    ; 4. enter protect mode

    mov eax, cr0

    or eax, 0x01

    mov cr0, eax

    ; 5. jump to 32 bits code

    push Stack32Selector   ; 目标栈段选择子

    push TopOfStack32      ; 栈顶指针位置

    push Code32Selector    ; 目标代码段选择子

    push 0                 ; 目标代码段偏移

    retf

; esi    --> code segment label

; edi    --> descriptor label

InitDescItem:

    push eax

    mov eax, 0

    mov ax, cs

    shl eax, 4

    add eax, esi

    mov word [edi + 2], ax

    shr eax, 16

    mov byte [edi + 4], al

    mov byte [edi + 7], ah

    pop eax

    ret

[section .dat]

[bits 32]

DATA32_SEGMENT:

    DTOS               db  "D.T.OS!", 0

    DTOS_OFFSET        equ DTOS - $$

Data32SegLen equ $ - DATA32_SEGMENT

[section .s32]

[bits 32]

CODE32_SEGMENT:

    mov ax, VideoSelector

    mov gs, ax

    mov ax, Data32Selector

    mov ds, ax

    mov ax, Stack32Selector

    mov ss, ax

    mov eax, TopOfStack32

    mov esp, eax

    ; mov ebp, DTOS_OFFSET

    ; mov bx, 0x0C

    ; mov dh, 12

    ; mov dl, 33

    call FunctionSelector : PrintString

    jmp $

Code32SegLen    equ    $ - CODE32_SEGMENT

[section .func]

[bits 32]

FUNCTION_SEGMENT:

; ds:ebp    --> string address

; bx        --> attribute

; dx        --> dh : row, dl : col

PrintStringFunc:

    push ebp

    push eax

    push edi

    push cx

    push dx

print:

    mov cl, [ds:ebp]

    cmp cl, 0

    je end

    mov eax, 80

    mul dh

    add al, dl

    shl eax, 1

    mov edi, eax

    mov ah, bl

    mov al, cl

    mov [gs:edi], ax

    inc ebp

    inc dl

    jmp print

end:

    pop dx

    pop cx

    pop edi

    pop eax

    pop ebp

    retf

PrintString    equ   PrintStringFunc - $$

FunctionSegLen    equ   $ - FUNCTION_SEGMENT

[section .gs]

[bits 32]

STACK32_SEGMENT:

    times 1024 * 4 db 0

Stack32SegLen equ $ - STACK32_SEGMENT
TopOfStack32  equ Stack32SegLen - 1

1.首先进行非一致性代码段实验,跳转时只能平级跳转,现在将函数段特权级改为2,运行代码改动如下
操作系统-深入特权级转移中
实验结果产生异常,如下所示
操作系统-深入特权级转移中
该处异常指出的是非一致性代码段DPL!=CPL,两者之间不能跳转
2.一致性代码段的设置

[section .new]
[bits 32]
NEW_SEGMENT:
    mov ebp, DTOS_OFFSET
    mov bx, 0x0C
    mov dh, 12
    mov dl, 33

    call FunctionSelector : PrintString

    jmp $

NewSegLen    equ    $ - NEW_SEGMENT

操作系统-深入特权级转移中
一致性代码段的属性设置是不一样的,同时将其设置为0特权级,还需要将其初始化设置,跳转也要改变,将其设置为一致性代码段处
实验结果
操作系统-深入特权级转移中
代码运行成功说明对于一致性代码段来说,可以从低代码段到高代码段跳转
实验结论
特权级降低转移时,retf指令会触发栈段的特权级检查,一致性代码段可直接跳转到其它同级非一致性代码段执行,在大多数情况下,选择子中的RPL和对应段描述符中的DPL可设置为相同值

小结

1.CPL,RPL和DPL是处理进行特权级保护的依据
2.对于数据段:CPL<=DPL,RPL<=DPL
3.对于非一致性代码段:CPL==DPL,RPL<=DPL
4.对于一致性代码段:CPL>=DPL,转移后CPL不变