linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Luck, Tony" <tony.luck@intel.com>
To: Borislav Petkov <bp@alien8.de>
Cc: Ingo Molnar <mingo@kernel.org>, "H. Peter Anvin" <hpa@zytor.com>,
	Thomas Gleixner <tglx@linutronix.de>,
	Andy Lutomirski <luto@amacapital.net>,
	Peter Zijlstra <peterz@infradead.org>,
	Steven Rostedt <rostedt@goodmis.org>,
	Brian Gerst <brgerst@gmail.com>,
	lkml <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH -v2] x86: Add an archinfo dumper module
Date: Fri, 5 Feb 2016 11:51:45 -0800	[thread overview]
Message-ID: <20160205195145.GA10385@agluck-desk.sc.intel.com> (raw)
In-Reply-To: <20160204152235.GB5343@pd.tnic>

Patch on top of your v2 that defines a register priting function based
on a single string format descriptor.  CR4 changed to use it.

 arch/x86/kernel/archinfo.c |  135 ++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 114 insertions(+), 21 deletions(-)

We could break even on code lines with about four more registers using this!

I think it could print the GDT (though the format string will be very long).

Probably could use some sanity checks and cleanup. Maybe there might
be some more useful format cases for other registers.

-Tony


---

diff --git a/arch/x86/kernel/archinfo.c b/arch/x86/kernel/archinfo.c
index c04e98625565..3374e1307a96 100644
--- a/arch/x86/kernel/archinfo.c
+++ b/arch/x86/kernel/archinfo.c
@@ -132,31 +132,124 @@ static int gdt_open(struct inode *inode, struct file *filp)
 	return single_open(filp, gdt_show, NULL);
 }
 
+/*
+ * find the field name, and its length
+ */
+static char *get_fname(char *s, char *e, unsigned long val, int *flen)
+{
+	char *fe;
+
+	while (val--) {
+		s = strnchr(s, e - s, ',');
+		if (s >= e) {
+			*flen = 7;
+			return "unknown";
+		}
+		s++;
+	}
+	fe = strnchr(s, e - s, ',');
+	if (fe)
+		*flen = fe - s;
+	else
+		*flen = e - s;
+	return s;
+}
+
+/*
+ * print a value consisting of several bitfields in a human readable way
+ *
+ * Format string has a ":" separated set of descriptions from LSB to MSB
+ * interpreted like this:
+ *	<width>fmtNAME
+ *	width is the number of bits in this field
+ *	fmt is either a single character:
+ *		f - print the name of the field if the field value is non-zero
+ *		r - reserved field - skip it
+ *		d - Print NAME=%d
+ *		x - Print NAME=0x%x
+ *	or a set of comma separated strings inside [str0,str1,...strN]
+ *		print NAME=%s
+ *	If width is missing, default to "1f"
+ */
+static void show_reg_bits(struct seq_file *m, char *regname, char *fmt, unsigned long val)
+{
+	char *endptr, *s_fnames = NULL, *e_fnames = NULL, *f;
+	int nbits, totbits = 0;
+	int mode, flen, namelen;
+	unsigned long fval;
+	unsigned long orig_val = val;
+
+	seq_printf(m, "%s: [", regname);
+loop:
+	nbits = kstrtol(fmt, &endptr, 10);
+	if (endptr == fmt) {
+		nbits = 1;
+		mode = 'f';
+	} else {
+		switch (*endptr) {
+		case 'f': case 'r': case 'd': case 'x':
+			mode = *endptr;
+			endptr++;
+			break;
+		case '[':
+			mode = *endptr;
+			s_fnames = endptr + 1;
+			endptr = strchr(endptr, ']');
+			if (endptr) {
+				e_fnames = endptr;
+				endptr++;
+			} else {
+				seq_puts(m, "Missing ']'\n");
+				return;
+			}
+			break;
+		default:
+			seq_printf(m, "Unknown format '%c'\n", *endptr);
+			return;
+		}
+	}
+	totbits += nbits;
+	fmt = strchr(endptr, '|');
+	namelen = fmt ? fmt - endptr : strlen(endptr);
+
+	fval = val >> (64 - nbits);
+	switch (mode) {
+	case 'f':
+		if (fval)
+			seq_printf(m, "%.*s", namelen, endptr);
+		else
+			seq_puts(m, "-");
+		break;
+	case 'd':
+		seq_printf(m, "%.*s=%ld", namelen, endptr, fval);
+		break;
+	case 'x':
+		seq_printf(m, "%.*s=0x%lx", namelen, endptr, fval);
+		break;
+	case '[':
+		f = get_fname(s_fnames, e_fnames, fval, &flen);
+		seq_printf(m, "%.*s=%.*s", namelen, endptr, flen, f);
+	}
+
+	if (fmt) {
+		fmt++;
+		val <<= nbits;
+		if (mode != 'r')
+			seq_puts(m, "|");
+		goto loop;
+	}
+	if (totbits != 64)
+		seq_printf(m, "{format described %d bits}", totbits);
+	seq_printf(m, "]: 0x%lx\n", orig_val);
+}
+
+static char *cr4_format = "41r|PKE|SMAP|SMEP|1r|OSXSAVE|PCIDE|FSGSBASE|1r|SMXE|VMXE|2r|OSXMMEXCPT|OSFXSR|PCE|PGE|MCE|PAE|PSE|DE|TSD|PVI|VME";
+
 static int cr_show(struct seq_file *m, void *v)
 {
 	unsigned long cr4 = __read_cr4();
 
-	seq_printf(m, "CR4: [%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s]: 0x%lx\n",
-		      (cr4 & BIT(22) ? "PKE"		: "-"),
-		      (cr4 & BIT(21) ? "SMAP"		: "-"),
-		      (cr4 & BIT(20) ? "SMEP"		: "-"),
-		      (cr4 & BIT(18) ? "OSXSAVE"	: "-"),
-		      (cr4 & BIT(17) ? "PCIDE"		: "-"),
-		      (cr4 & BIT(16) ? "FSGSBASE"	: "-"),
-		      (cr4 & BIT(14) ? "SMXE"		: "-"),
-		      (cr4 & BIT(13) ? "VMXE"		: "-"),
-		      (cr4 & BIT(10) ? "OSXMMEXCPT"	: "-"),
-		      (cr4 & BIT(9)  ? "OSFXSR"		: "-"),
-		      (cr4 & BIT(8)  ? "PCE"		: "-"),
-		      (cr4 & BIT(7)  ? "PGE"		: "-"),
-		      (cr4 & BIT(6)  ? "MCE"		: "-"),
-		      (cr4 & BIT(5)  ? "PAE"		: "-"),
-		      (cr4 & BIT(4)  ? "PSE"		: "-"),
-		      (cr4 & BIT(3)  ? "DE"		: "-"),
-		      (cr4 & BIT(2)  ? "TSD"		: "-"),
-		      (cr4 & BIT(1)  ? "PVI"		: "-"),
-		      (cr4 & BIT(0)  ? "VME"		: "-"),
-		      cr4);
+	show_reg_bits(m, "CR4", cr4_format, cr4);
 
 	return 0;
 }
-- 
2.5.0

  parent reply	other threads:[~2016-02-05 19:51 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-02-01 11:56 [RFC] Dump interesting arch/platform info Borislav Petkov
2016-02-01 23:29 ` Luck, Tony
2016-02-02  9:41   ` Borislav Petkov
2016-02-03 10:48     ` Ingo Molnar
2016-02-03 11:00       ` Borislav Petkov
2016-02-04 15:22         ` [PATCH -v2] x86: Add an archinfo dumper module Borislav Petkov
2016-02-04 19:07           ` Luck, Tony
2016-02-07 10:51             ` Borislav Petkov
2016-02-09 19:17               ` Luck, Tony
2016-02-09 19:46                 ` Borislav Petkov
2016-02-05 19:51           ` Luck, Tony [this message]
2016-02-05 22:24             ` Luck, Tony
2016-02-08  0:01           ` H. Peter Anvin
2016-02-08  7:50             ` Boris Petkov
2016-02-09 12:23             ` Ingo Molnar
2016-02-09 14:01               ` Borislav Petkov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20160205195145.GA10385@agluck-desk.sc.intel.com \
    --to=tony.luck@intel.com \
    --cc=bp@alien8.de \
    --cc=brgerst@gmail.com \
    --cc=hpa@zytor.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luto@amacapital.net \
    --cc=mingo@kernel.org \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.org \
    --cc=tglx@linutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).