feat(rme): add realm_print_exception for realm payload

This is a fork of exception_report.c in TFTF framework to
realm_exception_report.c. realm_print_exception uses realm_printf
and removes platform specific calls.

Signed-off-by: Arunachalam Ganapathy <arunachalam.ganapathy@arm.com>
Change-Id: I26c8790e5cac81bbab01fa81c4282824ded72b55
diff --git a/realm/aarch64/realm_exceptions.S b/realm/aarch64/realm_exceptions.S
index 99b601d..210dd3e 100644
--- a/realm/aarch64/realm_exceptions.S
+++ b/realm/aarch64/realm_exceptions.S
@@ -110,7 +110,7 @@
 	/* Save original stack pointer value on the stack */
 	add	x1, x0, #0x100
 	str	x1, [x0, #0xf8]
-	b	print_exception
+	b	realm_print_exception
 0:	restore_gp_regs
 	add	sp, sp, #0x100
 	eret
@@ -136,5 +136,5 @@
 
 	/* Print the saved CPU context on the UART. */
 	mov	x0, sp
-	b	print_exception
+	b	realm_print_exception
 endfunc crash_dump
diff --git a/realm/include/platform.h b/realm/include/platform.h
deleted file mode 100644
index de91c16..0000000
--- a/realm/include/platform.h
+++ /dev/null
@@ -1,20 +0,0 @@
-/*
- * Copyright (c) 2022-2023, Arm Limited. All rights reserved.
- *
- * SPDX-License-Identifier: BSD-3-Clause
- *
- */
-
-#ifndef PLATFORM_H
-#define PLATFORM_H
-
-/*
- * Helper that returns a linear core ID from a MPID
- * Need to provide a RSI_HOST_CALL to request this from Host platform.
- */
-unsigned int platform_get_core_pos(u_register_t mpid)
-{
-	return 0U;
-}
-
-#endif /* PLATFORM_H */
diff --git a/realm/realm.mk b/realm/realm.mk
index 5e658ed..305c007 100644
--- a/realm/realm.mk
+++ b/realm/realm.mk
@@ -26,6 +26,7 @@
 	$(addprefix realm/,						\
 	aarch64/realm_entrypoint.S					\
 	aarch64/realm_exceptions.S					\
+	realm_exception_report.c					\
 	realm_debug.c							\
 	realm_interrupt.c						\
 	realm_multiple_rec.c						\
@@ -51,10 +52,6 @@
 	lib/extensions/sme/aarch64/sme.c				\
 	lib/extensions/sme/aarch64/sme_helpers.S
 
-# TODO: Remove dependency on TFTF files.
-REALM_SOURCES	+=							\
-	tftf/framework/${ARCH}/exception_report.c
-
 REALM_LINKERFILE:=	realm/realm.ld.S
 
 # ARMv8.3 Pointer Authentication support files
diff --git a/realm/realm_exception_report.c b/realm/realm_exception_report.c
new file mode 100644
index 0000000..b0297d7
--- /dev/null
+++ b/realm/realm_exception_report.c
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2023, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#include <arch_helpers.h>
+#include <debug.h>
+
+/* We save x0-x30. */
+#define GPRS_CNT 31U
+
+/* Set of registers saved by the crash_dump() assembly function in stack. */
+struct rec_regs {
+	unsigned long gprs[GPRS_CNT];
+	unsigned long sp;
+};
+
+void __dead2 realm_print_exception(const struct rec_regs *ctx)
+{
+	u_register_t mpid;
+
+	/*
+	 * The instruction barrier ensures we don't read stale values of system
+	 * registers.
+	 */
+	isb();
+
+	mpid = read_mpidr_el1();
+	realm_printf("Unhandled exception on REC%u.\n", mpid & MPID_MASK);
+
+	/* Dump some interesting system registers. */
+	realm_printf("System registers:\n");
+	realm_printf("  MPIDR=0x%lx\n", mpid);
+	realm_printf("  ESR=0x%lx  ELR=0x%lx  FAR=0x%lx\n", read_esr_el1(),
+		     read_elr_el1(), read_far_el1());
+	realm_printf("  SCTLR=0x%lx  SPSR=0x%lx  DAIF=0x%lx\n",
+		     read_sctlr_el1(), read_spsr_el1(), read_daif());
+
+	/* Dump general-purpose registers. */
+	realm_printf("General-purpose registers:\n");
+	for (unsigned int i = 0U; i < GPRS_CNT; i++) {
+		realm_printf("  x%u=0x%lx\n", i, ctx->gprs[i]);
+	}
+	realm_printf("  SP=0x%lx\n", ctx->sp);
+
+	while (1) {
+		wfi();
+	}
+}