188b094fbSAlok Kataria /*
288b094fbSAlok Kataria * Common hypervisor code
388b094fbSAlok Kataria *
488b094fbSAlok Kataria * Copyright (C) 2008, VMware, Inc.
588b094fbSAlok Kataria * Author : Alok N Kataria <akataria@vmware.com>
688b094fbSAlok Kataria *
788b094fbSAlok Kataria * This program is free software; you can redistribute it and/or modify
888b094fbSAlok Kataria * it under the terms of the GNU General Public License as published by
988b094fbSAlok Kataria * the Free Software Foundation; either version 2 of the License, or
1088b094fbSAlok Kataria * (at your option) any later version.
1188b094fbSAlok Kataria *
1288b094fbSAlok Kataria * This program is distributed in the hope that it will be useful, but
1388b094fbSAlok Kataria * WITHOUT ANY WARRANTY; without even the implied warranty of
1488b094fbSAlok Kataria * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
1588b094fbSAlok Kataria * NON INFRINGEMENT. See the GNU General Public License for more
1688b094fbSAlok Kataria * details.
1788b094fbSAlok Kataria *
1888b094fbSAlok Kataria * You should have received a copy of the GNU General Public License
1988b094fbSAlok Kataria * along with this program; if not, write to the Free Software
2088b094fbSAlok Kataria * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2188b094fbSAlok Kataria *
2288b094fbSAlok Kataria */
2388b094fbSAlok Kataria
24186f4360SPaul Gortmaker #include <linux/init.h>
25186f4360SPaul Gortmaker #include <linux/export.h>
2688b094fbSAlok Kataria #include <asm/processor.h>
274e42ebd5SHannes Eder #include <asm/hypervisor.h>
2888b094fbSAlok Kataria
29e08cae41SH. Peter Anvin static const __initconst struct hypervisor_x86 * const hypervisors[] =
3088b094fbSAlok Kataria {
315e57f1d6SVitaly Kuznetsov #ifdef CONFIG_XEN_PV
320991d22dSVitaly Kuznetsov &x86_hyper_xen_pv,
335e57f1d6SVitaly Kuznetsov #endif
345e57f1d6SVitaly Kuznetsov #ifdef CONFIG_XEN_PVHVM
350991d22dSVitaly Kuznetsov &x86_hyper_xen_hvm,
36b43275d6SJeremy Fitzhardinge #endif
3724a42baeSAnupam Chanda &x86_hyper_vmware,
3824a42baeSAnupam Chanda &x86_hyper_ms_hyperv,
39d63d3e62SAvi Kivity #ifdef CONFIG_KVM_GUEST
40fc73373bSPrarit Bhargava &x86_hyper_kvm,
41d63d3e62SAvi Kivity #endif
424a362601SJan Kiszka #ifdef CONFIG_JAILHOUSE_GUEST
434a362601SJan Kiszka &x86_hyper_jailhouse,
444a362601SJan Kiszka #endif
45ec7972c9SZhao Yakui #ifdef CONFIG_ACRN_GUEST
46ec7972c9SZhao Yakui &x86_hyper_acrn,
47ec7972c9SZhao Yakui #endif
48e08cae41SH. Peter Anvin };
4988b094fbSAlok Kataria
5003b2a320SJuergen Gross enum x86_hypervisor_type x86_hyper_type;
5103b2a320SJuergen Gross EXPORT_SYMBOL(x86_hyper_type);
52e08cae41SH. Peter Anvin
53*30978346SZhenzhong Duan bool __initdata nopv;
parse_nopv(char * arg)54*30978346SZhenzhong Duan static __init int parse_nopv(char *arg)
55*30978346SZhenzhong Duan {
56*30978346SZhenzhong Duan nopv = true;
57*30978346SZhenzhong Duan return 0;
58*30978346SZhenzhong Duan }
59*30978346SZhenzhong Duan early_param("nopv", parse_nopv);
60*30978346SZhenzhong Duan
61f72e38e8SJuergen Gross static inline const struct hypervisor_x86 * __init
detect_hypervisor_vendor(void)62e08cae41SH. Peter Anvin detect_hypervisor_vendor(void)
63eca0cd02SAlok Kataria {
64f72e38e8SJuergen Gross const struct hypervisor_x86 *h = NULL, * const *p;
659df56f19SJason Wang uint32_t pri, max_pri = 0;
66e08cae41SH. Peter Anvin
67e08cae41SH. Peter Anvin for (p = hypervisors; p < hypervisors + ARRAY_SIZE(hypervisors); p++) {
68*30978346SZhenzhong Duan if (unlikely(nopv) && !(*p)->ignore_nopv)
69*30978346SZhenzhong Duan continue;
70*30978346SZhenzhong Duan
71f72e38e8SJuergen Gross pri = (*p)->detect();
72f72e38e8SJuergen Gross if (pri > max_pri) {
739df56f19SJason Wang max_pri = pri;
74f72e38e8SJuergen Gross h = *p;
75e08cae41SH. Peter Anvin }
76e08cae41SH. Peter Anvin }
779df56f19SJason Wang
78f72e38e8SJuergen Gross if (h)
79f72e38e8SJuergen Gross pr_info("Hypervisor detected: %s\n", h->name);
80f72e38e8SJuergen Gross
81f72e38e8SJuergen Gross return h;
82f72e38e8SJuergen Gross }
83f72e38e8SJuergen Gross
copy_array(const void * src,void * target,unsigned int size)84f72e38e8SJuergen Gross static void __init copy_array(const void *src, void *target, unsigned int size)
85f72e38e8SJuergen Gross {
86f72e38e8SJuergen Gross unsigned int i, n = size / sizeof(void *);
87f72e38e8SJuergen Gross const void * const *from = (const void * const *)src;
88f72e38e8SJuergen Gross const void **to = (const void **)target;
89f72e38e8SJuergen Gross
90f72e38e8SJuergen Gross for (i = 0; i < n; i++)
91f72e38e8SJuergen Gross if (from[i])
92f72e38e8SJuergen Gross to[i] = from[i];
93eca0cd02SAlok Kataria }
94eca0cd02SAlok Kataria
init_hypervisor_platform(void)952d826404SThomas Gleixner void __init init_hypervisor_platform(void)
962d826404SThomas Gleixner {
97f72e38e8SJuergen Gross const struct hypervisor_x86 *h;
98e08cae41SH. Peter Anvin
99f72e38e8SJuergen Gross h = detect_hypervisor_vendor();
100e08cae41SH. Peter Anvin
101f72e38e8SJuergen Gross if (!h)
102e08cae41SH. Peter Anvin return;
103e08cae41SH. Peter Anvin
104f72e38e8SJuergen Gross copy_array(&h->init, &x86_init.hyper, sizeof(h->init));
105f72e38e8SJuergen Gross copy_array(&h->runtime, &x86_platform.hyper, sizeof(h->runtime));
1064cca6ea0SAlok N Kataria
10703b2a320SJuergen Gross x86_hyper_type = h->type;
108f72e38e8SJuergen Gross x86_init.hyper.init_platform();
10947ae4b05SJuergen Gross }
110