Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/arch/x86/xen/smp_pv.c
52542 views
1
// SPDX-License-Identifier: GPL-2.0
2
/*
3
* Xen SMP support
4
*
5
* This file implements the Xen versions of smp_ops. SMP under Xen is
6
* very straightforward. Bringing a CPU up is simply a matter of
7
* loading its initial context and setting it running.
8
*
9
* IPIs are handled through the Xen event mechanism.
10
*
11
* Because virtual CPUs can be scheduled onto any real CPU, there's no
12
* useful topology information for the kernel to make use of. As a
13
* result, all CPUs are treated as if they're single-core and
14
* single-threaded.
15
*/
16
#include <linux/sched.h>
17
#include <linux/sched/task_stack.h>
18
#include <linux/err.h>
19
#include <linux/slab.h>
20
#include <linux/smp.h>
21
#include <linux/irq_work.h>
22
#include <linux/tick.h>
23
#include <linux/nmi.h>
24
#include <linux/cpuhotplug.h>
25
#include <linux/stackprotector.h>
26
#include <linux/pgtable.h>
27
28
#include <asm/paravirt.h>
29
#include <asm/idtentry.h>
30
#include <asm/desc.h>
31
#include <asm/cpu.h>
32
#include <asm/apic.h>
33
#include <asm/io_apic.h>
34
35
#include <xen/interface/xen.h>
36
#include <xen/interface/vcpu.h>
37
#include <xen/interface/xenpmu.h>
38
39
#include <asm/spec-ctrl.h>
40
#include <asm/xen/interface.h>
41
#include <asm/xen/hypercall.h>
42
43
#include <xen/xen.h>
44
#include <xen/page.h>
45
#include <xen/events.h>
46
47
#include <xen/hvc-console.h>
48
#include "xen-ops.h"
49
50
cpumask_var_t xen_cpu_initialized_map;
51
52
static DEFINE_PER_CPU(struct xen_common_irq, xen_irq_work) = { .irq = -1 };
53
static DEFINE_PER_CPU(struct xen_common_irq, xen_pmu_irq) = { .irq = -1 };
54
55
static irqreturn_t xen_irq_work_interrupt(int irq, void *dev_id);
56
57
static void cpu_bringup(void)
58
{
59
int cpu;
60
61
cr4_init();
62
cpuhp_ap_sync_alive();
63
cpu_init();
64
fpu__init_cpu();
65
touch_softlockup_watchdog();
66
67
/* PVH runs in ring 0 and allows us to do native syscalls. Yay! */
68
if (!xen_feature(XENFEAT_supervisor_mode_kernel))
69
xen_enable_syscall();
70
71
cpu = smp_processor_id();
72
identify_secondary_cpu(cpu);
73
set_cpu_sibling_map(cpu);
74
75
speculative_store_bypass_ht_init();
76
77
xen_setup_cpu_clockevents();
78
79
notify_cpu_starting(cpu);
80
81
set_cpu_online(cpu, true);
82
83
smp_mb();
84
85
/* We can take interrupts now: we're officially "up". */
86
local_irq_enable();
87
}
88
89
asmlinkage __visible void cpu_bringup_and_idle(void)
90
{
91
cpu_bringup();
92
cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
93
}
94
95
void xen_smp_intr_free_pv(unsigned int cpu)
96
{
97
kfree(per_cpu(xen_irq_work, cpu).name);
98
per_cpu(xen_irq_work, cpu).name = NULL;
99
if (per_cpu(xen_irq_work, cpu).irq >= 0) {
100
unbind_from_irqhandler(per_cpu(xen_irq_work, cpu).irq, NULL);
101
per_cpu(xen_irq_work, cpu).irq = -1;
102
}
103
104
kfree(per_cpu(xen_pmu_irq, cpu).name);
105
per_cpu(xen_pmu_irq, cpu).name = NULL;
106
if (per_cpu(xen_pmu_irq, cpu).irq >= 0) {
107
unbind_from_irqhandler(per_cpu(xen_pmu_irq, cpu).irq, NULL);
108
per_cpu(xen_pmu_irq, cpu).irq = -1;
109
}
110
}
111
112
int xen_smp_intr_init_pv(unsigned int cpu)
113
{
114
int rc;
115
char *callfunc_name, *pmu_name;
116
117
callfunc_name = kasprintf(GFP_KERNEL, "irqwork%d", cpu);
118
per_cpu(xen_irq_work, cpu).name = callfunc_name;
119
rc = bind_ipi_to_irqhandler(XEN_IRQ_WORK_VECTOR,
120
cpu,
121
xen_irq_work_interrupt,
122
IRQF_PERCPU|IRQF_NOBALANCING,
123
callfunc_name,
124
NULL);
125
if (rc < 0)
126
goto fail;
127
per_cpu(xen_irq_work, cpu).irq = rc;
128
129
if (is_xen_pmu) {
130
pmu_name = kasprintf(GFP_KERNEL, "pmu%d", cpu);
131
per_cpu(xen_pmu_irq, cpu).name = pmu_name;
132
rc = bind_virq_to_irqhandler(VIRQ_XENPMU, cpu,
133
xen_pmu_irq_handler,
134
IRQF_PERCPU|IRQF_NOBALANCING,
135
pmu_name, NULL);
136
if (rc < 0)
137
goto fail;
138
per_cpu(xen_pmu_irq, cpu).irq = rc;
139
}
140
141
return 0;
142
143
fail:
144
xen_smp_intr_free_pv(cpu);
145
return rc;
146
}
147
148
static void __init xen_pv_smp_config(void)
149
{
150
u32 apicid = 0;
151
int i;
152
153
topology_register_boot_apic(apicid);
154
155
for (i = 0; i < nr_cpu_ids; i++)
156
topology_register_apic(apicid++, CPU_ACPIID_INVALID, true);
157
158
/* Pretend to be a proper enumerated system */
159
smp_found_config = 1;
160
}
161
162
static void __init xen_pv_smp_prepare_boot_cpu(void)
163
{
164
BUG_ON(smp_processor_id() != 0);
165
native_smp_prepare_boot_cpu();
166
167
if (!xen_feature(XENFEAT_writable_page_tables))
168
/* We've switched to the "real" per-cpu gdt, so make
169
* sure the old memory can be recycled. */
170
make_lowmem_page_readwrite(xen_initial_gdt);
171
172
xen_setup_vcpu_info_placement();
173
174
/*
175
* The alternative logic (which patches the unlock/lock) runs before
176
* the smp bootup up code is activated. Hence we need to set this up
177
* the core kernel is being patched. Otherwise we will have only
178
* modules patched but not core code.
179
*/
180
xen_init_spinlocks();
181
}
182
183
static void __init xen_pv_smp_prepare_cpus(unsigned int max_cpus)
184
{
185
unsigned cpu;
186
187
if (ioapic_is_disabled) {
188
char *m = (max_cpus == 0) ?
189
"The nosmp parameter is incompatible with Xen; " \
190
"use Xen dom0_max_vcpus=1 parameter" :
191
"The noapic parameter is incompatible with Xen";
192
193
xen_raw_printk(m);
194
panic(m);
195
}
196
xen_init_lock_cpu(0);
197
198
smp_prepare_cpus_common();
199
200
speculative_store_bypass_ht_init();
201
202
xen_pmu_init(0);
203
204
if (xen_smp_intr_init(0) || xen_smp_intr_init_pv(0))
205
BUG();
206
207
if (!alloc_cpumask_var(&xen_cpu_initialized_map, GFP_KERNEL))
208
panic("could not allocate xen_cpu_initialized_map\n");
209
210
cpumask_copy(xen_cpu_initialized_map, cpumask_of(0));
211
212
/* Restrict the possible_map according to max_cpus. */
213
while ((num_possible_cpus() > 1) && (num_possible_cpus() > max_cpus)) {
214
for (cpu = nr_cpu_ids - 1; !cpu_possible(cpu); cpu--)
215
continue;
216
set_cpu_possible(cpu, false);
217
}
218
219
for_each_possible_cpu(cpu)
220
set_cpu_present(cpu, true);
221
}
222
223
static int
224
cpu_initialize_context(unsigned int cpu, struct task_struct *idle)
225
{
226
struct vcpu_guest_context *ctxt;
227
struct desc_struct *gdt;
228
unsigned long gdt_mfn;
229
230
if (cpumask_test_and_set_cpu(cpu, xen_cpu_initialized_map))
231
return 0;
232
233
ctxt = kzalloc(sizeof(*ctxt), GFP_KERNEL);
234
if (ctxt == NULL) {
235
cpumask_clear_cpu(cpu, xen_cpu_initialized_map);
236
return -ENOMEM;
237
}
238
239
gdt = get_cpu_gdt_rw(cpu);
240
241
/*
242
* Bring up the CPU in cpu_bringup_and_idle() with the stack
243
* pointing just below where pt_regs would be if it were a normal
244
* kernel entry.
245
*/
246
ctxt->user_regs.eip = (unsigned long)asm_cpu_bringup_and_idle;
247
ctxt->flags = VGCF_IN_KERNEL;
248
ctxt->user_regs.eflags = 0x1000; /* IOPL_RING1 */
249
ctxt->user_regs.ds = __USER_DS;
250
ctxt->user_regs.es = __USER_DS;
251
ctxt->user_regs.ss = __KERNEL_DS;
252
ctxt->user_regs.cs = __KERNEL_CS;
253
ctxt->user_regs.esp = (unsigned long)task_pt_regs(idle);
254
255
xen_copy_trap_info(ctxt->trap_ctxt);
256
257
BUG_ON((unsigned long)gdt & ~PAGE_MASK);
258
259
gdt_mfn = arbitrary_virt_to_mfn(gdt);
260
make_lowmem_page_readonly(gdt);
261
make_lowmem_page_readonly(mfn_to_virt(gdt_mfn));
262
263
ctxt->gdt_frames[0] = gdt_mfn;
264
ctxt->gdt_ents = GDT_ENTRIES;
265
266
/*
267
* Set SS:SP that Xen will use when entering guest kernel mode
268
* from guest user mode. Subsequent calls to load_sp0() can
269
* change this value.
270
*/
271
ctxt->kernel_ss = __KERNEL_DS;
272
ctxt->kernel_sp = task_top_of_stack(idle);
273
274
ctxt->gs_base_kernel = per_cpu_offset(cpu);
275
ctxt->event_callback_eip =
276
(unsigned long)xen_asm_exc_xen_hypervisor_callback;
277
ctxt->failsafe_callback_eip =
278
(unsigned long)xen_failsafe_callback;
279
per_cpu(xen_cr3, cpu) = __pa(swapper_pg_dir);
280
281
ctxt->ctrlreg[3] = xen_pfn_to_cr3(virt_to_gfn(swapper_pg_dir));
282
if (HYPERVISOR_vcpu_op(VCPUOP_initialise, xen_vcpu_nr(cpu), ctxt))
283
BUG();
284
285
kfree(ctxt);
286
return 0;
287
}
288
289
static int xen_pv_kick_ap(unsigned int cpu, struct task_struct *idle)
290
{
291
int rc;
292
293
rc = common_cpu_up(cpu, idle);
294
if (rc)
295
return rc;
296
297
xen_setup_runstate_info(cpu);
298
299
/* make sure interrupts start blocked */
300
per_cpu(xen_vcpu, cpu)->evtchn_upcall_mask = 1;
301
302
rc = cpu_initialize_context(cpu, idle);
303
if (rc)
304
return rc;
305
306
xen_pmu_init(cpu);
307
308
/*
309
* Why is this a BUG? If the hypercall fails then everything can be
310
* rolled back, no?
311
*/
312
BUG_ON(HYPERVISOR_vcpu_op(VCPUOP_up, xen_vcpu_nr(cpu), NULL));
313
314
return 0;
315
}
316
317
static void xen_pv_poll_sync_state(void)
318
{
319
HYPERVISOR_sched_op(SCHEDOP_yield, NULL);
320
}
321
322
#ifdef CONFIG_HOTPLUG_CPU
323
static int xen_pv_cpu_disable(void)
324
{
325
unsigned int cpu = smp_processor_id();
326
if (cpu == 0)
327
return -EBUSY;
328
329
cpu_disable_common();
330
331
load_cr3(swapper_pg_dir);
332
return 0;
333
}
334
335
static void xen_pv_cpu_die(unsigned int cpu)
336
{
337
while (HYPERVISOR_vcpu_op(VCPUOP_is_up, xen_vcpu_nr(cpu), NULL)) {
338
__set_current_state(TASK_UNINTERRUPTIBLE);
339
schedule_timeout(HZ/10);
340
}
341
}
342
343
static void xen_pv_cleanup_dead_cpu(unsigned int cpu)
344
{
345
xen_smp_intr_free(cpu);
346
xen_uninit_lock_cpu(cpu);
347
xen_teardown_timer(cpu);
348
xen_pmu_finish(cpu);
349
}
350
351
static void __noreturn xen_pv_play_dead(void) /* used only with HOTPLUG_CPU */
352
{
353
play_dead_common();
354
HYPERVISOR_vcpu_op(VCPUOP_down, xen_vcpu_nr(smp_processor_id()), NULL);
355
xen_cpu_bringup_again((unsigned long)task_pt_regs(current));
356
BUG();
357
}
358
359
#else /* !CONFIG_HOTPLUG_CPU */
360
static int xen_pv_cpu_disable(void)
361
{
362
return -ENOSYS;
363
}
364
365
static void xen_pv_cpu_die(unsigned int cpu)
366
{
367
BUG();
368
}
369
370
static void xen_pv_cleanup_dead_cpu(unsigned int cpu)
371
{
372
BUG();
373
}
374
375
static void __noreturn xen_pv_play_dead(void)
376
{
377
BUG();
378
}
379
380
#endif
381
static void stop_self(void *v)
382
{
383
int cpu = smp_processor_id();
384
385
/* make sure we're not pinning something down */
386
load_cr3(swapper_pg_dir);
387
/* should set up a minimal gdt */
388
389
set_cpu_online(cpu, false);
390
391
HYPERVISOR_vcpu_op(VCPUOP_down, xen_vcpu_nr(cpu), NULL);
392
BUG();
393
}
394
395
static void xen_pv_stop_other_cpus(int wait)
396
{
397
smp_call_function(stop_self, NULL, wait);
398
}
399
400
static irqreturn_t xen_irq_work_interrupt(int irq, void *dev_id)
401
{
402
irq_work_run();
403
inc_irq_stat(apic_irq_work_irqs);
404
405
return IRQ_HANDLED;
406
}
407
408
void __init xen_smp_count_cpus(void)
409
{
410
unsigned int cpus;
411
412
for (cpus = 0; cpus < nr_cpu_ids; cpus++) {
413
if (HYPERVISOR_vcpu_op(VCPUOP_is_up, cpus, NULL) < 0)
414
break;
415
}
416
417
pr_info("Xen PV: Detected %u vCPUS\n", cpus);
418
if (cpus < nr_cpu_ids)
419
set_nr_cpu_ids(cpus);
420
}
421
422
static const struct smp_ops xen_smp_ops __initconst = {
423
.smp_prepare_boot_cpu = xen_pv_smp_prepare_boot_cpu,
424
.smp_prepare_cpus = xen_pv_smp_prepare_cpus,
425
.smp_cpus_done = xen_smp_cpus_done,
426
427
.kick_ap_alive = xen_pv_kick_ap,
428
.cpu_die = xen_pv_cpu_die,
429
.cleanup_dead_cpu = xen_pv_cleanup_dead_cpu,
430
.poll_sync_state = xen_pv_poll_sync_state,
431
.cpu_disable = xen_pv_cpu_disable,
432
.play_dead = xen_pv_play_dead,
433
434
.stop_other_cpus = xen_pv_stop_other_cpus,
435
.smp_send_reschedule = xen_smp_send_reschedule,
436
437
.send_call_func_ipi = xen_smp_send_call_function_ipi,
438
.send_call_func_single_ipi = xen_smp_send_call_function_single_ipi,
439
};
440
441
void __init xen_smp_init(void)
442
{
443
smp_ops = xen_smp_ops;
444
445
/* Avoid searching for BIOS MP tables */
446
x86_init.mpparse.find_mptable = x86_init_noop;
447
x86_init.mpparse.early_parse_smp_cfg = x86_init_noop;
448
449
/* XEN/PV Dom0 has halfways sane topology information via CPUID/MADT */
450
if (xen_initial_domain())
451
x86_init.mpparse.parse_smp_cfg = x86_init_noop;
452
else
453
x86_init.mpparse.parse_smp_cfg = xen_pv_smp_config;
454
}
455
456