Browse Source

cpuid: avoid unions in parsing data

This is a much more cleaner way to do it and _should_ be easier for people
to use.

Signed-off-by: Ahmed Samy <f.fallen45@gmail.com>
Ahmed Samy 12 years ago
parent
commit
d03f0db15b
3 changed files with 19 additions and 28 deletions
  1. 5 2
      ccan/cpuid/cpuid.c
  2. 7 1
      ccan/cpuid/cpuid.h
  3. 7 25
      ccan/cpuid/test/run.c

+ 5 - 2
ccan/cpuid/cpuid.c

@@ -298,13 +298,16 @@ void cpuid(cpuid_t info, uint32_t *buf)
 			buf[3] = edx;
 			buf[3] = edx;
 			break;
 			break;
 		case CPU_EXTENDED_L2_CACHE_FEATURES:
 		case CPU_EXTENDED_L2_CACHE_FEATURES:
-			*buf = ecx;
+			buf[0] = ecx & 0xFF; 		/* Line size.  */
+			buf[1] = (ecx >> 12) & 0xFF; 	/* Associativity.  */
+			buf[2] = ecx >> 16; 		/* Cache size.  */
 			break;
 			break;
 		case CPU_ADV_POWER_MGT_INFO:
 		case CPU_ADV_POWER_MGT_INFO:
 			*buf = edx;
 			*buf = edx;
 			break;
 			break;
 		case CPU_VIRT_PHYS_ADDR_SIZES:
 		case CPU_VIRT_PHYS_ADDR_SIZES:
-			*buf = eax;
+			buf[0] = eax & 0xFF; 		/* physical.  */
+			buf[1] = (eax >> 8) & 0xFF; 	/* virtual.  */
 			break;
 			break;
 		default:
 		default:
 			*buf = 0xbaadf00d;
 			*buf = 0xbaadf00d;

+ 7 - 1
ccan/cpuid/cpuid.h

@@ -205,8 +205,14 @@ uint32_t cpuid_highest_ext_func_supported(void);
  * For CPU_EXTENDED_PROC_INFO_FEATURE_BITS:
  * For CPU_EXTENDED_PROC_INFO_FEATURE_BITS:
  * 	Returns them in buf[0] and buf[1].
  * 	Returns them in buf[0] and buf[1].
  *
  *
+ * For CPU_EXTENDED_L2_CACHE_FEATURES:
+ * 	buf[0]: Line size
+ * 	buf[1]: Associativity
+ * 	buf[2]: Cache size.
+ *
  * For CPU_VIRT_PHYS_ADDR_SIZES:
  * For CPU_VIRT_PHYS_ADDR_SIZES:
- * 	Returns it as an integer in *buf.
+ * 	buf[0]: Physical
+ * 	buf[1]: Virtual
  *
  *
  * For CPU_PROC_BRAND_STRING:
  * For CPU_PROC_BRAND_STRING:
  * 	Have a char array with at least 48 bytes assigned to it.
  * 	Have a char array with at least 48 bytes assigned to it.

+ 7 - 25
ccan/cpuid/test/run.c

@@ -18,39 +18,21 @@ int main(void)
 
 
 	printf ("Highest extended function supported: %#010x\n", cpuid_highest_ext_func_supported());
 	printf ("Highest extended function supported: %#010x\n", cpuid_highest_ext_func_supported());
 
 
-	union {
-		struct {
-			uint32_t phys_bits : 8;
-			uint32_t virt_bits : 8;
-			uint32_t reserved  : 16;
-		};
-		uint32_t w;
-	} s;
-	cpuid(CPU_VIRT_PHYS_ADDR_SIZES, &s.w);
-	printf ("Physical address size: %d\nVirtual address size: %d\n", s.phys_bits, s.virt_bits);
+	uint32_t phys_virt[2];
+	cpuid(CPU_VIRT_PHYS_ADDR_SIZES, phys_virt);
+	printf ("Physical address size: %d\nVirtual address size: %d\n", phys_virt[0], phys_virt[1]);
 
 
 	uint32_t extfeatures[2];
 	uint32_t extfeatures[2];
 	cpuid(CPU_EXTENDED_PROC_INFO_FEATURE_BITS, extfeatures);
 	cpuid(CPU_EXTENDED_PROC_INFO_FEATURE_BITS, extfeatures);
 	printf ("Extended processor info and feature bits: %d %d\n", extfeatures[0], extfeatures[1]);
 	printf ("Extended processor info and feature bits: %d %d\n", extfeatures[0], extfeatures[1]);
 
 
-	union {
-		struct {
-			uint32_t line_size : 8;
-			uint32_t reserved : 4;
-			uint32_t assoc : 4;
-			uint32_t cache_size : 16;
-		};
-
-		uint32_t w;
-	} l2c;
-
-	cpuid(CPU_EXTENDED_L2_CACHE_FEATURES, &l2c.w);
-	printf ("L2 Cache Size: %u KB\tLine Size: %u bytes\tAssociativity: %02xh\n",
-			l2c.cache_size, l2c.line_size, l2c.assoc);
+	uint32_t l2c[3];
+	cpuid(CPU_EXTENDED_L2_CACHE_FEATURES, l2c);
+	printf("L2 Line size: %u bytes\tAssociativity: %02xh\tCache Size: %u KB\n",
+		l2c[0], l2c[1], l2c[2]);
 
 
 	uint32_t invalid;
 	uint32_t invalid;
 	cpuid(0x0ffffffUL, &invalid);
 	cpuid(0x0ffffffUL, &invalid);
 	printf ("Testing invalid: %#010x\n", invalid);
 	printf ("Testing invalid: %#010x\n", invalid);
 	return 0;
 	return 0;
 }
 }
-