iPXE
cpuid_settings.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 #include <string.h>
00027 #include <errno.h>
00028 #include <byteswap.h>
00029 #include <ipxe/init.h>
00030 #include <ipxe/settings.h>
00031 #include <ipxe/cpuid.h>
00032 
00033 /** @file
00034  *
00035  * x86 CPUID settings
00036  *
00037  * CPUID settings are numerically encoded as:
00038  *
00039  *  Bit  31     Extended function
00040  *  Bits 30-24  (bit 22 = 1) Subfunction number
00041  *              (bit 22 = 0) Number of consecutive functions to call, minus one
00042  *  Bit  23     Return result as little-endian (used for strings)
00043  *  Bit  22     Interpret bits 30-24 as a subfunction number
00044  *  Bits 21-18  Unused
00045  *  Bits 17-16  Number of registers in register array, minus one
00046  *  Bits 15-8   Array of register indices.  First entry in array is in
00047  *              bits 9-8.  Indices are 0-%eax, 1-%ebx, 2-%ecx, 3-%edx.
00048  *  Bits 7-0    Starting function number (excluding "extended" bit)
00049  *
00050  * This encoding scheme is designed to allow the common case of
00051  * extracting a single register from a single function to be encoded
00052  * using "cpuid/<register>.<function>", e.g. "cpuid/2.0x80000001" to
00053  * retrieve the value of %ecx from calling CPUID with %eax=0x80000001.
00054  *
00055  * A subfunction (i.e. an input value for %ecx) may be specified using
00056  * "cpuid/<subfunction>.0x40.<register>.<function>".  This slightly
00057  * cumbersome syntax is required in order to maintain backwards
00058  * compatibility with older scripts.
00059  */
00060 
00061 /** CPUID setting tag register indices */
00062 enum cpuid_registers {
00063         CPUID_EAX = 0,
00064         CPUID_EBX = 1,
00065         CPUID_ECX = 2,
00066         CPUID_EDX = 3,
00067 };
00068 
00069 /** CPUID setting tag flags */
00070 enum cpuid_flags {
00071         CPUID_LITTLE_ENDIAN = 0x00800000UL,
00072         CPUID_USE_SUBFUNCTION = 0x00400000UL,
00073 };
00074 
00075 /**
00076  * Construct CPUID setting tag
00077  *
00078  * @v function          Starting function number
00079  * @v subfunction       Subfunction, or number of consecutive functions minus 1
00080  * @v flags             Flags
00081  * @v num_registers     Number of registers in register array
00082  * @v register1         First register in register array (or zero, if empty)
00083  * @v register2         Second register in register array (or zero, if empty)
00084  * @v register3         Third register in register array (or zero, if empty)
00085  * @v register4         Fourth register in register array (or zero, if empty)
00086  * @ret tag             Setting tag
00087  */
00088 #define CPUID_TAG( function, subfunction, flags, num_registers,         \
00089                    register1, register2, register3, register4 )         \
00090         ( (function) | ( (subfunction) << 24 ) | (flags) |              \
00091           ( ( (num_registers) - 1 ) << 16 ) |                           \
00092           ( (register1) << 8 ) | ( (register2) << 10 ) |                \
00093           ( (register3) << 12 ) | ( (register4) << 14 ) )
00094 
00095 /**
00096  * Extract starting function number from CPUID setting tag
00097  *
00098  * @v tag               Setting tag
00099  * @ret function        Starting function number
00100  */
00101 #define CPUID_FUNCTION( tag ) ( (tag) & 0x800000ffUL )
00102 
00103 /**
00104  * Extract subfunction number from CPUID setting tag
00105  *
00106  * @v tag               Setting tag
00107  * @ret subfunction     Subfunction number
00108  */
00109 #define CPUID_SUBFUNCTION( tag ) ( ( (tag) >> 24 ) & 0x7f )
00110 
00111 /**
00112  * Extract register array from CPUID setting tag
00113  *
00114  * @v tag               Setting tag
00115  * @ret registers       Register array
00116  */
00117 #define CPUID_REGISTERS( tag ) ( ( (tag) >> 8 ) & 0xff )
00118 
00119 /**
00120  * Extract number of registers from CPUID setting tag
00121  *
00122  * @v tag               Setting tag
00123  * @ret num_registers   Number of registers within register array
00124  */
00125 #define CPUID_NUM_REGISTERS( tag ) ( ( ( (tag) >> 16 ) & 0x3 ) + 1 )
00126 
00127 /** CPUID settings scope */
00128 static const struct settings_scope cpuid_settings_scope;
00129 
00130 /**
00131  * Check applicability of CPUID setting
00132  *
00133  * @v settings          Settings block
00134  * @v setting           Setting
00135  * @ret applies         Setting applies within this settings block
00136  */
00137 static int cpuid_settings_applies ( struct settings *settings __unused,
00138                                     const struct setting *setting ) {
00139 
00140         return ( setting->scope == &cpuid_settings_scope );
00141 }
00142 
00143 /**
00144  * Fetch value of CPUID setting
00145  *
00146  * @v settings          Settings block
00147  * @v setting           Setting to fetch
00148  * @v data              Buffer to fill with setting data
00149  * @v len               Length of buffer
00150  * @ret len             Length of setting data, or negative error
00151  */
00152 static int cpuid_settings_fetch ( struct settings *settings,
00153                                   struct setting *setting,
00154                                   void *data, size_t len ) {
00155         uint32_t function;
00156         uint32_t subfunction;
00157         uint32_t num_functions;
00158         uint32_t registers;
00159         uint32_t num_registers;
00160         uint32_t buf[4];
00161         uint32_t output;
00162         size_t frag_len;
00163         size_t result_len = 0;
00164         int rc;
00165 
00166         /* Call each function in turn */
00167         function = CPUID_FUNCTION ( setting->tag );
00168         subfunction = CPUID_SUBFUNCTION ( setting->tag );
00169         if ( setting->tag & CPUID_USE_SUBFUNCTION ) {
00170                 num_functions = 1;
00171         } else {
00172                 num_functions = ( subfunction + 1 );
00173                 subfunction = 0;
00174         }
00175         for ( ; num_functions-- ; function++ ) {
00176 
00177                 /* Fail if this function is not supported */
00178                 if ( ( rc = cpuid_supported ( function ) ) != 0 ) {
00179                         DBGC ( settings, "CPUID function %#08x not supported: "
00180                                "%s\n", function, strerror ( rc ) );
00181                         return rc;
00182                 }
00183 
00184                 /* Issue CPUID */
00185                 cpuid ( function, subfunction, &buf[CPUID_EAX],
00186                         &buf[CPUID_EBX], &buf[CPUID_ECX], &buf[CPUID_EDX] );
00187                 DBGC ( settings, "CPUID %#08x:%x => %#08x:%#08x:%#08x:%#08x\n",
00188                        function, subfunction, buf[0], buf[1], buf[2], buf[3] );
00189 
00190                 /* Copy results to buffer */
00191                 registers = CPUID_REGISTERS ( setting->tag );
00192                 num_registers = CPUID_NUM_REGISTERS ( setting->tag );
00193                 for ( ; num_registers-- ; registers >>= 2 ) {
00194                         output = buf[ registers & 0x3 ];
00195                         if ( ! ( setting->tag & CPUID_LITTLE_ENDIAN ) )
00196                                 output = cpu_to_be32 ( output );
00197                         frag_len = sizeof ( output );
00198                         if ( frag_len > len )
00199                                 frag_len = len;
00200                         memcpy ( data, &output, frag_len );
00201                         data += frag_len;
00202                         len -= frag_len;
00203                         result_len += sizeof ( output );
00204                 }
00205         }
00206 
00207         /* Set type if not already specified */
00208         if ( ! setting->type )
00209                 setting->type = &setting_type_hexraw;
00210 
00211         return result_len;
00212 }
00213 
00214 /** CPUID settings operations */
00215 static struct settings_operations cpuid_settings_operations = {
00216         .applies = cpuid_settings_applies,
00217         .fetch = cpuid_settings_fetch,
00218 };
00219 
00220 /** CPUID settings */
00221 static struct settings cpuid_settings = {
00222         .refcnt = NULL,
00223         .siblings = LIST_HEAD_INIT ( cpuid_settings.siblings ),
00224         .children = LIST_HEAD_INIT ( cpuid_settings.children ),
00225         .op = &cpuid_settings_operations,
00226         .default_scope = &cpuid_settings_scope,
00227 };
00228 
00229 /** Initialise CPUID settings */
00230 static void cpuid_settings_init ( void ) {
00231         int rc;
00232 
00233         if ( ( rc = register_settings ( &cpuid_settings, NULL,
00234                                         "cpuid" ) ) != 0 ) {
00235                 DBG ( "CPUID could not register settings: %s\n",
00236                       strerror ( rc ) );
00237                 return;
00238         }
00239 }
00240 
00241 /** CPUID settings initialiser */
00242 struct init_fn cpuid_settings_init_fn __init_fn ( INIT_NORMAL ) = {
00243         .initialise = cpuid_settings_init,
00244 };
00245 
00246 /** CPU vendor setting */
00247 const struct setting cpuvendor_setting __setting ( SETTING_HOST_EXTRA,
00248                                                    cpuvendor ) = {
00249         .name = "cpuvendor",
00250         .description = "CPU vendor",
00251         .tag = CPUID_TAG ( CPUID_VENDOR_ID, 0, CPUID_LITTLE_ENDIAN, 3,
00252                            CPUID_EBX, CPUID_EDX, CPUID_ECX, 0 ),
00253         .type = &setting_type_string,
00254         .scope = &cpuid_settings_scope,
00255 };
00256 
00257 /** CPU model setting */
00258 const struct setting cpumodel_setting __setting ( SETTING_HOST_EXTRA,
00259                                                   cpumodel ) = {
00260         .name = "cpumodel",
00261         .description = "CPU model",
00262         .tag = CPUID_TAG ( CPUID_MODEL, 2, CPUID_LITTLE_ENDIAN, 4,
00263                            CPUID_EAX, CPUID_EBX, CPUID_ECX, CPUID_EDX ),
00264         .type = &setting_type_string,
00265         .scope = &cpuid_settings_scope,
00266 };