Improve debugability of VMware Linux guests by dumping
kernel messages during a panic to VM log file (vmware.log).

Co-developed-by: Bo Gan <bo....@broadcom.com>
Signed-off-by: Bo Gan <bo....@broadcom.com>
Signed-off-by: Alexey Makhalov <alexey.makha...@broadcom.com>
---
 arch/x86/include/asm/vmware.h |   1 +
 arch/x86/kernel/cpu/vmware.c  | 132 ++++++++++++++++++++++++++++++++++
 2 files changed, 133 insertions(+)

diff --git a/arch/x86/include/asm/vmware.h b/arch/x86/include/asm/vmware.h
index c9cf43d5ef23..433755b41a27 100644
--- a/arch/x86/include/asm/vmware.h
+++ b/arch/x86/include/asm/vmware.h
@@ -57,6 +57,7 @@
 #define VMWARE_HYPERVISOR_MAGIC                0x564d5868U
 
 #define VMWARE_CMD_GETVERSION          10
+#define VMWARE_CMD_MESSAGE             30
 #define VMWARE_CMD_GETHZ               45
 #define VMWARE_CMD_GETVCPU_INFO                68
 #define VMWARE_CMD_STEALCLOCK          91
diff --git a/arch/x86/kernel/cpu/vmware.c b/arch/x86/kernel/cpu/vmware.c
index 00189cdeb775..9883766715fe 100644
--- a/arch/x86/kernel/cpu/vmware.c
+++ b/arch/x86/kernel/cpu/vmware.c
@@ -28,6 +28,7 @@
 #include <linux/cpu.h>
 #include <linux/reboot.h>
 #include <linux/static_call.h>
+#include <linux/kmsg_dump.h>
 #include <asm/div64.h>
 #include <asm/x86_init.h>
 #include <asm/hypervisor.h>
@@ -139,6 +140,13 @@ static unsigned long vmware_get_tsc_khz(void)
        return vmware_tsc_khz;
 }
 
+static void kmsg_dumper_vmware_log(struct kmsg_dumper *dumper,
+                               enum kmsg_dump_reason reason);
+
+static struct kmsg_dumper kmsg_dumper = {
+       .dump = kmsg_dumper_vmware_log
+};
+
 #ifdef CONFIG_PARAVIRT
 static struct cyc2ns_data vmware_cyc2ns __ro_after_init;
 static bool vmw_sched_clock __initdata = true;
@@ -436,6 +444,8 @@ static void __init vmware_platform_setup(void)
 #endif
 
        vmware_set_capabilities();
+
+       kmsg_dump_register(&kmsg_dumper);
 }
 
 static u8 __init vmware_select_hypercall(void)
@@ -587,3 +597,125 @@ const __initconst struct hypervisor_x86 x86_hyper_vmware 
= {
        .runtime.sev_es_hcall_finish    = vmware_sev_es_hcall_finish,
 #endif
 };
+
+#define VMWARE_HB_CMD_MESSAGE  0
+#define MESSAGE_STATUS_SUCCESS (0x01 << 16)
+#define MESSAGE_STATUS_CPT     (0x10 << 16)
+#define MESSAGE_STATUS_HB      (0x80 << 16)
+
+#define RPCI_PROTOCOL_NUM      0x49435052 /* 'RPCI' */
+#define GUESTMSG_FLAG_COOKIE   0x80000000
+
+#define MESSAGE_TYPE_OPEN      (0 << 16)
+#define MESSAGE_TYPE_SENDSIZE  (1 << 16)
+#define MESSAGE_TYPE_SEND      (2 << 16)
+#define MESSAGE_TYPE_CLOSE     (6 << 16)
+
+struct vmw_msg {
+       u32 id;
+       u32 cookie_high;
+       u32 cookie_low;
+};
+
+static int
+vmware_log_open(struct vmw_msg *msg)
+{
+       u32 info;
+
+       vmware_hypercall6(VMWARE_CMD_MESSAGE | MESSAGE_TYPE_OPEN,
+                         RPCI_PROTOCOL_NUM | GUESTMSG_FLAG_COOKIE,
+                         0, &info, &msg->id, &msg->cookie_high,
+                         &msg->cookie_low);
+
+       if ((info & MESSAGE_STATUS_SUCCESS) == 0)
+               return 1;
+
+       msg->id &= 0xffff0000UL;
+       return 0;
+}
+
+static int
+vmware_log_close(struct vmw_msg *msg)
+{
+       u32 info;
+
+       vmware_hypercall5(VMWARE_CMD_MESSAGE | MESSAGE_TYPE_CLOSE, 0, msg->id,
+                         msg->cookie_high, msg->cookie_low, &info);
+
+       if ((info & MESSAGE_STATUS_SUCCESS) == 0)
+               return 1;
+       return 0;
+}
+
+static int
+vmware_log_send(struct vmw_msg *msg, const char *string)
+{
+       u32 info;
+       u32 len = strlen(string);
+
+retry:
+       vmware_hypercall5(VMWARE_CMD_MESSAGE | MESSAGE_TYPE_SENDSIZE, len,
+                         msg->id, msg->cookie_high, msg->cookie_low, &info);
+
+       if (!(info & MESSAGE_STATUS_SUCCESS))
+               return 1;
+
+       /* HB port can't access encrypted memory. */
+       if (!cc_platform_has(CC_ATTR_MEM_ENCRYPT) && (info & 
MESSAGE_STATUS_HB)) {
+               vmware_hypercall_hb_out(
+                       VMWARE_HB_CMD_MESSAGE | MESSAGE_STATUS_SUCCESS,
+                       len, msg->id, (uintptr_t) string, msg->cookie_low,
+                       msg->cookie_high, &info);
+       } else {
+               do {
+                       u32 word;
+                       size_t s = min_t(u32, len, sizeof(word));
+
+                       memcpy(&word, string, s);
+                       len -= s;
+                       string += s;
+
+                       vmware_hypercall5(VMWARE_CMD_MESSAGE | 
MESSAGE_TYPE_SEND,
+                                         word, msg->id, msg->cookie_high,
+                                         msg->cookie_low, &info);
+               } while (len && (info & MESSAGE_STATUS_SUCCESS));
+       }
+
+       if ((info & MESSAGE_STATUS_SUCCESS) == 0) {
+               if (info & MESSAGE_STATUS_CPT)
+                       /* A checkpoint occurred. Retry. */
+                       goto retry;
+               return 1;
+       }
+       return 0;
+}
+STACK_FRAME_NON_STANDARD(vmware_log_send);
+
+/**
+ * kmsg_dumper_vmware_log - dumps kmsg to vmware.log file on the host
+ */
+static void kmsg_dumper_vmware_log(struct kmsg_dumper *dumper,
+                               enum kmsg_dump_reason reason)
+{
+       struct vmw_msg msg;
+       struct kmsg_dump_iter iter;
+       static char line[1024];
+       size_t len = 0;
+
+       /* Line prefix to send to VM log file. */
+       line[0] = 'l';
+       line[1] = 'o';
+       line[2] = 'g';
+       line[3] = ' ';
+
+       kmsg_dump_rewind(&iter);
+       while (kmsg_dump_get_line(&iter, true, line + 4, sizeof(line) - 4,
+                                 &len)) {
+               line[len + 4] = '\0';
+               if (vmware_log_open(&msg))
+                       return;
+               if (vmware_log_send(&msg, line))
+                       return;
+               vmware_log_close(&msg);
+       }
+}
-- 
2.39.4


Reply via email to