Skip to contentSkip to conversation

s390: clear registers, psw and prefix at vcpu reset

PATCH In review Jens Freimann 2 participants 4 messages Nov 27, 2012

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 s390: clear registers, psw and prefix at vcpu reset

When resetting vcpus on s390/kvm we have to clear registers, psw and prefix as described in the z/Architecture PoP, otherwise a reboot won't work. IPL PSW and prefix are set later on by the s390-ipl device reset code.

---

changes v1->v2:
- moved cpu reset code from kvm.c to cpu.c
- only kvm initial_reset ioctl remains in kvm.c
- registered reset handler for s390 cpu reset, like x86 does it

 target-s390x/cpu.c | 29 +++++++++++++++++++++++++++--
 target-s390x/kvm.c |  9 ++++++++-
 2 files changed, 35 insertions(+), 3 deletions(-)
target-s390x/cpu.c +27 -2
index 619b202..f74d6f6 100644
@@ -24,10 +24,21 @@
 #include "qemu-common.h"
 #include "qemu-timer.h"
 
+#ifndef CONFIG_USER_ONLY
+#include "hw/s390x/sclp.h"
+
+/* TODO: remove me, when reset over QOM tree is implemented */
+static void s390_cpu_machine_reset_cb(void *opaque)
+{
+    S390CPU *cpu = opaque;
+    cpu_reset(CPU(cpu));
+}
+#endif
 
 /* CPUClass::reset() */
 static void s390_cpu_reset(CPUState *s)
 {
+    int i;
     S390CPU *cpu = S390_CPU(s);
     S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
     CPUS390XState *env = &cpu->env;
@@ -40,9 +51,22 @@ static void s390_cpu_reset(CPUState *s)
     scc->parent_reset(s);
 
     memset(env, 0, offsetof(CPUS390XState, breakpoints));
-    /* FIXME: reset vector? */
+
+    env->halted = 1;
+    env->exception_index = EXCP_HLT;
+    for (i = 0; i < 16; i++) {
+        env->regs[i] = 0;
+        env->aregs[i] = 0;
+        env->cregs[i] = 0;
+        env->fregs[i].ll = 0;
+    }
+    /* architectured initial values for CR 0 and 14 */
+    env->cregs[0] = 0xE0UL;
+    env->cregs[14] = 0xC2000000UL;
+    env->psw.mask = 0;
+    env->psw.addr = 0;
+    env->psa = 0;
     tlb_flush(env, 1);
-    s390_add_running_cpu(env);
 }
 
 static void s390_cpu_initfn(Object *obj)
@@ -56,6 +80,7 @@ static void s390_cpu_initfn(Object *obj)
 
     cpu_exec_init(env);
 #if !defined(CONFIG_USER_ONLY)
+    qemu_register_reset(s390_cpu_machine_reset_cb, cpu);
     qemu_get_timedate(&tm, 0);
     env->tod_offset = TOD_UNIX_EPOCH +
                       (time2tod(mktimegm(&tm)) * 1000000000ULL);
target-s390x/kvm.c +8 -1
index 94de764..d26555f 100644
@@ -85,7 +85,14 @@ int kvm_arch_init_vcpu(CPUS390XState *env)
 
 void kvm_arch_reset_vcpu(CPUS390XState *env)
 {
-    /* FIXME: add code to reset vcpu. */
+    /* The initial reset call is needed here to reset in-kernel
+     * vcpu data that we can't access directly from QEMU
+     * (i.e. with older kernels which don't support sync_regs/ONE_REG).
+     * Before this ioctl cpu_synchronize_state() is called in common kvm
+     * code (kvm-all) */
+    if (kvm_vcpu_ioctl(env, KVM_S390_INITIAL_RESET, NULL)) {
+        perror("Can't reset vcpu\n");
+    }
 }
 
 int kvm_arch_put_registers(CPUS390XState *env, int level)
Signature · 1 line
1.7.12.4
#1 lore raw
Quoted 44 lines · Jens Freimann

When resetting vcpus on s390/kvm we have to clear registers, psw and prefix as described in the z/Architecture PoP, otherwise a reboot won't work. IPL PSW and prefix are set later on by the s390-ipl device reset code.

---

changes v1->v2:
- moved cpu reset code from kvm.c to cpu.c
- only kvm initial_reset ioctl remains in kvm.c
- registered reset handler for s390 cpu reset, like x86 does it

target-s390x/cpu.c | 29 +++++++++++++++++++++++++++-- target-s390x/kvm.c | 9 ++++++++-

2 files changed, 35 insertions(+), 3 deletions(-)
target-s390x/cpu.c +11 -0
index 619b202..f74d6f6 100644
@@ -24,10 +24,21 @@
 #include "qemu-common.h"
 #include "qemu-timer.h"
 
+#ifndef CONFIG_USER_ONLY
+#include "hw/s390x/sclp.h"
+
+/* TODO: remove me, when reset over QOM tree is implemented */
+static void s390_cpu_machine_reset_cb(void *opaque)
+{
+    S390CPU *cpu = opaque;
+    cpu_reset(CPU(cpu));
+}
+#endif
 
 /* CPUClass::reset() */
 static void s390_cpu_reset(CPUState *s)
 {
+    int i;
    S390CPU *cpu = S390_CPU(s);
    S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
    CPUS390XState *env = &cpu->env;

target-s390x/cpu.c static void s390_cpu_reset(CPUState *s) · line 55

Quoted 6 lines
target-s390x/cpu.c +2 -1
@@ -40,9 +51,22 @@ static void s390_cpu_reset(CPUState *s)
    scc->parent_reset(s);
 
    memset(env, 0, offsetof(CPUS390XState, breakpoints));
-    /* FIXME: reset vector? */
+
+    env->halted = 1;

Every cpu would start in halted state? So how does the primary one get rolling?

target-s390x/cpu.c static void s390_cpu_reset(CPUState *s) · line 62

Quoted 7 lines
target-s390x/cpu.c +7 -0
+    env->exception_index = EXCP_HLT;
+    for (i = 0; i < 16; i++) {
+        env->regs[i] = 0;
+        env->aregs[i] = 0;
+        env->cregs[i] = 0;
+        env->fregs[i].ll = 0;
+    }

Please make this more self-adjusting. For example using memset(sizeof));. You could also make the clear implicit by ensuring the registers are in the cpu struct before breakpoints. But explicit tends to be more readable ;).

target-s390x/cpu.c static void s390_cpu_reset(CPUState *s) · line 45

Quoted 8 lines
target-s390x/cpu.c +6 -1
+    /* architectured initial values for CR 0 and 14 */
+    env->cregs[0] = 0xE0UL;
+    env->cregs[14] = 0xC2000000UL;
+    env->psw.mask = 0;
+    env->psw.addr = 0;
+    env->psa = 0;
    tlb_flush(env, 1);
-    s390_add_running_cpu(env);

Why can we remove this one?

Alex

Quoted 34 lines
target-s390x/cpu.c +1 -0
 }
 
 static void s390_cpu_initfn(Object *obj)
@@ -56,6 +80,7 @@ static void s390_cpu_initfn(Object *obj)
 
    cpu_exec_init(env);
 #if !defined(CONFIG_USER_ONLY)
+    qemu_register_reset(s390_cpu_machine_reset_cb, cpu);
    qemu_get_timedate(&tm, 0);
    env->tod_offset = TOD_UNIX_EPOCH +
                      (time2tod(mktimegm(&tm)) * 1000000000ULL);
target-s390x/kvm.c +8 -1
index 94de764..d26555f 100644
@@ -85,7 +85,14 @@ int kvm_arch_init_vcpu(CPUS390XState *env)
 
 void kvm_arch_reset_vcpu(CPUS390XState *env)
 {
-    /* FIXME: add code to reset vcpu. */
+    /* The initial reset call is needed here to reset in-kernel
+     * vcpu data that we can't access directly from QEMU
+     * (i.e. with older kernels which don't support sync_regs/ONE_REG).
+     * Before this ioctl cpu_synchronize_state() is called in common kvm
+     * code (kvm-all) */
+    if (kvm_vcpu_ioctl(env, KVM_S390_INITIAL_RESET, NULL)) {
+        perror("Can't reset vcpu\n");
+    }
 }
 
 int kvm_arch_put_registers(CPUS390XState *env, int level)
Signature · 1 line
1.7.12.4
#2 lore raw
Quoted 50 lines · Alexander Graf
Quoted 46 lines · Jens Freimann

When resetting vcpus on s390/kvm we have to clear registers, psw and prefix as described in the z/Architecture PoP, otherwise a reboot won't work. IPL PSW and prefix are set later on by the s390-ipl device reset code.

---

changes v1->v2:
- moved cpu reset code from kvm.c to cpu.c
- only kvm initial_reset ioctl remains in kvm.c
- registered reset handler for s390 cpu reset, like x86 does it

target-s390x/cpu.c | 29 +++++++++++++++++++++++++++-- target-s390x/kvm.c | 9 ++++++++-

2 files changed, 35 insertions(+), 3 deletions(-)
target-s390x/cpu.c +11 -0
index 619b202..f74d6f6 100644
@@ -24,10 +24,21 @@
 #include "qemu-common.h"
 #include "qemu-timer.h"
 
+#ifndef CONFIG_USER_ONLY
+#include "hw/s390x/sclp.h"
+
+/* TODO: remove me, when reset over QOM tree is implemented */
+static void s390_cpu_machine_reset_cb(void *opaque)
+{
+    S390CPU *cpu = opaque;
+    cpu_reset(CPU(cpu));
+}
+#endif
 
 /* CPUClass::reset() */
 static void s390_cpu_reset(CPUState *s)
 {
+    int i;
    S390CPU *cpu = S390_CPU(s);
    S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
    CPUS390XState *env = &cpu->env;
@@ -40,9 +51,22 @@ static void s390_cpu_reset(CPUState *s)
    scc->parent_reset(s);
 
Quoted 6 lines

target-s390x/cpu.c static void s390_cpu_reset(CPUState *s) · line 55

Quoted 4 lines
target-s390x/cpu.c +2 -1
    memset(env, 0, offsetof(CPUS390XState, breakpoints));
-    /* FIXME: reset vector? */
+
+    env->halted = 1;

Every cpu would start in halted state? So how does the primary one get rolling?

The first cpu is set to not-halted by the ipl device reset code.

Quoted 10 lines

target-s390x/cpu.c static void s390_cpu_reset(CPUState *s) · line 62

Quoted 8 lines
target-s390x/cpu.c +7 -0
+    env->exception_index = EXCP_HLT;
+    for (i = 0; i < 16; i++) {
+        env->regs[i] = 0;
+        env->aregs[i] = 0;
+        env->cregs[i] = 0;
+        env->fregs[i].ll = 0;
+    }

Please make this more self-adjusting. For example using memset(sizeof));. You could also make the clear implicit by ensuring the registers are in the cpu struct before breakpoints. But explicit tends to be more readable ;).

Ok

Quoted 10 lines

target-s390x/cpu.c static void s390_cpu_reset(CPUState *s) · line 45

Quoted 8 lines
target-s390x/cpu.c +6 -1
+    /* architectured initial values for CR 0 and 14 */
+    env->cregs[0] = 0xE0UL;
+    env->cregs[14] = 0xC2000000UL;
+    env->psw.mask = 0;
+    env->psw.addr = 0;
+    env->psa = 0;
    tlb_flush(env, 1);
-    s390_add_running_cpu(env);

Why can we remove this one?

Good point. I took a closer look and found that we add an additional cpu to the counter every time we reboot. Will fix this and send a new version.

Jens

Quoted 38 lines

Alex

Quoted 34 lines
target-s390x/cpu.c +1 -0
 }
 
 static void s390_cpu_initfn(Object *obj)
@@ -56,6 +80,7 @@ static void s390_cpu_initfn(Object *obj)
 
    cpu_exec_init(env);
 #if !defined(CONFIG_USER_ONLY)
+    qemu_register_reset(s390_cpu_machine_reset_cb, cpu);
    qemu_get_timedate(&tm, 0);
    env->tod_offset = TOD_UNIX_EPOCH +
                      (time2tod(mktimegm(&tm)) * 1000000000ULL);
target-s390x/kvm.c +8 -1
index 94de764..d26555f 100644
@@ -85,7 +85,14 @@ int kvm_arch_init_vcpu(CPUS390XState *env)
 
 void kvm_arch_reset_vcpu(CPUS390XState *env)
 {
-    /* FIXME: add code to reset vcpu. */
+    /* The initial reset call is needed here to reset in-kernel
+     * vcpu data that we can't access directly from QEMU
+     * (i.e. with older kernels which don't support sync_regs/ONE_REG).
+     * Before this ioctl cpu_synchronize_state() is called in common kvm
+     * code (kvm-all) */
+    if (kvm_vcpu_ioctl(env, KVM_S390_INITIAL_RESET, NULL)) {
+        perror("Can't reset vcpu\n");
+    }
 }
 
 int kvm_arch_put_registers(CPUS390XState *env, int level)
Signature · 1 line
1.7.12.4
Signature · 19 lines
Mit freundlichen Grüßen / Kind regards 

Jens Freimann 

-- 
IBM Linux Technology Center / Boeblingen lab
IBM Systems &Technology Group, Systems Software Development
-------------------------------------------------------------
IBM Deutschland
Schoenaicher Str 220
71032 Boeblingen
Phone: +49-7031-16 x1122
E-Mail: [email protected]
-------------------------------------------------------------
IBM Deutschland Research & Development GmbH
Vorsitzende des Aufsichtsrats: Martina Koederitz
Geschäftsführung: Dirk Wittkopp
Sitz der Gesellschaft: Böblingen
Registergericht: Amtsgericht Stuttgart, HRB 243294
#3 lore raw
Quoted 50 lines · Jens Freimann
Quoted 49 lines · Alexander Graf
Quoted 46 lines · Jens Freimann

When resetting vcpus on s390/kvm we have to clear registers, psw and prefix as described in the z/Architecture PoP, otherwise a reboot won't work. IPL PSW and prefix are set later on by the s390-ipl device reset code.

---

changes v1->v2:
- moved cpu reset code from kvm.c to cpu.c
- only kvm initial_reset ioctl remains in kvm.c
- registered reset handler for s390 cpu reset, like x86 does it

target-s390x/cpu.c | 29 +++++++++++++++++++++++++++-- target-s390x/kvm.c | 9 ++++++++-

2 files changed, 35 insertions(+), 3 deletions(-)
target-s390x/cpu.c +11 -0
index 619b202..f74d6f6 100644
@@ -24,10 +24,21 @@
 #include "qemu-common.h"
 #include "qemu-timer.h"
 
+#ifndef CONFIG_USER_ONLY
+#include "hw/s390x/sclp.h"
+
+/* TODO: remove me, when reset over QOM tree is implemented */
+static void s390_cpu_machine_reset_cb(void *opaque)
+{
+    S390CPU *cpu = opaque;
+    cpu_reset(CPU(cpu));
+}
+#endif
 
 /* CPUClass::reset() */
 static void s390_cpu_reset(CPUState *s)
 {
+    int i;
     S390CPU *cpu = S390_CPU(s);
     S390CPUClass *scc = S390_CPU_GET_CLASS(cpu);
     CPUS390XState *env =&cpu->env;
@@ -40,9 +51,22 @@ static void s390_cpu_reset(CPUState *s)
     scc->parent_reset(s);
 
Quoted 6 lines
Quoted 5 lines

target-s390x/cpu.c static void s390_cpu_reset(CPUState *s) · line 55

Quoted 4 lines
target-s390x/cpu.c +2 -1
     memset(env, 0, offsetof(CPUS390XState, breakpoints));
-    /* FIXME: reset vector? */
+
+    env->halted = 1;

Every cpu would start in halted state? So how does the primary one get rolling?

The first cpu is set to not-halted by the ipl device reset code.

Please document this here.

Quoted 18 lines
Quoted 8 lines

target-s390x/cpu.c static void s390_cpu_reset(CPUState *s) · line 62

Quoted 7 lines
target-s390x/cpu.c +7 -0
+    env->exception_index = EXCP_HLT;
+    for (i = 0; i<  16; i++) {
+        env->regs[i] = 0;
+        env->aregs[i] = 0;
+        env->cregs[i] = 0;
+        env->fregs[i].ll = 0;
+    }

Please make this more self-adjusting. For example using memset(sizeof));. You could also make the clear implicit by ensuring the registers are in the cpu struct before breakpoints. But explicit tends to be more readable ;).

Ok

Quoted 8 lines
Quoted 8 lines
target-s390x/cpu.c +6 -1
+    /* architectured initial values for CR 0 and 14 */
+    env->cregs[0] = 0xE0UL;
+    env->cregs[14] = 0xC2000000UL;
+    env->psw.mask = 0;
+    env->psw.addr = 0;
+    env->psa = 0;
     tlb_flush(env, 1);
-    s390_add_running_cpu(env);
Quoted 4 lines
Quoted 1 line

Why can we remove this one?

Good point. I took a closer look and found that we add an additional cpu to the counter every time we reboot. Will fix this and send a new version.

Yeah, if anything this should be a separate patch :).

Alex