From 89871cdeb650dcef3e851b649713612888223e22 Mon Sep 17 00:00:00 2001 From: John Baldwin Date: Fri, 29 Aug 2014 21:25:47 +0000 Subject: [PATCH] - Add a new structure type for the ACPI 3.0 SMAP entry that includes the optional attributes field. - Add a 'machdep.smap' sysctl that exports the SMAP table of the running system as an array of the ACPI 3.0 structure. (On older systems, the attributes are given a value of zero.) Note that the sysctl only exports the SMAP table if it is available via the metadata passed from the loader to the kernel. If an SMAP is not available, an empty array is returned. - Add a format handler for the ACPI 3.0 SMAP structure to the sysctl(8) binary to format the SMAP structures in a readable format similar to the format found in boot messages. MFC after: 2 weeks --- sbin/sysctl/sysctl.c | 29 +++++++++++++++++++++++++++++ sys/amd64/amd64/machdep.c | 36 ++++++++++++++++++++++++++++++++++++ sys/amd64/include/pc/bios.h | 8 ++++++++ sys/i386/i386/machdep.c | 36 ++++++++++++++++++++++++++++++++++++ sys/i386/include/pc/bios.h | 8 ++++++++ 5 files changed, 117 insertions(+) diff --git a/sbin/sysctl/sysctl.c b/sbin/sysctl/sysctl.c index 13596450d2e1..578260d63185 100644 --- a/sbin/sysctl/sysctl.c +++ b/sbin/sysctl/sysctl.c @@ -48,6 +48,10 @@ static const char rcsid[] = #include #include +#if defined(__amd64__) || defined(__i386__) +#include +#endif + #include #include #include @@ -541,6 +545,27 @@ S_vmtotal(int l2, void *p) return (0); } +#if defined(__amd64__) || defined(__i386__) +static int +S_bios_smap_xattr(int l2, void *p) +{ + struct bios_smap_xattr *smap, *end; + + if (l2 % sizeof(*smap) != 0) { + warnx("S_bios_smap_xattr %d is not a multiple of %zu", l2, + sizeof(*smap)); + return (1); + } + + end = (struct bios_smap_xattr *)((char *)p + l2); + for (smap = p; smap < end; smap++) + printf("\nSMAP type=%02x, xattr=%02x, base=%016jx, len=%016jx", + smap->type, smap->xattr, (uintmax_t)smap->base, + (uintmax_t)smap->length); + return (0); +} +#endif + static int set_IK(const char *str, int *val) { @@ -793,6 +818,10 @@ show_var(int *oid, int nlen) func = S_loadavg; else if (strcmp(fmt, "S,vmtotal") == 0) func = S_vmtotal; +#if defined(__amd64__) || defined(__i386__) + else if (strcmp(fmt, "S,bios_smap_xattr") == 0) + func = S_bios_smap_xattr; +#endif else func = NULL; if (func) { diff --git a/sys/amd64/amd64/machdep.c b/sys/amd64/amd64/machdep.c index f02045d64147..bb0bc210e68a 100644 --- a/sys/amd64/amd64/machdep.c +++ b/sys/amd64/amd64/machdep.c @@ -2090,6 +2090,42 @@ cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size) pcpu->pc_acpi_id = 0xffffffff; } +static int +smap_sysctl_handler(SYSCTL_HANDLER_ARGS) +{ + struct bios_smap *smapbase; + struct bios_smap_xattr smap; + caddr_t kmdp; + uint32_t *smapattr; + int count, error, i; + + /* Retrieve the system memory map from the loader. */ + kmdp = preload_search_by_type("elf kernel"); + if (kmdp == NULL) + kmdp = preload_search_by_type("elf64 kernel"); + smapbase = (struct bios_smap *)preload_search_info(kmdp, + MODINFO_METADATA | MODINFOMD_SMAP); + if (smapbase == NULL) + return (0); + smapattr = (uint32_t *)preload_search_info(kmdp, + MODINFO_METADATA | MODINFOMD_SMAP_XATTR); + count = *((uint32_t *)smapbase - 1) / sizeof(*smapbase); + error = 0; + for (i = 0; i < count; i++) { + smap.base = smapbase[i].base; + smap.length = smapbase[i].length; + smap.type = smapbase[i].type; + if (smapattr != NULL) + smap.xattr = smapattr[i]; + else + smap.xattr = 0; + error = SYSCTL_OUT(req, &smap, sizeof(smap)); + } + return (error); +} +SYSCTL_PROC(_machdep, OID_AUTO, smap, CTLTYPE_OPAQUE|CTLFLAG_RD, NULL, 0, + smap_sysctl_handler, "S,bios_smap_xattr", "Raw BIOS SMAP data"); + void spinlock_enter(void) { diff --git a/sys/amd64/include/pc/bios.h b/sys/amd64/include/pc/bios.h index 95ef703e498f..1dbf110219be 100644 --- a/sys/amd64/include/pc/bios.h +++ b/sys/amd64/include/pc/bios.h @@ -51,6 +51,14 @@ struct bios_smap { u_int32_t type; } __packed; +/* Structure extended to include extended attribute field in ACPI 3.0. */ +struct bios_smap_xattr { + u_int64_t base; + u_int64_t length; + u_int32_t type; + u_int32_t xattr; +} __packed; + /* * System Management BIOS */ diff --git a/sys/i386/i386/machdep.c b/sys/i386/i386/machdep.c index 243eab3a8043..b3338a13dfbf 100644 --- a/sys/i386/i386/machdep.c +++ b/sys/i386/i386/machdep.c @@ -3122,6 +3122,42 @@ cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size) pcpu->pc_acpi_id = 0xffffffff; } +static int +smap_sysctl_handler(SYSCTL_HANDLER_ARGS) +{ + struct bios_smap *smapbase; + struct bios_smap_xattr smap; + caddr_t kmdp; + uint32_t *smapattr; + int count, error, i; + + /* Retrieve the system memory map from the loader. */ + kmdp = preload_search_by_type("elf kernel"); + if (kmdp == NULL) + kmdp = preload_search_by_type("elf32 kernel"); + smapbase = (struct bios_smap *)preload_search_info(kmdp, + MODINFO_METADATA | MODINFOMD_SMAP); + if (smapbase == NULL) + return (0); + smapattr = (uint32_t *)preload_search_info(kmdp, + MODINFO_METADATA | MODINFOMD_SMAP_XATTR); + count = *((u_int32_t *)smapbase - 1) / sizeof(*smapbase); + error = 0; + for (i = 0; i < count; i++) { + smap.base = smapbase[i].base; + smap.length = smapbase[i].length; + smap.type = smapbase[i].type; + if (smapattr != NULL) + smap.xattr = smapattr[i]; + else + smap.xattr = 0; + error = SYSCTL_OUT(req, &smap, sizeof(smap)); + } + return (error); +} +SYSCTL_PROC(_machdep, OID_AUTO, smap, CTLTYPE_OPAQUE|CTLFLAG_RD, NULL, 0, + smap_sysctl_handler, "S,bios_smap_xattr", "Raw BIOS SMAP data"); + void spinlock_enter(void) { diff --git a/sys/i386/include/pc/bios.h b/sys/i386/include/pc/bios.h index f757e742ef55..d1d8caff4a61 100644 --- a/sys/i386/include/pc/bios.h +++ b/sys/i386/include/pc/bios.h @@ -221,6 +221,14 @@ struct bios_smap { u_int32_t type; } __packed; +/* Structure extended to include extended attribute field in ACPI 3.0. */ +struct bios_smap_xattr { + u_int64_t base; + u_int64_t length; + u_int32_t type; + u_int32_t xattr; +} __packed; + /* * System Management BIOS */