Alok Kataria | 88b094f | 2008-10-27 10:41:46 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Common hypervisor code |
| 3 | * |
| 4 | * Copyright (C) 2008, VMware, Inc. |
| 5 | * Author : Alok N Kataria <akataria@vmware.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or |
| 15 | * NON INFRINGEMENT. See the GNU General Public License for more |
| 16 | * details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | * |
| 22 | */ |
| 23 | |
Paul Gortmaker | 186f436 | 2016-07-13 20:18:56 -0400 | [diff] [blame] | 24 | #include <linux/init.h> |
| 25 | #include <linux/export.h> |
Alok Kataria | 88b094f | 2008-10-27 10:41:46 -0700 | [diff] [blame] | 26 | #include <asm/processor.h> |
Hannes Eder | 4e42ebd | 2008-11-21 22:56:17 +0100 | [diff] [blame] | 27 | #include <asm/hypervisor.h> |
Alok Kataria | 88b094f | 2008-10-27 10:41:46 -0700 | [diff] [blame] | 28 | |
H. Peter Anvin | e08cae4 | 2010-05-07 16:57:28 -0700 | [diff] [blame] | 29 | static const __initconst struct hypervisor_x86 * const hypervisors[] = |
Alok Kataria | 88b094f | 2008-10-27 10:41:46 -0700 | [diff] [blame] | 30 | { |
Vitaly Kuznetsov | 5e57f1d | 2017-03-14 18:35:38 +0100 | [diff] [blame] | 31 | #ifdef CONFIG_XEN_PV |
Vitaly Kuznetsov | 0991d22 | 2017-03-14 18:35:36 +0100 | [diff] [blame] | 32 | &x86_hyper_xen_pv, |
Vitaly Kuznetsov | 5e57f1d | 2017-03-14 18:35:38 +0100 | [diff] [blame] | 33 | #endif |
| 34 | #ifdef CONFIG_XEN_PVHVM |
Vitaly Kuznetsov | 0991d22 | 2017-03-14 18:35:36 +0100 | [diff] [blame] | 35 | &x86_hyper_xen_hvm, |
Jeremy Fitzhardinge | b43275d | 2010-07-26 10:38:45 -0700 | [diff] [blame] | 36 | #endif |
Anupam Chanda | 24a42ba | 2011-07-08 11:42:50 -0700 | [diff] [blame] | 37 | &x86_hyper_vmware, |
| 38 | &x86_hyper_ms_hyperv, |
Avi Kivity | d63d3e6 | 2012-07-18 11:48:50 +0300 | [diff] [blame] | 39 | #ifdef CONFIG_KVM_GUEST |
Prarit Bhargava | fc73373 | 2012-07-06 13:47:39 -0400 | [diff] [blame] | 40 | &x86_hyper_kvm, |
Avi Kivity | d63d3e6 | 2012-07-18 11:48:50 +0300 | [diff] [blame] | 41 | #endif |
Jan Kiszka | 4a36260 | 2017-11-27 09:11:46 +0100 | [diff] [blame] | 42 | #ifdef CONFIG_JAILHOUSE_GUEST |
| 43 | &x86_hyper_jailhouse, |
| 44 | #endif |
Zhao Yakui | ec7972c | 2019-04-30 11:45:24 +0800 | [diff] [blame] | 45 | #ifdef CONFIG_ACRN_GUEST |
| 46 | &x86_hyper_acrn, |
| 47 | #endif |
H. Peter Anvin | e08cae4 | 2010-05-07 16:57:28 -0700 | [diff] [blame] | 48 | }; |
Alok Kataria | 88b094f | 2008-10-27 10:41:46 -0700 | [diff] [blame] | 49 | |
Juergen Gross | 03b2a32 | 2017-11-09 14:27:36 +0100 | [diff] [blame] | 50 | enum x86_hypervisor_type x86_hyper_type; |
| 51 | EXPORT_SYMBOL(x86_hyper_type); |
H. Peter Anvin | e08cae4 | 2010-05-07 16:57:28 -0700 | [diff] [blame] | 52 | |
Zhenzhong Duan | 3097834 | 2019-07-11 20:02:09 +0800 | [diff] [blame] | 53 | bool __initdata nopv; |
| 54 | static __init int parse_nopv(char *arg) |
| 55 | { |
| 56 | nopv = true; |
| 57 | return 0; |
| 58 | } |
| 59 | early_param("nopv", parse_nopv); |
| 60 | |
Juergen Gross | f72e38e | 2017-11-09 14:27:35 +0100 | [diff] [blame] | 61 | static inline const struct hypervisor_x86 * __init |
H. Peter Anvin | e08cae4 | 2010-05-07 16:57:28 -0700 | [diff] [blame] | 62 | detect_hypervisor_vendor(void) |
Alok Kataria | eca0cd0 | 2008-10-31 12:01:58 -0700 | [diff] [blame] | 63 | { |
Juergen Gross | f72e38e | 2017-11-09 14:27:35 +0100 | [diff] [blame] | 64 | const struct hypervisor_x86 *h = NULL, * const *p; |
Jason Wang | 9df56f1 | 2013-07-25 16:54:35 +0800 | [diff] [blame] | 65 | uint32_t pri, max_pri = 0; |
H. Peter Anvin | e08cae4 | 2010-05-07 16:57:28 -0700 | [diff] [blame] | 66 | |
| 67 | for (p = hypervisors; p < hypervisors + ARRAY_SIZE(hypervisors); p++) { |
Zhenzhong Duan | 3097834 | 2019-07-11 20:02:09 +0800 | [diff] [blame] | 68 | if (unlikely(nopv) && !(*p)->ignore_nopv) |
| 69 | continue; |
| 70 | |
Juergen Gross | f72e38e | 2017-11-09 14:27:35 +0100 | [diff] [blame] | 71 | pri = (*p)->detect(); |
| 72 | if (pri > max_pri) { |
Jason Wang | 9df56f1 | 2013-07-25 16:54:35 +0800 | [diff] [blame] | 73 | max_pri = pri; |
Juergen Gross | f72e38e | 2017-11-09 14:27:35 +0100 | [diff] [blame] | 74 | h = *p; |
H. Peter Anvin | e08cae4 | 2010-05-07 16:57:28 -0700 | [diff] [blame] | 75 | } |
| 76 | } |
Jason Wang | 9df56f1 | 2013-07-25 16:54:35 +0800 | [diff] [blame] | 77 | |
Juergen Gross | f72e38e | 2017-11-09 14:27:35 +0100 | [diff] [blame] | 78 | if (h) |
| 79 | pr_info("Hypervisor detected: %s\n", h->name); |
| 80 | |
| 81 | return h; |
| 82 | } |
| 83 | |
| 84 | static void __init copy_array(const void *src, void *target, unsigned int size) |
| 85 | { |
| 86 | unsigned int i, n = size / sizeof(void *); |
| 87 | const void * const *from = (const void * const *)src; |
| 88 | const void **to = (const void **)target; |
| 89 | |
| 90 | for (i = 0; i < n; i++) |
| 91 | if (from[i]) |
| 92 | to[i] = from[i]; |
Alok Kataria | eca0cd0 | 2008-10-31 12:01:58 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Thomas Gleixner | 2d82640 | 2009-08-20 17:06:25 +0200 | [diff] [blame] | 95 | void __init init_hypervisor_platform(void) |
| 96 | { |
Juergen Gross | f72e38e | 2017-11-09 14:27:35 +0100 | [diff] [blame] | 97 | const struct hypervisor_x86 *h; |
H. Peter Anvin | e08cae4 | 2010-05-07 16:57:28 -0700 | [diff] [blame] | 98 | |
Juergen Gross | f72e38e | 2017-11-09 14:27:35 +0100 | [diff] [blame] | 99 | h = detect_hypervisor_vendor(); |
H. Peter Anvin | e08cae4 | 2010-05-07 16:57:28 -0700 | [diff] [blame] | 100 | |
Juergen Gross | f72e38e | 2017-11-09 14:27:35 +0100 | [diff] [blame] | 101 | if (!h) |
H. Peter Anvin | e08cae4 | 2010-05-07 16:57:28 -0700 | [diff] [blame] | 102 | return; |
| 103 | |
Juergen Gross | f72e38e | 2017-11-09 14:27:35 +0100 | [diff] [blame] | 104 | copy_array(&h->init, &x86_init.hyper, sizeof(h->init)); |
| 105 | copy_array(&h->runtime, &x86_platform.hyper, sizeof(h->runtime)); |
Alok N Kataria | 4cca6ea | 2013-01-17 15:44:42 -0800 | [diff] [blame] | 106 | |
Juergen Gross | 03b2a32 | 2017-11-09 14:27:36 +0100 | [diff] [blame] | 107 | x86_hyper_type = h->type; |
Juergen Gross | f72e38e | 2017-11-09 14:27:35 +0100 | [diff] [blame] | 108 | x86_init.hyper.init_platform(); |
Juergen Gross | 47ae4b0 | 2016-08-29 08:48:43 +0200 | [diff] [blame] | 109 | } |