[hellogcc] Re: QEMU Internal - Precise Exception Handling 1/5

  • From: Yao Qi <qiyaoltc@xxxxxxxxx>
  • To: hellogcc@xxxxxxxxxxxxx
  • Date: Tue, 6 Mar 2012 14:49:18 +0800

On Sat, Mar 03, 2012 at 09:51:22PM +0800, 陳韋任 wrote:
>                  Copyright (c) 2012 陳韋任 (Chen Wei-Ren)
> 
> 一般計算機架構會定義當例外/中斷發生時,當下的 CPU 狀態應當為何,這個稱之為
> precise exception。假設當下流水線是底下這樣:
> 
>   A. addl %eax,(%esp)
>   B. addl %ebx,(%esp)
>   C. movl %esi,(%ebp)
>   D. subl %ecx,5
> 
> 當執行到指令 (C),欲從 %ebp 所指的內存位址讀取資料至 %esi 時,發生頁缺失例外。
> 以 x86 對 precise exception 的定義,在指令 (C) 之前的指令,即 (A) 和 (B) 其
> 結果必須完成。也就是說,暫存器/內存的內容應該更新; 在指令 (C) 之後的指令,
> 即 (D) 的結果必須捨棄。此範例取自 [1]。
> 
> Precise exception 在 binary translation 中佔有重要地位。在此以 QEMU 為例,說
> 明 QEMU 如何確保 pecise exception。由以上對 pecise exception 的說明,我們可以
> 知道 precise exception 必須考量暫存器和內存。在 binary translation 中,我們
> 關注的是客戶 (guest) 的 precise exception。因此,我們必須確保當 guest 代碼發生
> 例外時,guest 的暫存器和內存其內容必須滿足 precise exception 的要求。這樣 guest
> 的 exception handler 才能正確處理該例外。
> 
> 就 guest 暫存器而言,在 QEMU 中需要考量的是 CPUState。QEMU 在每一個可能會發生
> 例外的指令或是 helper function 之前,會將 CPUState 中的大部分內容更新,少數
> 未更新的內容會在 guest 真正發生例外時重新再計算。以 x86 為例,pc 和 condition
> code 屬於後者。在 binary translation 中,出於效能上的考量,通常會在一個 basic
> block 的結尾才更新 guest pc,而非每翻譯一個 guest 指令就更新 guest pc。
> 
> 就 guest 內存而言,我們將重點放在 guest memory store operation 上,因為只有
> memory store operation 會修改 guest 內存的內容。第一,QEMU 會依照 guest 原本
> memory store operation 的順序進行翻譯。第二,針對所有潛在會發生例外的 guest
> 指令,QEMU 保留其相對於 guest memory store operation 的順序。簡單來說,QEMU
> 不會 reorder guest 指令順序。這簡化了 QEMU 維護 precise exception 的複雜度,
> 但同時也喪失了一些潛在可能的優化。[2][3]

能不能给几个例子解释,你这两个 第一和第二;

  char *p = NULL;
  int d1,d2;
  char c;

[1]  d1 = d2/0; // divide zero

  p = (char *) &d1;
[2]  d2 = * (int *) (p + 1); // un-aligned access, sigbus

  p = NULL;

[3] *p = 'a'; // write, segfault

[4] *p = 'b'; 


按照你 第一个说法,[4] 是不可能在 [3] 之前的。
按照你 第二个说法,[1] 和 [2] 相对于 3 的位置是不变的,也就是说 1 和 2
的相对位置是不变的。?
-- 
Yao Qi <qiyaoltc AT gmail DOT com>

You should emulate your heros, but don't carry it too far.  Especially
if they are dead.

Other related posts: