Skip to contentSkip to conversation

kvm-12 userland guest reboot fix

PATCH Applied Joerg Roedel 3 participants 6 messages Jan 31, 2007 – Feb 4, 2007 3 messages not in the archive KVM

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kvm-12 userland guest reboot fix

From: Markus Rechberger <[email protected]>
From: Joerg Roedel <[email protected]>

This patch fixes the initialization of the segment registers which solves the triple fault and keyboard controller reset problems in kvm/qemu guests as well as the slow grub menu interaction. The patch should also work on Intel VMX now.

Signature · 3 lines
Joerg Roedel
Operating System Research Center
AMD Saxony LLC & Co. KG
kernel/vmx.c +3 -0
diff -upr kvm-12/kernel/vmx.c kvm-12-reboot-fixed/kernel/vmx.c
@@ -781,6 +781,9 @@ static void vmx_set_cr0(struct kvm_vcpu 
  */
 static void vmx_set_cr0_no_modeswitch(struct kvm_vcpu *vcpu, unsigned long cr0)
 {
+	if (!vcpu->rmode.active && !(cr0 & CR0_PE_MASK))
+		enter_rmode(vcpu);
+	
 	vcpu->rmode.active = ((cr0 & CR0_PE_MASK) == 0);
 	update_exception_bitmap(vcpu);
 	vmcs_writel(CR0_READ_SHADOW, cr0);
qemu/qemu-kvm.c +7 -0
diff -upr kvm-12/qemu/qemu-kvm.c kvm-12-reboot-fixed/qemu/qemu-kvm.c
@@ -582,6 +582,12 @@ static int kvm_halt(void *opaque, int vc
 
     return 1;
 }
+
+static int kvm_shutdown(void *opaque, int vcpu)
+{
+    qemu_system_reset_request();
+    return 1;
+}
  
 static struct kvm_callbacks qemu_kvm_ops = {
     .cpuid = kvm_cpuid,
@@ -601,6 +607,7 @@ static struct kvm_callbacks qemu_kvm_ops
     .writel = kvm_writel,
     .writeq = kvm_writeq,
     .halt  = kvm_halt,
+    .shutdown = kvm_shutdown,
     .io_window = kvm_io_window,
     .try_push_interrupts = try_push_interrupts,
     .post_kvm_run = post_kvm_run,
qemu/target-i386/helper2.c +6 -3
diff -upr kvm-12/qemu/target-i386/helper2.c kvm-12-reboot-fixed/qemu/target-i386/helper2.c
@@ -151,6 +151,9 @@ CPUX86State *cpu_x86_init(void)
 void cpu_reset(CPUX86State *env)
 {
     int i;
+    unsigned int flags = DESC_P_MASK |
+                         DESC_S_MASK |
+                         (2 << DESC_TYPE_SHIFT);
 
     memset(env, 0, offsetof(CPUX86State, breakpoints));
 
@@ -173,9 +176,9 @@ void cpu_reset(CPUX86State *env)
     env->tr.flags = DESC_P_MASK;
     
     cpu_x86_load_seg_cache(env, R_CS, 0xf000, 0xffff0000, 0xffff, 0); 
-    cpu_x86_load_seg_cache(env, R_DS, 0, 0, 0xffff, 0);
-    cpu_x86_load_seg_cache(env, R_ES, 0, 0, 0xffff, 0);
-    cpu_x86_load_seg_cache(env, R_SS, 0, 0, 0xffff, 0);
+    cpu_x86_load_seg_cache(env, R_DS, 0, 0, 0xffff, flags);
+    cpu_x86_load_seg_cache(env, R_ES, 0, 0, 0xffff, flags);
+    cpu_x86_load_seg_cache(env, R_SS, 0, 0, 0xffff, flags);
     cpu_x86_load_seg_cache(env, R_FS, 0, 0, 0xffff, 0);
     cpu_x86_load_seg_cache(env, R_GS, 0, 0, 0xffff, 0);
     
qemu/vl.c +4 -0
diff -upr kvm-12/qemu/vl.c kvm-12-reboot-fixed/qemu/vl.c
@@ -5281,6 +5281,10 @@ int main_loop(void)
             if (reset_requested) {
                 reset_requested = 0;
                 qemu_system_reset();
+#ifdef USE_KVM
+		if (kvm_allowed)
+			kvm_load_registers(env);
+#endif
                 ret = EXCP_INTERRUPT;
             }
             if (powerdown_requested) {

Only in kvm-12-reboot-fixed/qemu: x86_64-softmmu

user/kvmctl.c +8 -0
diff -upr kvm-12/user/kvmctl.c kvm-12-reboot-fixed/user/kvmctl.c
@@ -522,6 +522,11 @@ static int handle_halt(kvm_context_t kvm
 	return kvm->callbacks->halt(kvm->opaque, kvm_run->vcpu);
 }
 
+static int handle_shutdown(kvm_context_t kvm, struct kvm_run *kvm_run)
+{
+	return kvm->callbacks->shutdown(kvm->opaque, kvm_run->vcpu);
+}
+
 int try_push_interrupts(kvm_context_t kvm)
 {
 	return kvm->callbacks->try_push_interrupts(kvm->opaque);
@@ -594,6 +599,9 @@ again:
 			break;
 		case KVM_EXIT_IRQ_WINDOW_OPEN:
 			break;
+		case KVM_EXIT_SHUTDOWN:
+			r = handle_shutdown(kvm, &kvm_run);
+			break;
 		default:
 			fprintf(stderr, "unhandled vm exit: 0x%x\n", kvm_run.exit_reason);
 			kvm_show_regs(kvm, vcpu);
user/kvmctl.h +1 -0
diff -upr kvm-12/user/kvmctl.h kvm-12-reboot-fixed/user/kvmctl.h
@@ -59,6 +59,7 @@ struct kvm_callbacks {
 	 * on the host CPU.
 	 */
     int (*halt)(void *opaque, int vcpu);
+    int (*shutdown)(void *opaque, int vcpu);
     int (*io_window)(void *opaque);
     int (*try_push_interrupts)(void *opaque);
     void (*post_kvm_run)(void *opaque, struct kvm_run *kvm_run);

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV

_______________________________________________
kvm-devel mailing list [email protected]
https://lists.sourceforge.net/lists/listinfo/kvm-devel

A message that is not in the archive (sent off-list or not yet received): [email protected]

#2 lore raw
Quoted 24 lines · Joerg Roedel

From: Markus Rechberger <[email protected]>
From: Joerg Roedel <[email protected]>

This patch fixes the initialization of the segment registers which solves the triple fault and keyboard controller reset problems in kvm/qemu guests as well as the slow grub menu interaction. The patch should also work on Intel VMX now.

diff -upr kvm-12/qemu/target-i386/helper2.c kvm-12-reboot-fixed/qemu/target-i386/helper2.c

qemu/target-i386/helper2.c +3 -0
@@ -151,6 +151,9 @@ CPUX86State *cpu_x86_init(void)
 void cpu_reset(CPUX86State *env)
 {
     int i;
+    unsigned int flags = DESC_P_MASK |
+                         DESC_S_MASK |
+                         (2 << DESC_TYPE_SHIFT);
 
     memset(env, 0, offsetof(CPUX86State, breakpoints));
 
@@ -173,9 +176,9 @@ void cpu_reset(CPUX86State *env)
     env->tr.flags = DESC_P_MASK;
 

qemu/target-i386/helper2.c void cpu_reset(CPUX86State *env) · line 184

Quoted 10 lines
qemu/target-i386/helper2.c +3 -3
     cpu_x86_load_seg_cache(env, R_CS, 0xf000, 0xffff0000, 0xffff, 0); 
-    cpu_x86_load_seg_cache(env, R_DS, 0, 0, 0xffff, 0);
-    cpu_x86_load_seg_cache(env, R_ES, 0, 0, 0xffff, 0);
-    cpu_x86_load_seg_cache(env, R_SS, 0, 0, 0xffff, 0);
+    cpu_x86_load_seg_cache(env, R_DS, 0, 0, 0xffff, flags);
+    cpu_x86_load_seg_cache(env, R_ES, 0, 0, 0xffff, flags);
+    cpu_x86_load_seg_cache(env, R_SS, 0, 0, 0xffff, flags);
     cpu_x86_load_seg_cache(env, R_FS, 0, 0, 0xffff, 0);
     cpu_x86_load_seg_cache(env, R_GS, 0, 0, 0xffff, 0);
 

This changes core qemu code, which may make merging our code back to qemu more difficult. It also means we need to test with -no-kvm.

Can you try making load_regs() in qemu-kvm.c detect the case of flags == 0 (perhaps only if cr0.pg == 0) and changing it only then? It should have the same effect.

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


-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier.
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642

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

#4 lore raw
Quoted 39 lines · Avi Kivity
Quoted 24 lines · Joerg Roedel

From: Markus Rechberger <[email protected]>
From: Joerg Roedel <[email protected]>

This patch fixes the initialization of the segment registers which solves the triple fault and keyboard controller reset problems in kvm/qemu guests as well as the slow grub menu interaction. The patch should also work on Intel VMX now.

diff -upr kvm-12/qemu/target-i386/helper2.c kvm-12-reboot-fixed/qemu/target-i386/helper2.c

qemu/target-i386/helper2.c +3 -0
@@ -151,6 +151,9 @@ CPUX86State *cpu_x86_init(void)
 void cpu_reset(CPUX86State *env)
 {
     int i;
+    unsigned int flags = DESC_P_MASK |
+                         DESC_S_MASK |
+                         (2 << DESC_TYPE_SHIFT);
 
     memset(env, 0, offsetof(CPUX86State, breakpoints));
 
@@ -173,9 +176,9 @@ void cpu_reset(CPUX86State *env)
     env->tr.flags = DESC_P_MASK;
 

qemu/target-i386/helper2.c void cpu_reset(CPUX86State *env) · line 184

Quoted 10 lines
qemu/target-i386/helper2.c +3 -3
     cpu_x86_load_seg_cache(env, R_CS, 0xf000, 0xffff0000, 0xffff, 0); 
-    cpu_x86_load_seg_cache(env, R_DS, 0, 0, 0xffff, 0);
-    cpu_x86_load_seg_cache(env, R_ES, 0, 0, 0xffff, 0);
-    cpu_x86_load_seg_cache(env, R_SS, 0, 0, 0xffff, 0);
+    cpu_x86_load_seg_cache(env, R_DS, 0, 0, 0xffff, flags);
+    cpu_x86_load_seg_cache(env, R_ES, 0, 0, 0xffff, flags);
+    cpu_x86_load_seg_cache(env, R_SS, 0, 0, 0xffff, flags);
     cpu_x86_load_seg_cache(env, R_FS, 0, 0, 0xffff, 0);
     cpu_x86_load_seg_cache(env, R_GS, 0, 0, 0xffff, 0);
 

This changes core qemu code, which may make merging our code back to qemu more difficult. It also means we need to test with -no-kvm.

Quoted 6 lines

Can you try making load_regs() in qemu-kvm.c detect the case of flags == 0 (perhaps only if cr0.pg == 0) and changing it only then? It should have the same effect.

Hello Avi!

I tried this patch yesterday whilst installing Windows 2003 with kvm svn
and when the installer requested a reboot I just got this in the console:

unhandled vm exit: 0x80000021
rax 0000000000000000 rbx 0000000000000000 rcx 0000000000000000 rdx 0000000000000600
rsi 0000000000000000 rdi 0000000000000000 rsp 0000000000000000 rbp 0000000000000000
r8 0d7905c600010000 r9 b7f506e0b7f4fff4 r10 bf9d7c3008049138 r11 b7f50898b7f42e42
r12 00000001b7bbf020 r13 0000000000000001 r14 000000920804a0a2 r15 0812335c00000000
rip 000000000000fff0 rflags 00023002

My host is a Fujitsu-Siemens S7110 with Intel Core 2 Duo with Ubuntu Feisty kernel 2.6.20 and the svn revision was 4368.

Daniel Hecken

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier. Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642

#5 lore raw
Quoted 14 lines · Daniel Hecken

I tried this patch yesterday whilst installing Windows 2003 with kvm svn
and when the installer requested a reboot I just got this in the console:

unhandled vm exit: 0x80000021
rax 0000000000000000 rbx 0000000000000000 rcx 0000000000000000 rdx 0000000000000600
rsi 0000000000000000 rdi 0000000000000000 rsp 0000000000000000 rbp 0000000000000000
r8 0d7905c600010000 r9 b7f506e0b7f4fff4 r10 bf9d7c3008049138 r11 b7f50898b7f42e42
r12 00000001b7bbf020 r13 0000000000000001 r14 000000920804a0a2 r15 0812335c00000000
rip 000000000000fff0 rflags 00023002

Quoted 3 lines

My host is a Fujitsu-Siemens S7110 with Intel Core 2 Duo with Ubuntu Feisty kernel 2.6.20 and the svn revision was 4368.

You're running on an Intel host, and it looks like the patch only fixes things for amd (I plan to apply it as it doesn't cause any regressions, though).

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


-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier.
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
#6 lore raw
In reply to a message not in the archive
Quoted 36 lines · Avi Kivity
Quoted 21 lines · Joerg Roedel

From: Markus Rechberger <[email protected]>
From: Joerg Roedel <[email protected]>

This patch fixes the initialization of the segment registers which solves the triple fault and keyboard controller reset problems in kvm/qemu guests as well as the slow grub menu interaction. The patch should also work on Intel VMX now.
diff -upr kvm-12/qemu/target-i386/helper2.c
kvm-12-reboot-fixed/qemu/target-i386/helper2.c

qemu/target-i386/helper2.c +3 -0
@@ -151,6 +151,9 @@ CPUX86State *cpu_x86_init(void)
 void cpu_reset(CPUX86State *env)
 {
    int i;
+    unsigned int flags = DESC_P_MASK |
+                         DESC_S_MASK |
+                         (2 << DESC_TYPE_SHIFT);
     memset(env, 0, offsetof(CPUX86State, breakpoints));
@@ -173,9 +176,9 @@ void cpu_reset(CPUX86State *env)
    env->tr.flags = DESC_P_MASK;

qemu/target-i386/helper2.c void cpu_reset(CPUX86State *env) · line 184

Quoted 10 lines
qemu/target-i386/helper2.c +3 -2
         cpu_x86_load_seg_cache(env, R_CS, 0xf000, 0xffff0000, 0xffff, 0); -    
 cpu_x86_load_seg_cache(env, R_DS, 0, 0, 0xffff, 0);
-    cpu_x86_load_seg_cache(env, R_ES, 0, 0, 0xffff, 0);
-    cpu_x86_load_seg_cache(env, R_SS, 0, 0, 0xffff, 0);
+    cpu_x86_load_seg_cache(env, R_DS, 0, 0, 0xffff, flags);
+    cpu_x86_load_seg_cache(env, R_ES, 0, 0, 0xffff, flags);
+    cpu_x86_load_seg_cache(env, R_SS, 0, 0, 0xffff, flags);
    cpu_x86_load_seg_cache(env, R_FS, 0, 0, 0xffff, 0);
    cpu_x86_load_seg_cache(env, R_GS, 0, 0, 0xffff, 0);
 

This changes core qemu code, which may make merging our code back to qemu more difficult. It also means we need to test with -no-kvm.

Quoted 2 lines

Can you try making load_regs() in qemu-kvm.c detect the case of flags == 0 (perhaps only if cr0.pg == 0) and changing it only then? It should have the same effect.

I tested the patch with -no-kvm too (with Linux guest) and it worked without problems. In my opinion it is cleaner to do it cpu_reset but YMMV. Anyway, here is a patch that implements this fixup in load_regs. You can choice which patch to apply :)

Joerg

Signature · 3 lines
Joerg Roedel
Operating System Research Center
AMD Saxony LLC & Co. KG
kernel/vmx.c +3 -0
diff -upr kvm-12/kernel/vmx.c kvm-12-reboot-fixed/kernel/vmx.c
@@ -781,6 +781,9 @@ static void vmx_set_cr0(struct kvm_vcpu 
  */
 static void vmx_set_cr0_no_modeswitch(struct kvm_vcpu *vcpu, unsigned long cr0)
 {
+	if (!vcpu->rmode.active && !(cr0 & CR0_PE_MASK))
+		enter_rmode(vcpu);
+	
 	vcpu->rmode.active = ((cr0 & CR0_PE_MASK) == 0);
 	update_exception_bitmap(vcpu);
 	vmcs_writel(CR0_READ_SHADOW, cr0);
qemu/qemu-kvm.c +22 -0
diff -upr kvm-12/qemu/qemu-kvm.c kvm-12-reboot-fixed/qemu/qemu-kvm.c
@@ -124,6 +124,15 @@ static void get_seg(SegmentCache *lhs, c
 	| (rhs->avl * DESC_AVL_MASK);
 }
 
+/* the reset values of qemu are not compatible to SVM
+ * this function is used to fix the segment descriptor values */
+static void fix_realmode_dataseg(struct kvm_segment *seg)
+{
+	seg->type = 0x02;
+	seg->present = 1;
+	seg->s = 1;
+}
+
 static void load_regs(CPUState *env)
 {
     struct kvm_regs regs;
@@ -182,6 +191,12 @@ static void load_regs(CPUState *env)
 			(sregs.cs.selector & 3);
 		sregs.ss.dpl = sregs.ss.selector & 3;
 	    }
+
+	    if (!(env->cr[0] & CR0_PG_MASK)) {
+		    fix_realmode_dataseg(&sregs.ds);
+		    fix_realmode_dataseg(&sregs.es);
+		    fix_realmode_dataseg(&sregs.ss);
+	    }
     }
 
     set_seg(&sregs.tr, &env->tr);
@@ -582,6 +597,12 @@ static int kvm_halt(void *opaque, int vc
 
     return 1;
 }
+
+static int kvm_shutdown(void *opaque, int vcpu)
+{
+    qemu_system_reset_request();
+    return 1;
+}
  
 static struct kvm_callbacks qemu_kvm_ops = {
     .cpuid = kvm_cpuid,
@@ -601,6 +622,7 @@ static struct kvm_callbacks qemu_kvm_ops
     .writel = kvm_writel,
     .writeq = kvm_writeq,
     .halt  = kvm_halt,
+    .shutdown = kvm_shutdown,
     .io_window = kvm_io_window,
     .try_push_interrupts = try_push_interrupts,
     .post_kvm_run = post_kvm_run,
qemu/vl.c +4 -0
diff -upr kvm-12/qemu/vl.c kvm-12-reboot-fixed/qemu/vl.c
@@ -5281,6 +5281,10 @@ int main_loop(void)
             if (reset_requested) {
                 reset_requested = 0;
                 qemu_system_reset();
+#ifdef USE_KVM
+		if (kvm_allowed)
+			kvm_load_registers(env);
+#endif
                 ret = EXCP_INTERRUPT;
             }
             if (powerdown_requested) {
user/kvmctl.c +8 -0
diff -upr kvm-12/user/kvmctl.c kvm-12-reboot-fixed/user/kvmctl.c
@@ -522,6 +522,11 @@ static int handle_halt(kvm_context_t kvm
 	return kvm->callbacks->halt(kvm->opaque, kvm_run->vcpu);
 }
 
+static int handle_shutdown(kvm_context_t kvm, struct kvm_run *kvm_run)
+{
+	return kvm->callbacks->shutdown(kvm->opaque, kvm_run->vcpu);
+}
+
 int try_push_interrupts(kvm_context_t kvm)
 {
 	return kvm->callbacks->try_push_interrupts(kvm->opaque);
@@ -594,6 +599,9 @@ again:
 			break;
 		case KVM_EXIT_IRQ_WINDOW_OPEN:
 			break;
+		case KVM_EXIT_SHUTDOWN:
+			r = handle_shutdown(kvm, &kvm_run);
+			break;
 		default:
 			fprintf(stderr, "unhandled vm exit: 0x%x\n", kvm_run.exit_reason);
 			kvm_show_regs(kvm, vcpu);
user/kvmctl.h +1 -0
diff -upr kvm-12/user/kvmctl.h kvm-12-reboot-fixed/user/kvmctl.h
@@ -59,6 +59,7 @@ struct kvm_callbacks {
 	 * on the host CPU.
 	 */
     int (*halt)(void *opaque, int vcpu);
+    int (*shutdown)(void *opaque, int vcpu);
     int (*io_window)(void *opaque);
     int (*try_push_interrupts)(void *opaque);
     void (*post_kvm_run)(void *opaque, struct kvm_run *kvm_run);

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier. Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642

_______________________________________________
kvm-devel mailing list [email protected]
https://lists.sourceforge.net/lists/listinfo/kvm-devel

A message that is not in the archive (sent off-list or not yet received): [email protected]

Applied
#8 lore raw
Quoted 6 lines · Joerg Roedel

I tested the patch with -no-kvm too (with Linux guest) and it worked without problems. In my opinion it is cleaner to do it cpu_reset but YMMV. Anyway, here is a patch that implements this fixup in load_regs. You can choice which patch to apply :)

Applied, thanks.

[I agree that #2 is cleaner, but I want to keep the merge to qemu simple]

Signature · 8 lines
Do not meddle in the internals of kernels, for they are subtle and quick to panic.


-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier.
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642