Port the logic over from x86 and also drop CHECK_ASSUMPTION here.

The only slightly ugly detail: the PERCPU_SIZE_SHIFT define is now
duplicated in both asm/percpu.h instances because there is no good
generic header yet to hold it. Can be cleaned up later on.

Signed-off-by: Jan Kiszka <[email protected]>
---
 hypervisor/arch/arm/asm-defines.c        | 12 ++++++++++++
 hypervisor/arch/arm/entry.S              |  3 ++-
 hypervisor/arch/arm/include/asm/percpu.h | 25 +++++--------------------
 hypervisor/arch/arm/setup.c              |  2 +-
 4 files changed, 20 insertions(+), 22 deletions(-)

diff --git a/hypervisor/arch/arm/asm-defines.c 
b/hypervisor/arch/arm/asm-defines.c
index 15ff68d..c54ebf4 100644
--- a/hypervisor/arch/arm/asm-defines.c
+++ b/hypervisor/arch/arm/asm-defines.c
@@ -11,9 +11,21 @@
  */
 
 #include <jailhouse/gen-defines.h>
+#include <jailhouse/utils.h>
+#include <asm/percpu.h>
 
 void common(void);
 
 void common(void)
 {
+       OFFSET(PERCPU_LINUX_SP, per_cpu, linux_sp);
+       BLANK();
+
+       /* GCC evaluates constant expressions involving built-ins
+        * at compilation time, so this yields computed value.
+        */
+       DEFINE(PERCPU_STACK_END,
+              __builtin_offsetof(struct per_cpu, stack) + \
+              FIELD_SIZEOF(struct per_cpu, stack));
+       DEFINE(PERCPU_SIZE_SHIFT_ASM, PERCPU_SIZE_SHIFT);
 }
diff --git a/hypervisor/arch/arm/entry.S b/hypervisor/arch/arm/entry.S
index 6f9178c..430bb3e 100644
--- a/hypervisor/arch/arm/entry.S
+++ b/hypervisor/arch/arm/entry.S
@@ -10,6 +10,7 @@
  * the COPYING file in the top-level directory.
  */
 
+#include <asm/asm-defines.h>
 #include <asm/head.h>
 #include <asm/percpu.h>
 
@@ -22,7 +23,7 @@ arch_entry:
 
        ldr     r1, =__page_pool
        mov     r4, #1
-       lsl     r4, #PERCPU_SIZE_SHIFT
+       lsl     r4, #PERCPU_SIZE_SHIFT_ASM
        /*
         * percpu data = pool + cpuid * shift
         * TODO: handle aff1 and aff2
diff --git a/hypervisor/arch/arm/include/asm/percpu.h 
b/hypervisor/arch/arm/include/asm/percpu.h
index 3ab3a68..e6562da 100644
--- a/hypervisor/arch/arm/include/asm/percpu.h
+++ b/hypervisor/arch/arm/include/asm/percpu.h
@@ -18,11 +18,6 @@
 
 #define NUM_ENTRY_REGS                 13
 
-/* Keep in sync with struct per_cpu! */
-#define PERCPU_SIZE_SHIFT              13
-#define PERCPU_STACK_END               PAGE_SIZE
-#define PERCPU_LINUX_SP                        PERCPU_STACK_END
-
 #ifndef __ASSEMBLY__
 
 #include <jailhouse/cell.h>
@@ -30,10 +25,13 @@
 #include <asm/spinlock.h>
 #include <asm/sysregs.h>
 
+/* Round up sizeof(struct per_cpu) to the next power of two. */
+#define PERCPU_SIZE_SHIFT \
+       (BITS_PER_LONG - __builtin_clzl(sizeof(struct per_cpu) - 1))
+
 struct pending_irq;
 
 struct per_cpu {
-       /* Keep these two in sync with defines above! */
        u8 stack[PAGE_SIZE];
        unsigned long linux_sp;
        unsigned long linux_ret;
@@ -93,7 +91,7 @@ static inline struct per_cpu *per_cpu(unsigned int cpu)
 static inline struct registers *guest_regs(struct per_cpu *cpu_data)
 {
        /* Assumes that the trap handler is entered with an empty stack */
-       return (struct registers *)(cpu_data->stack + PERCPU_STACK_END
+       return (struct registers *)(cpu_data->stack + sizeof(cpu_data->stack)
                        - sizeof(struct registers));
 }
 
@@ -103,19 +101,6 @@ static inline unsigned int arm_cpu_phys2virt(unsigned int 
cpu_id)
 }
 
 unsigned int arm_cpu_virt2phys(struct cell *cell, unsigned int virt_id);
-
-/* Validate defines */
-#define CHECK_ASSUMPTION(assume)       ((void)sizeof(char[1 - 2*!(assume)]))
-
-static inline void __check_assumptions(void)
-{
-       struct per_cpu cpu_data;
-
-       CHECK_ASSUMPTION(sizeof(struct per_cpu) == (1 << PERCPU_SIZE_SHIFT));
-       CHECK_ASSUMPTION(sizeof(cpu_data.stack) == PERCPU_STACK_END);
-       CHECK_ASSUMPTION(__builtin_offsetof(struct per_cpu, linux_sp) ==
-                        PERCPU_LINUX_SP);
-}
 #endif /* !__ASSEMBLY__ */
 
 #endif /* !_JAILHOUSE_ASM_PERCPU_H */
diff --git a/hypervisor/arch/arm/setup.c b/hypervisor/arch/arm/setup.c
index ef6f9e0..f9f59d8 100644
--- a/hypervisor/arch/arm/setup.c
+++ b/hypervisor/arch/arm/setup.c
@@ -129,7 +129,7 @@ void __attribute__((noreturn)) arch_cpu_activate_vmm(struct 
per_cpu *cpu_data)
                 */
                "eret\n\t"
                :
-               : "r" (cpu_data->stack + PERCPU_STACK_END),
+               : "r" (cpu_data->stack + sizeof(cpu_data->stack)),
                  "r" (cpu_data->linux_reg));
 
        __builtin_unreachable();
-- 
2.1.4

-- 
You received this message because you are subscribed to the Google Groups 
"Jailhouse" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to