Home | History | Annotate | Download | only in patches
      1 From 397690adc6271011fd191c3b1fbb888e0726a1df Mon Sep 17 00:00:00 2001
      2 From: Mike Frysinger <vapier (a] gentoo.org>
      3 Date: Thu, 8 Mar 2012 18:34:38 -0500
      4 Subject: [PATCH] cpuid: add common header
      5 
      6 Add a helper function for people to execute the cpuid asm code.
      7 This takes care of all the ugly asm issues (such as PIC/ebx).
      8 
      9 Signed-off-by: Mike Frysinger <vapier (a] gentoo.org>
     10 ---
     11  include/ltp_cpuid.h                                |   31 ++++++++++++++++++++
     12  .../sched/hyperthreading/ht_affinity/ht_utils.c    |   17 +---------
     13  .../sched/hyperthreading/ht_enabled/ht_utils.c     |   17 +---------
     14  .../sched/hyperthreading/ht_interrupt/ht_utils.c   |   17 +---------
     15  4 files changed, 37 insertions(+), 45 deletions(-)
     16  create mode 100644 include/ltp_cpuid.h
     17 
     18 diff --git a/include/ltp_cpuid.h b/include/ltp_cpuid.h
     19 new file mode 100644
     20 index 0000000..9052e29
     21 --- /dev/null
     22 +++ b/include/ltp_cpuid.h
     23 @@ -0,0 +1,31 @@
     24 +/*
     25 + * Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
     26 + *
     27 + * Licensed under the BSD 3-clause.
     28 + */
     29 +
     30 +#ifndef __LTP_CPUID_H__
     31 +#define __LTP_CPUID_H__
     32 +
     33 +static inline void cpuid(unsigned int info, unsigned int *eax, unsigned int *ebx,
     34 +                         unsigned int *ecx, unsigned int *edx)
     35 +{
     36 +#if defined(__i386__) || defined(__x86_64__)
     37 +	unsigned int _eax = info, _ebx, _ecx, _edx;
     38 +	asm volatile(
     39 +		"mov %%ebx, %%edi;" // save ebx (for PIC)
     40 +		"cpuid;"
     41 +		"mov %%ebx, %%esi;" // pass to caller
     42 +		"mov %%edi, %%ebx;" // restore ebx
     43 +		: "+a" (_eax), "=S" (_ebx), "=c" (_ecx), "=d" (_edx)
     44 +		:       /* inputs: eax is handled above */
     45 +		: "edi" /* clobbers: we hit edi directly */
     46 +	);
     47 +	if (eax) *eax = _eax;
     48 +	if (ebx) *ebx = _ebx;
     49 +	if (ecx) *ecx = _ecx;
     50 +	if (edx) *edx = _edx;
     51 +#endif
     52 +}
     53 +
     54 +#endif
     55 diff --git a/testcases/kernel/sched/hyperthreading/ht_affinity/ht_utils.c b/testcases/kernel/sched/hyperthreading/ht_affinity/ht_utils.c
     56 index 2f9b841..54167dd 100644
     57 --- a/testcases/kernel/sched/hyperthreading/ht_affinity/ht_utils.c
     58 +++ b/testcases/kernel/sched/hyperthreading/ht_affinity/ht_utils.c
     59 @@ -6,6 +6,7 @@
     60  #include <alloca.h>
     61  #include <string.h>
     62  #include <linux/unistd.h>
     63 +#include "ltp_cpuid.h"
     64  
     65  #define PROC_PATH	"/proc"
     66  #define BUFF_SIZE	8192
     67 @@ -15,27 +16,13 @@
     68  
     69  char buffer[BUFF_SIZE];
     70  
     71 -inline void cpuid(int op, int *eax, int *ebx, int *ecx, int *edx)
     72 -{
     73 -#if (!defined __i386__ && !defined __x86_64__)
     74 -	return;
     75 -#else
     76 -	__asm__("cpuid"
     77 -		: "=a" (*eax),
     78 -		  "=b" (*ebx),
     79 -		  "=c" (*ecx),
     80 -		  "=d" (*edx)
     81 -		: "0" (op));
     82 -#endif
     83 -}
     84 -
     85  int is_ht_cpu()
     86  {
     87  	/*Number of logic processor in a physical processor*/
     88  	int smp_num_siblings = -1;
     89  	/*ht flag*/
     90  	int ht = -1;
     91 -	int eax,ebx,ecx,edx;
     92 +	unsigned int eax,ebx,ecx,edx;
     93  	cpuid(1,&eax,&ebx,&ecx,&edx);
     94  	smp_num_siblings = (ebx&0xff0000) >> 16;
     95  	ht = (edx&0x10000000) >> 28;
     96 diff --git a/testcases/kernel/sched/hyperthreading/ht_enabled/ht_utils.c b/testcases/kernel/sched/hyperthreading/ht_enabled/ht_utils.c
     97 index 3cb1e54..fdb545a 100644
     98 --- a/testcases/kernel/sched/hyperthreading/ht_enabled/ht_utils.c
     99 +++ b/testcases/kernel/sched/hyperthreading/ht_enabled/ht_utils.c
    100 @@ -6,6 +6,7 @@
    101  #include <alloca.h>
    102  #include <string.h>
    103  #include <linux/unistd.h>
    104 +#include "ltp_cpuid.h"
    105  
    106  #define PROC_PATH	"/proc"
    107  #define BUFF_SIZE	8192
    108 @@ -18,27 +19,13 @@
    109  
    110  char buffer[BUFF_SIZE];
    111  
    112 -inline void cpuid(int op, int *eax, int *ebx, int *ecx, int *edx)
    113 -{
    114 -#if (!defined __i386__ && !defined __x86_64__)
    115 -	return;
    116 -#else
    117 -	__asm__("cpuid"
    118 -		: "=a" (*eax),
    119 -		  "=b" (*ebx),
    120 -		  "=c" (*ecx),
    121 -		  "=d" (*edx)
    122 -		: "0" (op));
    123 -#endif
    124 -}
    125 -
    126  int is_ht_cpu()
    127  {
    128  	/*Number of logic processor in a physical processor*/
    129  	int smp_num_siblings = -1;
    130  	/*ht flag*/
    131  	int ht = -1;
    132 -	int eax,ebx,ecx,edx;
    133 +	unsigned int eax,ebx,ecx,edx;
    134  	cpuid(1,&eax,&ebx,&ecx,&edx);
    135  	smp_num_siblings = (ebx&0xff0000) >> 16;
    136  	ht = (edx&0x10000000) >> 28;
    137 diff --git a/testcases/kernel/sched/hyperthreading/ht_interrupt/ht_utils.c b/testcases/kernel/sched/hyperthreading/ht_interrupt/ht_utils.c
    138 index d8bbdab..e829b31 100644
    139 --- a/testcases/kernel/sched/hyperthreading/ht_interrupt/ht_utils.c
    140 +++ b/testcases/kernel/sched/hyperthreading/ht_interrupt/ht_utils.c
    141 @@ -6,6 +6,7 @@
    142  #include <alloca.h>
    143  #include <string.h>
    144  #include <linux/unistd.h>
    145 +#include "ltp_cpuid.h"
    146  
    147  #define PROC_PATH	"/proc"
    148  #define BUFF_SIZE	8192
    149 @@ -58,27 +59,13 @@ int is_ht_kernel()
    150  	return 0;
    151  }
    152  
    153 -inline void cpuid(int op, int *eax, int *ebx, int *ecx, int *edx)
    154 -{
    155 -#if (!defined __i386__ && !defined __x86_64__)
    156 -	return;
    157 -#else
    158 -	__asm__("cpuid"
    159 -		: "=a" (*eax),
    160 -		  "=b" (*ebx),
    161 -		  "=c" (*ecx),
    162 -		  "=d" (*edx)
    163 -		: "0" (op));
    164 -#endif
    165 -}
    166 -
    167  int is_ht_cpu()
    168  {
    169  	/*Number of logic processor in a physical processor*/
    170  	int smp_num_siblings = -1;
    171  	/*ht flag*/
    172  	int ht = -1;
    173 -	int eax,ebx,ecx,edx;
    174 +	unsigned int eax,ebx,ecx,edx;
    175  	cpuid(1,&eax,&ebx,&ecx,&edx);
    176  	smp_num_siblings = (ebx&0xff0000) >> 16;
    177  	ht = (edx&0x10000000) >> 28;
    178 -- 
    179 1.7.8.4
    180 
    181