Remove 3rd buggy copy of this function and use snprintb(3) instead.

No need to allocate MAXPATHLEN buffers anymore.
This commit is contained in:
christos 2008-12-16 22:44:50 +00:00
parent 9a5d3f2817
commit 2cd3d9d4aa
4 changed files with 26 additions and 219 deletions

View File

@ -1,10 +1,10 @@
# $NetBSD: Makefile,v 1.2 2008/05/05 17:54:14 ad Exp $
# $NetBSD: Makefile,v 1.3 2008/12/16 22:44:50 christos Exp $
.PATH: ${.CURDIR}/arch
PROG= cpuctl
MAN= cpuctl.8
SRCS= cpuctl.c bitmask.c
SRCS= cpuctl.c
.if exists(${.CURDIR}/arch/${MACHINE_ARCH}.c)
SRCS+= ${MACHINE_ARCH}.c
@ -15,4 +15,7 @@ SRCS+= ${MACHINE_ARCH}-asm.S
SRCS+= noarch.c
.endif
LDADD+=-lutil
DPADD+=${LIBUTIL}
.include <bsd.prog.mk>

View File

@ -1,4 +1,4 @@
/* $NetBSD: i386.c,v 1.13 2008/10/14 15:49:04 cegger Exp $ */
/* $NetBSD: i386.c,v 1.14 2008/12/16 22:44:51 christos Exp $ */
/*-
* Copyright (c) 1999, 2000, 2001, 2006, 2007, 2008 The NetBSD Foundation, Inc.
@ -57,7 +57,7 @@
#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: i386.c,v 1.13 2008/10/14 15:49:04 cegger Exp $");
__RCSID("$NetBSD: i386.c,v 1.14 2008/12/16 22:44:51 christos Exp $");
#endif /* not lint */
#include <sys/types.h>
@ -71,6 +71,7 @@ __RCSID("$NetBSD: i386.c,v 1.13 2008/10/14 15:49:04 cegger Exp $");
#include <err.h>
#include <assert.h>
#include <math.h>
#include <util.h>
#include <machine/specialreg.h>
#include <machine/cpu.h>
@ -1185,12 +1186,12 @@ identifycpu(const char *cpuname)
int modif, family, model;
const struct cpu_cpuid_nameclass *cpup = NULL;
const struct cpu_cpuid_family *cpufam;
char *buf;
const char *feature_str[5];
struct cpu_info *ci, cistore;
extern int cpu;
extern int cpu_info_level;
size_t sz;
char buf[256];
ci = &cistore;
memset(ci, 0, sizeof(*ci));
@ -1200,7 +1201,6 @@ identifycpu(const char *cpuname)
ci->ci_cpuid_level = cpu_info_level;
cpu_probe_features(ci);
buf = malloc(MAXPATHLEN);
if (ci->ci_cpuid_level == -1) {
if (cpu < 0 || cpu >= __arraycount(i386_nocpuid_cpus))
errx(1, "unknown cpu type %d", cpu);
@ -1343,48 +1343,45 @@ identifycpu(const char *cpuname)
if (ci->ci_feature_flags) {
if ((ci->ci_feature_flags & CPUID_MASK1) != 0) {
bitmask_snprintf(ci->ci_feature_flags,
feature_str[0], buf, MAXPATHLEN);
snprintb(buf, sizeof(buf), feature_str[0],
ci->ci_feature_flags);
aprint_verbose("%s: features %s\n", cpuname, buf);
}
if ((ci->ci_feature_flags & CPUID_MASK2) != 0) {
bitmask_snprintf(ci->ci_feature_flags,
feature_str[1], buf, MAXPATHLEN);
snprintb(buf, sizeof(buf), feature_str[1],
ci->ci_feature_flags);
aprint_verbose("%s: features %s\n", cpuname, buf);
}
if ((ci->ci_feature_flags & CPUID_MASK3) != 0) {
bitmask_snprintf(ci->ci_feature_flags,
feature_str[2], buf, MAXPATHLEN);
snprintb(buf, sizeof(buf), feature_str[2],
ci->ci_feature_flags);
aprint_verbose("%s: features %s\n", cpuname, buf);
}
}
if (ci->ci_feature2_flags) {
bitmask_snprintf(ci->ci_feature2_flags,
CPUID2_FLAGS, buf, MAXPATHLEN);
snprintb(buf, sizeof(buf), CPUID2_FLAGS, ci->ci_feature2_flags);
aprint_verbose("%s: features2 %s\n", cpuname, buf);
}
if (ci->ci_feature3_flags) {
bitmask_snprintf(ci->ci_feature3_flags,
feature_str[3], buf, MAXPATHLEN);
snprintb(buf, sizeof(buf), feature_str[3],
ci->ci_feature3_flags);
aprint_verbose("%s: features3 %s\n", cpuname, buf);
}
if (ci->ci_feature4_flags) {
bitmask_snprintf(ci->ci_feature4_flags,
feature_str[4], buf, MAXPATHLEN);
snprintb(buf, sizeof(buf), feature_str[4],
ci->ci_feature4_flags);
aprint_verbose("%s: features4 %s\n", cpuname, buf);
}
if (ci->ci_padlock_flags) {
bitmask_snprintf(ci->ci_padlock_flags,
CPUID_FLAGS_PADLOCK, buf, MAXPATHLEN);
snprintb(buf, sizeof(buf), CPUID_FLAGS_PADLOCK,
ci->ci_padlock_flags);
aprint_verbose("%s: padlock features %s\n", cpuname, buf);
}
free(buf);
if (*cpu_brand_string != '\0')
aprint_normal("%s: \"%s\"\n", cpuname, cpu_brand_string);
@ -1778,7 +1775,7 @@ static void
powernow_probe(struct cpu_info *ci)
{
uint32_t regs[4];
char line[256];
char buf[256];
x86_cpuid(0x80000000, regs);
@ -1787,7 +1784,7 @@ powernow_probe(struct cpu_info *ci)
return;
x86_cpuid(0x80000007, regs);
bitmask_snprintf(regs[3], CPUID_APM_FLAGS, line, sizeof(line));
snprintb(buf, sizeof(buf), CPUID_APM_FLAGS, regs[3]);
aprint_normal_dev(ci->ci_dev, "AMD Power Management features: %s\n",
line);
buf);
}

View File

@ -1,191 +0,0 @@
/* $NetBSD: bitmask.c,v 1.1 2008/05/05 17:54:14 ad Exp $ */
/*-
* Copyright (c) 1986, 1988, 1991, 1993
* The Regents of the University of California. All rights reserved.
* (c) UNIX System Laboratories, Inc.
* All or some portions of this file are derived from material licensed
* to the University of California by American Telephone and Telegraph
* Co. or Unix System Laboratories, Inc. and are reproduced herein with
* the permission of UNIX System Laboratories, Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)subr_prf.c 8.4 (Berkeley) 5/4/95
*/
#ifndef lint
#include <sys/cdefs.h>
__RCSID("$NetBSD: bitmask.c,v 1.1 2008/05/05 17:54:14 ad Exp $");
#endif /* not lint */
#include <sys/types.h>
#include <string.h>
#include <stdio.h>
#include "cpuctl.h"
/*
* bitmask_snprintf: print an interpreted bitmask to a buffer
*
* => returns pointer to the buffer
*/
char *
bitmask_snprintf(uint64_t val, const char *p, char *bf, size_t buflen)
{
char *bp, *q;
size_t left;
const char *sbase;
char snbuf[BUFSIZ];
int base, bit, ch, len, sep;
u_quad_t field;
bp = bf;
memset(bf, 0, buflen);
/*
* Always leave room for the trailing NULL.
*/
left = buflen - 1;
/*
* Print the value into the buffer. Abort if there's not
* enough room.
*/
if (buflen < BUFSIZ)
return (bf);
ch = *p++;
base = ch != '\177' ? ch : *p++;
sbase = base == 8 ? "%qo" : base == 10 ? "%qd" : base == 16 ? "%qx" : 0;
if (sbase == 0)
return (bf); /* punt if not oct, dec, or hex */
snprintf(snbuf, sizeof(snbuf), sbase, val);
for (q = snbuf ; *q ; q++) {
*bp++ = *q;
left--;
}
/*
* If the value we printed was 0 and we're using the old-style format,
* or if we don't have room for "<x>", we're done.
*/
if (((val == 0) && (ch != '\177')) || left < 3)
return (bf);
#define PUTBYTE(b, c, l) do { \
*(b)++ = (c); \
if (--(l) == 0) \
goto out; \
} while (/*CONSTCOND*/ 0)
#define PUTSTR(b, p, l) do { \
int c; \
while ((c = *(p)++) != 0) { \
*(b)++ = c; \
if (--(l) == 0) \
goto out; \
} \
} while (/*CONSTCOND*/ 0)
/*
* Chris Torek's new bitmask format is identified by a leading \177
*/
sep = '<';
if (ch != '\177') {
/* old (standard) format. */
for (;(bit = *p++) != 0;) {
if (val & (1 << (bit - 1))) {
PUTBYTE(bp, sep, left);
for (; (ch = *p) > ' '; ++p) {
PUTBYTE(bp, ch, left);
}
sep = ',';
} else
for (; *p > ' '; ++p)
continue;
}
} else {
/* new quad-capable format; also does fields. */
field = val;
while ((ch = *p++) != '\0') {
bit = *p++; /* now 0-origin */
switch (ch) {
case 'b':
if (((u_int)(val >> bit) & 1) == 0)
goto skip;
PUTBYTE(bp, sep, left);
PUTSTR(bp, p, left);
sep = ',';
break;
case 'f':
case 'F':
len = *p++; /* field length */
field = (val >> bit) & ((1ULL << len) - 1);
if (ch == 'F') /* just extract */
break;
PUTBYTE(bp, sep, left);
sep = ',';
PUTSTR(bp, p, left);
PUTBYTE(bp, '=', left);
sprintf(snbuf, sbase, field);
q = snbuf; PUTSTR(bp, q, left);
break;
case '=':
case ':':
/*
* Here "bit" is actually a value instead,
* to be compared against the last field.
* This only works for values in [0..255],
* of course.
*/
if ((int)field != bit)
goto skip;
if (ch == '=')
PUTBYTE(bp, '=', left);
else {
PUTBYTE(bp, sep, left);
sep = ',';
}
PUTSTR(bp, p, left);
break;
default:
skip:
while (*p++ != '\0')
continue;
break;
}
}
}
if (sep != '<')
PUTBYTE(bp, '>', left);
out:
return (bf);
#undef PUTBYTE
#undef PUTSTR
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: cpuctl.h,v 1.1 2008/05/05 17:54:14 ad Exp $ */
/* $NetBSD: cpuctl.h,v 1.2 2008/12/16 22:44:51 christos Exp $ */
/*-
* Copyright (c) 2008 The NetBSD Foundation, Inc.
@ -33,6 +33,4 @@ int aprint_normal_dev(const char *, const char *, ...);
int aprint_verbose_dev(const char *, const char *, ...);
int aprint_error_dev(const char *, const char *, ...);
char *bitmask_snprintf(uint64_t, const char *, char *, size_t);
void identifycpu(const char *);