Skip to contentSkip to conversation

emulated cmpxchg8b should be atomic on i386

PATCH Applied Marcelo Tosatti 2 participants 4 messages Dec 10, 2007 – Dec 13, 2007 1 message not in the archive KVM

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 emulated cmpxchg8b should be atomic on i386

Emulate cmpxchg8b atomically on i386. This is required to avoid a guest pte walker from seeing a splitted write.

drivers/kvm/x86.c +27 -0
index 9db4e32..d58d795 100644
@@ -1674,6 +1674,33 @@ static int emulator_cmpxchg_emulated(uns
 		reported = 1;
 		printk(KERN_WARNING "kvm: emulating exchange as write\n");
 	}
+#ifndef CONFIG_X86_64
+	/* guests cmpxchg8b have to be emulated atomically */
+	if (bytes == 8) {
+		gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
+		struct page *page;
+		char *addr;
+		u64 *val;
+
+		if (gpa == UNMAPPED_GVA ||
+	    	   (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
+			goto emul_write;
+
+		val = (u64 *)new;
+		page = gfn_to_page(page, gpa >> PAGE_SHIFT);
+		addr = kmap_atomic(page, KM_USER0);
+		addr += offset_in_page(gpa);
+
+		set_64bit((unsigned long long *)addr, val);
+
+		kunmap_atomic(page, KM_USER0);
+		kvm_release_page_dirty(page);
+
+		return X86EMUL_CONTINUE;
+	}
+#endif
+
+emul_write:
 	return emulator_write_emulated(addr, new, bytes, vcpu);
 }
 

-------------------------------------------------------------------------
SF.Net email is sponsored by:
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php

#1 lore raw
Quoted 33 lines · Marcelo Tosatti

Emulate cmpxchg8b atomically on i386. This is required to avoid a guest pte walker from seeing a splitted write.

drivers/kvm/x86.c +20 -0
index 9db4e32..d58d795 100644
@@ -1674,6 +1674,33 @@ static int emulator_cmpxchg_emulated(uns
 		reported = 1;
 		printk(KERN_WARNING "kvm: emulating exchange as write\n");
 	}
+#ifndef CONFIG_X86_64
+	/* guests cmpxchg8b have to be emulated atomically */
+	if (bytes == 8) {
+		gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
+		struct page *page;
+		char *addr;
+		u64 *val;
+
+		if (gpa == UNMAPPED_GVA ||
+	    	   (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
+			goto emul_write;
+
+		val = (u64 *)new;
+		page = gfn_to_page(page, gpa >> PAGE_SHIFT);
+		addr = kmap_atomic(page, KM_USER0);
+		addr += offset_in_page(gpa);
+
+		set_64bit((unsigned long long *)addr, val);
+
+		kunmap_atomic(page, KM_USER0);

drivers/kvm/x86.c static int emulator_cmpxchg_emulated(uns · line 1706

Quoted 10 lines
drivers/kvm/x86.c +7 -0
+		kvm_release_page_dirty(page);
+
+		return X86EMUL_CONTINUE;
+	}
+#endif
+
+emul_write:
 	return emulator_write_emulated(addr, new, bytes, vcpu);
 }
 

Won't the compiler complain when it sees an unreferenced label?

Also, we should handle the page boundary crossing case (probably by just emulating as a write and crosssing fingers).

Signature · 9 lines
Any sufficiently difficult bug is indistinguishable from a feature.


-------------------------------------------------------------------------
SF.Net email is sponsored by:
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php

A message that is not in the archive (sent off-list or not yet received): 475DAFE3.30501-atKUWr5tajBWk0Htik3J/[email protected]

#3 lore raw
Quoted 47 lines · Avi Kivity
Quoted 33 lines · Marcelo Tosatti

Emulate cmpxchg8b atomically on i386. This is required to avoid a guest pte walker from seeing a splitted write.

drivers/kvm/x86.c +20 -0
index 9db4e32..d58d795 100644
@@ -1674,6 +1674,33 @@ static int emulator_cmpxchg_emulated(uns
 		reported = 1;
 		printk(KERN_WARNING "kvm: emulating exchange as write\n");
 	}
+#ifndef CONFIG_X86_64
+	/* guests cmpxchg8b have to be emulated atomically */
+	if (bytes == 8) {
+		gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
+		struct page *page;
+		char *addr;
+		u64 *val;
+
+		if (gpa == UNMAPPED_GVA ||
+	    	   (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
+			goto emul_write;
+
+		val = (u64 *)new;
+		page = gfn_to_page(page, gpa >> PAGE_SHIFT);
+		addr = kmap_atomic(page, KM_USER0);
+		addr += offset_in_page(gpa);
+
+		set_64bit((unsigned long long *)addr, val);
+
+		kunmap_atomic(page, KM_USER0);

drivers/kvm/x86.c static int emulator_cmpxchg_emulated(uns · line 1706

Quoted 10 lines
drivers/kvm/x86.c +7 -0
+		kvm_release_page_dirty(page);
+
+		return X86EMUL_CONTINUE;
+	}
+#endif
+
+emul_write:
 	return emulator_write_emulated(addr, new, bytes, vcpu);
 }
 

Won't the compiler complain when it sees an unreferenced label?

Quoted 2 lines

Also, we should handle the page boundary crossing case (probably by just emulating as a write and crosssing fingers).

Pagetable writes should not cross page boundaries, so that should be fine.

------------------

Emulate cmpxchg8b atomically on i386. This is required to avoid a guest pte walker from seeing a splitted write.

drivers/kvm/x86.c +27 -0
index 9db4e32..e37c19d 100644
@@ -1674,6 +1674,33 @@ static int emulator_cmpxchg_emulated(uns
 		reported = 1;
 		printk(KERN_WARNING "kvm: emulating exchange as write\n");
 	}
+#ifndef CONFIG_X86_64
+	/* guests cmpxchg8b have to be emulated atomically */
+	if (bytes == 8) {
+		gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
+		struct page *page;
+		char *addr;
+		u64 *val;
+
+		if (gpa == UNMAPPED_GVA ||
+	    	   (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
+			goto emul_write;
+
+		if (((addr + bytes - 1) & PAGE_MASK) != (addr & PAGE_MASK))
+			goto emul_write;
+
+		val = (u64 *)new;
+		page = gfn_to_page(page, gpa >> PAGE_SHIFT);
+		addr = kmap_atomic(page, KM_USER0);
+		addr += offset_in_page(gpa);
+
+		set_64bit((unsigned long long *)addr, val);
+		kunmap_atomic(page, KM_USER0);
+		kvm_release_page_dirty(page);
+	}
+emul_write:
+#endif
+
 	return emulator_write_emulated(addr, new, bytes, vcpu);
 }
 

-------------------------------------------------------------------------
SF.Net email is sponsored by:
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php

Applied
#4 lore raw
Quoted 3 lines · Marcelo Tosatti

Emulate cmpxchg8b atomically on i386. This is required to avoid a guest pte walker from seeing a splitted write.

Applied, thanks.

Signature · 9 lines
error compiling committee.c: too many arguments to function


-------------------------------------------------------------------------
SF.Net email is sponsored by:
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services
for just about anything Open Source.
http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace