1 /*
2  * cpu_conf.h: CPU XML handling
3  *
4  * Copyright (C) 2009-2011, 2013, 2014 Red Hat, Inc.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library.  If not, see
18  * <http://www.gnu.org/licenses/>.
19  */
20 
21 #pragma once
22 
23 #include "virbuffer.h"
24 #include "virxml.h"
25 #include "virbitmap.h"
26 #include "virarch.h"
27 #include "numa_conf.h"
28 #include "virenum.h"
29 #include "virhostcpu.h"
30 
31 #define VIR_CPU_VENDOR_ID_LENGTH 12
32 
33 typedef enum {
34     VIR_CPU_TYPE_HOST,
35     VIR_CPU_TYPE_GUEST,
36     VIR_CPU_TYPE_AUTO,
37 
38     VIR_CPU_TYPE_LAST
39 } virCPUType;
40 
41 VIR_ENUM_DECL(virCPU);
42 
43 typedef enum {
44     VIR_CPU_MODE_CUSTOM,
45     VIR_CPU_MODE_HOST_MODEL,
46     VIR_CPU_MODE_HOST_PASSTHROUGH,
47     VIR_CPU_MODE_MAXIMUM,
48 
49     VIR_CPU_MODE_LAST
50 } virCPUMode;
51 
52 VIR_ENUM_DECL(virCPUMode);
53 
54 typedef enum {
55     VIR_CPU_MATCH_EXACT,
56     VIR_CPU_MATCH_MINIMUM,
57     VIR_CPU_MATCH_STRICT,
58 
59     VIR_CPU_MATCH_LAST
60 } virCPUMatch;
61 
62 VIR_ENUM_DECL(virCPUMatch);
63 
64 typedef enum {
65     VIR_CPU_CHECK_DEFAULT,
66     VIR_CPU_CHECK_NONE,
67     VIR_CPU_CHECK_PARTIAL,
68     VIR_CPU_CHECK_FULL,
69 
70     VIR_CPU_CHECK_LAST
71 } virCPUCheck;
72 
73 VIR_ENUM_DECL(virCPUCheck);
74 
75 typedef enum {
76     VIR_CPU_FALLBACK_ALLOW,
77     VIR_CPU_FALLBACK_FORBID,
78 
79     VIR_CPU_FALLBACK_LAST
80 } virCPUFallback;
81 
82 VIR_ENUM_DECL(virCPUFallback);
83 
84 typedef enum {
85     VIR_CPU_FEATURE_FORCE,
86     VIR_CPU_FEATURE_REQUIRE,
87     VIR_CPU_FEATURE_OPTIONAL,
88     VIR_CPU_FEATURE_DISABLE,
89     VIR_CPU_FEATURE_FORBID,
90 
91     VIR_CPU_FEATURE_LAST
92 } virCPUFeaturePolicy;
93 
94 VIR_ENUM_DECL(virCPUFeaturePolicy);
95 
96 typedef struct _virCPUFeatureDef virCPUFeatureDef;
97 struct _virCPUFeatureDef {
98     char *name;
99     int policy;         /* enum virCPUFeaturePolicy */
100 };
101 
102 
103 typedef enum {
104     VIR_CPU_CACHE_MODE_EMULATE,
105     VIR_CPU_CACHE_MODE_PASSTHROUGH,
106     VIR_CPU_CACHE_MODE_DISABLE,
107 
108     VIR_CPU_CACHE_MODE_LAST
109 } virCPUCacheMode;
110 
111 VIR_ENUM_DECL(virCPUCacheMode);
112 
113 typedef struct _virCPUCacheDef virCPUCacheDef;
114 struct _virCPUCacheDef {
115     int level;          /* -1 for unspecified */
116     virCPUCacheMode mode;
117 };
118 
119 
120 typedef struct _virCPUDef virCPUDef;
121 struct _virCPUDef {
122     int refs;
123     int type;           /* enum virCPUType */
124     int mode;           /* enum virCPUMode */
125     int match;          /* enum virCPUMatch */
126     virCPUCheck check;
127     virArch arch;
128     char *model;
129     char *vendor_id;    /* vendor id returned by CPUID in the guest */
130     int fallback;       /* enum virCPUFallback */
131     char *vendor;
132     unsigned int microcodeVersion;
133     unsigned int sockets;
134     unsigned int dies;
135     unsigned int cores;
136     unsigned int threads;
137     size_t nfeatures;
138     size_t nfeatures_max;
139     virCPUFeatureDef *features;
140     virCPUCacheDef *cache;
141     virHostCPUTscInfo *tsc;
142     virTristateSwitch migratable; /* for host-passthrough mode */
143 };
144 
145 virCPUDef *virCPUDefNew(void);
146 
147 void ATTRIBUTE_NONNULL(1)
148 virCPUDefFreeFeatures(virCPUDef *def);
149 
150 void ATTRIBUTE_NONNULL(1)
151 virCPUDefFreeModel(virCPUDef *def);
152 
153 void
154 virCPUDefRef(virCPUDef *def);
155 void
156 virCPUDefFree(virCPUDef *def);
157 G_DEFINE_AUTOPTR_CLEANUP_FUNC(virCPUDef, virCPUDefFree);
158 
159 int ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2)
160 virCPUDefCopyModel(virCPUDef *dst,
161                    const virCPUDef *src,
162                    bool resetPolicy);
163 
164 /*
165  * Returns true if feature @name should copied, false otherwise.
166  */
167 typedef bool (*virCPUDefFeatureFilter)(const char *name,
168                                        virCPUFeaturePolicy policy,
169                                        void *opaque);
170 
171 int
172 virCPUDefCopyModelFilter(virCPUDef *dst,
173                          const virCPUDef *src,
174                          bool resetPolicy,
175                          virCPUDefFeatureFilter filter,
176                          void *opaque)
177     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
178 
179 void
180 virCPUDefStealModel(virCPUDef *dst,
181                     virCPUDef *src,
182                     bool keepVendor);
183 
184 virCPUDef *
185 virCPUDefCopy(const virCPUDef *cpu);
186 
187 virCPUDef *
188 virCPUDefCopyWithoutModel(const virCPUDef *cpu);
189 
190 int
191 virCPUDefParseXMLString(const char *xml,
192                         virCPUType type,
193                         virCPUDef **cpu,
194                         bool validateXML);
195 
196 int
197 virCPUDefParseXML(xmlXPathContextPtr ctxt,
198                   const char *xpath,
199                   virCPUType mode,
200                   virCPUDef **cpu,
201                   bool validateXML);
202 
203 bool
204 virCPUDefIsEqual(virCPUDef *src,
205                  virCPUDef *dst,
206                  bool reportError);
207 
208 char *
209 virCPUDefFormat(virCPUDef *def,
210                 virDomainNuma *numa);
211 
212 int
213 virCPUDefFormatBuf(virBuffer *buf,
214                    virCPUDef *def);
215 int
216 virCPUDefFormatBufFull(virBuffer *buf,
217                        virCPUDef *def,
218                        virDomainNuma *numa);
219 
220 int
221 virCPUDefAddFeature(virCPUDef *cpu,
222                     const char *name,
223                     int policy);
224 
225 int
226 virCPUDefUpdateFeature(virCPUDef *cpu,
227                        const char *name,
228                        int policy);
229 
230 int
231 virCPUDefAddFeatureIfMissing(virCPUDef *def,
232                              const char *name,
233                              int policy);
234 
235 virCPUFeatureDef *
236 virCPUDefFindFeature(const virCPUDef *def,
237                      const char *name);
238 
239 int
240 virCPUDefFilterFeatures(virCPUDef *cpu,
241                         virCPUDefFeatureFilter filter,
242                         void *opaque);
243 
244 int
245 virCPUDefCheckFeatures(virCPUDef *cpu,
246                        virCPUDefFeatureFilter filter,
247                        void *opaque,
248                        char ***features);
249 
250 virCPUDef **
251 virCPUDefListParse(const char **xmlCPUs,
252                    unsigned int ncpus,
253                    virCPUType cpuType);
254 void
255 virCPUDefListFree(virCPUDef **cpus);
256