Re: [PATCH -v2] x86: Add an archinfo dumper module

From: Luck, Tony
Date: Fri Feb 05 2016 - 14:51:51 EST


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