Introduce a new global variable, cpus_online. This variable counts
onlined CPUs. It is increased when the setup_cpu_stack macro is called.

This macro sets up the stack for one CPU by loading cpus_online,
incrementing it, and calculates the top of the stack pointer. This macro
returns a pointer to the IRQ stack in r0.

On startup, this is passed to inmate_main and optionally to gic_setup.

This is a preparation for the support of secondary CPUs.

Signed-off-by: Ralf Ramsauer <[email protected]>
---
 inmates/lib/arm-common/Makefile.lib |  2 +-
 inmates/lib/arm-common/psci.c       | 39 ++++++++++++++++++++++++++++
 inmates/lib/arm/header.S            | 52 ++++++++++++++++++++++++++++++++++++-
 inmates/lib/arm/include/mach.h      |  2 ++
 inmates/lib/arm/inmate.lds.S        |  3 +--
 5 files changed, 94 insertions(+), 4 deletions(-)
 create mode 100644 inmates/lib/arm-common/psci.c

diff --git a/inmates/lib/arm-common/Makefile.lib 
b/inmates/lib/arm-common/Makefile.lib
index 3fb84572..206fa60e 100644
--- a/inmates/lib/arm-common/Makefile.lib
+++ b/inmates/lib/arm-common/Makefile.lib
@@ -39,7 +39,7 @@
 GCOV_PROFILE := n
 
 OBJS-y := ../string.o ../cmdline.o
-OBJS-y += printk.o gic.o timer.o
+OBJS-y += printk.o gic.o timer.o psci.o
 OBJS-y += uart-jailhouse.o uart-pl011.o uart-8250.o uart-xuartps.o
 OBJS-y += gic-v2.o
 
diff --git a/inmates/lib/arm-common/psci.c b/inmates/lib/arm-common/psci.c
new file mode 100644
index 00000000..622200cc
--- /dev/null
+++ b/inmates/lib/arm-common/psci.c
@@ -0,0 +1,39 @@
+/*
+ * Jailhouse, a Linux-based partitioning hypervisor
+ *
+ * Copyright (c) OTH Regensburg, 2017
+ *
+ * Authors:
+ *  Ralf Ramsauer <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2.  See
+ * the COPYING file in the top-level directory.
+ *
+ * Alternatively, you can use or redistribute this file under the following
+ * BSD license:
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+volatile unsigned int cpus_online;
diff --git a/inmates/lib/arm/header.S b/inmates/lib/arm/header.S
index d0a8f219..13700f02 100644
--- a/inmates/lib/arm/header.S
+++ b/inmates/lib/arm/header.S
@@ -38,6 +38,7 @@
  * THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+#include <mach.h>
 #include <asm/sysregs.h>
 
        .arm
@@ -67,6 +68,52 @@ vectors:
        vector irq
        vector fiq
 
+/* Sets up the stack for a CPU. Clobbers r0-r2. Returns the location of the IRQ
+ * stack in r0.
+ *
+ * The stack for the n'th onlined CPU is calculated as follows:
+ * stack_top = stack_bottom + (n * stack_size * 2) + stack_size =
+ *           = stack_bottom + (2 * n + 1) * stack_size
+ *
+ * So the stack configuration looks as follows:
+ * +-----------+ <- stack_bottom
+ * |   Pri     |
+ * +-----------+ <- Primary CPU stack top
+ * | Pri_IRQ0  |
+ * +-----------+ <- Primary CPU IRQ stack top
+ * |   Sec1    |
+ * +-----------+ <- 1st secondary CPU stack top
+ * | Sec_IRQ1  |
+ * +-----------+ <- 1st secondary CPU IRQ stack top
+ * |   Sec2    |
+ * +-----------+ <- 2nd secondary CPU stack top
+ * |   ...     |
+ */
+.macro setup_cpu_stack
+       /* increment cpus_online and store */
+       ldr     r0, =cpus_online
+       ldr     r1, [r0]
+       add     r1, r1, #1
+       str     r1, [r0]
+       sub     r1, r1, #1
+
+       ldr     r0, =__stack_bottom
+
+       /* load the stack size */
+       ldr     r2, stack_size
+
+       /* calculate address of CPU stack */
+       mov     r1, r1, lsl #1
+       add     r1, r1, #1
+       mul     r1, r1, r2
+       add     r0, r0, r1
+
+       mov     sp, r0
+
+       /* return location of the IRQ stack */
+       add     r0, r0, r2
+.endm
+
        .globl __reset_entry
 __reset_entry:
        ldr     r0, =vectors
@@ -83,8 +130,11 @@ __reset_entry:
        subs    r2, #1
        bne     1b
 
-2:     ldr     sp, =stack_top
+2:     setup_cpu_stack
 
        b       inmate_main
 
+stack_size:
+       .word STACK_SIZE
+
        .ltorg
diff --git a/inmates/lib/arm/include/mach.h b/inmates/lib/arm/include/mach.h
index 6e91e62e..806a7e3c 100644
--- a/inmates/lib/arm/include/mach.h
+++ b/inmates/lib/arm/include/mach.h
@@ -39,6 +39,8 @@
  */
 
 #define PAGE_SIZE      0x1000
+#define STACK_PAGES    1
+#define STACK_SIZE     (STACK_PAGES * PAGE_SIZE)
 
 #ifdef CONFIG_MACH_JETSON_TK1
 #define CON_TYPE       "8250"
diff --git a/inmates/lib/arm/inmate.lds.S b/inmates/lib/arm/inmate.lds.S
index 6f64b5ed..4bd64910 100644
--- a/inmates/lib/arm/inmate.lds.S
+++ b/inmates/lib/arm/inmate.lds.S
@@ -75,8 +75,7 @@ SECTIONS {
        bss_dwords = SIZEOF(.bss) / 4;
 
        . = ALIGN(4096);
-       . += 0x1000;
-       stack_top = .;
+       __stack_bottom = .;
 }
 
 ENTRY(__reset_entry)
-- 
2.14.1

-- 
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