1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
|
/******************************************************************************
* Arch-specific sysctl.c
*
* System management operations. For use by node control stack.
*
* Copyright (c) 2002-2006, K Fraser
*/
#include <xen/types.h>
#include <xen/lib.h>
#include <xen/mm.h>
#include <xen/nospec.h>
#include <xen/guest_access.h>
#include <xen/hypercall.h>
#include <public/sysctl.h>
#include <xen/sched.h>
#include <xen/event.h>
#include <xen/domain_page.h>
#include <asm/msr.h>
#include <xen/trace.h>
#include <xen/console.h>
#include <xen/iocap.h>
#include <asm/irq.h>
#include <asm/hvm/hvm.h>
#include <asm/processor.h>
#include <asm/setup.h>
#include <asm/smp.h>
#include <asm/numa.h>
#include <xen/nodemask.h>
#include <xen/cpu.h>
#include <xsm/xsm.h>
#include <asm/psr.h>
#include <asm/cpu-policy.h>
struct l3_cache_info {
int ret;
unsigned long size;
};
static void cf_check l3_cache_get(void *arg)
{
struct cpuid4_info info;
struct l3_cache_info *l3_info = arg;
l3_info->ret = cpuid4_cache_lookup(3, &info);
if ( !l3_info->ret )
l3_info->size = info.size / 1024; /* in KB unit */
}
static long cf_check smt_up_down_helper(void *data)
{
bool up = (bool)data;
unsigned int cpu, sibling_mask = boot_cpu_data.x86_num_siblings - 1;
int ret = 0;
opt_smt = up;
for_each_present_cpu ( cpu )
{
/* Skip primary siblings (those whose thread id is 0). */
if ( !(x86_cpu_to_apicid[cpu] & sibling_mask) )
continue;
if ( !up && core_parking_remove(cpu) )
continue;
ret = up ? cpu_up_helper(_p(cpu))
: cpu_down_helper(_p(cpu));
if ( ret && ret != -EEXIST )
break;
/*
* Ensure forward progress by only considering preemption when we have
* changed the state of one or more cpus.
*/
if ( ret != -EEXIST && general_preempt_check() )
{
/* In tasklet context - can't create a contination. */
ret = -EBUSY;
break;
}
ret = 0; /* Avoid exiting with -EEXIST in the success case. */
}
if ( !ret )
printk(XENLOG_INFO "SMT %s - online CPUs 0x%*pb\n",
up ? "enabled" : "disabled", CPUMASK_PR(&cpu_online_map));
return ret;
}
void arch_do_physinfo(struct xen_sysctl_physinfo *pi)
{
memcpy(pi->hw_cap, boot_cpu_data.x86_capability,
min(sizeof(pi->hw_cap), sizeof(boot_cpu_data.x86_capability)));
if ( hvm_enabled )
pi->capabilities |= XEN_SYSCTL_PHYSCAP_hvm;
if ( IS_ENABLED(CONFIG_PV) )
pi->capabilities |= XEN_SYSCTL_PHYSCAP_pv;
if ( hvm_hap_supported() )
pi->capabilities |= XEN_SYSCTL_PHYSCAP_hap;
if ( IS_ENABLED(CONFIG_SHADOW_PAGING) )
pi->capabilities |= XEN_SYSCTL_PHYSCAP_shadow;
}
long arch_do_sysctl(
struct xen_sysctl *sysctl, XEN_GUEST_HANDLE_PARAM(xen_sysctl_t) u_sysctl)
{
long ret = 0;
switch ( sysctl->cmd )
{
case XEN_SYSCTL_cpu_hotplug:
{
unsigned int cpu = sysctl->u.cpu_hotplug.cpu;
unsigned int op = sysctl->u.cpu_hotplug.op;
bool plug;
long (*fn)(void *);
void *hcpu;
switch ( op )
{
case XEN_SYSCTL_CPU_HOTPLUG_ONLINE:
plug = true;
fn = cpu_up_helper;
hcpu = _p(cpu);
break;
case XEN_SYSCTL_CPU_HOTPLUG_OFFLINE:
plug = false;
fn = cpu_down_helper;
hcpu = _p(cpu);
break;
case XEN_SYSCTL_CPU_HOTPLUG_SMT_ENABLE:
case XEN_SYSCTL_CPU_HOTPLUG_SMT_DISABLE:
if ( !cpu_has_htt || boot_cpu_data.x86_num_siblings < 2 )
{
ret = -EOPNOTSUPP;
break;
}
if ( sched_disable_smt_switching )
{
ret = -EBUSY;
break;
}
if ( CONFIG_NR_CPUS <= 1 )
/* Mimic behavior of smt_up_down_helper(). */
return 0;
plug = op == XEN_SYSCTL_CPU_HOTPLUG_SMT_ENABLE;
fn = smt_up_down_helper;
hcpu = _p(plug);
break;
default:
ret = -EOPNOTSUPP;
break;
}
if ( !ret )
ret = plug ? xsm_resource_plug_core(XSM_HOOK)
: xsm_resource_unplug_core(XSM_HOOK);
if ( !ret )
ret = continue_hypercall_on_cpu(0, fn, hcpu);
}
break;
case XEN_SYSCTL_psr_cmt_op:
if ( !psr_cmt_enabled() )
return -ENODEV;
if ( sysctl->u.psr_cmt_op.flags != 0 )
return -EINVAL;
switch ( sysctl->u.psr_cmt_op.cmd )
{
case XEN_SYSCTL_PSR_CMT_enabled:
sysctl->u.psr_cmt_op.u.data =
(psr_cmt->features & PSR_RESOURCE_TYPE_L3) &&
(psr_cmt->l3.features & PSR_CMT_L3_OCCUPANCY);
break;
case XEN_SYSCTL_PSR_CMT_get_total_rmid:
sysctl->u.psr_cmt_op.u.data = psr_cmt->rmid_max;
break;
case XEN_SYSCTL_PSR_CMT_get_l3_upscaling_factor:
sysctl->u.psr_cmt_op.u.data = psr_cmt->l3.upscaling_factor;
break;
case XEN_SYSCTL_PSR_CMT_get_l3_cache_size:
{
struct l3_cache_info info;
unsigned int cpu = sysctl->u.psr_cmt_op.u.l3_cache.cpu;
if ( (cpu >= nr_cpu_ids) || !cpu_online(cpu) )
{
ret = -ENODEV;
sysctl->u.psr_cmt_op.u.data = 0;
break;
}
if ( cpu == smp_processor_id() )
l3_cache_get(&info);
else
on_selected_cpus(cpumask_of(cpu), l3_cache_get, &info, 1);
ret = info.ret;
sysctl->u.psr_cmt_op.u.data = (ret ? 0 : info.size);
break;
}
case XEN_SYSCTL_PSR_CMT_get_l3_event_mask:
sysctl->u.psr_cmt_op.u.data = psr_cmt->l3.features;
break;
default:
sysctl->u.psr_cmt_op.u.data = 0;
ret = -ENOSYS;
break;
}
if ( __copy_to_guest(u_sysctl, sysctl, 1) )
ret = -EFAULT;
break;
case XEN_SYSCTL_psr_alloc:
{
uint32_t data[PSR_INFO_ARRAY_SIZE] = { };
switch ( sysctl->u.psr_alloc.cmd )
{
case XEN_SYSCTL_PSR_get_l3_info:
ret = psr_get_info(sysctl->u.psr_alloc.target,
PSR_TYPE_L3_CBM, data, ARRAY_SIZE(data));
if ( ret )
break;
sysctl->u.psr_alloc.u.cat_info.cos_max =
data[PSR_INFO_IDX_COS_MAX];
sysctl->u.psr_alloc.u.cat_info.cbm_len =
data[PSR_INFO_IDX_CAT_CBM_LEN];
sysctl->u.psr_alloc.u.cat_info.flags =
data[PSR_INFO_IDX_CAT_FLAGS];
if ( __copy_field_to_guest(u_sysctl, sysctl, u.psr_alloc) )
ret = -EFAULT;
break;
case XEN_SYSCTL_PSR_get_l2_info:
ret = psr_get_info(sysctl->u.psr_alloc.target,
PSR_TYPE_L2_CBM, data, ARRAY_SIZE(data));
if ( ret )
break;
sysctl->u.psr_alloc.u.cat_info.cos_max =
data[PSR_INFO_IDX_COS_MAX];
sysctl->u.psr_alloc.u.cat_info.cbm_len =
data[PSR_INFO_IDX_CAT_CBM_LEN];
sysctl->u.psr_alloc.u.cat_info.flags =
data[PSR_INFO_IDX_CAT_FLAGS];
if ( __copy_field_to_guest(u_sysctl, sysctl, u.psr_alloc) )
ret = -EFAULT;
break;
case XEN_SYSCTL_PSR_get_mba_info:
ret = psr_get_info(sysctl->u.psr_alloc.target,
PSR_TYPE_MBA_THRTL, data, ARRAY_SIZE(data));
if ( ret )
break;
sysctl->u.psr_alloc.u.mba_info.cos_max =
data[PSR_INFO_IDX_COS_MAX];
sysctl->u.psr_alloc.u.mba_info.thrtl_max =
data[PSR_INFO_IDX_MBA_THRTL_MAX];
sysctl->u.psr_alloc.u.mba_info.flags =
data[PSR_INFO_IDX_MBA_FLAGS];
if ( __copy_field_to_guest(u_sysctl, sysctl, u.psr_alloc) )
ret = -EFAULT;
break;
default:
ret = -EOPNOTSUPP;
break;
}
break;
}
case XEN_SYSCTL_get_cpu_levelling_caps:
sysctl->u.cpu_levelling_caps.caps = levelling_caps;
if ( __copy_field_to_guest(u_sysctl, sysctl, u.cpu_levelling_caps.caps) )
ret = -EFAULT;
break;
case XEN_SYSCTL_get_cpu_featureset:
{
static const struct cpu_policy *const policy_table[6] = {
[XEN_SYSCTL_cpu_featureset_raw] = &raw_cpu_policy,
[XEN_SYSCTL_cpu_featureset_host] = &host_cpu_policy,
#ifdef CONFIG_PV
[XEN_SYSCTL_cpu_featureset_pv] = &pv_def_cpu_policy,
[XEN_SYSCTL_cpu_featureset_pv_max] = &pv_max_cpu_policy,
#endif
#ifdef CONFIG_HVM
[XEN_SYSCTL_cpu_featureset_hvm] = &hvm_def_cpu_policy,
[XEN_SYSCTL_cpu_featureset_hvm_max] = &hvm_max_cpu_policy,
#endif
};
const struct cpu_policy *p = NULL;
uint32_t featureset[FSCAPINTS];
unsigned int nr;
/* Request for maximum number of features? */
if ( guest_handle_is_null(sysctl->u.cpu_featureset.features) )
{
sysctl->u.cpu_featureset.nr_features = FSCAPINTS;
if ( __copy_field_to_guest(u_sysctl, sysctl,
u.cpu_featureset.nr_features) )
ret = -EFAULT;
break;
}
/* Clip the number of entries. */
nr = min_t(unsigned int, sysctl->u.cpu_featureset.nr_features,
FSCAPINTS);
/* Look up requested featureset. */
if ( sysctl->u.cpu_featureset.index < ARRAY_SIZE(policy_table) )
{
p = policy_table[sysctl->u.cpu_featureset.index];
if ( !p )
ret = -EOPNOTSUPP;
}
else
/* Bad featureset index? */
ret = -EINVAL;
if ( !ret )
x86_cpu_policy_to_featureset(p, featureset);
/* Copy the requested featureset into place. */
if ( !ret && copy_to_guest(sysctl->u.cpu_featureset.features,
featureset, nr) )
ret = -EFAULT;
/* Inform the caller of how many features we wrote. */
sysctl->u.cpu_featureset.nr_features = nr;
if ( !ret && __copy_field_to_guest(u_sysctl, sysctl,
u.cpu_featureset.nr_features) )
ret = -EFAULT;
/* Inform the caller if there was more data to provide. */
if ( !ret && nr < FSCAPINTS )
ret = -ENOBUFS;
break;
}
case XEN_SYSCTL_get_cpu_policy:
{
static const struct cpu_policy *const system_policies[6] = {
[XEN_SYSCTL_cpu_policy_raw] = &raw_cpu_policy,
[XEN_SYSCTL_cpu_policy_host] = &host_cpu_policy,
#ifdef CONFIG_PV
[XEN_SYSCTL_cpu_policy_pv_max] = &pv_max_cpu_policy,
[XEN_SYSCTL_cpu_policy_pv_default] = &pv_def_cpu_policy,
#endif
#ifdef CONFIG_HVM
[XEN_SYSCTL_cpu_policy_hvm_max] = &hvm_max_cpu_policy,
[XEN_SYSCTL_cpu_policy_hvm_default] = &hvm_def_cpu_policy,
#endif
};
const struct cpu_policy *policy;
/* Reserved field set, or bad policy index? */
if ( sysctl->u.cpu_policy._rsvd ||
sysctl->u.cpu_policy.index >= ARRAY_SIZE(system_policies) )
{
ret = -EINVAL;
break;
}
policy = system_policies[
array_index_nospec(sysctl->u.cpu_policy.index,
ARRAY_SIZE(system_policies))];
if ( !policy )
{
ret = -EOPNOTSUPP;
break;
}
/* Process the CPUID leaves. */
if ( guest_handle_is_null(sysctl->u.cpu_policy.leaves) )
sysctl->u.cpu_policy.nr_leaves = CPUID_MAX_SERIALISED_LEAVES;
else if ( (ret = x86_cpuid_copy_to_buffer(
policy,
sysctl->u.cpu_policy.leaves,
&sysctl->u.cpu_policy.nr_leaves)) )
break;
if ( __copy_field_to_guest(u_sysctl, sysctl,
u.cpu_policy.nr_leaves) )
{
ret = -EFAULT;
break;
}
/* Process the MSR entries. */
if ( guest_handle_is_null(sysctl->u.cpu_policy.msrs) )
sysctl->u.cpu_policy.nr_msrs = MSR_MAX_SERIALISED_ENTRIES;
else if ( (ret = x86_msr_copy_to_buffer(
policy,
sysctl->u.cpu_policy.msrs,
&sysctl->u.cpu_policy.nr_msrs)) )
break;
if ( __copy_field_to_guest(u_sysctl, sysctl,
u.cpu_policy.nr_msrs) )
ret = -EFAULT;
break;
}
default:
ret = -ENOSYS;
break;
}
return ret;
}
/*
* Local variables:
* mode: C
* c-file-style: "BSD"
* c-basic-offset: 4
* tab-width: 4
* indent-tabs-mode: nil
* End:
*/
|