Skip to contentSkip to conversation

arm: use a per-VCPU stack

PATCH Superseded Ian Campbell 2 participants 7 messages Feb 15, 2012 – Feb 22, 2012

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 arm: use a per-VCPU stack

---
 xen/arch/arm/asm-offsets.c    |    4 ++
 xen/arch/arm/domain.c         |  115 ++++++++++++----------------------------
 xen/arch/arm/domain_build.c   |    3 +-
 xen/arch/arm/entry.S          |   16 ++++++
 xen/arch/arm/setup.c          |    3 +-
 xen/include/asm-arm/current.h |   41 ++++++++------
 xen/include/asm-arm/domain.h  |   21 +++++++-
 xen/include/asm-arm/regs.h    |    4 +-
 xen/include/asm-arm/system.h  |    2 +
 9 files changed, 104 insertions(+), 105 deletions(-)
xen/arch/arm/asm-offsets.c +4 -0
index ee5d5d4..cc1a72a 100644
@@ -7,6 +7,7 @@
 
 #include <xen/config.h>
 #include <xen/types.h>
+#include <xen/sched.h>
 #include <public/xen.h>
 #include <asm/current.h>
 
@@ -65,7 +66,10 @@ void __dummy__(void)
    BLANK();
 
    DEFINE(CPUINFO_sizeof, sizeof(struct cpu_info));
+
+   OFFSET(VCPU_arch_saved_context, struct vcpu, arch.saved_context);
 }
+
 /*
  * Local variables:
  * mode: C
xen/arch/arm/domain.c +35 -80

174 lines not on this page · raw message

xen/arch/arm/domain_build.c +2 -1
index cbbc0b9..9240209 100644
@@ -5,6 +5,7 @@
 #include <xen/domain_page.h>
 #include <xen/sched.h>
 #include <asm/irq.h>
+#include <asm/regs.h>
 
 #include "gic.h"
 #include "kernel.h"
@@ -71,7 +72,7 @@ int construct_dom0(struct domain *d)
     int rc;
 
     struct vcpu *v = d->vcpu[0];
-    struct cpu_user_regs *regs = &v->arch.user_regs;
+    struct cpu_user_regs *regs = &v->arch.cpu_info->guest_cpu_user_regs;
 
     /* Sanity! */
     BUG_ON(d->domain_id != 0);
xen/arch/arm/entry.S +16 -0
index 16a8f36..0b9cce5 100644
@@ -105,3 +105,19 @@ ENTRY(return_to_hypervisor)
 	pop {r0-r12}
 	add sp, #(UREGS_R8_fiq - UREGS_sp); /* SP, LR, SPSR, PC */
 	eret
+
+/*
+ * struct vcpu *__context_switch(struct vcpu *prev, struct vcpu *next)
+ *
+ * r0 - prev
+ * r1 - next
+ *
+ * Returns prev in r0
+ */
+ENTRY(__context_switch)
+	add	ip, r0, #VCPU_arch_saved_context
+	stmia	ip!, {r4 - sl, fp, sp, lr}	/* Save register state */
+
+	add	r4, r1, #VCPU_arch_saved_context
+	ldmia	r4, {r4 - sl, fp, sp, pc}	/* Load registers and return */
+
xen/arch/arm/setup.c +1 -2
index 4c1d89c..55d3df0 100644
@@ -42,7 +42,7 @@
 static unsigned int __initdata max_cpus = NR_CPUS;
 
 /* Xen stack for bringing up the first CPU. */
-unsigned char init_stack[STACK_SIZE] __attribute__((__aligned__(STACK_SIZE)));
+unsigned char __initdata init_stack[STACK_SIZE] __attribute__((__aligned__(STACK_SIZE)));
 
 extern char __init_begin[], __init_end[], __bss_start[];
 
@@ -61,7 +61,6 @@ static void __init init_idle_domain(void)
 {
         scheduler_init();
         set_current(idle_vcpu[0]);
-        this_cpu(curr_vcpu) = current;
         /* TODO: setup_idle_pagetable(); */
 }
 
xen/include/asm-arm/current.h +23 -18

70 lines not on this page · raw message

xen/include/asm-arm/domain.h +20 -1
index 3372d14..c1afd19 100644
@@ -47,7 +47,26 @@ struct arch_domain
 
 struct arch_vcpu
 {
-    struct cpu_user_regs user_regs;
+    struct {
+        uint32_t    r4;
+        uint32_t    r5;
+        uint32_t    r6;
+        uint32_t    r7;
+        uint32_t    r8;
+        uint32_t    r9;
+        uint32_t    sl;
+        uint32_t    fp;
+        uint32_t    sp;
+        uint32_t    pc;
+    } saved_context;
+
+    void *stack;
+
+    /*
+     * Points into ->stack, more convenient than doing pointer arith
+     * all the time.
+     */
+    struct cpu_info *cpu_info;
 
     uint32_t sctlr;
     uint32_t ttbr0, ttbr1, ttbcr;
xen/include/asm-arm/regs.h +1 -3
index ee095bf..54f6ed8 100644
@@ -28,9 +28,7 @@
     (diff == 0);                                                              \
 })
 
-#define return_reg(v) ((v)->arch.user_regs.r0)
-
-#define CTXT_SWITCH_STACK_BYTES (sizeof(struct cpu_user_regs))
+#define return_reg(v) ((v)->arch.cpu_info->guest_cpu_user_regs.r0)
 
 #endif /* __ARM_REGS_H__ */
 /*
xen/include/asm-arm/system.h +2 -0
index 731d89f..7963ea5 100644
@@ -191,6 +191,8 @@ static inline int local_fiq_is_enabled(void)
     return !!(flags & PSR_FIQ_MASK);
 }
 
+extern struct vcpu *__context_switch(struct vcpu *prev, struct vcpu *next);
+
 #endif
 /*
  * Local variables:
Signature · 1 line
1.7.2.5
#1 lore raw

xen/include/asm-arm/current.h

Quoted 12 lines · Ian Campbell
xen/include/asm-arm/current.h +11 -0
+struct pcpu_info {
+    unsigned int processor_id;
+    struct vcpu *current_vcpu;
+};
+
+DECLARE_PER_CPU(struct pcpu_info, pcpu_info);
 
+static inline struct pcpu_info *get_pcpu_info(void)
+{
+    return &this_cpu(pcpu_info);
+}
+

I don't think it's worth declaring a struct and accessors for this; we should just have current_vcpu as an ordinary per-cpu variable.

Storing the CPU ID in the per-pcpu area only happens to work because per-cpu areas are a noop right now. I have a patch that re-enables them properly but for that we'll need a proper way of getting the CPU id. In the meantime I think it would be less confusing just to hard-code it as zero than to do this. :)

We could use the physical CPU ID register; I don't know whether it would be faster to stash the ID on the (per-vcpu) stack and update it during context switch.

Aside from that, this patch looks OK to me.

Cheers,

Tim.

#2 lore raw
Quoted 8 lines · Tim Deegan
Quoted 6 lines · Ian Campbell
xen/include/asm-arm/current.h +6 -0
+struct pcpu_info {
+    unsigned int processor_id;
+    struct vcpu *current_vcpu;
+};
+
+DECLARE_PER_CPU(struct pcpu_info, pcpu_info);
Quoted 8 lines

xen/include/asm-arm/current.h line 25

Quoted 5 lines
xen/include/asm-arm/current.h +5 -0
+static inline struct pcpu_info *get_pcpu_info(void)
+{
+    return &this_cpu(pcpu_info);
+}
+

I don't think it's worth declaring a struct and accessors for this; we should just have current_vcpu as an ordinary per-cpu variable.

That makes sense.

Quoted 3 lines

Storing the CPU ID in the per-pcpu area only happens to work because per-cpu areas are a noop right now. I have a patch that re-enables them properly but for that we'll need a proper way of getting the CPU id.

I had imagined that we would have per pVCPU page tables so the current CPU's per-pcpu area would always be at the same location. If that is not (going to be) the case then I'll stash it on the VCPU stack instead.

Thinking about it now playing tricks with the PTs does make it tricky on the rare occasions when you want to access another pCPUs per area.

Speaking of per-cpu areas -- I did notice a strange behaviour while debugging this. It seemed that a barrier() was not sufficient to keep the processor from caching the value of "current" in a register (i.e it would load into r6 before the barrier and use r6 after). I figured this was probably an unfortunate side effect of the current nobbled per-pcpu areas and would be fixed as part of your SMP bringup stuff.

Quoted 3 lines

We could use the physical CPU ID register; I don't know whether it would be faster to stash the ID on the (per-vcpu) stack and update it during context switch.

Does h/w CPU ID correspond to the s/w one in our circumstances? Might they be very sparse or something inconvenient like that?

I'd expect pulling things from registers to be faster in the normal case but in this specific scenario I'd imagine the base of the stack will be pretty cache hot since it has all the guest state in it etc which we've probably fairly recently pushed to or are about to pop from.

[...]

Quoted 1 line

Aside from that, this patch looks OK to me.

Thanks I'll repost next week with the fixes you suggest.

Ian.

#3 lore raw
Quoted 7 lines · Ian Campbell
Quoted 3 lines

Storing the CPU ID in the per-pcpu area only happens to work because per-cpu areas are a noop right now. I have a patch that re-enables them properly but for that we'll need a proper way of getting the CPU id.

I had imagined that we would have per pVCPU page tables so the current CPU's per-pcpu area would always be at the same location. If that is not (going to be) the case then I'll stash it on the VCPU stack instead.

Yes I'd thought that too but then when I came to implement it up...

Quoted 2 lines

Thinking about it now playing tricks with the PTs does make it tricky on the rare occasions when you want to access another pCPUs per area.

... I saw that and since I then had to use the normal relocation tricks I didn't bother with the local-var special case. Could still do it if it turns out to be a perf win (but w/out hardware to measure, I think I'll leave the optimizations alone for now).

Quoted 6 lines

Speaking of per-cpu areas -- I did notice a strange behaviour while debugging this. It seemed that a barrier() was not sufficient to keep the processor from caching the value of "current" in a register (i.e it would load into r6 before the barrier and use r6 after). I figured this was probably an unfortunate side effect of the current nobbled per-pcpu areas and would be fixed as part of your SMP bringup stuff.

Wierd. Must check that when I rebase the SMP patches.

Quoted 6 lines
Quoted 3 lines

We could use the physical CPU ID register; I don't know whether it would be faster to stash the ID on the (per-vcpu) stack and update it during context switch.

Does h/w CPU ID correspond to the s/w one in our circumstances? Might they be very sparse or something inconvenient like that?

It does on all the h/w we support :) but yes it could be sparse, encoding NUMA topology.

Quoted 4 lines

I'd expect pulling things from registers to be faster in the normal case but in this specific scenario I'd imagine the base of the stack will be pretty cache hot since it has all the guest state in it etc which we've probably fairly recently pushed to or are about to pop from.

Agreed.

Tim.

#4 lore raw

PATCH v2 1/1 arm: use a per-VCPU stack

We do not do any lazy state switching. Outside of context_switch() the current stack is always that of the VCPU which current returns.

---
 xen/arch/arm/asm-offsets.c    |    4 ++
 xen/arch/arm/domain.c         |  113 ++++++++++++----------------------------
 xen/arch/arm/domain_build.c   |    3 +-
 xen/arch/arm/entry.S          |   16 ++++++
 xen/arch/arm/setup.c          |    5 +-
 xen/include/asm-arm/current.h |   35 +++++++------
 xen/include/asm-arm/domain.h  |   21 +++++++-
 xen/include/asm-arm/regs.h    |    4 +-
 xen/include/asm-arm/system.h  |    2 +
 9 files changed, 101 insertions(+), 102 deletions(-)
xen/arch/arm/asm-offsets.c +4 -0
index ee5d5d4..cc1a72a 100644
@@ -7,6 +7,7 @@
 
 #include <xen/config.h>
 #include <xen/types.h>
+#include <xen/sched.h>
 #include <public/xen.h>
 #include <asm/current.h>
 
@@ -65,7 +66,10 @@ void __dummy__(void)
    BLANK();
 
    DEFINE(CPUINFO_sizeof, sizeof(struct cpu_info));
+
+   OFFSET(VCPU_arch_saved_context, struct vcpu, arch.saved_context);
 }
+
 /*
  * Local variables:
  * mode: C
xen/arch/arm/domain.c +34 -79

166 lines not on this page · raw message

xen/arch/arm/domain_build.c +2 -1
index cbbc0b9..9240209 100644
@@ -5,6 +5,7 @@
 #include <xen/domain_page.h>
 #include <xen/sched.h>
 #include <asm/irq.h>
+#include <asm/regs.h>
 
 #include "gic.h"
 #include "kernel.h"
@@ -71,7 +72,7 @@ int construct_dom0(struct domain *d)
     int rc;
 
     struct vcpu *v = d->vcpu[0];
-    struct cpu_user_regs *regs = &v->arch.user_regs;
+    struct cpu_user_regs *regs = &v->arch.cpu_info->guest_cpu_user_regs;
 
     /* Sanity! */
     BUG_ON(d->domain_id != 0);
xen/arch/arm/entry.S +16 -0
index 16a8f36..0b9cce5 100644
@@ -105,3 +105,19 @@ ENTRY(return_to_hypervisor)
 	pop {r0-r12}
 	add sp, #(UREGS_R8_fiq - UREGS_sp); /* SP, LR, SPSR, PC */
 	eret
+
+/*
+ * struct vcpu *__context_switch(struct vcpu *prev, struct vcpu *next)
+ *
+ * r0 - prev
+ * r1 - next
+ *
+ * Returns prev in r0
+ */
+ENTRY(__context_switch)
+	add	ip, r0, #VCPU_arch_saved_context
+	stmia	ip!, {r4 - sl, fp, sp, lr}	/* Save register state */
+
+	add	r4, r1, #VCPU_arch_saved_context
+	ldmia	r4, {r4 - sl, fp, sp, pc}	/* Load registers and return */
+
xen/arch/arm/setup.c +2 -3
index 4c1d89ce..4c0244c 100644
@@ -42,7 +42,7 @@
 static unsigned int __initdata max_cpus = NR_CPUS;
 
 /* Xen stack for bringing up the first CPU. */
-unsigned char init_stack[STACK_SIZE] __attribute__((__aligned__(STACK_SIZE)));
+unsigned char __initdata init_stack[STACK_SIZE] __attribute__((__aligned__(STACK_SIZE)));
 
 extern char __init_begin[], __init_end[], __bss_start[];
 
@@ -61,7 +61,6 @@ static void __init init_idle_domain(void)
 {
         scheduler_init();
         set_current(idle_vcpu[0]);
-        this_cpu(curr_vcpu) = current;
         /* TODO: setup_idle_pagetable(); */
 }
 
@@ -175,7 +174,7 @@ void __init start_xen(unsigned long boot_phys_offset,
     console_init_preirq();
 #endif
 
-    set_current((struct vcpu *)0xfffff000); /* debug sanity */
+    __set_current((struct vcpu *)0xfffff000); /* debug sanity */
     idle_vcpu[0] = current;
     set_processor_id(0); /* needed early, for smp_processor_id() */
 
xen/include/asm-arm/current.h +20 -15
index 826efa5..72d4945 100644
@@ -5,49 +5,54 @@
 #include <xen/percpu.h>
 #include <public/xen.h>
 
+#include <asm/percpu.h>
+
 #ifndef __ASSEMBLY__
 
 struct vcpu;
 
+/*
+ * Which VCPU is "current" on this PCPU.
+ */
+DECLARE_PER_CPU(struct vcpu *, curr_vcpu);
+
 struct cpu_info {
     struct cpu_user_regs guest_cpu_user_regs;
     unsigned long elr;
+    /* The following are valid iff this VCPU is current */
     unsigned int processor_id;
-    struct vcpu *current_vcpu;
     unsigned long per_cpu_offset;
+    unsigned int pad;
 };
 
 static inline struct cpu_info *get_cpu_info(void)
 {
-        register unsigned long sp asm ("sp");
-        return (struct cpu_info *)((sp & ~(STACK_SIZE - 1)) + STACK_SIZE - sizeof(struct cpu_info));
+    register unsigned long sp asm ("sp");
+    return (struct cpu_info *)((sp & ~(STACK_SIZE - 1)) + STACK_SIZE - sizeof(struct cpu_info));
 }
 
-#define get_current()         (get_cpu_info()->current_vcpu)
-#define set_current(vcpu)     (get_cpu_info()->current_vcpu = (vcpu))
-#define current               (get_current())
-
 #define get_processor_id()    (get_cpu_info()->processor_id)
 #define set_processor_id(id)  do {                                      \
     struct cpu_info *ci__ = get_cpu_info();                             \
     ci__->per_cpu_offset = __per_cpu_offset[ci__->processor_id = (id)]; \
 } while (0)
 
+#define get_current()         (this_cpu(curr_vcpu))
+#define __set_current(vcpu)   (this_cpu(curr_vcpu) = (vcpu))
+#define set_current(vcpu)     do {                                      \
+    vcpu->arch.cpu_info->processor_id = get_processor_id();             \
+    __set_current(vcpu);                                                \
+} while (0)
+#define current               (get_current())
+
 #define guest_cpu_user_regs() (&get_cpu_info()->guest_cpu_user_regs)
 
 #define reset_stack_and_jump(__fn)              \
     __asm__ __volatile__ (                      \
         "mov sp,%0; b "STR(__fn)      \
         : : "r" (guest_cpu_user_regs()) : "memory" )
-#endif
-
 
-/*
- * Which VCPU's state is currently running on each CPU?
- * This is not necesasrily the same as 'current' as a CPU may be
- * executing a lazy state switch.
- */
-DECLARE_PER_CPU(struct vcpu *, curr_vcpu);
+#endif
 
 #endif /* __ARM_CURRENT_H__ */
 /*
xen/include/asm-arm/domain.h +20 -1
index 3372d14..c1afd19 100644
@@ -47,7 +47,26 @@ struct arch_domain
 
 struct arch_vcpu
 {
-    struct cpu_user_regs user_regs;
+    struct {
+        uint32_t    r4;
+        uint32_t    r5;
+        uint32_t    r6;
+        uint32_t    r7;
+        uint32_t    r8;
+        uint32_t    r9;
+        uint32_t    sl;
+        uint32_t    fp;
+        uint32_t    sp;
+        uint32_t    pc;
+    } saved_context;
+
+    void *stack;
+
+    /*
+     * Points into ->stack, more convenient than doing pointer arith
+     * all the time.
+     */
+    struct cpu_info *cpu_info;
 
     uint32_t sctlr;
     uint32_t ttbr0, ttbr1, ttbcr;
xen/include/asm-arm/regs.h +1 -3
index ee095bf..54f6ed8 100644
@@ -28,9 +28,7 @@
     (diff == 0);                                                              \
 })
 
-#define return_reg(v) ((v)->arch.user_regs.r0)
-
-#define CTXT_SWITCH_STACK_BYTES (sizeof(struct cpu_user_regs))
+#define return_reg(v) ((v)->arch.cpu_info->guest_cpu_user_regs.r0)
 
 #endif /* __ARM_REGS_H__ */
 /*
xen/include/asm-arm/system.h +2 -0
index 731d89f..7963ea5 100644
@@ -191,6 +191,8 @@ static inline int local_fiq_is_enabled(void)
     return !!(flags & PSR_FIQ_MASK);
 }
 
+extern struct vcpu *__context_switch(struct vcpu *prev, struct vcpu *next);
+
 #endif
 /*
  * Local variables:
Signature · 1 line
1.7.2.5