From ca52fa23a379699a0d3eded2d8e95e7011aa1418 Mon Sep 17 00:00:00 2001
From: Pekka Enberg <penberg@cloudius-systems.com>
Date: Wed, 16 Oct 2013 14:55:12 +0300
Subject: [PATCH] x64: Register dump on GP fault

Dump registers on general protection fault for debugging purposes.  Even
if you have gdb available, getting to the exception frame is not always
possible after OSv has crashed.

Example output looks as follows:

registers:
RIP: 0x0000100000b7e913  RFL: 0x0000000000010202  CS:  0x0000000000000008  SS:  0x0000000000000010
RAX: 0xffffc000418ed278  RBX: 0xffffc00041b2c050  RCX: 0x0000000000000004  RDX: 0x0000000000000000
RSI: 0x0000000000000001  RDI: 0x43e0000000000000  RBP: 0x0000200008548d10  R8:  0xffffc000426e3010
R9:  0x0000000000000004  R10: 0x43e0000000000000  R11: 0xffffc00041b2c050  R12: 0xffffc000418ed1e8
R13: 0x0000000000000004  R14: 0x43e0000000000000  R15: 0xffffc00041b2c050  RSP: 0x0000200008548aa0
general protection fault

Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com>
---
 arch/x64/dump.cc       | 20 ++++++++++++++++++++
 arch/x64/dump.hh       | 15 +++++++++++++++
 arch/x64/exceptions.cc |  4 ++++
 build.mk               |  1 +
 4 files changed, 40 insertions(+)
 create mode 100644 arch/x64/dump.cc
 create mode 100644 arch/x64/dump.hh

diff --git a/arch/x64/dump.cc b/arch/x64/dump.cc
new file mode 100644
index 000000000..baa19d70c
--- /dev/null
+++ b/arch/x64/dump.cc
@@ -0,0 +1,20 @@
+/*
+ * Copyright (C) 2013 Cloudius Systems, Ltd.
+ *
+ * This work is open source software, licensed under the terms of the
+ * BSD license as described in the LICENSE file in the top-level directory.
+ */
+
+#include "exceptions.hh"
+#include "debug.hh"
+#include "dump.hh"
+
+void dump_registers(exception_frame* ef)
+{
+    debug("registers:");
+    debug("RIP: 0x%016x  RFL: 0x%016x  CS:  0x%016x  SS:  0x%016x\n", ef->rip, ef->rflags, ef->cs, ef->ss);
+    debug("RAX: 0x%016x  RBX: 0x%016x  RCX: 0x%016x  RDX: 0x%016x\n", ef->rax, ef->rbx, ef->rcx, ef->rdx);
+    debug("RSI: 0x%016x  RDI: 0x%016x  RBP: 0x%016x  R8:  0x%016x\n", ef->rsi, ef->rdi, ef->rbp, ef->r8);
+    debug("R9:  0x%016x  R10: 0x%016x  R11: 0x%016x  R12: 0x%016x\n", ef->r9, ef->r10, ef->r11, ef->r12);
+    debug("R13: 0x%016x  R14: 0x%016x  R15: 0x%016x  RSP: 0x%016x\n", ef->r9, ef->r10, ef->r11, ef->rsp);
+}
diff --git a/arch/x64/dump.hh b/arch/x64/dump.hh
new file mode 100644
index 000000000..28e304b94
--- /dev/null
+++ b/arch/x64/dump.hh
@@ -0,0 +1,15 @@
+/*
+ * Copyright (C) 2013 Cloudius Systems, Ltd.
+ *
+ * This work is open source software, licensed under the terms of the
+ * BSD license as described in the LICENSE file in the top-level directory.
+ */
+
+#ifndef DUMP_HH
+#define DUMP_HH
+
+struct exception_frame;
+
+void dump_registers(exception_frame* ef);
+
+#endif
diff --git a/arch/x64/exceptions.cc b/arch/x64/exceptions.cc
index 4116df412..9360b900b 100644
--- a/arch/x64/exceptions.cc
+++ b/arch/x64/exceptions.cc
@@ -6,6 +6,7 @@
  */
 
 #include "exceptions.hh"
+#include "dump.hh"
 #include "mmu.hh"
 #include "processor.hh"
 #include "interrupt.hh"
@@ -209,6 +210,9 @@ void general_protection(exception_frame* ef)
     if (fixup_fault(ef)) {
         return;
     }
+
+    dump_registers(ef);
+
     abort("general protection fault\n");
 }
 
diff --git a/build.mk b/build.mk
index 0ccdc7b51..973a49e3b 100644
--- a/build.mk
+++ b/build.mk
@@ -476,6 +476,7 @@ drivers += drivers/hpet.o
 drivers += drivers/xenfront.o drivers/xenfront-xenbus.o drivers/xenfront-blk.o
 
 objects = bootfs.o
+objects += arch/x64/dump.o
 objects += arch/x64/exceptions.o
 objects += arch/x64/entry.o
 objects += arch/x64/ioapic.o
-- 
GitLab