1
2
3#ifdef CONFIG_XEN_BALLOON_MEMORY_HOTPLUG
4#include <linux/memblock.h>
5#endif
6#include <linux/console.h>
7#include <linux/cpu.h>
8#include <linux/kexec.h>
9#include <linux/slab.h>
10#include <linux/panic_notifier.h>
11
12#include <xen/xen.h>
13#include <xen/features.h>
14#include <xen/interface/sched.h>
15#include <xen/interface/version.h>
16#include <xen/page.h>
17
18#include <asm/xen/hypercall.h>
19#include <asm/xen/hypervisor.h>
20#include <asm/cpu.h>
21#include <asm/e820/api.h>
22#include <asm/setup.h>
23
24#include "xen-ops.h"
25#include "smp.h"
26#include "pmu.h"
27
28EXPORT_SYMBOL_GPL(hypercall_page);
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46DEFINE_PER_CPU(struct vcpu_info *, xen_vcpu);
47
48
49
50
51
52
53DEFINE_PER_CPU(struct vcpu_info, xen_vcpu_info);
54
55
56DEFINE_PER_CPU(uint32_t, xen_vcpu_id);
57EXPORT_PER_CPU_SYMBOL(xen_vcpu_id);
58
59unsigned long *machine_to_phys_mapping = (void *)MACH2PHYS_VIRT_START;
60EXPORT_SYMBOL(machine_to_phys_mapping);
61unsigned long machine_to_phys_nr;
62EXPORT_SYMBOL(machine_to_phys_nr);
63
64struct start_info *xen_start_info;
65EXPORT_SYMBOL_GPL(xen_start_info);
66
67struct shared_info xen_dummy_shared_info;
68
69__read_mostly int xen_have_vector_callback;
70EXPORT_SYMBOL_GPL(xen_have_vector_callback);
71
72
73
74
75
76enum xen_domain_type __ro_after_init xen_domain_type = XEN_NATIVE;
77EXPORT_SYMBOL_GPL(xen_domain_type);
78uint32_t __ro_after_init xen_start_flags;
79EXPORT_SYMBOL(xen_start_flags);
80
81
82
83
84
85struct shared_info *HYPERVISOR_shared_info = &xen_dummy_shared_info;
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100int xen_have_vcpu_info_placement = 1;
101
102static int xen_cpu_up_online(unsigned int cpu)
103{
104 xen_init_lock_cpu(cpu);
105 return 0;
106}
107
108int xen_cpuhp_setup(int (*cpu_up_prepare_cb)(unsigned int),
109 int (*cpu_dead_cb)(unsigned int))
110{
111 int rc;
112
113 rc = cpuhp_setup_state_nocalls(CPUHP_XEN_PREPARE,
114 "x86/xen/guest:prepare",
115 cpu_up_prepare_cb, cpu_dead_cb);
116 if (rc >= 0) {
117 rc = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
118 "x86/xen/guest:online",
119 xen_cpu_up_online, NULL);
120 if (rc < 0)
121 cpuhp_remove_state_nocalls(CPUHP_XEN_PREPARE);
122 }
123
124 return rc >= 0 ? 0 : rc;
125}
126
127static int xen_vcpu_setup_restore(int cpu)
128{
129 int rc = 0;
130
131
132 xen_vcpu_info_reset(cpu);
133
134
135
136
137
138 if (xen_pv_domain() ||
139 (xen_hvm_domain() && cpu_online(cpu))) {
140 rc = xen_vcpu_setup(cpu);
141 }
142
143 return rc;
144}
145
146
147
148
149
150
151void xen_vcpu_restore(void)
152{
153 int cpu, rc;
154
155 for_each_possible_cpu(cpu) {
156 bool other_cpu = (cpu != smp_processor_id());
157 bool is_up;
158
159 if (xen_vcpu_nr(cpu) == XEN_VCPU_ID_INVALID)
160 continue;
161
162
163 is_up = HYPERVISOR_vcpu_op(VCPUOP_is_up,
164 xen_vcpu_nr(cpu), NULL) > 0;
165
166 if (other_cpu && is_up &&
167 HYPERVISOR_vcpu_op(VCPUOP_down, xen_vcpu_nr(cpu), NULL))
168 BUG();
169
170 if (xen_pv_domain() || xen_feature(XENFEAT_hvm_safe_pvclock))
171 xen_setup_runstate_info(cpu);
172
173 rc = xen_vcpu_setup_restore(cpu);
174 if (rc)
175 pr_emerg_once("vcpu restore failed for cpu=%d err=%d. "
176 "System will hang.\n", cpu, rc);
177
178
179
180
181
182
183
184
185
186 if (other_cpu && is_up && (rc == 0) &&
187 HYPERVISOR_vcpu_op(VCPUOP_up, xen_vcpu_nr(cpu), NULL))
188 BUG();
189 }
190}
191
192void xen_vcpu_info_reset(int cpu)
193{
194 if (xen_vcpu_nr(cpu) < MAX_VIRT_CPUS) {
195 per_cpu(xen_vcpu, cpu) =
196 &HYPERVISOR_shared_info->vcpu_info[xen_vcpu_nr(cpu)];
197 } else {
198
199 per_cpu(xen_vcpu, cpu) = NULL;
200 }
201}
202
203int xen_vcpu_setup(int cpu)
204{
205 struct vcpu_register_vcpu_info info;
206 int err;
207 struct vcpu_info *vcpup;
208
209 BUG_ON(HYPERVISOR_shared_info == &xen_dummy_shared_info);
210
211
212
213
214
215
216
217
218
219
220
221
222 if (xen_hvm_domain()) {
223 if (per_cpu(xen_vcpu, cpu) == &per_cpu(xen_vcpu_info, cpu))
224 return 0;
225 }
226
227 if (xen_have_vcpu_info_placement) {
228 vcpup = &per_cpu(xen_vcpu_info, cpu);
229 info.mfn = arbitrary_virt_to_mfn(vcpup);
230 info.offset = offset_in_page(vcpup);
231
232
233
234
235
236
237
238
239
240
241
242 err = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info,
243 xen_vcpu_nr(cpu), &info);
244
245 if (err) {
246 pr_warn_once("register_vcpu_info failed: cpu=%d err=%d\n",
247 cpu, err);
248 xen_have_vcpu_info_placement = 0;
249 } else {
250
251
252
253
254 per_cpu(xen_vcpu, cpu) = vcpup;
255 }
256 }
257
258 if (!xen_have_vcpu_info_placement)
259 xen_vcpu_info_reset(cpu);
260
261 return ((per_cpu(xen_vcpu, cpu) == NULL) ? -ENODEV : 0);
262}
263
264void __init xen_banner(void)
265{
266 unsigned version = HYPERVISOR_xen_version(XENVER_version, NULL);
267 struct xen_extraversion extra;
268
269 HYPERVISOR_xen_version(XENVER_extraversion, &extra);
270
271 pr_info("Booting kernel on %s\n", pv_info.name);
272 pr_info("Xen version: %u.%u%s%s\n",
273 version >> 16, version & 0xffff, extra.extraversion,
274 xen_feature(XENFEAT_mmu_pt_update_preserve_ad)
275 ? " (preserve-AD)" : "");
276}
277
278
279bool xen_running_on_version_or_later(unsigned int major, unsigned int minor)
280{
281 unsigned int version;
282
283 if (!xen_domain())
284 return false;
285
286 version = HYPERVISOR_xen_version(XENVER_version, NULL);
287 if ((((version >> 16) == major) && ((version & 0xffff) >= minor)) ||
288 ((version >> 16) > major))
289 return true;
290 return false;
291}
292
293void __init xen_add_preferred_consoles(void)
294{
295 add_preferred_console("xenboot", 0, NULL);
296 if (!boot_params.screen_info.orig_video_isVGA)
297 add_preferred_console("tty", 0, NULL);
298 add_preferred_console("hvc", 0, NULL);
299 if (boot_params.screen_info.orig_video_isVGA)
300 add_preferred_console("tty", 0, NULL);
301}
302
303void xen_reboot(int reason)
304{
305 struct sched_shutdown r = { .reason = reason };
306 int cpu;
307
308 for_each_online_cpu(cpu)
309 xen_pmu_finish(cpu);
310
311 if (HYPERVISOR_sched_op(SCHEDOP_shutdown, &r))
312 BUG();
313}
314
315static int reboot_reason = SHUTDOWN_reboot;
316static bool xen_legacy_crash;
317void xen_emergency_restart(void)
318{
319 xen_reboot(reboot_reason);
320}
321
322static int
323xen_panic_event(struct notifier_block *this, unsigned long event, void *ptr)
324{
325 if (!kexec_crash_loaded()) {
326 if (xen_legacy_crash)
327 xen_reboot(SHUTDOWN_crash);
328
329 reboot_reason = SHUTDOWN_crash;
330
331
332
333
334
335
336
337 if (panic_timeout == 0)
338 panic_timeout = -1;
339 }
340 return NOTIFY_DONE;
341}
342
343static int __init parse_xen_legacy_crash(char *arg)
344{
345 xen_legacy_crash = true;
346 return 0;
347}
348early_param("xen_legacy_crash", parse_xen_legacy_crash);
349
350static struct notifier_block xen_panic_block = {
351 .notifier_call = xen_panic_event,
352 .priority = INT_MIN
353};
354
355int xen_panic_handler_init(void)
356{
357 atomic_notifier_chain_register(&panic_notifier_list, &xen_panic_block);
358 return 0;
359}
360
361void xen_pin_vcpu(int cpu)
362{
363 static bool disable_pinning;
364 struct sched_pin_override pin_override;
365 int ret;
366
367 if (disable_pinning)
368 return;
369
370 pin_override.pcpu = cpu;
371 ret = HYPERVISOR_sched_op(SCHEDOP_pin_override, &pin_override);
372
373
374 if (cpu < 0)
375 return;
376
377 switch (ret) {
378 case -ENOSYS:
379 pr_warn("Unable to pin on physical cpu %d. In case of problems consider vcpu pinning.\n",
380 cpu);
381 disable_pinning = true;
382 break;
383 case -EPERM:
384 WARN(1, "Trying to pin vcpu without having privilege to do so\n");
385 disable_pinning = true;
386 break;
387 case -EINVAL:
388 case -EBUSY:
389 pr_warn("Physical cpu %d not available for pinning. Check Xen cpu configuration.\n",
390 cpu);
391 break;
392 case 0:
393 break;
394 default:
395 WARN(1, "rc %d while trying to pin vcpu\n", ret);
396 disable_pinning = true;
397 }
398}
399
400#ifdef CONFIG_HOTPLUG_CPU
401void xen_arch_register_cpu(int num)
402{
403 arch_register_cpu(num);
404}
405EXPORT_SYMBOL(xen_arch_register_cpu);
406
407void xen_arch_unregister_cpu(int num)
408{
409 arch_unregister_cpu(num);
410}
411EXPORT_SYMBOL(xen_arch_unregister_cpu);
412#endif
413