Unify panic handling in a single location.

Change-Id: Ia5b0dd7c04b87ba87b190dfdc288fcaf92110091
diff --git a/src/BUILD.gn b/src/BUILD.gn
index d0cbc90..d7c0d92 100644
--- a/src/BUILD.gn
+++ b/src/BUILD.gn
@@ -51,6 +51,7 @@
     "fdt_handler.c",
     "mm.c",
     "mpool.c",
+    "panic.c",
     "vm.c",
   ]
 
diff --git a/src/arch/aarch64/handler.c b/src/arch/aarch64/handler.c
index 871a38e..0f173ab 100644
--- a/src/arch/aarch64/handler.c
+++ b/src/arch/aarch64/handler.c
@@ -21,6 +21,7 @@
 #include "hf/api.h"
 #include "hf/cpu.h"
 #include "hf/dlog.h"
+#include "hf/panic.h"
 #include "hf/spci.h"
 #include "hf/vm.h"
 
@@ -124,24 +125,12 @@
 	}
 }
 
-/**
- * This should never be reached as it means something has gone very wrong.
- */
-static noreturn void hang(void)
-{
-	dlog("Hang: something went very wrong!\n");
-	for (;;) {
-		/* Do nothing. */
-	}
-}
-
 noreturn void irq_current_exception(uintreg_t elr, uintreg_t spsr)
 {
 	(void)elr;
 	(void)spsr;
 
-	dlog("IRQ from current\n");
-	hang();
+	panic("IRQ from current");
 }
 
 noreturn void fiq_current_exception(uintreg_t elr, uintreg_t spsr)
@@ -149,8 +138,7 @@
 	(void)elr;
 	(void)spsr;
 
-	dlog("FIQ from current\n");
-	hang();
+	panic("FIQ from current");
 }
 
 noreturn void serr_current_exception(uintreg_t elr, uintreg_t spsr)
@@ -158,8 +146,7 @@
 	(void)elr;
 	(void)spsr;
 
-	dlog("SERR from current\n");
-	hang();
+	panic("SERR from current");
 }
 
 noreturn void sync_current_exception(uintreg_t elr, uintreg_t spsr)
@@ -188,7 +175,7 @@
 		break;
 	}
 
-	hang();
+	panic("EL2 exception");
 }
 
 /**
@@ -259,12 +246,12 @@
 
 	case PSCI_SYSTEM_OFF:
 		smc(PSCI_SYSTEM_OFF, 0, 0, 0);
-		hang();
+		panic("System off failed");
 		break;
 
 	case PSCI_SYSTEM_RESET:
 		smc(PSCI_SYSTEM_RESET, 0, 0, 0);
-		hang();
+		panic("System reset failed");
 		break;
 
 	case PSCI_AFFINITY_INFO:
@@ -306,7 +293,7 @@
 	case PSCI_CPU_OFF:
 		cpu_off(current()->cpu);
 		smc(PSCI_CPU_OFF, 0, 0, 0);
-		hang();
+		panic("CPU off failed");
 		break;
 
 	case PSCI_CPU_ON:
diff --git a/src/main.c b/src/main.c
index a537304..02fc0a8 100644
--- a/src/main.c
+++ b/src/main.c
@@ -16,7 +16,6 @@
 
 #include <stdalign.h>
 #include <stddef.h>
-#include <stdnoreturn.h>
 
 #include "hf/arch/init.h"
 #include "hf/arch/std.h"
@@ -29,6 +28,7 @@
 #include "hf/load.h"
 #include "hf/mm.h"
 #include "hf/mpool.h"
+#include "hf/panic.h"
 #include "hf/vm.h"
 
 #include "vmapi/hf/call.h"
@@ -38,29 +38,6 @@
 					       HEAP_PAGES];
 
 /**
- * Blocks the hypervisor.
- *
- * TODO: Determine if we want to omit strings on non-debug builds.
- */
-noreturn void panic(const char *fmt, ...)
-{
-	va_list args;
-
-	/* TODO: Block all CPUs. */
-
-	dlog("Panic: ");
-
-	va_start(args, fmt);
-	vdlog(fmt, args);
-	va_end(args);
-
-	dlog("\n");
-
-	for (;;) {
-	}
-}
-
-/**
  * Performs one-time initialisation of the hypervisor.
  */
 static void one_time_init(void)
diff --git a/src/panic.c b/src/panic.c
new file mode 100644
index 0000000..17399dc
--- /dev/null
+++ b/src/panic.c
@@ -0,0 +1,44 @@
+/*
+ * Copyright 2019 The Hafnium Authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "hf/panic.h"
+
+#include <stdarg.h>
+
+#include "hf/dlog.h"
+
+/**
+ * Blocks the hypervisor.
+ *
+ * TODO: Determine if we want to omit strings on non-debug builds.
+ */
+noreturn void panic(const char *fmt, ...)
+{
+	va_list args;
+
+	/* TODO: Block all CPUs. */
+
+	dlog("Panic: ");
+
+	va_start(args, fmt);
+	vdlog(fmt, args);
+	va_end(args);
+
+	dlog("\n");
+
+	for (;;) {
+	}
+}