第1 章 係統平颱 ............................................................................................. 1
1.1 環境及工具 .................................................................................................................. 1
1.1.1 使用VMware ........................................................................................................ 2
1.1.2 使用Bochs ............................................................................................................ 4
1.1.3 在真實機器上運行 ................................................................................................ 4
1.1.4 Build 工具............................................................................................................. 4
1.2 64 位與32 位代碼的混閤編譯 ................................................................................... 7
1.2.1 使用符號__X64 ..................................................................................................... 7
1.2.2 指令操作數 ........................................................................................................... 8
1.2.3 64-bit 模式下其他指令處理 .................................................................................. 11
1.2.4 函數重定義錶 ..................................................................................................... 15
1.3 地址空間 .................................................................................................................... 17
1.4 數據結構 .................................................................................................................... 23
1.4.1 PCB 結構 ............................................................................................................ 23
1.4.2 LSB 結構 ....................................................................................... 37
1.4.3 初始化PCB ........................................................................................................ 38
1.4.4 SDA 結構 ............................................................................................. 42
1.4.5 初始化SDA ........................................................................................................ 56
1.4.6 DRS 結構 ........................................................................................................... 57
1.5 係統啓動 .................................................................................................................... 59
1.5.1 Boot 階段 ........................................................................................................... 59
1.5.2 stage1 階段 ......................................................................................................... 62
1.5.2.1 stage1 階段的多處理器初始化 ................................................................. 66
1.5.2.2 BSP 的收尾工作 ..................................................................................... 68
1.5.2.3 APs 的stage1 階段工作 ........................................................................... 70
1.5.3 stage2 階段 ......................................................................................................... 73
1.5.3.1 BSP 在stage2 最後處理 ........................................................................... 80
1.5.3.2 APs 在stage2 階段收尾工作 .................................................................... 81
1.5.4 stage3 階段 ......................................................................................................... 83
1.5.4.1 BSP 在stage3 階段的最後工作 ................................................................ 87
1.5.4.2 APs 在stage3 階段收尾工作 .................................................................... 88
1.5.5 例子1-1 .............................................................................................................. 90
1.6 係統機製 .................................................................................................................... 91
1.6.1 分頁機製 ............................................................................................................ 91
1.6.1.1 PAE 分頁模式實現 ................................................................................. 91
1.6.1.2 IA-32e 分頁模式實現 .............................................................................. 98
1.6.2 多處理器機製 ................................................................................................... 102
1.6.2.1 調度任務 ............................................................................................. 102
1.6.2.2 處理器切換 .......................................................................................... 109
1.6.3 調試記錄機製 ................................................................................................... 113
1.6.3.1 例子1-2 ............................................................................................... 120
1.6.3.2 運行結果 ............................................................................................. 121
第2 章 VMX 架構基礎 ................................................................................ 122
2.1 虛擬化概述 .............................................................................................................. 123
2.1.1 虛擬設備 .......................................................................................................... 124
2.1.2 地址轉換 .......................................................................................................... 125
2.1.3 設備的I/O 訪問 ................................................................................................ 125
2.2 VMX 架構 ................................................................................................................ 126
2.2.1 VMM 與VM ..................................................................................................... 127
2.2.2 VMXON 與VMCS 區域 .................................................................................... 127
2.2.3 檢測VMX 支持 ................................................................................................ 128
2.2.4 開啓VMX 進入允許 .......................................................................................... 128
2.3 VMX operation 模式 ................................................................................................ 129
2.3.1 進入VMX operation 模式 ................................................................................... 130
2.3.2 進入VMX operation 的製約 ............................................................................... 131
2.3.2.1 IA32_FEATURE_CONTROL 寄存器 ...................................................... 131
2.3.2.2 CR0 與CR4 固定位 .............................................................................. 133
2.3.2.3 A20M 模式 .......................................................................................... 135
2.3.3 設置VMXON 區域 ............................................................................................ 135
2.3.3.1 分配VMXON 區域 ............................................................................... 135
2.3.3.2 VMXON 區域初始設置 ......................................................................... 135
2.3.4 退齣VMX operation 模式 ................................................................................... 136
2.4 VMX operation 模式切換 ........................................................................................ 137
2.4.1 VM entry .......................................................................................................... 138
2.4.2 VM exit ............................................................................................................ 139
2.4.3 SMM 雙重監控處理下 ....................................................................................... 140
2.5 VMX 能力的檢測 .................................................................................................... 141
2.5.1 檢測是否支持VMX .......................................................................................... 141
2.5.2 通過MSR 組檢查VMX 能力 ............................................................................. 141
2.5.3 例子2-1 ............................................................................................................ 146
2.5.4 基本信息檢測 ................................................................................................... 147
2.5.5 允許為0 以及允許為1 位 .................................................................................. 149
2.5.5.1 決定VMX 支持的功能 ......................................................................... 150
2.5.5.2 控製字段設置算法 ................................................................................ 150
2.5.6 VM-execution 控製字段 ..................................................................................... 151
2.5.6.1 Pin-based VM-execution control 字段 ....................................................... 151
2.5.6.2 primary processor-based VM-execution control 字段 .................................. 152
2.5.6.3 secondary processor-based VM-execution control 字段 ............................... 152
2.5.7 VM-exit control 字段 .......................................................................................... 152
2.5.8 VM-entry control 字段 ........................................................................................ 153
2.5.9 VM-function control 字段 .................................................................................... 153
2.5.10 CR0 與CR4 的固定位 ...................................................................................... 154
2.5.10.1 CR0 與CR4 寄存器設置算法 ............................................................... 155
2.5.11 VMX 雜項信息 ................................................................................................ 156
2.5.12 VMCS 區域字段index 值 ................................................................................. 157
2.5.13 VPID 與EPT 能力 ........................................................................................... 157
2.6 VMX 指令 ................................................................................................................ 158
2.6.1 VMX 指令執行環境 .......................................................................................... 159
2.6.2 指令執行的狀態 ................................................................................................ 159
2.6.3 VMfailValid 事件原因 ........................................................................................ 160
2.6.4 指令異常優先級 ................................................................................................ 161
2.6.5 VMCS 管理指令 ................................................................................................ 161
2.6.5.1 VMPTRLD 指令 ................................................................................... 162
2.6.5.2 VMPTRST 指令 .................................................................................... 162
2.6.5.3 VMCLEAR 指令 ................................................................................... 162
2.6.5.4 VMREAD 指令 ..................................................................................... 163
2.6.5.5 VMWRITE 指令 ................................................................................... 165
2.6.6 VMX 模式管理指令 .......................................................................................... 166
2.6.6.1 VMXON 指令 ...................................................................................... 167
2.6.6.2 VMXOFF 指令 ..................................................................................... 167
2.6.6.3 VMLAUNCH 指令 ................................................................................ 167
2.6.6.4 VMRESUME 指令 ................................................................................ 168
2.6.6.5 返迴到executive monitor ....................................................................... 168
2.6.7 cache 刷新指令.................................................................................................. 169
2.6.7.1 INVEPT 指令 ....................................................................................... 170
2.6.7.2 INVVPID 指令 ..................................................................................... 170
2.6.8 調用服務例程指令 ............................................................................................ 171
2.6.8.1 VMCALL 指令 ..................................................................................... 171
2.6.8.2 VMFUNC 指令 ..................................................................................... 172
第3 章 VMCS 結構 .................................................................................... 173
3.1 VMCS 狀態 .............................................................................................................. 173
3.1.1 activity 屬性 ...................................................................................................... 174
3.1.2 current 屬性 ...................................................................................................... 174
3.1.3 launch 屬性 ....................................................................................................... 174
3.2 VMCS 區域 .............................................................................................................. 175
3.2.1 VMXON 區域 ................................................................................................... 176
3.2.2 Executive-VMCS 與SMM-transfer VMCS ............................................................ 176
3.2.3 VMCS 區域格式 ................................................................................................ 176
3.3 訪問VMCS 字段 ..................................................................................................... 177
3.3.1 字段ID 格式 ..................................................................................................... 178
3.3.2 不同寬度的字段處理 ......................................................................................... 179
3.4 字段ID 值 ................................................................................................................ 181
3.4.1 16 位字段ID ..................................................................................................... 181
3.4.2 64 位字段ID ..................................................................................................... 182
3.4.3 32 位字段ID ..................................................................................................... 184
3.4.4 natural-width 字段ID ......................................................................................... 185
3.5 VM-execution 控製類字段 ...................................................................................... 187
3.5.1 Pin-based VM-execution control 字段 ................................................................... 188
3.5.2 processor-based VM-execution control 字段 ........................................................... 190
3.5.2.1 primary processor-based VM-execution control 字段 .................................. 191
3.5.2.2 secondary processor-based VM-execution control 字段 ............................... 195
3.5.3 exception bitmap 字段 ......................................................................................... 200
3.5.4 PFEC_MASK 與PFEC_MATCH 字段 ................................................................. 200
3.5.5 I/O bitmap address 字段 ...................................................................................... 202
3.5.6 TSC offset 字段 ................................................................................................. 202
3.5.7 guest/host mask 與read shadow 字段 ................................................... 202
3.5.8 CR3-target 字段 ................................................................................................. 203
3.5.9 APIC-access address 字段 ................................................................................... 203
3.5.10 virtual-APIC address 字段 ................................................................................. 204
3.5.11 TPR threshold 字段 ........................................................................................... 204
3.5.12 EOI-exit bitmap 字段 ........................................................................................ 204
3.5.13 posted-interrupt notification vector 字段 .............................................. 205
3.5.14 posted-interrupt descriptor address 字段 ........................................ 205
3.5.15 MSR bitmap address 字段 .................................................................................. 205
3.5.16 executive-VMCS pointer .................................................................................... 206
3.5.17 EPTP 字段 ...................................................................................................... 206
3.5.18 virtual-processor identifier 字段 .......................................................................... 207
3.5.19 PLE_Gap 與PLE_Window 字段 ........................................................................ 207
3.5.20 VM-function control 字段 .................................................................................. 209
3.5.21 EPTP-list address 字段 ...................................................................................... 210
3.6 VM-entry 控製類字段 ............................................................................................. 210
3.6.1 VM-entry control 字段 ........................................................................................ 211
3.6.2 VM-entry MSR-load 字段 ................................................................................... 214
3.6.3 事件注入控製字段 ............................................................................................ 214
3.6.3.1 VM-entry interruption information 字段 .................................................... 215
3.6.3.2 VM-entry exception error code 字段 ......................................................... 217
3.6.3.3 VM-entry instruction length 字段 ............................................................. 217
3.7 VM-exit 控製類字段 ................................................................................................ 218
3.7.1 VM-exit control 字段 .......................................................................................... 218
3.7.2 VM-exit MSR-store 與MSR-load 字段 ................................................................. 220
3.8 guest-state 區域字段 ................................................................................................ 221
3.8.1 段寄存器字段 ................................................................................................... 224
3.8.1.1 access right 字段 ................................................................................... 224
3.8.2 GDTR 與IDTR 字段 .......................................................................................... 229
3.8.3 MSR 字段 ......................................................................................................... 229
3.8.4 SMBASE 字段 .................................................................................................. 229
3.8.5 activity state 字段 ............................................................................................... 230
3.8.6 interruptibility state 字段 ..................................................................................... 232
3.8.7 pending debug exceptions 字段 ............................................................................ 235
3.8.7.1 #DB 異常的處理 ................................................................................... 237
3.8.8 VMCS link pointer 字段 ...................................................................................... 243
3.8.9 VMX-preemption timer value 字段 ....................................................................... 243
3.8.10 PDPTEs 字段 ................................................................................................... 243
3.8.11 guest interrupt status 字段 .................................................................................. 244
3.9 host-state 區域字段 .................................................................................................. 245
3.10 VM-exit 信息類字段 .............................................................................................. 247
3.10.1 基本信息類字段 .............................................................................................. 248
3.10.1.1 Exit reason 字段 .................................................................................. 248
3.10.1.2 VM-exit 原因 ...................................................................................... 249
3.10.1.3 Exit qualification 字段 .......................................................................... 255
3.10.1.4 由某些指令引發的VM-exit ................................................................. 256
3.10.1.5 由#DB 異常引發的VM-exit ................................................................. 256
3.10.1.6 由#PF 異常引發的VM-exit .................................................................. 257
3.10.1.7 由SIPI 引發的VM-exit ....................................................................... 257
3.10.1.8 由I/O SMI 引發的VM-exit .................................................................. 257
3.10.1.9 由任務切換引發的VM-exit ................................................................. 258
3.10.1.10 訪問控製寄存器引發的VM-exit ......................................................... 259
3.10.1.11 由MOV-DR 指令引發的VM-exit ....................................................... 260
3.10.1.12 由I/O 指令引發的VM-exit ................................................................ 260
3.10.1.13 由於訪問APIC-access page 引發的VM-exit......................................... 261
3.10.1.14 由EPT violation 引發的VM-exit ......................................................... 262
3.10.1.15 由EOI 虛擬化引發的VM-exit ............................................................ 264
3.10.1.16 由APIC-write 引發的VM-exit ............................................................ 264
3.10.1.17 guest-linear address 字段 ..................................................................... 264
3.10.1.18 guest-physical address 字段 ................................................................. 265
3.10.2 直接嚮量事件類信息字段 ................................................................................. 265
3.10.2.1 VM-exit interruption information 字段 .................................................... 265
3.10.2.2 VM-exit interruption error code 字段 ...................................................... 267
3.10.3 間接嚮量事件類信息字段 ................................................................................. 267
3.10.3.1 IDT-vectoring information 字段 ............................................................. 268
3.10.3.2 IDT-vectoring error code 字段 ............................................................... 269
3.10.4 指令類信息字段 .............................................................................................. 269
3.10.4.1 VM-exit instruction length 字段 ............................................................. 269
3.10.4.2 VM-exit instruction information 字段 ..................................................... 272
3.10.5 I/O SMI 信息類字段 ......................................................................................... 280
3.10.6 指令錯誤類字段 .............................................................................................. 280
3.11 VMM 初始化實例 ................................................................................................. 280
3.11.1 VMCS 相關的數據結構 .................................................................................... 281
3.11.1.1 VMB 結構 .......................................................................................... 281
3.11.1.2 VSB 結構 ........................................................................................... 284
3.11.1.3 VMCS buffer 結構 ............................................................................... 287
3.11.2 初始化VMXON 區域 ...................................................................................... 288
3.11.3 初始化VMCS 區域 .......................................................................................... 289
3.11.3.1 分配VMCS 區域 ................................................................................ 290
3.11.3.2 VMCS 初始化模式 .............................................................................. 291
3.11.3.3 VMCS buffer 初始化 ........................................................................... 293
3.11.4 例子3-1 .......................................................................................................... 297
第4 章 VM-entry 處理 ................................................................................... 301
4.1 發起VM-entry 操作 ................................................................................................ 302
4.2 VM-entry 執行流程 ................................................................................................. 303
4.3 指令執行的基本檢查 .............................................................................................. 303
4.4 檢查控製區域及host-state 區域 ............................................................................. 305
4.4.1 VM-execution 控製區域檢查 ............................................................................... 305
4.4.1.1 檢查pin-based VM-execution control 字段 ............................................... 306
4.4.1.2 檢查primary processor-based VM-execution control 字段 ........................... 306
4.4.1.3 檢查secondary processor-based VM-execution control 字段 ........................ 307
4.4.1.4 檢查CR3-target 字段 ............................................................................ 308
4.4.2 VM-exit 控製區域檢查 ....................................................................................... 308
4.4.2.1 VM-exit control 字段的檢查 ................................................................... 308
4.4.2.2 MSR-store 與MSR-load 相關字段的檢查 ................................................ 308
4.4.3 VM-entry 控製區域檢查 ..................................................................................... 309
4.4.3.1 VM-entry control 字段的檢查 ................................................................. 309
4.4.3.2 MSR-load 相關字段的檢查 .................................................................... 309
4.4.3.3 事件注入相關字段的檢查 ..................................................................... 309
4.4.4 Host-state 區域的檢查 ........................................................................................ 310
4.4.4.1 Host 控製寄存器字段的檢查 .................................................................. 310
4.4.4.2 Host-RIP 的檢查 ................................................................................... 310
4.4.4.3 段selector 字段的檢查 .......................................................................... 311
4.4.4.4 段基址字段的檢查 ................................................................................ 311
4.4.4.5 MSR 字段的檢查 .................................................................................. 311
4.5 檢查guest-state 區域 ............................................................................................... 311
4.5.1 檢查控製寄存器字段 ......................................................................................... 312
4.5.2 檢查RIP 與RFLAGS 字段 ................................................................................. 312
4.5.3 檢查DR7 與IA32_DEBUGCTL 字段 .................................................................. 313
4.5.4 檢查段寄存器字段 ............................................................................................ 313
4.5.4.1 virtual-8086 模式下的檢查 ..................................................................... 314
4.5.4.2 unrestricted guest 位為0 時的檢查 .......................................................... 315
4.5.4.3 unrestricted guest 位為1 時的檢查 .......................................................... 318
4.5.5 檢查GDTR 與IDTR 字段 .................................................................................. 320
4.5.6 檢查MSR 字段 ................................................................................................. 320
4.5.7 檢查activity state 字段 ....................................................................................... 321
4.5.8 檢查interruptibility state 字段 ............................................................................. 321
4.5.9 檢查pending debug exception 字段 ...................................................................... 322
4.5.10 檢查VMCS link pointer 字段 ............................................................................ 322
4.5.11 檢查PDPTE 字段 ............................................................................................ 323
4.5.11.1 由加載CR3 引發的PDPTE 檢查 .......................................................... 323
4.6 檢查guest state 引起的VM-entry 失敗 .................................................................. 324
4.7 加載guest 環境信息 ................................................................................................ 324
4.7.1 加載控製寄存器 ................................................................................................ 325
4.7.2 加載DR7 與IA32_DEBUGCTL .......................................................................... 325
4.7.3 加載MSR ......................................................................................................... 325
4.7.4 SMBASE 字段處理 ............................................................................................ 326
4.7.5 加載段寄存器與描述符錶寄存器 ........................................................................ 326
4.7.5.1 unusable 段寄存器 ................................................................................ 327
4.7.5.2 加載GDTR 與IDTR ............................................................................. 327
4.7.6 加載RIP、RSP 和RFLAGS ............................................................................... 327
4.7.7 加載PDPTE 錶項 .............................................................................................. 327
4.8 刷新處理器cache .................................................................................................... 328
4.9 更新Vritual-APIC 狀態 ........................................................................................... 328
4.9.1 PPR 虛擬化 ....................................................................................................... 329
4.9.2 虛擬中斷評估與delivery .................................................................................... 329
4.10 加載MSR-load 列錶 .............................................................................................. 329
4.10.1 IA32_EFER 的加載處理 ................................................................................... 330
4.10.2 其他MSR 字段的加載處理 .............................................................................. 331
4.11 由加載guest state 引起的VM-entry 失敗 ............................................................ 331
4.12 事件注入 ................................................................................................................ 332
4.12.1 注入事件的delivery ......................................................................................... 335
4.12.1.1 保護模式下的事件注入 ....................................................................... 335
4.12.1.2 實模式下的事件注入 ........................................................................... 338
4.12.1.3 virtual-8086 模式下的事件注入 ............................................................ 338
4.12.2 注入事件的間接VM-exit .................................................................................. 339
4.13 執行pending debug exception ............................................................................... 341
4.13.1 注入事件下的#DB 異常delivery ....................................................................... 342
4.13.2 例子4-1 .......................................................................................................... 346
4.13.3 非注入事件下的#DB 異常delivery .................................................................... 351
4.14 使用MTF VM-exit 功能 ....................................................................................... 354
4.14.1 注入事件下的MTF VM-exit ............................................................................. 354
4.14.2 非注入事件下的MTF VM-exit .......................................................................... 355
4.14.3 MTF VM-exit 與其他VM-exit ........................................................................... 355
4.14.4 MTF VM-exit 的優先級彆 ................................................................................. 356
4.14.5 例子4-2 .......................................................................................................... 356
4.15 VM-entry 後直接導緻VM-exit 的事件 ................................................................ 362
4.15.1 VM-exit 事件的優先級彆 .................................................................................. 362
4.15.2 TPR below threshold VM-exit ............................................................................ 363
4.15.3 pending MTF VM-exit ....................................................................................... 364
4.15.4 由pending debug exception 引發的VM-exit ........................................................ 364
4.15.5 VMX-preemption timer ..................................................................................... 364
4.15.6 NMI-window exiting ......................................................................................... 366
4.15.7 interrupt-window exiting .................................................................................... 367
4.16 處理器的可中斷狀態 ............................................................................................ 367
4.16.1 中斷的阻塞狀態 .............................................................................................. 367
4.16.2 阻塞狀態的解除 .............................................................................................. 368
4.16.3 中斷的阻塞 ..................................................................................................... 369
4.16.4 VM-entry 後的可中斷狀態 ................................................................................ 370
4.17 處理器的活動狀態 ................................................................................................ 370
4.17.1 active 與inactive 狀態 ...................................................................................... 371
4.17.2 事件的阻塞 ..................................................................................................... 371
4.17.3 inactive 狀態的喚醒 ......................................................................................... 372
4.17.4 VM-entry 後的活動狀態 ................................................................................... 372
4.18 VM-entry 的機器檢查事件 ................................................................................... 373
第5 章 VM-exit 處理 ...................................................................................... 374
5.1 無條件引發VM-exit 的指令 ................................................................................... 374
5.2 有條件引發VM-exit 的指令 ................................................................................... 375
5.3 引發VM-exit 的事件 ............................................................................................... 377
5.4 由於VM-entry 失敗導緻的VM-exit ...................................................................... 380
5.5 例子5-1 .................................................................................................................... 380
5.6 指令引發的異常與VM-exit .................................................................................... 385
5.6.1 優先級高於VM-exit 的異常 ............................................................................... 386
5.6.2 VM-exit 優先級高於指令的異常 ......................................................................... 387
5.6.3 例子5-2 ............................................................................................................ 387
5.7 VM-exit 的處理流程 ................................................................................................ 389
5.8 記錄VM-exit 的相關信息 ....................................................................................... 390
5.9 更新VM-entry 區域字段 ........................................................................................ 391
5.10 更新處理器狀態信息 ............................................................................................ 391
5.10.1 直接VM-exit 事件下的狀態更新 ....................................................................... 393
5.10.2 間接VM-exit 事件下的狀態更新 ....................................................................... 394
5.10.3 其他情況下的狀態更新 .................................................................................... 395
5.11 保存guest 環境信息 .............................................................................................. 397
5.11.1 保存控製寄存器,debug 寄存器及MSR ............................................................ 397
5.11.2 保存RIP 與RSP .............................................................................................. 397
5.11.3 保存RFLAGS ................................................................................................. 399
5.11.4 保存段寄存器 .................................................................................................. 399
5.11.5 保存GDTR 與IDTR ........................................................................................ 400
5.11.6 保存activity 與interruptibility 狀態信息 ............................................. 400
5.11.7 保存pending debug exception 信息 .................................................................... 400
5.11.8 保存VMX-preemption timer 值 ......................................................................... 402
5.11.9 保存PDPTE .................................................................................................... 402
5.11.10 保存SMBASE 與VMCS-link pointer ............................................................... 403
5.12 保存MSR-store 列錶 ............................................................................................. 403
5.13 加載host 環境 ........................................................................................................ 404
5.13.1 加載控製寄存器 .............................................................................................. 404
5.13.2 加載DR7 與MSR ............................................................................................ 405
5.13.3 加載host 段寄存器 .......................................................................................... 405
5.13.3.1 加載selector ....................................................................................... 406
5.13.3.2 加載base ............................................................................................ 406
5.13.3.3 加載limit ........................................................................................... 406
5.13.3.4 加載access rights ................................................................................ 407
5.13.4 加載GDTR 與IDTR ........................................................................................ 408
5.13.5 加載RIP,RSP 及RFLAGS ............................................................................. 408
5.13.6 加載PDPTE .................................................................................................... 408
5.14 更新host 處理器狀態信息 .................................................................................... 409
5.15 刷新處理器cache 信息 ......................................................................................... 409
5.16 加載MSR-load 列錶 .............................................................................................. 410
5.17 VMX-abort ............................................................................................................. 411
第6 章 內存虛擬化 ................................................................................... 412
6.1 EPT(擴展頁錶)機製 ........................................................................................... 412
6.1.1 EPT 機製概述 ................................................................................................... 413
6.1.1.1 guest 分頁機製與EPT ........................................................................... 413
6.1.2 EPT 頁錶結構 ................................................................................................... 416
6.1.3 guest-physical address ......................................................................................... 417
6.1.4 EPTP ................................................................................................................ 417
6.1.5 4K 頁麵下的EPT 頁錶結構 ................................................................................ 418
6.1.6 2M 頁麵下的EPT 頁錶結構 ............................................................................... 422
6.1.7 1G 頁麵下的EPT 頁錶結構 ................................................................................ 424
6.1.8 EPT 導緻的VM-exit .......................................................................................... 426
6.1.8.1 EPT violation ........................................................................................ 426
6.1.8.2 EPT misconfiguration ............................................................................. 427
6.1.8.3 EPT 頁故障的優先級 ........................................................................... 428
6.1.8.4 修復EPT 頁故障 .................................................................................. 431
6.1.9 accessed 與dirty 標誌位 ..................................................................................... 436
6.1.10 EPT 內存類型.................................................................................................. 438
6.1.11 EPTP switching ................................................................................................ 440
6.1.12 實現EPT 機製 ................................................................................................. 442
6.2 Cache 管理 ............................................................................................................... 454
6.2.1 linear mapping(綫性映射) ............................................................................... 455
6.2.2 guest-physical mapping(guest 物理映射) ........................................................... 456
6.2.3 combined mapping(閤並映射) ......................................................................... 457
6.2.4 cache 域 ............................................................................................................ 458
6.2.5 cache 建立 ........................................................................................................ 463
6.2.6 cache 刷新 ........................................................................................................ 465
6.2.6.1 INVLPG 指令刷新cache ....................................................................... 468
6.2.6.2 INVPCID 指令刷新cache ...................................................................... 468
6.2.6.3 INVVPID 指令刷新cache ...................................................................... 469
6.2.6.4 INVEPT 指令刷新cache ........................................................................ 470
6.2.6.5 INVVPID 指令使用指南 ........................................................................ 470
6.2.6.6 INVEPT 指令使用指南 .......................................................................... 471
6.3 內存虛擬化管理 ...................................................................................................... 473
6.3.1 分配物理內存 ................................................................................................... 473
6.3.2 實模式guest OS 內存處理 .................................................................................. 475
6.3.3 guest 內存虛擬化 ............................................................................................... 476
6.3.3.1 guest 虛擬地址轉換 .............................................................................. 477
6.3.3.2 guest OS 的cache 管理 .......................................................................... 479
6.4 例子6-1 .................................................................................................................... 482
6.4.1 GuestBoot 模塊.................................................................................................. 483
6.4.2 GuestKernel 模塊 ............................................................................................... 486
6.4.3 VSB 結構 ......................................................................................................... 495
6.4.4 VMM 初始化guest ............................................................................................ 498
6.4.5 使用VMX-preemption timer ............................................................................... 503
6.4.6 host 處理流程 .................................................................................................... 507
6.4.7 運行結果 .......................................................................................................... 511
第7 章 中斷虛擬化 ........................................................................................ 522
7.1 異常處理 .................................................................................................................. 522
7.1.1 反射異常給guest ............................................................................................... 523
7.1.2 恢復guest 異常 ................................................................................................. 526
7.1.2.1 直接恢復 ............................................................................................. 526
7.1.2.2 例子7-1 ............................................................................................... 527
7.1.2.3 恢復原始嚮量事件 ................................................................................ 533
7.1.3 處理任務切換 ................................................................................................... 535
7.1.3.1 檢查任務切換條件 ................................................................................ 535
7.1.3.2 VMM 處理任務切換 ............................................................................. 537
7.1.3.3 恢復guest 運行 .................................................................................... 547
7.1.3.4 例子7-2 ............................................................................................... 551
7.2 Local APIC 虛擬化 .................................................................................................. 554
7.2.1 監控guest 訪問local APIC ................................................................................. 554
7.2.1.1 例子7-3 ............................................................................................... 555
7.2.2 local APIC 虛擬化機製 ....................................................................................... 571
7.2.3 APIC-access page ............................................................................................... 573
7.2.3.1 APIC-access page 的設置 ....................................................................... 574
7.2.4 虛擬化x2APIC MSR 組 ..................................................................................... 577
7.2.5 virtual-APIC page ............................................................................................... 578
7.2.6 APIC-access VM-exit .......................................................................................... 581
7.2.6.1 APIC-access VM-exit 優先級彆 .............................................................. 581
7.2.7 虛擬化讀取APIC-access page ............................................................................. 582
7.2.8 虛擬化寫入APIC-access page ............................................................................. 584
7.2.9 虛擬化基於MSR 讀local APIC .......................................................................... 587
7.2.10 虛擬化基於MSR 寫local APIC ......................................................................... 588
7.2.11 虛擬化基於CR8 訪問TPR ............................................................................... 589
7.2.12 local APIC 虛擬化操作 ..................................................................................... 589
7.2.12.1 TPR 虛擬化 ........................................................................................ 590
7.2.12.2 PPR 虛擬化 ........................................................................................ 591
7.2.12.3 EOI 虛擬化......................................................................................... 591
7.2.12.4 Self-IPI 虛擬化 ................................................................................... 593
7.2.13 虛擬中斷的評估與delivery ............................................................................... 593
7.2.13.1 虛擬中斷的評估 ................................................................................. 594
7.2.13.2 虛擬中斷的delivery ............................................................................ 596
7.2.14 posted-interrupt 處理 ......................................................................................... 597
7.3 中斷處理 .................................................................................................................. 601
7.3.1 攔截INT 指令 ................................................................................................... 601
7.3.1.1 處理IDTR.limit .................................................................................... 602
7.3.1.2 處理#GP 異常 ...................................................................................... 605
7.3.1.3 處理中斷delivery ................................................................................. 608
7.3.1.4 完成中斷的delivery 操作 ...................................................................... 618
7.3.1.5 例子7-4 ............................................................................................... 628
7.3.2 處理NMI ......................................................................................................... 632
7.3.2.1 攔截NMI ............................................................................................. 632
7.3.2.2 虛擬NMI ............................................................................................. 634
7.3.3 處理外部中斷 ................................................................................................... 634
7.3.3.1 攔截外部中斷 ....................................................................................... 634
7.3.3.2 轉發外部中斷 ....................................................................................... 635
7.3.3.3 監控guest 設置8259 ............................................................................. 637
7.3.3.4 例子7-5 ............................................................................................... 642
· · · · · · (
收起)