]>
Commit | Line | Data |
---|---|---|
b3cf8528 BO |
1 | #ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG |
2 | #include <linux/bootmem.h> | |
3 | #endif | |
38e20b07 | 4 | #include <linux/cpu.h> |
0b34a166 | 5 | #include <linux/kexec.h> |
f901f138 | 6 | #include <linux/slab.h> |
0b34a166 | 7 | |
5ead97c8 JF |
8 | #include <xen/features.h> |
9 | #include <xen/page.h> | |
10 | ||
5ead97c8 JF |
11 | #include <asm/xen/hypercall.h> |
12 | #include <asm/xen/hypervisor.h> | |
a314e3eb | 13 | #include <asm/cpu.h> |
687d77a5 | 14 | #include <asm/e820/api.h> |
73c154c6 | 15 | |
5ead97c8 | 16 | #include "xen-ops.h" |
f447d56d | 17 | #include "smp.h" |
65d0cf0b | 18 | #include "pmu.h" |
5ead97c8 JF |
19 | |
20 | EXPORT_SYMBOL_GPL(hypercall_page); | |
21 | ||
a520996a KRW |
22 | /* |
23 | * Pointer to the xen_vcpu_info structure or | |
24 | * &HYPERVISOR_shared_info->vcpu_info[cpu]. See xen_hvm_init_shared_info | |
25 | * and xen_vcpu_setup for details. By default it points to share_info->vcpu_info | |
26 | * but if the hypervisor supports VCPUOP_register_vcpu_info then it can point | |
27 | * to xen_vcpu_info. The pointer is used in __xen_evtchn_do_upcall to | |
28 | * acknowledge pending events. | |
29 | * Also more subtly it is used by the patched version of irq enable/disable | |
30 | * e.g. xen_irq_enable_direct and xen_iret in PV mode. | |
31 | * | |
32 | * The desire to be able to do those mask/unmask operations as a single | |
33 | * instruction by using the per-cpu offset held in %gs is the real reason | |
34 | * vcpu info is in a per-cpu pointer and the original reason for this | |
35 | * hypercall. | |
36 | * | |
37 | */ | |
5ead97c8 | 38 | DEFINE_PER_CPU(struct vcpu_info *, xen_vcpu); |
a520996a KRW |
39 | |
40 | /* | |
41 | * Per CPU pages used if hypervisor supports VCPUOP_register_vcpu_info | |
42 | * hypercall. This can be used both in PV and PVHVM mode. The structure | |
43 | * overrides the default per_cpu(xen_vcpu, cpu) value. | |
44 | */ | |
5ead97c8 | 45 | DEFINE_PER_CPU(struct vcpu_info, xen_vcpu_info); |
9f79991d | 46 | |
88e957d6 | 47 | /* Linux <-> Xen vCPU id mapping */ |
55467dea | 48 | DEFINE_PER_CPU(uint32_t, xen_vcpu_id); |
88e957d6 VK |
49 | EXPORT_PER_CPU_SYMBOL(xen_vcpu_id); |
50 | ||
6e833587 JF |
51 | enum xen_domain_type xen_domain_type = XEN_NATIVE; |
52 | EXPORT_SYMBOL_GPL(xen_domain_type); | |
53 | ||
7e77506a IC |
54 | unsigned long *machine_to_phys_mapping = (void *)MACH2PHYS_VIRT_START; |
55 | EXPORT_SYMBOL(machine_to_phys_mapping); | |
ccbcdf7c JB |
56 | unsigned long machine_to_phys_nr; |
57 | EXPORT_SYMBOL(machine_to_phys_nr); | |
7e77506a | 58 | |
5ead97c8 JF |
59 | struct start_info *xen_start_info; |
60 | EXPORT_SYMBOL_GPL(xen_start_info); | |
61 | ||
a0d695c8 | 62 | struct shared_info xen_dummy_shared_info; |
60223a32 | 63 | |
3dbd8204 BO |
64 | __read_mostly int xen_have_vector_callback; |
65 | EXPORT_SYMBOL_GPL(xen_have_vector_callback); | |
66 | ||
60223a32 JF |
67 | /* |
68 | * Point at some empty memory to start with. We map the real shared_info | |
69 | * page as soon as fixmap is up and running. | |
70 | */ | |
4648da7c | 71 | struct shared_info *HYPERVISOR_shared_info = &xen_dummy_shared_info; |
60223a32 JF |
72 | |
73 | /* | |
74 | * Flag to determine whether vcpu info placement is available on all | |
75 | * VCPUs. We assume it is to start with, and then set it to zero on | |
76 | * the first failure. This is because it can succeed on some VCPUs | |
77 | * and not others, since it can involve hypervisor memory allocation, | |
78 | * or because the guest failed to guarantee all the appropriate | |
79 | * constraints on all VCPUs (ie buffer can't cross a page boundary). | |
80 | * | |
81 | * Note that any particular CPU may be using a placed vcpu structure, | |
82 | * but we can only optimise if the all are. | |
83 | * | |
84 | * 0: not available, 1: available | |
85 | */ | |
52519f2a | 86 | int xen_have_vcpu_info_placement = 1; |
60223a32 | 87 | |
e1dab14c VK |
88 | static int xen_cpu_up_online(unsigned int cpu) |
89 | { | |
90 | xen_init_lock_cpu(cpu); | |
91 | return 0; | |
92 | } | |
1c32cdc6 | 93 | |
e1dab14c VK |
94 | int xen_cpuhp_setup(int (*cpu_up_prepare_cb)(unsigned int), |
95 | int (*cpu_dead_cb)(unsigned int)) | |
96 | { | |
97 | int rc; | |
98 | ||
99 | rc = cpuhp_setup_state_nocalls(CPUHP_XEN_PREPARE, | |
eac779aa | 100 | "x86/xen/guest:prepare", |
e1dab14c VK |
101 | cpu_up_prepare_cb, cpu_dead_cb); |
102 | if (rc >= 0) { | |
103 | rc = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, | |
eac779aa | 104 | "x86/xen/guest:online", |
e1dab14c VK |
105 | xen_cpu_up_online, NULL); |
106 | if (rc < 0) | |
107 | cpuhp_remove_state_nocalls(CPUHP_XEN_PREPARE); | |
108 | } | |
109 | ||
110 | return rc >= 0 ? 0 : rc; | |
111 | } | |
1c32cdc6 | 112 | |
c9b5d98b | 113 | static int xen_vcpu_setup_restore(int cpu) |
0b64ffb8 | 114 | { |
c9b5d98b AA |
115 | int rc = 0; |
116 | ||
0b64ffb8 AA |
117 | /* Any per_cpu(xen_vcpu) is stale, so reset it */ |
118 | xen_vcpu_info_reset(cpu); | |
119 | ||
120 | /* | |
121 | * For PVH and PVHVM, setup online VCPUs only. The rest will | |
122 | * be handled by hotplug. | |
123 | */ | |
124 | if (xen_pv_domain() || | |
125 | (xen_hvm_domain() && cpu_online(cpu))) { | |
c9b5d98b | 126 | rc = xen_vcpu_setup(cpu); |
0b64ffb8 | 127 | } |
c9b5d98b AA |
128 | |
129 | return rc; | |
0b64ffb8 AA |
130 | } |
131 | ||
ad73fd59 AA |
132 | /* |
133 | * On restore, set the vcpu placement up again. | |
134 | * If it fails, then we're in a bad state, since | |
135 | * we can't back out from using it... | |
136 | */ | |
137 | void xen_vcpu_restore(void) | |
138 | { | |
c9b5d98b | 139 | int cpu, rc; |
ad73fd59 AA |
140 | |
141 | for_each_possible_cpu(cpu) { | |
142 | bool other_cpu = (cpu != smp_processor_id()); | |
0b64ffb8 AA |
143 | bool is_up; |
144 | ||
145 | if (xen_vcpu_nr(cpu) == XEN_VCPU_ID_INVALID) | |
146 | continue; | |
147 | ||
148 | /* Only Xen 4.5 and higher support this. */ | |
149 | is_up = HYPERVISOR_vcpu_op(VCPUOP_is_up, | |
150 | xen_vcpu_nr(cpu), NULL) > 0; | |
ad73fd59 AA |
151 | |
152 | if (other_cpu && is_up && | |
153 | HYPERVISOR_vcpu_op(VCPUOP_down, xen_vcpu_nr(cpu), NULL)) | |
154 | BUG(); | |
155 | ||
0b64ffb8 AA |
156 | if (xen_pv_domain() || xen_feature(XENFEAT_hvm_safe_pvclock)) |
157 | xen_setup_runstate_info(cpu); | |
ad73fd59 | 158 | |
c9b5d98b AA |
159 | rc = xen_vcpu_setup_restore(cpu); |
160 | if (rc) | |
161 | pr_emerg_once("vcpu restore failed for cpu=%d err=%d. " | |
162 | "System will hang.\n", cpu, rc); | |
163 | /* | |
164 | * In case xen_vcpu_setup_restore() fails, do not bring up the | |
165 | * VCPU. This helps us avoid the resulting OOPS when the VCPU | |
166 | * accesses pvclock_vcpu_time via xen_vcpu (which is NULL.) | |
167 | * Note that this does not improve the situation much -- now the | |
168 | * VM hangs instead of OOPSing -- with the VCPUs that did not | |
169 | * fail, spinning in stop_machine(), waiting for the failed | |
170 | * VCPUs to come up. | |
171 | */ | |
172 | if (other_cpu && is_up && (rc == 0) && | |
ad73fd59 AA |
173 | HYPERVISOR_vcpu_op(VCPUOP_up, xen_vcpu_nr(cpu), NULL)) |
174 | BUG(); | |
175 | } | |
176 | } | |
177 | ||
ad73fd59 AA |
178 | void xen_vcpu_info_reset(int cpu) |
179 | { | |
180 | if (xen_vcpu_nr(cpu) < MAX_VIRT_CPUS) { | |
181 | per_cpu(xen_vcpu, cpu) = | |
182 | &HYPERVISOR_shared_info->vcpu_info[xen_vcpu_nr(cpu)]; | |
183 | } else { | |
184 | /* Set to NULL so that if somebody accesses it we get an OOPS */ | |
185 | per_cpu(xen_vcpu, cpu) = NULL; | |
186 | } | |
187 | } | |
188 | ||
c9b5d98b | 189 | int xen_vcpu_setup(int cpu) |
5ead97c8 | 190 | { |
60223a32 JF |
191 | struct vcpu_register_vcpu_info info; |
192 | int err; | |
193 | struct vcpu_info *vcpup; | |
194 | ||
a0d695c8 | 195 | BUG_ON(HYPERVISOR_shared_info == &xen_dummy_shared_info); |
60223a32 | 196 | |
7f1fc268 | 197 | /* |
0b64ffb8 AA |
198 | * This path is called on PVHVM at bootup (xen_hvm_smp_prepare_boot_cpu) |
199 | * and at restore (xen_vcpu_restore). Also called for hotplugged | |
200 | * VCPUs (cpu_init -> xen_hvm_cpu_prepare_hvm). | |
201 | * However, the hypercall can only be done once (see below) so if a VCPU | |
202 | * is offlined and comes back online then let's not redo the hypercall. | |
7f1fc268 KRW |
203 | * |
204 | * For PV it is called during restore (xen_vcpu_restore) and bootup | |
205 | * (xen_setup_vcpu_info_placement). The hotplug mechanism does not | |
206 | * use this function. | |
207 | */ | |
208 | if (xen_hvm_domain()) { | |
209 | if (per_cpu(xen_vcpu, cpu) == &per_cpu(xen_vcpu_info, cpu)) | |
c9b5d98b | 210 | return 0; |
7f1fc268 | 211 | } |
ad73fd59 | 212 | |
ad73fd59 AA |
213 | if (xen_have_vcpu_info_placement) { |
214 | vcpup = &per_cpu(xen_vcpu_info, cpu); | |
215 | info.mfn = arbitrary_virt_to_mfn(vcpup); | |
216 | info.offset = offset_in_page(vcpup); | |
217 | ||
218 | /* | |
219 | * Check to see if the hypervisor will put the vcpu_info | |
220 | * structure where we want it, which allows direct access via | |
221 | * a percpu-variable. | |
222 | * N.B. This hypercall can _only_ be called once per CPU. | |
223 | * Subsequent calls will error out with -EINVAL. This is due to | |
224 | * the fact that hypervisor has no unregister variant and this | |
225 | * hypercall does not allow to over-write info.mfn and | |
226 | * info.offset. | |
227 | */ | |
228 | err = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info, | |
229 | xen_vcpu_nr(cpu), &info); | |
230 | ||
231 | if (err) { | |
232 | pr_warn_once("register_vcpu_info failed: cpu=%d err=%d\n", | |
233 | cpu, err); | |
234 | xen_have_vcpu_info_placement = 0; | |
235 | } else { | |
236 | /* | |
237 | * This cpu is using the registered vcpu info, even if | |
238 | * later ones fail to. | |
239 | */ | |
240 | per_cpu(xen_vcpu, cpu) = vcpup; | |
241 | } | |
242 | } | |
60223a32 | 243 | |
c9b5d98b | 244 | if (!xen_have_vcpu_info_placement) |
0b64ffb8 | 245 | xen_vcpu_info_reset(cpu); |
c9b5d98b AA |
246 | |
247 | return ((per_cpu(xen_vcpu, cpu) == NULL) ? -ENODEV : 0); | |
5ead97c8 JF |
248 | } |
249 | ||
e1dab14c | 250 | void xen_reboot(int reason) |
9c7a7942 | 251 | { |
e1dab14c | 252 | struct sched_shutdown r = { .reason = reason }; |
3905bb2a | 253 | int cpu; |
9c7a7942 | 254 | |
e1dab14c VK |
255 | for_each_online_cpu(cpu) |
256 | xen_pmu_finish(cpu); | |
aa1acff3 | 257 | |
e1dab14c | 258 | if (HYPERVISOR_sched_op(SCHEDOP_shutdown, &r)) |
a05d2eba | 259 | BUG(); |
38ffbe66 JF |
260 | } |
261 | ||
e64abd06 BO |
262 | static int reboot_reason = SHUTDOWN_reboot; |
263 | static bool xen_legacy_crash; | |
e1dab14c | 264 | void xen_emergency_restart(void) |
5ead97c8 | 265 | { |
e64abd06 | 266 | xen_reboot(reboot_reason); |
5ead97c8 JF |
267 | } |
268 | ||
e1dab14c VK |
269 | static int |
270 | xen_panic_event(struct notifier_block *this, unsigned long event, void *ptr) | |
5ead97c8 | 271 | { |
e64abd06 BO |
272 | if (!kexec_crash_loaded()) { |
273 | if (xen_legacy_crash) | |
274 | xen_reboot(SHUTDOWN_crash); | |
275 | ||
276 | reboot_reason = SHUTDOWN_crash; | |
277 | ||
278 | /* | |
279 | * If panic_timeout==0 then we are supposed to wait forever. | |
280 | * However, to preserve original dom0 behavior we have to drop | |
281 | * into hypervisor. (domU behavior is controlled by its | |
282 | * config file) | |
283 | */ | |
284 | if (panic_timeout == 0) | |
285 | panic_timeout = -1; | |
286 | } | |
e1dab14c | 287 | return NOTIFY_DONE; |
5ead97c8 JF |
288 | } |
289 | ||
e64abd06 BO |
290 | static int __init parse_xen_legacy_crash(char *arg) |
291 | { | |
292 | xen_legacy_crash = true; | |
293 | return 0; | |
294 | } | |
295 | early_param("xen_legacy_crash", parse_xen_legacy_crash); | |
296 | ||
e1dab14c VK |
297 | static struct notifier_block xen_panic_block = { |
298 | .notifier_call = xen_panic_event, | |
299 | .priority = INT_MIN | |
300 | }; | |
577eebea | 301 | |
e1dab14c | 302 | int xen_panic_handler_init(void) |
59290362 | 303 | { |
e1dab14c VK |
304 | atomic_notifier_chain_register(&panic_notifier_list, &xen_panic_block); |
305 | return 0; | |
59290362 DV |
306 | } |
307 | ||
e1dab14c | 308 | void xen_pin_vcpu(int cpu) |
5ead97c8 | 309 | { |
e1dab14c VK |
310 | static bool disable_pinning; |
311 | struct sched_pin_override pin_override; | |
312 | int ret; | |
1c32cdc6 | 313 | |
e1dab14c | 314 | if (disable_pinning) |
1c32cdc6 DV |
315 | return; |
316 | ||
e1dab14c VK |
317 | pin_override.pcpu = cpu; |
318 | ret = HYPERVISOR_sched_op(SCHEDOP_pin_override, &pin_override); | |
5ead97c8 | 319 | |
e1dab14c VK |
320 | /* Ignore errors when removing override. */ |
321 | if (cpu < 0) | |
322 | return; | |
5ead97c8 | 323 | |
e1dab14c VK |
324 | switch (ret) { |
325 | case -ENOSYS: | |
326 | pr_warn("Unable to pin on physical cpu %d. In case of problems consider vcpu pinning.\n", | |
327 | cpu); | |
328 | disable_pinning = true; | |
329 | break; | |
330 | case -EPERM: | |
331 | WARN(1, "Trying to pin vcpu without having privilege to do so\n"); | |
332 | disable_pinning = true; | |
333 | break; | |
334 | case -EINVAL: | |
335 | case -EBUSY: | |
336 | pr_warn("Physical cpu %d not available for pinning. Check Xen cpu configuration.\n", | |
337 | cpu); | |
338 | break; | |
339 | case 0: | |
340 | break; | |
341 | default: | |
342 | WARN(1, "rc %d while trying to pin vcpu\n", ret); | |
343 | disable_pinning = true; | |
8a95408e | 344 | } |
5ead97c8 JF |
345 | } |
346 | ||
a314e3eb SS |
347 | #ifdef CONFIG_HOTPLUG_CPU |
348 | void xen_arch_register_cpu(int num) | |
349 | { | |
350 | arch_register_cpu(num); | |
351 | } | |
352 | EXPORT_SYMBOL(xen_arch_register_cpu); | |
353 | ||
354 | void xen_arch_unregister_cpu(int num) | |
355 | { | |
356 | arch_unregister_cpu(num); | |
357 | } | |
358 | EXPORT_SYMBOL(xen_arch_unregister_cpu); | |
359 | #endif |