Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / arch / x86 / hci / commands / cpuid_cmd.c
1 /*
2  * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  */
19
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 #include <stdint.h>
23 #include <stdio.h>
24 #include <errno.h>
25 #include <getopt.h>
26 #include <ipxe/cpuid.h>
27 #include <ipxe/command.h>
28 #include <ipxe/parseopt.h>
29
30 /** @file
31  *
32  * x86 CPU feature detection command
33  *
34  */
35
36 /** "cpuid" options */
37 struct cpuid_options {
38         /** Check AMD-defined features (%eax=0x80000001) */
39         int amd;
40         /** Check features defined via %ecx */
41         int ecx;
42 };
43
44 /** "cpuid" option list */
45 static struct option_descriptor cpuid_opts[] = {
46         OPTION_DESC ( "ext", 'e', no_argument,
47                       struct cpuid_options, amd, parse_flag ),
48         /* "--amd" retained for backwards compatibility */
49         OPTION_DESC ( "amd", 'a', no_argument,
50                       struct cpuid_options, amd, parse_flag ),
51         OPTION_DESC ( "ecx", 'c', no_argument,
52                       struct cpuid_options, ecx, parse_flag ),
53 };
54
55 /** "cpuid" command descriptor */
56 static struct command_descriptor cpuid_cmd =
57         COMMAND_DESC ( struct cpuid_options, cpuid_opts, 1, 1, "<bit>" );
58
59 /**
60  * The "cpuid" command
61  *
62  * @v argc              Argument count
63  * @v argv              Argument list
64  * @ret rc              Return status code
65  */
66 static int cpuid_exec ( int argc, char **argv ) {
67         struct cpuid_options opts;
68         struct x86_features features;
69         struct x86_feature_registers *feature_regs;
70         uint32_t feature_reg;
71         unsigned int bit;
72         int rc;
73
74         /* Parse options */
75         if ( ( rc = parse_options ( argc, argv, &cpuid_cmd, &opts ) ) != 0 )
76                 return rc;
77
78         /* Parse bit number */
79         if ( ( rc = parse_integer ( argv[optind], &bit ) ) != 0 )
80                 return rc;
81
82         /* Get CPU features */
83         x86_features ( &features );
84
85         /* Extract relevant feature register */
86         feature_regs = ( opts.amd ? &features.amd : &features.intel );
87         feature_reg = ( opts.ecx ? feature_regs->ecx : feature_regs->edx );
88
89         /* Check presence of specified feature */
90         return ( ( feature_reg & ( 1 << bit ) ) ? 0 : -ENOENT );
91 }
92
93 /** x86 CPU feature detection command */
94 struct command cpuid_command __command = {
95         .name = "cpuid",
96         .exec = cpuid_exec,
97 };