]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - arch/x86/xen/enlighten.c
xen/vcpu: Simplify xen_vcpu related code
[karo-tx-linux.git] / arch / x86 / xen / enlighten.c
1 #include <linux/cpu.h>
2 #include <linux/kexec.h>
3
4 #include <xen/features.h>
5 #include <xen/page.h>
6
7 #include <asm/xen/hypercall.h>
8 #include <asm/xen/hypervisor.h>
9 #include <asm/cpu.h>
10 #include <asm/e820/api.h> 
11
12 #include "xen-ops.h"
13 #include "smp.h"
14 #include "pmu.h"
15
16 EXPORT_SYMBOL_GPL(hypercall_page);
17
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  */
34 DEFINE_PER_CPU(struct vcpu_info *, xen_vcpu);
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  */
41 DEFINE_PER_CPU(struct vcpu_info, xen_vcpu_info);
42
43 /* Linux <-> Xen vCPU id mapping */
44 DEFINE_PER_CPU(uint32_t, xen_vcpu_id);
45 EXPORT_PER_CPU_SYMBOL(xen_vcpu_id);
46
47 enum xen_domain_type xen_domain_type = XEN_NATIVE;
48 EXPORT_SYMBOL_GPL(xen_domain_type);
49
50 unsigned long *machine_to_phys_mapping = (void *)MACH2PHYS_VIRT_START;
51 EXPORT_SYMBOL(machine_to_phys_mapping);
52 unsigned long  machine_to_phys_nr;
53 EXPORT_SYMBOL(machine_to_phys_nr);
54
55 struct start_info *xen_start_info;
56 EXPORT_SYMBOL_GPL(xen_start_info);
57
58 struct shared_info xen_dummy_shared_info;
59
60 __read_mostly int xen_have_vector_callback;
61 EXPORT_SYMBOL_GPL(xen_have_vector_callback);
62
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  */
67 struct shared_info *HYPERVISOR_shared_info = &xen_dummy_shared_info;
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  */
82 int xen_have_vcpu_info_placement = 1;
83
84 static int xen_cpu_up_online(unsigned int cpu)
85 {
86         xen_init_lock_cpu(cpu);
87         return 0;
88 }
89
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 }
108
109 /*
110  * On restore, set the vcpu placement up again.
111  * If it fails, then we're in a bad state, since
112  * we can't back out from using it...
113  */
114 void xen_vcpu_restore(void)
115 {
116         int cpu;
117
118         for_each_possible_cpu(cpu) {
119                 bool other_cpu = (cpu != smp_processor_id());
120                 bool is_up = HYPERVISOR_vcpu_op(VCPUOP_is_up, xen_vcpu_nr(cpu),
121                                                 NULL);
122
123                 if (other_cpu && is_up &&
124                     HYPERVISOR_vcpu_op(VCPUOP_down, xen_vcpu_nr(cpu), NULL))
125                         BUG();
126
127                 xen_setup_runstate_info(cpu);
128
129                 if (xen_have_vcpu_info_placement)
130                         xen_vcpu_setup(cpu);
131
132                 if (other_cpu && is_up &&
133                     HYPERVISOR_vcpu_op(VCPUOP_up, xen_vcpu_nr(cpu), NULL))
134                         BUG();
135         }
136 }
137
138 static void clamp_max_cpus(void)
139 {
140 #ifdef CONFIG_SMP
141         if (setup_max_cpus > MAX_VIRT_CPUS)
142                 setup_max_cpus = MAX_VIRT_CPUS;
143 #endif
144 }
145
146 void xen_vcpu_info_reset(int cpu)
147 {
148         if (xen_vcpu_nr(cpu) < MAX_VIRT_CPUS) {
149                 per_cpu(xen_vcpu, cpu) =
150                         &HYPERVISOR_shared_info->vcpu_info[xen_vcpu_nr(cpu)];
151         } else {
152                 /* Set to NULL so that if somebody accesses it we get an OOPS */
153                 per_cpu(xen_vcpu, cpu) = NULL;
154         }
155 }
156
157 void xen_vcpu_setup(int cpu)
158 {
159         struct vcpu_register_vcpu_info info;
160         int err;
161         struct vcpu_info *vcpup;
162
163         BUG_ON(HYPERVISOR_shared_info == &xen_dummy_shared_info);
164
165         /*
166          * This path is called twice on PVHVM - first during bootup via
167          * smp_init -> xen_hvm_cpu_notify, and then if the VCPU is being
168          * hotplugged: cpu_up -> xen_hvm_cpu_notify.
169          * As we can only do the VCPUOP_register_vcpu_info once lets
170          * not over-write its result.
171          *
172          * For PV it is called during restore (xen_vcpu_restore) and bootup
173          * (xen_setup_vcpu_info_placement). The hotplug mechanism does not
174          * use this function.
175          */
176         if (xen_hvm_domain()) {
177                 if (per_cpu(xen_vcpu, cpu) == &per_cpu(xen_vcpu_info, cpu))
178                         return;
179         }
180
181         xen_vcpu_info_reset(cpu);
182
183         if (xen_have_vcpu_info_placement) {
184                 vcpup = &per_cpu(xen_vcpu_info, cpu);
185                 info.mfn = arbitrary_virt_to_mfn(vcpup);
186                 info.offset = offset_in_page(vcpup);
187
188                 /*
189                  * Check to see if the hypervisor will put the vcpu_info
190                  * structure where we want it, which allows direct access via
191                  * a percpu-variable.
192                  * N.B. This hypercall can _only_ be called once per CPU.
193                  * Subsequent calls will error out with -EINVAL. This is due to
194                  * the fact that hypervisor has no unregister variant and this
195                  * hypercall does not allow to over-write info.mfn and
196                  * info.offset.
197                  */
198                 err = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info,
199                                          xen_vcpu_nr(cpu), &info);
200
201                 if (err) {
202                         pr_warn_once("register_vcpu_info failed: cpu=%d err=%d\n",
203                                      cpu, err);
204                         xen_have_vcpu_info_placement = 0;
205                 } else {
206                         /*
207                          * This cpu is using the registered vcpu info, even if
208                          * later ones fail to.
209                          */
210                         per_cpu(xen_vcpu, cpu) = vcpup;
211                 }
212         }
213
214         if (!xen_have_vcpu_info_placement) {
215                 if (cpu >= MAX_VIRT_CPUS)
216                         clamp_max_cpus();
217                 return;
218         }
219 }
220
221 void xen_reboot(int reason)
222 {
223         struct sched_shutdown r = { .reason = reason };
224         int cpu;
225
226         for_each_online_cpu(cpu)
227                 xen_pmu_finish(cpu);
228
229         if (HYPERVISOR_sched_op(SCHEDOP_shutdown, &r))
230                 BUG();
231 }
232
233 void xen_emergency_restart(void)
234 {
235         xen_reboot(SHUTDOWN_reboot);
236 }
237
238 static int
239 xen_panic_event(struct notifier_block *this, unsigned long event, void *ptr)
240 {
241         if (!kexec_crash_loaded())
242                 xen_reboot(SHUTDOWN_crash);
243         return NOTIFY_DONE;
244 }
245
246 static struct notifier_block xen_panic_block = {
247         .notifier_call = xen_panic_event,
248         .priority = INT_MIN
249 };
250
251 int xen_panic_handler_init(void)
252 {
253         atomic_notifier_chain_register(&panic_notifier_list, &xen_panic_block);
254         return 0;
255 }
256
257 void xen_pin_vcpu(int cpu)
258 {
259         static bool disable_pinning;
260         struct sched_pin_override pin_override;
261         int ret;
262
263         if (disable_pinning)
264                 return;
265
266         pin_override.pcpu = cpu;
267         ret = HYPERVISOR_sched_op(SCHEDOP_pin_override, &pin_override);
268
269         /* Ignore errors when removing override. */
270         if (cpu < 0)
271                 return;
272
273         switch (ret) {
274         case -ENOSYS:
275                 pr_warn("Unable to pin on physical cpu %d. In case of problems consider vcpu pinning.\n",
276                         cpu);
277                 disable_pinning = true;
278                 break;
279         case -EPERM:
280                 WARN(1, "Trying to pin vcpu without having privilege to do so\n");
281                 disable_pinning = true;
282                 break;
283         case -EINVAL:
284         case -EBUSY:
285                 pr_warn("Physical cpu %d not available for pinning. Check Xen cpu configuration.\n",
286                         cpu);
287                 break;
288         case 0:
289                 break;
290         default:
291                 WARN(1, "rc %d while trying to pin vcpu\n", ret);
292                 disable_pinning = true;
293         }
294 }
295
296 #ifdef CONFIG_HOTPLUG_CPU
297 void xen_arch_register_cpu(int num)
298 {
299         arch_register_cpu(num);
300 }
301 EXPORT_SYMBOL(xen_arch_register_cpu);
302
303 void xen_arch_unregister_cpu(int num)
304 {
305         arch_unregister_cpu(num);
306 }
307 EXPORT_SYMBOL(xen_arch_unregister_cpu);
308 #endif