Add support for handling runtime service requests

This patch uses the reworked exception handling support to handle
runtime service requests through SMCs following the SMC calling
convention. This is a giant commit since all the changes are
inter-related. It does the following:

1. Replace the old exception handling mechanism with the new one
2. Enforce that SP_EL0 is used C runtime stacks.
3. Ensures that the cold and warm boot paths use the 'cpu_context'
   structure to program an ERET into the next lower EL.
4. Ensures that SP_EL3 always points to the next 'cpu_context'
   structure prior to an ERET into the next lower EL
5. Introduces a PSCI SMC handler which completes the use of PSCI as a
   runtime service

Change-Id: I661797f834c0803d2c674d20f504df1b04c2b852
Co-authored-by: Achin Gupta <achin.gupta@arm.com>
diff --git a/include/aarch64/arch.h b/include/aarch64/arch.h
index e8773d9..69926a3 100644
--- a/include/aarch64/arch.h
+++ b/include/aarch64/arch.h
@@ -320,7 +320,7 @@
 extern void bl1_arch_setup(void);
 extern void bl2_arch_setup(void);
 extern void bl31_arch_setup(void);
-
+extern void bl31_arch_next_el_setup(void);
 #endif /*__ASSEMBLY__*/
 
 #endif /* __ARCH_H__ */
diff --git a/include/asm_macros.S b/include/asm_macros.S
index 9de135c..decef0b 100644
--- a/include/asm_macros.S
+++ b/include/asm_macros.S
@@ -54,18 +54,6 @@
 	.endm
 
 
-	.macro	exception_entry  func
-	stp	x29, x30, [sp, #-(SIZEOF_GPREGS - GPREGS_FP_OFF)]!
-	bl	\func
-	.endm
-
-
-	.macro	exception_exit  func
-	bl	\func
-	ldp	x29, x30, [sp], #(SIZEOF_GPREGS - GPREGS_FP_OFF)
-	.endm
-
-
 	.macro	smc_check  label
 	bl	read_esr
 	ubfx	x0, x0, #ESR_EC_SHIFT, #ESR_EC_LENGTH
diff --git a/include/context.h b/include/context.h
index 1c1a3a2..082e511 100644
--- a/include/context.h
+++ b/include/context.h
@@ -193,7 +193,7 @@
  * callee saved registers if used by the C runtime and the assembler
  * does not touch the remaining.
  */
-DEFINE_REG_STRUCT(gp_regs_next, CTX_GPREG_ALL);
+DEFINE_REG_STRUCT(gp_regs, CTX_GPREG_ALL);
 
 /*
  * AArch64 EL1 system register context structure for preserving the
@@ -233,7 +233,7 @@
  * correspond to either the secure or the non-secure state.
  */
 typedef struct {
-	gp_regs_next gpregs_ctx;
+	gp_regs gpregs_ctx;
 	el3_state el3state_ctx;
 	el1_sys_regs sysregs_ctx;
 	fp_regs fpregs_ctx;
diff --git a/include/context_mgmt.h b/include/context_mgmt.h
index 2150a55..b8c8077 100644
--- a/include/context_mgmt.h
+++ b/include/context_mgmt.h
@@ -46,6 +46,9 @@
 extern void cm_el3_sysregs_context_restore(uint32_t security_state);
 extern void cm_el1_sysregs_context_save(uint32_t security_state);
 extern void cm_el1_sysregs_context_restore(uint32_t security_state);
+extern void cm_set_el3_eret_context(uint32_t security_state, uint64_t entrypoint,
+		uint32_t spsr, uint32_t scr);
 extern void cm_set_next_eret_context(uint32_t security_state);
+extern void cm_init_exception_stack(uint64_t mpidr, uint32_t security_state);
 #endif /*__ASSEMBLY__*/
 #endif /* __CM_H__ */
diff --git a/include/runtime_svc.h b/include/runtime_svc.h
index 3ee043e..ad202a9 100644
--- a/include/runtime_svc.h
+++ b/include/runtime_svc.h
@@ -101,43 +101,8 @@
 #define SERROR_AARCH32			0xf
 
 /*******************************************************************************
- * Constants that allow assembler code to access members of the 'gp_regs'
- * structure at their correct offsets.
+ * Structure definition, typedefs & constants for the runtime service framework
  ******************************************************************************/
-#define SIZEOF_GPREGS		0x110
-#define GPREGS_X0_OFF		0x0
-#define GPREGS_X1_OFF		0x8
-#define GPREGS_X2_OFF		0x10
-#define GPREGS_X3_OFF		0x18
-#define GPREGS_X4_OFF		0x20
-#define GPREGS_X5_OFF		0x28
-#define GPREGS_X6_OFF		0x30
-#define GPREGS_X7_OFF		0x38
-#define GPREGS_X8_OFF		0x40
-#define GPREGS_X9_OFF		0x48
-#define GPREGS_X10_OFF		0x50
-#define GPREGS_X11_OFF		0x58
-#define GPREGS_X12_OFF		0x60
-#define GPREGS_X13_OFF		0x68
-#define GPREGS_X14_OFF		0x70
-#define GPREGS_X15_OFF		0x78
-#define GPREGS_X16_OFF		0x80
-#define GPREGS_X17_OFF		0x88
-#define GPREGS_X18_OFF		0x90
-#define GPREGS_X19_OFF		0x98
-#define GPREGS_X20_OFF		0xA0
-#define GPREGS_X21_OFF		0xA8
-#define GPREGS_X22_OFF		0xB0
-#define GPREGS_X23_OFF		0xB8
-#define GPREGS_X24_OFF		0xC0
-#define GPREGS_X25_OFF		0xC8
-#define GPREGS_X26_OFF		0xD0
-#define GPREGS_X27_OFF		0xD8
-#define GPREGS_X28_OFF		0xE0
-#define GPREGS_SP_EL0_OFF	0xE8
-#define GPREGS_SPSR_OFF		0xF0
-#define GPREGS_FP_OFF		0x100
-#define GPREGS_LR_OFF		0x108
 
 /*
  * Constants to allow the assembler access a runtime service
@@ -219,46 +184,6 @@
 				  void *handle,
 				  uint64_t flags);
 typedef struct {
-	uint64_t x0;
-	uint64_t x1;
-	uint64_t x2;
-	uint64_t x3;
-	uint64_t x4;
-	uint64_t x5;
-	uint64_t x6;
-	uint64_t x7;
-	uint64_t x8;
-	uint64_t x9;
-	uint64_t x10;
-	uint64_t x11;
-	uint64_t x12;
-	uint64_t x13;
-	uint64_t x14;
-	uint64_t x15;
-	uint64_t x16;
-	uint64_t x17;
-	uint64_t x18;
-	uint64_t x19;
-	uint64_t x20;
-	uint64_t x21;
-	uint64_t x22;
-	uint64_t x23;
-	uint64_t x24;
-	uint64_t x25;
-	uint64_t x26;
-	uint64_t x27;
-	uint64_t x28;
-	uint64_t sp_el0;
-	uint32_t spsr;
-	/*
-	 * Alignment constraint which allows save & restore of fp & lr on the
-	 * stack during exception handling
-	 */
-	uint64_t fp __aligned(16);
-	uint64_t lr;
-} __aligned(16) gp_regs;
-
-typedef struct {
 	uint8_t start_oen;
 	uint8_t end_oen;
 	uint8_t call_type;
@@ -280,16 +205,6 @@
 			_setup, \
 			_smch }
 
-/*******************************************************************************
- * Compile time assertions to ensure that:
- * 1)  the assembler code's view of the size of the 'gp_regs' data structure is
- *     the same as the actual size of this data structure.
- * 2)  the assembler code's view of the offset of the frame pointer member of
- *     the 'gp_regs' structure is the same as the actual offset of this member.
- ******************************************************************************/
-CASSERT((sizeof(gp_regs) == SIZEOF_GPREGS), assert_sizeof_gpregs_mismatch);
-CASSERT(GPREGS_FP_OFF == __builtin_offsetof(gp_regs, fp), \
-	assert_gpregs_fp_offset_mismatch);
 /*
  * Compile time assertions related to the 'rt_svc_desc' structure to:
  * 1. ensure that the assembler and the compiler view of the size
@@ -323,6 +238,7 @@
 extern void runtime_svc_init();
 extern uint64_t __RT_SVC_DESCS_START__;
 extern uint64_t __RT_SVC_DESCS_END__;
-
+extern uint64_t get_exception_stack(uint64_t mpidr);
+extern void fault_handler(void *handle);
 #endif /*__ASSEMBLY__*/
 #endif /* __RUNTIME_SVC_H__ */