Transcript vm_entry

VM Entry
Iamroot xen 분석 스터디 2기
이상철
VM Entry
• VM Entry
– Guest로 전환하는 것을 나타냄.
– VMLAUNCH, VMRESUME을 수행 시 VM Entry 과정 수행.
• VM Entry Instruction
– VMLAUNCH
• VMCS의 launch state가 ‘clear’일 경우 사용.
• 명령 수행 후 ‘clear’에서 ‘launched’로 상태 변경.
• VMCLEAR를 수행 한 후 처음 VM Entry 시 사용.
– VMRESUME
• VMCS의 launch state가 ‘launched’일 경우 사용.
• VMLAUNCH 수행 후 ‘launched’ 상태로 바뀐 후는 이 명령을 통해서 VM
Entry 발생.
– VMLAUNCH 또는 VMRESUME 명령 다음 명령은 예외 처리 명령
을 수행.
– VMLAUNCH 또는 VMRESUME 명령 성공 시 다음 명령은 수행
되지 않음.
VM Entry 수행 과정
•
VM Entry 진입 과정
–
–
–
–
VM entry 수행 할 수 있는지 logical processor 상태 체크.
Non root operation 지원 가능한지 체크.
VMCS의 Guest State 영역 check와 loading.
VMCS의 VM-entry MSR-load area로부터 MSR 로딩.
Logical Processor State Check
• Logical Processor State Check.
– virtual-8086 mode, compatibility mode 일 경우 exception 발생.
• Invalid-opcode exception 발생.
– CPL이 0이 아니면 Protection Exception 발생.
– Current VMCS가 없을 경우 RFLAGS.CF=1로 설정 후 다음 명령
수행.
• 일반적으로 VMLAUNCH 또는 VMRESUME 명령 다음은 에러 처리 명령.
• VMLAUNCH 또는 VMRESUME 명령 성공 시 다음 명령은 수행 되지 않음.
– Current VMCS가 있을 경우 체크.
• Current VMCS영역이 MOV-SS blocking 영역이 아니어야 함.
• 각 명령에 맞는 VMCS의 launch state.
• 이상이 있을 경우 RFLAGS.ZF=1로 설정 후 다음 명령 수행. 에러 넘버는
VM-Instruction error field에 저장됨.(Intel manual 2B chap5.4 참조)
VMCS control and host-state area check
• Non root operation 지원 가능한지 체크.
– Current VMCS 설정 중 VM Exit 지원 가능 여부 체크.
– VM Exit 수행 후 Host 상태 유지 가능한지 체크.
– 실패 시 RFLAGS.ZF=1, 에러 넘버 기록 후 다음 명령 수행.
• Checking control area of current VMCS
– Checking VM-Execution field
• Reserved bit, address align, 각 설정에 따른 연계 설정 여부 체크.
• ‘Intel manual 3B 23.2.1.1 VM-Execution Control Fields’ 참고.
– Checking VM-Exit field
• Reserved bit, address align, 각 설정에 따른 연계 설정 여부 체크.
• ‘Intel manual 3B 23.2.1.2 VM-Exit Control Fields’ 참고.
– Checking VM-Entry field
• ‘Intel manual 3B 23.2.1.3 VM-Entry Control Fields’ 참고.
VMCS control and host-state area check
• Checking host state area of current VMCS
– Host Control register와 MSR 체크.
• VMX operation에서 지원하지 않는 것에 대한 CR0, CR4 비트 설정 금지.
• IA32_SYSENTER_ESP와 IA32_SYSENTER_EIP 필드 설정.
• 세부 사항은 “23.2.2 Checks on Host Control Registers and MSRs” 참고
– Host Segment register와 Descriptor Table register 체크.
• CS, CS, SS, DS, ES, FS, GS, TR의 RPL과 TI 0으로 설정.
• CS와 TR은 0이 아닌 다른 값으로 설정.
• 세부 사항은 “23.2.3 Checks on Host Segment and Descriptor-Table
Registers” 참고
• Checks Related to Address-Space Size
– Logical processor의 IA32e mode 설정에 따라 host state 설정.
– 세부 사항은 “23.2.4 Checks Related to Address-Space Size”
참고.
Checking and Loading Guest State of the
current VMCS
• Checking 및 Loading 실패 시 VM exit 발생.
• Checks on the Guest State Area
–
–
–
–
–
–
Guest
Guest
Guest
Guest
Guest
Guest
Control Registers, Debug Registers, and MSRs
Segment Registers
Descriptor-Table Registers
RIP and RFLAGS
Non-Register State
Page-Directory-Pointer-Table Entries
Checking and Loading Guest State of the
current VMCS
• Loading Guest State
– Guest State field내에 있는 정보들을 이용하여 로딩.
– Loading Guest Control Registers, Debug Registers, and MSRs
• 몇몇 MSR은 Guest state field의 정보를 이용하여 로딩.
– Loading Guest Segment Registers and Descriptor-Table
Registers
– Loading Guest RIP, RSP, and RFLAGS
– Loading Page-Directory-Pointer-Table Entries
– Updating Non-Register State
• Clearing Address-Range Monitoring
– MONITOR and MWAIT 명령이 사용하는 주소공간 설정 해제.
Management of idle and blocked conditions
(첨부)
•
•
HLT
–
–
–
–
Current logical processor를 정지.
깨우기 위해 NMI, SMI (system management interrupt), hardware reset, INIT# 중 하나 필요.
Active logical processor는 자원 제약에 자유로워 짐.
Idle process만을 실행하는 logical processor에 주로 사용.
–
–
Critical section에 대한 loop처리의 성능 향상.
PAUSE 명령을 통해 spin wait loop상태라는 것을 알려줌으로 out order에 의한 memory
violation이나 pipeline flush 최소화.
파워 절약, 리소스 절약 효과.
PAUSE
–
•
MONITOR/MWAIT
–
–
–
–
Multithread환경(Hyper Threading)의 thread synchronization 성능 향상을 위해 사용.
MONITOR 명령을 통해 감시를 할 영역을 설정. Write-back caching 영역 사용.
MONITOR에서 설정한 공간에 동작이 일어나면 MWAIT를 통해 잠든 곳이 깨어남. 공유자원
접근에 활용.
초기 설정은 level 0에서 설정. 다른 레벨에서 사용 가능. 단 0이 아닌 level에서는 exception
발생 시 처리 못함.
Loading MSRs
• Loading MSR
– VMCS내의 VM-Entry MSR-load area로 부터 MSR loading
• VM-Entry MSR-load address, VM-Entry MSR-load count 이용.
– 16byte로 정렬된 주소를 사용. ( 하나의 엔트리는 128bit로 구성
됨.)
• [31:0] MSR Index
• [63:32] Reserved
• [127:64] MSR data
– MSR 로딩이 실패 시 VM exit 과정 수행.
Event Injection
• Event Injection
– Guest context가 모두 설정 된 뒤에 이벤트 발생.
– VM-entry interruption-information field내 valid bit(bit 31)이 1일
경우 VM-entry 시 field내 전송된 event가 발생.
• VM-entry interruption-information field는 “Table 21-12. Format of the VMEntry Interruption-Information Field” 참고.
– Valid bit는 VM-exit가 일어날 경우 자동으로 clear. 단 VM-entry 실
패로 인한 VM-exit 발생 할 경우는 clear안됨.
• Vectored-Event Injection
–
–
–
–
–
–
External interrupt.
Non-maskable interrupt.
Hardware exception.
Software interrupt.
Privileged software exception.
Software exception.
Event Injection
• VM Exits During Event Injection
– IDT 중 task gate 일 경우, task switch을 시도시 VM exit 발생.
– 중첩된 exception이 발생 했을 경우, exception bitmap 설정에
따라 VM exit 발생. (“22.3 Other Cause of VM Exits” 참고)
– Triple fault 발생 시 VM exit 발생.
– VM-execution control의 “virtualize APIC accesses”가 1일 경우,
APIC-access page에 접근 시 VM-exit 발생.
– VM-exit 발생 시 어떤 이벤트에 의해 VM-exit가 발생했는지에
대한 정보를 IDT-vectoring information field에 저장한다.(24.2.3
참고)
Special features of VM entry
• Interruptibility State
• Activity State
• Delivery of Pending Debug Exceptions after VM
Entry
• VMX-Preemption Timer
• Interrupt-Window Exiting
• NMI-Window Exiting
• VM Exits Induced by the TPR Shadow
• Pending MTF VM Exits
VM-entry failures during or after loading
guest state
• VM-entry 실패 시 수행.
– VMCS 체크 나 MSR loading 실패 시 수행.
• 처리 순서
– VM-exit information field에 실패 이유 기록.( 상세는 23.7 참고)
– Processor state 로딩. PAE 모드 사용 시 page-directory-pointertable entries (PDPTEs)를 체크하고 로딩.
– VM entry전 NMI에 의해 blocking된 상태로 복구.
– MSRs are loaded as specified in the VM-exit MSR-load area.
• 실패 시 처리 순서는 대부분 과정이 VM-exit와 동일.
– 대부분 VM-exit information field를 업데이트 수행 하지 않음.
– The valid bit in the VM-entry interruption-information field is not
cleared
– The guest-state area is not modified.
– No MSRs are saved into the VM-exit MSR-store area.
Machine checks during VM entry
• VM entry 중 machine check 발생 시 아래와 같이 처리.
– CR4.MCE=1 이면 해당 guest IDT에서 처리.
– CR4.MCE=0 일 경우 SMX(Safer Mode Extension, “chapter 6
Safer Mode Extension” intel mannual 2B 참고) operation에서
수행했는지 여부에 따라 처리과정 틀림.
• SMX operation일 경우 Intel TXT shutdown 상황 발생.
• 아닐 경우 바로 Shutdown state로 전환.
• VM entry failure 처리. Exit reason기록.
– Basic exit reason 41, “VM-entry failure due to machine check”