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