Add CPUID subleaf capability to cpuctl/cpucontrol

Jia-Shiun Li jiashiun at gmail.com
Wed Mar 26 15:18:34 UTC 2014


Hi all,

I am recently writing a small tool playing msr with cpuctl(4).
Meanwhile I found that it is currently not passing value but 0 in ECX
register to CPUID instruction as input. So I have the attached patch
to do it.

ECX is used to specify sub-leaf for some EAX leaf value. For example
  EAX=0x04: Deterministic Cache Parameters Leaf
  EAX=0x0b: Extended Topology Enumeration Leaf
with the attached patch user will be able to get subleaf info by
writing applications using cpuctl(4) ioctl or by using cpucontrol(8)
with -s flag.

Please comment.

Regards,
Jia-Shiun.
-------------- next part --------------
Index: sys/dev/cpuctl/cpuctl.c
===================================================================
--- sys/dev/cpuctl/cpuctl.c	(revision 263420)
+++ sys/dev/cpuctl/cpuctl.c	(working copy)
@@ -204,7 +204,7 @@
 	oldcpu = td->td_oncpu;
 	is_bound = cpu_sched_is_bound(td);
 	set_cpu(cpu, td);
-	cpuid_count(data->level, 0, data->data);
+	cpuid_count(data->level, data->sublevel, data->data);
 	restore_cpu(oldcpu, is_bound, td);
 	return (0);
 }
Index: sys/sys/cpuctl.h
===================================================================
--- sys/sys/cpuctl.h	(revision 263420)
+++ sys/sys/cpuctl.h	(working copy)
@@ -36,6 +36,7 @@
 
 typedef struct {
 	int		level;	/* CPUID level */
+	int		sublevel; /* sublevel */
 	uint32_t	data[4];
 } cpuctl_cpuid_args_t;
 
Index: usr.sbin/cpucontrol/cpucontrol.c
===================================================================
--- usr.sbin/cpucontrol/cpucontrol.c	(revision 263420)
+++ usr.sbin/cpucontrol/cpucontrol.c	(working copy)
@@ -60,6 +60,7 @@
 #define	FLAG_I	0x01
 #define	FLAG_M	0x02
 #define	FLAG_U	0x04
+#define	FLAG_S	0x08
 
 #define	OP_INVAL	0x00
 #define	OP_READ		0x01
@@ -98,7 +99,7 @@
 
 static void	usage(void);
 static int	isdir(const char *path);
-static int	do_cpuid(const char *cmdarg, const char *dev);
+static int	do_cpuid(const char *cmdarg, const char *sublevelarg, const char *dev);
 static int	do_msr(const char *cmdarg, const char *dev);
 static int	do_update(const char *dev);
 static void	datadir_add(const char *path);
@@ -131,9 +132,10 @@
 }
 
 static int
-do_cpuid(const char *cmdarg, const char *dev)
+do_cpuid(const char *cmdarg, const char *sublevelarg, const char *dev)
 {
 	unsigned int level;
+	unsigned int sublevel;
 	cpuctl_cpuid_args_t args;
 	int fd, error;
 	char *endptr;
@@ -147,11 +149,22 @@
 		usage();
 		/* NOTREACHED */
 	}
+	if (sublevelarg != NULL) {
+		sublevel = strtoul(sublevelarg, &endptr, 16);
+		if (*sublevelarg == '\0' || *endptr != '\0') {
+			WARNX(0, "incorrect operand: %s", cmdarg);
+			usage();
+			/* NOTREACHED */
+		}
+	} else {
+		sublevel = 0;
+	}
 
 	/*
 	 * Fill ioctl argument structure.
 	 */
 	args.level = level;
+	args.sublevel = sublevel;
 	fd = open(dev, O_RDONLY);
 	if (fd < 0) {
 		WARN(0, "error opening %s for reading", dev);
@@ -163,8 +176,8 @@
 		close(fd);
 		return (error);
 	}
-	fprintf(stdout, "cpuid level 0x%x: 0x%.8x 0x%.8x 0x%.8x 0x%.8x\n",
-	    level, args.data[0], args.data[1], args.data[2], args.data[3]);
+	fprintf(stdout, "cpuid level 0x%x sublevel 0x%x: 0x%.8x 0x%.8x 0x%.8x 0x%.8x\n",
+	    level, sublevel, args.data[0], args.data[1], args.data[2], args.data[3]);
 	close(fd);
 	return (0);
 }
@@ -367,18 +380,20 @@
 {
 	int c, flags;
 	const char *cmdarg;
+	const char *sublevelarg;
 	const char *dev;
 	int error;
 
 	flags = 0;
 	error = 0;
-	cmdarg = "";	/* To keep gcc3 happy. */
+	cmdarg = NULL;
+	sublevelarg = NULL;
 
 	/*
 	 * Add all default data dirs to the list first.
 	 */
 	datadir_add(DEFAULT_DATADIR);
-	while ((c = getopt(argc, argv, "d:hi:m:uv")) != -1) {
+	while ((c = getopt(argc, argv, "d:hi:m:uvs:")) != -1) {
 		switch (c) {
 		case 'd':
 			datadir_add(optarg);
@@ -391,6 +406,10 @@
 			flags |= FLAG_M;
 			cmdarg = optarg;
 			break;
+		case 's':
+			flags |= FLAG_S;
+			sublevelarg = optarg;
+			break;
 		case 'u':
 			flags |= FLAG_U;
 			break;
@@ -414,7 +433,11 @@
 	c = flags & (FLAG_I | FLAG_M | FLAG_U);
 	switch (c) {
 		case FLAG_I:
-			error = do_cpuid(cmdarg, dev);
+			if (0 != (flags & FLAG_S)) {
+				error = do_cpuid(cmdarg, sublevelarg, dev);
+			} else {
+				error = do_cpuid(cmdarg, NULL, dev);
+			}
 			break;
 		case FLAG_M:
 			error = do_msr(cmdarg, dev);


More information about the freebsd-hackers mailing list