Gentoo Archives: gentoo-commits

From: Mike Pagano <mpagano@g.o>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] proj/linux-patches:4.19 commit in: /
Date: Mon, 24 Feb 2020 11:06:58
Message-Id: 1582542395.86fa72f29ab4a860912cb34ce60fbd2053ddf2d3.mpagano@gentoo
1 commit: 86fa72f29ab4a860912cb34ce60fbd2053ddf2d3
2 Author: Mike Pagano <mpagano <AT> gentoo <DOT> org>
3 AuthorDate: Mon Feb 24 11:06:35 2020 +0000
4 Commit: Mike Pagano <mpagano <AT> gentoo <DOT> org>
5 CommitDate: Mon Feb 24 11:06:35 2020 +0000
6 URL: https://gitweb.gentoo.org/proj/linux-patches.git/commit/?id=86fa72f2
7
8 Linux patch 4.19.106
9
10 Signed-off-by: Mike Pagano <mpagano <AT> gentoo.org>
11
12 0000_README | 4 +
13 1105_linux-4.19.106.patch | 13713 ++++++++++++++++++++++++++++++++++++++++++++
14 2 files changed, 13717 insertions(+)
15
16 diff --git a/0000_README b/0000_README
17 index f83fdcc..3213eab 100644
18 --- a/0000_README
19 +++ b/0000_README
20 @@ -459,6 +459,10 @@ Patch: 1104_linux-4.19.105.patch
21 From: https://www.kernel.org
22 Desc: Linux 4.19.105
23
24 +Patch: 1105_linux-4.19.106.patch
25 +From: https://www.kernel.org
26 +Desc: Linux 4.19.106
27 +
28 Patch: 1500_XATTR_USER_PREFIX.patch
29 From: https://bugs.gentoo.org/show_bug.cgi?id=470644
30 Desc: Support for namespace user.pax.* on tmpfs.
31
32 diff --git a/1105_linux-4.19.106.patch b/1105_linux-4.19.106.patch
33 new file mode 100644
34 index 0000000..ea910af
35 --- /dev/null
36 +++ b/1105_linux-4.19.106.patch
37 @@ -0,0 +1,13713 @@
38 +diff --git a/Makefile b/Makefile
39 +index eef7de60cd94..c010fd4a3286 100644
40 +--- a/Makefile
41 ++++ b/Makefile
42 +@@ -1,7 +1,7 @@
43 + # SPDX-License-Identifier: GPL-2.0
44 + VERSION = 4
45 + PATCHLEVEL = 19
46 +-SUBLEVEL = 105
47 ++SUBLEVEL = 106
48 + EXTRAVERSION =
49 + NAME = "People's Front"
50 +
51 +diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig
52 +index 185e552f1461..e2f7c50dbace 100644
53 +--- a/arch/arm/Kconfig
54 ++++ b/arch/arm/Kconfig
55 +@@ -61,7 +61,7 @@ config ARM
56 + select HAVE_EBPF_JIT if !CPU_ENDIAN_BE32
57 + select HAVE_CONTEXT_TRACKING
58 + select HAVE_C_RECORDMCOUNT
59 +- select HAVE_DEBUG_KMEMLEAK
60 ++ select HAVE_DEBUG_KMEMLEAK if !XIP_KERNEL
61 + select HAVE_DMA_CONTIGUOUS if MMU
62 + select HAVE_DYNAMIC_FTRACE if (!XIP_KERNEL) && !CPU_ENDIAN_BE32 && MMU
63 + select HAVE_DYNAMIC_FTRACE_WITH_REGS if HAVE_DYNAMIC_FTRACE
64 +@@ -2008,7 +2008,7 @@ config XIP_DEFLATED_DATA
65 + config KEXEC
66 + bool "Kexec system call (EXPERIMENTAL)"
67 + depends on (!SMP || PM_SLEEP_SMP)
68 +- depends on !CPU_V7M
69 ++ depends on MMU
70 + select KEXEC_CORE
71 + help
72 + kexec is a system call that implements the ability to shutdown your
73 +diff --git a/arch/arm/boot/dts/imx6qdl-zii-rdu2.dtsi b/arch/arm/boot/dts/imx6qdl-zii-rdu2.dtsi
74 +index 315d0e7615f3..bc5f2de02d43 100644
75 +--- a/arch/arm/boot/dts/imx6qdl-zii-rdu2.dtsi
76 ++++ b/arch/arm/boot/dts/imx6qdl-zii-rdu2.dtsi
77 +@@ -657,7 +657,7 @@
78 + pinctrl-0 = <&pinctrl_usdhc2>;
79 + bus-width = <4>;
80 + cd-gpios = <&gpio2 2 GPIO_ACTIVE_LOW>;
81 +- wp-gpios = <&gpio2 3 GPIO_ACTIVE_HIGH>;
82 ++ disable-wp;
83 + vmmc-supply = <&reg_3p3v_sd>;
84 + vqmmc-supply = <&reg_3p3v>;
85 + no-1-8-v;
86 +@@ -670,7 +670,7 @@
87 + pinctrl-0 = <&pinctrl_usdhc3>;
88 + bus-width = <4>;
89 + cd-gpios = <&gpio2 0 GPIO_ACTIVE_LOW>;
90 +- wp-gpios = <&gpio2 1 GPIO_ACTIVE_HIGH>;
91 ++ disable-wp;
92 + vmmc-supply = <&reg_3p3v_sd>;
93 + vqmmc-supply = <&reg_3p3v>;
94 + no-1-8-v;
95 +@@ -804,6 +804,7 @@
96 + &usbh1 {
97 + vbus-supply = <&reg_5p0v_main>;
98 + disable-over-current;
99 ++ maximum-speed = "full-speed";
100 + status = "okay";
101 + };
102 +
103 +@@ -1081,7 +1082,6 @@
104 + MX6QDL_PAD_SD2_DAT1__SD2_DATA1 0x17059
105 + MX6QDL_PAD_SD2_DAT2__SD2_DATA2 0x17059
106 + MX6QDL_PAD_SD2_DAT3__SD2_DATA3 0x17059
107 +- MX6QDL_PAD_NANDF_D3__GPIO2_IO03 0x40010040
108 + MX6QDL_PAD_NANDF_D2__GPIO2_IO02 0x40010040
109 + >;
110 + };
111 +@@ -1094,7 +1094,6 @@
112 + MX6QDL_PAD_SD3_DAT1__SD3_DATA1 0x17059
113 + MX6QDL_PAD_SD3_DAT2__SD3_DATA2 0x17059
114 + MX6QDL_PAD_SD3_DAT3__SD3_DATA3 0x17059
115 +- MX6QDL_PAD_NANDF_D1__GPIO2_IO01 0x40010040
116 + MX6QDL_PAD_NANDF_D0__GPIO2_IO00 0x40010040
117 +
118 + >;
119 +diff --git a/arch/arm/boot/dts/r8a7779.dtsi b/arch/arm/boot/dts/r8a7779.dtsi
120 +index 03919714645a..f1c9b2bc542c 100644
121 +--- a/arch/arm/boot/dts/r8a7779.dtsi
122 ++++ b/arch/arm/boot/dts/r8a7779.dtsi
123 +@@ -68,6 +68,14 @@
124 + <0xf0000100 0x100>;
125 + };
126 +
127 ++ timer@f0000200 {
128 ++ compatible = "arm,cortex-a9-global-timer";
129 ++ reg = <0xf0000200 0x100>;
130 ++ interrupts = <GIC_PPI 11
131 ++ (GIC_CPU_MASK_SIMPLE(4) | IRQ_TYPE_EDGE_RISING)>;
132 ++ clocks = <&cpg_clocks R8A7779_CLK_ZS>;
133 ++ };
134 ++
135 + timer@f0000600 {
136 + compatible = "arm,cortex-a9-twd-timer";
137 + reg = <0xf0000600 0x20>;
138 +diff --git a/arch/arm/boot/dts/stm32f469-disco.dts b/arch/arm/boot/dts/stm32f469-disco.dts
139 +index 3ee768cb86fc..eea979ef5512 100644
140 +--- a/arch/arm/boot/dts/stm32f469-disco.dts
141 ++++ b/arch/arm/boot/dts/stm32f469-disco.dts
142 +@@ -75,6 +75,13 @@
143 + regulator-max-microvolt = <3300000>;
144 + };
145 +
146 ++ vdd_dsi: vdd-dsi {
147 ++ compatible = "regulator-fixed";
148 ++ regulator-name = "vdd_dsi";
149 ++ regulator-min-microvolt = <3300000>;
150 ++ regulator-max-microvolt = <3300000>;
151 ++ };
152 ++
153 + soc {
154 + dma-ranges = <0xc0000000 0x0 0x10000000>;
155 + };
156 +@@ -154,6 +161,7 @@
157 + compatible = "orisetech,otm8009a";
158 + reg = <0>; /* dsi virtual channel (0..3) */
159 + reset-gpios = <&gpioh 7 GPIO_ACTIVE_LOW>;
160 ++ power-supply = <&vdd_dsi>;
161 + status = "okay";
162 +
163 + port {
164 +diff --git a/arch/arm/boot/dts/sun8i-h3.dtsi b/arch/arm/boot/dts/sun8i-h3.dtsi
165 +index 9233ba30a857..11172fbdc03a 100644
166 +--- a/arch/arm/boot/dts/sun8i-h3.dtsi
167 ++++ b/arch/arm/boot/dts/sun8i-h3.dtsi
168 +@@ -80,7 +80,7 @@
169 + #cooling-cells = <2>;
170 + };
171 +
172 +- cpu@1 {
173 ++ cpu1: cpu@1 {
174 + compatible = "arm,cortex-a7";
175 + device_type = "cpu";
176 + reg = <1>;
177 +@@ -90,7 +90,7 @@
178 + #cooling-cells = <2>;
179 + };
180 +
181 +- cpu@2 {
182 ++ cpu2: cpu@2 {
183 + compatible = "arm,cortex-a7";
184 + device_type = "cpu";
185 + reg = <2>;
186 +@@ -100,7 +100,7 @@
187 + #cooling-cells = <2>;
188 + };
189 +
190 +- cpu@3 {
191 ++ cpu3: cpu@3 {
192 + compatible = "arm,cortex-a7";
193 + device_type = "cpu";
194 + reg = <3>;
195 +@@ -111,6 +111,15 @@
196 + };
197 + };
198 +
199 ++ pmu {
200 ++ compatible = "arm,cortex-a7-pmu";
201 ++ interrupts = <GIC_SPI 120 IRQ_TYPE_LEVEL_HIGH>,
202 ++ <GIC_SPI 121 IRQ_TYPE_LEVEL_HIGH>,
203 ++ <GIC_SPI 122 IRQ_TYPE_LEVEL_HIGH>,
204 ++ <GIC_SPI 123 IRQ_TYPE_LEVEL_HIGH>;
205 ++ interrupt-affinity = <&cpu0>, <&cpu1>, <&cpu2>, <&cpu3>;
206 ++ };
207 ++
208 + timer {
209 + compatible = "arm,armv7-timer";
210 + interrupts = <GIC_PPI 13 (GIC_CPU_MASK_SIMPLE(4) | IRQ_TYPE_LEVEL_LOW)>,
211 +diff --git a/arch/arm64/boot/dts/allwinner/sun50i-h6.dtsi b/arch/arm64/boot/dts/allwinner/sun50i-h6.dtsi
212 +index 72813e7aefb8..bd4391269611 100644
213 +--- a/arch/arm64/boot/dts/allwinner/sun50i-h6.dtsi
214 ++++ b/arch/arm64/boot/dts/allwinner/sun50i-h6.dtsi
215 +@@ -69,6 +69,16 @@
216 + clock-output-names = "osc32k";
217 + };
218 +
219 ++ pmu {
220 ++ compatible = "arm,cortex-a53-pmu",
221 ++ "arm,armv8-pmuv3";
222 ++ interrupts = <GIC_SPI 140 IRQ_TYPE_LEVEL_HIGH>,
223 ++ <GIC_SPI 141 IRQ_TYPE_LEVEL_HIGH>,
224 ++ <GIC_SPI 142 IRQ_TYPE_LEVEL_HIGH>,
225 ++ <GIC_SPI 143 IRQ_TYPE_LEVEL_HIGH>;
226 ++ interrupt-affinity = <&cpu0>, <&cpu1>, <&cpu2>, <&cpu3>;
227 ++ };
228 ++
229 + psci {
230 + compatible = "arm,psci-0.2";
231 + method = "smc";
232 +diff --git a/arch/arm64/boot/dts/qcom/msm8996.dtsi b/arch/arm64/boot/dts/qcom/msm8996.dtsi
233 +index 8c86c41a0d25..3e7baabf6450 100644
234 +--- a/arch/arm64/boot/dts/qcom/msm8996.dtsi
235 ++++ b/arch/arm64/boot/dts/qcom/msm8996.dtsi
236 +@@ -918,6 +918,8 @@
237 + interrupts = <0 138 IRQ_TYPE_LEVEL_HIGH>;
238 + phys = <&hsusb_phy2>;
239 + phy-names = "usb2-phy";
240 ++ snps,dis_u2_susphy_quirk;
241 ++ snps,dis_enblslpm_quirk;
242 + };
243 + };
244 +
245 +@@ -947,6 +949,8 @@
246 + interrupts = <0 131 IRQ_TYPE_LEVEL_HIGH>;
247 + phys = <&hsusb_phy1>, <&ssusb_phy_0>;
248 + phy-names = "usb2-phy", "usb3-phy";
249 ++ snps,dis_u2_susphy_quirk;
250 ++ snps,dis_enblslpm_quirk;
251 + };
252 + };
253 +
254 +diff --git a/arch/arm64/include/asm/alternative.h b/arch/arm64/include/asm/alternative.h
255 +index 4b650ec1d7dd..887a8512bf10 100644
256 +--- a/arch/arm64/include/asm/alternative.h
257 ++++ b/arch/arm64/include/asm/alternative.h
258 +@@ -35,13 +35,16 @@ void apply_alternatives_module(void *start, size_t length);
259 + static inline void apply_alternatives_module(void *start, size_t length) { }
260 + #endif
261 +
262 +-#define ALTINSTR_ENTRY(feature,cb) \
263 ++#define ALTINSTR_ENTRY(feature) \
264 + " .word 661b - .\n" /* label */ \
265 +- " .if " __stringify(cb) " == 0\n" \
266 + " .word 663f - .\n" /* new instruction */ \
267 +- " .else\n" \
268 ++ " .hword " __stringify(feature) "\n" /* feature bit */ \
269 ++ " .byte 662b-661b\n" /* source len */ \
270 ++ " .byte 664f-663f\n" /* replacement len */
271 ++
272 ++#define ALTINSTR_ENTRY_CB(feature, cb) \
273 ++ " .word 661b - .\n" /* label */ \
274 + " .word " __stringify(cb) "- .\n" /* callback */ \
275 +- " .endif\n" \
276 + " .hword " __stringify(feature) "\n" /* feature bit */ \
277 + " .byte 662b-661b\n" /* source len */ \
278 + " .byte 664f-663f\n" /* replacement len */
279 +@@ -62,15 +65,14 @@ static inline void apply_alternatives_module(void *start, size_t length) { }
280 + *
281 + * Alternatives with callbacks do not generate replacement instructions.
282 + */
283 +-#define __ALTERNATIVE_CFG(oldinstr, newinstr, feature, cfg_enabled, cb) \
284 ++#define __ALTERNATIVE_CFG(oldinstr, newinstr, feature, cfg_enabled) \
285 + ".if "__stringify(cfg_enabled)" == 1\n" \
286 + "661:\n\t" \
287 + oldinstr "\n" \
288 + "662:\n" \
289 + ".pushsection .altinstructions,\"a\"\n" \
290 +- ALTINSTR_ENTRY(feature,cb) \
291 ++ ALTINSTR_ENTRY(feature) \
292 + ".popsection\n" \
293 +- " .if " __stringify(cb) " == 0\n" \
294 + ".pushsection .altinstr_replacement, \"a\"\n" \
295 + "663:\n\t" \
296 + newinstr "\n" \
297 +@@ -78,17 +80,25 @@ static inline void apply_alternatives_module(void *start, size_t length) { }
298 + ".popsection\n\t" \
299 + ".org . - (664b-663b) + (662b-661b)\n\t" \
300 + ".org . - (662b-661b) + (664b-663b)\n" \
301 +- ".else\n\t" \
302 ++ ".endif\n"
303 ++
304 ++#define __ALTERNATIVE_CFG_CB(oldinstr, feature, cfg_enabled, cb) \
305 ++ ".if "__stringify(cfg_enabled)" == 1\n" \
306 ++ "661:\n\t" \
307 ++ oldinstr "\n" \
308 ++ "662:\n" \
309 ++ ".pushsection .altinstructions,\"a\"\n" \
310 ++ ALTINSTR_ENTRY_CB(feature, cb) \
311 ++ ".popsection\n" \
312 + "663:\n\t" \
313 + "664:\n\t" \
314 +- ".endif\n" \
315 + ".endif\n"
316 +
317 + #define _ALTERNATIVE_CFG(oldinstr, newinstr, feature, cfg, ...) \
318 +- __ALTERNATIVE_CFG(oldinstr, newinstr, feature, IS_ENABLED(cfg), 0)
319 ++ __ALTERNATIVE_CFG(oldinstr, newinstr, feature, IS_ENABLED(cfg))
320 +
321 + #define ALTERNATIVE_CB(oldinstr, cb) \
322 +- __ALTERNATIVE_CFG(oldinstr, "NOT_AN_INSTRUCTION", ARM64_CB_PATCH, 1, cb)
323 ++ __ALTERNATIVE_CFG_CB(oldinstr, ARM64_CB_PATCH, 1, cb)
324 + #else
325 +
326 + #include <asm/assembler.h>
327 +diff --git a/arch/microblaze/kernel/cpu/cache.c b/arch/microblaze/kernel/cpu/cache.c
328 +index 0bde47e4fa69..dcba53803fa5 100644
329 +--- a/arch/microblaze/kernel/cpu/cache.c
330 ++++ b/arch/microblaze/kernel/cpu/cache.c
331 +@@ -92,7 +92,8 @@ static inline void __disable_dcache_nomsr(void)
332 + #define CACHE_LOOP_LIMITS(start, end, cache_line_length, cache_size) \
333 + do { \
334 + int align = ~(cache_line_length - 1); \
335 +- end = min(start + cache_size, end); \
336 ++ if (start < UINT_MAX - cache_size) \
337 ++ end = min(start + cache_size, end); \
338 + start &= align; \
339 + } while (0)
340 +
341 +diff --git a/arch/mips/loongson64/loongson-3/platform.c b/arch/mips/loongson64/loongson-3/platform.c
342 +index 25a97cc0ee33..0db4cc3196eb 100644
343 +--- a/arch/mips/loongson64/loongson-3/platform.c
344 ++++ b/arch/mips/loongson64/loongson-3/platform.c
345 +@@ -31,6 +31,9 @@ static int __init loongson3_platform_init(void)
346 + continue;
347 +
348 + pdev = kzalloc(sizeof(struct platform_device), GFP_KERNEL);
349 ++ if (!pdev)
350 ++ return -ENOMEM;
351 ++
352 + pdev->name = loongson_sysconf.sensors[i].name;
353 + pdev->id = loongson_sysconf.sensors[i].id;
354 + pdev->dev.platform_data = &loongson_sysconf.sensors[i];
355 +diff --git a/arch/powerpc/kernel/eeh_driver.c b/arch/powerpc/kernel/eeh_driver.c
356 +index af1f3d5f9a0f..377d23f58197 100644
357 +--- a/arch/powerpc/kernel/eeh_driver.c
358 ++++ b/arch/powerpc/kernel/eeh_driver.c
359 +@@ -554,12 +554,6 @@ static void *eeh_rmv_device(struct eeh_dev *edev, void *userdata)
360 +
361 + pci_iov_remove_virtfn(edev->physfn, pdn->vf_index);
362 + edev->pdev = NULL;
363 +-
364 +- /*
365 +- * We have to set the VF PE number to invalid one, which is
366 +- * required to plug the VF successfully.
367 +- */
368 +- pdn->pe_number = IODA_INVALID_PE;
369 + #endif
370 + if (rmv_data)
371 + list_add(&edev->rmv_list, &rmv_data->edev_list);
372 +diff --git a/arch/powerpc/kernel/pci_dn.c b/arch/powerpc/kernel/pci_dn.c
373 +index ab147a1909c8..7cecc3bd953b 100644
374 +--- a/arch/powerpc/kernel/pci_dn.c
375 ++++ b/arch/powerpc/kernel/pci_dn.c
376 +@@ -257,9 +257,22 @@ void remove_dev_pci_data(struct pci_dev *pdev)
377 + continue;
378 +
379 + #ifdef CONFIG_EEH
380 +- /* Release EEH device for the VF */
381 ++ /*
382 ++ * Release EEH state for this VF. The PCI core
383 ++ * has already torn down the pci_dev for this VF, but
384 ++ * we're responsible to removing the eeh_dev since it
385 ++ * has the same lifetime as the pci_dn that spawned it.
386 ++ */
387 + edev = pdn_to_eeh_dev(pdn);
388 + if (edev) {
389 ++ /*
390 ++ * We allocate pci_dn's for the totalvfs count,
391 ++ * but only only the vfs that were activated
392 ++ * have a configured PE.
393 ++ */
394 ++ if (edev->pe)
395 ++ eeh_rmv_from_parent_pe(edev);
396 ++
397 + pdn->edev = NULL;
398 + kfree(edev);
399 + }
400 +diff --git a/arch/powerpc/platforms/powernv/pci-ioda.c b/arch/powerpc/platforms/powernv/pci-ioda.c
401 +index ee63749a2d47..ecd211c5f24a 100644
402 +--- a/arch/powerpc/platforms/powernv/pci-ioda.c
403 ++++ b/arch/powerpc/platforms/powernv/pci-ioda.c
404 +@@ -1552,6 +1552,10 @@ static void pnv_ioda_setup_vf_PE(struct pci_dev *pdev, u16 num_vfs)
405 +
406 + /* Reserve PE for each VF */
407 + for (vf_index = 0; vf_index < num_vfs; vf_index++) {
408 ++ int vf_devfn = pci_iov_virtfn_devfn(pdev, vf_index);
409 ++ int vf_bus = pci_iov_virtfn_bus(pdev, vf_index);
410 ++ struct pci_dn *vf_pdn;
411 ++
412 + if (pdn->m64_single_mode)
413 + pe_num = pdn->pe_num_map[vf_index];
414 + else
415 +@@ -1564,13 +1568,11 @@ static void pnv_ioda_setup_vf_PE(struct pci_dev *pdev, u16 num_vfs)
416 + pe->pbus = NULL;
417 + pe->parent_dev = pdev;
418 + pe->mve_number = -1;
419 +- pe->rid = (pci_iov_virtfn_bus(pdev, vf_index) << 8) |
420 +- pci_iov_virtfn_devfn(pdev, vf_index);
421 ++ pe->rid = (vf_bus << 8) | vf_devfn;
422 +
423 + pe_info(pe, "VF %04d:%02d:%02d.%d associated with PE#%x\n",
424 + hose->global_number, pdev->bus->number,
425 +- PCI_SLOT(pci_iov_virtfn_devfn(pdev, vf_index)),
426 +- PCI_FUNC(pci_iov_virtfn_devfn(pdev, vf_index)), pe_num);
427 ++ PCI_SLOT(vf_devfn), PCI_FUNC(vf_devfn), pe_num);
428 +
429 + if (pnv_ioda_configure_pe(phb, pe)) {
430 + /* XXX What do we do here ? */
431 +@@ -1584,6 +1586,15 @@ static void pnv_ioda_setup_vf_PE(struct pci_dev *pdev, u16 num_vfs)
432 + list_add_tail(&pe->list, &phb->ioda.pe_list);
433 + mutex_unlock(&phb->ioda.pe_list_mutex);
434 +
435 ++ /* associate this pe to it's pdn */
436 ++ list_for_each_entry(vf_pdn, &pdn->parent->child_list, list) {
437 ++ if (vf_pdn->busno == vf_bus &&
438 ++ vf_pdn->devfn == vf_devfn) {
439 ++ vf_pdn->pe_number = pe_num;
440 ++ break;
441 ++ }
442 ++ }
443 ++
444 + pnv_pci_ioda2_setup_dma_pe(phb, pe);
445 + }
446 + }
447 +@@ -3004,9 +3015,6 @@ static void pnv_pci_ioda_fixup_iov_resources(struct pci_dev *pdev)
448 + struct pci_dn *pdn;
449 + int mul, total_vfs;
450 +
451 +- if (!pdev->is_physfn || pci_dev_is_added(pdev))
452 +- return;
453 +-
454 + pdn = pci_get_pdn(pdev);
455 + pdn->vfs_expanded = 0;
456 + pdn->m64_single_mode = false;
457 +@@ -3081,6 +3089,30 @@ truncate_iov:
458 + res->end = res->start - 1;
459 + }
460 + }
461 ++
462 ++static void pnv_pci_ioda_fixup_iov(struct pci_dev *pdev)
463 ++{
464 ++ if (WARN_ON(pci_dev_is_added(pdev)))
465 ++ return;
466 ++
467 ++ if (pdev->is_virtfn) {
468 ++ struct pnv_ioda_pe *pe = pnv_ioda_get_pe(pdev);
469 ++
470 ++ /*
471 ++ * VF PEs are single-device PEs so their pdev pointer needs to
472 ++ * be set. The pdev doesn't exist when the PE is allocated (in
473 ++ * (pcibios_sriov_enable()) so we fix it up here.
474 ++ */
475 ++ pe->pdev = pdev;
476 ++ WARN_ON(!(pe->flags & PNV_IODA_PE_VF));
477 ++ } else if (pdev->is_physfn) {
478 ++ /*
479 ++ * For PFs adjust their allocated IOV resources to match what
480 ++ * the PHB can support using it's M64 BAR table.
481 ++ */
482 ++ pnv_pci_ioda_fixup_iov_resources(pdev);
483 ++ }
484 ++}
485 + #endif /* CONFIG_PCI_IOV */
486 +
487 + static void pnv_ioda_setup_pe_res(struct pnv_ioda_pe *pe,
488 +@@ -3974,7 +4006,7 @@ static void __init pnv_pci_init_ioda_phb(struct device_node *np,
489 + ppc_md.pcibios_default_alignment = pnv_pci_default_alignment;
490 +
491 + #ifdef CONFIG_PCI_IOV
492 +- ppc_md.pcibios_fixup_sriov = pnv_pci_ioda_fixup_iov_resources;
493 ++ ppc_md.pcibios_fixup_sriov = pnv_pci_ioda_fixup_iov;
494 + ppc_md.pcibios_iov_resource_alignment = pnv_pci_iov_resource_alignment;
495 + ppc_md.pcibios_sriov_enable = pnv_pcibios_sriov_enable;
496 + ppc_md.pcibios_sriov_disable = pnv_pcibios_sriov_disable;
497 +diff --git a/arch/powerpc/platforms/powernv/pci.c b/arch/powerpc/platforms/powernv/pci.c
498 +index c846300b7836..b6fa900af5da 100644
499 +--- a/arch/powerpc/platforms/powernv/pci.c
500 ++++ b/arch/powerpc/platforms/powernv/pci.c
501 +@@ -820,24 +820,6 @@ void pnv_pci_dma_dev_setup(struct pci_dev *pdev)
502 + {
503 + struct pci_controller *hose = pci_bus_to_host(pdev->bus);
504 + struct pnv_phb *phb = hose->private_data;
505 +-#ifdef CONFIG_PCI_IOV
506 +- struct pnv_ioda_pe *pe;
507 +- struct pci_dn *pdn;
508 +-
509 +- /* Fix the VF pdn PE number */
510 +- if (pdev->is_virtfn) {
511 +- pdn = pci_get_pdn(pdev);
512 +- WARN_ON(pdn->pe_number != IODA_INVALID_PE);
513 +- list_for_each_entry(pe, &phb->ioda.pe_list, list) {
514 +- if (pe->rid == ((pdev->bus->number << 8) |
515 +- (pdev->devfn & 0xff))) {
516 +- pdn->pe_number = pe->pe_number;
517 +- pe->pdev = pdev;
518 +- break;
519 +- }
520 +- }
521 +- }
522 +-#endif /* CONFIG_PCI_IOV */
523 +
524 + if (phb && phb->dma_dev_setup)
525 + phb->dma_dev_setup(phb, pdev);
526 +diff --git a/arch/s390/Makefile b/arch/s390/Makefile
527 +index e6c2e8925fef..4bccde36cb16 100644
528 +--- a/arch/s390/Makefile
529 ++++ b/arch/s390/Makefile
530 +@@ -63,7 +63,7 @@ cflags-y += -Wa,-I$(srctree)/arch/$(ARCH)/include
531 + #
532 + cflags-$(CONFIG_FRAME_POINTER) += -fno-optimize-sibling-calls
533 +
534 +-ifeq ($(call cc-option-yn,-mpacked-stack),y)
535 ++ifeq ($(call cc-option-yn,-mpacked-stack -mbackchain -msoft-float),y)
536 + cflags-$(CONFIG_PACK_STACK) += -mpacked-stack -D__PACK_STACK
537 + aflags-$(CONFIG_PACK_STACK) += -D__PACK_STACK
538 + endif
539 +diff --git a/arch/s390/kernel/mcount.S b/arch/s390/kernel/mcount.S
540 +index e93fbf02490c..83afd5b78e16 100644
541 +--- a/arch/s390/kernel/mcount.S
542 ++++ b/arch/s390/kernel/mcount.S
543 +@@ -25,6 +25,12 @@ ENTRY(ftrace_stub)
544 + #define STACK_PTREGS (STACK_FRAME_OVERHEAD)
545 + #define STACK_PTREGS_GPRS (STACK_PTREGS + __PT_GPRS)
546 + #define STACK_PTREGS_PSW (STACK_PTREGS + __PT_PSW)
547 ++#ifdef __PACK_STACK
548 ++/* allocate just enough for r14, r15 and backchain */
549 ++#define TRACED_FUNC_FRAME_SIZE 24
550 ++#else
551 ++#define TRACED_FUNC_FRAME_SIZE STACK_FRAME_OVERHEAD
552 ++#endif
553 +
554 + ENTRY(_mcount)
555 + BR_EX %r14
556 +@@ -38,9 +44,16 @@ ENTRY(ftrace_caller)
557 + #if !(defined(CC_USING_HOTPATCH) || defined(CC_USING_NOP_MCOUNT))
558 + aghi %r0,MCOUNT_RETURN_FIXUP
559 + #endif
560 +- aghi %r15,-STACK_FRAME_SIZE
561 ++ # allocate stack frame for ftrace_caller to contain traced function
562 ++ aghi %r15,-TRACED_FUNC_FRAME_SIZE
563 + stg %r1,__SF_BACKCHAIN(%r15)
564 ++ stg %r0,(__SF_GPRS+8*8)(%r15)
565 ++ stg %r15,(__SF_GPRS+9*8)(%r15)
566 ++ # allocate pt_regs and stack frame for ftrace_trace_function
567 ++ aghi %r15,-STACK_FRAME_SIZE
568 + stg %r1,(STACK_PTREGS_GPRS+15*8)(%r15)
569 ++ aghi %r1,-TRACED_FUNC_FRAME_SIZE
570 ++ stg %r1,__SF_BACKCHAIN(%r15)
571 + stg %r0,(STACK_PTREGS_PSW+8)(%r15)
572 + stmg %r2,%r14,(STACK_PTREGS_GPRS+2*8)(%r15)
573 + #ifdef CONFIG_HAVE_MARCH_Z196_FEATURES
574 +diff --git a/arch/s390/kvm/interrupt.c b/arch/s390/kvm/interrupt.c
575 +index 05ea466b9e40..3515f2b55eb9 100644
576 +--- a/arch/s390/kvm/interrupt.c
577 ++++ b/arch/s390/kvm/interrupt.c
578 +@@ -2109,7 +2109,7 @@ static int flic_ais_mode_get_all(struct kvm *kvm, struct kvm_device_attr *attr)
579 + return -EINVAL;
580 +
581 + if (!test_kvm_facility(kvm, 72))
582 +- return -ENOTSUPP;
583 ++ return -EOPNOTSUPP;
584 +
585 + mutex_lock(&fi->ais_lock);
586 + ais.simm = fi->simm;
587 +@@ -2412,7 +2412,7 @@ static int modify_ais_mode(struct kvm *kvm, struct kvm_device_attr *attr)
588 + int ret = 0;
589 +
590 + if (!test_kvm_facility(kvm, 72))
591 +- return -ENOTSUPP;
592 ++ return -EOPNOTSUPP;
593 +
594 + if (copy_from_user(&req, (void __user *)attr->addr, sizeof(req)))
595 + return -EFAULT;
596 +@@ -2492,7 +2492,7 @@ static int flic_ais_mode_set_all(struct kvm *kvm, struct kvm_device_attr *attr)
597 + struct kvm_s390_ais_all ais;
598 +
599 + if (!test_kvm_facility(kvm, 72))
600 +- return -ENOTSUPP;
601 ++ return -EOPNOTSUPP;
602 +
603 + if (copy_from_user(&ais, (void __user *)attr->addr, sizeof(ais)))
604 + return -EFAULT;
605 +diff --git a/arch/s390/pci/pci_sysfs.c b/arch/s390/pci/pci_sysfs.c
606 +index 430c14b006d1..0e11fc023fe7 100644
607 +--- a/arch/s390/pci/pci_sysfs.c
608 ++++ b/arch/s390/pci/pci_sysfs.c
609 +@@ -13,6 +13,8 @@
610 + #include <linux/stat.h>
611 + #include <linux/pci.h>
612 +
613 ++#include "../../../drivers/pci/pci.h"
614 ++
615 + #include <asm/sclp.h>
616 +
617 + #define zpci_attr(name, fmt, member) \
618 +@@ -40,31 +42,50 @@ zpci_attr(segment3, "0x%02x\n", pfip[3]);
619 + static ssize_t recover_store(struct device *dev, struct device_attribute *attr,
620 + const char *buf, size_t count)
621 + {
622 ++ struct kernfs_node *kn;
623 + struct pci_dev *pdev = to_pci_dev(dev);
624 + struct zpci_dev *zdev = to_zpci(pdev);
625 +- int ret;
626 +-
627 +- if (!device_remove_file_self(dev, attr))
628 +- return count;
629 +-
630 ++ int ret = 0;
631 ++
632 ++ /* Can't use device_remove_self() here as that would lead us to lock
633 ++ * the pci_rescan_remove_lock while holding the device' kernfs lock.
634 ++ * This would create a possible deadlock with disable_slot() which is
635 ++ * not directly protected by the device' kernfs lock but takes it
636 ++ * during the device removal which happens under
637 ++ * pci_rescan_remove_lock.
638 ++ *
639 ++ * This is analogous to sdev_store_delete() in
640 ++ * drivers/scsi/scsi_sysfs.c
641 ++ */
642 ++ kn = sysfs_break_active_protection(&dev->kobj, &attr->attr);
643 ++ WARN_ON_ONCE(!kn);
644 ++ /* device_remove_file() serializes concurrent calls ignoring all but
645 ++ * the first
646 ++ */
647 ++ device_remove_file(dev, attr);
648 ++
649 ++ /* A concurrent call to recover_store() may slip between
650 ++ * sysfs_break_active_protection() and the sysfs file removal.
651 ++ * Once it unblocks from pci_lock_rescan_remove() the original pdev
652 ++ * will already be removed.
653 ++ */
654 + pci_lock_rescan_remove();
655 +- pci_stop_and_remove_bus_device(pdev);
656 +- ret = zpci_disable_device(zdev);
657 +- if (ret)
658 +- goto error;
659 +-
660 +- ret = zpci_enable_device(zdev);
661 +- if (ret)
662 +- goto error;
663 +-
664 +- pci_rescan_bus(zdev->bus);
665 ++ if (pci_dev_is_added(pdev)) {
666 ++ pci_stop_and_remove_bus_device(pdev);
667 ++ ret = zpci_disable_device(zdev);
668 ++ if (ret)
669 ++ goto out;
670 ++
671 ++ ret = zpci_enable_device(zdev);
672 ++ if (ret)
673 ++ goto out;
674 ++ pci_rescan_bus(zdev->bus);
675 ++ }
676 ++out:
677 + pci_unlock_rescan_remove();
678 +-
679 +- return count;
680 +-
681 +-error:
682 +- pci_unlock_rescan_remove();
683 +- return ret;
684 ++ if (kn)
685 ++ sysfs_unbreak_active_protection(kn);
686 ++ return ret ? ret : count;
687 + }
688 + static DEVICE_ATTR_WO(recover);
689 +
690 +diff --git a/arch/sh/include/cpu-sh2a/cpu/sh7269.h b/arch/sh/include/cpu-sh2a/cpu/sh7269.h
691 +index d516e5d48818..b887cc402b71 100644
692 +--- a/arch/sh/include/cpu-sh2a/cpu/sh7269.h
693 ++++ b/arch/sh/include/cpu-sh2a/cpu/sh7269.h
694 +@@ -78,8 +78,15 @@ enum {
695 + GPIO_FN_WDTOVF,
696 +
697 + /* CAN */
698 +- GPIO_FN_CTX1, GPIO_FN_CRX1, GPIO_FN_CTX0, GPIO_FN_CTX0_CTX1,
699 +- GPIO_FN_CRX0, GPIO_FN_CRX0_CRX1, GPIO_FN_CRX0_CRX1_CRX2,
700 ++ GPIO_FN_CTX2, GPIO_FN_CRX2,
701 ++ GPIO_FN_CTX1, GPIO_FN_CRX1,
702 ++ GPIO_FN_CTX0, GPIO_FN_CRX0,
703 ++ GPIO_FN_CTX0_CTX1, GPIO_FN_CRX0_CRX1,
704 ++ GPIO_FN_CTX0_CTX1_CTX2, GPIO_FN_CRX0_CRX1_CRX2,
705 ++ GPIO_FN_CTX2_PJ21, GPIO_FN_CRX2_PJ20,
706 ++ GPIO_FN_CTX1_PJ23, GPIO_FN_CRX1_PJ22,
707 ++ GPIO_FN_CTX0_CTX1_PJ23, GPIO_FN_CRX0_CRX1_PJ22,
708 ++ GPIO_FN_CTX0_CTX1_CTX2_PJ21, GPIO_FN_CRX0_CRX1_CRX2_PJ20,
709 +
710 + /* DMAC */
711 + GPIO_FN_TEND0, GPIO_FN_DACK0, GPIO_FN_DREQ0,
712 +diff --git a/arch/sparc/kernel/vmlinux.lds.S b/arch/sparc/kernel/vmlinux.lds.S
713 +index 61afd787bd0c..59b6df13ddea 100644
714 +--- a/arch/sparc/kernel/vmlinux.lds.S
715 ++++ b/arch/sparc/kernel/vmlinux.lds.S
716 +@@ -172,12 +172,14 @@ SECTIONS
717 + }
718 + PERCPU_SECTION(SMP_CACHE_BYTES)
719 +
720 +-#ifdef CONFIG_JUMP_LABEL
721 + . = ALIGN(PAGE_SIZE);
722 + .exit.text : {
723 + EXIT_TEXT
724 + }
725 +-#endif
726 ++
727 ++ .exit.data : {
728 ++ EXIT_DATA
729 ++ }
730 +
731 + . = ALIGN(PAGE_SIZE);
732 + __init_end = .;
733 +diff --git a/arch/x86/entry/vdso/vdso32-setup.c b/arch/x86/entry/vdso/vdso32-setup.c
734 +index 42d4c89f990e..ddff0ca6f509 100644
735 +--- a/arch/x86/entry/vdso/vdso32-setup.c
736 ++++ b/arch/x86/entry/vdso/vdso32-setup.c
737 +@@ -11,6 +11,7 @@
738 + #include <linux/smp.h>
739 + #include <linux/kernel.h>
740 + #include <linux/mm_types.h>
741 ++#include <linux/elf.h>
742 +
743 + #include <asm/processor.h>
744 + #include <asm/vdso.h>
745 +diff --git a/arch/x86/include/asm/nmi.h b/arch/x86/include/asm/nmi.h
746 +index 75ded1d13d98..9d5d949e662e 100644
747 +--- a/arch/x86/include/asm/nmi.h
748 ++++ b/arch/x86/include/asm/nmi.h
749 +@@ -41,7 +41,6 @@ struct nmiaction {
750 + struct list_head list;
751 + nmi_handler_t handler;
752 + u64 max_duration;
753 +- struct irq_work irq_work;
754 + unsigned long flags;
755 + const char *name;
756 + };
757 +diff --git a/arch/x86/kernel/nmi.c b/arch/x86/kernel/nmi.c
758 +index 086cf1d1d71d..0f8b9b900b0e 100644
759 +--- a/arch/x86/kernel/nmi.c
760 ++++ b/arch/x86/kernel/nmi.c
761 +@@ -102,18 +102,22 @@ static int __init nmi_warning_debugfs(void)
762 + }
763 + fs_initcall(nmi_warning_debugfs);
764 +
765 +-static void nmi_max_handler(struct irq_work *w)
766 ++static void nmi_check_duration(struct nmiaction *action, u64 duration)
767 + {
768 +- struct nmiaction *a = container_of(w, struct nmiaction, irq_work);
769 ++ u64 whole_msecs = READ_ONCE(action->max_duration);
770 + int remainder_ns, decimal_msecs;
771 +- u64 whole_msecs = READ_ONCE(a->max_duration);
772 ++
773 ++ if (duration < nmi_longest_ns || duration < action->max_duration)
774 ++ return;
775 ++
776 ++ action->max_duration = duration;
777 +
778 + remainder_ns = do_div(whole_msecs, (1000 * 1000));
779 + decimal_msecs = remainder_ns / 1000;
780 +
781 + printk_ratelimited(KERN_INFO
782 + "INFO: NMI handler (%ps) took too long to run: %lld.%03d msecs\n",
783 +- a->handler, whole_msecs, decimal_msecs);
784 ++ action->handler, whole_msecs, decimal_msecs);
785 + }
786 +
787 + static int nmi_handle(unsigned int type, struct pt_regs *regs)
788 +@@ -140,11 +144,7 @@ static int nmi_handle(unsigned int type, struct pt_regs *regs)
789 + delta = sched_clock() - delta;
790 + trace_nmi_handler(a->handler, (int)delta, thishandled);
791 +
792 +- if (delta < nmi_longest_ns || delta < a->max_duration)
793 +- continue;
794 +-
795 +- a->max_duration = delta;
796 +- irq_work_queue(&a->irq_work);
797 ++ nmi_check_duration(a, delta);
798 + }
799 +
800 + rcu_read_unlock();
801 +@@ -162,8 +162,6 @@ int __register_nmi_handler(unsigned int type, struct nmiaction *action)
802 + if (!action->handler)
803 + return -EINVAL;
804 +
805 +- init_irq_work(&action->irq_work, nmi_max_handler);
806 +-
807 + raw_spin_lock_irqsave(&desc->lock, flags);
808 +
809 + /*
810 +diff --git a/arch/x86/kernel/sysfb_simplefb.c b/arch/x86/kernel/sysfb_simplefb.c
811 +index 85195d447a92..f3215346e47f 100644
812 +--- a/arch/x86/kernel/sysfb_simplefb.c
813 ++++ b/arch/x86/kernel/sysfb_simplefb.c
814 +@@ -94,11 +94,11 @@ __init int create_simplefb(const struct screen_info *si,
815 + if (si->orig_video_isVGA == VIDEO_TYPE_VLFB)
816 + size <<= 16;
817 + length = mode->height * mode->stride;
818 +- length = PAGE_ALIGN(length);
819 + if (length > size) {
820 + printk(KERN_WARNING "sysfb: VRAM smaller than advertised\n");
821 + return -EINVAL;
822 + }
823 ++ length = PAGE_ALIGN(length);
824 +
825 + /* setup IORESOURCE_MEM as framebuffer memory */
826 + memset(&res, 0, sizeof(res));
827 +diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c
828 +index 2660c01eadae..aead984d89ad 100644
829 +--- a/arch/x86/kvm/vmx.c
830 ++++ b/arch/x86/kvm/vmx.c
831 +@@ -5302,6 +5302,9 @@ static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
832 +
833 + static int get_ept_level(struct kvm_vcpu *vcpu)
834 + {
835 ++ /* Nested EPT currently only supports 4-level walks. */
836 ++ if (is_guest_mode(vcpu) && nested_cpu_has_ept(get_vmcs12(vcpu)))
837 ++ return 4;
838 + if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
839 + return 5;
840 + return 4;
841 +diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
842 +deleted file mode 100644
843 +index 997926a9121c..000000000000
844 +--- a/arch/x86/kvm/vmx/vmx.c
845 ++++ /dev/null
846 +@@ -1,8036 +0,0 @@
847 +-// SPDX-License-Identifier: GPL-2.0-only
848 +-/*
849 +- * Kernel-based Virtual Machine driver for Linux
850 +- *
851 +- * This module enables machines with Intel VT-x extensions to run virtual
852 +- * machines without emulation or binary translation.
853 +- *
854 +- * Copyright (C) 2006 Qumranet, Inc.
855 +- * Copyright 2010 Red Hat, Inc. and/or its affiliates.
856 +- *
857 +- * Authors:
858 +- * Avi Kivity <avi@××××××××.com>
859 +- * Yaniv Kamay <yaniv@××××××××.com>
860 +- */
861 +-
862 +-#include <linux/frame.h>
863 +-#include <linux/highmem.h>
864 +-#include <linux/hrtimer.h>
865 +-#include <linux/kernel.h>
866 +-#include <linux/kvm_host.h>
867 +-#include <linux/module.h>
868 +-#include <linux/moduleparam.h>
869 +-#include <linux/mod_devicetable.h>
870 +-#include <linux/mm.h>
871 +-#include <linux/sched.h>
872 +-#include <linux/sched/smt.h>
873 +-#include <linux/slab.h>
874 +-#include <linux/tboot.h>
875 +-#include <linux/trace_events.h>
876 +-
877 +-#include <asm/apic.h>
878 +-#include <asm/asm.h>
879 +-#include <asm/cpu.h>
880 +-#include <asm/debugreg.h>
881 +-#include <asm/desc.h>
882 +-#include <asm/fpu/internal.h>
883 +-#include <asm/io.h>
884 +-#include <asm/irq_remapping.h>
885 +-#include <asm/kexec.h>
886 +-#include <asm/perf_event.h>
887 +-#include <asm/mce.h>
888 +-#include <asm/mmu_context.h>
889 +-#include <asm/mshyperv.h>
890 +-#include <asm/spec-ctrl.h>
891 +-#include <asm/virtext.h>
892 +-#include <asm/vmx.h>
893 +-
894 +-#include "capabilities.h"
895 +-#include "cpuid.h"
896 +-#include "evmcs.h"
897 +-#include "irq.h"
898 +-#include "kvm_cache_regs.h"
899 +-#include "lapic.h"
900 +-#include "mmu.h"
901 +-#include "nested.h"
902 +-#include "ops.h"
903 +-#include "pmu.h"
904 +-#include "trace.h"
905 +-#include "vmcs.h"
906 +-#include "vmcs12.h"
907 +-#include "vmx.h"
908 +-#include "x86.h"
909 +-
910 +-MODULE_AUTHOR("Qumranet");
911 +-MODULE_LICENSE("GPL");
912 +-
913 +-static const struct x86_cpu_id vmx_cpu_id[] = {
914 +- X86_FEATURE_MATCH(X86_FEATURE_VMX),
915 +- {}
916 +-};
917 +-MODULE_DEVICE_TABLE(x86cpu, vmx_cpu_id);
918 +-
919 +-bool __read_mostly enable_vpid = 1;
920 +-module_param_named(vpid, enable_vpid, bool, 0444);
921 +-
922 +-static bool __read_mostly enable_vnmi = 1;
923 +-module_param_named(vnmi, enable_vnmi, bool, S_IRUGO);
924 +-
925 +-bool __read_mostly flexpriority_enabled = 1;
926 +-module_param_named(flexpriority, flexpriority_enabled, bool, S_IRUGO);
927 +-
928 +-bool __read_mostly enable_ept = 1;
929 +-module_param_named(ept, enable_ept, bool, S_IRUGO);
930 +-
931 +-bool __read_mostly enable_unrestricted_guest = 1;
932 +-module_param_named(unrestricted_guest,
933 +- enable_unrestricted_guest, bool, S_IRUGO);
934 +-
935 +-bool __read_mostly enable_ept_ad_bits = 1;
936 +-module_param_named(eptad, enable_ept_ad_bits, bool, S_IRUGO);
937 +-
938 +-static bool __read_mostly emulate_invalid_guest_state = true;
939 +-module_param(emulate_invalid_guest_state, bool, S_IRUGO);
940 +-
941 +-static bool __read_mostly fasteoi = 1;
942 +-module_param(fasteoi, bool, S_IRUGO);
943 +-
944 +-static bool __read_mostly enable_apicv = 1;
945 +-module_param(enable_apicv, bool, S_IRUGO);
946 +-
947 +-/*
948 +- * If nested=1, nested virtualization is supported, i.e., guests may use
949 +- * VMX and be a hypervisor for its own guests. If nested=0, guests may not
950 +- * use VMX instructions.
951 +- */
952 +-static bool __read_mostly nested = 1;
953 +-module_param(nested, bool, S_IRUGO);
954 +-
955 +-bool __read_mostly enable_pml = 1;
956 +-module_param_named(pml, enable_pml, bool, S_IRUGO);
957 +-
958 +-static bool __read_mostly dump_invalid_vmcs = 0;
959 +-module_param(dump_invalid_vmcs, bool, 0644);
960 +-
961 +-#define MSR_BITMAP_MODE_X2APIC 1
962 +-#define MSR_BITMAP_MODE_X2APIC_APICV 2
963 +-
964 +-#define KVM_VMX_TSC_MULTIPLIER_MAX 0xffffffffffffffffULL
965 +-
966 +-/* Guest_tsc -> host_tsc conversion requires 64-bit division. */
967 +-static int __read_mostly cpu_preemption_timer_multi;
968 +-static bool __read_mostly enable_preemption_timer = 1;
969 +-#ifdef CONFIG_X86_64
970 +-module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
971 +-#endif
972 +-
973 +-#define KVM_VM_CR0_ALWAYS_OFF (X86_CR0_NW | X86_CR0_CD)
974 +-#define KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR0_NE
975 +-#define KVM_VM_CR0_ALWAYS_ON \
976 +- (KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST | \
977 +- X86_CR0_WP | X86_CR0_PG | X86_CR0_PE)
978 +-#define KVM_CR4_GUEST_OWNED_BITS \
979 +- (X86_CR4_PVI | X86_CR4_DE | X86_CR4_PCE | X86_CR4_OSFXSR \
980 +- | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_TSD)
981 +-
982 +-#define KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST X86_CR4_VMXE
983 +-#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
984 +-#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
985 +-
986 +-#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
987 +-
988 +-#define MSR_IA32_RTIT_STATUS_MASK (~(RTIT_STATUS_FILTEREN | \
989 +- RTIT_STATUS_CONTEXTEN | RTIT_STATUS_TRIGGEREN | \
990 +- RTIT_STATUS_ERROR | RTIT_STATUS_STOPPED | \
991 +- RTIT_STATUS_BYTECNT))
992 +-
993 +-#define MSR_IA32_RTIT_OUTPUT_BASE_MASK \
994 +- (~((1UL << cpuid_query_maxphyaddr(vcpu)) - 1) | 0x7f)
995 +-
996 +-/*
997 +- * These 2 parameters are used to config the controls for Pause-Loop Exiting:
998 +- * ple_gap: upper bound on the amount of time between two successive
999 +- * executions of PAUSE in a loop. Also indicate if ple enabled.
1000 +- * According to test, this time is usually smaller than 128 cycles.
1001 +- * ple_window: upper bound on the amount of time a guest is allowed to execute
1002 +- * in a PAUSE loop. Tests indicate that most spinlocks are held for
1003 +- * less than 2^12 cycles
1004 +- * Time is measured based on a counter that runs at the same rate as the TSC,
1005 +- * refer SDM volume 3b section 21.6.13 & 22.1.3.
1006 +- */
1007 +-static unsigned int ple_gap = KVM_DEFAULT_PLE_GAP;
1008 +-module_param(ple_gap, uint, 0444);
1009 +-
1010 +-static unsigned int ple_window = KVM_VMX_DEFAULT_PLE_WINDOW;
1011 +-module_param(ple_window, uint, 0444);
1012 +-
1013 +-/* Default doubles per-vcpu window every exit. */
1014 +-static unsigned int ple_window_grow = KVM_DEFAULT_PLE_WINDOW_GROW;
1015 +-module_param(ple_window_grow, uint, 0444);
1016 +-
1017 +-/* Default resets per-vcpu window every exit to ple_window. */
1018 +-static unsigned int ple_window_shrink = KVM_DEFAULT_PLE_WINDOW_SHRINK;
1019 +-module_param(ple_window_shrink, uint, 0444);
1020 +-
1021 +-/* Default is to compute the maximum so we can never overflow. */
1022 +-static unsigned int ple_window_max = KVM_VMX_DEFAULT_PLE_WINDOW_MAX;
1023 +-module_param(ple_window_max, uint, 0444);
1024 +-
1025 +-/* Default is SYSTEM mode, 1 for host-guest mode */
1026 +-int __read_mostly pt_mode = PT_MODE_SYSTEM;
1027 +-module_param(pt_mode, int, S_IRUGO);
1028 +-
1029 +-static DEFINE_STATIC_KEY_FALSE(vmx_l1d_should_flush);
1030 +-static DEFINE_STATIC_KEY_FALSE(vmx_l1d_flush_cond);
1031 +-static DEFINE_MUTEX(vmx_l1d_flush_mutex);
1032 +-
1033 +-/* Storage for pre module init parameter parsing */
1034 +-static enum vmx_l1d_flush_state __read_mostly vmentry_l1d_flush_param = VMENTER_L1D_FLUSH_AUTO;
1035 +-
1036 +-static const struct {
1037 +- const char *option;
1038 +- bool for_parse;
1039 +-} vmentry_l1d_param[] = {
1040 +- [VMENTER_L1D_FLUSH_AUTO] = {"auto", true},
1041 +- [VMENTER_L1D_FLUSH_NEVER] = {"never", true},
1042 +- [VMENTER_L1D_FLUSH_COND] = {"cond", true},
1043 +- [VMENTER_L1D_FLUSH_ALWAYS] = {"always", true},
1044 +- [VMENTER_L1D_FLUSH_EPT_DISABLED] = {"EPT disabled", false},
1045 +- [VMENTER_L1D_FLUSH_NOT_REQUIRED] = {"not required", false},
1046 +-};
1047 +-
1048 +-#define L1D_CACHE_ORDER 4
1049 +-static void *vmx_l1d_flush_pages;
1050 +-
1051 +-static int vmx_setup_l1d_flush(enum vmx_l1d_flush_state l1tf)
1052 +-{
1053 +- struct page *page;
1054 +- unsigned int i;
1055 +-
1056 +- if (!boot_cpu_has_bug(X86_BUG_L1TF)) {
1057 +- l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_NOT_REQUIRED;
1058 +- return 0;
1059 +- }
1060 +-
1061 +- if (!enable_ept) {
1062 +- l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_EPT_DISABLED;
1063 +- return 0;
1064 +- }
1065 +-
1066 +- if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) {
1067 +- u64 msr;
1068 +-
1069 +- rdmsrl(MSR_IA32_ARCH_CAPABILITIES, msr);
1070 +- if (msr & ARCH_CAP_SKIP_VMENTRY_L1DFLUSH) {
1071 +- l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_NOT_REQUIRED;
1072 +- return 0;
1073 +- }
1074 +- }
1075 +-
1076 +- /* If set to auto use the default l1tf mitigation method */
1077 +- if (l1tf == VMENTER_L1D_FLUSH_AUTO) {
1078 +- switch (l1tf_mitigation) {
1079 +- case L1TF_MITIGATION_OFF:
1080 +- l1tf = VMENTER_L1D_FLUSH_NEVER;
1081 +- break;
1082 +- case L1TF_MITIGATION_FLUSH_NOWARN:
1083 +- case L1TF_MITIGATION_FLUSH:
1084 +- case L1TF_MITIGATION_FLUSH_NOSMT:
1085 +- l1tf = VMENTER_L1D_FLUSH_COND;
1086 +- break;
1087 +- case L1TF_MITIGATION_FULL:
1088 +- case L1TF_MITIGATION_FULL_FORCE:
1089 +- l1tf = VMENTER_L1D_FLUSH_ALWAYS;
1090 +- break;
1091 +- }
1092 +- } else if (l1tf_mitigation == L1TF_MITIGATION_FULL_FORCE) {
1093 +- l1tf = VMENTER_L1D_FLUSH_ALWAYS;
1094 +- }
1095 +-
1096 +- if (l1tf != VMENTER_L1D_FLUSH_NEVER && !vmx_l1d_flush_pages &&
1097 +- !boot_cpu_has(X86_FEATURE_FLUSH_L1D)) {
1098 +- /*
1099 +- * This allocation for vmx_l1d_flush_pages is not tied to a VM
1100 +- * lifetime and so should not be charged to a memcg.
1101 +- */
1102 +- page = alloc_pages(GFP_KERNEL, L1D_CACHE_ORDER);
1103 +- if (!page)
1104 +- return -ENOMEM;
1105 +- vmx_l1d_flush_pages = page_address(page);
1106 +-
1107 +- /*
1108 +- * Initialize each page with a different pattern in
1109 +- * order to protect against KSM in the nested
1110 +- * virtualization case.
1111 +- */
1112 +- for (i = 0; i < 1u << L1D_CACHE_ORDER; ++i) {
1113 +- memset(vmx_l1d_flush_pages + i * PAGE_SIZE, i + 1,
1114 +- PAGE_SIZE);
1115 +- }
1116 +- }
1117 +-
1118 +- l1tf_vmx_mitigation = l1tf;
1119 +-
1120 +- if (l1tf != VMENTER_L1D_FLUSH_NEVER)
1121 +- static_branch_enable(&vmx_l1d_should_flush);
1122 +- else
1123 +- static_branch_disable(&vmx_l1d_should_flush);
1124 +-
1125 +- if (l1tf == VMENTER_L1D_FLUSH_COND)
1126 +- static_branch_enable(&vmx_l1d_flush_cond);
1127 +- else
1128 +- static_branch_disable(&vmx_l1d_flush_cond);
1129 +- return 0;
1130 +-}
1131 +-
1132 +-static int vmentry_l1d_flush_parse(const char *s)
1133 +-{
1134 +- unsigned int i;
1135 +-
1136 +- if (s) {
1137 +- for (i = 0; i < ARRAY_SIZE(vmentry_l1d_param); i++) {
1138 +- if (vmentry_l1d_param[i].for_parse &&
1139 +- sysfs_streq(s, vmentry_l1d_param[i].option))
1140 +- return i;
1141 +- }
1142 +- }
1143 +- return -EINVAL;
1144 +-}
1145 +-
1146 +-static int vmentry_l1d_flush_set(const char *s, const struct kernel_param *kp)
1147 +-{
1148 +- int l1tf, ret;
1149 +-
1150 +- l1tf = vmentry_l1d_flush_parse(s);
1151 +- if (l1tf < 0)
1152 +- return l1tf;
1153 +-
1154 +- if (!boot_cpu_has(X86_BUG_L1TF))
1155 +- return 0;
1156 +-
1157 +- /*
1158 +- * Has vmx_init() run already? If not then this is the pre init
1159 +- * parameter parsing. In that case just store the value and let
1160 +- * vmx_init() do the proper setup after enable_ept has been
1161 +- * established.
1162 +- */
1163 +- if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_AUTO) {
1164 +- vmentry_l1d_flush_param = l1tf;
1165 +- return 0;
1166 +- }
1167 +-
1168 +- mutex_lock(&vmx_l1d_flush_mutex);
1169 +- ret = vmx_setup_l1d_flush(l1tf);
1170 +- mutex_unlock(&vmx_l1d_flush_mutex);
1171 +- return ret;
1172 +-}
1173 +-
1174 +-static int vmentry_l1d_flush_get(char *s, const struct kernel_param *kp)
1175 +-{
1176 +- if (WARN_ON_ONCE(l1tf_vmx_mitigation >= ARRAY_SIZE(vmentry_l1d_param)))
1177 +- return sprintf(s, "???\n");
1178 +-
1179 +- return sprintf(s, "%s\n", vmentry_l1d_param[l1tf_vmx_mitigation].option);
1180 +-}
1181 +-
1182 +-static const struct kernel_param_ops vmentry_l1d_flush_ops = {
1183 +- .set = vmentry_l1d_flush_set,
1184 +- .get = vmentry_l1d_flush_get,
1185 +-};
1186 +-module_param_cb(vmentry_l1d_flush, &vmentry_l1d_flush_ops, NULL, 0644);
1187 +-
1188 +-static bool guest_state_valid(struct kvm_vcpu *vcpu);
1189 +-static u32 vmx_segment_access_rights(struct kvm_segment *var);
1190 +-static __always_inline void vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
1191 +- u32 msr, int type);
1192 +-
1193 +-void vmx_vmexit(void);
1194 +-
1195 +-#define vmx_insn_failed(fmt...) \
1196 +-do { \
1197 +- WARN_ONCE(1, fmt); \
1198 +- pr_warn_ratelimited(fmt); \
1199 +-} while (0)
1200 +-
1201 +-asmlinkage void vmread_error(unsigned long field, bool fault)
1202 +-{
1203 +- if (fault)
1204 +- kvm_spurious_fault();
1205 +- else
1206 +- vmx_insn_failed("kvm: vmread failed: field=%lx\n", field);
1207 +-}
1208 +-
1209 +-noinline void vmwrite_error(unsigned long field, unsigned long value)
1210 +-{
1211 +- vmx_insn_failed("kvm: vmwrite failed: field=%lx val=%lx err=%d\n",
1212 +- field, value, vmcs_read32(VM_INSTRUCTION_ERROR));
1213 +-}
1214 +-
1215 +-noinline void vmclear_error(struct vmcs *vmcs, u64 phys_addr)
1216 +-{
1217 +- vmx_insn_failed("kvm: vmclear failed: %p/%llx\n", vmcs, phys_addr);
1218 +-}
1219 +-
1220 +-noinline void vmptrld_error(struct vmcs *vmcs, u64 phys_addr)
1221 +-{
1222 +- vmx_insn_failed("kvm: vmptrld failed: %p/%llx\n", vmcs, phys_addr);
1223 +-}
1224 +-
1225 +-noinline void invvpid_error(unsigned long ext, u16 vpid, gva_t gva)
1226 +-{
1227 +- vmx_insn_failed("kvm: invvpid failed: ext=0x%lx vpid=%u gva=0x%lx\n",
1228 +- ext, vpid, gva);
1229 +-}
1230 +-
1231 +-noinline void invept_error(unsigned long ext, u64 eptp, gpa_t gpa)
1232 +-{
1233 +- vmx_insn_failed("kvm: invept failed: ext=0x%lx eptp=%llx gpa=0x%llx\n",
1234 +- ext, eptp, gpa);
1235 +-}
1236 +-
1237 +-static DEFINE_PER_CPU(struct vmcs *, vmxarea);
1238 +-DEFINE_PER_CPU(struct vmcs *, current_vmcs);
1239 +-/*
1240 +- * We maintain a per-CPU linked-list of VMCS loaded on that CPU. This is needed
1241 +- * when a CPU is brought down, and we need to VMCLEAR all VMCSs loaded on it.
1242 +- */
1243 +-static DEFINE_PER_CPU(struct list_head, loaded_vmcss_on_cpu);
1244 +-
1245 +-/*
1246 +- * We maintian a per-CPU linked-list of vCPU, so in wakeup_handler() we
1247 +- * can find which vCPU should be waken up.
1248 +- */
1249 +-static DEFINE_PER_CPU(struct list_head, blocked_vcpu_on_cpu);
1250 +-static DEFINE_PER_CPU(spinlock_t, blocked_vcpu_on_cpu_lock);
1251 +-
1252 +-static DECLARE_BITMAP(vmx_vpid_bitmap, VMX_NR_VPIDS);
1253 +-static DEFINE_SPINLOCK(vmx_vpid_lock);
1254 +-
1255 +-struct vmcs_config vmcs_config;
1256 +-struct vmx_capability vmx_capability;
1257 +-
1258 +-#define VMX_SEGMENT_FIELD(seg) \
1259 +- [VCPU_SREG_##seg] = { \
1260 +- .selector = GUEST_##seg##_SELECTOR, \
1261 +- .base = GUEST_##seg##_BASE, \
1262 +- .limit = GUEST_##seg##_LIMIT, \
1263 +- .ar_bytes = GUEST_##seg##_AR_BYTES, \
1264 +- }
1265 +-
1266 +-static const struct kvm_vmx_segment_field {
1267 +- unsigned selector;
1268 +- unsigned base;
1269 +- unsigned limit;
1270 +- unsigned ar_bytes;
1271 +-} kvm_vmx_segment_fields[] = {
1272 +- VMX_SEGMENT_FIELD(CS),
1273 +- VMX_SEGMENT_FIELD(DS),
1274 +- VMX_SEGMENT_FIELD(ES),
1275 +- VMX_SEGMENT_FIELD(FS),
1276 +- VMX_SEGMENT_FIELD(GS),
1277 +- VMX_SEGMENT_FIELD(SS),
1278 +- VMX_SEGMENT_FIELD(TR),
1279 +- VMX_SEGMENT_FIELD(LDTR),
1280 +-};
1281 +-
1282 +-u64 host_efer;
1283 +-static unsigned long host_idt_base;
1284 +-
1285 +-/*
1286 +- * Though SYSCALL is only supported in 64-bit mode on Intel CPUs, kvm
1287 +- * will emulate SYSCALL in legacy mode if the vendor string in guest
1288 +- * CPUID.0:{EBX,ECX,EDX} is "AuthenticAMD" or "AMDisbetter!" To
1289 +- * support this emulation, IA32_STAR must always be included in
1290 +- * vmx_msr_index[], even in i386 builds.
1291 +- */
1292 +-const u32 vmx_msr_index[] = {
1293 +-#ifdef CONFIG_X86_64
1294 +- MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR,
1295 +-#endif
1296 +- MSR_EFER, MSR_TSC_AUX, MSR_STAR,
1297 +- MSR_IA32_TSX_CTRL,
1298 +-};
1299 +-
1300 +-#if IS_ENABLED(CONFIG_HYPERV)
1301 +-static bool __read_mostly enlightened_vmcs = true;
1302 +-module_param(enlightened_vmcs, bool, 0444);
1303 +-
1304 +-/* check_ept_pointer() should be under protection of ept_pointer_lock. */
1305 +-static void check_ept_pointer_match(struct kvm *kvm)
1306 +-{
1307 +- struct kvm_vcpu *vcpu;
1308 +- u64 tmp_eptp = INVALID_PAGE;
1309 +- int i;
1310 +-
1311 +- kvm_for_each_vcpu(i, vcpu, kvm) {
1312 +- if (!VALID_PAGE(tmp_eptp)) {
1313 +- tmp_eptp = to_vmx(vcpu)->ept_pointer;
1314 +- } else if (tmp_eptp != to_vmx(vcpu)->ept_pointer) {
1315 +- to_kvm_vmx(kvm)->ept_pointers_match
1316 +- = EPT_POINTERS_MISMATCH;
1317 +- return;
1318 +- }
1319 +- }
1320 +-
1321 +- to_kvm_vmx(kvm)->ept_pointers_match = EPT_POINTERS_MATCH;
1322 +-}
1323 +-
1324 +-static int kvm_fill_hv_flush_list_func(struct hv_guest_mapping_flush_list *flush,
1325 +- void *data)
1326 +-{
1327 +- struct kvm_tlb_range *range = data;
1328 +-
1329 +- return hyperv_fill_flush_guest_mapping_list(flush, range->start_gfn,
1330 +- range->pages);
1331 +-}
1332 +-
1333 +-static inline int __hv_remote_flush_tlb_with_range(struct kvm *kvm,
1334 +- struct kvm_vcpu *vcpu, struct kvm_tlb_range *range)
1335 +-{
1336 +- u64 ept_pointer = to_vmx(vcpu)->ept_pointer;
1337 +-
1338 +- /*
1339 +- * FLUSH_GUEST_PHYSICAL_ADDRESS_SPACE hypercall needs address
1340 +- * of the base of EPT PML4 table, strip off EPT configuration
1341 +- * information.
1342 +- */
1343 +- if (range)
1344 +- return hyperv_flush_guest_mapping_range(ept_pointer & PAGE_MASK,
1345 +- kvm_fill_hv_flush_list_func, (void *)range);
1346 +- else
1347 +- return hyperv_flush_guest_mapping(ept_pointer & PAGE_MASK);
1348 +-}
1349 +-
1350 +-static int hv_remote_flush_tlb_with_range(struct kvm *kvm,
1351 +- struct kvm_tlb_range *range)
1352 +-{
1353 +- struct kvm_vcpu *vcpu;
1354 +- int ret = 0, i;
1355 +-
1356 +- spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
1357 +-
1358 +- if (to_kvm_vmx(kvm)->ept_pointers_match == EPT_POINTERS_CHECK)
1359 +- check_ept_pointer_match(kvm);
1360 +-
1361 +- if (to_kvm_vmx(kvm)->ept_pointers_match != EPT_POINTERS_MATCH) {
1362 +- kvm_for_each_vcpu(i, vcpu, kvm) {
1363 +- /* If ept_pointer is invalid pointer, bypass flush request. */
1364 +- if (VALID_PAGE(to_vmx(vcpu)->ept_pointer))
1365 +- ret |= __hv_remote_flush_tlb_with_range(
1366 +- kvm, vcpu, range);
1367 +- }
1368 +- } else {
1369 +- ret = __hv_remote_flush_tlb_with_range(kvm,
1370 +- kvm_get_vcpu(kvm, 0), range);
1371 +- }
1372 +-
1373 +- spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
1374 +- return ret;
1375 +-}
1376 +-static int hv_remote_flush_tlb(struct kvm *kvm)
1377 +-{
1378 +- return hv_remote_flush_tlb_with_range(kvm, NULL);
1379 +-}
1380 +-
1381 +-static int hv_enable_direct_tlbflush(struct kvm_vcpu *vcpu)
1382 +-{
1383 +- struct hv_enlightened_vmcs *evmcs;
1384 +- struct hv_partition_assist_pg **p_hv_pa_pg =
1385 +- &vcpu->kvm->arch.hyperv.hv_pa_pg;
1386 +- /*
1387 +- * Synthetic VM-Exit is not enabled in current code and so All
1388 +- * evmcs in singe VM shares same assist page.
1389 +- */
1390 +- if (!*p_hv_pa_pg)
1391 +- *p_hv_pa_pg = kzalloc(PAGE_SIZE, GFP_KERNEL);
1392 +-
1393 +- if (!*p_hv_pa_pg)
1394 +- return -ENOMEM;
1395 +-
1396 +- evmcs = (struct hv_enlightened_vmcs *)to_vmx(vcpu)->loaded_vmcs->vmcs;
1397 +-
1398 +- evmcs->partition_assist_page =
1399 +- __pa(*p_hv_pa_pg);
1400 +- evmcs->hv_vm_id = (unsigned long)vcpu->kvm;
1401 +- evmcs->hv_enlightenments_control.nested_flush_hypercall = 1;
1402 +-
1403 +- return 0;
1404 +-}
1405 +-
1406 +-#endif /* IS_ENABLED(CONFIG_HYPERV) */
1407 +-
1408 +-/*
1409 +- * Comment's format: document - errata name - stepping - processor name.
1410 +- * Refer from
1411 +- * https://www.virtualbox.org/svn/vbox/trunk/src/VBox/VMM/VMMR0/HMR0.cpp
1412 +- */
1413 +-static u32 vmx_preemption_cpu_tfms[] = {
1414 +-/* 323344.pdf - BA86 - D0 - Xeon 7500 Series */
1415 +-0x000206E6,
1416 +-/* 323056.pdf - AAX65 - C2 - Xeon L3406 */
1417 +-/* 322814.pdf - AAT59 - C2 - i7-600, i5-500, i5-400 and i3-300 Mobile */
1418 +-/* 322911.pdf - AAU65 - C2 - i5-600, i3-500 Desktop and Pentium G6950 */
1419 +-0x00020652,
1420 +-/* 322911.pdf - AAU65 - K0 - i5-600, i3-500 Desktop and Pentium G6950 */
1421 +-0x00020655,
1422 +-/* 322373.pdf - AAO95 - B1 - Xeon 3400 Series */
1423 +-/* 322166.pdf - AAN92 - B1 - i7-800 and i5-700 Desktop */
1424 +-/*
1425 +- * 320767.pdf - AAP86 - B1 -
1426 +- * i7-900 Mobile Extreme, i7-800 and i7-700 Mobile
1427 +- */
1428 +-0x000106E5,
1429 +-/* 321333.pdf - AAM126 - C0 - Xeon 3500 */
1430 +-0x000106A0,
1431 +-/* 321333.pdf - AAM126 - C1 - Xeon 3500 */
1432 +-0x000106A1,
1433 +-/* 320836.pdf - AAJ124 - C0 - i7-900 Desktop Extreme and i7-900 Desktop */
1434 +-0x000106A4,
1435 +- /* 321333.pdf - AAM126 - D0 - Xeon 3500 */
1436 +- /* 321324.pdf - AAK139 - D0 - Xeon 5500 */
1437 +- /* 320836.pdf - AAJ124 - D0 - i7-900 Extreme and i7-900 Desktop */
1438 +-0x000106A5,
1439 +- /* Xeon E3-1220 V2 */
1440 +-0x000306A8,
1441 +-};
1442 +-
1443 +-static inline bool cpu_has_broken_vmx_preemption_timer(void)
1444 +-{
1445 +- u32 eax = cpuid_eax(0x00000001), i;
1446 +-
1447 +- /* Clear the reserved bits */
1448 +- eax &= ~(0x3U << 14 | 0xfU << 28);
1449 +- for (i = 0; i < ARRAY_SIZE(vmx_preemption_cpu_tfms); i++)
1450 +- if (eax == vmx_preemption_cpu_tfms[i])
1451 +- return true;
1452 +-
1453 +- return false;
1454 +-}
1455 +-
1456 +-static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu)
1457 +-{
1458 +- return flexpriority_enabled && lapic_in_kernel(vcpu);
1459 +-}
1460 +-
1461 +-static inline bool report_flexpriority(void)
1462 +-{
1463 +- return flexpriority_enabled;
1464 +-}
1465 +-
1466 +-static inline int __find_msr_index(struct vcpu_vmx *vmx, u32 msr)
1467 +-{
1468 +- int i;
1469 +-
1470 +- for (i = 0; i < vmx->nmsrs; ++i)
1471 +- if (vmx_msr_index[vmx->guest_msrs[i].index] == msr)
1472 +- return i;
1473 +- return -1;
1474 +-}
1475 +-
1476 +-struct shared_msr_entry *find_msr_entry(struct vcpu_vmx *vmx, u32 msr)
1477 +-{
1478 +- int i;
1479 +-
1480 +- i = __find_msr_index(vmx, msr);
1481 +- if (i >= 0)
1482 +- return &vmx->guest_msrs[i];
1483 +- return NULL;
1484 +-}
1485 +-
1486 +-static int vmx_set_guest_msr(struct vcpu_vmx *vmx, struct shared_msr_entry *msr, u64 data)
1487 +-{
1488 +- int ret = 0;
1489 +-
1490 +- u64 old_msr_data = msr->data;
1491 +- msr->data = data;
1492 +- if (msr - vmx->guest_msrs < vmx->save_nmsrs) {
1493 +- preempt_disable();
1494 +- ret = kvm_set_shared_msr(msr->index, msr->data,
1495 +- msr->mask);
1496 +- preempt_enable();
1497 +- if (ret)
1498 +- msr->data = old_msr_data;
1499 +- }
1500 +- return ret;
1501 +-}
1502 +-
1503 +-void loaded_vmcs_init(struct loaded_vmcs *loaded_vmcs)
1504 +-{
1505 +- vmcs_clear(loaded_vmcs->vmcs);
1506 +- if (loaded_vmcs->shadow_vmcs && loaded_vmcs->launched)
1507 +- vmcs_clear(loaded_vmcs->shadow_vmcs);
1508 +- loaded_vmcs->cpu = -1;
1509 +- loaded_vmcs->launched = 0;
1510 +-}
1511 +-
1512 +-#ifdef CONFIG_KEXEC_CORE
1513 +-/*
1514 +- * This bitmap is used to indicate whether the vmclear
1515 +- * operation is enabled on all cpus. All disabled by
1516 +- * default.
1517 +- */
1518 +-static cpumask_t crash_vmclear_enabled_bitmap = CPU_MASK_NONE;
1519 +-
1520 +-static inline void crash_enable_local_vmclear(int cpu)
1521 +-{
1522 +- cpumask_set_cpu(cpu, &crash_vmclear_enabled_bitmap);
1523 +-}
1524 +-
1525 +-static inline void crash_disable_local_vmclear(int cpu)
1526 +-{
1527 +- cpumask_clear_cpu(cpu, &crash_vmclear_enabled_bitmap);
1528 +-}
1529 +-
1530 +-static inline int crash_local_vmclear_enabled(int cpu)
1531 +-{
1532 +- return cpumask_test_cpu(cpu, &crash_vmclear_enabled_bitmap);
1533 +-}
1534 +-
1535 +-static void crash_vmclear_local_loaded_vmcss(void)
1536 +-{
1537 +- int cpu = raw_smp_processor_id();
1538 +- struct loaded_vmcs *v;
1539 +-
1540 +- if (!crash_local_vmclear_enabled(cpu))
1541 +- return;
1542 +-
1543 +- list_for_each_entry(v, &per_cpu(loaded_vmcss_on_cpu, cpu),
1544 +- loaded_vmcss_on_cpu_link)
1545 +- vmcs_clear(v->vmcs);
1546 +-}
1547 +-#else
1548 +-static inline void crash_enable_local_vmclear(int cpu) { }
1549 +-static inline void crash_disable_local_vmclear(int cpu) { }
1550 +-#endif /* CONFIG_KEXEC_CORE */
1551 +-
1552 +-static void __loaded_vmcs_clear(void *arg)
1553 +-{
1554 +- struct loaded_vmcs *loaded_vmcs = arg;
1555 +- int cpu = raw_smp_processor_id();
1556 +-
1557 +- if (loaded_vmcs->cpu != cpu)
1558 +- return; /* vcpu migration can race with cpu offline */
1559 +- if (per_cpu(current_vmcs, cpu) == loaded_vmcs->vmcs)
1560 +- per_cpu(current_vmcs, cpu) = NULL;
1561 +- crash_disable_local_vmclear(cpu);
1562 +- list_del(&loaded_vmcs->loaded_vmcss_on_cpu_link);
1563 +-
1564 +- /*
1565 +- * we should ensure updating loaded_vmcs->loaded_vmcss_on_cpu_link
1566 +- * is before setting loaded_vmcs->vcpu to -1 which is done in
1567 +- * loaded_vmcs_init. Otherwise, other cpu can see vcpu = -1 fist
1568 +- * then adds the vmcs into percpu list before it is deleted.
1569 +- */
1570 +- smp_wmb();
1571 +-
1572 +- loaded_vmcs_init(loaded_vmcs);
1573 +- crash_enable_local_vmclear(cpu);
1574 +-}
1575 +-
1576 +-void loaded_vmcs_clear(struct loaded_vmcs *loaded_vmcs)
1577 +-{
1578 +- int cpu = loaded_vmcs->cpu;
1579 +-
1580 +- if (cpu != -1)
1581 +- smp_call_function_single(cpu,
1582 +- __loaded_vmcs_clear, loaded_vmcs, 1);
1583 +-}
1584 +-
1585 +-static bool vmx_segment_cache_test_set(struct vcpu_vmx *vmx, unsigned seg,
1586 +- unsigned field)
1587 +-{
1588 +- bool ret;
1589 +- u32 mask = 1 << (seg * SEG_FIELD_NR + field);
1590 +-
1591 +- if (!kvm_register_is_available(&vmx->vcpu, VCPU_EXREG_SEGMENTS)) {
1592 +- kvm_register_mark_available(&vmx->vcpu, VCPU_EXREG_SEGMENTS);
1593 +- vmx->segment_cache.bitmask = 0;
1594 +- }
1595 +- ret = vmx->segment_cache.bitmask & mask;
1596 +- vmx->segment_cache.bitmask |= mask;
1597 +- return ret;
1598 +-}
1599 +-
1600 +-static u16 vmx_read_guest_seg_selector(struct vcpu_vmx *vmx, unsigned seg)
1601 +-{
1602 +- u16 *p = &vmx->segment_cache.seg[seg].selector;
1603 +-
1604 +- if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_SEL))
1605 +- *p = vmcs_read16(kvm_vmx_segment_fields[seg].selector);
1606 +- return *p;
1607 +-}
1608 +-
1609 +-static ulong vmx_read_guest_seg_base(struct vcpu_vmx *vmx, unsigned seg)
1610 +-{
1611 +- ulong *p = &vmx->segment_cache.seg[seg].base;
1612 +-
1613 +- if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_BASE))
1614 +- *p = vmcs_readl(kvm_vmx_segment_fields[seg].base);
1615 +- return *p;
1616 +-}
1617 +-
1618 +-static u32 vmx_read_guest_seg_limit(struct vcpu_vmx *vmx, unsigned seg)
1619 +-{
1620 +- u32 *p = &vmx->segment_cache.seg[seg].limit;
1621 +-
1622 +- if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_LIMIT))
1623 +- *p = vmcs_read32(kvm_vmx_segment_fields[seg].limit);
1624 +- return *p;
1625 +-}
1626 +-
1627 +-static u32 vmx_read_guest_seg_ar(struct vcpu_vmx *vmx, unsigned seg)
1628 +-{
1629 +- u32 *p = &vmx->segment_cache.seg[seg].ar;
1630 +-
1631 +- if (!vmx_segment_cache_test_set(vmx, seg, SEG_FIELD_AR))
1632 +- *p = vmcs_read32(kvm_vmx_segment_fields[seg].ar_bytes);
1633 +- return *p;
1634 +-}
1635 +-
1636 +-void update_exception_bitmap(struct kvm_vcpu *vcpu)
1637 +-{
1638 +- u32 eb;
1639 +-
1640 +- eb = (1u << PF_VECTOR) | (1u << UD_VECTOR) | (1u << MC_VECTOR) |
1641 +- (1u << DB_VECTOR) | (1u << AC_VECTOR);
1642 +- /*
1643 +- * Guest access to VMware backdoor ports could legitimately
1644 +- * trigger #GP because of TSS I/O permission bitmap.
1645 +- * We intercept those #GP and allow access to them anyway
1646 +- * as VMware does.
1647 +- */
1648 +- if (enable_vmware_backdoor)
1649 +- eb |= (1u << GP_VECTOR);
1650 +- if ((vcpu->guest_debug &
1651 +- (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP)) ==
1652 +- (KVM_GUESTDBG_ENABLE | KVM_GUESTDBG_USE_SW_BP))
1653 +- eb |= 1u << BP_VECTOR;
1654 +- if (to_vmx(vcpu)->rmode.vm86_active)
1655 +- eb = ~0;
1656 +- if (enable_ept)
1657 +- eb &= ~(1u << PF_VECTOR); /* bypass_guest_pf = 0 */
1658 +-
1659 +- /* When we are running a nested L2 guest and L1 specified for it a
1660 +- * certain exception bitmap, we must trap the same exceptions and pass
1661 +- * them to L1. When running L2, we will only handle the exceptions
1662 +- * specified above if L1 did not want them.
1663 +- */
1664 +- if (is_guest_mode(vcpu))
1665 +- eb |= get_vmcs12(vcpu)->exception_bitmap;
1666 +-
1667 +- vmcs_write32(EXCEPTION_BITMAP, eb);
1668 +-}
1669 +-
1670 +-/*
1671 +- * Check if MSR is intercepted for currently loaded MSR bitmap.
1672 +- */
1673 +-static bool msr_write_intercepted(struct kvm_vcpu *vcpu, u32 msr)
1674 +-{
1675 +- unsigned long *msr_bitmap;
1676 +- int f = sizeof(unsigned long);
1677 +-
1678 +- if (!cpu_has_vmx_msr_bitmap())
1679 +- return true;
1680 +-
1681 +- msr_bitmap = to_vmx(vcpu)->loaded_vmcs->msr_bitmap;
1682 +-
1683 +- if (msr <= 0x1fff) {
1684 +- return !!test_bit(msr, msr_bitmap + 0x800 / f);
1685 +- } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
1686 +- msr &= 0x1fff;
1687 +- return !!test_bit(msr, msr_bitmap + 0xc00 / f);
1688 +- }
1689 +-
1690 +- return true;
1691 +-}
1692 +-
1693 +-static void clear_atomic_switch_msr_special(struct vcpu_vmx *vmx,
1694 +- unsigned long entry, unsigned long exit)
1695 +-{
1696 +- vm_entry_controls_clearbit(vmx, entry);
1697 +- vm_exit_controls_clearbit(vmx, exit);
1698 +-}
1699 +-
1700 +-int vmx_find_msr_index(struct vmx_msrs *m, u32 msr)
1701 +-{
1702 +- unsigned int i;
1703 +-
1704 +- for (i = 0; i < m->nr; ++i) {
1705 +- if (m->val[i].index == msr)
1706 +- return i;
1707 +- }
1708 +- return -ENOENT;
1709 +-}
1710 +-
1711 +-static void clear_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr)
1712 +-{
1713 +- int i;
1714 +- struct msr_autoload *m = &vmx->msr_autoload;
1715 +-
1716 +- switch (msr) {
1717 +- case MSR_EFER:
1718 +- if (cpu_has_load_ia32_efer()) {
1719 +- clear_atomic_switch_msr_special(vmx,
1720 +- VM_ENTRY_LOAD_IA32_EFER,
1721 +- VM_EXIT_LOAD_IA32_EFER);
1722 +- return;
1723 +- }
1724 +- break;
1725 +- case MSR_CORE_PERF_GLOBAL_CTRL:
1726 +- if (cpu_has_load_perf_global_ctrl()) {
1727 +- clear_atomic_switch_msr_special(vmx,
1728 +- VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
1729 +- VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL);
1730 +- return;
1731 +- }
1732 +- break;
1733 +- }
1734 +- i = vmx_find_msr_index(&m->guest, msr);
1735 +- if (i < 0)
1736 +- goto skip_guest;
1737 +- --m->guest.nr;
1738 +- m->guest.val[i] = m->guest.val[m->guest.nr];
1739 +- vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
1740 +-
1741 +-skip_guest:
1742 +- i = vmx_find_msr_index(&m->host, msr);
1743 +- if (i < 0)
1744 +- return;
1745 +-
1746 +- --m->host.nr;
1747 +- m->host.val[i] = m->host.val[m->host.nr];
1748 +- vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
1749 +-}
1750 +-
1751 +-static void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
1752 +- unsigned long entry, unsigned long exit,
1753 +- unsigned long guest_val_vmcs, unsigned long host_val_vmcs,
1754 +- u64 guest_val, u64 host_val)
1755 +-{
1756 +- vmcs_write64(guest_val_vmcs, guest_val);
1757 +- if (host_val_vmcs != HOST_IA32_EFER)
1758 +- vmcs_write64(host_val_vmcs, host_val);
1759 +- vm_entry_controls_setbit(vmx, entry);
1760 +- vm_exit_controls_setbit(vmx, exit);
1761 +-}
1762 +-
1763 +-static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
1764 +- u64 guest_val, u64 host_val, bool entry_only)
1765 +-{
1766 +- int i, j = 0;
1767 +- struct msr_autoload *m = &vmx->msr_autoload;
1768 +-
1769 +- switch (msr) {
1770 +- case MSR_EFER:
1771 +- if (cpu_has_load_ia32_efer()) {
1772 +- add_atomic_switch_msr_special(vmx,
1773 +- VM_ENTRY_LOAD_IA32_EFER,
1774 +- VM_EXIT_LOAD_IA32_EFER,
1775 +- GUEST_IA32_EFER,
1776 +- HOST_IA32_EFER,
1777 +- guest_val, host_val);
1778 +- return;
1779 +- }
1780 +- break;
1781 +- case MSR_CORE_PERF_GLOBAL_CTRL:
1782 +- if (cpu_has_load_perf_global_ctrl()) {
1783 +- add_atomic_switch_msr_special(vmx,
1784 +- VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL,
1785 +- VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL,
1786 +- GUEST_IA32_PERF_GLOBAL_CTRL,
1787 +- HOST_IA32_PERF_GLOBAL_CTRL,
1788 +- guest_val, host_val);
1789 +- return;
1790 +- }
1791 +- break;
1792 +- case MSR_IA32_PEBS_ENABLE:
1793 +- /* PEBS needs a quiescent period after being disabled (to write
1794 +- * a record). Disabling PEBS through VMX MSR swapping doesn't
1795 +- * provide that period, so a CPU could write host's record into
1796 +- * guest's memory.
1797 +- */
1798 +- wrmsrl(MSR_IA32_PEBS_ENABLE, 0);
1799 +- }
1800 +-
1801 +- i = vmx_find_msr_index(&m->guest, msr);
1802 +- if (!entry_only)
1803 +- j = vmx_find_msr_index(&m->host, msr);
1804 +-
1805 +- if ((i < 0 && m->guest.nr == NR_LOADSTORE_MSRS) ||
1806 +- (j < 0 && m->host.nr == NR_LOADSTORE_MSRS)) {
1807 +- printk_once(KERN_WARNING "Not enough msr switch entries. "
1808 +- "Can't add msr %x\n", msr);
1809 +- return;
1810 +- }
1811 +- if (i < 0) {
1812 +- i = m->guest.nr++;
1813 +- vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
1814 +- }
1815 +- m->guest.val[i].index = msr;
1816 +- m->guest.val[i].value = guest_val;
1817 +-
1818 +- if (entry_only)
1819 +- return;
1820 +-
1821 +- if (j < 0) {
1822 +- j = m->host.nr++;
1823 +- vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
1824 +- }
1825 +- m->host.val[j].index = msr;
1826 +- m->host.val[j].value = host_val;
1827 +-}
1828 +-
1829 +-static bool update_transition_efer(struct vcpu_vmx *vmx, int efer_offset)
1830 +-{
1831 +- u64 guest_efer = vmx->vcpu.arch.efer;
1832 +- u64 ignore_bits = 0;
1833 +-
1834 +- /* Shadow paging assumes NX to be available. */
1835 +- if (!enable_ept)
1836 +- guest_efer |= EFER_NX;
1837 +-
1838 +- /*
1839 +- * LMA and LME handled by hardware; SCE meaningless outside long mode.
1840 +- */
1841 +- ignore_bits |= EFER_SCE;
1842 +-#ifdef CONFIG_X86_64
1843 +- ignore_bits |= EFER_LMA | EFER_LME;
1844 +- /* SCE is meaningful only in long mode on Intel */
1845 +- if (guest_efer & EFER_LMA)
1846 +- ignore_bits &= ~(u64)EFER_SCE;
1847 +-#endif
1848 +-
1849 +- /*
1850 +- * On EPT, we can't emulate NX, so we must switch EFER atomically.
1851 +- * On CPUs that support "load IA32_EFER", always switch EFER
1852 +- * atomically, since it's faster than switching it manually.
1853 +- */
1854 +- if (cpu_has_load_ia32_efer() ||
1855 +- (enable_ept && ((vmx->vcpu.arch.efer ^ host_efer) & EFER_NX))) {
1856 +- if (!(guest_efer & EFER_LMA))
1857 +- guest_efer &= ~EFER_LME;
1858 +- if (guest_efer != host_efer)
1859 +- add_atomic_switch_msr(vmx, MSR_EFER,
1860 +- guest_efer, host_efer, false);
1861 +- else
1862 +- clear_atomic_switch_msr(vmx, MSR_EFER);
1863 +- return false;
1864 +- } else {
1865 +- clear_atomic_switch_msr(vmx, MSR_EFER);
1866 +-
1867 +- guest_efer &= ~ignore_bits;
1868 +- guest_efer |= host_efer & ignore_bits;
1869 +-
1870 +- vmx->guest_msrs[efer_offset].data = guest_efer;
1871 +- vmx->guest_msrs[efer_offset].mask = ~ignore_bits;
1872 +-
1873 +- return true;
1874 +- }
1875 +-}
1876 +-
1877 +-#ifdef CONFIG_X86_32
1878 +-/*
1879 +- * On 32-bit kernels, VM exits still load the FS and GS bases from the
1880 +- * VMCS rather than the segment table. KVM uses this helper to figure
1881 +- * out the current bases to poke them into the VMCS before entry.
1882 +- */
1883 +-static unsigned long segment_base(u16 selector)
1884 +-{
1885 +- struct desc_struct *table;
1886 +- unsigned long v;
1887 +-
1888 +- if (!(selector & ~SEGMENT_RPL_MASK))
1889 +- return 0;
1890 +-
1891 +- table = get_current_gdt_ro();
1892 +-
1893 +- if ((selector & SEGMENT_TI_MASK) == SEGMENT_LDT) {
1894 +- u16 ldt_selector = kvm_read_ldt();
1895 +-
1896 +- if (!(ldt_selector & ~SEGMENT_RPL_MASK))
1897 +- return 0;
1898 +-
1899 +- table = (struct desc_struct *)segment_base(ldt_selector);
1900 +- }
1901 +- v = get_desc_base(&table[selector >> 3]);
1902 +- return v;
1903 +-}
1904 +-#endif
1905 +-
1906 +-static inline void pt_load_msr(struct pt_ctx *ctx, u32 addr_range)
1907 +-{
1908 +- u32 i;
1909 +-
1910 +- wrmsrl(MSR_IA32_RTIT_STATUS, ctx->status);
1911 +- wrmsrl(MSR_IA32_RTIT_OUTPUT_BASE, ctx->output_base);
1912 +- wrmsrl(MSR_IA32_RTIT_OUTPUT_MASK, ctx->output_mask);
1913 +- wrmsrl(MSR_IA32_RTIT_CR3_MATCH, ctx->cr3_match);
1914 +- for (i = 0; i < addr_range; i++) {
1915 +- wrmsrl(MSR_IA32_RTIT_ADDR0_A + i * 2, ctx->addr_a[i]);
1916 +- wrmsrl(MSR_IA32_RTIT_ADDR0_B + i * 2, ctx->addr_b[i]);
1917 +- }
1918 +-}
1919 +-
1920 +-static inline void pt_save_msr(struct pt_ctx *ctx, u32 addr_range)
1921 +-{
1922 +- u32 i;
1923 +-
1924 +- rdmsrl(MSR_IA32_RTIT_STATUS, ctx->status);
1925 +- rdmsrl(MSR_IA32_RTIT_OUTPUT_BASE, ctx->output_base);
1926 +- rdmsrl(MSR_IA32_RTIT_OUTPUT_MASK, ctx->output_mask);
1927 +- rdmsrl(MSR_IA32_RTIT_CR3_MATCH, ctx->cr3_match);
1928 +- for (i = 0; i < addr_range; i++) {
1929 +- rdmsrl(MSR_IA32_RTIT_ADDR0_A + i * 2, ctx->addr_a[i]);
1930 +- rdmsrl(MSR_IA32_RTIT_ADDR0_B + i * 2, ctx->addr_b[i]);
1931 +- }
1932 +-}
1933 +-
1934 +-static void pt_guest_enter(struct vcpu_vmx *vmx)
1935 +-{
1936 +- if (pt_mode == PT_MODE_SYSTEM)
1937 +- return;
1938 +-
1939 +- /*
1940 +- * GUEST_IA32_RTIT_CTL is already set in the VMCS.
1941 +- * Save host state before VM entry.
1942 +- */
1943 +- rdmsrl(MSR_IA32_RTIT_CTL, vmx->pt_desc.host.ctl);
1944 +- if (vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN) {
1945 +- wrmsrl(MSR_IA32_RTIT_CTL, 0);
1946 +- pt_save_msr(&vmx->pt_desc.host, vmx->pt_desc.addr_range);
1947 +- pt_load_msr(&vmx->pt_desc.guest, vmx->pt_desc.addr_range);
1948 +- }
1949 +-}
1950 +-
1951 +-static void pt_guest_exit(struct vcpu_vmx *vmx)
1952 +-{
1953 +- if (pt_mode == PT_MODE_SYSTEM)
1954 +- return;
1955 +-
1956 +- if (vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN) {
1957 +- pt_save_msr(&vmx->pt_desc.guest, vmx->pt_desc.addr_range);
1958 +- pt_load_msr(&vmx->pt_desc.host, vmx->pt_desc.addr_range);
1959 +- }
1960 +-
1961 +- /* Reload host state (IA32_RTIT_CTL will be cleared on VM exit). */
1962 +- wrmsrl(MSR_IA32_RTIT_CTL, vmx->pt_desc.host.ctl);
1963 +-}
1964 +-
1965 +-void vmx_set_host_fs_gs(struct vmcs_host_state *host, u16 fs_sel, u16 gs_sel,
1966 +- unsigned long fs_base, unsigned long gs_base)
1967 +-{
1968 +- if (unlikely(fs_sel != host->fs_sel)) {
1969 +- if (!(fs_sel & 7))
1970 +- vmcs_write16(HOST_FS_SELECTOR, fs_sel);
1971 +- else
1972 +- vmcs_write16(HOST_FS_SELECTOR, 0);
1973 +- host->fs_sel = fs_sel;
1974 +- }
1975 +- if (unlikely(gs_sel != host->gs_sel)) {
1976 +- if (!(gs_sel & 7))
1977 +- vmcs_write16(HOST_GS_SELECTOR, gs_sel);
1978 +- else
1979 +- vmcs_write16(HOST_GS_SELECTOR, 0);
1980 +- host->gs_sel = gs_sel;
1981 +- }
1982 +- if (unlikely(fs_base != host->fs_base)) {
1983 +- vmcs_writel(HOST_FS_BASE, fs_base);
1984 +- host->fs_base = fs_base;
1985 +- }
1986 +- if (unlikely(gs_base != host->gs_base)) {
1987 +- vmcs_writel(HOST_GS_BASE, gs_base);
1988 +- host->gs_base = gs_base;
1989 +- }
1990 +-}
1991 +-
1992 +-void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu)
1993 +-{
1994 +- struct vcpu_vmx *vmx = to_vmx(vcpu);
1995 +- struct vmcs_host_state *host_state;
1996 +-#ifdef CONFIG_X86_64
1997 +- int cpu = raw_smp_processor_id();
1998 +-#endif
1999 +- unsigned long fs_base, gs_base;
2000 +- u16 fs_sel, gs_sel;
2001 +- int i;
2002 +-
2003 +- vmx->req_immediate_exit = false;
2004 +-
2005 +- /*
2006 +- * Note that guest MSRs to be saved/restored can also be changed
2007 +- * when guest state is loaded. This happens when guest transitions
2008 +- * to/from long-mode by setting MSR_EFER.LMA.
2009 +- */
2010 +- if (!vmx->guest_msrs_ready) {
2011 +- vmx->guest_msrs_ready = true;
2012 +- for (i = 0; i < vmx->save_nmsrs; ++i)
2013 +- kvm_set_shared_msr(vmx->guest_msrs[i].index,
2014 +- vmx->guest_msrs[i].data,
2015 +- vmx->guest_msrs[i].mask);
2016 +-
2017 +- }
2018 +- if (vmx->guest_state_loaded)
2019 +- return;
2020 +-
2021 +- host_state = &vmx->loaded_vmcs->host_state;
2022 +-
2023 +- /*
2024 +- * Set host fs and gs selectors. Unfortunately, 22.2.3 does not
2025 +- * allow segment selectors with cpl > 0 or ti == 1.
2026 +- */
2027 +- host_state->ldt_sel = kvm_read_ldt();
2028 +-
2029 +-#ifdef CONFIG_X86_64
2030 +- savesegment(ds, host_state->ds_sel);
2031 +- savesegment(es, host_state->es_sel);
2032 +-
2033 +- gs_base = cpu_kernelmode_gs_base(cpu);
2034 +- if (likely(is_64bit_mm(current->mm))) {
2035 +- save_fsgs_for_kvm();
2036 +- fs_sel = current->thread.fsindex;
2037 +- gs_sel = current->thread.gsindex;
2038 +- fs_base = current->thread.fsbase;
2039 +- vmx->msr_host_kernel_gs_base = current->thread.gsbase;
2040 +- } else {
2041 +- savesegment(fs, fs_sel);
2042 +- savesegment(gs, gs_sel);
2043 +- fs_base = read_msr(MSR_FS_BASE);
2044 +- vmx->msr_host_kernel_gs_base = read_msr(MSR_KERNEL_GS_BASE);
2045 +- }
2046 +-
2047 +- wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2048 +-#else
2049 +- savesegment(fs, fs_sel);
2050 +- savesegment(gs, gs_sel);
2051 +- fs_base = segment_base(fs_sel);
2052 +- gs_base = segment_base(gs_sel);
2053 +-#endif
2054 +-
2055 +- vmx_set_host_fs_gs(host_state, fs_sel, gs_sel, fs_base, gs_base);
2056 +- vmx->guest_state_loaded = true;
2057 +-}
2058 +-
2059 +-static void vmx_prepare_switch_to_host(struct vcpu_vmx *vmx)
2060 +-{
2061 +- struct vmcs_host_state *host_state;
2062 +-
2063 +- if (!vmx->guest_state_loaded)
2064 +- return;
2065 +-
2066 +- host_state = &vmx->loaded_vmcs->host_state;
2067 +-
2068 +- ++vmx->vcpu.stat.host_state_reload;
2069 +-
2070 +-#ifdef CONFIG_X86_64
2071 +- rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2072 +-#endif
2073 +- if (host_state->ldt_sel || (host_state->gs_sel & 7)) {
2074 +- kvm_load_ldt(host_state->ldt_sel);
2075 +-#ifdef CONFIG_X86_64
2076 +- load_gs_index(host_state->gs_sel);
2077 +-#else
2078 +- loadsegment(gs, host_state->gs_sel);
2079 +-#endif
2080 +- }
2081 +- if (host_state->fs_sel & 7)
2082 +- loadsegment(fs, host_state->fs_sel);
2083 +-#ifdef CONFIG_X86_64
2084 +- if (unlikely(host_state->ds_sel | host_state->es_sel)) {
2085 +- loadsegment(ds, host_state->ds_sel);
2086 +- loadsegment(es, host_state->es_sel);
2087 +- }
2088 +-#endif
2089 +- invalidate_tss_limit();
2090 +-#ifdef CONFIG_X86_64
2091 +- wrmsrl(MSR_KERNEL_GS_BASE, vmx->msr_host_kernel_gs_base);
2092 +-#endif
2093 +- load_fixmap_gdt(raw_smp_processor_id());
2094 +- vmx->guest_state_loaded = false;
2095 +- vmx->guest_msrs_ready = false;
2096 +-}
2097 +-
2098 +-#ifdef CONFIG_X86_64
2099 +-static u64 vmx_read_guest_kernel_gs_base(struct vcpu_vmx *vmx)
2100 +-{
2101 +- preempt_disable();
2102 +- if (vmx->guest_state_loaded)
2103 +- rdmsrl(MSR_KERNEL_GS_BASE, vmx->msr_guest_kernel_gs_base);
2104 +- preempt_enable();
2105 +- return vmx->msr_guest_kernel_gs_base;
2106 +-}
2107 +-
2108 +-static void vmx_write_guest_kernel_gs_base(struct vcpu_vmx *vmx, u64 data)
2109 +-{
2110 +- preempt_disable();
2111 +- if (vmx->guest_state_loaded)
2112 +- wrmsrl(MSR_KERNEL_GS_BASE, data);
2113 +- preempt_enable();
2114 +- vmx->msr_guest_kernel_gs_base = data;
2115 +-}
2116 +-#endif
2117 +-
2118 +-static void vmx_vcpu_pi_load(struct kvm_vcpu *vcpu, int cpu)
2119 +-{
2120 +- struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2121 +- struct pi_desc old, new;
2122 +- unsigned int dest;
2123 +-
2124 +- /*
2125 +- * In case of hot-plug or hot-unplug, we may have to undo
2126 +- * vmx_vcpu_pi_put even if there is no assigned device. And we
2127 +- * always keep PI.NDST up to date for simplicity: it makes the
2128 +- * code easier, and CPU migration is not a fast path.
2129 +- */
2130 +- if (!pi_test_sn(pi_desc) && vcpu->cpu == cpu)
2131 +- return;
2132 +-
2133 +- /*
2134 +- * If the 'nv' field is POSTED_INTR_WAKEUP_VECTOR, do not change
2135 +- * PI.NDST: pi_post_block is the one expected to change PID.NDST and the
2136 +- * wakeup handler expects the vCPU to be on the blocked_vcpu_list that
2137 +- * matches PI.NDST. Otherwise, a vcpu may not be able to be woken up
2138 +- * correctly.
2139 +- */
2140 +- if (pi_desc->nv == POSTED_INTR_WAKEUP_VECTOR || vcpu->cpu == cpu) {
2141 +- pi_clear_sn(pi_desc);
2142 +- goto after_clear_sn;
2143 +- }
2144 +-
2145 +- /* The full case. */
2146 +- do {
2147 +- old.control = new.control = pi_desc->control;
2148 +-
2149 +- dest = cpu_physical_id(cpu);
2150 +-
2151 +- if (x2apic_enabled())
2152 +- new.ndst = dest;
2153 +- else
2154 +- new.ndst = (dest << 8) & 0xFF00;
2155 +-
2156 +- new.sn = 0;
2157 +- } while (cmpxchg64(&pi_desc->control, old.control,
2158 +- new.control) != old.control);
2159 +-
2160 +-after_clear_sn:
2161 +-
2162 +- /*
2163 +- * Clear SN before reading the bitmap. The VT-d firmware
2164 +- * writes the bitmap and reads SN atomically (5.2.3 in the
2165 +- * spec), so it doesn't really have a memory barrier that
2166 +- * pairs with this, but we cannot do that and we need one.
2167 +- */
2168 +- smp_mb__after_atomic();
2169 +-
2170 +- if (!pi_is_pir_empty(pi_desc))
2171 +- pi_set_on(pi_desc);
2172 +-}
2173 +-
2174 +-void vmx_vcpu_load_vmcs(struct kvm_vcpu *vcpu, int cpu)
2175 +-{
2176 +- struct vcpu_vmx *vmx = to_vmx(vcpu);
2177 +- bool already_loaded = vmx->loaded_vmcs->cpu == cpu;
2178 +-
2179 +- if (!already_loaded) {
2180 +- loaded_vmcs_clear(vmx->loaded_vmcs);
2181 +- local_irq_disable();
2182 +- crash_disable_local_vmclear(cpu);
2183 +-
2184 +- /*
2185 +- * Read loaded_vmcs->cpu should be before fetching
2186 +- * loaded_vmcs->loaded_vmcss_on_cpu_link.
2187 +- * See the comments in __loaded_vmcs_clear().
2188 +- */
2189 +- smp_rmb();
2190 +-
2191 +- list_add(&vmx->loaded_vmcs->loaded_vmcss_on_cpu_link,
2192 +- &per_cpu(loaded_vmcss_on_cpu, cpu));
2193 +- crash_enable_local_vmclear(cpu);
2194 +- local_irq_enable();
2195 +- }
2196 +-
2197 +- if (per_cpu(current_vmcs, cpu) != vmx->loaded_vmcs->vmcs) {
2198 +- per_cpu(current_vmcs, cpu) = vmx->loaded_vmcs->vmcs;
2199 +- vmcs_load(vmx->loaded_vmcs->vmcs);
2200 +- indirect_branch_prediction_barrier();
2201 +- }
2202 +-
2203 +- if (!already_loaded) {
2204 +- void *gdt = get_current_gdt_ro();
2205 +- unsigned long sysenter_esp;
2206 +-
2207 +- kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
2208 +-
2209 +- /*
2210 +- * Linux uses per-cpu TSS and GDT, so set these when switching
2211 +- * processors. See 22.2.4.
2212 +- */
2213 +- vmcs_writel(HOST_TR_BASE,
2214 +- (unsigned long)&get_cpu_entry_area(cpu)->tss.x86_tss);
2215 +- vmcs_writel(HOST_GDTR_BASE, (unsigned long)gdt); /* 22.2.4 */
2216 +-
2217 +- rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp);
2218 +- vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */
2219 +-
2220 +- vmx->loaded_vmcs->cpu = cpu;
2221 +- }
2222 +-
2223 +- /* Setup TSC multiplier */
2224 +- if (kvm_has_tsc_control &&
2225 +- vmx->current_tsc_ratio != vcpu->arch.tsc_scaling_ratio)
2226 +- decache_tsc_multiplier(vmx);
2227 +-}
2228 +-
2229 +-/*
2230 +- * Switches to specified vcpu, until a matching vcpu_put(), but assumes
2231 +- * vcpu mutex is already taken.
2232 +- */
2233 +-void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
2234 +-{
2235 +- struct vcpu_vmx *vmx = to_vmx(vcpu);
2236 +-
2237 +- vmx_vcpu_load_vmcs(vcpu, cpu);
2238 +-
2239 +- vmx_vcpu_pi_load(vcpu, cpu);
2240 +-
2241 +- vmx->host_pkru = read_pkru();
2242 +- vmx->host_debugctlmsr = get_debugctlmsr();
2243 +-}
2244 +-
2245 +-static void vmx_vcpu_pi_put(struct kvm_vcpu *vcpu)
2246 +-{
2247 +- struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
2248 +-
2249 +- if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
2250 +- !irq_remapping_cap(IRQ_POSTING_CAP) ||
2251 +- !kvm_vcpu_apicv_active(vcpu))
2252 +- return;
2253 +-
2254 +- /* Set SN when the vCPU is preempted */
2255 +- if (vcpu->preempted)
2256 +- pi_set_sn(pi_desc);
2257 +-}
2258 +-
2259 +-static void vmx_vcpu_put(struct kvm_vcpu *vcpu)
2260 +-{
2261 +- vmx_vcpu_pi_put(vcpu);
2262 +-
2263 +- vmx_prepare_switch_to_host(to_vmx(vcpu));
2264 +-}
2265 +-
2266 +-static bool emulation_required(struct kvm_vcpu *vcpu)
2267 +-{
2268 +- return emulate_invalid_guest_state && !guest_state_valid(vcpu);
2269 +-}
2270 +-
2271 +-static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu);
2272 +-
2273 +-unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu)
2274 +-{
2275 +- struct vcpu_vmx *vmx = to_vmx(vcpu);
2276 +- unsigned long rflags, save_rflags;
2277 +-
2278 +- if (!kvm_register_is_available(vcpu, VCPU_EXREG_RFLAGS)) {
2279 +- kvm_register_mark_available(vcpu, VCPU_EXREG_RFLAGS);
2280 +- rflags = vmcs_readl(GUEST_RFLAGS);
2281 +- if (vmx->rmode.vm86_active) {
2282 +- rflags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
2283 +- save_rflags = vmx->rmode.save_rflags;
2284 +- rflags |= save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
2285 +- }
2286 +- vmx->rflags = rflags;
2287 +- }
2288 +- return vmx->rflags;
2289 +-}
2290 +-
2291 +-void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
2292 +-{
2293 +- struct vcpu_vmx *vmx = to_vmx(vcpu);
2294 +- unsigned long old_rflags;
2295 +-
2296 +- if (enable_unrestricted_guest) {
2297 +- kvm_register_mark_available(vcpu, VCPU_EXREG_RFLAGS);
2298 +- vmx->rflags = rflags;
2299 +- vmcs_writel(GUEST_RFLAGS, rflags);
2300 +- return;
2301 +- }
2302 +-
2303 +- old_rflags = vmx_get_rflags(vcpu);
2304 +- vmx->rflags = rflags;
2305 +- if (vmx->rmode.vm86_active) {
2306 +- vmx->rmode.save_rflags = rflags;
2307 +- rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
2308 +- }
2309 +- vmcs_writel(GUEST_RFLAGS, rflags);
2310 +-
2311 +- if ((old_rflags ^ vmx->rflags) & X86_EFLAGS_VM)
2312 +- vmx->emulation_required = emulation_required(vcpu);
2313 +-}
2314 +-
2315 +-u32 vmx_get_interrupt_shadow(struct kvm_vcpu *vcpu)
2316 +-{
2317 +- u32 interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2318 +- int ret = 0;
2319 +-
2320 +- if (interruptibility & GUEST_INTR_STATE_STI)
2321 +- ret |= KVM_X86_SHADOW_INT_STI;
2322 +- if (interruptibility & GUEST_INTR_STATE_MOV_SS)
2323 +- ret |= KVM_X86_SHADOW_INT_MOV_SS;
2324 +-
2325 +- return ret;
2326 +-}
2327 +-
2328 +-void vmx_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
2329 +-{
2330 +- u32 interruptibility_old = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO);
2331 +- u32 interruptibility = interruptibility_old;
2332 +-
2333 +- interruptibility &= ~(GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS);
2334 +-
2335 +- if (mask & KVM_X86_SHADOW_INT_MOV_SS)
2336 +- interruptibility |= GUEST_INTR_STATE_MOV_SS;
2337 +- else if (mask & KVM_X86_SHADOW_INT_STI)
2338 +- interruptibility |= GUEST_INTR_STATE_STI;
2339 +-
2340 +- if ((interruptibility != interruptibility_old))
2341 +- vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, interruptibility);
2342 +-}
2343 +-
2344 +-static int vmx_rtit_ctl_check(struct kvm_vcpu *vcpu, u64 data)
2345 +-{
2346 +- struct vcpu_vmx *vmx = to_vmx(vcpu);
2347 +- unsigned long value;
2348 +-
2349 +- /*
2350 +- * Any MSR write that attempts to change bits marked reserved will
2351 +- * case a #GP fault.
2352 +- */
2353 +- if (data & vmx->pt_desc.ctl_bitmask)
2354 +- return 1;
2355 +-
2356 +- /*
2357 +- * Any attempt to modify IA32_RTIT_CTL while TraceEn is set will
2358 +- * result in a #GP unless the same write also clears TraceEn.
2359 +- */
2360 +- if ((vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN) &&
2361 +- ((vmx->pt_desc.guest.ctl ^ data) & ~RTIT_CTL_TRACEEN))
2362 +- return 1;
2363 +-
2364 +- /*
2365 +- * WRMSR to IA32_RTIT_CTL that sets TraceEn but clears this bit
2366 +- * and FabricEn would cause #GP, if
2367 +- * CPUID.(EAX=14H, ECX=0):ECX.SNGLRGNOUT[bit 2] = 0
2368 +- */
2369 +- if ((data & RTIT_CTL_TRACEEN) && !(data & RTIT_CTL_TOPA) &&
2370 +- !(data & RTIT_CTL_FABRIC_EN) &&
2371 +- !intel_pt_validate_cap(vmx->pt_desc.caps,
2372 +- PT_CAP_single_range_output))
2373 +- return 1;
2374 +-
2375 +- /*
2376 +- * MTCFreq, CycThresh and PSBFreq encodings check, any MSR write that
2377 +- * utilize encodings marked reserved will casue a #GP fault.
2378 +- */
2379 +- value = intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_mtc_periods);
2380 +- if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_mtc) &&
2381 +- !test_bit((data & RTIT_CTL_MTC_RANGE) >>
2382 +- RTIT_CTL_MTC_RANGE_OFFSET, &value))
2383 +- return 1;
2384 +- value = intel_pt_validate_cap(vmx->pt_desc.caps,
2385 +- PT_CAP_cycle_thresholds);
2386 +- if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_psb_cyc) &&
2387 +- !test_bit((data & RTIT_CTL_CYC_THRESH) >>
2388 +- RTIT_CTL_CYC_THRESH_OFFSET, &value))
2389 +- return 1;
2390 +- value = intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_psb_periods);
2391 +- if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_psb_cyc) &&
2392 +- !test_bit((data & RTIT_CTL_PSB_FREQ) >>
2393 +- RTIT_CTL_PSB_FREQ_OFFSET, &value))
2394 +- return 1;
2395 +-
2396 +- /*
2397 +- * If ADDRx_CFG is reserved or the encodings is >2 will
2398 +- * cause a #GP fault.
2399 +- */
2400 +- value = (data & RTIT_CTL_ADDR0) >> RTIT_CTL_ADDR0_OFFSET;
2401 +- if ((value && (vmx->pt_desc.addr_range < 1)) || (value > 2))
2402 +- return 1;
2403 +- value = (data & RTIT_CTL_ADDR1) >> RTIT_CTL_ADDR1_OFFSET;
2404 +- if ((value && (vmx->pt_desc.addr_range < 2)) || (value > 2))
2405 +- return 1;
2406 +- value = (data & RTIT_CTL_ADDR2) >> RTIT_CTL_ADDR2_OFFSET;
2407 +- if ((value && (vmx->pt_desc.addr_range < 3)) || (value > 2))
2408 +- return 1;
2409 +- value = (data & RTIT_CTL_ADDR3) >> RTIT_CTL_ADDR3_OFFSET;
2410 +- if ((value && (vmx->pt_desc.addr_range < 4)) || (value > 2))
2411 +- return 1;
2412 +-
2413 +- return 0;
2414 +-}
2415 +-
2416 +-static int skip_emulated_instruction(struct kvm_vcpu *vcpu)
2417 +-{
2418 +- unsigned long rip;
2419 +-
2420 +- /*
2421 +- * Using VMCS.VM_EXIT_INSTRUCTION_LEN on EPT misconfig depends on
2422 +- * undefined behavior: Intel's SDM doesn't mandate the VMCS field be
2423 +- * set when EPT misconfig occurs. In practice, real hardware updates
2424 +- * VM_EXIT_INSTRUCTION_LEN on EPT misconfig, but other hypervisors
2425 +- * (namely Hyper-V) don't set it due to it being undefined behavior,
2426 +- * i.e. we end up advancing IP with some random value.
2427 +- */
2428 +- if (!static_cpu_has(X86_FEATURE_HYPERVISOR) ||
2429 +- to_vmx(vcpu)->exit_reason != EXIT_REASON_EPT_MISCONFIG) {
2430 +- rip = kvm_rip_read(vcpu);
2431 +- rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
2432 +- kvm_rip_write(vcpu, rip);
2433 +- } else {
2434 +- if (!kvm_emulate_instruction(vcpu, EMULTYPE_SKIP))
2435 +- return 0;
2436 +- }
2437 +-
2438 +- /* skipping an emulated instruction also counts */
2439 +- vmx_set_interrupt_shadow(vcpu, 0);
2440 +-
2441 +- return 1;
2442 +-}
2443 +-
2444 +-static void vmx_clear_hlt(struct kvm_vcpu *vcpu)
2445 +-{
2446 +- /*
2447 +- * Ensure that we clear the HLT state in the VMCS. We don't need to
2448 +- * explicitly skip the instruction because if the HLT state is set,
2449 +- * then the instruction is already executing and RIP has already been
2450 +- * advanced.
2451 +- */
2452 +- if (kvm_hlt_in_guest(vcpu->kvm) &&
2453 +- vmcs_read32(GUEST_ACTIVITY_STATE) == GUEST_ACTIVITY_HLT)
2454 +- vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
2455 +-}
2456 +-
2457 +-static void vmx_queue_exception(struct kvm_vcpu *vcpu)
2458 +-{
2459 +- struct vcpu_vmx *vmx = to_vmx(vcpu);
2460 +- unsigned nr = vcpu->arch.exception.nr;
2461 +- bool has_error_code = vcpu->arch.exception.has_error_code;
2462 +- u32 error_code = vcpu->arch.exception.error_code;
2463 +- u32 intr_info = nr | INTR_INFO_VALID_MASK;
2464 +-
2465 +- kvm_deliver_exception_payload(vcpu);
2466 +-
2467 +- if (has_error_code) {
2468 +- vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code);
2469 +- intr_info |= INTR_INFO_DELIVER_CODE_MASK;
2470 +- }
2471 +-
2472 +- if (vmx->rmode.vm86_active) {
2473 +- int inc_eip = 0;
2474 +- if (kvm_exception_is_soft(nr))
2475 +- inc_eip = vcpu->arch.event_exit_inst_len;
2476 +- kvm_inject_realmode_interrupt(vcpu, nr, inc_eip);
2477 +- return;
2478 +- }
2479 +-
2480 +- WARN_ON_ONCE(vmx->emulation_required);
2481 +-
2482 +- if (kvm_exception_is_soft(nr)) {
2483 +- vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
2484 +- vmx->vcpu.arch.event_exit_inst_len);
2485 +- intr_info |= INTR_TYPE_SOFT_EXCEPTION;
2486 +- } else
2487 +- intr_info |= INTR_TYPE_HARD_EXCEPTION;
2488 +-
2489 +- vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr_info);
2490 +-
2491 +- vmx_clear_hlt(vcpu);
2492 +-}
2493 +-
2494 +-static bool vmx_rdtscp_supported(void)
2495 +-{
2496 +- return cpu_has_vmx_rdtscp();
2497 +-}
2498 +-
2499 +-static bool vmx_invpcid_supported(void)
2500 +-{
2501 +- return cpu_has_vmx_invpcid();
2502 +-}
2503 +-
2504 +-/*
2505 +- * Swap MSR entry in host/guest MSR entry array.
2506 +- */
2507 +-static void move_msr_up(struct vcpu_vmx *vmx, int from, int to)
2508 +-{
2509 +- struct shared_msr_entry tmp;
2510 +-
2511 +- tmp = vmx->guest_msrs[to];
2512 +- vmx->guest_msrs[to] = vmx->guest_msrs[from];
2513 +- vmx->guest_msrs[from] = tmp;
2514 +-}
2515 +-
2516 +-/*
2517 +- * Set up the vmcs to automatically save and restore system
2518 +- * msrs. Don't touch the 64-bit msrs if the guest is in legacy
2519 +- * mode, as fiddling with msrs is very expensive.
2520 +- */
2521 +-static void setup_msrs(struct vcpu_vmx *vmx)
2522 +-{
2523 +- int save_nmsrs, index;
2524 +-
2525 +- save_nmsrs = 0;
2526 +-#ifdef CONFIG_X86_64
2527 +- /*
2528 +- * The SYSCALL MSRs are only needed on long mode guests, and only
2529 +- * when EFER.SCE is set.
2530 +- */
2531 +- if (is_long_mode(&vmx->vcpu) && (vmx->vcpu.arch.efer & EFER_SCE)) {
2532 +- index = __find_msr_index(vmx, MSR_STAR);
2533 +- if (index >= 0)
2534 +- move_msr_up(vmx, index, save_nmsrs++);
2535 +- index = __find_msr_index(vmx, MSR_LSTAR);
2536 +- if (index >= 0)
2537 +- move_msr_up(vmx, index, save_nmsrs++);
2538 +- index = __find_msr_index(vmx, MSR_SYSCALL_MASK);
2539 +- if (index >= 0)
2540 +- move_msr_up(vmx, index, save_nmsrs++);
2541 +- }
2542 +-#endif
2543 +- index = __find_msr_index(vmx, MSR_EFER);
2544 +- if (index >= 0 && update_transition_efer(vmx, index))
2545 +- move_msr_up(vmx, index, save_nmsrs++);
2546 +- index = __find_msr_index(vmx, MSR_TSC_AUX);
2547 +- if (index >= 0 && guest_cpuid_has(&vmx->vcpu, X86_FEATURE_RDTSCP))
2548 +- move_msr_up(vmx, index, save_nmsrs++);
2549 +- index = __find_msr_index(vmx, MSR_IA32_TSX_CTRL);
2550 +- if (index >= 0)
2551 +- move_msr_up(vmx, index, save_nmsrs++);
2552 +-
2553 +- vmx->save_nmsrs = save_nmsrs;
2554 +- vmx->guest_msrs_ready = false;
2555 +-
2556 +- if (cpu_has_vmx_msr_bitmap())
2557 +- vmx_update_msr_bitmap(&vmx->vcpu);
2558 +-}
2559 +-
2560 +-static u64 vmx_read_l1_tsc_offset(struct kvm_vcpu *vcpu)
2561 +-{
2562 +- struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2563 +-
2564 +- if (is_guest_mode(vcpu) &&
2565 +- (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETTING))
2566 +- return vcpu->arch.tsc_offset - vmcs12->tsc_offset;
2567 +-
2568 +- return vcpu->arch.tsc_offset;
2569 +-}
2570 +-
2571 +-static u64 vmx_write_l1_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
2572 +-{
2573 +- struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
2574 +- u64 g_tsc_offset = 0;
2575 +-
2576 +- /*
2577 +- * We're here if L1 chose not to trap WRMSR to TSC. According
2578 +- * to the spec, this should set L1's TSC; The offset that L1
2579 +- * set for L2 remains unchanged, and still needs to be added
2580 +- * to the newly set TSC to get L2's TSC.
2581 +- */
2582 +- if (is_guest_mode(vcpu) &&
2583 +- (vmcs12->cpu_based_vm_exec_control & CPU_BASED_USE_TSC_OFFSETTING))
2584 +- g_tsc_offset = vmcs12->tsc_offset;
2585 +-
2586 +- trace_kvm_write_tsc_offset(vcpu->vcpu_id,
2587 +- vcpu->arch.tsc_offset - g_tsc_offset,
2588 +- offset);
2589 +- vmcs_write64(TSC_OFFSET, offset + g_tsc_offset);
2590 +- return offset + g_tsc_offset;
2591 +-}
2592 +-
2593 +-/*
2594 +- * nested_vmx_allowed() checks whether a guest should be allowed to use VMX
2595 +- * instructions and MSRs (i.e., nested VMX). Nested VMX is disabled for
2596 +- * all guests if the "nested" module option is off, and can also be disabled
2597 +- * for a single guest by disabling its VMX cpuid bit.
2598 +- */
2599 +-bool nested_vmx_allowed(struct kvm_vcpu *vcpu)
2600 +-{
2601 +- return nested && guest_cpuid_has(vcpu, X86_FEATURE_VMX);
2602 +-}
2603 +-
2604 +-static inline bool vmx_feature_control_msr_valid(struct kvm_vcpu *vcpu,
2605 +- uint64_t val)
2606 +-{
2607 +- uint64_t valid_bits = to_vmx(vcpu)->msr_ia32_feature_control_valid_bits;
2608 +-
2609 +- return !(val & ~valid_bits);
2610 +-}
2611 +-
2612 +-static int vmx_get_msr_feature(struct kvm_msr_entry *msr)
2613 +-{
2614 +- switch (msr->index) {
2615 +- case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
2616 +- if (!nested)
2617 +- return 1;
2618 +- return vmx_get_vmx_msr(&vmcs_config.nested, msr->index, &msr->data);
2619 +- default:
2620 +- return 1;
2621 +- }
2622 +-}
2623 +-
2624 +-/*
2625 +- * Reads an msr value (of 'msr_index') into 'pdata'.
2626 +- * Returns 0 on success, non-0 otherwise.
2627 +- * Assumes vcpu_load() was already called.
2628 +- */
2629 +-static int vmx_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
2630 +-{
2631 +- struct vcpu_vmx *vmx = to_vmx(vcpu);
2632 +- struct shared_msr_entry *msr;
2633 +- u32 index;
2634 +-
2635 +- switch (msr_info->index) {
2636 +-#ifdef CONFIG_X86_64
2637 +- case MSR_FS_BASE:
2638 +- msr_info->data = vmcs_readl(GUEST_FS_BASE);
2639 +- break;
2640 +- case MSR_GS_BASE:
2641 +- msr_info->data = vmcs_readl(GUEST_GS_BASE);
2642 +- break;
2643 +- case MSR_KERNEL_GS_BASE:
2644 +- msr_info->data = vmx_read_guest_kernel_gs_base(vmx);
2645 +- break;
2646 +-#endif
2647 +- case MSR_EFER:
2648 +- return kvm_get_msr_common(vcpu, msr_info);
2649 +- case MSR_IA32_TSX_CTRL:
2650 +- if (!msr_info->host_initiated &&
2651 +- !(vcpu->arch.arch_capabilities & ARCH_CAP_TSX_CTRL_MSR))
2652 +- return 1;
2653 +- goto find_shared_msr;
2654 +- case MSR_IA32_UMWAIT_CONTROL:
2655 +- if (!msr_info->host_initiated && !vmx_has_waitpkg(vmx))
2656 +- return 1;
2657 +-
2658 +- msr_info->data = vmx->msr_ia32_umwait_control;
2659 +- break;
2660 +- case MSR_IA32_SPEC_CTRL:
2661 +- if (!msr_info->host_initiated &&
2662 +- !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
2663 +- return 1;
2664 +-
2665 +- msr_info->data = to_vmx(vcpu)->spec_ctrl;
2666 +- break;
2667 +- case MSR_IA32_SYSENTER_CS:
2668 +- msr_info->data = vmcs_read32(GUEST_SYSENTER_CS);
2669 +- break;
2670 +- case MSR_IA32_SYSENTER_EIP:
2671 +- msr_info->data = vmcs_readl(GUEST_SYSENTER_EIP);
2672 +- break;
2673 +- case MSR_IA32_SYSENTER_ESP:
2674 +- msr_info->data = vmcs_readl(GUEST_SYSENTER_ESP);
2675 +- break;
2676 +- case MSR_IA32_BNDCFGS:
2677 +- if (!kvm_mpx_supported() ||
2678 +- (!msr_info->host_initiated &&
2679 +- !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
2680 +- return 1;
2681 +- msr_info->data = vmcs_read64(GUEST_BNDCFGS);
2682 +- break;
2683 +- case MSR_IA32_MCG_EXT_CTL:
2684 +- if (!msr_info->host_initiated &&
2685 +- !(vmx->msr_ia32_feature_control &
2686 +- FEATURE_CONTROL_LMCE))
2687 +- return 1;
2688 +- msr_info->data = vcpu->arch.mcg_ext_ctl;
2689 +- break;
2690 +- case MSR_IA32_FEATURE_CONTROL:
2691 +- msr_info->data = vmx->msr_ia32_feature_control;
2692 +- break;
2693 +- case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
2694 +- if (!nested_vmx_allowed(vcpu))
2695 +- return 1;
2696 +- return vmx_get_vmx_msr(&vmx->nested.msrs, msr_info->index,
2697 +- &msr_info->data);
2698 +- case MSR_IA32_RTIT_CTL:
2699 +- if (pt_mode != PT_MODE_HOST_GUEST)
2700 +- return 1;
2701 +- msr_info->data = vmx->pt_desc.guest.ctl;
2702 +- break;
2703 +- case MSR_IA32_RTIT_STATUS:
2704 +- if (pt_mode != PT_MODE_HOST_GUEST)
2705 +- return 1;
2706 +- msr_info->data = vmx->pt_desc.guest.status;
2707 +- break;
2708 +- case MSR_IA32_RTIT_CR3_MATCH:
2709 +- if ((pt_mode != PT_MODE_HOST_GUEST) ||
2710 +- !intel_pt_validate_cap(vmx->pt_desc.caps,
2711 +- PT_CAP_cr3_filtering))
2712 +- return 1;
2713 +- msr_info->data = vmx->pt_desc.guest.cr3_match;
2714 +- break;
2715 +- case MSR_IA32_RTIT_OUTPUT_BASE:
2716 +- if ((pt_mode != PT_MODE_HOST_GUEST) ||
2717 +- (!intel_pt_validate_cap(vmx->pt_desc.caps,
2718 +- PT_CAP_topa_output) &&
2719 +- !intel_pt_validate_cap(vmx->pt_desc.caps,
2720 +- PT_CAP_single_range_output)))
2721 +- return 1;
2722 +- msr_info->data = vmx->pt_desc.guest.output_base;
2723 +- break;
2724 +- case MSR_IA32_RTIT_OUTPUT_MASK:
2725 +- if ((pt_mode != PT_MODE_HOST_GUEST) ||
2726 +- (!intel_pt_validate_cap(vmx->pt_desc.caps,
2727 +- PT_CAP_topa_output) &&
2728 +- !intel_pt_validate_cap(vmx->pt_desc.caps,
2729 +- PT_CAP_single_range_output)))
2730 +- return 1;
2731 +- msr_info->data = vmx->pt_desc.guest.output_mask;
2732 +- break;
2733 +- case MSR_IA32_RTIT_ADDR0_A ... MSR_IA32_RTIT_ADDR3_B:
2734 +- index = msr_info->index - MSR_IA32_RTIT_ADDR0_A;
2735 +- if ((pt_mode != PT_MODE_HOST_GUEST) ||
2736 +- (index >= 2 * intel_pt_validate_cap(vmx->pt_desc.caps,
2737 +- PT_CAP_num_address_ranges)))
2738 +- return 1;
2739 +- if (is_noncanonical_address(data, vcpu))
2740 +- return 1;
2741 +- if (index % 2)
2742 +- msr_info->data = vmx->pt_desc.guest.addr_b[index / 2];
2743 +- else
2744 +- msr_info->data = vmx->pt_desc.guest.addr_a[index / 2];
2745 +- break;
2746 +- case MSR_TSC_AUX:
2747 +- if (!msr_info->host_initiated &&
2748 +- !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
2749 +- return 1;
2750 +- goto find_shared_msr;
2751 +- default:
2752 +- find_shared_msr:
2753 +- msr = find_msr_entry(vmx, msr_info->index);
2754 +- if (msr) {
2755 +- msr_info->data = msr->data;
2756 +- break;
2757 +- }
2758 +- return kvm_get_msr_common(vcpu, msr_info);
2759 +- }
2760 +-
2761 +- return 0;
2762 +-}
2763 +-
2764 +-/*
2765 +- * Writes msr value into the appropriate "register".
2766 +- * Returns 0 on success, non-0 otherwise.
2767 +- * Assumes vcpu_load() was already called.
2768 +- */
2769 +-static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
2770 +-{
2771 +- struct vcpu_vmx *vmx = to_vmx(vcpu);
2772 +- struct shared_msr_entry *msr;
2773 +- int ret = 0;
2774 +- u32 msr_index = msr_info->index;
2775 +- u64 data = msr_info->data;
2776 +- u32 index;
2777 +-
2778 +- switch (msr_index) {
2779 +- case MSR_EFER:
2780 +- ret = kvm_set_msr_common(vcpu, msr_info);
2781 +- break;
2782 +-#ifdef CONFIG_X86_64
2783 +- case MSR_FS_BASE:
2784 +- vmx_segment_cache_clear(vmx);
2785 +- vmcs_writel(GUEST_FS_BASE, data);
2786 +- break;
2787 +- case MSR_GS_BASE:
2788 +- vmx_segment_cache_clear(vmx);
2789 +- vmcs_writel(GUEST_GS_BASE, data);
2790 +- break;
2791 +- case MSR_KERNEL_GS_BASE:
2792 +- vmx_write_guest_kernel_gs_base(vmx, data);
2793 +- break;
2794 +-#endif
2795 +- case MSR_IA32_SYSENTER_CS:
2796 +- if (is_guest_mode(vcpu))
2797 +- get_vmcs12(vcpu)->guest_sysenter_cs = data;
2798 +- vmcs_write32(GUEST_SYSENTER_CS, data);
2799 +- break;
2800 +- case MSR_IA32_SYSENTER_EIP:
2801 +- if (is_guest_mode(vcpu))
2802 +- get_vmcs12(vcpu)->guest_sysenter_eip = data;
2803 +- vmcs_writel(GUEST_SYSENTER_EIP, data);
2804 +- break;
2805 +- case MSR_IA32_SYSENTER_ESP:
2806 +- if (is_guest_mode(vcpu))
2807 +- get_vmcs12(vcpu)->guest_sysenter_esp = data;
2808 +- vmcs_writel(GUEST_SYSENTER_ESP, data);
2809 +- break;
2810 +- case MSR_IA32_DEBUGCTLMSR:
2811 +- if (is_guest_mode(vcpu) && get_vmcs12(vcpu)->vm_exit_controls &
2812 +- VM_EXIT_SAVE_DEBUG_CONTROLS)
2813 +- get_vmcs12(vcpu)->guest_ia32_debugctl = data;
2814 +-
2815 +- ret = kvm_set_msr_common(vcpu, msr_info);
2816 +- break;
2817 +-
2818 +- case MSR_IA32_BNDCFGS:
2819 +- if (!kvm_mpx_supported() ||
2820 +- (!msr_info->host_initiated &&
2821 +- !guest_cpuid_has(vcpu, X86_FEATURE_MPX)))
2822 +- return 1;
2823 +- if (is_noncanonical_address(data & PAGE_MASK, vcpu) ||
2824 +- (data & MSR_IA32_BNDCFGS_RSVD))
2825 +- return 1;
2826 +- vmcs_write64(GUEST_BNDCFGS, data);
2827 +- break;
2828 +- case MSR_IA32_UMWAIT_CONTROL:
2829 +- if (!msr_info->host_initiated && !vmx_has_waitpkg(vmx))
2830 +- return 1;
2831 +-
2832 +- /* The reserved bit 1 and non-32 bit [63:32] should be zero */
2833 +- if (data & (BIT_ULL(1) | GENMASK_ULL(63, 32)))
2834 +- return 1;
2835 +-
2836 +- vmx->msr_ia32_umwait_control = data;
2837 +- break;
2838 +- case MSR_IA32_SPEC_CTRL:
2839 +- if (!msr_info->host_initiated &&
2840 +- !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
2841 +- return 1;
2842 +-
2843 +- /* The STIBP bit doesn't fault even if it's not advertised */
2844 +- if (data & ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD))
2845 +- return 1;
2846 +-
2847 +- vmx->spec_ctrl = data;
2848 +-
2849 +- if (!data)
2850 +- break;
2851 +-
2852 +- /*
2853 +- * For non-nested:
2854 +- * When it's written (to non-zero) for the first time, pass
2855 +- * it through.
2856 +- *
2857 +- * For nested:
2858 +- * The handling of the MSR bitmap for L2 guests is done in
2859 +- * nested_vmx_prepare_msr_bitmap. We should not touch the
2860 +- * vmcs02.msr_bitmap here since it gets completely overwritten
2861 +- * in the merging. We update the vmcs01 here for L1 as well
2862 +- * since it will end up touching the MSR anyway now.
2863 +- */
2864 +- vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap,
2865 +- MSR_IA32_SPEC_CTRL,
2866 +- MSR_TYPE_RW);
2867 +- break;
2868 +- case MSR_IA32_TSX_CTRL:
2869 +- if (!msr_info->host_initiated &&
2870 +- !(vcpu->arch.arch_capabilities & ARCH_CAP_TSX_CTRL_MSR))
2871 +- return 1;
2872 +- if (data & ~(TSX_CTRL_RTM_DISABLE | TSX_CTRL_CPUID_CLEAR))
2873 +- return 1;
2874 +- goto find_shared_msr;
2875 +- case MSR_IA32_PRED_CMD:
2876 +- if (!msr_info->host_initiated &&
2877 +- !guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL))
2878 +- return 1;
2879 +-
2880 +- if (data & ~PRED_CMD_IBPB)
2881 +- return 1;
2882 +-
2883 +- if (!data)
2884 +- break;
2885 +-
2886 +- wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB);
2887 +-
2888 +- /*
2889 +- * For non-nested:
2890 +- * When it's written (to non-zero) for the first time, pass
2891 +- * it through.
2892 +- *
2893 +- * For nested:
2894 +- * The handling of the MSR bitmap for L2 guests is done in
2895 +- * nested_vmx_prepare_msr_bitmap. We should not touch the
2896 +- * vmcs02.msr_bitmap here since it gets completely overwritten
2897 +- * in the merging.
2898 +- */
2899 +- vmx_disable_intercept_for_msr(vmx->vmcs01.msr_bitmap, MSR_IA32_PRED_CMD,
2900 +- MSR_TYPE_W);
2901 +- break;
2902 +- case MSR_IA32_CR_PAT:
2903 +- if (!kvm_pat_valid(data))
2904 +- return 1;
2905 +-
2906 +- if (is_guest_mode(vcpu) &&
2907 +- get_vmcs12(vcpu)->vm_exit_controls & VM_EXIT_SAVE_IA32_PAT)
2908 +- get_vmcs12(vcpu)->guest_ia32_pat = data;
2909 +-
2910 +- if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT) {
2911 +- vmcs_write64(GUEST_IA32_PAT, data);
2912 +- vcpu->arch.pat = data;
2913 +- break;
2914 +- }
2915 +- ret = kvm_set_msr_common(vcpu, msr_info);
2916 +- break;
2917 +- case MSR_IA32_TSC_ADJUST:
2918 +- ret = kvm_set_msr_common(vcpu, msr_info);
2919 +- break;
2920 +- case MSR_IA32_MCG_EXT_CTL:
2921 +- if ((!msr_info->host_initiated &&
2922 +- !(to_vmx(vcpu)->msr_ia32_feature_control &
2923 +- FEATURE_CONTROL_LMCE)) ||
2924 +- (data & ~MCG_EXT_CTL_LMCE_EN))
2925 +- return 1;
2926 +- vcpu->arch.mcg_ext_ctl = data;
2927 +- break;
2928 +- case MSR_IA32_FEATURE_CONTROL:
2929 +- if (!vmx_feature_control_msr_valid(vcpu, data) ||
2930 +- (to_vmx(vcpu)->msr_ia32_feature_control &
2931 +- FEATURE_CONTROL_LOCKED && !msr_info->host_initiated))
2932 +- return 1;
2933 +- vmx->msr_ia32_feature_control = data;
2934 +- if (msr_info->host_initiated && data == 0)
2935 +- vmx_leave_nested(vcpu);
2936 +- break;
2937 +- case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
2938 +- if (!msr_info->host_initiated)
2939 +- return 1; /* they are read-only */
2940 +- if (!nested_vmx_allowed(vcpu))
2941 +- return 1;
2942 +- return vmx_set_vmx_msr(vcpu, msr_index, data);
2943 +- case MSR_IA32_RTIT_CTL:
2944 +- if ((pt_mode != PT_MODE_HOST_GUEST) ||
2945 +- vmx_rtit_ctl_check(vcpu, data) ||
2946 +- vmx->nested.vmxon)
2947 +- return 1;
2948 +- vmcs_write64(GUEST_IA32_RTIT_CTL, data);
2949 +- vmx->pt_desc.guest.ctl = data;
2950 +- pt_update_intercept_for_msr(vmx);
2951 +- break;
2952 +- case MSR_IA32_RTIT_STATUS:
2953 +- if ((pt_mode != PT_MODE_HOST_GUEST) ||
2954 +- (vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN) ||
2955 +- (data & MSR_IA32_RTIT_STATUS_MASK))
2956 +- return 1;
2957 +- vmx->pt_desc.guest.status = data;
2958 +- break;
2959 +- case MSR_IA32_RTIT_CR3_MATCH:
2960 +- if ((pt_mode != PT_MODE_HOST_GUEST) ||
2961 +- (vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN) ||
2962 +- !intel_pt_validate_cap(vmx->pt_desc.caps,
2963 +- PT_CAP_cr3_filtering))
2964 +- return 1;
2965 +- vmx->pt_desc.guest.cr3_match = data;
2966 +- break;
2967 +- case MSR_IA32_RTIT_OUTPUT_BASE:
2968 +- if ((pt_mode != PT_MODE_HOST_GUEST) ||
2969 +- (vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN) ||
2970 +- (!intel_pt_validate_cap(vmx->pt_desc.caps,
2971 +- PT_CAP_topa_output) &&
2972 +- !intel_pt_validate_cap(vmx->pt_desc.caps,
2973 +- PT_CAP_single_range_output)) ||
2974 +- (data & MSR_IA32_RTIT_OUTPUT_BASE_MASK))
2975 +- return 1;
2976 +- vmx->pt_desc.guest.output_base = data;
2977 +- break;
2978 +- case MSR_IA32_RTIT_OUTPUT_MASK:
2979 +- if ((pt_mode != PT_MODE_HOST_GUEST) ||
2980 +- (vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN) ||
2981 +- (!intel_pt_validate_cap(vmx->pt_desc.caps,
2982 +- PT_CAP_topa_output) &&
2983 +- !intel_pt_validate_cap(vmx->pt_desc.caps,
2984 +- PT_CAP_single_range_output)))
2985 +- return 1;
2986 +- vmx->pt_desc.guest.output_mask = data;
2987 +- break;
2988 +- case MSR_IA32_RTIT_ADDR0_A ... MSR_IA32_RTIT_ADDR3_B:
2989 +- index = msr_info->index - MSR_IA32_RTIT_ADDR0_A;
2990 +- if ((pt_mode != PT_MODE_HOST_GUEST) ||
2991 +- (vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN) ||
2992 +- (index >= 2 * intel_pt_validate_cap(vmx->pt_desc.caps,
2993 +- PT_CAP_num_address_ranges)))
2994 +- return 1;
2995 +- if (is_noncanonical_address(data, vcpu))
2996 +- return 1;
2997 +- if (index % 2)
2998 +- vmx->pt_desc.guest.addr_b[index / 2] = data;
2999 +- else
3000 +- vmx->pt_desc.guest.addr_a[index / 2] = data;
3001 +- break;
3002 +- case MSR_TSC_AUX:
3003 +- if (!msr_info->host_initiated &&
3004 +- !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP))
3005 +- return 1;
3006 +- /* Check reserved bit, higher 32 bits should be zero */
3007 +- if ((data >> 32) != 0)
3008 +- return 1;
3009 +- goto find_shared_msr;
3010 +-
3011 +- default:
3012 +- find_shared_msr:
3013 +- msr = find_msr_entry(vmx, msr_index);
3014 +- if (msr)
3015 +- ret = vmx_set_guest_msr(vmx, msr, data);
3016 +- else
3017 +- ret = kvm_set_msr_common(vcpu, msr_info);
3018 +- }
3019 +-
3020 +- return ret;
3021 +-}
3022 +-
3023 +-static void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
3024 +-{
3025 +- kvm_register_mark_available(vcpu, reg);
3026 +-
3027 +- switch (reg) {
3028 +- case VCPU_REGS_RSP:
3029 +- vcpu->arch.regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP);
3030 +- break;
3031 +- case VCPU_REGS_RIP:
3032 +- vcpu->arch.regs[VCPU_REGS_RIP] = vmcs_readl(GUEST_RIP);
3033 +- break;
3034 +- case VCPU_EXREG_PDPTR:
3035 +- if (enable_ept)
3036 +- ept_save_pdptrs(vcpu);
3037 +- break;
3038 +- case VCPU_EXREG_CR3:
3039 +- if (enable_unrestricted_guest || (enable_ept && is_paging(vcpu)))
3040 +- vcpu->arch.cr3 = vmcs_readl(GUEST_CR3);
3041 +- break;
3042 +- default:
3043 +- WARN_ON_ONCE(1);
3044 +- break;
3045 +- }
3046 +-}
3047 +-
3048 +-static __init int cpu_has_kvm_support(void)
3049 +-{
3050 +- return cpu_has_vmx();
3051 +-}
3052 +-
3053 +-static __init int vmx_disabled_by_bios(void)
3054 +-{
3055 +- u64 msr;
3056 +-
3057 +- rdmsrl(MSR_IA32_FEATURE_CONTROL, msr);
3058 +- if (msr & FEATURE_CONTROL_LOCKED) {
3059 +- /* launched w/ TXT and VMX disabled */
3060 +- if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
3061 +- && tboot_enabled())
3062 +- return 1;
3063 +- /* launched w/o TXT and VMX only enabled w/ TXT */
3064 +- if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
3065 +- && (msr & FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX)
3066 +- && !tboot_enabled()) {
3067 +- printk(KERN_WARNING "kvm: disable TXT in the BIOS or "
3068 +- "activate TXT before enabling KVM\n");
3069 +- return 1;
3070 +- }
3071 +- /* launched w/o TXT and VMX disabled */
3072 +- if (!(msr & FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX)
3073 +- && !tboot_enabled())
3074 +- return 1;
3075 +- }
3076 +-
3077 +- return 0;
3078 +-}
3079 +-
3080 +-static void kvm_cpu_vmxon(u64 addr)
3081 +-{
3082 +- cr4_set_bits(X86_CR4_VMXE);
3083 +- intel_pt_handle_vmx(1);
3084 +-
3085 +- asm volatile ("vmxon %0" : : "m"(addr));
3086 +-}
3087 +-
3088 +-static int hardware_enable(void)
3089 +-{
3090 +- int cpu = raw_smp_processor_id();
3091 +- u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
3092 +- u64 old, test_bits;
3093 +-
3094 +- if (cr4_read_shadow() & X86_CR4_VMXE)
3095 +- return -EBUSY;
3096 +-
3097 +- /*
3098 +- * This can happen if we hot-added a CPU but failed to allocate
3099 +- * VP assist page for it.
3100 +- */
3101 +- if (static_branch_unlikely(&enable_evmcs) &&
3102 +- !hv_get_vp_assist_page(cpu))
3103 +- return -EFAULT;
3104 +-
3105 +- INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
3106 +- INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
3107 +- spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
3108 +-
3109 +- /*
3110 +- * Now we can enable the vmclear operation in kdump
3111 +- * since the loaded_vmcss_on_cpu list on this cpu
3112 +- * has been initialized.
3113 +- *
3114 +- * Though the cpu is not in VMX operation now, there
3115 +- * is no problem to enable the vmclear operation
3116 +- * for the loaded_vmcss_on_cpu list is empty!
3117 +- */
3118 +- crash_enable_local_vmclear(cpu);
3119 +-
3120 +- rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
3121 +-
3122 +- test_bits = FEATURE_CONTROL_LOCKED;
3123 +- test_bits |= FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
3124 +- if (tboot_enabled())
3125 +- test_bits |= FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX;
3126 +-
3127 +- if ((old & test_bits) != test_bits) {
3128 +- /* enable and lock */
3129 +- wrmsrl(MSR_IA32_FEATURE_CONTROL, old | test_bits);
3130 +- }
3131 +- kvm_cpu_vmxon(phys_addr);
3132 +- if (enable_ept)
3133 +- ept_sync_global();
3134 +-
3135 +- return 0;
3136 +-}
3137 +-
3138 +-static void vmclear_local_loaded_vmcss(void)
3139 +-{
3140 +- int cpu = raw_smp_processor_id();
3141 +- struct loaded_vmcs *v, *n;
3142 +-
3143 +- list_for_each_entry_safe(v, n, &per_cpu(loaded_vmcss_on_cpu, cpu),
3144 +- loaded_vmcss_on_cpu_link)
3145 +- __loaded_vmcs_clear(v);
3146 +-}
3147 +-
3148 +-
3149 +-/* Just like cpu_vmxoff(), but with the __kvm_handle_fault_on_reboot()
3150 +- * tricks.
3151 +- */
3152 +-static void kvm_cpu_vmxoff(void)
3153 +-{
3154 +- asm volatile (__ex("vmxoff"));
3155 +-
3156 +- intel_pt_handle_vmx(0);
3157 +- cr4_clear_bits(X86_CR4_VMXE);
3158 +-}
3159 +-
3160 +-static void hardware_disable(void)
3161 +-{
3162 +- vmclear_local_loaded_vmcss();
3163 +- kvm_cpu_vmxoff();
3164 +-}
3165 +-
3166 +-static __init int adjust_vmx_controls(u32 ctl_min, u32 ctl_opt,
3167 +- u32 msr, u32 *result)
3168 +-{
3169 +- u32 vmx_msr_low, vmx_msr_high;
3170 +- u32 ctl = ctl_min | ctl_opt;
3171 +-
3172 +- rdmsr(msr, vmx_msr_low, vmx_msr_high);
3173 +-
3174 +- ctl &= vmx_msr_high; /* bit == 0 in high word ==> must be zero */
3175 +- ctl |= vmx_msr_low; /* bit == 1 in low word ==> must be one */
3176 +-
3177 +- /* Ensure minimum (required) set of control bits are supported. */
3178 +- if (ctl_min & ~ctl)
3179 +- return -EIO;
3180 +-
3181 +- *result = ctl;
3182 +- return 0;
3183 +-}
3184 +-
3185 +-static __init int setup_vmcs_config(struct vmcs_config *vmcs_conf,
3186 +- struct vmx_capability *vmx_cap)
3187 +-{
3188 +- u32 vmx_msr_low, vmx_msr_high;
3189 +- u32 min, opt, min2, opt2;
3190 +- u32 _pin_based_exec_control = 0;
3191 +- u32 _cpu_based_exec_control = 0;
3192 +- u32 _cpu_based_2nd_exec_control = 0;
3193 +- u32 _vmexit_control = 0;
3194 +- u32 _vmentry_control = 0;
3195 +-
3196 +- memset(vmcs_conf, 0, sizeof(*vmcs_conf));
3197 +- min = CPU_BASED_HLT_EXITING |
3198 +-#ifdef CONFIG_X86_64
3199 +- CPU_BASED_CR8_LOAD_EXITING |
3200 +- CPU_BASED_CR8_STORE_EXITING |
3201 +-#endif
3202 +- CPU_BASED_CR3_LOAD_EXITING |
3203 +- CPU_BASED_CR3_STORE_EXITING |
3204 +- CPU_BASED_UNCOND_IO_EXITING |
3205 +- CPU_BASED_MOV_DR_EXITING |
3206 +- CPU_BASED_USE_TSC_OFFSETTING |
3207 +- CPU_BASED_MWAIT_EXITING |
3208 +- CPU_BASED_MONITOR_EXITING |
3209 +- CPU_BASED_INVLPG_EXITING |
3210 +- CPU_BASED_RDPMC_EXITING;
3211 +-
3212 +- opt = CPU_BASED_TPR_SHADOW |
3213 +- CPU_BASED_USE_MSR_BITMAPS |
3214 +- CPU_BASED_ACTIVATE_SECONDARY_CONTROLS;
3215 +- if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PROCBASED_CTLS,
3216 +- &_cpu_based_exec_control) < 0)
3217 +- return -EIO;
3218 +-#ifdef CONFIG_X86_64
3219 +- if ((_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3220 +- _cpu_based_exec_control &= ~CPU_BASED_CR8_LOAD_EXITING &
3221 +- ~CPU_BASED_CR8_STORE_EXITING;
3222 +-#endif
3223 +- if (_cpu_based_exec_control & CPU_BASED_ACTIVATE_SECONDARY_CONTROLS) {
3224 +- min2 = 0;
3225 +- opt2 = SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
3226 +- SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
3227 +- SECONDARY_EXEC_WBINVD_EXITING |
3228 +- SECONDARY_EXEC_ENABLE_VPID |
3229 +- SECONDARY_EXEC_ENABLE_EPT |
3230 +- SECONDARY_EXEC_UNRESTRICTED_GUEST |
3231 +- SECONDARY_EXEC_PAUSE_LOOP_EXITING |
3232 +- SECONDARY_EXEC_DESC |
3233 +- SECONDARY_EXEC_RDTSCP |
3234 +- SECONDARY_EXEC_ENABLE_INVPCID |
3235 +- SECONDARY_EXEC_APIC_REGISTER_VIRT |
3236 +- SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY |
3237 +- SECONDARY_EXEC_SHADOW_VMCS |
3238 +- SECONDARY_EXEC_XSAVES |
3239 +- SECONDARY_EXEC_RDSEED_EXITING |
3240 +- SECONDARY_EXEC_RDRAND_EXITING |
3241 +- SECONDARY_EXEC_ENABLE_PML |
3242 +- SECONDARY_EXEC_TSC_SCALING |
3243 +- SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE |
3244 +- SECONDARY_EXEC_PT_USE_GPA |
3245 +- SECONDARY_EXEC_PT_CONCEAL_VMX |
3246 +- SECONDARY_EXEC_ENABLE_VMFUNC |
3247 +- SECONDARY_EXEC_ENCLS_EXITING;
3248 +- if (adjust_vmx_controls(min2, opt2,
3249 +- MSR_IA32_VMX_PROCBASED_CTLS2,
3250 +- &_cpu_based_2nd_exec_control) < 0)
3251 +- return -EIO;
3252 +- }
3253 +-#ifndef CONFIG_X86_64
3254 +- if (!(_cpu_based_2nd_exec_control &
3255 +- SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES))
3256 +- _cpu_based_exec_control &= ~CPU_BASED_TPR_SHADOW;
3257 +-#endif
3258 +-
3259 +- if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
3260 +- _cpu_based_2nd_exec_control &= ~(
3261 +- SECONDARY_EXEC_APIC_REGISTER_VIRT |
3262 +- SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
3263 +- SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
3264 +-
3265 +- rdmsr_safe(MSR_IA32_VMX_EPT_VPID_CAP,
3266 +- &vmx_cap->ept, &vmx_cap->vpid);
3267 +-
3268 +- if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
3269 +- /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
3270 +- enabled */
3271 +- _cpu_based_exec_control &= ~(CPU_BASED_CR3_LOAD_EXITING |
3272 +- CPU_BASED_CR3_STORE_EXITING |
3273 +- CPU_BASED_INVLPG_EXITING);
3274 +- } else if (vmx_cap->ept) {
3275 +- vmx_cap->ept = 0;
3276 +- pr_warn_once("EPT CAP should not exist if not support "
3277 +- "1-setting enable EPT VM-execution control\n");
3278 +- }
3279 +- if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
3280 +- vmx_cap->vpid) {
3281 +- vmx_cap->vpid = 0;
3282 +- pr_warn_once("VPID CAP should not exist if not support "
3283 +- "1-setting enable VPID VM-execution control\n");
3284 +- }
3285 +-
3286 +- min = VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACK_INTR_ON_EXIT;
3287 +-#ifdef CONFIG_X86_64
3288 +- min |= VM_EXIT_HOST_ADDR_SPACE_SIZE;
3289 +-#endif
3290 +- opt = VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL |
3291 +- VM_EXIT_LOAD_IA32_PAT |
3292 +- VM_EXIT_LOAD_IA32_EFER |
3293 +- VM_EXIT_CLEAR_BNDCFGS |
3294 +- VM_EXIT_PT_CONCEAL_PIP |
3295 +- VM_EXIT_CLEAR_IA32_RTIT_CTL;
3296 +- if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_EXIT_CTLS,
3297 +- &_vmexit_control) < 0)
3298 +- return -EIO;
3299 +-
3300 +- min = PIN_BASED_EXT_INTR_MASK | PIN_BASED_NMI_EXITING;
3301 +- opt = PIN_BASED_VIRTUAL_NMIS | PIN_BASED_POSTED_INTR |
3302 +- PIN_BASED_VMX_PREEMPTION_TIMER;
3303 +- if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_PINBASED_CTLS,
3304 +- &_pin_based_exec_control) < 0)
3305 +- return -EIO;
3306 +-
3307 +- if (cpu_has_broken_vmx_preemption_timer())
3308 +- _pin_based_exec_control &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
3309 +- if (!(_cpu_based_2nd_exec_control &
3310 +- SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY))
3311 +- _pin_based_exec_control &= ~PIN_BASED_POSTED_INTR;
3312 +-
3313 +- min = VM_ENTRY_LOAD_DEBUG_CONTROLS;
3314 +- opt = VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL |
3315 +- VM_ENTRY_LOAD_IA32_PAT |
3316 +- VM_ENTRY_LOAD_IA32_EFER |
3317 +- VM_ENTRY_LOAD_BNDCFGS |
3318 +- VM_ENTRY_PT_CONCEAL_PIP |
3319 +- VM_ENTRY_LOAD_IA32_RTIT_CTL;
3320 +- if (adjust_vmx_controls(min, opt, MSR_IA32_VMX_ENTRY_CTLS,
3321 +- &_vmentry_control) < 0)
3322 +- return -EIO;
3323 +-
3324 +- /*
3325 +- * Some cpus support VM_{ENTRY,EXIT}_IA32_PERF_GLOBAL_CTRL but they
3326 +- * can't be used due to an errata where VM Exit may incorrectly clear
3327 +- * IA32_PERF_GLOBAL_CTRL[34:32]. Workaround the errata by using the
3328 +- * MSR load mechanism to switch IA32_PERF_GLOBAL_CTRL.
3329 +- */
3330 +- if (boot_cpu_data.x86 == 0x6) {
3331 +- switch (boot_cpu_data.x86_model) {
3332 +- case 26: /* AAK155 */
3333 +- case 30: /* AAP115 */
3334 +- case 37: /* AAT100 */
3335 +- case 44: /* BC86,AAY89,BD102 */
3336 +- case 46: /* BA97 */
3337 +- _vmentry_control &= ~VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL;
3338 +- _vmexit_control &= ~VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL;
3339 +- pr_warn_once("kvm: VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL "
3340 +- "does not work properly. Using workaround\n");
3341 +- break;
3342 +- default:
3343 +- break;
3344 +- }
3345 +- }
3346 +-
3347 +-
3348 +- rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high);
3349 +-
3350 +- /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */
3351 +- if ((vmx_msr_high & 0x1fff) > PAGE_SIZE)
3352 +- return -EIO;
3353 +-
3354 +-#ifdef CONFIG_X86_64
3355 +- /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */
3356 +- if (vmx_msr_high & (1u<<16))
3357 +- return -EIO;
3358 +-#endif
3359 +-
3360 +- /* Require Write-Back (WB) memory type for VMCS accesses. */
3361 +- if (((vmx_msr_high >> 18) & 15) != 6)
3362 +- return -EIO;
3363 +-
3364 +- vmcs_conf->size = vmx_msr_high & 0x1fff;
3365 +- vmcs_conf->order = get_order(vmcs_conf->size);
3366 +- vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff;
3367 +-
3368 +- vmcs_conf->revision_id = vmx_msr_low;
3369 +-
3370 +- vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control;
3371 +- vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;
3372 +- vmcs_conf->cpu_based_2nd_exec_ctrl = _cpu_based_2nd_exec_control;
3373 +- vmcs_conf->vmexit_ctrl = _vmexit_control;
3374 +- vmcs_conf->vmentry_ctrl = _vmentry_control;
3375 +-
3376 +- if (static_branch_unlikely(&enable_evmcs))
3377 +- evmcs_sanitize_exec_ctrls(vmcs_conf);
3378 +-
3379 +- return 0;
3380 +-}
3381 +-
3382 +-struct vmcs *alloc_vmcs_cpu(bool shadow, int cpu, gfp_t flags)
3383 +-{
3384 +- int node = cpu_to_node(cpu);
3385 +- struct page *pages;
3386 +- struct vmcs *vmcs;
3387 +-
3388 +- pages = __alloc_pages_node(node, flags, vmcs_config.order);
3389 +- if (!pages)
3390 +- return NULL;
3391 +- vmcs = page_address(pages);
3392 +- memset(vmcs, 0, vmcs_config.size);
3393 +-
3394 +- /* KVM supports Enlightened VMCS v1 only */
3395 +- if (static_branch_unlikely(&enable_evmcs))
3396 +- vmcs->hdr.revision_id = KVM_EVMCS_VERSION;
3397 +- else
3398 +- vmcs->hdr.revision_id = vmcs_config.revision_id;
3399 +-
3400 +- if (shadow)
3401 +- vmcs->hdr.shadow_vmcs = 1;
3402 +- return vmcs;
3403 +-}
3404 +-
3405 +-void free_vmcs(struct vmcs *vmcs)
3406 +-{
3407 +- free_pages((unsigned long)vmcs, vmcs_config.order);
3408 +-}
3409 +-
3410 +-/*
3411 +- * Free a VMCS, but before that VMCLEAR it on the CPU where it was last loaded
3412 +- */
3413 +-void free_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
3414 +-{
3415 +- if (!loaded_vmcs->vmcs)
3416 +- return;
3417 +- loaded_vmcs_clear(loaded_vmcs);
3418 +- free_vmcs(loaded_vmcs->vmcs);
3419 +- loaded_vmcs->vmcs = NULL;
3420 +- if (loaded_vmcs->msr_bitmap)
3421 +- free_page((unsigned long)loaded_vmcs->msr_bitmap);
3422 +- WARN_ON(loaded_vmcs->shadow_vmcs != NULL);
3423 +-}
3424 +-
3425 +-int alloc_loaded_vmcs(struct loaded_vmcs *loaded_vmcs)
3426 +-{
3427 +- loaded_vmcs->vmcs = alloc_vmcs(false);
3428 +- if (!loaded_vmcs->vmcs)
3429 +- return -ENOMEM;
3430 +-
3431 +- loaded_vmcs->shadow_vmcs = NULL;
3432 +- loaded_vmcs->hv_timer_soft_disabled = false;
3433 +- loaded_vmcs_init(loaded_vmcs);
3434 +-
3435 +- if (cpu_has_vmx_msr_bitmap()) {
3436 +- loaded_vmcs->msr_bitmap = (unsigned long *)
3437 +- __get_free_page(GFP_KERNEL_ACCOUNT);
3438 +- if (!loaded_vmcs->msr_bitmap)
3439 +- goto out_vmcs;
3440 +- memset(loaded_vmcs->msr_bitmap, 0xff, PAGE_SIZE);
3441 +-
3442 +- if (IS_ENABLED(CONFIG_HYPERV) &&
3443 +- static_branch_unlikely(&enable_evmcs) &&
3444 +- (ms_hyperv.nested_features & HV_X64_NESTED_MSR_BITMAP)) {
3445 +- struct hv_enlightened_vmcs *evmcs =
3446 +- (struct hv_enlightened_vmcs *)loaded_vmcs->vmcs;
3447 +-
3448 +- evmcs->hv_enlightenments_control.msr_bitmap = 1;
3449 +- }
3450 +- }
3451 +-
3452 +- memset(&loaded_vmcs->host_state, 0, sizeof(struct vmcs_host_state));
3453 +- memset(&loaded_vmcs->controls_shadow, 0,
3454 +- sizeof(struct vmcs_controls_shadow));
3455 +-
3456 +- return 0;
3457 +-
3458 +-out_vmcs:
3459 +- free_loaded_vmcs(loaded_vmcs);
3460 +- return -ENOMEM;
3461 +-}
3462 +-
3463 +-static void free_kvm_area(void)
3464 +-{
3465 +- int cpu;
3466 +-
3467 +- for_each_possible_cpu(cpu) {
3468 +- free_vmcs(per_cpu(vmxarea, cpu));
3469 +- per_cpu(vmxarea, cpu) = NULL;
3470 +- }
3471 +-}
3472 +-
3473 +-static __init int alloc_kvm_area(void)
3474 +-{
3475 +- int cpu;
3476 +-
3477 +- for_each_possible_cpu(cpu) {
3478 +- struct vmcs *vmcs;
3479 +-
3480 +- vmcs = alloc_vmcs_cpu(false, cpu, GFP_KERNEL);
3481 +- if (!vmcs) {
3482 +- free_kvm_area();
3483 +- return -ENOMEM;
3484 +- }
3485 +-
3486 +- /*
3487 +- * When eVMCS is enabled, alloc_vmcs_cpu() sets
3488 +- * vmcs->revision_id to KVM_EVMCS_VERSION instead of
3489 +- * revision_id reported by MSR_IA32_VMX_BASIC.
3490 +- *
3491 +- * However, even though not explicitly documented by
3492 +- * TLFS, VMXArea passed as VMXON argument should
3493 +- * still be marked with revision_id reported by
3494 +- * physical CPU.
3495 +- */
3496 +- if (static_branch_unlikely(&enable_evmcs))
3497 +- vmcs->hdr.revision_id = vmcs_config.revision_id;
3498 +-
3499 +- per_cpu(vmxarea, cpu) = vmcs;
3500 +- }
3501 +- return 0;
3502 +-}
3503 +-
3504 +-static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg,
3505 +- struct kvm_segment *save)
3506 +-{
3507 +- if (!emulate_invalid_guest_state) {
3508 +- /*
3509 +- * CS and SS RPL should be equal during guest entry according
3510 +- * to VMX spec, but in reality it is not always so. Since vcpu
3511 +- * is in the middle of the transition from real mode to
3512 +- * protected mode it is safe to assume that RPL 0 is a good
3513 +- * default value.
3514 +- */
3515 +- if (seg == VCPU_SREG_CS || seg == VCPU_SREG_SS)
3516 +- save->selector &= ~SEGMENT_RPL_MASK;
3517 +- save->dpl = save->selector & SEGMENT_RPL_MASK;
3518 +- save->s = 1;
3519 +- }
3520 +- vmx_set_segment(vcpu, save, seg);
3521 +-}
3522 +-
3523 +-static void enter_pmode(struct kvm_vcpu *vcpu)
3524 +-{
3525 +- unsigned long flags;
3526 +- struct vcpu_vmx *vmx = to_vmx(vcpu);
3527 +-
3528 +- /*
3529 +- * Update real mode segment cache. It may be not up-to-date if sement
3530 +- * register was written while vcpu was in a guest mode.
3531 +- */
3532 +- vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
3533 +- vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
3534 +- vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
3535 +- vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
3536 +- vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
3537 +- vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
3538 +-
3539 +- vmx->rmode.vm86_active = 0;
3540 +-
3541 +- vmx_segment_cache_clear(vmx);
3542 +-
3543 +- vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
3544 +-
3545 +- flags = vmcs_readl(GUEST_RFLAGS);
3546 +- flags &= RMODE_GUEST_OWNED_EFLAGS_BITS;
3547 +- flags |= vmx->rmode.save_rflags & ~RMODE_GUEST_OWNED_EFLAGS_BITS;
3548 +- vmcs_writel(GUEST_RFLAGS, flags);
3549 +-
3550 +- vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~X86_CR4_VME) |
3551 +- (vmcs_readl(CR4_READ_SHADOW) & X86_CR4_VME));
3552 +-
3553 +- update_exception_bitmap(vcpu);
3554 +-
3555 +- fix_pmode_seg(vcpu, VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
3556 +- fix_pmode_seg(vcpu, VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
3557 +- fix_pmode_seg(vcpu, VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
3558 +- fix_pmode_seg(vcpu, VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
3559 +- fix_pmode_seg(vcpu, VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
3560 +- fix_pmode_seg(vcpu, VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
3561 +-}
3562 +-
3563 +-static void fix_rmode_seg(int seg, struct kvm_segment *save)
3564 +-{
3565 +- const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
3566 +- struct kvm_segment var = *save;
3567 +-
3568 +- var.dpl = 0x3;
3569 +- if (seg == VCPU_SREG_CS)
3570 +- var.type = 0x3;
3571 +-
3572 +- if (!emulate_invalid_guest_state) {
3573 +- var.selector = var.base >> 4;
3574 +- var.base = var.base & 0xffff0;
3575 +- var.limit = 0xffff;
3576 +- var.g = 0;
3577 +- var.db = 0;
3578 +- var.present = 1;
3579 +- var.s = 1;
3580 +- var.l = 0;
3581 +- var.unusable = 0;
3582 +- var.type = 0x3;
3583 +- var.avl = 0;
3584 +- if (save->base & 0xf)
3585 +- printk_once(KERN_WARNING "kvm: segment base is not "
3586 +- "paragraph aligned when entering "
3587 +- "protected mode (seg=%d)", seg);
3588 +- }
3589 +-
3590 +- vmcs_write16(sf->selector, var.selector);
3591 +- vmcs_writel(sf->base, var.base);
3592 +- vmcs_write32(sf->limit, var.limit);
3593 +- vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(&var));
3594 +-}
3595 +-
3596 +-static void enter_rmode(struct kvm_vcpu *vcpu)
3597 +-{
3598 +- unsigned long flags;
3599 +- struct vcpu_vmx *vmx = to_vmx(vcpu);
3600 +- struct kvm_vmx *kvm_vmx = to_kvm_vmx(vcpu->kvm);
3601 +-
3602 +- vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR);
3603 +- vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_ES], VCPU_SREG_ES);
3604 +- vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_DS], VCPU_SREG_DS);
3605 +- vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_FS], VCPU_SREG_FS);
3606 +- vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_GS], VCPU_SREG_GS);
3607 +- vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_SS], VCPU_SREG_SS);
3608 +- vmx_get_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_CS], VCPU_SREG_CS);
3609 +-
3610 +- vmx->rmode.vm86_active = 1;
3611 +-
3612 +- /*
3613 +- * Very old userspace does not call KVM_SET_TSS_ADDR before entering
3614 +- * vcpu. Warn the user that an update is overdue.
3615 +- */
3616 +- if (!kvm_vmx->tss_addr)
3617 +- printk_once(KERN_WARNING "kvm: KVM_SET_TSS_ADDR need to be "
3618 +- "called before entering vcpu\n");
3619 +-
3620 +- vmx_segment_cache_clear(vmx);
3621 +-
3622 +- vmcs_writel(GUEST_TR_BASE, kvm_vmx->tss_addr);
3623 +- vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1);
3624 +- vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
3625 +-
3626 +- flags = vmcs_readl(GUEST_RFLAGS);
3627 +- vmx->rmode.save_rflags = flags;
3628 +-
3629 +- flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
3630 +-
3631 +- vmcs_writel(GUEST_RFLAGS, flags);
3632 +- vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
3633 +- update_exception_bitmap(vcpu);
3634 +-
3635 +- fix_rmode_seg(VCPU_SREG_SS, &vmx->rmode.segs[VCPU_SREG_SS]);
3636 +- fix_rmode_seg(VCPU_SREG_CS, &vmx->rmode.segs[VCPU_SREG_CS]);
3637 +- fix_rmode_seg(VCPU_SREG_ES, &vmx->rmode.segs[VCPU_SREG_ES]);
3638 +- fix_rmode_seg(VCPU_SREG_DS, &vmx->rmode.segs[VCPU_SREG_DS]);
3639 +- fix_rmode_seg(VCPU_SREG_GS, &vmx->rmode.segs[VCPU_SREG_GS]);
3640 +- fix_rmode_seg(VCPU_SREG_FS, &vmx->rmode.segs[VCPU_SREG_FS]);
3641 +-
3642 +- kvm_mmu_reset_context(vcpu);
3643 +-}
3644 +-
3645 +-void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer)
3646 +-{
3647 +- struct vcpu_vmx *vmx = to_vmx(vcpu);
3648 +- struct shared_msr_entry *msr = find_msr_entry(vmx, MSR_EFER);
3649 +-
3650 +- if (!msr)
3651 +- return;
3652 +-
3653 +- vcpu->arch.efer = efer;
3654 +- if (efer & EFER_LMA) {
3655 +- vm_entry_controls_setbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
3656 +- msr->data = efer;
3657 +- } else {
3658 +- vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
3659 +-
3660 +- msr->data = efer & ~EFER_LME;
3661 +- }
3662 +- setup_msrs(vmx);
3663 +-}
3664 +-
3665 +-#ifdef CONFIG_X86_64
3666 +-
3667 +-static void enter_lmode(struct kvm_vcpu *vcpu)
3668 +-{
3669 +- u32 guest_tr_ar;
3670 +-
3671 +- vmx_segment_cache_clear(to_vmx(vcpu));
3672 +-
3673 +- guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES);
3674 +- if ((guest_tr_ar & VMX_AR_TYPE_MASK) != VMX_AR_TYPE_BUSY_64_TSS) {
3675 +- pr_debug_ratelimited("%s: tss fixup for long mode. \n",
3676 +- __func__);
3677 +- vmcs_write32(GUEST_TR_AR_BYTES,
3678 +- (guest_tr_ar & ~VMX_AR_TYPE_MASK)
3679 +- | VMX_AR_TYPE_BUSY_64_TSS);
3680 +- }
3681 +- vmx_set_efer(vcpu, vcpu->arch.efer | EFER_LMA);
3682 +-}
3683 +-
3684 +-static void exit_lmode(struct kvm_vcpu *vcpu)
3685 +-{
3686 +- vm_entry_controls_clearbit(to_vmx(vcpu), VM_ENTRY_IA32E_MODE);
3687 +- vmx_set_efer(vcpu, vcpu->arch.efer & ~EFER_LMA);
3688 +-}
3689 +-
3690 +-#endif
3691 +-
3692 +-static void vmx_flush_tlb_gva(struct kvm_vcpu *vcpu, gva_t addr)
3693 +-{
3694 +- int vpid = to_vmx(vcpu)->vpid;
3695 +-
3696 +- if (!vpid_sync_vcpu_addr(vpid, addr))
3697 +- vpid_sync_context(vpid);
3698 +-
3699 +- /*
3700 +- * If VPIDs are not supported or enabled, then the above is a no-op.
3701 +- * But we don't really need a TLB flush in that case anyway, because
3702 +- * each VM entry/exit includes an implicit flush when VPID is 0.
3703 +- */
3704 +-}
3705 +-
3706 +-static void vmx_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
3707 +-{
3708 +- ulong cr0_guest_owned_bits = vcpu->arch.cr0_guest_owned_bits;
3709 +-
3710 +- vcpu->arch.cr0 &= ~cr0_guest_owned_bits;
3711 +- vcpu->arch.cr0 |= vmcs_readl(GUEST_CR0) & cr0_guest_owned_bits;
3712 +-}
3713 +-
3714 +-static void vmx_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
3715 +-{
3716 +- ulong cr4_guest_owned_bits = vcpu->arch.cr4_guest_owned_bits;
3717 +-
3718 +- vcpu->arch.cr4 &= ~cr4_guest_owned_bits;
3719 +- vcpu->arch.cr4 |= vmcs_readl(GUEST_CR4) & cr4_guest_owned_bits;
3720 +-}
3721 +-
3722 +-static void ept_load_pdptrs(struct kvm_vcpu *vcpu)
3723 +-{
3724 +- struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
3725 +-
3726 +- if (!kvm_register_is_dirty(vcpu, VCPU_EXREG_PDPTR))
3727 +- return;
3728 +-
3729 +- if (is_pae_paging(vcpu)) {
3730 +- vmcs_write64(GUEST_PDPTR0, mmu->pdptrs[0]);
3731 +- vmcs_write64(GUEST_PDPTR1, mmu->pdptrs[1]);
3732 +- vmcs_write64(GUEST_PDPTR2, mmu->pdptrs[2]);
3733 +- vmcs_write64(GUEST_PDPTR3, mmu->pdptrs[3]);
3734 +- }
3735 +-}
3736 +-
3737 +-void ept_save_pdptrs(struct kvm_vcpu *vcpu)
3738 +-{
3739 +- struct kvm_mmu *mmu = vcpu->arch.walk_mmu;
3740 +-
3741 +- if (is_pae_paging(vcpu)) {
3742 +- mmu->pdptrs[0] = vmcs_read64(GUEST_PDPTR0);
3743 +- mmu->pdptrs[1] = vmcs_read64(GUEST_PDPTR1);
3744 +- mmu->pdptrs[2] = vmcs_read64(GUEST_PDPTR2);
3745 +- mmu->pdptrs[3] = vmcs_read64(GUEST_PDPTR3);
3746 +- }
3747 +-
3748 +- kvm_register_mark_dirty(vcpu, VCPU_EXREG_PDPTR);
3749 +-}
3750 +-
3751 +-static void ept_update_paging_mode_cr0(unsigned long *hw_cr0,
3752 +- unsigned long cr0,
3753 +- struct kvm_vcpu *vcpu)
3754 +-{
3755 +- struct vcpu_vmx *vmx = to_vmx(vcpu);
3756 +-
3757 +- if (!kvm_register_is_available(vcpu, VCPU_EXREG_CR3))
3758 +- vmx_cache_reg(vcpu, VCPU_EXREG_CR3);
3759 +- if (!(cr0 & X86_CR0_PG)) {
3760 +- /* From paging/starting to nonpaging */
3761 +- exec_controls_setbit(vmx, CPU_BASED_CR3_LOAD_EXITING |
3762 +- CPU_BASED_CR3_STORE_EXITING);
3763 +- vcpu->arch.cr0 = cr0;
3764 +- vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
3765 +- } else if (!is_paging(vcpu)) {
3766 +- /* From nonpaging to paging */
3767 +- exec_controls_clearbit(vmx, CPU_BASED_CR3_LOAD_EXITING |
3768 +- CPU_BASED_CR3_STORE_EXITING);
3769 +- vcpu->arch.cr0 = cr0;
3770 +- vmx_set_cr4(vcpu, kvm_read_cr4(vcpu));
3771 +- }
3772 +-
3773 +- if (!(cr0 & X86_CR0_WP))
3774 +- *hw_cr0 &= ~X86_CR0_WP;
3775 +-}
3776 +-
3777 +-void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
3778 +-{
3779 +- struct vcpu_vmx *vmx = to_vmx(vcpu);
3780 +- unsigned long hw_cr0;
3781 +-
3782 +- hw_cr0 = (cr0 & ~KVM_VM_CR0_ALWAYS_OFF);
3783 +- if (enable_unrestricted_guest)
3784 +- hw_cr0 |= KVM_VM_CR0_ALWAYS_ON_UNRESTRICTED_GUEST;
3785 +- else {
3786 +- hw_cr0 |= KVM_VM_CR0_ALWAYS_ON;
3787 +-
3788 +- if (vmx->rmode.vm86_active && (cr0 & X86_CR0_PE))
3789 +- enter_pmode(vcpu);
3790 +-
3791 +- if (!vmx->rmode.vm86_active && !(cr0 & X86_CR0_PE))
3792 +- enter_rmode(vcpu);
3793 +- }
3794 +-
3795 +-#ifdef CONFIG_X86_64
3796 +- if (vcpu->arch.efer & EFER_LME) {
3797 +- if (!is_paging(vcpu) && (cr0 & X86_CR0_PG))
3798 +- enter_lmode(vcpu);
3799 +- if (is_paging(vcpu) && !(cr0 & X86_CR0_PG))
3800 +- exit_lmode(vcpu);
3801 +- }
3802 +-#endif
3803 +-
3804 +- if (enable_ept && !enable_unrestricted_guest)
3805 +- ept_update_paging_mode_cr0(&hw_cr0, cr0, vcpu);
3806 +-
3807 +- vmcs_writel(CR0_READ_SHADOW, cr0);
3808 +- vmcs_writel(GUEST_CR0, hw_cr0);
3809 +- vcpu->arch.cr0 = cr0;
3810 +-
3811 +- /* depends on vcpu->arch.cr0 to be set to a new value */
3812 +- vmx->emulation_required = emulation_required(vcpu);
3813 +-}
3814 +-
3815 +-static int get_ept_level(struct kvm_vcpu *vcpu)
3816 +-{
3817 +- /* Nested EPT currently only supports 4-level walks. */
3818 +- if (is_guest_mode(vcpu) && nested_cpu_has_ept(get_vmcs12(vcpu)))
3819 +- return 4;
3820 +- if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
3821 +- return 5;
3822 +- return 4;
3823 +-}
3824 +-
3825 +-u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
3826 +-{
3827 +- u64 eptp = VMX_EPTP_MT_WB;
3828 +-
3829 +- eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
3830 +-
3831 +- if (enable_ept_ad_bits &&
3832 +- (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
3833 +- eptp |= VMX_EPTP_AD_ENABLE_BIT;
3834 +- eptp |= (root_hpa & PAGE_MASK);
3835 +-
3836 +- return eptp;
3837 +-}
3838 +-
3839 +-void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
3840 +-{
3841 +- struct kvm *kvm = vcpu->kvm;
3842 +- bool update_guest_cr3 = true;
3843 +- unsigned long guest_cr3;
3844 +- u64 eptp;
3845 +-
3846 +- guest_cr3 = cr3;
3847 +- if (enable_ept) {
3848 +- eptp = construct_eptp(vcpu, cr3);
3849 +- vmcs_write64(EPT_POINTER, eptp);
3850 +-
3851 +- if (kvm_x86_ops->tlb_remote_flush) {
3852 +- spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
3853 +- to_vmx(vcpu)->ept_pointer = eptp;
3854 +- to_kvm_vmx(kvm)->ept_pointers_match
3855 +- = EPT_POINTERS_CHECK;
3856 +- spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
3857 +- }
3858 +-
3859 +- /* Loading vmcs02.GUEST_CR3 is handled by nested VM-Enter. */
3860 +- if (is_guest_mode(vcpu))
3861 +- update_guest_cr3 = false;
3862 +- else if (!enable_unrestricted_guest && !is_paging(vcpu))
3863 +- guest_cr3 = to_kvm_vmx(kvm)->ept_identity_map_addr;
3864 +- else if (test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
3865 +- guest_cr3 = vcpu->arch.cr3;
3866 +- else /* vmcs01.GUEST_CR3 is already up-to-date. */
3867 +- update_guest_cr3 = false;
3868 +- ept_load_pdptrs(vcpu);
3869 +- }
3870 +-
3871 +- if (update_guest_cr3)
3872 +- vmcs_writel(GUEST_CR3, guest_cr3);
3873 +-}
3874 +-
3875 +-int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
3876 +-{
3877 +- struct vcpu_vmx *vmx = to_vmx(vcpu);
3878 +- /*
3879 +- * Pass through host's Machine Check Enable value to hw_cr4, which
3880 +- * is in force while we are in guest mode. Do not let guests control
3881 +- * this bit, even if host CR4.MCE == 0.
3882 +- */
3883 +- unsigned long hw_cr4;
3884 +-
3885 +- hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
3886 +- if (enable_unrestricted_guest)
3887 +- hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
3888 +- else if (vmx->rmode.vm86_active)
3889 +- hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
3890 +- else
3891 +- hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
3892 +-
3893 +- if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
3894 +- if (cr4 & X86_CR4_UMIP) {
3895 +- secondary_exec_controls_setbit(vmx, SECONDARY_EXEC_DESC);
3896 +- hw_cr4 &= ~X86_CR4_UMIP;
3897 +- } else if (!is_guest_mode(vcpu) ||
3898 +- !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC)) {
3899 +- secondary_exec_controls_clearbit(vmx, SECONDARY_EXEC_DESC);
3900 +- }
3901 +- }
3902 +-
3903 +- if (cr4 & X86_CR4_VMXE) {
3904 +- /*
3905 +- * To use VMXON (and later other VMX instructions), a guest
3906 +- * must first be able to turn on cr4.VMXE (see handle_vmon()).
3907 +- * So basically the check on whether to allow nested VMX
3908 +- * is here. We operate under the default treatment of SMM,
3909 +- * so VMX cannot be enabled under SMM.
3910 +- */
3911 +- if (!nested_vmx_allowed(vcpu) || is_smm(vcpu))
3912 +- return 1;
3913 +- }
3914 +-
3915 +- if (vmx->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
3916 +- return 1;
3917 +-
3918 +- vcpu->arch.cr4 = cr4;
3919 +-
3920 +- if (!enable_unrestricted_guest) {
3921 +- if (enable_ept) {
3922 +- if (!is_paging(vcpu)) {
3923 +- hw_cr4 &= ~X86_CR4_PAE;
3924 +- hw_cr4 |= X86_CR4_PSE;
3925 +- } else if (!(cr4 & X86_CR4_PAE)) {
3926 +- hw_cr4 &= ~X86_CR4_PAE;
3927 +- }
3928 +- }
3929 +-
3930 +- /*
3931 +- * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
3932 +- * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
3933 +- * to be manually disabled when guest switches to non-paging
3934 +- * mode.
3935 +- *
3936 +- * If !enable_unrestricted_guest, the CPU is always running
3937 +- * with CR0.PG=1 and CR4 needs to be modified.
3938 +- * If enable_unrestricted_guest, the CPU automatically
3939 +- * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
3940 +- */
3941 +- if (!is_paging(vcpu))
3942 +- hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
3943 +- }
3944 +-
3945 +- vmcs_writel(CR4_READ_SHADOW, cr4);
3946 +- vmcs_writel(GUEST_CR4, hw_cr4);
3947 +- return 0;
3948 +-}
3949 +-
3950 +-void vmx_get_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg)
3951 +-{
3952 +- struct vcpu_vmx *vmx = to_vmx(vcpu);
3953 +- u32 ar;
3954 +-
3955 +- if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
3956 +- *var = vmx->rmode.segs[seg];
3957 +- if (seg == VCPU_SREG_TR
3958 +- || var->selector == vmx_read_guest_seg_selector(vmx, seg))
3959 +- return;
3960 +- var->base = vmx_read_guest_seg_base(vmx, seg);
3961 +- var->selector = vmx_read_guest_seg_selector(vmx, seg);
3962 +- return;
3963 +- }
3964 +- var->base = vmx_read_guest_seg_base(vmx, seg);
3965 +- var->limit = vmx_read_guest_seg_limit(vmx, seg);
3966 +- var->selector = vmx_read_guest_seg_selector(vmx, seg);
3967 +- ar = vmx_read_guest_seg_ar(vmx, seg);
3968 +- var->unusable = (ar >> 16) & 1;
3969 +- var->type = ar & 15;
3970 +- var->s = (ar >> 4) & 1;
3971 +- var->dpl = (ar >> 5) & 3;
3972 +- /*
3973 +- * Some userspaces do not preserve unusable property. Since usable
3974 +- * segment has to be present according to VMX spec we can use present
3975 +- * property to amend userspace bug by making unusable segment always
3976 +- * nonpresent. vmx_segment_access_rights() already marks nonpresent
3977 +- * segment as unusable.
3978 +- */
3979 +- var->present = !var->unusable;
3980 +- var->avl = (ar >> 12) & 1;
3981 +- var->l = (ar >> 13) & 1;
3982 +- var->db = (ar >> 14) & 1;
3983 +- var->g = (ar >> 15) & 1;
3984 +-}
3985 +-
3986 +-static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
3987 +-{
3988 +- struct kvm_segment s;
3989 +-
3990 +- if (to_vmx(vcpu)->rmode.vm86_active) {
3991 +- vmx_get_segment(vcpu, &s, seg);
3992 +- return s.base;
3993 +- }
3994 +- return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
3995 +-}
3996 +-
3997 +-int vmx_get_cpl(struct kvm_vcpu *vcpu)
3998 +-{
3999 +- struct vcpu_vmx *vmx = to_vmx(vcpu);
4000 +-
4001 +- if (unlikely(vmx->rmode.vm86_active))
4002 +- return 0;
4003 +- else {
4004 +- int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
4005 +- return VMX_AR_DPL(ar);
4006 +- }
4007 +-}
4008 +-
4009 +-static u32 vmx_segment_access_rights(struct kvm_segment *var)
4010 +-{
4011 +- u32 ar;
4012 +-
4013 +- if (var->unusable || !var->present)
4014 +- ar = 1 << 16;
4015 +- else {
4016 +- ar = var->type & 15;
4017 +- ar |= (var->s & 1) << 4;
4018 +- ar |= (var->dpl & 3) << 5;
4019 +- ar |= (var->present & 1) << 7;
4020 +- ar |= (var->avl & 1) << 12;
4021 +- ar |= (var->l & 1) << 13;
4022 +- ar |= (var->db & 1) << 14;
4023 +- ar |= (var->g & 1) << 15;
4024 +- }
4025 +-
4026 +- return ar;
4027 +-}
4028 +-
4029 +-void vmx_set_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg)
4030 +-{
4031 +- struct vcpu_vmx *vmx = to_vmx(vcpu);
4032 +- const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
4033 +-
4034 +- vmx_segment_cache_clear(vmx);
4035 +-
4036 +- if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
4037 +- vmx->rmode.segs[seg] = *var;
4038 +- if (seg == VCPU_SREG_TR)
4039 +- vmcs_write16(sf->selector, var->selector);
4040 +- else if (var->s)
4041 +- fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
4042 +- goto out;
4043 +- }
4044 +-
4045 +- vmcs_writel(sf->base, var->base);
4046 +- vmcs_write32(sf->limit, var->limit);
4047 +- vmcs_write16(sf->selector, var->selector);
4048 +-
4049 +- /*
4050 +- * Fix the "Accessed" bit in AR field of segment registers for older
4051 +- * qemu binaries.
4052 +- * IA32 arch specifies that at the time of processor reset the
4053 +- * "Accessed" bit in the AR field of segment registers is 1. And qemu
4054 +- * is setting it to 0 in the userland code. This causes invalid guest
4055 +- * state vmexit when "unrestricted guest" mode is turned on.
4056 +- * Fix for this setup issue in cpu_reset is being pushed in the qemu
4057 +- * tree. Newer qemu binaries with that qemu fix would not need this
4058 +- * kvm hack.
4059 +- */
4060 +- if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
4061 +- var->type |= 0x1; /* Accessed */
4062 +-
4063 +- vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
4064 +-
4065 +-out:
4066 +- vmx->emulation_required = emulation_required(vcpu);
4067 +-}
4068 +-
4069 +-static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
4070 +-{
4071 +- u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
4072 +-
4073 +- *db = (ar >> 14) & 1;
4074 +- *l = (ar >> 13) & 1;
4075 +-}
4076 +-
4077 +-static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
4078 +-{
4079 +- dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
4080 +- dt->address = vmcs_readl(GUEST_IDTR_BASE);
4081 +-}
4082 +-
4083 +-static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
4084 +-{
4085 +- vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
4086 +- vmcs_writel(GUEST_IDTR_BASE, dt->address);
4087 +-}
4088 +-
4089 +-static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
4090 +-{
4091 +- dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
4092 +- dt->address = vmcs_readl(GUEST_GDTR_BASE);
4093 +-}
4094 +-
4095 +-static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
4096 +-{
4097 +- vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
4098 +- vmcs_writel(GUEST_GDTR_BASE, dt->address);
4099 +-}
4100 +-
4101 +-static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
4102 +-{
4103 +- struct kvm_segment var;
4104 +- u32 ar;
4105 +-
4106 +- vmx_get_segment(vcpu, &var, seg);
4107 +- var.dpl = 0x3;
4108 +- if (seg == VCPU_SREG_CS)
4109 +- var.type = 0x3;
4110 +- ar = vmx_segment_access_rights(&var);
4111 +-
4112 +- if (var.base != (var.selector << 4))
4113 +- return false;
4114 +- if (var.limit != 0xffff)
4115 +- return false;
4116 +- if (ar != 0xf3)
4117 +- return false;
4118 +-
4119 +- return true;
4120 +-}
4121 +-
4122 +-static bool code_segment_valid(struct kvm_vcpu *vcpu)
4123 +-{
4124 +- struct kvm_segment cs;
4125 +- unsigned int cs_rpl;
4126 +-
4127 +- vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
4128 +- cs_rpl = cs.selector & SEGMENT_RPL_MASK;
4129 +-
4130 +- if (cs.unusable)
4131 +- return false;
4132 +- if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
4133 +- return false;
4134 +- if (!cs.s)
4135 +- return false;
4136 +- if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
4137 +- if (cs.dpl > cs_rpl)
4138 +- return false;
4139 +- } else {
4140 +- if (cs.dpl != cs_rpl)
4141 +- return false;
4142 +- }
4143 +- if (!cs.present)
4144 +- return false;
4145 +-
4146 +- /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
4147 +- return true;
4148 +-}
4149 +-
4150 +-static bool stack_segment_valid(struct kvm_vcpu *vcpu)
4151 +-{
4152 +- struct kvm_segment ss;
4153 +- unsigned int ss_rpl;
4154 +-
4155 +- vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
4156 +- ss_rpl = ss.selector & SEGMENT_RPL_MASK;
4157 +-
4158 +- if (ss.unusable)
4159 +- return true;
4160 +- if (ss.type != 3 && ss.type != 7)
4161 +- return false;
4162 +- if (!ss.s)
4163 +- return false;
4164 +- if (ss.dpl != ss_rpl) /* DPL != RPL */
4165 +- return false;
4166 +- if (!ss.present)
4167 +- return false;
4168 +-
4169 +- return true;
4170 +-}
4171 +-
4172 +-static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
4173 +-{
4174 +- struct kvm_segment var;
4175 +- unsigned int rpl;
4176 +-
4177 +- vmx_get_segment(vcpu, &var, seg);
4178 +- rpl = var.selector & SEGMENT_RPL_MASK;
4179 +-
4180 +- if (var.unusable)
4181 +- return true;
4182 +- if (!var.s)
4183 +- return false;
4184 +- if (!var.present)
4185 +- return false;
4186 +- if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
4187 +- if (var.dpl < rpl) /* DPL < RPL */
4188 +- return false;
4189 +- }
4190 +-
4191 +- /* TODO: Add other members to kvm_segment_field to allow checking for other access
4192 +- * rights flags
4193 +- */
4194 +- return true;
4195 +-}
4196 +-
4197 +-static bool tr_valid(struct kvm_vcpu *vcpu)
4198 +-{
4199 +- struct kvm_segment tr;
4200 +-
4201 +- vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
4202 +-
4203 +- if (tr.unusable)
4204 +- return false;
4205 +- if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
4206 +- return false;
4207 +- if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
4208 +- return false;
4209 +- if (!tr.present)
4210 +- return false;
4211 +-
4212 +- return true;
4213 +-}
4214 +-
4215 +-static bool ldtr_valid(struct kvm_vcpu *vcpu)
4216 +-{
4217 +- struct kvm_segment ldtr;
4218 +-
4219 +- vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
4220 +-
4221 +- if (ldtr.unusable)
4222 +- return true;
4223 +- if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
4224 +- return false;
4225 +- if (ldtr.type != 2)
4226 +- return false;
4227 +- if (!ldtr.present)
4228 +- return false;
4229 +-
4230 +- return true;
4231 +-}
4232 +-
4233 +-static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
4234 +-{
4235 +- struct kvm_segment cs, ss;
4236 +-
4237 +- vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
4238 +- vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
4239 +-
4240 +- return ((cs.selector & SEGMENT_RPL_MASK) ==
4241 +- (ss.selector & SEGMENT_RPL_MASK));
4242 +-}
4243 +-
4244 +-/*
4245 +- * Check if guest state is valid. Returns true if valid, false if
4246 +- * not.
4247 +- * We assume that registers are always usable
4248 +- */
4249 +-static bool guest_state_valid(struct kvm_vcpu *vcpu)
4250 +-{
4251 +- if (enable_unrestricted_guest)
4252 +- return true;
4253 +-
4254 +- /* real mode guest state checks */
4255 +- if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
4256 +- if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
4257 +- return false;
4258 +- if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
4259 +- return false;
4260 +- if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
4261 +- return false;
4262 +- if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
4263 +- return false;
4264 +- if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
4265 +- return false;
4266 +- if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
4267 +- return false;
4268 +- } else {
4269 +- /* protected mode guest state checks */
4270 +- if (!cs_ss_rpl_check(vcpu))
4271 +- return false;
4272 +- if (!code_segment_valid(vcpu))
4273 +- return false;
4274 +- if (!stack_segment_valid(vcpu))
4275 +- return false;
4276 +- if (!data_segment_valid(vcpu, VCPU_SREG_DS))
4277 +- return false;
4278 +- if (!data_segment_valid(vcpu, VCPU_SREG_ES))
4279 +- return false;
4280 +- if (!data_segment_valid(vcpu, VCPU_SREG_FS))
4281 +- return false;
4282 +- if (!data_segment_valid(vcpu, VCPU_SREG_GS))
4283 +- return false;
4284 +- if (!tr_valid(vcpu))
4285 +- return false;
4286 +- if (!ldtr_valid(vcpu))
4287 +- return false;
4288 +- }
4289 +- /* TODO:
4290 +- * - Add checks on RIP
4291 +- * - Add checks on RFLAGS
4292 +- */
4293 +-
4294 +- return true;
4295 +-}
4296 +-
4297 +-static int init_rmode_tss(struct kvm *kvm)
4298 +-{
4299 +- gfn_t fn;
4300 +- u16 data = 0;
4301 +- int idx, r;
4302 +-
4303 +- idx = srcu_read_lock(&kvm->srcu);
4304 +- fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
4305 +- r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4306 +- if (r < 0)
4307 +- goto out;
4308 +- data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
4309 +- r = kvm_write_guest_page(kvm, fn++, &data,
4310 +- TSS_IOPB_BASE_OFFSET, sizeof(u16));
4311 +- if (r < 0)
4312 +- goto out;
4313 +- r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
4314 +- if (r < 0)
4315 +- goto out;
4316 +- r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4317 +- if (r < 0)
4318 +- goto out;
4319 +- data = ~0;
4320 +- r = kvm_write_guest_page(kvm, fn, &data,
4321 +- RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
4322 +- sizeof(u8));
4323 +-out:
4324 +- srcu_read_unlock(&kvm->srcu, idx);
4325 +- return r;
4326 +-}
4327 +-
4328 +-static int init_rmode_identity_map(struct kvm *kvm)
4329 +-{
4330 +- struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
4331 +- int i, idx, r = 0;
4332 +- kvm_pfn_t identity_map_pfn;
4333 +- u32 tmp;
4334 +-
4335 +- /* Protect kvm_vmx->ept_identity_pagetable_done. */
4336 +- mutex_lock(&kvm->slots_lock);
4337 +-
4338 +- if (likely(kvm_vmx->ept_identity_pagetable_done))
4339 +- goto out2;
4340 +-
4341 +- if (!kvm_vmx->ept_identity_map_addr)
4342 +- kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
4343 +- identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
4344 +-
4345 +- r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
4346 +- kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
4347 +- if (r < 0)
4348 +- goto out2;
4349 +-
4350 +- idx = srcu_read_lock(&kvm->srcu);
4351 +- r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
4352 +- if (r < 0)
4353 +- goto out;
4354 +- /* Set up identity-mapping pagetable for EPT in real mode */
4355 +- for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
4356 +- tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
4357 +- _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
4358 +- r = kvm_write_guest_page(kvm, identity_map_pfn,
4359 +- &tmp, i * sizeof(tmp), sizeof(tmp));
4360 +- if (r < 0)
4361 +- goto out;
4362 +- }
4363 +- kvm_vmx->ept_identity_pagetable_done = true;
4364 +-
4365 +-out:
4366 +- srcu_read_unlock(&kvm->srcu, idx);
4367 +-
4368 +-out2:
4369 +- mutex_unlock(&kvm->slots_lock);
4370 +- return r;
4371 +-}
4372 +-
4373 +-static void seg_setup(int seg)
4374 +-{
4375 +- const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
4376 +- unsigned int ar;
4377 +-
4378 +- vmcs_write16(sf->selector, 0);
4379 +- vmcs_writel(sf->base, 0);
4380 +- vmcs_write32(sf->limit, 0xffff);
4381 +- ar = 0x93;
4382 +- if (seg == VCPU_SREG_CS)
4383 +- ar |= 0x08; /* code segment */
4384 +-
4385 +- vmcs_write32(sf->ar_bytes, ar);
4386 +-}
4387 +-
4388 +-static int alloc_apic_access_page(struct kvm *kvm)
4389 +-{
4390 +- struct page *page;
4391 +- int r = 0;
4392 +-
4393 +- mutex_lock(&kvm->slots_lock);
4394 +- if (kvm->arch.apic_access_page_done)
4395 +- goto out;
4396 +- r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
4397 +- APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
4398 +- if (r)
4399 +- goto out;
4400 +-
4401 +- page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
4402 +- if (is_error_page(page)) {
4403 +- r = -EFAULT;
4404 +- goto out;
4405 +- }
4406 +-
4407 +- /*
4408 +- * Do not pin the page in memory, so that memory hot-unplug
4409 +- * is able to migrate it.
4410 +- */
4411 +- put_page(page);
4412 +- kvm->arch.apic_access_page_done = true;
4413 +-out:
4414 +- mutex_unlock(&kvm->slots_lock);
4415 +- return r;
4416 +-}
4417 +-
4418 +-int allocate_vpid(void)
4419 +-{
4420 +- int vpid;
4421 +-
4422 +- if (!enable_vpid)
4423 +- return 0;
4424 +- spin_lock(&vmx_vpid_lock);
4425 +- vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
4426 +- if (vpid < VMX_NR_VPIDS)
4427 +- __set_bit(vpid, vmx_vpid_bitmap);
4428 +- else
4429 +- vpid = 0;
4430 +- spin_unlock(&vmx_vpid_lock);
4431 +- return vpid;
4432 +-}
4433 +-
4434 +-void free_vpid(int vpid)
4435 +-{
4436 +- if (!enable_vpid || vpid == 0)
4437 +- return;
4438 +- spin_lock(&vmx_vpid_lock);
4439 +- __clear_bit(vpid, vmx_vpid_bitmap);
4440 +- spin_unlock(&vmx_vpid_lock);
4441 +-}
4442 +-
4443 +-static __always_inline void vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
4444 +- u32 msr, int type)
4445 +-{
4446 +- int f = sizeof(unsigned long);
4447 +-
4448 +- if (!cpu_has_vmx_msr_bitmap())
4449 +- return;
4450 +-
4451 +- if (static_branch_unlikely(&enable_evmcs))
4452 +- evmcs_touch_msr_bitmap();
4453 +-
4454 +- /*
4455 +- * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4456 +- * have the write-low and read-high bitmap offsets the wrong way round.
4457 +- * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4458 +- */
4459 +- if (msr <= 0x1fff) {
4460 +- if (type & MSR_TYPE_R)
4461 +- /* read-low */
4462 +- __clear_bit(msr, msr_bitmap + 0x000 / f);
4463 +-
4464 +- if (type & MSR_TYPE_W)
4465 +- /* write-low */
4466 +- __clear_bit(msr, msr_bitmap + 0x800 / f);
4467 +-
4468 +- } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4469 +- msr &= 0x1fff;
4470 +- if (type & MSR_TYPE_R)
4471 +- /* read-high */
4472 +- __clear_bit(msr, msr_bitmap + 0x400 / f);
4473 +-
4474 +- if (type & MSR_TYPE_W)
4475 +- /* write-high */
4476 +- __clear_bit(msr, msr_bitmap + 0xc00 / f);
4477 +-
4478 +- }
4479 +-}
4480 +-
4481 +-static __always_inline void vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
4482 +- u32 msr, int type)
4483 +-{
4484 +- int f = sizeof(unsigned long);
4485 +-
4486 +- if (!cpu_has_vmx_msr_bitmap())
4487 +- return;
4488 +-
4489 +- if (static_branch_unlikely(&enable_evmcs))
4490 +- evmcs_touch_msr_bitmap();
4491 +-
4492 +- /*
4493 +- * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4494 +- * have the write-low and read-high bitmap offsets the wrong way round.
4495 +- * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4496 +- */
4497 +- if (msr <= 0x1fff) {
4498 +- if (type & MSR_TYPE_R)
4499 +- /* read-low */
4500 +- __set_bit(msr, msr_bitmap + 0x000 / f);
4501 +-
4502 +- if (type & MSR_TYPE_W)
4503 +- /* write-low */
4504 +- __set_bit(msr, msr_bitmap + 0x800 / f);
4505 +-
4506 +- } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4507 +- msr &= 0x1fff;
4508 +- if (type & MSR_TYPE_R)
4509 +- /* read-high */
4510 +- __set_bit(msr, msr_bitmap + 0x400 / f);
4511 +-
4512 +- if (type & MSR_TYPE_W)
4513 +- /* write-high */
4514 +- __set_bit(msr, msr_bitmap + 0xc00 / f);
4515 +-
4516 +- }
4517 +-}
4518 +-
4519 +-static __always_inline void vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
4520 +- u32 msr, int type, bool value)
4521 +-{
4522 +- if (value)
4523 +- vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
4524 +- else
4525 +- vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
4526 +-}
4527 +-
4528 +-static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
4529 +-{
4530 +- u8 mode = 0;
4531 +-
4532 +- if (cpu_has_secondary_exec_ctrls() &&
4533 +- (secondary_exec_controls_get(to_vmx(vcpu)) &
4534 +- SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
4535 +- mode |= MSR_BITMAP_MODE_X2APIC;
4536 +- if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
4537 +- mode |= MSR_BITMAP_MODE_X2APIC_APICV;
4538 +- }
4539 +-
4540 +- return mode;
4541 +-}
4542 +-
4543 +-static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
4544 +- u8 mode)
4545 +-{
4546 +- int msr;
4547 +-
4548 +- for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
4549 +- unsigned word = msr / BITS_PER_LONG;
4550 +- msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
4551 +- msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
4552 +- }
4553 +-
4554 +- if (mode & MSR_BITMAP_MODE_X2APIC) {
4555 +- /*
4556 +- * TPR reads and writes can be virtualized even if virtual interrupt
4557 +- * delivery is not in use.
4558 +- */
4559 +- vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
4560 +- if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
4561 +- vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
4562 +- vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
4563 +- vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
4564 +- }
4565 +- }
4566 +-}
4567 +-
4568 +-void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
4569 +-{
4570 +- struct vcpu_vmx *vmx = to_vmx(vcpu);
4571 +- unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
4572 +- u8 mode = vmx_msr_bitmap_mode(vcpu);
4573 +- u8 changed = mode ^ vmx->msr_bitmap_mode;
4574 +-
4575 +- if (!changed)
4576 +- return;
4577 +-
4578 +- if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
4579 +- vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
4580 +-
4581 +- vmx->msr_bitmap_mode = mode;
4582 +-}
4583 +-
4584 +-void pt_update_intercept_for_msr(struct vcpu_vmx *vmx)
4585 +-{
4586 +- unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
4587 +- bool flag = !(vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN);
4588 +- u32 i;
4589 +-
4590 +- vmx_set_intercept_for_msr(msr_bitmap, MSR_IA32_RTIT_STATUS,
4591 +- MSR_TYPE_RW, flag);
4592 +- vmx_set_intercept_for_msr(msr_bitmap, MSR_IA32_RTIT_OUTPUT_BASE,
4593 +- MSR_TYPE_RW, flag);
4594 +- vmx_set_intercept_for_msr(msr_bitmap, MSR_IA32_RTIT_OUTPUT_MASK,
4595 +- MSR_TYPE_RW, flag);
4596 +- vmx_set_intercept_for_msr(msr_bitmap, MSR_IA32_RTIT_CR3_MATCH,
4597 +- MSR_TYPE_RW, flag);
4598 +- for (i = 0; i < vmx->pt_desc.addr_range; i++) {
4599 +- vmx_set_intercept_for_msr(msr_bitmap,
4600 +- MSR_IA32_RTIT_ADDR0_A + i * 2, MSR_TYPE_RW, flag);
4601 +- vmx_set_intercept_for_msr(msr_bitmap,
4602 +- MSR_IA32_RTIT_ADDR0_B + i * 2, MSR_TYPE_RW, flag);
4603 +- }
4604 +-}
4605 +-
4606 +-static bool vmx_get_enable_apicv(struct kvm *kvm)
4607 +-{
4608 +- return enable_apicv;
4609 +-}
4610 +-
4611 +-static bool vmx_guest_apic_has_interrupt(struct kvm_vcpu *vcpu)
4612 +-{
4613 +- struct vcpu_vmx *vmx = to_vmx(vcpu);
4614 +- void *vapic_page;
4615 +- u32 vppr;
4616 +- int rvi;
4617 +-
4618 +- if (WARN_ON_ONCE(!is_guest_mode(vcpu)) ||
4619 +- !nested_cpu_has_vid(get_vmcs12(vcpu)) ||
4620 +- WARN_ON_ONCE(!vmx->nested.virtual_apic_map.gfn))
4621 +- return false;
4622 +-
4623 +- rvi = vmx_get_rvi();
4624 +-
4625 +- vapic_page = vmx->nested.virtual_apic_map.hva;
4626 +- vppr = *((u32 *)(vapic_page + APIC_PROCPRI));
4627 +-
4628 +- return ((rvi & 0xf0) > (vppr & 0xf0));
4629 +-}
4630 +-
4631 +-static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
4632 +- bool nested)
4633 +-{
4634 +-#ifdef CONFIG_SMP
4635 +- int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
4636 +-
4637 +- if (vcpu->mode == IN_GUEST_MODE) {
4638 +- /*
4639 +- * The vector of interrupt to be delivered to vcpu had
4640 +- * been set in PIR before this function.
4641 +- *
4642 +- * Following cases will be reached in this block, and
4643 +- * we always send a notification event in all cases as
4644 +- * explained below.
4645 +- *
4646 +- * Case 1: vcpu keeps in non-root mode. Sending a
4647 +- * notification event posts the interrupt to vcpu.
4648 +- *
4649 +- * Case 2: vcpu exits to root mode and is still
4650 +- * runnable. PIR will be synced to vIRR before the
4651 +- * next vcpu entry. Sending a notification event in
4652 +- * this case has no effect, as vcpu is not in root
4653 +- * mode.
4654 +- *
4655 +- * Case 3: vcpu exits to root mode and is blocked.
4656 +- * vcpu_block() has already synced PIR to vIRR and
4657 +- * never blocks vcpu if vIRR is not cleared. Therefore,
4658 +- * a blocked vcpu here does not wait for any requested
4659 +- * interrupts in PIR, and sending a notification event
4660 +- * which has no effect is safe here.
4661 +- */
4662 +-
4663 +- apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
4664 +- return true;
4665 +- }
4666 +-#endif
4667 +- return false;
4668 +-}
4669 +-
4670 +-static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
4671 +- int vector)
4672 +-{
4673 +- struct vcpu_vmx *vmx = to_vmx(vcpu);
4674 +-
4675 +- if (is_guest_mode(vcpu) &&
4676 +- vector == vmx->nested.posted_intr_nv) {
4677 +- /*
4678 +- * If a posted intr is not recognized by hardware,
4679 +- * we will accomplish it in the next vmentry.
4680 +- */
4681 +- vmx->nested.pi_pending = true;
4682 +- kvm_make_request(KVM_REQ_EVENT, vcpu);
4683 +- /* the PIR and ON have been set by L1. */
4684 +- if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
4685 +- kvm_vcpu_kick(vcpu);
4686 +- return 0;
4687 +- }
4688 +- return -1;
4689 +-}
4690 +-/*
4691 +- * Send interrupt to vcpu via posted interrupt way.
4692 +- * 1. If target vcpu is running(non-root mode), send posted interrupt
4693 +- * notification to vcpu and hardware will sync PIR to vIRR atomically.
4694 +- * 2. If target vcpu isn't running(root mode), kick it to pick up the
4695 +- * interrupt from PIR in next vmentry.
4696 +- */
4697 +-static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
4698 +-{
4699 +- struct vcpu_vmx *vmx = to_vmx(vcpu);
4700 +- int r;
4701 +-
4702 +- r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
4703 +- if (!r)
4704 +- return;
4705 +-
4706 +- if (pi_test_and_set_pir(vector, &vmx->pi_desc))
4707 +- return;
4708 +-
4709 +- /* If a previous notification has sent the IPI, nothing to do. */
4710 +- if (pi_test_and_set_on(&vmx->pi_desc))
4711 +- return;
4712 +-
4713 +- if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
4714 +- kvm_vcpu_kick(vcpu);
4715 +-}
4716 +-
4717 +-/*
4718 +- * Set up the vmcs's constant host-state fields, i.e., host-state fields that
4719 +- * will not change in the lifetime of the guest.
4720 +- * Note that host-state that does change is set elsewhere. E.g., host-state
4721 +- * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
4722 +- */
4723 +-void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
4724 +-{
4725 +- u32 low32, high32;
4726 +- unsigned long tmpl;
4727 +- unsigned long cr0, cr3, cr4;
4728 +-
4729 +- cr0 = read_cr0();
4730 +- WARN_ON(cr0 & X86_CR0_TS);
4731 +- vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
4732 +-
4733 +- /*
4734 +- * Save the most likely value for this task's CR3 in the VMCS.
4735 +- * We can't use __get_current_cr3_fast() because we're not atomic.
4736 +- */
4737 +- cr3 = __read_cr3();
4738 +- vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
4739 +- vmx->loaded_vmcs->host_state.cr3 = cr3;
4740 +-
4741 +- /* Save the most likely value for this task's CR4 in the VMCS. */
4742 +- cr4 = cr4_read_shadow();
4743 +- vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
4744 +- vmx->loaded_vmcs->host_state.cr4 = cr4;
4745 +-
4746 +- vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
4747 +-#ifdef CONFIG_X86_64
4748 +- /*
4749 +- * Load null selectors, so we can avoid reloading them in
4750 +- * vmx_prepare_switch_to_host(), in case userspace uses
4751 +- * the null selectors too (the expected case).
4752 +- */
4753 +- vmcs_write16(HOST_DS_SELECTOR, 0);
4754 +- vmcs_write16(HOST_ES_SELECTOR, 0);
4755 +-#else
4756 +- vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
4757 +- vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
4758 +-#endif
4759 +- vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
4760 +- vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
4761 +-
4762 +- vmcs_writel(HOST_IDTR_BASE, host_idt_base); /* 22.2.4 */
4763 +-
4764 +- vmcs_writel(HOST_RIP, (unsigned long)vmx_vmexit); /* 22.2.5 */
4765 +-
4766 +- rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
4767 +- vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
4768 +- rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
4769 +- vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
4770 +-
4771 +- if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
4772 +- rdmsr(MSR_IA32_CR_PAT, low32, high32);
4773 +- vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
4774 +- }
4775 +-
4776 +- if (cpu_has_load_ia32_efer())
4777 +- vmcs_write64(HOST_IA32_EFER, host_efer);
4778 +-}
4779 +-
4780 +-void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
4781 +-{
4782 +- vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
4783 +- if (enable_ept)
4784 +- vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
4785 +- if (is_guest_mode(&vmx->vcpu))
4786 +- vmx->vcpu.arch.cr4_guest_owned_bits &=
4787 +- ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
4788 +- vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
4789 +-}
4790 +-
4791 +-u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
4792 +-{
4793 +- u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
4794 +-
4795 +- if (!kvm_vcpu_apicv_active(&vmx->vcpu))
4796 +- pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
4797 +-
4798 +- if (!enable_vnmi)
4799 +- pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
4800 +-
4801 +- if (!enable_preemption_timer)
4802 +- pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
4803 +-
4804 +- return pin_based_exec_ctrl;
4805 +-}
4806 +-
4807 +-static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
4808 +-{
4809 +- struct vcpu_vmx *vmx = to_vmx(vcpu);
4810 +-
4811 +- pin_controls_set(vmx, vmx_pin_based_exec_ctrl(vmx));
4812 +- if (cpu_has_secondary_exec_ctrls()) {
4813 +- if (kvm_vcpu_apicv_active(vcpu))
4814 +- secondary_exec_controls_setbit(vmx,
4815 +- SECONDARY_EXEC_APIC_REGISTER_VIRT |
4816 +- SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
4817 +- else
4818 +- secondary_exec_controls_clearbit(vmx,
4819 +- SECONDARY_EXEC_APIC_REGISTER_VIRT |
4820 +- SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
4821 +- }
4822 +-
4823 +- if (cpu_has_vmx_msr_bitmap())
4824 +- vmx_update_msr_bitmap(vcpu);
4825 +-}
4826 +-
4827 +-u32 vmx_exec_control(struct vcpu_vmx *vmx)
4828 +-{
4829 +- u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
4830 +-
4831 +- if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
4832 +- exec_control &= ~CPU_BASED_MOV_DR_EXITING;
4833 +-
4834 +- if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
4835 +- exec_control &= ~CPU_BASED_TPR_SHADOW;
4836 +-#ifdef CONFIG_X86_64
4837 +- exec_control |= CPU_BASED_CR8_STORE_EXITING |
4838 +- CPU_BASED_CR8_LOAD_EXITING;
4839 +-#endif
4840 +- }
4841 +- if (!enable_ept)
4842 +- exec_control |= CPU_BASED_CR3_STORE_EXITING |
4843 +- CPU_BASED_CR3_LOAD_EXITING |
4844 +- CPU_BASED_INVLPG_EXITING;
4845 +- if (kvm_mwait_in_guest(vmx->vcpu.kvm))
4846 +- exec_control &= ~(CPU_BASED_MWAIT_EXITING |
4847 +- CPU_BASED_MONITOR_EXITING);
4848 +- if (kvm_hlt_in_guest(vmx->vcpu.kvm))
4849 +- exec_control &= ~CPU_BASED_HLT_EXITING;
4850 +- return exec_control;
4851 +-}
4852 +-
4853 +-
4854 +-static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
4855 +-{
4856 +- struct kvm_vcpu *vcpu = &vmx->vcpu;
4857 +-
4858 +- u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
4859 +-
4860 +- if (pt_mode == PT_MODE_SYSTEM)
4861 +- exec_control &= ~(SECONDARY_EXEC_PT_USE_GPA | SECONDARY_EXEC_PT_CONCEAL_VMX);
4862 +- if (!cpu_need_virtualize_apic_accesses(vcpu))
4863 +- exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
4864 +- if (vmx->vpid == 0)
4865 +- exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
4866 +- if (!enable_ept) {
4867 +- exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
4868 +- enable_unrestricted_guest = 0;
4869 +- }
4870 +- if (!enable_unrestricted_guest)
4871 +- exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
4872 +- if (kvm_pause_in_guest(vmx->vcpu.kvm))
4873 +- exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
4874 +- if (!kvm_vcpu_apicv_active(vcpu))
4875 +- exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
4876 +- SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
4877 +- exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
4878 +-
4879 +- /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
4880 +- * in vmx_set_cr4. */
4881 +- exec_control &= ~SECONDARY_EXEC_DESC;
4882 +-
4883 +- /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
4884 +- (handle_vmptrld).
4885 +- We can NOT enable shadow_vmcs here because we don't have yet
4886 +- a current VMCS12
4887 +- */
4888 +- exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
4889 +-
4890 +- if (!enable_pml)
4891 +- exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
4892 +-
4893 +- if (vmx_xsaves_supported()) {
4894 +- /* Exposing XSAVES only when XSAVE is exposed */
4895 +- bool xsaves_enabled =
4896 +- guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
4897 +- guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
4898 +-
4899 +- vcpu->arch.xsaves_enabled = xsaves_enabled;
4900 +-
4901 +- if (!xsaves_enabled)
4902 +- exec_control &= ~SECONDARY_EXEC_XSAVES;
4903 +-
4904 +- if (nested) {
4905 +- if (xsaves_enabled)
4906 +- vmx->nested.msrs.secondary_ctls_high |=
4907 +- SECONDARY_EXEC_XSAVES;
4908 +- else
4909 +- vmx->nested.msrs.secondary_ctls_high &=
4910 +- ~SECONDARY_EXEC_XSAVES;
4911 +- }
4912 +- }
4913 +-
4914 +- if (vmx_rdtscp_supported()) {
4915 +- bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
4916 +- if (!rdtscp_enabled)
4917 +- exec_control &= ~SECONDARY_EXEC_RDTSCP;
4918 +-
4919 +- if (nested) {
4920 +- if (rdtscp_enabled)
4921 +- vmx->nested.msrs.secondary_ctls_high |=
4922 +- SECONDARY_EXEC_RDTSCP;
4923 +- else
4924 +- vmx->nested.msrs.secondary_ctls_high &=
4925 +- ~SECONDARY_EXEC_RDTSCP;
4926 +- }
4927 +- }
4928 +-
4929 +- if (vmx_invpcid_supported()) {
4930 +- /* Exposing INVPCID only when PCID is exposed */
4931 +- bool invpcid_enabled =
4932 +- guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
4933 +- guest_cpuid_has(vcpu, X86_FEATURE_PCID);
4934 +-
4935 +- if (!invpcid_enabled) {
4936 +- exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
4937 +- guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
4938 +- }
4939 +-
4940 +- if (nested) {
4941 +- if (invpcid_enabled)
4942 +- vmx->nested.msrs.secondary_ctls_high |=
4943 +- SECONDARY_EXEC_ENABLE_INVPCID;
4944 +- else
4945 +- vmx->nested.msrs.secondary_ctls_high &=
4946 +- ~SECONDARY_EXEC_ENABLE_INVPCID;
4947 +- }
4948 +- }
4949 +-
4950 +- if (vmx_rdrand_supported()) {
4951 +- bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
4952 +- if (rdrand_enabled)
4953 +- exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
4954 +-
4955 +- if (nested) {
4956 +- if (rdrand_enabled)
4957 +- vmx->nested.msrs.secondary_ctls_high |=
4958 +- SECONDARY_EXEC_RDRAND_EXITING;
4959 +- else
4960 +- vmx->nested.msrs.secondary_ctls_high &=
4961 +- ~SECONDARY_EXEC_RDRAND_EXITING;
4962 +- }
4963 +- }
4964 +-
4965 +- if (vmx_rdseed_supported()) {
4966 +- bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
4967 +- if (rdseed_enabled)
4968 +- exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
4969 +-
4970 +- if (nested) {
4971 +- if (rdseed_enabled)
4972 +- vmx->nested.msrs.secondary_ctls_high |=
4973 +- SECONDARY_EXEC_RDSEED_EXITING;
4974 +- else
4975 +- vmx->nested.msrs.secondary_ctls_high &=
4976 +- ~SECONDARY_EXEC_RDSEED_EXITING;
4977 +- }
4978 +- }
4979 +-
4980 +- if (vmx_waitpkg_supported()) {
4981 +- bool waitpkg_enabled =
4982 +- guest_cpuid_has(vcpu, X86_FEATURE_WAITPKG);
4983 +-
4984 +- if (!waitpkg_enabled)
4985 +- exec_control &= ~SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE;
4986 +-
4987 +- if (nested) {
4988 +- if (waitpkg_enabled)
4989 +- vmx->nested.msrs.secondary_ctls_high |=
4990 +- SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE;
4991 +- else
4992 +- vmx->nested.msrs.secondary_ctls_high &=
4993 +- ~SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE;
4994 +- }
4995 +- }
4996 +-
4997 +- vmx->secondary_exec_control = exec_control;
4998 +-}
4999 +-
5000 +-static void ept_set_mmio_spte_mask(void)
5001 +-{
5002 +- /*
5003 +- * EPT Misconfigurations can be generated if the value of bits 2:0
5004 +- * of an EPT paging-structure entry is 110b (write/execute).
5005 +- */
5006 +- kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
5007 +- VMX_EPT_MISCONFIG_WX_VALUE, 0);
5008 +-}
5009 +-
5010 +-#define VMX_XSS_EXIT_BITMAP 0
5011 +-
5012 +-/*
5013 +- * Noting that the initialization of Guest-state Area of VMCS is in
5014 +- * vmx_vcpu_reset().
5015 +- */
5016 +-static void init_vmcs(struct vcpu_vmx *vmx)
5017 +-{
5018 +- if (nested)
5019 +- nested_vmx_set_vmcs_shadowing_bitmap();
5020 +-
5021 +- if (cpu_has_vmx_msr_bitmap())
5022 +- vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
5023 +-
5024 +- vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
5025 +-
5026 +- /* Control */
5027 +- pin_controls_set(vmx, vmx_pin_based_exec_ctrl(vmx));
5028 +-
5029 +- exec_controls_set(vmx, vmx_exec_control(vmx));
5030 +-
5031 +- if (cpu_has_secondary_exec_ctrls()) {
5032 +- vmx_compute_secondary_exec_control(vmx);
5033 +- secondary_exec_controls_set(vmx, vmx->secondary_exec_control);
5034 +- }
5035 +-
5036 +- if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
5037 +- vmcs_write64(EOI_EXIT_BITMAP0, 0);
5038 +- vmcs_write64(EOI_EXIT_BITMAP1, 0);
5039 +- vmcs_write64(EOI_EXIT_BITMAP2, 0);
5040 +- vmcs_write64(EOI_EXIT_BITMAP3, 0);
5041 +-
5042 +- vmcs_write16(GUEST_INTR_STATUS, 0);
5043 +-
5044 +- vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
5045 +- vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
5046 +- }
5047 +-
5048 +- if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
5049 +- vmcs_write32(PLE_GAP, ple_gap);
5050 +- vmx->ple_window = ple_window;
5051 +- vmx->ple_window_dirty = true;
5052 +- }
5053 +-
5054 +- vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
5055 +- vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
5056 +- vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
5057 +-
5058 +- vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
5059 +- vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
5060 +- vmx_set_constant_host_state(vmx);
5061 +- vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
5062 +- vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
5063 +-
5064 +- if (cpu_has_vmx_vmfunc())
5065 +- vmcs_write64(VM_FUNCTION_CONTROL, 0);
5066 +-
5067 +- vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
5068 +- vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
5069 +- vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
5070 +- vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
5071 +- vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
5072 +-
5073 +- if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
5074 +- vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
5075 +-
5076 +- vm_exit_controls_set(vmx, vmx_vmexit_ctrl());
5077 +-
5078 +- /* 22.2.1, 20.8.1 */
5079 +- vm_entry_controls_set(vmx, vmx_vmentry_ctrl());
5080 +-
5081 +- vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
5082 +- vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
5083 +-
5084 +- set_cr4_guest_host_mask(vmx);
5085 +-
5086 +- if (vmx->vpid != 0)
5087 +- vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
5088 +-
5089 +- if (vmx_xsaves_supported())
5090 +- vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
5091 +-
5092 +- if (enable_pml) {
5093 +- vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
5094 +- vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
5095 +- }
5096 +-
5097 +- if (cpu_has_vmx_encls_vmexit())
5098 +- vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
5099 +-
5100 +- if (pt_mode == PT_MODE_HOST_GUEST) {
5101 +- memset(&vmx->pt_desc, 0, sizeof(vmx->pt_desc));
5102 +- /* Bit[6~0] are forced to 1, writes are ignored. */
5103 +- vmx->pt_desc.guest.output_mask = 0x7F;
5104 +- vmcs_write64(GUEST_IA32_RTIT_CTL, 0);
5105 +- }
5106 +-}
5107 +-
5108 +-static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
5109 +-{
5110 +- struct vcpu_vmx *vmx = to_vmx(vcpu);
5111 +- struct msr_data apic_base_msr;
5112 +- u64 cr0;
5113 +-
5114 +- vmx->rmode.vm86_active = 0;
5115 +- vmx->spec_ctrl = 0;
5116 +-
5117 +- vmx->msr_ia32_umwait_control = 0;
5118 +-
5119 +- vcpu->arch.microcode_version = 0x100000000ULL;
5120 +- vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
5121 +- vmx->hv_deadline_tsc = -1;
5122 +- kvm_set_cr8(vcpu, 0);
5123 +-
5124 +- if (!init_event) {
5125 +- apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
5126 +- MSR_IA32_APICBASE_ENABLE;
5127 +- if (kvm_vcpu_is_reset_bsp(vcpu))
5128 +- apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
5129 +- apic_base_msr.host_initiated = true;
5130 +- kvm_set_apic_base(vcpu, &apic_base_msr);
5131 +- }
5132 +-
5133 +- vmx_segment_cache_clear(vmx);
5134 +-
5135 +- seg_setup(VCPU_SREG_CS);
5136 +- vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
5137 +- vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
5138 +-
5139 +- seg_setup(VCPU_SREG_DS);
5140 +- seg_setup(VCPU_SREG_ES);
5141 +- seg_setup(VCPU_SREG_FS);
5142 +- seg_setup(VCPU_SREG_GS);
5143 +- seg_setup(VCPU_SREG_SS);
5144 +-
5145 +- vmcs_write16(GUEST_TR_SELECTOR, 0);
5146 +- vmcs_writel(GUEST_TR_BASE, 0);
5147 +- vmcs_write32(GUEST_TR_LIMIT, 0xffff);
5148 +- vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
5149 +-
5150 +- vmcs_write16(GUEST_LDTR_SELECTOR, 0);
5151 +- vmcs_writel(GUEST_LDTR_BASE, 0);
5152 +- vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
5153 +- vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
5154 +-
5155 +- if (!init_event) {
5156 +- vmcs_write32(GUEST_SYSENTER_CS, 0);
5157 +- vmcs_writel(GUEST_SYSENTER_ESP, 0);
5158 +- vmcs_writel(GUEST_SYSENTER_EIP, 0);
5159 +- vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
5160 +- }
5161 +-
5162 +- kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
5163 +- kvm_rip_write(vcpu, 0xfff0);
5164 +-
5165 +- vmcs_writel(GUEST_GDTR_BASE, 0);
5166 +- vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
5167 +-
5168 +- vmcs_writel(GUEST_IDTR_BASE, 0);
5169 +- vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
5170 +-
5171 +- vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
5172 +- vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
5173 +- vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
5174 +- if (kvm_mpx_supported())
5175 +- vmcs_write64(GUEST_BNDCFGS, 0);
5176 +-
5177 +- setup_msrs(vmx);
5178 +-
5179 +- vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
5180 +-
5181 +- if (cpu_has_vmx_tpr_shadow() && !init_event) {
5182 +- vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
5183 +- if (cpu_need_tpr_shadow(vcpu))
5184 +- vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
5185 +- __pa(vcpu->arch.apic->regs));
5186 +- vmcs_write32(TPR_THRESHOLD, 0);
5187 +- }
5188 +-
5189 +- kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
5190 +-
5191 +- cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
5192 +- vmx->vcpu.arch.cr0 = cr0;
5193 +- vmx_set_cr0(vcpu, cr0); /* enter rmode */
5194 +- vmx_set_cr4(vcpu, 0);
5195 +- vmx_set_efer(vcpu, 0);
5196 +-
5197 +- update_exception_bitmap(vcpu);
5198 +-
5199 +- vpid_sync_context(vmx->vpid);
5200 +- if (init_event)
5201 +- vmx_clear_hlt(vcpu);
5202 +-}
5203 +-
5204 +-static void enable_irq_window(struct kvm_vcpu *vcpu)
5205 +-{
5206 +- exec_controls_setbit(to_vmx(vcpu), CPU_BASED_INTR_WINDOW_EXITING);
5207 +-}
5208 +-
5209 +-static void enable_nmi_window(struct kvm_vcpu *vcpu)
5210 +-{
5211 +- if (!enable_vnmi ||
5212 +- vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
5213 +- enable_irq_window(vcpu);
5214 +- return;
5215 +- }
5216 +-
5217 +- exec_controls_setbit(to_vmx(vcpu), CPU_BASED_NMI_WINDOW_EXITING);
5218 +-}
5219 +-
5220 +-static void vmx_inject_irq(struct kvm_vcpu *vcpu)
5221 +-{
5222 +- struct vcpu_vmx *vmx = to_vmx(vcpu);
5223 +- uint32_t intr;
5224 +- int irq = vcpu->arch.interrupt.nr;
5225 +-
5226 +- trace_kvm_inj_virq(irq);
5227 +-
5228 +- ++vcpu->stat.irq_injections;
5229 +- if (vmx->rmode.vm86_active) {
5230 +- int inc_eip = 0;
5231 +- if (vcpu->arch.interrupt.soft)
5232 +- inc_eip = vcpu->arch.event_exit_inst_len;
5233 +- kvm_inject_realmode_interrupt(vcpu, irq, inc_eip);
5234 +- return;
5235 +- }
5236 +- intr = irq | INTR_INFO_VALID_MASK;
5237 +- if (vcpu->arch.interrupt.soft) {
5238 +- intr |= INTR_TYPE_SOFT_INTR;
5239 +- vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
5240 +- vmx->vcpu.arch.event_exit_inst_len);
5241 +- } else
5242 +- intr |= INTR_TYPE_EXT_INTR;
5243 +- vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
5244 +-
5245 +- vmx_clear_hlt(vcpu);
5246 +-}
5247 +-
5248 +-static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
5249 +-{
5250 +- struct vcpu_vmx *vmx = to_vmx(vcpu);
5251 +-
5252 +- if (!enable_vnmi) {
5253 +- /*
5254 +- * Tracking the NMI-blocked state in software is built upon
5255 +- * finding the next open IRQ window. This, in turn, depends on
5256 +- * well-behaving guests: They have to keep IRQs disabled at
5257 +- * least as long as the NMI handler runs. Otherwise we may
5258 +- * cause NMI nesting, maybe breaking the guest. But as this is
5259 +- * highly unlikely, we can live with the residual risk.
5260 +- */
5261 +- vmx->loaded_vmcs->soft_vnmi_blocked = 1;
5262 +- vmx->loaded_vmcs->vnmi_blocked_time = 0;
5263 +- }
5264 +-
5265 +- ++vcpu->stat.nmi_injections;
5266 +- vmx->loaded_vmcs->nmi_known_unmasked = false;
5267 +-
5268 +- if (vmx->rmode.vm86_active) {
5269 +- kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0);
5270 +- return;
5271 +- }
5272 +-
5273 +- vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
5274 +- INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
5275 +-
5276 +- vmx_clear_hlt(vcpu);
5277 +-}
5278 +-
5279 +-bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
5280 +-{
5281 +- struct vcpu_vmx *vmx = to_vmx(vcpu);
5282 +- bool masked;
5283 +-
5284 +- if (!enable_vnmi)
5285 +- return vmx->loaded_vmcs->soft_vnmi_blocked;
5286 +- if (vmx->loaded_vmcs->nmi_known_unmasked)
5287 +- return false;
5288 +- masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
5289 +- vmx->loaded_vmcs->nmi_known_unmasked = !masked;
5290 +- return masked;
5291 +-}
5292 +-
5293 +-void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
5294 +-{
5295 +- struct vcpu_vmx *vmx = to_vmx(vcpu);
5296 +-
5297 +- if (!enable_vnmi) {
5298 +- if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
5299 +- vmx->loaded_vmcs->soft_vnmi_blocked = masked;
5300 +- vmx->loaded_vmcs->vnmi_blocked_time = 0;
5301 +- }
5302 +- } else {
5303 +- vmx->loaded_vmcs->nmi_known_unmasked = !masked;
5304 +- if (masked)
5305 +- vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
5306 +- GUEST_INTR_STATE_NMI);
5307 +- else
5308 +- vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
5309 +- GUEST_INTR_STATE_NMI);
5310 +- }
5311 +-}
5312 +-
5313 +-static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
5314 +-{
5315 +- if (to_vmx(vcpu)->nested.nested_run_pending)
5316 +- return 0;
5317 +-
5318 +- if (!enable_vnmi &&
5319 +- to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
5320 +- return 0;
5321 +-
5322 +- return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5323 +- (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
5324 +- | GUEST_INTR_STATE_NMI));
5325 +-}
5326 +-
5327 +-static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
5328 +-{
5329 +- return (!to_vmx(vcpu)->nested.nested_run_pending &&
5330 +- vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
5331 +- !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5332 +- (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
5333 +-}
5334 +-
5335 +-static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
5336 +-{
5337 +- int ret;
5338 +-
5339 +- if (enable_unrestricted_guest)
5340 +- return 0;
5341 +-
5342 +- ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
5343 +- PAGE_SIZE * 3);
5344 +- if (ret)
5345 +- return ret;
5346 +- to_kvm_vmx(kvm)->tss_addr = addr;
5347 +- return init_rmode_tss(kvm);
5348 +-}
5349 +-
5350 +-static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
5351 +-{
5352 +- to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
5353 +- return 0;
5354 +-}
5355 +-
5356 +-static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
5357 +-{
5358 +- switch (vec) {
5359 +- case BP_VECTOR:
5360 +- /*
5361 +- * Update instruction length as we may reinject the exception
5362 +- * from user space while in guest debugging mode.
5363 +- */
5364 +- to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
5365 +- vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
5366 +- if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
5367 +- return false;
5368 +- /* fall through */
5369 +- case DB_VECTOR:
5370 +- if (vcpu->guest_debug &
5371 +- (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
5372 +- return false;
5373 +- /* fall through */
5374 +- case DE_VECTOR:
5375 +- case OF_VECTOR:
5376 +- case BR_VECTOR:
5377 +- case UD_VECTOR:
5378 +- case DF_VECTOR:
5379 +- case SS_VECTOR:
5380 +- case GP_VECTOR:
5381 +- case MF_VECTOR:
5382 +- return true;
5383 +- break;
5384 +- }
5385 +- return false;
5386 +-}
5387 +-
5388 +-static int handle_rmode_exception(struct kvm_vcpu *vcpu,
5389 +- int vec, u32 err_code)
5390 +-{
5391 +- /*
5392 +- * Instruction with address size override prefix opcode 0x67
5393 +- * Cause the #SS fault with 0 error code in VM86 mode.
5394 +- */
5395 +- if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
5396 +- if (kvm_emulate_instruction(vcpu, 0)) {
5397 +- if (vcpu->arch.halt_request) {
5398 +- vcpu->arch.halt_request = 0;
5399 +- return kvm_vcpu_halt(vcpu);
5400 +- }
5401 +- return 1;
5402 +- }
5403 +- return 0;
5404 +- }
5405 +-
5406 +- /*
5407 +- * Forward all other exceptions that are valid in real mode.
5408 +- * FIXME: Breaks guest debugging in real mode, needs to be fixed with
5409 +- * the required debugging infrastructure rework.
5410 +- */
5411 +- kvm_queue_exception(vcpu, vec);
5412 +- return 1;
5413 +-}
5414 +-
5415 +-/*
5416 +- * Trigger machine check on the host. We assume all the MSRs are already set up
5417 +- * by the CPU and that we still run on the same CPU as the MCE occurred on.
5418 +- * We pass a fake environment to the machine check handler because we want
5419 +- * the guest to be always treated like user space, no matter what context
5420 +- * it used internally.
5421 +- */
5422 +-static void kvm_machine_check(void)
5423 +-{
5424 +-#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
5425 +- struct pt_regs regs = {
5426 +- .cs = 3, /* Fake ring 3 no matter what the guest ran on */
5427 +- .flags = X86_EFLAGS_IF,
5428 +- };
5429 +-
5430 +- do_machine_check(&regs, 0);
5431 +-#endif
5432 +-}
5433 +-
5434 +-static int handle_machine_check(struct kvm_vcpu *vcpu)
5435 +-{
5436 +- /* handled by vmx_vcpu_run() */
5437 +- return 1;
5438 +-}
5439 +-
5440 +-static int handle_exception_nmi(struct kvm_vcpu *vcpu)
5441 +-{
5442 +- struct vcpu_vmx *vmx = to_vmx(vcpu);
5443 +- struct kvm_run *kvm_run = vcpu->run;
5444 +- u32 intr_info, ex_no, error_code;
5445 +- unsigned long cr2, rip, dr6;
5446 +- u32 vect_info;
5447 +-
5448 +- vect_info = vmx->idt_vectoring_info;
5449 +- intr_info = vmx->exit_intr_info;
5450 +-
5451 +- if (is_machine_check(intr_info) || is_nmi(intr_info))
5452 +- return 1; /* handled by handle_exception_nmi_irqoff() */
5453 +-
5454 +- if (is_invalid_opcode(intr_info))
5455 +- return handle_ud(vcpu);
5456 +-
5457 +- error_code = 0;
5458 +- if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
5459 +- error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
5460 +-
5461 +- if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
5462 +- WARN_ON_ONCE(!enable_vmware_backdoor);
5463 +-
5464 +- /*
5465 +- * VMware backdoor emulation on #GP interception only handles
5466 +- * IN{S}, OUT{S}, and RDPMC, none of which generate a non-zero
5467 +- * error code on #GP.
5468 +- */
5469 +- if (error_code) {
5470 +- kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
5471 +- return 1;
5472 +- }
5473 +- return kvm_emulate_instruction(vcpu, EMULTYPE_VMWARE_GP);
5474 +- }
5475 +-
5476 +- /*
5477 +- * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
5478 +- * MMIO, it is better to report an internal error.
5479 +- * See the comments in vmx_handle_exit.
5480 +- */
5481 +- if ((vect_info & VECTORING_INFO_VALID_MASK) &&
5482 +- !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
5483 +- vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5484 +- vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
5485 +- vcpu->run->internal.ndata = 3;
5486 +- vcpu->run->internal.data[0] = vect_info;
5487 +- vcpu->run->internal.data[1] = intr_info;
5488 +- vcpu->run->internal.data[2] = error_code;
5489 +- return 0;
5490 +- }
5491 +-
5492 +- if (is_page_fault(intr_info)) {
5493 +- cr2 = vmcs_readl(EXIT_QUALIFICATION);
5494 +- /* EPT won't cause page fault directly */
5495 +- WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
5496 +- return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
5497 +- }
5498 +-
5499 +- ex_no = intr_info & INTR_INFO_VECTOR_MASK;
5500 +-
5501 +- if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
5502 +- return handle_rmode_exception(vcpu, ex_no, error_code);
5503 +-
5504 +- switch (ex_no) {
5505 +- case AC_VECTOR:
5506 +- kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
5507 +- return 1;
5508 +- case DB_VECTOR:
5509 +- dr6 = vmcs_readl(EXIT_QUALIFICATION);
5510 +- if (!(vcpu->guest_debug &
5511 +- (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
5512 +- vcpu->arch.dr6 &= ~DR_TRAP_BITS;
5513 +- vcpu->arch.dr6 |= dr6 | DR6_RTM;
5514 +- if (is_icebp(intr_info))
5515 +- WARN_ON(!skip_emulated_instruction(vcpu));
5516 +-
5517 +- kvm_queue_exception(vcpu, DB_VECTOR);
5518 +- return 1;
5519 +- }
5520 +- kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
5521 +- kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
5522 +- /* fall through */
5523 +- case BP_VECTOR:
5524 +- /*
5525 +- * Update instruction length as we may reinject #BP from
5526 +- * user space while in guest debugging mode. Reading it for
5527 +- * #DB as well causes no harm, it is not used in that case.
5528 +- */
5529 +- vmx->vcpu.arch.event_exit_inst_len =
5530 +- vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
5531 +- kvm_run->exit_reason = KVM_EXIT_DEBUG;
5532 +- rip = kvm_rip_read(vcpu);
5533 +- kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
5534 +- kvm_run->debug.arch.exception = ex_no;
5535 +- break;
5536 +- default:
5537 +- kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
5538 +- kvm_run->ex.exception = ex_no;
5539 +- kvm_run->ex.error_code = error_code;
5540 +- break;
5541 +- }
5542 +- return 0;
5543 +-}
5544 +-
5545 +-static __always_inline int handle_external_interrupt(struct kvm_vcpu *vcpu)
5546 +-{
5547 +- ++vcpu->stat.irq_exits;
5548 +- return 1;
5549 +-}
5550 +-
5551 +-static int handle_triple_fault(struct kvm_vcpu *vcpu)
5552 +-{
5553 +- vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
5554 +- vcpu->mmio_needed = 0;
5555 +- return 0;
5556 +-}
5557 +-
5558 +-static int handle_io(struct kvm_vcpu *vcpu)
5559 +-{
5560 +- unsigned long exit_qualification;
5561 +- int size, in, string;
5562 +- unsigned port;
5563 +-
5564 +- exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5565 +- string = (exit_qualification & 16) != 0;
5566 +-
5567 +- ++vcpu->stat.io_exits;
5568 +-
5569 +- if (string)
5570 +- return kvm_emulate_instruction(vcpu, 0);
5571 +-
5572 +- port = exit_qualification >> 16;
5573 +- size = (exit_qualification & 7) + 1;
5574 +- in = (exit_qualification & 8) != 0;
5575 +-
5576 +- return kvm_fast_pio(vcpu, size, port, in);
5577 +-}
5578 +-
5579 +-static void
5580 +-vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
5581 +-{
5582 +- /*
5583 +- * Patch in the VMCALL instruction:
5584 +- */
5585 +- hypercall[0] = 0x0f;
5586 +- hypercall[1] = 0x01;
5587 +- hypercall[2] = 0xc1;
5588 +-}
5589 +-
5590 +-/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
5591 +-static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
5592 +-{
5593 +- if (is_guest_mode(vcpu)) {
5594 +- struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5595 +- unsigned long orig_val = val;
5596 +-
5597 +- /*
5598 +- * We get here when L2 changed cr0 in a way that did not change
5599 +- * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
5600 +- * but did change L0 shadowed bits. So we first calculate the
5601 +- * effective cr0 value that L1 would like to write into the
5602 +- * hardware. It consists of the L2-owned bits from the new
5603 +- * value combined with the L1-owned bits from L1's guest_cr0.
5604 +- */
5605 +- val = (val & ~vmcs12->cr0_guest_host_mask) |
5606 +- (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
5607 +-
5608 +- if (!nested_guest_cr0_valid(vcpu, val))
5609 +- return 1;
5610 +-
5611 +- if (kvm_set_cr0(vcpu, val))
5612 +- return 1;
5613 +- vmcs_writel(CR0_READ_SHADOW, orig_val);
5614 +- return 0;
5615 +- } else {
5616 +- if (to_vmx(vcpu)->nested.vmxon &&
5617 +- !nested_host_cr0_valid(vcpu, val))
5618 +- return 1;
5619 +-
5620 +- return kvm_set_cr0(vcpu, val);
5621 +- }
5622 +-}
5623 +-
5624 +-static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
5625 +-{
5626 +- if (is_guest_mode(vcpu)) {
5627 +- struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5628 +- unsigned long orig_val = val;
5629 +-
5630 +- /* analogously to handle_set_cr0 */
5631 +- val = (val & ~vmcs12->cr4_guest_host_mask) |
5632 +- (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
5633 +- if (kvm_set_cr4(vcpu, val))
5634 +- return 1;
5635 +- vmcs_writel(CR4_READ_SHADOW, orig_val);
5636 +- return 0;
5637 +- } else
5638 +- return kvm_set_cr4(vcpu, val);
5639 +-}
5640 +-
5641 +-static int handle_desc(struct kvm_vcpu *vcpu)
5642 +-{
5643 +- WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
5644 +- return kvm_emulate_instruction(vcpu, 0);
5645 +-}
5646 +-
5647 +-static int handle_cr(struct kvm_vcpu *vcpu)
5648 +-{
5649 +- unsigned long exit_qualification, val;
5650 +- int cr;
5651 +- int reg;
5652 +- int err;
5653 +- int ret;
5654 +-
5655 +- exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5656 +- cr = exit_qualification & 15;
5657 +- reg = (exit_qualification >> 8) & 15;
5658 +- switch ((exit_qualification >> 4) & 3) {
5659 +- case 0: /* mov to cr */
5660 +- val = kvm_register_readl(vcpu, reg);
5661 +- trace_kvm_cr_write(cr, val);
5662 +- switch (cr) {
5663 +- case 0:
5664 +- err = handle_set_cr0(vcpu, val);
5665 +- return kvm_complete_insn_gp(vcpu, err);
5666 +- case 3:
5667 +- WARN_ON_ONCE(enable_unrestricted_guest);
5668 +- err = kvm_set_cr3(vcpu, val);
5669 +- return kvm_complete_insn_gp(vcpu, err);
5670 +- case 4:
5671 +- err = handle_set_cr4(vcpu, val);
5672 +- return kvm_complete_insn_gp(vcpu, err);
5673 +- case 8: {
5674 +- u8 cr8_prev = kvm_get_cr8(vcpu);
5675 +- u8 cr8 = (u8)val;
5676 +- err = kvm_set_cr8(vcpu, cr8);
5677 +- ret = kvm_complete_insn_gp(vcpu, err);
5678 +- if (lapic_in_kernel(vcpu))
5679 +- return ret;
5680 +- if (cr8_prev <= cr8)
5681 +- return ret;
5682 +- /*
5683 +- * TODO: we might be squashing a
5684 +- * KVM_GUESTDBG_SINGLESTEP-triggered
5685 +- * KVM_EXIT_DEBUG here.
5686 +- */
5687 +- vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
5688 +- return 0;
5689 +- }
5690 +- }
5691 +- break;
5692 +- case 2: /* clts */
5693 +- WARN_ONCE(1, "Guest should always own CR0.TS");
5694 +- vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
5695 +- trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
5696 +- return kvm_skip_emulated_instruction(vcpu);
5697 +- case 1: /*mov from cr*/
5698 +- switch (cr) {
5699 +- case 3:
5700 +- WARN_ON_ONCE(enable_unrestricted_guest);
5701 +- val = kvm_read_cr3(vcpu);
5702 +- kvm_register_write(vcpu, reg, val);
5703 +- trace_kvm_cr_read(cr, val);
5704 +- return kvm_skip_emulated_instruction(vcpu);
5705 +- case 8:
5706 +- val = kvm_get_cr8(vcpu);
5707 +- kvm_register_write(vcpu, reg, val);
5708 +- trace_kvm_cr_read(cr, val);
5709 +- return kvm_skip_emulated_instruction(vcpu);
5710 +- }
5711 +- break;
5712 +- case 3: /* lmsw */
5713 +- val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
5714 +- trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
5715 +- kvm_lmsw(vcpu, val);
5716 +-
5717 +- return kvm_skip_emulated_instruction(vcpu);
5718 +- default:
5719 +- break;
5720 +- }
5721 +- vcpu->run->exit_reason = 0;
5722 +- vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
5723 +- (int)(exit_qualification >> 4) & 3, cr);
5724 +- return 0;
5725 +-}
5726 +-
5727 +-static int handle_dr(struct kvm_vcpu *vcpu)
5728 +-{
5729 +- unsigned long exit_qualification;
5730 +- int dr, dr7, reg;
5731 +-
5732 +- exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5733 +- dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
5734 +-
5735 +- /* First, if DR does not exist, trigger UD */
5736 +- if (!kvm_require_dr(vcpu, dr))
5737 +- return 1;
5738 +-
5739 +- /* Do not handle if the CPL > 0, will trigger GP on re-entry */
5740 +- if (!kvm_require_cpl(vcpu, 0))
5741 +- return 1;
5742 +- dr7 = vmcs_readl(GUEST_DR7);
5743 +- if (dr7 & DR7_GD) {
5744 +- /*
5745 +- * As the vm-exit takes precedence over the debug trap, we
5746 +- * need to emulate the latter, either for the host or the
5747 +- * guest debugging itself.
5748 +- */
5749 +- if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
5750 +- vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
5751 +- vcpu->run->debug.arch.dr7 = dr7;
5752 +- vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
5753 +- vcpu->run->debug.arch.exception = DB_VECTOR;
5754 +- vcpu->run->exit_reason = KVM_EXIT_DEBUG;
5755 +- return 0;
5756 +- } else {
5757 +- vcpu->arch.dr6 &= ~DR_TRAP_BITS;
5758 +- vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
5759 +- kvm_queue_exception(vcpu, DB_VECTOR);
5760 +- return 1;
5761 +- }
5762 +- }
5763 +-
5764 +- if (vcpu->guest_debug == 0) {
5765 +- exec_controls_clearbit(to_vmx(vcpu), CPU_BASED_MOV_DR_EXITING);
5766 +-
5767 +- /*
5768 +- * No more DR vmexits; force a reload of the debug registers
5769 +- * and reenter on this instruction. The next vmexit will
5770 +- * retrieve the full state of the debug registers.
5771 +- */
5772 +- vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
5773 +- return 1;
5774 +- }
5775 +-
5776 +- reg = DEBUG_REG_ACCESS_REG(exit_qualification);
5777 +- if (exit_qualification & TYPE_MOV_FROM_DR) {
5778 +- unsigned long val;
5779 +-
5780 +- if (kvm_get_dr(vcpu, dr, &val))
5781 +- return 1;
5782 +- kvm_register_write(vcpu, reg, val);
5783 +- } else
5784 +- if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
5785 +- return 1;
5786 +-
5787 +- return kvm_skip_emulated_instruction(vcpu);
5788 +-}
5789 +-
5790 +-static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
5791 +-{
5792 +- return vcpu->arch.dr6;
5793 +-}
5794 +-
5795 +-static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
5796 +-{
5797 +-}
5798 +-
5799 +-static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
5800 +-{
5801 +- get_debugreg(vcpu->arch.db[0], 0);
5802 +- get_debugreg(vcpu->arch.db[1], 1);
5803 +- get_debugreg(vcpu->arch.db[2], 2);
5804 +- get_debugreg(vcpu->arch.db[3], 3);
5805 +- get_debugreg(vcpu->arch.dr6, 6);
5806 +- vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
5807 +-
5808 +- vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
5809 +- exec_controls_setbit(to_vmx(vcpu), CPU_BASED_MOV_DR_EXITING);
5810 +-}
5811 +-
5812 +-static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
5813 +-{
5814 +- vmcs_writel(GUEST_DR7, val);
5815 +-}
5816 +-
5817 +-static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
5818 +-{
5819 +- kvm_apic_update_ppr(vcpu);
5820 +- return 1;
5821 +-}
5822 +-
5823 +-static int handle_interrupt_window(struct kvm_vcpu *vcpu)
5824 +-{
5825 +- exec_controls_clearbit(to_vmx(vcpu), CPU_BASED_INTR_WINDOW_EXITING);
5826 +-
5827 +- kvm_make_request(KVM_REQ_EVENT, vcpu);
5828 +-
5829 +- ++vcpu->stat.irq_window_exits;
5830 +- return 1;
5831 +-}
5832 +-
5833 +-static int handle_vmcall(struct kvm_vcpu *vcpu)
5834 +-{
5835 +- return kvm_emulate_hypercall(vcpu);
5836 +-}
5837 +-
5838 +-static int handle_invd(struct kvm_vcpu *vcpu)
5839 +-{
5840 +- return kvm_emulate_instruction(vcpu, 0);
5841 +-}
5842 +-
5843 +-static int handle_invlpg(struct kvm_vcpu *vcpu)
5844 +-{
5845 +- unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5846 +-
5847 +- kvm_mmu_invlpg(vcpu, exit_qualification);
5848 +- return kvm_skip_emulated_instruction(vcpu);
5849 +-}
5850 +-
5851 +-static int handle_rdpmc(struct kvm_vcpu *vcpu)
5852 +-{
5853 +- int err;
5854 +-
5855 +- err = kvm_rdpmc(vcpu);
5856 +- return kvm_complete_insn_gp(vcpu, err);
5857 +-}
5858 +-
5859 +-static int handle_wbinvd(struct kvm_vcpu *vcpu)
5860 +-{
5861 +- return kvm_emulate_wbinvd(vcpu);
5862 +-}
5863 +-
5864 +-static int handle_xsetbv(struct kvm_vcpu *vcpu)
5865 +-{
5866 +- u64 new_bv = kvm_read_edx_eax(vcpu);
5867 +- u32 index = kvm_rcx_read(vcpu);
5868 +-
5869 +- if (kvm_set_xcr(vcpu, index, new_bv) == 0)
5870 +- return kvm_skip_emulated_instruction(vcpu);
5871 +- return 1;
5872 +-}
5873 +-
5874 +-static int handle_apic_access(struct kvm_vcpu *vcpu)
5875 +-{
5876 +- if (likely(fasteoi)) {
5877 +- unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5878 +- int access_type, offset;
5879 +-
5880 +- access_type = exit_qualification & APIC_ACCESS_TYPE;
5881 +- offset = exit_qualification & APIC_ACCESS_OFFSET;
5882 +- /*
5883 +- * Sane guest uses MOV to write EOI, with written value
5884 +- * not cared. So make a short-circuit here by avoiding
5885 +- * heavy instruction emulation.
5886 +- */
5887 +- if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
5888 +- (offset == APIC_EOI)) {
5889 +- kvm_lapic_set_eoi(vcpu);
5890 +- return kvm_skip_emulated_instruction(vcpu);
5891 +- }
5892 +- }
5893 +- return kvm_emulate_instruction(vcpu, 0);
5894 +-}
5895 +-
5896 +-static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
5897 +-{
5898 +- unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5899 +- int vector = exit_qualification & 0xff;
5900 +-
5901 +- /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
5902 +- kvm_apic_set_eoi_accelerated(vcpu, vector);
5903 +- return 1;
5904 +-}
5905 +-
5906 +-static int handle_apic_write(struct kvm_vcpu *vcpu)
5907 +-{
5908 +- unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5909 +- u32 offset = exit_qualification & 0xfff;
5910 +-
5911 +- /* APIC-write VM exit is trap-like and thus no need to adjust IP */
5912 +- kvm_apic_write_nodecode(vcpu, offset);
5913 +- return 1;
5914 +-}
5915 +-
5916 +-static int handle_task_switch(struct kvm_vcpu *vcpu)
5917 +-{
5918 +- struct vcpu_vmx *vmx = to_vmx(vcpu);
5919 +- unsigned long exit_qualification;
5920 +- bool has_error_code = false;
5921 +- u32 error_code = 0;
5922 +- u16 tss_selector;
5923 +- int reason, type, idt_v, idt_index;
5924 +-
5925 +- idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
5926 +- idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
5927 +- type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
5928 +-
5929 +- exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5930 +-
5931 +- reason = (u32)exit_qualification >> 30;
5932 +- if (reason == TASK_SWITCH_GATE && idt_v) {
5933 +- switch (type) {
5934 +- case INTR_TYPE_NMI_INTR:
5935 +- vcpu->arch.nmi_injected = false;
5936 +- vmx_set_nmi_mask(vcpu, true);
5937 +- break;
5938 +- case INTR_TYPE_EXT_INTR:
5939 +- case INTR_TYPE_SOFT_INTR:
5940 +- kvm_clear_interrupt_queue(vcpu);
5941 +- break;
5942 +- case INTR_TYPE_HARD_EXCEPTION:
5943 +- if (vmx->idt_vectoring_info &
5944 +- VECTORING_INFO_DELIVER_CODE_MASK) {
5945 +- has_error_code = true;
5946 +- error_code =
5947 +- vmcs_read32(IDT_VECTORING_ERROR_CODE);
5948 +- }
5949 +- /* fall through */
5950 +- case INTR_TYPE_SOFT_EXCEPTION:
5951 +- kvm_clear_exception_queue(vcpu);
5952 +- break;
5953 +- default:
5954 +- break;
5955 +- }
5956 +- }
5957 +- tss_selector = exit_qualification;
5958 +-
5959 +- if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
5960 +- type != INTR_TYPE_EXT_INTR &&
5961 +- type != INTR_TYPE_NMI_INTR))
5962 +- WARN_ON(!skip_emulated_instruction(vcpu));
5963 +-
5964 +- /*
5965 +- * TODO: What about debug traps on tss switch?
5966 +- * Are we supposed to inject them and update dr6?
5967 +- */
5968 +- return kvm_task_switch(vcpu, tss_selector,
5969 +- type == INTR_TYPE_SOFT_INTR ? idt_index : -1,
5970 +- reason, has_error_code, error_code);
5971 +-}
5972 +-
5973 +-static int handle_ept_violation(struct kvm_vcpu *vcpu)
5974 +-{
5975 +- unsigned long exit_qualification;
5976 +- gpa_t gpa;
5977 +- u64 error_code;
5978 +-
5979 +- exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5980 +-
5981 +- /*
5982 +- * EPT violation happened while executing iret from NMI,
5983 +- * "blocked by NMI" bit has to be set before next VM entry.
5984 +- * There are errata that may cause this bit to not be set:
5985 +- * AAK134, BY25.
5986 +- */
5987 +- if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
5988 +- enable_vnmi &&
5989 +- (exit_qualification & INTR_INFO_UNBLOCK_NMI))
5990 +- vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
5991 +-
5992 +- gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
5993 +- trace_kvm_page_fault(gpa, exit_qualification);
5994 +-
5995 +- /* Is it a read fault? */
5996 +- error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
5997 +- ? PFERR_USER_MASK : 0;
5998 +- /* Is it a write fault? */
5999 +- error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
6000 +- ? PFERR_WRITE_MASK : 0;
6001 +- /* Is it a fetch fault? */
6002 +- error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
6003 +- ? PFERR_FETCH_MASK : 0;
6004 +- /* ept page table entry is present? */
6005 +- error_code |= (exit_qualification &
6006 +- (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
6007 +- EPT_VIOLATION_EXECUTABLE))
6008 +- ? PFERR_PRESENT_MASK : 0;
6009 +-
6010 +- error_code |= (exit_qualification & 0x100) != 0 ?
6011 +- PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
6012 +-
6013 +- vcpu->arch.exit_qualification = exit_qualification;
6014 +- return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
6015 +-}
6016 +-
6017 +-static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
6018 +-{
6019 +- gpa_t gpa;
6020 +-
6021 +- /*
6022 +- * A nested guest cannot optimize MMIO vmexits, because we have an
6023 +- * nGPA here instead of the required GPA.
6024 +- */
6025 +- gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
6026 +- if (!is_guest_mode(vcpu) &&
6027 +- !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
6028 +- trace_kvm_fast_mmio(gpa);
6029 +- return kvm_skip_emulated_instruction(vcpu);
6030 +- }
6031 +-
6032 +- return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
6033 +-}
6034 +-
6035 +-static int handle_nmi_window(struct kvm_vcpu *vcpu)
6036 +-{
6037 +- WARN_ON_ONCE(!enable_vnmi);
6038 +- exec_controls_clearbit(to_vmx(vcpu), CPU_BASED_NMI_WINDOW_EXITING);
6039 +- ++vcpu->stat.nmi_window_exits;
6040 +- kvm_make_request(KVM_REQ_EVENT, vcpu);
6041 +-
6042 +- return 1;
6043 +-}
6044 +-
6045 +-static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
6046 +-{
6047 +- struct vcpu_vmx *vmx = to_vmx(vcpu);
6048 +- bool intr_window_requested;
6049 +- unsigned count = 130;
6050 +-
6051 +- /*
6052 +- * We should never reach the point where we are emulating L2
6053 +- * due to invalid guest state as that means we incorrectly
6054 +- * allowed a nested VMEntry with an invalid vmcs12.
6055 +- */
6056 +- WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
6057 +-
6058 +- intr_window_requested = exec_controls_get(vmx) &
6059 +- CPU_BASED_INTR_WINDOW_EXITING;
6060 +-
6061 +- while (vmx->emulation_required && count-- != 0) {
6062 +- if (intr_window_requested && vmx_interrupt_allowed(vcpu))
6063 +- return handle_interrupt_window(&vmx->vcpu);
6064 +-
6065 +- if (kvm_test_request(KVM_REQ_EVENT, vcpu))
6066 +- return 1;
6067 +-
6068 +- if (!kvm_emulate_instruction(vcpu, 0))
6069 +- return 0;
6070 +-
6071 +- if (vmx->emulation_required && !vmx->rmode.vm86_active &&
6072 +- vcpu->arch.exception.pending) {
6073 +- vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6074 +- vcpu->run->internal.suberror =
6075 +- KVM_INTERNAL_ERROR_EMULATION;
6076 +- vcpu->run->internal.ndata = 0;
6077 +- return 0;
6078 +- }
6079 +-
6080 +- if (vcpu->arch.halt_request) {
6081 +- vcpu->arch.halt_request = 0;
6082 +- return kvm_vcpu_halt(vcpu);
6083 +- }
6084 +-
6085 +- /*
6086 +- * Note, return 1 and not 0, vcpu_run() is responsible for
6087 +- * morphing the pending signal into the proper return code.
6088 +- */
6089 +- if (signal_pending(current))
6090 +- return 1;
6091 +-
6092 +- if (need_resched())
6093 +- schedule();
6094 +- }
6095 +-
6096 +- return 1;
6097 +-}
6098 +-
6099 +-static void grow_ple_window(struct kvm_vcpu *vcpu)
6100 +-{
6101 +- struct vcpu_vmx *vmx = to_vmx(vcpu);
6102 +- unsigned int old = vmx->ple_window;
6103 +-
6104 +- vmx->ple_window = __grow_ple_window(old, ple_window,
6105 +- ple_window_grow,
6106 +- ple_window_max);
6107 +-
6108 +- if (vmx->ple_window != old) {
6109 +- vmx->ple_window_dirty = true;
6110 +- trace_kvm_ple_window_update(vcpu->vcpu_id,
6111 +- vmx->ple_window, old);
6112 +- }
6113 +-}
6114 +-
6115 +-static void shrink_ple_window(struct kvm_vcpu *vcpu)
6116 +-{
6117 +- struct vcpu_vmx *vmx = to_vmx(vcpu);
6118 +- unsigned int old = vmx->ple_window;
6119 +-
6120 +- vmx->ple_window = __shrink_ple_window(old, ple_window,
6121 +- ple_window_shrink,
6122 +- ple_window);
6123 +-
6124 +- if (vmx->ple_window != old) {
6125 +- vmx->ple_window_dirty = true;
6126 +- trace_kvm_ple_window_update(vcpu->vcpu_id,
6127 +- vmx->ple_window, old);
6128 +- }
6129 +-}
6130 +-
6131 +-/*
6132 +- * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
6133 +- */
6134 +-static void wakeup_handler(void)
6135 +-{
6136 +- struct kvm_vcpu *vcpu;
6137 +- int cpu = smp_processor_id();
6138 +-
6139 +- spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6140 +- list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
6141 +- blocked_vcpu_list) {
6142 +- struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
6143 +-
6144 +- if (pi_test_on(pi_desc) == 1)
6145 +- kvm_vcpu_kick(vcpu);
6146 +- }
6147 +- spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6148 +-}
6149 +-
6150 +-static void vmx_enable_tdp(void)
6151 +-{
6152 +- kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
6153 +- enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
6154 +- enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
6155 +- 0ull, VMX_EPT_EXECUTABLE_MASK,
6156 +- cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
6157 +- VMX_EPT_RWX_MASK, 0ull);
6158 +-
6159 +- ept_set_mmio_spte_mask();
6160 +- kvm_enable_tdp();
6161 +-}
6162 +-
6163 +-/*
6164 +- * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
6165 +- * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
6166 +- */
6167 +-static int handle_pause(struct kvm_vcpu *vcpu)
6168 +-{
6169 +- if (!kvm_pause_in_guest(vcpu->kvm))
6170 +- grow_ple_window(vcpu);
6171 +-
6172 +- /*
6173 +- * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
6174 +- * VM-execution control is ignored if CPL > 0. OTOH, KVM
6175 +- * never set PAUSE_EXITING and just set PLE if supported,
6176 +- * so the vcpu must be CPL=0 if it gets a PAUSE exit.
6177 +- */
6178 +- kvm_vcpu_on_spin(vcpu, true);
6179 +- return kvm_skip_emulated_instruction(vcpu);
6180 +-}
6181 +-
6182 +-static int handle_nop(struct kvm_vcpu *vcpu)
6183 +-{
6184 +- return kvm_skip_emulated_instruction(vcpu);
6185 +-}
6186 +-
6187 +-static int handle_mwait(struct kvm_vcpu *vcpu)
6188 +-{
6189 +- printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
6190 +- return handle_nop(vcpu);
6191 +-}
6192 +-
6193 +-static int handle_invalid_op(struct kvm_vcpu *vcpu)
6194 +-{
6195 +- kvm_queue_exception(vcpu, UD_VECTOR);
6196 +- return 1;
6197 +-}
6198 +-
6199 +-static int handle_monitor_trap(struct kvm_vcpu *vcpu)
6200 +-{
6201 +- return 1;
6202 +-}
6203 +-
6204 +-static int handle_monitor(struct kvm_vcpu *vcpu)
6205 +-{
6206 +- printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
6207 +- return handle_nop(vcpu);
6208 +-}
6209 +-
6210 +-static int handle_invpcid(struct kvm_vcpu *vcpu)
6211 +-{
6212 +- u32 vmx_instruction_info;
6213 +- unsigned long type;
6214 +- bool pcid_enabled;
6215 +- gva_t gva;
6216 +- struct x86_exception e;
6217 +- unsigned i;
6218 +- unsigned long roots_to_free = 0;
6219 +- struct {
6220 +- u64 pcid;
6221 +- u64 gla;
6222 +- } operand;
6223 +-
6224 +- if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
6225 +- kvm_queue_exception(vcpu, UD_VECTOR);
6226 +- return 1;
6227 +- }
6228 +-
6229 +- vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
6230 +- type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
6231 +-
6232 +- if (type > 3) {
6233 +- kvm_inject_gp(vcpu, 0);
6234 +- return 1;
6235 +- }
6236 +-
6237 +- /* According to the Intel instruction reference, the memory operand
6238 +- * is read even if it isn't needed (e.g., for type==all)
6239 +- */
6240 +- if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
6241 +- vmx_instruction_info, false,
6242 +- sizeof(operand), &gva))
6243 +- return 1;
6244 +-
6245 +- if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
6246 +- kvm_inject_page_fault(vcpu, &e);
6247 +- return 1;
6248 +- }
6249 +-
6250 +- if (operand.pcid >> 12 != 0) {
6251 +- kvm_inject_gp(vcpu, 0);
6252 +- return 1;
6253 +- }
6254 +-
6255 +- pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
6256 +-
6257 +- switch (type) {
6258 +- case INVPCID_TYPE_INDIV_ADDR:
6259 +- if ((!pcid_enabled && (operand.pcid != 0)) ||
6260 +- is_noncanonical_address(operand.gla, vcpu)) {
6261 +- kvm_inject_gp(vcpu, 0);
6262 +- return 1;
6263 +- }
6264 +- kvm_mmu_invpcid_gva(vcpu, operand.gla, operand.pcid);
6265 +- return kvm_skip_emulated_instruction(vcpu);
6266 +-
6267 +- case INVPCID_TYPE_SINGLE_CTXT:
6268 +- if (!pcid_enabled && (operand.pcid != 0)) {
6269 +- kvm_inject_gp(vcpu, 0);
6270 +- return 1;
6271 +- }
6272 +-
6273 +- if (kvm_get_active_pcid(vcpu) == operand.pcid) {
6274 +- kvm_mmu_sync_roots(vcpu);
6275 +- kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
6276 +- }
6277 +-
6278 +- for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
6279 +- if (kvm_get_pcid(vcpu, vcpu->arch.mmu->prev_roots[i].cr3)
6280 +- == operand.pcid)
6281 +- roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
6282 +-
6283 +- kvm_mmu_free_roots(vcpu, vcpu->arch.mmu, roots_to_free);
6284 +- /*
6285 +- * If neither the current cr3 nor any of the prev_roots use the
6286 +- * given PCID, then nothing needs to be done here because a
6287 +- * resync will happen anyway before switching to any other CR3.
6288 +- */
6289 +-
6290 +- return kvm_skip_emulated_instruction(vcpu);
6291 +-
6292 +- case INVPCID_TYPE_ALL_NON_GLOBAL:
6293 +- /*
6294 +- * Currently, KVM doesn't mark global entries in the shadow
6295 +- * page tables, so a non-global flush just degenerates to a
6296 +- * global flush. If needed, we could optimize this later by
6297 +- * keeping track of global entries in shadow page tables.
6298 +- */
6299 +-
6300 +- /* fall-through */
6301 +- case INVPCID_TYPE_ALL_INCL_GLOBAL:
6302 +- kvm_mmu_unload(vcpu);
6303 +- return kvm_skip_emulated_instruction(vcpu);
6304 +-
6305 +- default:
6306 +- BUG(); /* We have already checked above that type <= 3 */
6307 +- }
6308 +-}
6309 +-
6310 +-static int handle_pml_full(struct kvm_vcpu *vcpu)
6311 +-{
6312 +- unsigned long exit_qualification;
6313 +-
6314 +- trace_kvm_pml_full(vcpu->vcpu_id);
6315 +-
6316 +- exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6317 +-
6318 +- /*
6319 +- * PML buffer FULL happened while executing iret from NMI,
6320 +- * "blocked by NMI" bit has to be set before next VM entry.
6321 +- */
6322 +- if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
6323 +- enable_vnmi &&
6324 +- (exit_qualification & INTR_INFO_UNBLOCK_NMI))
6325 +- vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6326 +- GUEST_INTR_STATE_NMI);
6327 +-
6328 +- /*
6329 +- * PML buffer already flushed at beginning of VMEXIT. Nothing to do
6330 +- * here.., and there's no userspace involvement needed for PML.
6331 +- */
6332 +- return 1;
6333 +-}
6334 +-
6335 +-static int handle_preemption_timer(struct kvm_vcpu *vcpu)
6336 +-{
6337 +- struct vcpu_vmx *vmx = to_vmx(vcpu);
6338 +-
6339 +- if (!vmx->req_immediate_exit &&
6340 +- !unlikely(vmx->loaded_vmcs->hv_timer_soft_disabled))
6341 +- kvm_lapic_expired_hv_timer(vcpu);
6342 +-
6343 +- return 1;
6344 +-}
6345 +-
6346 +-/*
6347 +- * When nested=0, all VMX instruction VM Exits filter here. The handlers
6348 +- * are overwritten by nested_vmx_setup() when nested=1.
6349 +- */
6350 +-static int handle_vmx_instruction(struct kvm_vcpu *vcpu)
6351 +-{
6352 +- kvm_queue_exception(vcpu, UD_VECTOR);
6353 +- return 1;
6354 +-}
6355 +-
6356 +-static int handle_encls(struct kvm_vcpu *vcpu)
6357 +-{
6358 +- /*
6359 +- * SGX virtualization is not yet supported. There is no software
6360 +- * enable bit for SGX, so we have to trap ENCLS and inject a #UD
6361 +- * to prevent the guest from executing ENCLS.
6362 +- */
6363 +- kvm_queue_exception(vcpu, UD_VECTOR);
6364 +- return 1;
6365 +-}
6366 +-
6367 +-/*
6368 +- * The exit handlers return 1 if the exit was handled fully and guest execution
6369 +- * may resume. Otherwise they set the kvm_run parameter to indicate what needs
6370 +- * to be done to userspace and return 0.
6371 +- */
6372 +-static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
6373 +- [EXIT_REASON_EXCEPTION_NMI] = handle_exception_nmi,
6374 +- [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
6375 +- [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
6376 +- [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
6377 +- [EXIT_REASON_IO_INSTRUCTION] = handle_io,
6378 +- [EXIT_REASON_CR_ACCESS] = handle_cr,
6379 +- [EXIT_REASON_DR_ACCESS] = handle_dr,
6380 +- [EXIT_REASON_CPUID] = kvm_emulate_cpuid,
6381 +- [EXIT_REASON_MSR_READ] = kvm_emulate_rdmsr,
6382 +- [EXIT_REASON_MSR_WRITE] = kvm_emulate_wrmsr,
6383 +- [EXIT_REASON_INTERRUPT_WINDOW] = handle_interrupt_window,
6384 +- [EXIT_REASON_HLT] = kvm_emulate_halt,
6385 +- [EXIT_REASON_INVD] = handle_invd,
6386 +- [EXIT_REASON_INVLPG] = handle_invlpg,
6387 +- [EXIT_REASON_RDPMC] = handle_rdpmc,
6388 +- [EXIT_REASON_VMCALL] = handle_vmcall,
6389 +- [EXIT_REASON_VMCLEAR] = handle_vmx_instruction,
6390 +- [EXIT_REASON_VMLAUNCH] = handle_vmx_instruction,
6391 +- [EXIT_REASON_VMPTRLD] = handle_vmx_instruction,
6392 +- [EXIT_REASON_VMPTRST] = handle_vmx_instruction,
6393 +- [EXIT_REASON_VMREAD] = handle_vmx_instruction,
6394 +- [EXIT_REASON_VMRESUME] = handle_vmx_instruction,
6395 +- [EXIT_REASON_VMWRITE] = handle_vmx_instruction,
6396 +- [EXIT_REASON_VMOFF] = handle_vmx_instruction,
6397 +- [EXIT_REASON_VMON] = handle_vmx_instruction,
6398 +- [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
6399 +- [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
6400 +- [EXIT_REASON_APIC_WRITE] = handle_apic_write,
6401 +- [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
6402 +- [EXIT_REASON_WBINVD] = handle_wbinvd,
6403 +- [EXIT_REASON_XSETBV] = handle_xsetbv,
6404 +- [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
6405 +- [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
6406 +- [EXIT_REASON_GDTR_IDTR] = handle_desc,
6407 +- [EXIT_REASON_LDTR_TR] = handle_desc,
6408 +- [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
6409 +- [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
6410 +- [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
6411 +- [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
6412 +- [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
6413 +- [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
6414 +- [EXIT_REASON_INVEPT] = handle_vmx_instruction,
6415 +- [EXIT_REASON_INVVPID] = handle_vmx_instruction,
6416 +- [EXIT_REASON_RDRAND] = handle_invalid_op,
6417 +- [EXIT_REASON_RDSEED] = handle_invalid_op,
6418 +- [EXIT_REASON_PML_FULL] = handle_pml_full,
6419 +- [EXIT_REASON_INVPCID] = handle_invpcid,
6420 +- [EXIT_REASON_VMFUNC] = handle_vmx_instruction,
6421 +- [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
6422 +- [EXIT_REASON_ENCLS] = handle_encls,
6423 +-};
6424 +-
6425 +-static const int kvm_vmx_max_exit_handlers =
6426 +- ARRAY_SIZE(kvm_vmx_exit_handlers);
6427 +-
6428 +-static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
6429 +-{
6430 +- *info1 = vmcs_readl(EXIT_QUALIFICATION);
6431 +- *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
6432 +-}
6433 +-
6434 +-static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
6435 +-{
6436 +- if (vmx->pml_pg) {
6437 +- __free_page(vmx->pml_pg);
6438 +- vmx->pml_pg = NULL;
6439 +- }
6440 +-}
6441 +-
6442 +-static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
6443 +-{
6444 +- struct vcpu_vmx *vmx = to_vmx(vcpu);
6445 +- u64 *pml_buf;
6446 +- u16 pml_idx;
6447 +-
6448 +- pml_idx = vmcs_read16(GUEST_PML_INDEX);
6449 +-
6450 +- /* Do nothing if PML buffer is empty */
6451 +- if (pml_idx == (PML_ENTITY_NUM - 1))
6452 +- return;
6453 +-
6454 +- /* PML index always points to next available PML buffer entity */
6455 +- if (pml_idx >= PML_ENTITY_NUM)
6456 +- pml_idx = 0;
6457 +- else
6458 +- pml_idx++;
6459 +-
6460 +- pml_buf = page_address(vmx->pml_pg);
6461 +- for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
6462 +- u64 gpa;
6463 +-
6464 +- gpa = pml_buf[pml_idx];
6465 +- WARN_ON(gpa & (PAGE_SIZE - 1));
6466 +- kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
6467 +- }
6468 +-
6469 +- /* reset PML index */
6470 +- vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
6471 +-}
6472 +-
6473 +-/*
6474 +- * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
6475 +- * Called before reporting dirty_bitmap to userspace.
6476 +- */
6477 +-static void kvm_flush_pml_buffers(struct kvm *kvm)
6478 +-{
6479 +- int i;
6480 +- struct kvm_vcpu *vcpu;
6481 +- /*
6482 +- * We only need to kick vcpu out of guest mode here, as PML buffer
6483 +- * is flushed at beginning of all VMEXITs, and it's obvious that only
6484 +- * vcpus running in guest are possible to have unflushed GPAs in PML
6485 +- * buffer.
6486 +- */
6487 +- kvm_for_each_vcpu(i, vcpu, kvm)
6488 +- kvm_vcpu_kick(vcpu);
6489 +-}
6490 +-
6491 +-static void vmx_dump_sel(char *name, uint32_t sel)
6492 +-{
6493 +- pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
6494 +- name, vmcs_read16(sel),
6495 +- vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
6496 +- vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
6497 +- vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
6498 +-}
6499 +-
6500 +-static void vmx_dump_dtsel(char *name, uint32_t limit)
6501 +-{
6502 +- pr_err("%s limit=0x%08x, base=0x%016lx\n",
6503 +- name, vmcs_read32(limit),
6504 +- vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
6505 +-}
6506 +-
6507 +-void dump_vmcs(void)
6508 +-{
6509 +- u32 vmentry_ctl, vmexit_ctl;
6510 +- u32 cpu_based_exec_ctrl, pin_based_exec_ctrl, secondary_exec_control;
6511 +- unsigned long cr4;
6512 +- u64 efer;
6513 +- int i, n;
6514 +-
6515 +- if (!dump_invalid_vmcs) {
6516 +- pr_warn_ratelimited("set kvm_intel.dump_invalid_vmcs=1 to dump internal KVM state.\n");
6517 +- return;
6518 +- }
6519 +-
6520 +- vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
6521 +- vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
6522 +- cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6523 +- pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
6524 +- cr4 = vmcs_readl(GUEST_CR4);
6525 +- efer = vmcs_read64(GUEST_IA32_EFER);
6526 +- secondary_exec_control = 0;
6527 +- if (cpu_has_secondary_exec_ctrls())
6528 +- secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
6529 +-
6530 +- pr_err("*** Guest State ***\n");
6531 +- pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
6532 +- vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
6533 +- vmcs_readl(CR0_GUEST_HOST_MASK));
6534 +- pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
6535 +- cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
6536 +- pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
6537 +- if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
6538 +- (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
6539 +- {
6540 +- pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
6541 +- vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
6542 +- pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
6543 +- vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
6544 +- }
6545 +- pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
6546 +- vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
6547 +- pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
6548 +- vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
6549 +- pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
6550 +- vmcs_readl(GUEST_SYSENTER_ESP),
6551 +- vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
6552 +- vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
6553 +- vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
6554 +- vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
6555 +- vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
6556 +- vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
6557 +- vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
6558 +- vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
6559 +- vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
6560 +- vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
6561 +- vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
6562 +- if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
6563 +- (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
6564 +- pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
6565 +- efer, vmcs_read64(GUEST_IA32_PAT));
6566 +- pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
6567 +- vmcs_read64(GUEST_IA32_DEBUGCTL),
6568 +- vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
6569 +- if (cpu_has_load_perf_global_ctrl() &&
6570 +- vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
6571 +- pr_err("PerfGlobCtl = 0x%016llx\n",
6572 +- vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
6573 +- if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
6574 +- pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
6575 +- pr_err("Interruptibility = %08x ActivityState = %08x\n",
6576 +- vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
6577 +- vmcs_read32(GUEST_ACTIVITY_STATE));
6578 +- if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
6579 +- pr_err("InterruptStatus = %04x\n",
6580 +- vmcs_read16(GUEST_INTR_STATUS));
6581 +-
6582 +- pr_err("*** Host State ***\n");
6583 +- pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
6584 +- vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
6585 +- pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
6586 +- vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
6587 +- vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
6588 +- vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
6589 +- vmcs_read16(HOST_TR_SELECTOR));
6590 +- pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
6591 +- vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
6592 +- vmcs_readl(HOST_TR_BASE));
6593 +- pr_err("GDTBase=%016lx IDTBase=%016lx\n",
6594 +- vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
6595 +- pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
6596 +- vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
6597 +- vmcs_readl(HOST_CR4));
6598 +- pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
6599 +- vmcs_readl(HOST_IA32_SYSENTER_ESP),
6600 +- vmcs_read32(HOST_IA32_SYSENTER_CS),
6601 +- vmcs_readl(HOST_IA32_SYSENTER_EIP));
6602 +- if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
6603 +- pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
6604 +- vmcs_read64(HOST_IA32_EFER),
6605 +- vmcs_read64(HOST_IA32_PAT));
6606 +- if (cpu_has_load_perf_global_ctrl() &&
6607 +- vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
6608 +- pr_err("PerfGlobCtl = 0x%016llx\n",
6609 +- vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
6610 +-
6611 +- pr_err("*** Control State ***\n");
6612 +- pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
6613 +- pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
6614 +- pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
6615 +- pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
6616 +- vmcs_read32(EXCEPTION_BITMAP),
6617 +- vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
6618 +- vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
6619 +- pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
6620 +- vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
6621 +- vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
6622 +- vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
6623 +- pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
6624 +- vmcs_read32(VM_EXIT_INTR_INFO),
6625 +- vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
6626 +- vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
6627 +- pr_err(" reason=%08x qualification=%016lx\n",
6628 +- vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
6629 +- pr_err("IDTVectoring: info=%08x errcode=%08x\n",
6630 +- vmcs_read32(IDT_VECTORING_INFO_FIELD),
6631 +- vmcs_read32(IDT_VECTORING_ERROR_CODE));
6632 +- pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
6633 +- if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
6634 +- pr_err("TSC Multiplier = 0x%016llx\n",
6635 +- vmcs_read64(TSC_MULTIPLIER));
6636 +- if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW) {
6637 +- if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) {
6638 +- u16 status = vmcs_read16(GUEST_INTR_STATUS);
6639 +- pr_err("SVI|RVI = %02x|%02x ", status >> 8, status & 0xff);
6640 +- }
6641 +- pr_cont("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
6642 +- if (secondary_exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
6643 +- pr_err("APIC-access addr = 0x%016llx ", vmcs_read64(APIC_ACCESS_ADDR));
6644 +- pr_cont("virt-APIC addr = 0x%016llx\n", vmcs_read64(VIRTUAL_APIC_PAGE_ADDR));
6645 +- }
6646 +- if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
6647 +- pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
6648 +- if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
6649 +- pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
6650 +- n = vmcs_read32(CR3_TARGET_COUNT);
6651 +- for (i = 0; i + 1 < n; i += 4)
6652 +- pr_err("CR3 target%u=%016lx target%u=%016lx\n",
6653 +- i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
6654 +- i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
6655 +- if (i < n)
6656 +- pr_err("CR3 target%u=%016lx\n",
6657 +- i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
6658 +- if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
6659 +- pr_err("PLE Gap=%08x Window=%08x\n",
6660 +- vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
6661 +- if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
6662 +- pr_err("Virtual processor ID = 0x%04x\n",
6663 +- vmcs_read16(VIRTUAL_PROCESSOR_ID));
6664 +-}
6665 +-
6666 +-/*
6667 +- * The guest has exited. See if we can fix it or if we need userspace
6668 +- * assistance.
6669 +- */
6670 +-static int vmx_handle_exit(struct kvm_vcpu *vcpu,
6671 +- enum exit_fastpath_completion exit_fastpath)
6672 +-{
6673 +- struct vcpu_vmx *vmx = to_vmx(vcpu);
6674 +- u32 exit_reason = vmx->exit_reason;
6675 +- u32 vectoring_info = vmx->idt_vectoring_info;
6676 +-
6677 +- trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
6678 +-
6679 +- /*
6680 +- * Flush logged GPAs PML buffer, this will make dirty_bitmap more
6681 +- * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
6682 +- * querying dirty_bitmap, we only need to kick all vcpus out of guest
6683 +- * mode as if vcpus is in root mode, the PML buffer must has been
6684 +- * flushed already.
6685 +- */
6686 +- if (enable_pml)
6687 +- vmx_flush_pml_buffer(vcpu);
6688 +-
6689 +- /* If guest state is invalid, start emulating */
6690 +- if (vmx->emulation_required)
6691 +- return handle_invalid_guest_state(vcpu);
6692 +-
6693 +- if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
6694 +- return nested_vmx_reflect_vmexit(vcpu, exit_reason);
6695 +-
6696 +- if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
6697 +- dump_vmcs();
6698 +- vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
6699 +- vcpu->run->fail_entry.hardware_entry_failure_reason
6700 +- = exit_reason;
6701 +- return 0;
6702 +- }
6703 +-
6704 +- if (unlikely(vmx->fail)) {
6705 +- dump_vmcs();
6706 +- vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
6707 +- vcpu->run->fail_entry.hardware_entry_failure_reason
6708 +- = vmcs_read32(VM_INSTRUCTION_ERROR);
6709 +- return 0;
6710 +- }
6711 +-
6712 +- /*
6713 +- * Note:
6714 +- * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
6715 +- * delivery event since it indicates guest is accessing MMIO.
6716 +- * The vm-exit can be triggered again after return to guest that
6717 +- * will cause infinite loop.
6718 +- */
6719 +- if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
6720 +- (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
6721 +- exit_reason != EXIT_REASON_EPT_VIOLATION &&
6722 +- exit_reason != EXIT_REASON_PML_FULL &&
6723 +- exit_reason != EXIT_REASON_TASK_SWITCH)) {
6724 +- vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6725 +- vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
6726 +- vcpu->run->internal.ndata = 3;
6727 +- vcpu->run->internal.data[0] = vectoring_info;
6728 +- vcpu->run->internal.data[1] = exit_reason;
6729 +- vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
6730 +- if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
6731 +- vcpu->run->internal.ndata++;
6732 +- vcpu->run->internal.data[3] =
6733 +- vmcs_read64(GUEST_PHYSICAL_ADDRESS);
6734 +- }
6735 +- return 0;
6736 +- }
6737 +-
6738 +- if (unlikely(!enable_vnmi &&
6739 +- vmx->loaded_vmcs->soft_vnmi_blocked)) {
6740 +- if (vmx_interrupt_allowed(vcpu)) {
6741 +- vmx->loaded_vmcs->soft_vnmi_blocked = 0;
6742 +- } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
6743 +- vcpu->arch.nmi_pending) {
6744 +- /*
6745 +- * This CPU don't support us in finding the end of an
6746 +- * NMI-blocked window if the guest runs with IRQs
6747 +- * disabled. So we pull the trigger after 1 s of
6748 +- * futile waiting, but inform the user about this.
6749 +- */
6750 +- printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
6751 +- "state on VCPU %d after 1 s timeout\n",
6752 +- __func__, vcpu->vcpu_id);
6753 +- vmx->loaded_vmcs->soft_vnmi_blocked = 0;
6754 +- }
6755 +- }
6756 +-
6757 +- if (exit_fastpath == EXIT_FASTPATH_SKIP_EMUL_INS) {
6758 +- kvm_skip_emulated_instruction(vcpu);
6759 +- return 1;
6760 +- } else if (exit_reason < kvm_vmx_max_exit_handlers
6761 +- && kvm_vmx_exit_handlers[exit_reason]) {
6762 +-#ifdef CONFIG_RETPOLINE
6763 +- if (exit_reason == EXIT_REASON_MSR_WRITE)
6764 +- return kvm_emulate_wrmsr(vcpu);
6765 +- else if (exit_reason == EXIT_REASON_PREEMPTION_TIMER)
6766 +- return handle_preemption_timer(vcpu);
6767 +- else if (exit_reason == EXIT_REASON_INTERRUPT_WINDOW)
6768 +- return handle_interrupt_window(vcpu);
6769 +- else if (exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT)
6770 +- return handle_external_interrupt(vcpu);
6771 +- else if (exit_reason == EXIT_REASON_HLT)
6772 +- return kvm_emulate_halt(vcpu);
6773 +- else if (exit_reason == EXIT_REASON_EPT_MISCONFIG)
6774 +- return handle_ept_misconfig(vcpu);
6775 +-#endif
6776 +- return kvm_vmx_exit_handlers[exit_reason](vcpu);
6777 +- } else {
6778 +- vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
6779 +- exit_reason);
6780 +- dump_vmcs();
6781 +- vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6782 +- vcpu->run->internal.suberror =
6783 +- KVM_INTERNAL_ERROR_UNEXPECTED_EXIT_REASON;
6784 +- vcpu->run->internal.ndata = 1;
6785 +- vcpu->run->internal.data[0] = exit_reason;
6786 +- return 0;
6787 +- }
6788 +-}
6789 +-
6790 +-/*
6791 +- * Software based L1D cache flush which is used when microcode providing
6792 +- * the cache control MSR is not loaded.
6793 +- *
6794 +- * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
6795 +- * flush it is required to read in 64 KiB because the replacement algorithm
6796 +- * is not exactly LRU. This could be sized at runtime via topology
6797 +- * information but as all relevant affected CPUs have 32KiB L1D cache size
6798 +- * there is no point in doing so.
6799 +- */
6800 +-static void vmx_l1d_flush(struct kvm_vcpu *vcpu)
6801 +-{
6802 +- int size = PAGE_SIZE << L1D_CACHE_ORDER;
6803 +-
6804 +- /*
6805 +- * This code is only executed when the the flush mode is 'cond' or
6806 +- * 'always'
6807 +- */
6808 +- if (static_branch_likely(&vmx_l1d_flush_cond)) {
6809 +- bool flush_l1d;
6810 +-
6811 +- /*
6812 +- * Clear the per-vcpu flush bit, it gets set again
6813 +- * either from vcpu_run() or from one of the unsafe
6814 +- * VMEXIT handlers.
6815 +- */
6816 +- flush_l1d = vcpu->arch.l1tf_flush_l1d;
6817 +- vcpu->arch.l1tf_flush_l1d = false;
6818 +-
6819 +- /*
6820 +- * Clear the per-cpu flush bit, it gets set again from
6821 +- * the interrupt handlers.
6822 +- */
6823 +- flush_l1d |= kvm_get_cpu_l1tf_flush_l1d();
6824 +- kvm_clear_cpu_l1tf_flush_l1d();
6825 +-
6826 +- if (!flush_l1d)
6827 +- return;
6828 +- }
6829 +-
6830 +- vcpu->stat.l1d_flush++;
6831 +-
6832 +- if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
6833 +- wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
6834 +- return;
6835 +- }
6836 +-
6837 +- asm volatile(
6838 +- /* First ensure the pages are in the TLB */
6839 +- "xorl %%eax, %%eax\n"
6840 +- ".Lpopulate_tlb:\n\t"
6841 +- "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
6842 +- "addl $4096, %%eax\n\t"
6843 +- "cmpl %%eax, %[size]\n\t"
6844 +- "jne .Lpopulate_tlb\n\t"
6845 +- "xorl %%eax, %%eax\n\t"
6846 +- "cpuid\n\t"
6847 +- /* Now fill the cache */
6848 +- "xorl %%eax, %%eax\n"
6849 +- ".Lfill_cache:\n"
6850 +- "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
6851 +- "addl $64, %%eax\n\t"
6852 +- "cmpl %%eax, %[size]\n\t"
6853 +- "jne .Lfill_cache\n\t"
6854 +- "lfence\n"
6855 +- :: [flush_pages] "r" (vmx_l1d_flush_pages),
6856 +- [size] "r" (size)
6857 +- : "eax", "ebx", "ecx", "edx");
6858 +-}
6859 +-
6860 +-static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
6861 +-{
6862 +- struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6863 +- int tpr_threshold;
6864 +-
6865 +- if (is_guest_mode(vcpu) &&
6866 +- nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
6867 +- return;
6868 +-
6869 +- tpr_threshold = (irr == -1 || tpr < irr) ? 0 : irr;
6870 +- if (is_guest_mode(vcpu))
6871 +- to_vmx(vcpu)->nested.l1_tpr_threshold = tpr_threshold;
6872 +- else
6873 +- vmcs_write32(TPR_THRESHOLD, tpr_threshold);
6874 +-}
6875 +-
6876 +-void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
6877 +-{
6878 +- struct vcpu_vmx *vmx = to_vmx(vcpu);
6879 +- u32 sec_exec_control;
6880 +-
6881 +- if (!lapic_in_kernel(vcpu))
6882 +- return;
6883 +-
6884 +- if (!flexpriority_enabled &&
6885 +- !cpu_has_vmx_virtualize_x2apic_mode())
6886 +- return;
6887 +-
6888 +- /* Postpone execution until vmcs01 is the current VMCS. */
6889 +- if (is_guest_mode(vcpu)) {
6890 +- vmx->nested.change_vmcs01_virtual_apic_mode = true;
6891 +- return;
6892 +- }
6893 +-
6894 +- sec_exec_control = secondary_exec_controls_get(vmx);
6895 +- sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
6896 +- SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
6897 +-
6898 +- switch (kvm_get_apic_mode(vcpu)) {
6899 +- case LAPIC_MODE_INVALID:
6900 +- WARN_ONCE(true, "Invalid local APIC state");
6901 +- case LAPIC_MODE_DISABLED:
6902 +- break;
6903 +- case LAPIC_MODE_XAPIC:
6904 +- if (flexpriority_enabled) {
6905 +- sec_exec_control |=
6906 +- SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6907 +- vmx_flush_tlb(vcpu, true);
6908 +- }
6909 +- break;
6910 +- case LAPIC_MODE_X2APIC:
6911 +- if (cpu_has_vmx_virtualize_x2apic_mode())
6912 +- sec_exec_control |=
6913 +- SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
6914 +- break;
6915 +- }
6916 +- secondary_exec_controls_set(vmx, sec_exec_control);
6917 +-
6918 +- vmx_update_msr_bitmap(vcpu);
6919 +-}
6920 +-
6921 +-static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
6922 +-{
6923 +- if (!is_guest_mode(vcpu)) {
6924 +- vmcs_write64(APIC_ACCESS_ADDR, hpa);
6925 +- vmx_flush_tlb(vcpu, true);
6926 +- }
6927 +-}
6928 +-
6929 +-static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
6930 +-{
6931 +- u16 status;
6932 +- u8 old;
6933 +-
6934 +- if (max_isr == -1)
6935 +- max_isr = 0;
6936 +-
6937 +- status = vmcs_read16(GUEST_INTR_STATUS);
6938 +- old = status >> 8;
6939 +- if (max_isr != old) {
6940 +- status &= 0xff;
6941 +- status |= max_isr << 8;
6942 +- vmcs_write16(GUEST_INTR_STATUS, status);
6943 +- }
6944 +-}
6945 +-
6946 +-static void vmx_set_rvi(int vector)
6947 +-{
6948 +- u16 status;
6949 +- u8 old;
6950 +-
6951 +- if (vector == -1)
6952 +- vector = 0;
6953 +-
6954 +- status = vmcs_read16(GUEST_INTR_STATUS);
6955 +- old = (u8)status & 0xff;
6956 +- if ((u8)vector != old) {
6957 +- status &= ~0xff;
6958 +- status |= (u8)vector;
6959 +- vmcs_write16(GUEST_INTR_STATUS, status);
6960 +- }
6961 +-}
6962 +-
6963 +-static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
6964 +-{
6965 +- /*
6966 +- * When running L2, updating RVI is only relevant when
6967 +- * vmcs12 virtual-interrupt-delivery enabled.
6968 +- * However, it can be enabled only when L1 also
6969 +- * intercepts external-interrupts and in that case
6970 +- * we should not update vmcs02 RVI but instead intercept
6971 +- * interrupt. Therefore, do nothing when running L2.
6972 +- */
6973 +- if (!is_guest_mode(vcpu))
6974 +- vmx_set_rvi(max_irr);
6975 +-}
6976 +-
6977 +-static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
6978 +-{
6979 +- struct vcpu_vmx *vmx = to_vmx(vcpu);
6980 +- int max_irr;
6981 +- bool max_irr_updated;
6982 +-
6983 +- WARN_ON(!vcpu->arch.apicv_active);
6984 +- if (pi_test_on(&vmx->pi_desc)) {
6985 +- pi_clear_on(&vmx->pi_desc);
6986 +- /*
6987 +- * IOMMU can write to PID.ON, so the barrier matters even on UP.
6988 +- * But on x86 this is just a compiler barrier anyway.
6989 +- */
6990 +- smp_mb__after_atomic();
6991 +- max_irr_updated =
6992 +- kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
6993 +-
6994 +- /*
6995 +- * If we are running L2 and L1 has a new pending interrupt
6996 +- * which can be injected, we should re-evaluate
6997 +- * what should be done with this new L1 interrupt.
6998 +- * If L1 intercepts external-interrupts, we should
6999 +- * exit from L2 to L1. Otherwise, interrupt should be
7000 +- * delivered directly to L2.
7001 +- */
7002 +- if (is_guest_mode(vcpu) && max_irr_updated) {
7003 +- if (nested_exit_on_intr(vcpu))
7004 +- kvm_vcpu_exiting_guest_mode(vcpu);
7005 +- else
7006 +- kvm_make_request(KVM_REQ_EVENT, vcpu);
7007 +- }
7008 +- } else {
7009 +- max_irr = kvm_lapic_find_highest_irr(vcpu);
7010 +- }
7011 +- vmx_hwapic_irr_update(vcpu, max_irr);
7012 +- return max_irr;
7013 +-}
7014 +-
7015 +-static bool vmx_dy_apicv_has_pending_interrupt(struct kvm_vcpu *vcpu)
7016 +-{
7017 +- struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7018 +-
7019 +- return pi_test_on(pi_desc) ||
7020 +- (pi_test_sn(pi_desc) && !pi_is_pir_empty(pi_desc));
7021 +-}
7022 +-
7023 +-static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
7024 +-{
7025 +- if (!kvm_vcpu_apicv_active(vcpu))
7026 +- return;
7027 +-
7028 +- vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
7029 +- vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
7030 +- vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
7031 +- vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
7032 +-}
7033 +-
7034 +-static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
7035 +-{
7036 +- struct vcpu_vmx *vmx = to_vmx(vcpu);
7037 +-
7038 +- pi_clear_on(&vmx->pi_desc);
7039 +- memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
7040 +-}
7041 +-
7042 +-static void handle_exception_nmi_irqoff(struct vcpu_vmx *vmx)
7043 +-{
7044 +- vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
7045 +-
7046 +- /* if exit due to PF check for async PF */
7047 +- if (is_page_fault(vmx->exit_intr_info))
7048 +- vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
7049 +-
7050 +- /* Handle machine checks before interrupts are enabled */
7051 +- if (is_machine_check(vmx->exit_intr_info))
7052 +- kvm_machine_check();
7053 +-
7054 +- /* We need to handle NMIs before interrupts are enabled */
7055 +- if (is_nmi(vmx->exit_intr_info)) {
7056 +- kvm_before_interrupt(&vmx->vcpu);
7057 +- asm("int $2");
7058 +- kvm_after_interrupt(&vmx->vcpu);
7059 +- }
7060 +-}
7061 +-
7062 +-static void handle_external_interrupt_irqoff(struct kvm_vcpu *vcpu)
7063 +-{
7064 +- unsigned int vector;
7065 +- unsigned long entry;
7066 +-#ifdef CONFIG_X86_64
7067 +- unsigned long tmp;
7068 +-#endif
7069 +- gate_desc *desc;
7070 +- u32 intr_info;
7071 +-
7072 +- intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
7073 +- if (WARN_ONCE(!is_external_intr(intr_info),
7074 +- "KVM: unexpected VM-Exit interrupt info: 0x%x", intr_info))
7075 +- return;
7076 +-
7077 +- vector = intr_info & INTR_INFO_VECTOR_MASK;
7078 +- desc = (gate_desc *)host_idt_base + vector;
7079 +- entry = gate_offset(desc);
7080 +-
7081 +- kvm_before_interrupt(vcpu);
7082 +-
7083 +- asm volatile(
7084 +-#ifdef CONFIG_X86_64
7085 +- "mov %%" _ASM_SP ", %[sp]\n\t"
7086 +- "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
7087 +- "push $%c[ss]\n\t"
7088 +- "push %[sp]\n\t"
7089 +-#endif
7090 +- "pushf\n\t"
7091 +- __ASM_SIZE(push) " $%c[cs]\n\t"
7092 +- CALL_NOSPEC
7093 +- :
7094 +-#ifdef CONFIG_X86_64
7095 +- [sp]"=&r"(tmp),
7096 +-#endif
7097 +- ASM_CALL_CONSTRAINT
7098 +- :
7099 +- THUNK_TARGET(entry),
7100 +- [ss]"i"(__KERNEL_DS),
7101 +- [cs]"i"(__KERNEL_CS)
7102 +- );
7103 +-
7104 +- kvm_after_interrupt(vcpu);
7105 +-}
7106 +-STACK_FRAME_NON_STANDARD(handle_external_interrupt_irqoff);
7107 +-
7108 +-static void vmx_handle_exit_irqoff(struct kvm_vcpu *vcpu,
7109 +- enum exit_fastpath_completion *exit_fastpath)
7110 +-{
7111 +- struct vcpu_vmx *vmx = to_vmx(vcpu);
7112 +-
7113 +- if (vmx->exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT)
7114 +- handle_external_interrupt_irqoff(vcpu);
7115 +- else if (vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI)
7116 +- handle_exception_nmi_irqoff(vmx);
7117 +- else if (!is_guest_mode(vcpu) &&
7118 +- vmx->exit_reason == EXIT_REASON_MSR_WRITE)
7119 +- *exit_fastpath = handle_fastpath_set_msr_irqoff(vcpu);
7120 +-}
7121 +-
7122 +-static bool vmx_has_emulated_msr(int index)
7123 +-{
7124 +- switch (index) {
7125 +- case MSR_IA32_SMBASE:
7126 +- /*
7127 +- * We cannot do SMM unless we can run the guest in big
7128 +- * real mode.
7129 +- */
7130 +- return enable_unrestricted_guest || emulate_invalid_guest_state;
7131 +- case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
7132 +- return nested;
7133 +- case MSR_AMD64_VIRT_SPEC_CTRL:
7134 +- /* This is AMD only. */
7135 +- return false;
7136 +- default:
7137 +- return true;
7138 +- }
7139 +-}
7140 +-
7141 +-static bool vmx_pt_supported(void)
7142 +-{
7143 +- return pt_mode == PT_MODE_HOST_GUEST;
7144 +-}
7145 +-
7146 +-static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
7147 +-{
7148 +- u32 exit_intr_info;
7149 +- bool unblock_nmi;
7150 +- u8 vector;
7151 +- bool idtv_info_valid;
7152 +-
7153 +- idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
7154 +-
7155 +- if (enable_vnmi) {
7156 +- if (vmx->loaded_vmcs->nmi_known_unmasked)
7157 +- return;
7158 +- /*
7159 +- * Can't use vmx->exit_intr_info since we're not sure what
7160 +- * the exit reason is.
7161 +- */
7162 +- exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
7163 +- unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
7164 +- vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
7165 +- /*
7166 +- * SDM 3: 27.7.1.2 (September 2008)
7167 +- * Re-set bit "block by NMI" before VM entry if vmexit caused by
7168 +- * a guest IRET fault.
7169 +- * SDM 3: 23.2.2 (September 2008)
7170 +- * Bit 12 is undefined in any of the following cases:
7171 +- * If the VM exit sets the valid bit in the IDT-vectoring
7172 +- * information field.
7173 +- * If the VM exit is due to a double fault.
7174 +- */
7175 +- if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
7176 +- vector != DF_VECTOR && !idtv_info_valid)
7177 +- vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
7178 +- GUEST_INTR_STATE_NMI);
7179 +- else
7180 +- vmx->loaded_vmcs->nmi_known_unmasked =
7181 +- !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
7182 +- & GUEST_INTR_STATE_NMI);
7183 +- } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
7184 +- vmx->loaded_vmcs->vnmi_blocked_time +=
7185 +- ktime_to_ns(ktime_sub(ktime_get(),
7186 +- vmx->loaded_vmcs->entry_time));
7187 +-}
7188 +-
7189 +-static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
7190 +- u32 idt_vectoring_info,
7191 +- int instr_len_field,
7192 +- int error_code_field)
7193 +-{
7194 +- u8 vector;
7195 +- int type;
7196 +- bool idtv_info_valid;
7197 +-
7198 +- idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
7199 +-
7200 +- vcpu->arch.nmi_injected = false;
7201 +- kvm_clear_exception_queue(vcpu);
7202 +- kvm_clear_interrupt_queue(vcpu);
7203 +-
7204 +- if (!idtv_info_valid)
7205 +- return;
7206 +-
7207 +- kvm_make_request(KVM_REQ_EVENT, vcpu);
7208 +-
7209 +- vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
7210 +- type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
7211 +-
7212 +- switch (type) {
7213 +- case INTR_TYPE_NMI_INTR:
7214 +- vcpu->arch.nmi_injected = true;
7215 +- /*
7216 +- * SDM 3: 27.7.1.2 (September 2008)
7217 +- * Clear bit "block by NMI" before VM entry if a NMI
7218 +- * delivery faulted.
7219 +- */
7220 +- vmx_set_nmi_mask(vcpu, false);
7221 +- break;
7222 +- case INTR_TYPE_SOFT_EXCEPTION:
7223 +- vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
7224 +- /* fall through */
7225 +- case INTR_TYPE_HARD_EXCEPTION:
7226 +- if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
7227 +- u32 err = vmcs_read32(error_code_field);
7228 +- kvm_requeue_exception_e(vcpu, vector, err);
7229 +- } else
7230 +- kvm_requeue_exception(vcpu, vector);
7231 +- break;
7232 +- case INTR_TYPE_SOFT_INTR:
7233 +- vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
7234 +- /* fall through */
7235 +- case INTR_TYPE_EXT_INTR:
7236 +- kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
7237 +- break;
7238 +- default:
7239 +- break;
7240 +- }
7241 +-}
7242 +-
7243 +-static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
7244 +-{
7245 +- __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
7246 +- VM_EXIT_INSTRUCTION_LEN,
7247 +- IDT_VECTORING_ERROR_CODE);
7248 +-}
7249 +-
7250 +-static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
7251 +-{
7252 +- __vmx_complete_interrupts(vcpu,
7253 +- vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
7254 +- VM_ENTRY_INSTRUCTION_LEN,
7255 +- VM_ENTRY_EXCEPTION_ERROR_CODE);
7256 +-
7257 +- vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
7258 +-}
7259 +-
7260 +-static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
7261 +-{
7262 +- int i, nr_msrs;
7263 +- struct perf_guest_switch_msr *msrs;
7264 +-
7265 +- msrs = perf_guest_get_msrs(&nr_msrs);
7266 +-
7267 +- if (!msrs)
7268 +- return;
7269 +-
7270 +- for (i = 0; i < nr_msrs; i++)
7271 +- if (msrs[i].host == msrs[i].guest)
7272 +- clear_atomic_switch_msr(vmx, msrs[i].msr);
7273 +- else
7274 +- add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
7275 +- msrs[i].host, false);
7276 +-}
7277 +-
7278 +-static void atomic_switch_umwait_control_msr(struct vcpu_vmx *vmx)
7279 +-{
7280 +- u32 host_umwait_control;
7281 +-
7282 +- if (!vmx_has_waitpkg(vmx))
7283 +- return;
7284 +-
7285 +- host_umwait_control = get_umwait_control_msr();
7286 +-
7287 +- if (vmx->msr_ia32_umwait_control != host_umwait_control)
7288 +- add_atomic_switch_msr(vmx, MSR_IA32_UMWAIT_CONTROL,
7289 +- vmx->msr_ia32_umwait_control,
7290 +- host_umwait_control, false);
7291 +- else
7292 +- clear_atomic_switch_msr(vmx, MSR_IA32_UMWAIT_CONTROL);
7293 +-}
7294 +-
7295 +-static void vmx_update_hv_timer(struct kvm_vcpu *vcpu)
7296 +-{
7297 +- struct vcpu_vmx *vmx = to_vmx(vcpu);
7298 +- u64 tscl;
7299 +- u32 delta_tsc;
7300 +-
7301 +- if (vmx->req_immediate_exit) {
7302 +- vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, 0);
7303 +- vmx->loaded_vmcs->hv_timer_soft_disabled = false;
7304 +- } else if (vmx->hv_deadline_tsc != -1) {
7305 +- tscl = rdtsc();
7306 +- if (vmx->hv_deadline_tsc > tscl)
7307 +- /* set_hv_timer ensures the delta fits in 32-bits */
7308 +- delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
7309 +- cpu_preemption_timer_multi);
7310 +- else
7311 +- delta_tsc = 0;
7312 +-
7313 +- vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
7314 +- vmx->loaded_vmcs->hv_timer_soft_disabled = false;
7315 +- } else if (!vmx->loaded_vmcs->hv_timer_soft_disabled) {
7316 +- vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, -1);
7317 +- vmx->loaded_vmcs->hv_timer_soft_disabled = true;
7318 +- }
7319 +-}
7320 +-
7321 +-void vmx_update_host_rsp(struct vcpu_vmx *vmx, unsigned long host_rsp)
7322 +-{
7323 +- if (unlikely(host_rsp != vmx->loaded_vmcs->host_state.rsp)) {
7324 +- vmx->loaded_vmcs->host_state.rsp = host_rsp;
7325 +- vmcs_writel(HOST_RSP, host_rsp);
7326 +- }
7327 +-}
7328 +-
7329 +-bool __vmx_vcpu_run(struct vcpu_vmx *vmx, unsigned long *regs, bool launched);
7330 +-
7331 +-static void vmx_vcpu_run(struct kvm_vcpu *vcpu)
7332 +-{
7333 +- struct vcpu_vmx *vmx = to_vmx(vcpu);
7334 +- unsigned long cr3, cr4;
7335 +-
7336 +- /* Record the guest's net vcpu time for enforced NMI injections. */
7337 +- if (unlikely(!enable_vnmi &&
7338 +- vmx->loaded_vmcs->soft_vnmi_blocked))
7339 +- vmx->loaded_vmcs->entry_time = ktime_get();
7340 +-
7341 +- /* Don't enter VMX if guest state is invalid, let the exit handler
7342 +- start emulation until we arrive back to a valid state */
7343 +- if (vmx->emulation_required)
7344 +- return;
7345 +-
7346 +- if (vmx->ple_window_dirty) {
7347 +- vmx->ple_window_dirty = false;
7348 +- vmcs_write32(PLE_WINDOW, vmx->ple_window);
7349 +- }
7350 +-
7351 +- if (vmx->nested.need_vmcs12_to_shadow_sync)
7352 +- nested_sync_vmcs12_to_shadow(vcpu);
7353 +-
7354 +- if (kvm_register_is_dirty(vcpu, VCPU_REGS_RSP))
7355 +- vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
7356 +- if (kvm_register_is_dirty(vcpu, VCPU_REGS_RIP))
7357 +- vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
7358 +-
7359 +- cr3 = __get_current_cr3_fast();
7360 +- if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
7361 +- vmcs_writel(HOST_CR3, cr3);
7362 +- vmx->loaded_vmcs->host_state.cr3 = cr3;
7363 +- }
7364 +-
7365 +- cr4 = cr4_read_shadow();
7366 +- if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
7367 +- vmcs_writel(HOST_CR4, cr4);
7368 +- vmx->loaded_vmcs->host_state.cr4 = cr4;
7369 +- }
7370 +-
7371 +- /* When single-stepping over STI and MOV SS, we must clear the
7372 +- * corresponding interruptibility bits in the guest state. Otherwise
7373 +- * vmentry fails as it then expects bit 14 (BS) in pending debug
7374 +- * exceptions being set, but that's not correct for the guest debugging
7375 +- * case. */
7376 +- if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
7377 +- vmx_set_interrupt_shadow(vcpu, 0);
7378 +-
7379 +- kvm_load_guest_xsave_state(vcpu);
7380 +-
7381 +- if (static_cpu_has(X86_FEATURE_PKU) &&
7382 +- kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
7383 +- vcpu->arch.pkru != vmx->host_pkru)
7384 +- __write_pkru(vcpu->arch.pkru);
7385 +-
7386 +- pt_guest_enter(vmx);
7387 +-
7388 +- atomic_switch_perf_msrs(vmx);
7389 +- atomic_switch_umwait_control_msr(vmx);
7390 +-
7391 +- if (enable_preemption_timer)
7392 +- vmx_update_hv_timer(vcpu);
7393 +-
7394 +- if (lapic_in_kernel(vcpu) &&
7395 +- vcpu->arch.apic->lapic_timer.timer_advance_ns)
7396 +- kvm_wait_lapic_expire(vcpu);
7397 +-
7398 +- /*
7399 +- * If this vCPU has touched SPEC_CTRL, restore the guest's value if
7400 +- * it's non-zero. Since vmentry is serialising on affected CPUs, there
7401 +- * is no need to worry about the conditional branch over the wrmsr
7402 +- * being speculatively taken.
7403 +- */
7404 +- x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
7405 +-
7406 +- /* L1D Flush includes CPU buffer clear to mitigate MDS */
7407 +- if (static_branch_unlikely(&vmx_l1d_should_flush))
7408 +- vmx_l1d_flush(vcpu);
7409 +- else if (static_branch_unlikely(&mds_user_clear))
7410 +- mds_clear_cpu_buffers();
7411 +-
7412 +- if (vcpu->arch.cr2 != read_cr2())
7413 +- write_cr2(vcpu->arch.cr2);
7414 +-
7415 +- vmx->fail = __vmx_vcpu_run(vmx, (unsigned long *)&vcpu->arch.regs,
7416 +- vmx->loaded_vmcs->launched);
7417 +-
7418 +- vcpu->arch.cr2 = read_cr2();
7419 +-
7420 +- /*
7421 +- * We do not use IBRS in the kernel. If this vCPU has used the
7422 +- * SPEC_CTRL MSR it may have left it on; save the value and
7423 +- * turn it off. This is much more efficient than blindly adding
7424 +- * it to the atomic save/restore list. Especially as the former
7425 +- * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
7426 +- *
7427 +- * For non-nested case:
7428 +- * If the L01 MSR bitmap does not intercept the MSR, then we need to
7429 +- * save it.
7430 +- *
7431 +- * For nested case:
7432 +- * If the L02 MSR bitmap does not intercept the MSR, then we need to
7433 +- * save it.
7434 +- */
7435 +- if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
7436 +- vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
7437 +-
7438 +- x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
7439 +-
7440 +- /* All fields are clean at this point */
7441 +- if (static_branch_unlikely(&enable_evmcs))
7442 +- current_evmcs->hv_clean_fields |=
7443 +- HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
7444 +-
7445 +- if (static_branch_unlikely(&enable_evmcs))
7446 +- current_evmcs->hv_vp_id = vcpu->arch.hyperv.vp_index;
7447 +-
7448 +- /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
7449 +- if (vmx->host_debugctlmsr)
7450 +- update_debugctlmsr(vmx->host_debugctlmsr);
7451 +-
7452 +-#ifndef CONFIG_X86_64
7453 +- /*
7454 +- * The sysexit path does not restore ds/es, so we must set them to
7455 +- * a reasonable value ourselves.
7456 +- *
7457 +- * We can't defer this to vmx_prepare_switch_to_host() since that
7458 +- * function may be executed in interrupt context, which saves and
7459 +- * restore segments around it, nullifying its effect.
7460 +- */
7461 +- loadsegment(ds, __USER_DS);
7462 +- loadsegment(es, __USER_DS);
7463 +-#endif
7464 +-
7465 +- vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
7466 +- | (1 << VCPU_EXREG_RFLAGS)
7467 +- | (1 << VCPU_EXREG_PDPTR)
7468 +- | (1 << VCPU_EXREG_SEGMENTS)
7469 +- | (1 << VCPU_EXREG_CR3));
7470 +- vcpu->arch.regs_dirty = 0;
7471 +-
7472 +- pt_guest_exit(vmx);
7473 +-
7474 +- /*
7475 +- * eager fpu is enabled if PKEY is supported and CR4 is switched
7476 +- * back on host, so it is safe to read guest PKRU from current
7477 +- * XSAVE.
7478 +- */
7479 +- if (static_cpu_has(X86_FEATURE_PKU) &&
7480 +- kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
7481 +- vcpu->arch.pkru = rdpkru();
7482 +- if (vcpu->arch.pkru != vmx->host_pkru)
7483 +- __write_pkru(vmx->host_pkru);
7484 +- }
7485 +-
7486 +- kvm_load_host_xsave_state(vcpu);
7487 +-
7488 +- vmx->nested.nested_run_pending = 0;
7489 +- vmx->idt_vectoring_info = 0;
7490 +-
7491 +- vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
7492 +- if ((u16)vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY)
7493 +- kvm_machine_check();
7494 +-
7495 +- if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
7496 +- return;
7497 +-
7498 +- vmx->loaded_vmcs->launched = 1;
7499 +- vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
7500 +-
7501 +- vmx_recover_nmi_blocking(vmx);
7502 +- vmx_complete_interrupts(vmx);
7503 +-}
7504 +-
7505 +-static struct kvm *vmx_vm_alloc(void)
7506 +-{
7507 +- struct kvm_vmx *kvm_vmx = __vmalloc(sizeof(struct kvm_vmx),
7508 +- GFP_KERNEL_ACCOUNT | __GFP_ZERO,
7509 +- PAGE_KERNEL);
7510 +- return &kvm_vmx->kvm;
7511 +-}
7512 +-
7513 +-static void vmx_vm_free(struct kvm *kvm)
7514 +-{
7515 +- kfree(kvm->arch.hyperv.hv_pa_pg);
7516 +- vfree(to_kvm_vmx(kvm));
7517 +-}
7518 +-
7519 +-static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
7520 +-{
7521 +- struct vcpu_vmx *vmx = to_vmx(vcpu);
7522 +-
7523 +- if (enable_pml)
7524 +- vmx_destroy_pml_buffer(vmx);
7525 +- free_vpid(vmx->vpid);
7526 +- nested_vmx_free_vcpu(vcpu);
7527 +- free_loaded_vmcs(vmx->loaded_vmcs);
7528 +- kvm_vcpu_uninit(vcpu);
7529 +- kmem_cache_free(x86_fpu_cache, vmx->vcpu.arch.user_fpu);
7530 +- kmem_cache_free(x86_fpu_cache, vmx->vcpu.arch.guest_fpu);
7531 +- kmem_cache_free(kvm_vcpu_cache, vmx);
7532 +-}
7533 +-
7534 +-static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
7535 +-{
7536 +- int err;
7537 +- struct vcpu_vmx *vmx;
7538 +- unsigned long *msr_bitmap;
7539 +- int i, cpu;
7540 +-
7541 +- BUILD_BUG_ON_MSG(offsetof(struct vcpu_vmx, vcpu) != 0,
7542 +- "struct kvm_vcpu must be at offset 0 for arch usercopy region");
7543 +-
7544 +- vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL_ACCOUNT);
7545 +- if (!vmx)
7546 +- return ERR_PTR(-ENOMEM);
7547 +-
7548 +- vmx->vcpu.arch.user_fpu = kmem_cache_zalloc(x86_fpu_cache,
7549 +- GFP_KERNEL_ACCOUNT);
7550 +- if (!vmx->vcpu.arch.user_fpu) {
7551 +- printk(KERN_ERR "kvm: failed to allocate kvm userspace's fpu\n");
7552 +- err = -ENOMEM;
7553 +- goto free_partial_vcpu;
7554 +- }
7555 +-
7556 +- vmx->vcpu.arch.guest_fpu = kmem_cache_zalloc(x86_fpu_cache,
7557 +- GFP_KERNEL_ACCOUNT);
7558 +- if (!vmx->vcpu.arch.guest_fpu) {
7559 +- printk(KERN_ERR "kvm: failed to allocate vcpu's fpu\n");
7560 +- err = -ENOMEM;
7561 +- goto free_user_fpu;
7562 +- }
7563 +-
7564 +- vmx->vpid = allocate_vpid();
7565 +-
7566 +- err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
7567 +- if (err)
7568 +- goto free_vcpu;
7569 +-
7570 +- err = -ENOMEM;
7571 +-
7572 +- /*
7573 +- * If PML is turned on, failure on enabling PML just results in failure
7574 +- * of creating the vcpu, therefore we can simplify PML logic (by
7575 +- * avoiding dealing with cases, such as enabling PML partially on vcpus
7576 +- * for the guest), etc.
7577 +- */
7578 +- if (enable_pml) {
7579 +- vmx->pml_pg = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
7580 +- if (!vmx->pml_pg)
7581 +- goto uninit_vcpu;
7582 +- }
7583 +-
7584 +- BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) != NR_SHARED_MSRS);
7585 +-
7586 +- for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
7587 +- u32 index = vmx_msr_index[i];
7588 +- u32 data_low, data_high;
7589 +- int j = vmx->nmsrs;
7590 +-
7591 +- if (rdmsr_safe(index, &data_low, &data_high) < 0)
7592 +- continue;
7593 +- if (wrmsr_safe(index, data_low, data_high) < 0)
7594 +- continue;
7595 +-
7596 +- vmx->guest_msrs[j].index = i;
7597 +- vmx->guest_msrs[j].data = 0;
7598 +- switch (index) {
7599 +- case MSR_IA32_TSX_CTRL:
7600 +- /*
7601 +- * No need to pass TSX_CTRL_CPUID_CLEAR through, so
7602 +- * let's avoid changing CPUID bits under the host
7603 +- * kernel's feet.
7604 +- */
7605 +- vmx->guest_msrs[j].mask = ~(u64)TSX_CTRL_CPUID_CLEAR;
7606 +- break;
7607 +- default:
7608 +- vmx->guest_msrs[j].mask = -1ull;
7609 +- break;
7610 +- }
7611 +- ++vmx->nmsrs;
7612 +- }
7613 +-
7614 +- err = alloc_loaded_vmcs(&vmx->vmcs01);
7615 +- if (err < 0)
7616 +- goto free_pml;
7617 +-
7618 +- msr_bitmap = vmx->vmcs01.msr_bitmap;
7619 +- vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_TSC, MSR_TYPE_R);
7620 +- vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
7621 +- vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
7622 +- vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
7623 +- vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
7624 +- vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
7625 +- vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
7626 +- if (kvm_cstate_in_guest(kvm)) {
7627 +- vmx_disable_intercept_for_msr(msr_bitmap, MSR_CORE_C1_RES, MSR_TYPE_R);
7628 +- vmx_disable_intercept_for_msr(msr_bitmap, MSR_CORE_C3_RESIDENCY, MSR_TYPE_R);
7629 +- vmx_disable_intercept_for_msr(msr_bitmap, MSR_CORE_C6_RESIDENCY, MSR_TYPE_R);
7630 +- vmx_disable_intercept_for_msr(msr_bitmap, MSR_CORE_C7_RESIDENCY, MSR_TYPE_R);
7631 +- }
7632 +- vmx->msr_bitmap_mode = 0;
7633 +-
7634 +- vmx->loaded_vmcs = &vmx->vmcs01;
7635 +- cpu = get_cpu();
7636 +- vmx_vcpu_load(&vmx->vcpu, cpu);
7637 +- vmx->vcpu.cpu = cpu;
7638 +- init_vmcs(vmx);
7639 +- vmx_vcpu_put(&vmx->vcpu);
7640 +- put_cpu();
7641 +- if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
7642 +- err = alloc_apic_access_page(kvm);
7643 +- if (err)
7644 +- goto free_vmcs;
7645 +- }
7646 +-
7647 +- if (enable_ept && !enable_unrestricted_guest) {
7648 +- err = init_rmode_identity_map(kvm);
7649 +- if (err)
7650 +- goto free_vmcs;
7651 +- }
7652 +-
7653 +- if (nested)
7654 +- nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
7655 +- vmx_capability.ept,
7656 +- kvm_vcpu_apicv_active(&vmx->vcpu));
7657 +- else
7658 +- memset(&vmx->nested.msrs, 0, sizeof(vmx->nested.msrs));
7659 +-
7660 +- vmx->nested.posted_intr_nv = -1;
7661 +- vmx->nested.current_vmptr = -1ull;
7662 +-
7663 +- vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
7664 +-
7665 +- /*
7666 +- * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
7667 +- * or POSTED_INTR_WAKEUP_VECTOR.
7668 +- */
7669 +- vmx->pi_desc.nv = POSTED_INTR_VECTOR;
7670 +- vmx->pi_desc.sn = 1;
7671 +-
7672 +- vmx->ept_pointer = INVALID_PAGE;
7673 +-
7674 +- return &vmx->vcpu;
7675 +-
7676 +-free_vmcs:
7677 +- free_loaded_vmcs(vmx->loaded_vmcs);
7678 +-free_pml:
7679 +- vmx_destroy_pml_buffer(vmx);
7680 +-uninit_vcpu:
7681 +- kvm_vcpu_uninit(&vmx->vcpu);
7682 +-free_vcpu:
7683 +- free_vpid(vmx->vpid);
7684 +- kmem_cache_free(x86_fpu_cache, vmx->vcpu.arch.guest_fpu);
7685 +-free_user_fpu:
7686 +- kmem_cache_free(x86_fpu_cache, vmx->vcpu.arch.user_fpu);
7687 +-free_partial_vcpu:
7688 +- kmem_cache_free(kvm_vcpu_cache, vmx);
7689 +- return ERR_PTR(err);
7690 +-}
7691 +-
7692 +-#define L1TF_MSG_SMT "L1TF CPU bug present and SMT on, data leak possible. See CVE-2018-3646 and https://www.kernel.org/doc/html/latest/admin-guide/hw-vuln/l1tf.html for details.\n"
7693 +-#define L1TF_MSG_L1D "L1TF CPU bug present and virtualization mitigation disabled, data leak possible. See CVE-2018-3646 and https://www.kernel.org/doc/html/latest/admin-guide/hw-vuln/l1tf.html for details.\n"
7694 +-
7695 +-static int vmx_vm_init(struct kvm *kvm)
7696 +-{
7697 +- spin_lock_init(&to_kvm_vmx(kvm)->ept_pointer_lock);
7698 +-
7699 +- if (!ple_gap)
7700 +- kvm->arch.pause_in_guest = true;
7701 +-
7702 +- if (boot_cpu_has(X86_BUG_L1TF) && enable_ept) {
7703 +- switch (l1tf_mitigation) {
7704 +- case L1TF_MITIGATION_OFF:
7705 +- case L1TF_MITIGATION_FLUSH_NOWARN:
7706 +- /* 'I explicitly don't care' is set */
7707 +- break;
7708 +- case L1TF_MITIGATION_FLUSH:
7709 +- case L1TF_MITIGATION_FLUSH_NOSMT:
7710 +- case L1TF_MITIGATION_FULL:
7711 +- /*
7712 +- * Warn upon starting the first VM in a potentially
7713 +- * insecure environment.
7714 +- */
7715 +- if (sched_smt_active())
7716 +- pr_warn_once(L1TF_MSG_SMT);
7717 +- if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER)
7718 +- pr_warn_once(L1TF_MSG_L1D);
7719 +- break;
7720 +- case L1TF_MITIGATION_FULL_FORCE:
7721 +- /* Flush is enforced */
7722 +- break;
7723 +- }
7724 +- }
7725 +- return 0;
7726 +-}
7727 +-
7728 +-static int __init vmx_check_processor_compat(void)
7729 +-{
7730 +- struct vmcs_config vmcs_conf;
7731 +- struct vmx_capability vmx_cap;
7732 +-
7733 +- if (setup_vmcs_config(&vmcs_conf, &vmx_cap) < 0)
7734 +- return -EIO;
7735 +- if (nested)
7736 +- nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, vmx_cap.ept,
7737 +- enable_apicv);
7738 +- if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
7739 +- printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
7740 +- smp_processor_id());
7741 +- return -EIO;
7742 +- }
7743 +- return 0;
7744 +-}
7745 +-
7746 +-static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
7747 +-{
7748 +- u8 cache;
7749 +- u64 ipat = 0;
7750 +-
7751 +- /* For VT-d and EPT combination
7752 +- * 1. MMIO: always map as UC
7753 +- * 2. EPT with VT-d:
7754 +- * a. VT-d without snooping control feature: can't guarantee the
7755 +- * result, try to trust guest.
7756 +- * b. VT-d with snooping control feature: snooping control feature of
7757 +- * VT-d engine can guarantee the cache correctness. Just set it
7758 +- * to WB to keep consistent with host. So the same as item 3.
7759 +- * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
7760 +- * consistent with host MTRR
7761 +- */
7762 +- if (is_mmio) {
7763 +- cache = MTRR_TYPE_UNCACHABLE;
7764 +- goto exit;
7765 +- }
7766 +-
7767 +- if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
7768 +- ipat = VMX_EPT_IPAT_BIT;
7769 +- cache = MTRR_TYPE_WRBACK;
7770 +- goto exit;
7771 +- }
7772 +-
7773 +- if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
7774 +- ipat = VMX_EPT_IPAT_BIT;
7775 +- if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
7776 +- cache = MTRR_TYPE_WRBACK;
7777 +- else
7778 +- cache = MTRR_TYPE_UNCACHABLE;
7779 +- goto exit;
7780 +- }
7781 +-
7782 +- cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
7783 +-
7784 +-exit:
7785 +- return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
7786 +-}
7787 +-
7788 +-static int vmx_get_lpage_level(void)
7789 +-{
7790 +- if (enable_ept && !cpu_has_vmx_ept_1g_page())
7791 +- return PT_DIRECTORY_LEVEL;
7792 +- else
7793 +- /* For shadow and EPT supported 1GB page */
7794 +- return PT_PDPE_LEVEL;
7795 +-}
7796 +-
7797 +-static void vmcs_set_secondary_exec_control(struct vcpu_vmx *vmx)
7798 +-{
7799 +- /*
7800 +- * These bits in the secondary execution controls field
7801 +- * are dynamic, the others are mostly based on the hypervisor
7802 +- * architecture and the guest's CPUID. Do not touch the
7803 +- * dynamic bits.
7804 +- */
7805 +- u32 mask =
7806 +- SECONDARY_EXEC_SHADOW_VMCS |
7807 +- SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
7808 +- SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
7809 +- SECONDARY_EXEC_DESC;
7810 +-
7811 +- u32 new_ctl = vmx->secondary_exec_control;
7812 +- u32 cur_ctl = secondary_exec_controls_get(vmx);
7813 +-
7814 +- secondary_exec_controls_set(vmx, (new_ctl & ~mask) | (cur_ctl & mask));
7815 +-}
7816 +-
7817 +-/*
7818 +- * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
7819 +- * (indicating "allowed-1") if they are supported in the guest's CPUID.
7820 +- */
7821 +-static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
7822 +-{
7823 +- struct vcpu_vmx *vmx = to_vmx(vcpu);
7824 +- struct kvm_cpuid_entry2 *entry;
7825 +-
7826 +- vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
7827 +- vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
7828 +-
7829 +-#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
7830 +- if (entry && (entry->_reg & (_cpuid_mask))) \
7831 +- vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
7832 +-} while (0)
7833 +-
7834 +- entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
7835 +- cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
7836 +- cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
7837 +- cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
7838 +- cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
7839 +- cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
7840 +- cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
7841 +- cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
7842 +- cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
7843 +- cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
7844 +- cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
7845 +- cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
7846 +- cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
7847 +- cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
7848 +- cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
7849 +-
7850 +- entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
7851 +- cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
7852 +- cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
7853 +- cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
7854 +- cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
7855 +- cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
7856 +- cr4_fixed1_update(X86_CR4_LA57, ecx, bit(X86_FEATURE_LA57));
7857 +-
7858 +-#undef cr4_fixed1_update
7859 +-}
7860 +-
7861 +-static void nested_vmx_entry_exit_ctls_update(struct kvm_vcpu *vcpu)
7862 +-{
7863 +- struct vcpu_vmx *vmx = to_vmx(vcpu);
7864 +-
7865 +- if (kvm_mpx_supported()) {
7866 +- bool mpx_enabled = guest_cpuid_has(vcpu, X86_FEATURE_MPX);
7867 +-
7868 +- if (mpx_enabled) {
7869 +- vmx->nested.msrs.entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
7870 +- vmx->nested.msrs.exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
7871 +- } else {
7872 +- vmx->nested.msrs.entry_ctls_high &= ~VM_ENTRY_LOAD_BNDCFGS;
7873 +- vmx->nested.msrs.exit_ctls_high &= ~VM_EXIT_CLEAR_BNDCFGS;
7874 +- }
7875 +- }
7876 +-}
7877 +-
7878 +-static void update_intel_pt_cfg(struct kvm_vcpu *vcpu)
7879 +-{
7880 +- struct vcpu_vmx *vmx = to_vmx(vcpu);
7881 +- struct kvm_cpuid_entry2 *best = NULL;
7882 +- int i;
7883 +-
7884 +- for (i = 0; i < PT_CPUID_LEAVES; i++) {
7885 +- best = kvm_find_cpuid_entry(vcpu, 0x14, i);
7886 +- if (!best)
7887 +- return;
7888 +- vmx->pt_desc.caps[CPUID_EAX + i*PT_CPUID_REGS_NUM] = best->eax;
7889 +- vmx->pt_desc.caps[CPUID_EBX + i*PT_CPUID_REGS_NUM] = best->ebx;
7890 +- vmx->pt_desc.caps[CPUID_ECX + i*PT_CPUID_REGS_NUM] = best->ecx;
7891 +- vmx->pt_desc.caps[CPUID_EDX + i*PT_CPUID_REGS_NUM] = best->edx;
7892 +- }
7893 +-
7894 +- /* Get the number of configurable Address Ranges for filtering */
7895 +- vmx->pt_desc.addr_range = intel_pt_validate_cap(vmx->pt_desc.caps,
7896 +- PT_CAP_num_address_ranges);
7897 +-
7898 +- /* Initialize and clear the no dependency bits */
7899 +- vmx->pt_desc.ctl_bitmask = ~(RTIT_CTL_TRACEEN | RTIT_CTL_OS |
7900 +- RTIT_CTL_USR | RTIT_CTL_TSC_EN | RTIT_CTL_DISRETC);
7901 +-
7902 +- /*
7903 +- * If CPUID.(EAX=14H,ECX=0):EBX[0]=1 CR3Filter can be set otherwise
7904 +- * will inject an #GP
7905 +- */
7906 +- if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_cr3_filtering))
7907 +- vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_CR3EN;
7908 +-
7909 +- /*
7910 +- * If CPUID.(EAX=14H,ECX=0):EBX[1]=1 CYCEn, CycThresh and
7911 +- * PSBFreq can be set
7912 +- */
7913 +- if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_psb_cyc))
7914 +- vmx->pt_desc.ctl_bitmask &= ~(RTIT_CTL_CYCLEACC |
7915 +- RTIT_CTL_CYC_THRESH | RTIT_CTL_PSB_FREQ);
7916 +-
7917 +- /*
7918 +- * If CPUID.(EAX=14H,ECX=0):EBX[3]=1 MTCEn BranchEn and
7919 +- * MTCFreq can be set
7920 +- */
7921 +- if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_mtc))
7922 +- vmx->pt_desc.ctl_bitmask &= ~(RTIT_CTL_MTC_EN |
7923 +- RTIT_CTL_BRANCH_EN | RTIT_CTL_MTC_RANGE);
7924 +-
7925 +- /* If CPUID.(EAX=14H,ECX=0):EBX[4]=1 FUPonPTW and PTWEn can be set */
7926 +- if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_ptwrite))
7927 +- vmx->pt_desc.ctl_bitmask &= ~(RTIT_CTL_FUP_ON_PTW |
7928 +- RTIT_CTL_PTW_EN);
7929 +-
7930 +- /* If CPUID.(EAX=14H,ECX=0):EBX[5]=1 PwrEvEn can be set */
7931 +- if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_power_event_trace))
7932 +- vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_PWR_EVT_EN;
7933 +-
7934 +- /* If CPUID.(EAX=14H,ECX=0):ECX[0]=1 ToPA can be set */
7935 +- if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_topa_output))
7936 +- vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_TOPA;
7937 +-
7938 +- /* If CPUID.(EAX=14H,ECX=0):ECX[3]=1 FabircEn can be set */
7939 +- if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_output_subsys))
7940 +- vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_FABRIC_EN;
7941 +-
7942 +- /* unmask address range configure area */
7943 +- for (i = 0; i < vmx->pt_desc.addr_range; i++)
7944 +- vmx->pt_desc.ctl_bitmask &= ~(0xfULL << (32 + i * 4));
7945 +-}
7946 +-
7947 +-static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
7948 +-{
7949 +- struct vcpu_vmx *vmx = to_vmx(vcpu);
7950 +-
7951 +- /* xsaves_enabled is recomputed in vmx_compute_secondary_exec_control(). */
7952 +- vcpu->arch.xsaves_enabled = false;
7953 +-
7954 +- if (cpu_has_secondary_exec_ctrls()) {
7955 +- vmx_compute_secondary_exec_control(vmx);
7956 +- vmcs_set_secondary_exec_control(vmx);
7957 +- }
7958 +-
7959 +- if (nested_vmx_allowed(vcpu))
7960 +- to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
7961 +- FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX |
7962 +- FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
7963 +- else
7964 +- to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
7965 +- ~(FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX |
7966 +- FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX);
7967 +-
7968 +- if (nested_vmx_allowed(vcpu)) {
7969 +- nested_vmx_cr_fixed1_bits_update(vcpu);
7970 +- nested_vmx_entry_exit_ctls_update(vcpu);
7971 +- }
7972 +-
7973 +- if (boot_cpu_has(X86_FEATURE_INTEL_PT) &&
7974 +- guest_cpuid_has(vcpu, X86_FEATURE_INTEL_PT))
7975 +- update_intel_pt_cfg(vcpu);
7976 +-
7977 +- if (boot_cpu_has(X86_FEATURE_RTM)) {
7978 +- struct shared_msr_entry *msr;
7979 +- msr = find_msr_entry(vmx, MSR_IA32_TSX_CTRL);
7980 +- if (msr) {
7981 +- bool enabled = guest_cpuid_has(vcpu, X86_FEATURE_RTM);
7982 +- vmx_set_guest_msr(vmx, msr, enabled ? 0 : TSX_CTRL_RTM_DISABLE);
7983 +- }
7984 +- }
7985 +-}
7986 +-
7987 +-static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
7988 +-{
7989 +- if (func == 1 && nested)
7990 +- entry->ecx |= bit(X86_FEATURE_VMX);
7991 +-}
7992 +-
7993 +-static void vmx_request_immediate_exit(struct kvm_vcpu *vcpu)
7994 +-{
7995 +- to_vmx(vcpu)->req_immediate_exit = true;
7996 +-}
7997 +-
7998 +-static int vmx_check_intercept(struct kvm_vcpu *vcpu,
7999 +- struct x86_instruction_info *info,
8000 +- enum x86_intercept_stage stage)
8001 +-{
8002 +- struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8003 +- struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
8004 +-
8005 +- /*
8006 +- * RDPID causes #UD if disabled through secondary execution controls.
8007 +- * Because it is marked as EmulateOnUD, we need to intercept it here.
8008 +- */
8009 +- if (info->intercept == x86_intercept_rdtscp &&
8010 +- !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
8011 +- ctxt->exception.vector = UD_VECTOR;
8012 +- ctxt->exception.error_code_valid = false;
8013 +- return X86EMUL_PROPAGATE_FAULT;
8014 +- }
8015 +-
8016 +- /* TODO: check more intercepts... */
8017 +- return X86EMUL_CONTINUE;
8018 +-}
8019 +-
8020 +-#ifdef CONFIG_X86_64
8021 +-/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
8022 +-static inline int u64_shl_div_u64(u64 a, unsigned int shift,
8023 +- u64 divisor, u64 *result)
8024 +-{
8025 +- u64 low = a << shift, high = a >> (64 - shift);
8026 +-
8027 +- /* To avoid the overflow on divq */
8028 +- if (high >= divisor)
8029 +- return 1;
8030 +-
8031 +- /* Low hold the result, high hold rem which is discarded */
8032 +- asm("divq %2\n\t" : "=a" (low), "=d" (high) :
8033 +- "rm" (divisor), "0" (low), "1" (high));
8034 +- *result = low;
8035 +-
8036 +- return 0;
8037 +-}
8038 +-
8039 +-static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc,
8040 +- bool *expired)
8041 +-{
8042 +- struct vcpu_vmx *vmx;
8043 +- u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
8044 +- struct kvm_timer *ktimer = &vcpu->arch.apic->lapic_timer;
8045 +-
8046 +- if (kvm_mwait_in_guest(vcpu->kvm) ||
8047 +- kvm_can_post_timer_interrupt(vcpu))
8048 +- return -EOPNOTSUPP;
8049 +-
8050 +- vmx = to_vmx(vcpu);
8051 +- tscl = rdtsc();
8052 +- guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
8053 +- delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
8054 +- lapic_timer_advance_cycles = nsec_to_cycles(vcpu,
8055 +- ktimer->timer_advance_ns);
8056 +-
8057 +- if (delta_tsc > lapic_timer_advance_cycles)
8058 +- delta_tsc -= lapic_timer_advance_cycles;
8059 +- else
8060 +- delta_tsc = 0;
8061 +-
8062 +- /* Convert to host delta tsc if tsc scaling is enabled */
8063 +- if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
8064 +- delta_tsc && u64_shl_div_u64(delta_tsc,
8065 +- kvm_tsc_scaling_ratio_frac_bits,
8066 +- vcpu->arch.tsc_scaling_ratio, &delta_tsc))
8067 +- return -ERANGE;
8068 +-
8069 +- /*
8070 +- * If the delta tsc can't fit in the 32 bit after the multi shift,
8071 +- * we can't use the preemption timer.
8072 +- * It's possible that it fits on later vmentries, but checking
8073 +- * on every vmentry is costly so we just use an hrtimer.
8074 +- */
8075 +- if (delta_tsc >> (cpu_preemption_timer_multi + 32))
8076 +- return -ERANGE;
8077 +-
8078 +- vmx->hv_deadline_tsc = tscl + delta_tsc;
8079 +- *expired = !delta_tsc;
8080 +- return 0;
8081 +-}
8082 +-
8083 +-static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
8084 +-{
8085 +- to_vmx(vcpu)->hv_deadline_tsc = -1;
8086 +-}
8087 +-#endif
8088 +-
8089 +-static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
8090 +-{
8091 +- if (!kvm_pause_in_guest(vcpu->kvm))
8092 +- shrink_ple_window(vcpu);
8093 +-}
8094 +-
8095 +-static void vmx_slot_enable_log_dirty(struct kvm *kvm,
8096 +- struct kvm_memory_slot *slot)
8097 +-{
8098 +- kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
8099 +- kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
8100 +-}
8101 +-
8102 +-static void vmx_slot_disable_log_dirty(struct kvm *kvm,
8103 +- struct kvm_memory_slot *slot)
8104 +-{
8105 +- kvm_mmu_slot_set_dirty(kvm, slot);
8106 +-}
8107 +-
8108 +-static void vmx_flush_log_dirty(struct kvm *kvm)
8109 +-{
8110 +- kvm_flush_pml_buffers(kvm);
8111 +-}
8112 +-
8113 +-static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
8114 +-{
8115 +- struct vmcs12 *vmcs12;
8116 +- struct vcpu_vmx *vmx = to_vmx(vcpu);
8117 +- gpa_t gpa, dst;
8118 +-
8119 +- if (is_guest_mode(vcpu)) {
8120 +- WARN_ON_ONCE(vmx->nested.pml_full);
8121 +-
8122 +- /*
8123 +- * Check if PML is enabled for the nested guest.
8124 +- * Whether eptp bit 6 is set is already checked
8125 +- * as part of A/D emulation.
8126 +- */
8127 +- vmcs12 = get_vmcs12(vcpu);
8128 +- if (!nested_cpu_has_pml(vmcs12))
8129 +- return 0;
8130 +-
8131 +- if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
8132 +- vmx->nested.pml_full = true;
8133 +- return 1;
8134 +- }
8135 +-
8136 +- gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
8137 +- dst = vmcs12->pml_address + sizeof(u64) * vmcs12->guest_pml_index;
8138 +-
8139 +- if (kvm_write_guest_page(vcpu->kvm, gpa_to_gfn(dst), &gpa,
8140 +- offset_in_page(dst), sizeof(gpa)))
8141 +- return 0;
8142 +-
8143 +- vmcs12->guest_pml_index--;
8144 +- }
8145 +-
8146 +- return 0;
8147 +-}
8148 +-
8149 +-static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
8150 +- struct kvm_memory_slot *memslot,
8151 +- gfn_t offset, unsigned long mask)
8152 +-{
8153 +- kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
8154 +-}
8155 +-
8156 +-static void __pi_post_block(struct kvm_vcpu *vcpu)
8157 +-{
8158 +- struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
8159 +- struct pi_desc old, new;
8160 +- unsigned int dest;
8161 +-
8162 +- do {
8163 +- old.control = new.control = pi_desc->control;
8164 +- WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
8165 +- "Wakeup handler not enabled while the VCPU is blocked\n");
8166 +-
8167 +- dest = cpu_physical_id(vcpu->cpu);
8168 +-
8169 +- if (x2apic_enabled())
8170 +- new.ndst = dest;
8171 +- else
8172 +- new.ndst = (dest << 8) & 0xFF00;
8173 +-
8174 +- /* set 'NV' to 'notification vector' */
8175 +- new.nv = POSTED_INTR_VECTOR;
8176 +- } while (cmpxchg64(&pi_desc->control, old.control,
8177 +- new.control) != old.control);
8178 +-
8179 +- if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
8180 +- spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
8181 +- list_del(&vcpu->blocked_vcpu_list);
8182 +- spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
8183 +- vcpu->pre_pcpu = -1;
8184 +- }
8185 +-}
8186 +-
8187 +-/*
8188 +- * This routine does the following things for vCPU which is going
8189 +- * to be blocked if VT-d PI is enabled.
8190 +- * - Store the vCPU to the wakeup list, so when interrupts happen
8191 +- * we can find the right vCPU to wake up.
8192 +- * - Change the Posted-interrupt descriptor as below:
8193 +- * 'NDST' <-- vcpu->pre_pcpu
8194 +- * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
8195 +- * - If 'ON' is set during this process, which means at least one
8196 +- * interrupt is posted for this vCPU, we cannot block it, in
8197 +- * this case, return 1, otherwise, return 0.
8198 +- *
8199 +- */
8200 +-static int pi_pre_block(struct kvm_vcpu *vcpu)
8201 +-{
8202 +- unsigned int dest;
8203 +- struct pi_desc old, new;
8204 +- struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
8205 +-
8206 +- if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
8207 +- !irq_remapping_cap(IRQ_POSTING_CAP) ||
8208 +- !kvm_vcpu_apicv_active(vcpu))
8209 +- return 0;
8210 +-
8211 +- WARN_ON(irqs_disabled());
8212 +- local_irq_disable();
8213 +- if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
8214 +- vcpu->pre_pcpu = vcpu->cpu;
8215 +- spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
8216 +- list_add_tail(&vcpu->blocked_vcpu_list,
8217 +- &per_cpu(blocked_vcpu_on_cpu,
8218 +- vcpu->pre_pcpu));
8219 +- spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
8220 +- }
8221 +-
8222 +- do {
8223 +- old.control = new.control = pi_desc->control;
8224 +-
8225 +- WARN((pi_desc->sn == 1),
8226 +- "Warning: SN field of posted-interrupts "
8227 +- "is set before blocking\n");
8228 +-
8229 +- /*
8230 +- * Since vCPU can be preempted during this process,
8231 +- * vcpu->cpu could be different with pre_pcpu, we
8232 +- * need to set pre_pcpu as the destination of wakeup
8233 +- * notification event, then we can find the right vCPU
8234 +- * to wakeup in wakeup handler if interrupts happen
8235 +- * when the vCPU is in blocked state.
8236 +- */
8237 +- dest = cpu_physical_id(vcpu->pre_pcpu);
8238 +-
8239 +- if (x2apic_enabled())
8240 +- new.ndst = dest;
8241 +- else
8242 +- new.ndst = (dest << 8) & 0xFF00;
8243 +-
8244 +- /* set 'NV' to 'wakeup vector' */
8245 +- new.nv = POSTED_INTR_WAKEUP_VECTOR;
8246 +- } while (cmpxchg64(&pi_desc->control, old.control,
8247 +- new.control) != old.control);
8248 +-
8249 +- /* We should not block the vCPU if an interrupt is posted for it. */
8250 +- if (pi_test_on(pi_desc) == 1)
8251 +- __pi_post_block(vcpu);
8252 +-
8253 +- local_irq_enable();
8254 +- return (vcpu->pre_pcpu == -1);
8255 +-}
8256 +-
8257 +-static int vmx_pre_block(struct kvm_vcpu *vcpu)
8258 +-{
8259 +- if (pi_pre_block(vcpu))
8260 +- return 1;
8261 +-
8262 +- if (kvm_lapic_hv_timer_in_use(vcpu))
8263 +- kvm_lapic_switch_to_sw_timer(vcpu);
8264 +-
8265 +- return 0;
8266 +-}
8267 +-
8268 +-static void pi_post_block(struct kvm_vcpu *vcpu)
8269 +-{
8270 +- if (vcpu->pre_pcpu == -1)
8271 +- return;
8272 +-
8273 +- WARN_ON(irqs_disabled());
8274 +- local_irq_disable();
8275 +- __pi_post_block(vcpu);
8276 +- local_irq_enable();
8277 +-}
8278 +-
8279 +-static void vmx_post_block(struct kvm_vcpu *vcpu)
8280 +-{
8281 +- if (kvm_x86_ops->set_hv_timer)
8282 +- kvm_lapic_switch_to_hv_timer(vcpu);
8283 +-
8284 +- pi_post_block(vcpu);
8285 +-}
8286 +-
8287 +-/*
8288 +- * vmx_update_pi_irte - set IRTE for Posted-Interrupts
8289 +- *
8290 +- * @kvm: kvm
8291 +- * @host_irq: host irq of the interrupt
8292 +- * @guest_irq: gsi of the interrupt
8293 +- * @set: set or unset PI
8294 +- * returns 0 on success, < 0 on failure
8295 +- */
8296 +-static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
8297 +- uint32_t guest_irq, bool set)
8298 +-{
8299 +- struct kvm_kernel_irq_routing_entry *e;
8300 +- struct kvm_irq_routing_table *irq_rt;
8301 +- struct kvm_lapic_irq irq;
8302 +- struct kvm_vcpu *vcpu;
8303 +- struct vcpu_data vcpu_info;
8304 +- int idx, ret = 0;
8305 +-
8306 +- if (!kvm_arch_has_assigned_device(kvm) ||
8307 +- !irq_remapping_cap(IRQ_POSTING_CAP) ||
8308 +- !kvm_vcpu_apicv_active(kvm->vcpus[0]))
8309 +- return 0;
8310 +-
8311 +- idx = srcu_read_lock(&kvm->irq_srcu);
8312 +- irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
8313 +- if (guest_irq >= irq_rt->nr_rt_entries ||
8314 +- hlist_empty(&irq_rt->map[guest_irq])) {
8315 +- pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
8316 +- guest_irq, irq_rt->nr_rt_entries);
8317 +- goto out;
8318 +- }
8319 +-
8320 +- hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
8321 +- if (e->type != KVM_IRQ_ROUTING_MSI)
8322 +- continue;
8323 +- /*
8324 +- * VT-d PI cannot support posting multicast/broadcast
8325 +- * interrupts to a vCPU, we still use interrupt remapping
8326 +- * for these kind of interrupts.
8327 +- *
8328 +- * For lowest-priority interrupts, we only support
8329 +- * those with single CPU as the destination, e.g. user
8330 +- * configures the interrupts via /proc/irq or uses
8331 +- * irqbalance to make the interrupts single-CPU.
8332 +- *
8333 +- * We will support full lowest-priority interrupt later.
8334 +- *
8335 +- * In addition, we can only inject generic interrupts using
8336 +- * the PI mechanism, refuse to route others through it.
8337 +- */
8338 +-
8339 +- kvm_set_msi_irq(kvm, e, &irq);
8340 +- if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu) ||
8341 +- !kvm_irq_is_postable(&irq)) {
8342 +- /*
8343 +- * Make sure the IRTE is in remapped mode if
8344 +- * we don't handle it in posted mode.
8345 +- */
8346 +- ret = irq_set_vcpu_affinity(host_irq, NULL);
8347 +- if (ret < 0) {
8348 +- printk(KERN_INFO
8349 +- "failed to back to remapped mode, irq: %u\n",
8350 +- host_irq);
8351 +- goto out;
8352 +- }
8353 +-
8354 +- continue;
8355 +- }
8356 +-
8357 +- vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
8358 +- vcpu_info.vector = irq.vector;
8359 +-
8360 +- trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
8361 +- vcpu_info.vector, vcpu_info.pi_desc_addr, set);
8362 +-
8363 +- if (set)
8364 +- ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
8365 +- else
8366 +- ret = irq_set_vcpu_affinity(host_irq, NULL);
8367 +-
8368 +- if (ret < 0) {
8369 +- printk(KERN_INFO "%s: failed to update PI IRTE\n",
8370 +- __func__);
8371 +- goto out;
8372 +- }
8373 +- }
8374 +-
8375 +- ret = 0;
8376 +-out:
8377 +- srcu_read_unlock(&kvm->irq_srcu, idx);
8378 +- return ret;
8379 +-}
8380 +-
8381 +-static void vmx_setup_mce(struct kvm_vcpu *vcpu)
8382 +-{
8383 +- if (vcpu->arch.mcg_cap & MCG_LMCE_P)
8384 +- to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
8385 +- FEATURE_CONTROL_LMCE;
8386 +- else
8387 +- to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
8388 +- ~FEATURE_CONTROL_LMCE;
8389 +-}
8390 +-
8391 +-static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
8392 +-{
8393 +- /* we need a nested vmexit to enter SMM, postpone if run is pending */
8394 +- if (to_vmx(vcpu)->nested.nested_run_pending)
8395 +- return 0;
8396 +- return 1;
8397 +-}
8398 +-
8399 +-static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
8400 +-{
8401 +- struct vcpu_vmx *vmx = to_vmx(vcpu);
8402 +-
8403 +- vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
8404 +- if (vmx->nested.smm.guest_mode)
8405 +- nested_vmx_vmexit(vcpu, -1, 0, 0);
8406 +-
8407 +- vmx->nested.smm.vmxon = vmx->nested.vmxon;
8408 +- vmx->nested.vmxon = false;
8409 +- vmx_clear_hlt(vcpu);
8410 +- return 0;
8411 +-}
8412 +-
8413 +-static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, const char *smstate)
8414 +-{
8415 +- struct vcpu_vmx *vmx = to_vmx(vcpu);
8416 +- int ret;
8417 +-
8418 +- if (vmx->nested.smm.vmxon) {
8419 +- vmx->nested.vmxon = true;
8420 +- vmx->nested.smm.vmxon = false;
8421 +- }
8422 +-
8423 +- if (vmx->nested.smm.guest_mode) {
8424 +- ret = nested_vmx_enter_non_root_mode(vcpu, false);
8425 +- if (ret)
8426 +- return ret;
8427 +-
8428 +- vmx->nested.smm.guest_mode = false;
8429 +- }
8430 +- return 0;
8431 +-}
8432 +-
8433 +-static int enable_smi_window(struct kvm_vcpu *vcpu)
8434 +-{
8435 +- return 0;
8436 +-}
8437 +-
8438 +-static bool vmx_need_emulation_on_page_fault(struct kvm_vcpu *vcpu)
8439 +-{
8440 +- return false;
8441 +-}
8442 +-
8443 +-static bool vmx_apic_init_signal_blocked(struct kvm_vcpu *vcpu)
8444 +-{
8445 +- return to_vmx(vcpu)->nested.vmxon;
8446 +-}
8447 +-
8448 +-static __init int hardware_setup(void)
8449 +-{
8450 +- unsigned long host_bndcfgs;
8451 +- struct desc_ptr dt;
8452 +- int r, i;
8453 +-
8454 +- rdmsrl_safe(MSR_EFER, &host_efer);
8455 +-
8456 +- store_idt(&dt);
8457 +- host_idt_base = dt.address;
8458 +-
8459 +- for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
8460 +- kvm_define_shared_msr(i, vmx_msr_index[i]);
8461 +-
8462 +- if (setup_vmcs_config(&vmcs_config, &vmx_capability) < 0)
8463 +- return -EIO;
8464 +-
8465 +- if (boot_cpu_has(X86_FEATURE_NX))
8466 +- kvm_enable_efer_bits(EFER_NX);
8467 +-
8468 +- if (boot_cpu_has(X86_FEATURE_MPX)) {
8469 +- rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs);
8470 +- WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost");
8471 +- }
8472 +-
8473 +- if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
8474 +- !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
8475 +- enable_vpid = 0;
8476 +-
8477 +- if (!cpu_has_vmx_ept() ||
8478 +- !cpu_has_vmx_ept_4levels() ||
8479 +- !cpu_has_vmx_ept_mt_wb() ||
8480 +- !cpu_has_vmx_invept_global())
8481 +- enable_ept = 0;
8482 +-
8483 +- if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
8484 +- enable_ept_ad_bits = 0;
8485 +-
8486 +- if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
8487 +- enable_unrestricted_guest = 0;
8488 +-
8489 +- if (!cpu_has_vmx_flexpriority())
8490 +- flexpriority_enabled = 0;
8491 +-
8492 +- if (!cpu_has_virtual_nmis())
8493 +- enable_vnmi = 0;
8494 +-
8495 +- /*
8496 +- * set_apic_access_page_addr() is used to reload apic access
8497 +- * page upon invalidation. No need to do anything if not
8498 +- * using the APIC_ACCESS_ADDR VMCS field.
8499 +- */
8500 +- if (!flexpriority_enabled)
8501 +- kvm_x86_ops->set_apic_access_page_addr = NULL;
8502 +-
8503 +- if (!cpu_has_vmx_tpr_shadow())
8504 +- kvm_x86_ops->update_cr8_intercept = NULL;
8505 +-
8506 +- if (enable_ept && !cpu_has_vmx_ept_2m_page())
8507 +- kvm_disable_largepages();
8508 +-
8509 +-#if IS_ENABLED(CONFIG_HYPERV)
8510 +- if (ms_hyperv.nested_features & HV_X64_NESTED_GUEST_MAPPING_FLUSH
8511 +- && enable_ept) {
8512 +- kvm_x86_ops->tlb_remote_flush = hv_remote_flush_tlb;
8513 +- kvm_x86_ops->tlb_remote_flush_with_range =
8514 +- hv_remote_flush_tlb_with_range;
8515 +- }
8516 +-#endif
8517 +-
8518 +- if (!cpu_has_vmx_ple()) {
8519 +- ple_gap = 0;
8520 +- ple_window = 0;
8521 +- ple_window_grow = 0;
8522 +- ple_window_max = 0;
8523 +- ple_window_shrink = 0;
8524 +- }
8525 +-
8526 +- if (!cpu_has_vmx_apicv()) {
8527 +- enable_apicv = 0;
8528 +- kvm_x86_ops->sync_pir_to_irr = NULL;
8529 +- }
8530 +-
8531 +- if (cpu_has_vmx_tsc_scaling()) {
8532 +- kvm_has_tsc_control = true;
8533 +- kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
8534 +- kvm_tsc_scaling_ratio_frac_bits = 48;
8535 +- }
8536 +-
8537 +- set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
8538 +-
8539 +- if (enable_ept)
8540 +- vmx_enable_tdp();
8541 +- else
8542 +- kvm_disable_tdp();
8543 +-
8544 +- /*
8545 +- * Only enable PML when hardware supports PML feature, and both EPT
8546 +- * and EPT A/D bit features are enabled -- PML depends on them to work.
8547 +- */
8548 +- if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
8549 +- enable_pml = 0;
8550 +-
8551 +- if (!enable_pml) {
8552 +- kvm_x86_ops->slot_enable_log_dirty = NULL;
8553 +- kvm_x86_ops->slot_disable_log_dirty = NULL;
8554 +- kvm_x86_ops->flush_log_dirty = NULL;
8555 +- kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
8556 +- }
8557 +-
8558 +- if (!cpu_has_vmx_preemption_timer())
8559 +- enable_preemption_timer = false;
8560 +-
8561 +- if (enable_preemption_timer) {
8562 +- u64 use_timer_freq = 5000ULL * 1000 * 1000;
8563 +- u64 vmx_msr;
8564 +-
8565 +- rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
8566 +- cpu_preemption_timer_multi =
8567 +- vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
8568 +-
8569 +- if (tsc_khz)
8570 +- use_timer_freq = (u64)tsc_khz * 1000;
8571 +- use_timer_freq >>= cpu_preemption_timer_multi;
8572 +-
8573 +- /*
8574 +- * KVM "disables" the preemption timer by setting it to its max
8575 +- * value. Don't use the timer if it might cause spurious exits
8576 +- * at a rate faster than 0.1 Hz (of uninterrupted guest time).
8577 +- */
8578 +- if (use_timer_freq > 0xffffffffu / 10)
8579 +- enable_preemption_timer = false;
8580 +- }
8581 +-
8582 +- if (!enable_preemption_timer) {
8583 +- kvm_x86_ops->set_hv_timer = NULL;
8584 +- kvm_x86_ops->cancel_hv_timer = NULL;
8585 +- kvm_x86_ops->request_immediate_exit = __kvm_request_immediate_exit;
8586 +- }
8587 +-
8588 +- kvm_set_posted_intr_wakeup_handler(wakeup_handler);
8589 +-
8590 +- kvm_mce_cap_supported |= MCG_LMCE_P;
8591 +-
8592 +- if (pt_mode != PT_MODE_SYSTEM && pt_mode != PT_MODE_HOST_GUEST)
8593 +- return -EINVAL;
8594 +- if (!enable_ept || !cpu_has_vmx_intel_pt())
8595 +- pt_mode = PT_MODE_SYSTEM;
8596 +-
8597 +- if (nested) {
8598 +- nested_vmx_setup_ctls_msrs(&vmcs_config.nested,
8599 +- vmx_capability.ept, enable_apicv);
8600 +-
8601 +- r = nested_vmx_hardware_setup(kvm_vmx_exit_handlers);
8602 +- if (r)
8603 +- return r;
8604 +- }
8605 +-
8606 +- r = alloc_kvm_area();
8607 +- if (r)
8608 +- nested_vmx_hardware_unsetup();
8609 +- return r;
8610 +-}
8611 +-
8612 +-static __exit void hardware_unsetup(void)
8613 +-{
8614 +- if (nested)
8615 +- nested_vmx_hardware_unsetup();
8616 +-
8617 +- free_kvm_area();
8618 +-}
8619 +-
8620 +-static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
8621 +- .cpu_has_kvm_support = cpu_has_kvm_support,
8622 +- .disabled_by_bios = vmx_disabled_by_bios,
8623 +- .hardware_setup = hardware_setup,
8624 +- .hardware_unsetup = hardware_unsetup,
8625 +- .check_processor_compatibility = vmx_check_processor_compat,
8626 +- .hardware_enable = hardware_enable,
8627 +- .hardware_disable = hardware_disable,
8628 +- .cpu_has_accelerated_tpr = report_flexpriority,
8629 +- .has_emulated_msr = vmx_has_emulated_msr,
8630 +-
8631 +- .vm_init = vmx_vm_init,
8632 +- .vm_alloc = vmx_vm_alloc,
8633 +- .vm_free = vmx_vm_free,
8634 +-
8635 +- .vcpu_create = vmx_create_vcpu,
8636 +- .vcpu_free = vmx_free_vcpu,
8637 +- .vcpu_reset = vmx_vcpu_reset,
8638 +-
8639 +- .prepare_guest_switch = vmx_prepare_switch_to_guest,
8640 +- .vcpu_load = vmx_vcpu_load,
8641 +- .vcpu_put = vmx_vcpu_put,
8642 +-
8643 +- .update_bp_intercept = update_exception_bitmap,
8644 +- .get_msr_feature = vmx_get_msr_feature,
8645 +- .get_msr = vmx_get_msr,
8646 +- .set_msr = vmx_set_msr,
8647 +- .get_segment_base = vmx_get_segment_base,
8648 +- .get_segment = vmx_get_segment,
8649 +- .set_segment = vmx_set_segment,
8650 +- .get_cpl = vmx_get_cpl,
8651 +- .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
8652 +- .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
8653 +- .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
8654 +- .set_cr0 = vmx_set_cr0,
8655 +- .set_cr3 = vmx_set_cr3,
8656 +- .set_cr4 = vmx_set_cr4,
8657 +- .set_efer = vmx_set_efer,
8658 +- .get_idt = vmx_get_idt,
8659 +- .set_idt = vmx_set_idt,
8660 +- .get_gdt = vmx_get_gdt,
8661 +- .set_gdt = vmx_set_gdt,
8662 +- .get_dr6 = vmx_get_dr6,
8663 +- .set_dr6 = vmx_set_dr6,
8664 +- .set_dr7 = vmx_set_dr7,
8665 +- .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
8666 +- .cache_reg = vmx_cache_reg,
8667 +- .get_rflags = vmx_get_rflags,
8668 +- .set_rflags = vmx_set_rflags,
8669 +-
8670 +- .tlb_flush = vmx_flush_tlb,
8671 +- .tlb_flush_gva = vmx_flush_tlb_gva,
8672 +-
8673 +- .run = vmx_vcpu_run,
8674 +- .handle_exit = vmx_handle_exit,
8675 +- .skip_emulated_instruction = skip_emulated_instruction,
8676 +- .set_interrupt_shadow = vmx_set_interrupt_shadow,
8677 +- .get_interrupt_shadow = vmx_get_interrupt_shadow,
8678 +- .patch_hypercall = vmx_patch_hypercall,
8679 +- .set_irq = vmx_inject_irq,
8680 +- .set_nmi = vmx_inject_nmi,
8681 +- .queue_exception = vmx_queue_exception,
8682 +- .cancel_injection = vmx_cancel_injection,
8683 +- .interrupt_allowed = vmx_interrupt_allowed,
8684 +- .nmi_allowed = vmx_nmi_allowed,
8685 +- .get_nmi_mask = vmx_get_nmi_mask,
8686 +- .set_nmi_mask = vmx_set_nmi_mask,
8687 +- .enable_nmi_window = enable_nmi_window,
8688 +- .enable_irq_window = enable_irq_window,
8689 +- .update_cr8_intercept = update_cr8_intercept,
8690 +- .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
8691 +- .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
8692 +- .get_enable_apicv = vmx_get_enable_apicv,
8693 +- .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
8694 +- .load_eoi_exitmap = vmx_load_eoi_exitmap,
8695 +- .apicv_post_state_restore = vmx_apicv_post_state_restore,
8696 +- .hwapic_irr_update = vmx_hwapic_irr_update,
8697 +- .hwapic_isr_update = vmx_hwapic_isr_update,
8698 +- .guest_apic_has_interrupt = vmx_guest_apic_has_interrupt,
8699 +- .sync_pir_to_irr = vmx_sync_pir_to_irr,
8700 +- .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
8701 +- .dy_apicv_has_pending_interrupt = vmx_dy_apicv_has_pending_interrupt,
8702 +-
8703 +- .set_tss_addr = vmx_set_tss_addr,
8704 +- .set_identity_map_addr = vmx_set_identity_map_addr,
8705 +- .get_tdp_level = get_ept_level,
8706 +- .get_mt_mask = vmx_get_mt_mask,
8707 +-
8708 +- .get_exit_info = vmx_get_exit_info,
8709 +-
8710 +- .get_lpage_level = vmx_get_lpage_level,
8711 +-
8712 +- .cpuid_update = vmx_cpuid_update,
8713 +-
8714 +- .rdtscp_supported = vmx_rdtscp_supported,
8715 +- .invpcid_supported = vmx_invpcid_supported,
8716 +-
8717 +- .set_supported_cpuid = vmx_set_supported_cpuid,
8718 +-
8719 +- .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
8720 +-
8721 +- .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
8722 +- .write_l1_tsc_offset = vmx_write_l1_tsc_offset,
8723 +-
8724 +- .set_tdp_cr3 = vmx_set_cr3,
8725 +-
8726 +- .check_intercept = vmx_check_intercept,
8727 +- .handle_exit_irqoff = vmx_handle_exit_irqoff,
8728 +- .mpx_supported = vmx_mpx_supported,
8729 +- .xsaves_supported = vmx_xsaves_supported,
8730 +- .umip_emulated = vmx_umip_emulated,
8731 +- .pt_supported = vmx_pt_supported,
8732 +-
8733 +- .request_immediate_exit = vmx_request_immediate_exit,
8734 +-
8735 +- .sched_in = vmx_sched_in,
8736 +-
8737 +- .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
8738 +- .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
8739 +- .flush_log_dirty = vmx_flush_log_dirty,
8740 +- .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
8741 +- .write_log_dirty = vmx_write_pml_buffer,
8742 +-
8743 +- .pre_block = vmx_pre_block,
8744 +- .post_block = vmx_post_block,
8745 +-
8746 +- .pmu_ops = &intel_pmu_ops,
8747 +-
8748 +- .update_pi_irte = vmx_update_pi_irte,
8749 +-
8750 +-#ifdef CONFIG_X86_64
8751 +- .set_hv_timer = vmx_set_hv_timer,
8752 +- .cancel_hv_timer = vmx_cancel_hv_timer,
8753 +-#endif
8754 +-
8755 +- .setup_mce = vmx_setup_mce,
8756 +-
8757 +- .smi_allowed = vmx_smi_allowed,
8758 +- .pre_enter_smm = vmx_pre_enter_smm,
8759 +- .pre_leave_smm = vmx_pre_leave_smm,
8760 +- .enable_smi_window = enable_smi_window,
8761 +-
8762 +- .check_nested_events = NULL,
8763 +- .get_nested_state = NULL,
8764 +- .set_nested_state = NULL,
8765 +- .get_vmcs12_pages = NULL,
8766 +- .nested_enable_evmcs = NULL,
8767 +- .nested_get_evmcs_version = NULL,
8768 +- .need_emulation_on_page_fault = vmx_need_emulation_on_page_fault,
8769 +- .apic_init_signal_blocked = vmx_apic_init_signal_blocked,
8770 +-};
8771 +-
8772 +-static void vmx_cleanup_l1d_flush(void)
8773 +-{
8774 +- if (vmx_l1d_flush_pages) {
8775 +- free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
8776 +- vmx_l1d_flush_pages = NULL;
8777 +- }
8778 +- /* Restore state so sysfs ignores VMX */
8779 +- l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
8780 +-}
8781 +-
8782 +-static void vmx_exit(void)
8783 +-{
8784 +-#ifdef CONFIG_KEXEC_CORE
8785 +- RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
8786 +- synchronize_rcu();
8787 +-#endif
8788 +-
8789 +- kvm_exit();
8790 +-
8791 +-#if IS_ENABLED(CONFIG_HYPERV)
8792 +- if (static_branch_unlikely(&enable_evmcs)) {
8793 +- int cpu;
8794 +- struct hv_vp_assist_page *vp_ap;
8795 +- /*
8796 +- * Reset everything to support using non-enlightened VMCS
8797 +- * access later (e.g. when we reload the module with
8798 +- * enlightened_vmcs=0)
8799 +- */
8800 +- for_each_online_cpu(cpu) {
8801 +- vp_ap = hv_get_vp_assist_page(cpu);
8802 +-
8803 +- if (!vp_ap)
8804 +- continue;
8805 +-
8806 +- vp_ap->nested_control.features.directhypercall = 0;
8807 +- vp_ap->current_nested_vmcs = 0;
8808 +- vp_ap->enlighten_vmentry = 0;
8809 +- }
8810 +-
8811 +- static_branch_disable(&enable_evmcs);
8812 +- }
8813 +-#endif
8814 +- vmx_cleanup_l1d_flush();
8815 +-}
8816 +-module_exit(vmx_exit);
8817 +-
8818 +-static int __init vmx_init(void)
8819 +-{
8820 +- int r;
8821 +-
8822 +-#if IS_ENABLED(CONFIG_HYPERV)
8823 +- /*
8824 +- * Enlightened VMCS usage should be recommended and the host needs
8825 +- * to support eVMCS v1 or above. We can also disable eVMCS support
8826 +- * with module parameter.
8827 +- */
8828 +- if (enlightened_vmcs &&
8829 +- ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
8830 +- (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
8831 +- KVM_EVMCS_VERSION) {
8832 +- int cpu;
8833 +-
8834 +- /* Check that we have assist pages on all online CPUs */
8835 +- for_each_online_cpu(cpu) {
8836 +- if (!hv_get_vp_assist_page(cpu)) {
8837 +- enlightened_vmcs = false;
8838 +- break;
8839 +- }
8840 +- }
8841 +-
8842 +- if (enlightened_vmcs) {
8843 +- pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
8844 +- static_branch_enable(&enable_evmcs);
8845 +- }
8846 +-
8847 +- if (ms_hyperv.nested_features & HV_X64_NESTED_DIRECT_FLUSH)
8848 +- vmx_x86_ops.enable_direct_tlbflush
8849 +- = hv_enable_direct_tlbflush;
8850 +-
8851 +- } else {
8852 +- enlightened_vmcs = false;
8853 +- }
8854 +-#endif
8855 +-
8856 +- r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
8857 +- __alignof__(struct vcpu_vmx), THIS_MODULE);
8858 +- if (r)
8859 +- return r;
8860 +-
8861 +- /*
8862 +- * Must be called after kvm_init() so enable_ept is properly set
8863 +- * up. Hand the parameter mitigation value in which was stored in
8864 +- * the pre module init parser. If no parameter was given, it will
8865 +- * contain 'auto' which will be turned into the default 'cond'
8866 +- * mitigation mode.
8867 +- */
8868 +- r = vmx_setup_l1d_flush(vmentry_l1d_flush_param);
8869 +- if (r) {
8870 +- vmx_exit();
8871 +- return r;
8872 +- }
8873 +-
8874 +-#ifdef CONFIG_KEXEC_CORE
8875 +- rcu_assign_pointer(crash_vmclear_loaded_vmcss,
8876 +- crash_vmclear_local_loaded_vmcss);
8877 +-#endif
8878 +- vmx_check_vmcs12_offsets();
8879 +-
8880 +- return 0;
8881 +-}
8882 +-module_init(vmx_init);
8883 +diff --git a/arch/x86/lib/x86-opcode-map.txt b/arch/x86/lib/x86-opcode-map.txt
8884 +index 0a0e9112f284..5cb9f009f2be 100644
8885 +--- a/arch/x86/lib/x86-opcode-map.txt
8886 ++++ b/arch/x86/lib/x86-opcode-map.txt
8887 +@@ -909,7 +909,7 @@ EndTable
8888 +
8889 + GrpTable: Grp3_2
8890 + 0: TEST Ev,Iz
8891 +-1:
8892 ++1: TEST Ev,Iz
8893 + 2: NOT Ev
8894 + 3: NEG Ev
8895 + 4: MUL rAX,Ev
8896 +diff --git a/arch/x86/mm/pageattr.c b/arch/x86/mm/pageattr.c
8897 +index e2d4b25c7aa4..101f3ad0d6ad 100644
8898 +--- a/arch/x86/mm/pageattr.c
8899 ++++ b/arch/x86/mm/pageattr.c
8900 +@@ -2126,19 +2126,13 @@ int kernel_map_pages_in_pgd(pgd_t *pgd, u64 pfn, unsigned long address,
8901 + .pgd = pgd,
8902 + .numpages = numpages,
8903 + .mask_set = __pgprot(0),
8904 +- .mask_clr = __pgprot(0),
8905 ++ .mask_clr = __pgprot(~page_flags & (_PAGE_NX|_PAGE_RW)),
8906 + .flags = 0,
8907 + };
8908 +
8909 + if (!(__supported_pte_mask & _PAGE_NX))
8910 + goto out;
8911 +
8912 +- if (!(page_flags & _PAGE_NX))
8913 +- cpa.mask_clr = __pgprot(_PAGE_NX);
8914 +-
8915 +- if (!(page_flags & _PAGE_RW))
8916 +- cpa.mask_clr = __pgprot(_PAGE_RW);
8917 +-
8918 + if (!(page_flags & _PAGE_ENC))
8919 + cpa.mask_clr = pgprot_encrypted(cpa.mask_clr);
8920 +
8921 +diff --git a/arch/x86/platform/efi/efi.c b/arch/x86/platform/efi/efi.c
8922 +index 335a62e74a2e..e7f19dec16b9 100644
8923 +--- a/arch/x86/platform/efi/efi.c
8924 ++++ b/arch/x86/platform/efi/efi.c
8925 +@@ -480,7 +480,6 @@ void __init efi_init(void)
8926 + efi_char16_t *c16;
8927 + char vendor[100] = "unknown";
8928 + int i = 0;
8929 +- void *tmp;
8930 +
8931 + #ifdef CONFIG_X86_32
8932 + if (boot_params.efi_info.efi_systab_hi ||
8933 +@@ -505,14 +504,16 @@ void __init efi_init(void)
8934 + /*
8935 + * Show what we know for posterity
8936 + */
8937 +- c16 = tmp = early_memremap(efi.systab->fw_vendor, 2);
8938 ++ c16 = early_memremap_ro(efi.systab->fw_vendor,
8939 ++ sizeof(vendor) * sizeof(efi_char16_t));
8940 + if (c16) {
8941 +- for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
8942 +- vendor[i] = *c16++;
8943 ++ for (i = 0; i < sizeof(vendor) - 1 && c16[i]; ++i)
8944 ++ vendor[i] = c16[i];
8945 + vendor[i] = '\0';
8946 +- } else
8947 ++ early_memunmap(c16, sizeof(vendor) * sizeof(efi_char16_t));
8948 ++ } else {
8949 + pr_err("Could not map the firmware vendor!\n");
8950 +- early_memunmap(tmp, 2);
8951 ++ }
8952 +
8953 + pr_info("EFI v%u.%.02u by %s\n",
8954 + efi.systab->hdr.revision >> 16,
8955 +@@ -929,16 +930,14 @@ static void __init __efi_enter_virtual_mode(void)
8956 +
8957 + if (efi_alloc_page_tables()) {
8958 + pr_err("Failed to allocate EFI page tables\n");
8959 +- clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
8960 +- return;
8961 ++ goto err;
8962 + }
8963 +
8964 + efi_merge_regions();
8965 + new_memmap = efi_map_regions(&count, &pg_shift);
8966 + if (!new_memmap) {
8967 + pr_err("Error reallocating memory, EFI runtime non-functional!\n");
8968 +- clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
8969 +- return;
8970 ++ goto err;
8971 + }
8972 +
8973 + pa = __pa(new_memmap);
8974 +@@ -952,8 +951,7 @@ static void __init __efi_enter_virtual_mode(void)
8975 +
8976 + if (efi_memmap_init_late(pa, efi.memmap.desc_size * count)) {
8977 + pr_err("Failed to remap late EFI memory map\n");
8978 +- clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
8979 +- return;
8980 ++ goto err;
8981 + }
8982 +
8983 + if (efi_enabled(EFI_DBG)) {
8984 +@@ -961,12 +959,11 @@ static void __init __efi_enter_virtual_mode(void)
8985 + efi_print_memmap();
8986 + }
8987 +
8988 +- BUG_ON(!efi.systab);
8989 ++ if (WARN_ON(!efi.systab))
8990 ++ goto err;
8991 +
8992 +- if (efi_setup_page_tables(pa, 1 << pg_shift)) {
8993 +- clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
8994 +- return;
8995 +- }
8996 ++ if (efi_setup_page_tables(pa, 1 << pg_shift))
8997 ++ goto err;
8998 +
8999 + efi_sync_low_kernel_mappings();
9000 +
9001 +@@ -986,9 +983,9 @@ static void __init __efi_enter_virtual_mode(void)
9002 + }
9003 +
9004 + if (status != EFI_SUCCESS) {
9005 +- pr_alert("Unable to switch EFI into virtual mode (status=%lx)!\n",
9006 +- status);
9007 +- panic("EFI call to SetVirtualAddressMap() failed!");
9008 ++ pr_err("Unable to switch EFI into virtual mode (status=%lx)!\n",
9009 ++ status);
9010 ++ goto err;
9011 + }
9012 +
9013 + /*
9014 +@@ -1015,6 +1012,10 @@ static void __init __efi_enter_virtual_mode(void)
9015 +
9016 + /* clean DUMMY object */
9017 + efi_delete_dummy_variable();
9018 ++ return;
9019 ++
9020 ++err:
9021 ++ clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
9022 + }
9023 +
9024 + void __init efi_enter_virtual_mode(void)
9025 +diff --git a/arch/x86/platform/efi/efi_64.c b/arch/x86/platform/efi/efi_64.c
9026 +index ee5d08f25ce4..6db8f3598c80 100644
9027 +--- a/arch/x86/platform/efi/efi_64.c
9028 ++++ b/arch/x86/platform/efi/efi_64.c
9029 +@@ -389,11 +389,12 @@ int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages)
9030 + return 0;
9031 +
9032 + page = alloc_page(GFP_KERNEL|__GFP_DMA32);
9033 +- if (!page)
9034 +- panic("Unable to allocate EFI runtime stack < 4GB\n");
9035 ++ if (!page) {
9036 ++ pr_err("Unable to allocate EFI runtime stack < 4GB\n");
9037 ++ return 1;
9038 ++ }
9039 +
9040 +- efi_scratch.phys_stack = virt_to_phys(page_address(page));
9041 +- efi_scratch.phys_stack += PAGE_SIZE; /* stack grows down */
9042 ++ efi_scratch.phys_stack = page_to_phys(page + 1); /* stack grows down */
9043 +
9044 + npages = (_etext - _text) >> PAGE_SHIFT;
9045 + text = __pa(_text);
9046 +diff --git a/drivers/acpi/acpica/dsfield.c b/drivers/acpi/acpica/dsfield.c
9047 +index 30fe89545d6a..bcc6a7acc576 100644
9048 +--- a/drivers/acpi/acpica/dsfield.c
9049 ++++ b/drivers/acpi/acpica/dsfield.c
9050 +@@ -244,7 +244,7 @@ cleanup:
9051 + * FUNCTION: acpi_ds_get_field_names
9052 + *
9053 + * PARAMETERS: info - create_field info structure
9054 +- * ` walk_state - Current method state
9055 ++ * walk_state - Current method state
9056 + * arg - First parser arg for the field name list
9057 + *
9058 + * RETURN: Status
9059 +diff --git a/drivers/acpi/acpica/dswload.c b/drivers/acpi/acpica/dswload.c
9060 +index d06c41446282..ba53662f1217 100644
9061 +--- a/drivers/acpi/acpica/dswload.c
9062 ++++ b/drivers/acpi/acpica/dswload.c
9063 +@@ -412,6 +412,27 @@ acpi_status acpi_ds_load1_end_op(struct acpi_walk_state *walk_state)
9064 + ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH, "Op=%p State=%p\n", op,
9065 + walk_state));
9066 +
9067 ++ /*
9068 ++ * Disassembler: handle create field operators here.
9069 ++ *
9070 ++ * create_buffer_field is a deferred op that is typically processed in load
9071 ++ * pass 2. However, disassembly of control method contents walk the parse
9072 ++ * tree with ACPI_PARSE_LOAD_PASS1 and AML_CREATE operators are processed
9073 ++ * in a later walk. This is a problem when there is a control method that
9074 ++ * has the same name as the AML_CREATE object. In this case, any use of the
9075 ++ * name segment will be detected as a method call rather than a reference
9076 ++ * to a buffer field.
9077 ++ *
9078 ++ * This earlier creation during disassembly solves this issue by inserting
9079 ++ * the named object in the ACPI namespace so that references to this name
9080 ++ * would be a name string rather than a method call.
9081 ++ */
9082 ++ if ((walk_state->parse_flags & ACPI_PARSE_DISASSEMBLE) &&
9083 ++ (walk_state->op_info->flags & AML_CREATE)) {
9084 ++ status = acpi_ds_create_buffer_field(op, walk_state);
9085 ++ return_ACPI_STATUS(status);
9086 ++ }
9087 ++
9088 + /* We are only interested in opcodes that have an associated name */
9089 +
9090 + if (!(walk_state->op_info->flags & (AML_NAMED | AML_FIELD))) {
9091 +diff --git a/drivers/acpi/button.c b/drivers/acpi/button.c
9092 +index a25d77b3a16a..d5c19e25ddf5 100644
9093 +--- a/drivers/acpi/button.c
9094 ++++ b/drivers/acpi/button.c
9095 +@@ -102,6 +102,17 @@ static const struct dmi_system_id lid_blacklst[] = {
9096 + },
9097 + .driver_data = (void *)(long)ACPI_BUTTON_LID_INIT_OPEN,
9098 + },
9099 ++ {
9100 ++ /*
9101 ++ * Razer Blade Stealth 13 late 2019, notification of the LID device
9102 ++ * only happens on close, not on open and _LID always returns closed.
9103 ++ */
9104 ++ .matches = {
9105 ++ DMI_MATCH(DMI_SYS_VENDOR, "Razer"),
9106 ++ DMI_MATCH(DMI_PRODUCT_NAME, "Razer Blade Stealth 13 Late 2019"),
9107 ++ },
9108 ++ .driver_data = (void *)(long)ACPI_BUTTON_LID_INIT_OPEN,
9109 ++ },
9110 + {}
9111 + };
9112 +
9113 +diff --git a/drivers/atm/fore200e.c b/drivers/atm/fore200e.c
9114 +index 99a38115b0a8..86aab14872fd 100644
9115 +--- a/drivers/atm/fore200e.c
9116 ++++ b/drivers/atm/fore200e.c
9117 +@@ -1504,12 +1504,14 @@ fore200e_open(struct atm_vcc *vcc)
9118 + static void
9119 + fore200e_close(struct atm_vcc* vcc)
9120 + {
9121 +- struct fore200e* fore200e = FORE200E_DEV(vcc->dev);
9122 + struct fore200e_vcc* fore200e_vcc;
9123 ++ struct fore200e* fore200e;
9124 + struct fore200e_vc_map* vc_map;
9125 + unsigned long flags;
9126 +
9127 + ASSERT(vcc);
9128 ++ fore200e = FORE200E_DEV(vcc->dev);
9129 ++
9130 + ASSERT((vcc->vpi >= 0) && (vcc->vpi < 1<<FORE200E_VPI_BITS));
9131 + ASSERT((vcc->vci >= 0) && (vcc->vci < 1<<FORE200E_VCI_BITS));
9132 +
9133 +@@ -1554,10 +1556,10 @@ fore200e_close(struct atm_vcc* vcc)
9134 + static int
9135 + fore200e_send(struct atm_vcc *vcc, struct sk_buff *skb)
9136 + {
9137 +- struct fore200e* fore200e = FORE200E_DEV(vcc->dev);
9138 +- struct fore200e_vcc* fore200e_vcc = FORE200E_VCC(vcc);
9139 ++ struct fore200e* fore200e;
9140 ++ struct fore200e_vcc* fore200e_vcc;
9141 + struct fore200e_vc_map* vc_map;
9142 +- struct host_txq* txq = &fore200e->host_txq;
9143 ++ struct host_txq* txq;
9144 + struct host_txq_entry* entry;
9145 + struct tpd* tpd;
9146 + struct tpd_haddr tpd_haddr;
9147 +@@ -1570,9 +1572,18 @@ fore200e_send(struct atm_vcc *vcc, struct sk_buff *skb)
9148 + unsigned char* data;
9149 + unsigned long flags;
9150 +
9151 +- ASSERT(vcc);
9152 +- ASSERT(fore200e);
9153 +- ASSERT(fore200e_vcc);
9154 ++ if (!vcc)
9155 ++ return -EINVAL;
9156 ++
9157 ++ fore200e = FORE200E_DEV(vcc->dev);
9158 ++ fore200e_vcc = FORE200E_VCC(vcc);
9159 ++
9160 ++ if (!fore200e)
9161 ++ return -EINVAL;
9162 ++
9163 ++ txq = &fore200e->host_txq;
9164 ++ if (!fore200e_vcc)
9165 ++ return -EINVAL;
9166 +
9167 + if (!test_bit(ATM_VF_READY, &vcc->flags)) {
9168 + DPRINTK(1, "VC %d.%d.%d not ready for tx\n", vcc->itf, vcc->vpi, vcc->vpi);
9169 +diff --git a/drivers/base/dd.c b/drivers/base/dd.c
9170 +index 11d24a552ee4..5f6416e6ba96 100644
9171 +--- a/drivers/base/dd.c
9172 ++++ b/drivers/base/dd.c
9173 +@@ -470,7 +470,10 @@ static int really_probe(struct device *dev, struct device_driver *drv)
9174 + atomic_inc(&probe_count);
9175 + pr_debug("bus: '%s': %s: probing driver %s with device %s\n",
9176 + drv->bus->name, __func__, drv->name, dev_name(dev));
9177 +- WARN_ON(!list_empty(&dev->devres_head));
9178 ++ if (!list_empty(&dev->devres_head)) {
9179 ++ dev_crit(dev, "Resources present before probing\n");
9180 ++ return -EBUSY;
9181 ++ }
9182 +
9183 + re_probe:
9184 + dev->driver = drv;
9185 +diff --git a/drivers/base/platform.c b/drivers/base/platform.c
9186 +index e9be1f56929a..d1f901b58f75 100644
9187 +--- a/drivers/base/platform.c
9188 ++++ b/drivers/base/platform.c
9189 +@@ -27,6 +27,7 @@
9190 + #include <linux/limits.h>
9191 + #include <linux/property.h>
9192 + #include <linux/kmemleak.h>
9193 ++#include <linux/types.h>
9194 +
9195 + #include "base.h"
9196 + #include "power/power.h"
9197 +@@ -67,7 +68,7 @@ void __weak arch_setup_pdev_archdata(struct platform_device *pdev)
9198 + struct resource *platform_get_resource(struct platform_device *dev,
9199 + unsigned int type, unsigned int num)
9200 + {
9201 +- int i;
9202 ++ u32 i;
9203 +
9204 + for (i = 0; i < dev->num_resources; i++) {
9205 + struct resource *r = &dev->resource[i];
9206 +@@ -162,7 +163,7 @@ struct resource *platform_get_resource_byname(struct platform_device *dev,
9207 + unsigned int type,
9208 + const char *name)
9209 + {
9210 +- int i;
9211 ++ u32 i;
9212 +
9213 + for (i = 0; i < dev->num_resources; i++) {
9214 + struct resource *r = &dev->resource[i];
9215 +@@ -359,7 +360,8 @@ EXPORT_SYMBOL_GPL(platform_device_add_properties);
9216 + */
9217 + int platform_device_add(struct platform_device *pdev)
9218 + {
9219 +- int i, ret;
9220 ++ u32 i;
9221 ++ int ret;
9222 +
9223 + if (!pdev)
9224 + return -EINVAL;
9225 +@@ -425,7 +427,7 @@ int platform_device_add(struct platform_device *pdev)
9226 + pdev->id = PLATFORM_DEVID_AUTO;
9227 + }
9228 +
9229 +- while (--i >= 0) {
9230 ++ while (i--) {
9231 + struct resource *r = &pdev->resource[i];
9232 + if (r->parent)
9233 + release_resource(r);
9234 +@@ -446,7 +448,7 @@ EXPORT_SYMBOL_GPL(platform_device_add);
9235 + */
9236 + void platform_device_del(struct platform_device *pdev)
9237 + {
9238 +- int i;
9239 ++ u32 i;
9240 +
9241 + if (pdev) {
9242 + device_remove_properties(&pdev->dev);
9243 +diff --git a/drivers/block/brd.c b/drivers/block/brd.c
9244 +index 17defbf4f332..02e8fff3f828 100644
9245 +--- a/drivers/block/brd.c
9246 ++++ b/drivers/block/brd.c
9247 +@@ -463,6 +463,25 @@ static struct kobject *brd_probe(dev_t dev, int *part, void *data)
9248 + return kobj;
9249 + }
9250 +
9251 ++static inline void brd_check_and_reset_par(void)
9252 ++{
9253 ++ if (unlikely(!max_part))
9254 ++ max_part = 1;
9255 ++
9256 ++ /*
9257 ++ * make sure 'max_part' can be divided exactly by (1U << MINORBITS),
9258 ++ * otherwise, it is possiable to get same dev_t when adding partitions.
9259 ++ */
9260 ++ if ((1U << MINORBITS) % max_part != 0)
9261 ++ max_part = 1UL << fls(max_part);
9262 ++
9263 ++ if (max_part > DISK_MAX_PARTS) {
9264 ++ pr_info("brd: max_part can't be larger than %d, reset max_part = %d.\n",
9265 ++ DISK_MAX_PARTS, DISK_MAX_PARTS);
9266 ++ max_part = DISK_MAX_PARTS;
9267 ++ }
9268 ++}
9269 ++
9270 + static int __init brd_init(void)
9271 + {
9272 + struct brd_device *brd, *next;
9273 +@@ -486,8 +505,7 @@ static int __init brd_init(void)
9274 + if (register_blkdev(RAMDISK_MAJOR, "ramdisk"))
9275 + return -EIO;
9276 +
9277 +- if (unlikely(!max_part))
9278 +- max_part = 1;
9279 ++ brd_check_and_reset_par();
9280 +
9281 + for (i = 0; i < rd_nr; i++) {
9282 + brd = brd_alloc(i);
9283 +diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
9284 +index b9d321bdaa8a..226103af30f0 100644
9285 +--- a/drivers/block/nbd.c
9286 ++++ b/drivers/block/nbd.c
9287 +@@ -1216,6 +1216,16 @@ static int nbd_start_device(struct nbd_device *nbd)
9288 + args = kzalloc(sizeof(*args), GFP_KERNEL);
9289 + if (!args) {
9290 + sock_shutdown(nbd);
9291 ++ /*
9292 ++ * If num_connections is m (2 < m),
9293 ++ * and NO.1 ~ NO.n(1 < n < m) kzallocs are successful.
9294 ++ * But NO.(n + 1) failed. We still have n recv threads.
9295 ++ * So, add flush_workqueue here to prevent recv threads
9296 ++ * dropping the last config_refs and trying to destroy
9297 ++ * the workqueue from inside the workqueue.
9298 ++ */
9299 ++ if (i)
9300 ++ flush_workqueue(nbd->recv_workq);
9301 + return -ENOMEM;
9302 + }
9303 + sk_set_memalloc(config->socks[i]->sock->sk);
9304 +diff --git a/drivers/block/rbd.c b/drivers/block/rbd.c
9305 +index b942f4c8cea8..d3ad1b8c133e 100644
9306 +--- a/drivers/block/rbd.c
9307 ++++ b/drivers/block/rbd.c
9308 +@@ -2097,7 +2097,7 @@ static int rbd_img_fill_nodata(struct rbd_img_request *img_req,
9309 + u64 off, u64 len)
9310 + {
9311 + struct ceph_file_extent ex = { off, len };
9312 +- union rbd_img_fill_iter dummy;
9313 ++ union rbd_img_fill_iter dummy = {};
9314 + struct rbd_img_fill_ctx fctx = {
9315 + .pos_type = OBJ_REQUEST_NODATA,
9316 + .pos = &dummy,
9317 +diff --git a/drivers/char/random.c b/drivers/char/random.c
9318 +index 53e822793d46..28b110cd3977 100644
9319 +--- a/drivers/char/random.c
9320 ++++ b/drivers/char/random.c
9321 +@@ -1609,8 +1609,9 @@ static void _warn_unseeded_randomness(const char *func_name, void *caller,
9322 + print_once = true;
9323 + #endif
9324 + if (__ratelimit(&unseeded_warning))
9325 +- pr_notice("random: %s called from %pS with crng_init=%d\n",
9326 +- func_name, caller, crng_init);
9327 ++ printk_deferred(KERN_NOTICE "random: %s called from %pS "
9328 ++ "with crng_init=%d\n", func_name, caller,
9329 ++ crng_init);
9330 + }
9331 +
9332 + /*
9333 +diff --git a/drivers/clk/qcom/clk-rcg2.c b/drivers/clk/qcom/clk-rcg2.c
9334 +index 51b2388d80ac..ee693e15d9eb 100644
9335 +--- a/drivers/clk/qcom/clk-rcg2.c
9336 ++++ b/drivers/clk/qcom/clk-rcg2.c
9337 +@@ -203,6 +203,9 @@ static int _freq_tbl_determine_rate(struct clk_hw *hw, const struct freq_tbl *f,
9338 +
9339 + clk_flags = clk_hw_get_flags(hw);
9340 + p = clk_hw_get_parent_by_index(hw, index);
9341 ++ if (!p)
9342 ++ return -EINVAL;
9343 ++
9344 + if (clk_flags & CLK_SET_RATE_PARENT) {
9345 + rate = f->freq;
9346 + if (f->pre_div) {
9347 +diff --git a/drivers/clk/sunxi-ng/ccu-sun50i-a64.c b/drivers/clk/sunxi-ng/ccu-sun50i-a64.c
9348 +index dec4a130390a..9ac6c299e074 100644
9349 +--- a/drivers/clk/sunxi-ng/ccu-sun50i-a64.c
9350 ++++ b/drivers/clk/sunxi-ng/ccu-sun50i-a64.c
9351 +@@ -901,11 +901,26 @@ static const struct sunxi_ccu_desc sun50i_a64_ccu_desc = {
9352 + .num_resets = ARRAY_SIZE(sun50i_a64_ccu_resets),
9353 + };
9354 +
9355 ++static struct ccu_pll_nb sun50i_a64_pll_cpu_nb = {
9356 ++ .common = &pll_cpux_clk.common,
9357 ++ /* copy from pll_cpux_clk */
9358 ++ .enable = BIT(31),
9359 ++ .lock = BIT(28),
9360 ++};
9361 ++
9362 ++static struct ccu_mux_nb sun50i_a64_cpu_nb = {
9363 ++ .common = &cpux_clk.common,
9364 ++ .cm = &cpux_clk.mux,
9365 ++ .delay_us = 1, /* > 8 clock cycles at 24 MHz */
9366 ++ .bypass_index = 1, /* index of 24 MHz oscillator */
9367 ++};
9368 ++
9369 + static int sun50i_a64_ccu_probe(struct platform_device *pdev)
9370 + {
9371 + struct resource *res;
9372 + void __iomem *reg;
9373 + u32 val;
9374 ++ int ret;
9375 +
9376 + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
9377 + reg = devm_ioremap_resource(&pdev->dev, res);
9378 +@@ -919,7 +934,18 @@ static int sun50i_a64_ccu_probe(struct platform_device *pdev)
9379 +
9380 + writel(0x515, reg + SUN50I_A64_PLL_MIPI_REG);
9381 +
9382 +- return sunxi_ccu_probe(pdev->dev.of_node, reg, &sun50i_a64_ccu_desc);
9383 ++ ret = sunxi_ccu_probe(pdev->dev.of_node, reg, &sun50i_a64_ccu_desc);
9384 ++ if (ret)
9385 ++ return ret;
9386 ++
9387 ++ /* Gate then ungate PLL CPU after any rate changes */
9388 ++ ccu_pll_notifier_register(&sun50i_a64_pll_cpu_nb);
9389 ++
9390 ++ /* Reparent CPU during PLL CPU rate changes */
9391 ++ ccu_mux_notifier_register(pll_cpux_clk.common.hw.clk,
9392 ++ &sun50i_a64_cpu_nb);
9393 ++
9394 ++ return 0;
9395 + }
9396 +
9397 + static const struct of_device_id sun50i_a64_ccu_ids[] = {
9398 +diff --git a/drivers/clk/uniphier/clk-uniphier-peri.c b/drivers/clk/uniphier/clk-uniphier-peri.c
9399 +index 89b3ac378b3f..8b75dc116a98 100644
9400 +--- a/drivers/clk/uniphier/clk-uniphier-peri.c
9401 ++++ b/drivers/clk/uniphier/clk-uniphier-peri.c
9402 +@@ -27,8 +27,8 @@
9403 + #define UNIPHIER_PERI_CLK_FI2C(idx, ch) \
9404 + UNIPHIER_CLK_GATE("i2c" #ch, (idx), "i2c", 0x24, 24 + (ch))
9405 +
9406 +-#define UNIPHIER_PERI_CLK_SCSSI(idx) \
9407 +- UNIPHIER_CLK_GATE("scssi", (idx), "spi", 0x20, 17)
9408 ++#define UNIPHIER_PERI_CLK_SCSSI(idx, ch) \
9409 ++ UNIPHIER_CLK_GATE("scssi" #ch, (idx), "spi", 0x20, 17 + (ch))
9410 +
9411 + #define UNIPHIER_PERI_CLK_MCSSI(idx) \
9412 + UNIPHIER_CLK_GATE("mcssi", (idx), "spi", 0x24, 14)
9413 +@@ -44,7 +44,7 @@ const struct uniphier_clk_data uniphier_ld4_peri_clk_data[] = {
9414 + UNIPHIER_PERI_CLK_I2C(6, 2),
9415 + UNIPHIER_PERI_CLK_I2C(7, 3),
9416 + UNIPHIER_PERI_CLK_I2C(8, 4),
9417 +- UNIPHIER_PERI_CLK_SCSSI(11),
9418 ++ UNIPHIER_PERI_CLK_SCSSI(11, 0),
9419 + { /* sentinel */ }
9420 + };
9421 +
9422 +@@ -60,7 +60,10 @@ const struct uniphier_clk_data uniphier_pro4_peri_clk_data[] = {
9423 + UNIPHIER_PERI_CLK_FI2C(8, 4),
9424 + UNIPHIER_PERI_CLK_FI2C(9, 5),
9425 + UNIPHIER_PERI_CLK_FI2C(10, 6),
9426 +- UNIPHIER_PERI_CLK_SCSSI(11),
9427 +- UNIPHIER_PERI_CLK_MCSSI(12),
9428 ++ UNIPHIER_PERI_CLK_SCSSI(11, 0),
9429 ++ UNIPHIER_PERI_CLK_SCSSI(12, 1),
9430 ++ UNIPHIER_PERI_CLK_SCSSI(13, 2),
9431 ++ UNIPHIER_PERI_CLK_SCSSI(14, 3),
9432 ++ UNIPHIER_PERI_CLK_MCSSI(15),
9433 + { /* sentinel */ }
9434 + };
9435 +diff --git a/drivers/clocksource/bcm2835_timer.c b/drivers/clocksource/bcm2835_timer.c
9436 +index 60da2537bef9..1082dcef17d1 100644
9437 +--- a/drivers/clocksource/bcm2835_timer.c
9438 ++++ b/drivers/clocksource/bcm2835_timer.c
9439 +@@ -134,7 +134,7 @@ static int __init bcm2835_timer_init(struct device_node *node)
9440 + ret = setup_irq(irq, &timer->act);
9441 + if (ret) {
9442 + pr_err("Can't set up timer IRQ\n");
9443 +- goto err_iounmap;
9444 ++ goto err_timer_free;
9445 + }
9446 +
9447 + clockevents_config_and_register(&timer->evt, freq, 0xf, 0xffffffff);
9448 +@@ -143,6 +143,9 @@ static int __init bcm2835_timer_init(struct device_node *node)
9449 +
9450 + return 0;
9451 +
9452 ++err_timer_free:
9453 ++ kfree(timer);
9454 ++
9455 + err_iounmap:
9456 + iounmap(base);
9457 + return ret;
9458 +diff --git a/drivers/crypto/chelsio/chtls/chtls_cm.c b/drivers/crypto/chelsio/chtls/chtls_cm.c
9459 +index 8b749c721c87..28d24118c645 100644
9460 +--- a/drivers/crypto/chelsio/chtls/chtls_cm.c
9461 ++++ b/drivers/crypto/chelsio/chtls/chtls_cm.c
9462 +@@ -731,6 +731,14 @@ static int chtls_close_listsrv_rpl(struct chtls_dev *cdev, struct sk_buff *skb)
9463 + return 0;
9464 + }
9465 +
9466 ++static void chtls_purge_wr_queue(struct sock *sk)
9467 ++{
9468 ++ struct sk_buff *skb;
9469 ++
9470 ++ while ((skb = dequeue_wr(sk)) != NULL)
9471 ++ kfree_skb(skb);
9472 ++}
9473 ++
9474 + static void chtls_release_resources(struct sock *sk)
9475 + {
9476 + struct chtls_sock *csk = rcu_dereference_sk_user_data(sk);
9477 +@@ -745,6 +753,11 @@ static void chtls_release_resources(struct sock *sk)
9478 + kfree_skb(csk->txdata_skb_cache);
9479 + csk->txdata_skb_cache = NULL;
9480 +
9481 ++ if (csk->wr_credits != csk->wr_max_credits) {
9482 ++ chtls_purge_wr_queue(sk);
9483 ++ chtls_reset_wr_list(csk);
9484 ++ }
9485 ++
9486 + if (csk->l2t_entry) {
9487 + cxgb4_l2t_release(csk->l2t_entry);
9488 + csk->l2t_entry = NULL;
9489 +@@ -1714,6 +1727,7 @@ static void chtls_peer_close(struct sock *sk, struct sk_buff *skb)
9490 + else
9491 + sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
9492 + }
9493 ++ kfree_skb(skb);
9494 + }
9495 +
9496 + static void chtls_close_con_rpl(struct sock *sk, struct sk_buff *skb)
9497 +@@ -2041,19 +2055,6 @@ rel_skb:
9498 + return 0;
9499 + }
9500 +
9501 +-static struct sk_buff *dequeue_wr(struct sock *sk)
9502 +-{
9503 +- struct chtls_sock *csk = rcu_dereference_sk_user_data(sk);
9504 +- struct sk_buff *skb = csk->wr_skb_head;
9505 +-
9506 +- if (likely(skb)) {
9507 +- /* Don't bother clearing the tail */
9508 +- csk->wr_skb_head = WR_SKB_CB(skb)->next_wr;
9509 +- WR_SKB_CB(skb)->next_wr = NULL;
9510 +- }
9511 +- return skb;
9512 +-}
9513 +-
9514 + static void chtls_rx_ack(struct sock *sk, struct sk_buff *skb)
9515 + {
9516 + struct cpl_fw4_ack *hdr = cplhdr(skb) + RSS_HDR;
9517 +diff --git a/drivers/crypto/chelsio/chtls/chtls_cm.h b/drivers/crypto/chelsio/chtls/chtls_cm.h
9518 +index 78eb3afa3a80..4282d8a4eae4 100644
9519 +--- a/drivers/crypto/chelsio/chtls/chtls_cm.h
9520 ++++ b/drivers/crypto/chelsio/chtls/chtls_cm.h
9521 +@@ -188,6 +188,12 @@ static inline void chtls_kfree_skb(struct sock *sk, struct sk_buff *skb)
9522 + kfree_skb(skb);
9523 + }
9524 +
9525 ++static inline void chtls_reset_wr_list(struct chtls_sock *csk)
9526 ++{
9527 ++ csk->wr_skb_head = NULL;
9528 ++ csk->wr_skb_tail = NULL;
9529 ++}
9530 ++
9531 + static inline void enqueue_wr(struct chtls_sock *csk, struct sk_buff *skb)
9532 + {
9533 + WR_SKB_CB(skb)->next_wr = NULL;
9534 +@@ -200,4 +206,19 @@ static inline void enqueue_wr(struct chtls_sock *csk, struct sk_buff *skb)
9535 + WR_SKB_CB(csk->wr_skb_tail)->next_wr = skb;
9536 + csk->wr_skb_tail = skb;
9537 + }
9538 ++
9539 ++static inline struct sk_buff *dequeue_wr(struct sock *sk)
9540 ++{
9541 ++ struct chtls_sock *csk = rcu_dereference_sk_user_data(sk);
9542 ++ struct sk_buff *skb = NULL;
9543 ++
9544 ++ skb = csk->wr_skb_head;
9545 ++
9546 ++ if (likely(skb)) {
9547 ++ /* Don't bother clearing the tail */
9548 ++ csk->wr_skb_head = WR_SKB_CB(skb)->next_wr;
9549 ++ WR_SKB_CB(skb)->next_wr = NULL;
9550 ++ }
9551 ++ return skb;
9552 ++}
9553 + #endif
9554 +diff --git a/drivers/crypto/chelsio/chtls/chtls_hw.c b/drivers/crypto/chelsio/chtls/chtls_hw.c
9555 +index 490960755864..64d24823c65a 100644
9556 +--- a/drivers/crypto/chelsio/chtls/chtls_hw.c
9557 ++++ b/drivers/crypto/chelsio/chtls/chtls_hw.c
9558 +@@ -361,6 +361,7 @@ int chtls_setkey(struct chtls_sock *csk, u32 keylen, u32 optname)
9559 + kwr->sc_imm.cmd_more = cpu_to_be32(ULPTX_CMD_V(ULP_TX_SC_IMM));
9560 + kwr->sc_imm.len = cpu_to_be32(klen);
9561 +
9562 ++ lock_sock(sk);
9563 + /* key info */
9564 + kctx = (struct _key_ctx *)(kwr + 1);
9565 + ret = chtls_key_info(csk, kctx, keylen, optname);
9566 +@@ -399,8 +400,10 @@ int chtls_setkey(struct chtls_sock *csk, u32 keylen, u32 optname)
9567 + csk->tlshws.txkey = keyid;
9568 + }
9569 +
9570 ++ release_sock(sk);
9571 + return ret;
9572 + out_notcb:
9573 ++ release_sock(sk);
9574 + free_tls_keyid(sk);
9575 + out_nokey:
9576 + kfree_skb(skb);
9577 +diff --git a/drivers/devfreq/Kconfig b/drivers/devfreq/Kconfig
9578 +index 6a172d338f6d..4c4ec68b0566 100644
9579 +--- a/drivers/devfreq/Kconfig
9580 ++++ b/drivers/devfreq/Kconfig
9581 +@@ -103,7 +103,8 @@ config ARM_TEGRA_DEVFREQ
9582 +
9583 + config ARM_RK3399_DMC_DEVFREQ
9584 + tristate "ARM RK3399 DMC DEVFREQ Driver"
9585 +- depends on ARCH_ROCKCHIP
9586 ++ depends on (ARCH_ROCKCHIP && HAVE_ARM_SMCCC) || \
9587 ++ (COMPILE_TEST && HAVE_ARM_SMCCC)
9588 + select DEVFREQ_EVENT_ROCKCHIP_DFI
9589 + select DEVFREQ_GOV_SIMPLE_ONDEMAND
9590 + select PM_DEVFREQ_EVENT
9591 +diff --git a/drivers/devfreq/event/Kconfig b/drivers/devfreq/event/Kconfig
9592 +index cd949800eed9..8851bc4e8e3e 100644
9593 +--- a/drivers/devfreq/event/Kconfig
9594 ++++ b/drivers/devfreq/event/Kconfig
9595 +@@ -33,7 +33,7 @@ config DEVFREQ_EVENT_EXYNOS_PPMU
9596 +
9597 + config DEVFREQ_EVENT_ROCKCHIP_DFI
9598 + tristate "ROCKCHIP DFI DEVFREQ event Driver"
9599 +- depends on ARCH_ROCKCHIP
9600 ++ depends on ARCH_ROCKCHIP || COMPILE_TEST
9601 + help
9602 + This add the devfreq-event driver for Rockchip SoC. It provides DFI
9603 + (DDR Monitor Module) driver to count ddr load.
9604 +diff --git a/drivers/dma/dmaengine.c b/drivers/dma/dmaengine.c
9605 +index f1a441ab395d..8a52a5efee4f 100644
9606 +--- a/drivers/dma/dmaengine.c
9607 ++++ b/drivers/dma/dmaengine.c
9608 +@@ -190,7 +190,7 @@ __dma_device_satisfies_mask(struct dma_device *device,
9609 +
9610 + static struct module *dma_chan_to_owner(struct dma_chan *chan)
9611 + {
9612 +- return chan->device->dev->driver->owner;
9613 ++ return chan->device->owner;
9614 + }
9615 +
9616 + /**
9617 +@@ -923,6 +923,8 @@ int dma_async_device_register(struct dma_device *device)
9618 + return -EIO;
9619 + }
9620 +
9621 ++ device->owner = device->dev->driver->owner;
9622 ++
9623 + if (dma_has_cap(DMA_MEMCPY, device->cap_mask) && !device->device_prep_dma_memcpy) {
9624 + dev_err(device->dev,
9625 + "Device claims capability %s, but op is not defined\n",
9626 +diff --git a/drivers/dma/imx-sdma.c b/drivers/dma/imx-sdma.c
9627 +index ceb82e74f5b4..d66a7fdff898 100644
9628 +--- a/drivers/dma/imx-sdma.c
9629 ++++ b/drivers/dma/imx-sdma.c
9630 +@@ -738,12 +738,8 @@ static void sdma_start_desc(struct sdma_channel *sdmac)
9631 + return;
9632 + }
9633 + sdmac->desc = desc = to_sdma_desc(&vd->tx);
9634 +- /*
9635 +- * Do not delete the node in desc_issued list in cyclic mode, otherwise
9636 +- * the desc allocated will never be freed in vchan_dma_desc_free_list
9637 +- */
9638 +- if (!(sdmac->flags & IMX_DMA_SG_LOOP))
9639 +- list_del(&vd->node);
9640 ++
9641 ++ list_del(&vd->node);
9642 +
9643 + sdma->channel_control[channel].base_bd_ptr = desc->bd_phys;
9644 + sdma->channel_control[channel].current_bd_ptr = desc->bd_phys;
9645 +@@ -1044,7 +1040,6 @@ static void sdma_channel_terminate_work(struct work_struct *work)
9646 +
9647 + spin_lock_irqsave(&sdmac->vc.lock, flags);
9648 + vchan_get_all_descriptors(&sdmac->vc, &head);
9649 +- sdmac->desc = NULL;
9650 + spin_unlock_irqrestore(&sdmac->vc.lock, flags);
9651 + vchan_dma_desc_free_list(&sdmac->vc, &head);
9652 + }
9653 +@@ -1052,11 +1047,19 @@ static void sdma_channel_terminate_work(struct work_struct *work)
9654 + static int sdma_disable_channel_async(struct dma_chan *chan)
9655 + {
9656 + struct sdma_channel *sdmac = to_sdma_chan(chan);
9657 ++ unsigned long flags;
9658 ++
9659 ++ spin_lock_irqsave(&sdmac->vc.lock, flags);
9660 +
9661 + sdma_disable_channel(chan);
9662 +
9663 +- if (sdmac->desc)
9664 ++ if (sdmac->desc) {
9665 ++ vchan_terminate_vdesc(&sdmac->desc->vd);
9666 ++ sdmac->desc = NULL;
9667 + schedule_work(&sdmac->terminate_worker);
9668 ++ }
9669 ++
9670 ++ spin_unlock_irqrestore(&sdmac->vc.lock, flags);
9671 +
9672 + return 0;
9673 + }
9674 +diff --git a/drivers/gpio/gpio-grgpio.c b/drivers/gpio/gpio-grgpio.c
9675 +index 60a1556c570a..c1be299e5567 100644
9676 +--- a/drivers/gpio/gpio-grgpio.c
9677 ++++ b/drivers/gpio/gpio-grgpio.c
9678 +@@ -258,17 +258,16 @@ static int grgpio_irq_map(struct irq_domain *d, unsigned int irq,
9679 + lirq->irq = irq;
9680 + uirq = &priv->uirqs[lirq->index];
9681 + if (uirq->refcnt == 0) {
9682 ++ spin_unlock_irqrestore(&priv->gc.bgpio_lock, flags);
9683 + ret = request_irq(uirq->uirq, grgpio_irq_handler, 0,
9684 + dev_name(priv->dev), priv);
9685 + if (ret) {
9686 + dev_err(priv->dev,
9687 + "Could not request underlying irq %d\n",
9688 + uirq->uirq);
9689 +-
9690 +- spin_unlock_irqrestore(&priv->gc.bgpio_lock, flags);
9691 +-
9692 + return ret;
9693 + }
9694 ++ spin_lock_irqsave(&priv->gc.bgpio_lock, flags);
9695 + }
9696 + uirq->refcnt++;
9697 +
9698 +@@ -314,8 +313,11 @@ static void grgpio_irq_unmap(struct irq_domain *d, unsigned int irq)
9699 + if (index >= 0) {
9700 + uirq = &priv->uirqs[lirq->index];
9701 + uirq->refcnt--;
9702 +- if (uirq->refcnt == 0)
9703 ++ if (uirq->refcnt == 0) {
9704 ++ spin_unlock_irqrestore(&priv->gc.bgpio_lock, flags);
9705 + free_irq(uirq->uirq, priv);
9706 ++ return;
9707 ++ }
9708 + }
9709 +
9710 + spin_unlock_irqrestore(&priv->gc.bgpio_lock, flags);
9711 +diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_atombios.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_atombios.c
9712 +index bf872f694f50..d1fbaea91f58 100644
9713 +--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_atombios.c
9714 ++++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_atombios.c
9715 +@@ -337,17 +337,9 @@ bool amdgpu_atombios_get_connector_info_from_object_table(struct amdgpu_device *
9716 + path_size += le16_to_cpu(path->usSize);
9717 +
9718 + if (device_support & le16_to_cpu(path->usDeviceTag)) {
9719 +- uint8_t con_obj_id, con_obj_num, con_obj_type;
9720 +-
9721 +- con_obj_id =
9722 ++ uint8_t con_obj_id =
9723 + (le16_to_cpu(path->usConnObjectId) & OBJECT_ID_MASK)
9724 + >> OBJECT_ID_SHIFT;
9725 +- con_obj_num =
9726 +- (le16_to_cpu(path->usConnObjectId) & ENUM_ID_MASK)
9727 +- >> ENUM_ID_SHIFT;
9728 +- con_obj_type =
9729 +- (le16_to_cpu(path->usConnObjectId) &
9730 +- OBJECT_TYPE_MASK) >> OBJECT_TYPE_SHIFT;
9731 +
9732 + /* Skip TV/CV support */
9733 + if ((le16_to_cpu(path->usDeviceTag) ==
9734 +@@ -372,14 +364,7 @@ bool amdgpu_atombios_get_connector_info_from_object_table(struct amdgpu_device *
9735 + router.ddc_valid = false;
9736 + router.cd_valid = false;
9737 + for (j = 0; j < ((le16_to_cpu(path->usSize) - 8) / 2); j++) {
9738 +- uint8_t grph_obj_id, grph_obj_num, grph_obj_type;
9739 +-
9740 +- grph_obj_id =
9741 +- (le16_to_cpu(path->usGraphicObjIds[j]) &
9742 +- OBJECT_ID_MASK) >> OBJECT_ID_SHIFT;
9743 +- grph_obj_num =
9744 +- (le16_to_cpu(path->usGraphicObjIds[j]) &
9745 +- ENUM_ID_MASK) >> ENUM_ID_SHIFT;
9746 ++ uint8_t grph_obj_type=
9747 + grph_obj_type =
9748 + (le16_to_cpu(path->usGraphicObjIds[j]) &
9749 + OBJECT_TYPE_MASK) >> OBJECT_TYPE_SHIFT;
9750 +diff --git a/drivers/gpu/drm/amd/amdgpu/soc15_common.h b/drivers/gpu/drm/amd/amdgpu/soc15_common.h
9751 +index 0942f492d2e1..9d444f7fdfcd 100644
9752 +--- a/drivers/gpu/drm/amd/amdgpu/soc15_common.h
9753 ++++ b/drivers/gpu/drm/amd/amdgpu/soc15_common.h
9754 +@@ -51,6 +51,7 @@
9755 + do { \
9756 + uint32_t tmp_ = RREG32(adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg); \
9757 + uint32_t loop = adev->usec_timeout; \
9758 ++ ret = 0; \
9759 + while ((tmp_ & (mask)) != (expected_value)) { \
9760 + udelay(2); \
9761 + tmp_ = RREG32(adev->reg_offset[ip##_HWIP][inst][reg##_BASE_IDX] + reg); \
9762 +diff --git a/drivers/gpu/drm/amd/display/dc/calcs/dcn_calc_math.h b/drivers/gpu/drm/amd/display/dc/calcs/dcn_calc_math.h
9763 +deleted file mode 100644
9764 +index f46ab0e24ca1..000000000000
9765 +--- a/drivers/gpu/drm/amd/display/dc/calcs/dcn_calc_math.h
9766 ++++ /dev/null
9767 +@@ -1,40 +0,0 @@
9768 +-/*
9769 +- * Copyright 2017 Advanced Micro Devices, Inc.
9770 +- *
9771 +- * Permission is hereby granted, free of charge, to any person obtaining a
9772 +- * copy of this software and associated documentation files (the "Software"),
9773 +- * to deal in the Software without restriction, including without limitation
9774 +- * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9775 +- * and/or sell copies of the Software, and to permit persons to whom the
9776 +- * Software is furnished to do so, subject to the following conditions:
9777 +- *
9778 +- * The above copyright notice and this permission notice shall be included in
9779 +- * all copies or substantial portions of the Software.
9780 +- *
9781 +- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
9782 +- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
9783 +- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
9784 +- * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
9785 +- * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
9786 +- * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
9787 +- * OTHER DEALINGS IN THE SOFTWARE.
9788 +- *
9789 +- * Authors: AMD
9790 +- *
9791 +- */
9792 +-
9793 +-#ifndef _DCN_CALC_MATH_H_
9794 +-#define _DCN_CALC_MATH_H_
9795 +-
9796 +-float dcn_bw_mod(const float arg1, const float arg2);
9797 +-float dcn_bw_min2(const float arg1, const float arg2);
9798 +-unsigned int dcn_bw_max(const unsigned int arg1, const unsigned int arg2);
9799 +-float dcn_bw_max2(const float arg1, const float arg2);
9800 +-float dcn_bw_floor2(const float arg, const float significance);
9801 +-float dcn_bw_ceil2(const float arg, const float significance);
9802 +-float dcn_bw_max3(float v1, float v2, float v3);
9803 +-float dcn_bw_max5(float v1, float v2, float v3, float v4, float v5);
9804 +-float dcn_bw_pow(float a, float exp);
9805 +-float dcn_bw_log(float a, float b);
9806 +-
9807 +-#endif /* _DCN_CALC_MATH_H_ */
9808 +diff --git a/drivers/gpu/drm/amd/display/dc/calcs/dcn_calcs.c b/drivers/gpu/drm/amd/display/dc/calcs/dcn_calcs.c
9809 +index 6342f6499351..b0956c360393 100644
9810 +--- a/drivers/gpu/drm/amd/display/dc/calcs/dcn_calcs.c
9811 ++++ b/drivers/gpu/drm/amd/display/dc/calcs/dcn_calcs.c
9812 +@@ -1346,6 +1346,7 @@ void dcn_bw_update_from_pplib(struct dc *dc)
9813 + struct dc_context *ctx = dc->ctx;
9814 + struct dm_pp_clock_levels_with_voltage fclks = {0}, dcfclks = {0};
9815 + bool res;
9816 ++ unsigned vmin0p65_idx, vmid0p72_idx, vnom0p8_idx, vmax0p9_idx;
9817 +
9818 + /* TODO: This is not the proper way to obtain fabric_and_dram_bandwidth, should be min(fclk, memclk) */
9819 + res = dm_pp_get_clock_levels_by_type_with_voltage(
9820 +@@ -1357,17 +1358,28 @@ void dcn_bw_update_from_pplib(struct dc *dc)
9821 + res = verify_clock_values(&fclks);
9822 +
9823 + if (res) {
9824 +- ASSERT(fclks.num_levels >= 3);
9825 +- dc->dcn_soc->fabric_and_dram_bandwidth_vmin0p65 = 32 * (fclks.data[0].clocks_in_khz / 1000.0) / 1000.0;
9826 +- dc->dcn_soc->fabric_and_dram_bandwidth_vmid0p72 = dc->dcn_soc->number_of_channels *
9827 +- (fclks.data[fclks.num_levels - (fclks.num_levels > 2 ? 3 : 2)].clocks_in_khz / 1000.0)
9828 +- * ddr4_dram_factor_single_Channel / 1000.0;
9829 +- dc->dcn_soc->fabric_and_dram_bandwidth_vnom0p8 = dc->dcn_soc->number_of_channels *
9830 +- (fclks.data[fclks.num_levels - 2].clocks_in_khz / 1000.0)
9831 +- * ddr4_dram_factor_single_Channel / 1000.0;
9832 +- dc->dcn_soc->fabric_and_dram_bandwidth_vmax0p9 = dc->dcn_soc->number_of_channels *
9833 +- (fclks.data[fclks.num_levels - 1].clocks_in_khz / 1000.0)
9834 +- * ddr4_dram_factor_single_Channel / 1000.0;
9835 ++ ASSERT(fclks.num_levels);
9836 ++
9837 ++ vmin0p65_idx = 0;
9838 ++ vmid0p72_idx = fclks.num_levels -
9839 ++ (fclks.num_levels > 2 ? 3 : (fclks.num_levels > 1 ? 2 : 1));
9840 ++ vnom0p8_idx = fclks.num_levels - (fclks.num_levels > 1 ? 2 : 1);
9841 ++ vmax0p9_idx = fclks.num_levels - 1;
9842 ++
9843 ++ dc->dcn_soc->fabric_and_dram_bandwidth_vmin0p65 =
9844 ++ 32 * (fclks.data[vmin0p65_idx].clocks_in_khz / 1000.0) / 1000.0;
9845 ++ dc->dcn_soc->fabric_and_dram_bandwidth_vmid0p72 =
9846 ++ dc->dcn_soc->number_of_channels *
9847 ++ (fclks.data[vmid0p72_idx].clocks_in_khz / 1000.0)
9848 ++ * ddr4_dram_factor_single_Channel / 1000.0;
9849 ++ dc->dcn_soc->fabric_and_dram_bandwidth_vnom0p8 =
9850 ++ dc->dcn_soc->number_of_channels *
9851 ++ (fclks.data[vnom0p8_idx].clocks_in_khz / 1000.0)
9852 ++ * ddr4_dram_factor_single_Channel / 1000.0;
9853 ++ dc->dcn_soc->fabric_and_dram_bandwidth_vmax0p9 =
9854 ++ dc->dcn_soc->number_of_channels *
9855 ++ (fclks.data[vmax0p9_idx].clocks_in_khz / 1000.0)
9856 ++ * ddr4_dram_factor_single_Channel / 1000.0;
9857 + } else
9858 + BREAK_TO_DEBUGGER();
9859 +
9860 +diff --git a/drivers/gpu/drm/amd/display/dc/core/dc_link.c b/drivers/gpu/drm/amd/display/dc/core/dc_link.c
9861 +index 2f42964fb9f4..3abc0294c05f 100644
9862 +--- a/drivers/gpu/drm/amd/display/dc/core/dc_link.c
9863 ++++ b/drivers/gpu/drm/amd/display/dc/core/dc_link.c
9864 +@@ -780,8 +780,7 @@ bool dc_link_detect(struct dc_link *link, enum dc_detect_reason reason)
9865 + same_edid = is_same_edid(&prev_sink->dc_edid, &sink->dc_edid);
9866 +
9867 + if (link->connector_signal == SIGNAL_TYPE_DISPLAY_PORT &&
9868 +- sink_caps.transaction_type == DDC_TRANSACTION_TYPE_I2C_OVER_AUX &&
9869 +- reason != DETECT_REASON_HPDRX) {
9870 ++ sink_caps.transaction_type == DDC_TRANSACTION_TYPE_I2C_OVER_AUX) {
9871 + /*
9872 + * TODO debug why Dell 2413 doesn't like
9873 + * two link trainings
9874 +diff --git a/drivers/gpu/drm/amd/display/dc/dml/dml_common_defs.c b/drivers/gpu/drm/amd/display/dc/dml/dml_common_defs.c
9875 +index b953b02a1512..723af0b2dda0 100644
9876 +--- a/drivers/gpu/drm/amd/display/dc/dml/dml_common_defs.c
9877 ++++ b/drivers/gpu/drm/amd/display/dc/dml/dml_common_defs.c
9878 +@@ -24,7 +24,7 @@
9879 + */
9880 +
9881 + #include "dml_common_defs.h"
9882 +-#include "../calcs/dcn_calc_math.h"
9883 ++#include "dcn_calc_math.h"
9884 +
9885 + #include "dml_inline_defs.h"
9886 +
9887 +diff --git a/drivers/gpu/drm/amd/display/dc/dml/dml_inline_defs.h b/drivers/gpu/drm/amd/display/dc/dml/dml_inline_defs.h
9888 +index e8ce08567cd8..e4f595a3038c 100644
9889 +--- a/drivers/gpu/drm/amd/display/dc/dml/dml_inline_defs.h
9890 ++++ b/drivers/gpu/drm/amd/display/dc/dml/dml_inline_defs.h
9891 +@@ -27,7 +27,7 @@
9892 + #define __DML_INLINE_DEFS_H__
9893 +
9894 + #include "dml_common_defs.h"
9895 +-#include "../calcs/dcn_calc_math.h"
9896 ++#include "dcn_calc_math.h"
9897 + #include "dml_logger.h"
9898 +
9899 + static inline double dml_min(double a, double b)
9900 +diff --git a/drivers/gpu/drm/amd/display/dc/inc/dcn_calc_math.h b/drivers/gpu/drm/amd/display/dc/inc/dcn_calc_math.h
9901 +new file mode 100644
9902 +index 000000000000..f46ab0e24ca1
9903 +--- /dev/null
9904 ++++ b/drivers/gpu/drm/amd/display/dc/inc/dcn_calc_math.h
9905 +@@ -0,0 +1,40 @@
9906 ++/*
9907 ++ * Copyright 2017 Advanced Micro Devices, Inc.
9908 ++ *
9909 ++ * Permission is hereby granted, free of charge, to any person obtaining a
9910 ++ * copy of this software and associated documentation files (the "Software"),
9911 ++ * to deal in the Software without restriction, including without limitation
9912 ++ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9913 ++ * and/or sell copies of the Software, and to permit persons to whom the
9914 ++ * Software is furnished to do so, subject to the following conditions:
9915 ++ *
9916 ++ * The above copyright notice and this permission notice shall be included in
9917 ++ * all copies or substantial portions of the Software.
9918 ++ *
9919 ++ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
9920 ++ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
9921 ++ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
9922 ++ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
9923 ++ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
9924 ++ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
9925 ++ * OTHER DEALINGS IN THE SOFTWARE.
9926 ++ *
9927 ++ * Authors: AMD
9928 ++ *
9929 ++ */
9930 ++
9931 ++#ifndef _DCN_CALC_MATH_H_
9932 ++#define _DCN_CALC_MATH_H_
9933 ++
9934 ++float dcn_bw_mod(const float arg1, const float arg2);
9935 ++float dcn_bw_min2(const float arg1, const float arg2);
9936 ++unsigned int dcn_bw_max(const unsigned int arg1, const unsigned int arg2);
9937 ++float dcn_bw_max2(const float arg1, const float arg2);
9938 ++float dcn_bw_floor2(const float arg, const float significance);
9939 ++float dcn_bw_ceil2(const float arg, const float significance);
9940 ++float dcn_bw_max3(float v1, float v2, float v3);
9941 ++float dcn_bw_max5(float v1, float v2, float v3, float v4, float v5);
9942 ++float dcn_bw_pow(float a, float exp);
9943 ++float dcn_bw_log(float a, float b);
9944 ++
9945 ++#endif /* _DCN_CALC_MATH_H_ */
9946 +diff --git a/drivers/gpu/drm/amd/powerplay/hwmgr/smu10_hwmgr.c b/drivers/gpu/drm/amd/powerplay/hwmgr/smu10_hwmgr.c
9947 +index 1546bc49004f..48e31711bc68 100644
9948 +--- a/drivers/gpu/drm/amd/powerplay/hwmgr/smu10_hwmgr.c
9949 ++++ b/drivers/gpu/drm/amd/powerplay/hwmgr/smu10_hwmgr.c
9950 +@@ -994,12 +994,15 @@ static int smu10_get_clock_by_type_with_latency(struct pp_hwmgr *hwmgr,
9951 +
9952 + clocks->num_levels = 0;
9953 + for (i = 0; i < pclk_vol_table->count; i++) {
9954 +- clocks->data[i].clocks_in_khz = pclk_vol_table->entries[i].clk * 10;
9955 +- clocks->data[i].latency_in_us = latency_required ?
9956 +- smu10_get_mem_latency(hwmgr,
9957 +- pclk_vol_table->entries[i].clk) :
9958 +- 0;
9959 +- clocks->num_levels++;
9960 ++ if (pclk_vol_table->entries[i].clk) {
9961 ++ clocks->data[clocks->num_levels].clocks_in_khz =
9962 ++ pclk_vol_table->entries[i].clk * 10;
9963 ++ clocks->data[clocks->num_levels].latency_in_us = latency_required ?
9964 ++ smu10_get_mem_latency(hwmgr,
9965 ++ pclk_vol_table->entries[i].clk) :
9966 ++ 0;
9967 ++ clocks->num_levels++;
9968 ++ }
9969 + }
9970 +
9971 + return 0;
9972 +@@ -1045,9 +1048,11 @@ static int smu10_get_clock_by_type_with_voltage(struct pp_hwmgr *hwmgr,
9973 +
9974 + clocks->num_levels = 0;
9975 + for (i = 0; i < pclk_vol_table->count; i++) {
9976 +- clocks->data[i].clocks_in_khz = pclk_vol_table->entries[i].clk * 10;
9977 +- clocks->data[i].voltage_in_mv = pclk_vol_table->entries[i].vol;
9978 +- clocks->num_levels++;
9979 ++ if (pclk_vol_table->entries[i].clk) {
9980 ++ clocks->data[clocks->num_levels].clocks_in_khz = pclk_vol_table->entries[i].clk * 10;
9981 ++ clocks->data[clocks->num_levels].voltage_in_mv = pclk_vol_table->entries[i].vol;
9982 ++ clocks->num_levels++;
9983 ++ }
9984 + }
9985 +
9986 + return 0;
9987 +diff --git a/drivers/gpu/drm/drm_debugfs_crc.c b/drivers/gpu/drm/drm_debugfs_crc.c
9988 +index c88e5ff41add..a3c756710845 100644
9989 +--- a/drivers/gpu/drm/drm_debugfs_crc.c
9990 ++++ b/drivers/gpu/drm/drm_debugfs_crc.c
9991 +@@ -101,8 +101,8 @@ static ssize_t crc_control_write(struct file *file, const char __user *ubuf,
9992 + if (IS_ERR(source))
9993 + return PTR_ERR(source);
9994 +
9995 +- if (source[len] == '\n')
9996 +- source[len] = '\0';
9997 ++ if (source[len - 1] == '\n')
9998 ++ source[len - 1] = '\0';
9999 +
10000 + spin_lock_irq(&crc->lock);
10001 +
10002 +diff --git a/drivers/gpu/drm/gma500/framebuffer.c b/drivers/gpu/drm/gma500/framebuffer.c
10003 +index adefae58b5fc..b4035ef72af8 100644
10004 +--- a/drivers/gpu/drm/gma500/framebuffer.c
10005 ++++ b/drivers/gpu/drm/gma500/framebuffer.c
10006 +@@ -480,6 +480,7 @@ static int psbfb_probe(struct drm_fb_helper *helper,
10007 + container_of(helper, struct psb_fbdev, psb_fb_helper);
10008 + struct drm_device *dev = psb_fbdev->psb_fb_helper.dev;
10009 + struct drm_psb_private *dev_priv = dev->dev_private;
10010 ++ unsigned int fb_size;
10011 + int bytespp;
10012 +
10013 + bytespp = sizes->surface_bpp / 8;
10014 +@@ -489,8 +490,11 @@ static int psbfb_probe(struct drm_fb_helper *helper,
10015 + /* If the mode will not fit in 32bit then switch to 16bit to get
10016 + a console on full resolution. The X mode setting server will
10017 + allocate its own 32bit GEM framebuffer */
10018 +- if (ALIGN(sizes->fb_width * bytespp, 64) * sizes->fb_height >
10019 +- dev_priv->vram_stolen_size) {
10020 ++ fb_size = ALIGN(sizes->surface_width * bytespp, 64) *
10021 ++ sizes->surface_height;
10022 ++ fb_size = ALIGN(fb_size, PAGE_SIZE);
10023 ++
10024 ++ if (fb_size > dev_priv->vram_stolen_size) {
10025 + sizes->surface_bpp = 16;
10026 + sizes->surface_depth = 16;
10027 + }
10028 +diff --git a/drivers/gpu/drm/mediatek/mtk_drm_crtc.c b/drivers/gpu/drm/mediatek/mtk_drm_crtc.c
10029 +index 92ecb9bf982c..b86ee7d25af3 100644
10030 +--- a/drivers/gpu/drm/mediatek/mtk_drm_crtc.c
10031 ++++ b/drivers/gpu/drm/mediatek/mtk_drm_crtc.c
10032 +@@ -308,6 +308,7 @@ err_pm_runtime_put:
10033 + static void mtk_crtc_ddp_hw_fini(struct mtk_drm_crtc *mtk_crtc)
10034 + {
10035 + struct drm_device *drm = mtk_crtc->base.dev;
10036 ++ struct drm_crtc *crtc = &mtk_crtc->base;
10037 + int i;
10038 +
10039 + DRM_DEBUG_DRIVER("%s\n", __func__);
10040 +@@ -329,6 +330,13 @@ static void mtk_crtc_ddp_hw_fini(struct mtk_drm_crtc *mtk_crtc)
10041 + mtk_disp_mutex_unprepare(mtk_crtc->mutex);
10042 +
10043 + pm_runtime_put(drm->dev);
10044 ++
10045 ++ if (crtc->state->event && !crtc->state->active) {
10046 ++ spin_lock_irq(&crtc->dev->event_lock);
10047 ++ drm_crtc_send_vblank_event(crtc, crtc->state->event);
10048 ++ crtc->state->event = NULL;
10049 ++ spin_unlock_irq(&crtc->dev->event_lock);
10050 ++ }
10051 + }
10052 +
10053 + static void mtk_crtc_ddp_config(struct drm_crtc *crtc)
10054 +diff --git a/drivers/gpu/drm/nouveau/nouveau_fence.c b/drivers/gpu/drm/nouveau/nouveau_fence.c
10055 +index 412d49bc6e56..ba3883aed456 100644
10056 +--- a/drivers/gpu/drm/nouveau/nouveau_fence.c
10057 ++++ b/drivers/gpu/drm/nouveau/nouveau_fence.c
10058 +@@ -157,7 +157,7 @@ nouveau_fence_wait_uevent_handler(struct nvif_notify *notify)
10059 +
10060 + fence = list_entry(fctx->pending.next, typeof(*fence), head);
10061 + chan = rcu_dereference_protected(fence->channel, lockdep_is_held(&fctx->lock));
10062 +- if (nouveau_fence_update(fence->channel, fctx))
10063 ++ if (nouveau_fence_update(chan, fctx))
10064 + ret = NVIF_NOTIFY_DROP;
10065 + }
10066 + spin_unlock_irqrestore(&fctx->lock, flags);
10067 +diff --git a/drivers/gpu/drm/nouveau/nouveau_ttm.c b/drivers/gpu/drm/nouveau/nouveau_ttm.c
10068 +index e4b977cc8452..37715a2a2f3f 100644
10069 +--- a/drivers/gpu/drm/nouveau/nouveau_ttm.c
10070 ++++ b/drivers/gpu/drm/nouveau/nouveau_ttm.c
10071 +@@ -63,14 +63,12 @@ nouveau_vram_manager_new(struct ttm_mem_type_manager *man,
10072 + {
10073 + struct nouveau_bo *nvbo = nouveau_bo(bo);
10074 + struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
10075 +- struct nouveau_mem *mem;
10076 + int ret;
10077 +
10078 + if (drm->client.device.info.ram_size == 0)
10079 + return -ENOMEM;
10080 +
10081 + ret = nouveau_mem_new(&drm->master, nvbo->kind, nvbo->comp, reg);
10082 +- mem = nouveau_mem(reg);
10083 + if (ret)
10084 + return ret;
10085 +
10086 +@@ -103,11 +101,9 @@ nouveau_gart_manager_new(struct ttm_mem_type_manager *man,
10087 + {
10088 + struct nouveau_bo *nvbo = nouveau_bo(bo);
10089 + struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
10090 +- struct nouveau_mem *mem;
10091 + int ret;
10092 +
10093 + ret = nouveau_mem_new(&drm->master, nvbo->kind, nvbo->comp, reg);
10094 +- mem = nouveau_mem(reg);
10095 + if (ret)
10096 + return ret;
10097 +
10098 +diff --git a/drivers/gpu/drm/nouveau/nvkm/core/memory.c b/drivers/gpu/drm/nouveau/nvkm/core/memory.c
10099 +index e85a08ecd9da..4cc186262d34 100644
10100 +--- a/drivers/gpu/drm/nouveau/nvkm/core/memory.c
10101 ++++ b/drivers/gpu/drm/nouveau/nvkm/core/memory.c
10102 +@@ -91,8 +91,8 @@ nvkm_memory_tags_get(struct nvkm_memory *memory, struct nvkm_device *device,
10103 + }
10104 +
10105 + refcount_set(&tags->refcount, 1);
10106 ++ *ptags = memory->tags = tags;
10107 + mutex_unlock(&fb->subdev.mutex);
10108 +- *ptags = tags;
10109 + return 0;
10110 + }
10111 +
10112 +diff --git a/drivers/gpu/drm/nouveau/nvkm/engine/disp/channv50.c b/drivers/gpu/drm/nouveau/nvkm/engine/disp/channv50.c
10113 +index bcf32d92ee5a..50e3539f33d2 100644
10114 +--- a/drivers/gpu/drm/nouveau/nvkm/engine/disp/channv50.c
10115 ++++ b/drivers/gpu/drm/nouveau/nvkm/engine/disp/channv50.c
10116 +@@ -74,6 +74,8 @@ nv50_disp_chan_mthd(struct nv50_disp_chan *chan, int debug)
10117 +
10118 + if (debug > subdev->debug)
10119 + return;
10120 ++ if (!mthd)
10121 ++ return;
10122 +
10123 + for (i = 0; (list = mthd->data[i].mthd) != NULL; i++) {
10124 + u32 base = chan->head * mthd->addr;
10125 +diff --git a/drivers/gpu/drm/nouveau/nvkm/engine/gr/gk20a.c b/drivers/gpu/drm/nouveau/nvkm/engine/gr/gk20a.c
10126 +index 500cb08dd608..b57ab5cea9a1 100644
10127 +--- a/drivers/gpu/drm/nouveau/nvkm/engine/gr/gk20a.c
10128 ++++ b/drivers/gpu/drm/nouveau/nvkm/engine/gr/gk20a.c
10129 +@@ -143,23 +143,24 @@ gk20a_gr_av_to_method(struct gf100_gr *gr, const char *fw_name,
10130 +
10131 + nent = (fuc.size / sizeof(struct gk20a_fw_av));
10132 +
10133 +- pack = vzalloc((sizeof(*pack) * max_classes) +
10134 +- (sizeof(*init) * (nent + 1)));
10135 ++ pack = vzalloc((sizeof(*pack) * (max_classes + 1)) +
10136 ++ (sizeof(*init) * (nent + max_classes + 1)));
10137 + if (!pack) {
10138 + ret = -ENOMEM;
10139 + goto end;
10140 + }
10141 +
10142 +- init = (void *)(pack + max_classes);
10143 ++ init = (void *)(pack + max_classes + 1);
10144 +
10145 +- for (i = 0; i < nent; i++) {
10146 +- struct gf100_gr_init *ent = &init[i];
10147 ++ for (i = 0; i < nent; i++, init++) {
10148 + struct gk20a_fw_av *av = &((struct gk20a_fw_av *)fuc.data)[i];
10149 + u32 class = av->addr & 0xffff;
10150 + u32 addr = (av->addr & 0xffff0000) >> 14;
10151 +
10152 + if (prevclass != class) {
10153 +- pack[classidx].init = ent;
10154 ++ if (prevclass) /* Add terminator to the method list. */
10155 ++ init++;
10156 ++ pack[classidx].init = init;
10157 + pack[classidx].type = class;
10158 + prevclass = class;
10159 + if (++classidx >= max_classes) {
10160 +@@ -169,10 +170,10 @@ gk20a_gr_av_to_method(struct gf100_gr *gr, const char *fw_name,
10161 + }
10162 + }
10163 +
10164 +- ent->addr = addr;
10165 +- ent->data = av->data;
10166 +- ent->count = 1;
10167 +- ent->pitch = 1;
10168 ++ init->addr = addr;
10169 ++ init->data = av->data;
10170 ++ init->count = 1;
10171 ++ init->pitch = 1;
10172 + }
10173 +
10174 + *ppack = pack;
10175 +diff --git a/drivers/gpu/drm/nouveau/nvkm/subdev/fault/base.c b/drivers/gpu/drm/nouveau/nvkm/subdev/fault/base.c
10176 +index 16ad91c91a7b..f18ce6ff5b7e 100644
10177 +--- a/drivers/gpu/drm/nouveau/nvkm/subdev/fault/base.c
10178 ++++ b/drivers/gpu/drm/nouveau/nvkm/subdev/fault/base.c
10179 +@@ -150,6 +150,7 @@ nvkm_fault_dtor(struct nvkm_subdev *subdev)
10180 + struct nvkm_fault *fault = nvkm_fault(subdev);
10181 + int i;
10182 +
10183 ++ nvkm_notify_fini(&fault->nrpfb);
10184 + nvkm_event_fini(&fault->event);
10185 +
10186 + for (i = 0; i < fault->buffer_nr; i++) {
10187 +diff --git a/drivers/gpu/drm/nouveau/nvkm/subdev/secboot/gm20b.c b/drivers/gpu/drm/nouveau/nvkm/subdev/secboot/gm20b.c
10188 +index df8b919dcf09..ace6fefba428 100644
10189 +--- a/drivers/gpu/drm/nouveau/nvkm/subdev/secboot/gm20b.c
10190 ++++ b/drivers/gpu/drm/nouveau/nvkm/subdev/secboot/gm20b.c
10191 +@@ -108,6 +108,7 @@ gm20b_secboot_new(struct nvkm_device *device, int index,
10192 + struct gm200_secboot *gsb;
10193 + struct nvkm_acr *acr;
10194 +
10195 ++ *psb = NULL;
10196 + acr = acr_r352_new(BIT(NVKM_SECBOOT_FALCON_FECS) |
10197 + BIT(NVKM_SECBOOT_FALCON_PMU));
10198 + if (IS_ERR(acr))
10199 +@@ -116,10 +117,8 @@ gm20b_secboot_new(struct nvkm_device *device, int index,
10200 + acr->optional_falcons = BIT(NVKM_SECBOOT_FALCON_PMU);
10201 +
10202 + gsb = kzalloc(sizeof(*gsb), GFP_KERNEL);
10203 +- if (!gsb) {
10204 +- psb = NULL;
10205 ++ if (!gsb)
10206 + return -ENOMEM;
10207 +- }
10208 + *psb = &gsb->base;
10209 +
10210 + ret = nvkm_secboot_ctor(&gm20b_secboot, acr, device, index, &gsb->base);
10211 +diff --git a/drivers/gpu/drm/radeon/radeon_display.c b/drivers/gpu/drm/radeon/radeon_display.c
10212 +index d8e2d7b3b836..7d1e14f0140a 100644
10213 +--- a/drivers/gpu/drm/radeon/radeon_display.c
10214 ++++ b/drivers/gpu/drm/radeon/radeon_display.c
10215 +@@ -121,6 +121,8 @@ static void dce5_crtc_load_lut(struct drm_crtc *crtc)
10216 +
10217 + DRM_DEBUG_KMS("%d\n", radeon_crtc->crtc_id);
10218 +
10219 ++ msleep(10);
10220 ++
10221 + WREG32(NI_INPUT_CSC_CONTROL + radeon_crtc->crtc_offset,
10222 + (NI_INPUT_CSC_GRPH_MODE(NI_INPUT_CSC_BYPASS) |
10223 + NI_INPUT_CSC_OVL_MODE(NI_INPUT_CSC_BYPASS)));
10224 +diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_cmdbuf_res.c b/drivers/gpu/drm/vmwgfx/vmwgfx_cmdbuf_res.c
10225 +index 3b75af9bf85f..f27bd7cff579 100644
10226 +--- a/drivers/gpu/drm/vmwgfx/vmwgfx_cmdbuf_res.c
10227 ++++ b/drivers/gpu/drm/vmwgfx/vmwgfx_cmdbuf_res.c
10228 +@@ -210,8 +210,10 @@ int vmw_cmdbuf_res_add(struct vmw_cmdbuf_res_manager *man,
10229 +
10230 + cres->hash.key = user_key | (res_type << 24);
10231 + ret = drm_ht_insert_item(&man->resources, &cres->hash);
10232 +- if (unlikely(ret != 0))
10233 ++ if (unlikely(ret != 0)) {
10234 ++ kfree(cres);
10235 + goto out_invalid_key;
10236 ++ }
10237 +
10238 + cres->state = VMW_CMDBUF_RES_ADD;
10239 + cres->res = vmw_resource_reference(res);
10240 +diff --git a/drivers/ide/cmd64x.c b/drivers/ide/cmd64x.c
10241 +index b127ed60c733..9dde8390da09 100644
10242 +--- a/drivers/ide/cmd64x.c
10243 ++++ b/drivers/ide/cmd64x.c
10244 +@@ -65,6 +65,9 @@ static void cmd64x_program_timings(ide_drive_t *drive, u8 mode)
10245 + struct ide_timing t;
10246 + u8 arttim = 0;
10247 +
10248 ++ if (drive->dn >= ARRAY_SIZE(drwtim_regs))
10249 ++ return;
10250 ++
10251 + ide_timing_compute(drive, mode, &t, T, 0);
10252 +
10253 + /*
10254 +diff --git a/drivers/ide/serverworks.c b/drivers/ide/serverworks.c
10255 +index a97affca18ab..0f57d45484d1 100644
10256 +--- a/drivers/ide/serverworks.c
10257 ++++ b/drivers/ide/serverworks.c
10258 +@@ -114,6 +114,9 @@ static void svwks_set_pio_mode(ide_hwif_t *hwif, ide_drive_t *drive)
10259 + struct pci_dev *dev = to_pci_dev(hwif->dev);
10260 + const u8 pio = drive->pio_mode - XFER_PIO_0;
10261 +
10262 ++ if (drive->dn >= ARRAY_SIZE(drive_pci))
10263 ++ return;
10264 ++
10265 + pci_write_config_byte(dev, drive_pci[drive->dn], pio_modes[pio]);
10266 +
10267 + if (svwks_csb_check(dev)) {
10268 +@@ -140,6 +143,9 @@ static void svwks_set_dma_mode(ide_hwif_t *hwif, ide_drive_t *drive)
10269 +
10270 + u8 ultra_enable = 0, ultra_timing = 0, dma_timing = 0;
10271 +
10272 ++ if (drive->dn >= ARRAY_SIZE(drive_pci2))
10273 ++ return;
10274 ++
10275 + pci_read_config_byte(dev, (0x56|hwif->channel), &ultra_timing);
10276 + pci_read_config_byte(dev, 0x54, &ultra_enable);
10277 +
10278 +diff --git a/drivers/infiniband/hw/hfi1/chip.c b/drivers/infiniband/hw/hfi1/chip.c
10279 +index b09a4b1cf397..1221faea75a6 100644
10280 +--- a/drivers/infiniband/hw/hfi1/chip.c
10281 ++++ b/drivers/infiniband/hw/hfi1/chip.c
10282 +@@ -1687,6 +1687,14 @@ static u64 access_sw_pio_drain(const struct cntr_entry *entry,
10283 + return dd->verbs_dev.n_piodrain;
10284 + }
10285 +
10286 ++static u64 access_sw_ctx0_seq_drop(const struct cntr_entry *entry,
10287 ++ void *context, int vl, int mode, u64 data)
10288 ++{
10289 ++ struct hfi1_devdata *dd = context;
10290 ++
10291 ++ return dd->ctx0_seq_drop;
10292 ++}
10293 ++
10294 + static u64 access_sw_vtx_wait(const struct cntr_entry *entry,
10295 + void *context, int vl, int mode, u64 data)
10296 + {
10297 +@@ -4247,6 +4255,8 @@ static struct cntr_entry dev_cntrs[DEV_CNTR_LAST] = {
10298 + access_sw_cpu_intr),
10299 + [C_SW_CPU_RCV_LIM] = CNTR_ELEM("RcvLimit", 0, 0, CNTR_NORMAL,
10300 + access_sw_cpu_rcv_limit),
10301 ++[C_SW_CTX0_SEQ_DROP] = CNTR_ELEM("SeqDrop0", 0, 0, CNTR_NORMAL,
10302 ++ access_sw_ctx0_seq_drop),
10303 + [C_SW_VTX_WAIT] = CNTR_ELEM("vTxWait", 0, 0, CNTR_NORMAL,
10304 + access_sw_vtx_wait),
10305 + [C_SW_PIO_WAIT] = CNTR_ELEM("PioWait", 0, 0, CNTR_NORMAL,
10306 +diff --git a/drivers/infiniband/hw/hfi1/chip.h b/drivers/infiniband/hw/hfi1/chip.h
10307 +index 36b04d6300e5..c9a352d8a7e1 100644
10308 +--- a/drivers/infiniband/hw/hfi1/chip.h
10309 ++++ b/drivers/infiniband/hw/hfi1/chip.h
10310 +@@ -909,6 +909,7 @@ enum {
10311 + C_DC_PG_STS_TX_MBE_CNT,
10312 + C_SW_CPU_INTR,
10313 + C_SW_CPU_RCV_LIM,
10314 ++ C_SW_CTX0_SEQ_DROP,
10315 + C_SW_VTX_WAIT,
10316 + C_SW_PIO_WAIT,
10317 + C_SW_PIO_DRAIN,
10318 +diff --git a/drivers/infiniband/hw/hfi1/driver.c b/drivers/infiniband/hw/hfi1/driver.c
10319 +index d5277c23cba6..769e114567a0 100644
10320 +--- a/drivers/infiniband/hw/hfi1/driver.c
10321 ++++ b/drivers/infiniband/hw/hfi1/driver.c
10322 +@@ -734,6 +734,7 @@ static noinline int skip_rcv_packet(struct hfi1_packet *packet, int thread)
10323 + {
10324 + int ret;
10325 +
10326 ++ packet->rcd->dd->ctx0_seq_drop++;
10327 + /* Set up for the next packet */
10328 + packet->rhqoff += packet->rsize;
10329 + if (packet->rhqoff >= packet->maxcnt)
10330 +diff --git a/drivers/infiniband/hw/hfi1/hfi.h b/drivers/infiniband/hw/hfi1/hfi.h
10331 +index ab981874c71c..e38de547785d 100644
10332 +--- a/drivers/infiniband/hw/hfi1/hfi.h
10333 ++++ b/drivers/infiniband/hw/hfi1/hfi.h
10334 +@@ -1093,6 +1093,8 @@ struct hfi1_devdata {
10335 +
10336 + char *boardname; /* human readable board info */
10337 +
10338 ++ u64 ctx0_seq_drop;
10339 ++
10340 + /* reset value */
10341 + u64 z_int_counter;
10342 + u64 z_rcv_limit;
10343 +diff --git a/drivers/infiniband/sw/rxe/rxe_verbs.h b/drivers/infiniband/sw/rxe/rxe_verbs.h
10344 +index 6a75f96b9096..b4e24362edbb 100644
10345 +--- a/drivers/infiniband/sw/rxe/rxe_verbs.h
10346 ++++ b/drivers/infiniband/sw/rxe/rxe_verbs.h
10347 +@@ -407,7 +407,7 @@ struct rxe_dev {
10348 + struct list_head pending_mmaps;
10349 +
10350 + spinlock_t mmap_offset_lock; /* guard mmap_offset */
10351 +- int mmap_offset;
10352 ++ u64 mmap_offset;
10353 +
10354 + atomic64_t stats_counters[RXE_NUM_OF_COUNTERS];
10355 +
10356 +diff --git a/drivers/input/touchscreen/edt-ft5x06.c b/drivers/input/touchscreen/edt-ft5x06.c
10357 +index 1e18ca0d1b4e..3fdaa644a82c 100644
10358 +--- a/drivers/input/touchscreen/edt-ft5x06.c
10359 ++++ b/drivers/input/touchscreen/edt-ft5x06.c
10360 +@@ -968,6 +968,7 @@ static int edt_ft5x06_ts_probe(struct i2c_client *client,
10361 + {
10362 + const struct edt_i2c_chip_data *chip_data;
10363 + struct edt_ft5x06_ts_data *tsdata;
10364 ++ u8 buf[2] = { 0xfc, 0x00 };
10365 + struct input_dev *input;
10366 + unsigned long irq_flags;
10367 + int error;
10368 +@@ -1037,6 +1038,12 @@ static int edt_ft5x06_ts_probe(struct i2c_client *client,
10369 + return error;
10370 + }
10371 +
10372 ++ /*
10373 ++ * Dummy read access. EP0700MLP1 returns bogus data on the first
10374 ++ * register read access and ignores writes.
10375 ++ */
10376 ++ edt_ft5x06_ts_readwrite(tsdata->client, 2, buf, 2, buf);
10377 ++
10378 + edt_ft5x06_ts_set_regs(tsdata);
10379 + edt_ft5x06_ts_get_defaults(&client->dev, tsdata);
10380 + edt_ft5x06_ts_get_parameters(tsdata);
10381 +diff --git a/drivers/iommu/arm-smmu-v3.c b/drivers/iommu/arm-smmu-v3.c
10382 +index eff1f3aa5ef4..6b7664052b5b 100644
10383 +--- a/drivers/iommu/arm-smmu-v3.c
10384 ++++ b/drivers/iommu/arm-smmu-v3.c
10385 +@@ -1185,7 +1185,8 @@ static void arm_smmu_write_strtab_ent(struct arm_smmu_device *smmu, u32 sid,
10386 + }
10387 +
10388 + arm_smmu_sync_ste_for_sid(smmu, sid);
10389 +- dst[0] = cpu_to_le64(val);
10390 ++ /* See comment in arm_smmu_write_ctx_desc() */
10391 ++ WRITE_ONCE(dst[0], cpu_to_le64(val));
10392 + arm_smmu_sync_ste_for_sid(smmu, sid);
10393 +
10394 + /* It's likely that we'll want to use the new STE soon */
10395 +diff --git a/drivers/iommu/dmar.c b/drivers/iommu/dmar.c
10396 +index 7f9824b0609e..72994d67bc5b 100644
10397 +--- a/drivers/iommu/dmar.c
10398 ++++ b/drivers/iommu/dmar.c
10399 +@@ -1345,7 +1345,6 @@ void qi_flush_dev_iotlb(struct intel_iommu *iommu, u16 sid, u16 pfsid,
10400 + struct qi_desc desc;
10401 +
10402 + if (mask) {
10403 +- WARN_ON_ONCE(addr & ((1ULL << (VTD_PAGE_SHIFT + mask)) - 1));
10404 + addr |= (1ULL << (VTD_PAGE_SHIFT + mask - 1)) - 1;
10405 + desc.high = QI_DEV_IOTLB_ADDR(addr) | QI_DEV_IOTLB_SIZE;
10406 + } else
10407 +diff --git a/drivers/iommu/intel-svm.c b/drivers/iommu/intel-svm.c
10408 +index fd8730b2cd46..5944d3b4dca3 100644
10409 +--- a/drivers/iommu/intel-svm.c
10410 ++++ b/drivers/iommu/intel-svm.c
10411 +@@ -377,7 +377,7 @@ int intel_svm_bind_mm(struct device *dev, int *pasid, int flags, struct svm_dev_
10412 + /* Do not use PASID 0 in caching mode (virtualised IOMMU) */
10413 + ret = intel_pasid_alloc_id(svm,
10414 + !!cap_caching_mode(iommu->cap),
10415 +- pasid_max - 1, GFP_KERNEL);
10416 ++ pasid_max, GFP_KERNEL);
10417 + if (ret < 0) {
10418 + kfree(svm);
10419 + kfree(sdev);
10420 +diff --git a/drivers/irqchip/irq-gic-v3-its.c b/drivers/irqchip/irq-gic-v3-its.c
10421 +index 050d6e040128..bf7b69449b43 100644
10422 +--- a/drivers/irqchip/irq-gic-v3-its.c
10423 ++++ b/drivers/irqchip/irq-gic-v3-its.c
10424 +@@ -578,7 +578,7 @@ static struct its_collection *its_build_invall_cmd(struct its_node *its,
10425 + struct its_cmd_desc *desc)
10426 + {
10427 + its_encode_cmd(cmd, GITS_CMD_INVALL);
10428 +- its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id);
10429 ++ its_encode_collection(cmd, desc->its_invall_cmd.col->col_id);
10430 +
10431 + its_fixup_cmd(cmd);
10432 +
10433 +diff --git a/drivers/irqchip/irq-gic-v3.c b/drivers/irqchip/irq-gic-v3.c
10434 +index d5912f1ec884..ac888d7a0b00 100644
10435 +--- a/drivers/irqchip/irq-gic-v3.c
10436 ++++ b/drivers/irqchip/irq-gic-v3.c
10437 +@@ -1347,6 +1347,7 @@ static struct
10438 + struct redist_region *redist_regs;
10439 + u32 nr_redist_regions;
10440 + bool single_redist;
10441 ++ int enabled_rdists;
10442 + u32 maint_irq;
10443 + int maint_irq_mode;
10444 + phys_addr_t vcpu_base;
10445 +@@ -1441,8 +1442,10 @@ static int __init gic_acpi_match_gicc(struct acpi_subtable_header *header,
10446 + * If GICC is enabled and has valid gicr base address, then it means
10447 + * GICR base is presented via GICC
10448 + */
10449 +- if ((gicc->flags & ACPI_MADT_ENABLED) && gicc->gicr_base_address)
10450 ++ if ((gicc->flags & ACPI_MADT_ENABLED) && gicc->gicr_base_address) {
10451 ++ acpi_data.enabled_rdists++;
10452 + return 0;
10453 ++ }
10454 +
10455 + /*
10456 + * It's perfectly valid firmware can pass disabled GICC entry, driver
10457 +@@ -1472,8 +1475,10 @@ static int __init gic_acpi_count_gicr_regions(void)
10458 +
10459 + count = acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_INTERRUPT,
10460 + gic_acpi_match_gicc, 0);
10461 +- if (count > 0)
10462 ++ if (count > 0) {
10463 + acpi_data.single_redist = true;
10464 ++ count = acpi_data.enabled_rdists;
10465 ++ }
10466 +
10467 + return count;
10468 + }
10469 +diff --git a/drivers/irqchip/irq-mbigen.c b/drivers/irqchip/irq-mbigen.c
10470 +index 98b6e1d4b1a6..f7fdbf5d183b 100644
10471 +--- a/drivers/irqchip/irq-mbigen.c
10472 ++++ b/drivers/irqchip/irq-mbigen.c
10473 +@@ -381,6 +381,7 @@ static struct platform_driver mbigen_platform_driver = {
10474 + .name = "Hisilicon MBIGEN-V2",
10475 + .of_match_table = mbigen_of_match,
10476 + .acpi_match_table = ACPI_PTR(mbigen_acpi_match),
10477 ++ .suppress_bind_attrs = true,
10478 + },
10479 + .probe = mbigen_device_probe,
10480 + };
10481 +diff --git a/drivers/leds/leds-pca963x.c b/drivers/leds/leds-pca963x.c
10482 +index 5c0908113e38..bbcde13b77f1 100644
10483 +--- a/drivers/leds/leds-pca963x.c
10484 ++++ b/drivers/leds/leds-pca963x.c
10485 +@@ -43,6 +43,8 @@
10486 + #define PCA963X_LED_PWM 0x2 /* Controlled through PWM */
10487 + #define PCA963X_LED_GRP_PWM 0x3 /* Controlled through PWM/GRPPWM */
10488 +
10489 ++#define PCA963X_MODE2_OUTDRV 0x04 /* Open-drain or totem pole */
10490 ++#define PCA963X_MODE2_INVRT 0x10 /* Normal or inverted direction */
10491 + #define PCA963X_MODE2_DMBLNK 0x20 /* Enable blinking */
10492 +
10493 + #define PCA963X_MODE1 0x00
10494 +@@ -462,12 +464,12 @@ static int pca963x_probe(struct i2c_client *client,
10495 + PCA963X_MODE2);
10496 + /* Configure output: open-drain or totem pole (push-pull) */
10497 + if (pdata->outdrv == PCA963X_OPEN_DRAIN)
10498 +- mode2 |= 0x01;
10499 ++ mode2 &= ~PCA963X_MODE2_OUTDRV;
10500 + else
10501 +- mode2 |= 0x05;
10502 ++ mode2 |= PCA963X_MODE2_OUTDRV;
10503 + /* Configure direction: normal or inverted */
10504 + if (pdata->dir == PCA963X_INVERTED)
10505 +- mode2 |= 0x10;
10506 ++ mode2 |= PCA963X_MODE2_INVRT;
10507 + i2c_smbus_write_byte_data(pca963x->chip->client, PCA963X_MODE2,
10508 + mode2);
10509 + }
10510 +diff --git a/drivers/md/bcache/bset.h b/drivers/md/bcache/bset.h
10511 +index c71365e7c1fa..a50dcfda656f 100644
10512 +--- a/drivers/md/bcache/bset.h
10513 ++++ b/drivers/md/bcache/bset.h
10514 +@@ -397,7 +397,8 @@ void bch_btree_keys_stats(struct btree_keys *b, struct bset_stats *state);
10515 +
10516 + /* Bkey utility code */
10517 +
10518 +-#define bset_bkey_last(i) bkey_idx((struct bkey *) (i)->d, (i)->keys)
10519 ++#define bset_bkey_last(i) bkey_idx((struct bkey *) (i)->d, \
10520 ++ (unsigned int)(i)->keys)
10521 +
10522 + static inline struct bkey *bset_bkey_idx(struct bset *i, unsigned int idx)
10523 + {
10524 +diff --git a/drivers/md/bcache/super.c b/drivers/md/bcache/super.c
10525 +index c45d9ad01077..5b5cbfadd003 100644
10526 +--- a/drivers/md/bcache/super.c
10527 ++++ b/drivers/md/bcache/super.c
10528 +@@ -1226,6 +1226,9 @@ static void cached_dev_free(struct closure *cl)
10529 +
10530 + mutex_unlock(&bch_register_lock);
10531 +
10532 ++ if (dc->sb_bio.bi_inline_vecs[0].bv_page)
10533 ++ put_page(bio_first_page_all(&dc->sb_bio));
10534 ++
10535 + if (!IS_ERR_OR_NULL(dc->bdev))
10536 + blkdev_put(dc->bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
10537 +
10538 +diff --git a/drivers/media/i2c/mt9v032.c b/drivers/media/i2c/mt9v032.c
10539 +index f74730d24d8f..04788692c9ff 100644
10540 +--- a/drivers/media/i2c/mt9v032.c
10541 ++++ b/drivers/media/i2c/mt9v032.c
10542 +@@ -431,10 +431,12 @@ static int mt9v032_enum_mbus_code(struct v4l2_subdev *subdev,
10543 + struct v4l2_subdev_pad_config *cfg,
10544 + struct v4l2_subdev_mbus_code_enum *code)
10545 + {
10546 ++ struct mt9v032 *mt9v032 = to_mt9v032(subdev);
10547 ++
10548 + if (code->index > 0)
10549 + return -EINVAL;
10550 +
10551 +- code->code = MEDIA_BUS_FMT_SGRBG10_1X10;
10552 ++ code->code = mt9v032->format.code;
10553 + return 0;
10554 + }
10555 +
10556 +@@ -442,7 +444,11 @@ static int mt9v032_enum_frame_size(struct v4l2_subdev *subdev,
10557 + struct v4l2_subdev_pad_config *cfg,
10558 + struct v4l2_subdev_frame_size_enum *fse)
10559 + {
10560 +- if (fse->index >= 3 || fse->code != MEDIA_BUS_FMT_SGRBG10_1X10)
10561 ++ struct mt9v032 *mt9v032 = to_mt9v032(subdev);
10562 ++
10563 ++ if (fse->index >= 3)
10564 ++ return -EINVAL;
10565 ++ if (mt9v032->format.code != fse->code)
10566 + return -EINVAL;
10567 +
10568 + fse->min_width = MT9V032_WINDOW_WIDTH_DEF / (1 << fse->index);
10569 +diff --git a/drivers/media/pci/cx23885/cx23885-cards.c b/drivers/media/pci/cx23885/cx23885-cards.c
10570 +index ed3210dc50bc..642aefdbb7bb 100644
10571 +--- a/drivers/media/pci/cx23885/cx23885-cards.c
10572 ++++ b/drivers/media/pci/cx23885/cx23885-cards.c
10573 +@@ -811,6 +811,25 @@ struct cx23885_board cx23885_boards[] = {
10574 + .name = "Hauppauge WinTV-Starburst2",
10575 + .portb = CX23885_MPEG_DVB,
10576 + },
10577 ++ [CX23885_BOARD_AVERMEDIA_CE310B] = {
10578 ++ .name = "AVerMedia CE310B",
10579 ++ .porta = CX23885_ANALOG_VIDEO,
10580 ++ .force_bff = 1,
10581 ++ .input = {{
10582 ++ .type = CX23885_VMUX_COMPOSITE1,
10583 ++ .vmux = CX25840_VIN1_CH1 |
10584 ++ CX25840_NONE_CH2 |
10585 ++ CX25840_NONE0_CH3,
10586 ++ .amux = CX25840_AUDIO7,
10587 ++ }, {
10588 ++ .type = CX23885_VMUX_SVIDEO,
10589 ++ .vmux = CX25840_VIN8_CH1 |
10590 ++ CX25840_NONE_CH2 |
10591 ++ CX25840_VIN7_CH3 |
10592 ++ CX25840_SVIDEO_ON,
10593 ++ .amux = CX25840_AUDIO7,
10594 ++ } },
10595 ++ },
10596 + };
10597 + const unsigned int cx23885_bcount = ARRAY_SIZE(cx23885_boards);
10598 +
10599 +@@ -1134,6 +1153,10 @@ struct cx23885_subid cx23885_subids[] = {
10600 + .subvendor = 0x0070,
10601 + .subdevice = 0xf02a,
10602 + .card = CX23885_BOARD_HAUPPAUGE_STARBURST2,
10603 ++ }, {
10604 ++ .subvendor = 0x1461,
10605 ++ .subdevice = 0x3100,
10606 ++ .card = CX23885_BOARD_AVERMEDIA_CE310B,
10607 + },
10608 + };
10609 + const unsigned int cx23885_idcount = ARRAY_SIZE(cx23885_subids);
10610 +@@ -2358,6 +2381,7 @@ void cx23885_card_setup(struct cx23885_dev *dev)
10611 + case CX23885_BOARD_DVBSKY_T982:
10612 + case CX23885_BOARD_VIEWCAST_260E:
10613 + case CX23885_BOARD_VIEWCAST_460E:
10614 ++ case CX23885_BOARD_AVERMEDIA_CE310B:
10615 + dev->sd_cx25840 = v4l2_i2c_new_subdev(&dev->v4l2_dev,
10616 + &dev->i2c_bus[2].i2c_adap,
10617 + "cx25840", 0x88 >> 1, NULL);
10618 +diff --git a/drivers/media/pci/cx23885/cx23885-video.c b/drivers/media/pci/cx23885/cx23885-video.c
10619 +index f8a3deadc77a..2a20c7165e1e 100644
10620 +--- a/drivers/media/pci/cx23885/cx23885-video.c
10621 ++++ b/drivers/media/pci/cx23885/cx23885-video.c
10622 +@@ -268,7 +268,8 @@ static int cx23885_video_mux(struct cx23885_dev *dev, unsigned int input)
10623 + (dev->board == CX23885_BOARD_MYGICA_X8507) ||
10624 + (dev->board == CX23885_BOARD_AVERMEDIA_HC81R) ||
10625 + (dev->board == CX23885_BOARD_VIEWCAST_260E) ||
10626 +- (dev->board == CX23885_BOARD_VIEWCAST_460E)) {
10627 ++ (dev->board == CX23885_BOARD_VIEWCAST_460E) ||
10628 ++ (dev->board == CX23885_BOARD_AVERMEDIA_CE310B)) {
10629 + /* Configure audio routing */
10630 + v4l2_subdev_call(dev->sd_cx25840, audio, s_routing,
10631 + INPUT(input)->amux, 0, 0);
10632 +diff --git a/drivers/media/pci/cx23885/cx23885.h b/drivers/media/pci/cx23885/cx23885.h
10633 +index cf965efabe66..7bbd62cc993e 100644
10634 +--- a/drivers/media/pci/cx23885/cx23885.h
10635 ++++ b/drivers/media/pci/cx23885/cx23885.h
10636 +@@ -111,6 +111,7 @@
10637 + #define CX23885_BOARD_HAUPPAUGE_STARBURST2 59
10638 + #define CX23885_BOARD_HAUPPAUGE_QUADHD_DVB_885 60
10639 + #define CX23885_BOARD_HAUPPAUGE_QUADHD_ATSC_885 61
10640 ++#define CX23885_BOARD_AVERMEDIA_CE310B 62
10641 +
10642 + #define GPIO_0 0x00000001
10643 + #define GPIO_1 0x00000002
10644 +diff --git a/drivers/media/platform/sti/bdisp/bdisp-hw.c b/drivers/media/platform/sti/bdisp/bdisp-hw.c
10645 +index 26d9fa7aeb5f..d57f659d740a 100644
10646 +--- a/drivers/media/platform/sti/bdisp/bdisp-hw.c
10647 ++++ b/drivers/media/platform/sti/bdisp/bdisp-hw.c
10648 +@@ -14,8 +14,8 @@
10649 + #define MAX_SRC_WIDTH 2048
10650 +
10651 + /* Reset & boot poll config */
10652 +-#define POLL_RST_MAX 50
10653 +-#define POLL_RST_DELAY_MS 20
10654 ++#define POLL_RST_MAX 500
10655 ++#define POLL_RST_DELAY_MS 2
10656 +
10657 + enum bdisp_target_plan {
10658 + BDISP_RGB,
10659 +@@ -382,7 +382,7 @@ int bdisp_hw_reset(struct bdisp_dev *bdisp)
10660 + for (i = 0; i < POLL_RST_MAX; i++) {
10661 + if (readl(bdisp->regs + BLT_STA1) & BLT_STA1_IDLE)
10662 + break;
10663 +- msleep(POLL_RST_DELAY_MS);
10664 ++ udelay(POLL_RST_DELAY_MS * 1000);
10665 + }
10666 + if (i == POLL_RST_MAX)
10667 + dev_err(bdisp->dev, "Reset timeout\n");
10668 +diff --git a/drivers/net/ethernet/cisco/enic/enic_main.c b/drivers/net/ethernet/cisco/enic/enic_main.c
10669 +index 733d9172425b..026a3bd71204 100644
10670 +--- a/drivers/net/ethernet/cisco/enic/enic_main.c
10671 ++++ b/drivers/net/ethernet/cisco/enic/enic_main.c
10672 +@@ -2013,10 +2013,10 @@ static int enic_stop(struct net_device *netdev)
10673 + napi_disable(&enic->napi[i]);
10674 +
10675 + netif_carrier_off(netdev);
10676 +- netif_tx_disable(netdev);
10677 + if (vnic_dev_get_intr_mode(enic->vdev) == VNIC_DEV_INTR_MODE_MSIX)
10678 + for (i = 0; i < enic->wq_count; i++)
10679 + napi_disable(&enic->napi[enic_cq_wq(enic, i)]);
10680 ++ netif_tx_disable(netdev);
10681 +
10682 + if (!enic_is_dynamic(enic) && !enic_is_sriov_vf(enic))
10683 + enic_dev_del_station_addr(enic);
10684 +diff --git a/drivers/net/ethernet/freescale/gianfar.c b/drivers/net/ethernet/freescale/gianfar.c
10685 +index c97c4edfa31b..cf2d1e846a69 100644
10686 +--- a/drivers/net/ethernet/freescale/gianfar.c
10687 ++++ b/drivers/net/ethernet/freescale/gianfar.c
10688 +@@ -2685,13 +2685,17 @@ static void gfar_clean_tx_ring(struct gfar_priv_tx_q *tx_queue)
10689 + skb_dirtytx = tx_queue->skb_dirtytx;
10690 +
10691 + while ((skb = tx_queue->tx_skbuff[skb_dirtytx])) {
10692 ++ bool do_tstamp;
10693 ++
10694 ++ do_tstamp = (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
10695 ++ priv->hwts_tx_en;
10696 +
10697 + frags = skb_shinfo(skb)->nr_frags;
10698 +
10699 + /* When time stamping, one additional TxBD must be freed.
10700 + * Also, we need to dma_unmap_single() the TxPAL.
10701 + */
10702 +- if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS))
10703 ++ if (unlikely(do_tstamp))
10704 + nr_txbds = frags + 2;
10705 + else
10706 + nr_txbds = frags + 1;
10707 +@@ -2705,7 +2709,7 @@ static void gfar_clean_tx_ring(struct gfar_priv_tx_q *tx_queue)
10708 + (lstatus & BD_LENGTH_MASK))
10709 + break;
10710 +
10711 +- if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) {
10712 ++ if (unlikely(do_tstamp)) {
10713 + next = next_txbd(bdp, base, tx_ring_size);
10714 + buflen = be16_to_cpu(next->length) +
10715 + GMAC_FCB_LEN + GMAC_TXPAL_LEN;
10716 +@@ -2715,7 +2719,7 @@ static void gfar_clean_tx_ring(struct gfar_priv_tx_q *tx_queue)
10717 + dma_unmap_single(priv->dev, be32_to_cpu(bdp->bufPtr),
10718 + buflen, DMA_TO_DEVICE);
10719 +
10720 +- if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) {
10721 ++ if (unlikely(do_tstamp)) {
10722 + struct skb_shared_hwtstamps shhwtstamps;
10723 + u64 *ns = (u64 *)(((uintptr_t)skb->data + 0x10) &
10724 + ~0x7UL);
10725 +diff --git a/drivers/net/ethernet/mellanox/mlx5/core/en_stats.c b/drivers/net/ethernet/mellanox/mlx5/core/en_stats.c
10726 +index 8255d797ea94..9a68dee588c1 100644
10727 +--- a/drivers/net/ethernet/mellanox/mlx5/core/en_stats.c
10728 ++++ b/drivers/net/ethernet/mellanox/mlx5/core/en_stats.c
10729 +@@ -211,6 +211,9 @@ void mlx5e_grp_sw_update_stats(struct mlx5e_priv *priv)
10730 + s->tx_tls_resync_bytes += sq_stats->tls_resync_bytes;
10731 + #endif
10732 + s->tx_cqes += sq_stats->cqes;
10733 ++
10734 ++ /* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92657 */
10735 ++ barrier();
10736 + }
10737 + }
10738 +
10739 +diff --git a/drivers/net/ethernet/mellanox/mlxsw/spectrum_dpipe.c b/drivers/net/ethernet/mellanox/mlxsw/spectrum_dpipe.c
10740 +index 41e607a14846..4fe193c4fa55 100644
10741 +--- a/drivers/net/ethernet/mellanox/mlxsw/spectrum_dpipe.c
10742 ++++ b/drivers/net/ethernet/mellanox/mlxsw/spectrum_dpipe.c
10743 +@@ -215,7 +215,7 @@ mlxsw_sp_dpipe_table_erif_entries_dump(void *priv, bool counters_enabled,
10744 + start_again:
10745 + err = devlink_dpipe_entry_ctx_prepare(dump_ctx);
10746 + if (err)
10747 +- return err;
10748 ++ goto err_ctx_prepare;
10749 + j = 0;
10750 + for (; i < rif_count; i++) {
10751 + struct mlxsw_sp_rif *rif = mlxsw_sp_rif_by_index(mlxsw_sp, i);
10752 +@@ -247,6 +247,7 @@ start_again:
10753 + return 0;
10754 + err_entry_append:
10755 + err_entry_get:
10756 ++err_ctx_prepare:
10757 + rtnl_unlock();
10758 + devlink_dpipe_entry_clear(&entry);
10759 + return err;
10760 +diff --git a/drivers/net/ethernet/realtek/r8169.c b/drivers/net/ethernet/realtek/r8169.c
10761 +index 4ab87fe84542..6ea43e48d5f9 100644
10762 +--- a/drivers/net/ethernet/realtek/r8169.c
10763 ++++ b/drivers/net/ethernet/realtek/r8169.c
10764 +@@ -7433,6 +7433,15 @@ static int rtl_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
10765 + int chipset, region, i;
10766 + int jumbo_max, rc;
10767 +
10768 ++ /* Some tools for creating an initramfs don't consider softdeps, then
10769 ++ * r8169.ko may be in initramfs, but realtek.ko not. Then the generic
10770 ++ * PHY driver is used that doesn't work with most chip versions.
10771 ++ */
10772 ++ if (!driver_find("RTL8201CP Ethernet", &mdio_bus_type)) {
10773 ++ dev_err(&pdev->dev, "realtek.ko not loaded, maybe it needs to be added to initramfs?\n");
10774 ++ return -ENOENT;
10775 ++ }
10776 ++
10777 + dev = devm_alloc_etherdev(&pdev->dev, sizeof (*tp));
10778 + if (!dev)
10779 + return -ENOMEM;
10780 +diff --git a/drivers/net/wan/fsl_ucc_hdlc.c b/drivers/net/wan/fsl_ucc_hdlc.c
10781 +index daeab33f623e..9ab04ef532f3 100644
10782 +--- a/drivers/net/wan/fsl_ucc_hdlc.c
10783 ++++ b/drivers/net/wan/fsl_ucc_hdlc.c
10784 +@@ -242,6 +242,11 @@ static int uhdlc_init(struct ucc_hdlc_private *priv)
10785 + ret = -ENOMEM;
10786 + goto free_riptr;
10787 + }
10788 ++ if (riptr != (u16)riptr || tiptr != (u16)tiptr) {
10789 ++ dev_err(priv->dev, "MURAM allocation out of addressable range\n");
10790 ++ ret = -ENOMEM;
10791 ++ goto free_tiptr;
10792 ++ }
10793 +
10794 + /* Set RIPTR, TIPTR */
10795 + iowrite16be(riptr, &priv->ucc_pram->riptr);
10796 +diff --git a/drivers/net/wan/ixp4xx_hss.c b/drivers/net/wan/ixp4xx_hss.c
10797 +index 6a505c26a3e7..a269ed63d90f 100644
10798 +--- a/drivers/net/wan/ixp4xx_hss.c
10799 ++++ b/drivers/net/wan/ixp4xx_hss.c
10800 +@@ -261,7 +261,7 @@ struct port {
10801 + struct hss_plat_info *plat;
10802 + buffer_t *rx_buff_tab[RX_DESCS], *tx_buff_tab[TX_DESCS];
10803 + struct desc *desc_tab; /* coherent */
10804 +- u32 desc_tab_phys;
10805 ++ dma_addr_t desc_tab_phys;
10806 + unsigned int id;
10807 + unsigned int clock_type, clock_rate, loopback;
10808 + unsigned int initialized, carrier;
10809 +@@ -861,7 +861,7 @@ static int hss_hdlc_xmit(struct sk_buff *skb, struct net_device *dev)
10810 + dev->stats.tx_dropped++;
10811 + return NETDEV_TX_OK;
10812 + }
10813 +- memcpy_swab32(mem, (u32 *)((int)skb->data & ~3), bytes / 4);
10814 ++ memcpy_swab32(mem, (u32 *)((uintptr_t)skb->data & ~3), bytes / 4);
10815 + dev_kfree_skb(skb);
10816 + #endif
10817 +
10818 +diff --git a/drivers/net/wireless/ath/ath10k/wmi.c b/drivers/net/wireless/ath/ath10k/wmi.c
10819 +index 0f6ff7a78e49..3372dfa0decc 100644
10820 +--- a/drivers/net/wireless/ath/ath10k/wmi.c
10821 ++++ b/drivers/net/wireless/ath/ath10k/wmi.c
10822 +@@ -9193,7 +9193,7 @@ static int ath10k_wmi_mgmt_tx_clean_up_pending(int msdu_id, void *ptr,
10823 +
10824 + msdu = pkt_addr->vaddr;
10825 + dma_unmap_single(ar->dev, pkt_addr->paddr,
10826 +- msdu->len, DMA_FROM_DEVICE);
10827 ++ msdu->len, DMA_TO_DEVICE);
10828 + ieee80211_free_txskb(ar->hw, msdu);
10829 +
10830 + return 0;
10831 +diff --git a/drivers/net/wireless/broadcom/b43legacy/main.c b/drivers/net/wireless/broadcom/b43legacy/main.c
10832 +index 55f411925960..770cc218ca4b 100644
10833 +--- a/drivers/net/wireless/broadcom/b43legacy/main.c
10834 ++++ b/drivers/net/wireless/broadcom/b43legacy/main.c
10835 +@@ -1304,8 +1304,9 @@ static void handle_irq_ucode_debug(struct b43legacy_wldev *dev)
10836 + }
10837 +
10838 + /* Interrupt handler bottom-half */
10839 +-static void b43legacy_interrupt_tasklet(struct b43legacy_wldev *dev)
10840 ++static void b43legacy_interrupt_tasklet(unsigned long data)
10841 + {
10842 ++ struct b43legacy_wldev *dev = (struct b43legacy_wldev *)data;
10843 + u32 reason;
10844 + u32 dma_reason[ARRAY_SIZE(dev->dma_reason)];
10845 + u32 merged_dma_reason = 0;
10846 +@@ -3775,7 +3776,7 @@ static int b43legacy_one_core_attach(struct ssb_device *dev,
10847 + b43legacy_set_status(wldev, B43legacy_STAT_UNINIT);
10848 + wldev->bad_frames_preempt = modparam_bad_frames_preempt;
10849 + tasklet_init(&wldev->isr_tasklet,
10850 +- (void (*)(unsigned long))b43legacy_interrupt_tasklet,
10851 ++ b43legacy_interrupt_tasklet,
10852 + (unsigned long)wldev);
10853 + if (modparam_pio)
10854 + wldev->__using_pio = true;
10855 +diff --git a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/sdio.c b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/sdio.c
10856 +index 5c3b62e61980..e0211321fe9e 100644
10857 +--- a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/sdio.c
10858 ++++ b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/sdio.c
10859 +@@ -1934,6 +1934,7 @@ static uint brcmf_sdio_readframes(struct brcmf_sdio *bus, uint maxframes)
10860 + BRCMF_SDIO_FT_NORMAL)) {
10861 + rd->len = 0;
10862 + brcmu_pkt_buf_free_skb(pkt);
10863 ++ continue;
10864 + }
10865 + bus->sdcnt.rx_readahead_cnt++;
10866 + if (rd->len != roundup(rd_new.len, 16)) {
10867 +diff --git a/drivers/net/wireless/intel/ipw2x00/ipw2100.c b/drivers/net/wireless/intel/ipw2x00/ipw2100.c
10868 +index 910db46db6a1..a3a470976a5c 100644
10869 +--- a/drivers/net/wireless/intel/ipw2x00/ipw2100.c
10870 ++++ b/drivers/net/wireless/intel/ipw2x00/ipw2100.c
10871 +@@ -3220,8 +3220,9 @@ static void ipw2100_tx_send_data(struct ipw2100_priv *priv)
10872 + }
10873 + }
10874 +
10875 +-static void ipw2100_irq_tasklet(struct ipw2100_priv *priv)
10876 ++static void ipw2100_irq_tasklet(unsigned long data)
10877 + {
10878 ++ struct ipw2100_priv *priv = (struct ipw2100_priv *)data;
10879 + struct net_device *dev = priv->net_dev;
10880 + unsigned long flags;
10881 + u32 inta, tmp;
10882 +@@ -6025,7 +6026,7 @@ static void ipw2100_rf_kill(struct work_struct *work)
10883 + spin_unlock_irqrestore(&priv->low_lock, flags);
10884 + }
10885 +
10886 +-static void ipw2100_irq_tasklet(struct ipw2100_priv *priv);
10887 ++static void ipw2100_irq_tasklet(unsigned long data);
10888 +
10889 + static const struct net_device_ops ipw2100_netdev_ops = {
10890 + .ndo_open = ipw2100_open,
10891 +@@ -6155,7 +6156,7 @@ static struct net_device *ipw2100_alloc_device(struct pci_dev *pci_dev,
10892 + INIT_DELAYED_WORK(&priv->rf_kill, ipw2100_rf_kill);
10893 + INIT_DELAYED_WORK(&priv->scan_event, ipw2100_scan_event);
10894 +
10895 +- tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
10896 ++ tasklet_init(&priv->irq_tasklet,
10897 + ipw2100_irq_tasklet, (unsigned long)priv);
10898 +
10899 + /* NOTE: We do not start the deferred work for status checks yet */
10900 +diff --git a/drivers/net/wireless/intel/ipw2x00/ipw2200.c b/drivers/net/wireless/intel/ipw2x00/ipw2200.c
10901 +index 9644e7b93645..04aee2fdba37 100644
10902 +--- a/drivers/net/wireless/intel/ipw2x00/ipw2200.c
10903 ++++ b/drivers/net/wireless/intel/ipw2x00/ipw2200.c
10904 +@@ -1959,8 +1959,9 @@ static void notify_wx_assoc_event(struct ipw_priv *priv)
10905 + wireless_send_event(priv->net_dev, SIOCGIWAP, &wrqu, NULL);
10906 + }
10907 +
10908 +-static void ipw_irq_tasklet(struct ipw_priv *priv)
10909 ++static void ipw_irq_tasklet(unsigned long data)
10910 + {
10911 ++ struct ipw_priv *priv = (struct ipw_priv *)data;
10912 + u32 inta, inta_mask, handled = 0;
10913 + unsigned long flags;
10914 + int rc = 0;
10915 +@@ -10694,7 +10695,7 @@ static int ipw_setup_deferred_work(struct ipw_priv *priv)
10916 + INIT_WORK(&priv->qos_activate, ipw_bg_qos_activate);
10917 + #endif /* CONFIG_IPW2200_QOS */
10918 +
10919 +- tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
10920 ++ tasklet_init(&priv->irq_tasklet,
10921 + ipw_irq_tasklet, (unsigned long)priv);
10922 +
10923 + return ret;
10924 +diff --git a/drivers/net/wireless/intel/iwlegacy/3945-mac.c b/drivers/net/wireless/intel/iwlegacy/3945-mac.c
10925 +index 57e3b6cca234..b536ec20eacc 100644
10926 +--- a/drivers/net/wireless/intel/iwlegacy/3945-mac.c
10927 ++++ b/drivers/net/wireless/intel/iwlegacy/3945-mac.c
10928 +@@ -1392,8 +1392,9 @@ il3945_dump_nic_error_log(struct il_priv *il)
10929 + }
10930 +
10931 + static void
10932 +-il3945_irq_tasklet(struct il_priv *il)
10933 ++il3945_irq_tasklet(unsigned long data)
10934 + {
10935 ++ struct il_priv *il = (struct il_priv *)data;
10936 + u32 inta, handled = 0;
10937 + u32 inta_fh;
10938 + unsigned long flags;
10939 +@@ -3419,7 +3420,7 @@ il3945_setup_deferred_work(struct il_priv *il)
10940 + timer_setup(&il->watchdog, il_bg_watchdog, 0);
10941 +
10942 + tasklet_init(&il->irq_tasklet,
10943 +- (void (*)(unsigned long))il3945_irq_tasklet,
10944 ++ il3945_irq_tasklet,
10945 + (unsigned long)il);
10946 + }
10947 +
10948 +diff --git a/drivers/net/wireless/intel/iwlegacy/4965-mac.c b/drivers/net/wireless/intel/iwlegacy/4965-mac.c
10949 +index 280cd8ae1696..6fc51c74cdb8 100644
10950 +--- a/drivers/net/wireless/intel/iwlegacy/4965-mac.c
10951 ++++ b/drivers/net/wireless/intel/iwlegacy/4965-mac.c
10952 +@@ -4360,8 +4360,9 @@ il4965_synchronize_irq(struct il_priv *il)
10953 + }
10954 +
10955 + static void
10956 +-il4965_irq_tasklet(struct il_priv *il)
10957 ++il4965_irq_tasklet(unsigned long data)
10958 + {
10959 ++ struct il_priv *il = (struct il_priv *)data;
10960 + u32 inta, handled = 0;
10961 + u32 inta_fh;
10962 + unsigned long flags;
10963 +@@ -6257,7 +6258,7 @@ il4965_setup_deferred_work(struct il_priv *il)
10964 + timer_setup(&il->watchdog, il_bg_watchdog, 0);
10965 +
10966 + tasklet_init(&il->irq_tasklet,
10967 +- (void (*)(unsigned long))il4965_irq_tasklet,
10968 ++ il4965_irq_tasklet,
10969 + (unsigned long)il);
10970 + }
10971 +
10972 +diff --git a/drivers/net/wireless/intel/iwlegacy/common.c b/drivers/net/wireless/intel/iwlegacy/common.c
10973 +index 6514baf799fe..e16f2597c219 100644
10974 +--- a/drivers/net/wireless/intel/iwlegacy/common.c
10975 ++++ b/drivers/net/wireless/intel/iwlegacy/common.c
10976 +@@ -717,7 +717,7 @@ il_eeprom_init(struct il_priv *il)
10977 + u32 gp = _il_rd(il, CSR_EEPROM_GP);
10978 + int sz;
10979 + int ret;
10980 +- u16 addr;
10981 ++ int addr;
10982 +
10983 + /* allocate eeprom */
10984 + sz = il->cfg->eeprom_size;
10985 +diff --git a/drivers/net/wireless/intel/iwlwifi/mvm/tt.c b/drivers/net/wireless/intel/iwlwifi/mvm/tt.c
10986 +index 1232f63278eb..319103f4b432 100644
10987 +--- a/drivers/net/wireless/intel/iwlwifi/mvm/tt.c
10988 ++++ b/drivers/net/wireless/intel/iwlwifi/mvm/tt.c
10989 +@@ -739,7 +739,8 @@ static struct thermal_zone_device_ops tzone_ops = {
10990 + static void iwl_mvm_thermal_zone_register(struct iwl_mvm *mvm)
10991 + {
10992 + int i;
10993 +- char name[] = "iwlwifi";
10994 ++ char name[16];
10995 ++ static atomic_t counter = ATOMIC_INIT(0);
10996 +
10997 + if (!iwl_mvm_is_tt_in_fw(mvm)) {
10998 + mvm->tz_device.tzone = NULL;
10999 +@@ -749,6 +750,7 @@ static void iwl_mvm_thermal_zone_register(struct iwl_mvm *mvm)
11000 +
11001 + BUILD_BUG_ON(ARRAY_SIZE(name) >= THERMAL_NAME_LENGTH);
11002 +
11003 ++ sprintf(name, "iwlwifi_%u", atomic_inc_return(&counter) & 0xFF);
11004 + mvm->tz_device.tzone = thermal_zone_device_register(name,
11005 + IWL_MAX_DTS_TRIPS,
11006 + IWL_WRITABLE_TRIPS_MSK,
11007 +diff --git a/drivers/net/wireless/intersil/hostap/hostap_ap.c b/drivers/net/wireless/intersil/hostap/hostap_ap.c
11008 +index 0094b1d2b577..3ec46f48cfde 100644
11009 +--- a/drivers/net/wireless/intersil/hostap/hostap_ap.c
11010 ++++ b/drivers/net/wireless/intersil/hostap/hostap_ap.c
11011 +@@ -2508,7 +2508,7 @@ static int prism2_hostapd_add_sta(struct ap_data *ap,
11012 + sta->supported_rates[0] = 2;
11013 + if (sta->tx_supp_rates & WLAN_RATE_2M)
11014 + sta->supported_rates[1] = 4;
11015 +- if (sta->tx_supp_rates & WLAN_RATE_5M5)
11016 ++ if (sta->tx_supp_rates & WLAN_RATE_5M5)
11017 + sta->supported_rates[2] = 11;
11018 + if (sta->tx_supp_rates & WLAN_RATE_11M)
11019 + sta->supported_rates[3] = 22;
11020 +diff --git a/drivers/net/wireless/intersil/orinoco/orinoco_usb.c b/drivers/net/wireless/intersil/orinoco/orinoco_usb.c
11021 +index 2c7dd2a7350c..b704e4bce171 100644
11022 +--- a/drivers/net/wireless/intersil/orinoco/orinoco_usb.c
11023 ++++ b/drivers/net/wireless/intersil/orinoco/orinoco_usb.c
11024 +@@ -1364,7 +1364,8 @@ static int ezusb_init(struct hermes *hw)
11025 + int retval;
11026 +
11027 + BUG_ON(in_interrupt());
11028 +- BUG_ON(!upriv);
11029 ++ if (!upriv)
11030 ++ return -EINVAL;
11031 +
11032 + upriv->reply_count = 0;
11033 + /* Write the MAGIC number on the simulated registers to keep
11034 +diff --git a/drivers/net/wireless/realtek/rtlwifi/pci.c b/drivers/net/wireless/realtek/rtlwifi/pci.c
11035 +index 5d1fda16fc8c..83749578fa8b 100644
11036 +--- a/drivers/net/wireless/realtek/rtlwifi/pci.c
11037 ++++ b/drivers/net/wireless/realtek/rtlwifi/pci.c
11038 +@@ -1082,13 +1082,15 @@ done:
11039 + return ret;
11040 + }
11041 +
11042 +-static void _rtl_pci_irq_tasklet(struct ieee80211_hw *hw)
11043 ++static void _rtl_pci_irq_tasklet(unsigned long data)
11044 + {
11045 ++ struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
11046 + _rtl_pci_tx_chk_waitq(hw);
11047 + }
11048 +
11049 +-static void _rtl_pci_prepare_bcn_tasklet(struct ieee80211_hw *hw)
11050 ++static void _rtl_pci_prepare_bcn_tasklet(unsigned long data)
11051 + {
11052 ++ struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
11053 + struct rtl_priv *rtlpriv = rtl_priv(hw);
11054 + struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
11055 + struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
11056 +@@ -1214,10 +1216,10 @@ static void _rtl_pci_init_struct(struct ieee80211_hw *hw,
11057 +
11058 + /*task */
11059 + tasklet_init(&rtlpriv->works.irq_tasklet,
11060 +- (void (*)(unsigned long))_rtl_pci_irq_tasklet,
11061 ++ _rtl_pci_irq_tasklet,
11062 + (unsigned long)hw);
11063 + tasklet_init(&rtlpriv->works.irq_prepare_bcn_tasklet,
11064 +- (void (*)(unsigned long))_rtl_pci_prepare_bcn_tasklet,
11065 ++ _rtl_pci_prepare_bcn_tasklet,
11066 + (unsigned long)hw);
11067 + INIT_WORK(&rtlpriv->works.lps_change_work,
11068 + rtl_lps_change_work_callback);
11069 +diff --git a/drivers/nfc/port100.c b/drivers/nfc/port100.c
11070 +index 60ae382f50da..06bb226c62ef 100644
11071 +--- a/drivers/nfc/port100.c
11072 ++++ b/drivers/nfc/port100.c
11073 +@@ -574,7 +574,7 @@ static void port100_tx_update_payload_len(void *_frame, int len)
11074 + {
11075 + struct port100_frame *frame = _frame;
11076 +
11077 +- frame->datalen = cpu_to_le16(le16_to_cpu(frame->datalen) + len);
11078 ++ le16_add_cpu(&frame->datalen, len);
11079 + }
11080 +
11081 + static bool port100_rx_frame_is_valid(void *_frame)
11082 +diff --git a/drivers/pci/controller/pcie-iproc.c b/drivers/pci/controller/pcie-iproc.c
11083 +index 9d5cbc75d5ae..ec86414216f9 100644
11084 +--- a/drivers/pci/controller/pcie-iproc.c
11085 ++++ b/drivers/pci/controller/pcie-iproc.c
11086 +@@ -1526,6 +1526,30 @@ DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_BROADCOM, 0xd802,
11087 + DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_BROADCOM, 0xd804,
11088 + quirk_paxc_disable_msi_parsing);
11089 +
11090 ++static void quirk_paxc_bridge(struct pci_dev *pdev)
11091 ++{
11092 ++ /*
11093 ++ * The PCI config space is shared with the PAXC root port and the first
11094 ++ * Ethernet device. So, we need to workaround this by telling the PCI
11095 ++ * code that the bridge is not an Ethernet device.
11096 ++ */
11097 ++ if (pdev->hdr_type == PCI_HEADER_TYPE_BRIDGE)
11098 ++ pdev->class = PCI_CLASS_BRIDGE_PCI << 8;
11099 ++
11100 ++ /*
11101 ++ * MPSS is not being set properly (as it is currently 0). This is
11102 ++ * because that area of the PCI config space is hard coded to zero, and
11103 ++ * is not modifiable by firmware. Set this to 2 (e.g., 512 byte MPS)
11104 ++ * so that the MPS can be set to the real max value.
11105 ++ */
11106 ++ pdev->pcie_mpss = 2;
11107 ++}
11108 ++DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_BROADCOM, 0x16cd, quirk_paxc_bridge);
11109 ++DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_BROADCOM, 0x16f0, quirk_paxc_bridge);
11110 ++DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_BROADCOM, 0xd750, quirk_paxc_bridge);
11111 ++DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_BROADCOM, 0xd802, quirk_paxc_bridge);
11112 ++DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_BROADCOM, 0xd804, quirk_paxc_bridge);
11113 ++
11114 + MODULE_AUTHOR("Ray Jui <rjui@××××××××.com>");
11115 + MODULE_DESCRIPTION("Broadcom iPROC PCIe common driver");
11116 + MODULE_LICENSE("GPL v2");
11117 +diff --git a/drivers/pci/quirks.c b/drivers/pci/quirks.c
11118 +index 5b4c36ab1596..419dda6dbd16 100644
11119 +--- a/drivers/pci/quirks.c
11120 ++++ b/drivers/pci/quirks.c
11121 +@@ -1848,19 +1848,40 @@ DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x2609, quirk_intel_pcie_pm);
11122 + DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x260a, quirk_intel_pcie_pm);
11123 + DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_INTEL, 0x260b, quirk_intel_pcie_pm);
11124 +
11125 ++static void quirk_d3hot_delay(struct pci_dev *dev, unsigned int delay)
11126 ++{
11127 ++ if (dev->d3_delay >= delay)
11128 ++ return;
11129 ++
11130 ++ dev->d3_delay = delay;
11131 ++ pci_info(dev, "extending delay after power-on from D3hot to %d msec\n",
11132 ++ dev->d3_delay);
11133 ++}
11134 ++
11135 + static void quirk_radeon_pm(struct pci_dev *dev)
11136 + {
11137 + if (dev->subsystem_vendor == PCI_VENDOR_ID_APPLE &&
11138 +- dev->subsystem_device == 0x00e2) {
11139 +- if (dev->d3_delay < 20) {
11140 +- dev->d3_delay = 20;
11141 +- pci_info(dev, "extending delay after power-on from D3 to %d msec\n",
11142 +- dev->d3_delay);
11143 +- }
11144 +- }
11145 ++ dev->subsystem_device == 0x00e2)
11146 ++ quirk_d3hot_delay(dev, 20);
11147 + }
11148 + DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_ATI, 0x6741, quirk_radeon_pm);
11149 +
11150 ++/*
11151 ++ * Ryzen5/7 XHCI controllers fail upon resume from runtime suspend or s2idle.
11152 ++ * https://bugzilla.kernel.org/show_bug.cgi?id=205587
11153 ++ *
11154 ++ * The kernel attempts to transition these devices to D3cold, but that seems
11155 ++ * to be ineffective on the platforms in question; the PCI device appears to
11156 ++ * remain on in D3hot state. The D3hot-to-D0 transition then requires an
11157 ++ * extended delay in order to succeed.
11158 ++ */
11159 ++static void quirk_ryzen_xhci_d3hot(struct pci_dev *dev)
11160 ++{
11161 ++ quirk_d3hot_delay(dev, 20);
11162 ++}
11163 ++DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_AMD, 0x15e0, quirk_ryzen_xhci_d3hot);
11164 ++DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_AMD, 0x15e1, quirk_ryzen_xhci_d3hot);
11165 ++
11166 + #ifdef CONFIG_X86_IO_APIC
11167 + static int dmi_disable_ioapicreroute(const struct dmi_system_id *d)
11168 + {
11169 +@@ -2358,32 +2379,6 @@ DECLARE_PCI_FIXUP_ENABLE(PCI_VENDOR_ID_BROADCOM,
11170 + PCI_DEVICE_ID_TIGON3_5719,
11171 + quirk_brcm_5719_limit_mrrs);
11172 +
11173 +-#ifdef CONFIG_PCIE_IPROC_PLATFORM
11174 +-static void quirk_paxc_bridge(struct pci_dev *pdev)
11175 +-{
11176 +- /*
11177 +- * The PCI config space is shared with the PAXC root port and the first
11178 +- * Ethernet device. So, we need to workaround this by telling the PCI
11179 +- * code that the bridge is not an Ethernet device.
11180 +- */
11181 +- if (pdev->hdr_type == PCI_HEADER_TYPE_BRIDGE)
11182 +- pdev->class = PCI_CLASS_BRIDGE_PCI << 8;
11183 +-
11184 +- /*
11185 +- * MPSS is not being set properly (as it is currently 0). This is
11186 +- * because that area of the PCI config space is hard coded to zero, and
11187 +- * is not modifiable by firmware. Set this to 2 (e.g., 512 byte MPS)
11188 +- * so that the MPS can be set to the real max value.
11189 +- */
11190 +- pdev->pcie_mpss = 2;
11191 +-}
11192 +-DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_BROADCOM, 0x16cd, quirk_paxc_bridge);
11193 +-DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_BROADCOM, 0x16f0, quirk_paxc_bridge);
11194 +-DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_BROADCOM, 0xd750, quirk_paxc_bridge);
11195 +-DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_BROADCOM, 0xd802, quirk_paxc_bridge);
11196 +-DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_BROADCOM, 0xd804, quirk_paxc_bridge);
11197 +-#endif
11198 +-
11199 + /*
11200 + * Originally in EDAC sources for i82875P: Intel tells BIOS developers to
11201 + * hide device 6 which configures the overflow device access containing the
11202 +diff --git a/drivers/pinctrl/intel/pinctrl-baytrail.c b/drivers/pinctrl/intel/pinctrl-baytrail.c
11203 +index 021e28ff1194..a760d8bda0af 100644
11204 +--- a/drivers/pinctrl/intel/pinctrl-baytrail.c
11205 ++++ b/drivers/pinctrl/intel/pinctrl-baytrail.c
11206 +@@ -950,7 +950,13 @@ static void byt_gpio_clear_triggering(struct byt_gpio *vg, unsigned int offset)
11207 +
11208 + raw_spin_lock_irqsave(&byt_lock, flags);
11209 + value = readl(reg);
11210 +- value &= ~(BYT_TRIG_POS | BYT_TRIG_NEG | BYT_TRIG_LVL);
11211 ++
11212 ++ /* Do not clear direct-irq enabled IRQs (from gpio_disable_free) */
11213 ++ if (value & BYT_DIRECT_IRQ_EN)
11214 ++ /* nothing to do */ ;
11215 ++ else
11216 ++ value &= ~(BYT_TRIG_POS | BYT_TRIG_NEG | BYT_TRIG_LVL);
11217 ++
11218 + writel(value, reg);
11219 + raw_spin_unlock_irqrestore(&byt_lock, flags);
11220 + }
11221 +diff --git a/drivers/pinctrl/sh-pfc/pfc-sh7264.c b/drivers/pinctrl/sh-pfc/pfc-sh7264.c
11222 +index e1c34e19222e..3ddb9565ed80 100644
11223 +--- a/drivers/pinctrl/sh-pfc/pfc-sh7264.c
11224 ++++ b/drivers/pinctrl/sh-pfc/pfc-sh7264.c
11225 +@@ -500,17 +500,15 @@ enum {
11226 + SD_WP_MARK, SD_CLK_MARK, SD_CMD_MARK,
11227 + CRX0_MARK, CRX1_MARK,
11228 + CTX0_MARK, CTX1_MARK,
11229 ++ CRX0_CRX1_MARK, CTX0_CTX1_MARK,
11230 +
11231 + PWM1A_MARK, PWM1B_MARK, PWM1C_MARK, PWM1D_MARK,
11232 + PWM1E_MARK, PWM1F_MARK, PWM1G_MARK, PWM1H_MARK,
11233 + PWM2A_MARK, PWM2B_MARK, PWM2C_MARK, PWM2D_MARK,
11234 + PWM2E_MARK, PWM2F_MARK, PWM2G_MARK, PWM2H_MARK,
11235 + IERXD_MARK, IETXD_MARK,
11236 +- CRX0_CRX1_MARK,
11237 + WDTOVF_MARK,
11238 +
11239 +- CRX0X1_MARK,
11240 +-
11241 + /* DMAC */
11242 + TEND0_MARK, DACK0_MARK, DREQ0_MARK,
11243 + TEND1_MARK, DACK1_MARK, DREQ1_MARK,
11244 +@@ -998,12 +996,12 @@ static const u16 pinmux_data[] = {
11245 +
11246 + PINMUX_DATA(PJ3_DATA, PJ3MD_00),
11247 + PINMUX_DATA(CRX1_MARK, PJ3MD_01),
11248 +- PINMUX_DATA(CRX0X1_MARK, PJ3MD_10),
11249 ++ PINMUX_DATA(CRX0_CRX1_MARK, PJ3MD_10),
11250 + PINMUX_DATA(IRQ1_PJ_MARK, PJ3MD_11),
11251 +
11252 + PINMUX_DATA(PJ2_DATA, PJ2MD_000),
11253 + PINMUX_DATA(CTX1_MARK, PJ2MD_001),
11254 +- PINMUX_DATA(CRX0_CRX1_MARK, PJ2MD_010),
11255 ++ PINMUX_DATA(CTX0_CTX1_MARK, PJ2MD_010),
11256 + PINMUX_DATA(CS2_MARK, PJ2MD_011),
11257 + PINMUX_DATA(SCK0_MARK, PJ2MD_100),
11258 + PINMUX_DATA(LCD_M_DISP_MARK, PJ2MD_101),
11259 +@@ -1248,6 +1246,7 @@ static const struct pinmux_func pinmux_func_gpios[] = {
11260 + GPIO_FN(CTX1),
11261 + GPIO_FN(CRX1),
11262 + GPIO_FN(CTX0),
11263 ++ GPIO_FN(CTX0_CTX1),
11264 + GPIO_FN(CRX0),
11265 + GPIO_FN(CRX0_CRX1),
11266 +
11267 +diff --git a/drivers/pinctrl/sh-pfc/pfc-sh7269.c b/drivers/pinctrl/sh-pfc/pfc-sh7269.c
11268 +index cfdb4fc177c3..3df0c0d139d0 100644
11269 +--- a/drivers/pinctrl/sh-pfc/pfc-sh7269.c
11270 ++++ b/drivers/pinctrl/sh-pfc/pfc-sh7269.c
11271 +@@ -740,13 +740,12 @@ enum {
11272 + CRX0_MARK, CTX0_MARK,
11273 + CRX1_MARK, CTX1_MARK,
11274 + CRX2_MARK, CTX2_MARK,
11275 +- CRX0_CRX1_MARK,
11276 +- CRX0_CRX1_CRX2_MARK,
11277 +- CTX0CTX1CTX2_MARK,
11278 ++ CRX0_CRX1_MARK, CTX0_CTX1_MARK,
11279 ++ CRX0_CRX1_CRX2_MARK, CTX0_CTX1_CTX2_MARK,
11280 + CRX1_PJ22_MARK, CTX1_PJ23_MARK,
11281 + CRX2_PJ20_MARK, CTX2_PJ21_MARK,
11282 +- CRX0CRX1_PJ22_MARK,
11283 +- CRX0CRX1CRX2_PJ20_MARK,
11284 ++ CRX0_CRX1_PJ22_MARK, CTX0_CTX1_PJ23_MARK,
11285 ++ CRX0_CRX1_CRX2_PJ20_MARK, CTX0_CTX1_CTX2_PJ21_MARK,
11286 +
11287 + /* VDC */
11288 + DV_CLK_MARK,
11289 +@@ -824,6 +823,7 @@ static const u16 pinmux_data[] = {
11290 + PINMUX_DATA(CS3_MARK, PC8MD_001),
11291 + PINMUX_DATA(TXD7_MARK, PC8MD_010),
11292 + PINMUX_DATA(CTX1_MARK, PC8MD_011),
11293 ++ PINMUX_DATA(CTX0_CTX1_MARK, PC8MD_100),
11294 +
11295 + PINMUX_DATA(PC7_DATA, PC7MD_000),
11296 + PINMUX_DATA(CKE_MARK, PC7MD_001),
11297 +@@ -836,11 +836,12 @@ static const u16 pinmux_data[] = {
11298 + PINMUX_DATA(CAS_MARK, PC6MD_001),
11299 + PINMUX_DATA(SCK7_MARK, PC6MD_010),
11300 + PINMUX_DATA(CTX0_MARK, PC6MD_011),
11301 ++ PINMUX_DATA(CTX0_CTX1_CTX2_MARK, PC6MD_100),
11302 +
11303 + PINMUX_DATA(PC5_DATA, PC5MD_000),
11304 + PINMUX_DATA(RAS_MARK, PC5MD_001),
11305 + PINMUX_DATA(CRX0_MARK, PC5MD_011),
11306 +- PINMUX_DATA(CTX0CTX1CTX2_MARK, PC5MD_100),
11307 ++ PINMUX_DATA(CTX0_CTX1_CTX2_MARK, PC5MD_100),
11308 + PINMUX_DATA(IRQ0_PC_MARK, PC5MD_101),
11309 +
11310 + PINMUX_DATA(PC4_DATA, PC4MD_00),
11311 +@@ -1292,30 +1293,32 @@ static const u16 pinmux_data[] = {
11312 + PINMUX_DATA(LCD_DATA23_PJ23_MARK, PJ23MD_010),
11313 + PINMUX_DATA(LCD_TCON6_MARK, PJ23MD_011),
11314 + PINMUX_DATA(IRQ3_PJ_MARK, PJ23MD_100),
11315 +- PINMUX_DATA(CTX1_MARK, PJ23MD_101),
11316 ++ PINMUX_DATA(CTX1_PJ23_MARK, PJ23MD_101),
11317 ++ PINMUX_DATA(CTX0_CTX1_PJ23_MARK, PJ23MD_110),
11318 +
11319 + PINMUX_DATA(PJ22_DATA, PJ22MD_000),
11320 + PINMUX_DATA(DV_DATA22_MARK, PJ22MD_001),
11321 + PINMUX_DATA(LCD_DATA22_PJ22_MARK, PJ22MD_010),
11322 + PINMUX_DATA(LCD_TCON5_MARK, PJ22MD_011),
11323 + PINMUX_DATA(IRQ2_PJ_MARK, PJ22MD_100),
11324 +- PINMUX_DATA(CRX1_MARK, PJ22MD_101),
11325 +- PINMUX_DATA(CRX0_CRX1_MARK, PJ22MD_110),
11326 ++ PINMUX_DATA(CRX1_PJ22_MARK, PJ22MD_101),
11327 ++ PINMUX_DATA(CRX0_CRX1_PJ22_MARK, PJ22MD_110),
11328 +
11329 + PINMUX_DATA(PJ21_DATA, PJ21MD_000),
11330 + PINMUX_DATA(DV_DATA21_MARK, PJ21MD_001),
11331 + PINMUX_DATA(LCD_DATA21_PJ21_MARK, PJ21MD_010),
11332 + PINMUX_DATA(LCD_TCON4_MARK, PJ21MD_011),
11333 + PINMUX_DATA(IRQ1_PJ_MARK, PJ21MD_100),
11334 +- PINMUX_DATA(CTX2_MARK, PJ21MD_101),
11335 ++ PINMUX_DATA(CTX2_PJ21_MARK, PJ21MD_101),
11336 ++ PINMUX_DATA(CTX0_CTX1_CTX2_PJ21_MARK, PJ21MD_110),
11337 +
11338 + PINMUX_DATA(PJ20_DATA, PJ20MD_000),
11339 + PINMUX_DATA(DV_DATA20_MARK, PJ20MD_001),
11340 + PINMUX_DATA(LCD_DATA20_PJ20_MARK, PJ20MD_010),
11341 + PINMUX_DATA(LCD_TCON3_MARK, PJ20MD_011),
11342 + PINMUX_DATA(IRQ0_PJ_MARK, PJ20MD_100),
11343 +- PINMUX_DATA(CRX2_MARK, PJ20MD_101),
11344 +- PINMUX_DATA(CRX0CRX1CRX2_PJ20_MARK, PJ20MD_110),
11345 ++ PINMUX_DATA(CRX2_PJ20_MARK, PJ20MD_101),
11346 ++ PINMUX_DATA(CRX0_CRX1_CRX2_PJ20_MARK, PJ20MD_110),
11347 +
11348 + PINMUX_DATA(PJ19_DATA, PJ19MD_000),
11349 + PINMUX_DATA(DV_DATA19_MARK, PJ19MD_001),
11350 +@@ -1666,12 +1669,24 @@ static const struct pinmux_func pinmux_func_gpios[] = {
11351 + GPIO_FN(WDTOVF),
11352 +
11353 + /* CAN */
11354 ++ GPIO_FN(CTX2),
11355 ++ GPIO_FN(CRX2),
11356 + GPIO_FN(CTX1),
11357 + GPIO_FN(CRX1),
11358 + GPIO_FN(CTX0),
11359 + GPIO_FN(CRX0),
11360 ++ GPIO_FN(CTX0_CTX1),
11361 + GPIO_FN(CRX0_CRX1),
11362 ++ GPIO_FN(CTX0_CTX1_CTX2),
11363 + GPIO_FN(CRX0_CRX1_CRX2),
11364 ++ GPIO_FN(CTX2_PJ21),
11365 ++ GPIO_FN(CRX2_PJ20),
11366 ++ GPIO_FN(CTX1_PJ23),
11367 ++ GPIO_FN(CRX1_PJ22),
11368 ++ GPIO_FN(CTX0_CTX1_PJ23),
11369 ++ GPIO_FN(CRX0_CRX1_PJ22),
11370 ++ GPIO_FN(CTX0_CTX1_CTX2_PJ21),
11371 ++ GPIO_FN(CRX0_CRX1_CRX2_PJ20),
11372 +
11373 + /* DMAC */
11374 + GPIO_FN(TEND0),
11375 +diff --git a/drivers/pwm/pwm-omap-dmtimer.c b/drivers/pwm/pwm-omap-dmtimer.c
11376 +index f45798679e3c..c6e710a713d3 100644
11377 +--- a/drivers/pwm/pwm-omap-dmtimer.c
11378 ++++ b/drivers/pwm/pwm-omap-dmtimer.c
11379 +@@ -301,15 +301,10 @@ static int pwm_omap_dmtimer_probe(struct platform_device *pdev)
11380 + goto put;
11381 + }
11382 +
11383 +-put:
11384 +- of_node_put(timer);
11385 +- if (ret < 0)
11386 +- return ret;
11387 +-
11388 + omap = devm_kzalloc(&pdev->dev, sizeof(*omap), GFP_KERNEL);
11389 + if (!omap) {
11390 +- pdata->free(dm_timer);
11391 +- return -ENOMEM;
11392 ++ ret = -ENOMEM;
11393 ++ goto err_alloc_omap;
11394 + }
11395 +
11396 + omap->pdata = pdata;
11397 +@@ -342,18 +337,38 @@ put:
11398 + ret = pwmchip_add(&omap->chip);
11399 + if (ret < 0) {
11400 + dev_err(&pdev->dev, "failed to register PWM\n");
11401 +- omap->pdata->free(omap->dm_timer);
11402 +- return ret;
11403 ++ goto err_pwmchip_add;
11404 + }
11405 +
11406 ++ of_node_put(timer);
11407 ++
11408 + platform_set_drvdata(pdev, omap);
11409 +
11410 + return 0;
11411 ++
11412 ++err_pwmchip_add:
11413 ++
11414 ++ /*
11415 ++ * *omap is allocated using devm_kzalloc,
11416 ++ * so no free necessary here
11417 ++ */
11418 ++err_alloc_omap:
11419 ++
11420 ++ pdata->free(dm_timer);
11421 ++put:
11422 ++ of_node_put(timer);
11423 ++
11424 ++ return ret;
11425 + }
11426 +
11427 + static int pwm_omap_dmtimer_remove(struct platform_device *pdev)
11428 + {
11429 + struct pwm_omap_dmtimer_chip *omap = platform_get_drvdata(pdev);
11430 ++ int ret;
11431 ++
11432 ++ ret = pwmchip_remove(&omap->chip);
11433 ++ if (ret)
11434 ++ return ret;
11435 +
11436 + if (pm_runtime_active(&omap->dm_timer_pdev->dev))
11437 + omap->pdata->stop(omap->dm_timer);
11438 +@@ -362,7 +377,7 @@ static int pwm_omap_dmtimer_remove(struct platform_device *pdev)
11439 +
11440 + mutex_destroy(&omap->mutex);
11441 +
11442 +- return pwmchip_remove(&omap->chip);
11443 ++ return 0;
11444 + }
11445 +
11446 + static const struct of_device_id pwm_omap_dmtimer_of_match[] = {
11447 +diff --git a/drivers/pwm/pwm-pca9685.c b/drivers/pwm/pwm-pca9685.c
11448 +index 567f5e2771c4..e1e5dfcb16f3 100644
11449 +--- a/drivers/pwm/pwm-pca9685.c
11450 ++++ b/drivers/pwm/pwm-pca9685.c
11451 +@@ -170,13 +170,9 @@ static void pca9685_pwm_gpio_set(struct gpio_chip *gpio, unsigned int offset,
11452 + static void pca9685_pwm_gpio_free(struct gpio_chip *gpio, unsigned int offset)
11453 + {
11454 + struct pca9685 *pca = gpiochip_get_data(gpio);
11455 +- struct pwm_device *pwm;
11456 +
11457 + pca9685_pwm_gpio_set(gpio, offset, 0);
11458 + pm_runtime_put(pca->chip.dev);
11459 +- mutex_lock(&pca->lock);
11460 +- pwm = &pca->chip.pwms[offset];
11461 +- mutex_unlock(&pca->lock);
11462 + }
11463 +
11464 + static int pca9685_pwm_gpio_get_direction(struct gpio_chip *chip,
11465 +diff --git a/drivers/regulator/rk808-regulator.c b/drivers/regulator/rk808-regulator.c
11466 +index 213b68743cc8..92498ac50303 100644
11467 +--- a/drivers/regulator/rk808-regulator.c
11468 ++++ b/drivers/regulator/rk808-regulator.c
11469 +@@ -714,7 +714,7 @@ static int rk808_regulator_dt_parse_pdata(struct device *dev,
11470 + }
11471 +
11472 + if (!pdata->dvs_gpio[i]) {
11473 +- dev_warn(dev, "there is no dvs%d gpio\n", i);
11474 ++ dev_info(dev, "there is no dvs%d gpio\n", i);
11475 + continue;
11476 + }
11477 +
11478 +diff --git a/drivers/remoteproc/remoteproc_core.c b/drivers/remoteproc/remoteproc_core.c
11479 +index aa6206706fe3..abbef17c97ee 100644
11480 +--- a/drivers/remoteproc/remoteproc_core.c
11481 ++++ b/drivers/remoteproc/remoteproc_core.c
11482 +@@ -1786,7 +1786,7 @@ static int __init remoteproc_init(void)
11483 +
11484 + return 0;
11485 + }
11486 +-module_init(remoteproc_init);
11487 ++subsys_initcall(remoteproc_init);
11488 +
11489 + static void __exit remoteproc_exit(void)
11490 + {
11491 +diff --git a/drivers/reset/reset-uniphier.c b/drivers/reset/reset-uniphier.c
11492 +index 5605745663ae..adbecb2c7cd3 100644
11493 +--- a/drivers/reset/reset-uniphier.c
11494 ++++ b/drivers/reset/reset-uniphier.c
11495 +@@ -202,8 +202,8 @@ static const struct uniphier_reset_data uniphier_pro5_sd_reset_data[] = {
11496 + #define UNIPHIER_PERI_RESET_FI2C(id, ch) \
11497 + UNIPHIER_RESETX((id), 0x114, 24 + (ch))
11498 +
11499 +-#define UNIPHIER_PERI_RESET_SCSSI(id) \
11500 +- UNIPHIER_RESETX((id), 0x110, 17)
11501 ++#define UNIPHIER_PERI_RESET_SCSSI(id, ch) \
11502 ++ UNIPHIER_RESETX((id), 0x110, 17 + (ch))
11503 +
11504 + #define UNIPHIER_PERI_RESET_MCSSI(id) \
11505 + UNIPHIER_RESETX((id), 0x114, 14)
11506 +@@ -218,7 +218,7 @@ static const struct uniphier_reset_data uniphier_ld4_peri_reset_data[] = {
11507 + UNIPHIER_PERI_RESET_I2C(6, 2),
11508 + UNIPHIER_PERI_RESET_I2C(7, 3),
11509 + UNIPHIER_PERI_RESET_I2C(8, 4),
11510 +- UNIPHIER_PERI_RESET_SCSSI(11),
11511 ++ UNIPHIER_PERI_RESET_SCSSI(11, 0),
11512 + UNIPHIER_RESET_END,
11513 + };
11514 +
11515 +@@ -234,8 +234,11 @@ static const struct uniphier_reset_data uniphier_pro4_peri_reset_data[] = {
11516 + UNIPHIER_PERI_RESET_FI2C(8, 4),
11517 + UNIPHIER_PERI_RESET_FI2C(9, 5),
11518 + UNIPHIER_PERI_RESET_FI2C(10, 6),
11519 +- UNIPHIER_PERI_RESET_SCSSI(11),
11520 +- UNIPHIER_PERI_RESET_MCSSI(12),
11521 ++ UNIPHIER_PERI_RESET_SCSSI(11, 0),
11522 ++ UNIPHIER_PERI_RESET_SCSSI(12, 1),
11523 ++ UNIPHIER_PERI_RESET_SCSSI(13, 2),
11524 ++ UNIPHIER_PERI_RESET_SCSSI(14, 3),
11525 ++ UNIPHIER_PERI_RESET_MCSSI(15),
11526 + UNIPHIER_RESET_END,
11527 + };
11528 +
11529 +diff --git a/drivers/scsi/aic7xxx/aic7xxx_core.c b/drivers/scsi/aic7xxx/aic7xxx_core.c
11530 +index 915a34f141e4..49e02e874553 100644
11531 +--- a/drivers/scsi/aic7xxx/aic7xxx_core.c
11532 ++++ b/drivers/scsi/aic7xxx/aic7xxx_core.c
11533 +@@ -2321,7 +2321,7 @@ ahc_find_syncrate(struct ahc_softc *ahc, u_int *period,
11534 + * At some speeds, we only support
11535 + * ST transfers.
11536 + */
11537 +- if ((syncrate->sxfr_u2 & ST_SXFR) != 0)
11538 ++ if ((syncrate->sxfr_u2 & ST_SXFR) != 0)
11539 + *ppr_options &= ~MSG_EXT_PPR_DT_REQ;
11540 + break;
11541 + }
11542 +diff --git a/drivers/scsi/iscsi_tcp.c b/drivers/scsi/iscsi_tcp.c
11543 +index 55181d28291e..7212e3a13fe6 100644
11544 +--- a/drivers/scsi/iscsi_tcp.c
11545 ++++ b/drivers/scsi/iscsi_tcp.c
11546 +@@ -892,6 +892,10 @@ free_host:
11547 + static void iscsi_sw_tcp_session_destroy(struct iscsi_cls_session *cls_session)
11548 + {
11549 + struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
11550 ++ struct iscsi_session *session = cls_session->dd_data;
11551 ++
11552 ++ if (WARN_ON_ONCE(session->leadconn))
11553 ++ return;
11554 +
11555 + iscsi_tcp_r2tpool_free(cls_session->dd_data);
11556 + iscsi_session_teardown(cls_session);
11557 +diff --git a/drivers/scsi/scsi_transport_iscsi.c b/drivers/scsi/scsi_transport_iscsi.c
11558 +index 4c4781e5974f..c0fb9e789080 100644
11559 +--- a/drivers/scsi/scsi_transport_iscsi.c
11560 ++++ b/drivers/scsi/scsi_transport_iscsi.c
11561 +@@ -2945,6 +2945,24 @@ iscsi_set_path(struct iscsi_transport *transport, struct iscsi_uevent *ev)
11562 + return err;
11563 + }
11564 +
11565 ++static int iscsi_session_has_conns(int sid)
11566 ++{
11567 ++ struct iscsi_cls_conn *conn;
11568 ++ unsigned long flags;
11569 ++ int found = 0;
11570 ++
11571 ++ spin_lock_irqsave(&connlock, flags);
11572 ++ list_for_each_entry(conn, &connlist, conn_list) {
11573 ++ if (iscsi_conn_get_sid(conn) == sid) {
11574 ++ found = 1;
11575 ++ break;
11576 ++ }
11577 ++ }
11578 ++ spin_unlock_irqrestore(&connlock, flags);
11579 ++
11580 ++ return found;
11581 ++}
11582 ++
11583 + static int
11584 + iscsi_set_iface_params(struct iscsi_transport *transport,
11585 + struct iscsi_uevent *ev, uint32_t len)
11586 +@@ -3522,10 +3540,12 @@ iscsi_if_recv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, uint32_t *group)
11587 + break;
11588 + case ISCSI_UEVENT_DESTROY_SESSION:
11589 + session = iscsi_session_lookup(ev->u.d_session.sid);
11590 +- if (session)
11591 +- transport->destroy_session(session);
11592 +- else
11593 ++ if (!session)
11594 + err = -EINVAL;
11595 ++ else if (iscsi_session_has_conns(ev->u.d_session.sid))
11596 ++ err = -EBUSY;
11597 ++ else
11598 ++ transport->destroy_session(session);
11599 + break;
11600 + case ISCSI_UEVENT_UNBIND_SESSION:
11601 + session = iscsi_session_lookup(ev->u.d_session.sid);
11602 +diff --git a/drivers/scsi/ufs/ufshcd.c b/drivers/scsi/ufs/ufshcd.c
11603 +index f4fcaee41dc2..b3dee24917a8 100644
11604 +--- a/drivers/scsi/ufs/ufshcd.c
11605 ++++ b/drivers/scsi/ufs/ufshcd.c
11606 +@@ -4809,7 +4809,7 @@ ufshcd_transfer_rsp_status(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
11607 + break;
11608 + } /* end of switch */
11609 +
11610 +- if (host_byte(result) != DID_OK)
11611 ++ if ((host_byte(result) != DID_OK) && !hba->silence_err_logs)
11612 + ufshcd_print_trs(hba, 1 << lrbp->task_tag, true);
11613 + return result;
11614 + }
11615 +@@ -5341,8 +5341,8 @@ static void ufshcd_err_handler(struct work_struct *work)
11616 +
11617 + /*
11618 + * if host reset is required then skip clearing the pending
11619 +- * transfers forcefully because they will automatically get
11620 +- * cleared after link startup.
11621 ++ * transfers forcefully because they will get cleared during
11622 ++ * host reset and restore
11623 + */
11624 + if (needs_reset)
11625 + goto skip_pending_xfer_clear;
11626 +@@ -5996,9 +5996,15 @@ static int ufshcd_host_reset_and_restore(struct ufs_hba *hba)
11627 + int err;
11628 + unsigned long flags;
11629 +
11630 +- /* Reset the host controller */
11631 ++ /*
11632 ++ * Stop the host controller and complete the requests
11633 ++ * cleared by h/w
11634 ++ */
11635 + spin_lock_irqsave(hba->host->host_lock, flags);
11636 + ufshcd_hba_stop(hba, false);
11637 ++ hba->silence_err_logs = true;
11638 ++ ufshcd_complete_requests(hba);
11639 ++ hba->silence_err_logs = false;
11640 + spin_unlock_irqrestore(hba->host->host_lock, flags);
11641 +
11642 + /* scale up clocks to max frequency before full reinitialization */
11643 +@@ -6032,22 +6038,12 @@ out:
11644 + static int ufshcd_reset_and_restore(struct ufs_hba *hba)
11645 + {
11646 + int err = 0;
11647 +- unsigned long flags;
11648 + int retries = MAX_HOST_RESET_RETRIES;
11649 +
11650 + do {
11651 + err = ufshcd_host_reset_and_restore(hba);
11652 + } while (err && --retries);
11653 +
11654 +- /*
11655 +- * After reset the door-bell might be cleared, complete
11656 +- * outstanding requests in s/w here.
11657 +- */
11658 +- spin_lock_irqsave(hba->host->host_lock, flags);
11659 +- ufshcd_transfer_req_compl(hba);
11660 +- ufshcd_tmc_handler(hba);
11661 +- spin_unlock_irqrestore(hba->host->host_lock, flags);
11662 +-
11663 + return err;
11664 + }
11665 +
11666 +diff --git a/drivers/scsi/ufs/ufshcd.h b/drivers/scsi/ufs/ufshcd.h
11667 +index 33fdd3f281ae..4554a4b725b5 100644
11668 +--- a/drivers/scsi/ufs/ufshcd.h
11669 ++++ b/drivers/scsi/ufs/ufshcd.h
11670 +@@ -489,6 +489,7 @@ struct ufs_stats {
11671 + * @uic_error: UFS interconnect layer error status
11672 + * @saved_err: sticky error mask
11673 + * @saved_uic_err: sticky UIC error mask
11674 ++ * @silence_err_logs: flag to silence error logs
11675 + * @dev_cmd: ufs device management command information
11676 + * @last_dme_cmd_tstamp: time stamp of the last completed DME command
11677 + * @auto_bkops_enabled: to track whether bkops is enabled in device
11678 +@@ -645,6 +646,7 @@ struct ufs_hba {
11679 + u32 saved_err;
11680 + u32 saved_uic_err;
11681 + struct ufs_stats ufs_stats;
11682 ++ bool silence_err_logs;
11683 +
11684 + /* Device management request data */
11685 + struct ufs_dev_cmd dev_cmd;
11686 +diff --git a/drivers/soc/tegra/fuse/tegra-apbmisc.c b/drivers/soc/tegra/fuse/tegra-apbmisc.c
11687 +index e5a4d8f98b10..d1cbb0fe1691 100644
11688 +--- a/drivers/soc/tegra/fuse/tegra-apbmisc.c
11689 ++++ b/drivers/soc/tegra/fuse/tegra-apbmisc.c
11690 +@@ -135,7 +135,7 @@ void __init tegra_init_apbmisc(void)
11691 + apbmisc.flags = IORESOURCE_MEM;
11692 +
11693 + /* strapping options */
11694 +- if (tegra_get_chip_id() == TEGRA124) {
11695 ++ if (of_machine_is_compatible("nvidia,tegra124")) {
11696 + straps.start = 0x7000e864;
11697 + straps.end = 0x7000e867;
11698 + } else {
11699 +diff --git a/drivers/staging/rtl8188eu/os_dep/ioctl_linux.c b/drivers/staging/rtl8188eu/os_dep/ioctl_linux.c
11700 +index bee3c3a7a7a9..2db4444267a7 100644
11701 +--- a/drivers/staging/rtl8188eu/os_dep/ioctl_linux.c
11702 ++++ b/drivers/staging/rtl8188eu/os_dep/ioctl_linux.c
11703 +@@ -229,18 +229,21 @@ static char *translate_scan(struct adapter *padapter,
11704 +
11705 + /* parsing WPA/WPA2 IE */
11706 + {
11707 +- u8 buf[MAX_WPA_IE_LEN];
11708 ++ u8 *buf;
11709 + u8 wpa_ie[255], rsn_ie[255];
11710 + u16 wpa_len = 0, rsn_len = 0;
11711 + u8 *p;
11712 +
11713 ++ buf = kzalloc(MAX_WPA_IE_LEN, GFP_ATOMIC);
11714 ++ if (!buf)
11715 ++ return start;
11716 ++
11717 + rtw_get_sec_ie(pnetwork->network.ies, pnetwork->network.ie_length, rsn_ie, &rsn_len, wpa_ie, &wpa_len);
11718 + RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("rtw_wx_get_scan: ssid =%s\n", pnetwork->network.Ssid.Ssid));
11719 + RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("rtw_wx_get_scan: wpa_len =%d rsn_len =%d\n", wpa_len, rsn_len));
11720 +
11721 + if (wpa_len > 0) {
11722 + p = buf;
11723 +- memset(buf, 0, MAX_WPA_IE_LEN);
11724 + p += sprintf(p, "wpa_ie=");
11725 + for (i = 0; i < wpa_len; i++)
11726 + p += sprintf(p, "%02x", wpa_ie[i]);
11727 +@@ -257,7 +260,6 @@ static char *translate_scan(struct adapter *padapter,
11728 + }
11729 + if (rsn_len > 0) {
11730 + p = buf;
11731 +- memset(buf, 0, MAX_WPA_IE_LEN);
11732 + p += sprintf(p, "rsn_ie=");
11733 + for (i = 0; i < rsn_len; i++)
11734 + p += sprintf(p, "%02x", rsn_ie[i]);
11735 +@@ -271,6 +273,7 @@ static char *translate_scan(struct adapter *padapter,
11736 + iwe.u.data.length = rsn_len;
11737 + start = iwe_stream_add_point(info, start, stop, &iwe, rsn_ie);
11738 + }
11739 ++ kfree(buf);
11740 + }
11741 +
11742 + {/* parsing WPS IE */
11743 +diff --git a/drivers/tty/synclink_gt.c b/drivers/tty/synclink_gt.c
11744 +index b88ecf102764..e9779b03ee56 100644
11745 +--- a/drivers/tty/synclink_gt.c
11746 ++++ b/drivers/tty/synclink_gt.c
11747 +@@ -1335,10 +1335,10 @@ static void throttle(struct tty_struct * tty)
11748 + DBGINFO(("%s throttle\n", info->device_name));
11749 + if (I_IXOFF(tty))
11750 + send_xchar(tty, STOP_CHAR(tty));
11751 +- if (C_CRTSCTS(tty)) {
11752 ++ if (C_CRTSCTS(tty)) {
11753 + spin_lock_irqsave(&info->lock,flags);
11754 + info->signals &= ~SerialSignal_RTS;
11755 +- set_signals(info);
11756 ++ set_signals(info);
11757 + spin_unlock_irqrestore(&info->lock,flags);
11758 + }
11759 + }
11760 +@@ -1360,10 +1360,10 @@ static void unthrottle(struct tty_struct * tty)
11761 + else
11762 + send_xchar(tty, START_CHAR(tty));
11763 + }
11764 +- if (C_CRTSCTS(tty)) {
11765 ++ if (C_CRTSCTS(tty)) {
11766 + spin_lock_irqsave(&info->lock,flags);
11767 + info->signals |= SerialSignal_RTS;
11768 +- set_signals(info);
11769 ++ set_signals(info);
11770 + spin_unlock_irqrestore(&info->lock,flags);
11771 + }
11772 + }
11773 +@@ -2561,8 +2561,8 @@ static void change_params(struct slgt_info *info)
11774 + info->read_status_mask = IRQ_RXOVER;
11775 + if (I_INPCK(info->port.tty))
11776 + info->read_status_mask |= MASK_PARITY | MASK_FRAMING;
11777 +- if (I_BRKINT(info->port.tty) || I_PARMRK(info->port.tty))
11778 +- info->read_status_mask |= MASK_BREAK;
11779 ++ if (I_BRKINT(info->port.tty) || I_PARMRK(info->port.tty))
11780 ++ info->read_status_mask |= MASK_BREAK;
11781 + if (I_IGNPAR(info->port.tty))
11782 + info->ignore_status_mask |= MASK_PARITY | MASK_FRAMING;
11783 + if (I_IGNBRK(info->port.tty)) {
11784 +@@ -3193,7 +3193,7 @@ static int tiocmset(struct tty_struct *tty,
11785 + info->signals &= ~SerialSignal_DTR;
11786 +
11787 + spin_lock_irqsave(&info->lock,flags);
11788 +- set_signals(info);
11789 ++ set_signals(info);
11790 + spin_unlock_irqrestore(&info->lock,flags);
11791 + return 0;
11792 + }
11793 +@@ -3204,7 +3204,7 @@ static int carrier_raised(struct tty_port *port)
11794 + struct slgt_info *info = container_of(port, struct slgt_info, port);
11795 +
11796 + spin_lock_irqsave(&info->lock,flags);
11797 +- get_signals(info);
11798 ++ get_signals(info);
11799 + spin_unlock_irqrestore(&info->lock,flags);
11800 + return (info->signals & SerialSignal_DCD) ? 1 : 0;
11801 + }
11802 +@@ -3219,7 +3219,7 @@ static void dtr_rts(struct tty_port *port, int on)
11803 + info->signals |= SerialSignal_RTS | SerialSignal_DTR;
11804 + else
11805 + info->signals &= ~(SerialSignal_RTS | SerialSignal_DTR);
11806 +- set_signals(info);
11807 ++ set_signals(info);
11808 + spin_unlock_irqrestore(&info->lock,flags);
11809 + }
11810 +
11811 +diff --git a/drivers/tty/synclinkmp.c b/drivers/tty/synclinkmp.c
11812 +index 1e4d5b9c981a..57c2c647af61 100644
11813 +--- a/drivers/tty/synclinkmp.c
11814 ++++ b/drivers/tty/synclinkmp.c
11815 +@@ -1454,10 +1454,10 @@ static void throttle(struct tty_struct * tty)
11816 + if (I_IXOFF(tty))
11817 + send_xchar(tty, STOP_CHAR(tty));
11818 +
11819 +- if (C_CRTSCTS(tty)) {
11820 ++ if (C_CRTSCTS(tty)) {
11821 + spin_lock_irqsave(&info->lock,flags);
11822 + info->serial_signals &= ~SerialSignal_RTS;
11823 +- set_signals(info);
11824 ++ set_signals(info);
11825 + spin_unlock_irqrestore(&info->lock,flags);
11826 + }
11827 + }
11828 +@@ -1483,10 +1483,10 @@ static void unthrottle(struct tty_struct * tty)
11829 + send_xchar(tty, START_CHAR(tty));
11830 + }
11831 +
11832 +- if (C_CRTSCTS(tty)) {
11833 ++ if (C_CRTSCTS(tty)) {
11834 + spin_lock_irqsave(&info->lock,flags);
11835 + info->serial_signals |= SerialSignal_RTS;
11836 +- set_signals(info);
11837 ++ set_signals(info);
11838 + spin_unlock_irqrestore(&info->lock,flags);
11839 + }
11840 + }
11841 +@@ -2471,7 +2471,7 @@ static void isr_io_pin( SLMP_INFO *info, u16 status )
11842 + if (status & SerialSignal_CTS) {
11843 + if ( debug_level >= DEBUG_LEVEL_ISR )
11844 + printk("CTS tx start...");
11845 +- info->port.tty->hw_stopped = 0;
11846 ++ info->port.tty->hw_stopped = 0;
11847 + tx_start(info);
11848 + info->pending_bh |= BH_TRANSMIT;
11849 + return;
11850 +@@ -2480,7 +2480,7 @@ static void isr_io_pin( SLMP_INFO *info, u16 status )
11851 + if (!(status & SerialSignal_CTS)) {
11852 + if ( debug_level >= DEBUG_LEVEL_ISR )
11853 + printk("CTS tx stop...");
11854 +- info->port.tty->hw_stopped = 1;
11855 ++ info->port.tty->hw_stopped = 1;
11856 + tx_stop(info);
11857 + }
11858 + }
11859 +@@ -2807,8 +2807,8 @@ static void change_params(SLMP_INFO *info)
11860 + info->read_status_mask2 = OVRN;
11861 + if (I_INPCK(info->port.tty))
11862 + info->read_status_mask2 |= PE | FRME;
11863 +- if (I_BRKINT(info->port.tty) || I_PARMRK(info->port.tty))
11864 +- info->read_status_mask1 |= BRKD;
11865 ++ if (I_BRKINT(info->port.tty) || I_PARMRK(info->port.tty))
11866 ++ info->read_status_mask1 |= BRKD;
11867 + if (I_IGNPAR(info->port.tty))
11868 + info->ignore_status_mask2 |= PE | FRME;
11869 + if (I_IGNBRK(info->port.tty)) {
11870 +@@ -3178,7 +3178,7 @@ static int tiocmget(struct tty_struct *tty)
11871 + unsigned long flags;
11872 +
11873 + spin_lock_irqsave(&info->lock,flags);
11874 +- get_signals(info);
11875 ++ get_signals(info);
11876 + spin_unlock_irqrestore(&info->lock,flags);
11877 +
11878 + result = ((info->serial_signals & SerialSignal_RTS) ? TIOCM_RTS : 0) |
11879 +@@ -3216,7 +3216,7 @@ static int tiocmset(struct tty_struct *tty,
11880 + info->serial_signals &= ~SerialSignal_DTR;
11881 +
11882 + spin_lock_irqsave(&info->lock,flags);
11883 +- set_signals(info);
11884 ++ set_signals(info);
11885 + spin_unlock_irqrestore(&info->lock,flags);
11886 +
11887 + return 0;
11888 +@@ -3228,7 +3228,7 @@ static int carrier_raised(struct tty_port *port)
11889 + unsigned long flags;
11890 +
11891 + spin_lock_irqsave(&info->lock,flags);
11892 +- get_signals(info);
11893 ++ get_signals(info);
11894 + spin_unlock_irqrestore(&info->lock,flags);
11895 +
11896 + return (info->serial_signals & SerialSignal_DCD) ? 1 : 0;
11897 +@@ -3244,7 +3244,7 @@ static void dtr_rts(struct tty_port *port, int on)
11898 + info->serial_signals |= SerialSignal_RTS | SerialSignal_DTR;
11899 + else
11900 + info->serial_signals &= ~(SerialSignal_RTS | SerialSignal_DTR);
11901 +- set_signals(info);
11902 ++ set_signals(info);
11903 + spin_unlock_irqrestore(&info->lock,flags);
11904 + }
11905 +
11906 +diff --git a/drivers/uio/uio_dmem_genirq.c b/drivers/uio/uio_dmem_genirq.c
11907 +index e1134a4d97f3..a00b4aee6c79 100644
11908 +--- a/drivers/uio/uio_dmem_genirq.c
11909 ++++ b/drivers/uio/uio_dmem_genirq.c
11910 +@@ -135,11 +135,13 @@ static int uio_dmem_genirq_irqcontrol(struct uio_info *dev_info, s32 irq_on)
11911 + if (irq_on) {
11912 + if (test_and_clear_bit(0, &priv->flags))
11913 + enable_irq(dev_info->irq);
11914 ++ spin_unlock_irqrestore(&priv->lock, flags);
11915 + } else {
11916 +- if (!test_and_set_bit(0, &priv->flags))
11917 ++ if (!test_and_set_bit(0, &priv->flags)) {
11918 ++ spin_unlock_irqrestore(&priv->lock, flags);
11919 + disable_irq(dev_info->irq);
11920 ++ }
11921 + }
11922 +- spin_unlock_irqrestore(&priv->lock, flags);
11923 +
11924 + return 0;
11925 + }
11926 +diff --git a/drivers/usb/dwc2/gadget.c b/drivers/usb/dwc2/gadget.c
11927 +index f64d1cd08fb6..17f3e7b4d4fe 100644
11928 +--- a/drivers/usb/dwc2/gadget.c
11929 ++++ b/drivers/usb/dwc2/gadget.c
11930 +@@ -3918,11 +3918,12 @@ static int dwc2_hsotg_ep_enable(struct usb_ep *ep,
11931 + * a unique tx-fifo even if it is non-periodic.
11932 + */
11933 + if (dir_in && hsotg->dedicated_fifos) {
11934 ++ unsigned fifo_count = dwc2_hsotg_tx_fifo_count(hsotg);
11935 + u32 fifo_index = 0;
11936 + u32 fifo_size = UINT_MAX;
11937 +
11938 + size = hs_ep->ep.maxpacket * hs_ep->mc;
11939 +- for (i = 1; i < hsotg->num_of_eps; ++i) {
11940 ++ for (i = 1; i <= fifo_count; ++i) {
11941 + if (hsotg->fifo_map & (1 << i))
11942 + continue;
11943 + val = dwc2_readl(hsotg, DPTXFSIZN(i));
11944 +diff --git a/drivers/usb/gadget/udc/gr_udc.c b/drivers/usb/gadget/udc/gr_udc.c
11945 +index 729e60e49564..e50108f9a374 100644
11946 +--- a/drivers/usb/gadget/udc/gr_udc.c
11947 ++++ b/drivers/usb/gadget/udc/gr_udc.c
11948 +@@ -2180,8 +2180,6 @@ static int gr_probe(struct platform_device *pdev)
11949 + return -ENOMEM;
11950 + }
11951 +
11952 +- spin_lock(&dev->lock);
11953 +-
11954 + /* Inside lock so that no gadget can use this udc until probe is done */
11955 + retval = usb_add_gadget_udc(dev->dev, &dev->gadget);
11956 + if (retval) {
11957 +@@ -2190,15 +2188,21 @@ static int gr_probe(struct platform_device *pdev)
11958 + }
11959 + dev->added = 1;
11960 +
11961 ++ spin_lock(&dev->lock);
11962 ++
11963 + retval = gr_udc_init(dev);
11964 +- if (retval)
11965 ++ if (retval) {
11966 ++ spin_unlock(&dev->lock);
11967 + goto out;
11968 +-
11969 +- gr_dfs_create(dev);
11970 ++ }
11971 +
11972 + /* Clear all interrupt enables that might be left on since last boot */
11973 + gr_disable_interrupts_and_pullup(dev);
11974 +
11975 ++ spin_unlock(&dev->lock);
11976 ++
11977 ++ gr_dfs_create(dev);
11978 ++
11979 + retval = gr_request_irq(dev, dev->irq);
11980 + if (retval) {
11981 + dev_err(dev->dev, "Failed to request irq %d\n", dev->irq);
11982 +@@ -2227,8 +2231,6 @@ static int gr_probe(struct platform_device *pdev)
11983 + dev_info(dev->dev, "regs: %p, irq %d\n", dev->regs, dev->irq);
11984 +
11985 + out:
11986 +- spin_unlock(&dev->lock);
11987 +-
11988 + if (retval)
11989 + gr_remove(pdev);
11990 +
11991 +diff --git a/drivers/usb/musb/omap2430.c b/drivers/usb/musb/omap2430.c
11992 +index b1dd81fb5f55..24e622c05638 100644
11993 +--- a/drivers/usb/musb/omap2430.c
11994 ++++ b/drivers/usb/musb/omap2430.c
11995 +@@ -361,8 +361,6 @@ static const struct musb_platform_ops omap2430_ops = {
11996 + .init = omap2430_musb_init,
11997 + .exit = omap2430_musb_exit,
11998 +
11999 +- .set_vbus = omap2430_musb_set_vbus,
12000 +-
12001 + .enable = omap2430_musb_enable,
12002 + .disable = omap2430_musb_disable,
12003 +
12004 +diff --git a/drivers/video/fbdev/pxa168fb.c b/drivers/video/fbdev/pxa168fb.c
12005 +index d059d04c63ac..20195d3dbf08 100644
12006 +--- a/drivers/video/fbdev/pxa168fb.c
12007 ++++ b/drivers/video/fbdev/pxa168fb.c
12008 +@@ -769,8 +769,8 @@ failed_free_cmap:
12009 + failed_free_clk:
12010 + clk_disable_unprepare(fbi->clk);
12011 + failed_free_fbmem:
12012 +- dma_free_coherent(fbi->dev, info->fix.smem_len,
12013 +- info->screen_base, fbi->fb_start_dma);
12014 ++ dma_free_wc(fbi->dev, info->fix.smem_len,
12015 ++ info->screen_base, fbi->fb_start_dma);
12016 + failed_free_info:
12017 + kfree(info);
12018 +
12019 +@@ -804,7 +804,7 @@ static int pxa168fb_remove(struct platform_device *pdev)
12020 +
12021 + irq = platform_get_irq(pdev, 0);
12022 +
12023 +- dma_free_wc(fbi->dev, PAGE_ALIGN(info->fix.smem_len),
12024 ++ dma_free_wc(fbi->dev, info->fix.smem_len,
12025 + info->screen_base, info->fix.smem_start);
12026 +
12027 + clk_disable_unprepare(fbi->clk);
12028 +diff --git a/drivers/virtio/virtio_balloon.c b/drivers/virtio/virtio_balloon.c
12029 +index 14ac36ca8fbd..1afcbef397ab 100644
12030 +--- a/drivers/virtio/virtio_balloon.c
12031 ++++ b/drivers/virtio/virtio_balloon.c
12032 +@@ -126,6 +126,8 @@ static void set_page_pfns(struct virtio_balloon *vb,
12033 + {
12034 + unsigned int i;
12035 +
12036 ++ BUILD_BUG_ON(VIRTIO_BALLOON_PAGES_PER_PAGE > VIRTIO_BALLOON_ARRAY_PFNS_MAX);
12037 ++
12038 + /*
12039 + * Set balloon pfns pointing at this page.
12040 + * Note that the first pfn points at start of the page.
12041 +diff --git a/drivers/visorbus/visorchipset.c b/drivers/visorbus/visorchipset.c
12042 +index ca752b8f495f..cb1eb7e05f87 100644
12043 +--- a/drivers/visorbus/visorchipset.c
12044 ++++ b/drivers/visorbus/visorchipset.c
12045 +@@ -1210,14 +1210,17 @@ static void setup_crash_devices_work_queue(struct work_struct *work)
12046 + {
12047 + struct controlvm_message local_crash_bus_msg;
12048 + struct controlvm_message local_crash_dev_msg;
12049 +- struct controlvm_message msg;
12050 ++ struct controlvm_message msg = {
12051 ++ .hdr.id = CONTROLVM_CHIPSET_INIT,
12052 ++ .cmd.init_chipset = {
12053 ++ .bus_count = 23,
12054 ++ .switch_count = 0,
12055 ++ },
12056 ++ };
12057 + u32 local_crash_msg_offset;
12058 + u16 local_crash_msg_count;
12059 +
12060 + /* send init chipset msg */
12061 +- msg.hdr.id = CONTROLVM_CHIPSET_INIT;
12062 +- msg.cmd.init_chipset.bus_count = 23;
12063 +- msg.cmd.init_chipset.switch_count = 0;
12064 + chipset_init(&msg);
12065 + /* get saved message count */
12066 + if (visorchannel_read(chipset_dev->controlvm_channel,
12067 +diff --git a/drivers/vme/bridges/vme_fake.c b/drivers/vme/bridges/vme_fake.c
12068 +index 7d83691047f4..685a43bdc2a1 100644
12069 +--- a/drivers/vme/bridges/vme_fake.c
12070 ++++ b/drivers/vme/bridges/vme_fake.c
12071 +@@ -418,8 +418,9 @@ static void fake_lm_check(struct fake_driver *bridge, unsigned long long addr,
12072 + }
12073 + }
12074 +
12075 +-static u8 fake_vmeread8(struct fake_driver *bridge, unsigned long long addr,
12076 +- u32 aspace, u32 cycle)
12077 ++static noinline_for_stack u8 fake_vmeread8(struct fake_driver *bridge,
12078 ++ unsigned long long addr,
12079 ++ u32 aspace, u32 cycle)
12080 + {
12081 + u8 retval = 0xff;
12082 + int i;
12083 +@@ -450,8 +451,9 @@ static u8 fake_vmeread8(struct fake_driver *bridge, unsigned long long addr,
12084 + return retval;
12085 + }
12086 +
12087 +-static u16 fake_vmeread16(struct fake_driver *bridge, unsigned long long addr,
12088 +- u32 aspace, u32 cycle)
12089 ++static noinline_for_stack u16 fake_vmeread16(struct fake_driver *bridge,
12090 ++ unsigned long long addr,
12091 ++ u32 aspace, u32 cycle)
12092 + {
12093 + u16 retval = 0xffff;
12094 + int i;
12095 +@@ -482,8 +484,9 @@ static u16 fake_vmeread16(struct fake_driver *bridge, unsigned long long addr,
12096 + return retval;
12097 + }
12098 +
12099 +-static u32 fake_vmeread32(struct fake_driver *bridge, unsigned long long addr,
12100 +- u32 aspace, u32 cycle)
12101 ++static noinline_for_stack u32 fake_vmeread32(struct fake_driver *bridge,
12102 ++ unsigned long long addr,
12103 ++ u32 aspace, u32 cycle)
12104 + {
12105 + u32 retval = 0xffffffff;
12106 + int i;
12107 +@@ -613,8 +616,9 @@ out:
12108 + return retval;
12109 + }
12110 +
12111 +-static void fake_vmewrite8(struct fake_driver *bridge, u8 *buf,
12112 +- unsigned long long addr, u32 aspace, u32 cycle)
12113 ++static noinline_for_stack void fake_vmewrite8(struct fake_driver *bridge,
12114 ++ u8 *buf, unsigned long long addr,
12115 ++ u32 aspace, u32 cycle)
12116 + {
12117 + int i;
12118 + unsigned long long start, end, offset;
12119 +@@ -643,8 +647,9 @@ static void fake_vmewrite8(struct fake_driver *bridge, u8 *buf,
12120 +
12121 + }
12122 +
12123 +-static void fake_vmewrite16(struct fake_driver *bridge, u16 *buf,
12124 +- unsigned long long addr, u32 aspace, u32 cycle)
12125 ++static noinline_for_stack void fake_vmewrite16(struct fake_driver *bridge,
12126 ++ u16 *buf, unsigned long long addr,
12127 ++ u32 aspace, u32 cycle)
12128 + {
12129 + int i;
12130 + unsigned long long start, end, offset;
12131 +@@ -673,8 +678,9 @@ static void fake_vmewrite16(struct fake_driver *bridge, u16 *buf,
12132 +
12133 + }
12134 +
12135 +-static void fake_vmewrite32(struct fake_driver *bridge, u32 *buf,
12136 +- unsigned long long addr, u32 aspace, u32 cycle)
12137 ++static noinline_for_stack void fake_vmewrite32(struct fake_driver *bridge,
12138 ++ u32 *buf, unsigned long long addr,
12139 ++ u32 aspace, u32 cycle)
12140 + {
12141 + int i;
12142 + unsigned long long start, end, offset;
12143 +diff --git a/fs/btrfs/check-integrity.c b/fs/btrfs/check-integrity.c
12144 +index 833cf3c35b4d..3b77c8ab5357 100644
12145 +--- a/fs/btrfs/check-integrity.c
12146 ++++ b/fs/btrfs/check-integrity.c
12147 +@@ -629,7 +629,6 @@ static struct btrfsic_dev_state *btrfsic_dev_state_hashtable_lookup(dev_t dev,
12148 + static int btrfsic_process_superblock(struct btrfsic_state *state,
12149 + struct btrfs_fs_devices *fs_devices)
12150 + {
12151 +- struct btrfs_fs_info *fs_info = state->fs_info;
12152 + struct btrfs_super_block *selected_super;
12153 + struct list_head *dev_head = &fs_devices->devices;
12154 + struct btrfs_device *device;
12155 +@@ -700,7 +699,7 @@ static int btrfsic_process_superblock(struct btrfsic_state *state,
12156 + break;
12157 + }
12158 +
12159 +- num_copies = btrfs_num_copies(fs_info, next_bytenr,
12160 ++ num_copies = btrfs_num_copies(state->fs_info, next_bytenr,
12161 + state->metablock_size);
12162 + if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
12163 + pr_info("num_copies(log_bytenr=%llu) = %d\n",
12164 +diff --git a/fs/btrfs/file-item.c b/fs/btrfs/file-item.c
12165 +index 4cf2817ab120..f9e280d0b44f 100644
12166 +--- a/fs/btrfs/file-item.c
12167 ++++ b/fs/btrfs/file-item.c
12168 +@@ -275,7 +275,8 @@ found:
12169 + csum += count * csum_size;
12170 + nblocks -= count;
12171 + next:
12172 +- while (count--) {
12173 ++ while (count > 0) {
12174 ++ count--;
12175 + disk_bytenr += fs_info->sectorsize;
12176 + offset += fs_info->sectorsize;
12177 + page_bytes_left -= fs_info->sectorsize;
12178 +diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
12179 +index 5bbcdcff68a9..9c3b394b99fa 100644
12180 +--- a/fs/btrfs/volumes.c
12181 ++++ b/fs/btrfs/volumes.c
12182 +@@ -7260,6 +7260,8 @@ int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info,
12183 + else
12184 + btrfs_dev_stat_reset(dev, i);
12185 + }
12186 ++ btrfs_info(fs_info, "device stats zeroed by %s (%d)",
12187 ++ current->comm, task_pid_nr(current));
12188 + } else {
12189 + for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
12190 + if (stats->nr_items > i)
12191 +diff --git a/fs/ceph/mds_client.c b/fs/ceph/mds_client.c
12192 +index 09db6d08614d..a2e903203bf9 100644
12193 +--- a/fs/ceph/mds_client.c
12194 ++++ b/fs/ceph/mds_client.c
12195 +@@ -2343,8 +2343,7 @@ static void __do_request(struct ceph_mds_client *mdsc,
12196 + if (!(mdsc->fsc->mount_options->flags &
12197 + CEPH_MOUNT_OPT_MOUNTWAIT) &&
12198 + !ceph_mdsmap_is_cluster_available(mdsc->mdsmap)) {
12199 +- err = -ENOENT;
12200 +- pr_info("probably no mds server is up\n");
12201 ++ err = -EHOSTUNREACH;
12202 + goto finish;
12203 + }
12204 + }
12205 +diff --git a/fs/ceph/super.c b/fs/ceph/super.c
12206 +index 2bd0b1ed9708..c4314f449240 100644
12207 +--- a/fs/ceph/super.c
12208 ++++ b/fs/ceph/super.c
12209 +@@ -1106,6 +1106,11 @@ static struct dentry *ceph_mount(struct file_system_type *fs_type,
12210 + return res;
12211 +
12212 + out_splat:
12213 ++ if (!ceph_mdsmap_is_cluster_available(fsc->mdsc->mdsmap)) {
12214 ++ pr_info("No mds server is up or the cluster is laggy\n");
12215 ++ err = -EHOSTUNREACH;
12216 ++ }
12217 ++
12218 + ceph_mdsc_close_sessions(fsc->mdsc);
12219 + deactivate_locked_super(sb);
12220 + goto out_final;
12221 +diff --git a/fs/cifs/connect.c b/fs/cifs/connect.c
12222 +index 576cf71576da..6c62ce40608a 100644
12223 +--- a/fs/cifs/connect.c
12224 ++++ b/fs/cifs/connect.c
12225 +@@ -3342,8 +3342,10 @@ match_prepath(struct super_block *sb, struct cifs_mnt_data *mnt_data)
12226 + {
12227 + struct cifs_sb_info *old = CIFS_SB(sb);
12228 + struct cifs_sb_info *new = mnt_data->cifs_sb;
12229 +- bool old_set = old->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH;
12230 +- bool new_set = new->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH;
12231 ++ bool old_set = (old->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH) &&
12232 ++ old->prepath;
12233 ++ bool new_set = (new->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH) &&
12234 ++ new->prepath;
12235 +
12236 + if (old_set && new_set && !strcmp(new->prepath, old->prepath))
12237 + return 1;
12238 +diff --git a/fs/cifs/smb2pdu.c b/fs/cifs/smb2pdu.c
12239 +index 0d4e4d97e6cf..e2d2b749c8f3 100644
12240 +--- a/fs/cifs/smb2pdu.c
12241 ++++ b/fs/cifs/smb2pdu.c
12242 +@@ -3425,6 +3425,9 @@ smb2_writev_callback(struct mid_q_entry *mid)
12243 + wdata->cfile->fid.persistent_fid,
12244 + tcon->tid, tcon->ses->Suid, wdata->offset,
12245 + wdata->bytes, wdata->result);
12246 ++ if (wdata->result == -ENOSPC)
12247 ++ printk_once(KERN_WARNING "Out of space writing to %s\n",
12248 ++ tcon->treeName);
12249 + } else
12250 + trace_smb3_write_done(0 /* no xid */,
12251 + wdata->cfile->fid.persistent_fid,
12252 +diff --git a/fs/ext4/file.c b/fs/ext4/file.c
12253 +index f4a24a46245e..52d155b4e733 100644
12254 +--- a/fs/ext4/file.c
12255 ++++ b/fs/ext4/file.c
12256 +@@ -40,9 +40,10 @@ static ssize_t ext4_dax_read_iter(struct kiocb *iocb, struct iov_iter *to)
12257 + struct inode *inode = file_inode(iocb->ki_filp);
12258 + ssize_t ret;
12259 +
12260 +- if (!inode_trylock_shared(inode)) {
12261 +- if (iocb->ki_flags & IOCB_NOWAIT)
12262 ++ if (iocb->ki_flags & IOCB_NOWAIT) {
12263 ++ if (!inode_trylock_shared(inode))
12264 + return -EAGAIN;
12265 ++ } else {
12266 + inode_lock_shared(inode);
12267 + }
12268 + /*
12269 +@@ -190,9 +191,10 @@ ext4_dax_write_iter(struct kiocb *iocb, struct iov_iter *from)
12270 + struct inode *inode = file_inode(iocb->ki_filp);
12271 + ssize_t ret;
12272 +
12273 +- if (!inode_trylock(inode)) {
12274 +- if (iocb->ki_flags & IOCB_NOWAIT)
12275 ++ if (iocb->ki_flags & IOCB_NOWAIT) {
12276 ++ if (!inode_trylock(inode))
12277 + return -EAGAIN;
12278 ++ } else {
12279 + inode_lock(inode);
12280 + }
12281 + ret = ext4_write_checks(iocb, from);
12282 +diff --git a/fs/f2fs/namei.c b/fs/f2fs/namei.c
12283 +index 0ace2c2e3de9..4f0cc0c79d1e 100644
12284 +--- a/fs/f2fs/namei.c
12285 ++++ b/fs/f2fs/namei.c
12286 +@@ -769,6 +769,7 @@ static int __f2fs_tmpfile(struct inode *dir, struct dentry *dentry,
12287 +
12288 + if (whiteout) {
12289 + f2fs_i_links_write(inode, false);
12290 ++ inode->i_state |= I_LINKABLE;
12291 + *whiteout = inode;
12292 + } else {
12293 + d_tmpfile(dentry, inode);
12294 +@@ -835,6 +836,12 @@ static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
12295 + F2FS_I(old_dentry->d_inode)->i_projid)))
12296 + return -EXDEV;
12297 +
12298 ++ if (flags & RENAME_WHITEOUT) {
12299 ++ err = f2fs_create_whiteout(old_dir, &whiteout);
12300 ++ if (err)
12301 ++ return err;
12302 ++ }
12303 ++
12304 + err = dquot_initialize(old_dir);
12305 + if (err)
12306 + goto out;
12307 +@@ -865,17 +872,11 @@ static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
12308 + }
12309 + }
12310 +
12311 +- if (flags & RENAME_WHITEOUT) {
12312 +- err = f2fs_create_whiteout(old_dir, &whiteout);
12313 +- if (err)
12314 +- goto out_dir;
12315 +- }
12316 +-
12317 + if (new_inode) {
12318 +
12319 + err = -ENOTEMPTY;
12320 + if (old_dir_entry && !f2fs_empty_dir(new_inode))
12321 +- goto out_whiteout;
12322 ++ goto out_dir;
12323 +
12324 + err = -ENOENT;
12325 + new_entry = f2fs_find_entry(new_dir, &new_dentry->d_name,
12326 +@@ -883,7 +884,7 @@ static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
12327 + if (!new_entry) {
12328 + if (IS_ERR(new_page))
12329 + err = PTR_ERR(new_page);
12330 +- goto out_whiteout;
12331 ++ goto out_dir;
12332 + }
12333 +
12334 + f2fs_balance_fs(sbi, true);
12335 +@@ -915,7 +916,7 @@ static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
12336 + err = f2fs_add_link(new_dentry, old_inode);
12337 + if (err) {
12338 + f2fs_unlock_op(sbi);
12339 +- goto out_whiteout;
12340 ++ goto out_dir;
12341 + }
12342 +
12343 + if (old_dir_entry)
12344 +@@ -939,7 +940,7 @@ static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
12345 + if (IS_ERR(old_page))
12346 + err = PTR_ERR(old_page);
12347 + f2fs_unlock_op(sbi);
12348 +- goto out_whiteout;
12349 ++ goto out_dir;
12350 + }
12351 + }
12352 + }
12353 +@@ -958,7 +959,6 @@ static int f2fs_rename(struct inode *old_dir, struct dentry *old_dentry,
12354 + f2fs_delete_entry(old_entry, old_page, old_dir, NULL);
12355 +
12356 + if (whiteout) {
12357 +- whiteout->i_state |= I_LINKABLE;
12358 + set_inode_flag(whiteout, FI_INC_LINK);
12359 + err = f2fs_add_link(old_dentry, whiteout);
12360 + if (err)
12361 +@@ -992,15 +992,14 @@ put_out_dir:
12362 + f2fs_unlock_op(sbi);
12363 + if (new_page)
12364 + f2fs_put_page(new_page, 0);
12365 +-out_whiteout:
12366 +- if (whiteout)
12367 +- iput(whiteout);
12368 + out_dir:
12369 + if (old_dir_entry)
12370 + f2fs_put_page(old_dir_page, 0);
12371 + out_old:
12372 + f2fs_put_page(old_page, 0);
12373 + out:
12374 ++ if (whiteout)
12375 ++ iput(whiteout);
12376 + return err;
12377 + }
12378 +
12379 +diff --git a/fs/f2fs/sysfs.c b/fs/f2fs/sysfs.c
12380 +index 98887187af4c..9a59f49ba405 100644
12381 +--- a/fs/f2fs/sysfs.c
12382 ++++ b/fs/f2fs/sysfs.c
12383 +@@ -658,10 +658,12 @@ int __init f2fs_init_sysfs(void)
12384 +
12385 + ret = kobject_init_and_add(&f2fs_feat, &f2fs_feat_ktype,
12386 + NULL, "features");
12387 +- if (ret)
12388 ++ if (ret) {
12389 ++ kobject_put(&f2fs_feat);
12390 + kset_unregister(&f2fs_kset);
12391 +- else
12392 ++ } else {
12393 + f2fs_proc_root = proc_mkdir("fs/f2fs", NULL);
12394 ++ }
12395 + return ret;
12396 + }
12397 +
12398 +@@ -682,8 +684,11 @@ int f2fs_register_sysfs(struct f2fs_sb_info *sbi)
12399 + init_completion(&sbi->s_kobj_unregister);
12400 + err = kobject_init_and_add(&sbi->s_kobj, &f2fs_sb_ktype, NULL,
12401 + "%s", sb->s_id);
12402 +- if (err)
12403 ++ if (err) {
12404 ++ kobject_put(&sbi->s_kobj);
12405 ++ wait_for_completion(&sbi->s_kobj_unregister);
12406 + return err;
12407 ++ }
12408 +
12409 + if (f2fs_proc_root)
12410 + sbi->s_proc = proc_mkdir(sb->s_id, f2fs_proc_root);
12411 +@@ -711,4 +716,5 @@ void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi)
12412 + remove_proc_entry(sbi->sb->s_id, f2fs_proc_root);
12413 + }
12414 + kobject_del(&sbi->s_kobj);
12415 ++ kobject_put(&sbi->s_kobj);
12416 + }
12417 +diff --git a/fs/jbd2/checkpoint.c b/fs/jbd2/checkpoint.c
12418 +index 26f8d7e46462..66409cbd3ed5 100644
12419 +--- a/fs/jbd2/checkpoint.c
12420 ++++ b/fs/jbd2/checkpoint.c
12421 +@@ -165,7 +165,7 @@ void __jbd2_log_wait_for_space(journal_t *journal)
12422 + "journal space in %s\n", __func__,
12423 + journal->j_devname);
12424 + WARN_ON(1);
12425 +- jbd2_journal_abort(journal, 0);
12426 ++ jbd2_journal_abort(journal, -EIO);
12427 + }
12428 + write_lock(&journal->j_state_lock);
12429 + } else {
12430 +diff --git a/fs/jbd2/commit.c b/fs/jbd2/commit.c
12431 +index c321fa06081c..4200a6fe9599 100644
12432 +--- a/fs/jbd2/commit.c
12433 ++++ b/fs/jbd2/commit.c
12434 +@@ -781,7 +781,7 @@ start_journal_io:
12435 + err = journal_submit_commit_record(journal, commit_transaction,
12436 + &cbh, crc32_sum);
12437 + if (err)
12438 +- __jbd2_journal_abort_hard(journal);
12439 ++ jbd2_journal_abort(journal, err);
12440 + }
12441 +
12442 + blk_finish_plug(&plug);
12443 +@@ -874,7 +874,7 @@ start_journal_io:
12444 + err = journal_submit_commit_record(journal, commit_transaction,
12445 + &cbh, crc32_sum);
12446 + if (err)
12447 +- __jbd2_journal_abort_hard(journal);
12448 ++ jbd2_journal_abort(journal, err);
12449 + }
12450 + if (cbh)
12451 + err = journal_wait_on_commit_record(journal, cbh);
12452 +diff --git a/fs/jbd2/journal.c b/fs/jbd2/journal.c
12453 +index 1a2339f2cb49..a15a22d20909 100644
12454 +--- a/fs/jbd2/journal.c
12455 ++++ b/fs/jbd2/journal.c
12456 +@@ -1701,6 +1701,11 @@ int jbd2_journal_load(journal_t *journal)
12457 + journal->j_devname);
12458 + return -EFSCORRUPTED;
12459 + }
12460 ++ /*
12461 ++ * clear JBD2_ABORT flag initialized in journal_init_common
12462 ++ * here to update log tail information with the newest seq.
12463 ++ */
12464 ++ journal->j_flags &= ~JBD2_ABORT;
12465 +
12466 + /* OK, we've finished with the dynamic journal bits:
12467 + * reinitialise the dynamic contents of the superblock in memory
12468 +@@ -1708,7 +1713,6 @@ int jbd2_journal_load(journal_t *journal)
12469 + if (journal_reset(journal))
12470 + goto recovery_error;
12471 +
12472 +- journal->j_flags &= ~JBD2_ABORT;
12473 + journal->j_flags |= JBD2_LOADED;
12474 + return 0;
12475 +
12476 +@@ -2129,8 +2133,7 @@ static void __journal_abort_soft (journal_t *journal, int errno)
12477 +
12478 + if (journal->j_flags & JBD2_ABORT) {
12479 + write_unlock(&journal->j_state_lock);
12480 +- if (!old_errno && old_errno != -ESHUTDOWN &&
12481 +- errno == -ESHUTDOWN)
12482 ++ if (old_errno != -ESHUTDOWN && errno == -ESHUTDOWN)
12483 + jbd2_journal_update_sb_errno(journal);
12484 + return;
12485 + }
12486 +@@ -2138,12 +2141,10 @@ static void __journal_abort_soft (journal_t *journal, int errno)
12487 +
12488 + __jbd2_journal_abort_hard(journal);
12489 +
12490 +- if (errno) {
12491 +- jbd2_journal_update_sb_errno(journal);
12492 +- write_lock(&journal->j_state_lock);
12493 +- journal->j_flags |= JBD2_REC_ERR;
12494 +- write_unlock(&journal->j_state_lock);
12495 +- }
12496 ++ jbd2_journal_update_sb_errno(journal);
12497 ++ write_lock(&journal->j_state_lock);
12498 ++ journal->j_flags |= JBD2_REC_ERR;
12499 ++ write_unlock(&journal->j_state_lock);
12500 + }
12501 +
12502 + /**
12503 +@@ -2185,11 +2186,6 @@ static void __journal_abort_soft (journal_t *journal, int errno)
12504 + * failure to disk. ext3_error, for example, now uses this
12505 + * functionality.
12506 + *
12507 +- * Errors which originate from within the journaling layer will NOT
12508 +- * supply an errno; a null errno implies that absolutely no further
12509 +- * writes are done to the journal (unless there are any already in
12510 +- * progress).
12511 +- *
12512 + */
12513 +
12514 + void jbd2_journal_abort(journal_t *journal, int errno)
12515 +diff --git a/fs/nfs/nfs42proc.c b/fs/nfs/nfs42proc.c
12516 +index 94f98e190e63..526441de89c1 100644
12517 +--- a/fs/nfs/nfs42proc.c
12518 ++++ b/fs/nfs/nfs42proc.c
12519 +@@ -283,14 +283,14 @@ static ssize_t _nfs42_proc_copy(struct file *src,
12520 + status = handle_async_copy(res, server, src, dst,
12521 + &args->src_stateid);
12522 + if (status)
12523 +- return status;
12524 ++ goto out;
12525 + }
12526 +
12527 + if ((!res->synchronous || !args->sync) &&
12528 + res->write_res.verifier.committed != NFS_FILE_SYNC) {
12529 + status = process_copy_commit(dst, pos_dst, res);
12530 + if (status)
12531 +- return status;
12532 ++ goto out;
12533 + }
12534 +
12535 + truncate_pagecache_range(dst_inode, pos_dst,
12536 +diff --git a/fs/ocfs2/journal.h b/fs/ocfs2/journal.h
12537 +index 497a4171ef61..bfb50fc51528 100644
12538 +--- a/fs/ocfs2/journal.h
12539 ++++ b/fs/ocfs2/journal.h
12540 +@@ -637,9 +637,11 @@ static inline void ocfs2_update_inode_fsync_trans(handle_t *handle,
12541 + {
12542 + struct ocfs2_inode_info *oi = OCFS2_I(inode);
12543 +
12544 +- oi->i_sync_tid = handle->h_transaction->t_tid;
12545 +- if (datasync)
12546 +- oi->i_datasync_tid = handle->h_transaction->t_tid;
12547 ++ if (!is_handle_aborted(handle)) {
12548 ++ oi->i_sync_tid = handle->h_transaction->t_tid;
12549 ++ if (datasync)
12550 ++ oi->i_datasync_tid = handle->h_transaction->t_tid;
12551 ++ }
12552 + }
12553 +
12554 + #endif /* OCFS2_JOURNAL_H */
12555 +diff --git a/fs/orangefs/orangefs-debugfs.c b/fs/orangefs/orangefs-debugfs.c
12556 +index 0732cb08173e..e24738c691f6 100644
12557 +--- a/fs/orangefs/orangefs-debugfs.c
12558 ++++ b/fs/orangefs/orangefs-debugfs.c
12559 +@@ -305,6 +305,7 @@ static void *help_start(struct seq_file *m, loff_t *pos)
12560 +
12561 + static void *help_next(struct seq_file *m, void *v, loff_t *pos)
12562 + {
12563 ++ (*pos)++;
12564 + gossip_debug(GOSSIP_DEBUGFS_DEBUG, "help_next: start\n");
12565 +
12566 + return NULL;
12567 +diff --git a/fs/reiserfs/stree.c b/fs/reiserfs/stree.c
12568 +index 0037aea97d39..2946713cb00d 100644
12569 +--- a/fs/reiserfs/stree.c
12570 ++++ b/fs/reiserfs/stree.c
12571 +@@ -2250,7 +2250,8 @@ error_out:
12572 + /* also releases the path */
12573 + unfix_nodes(&s_ins_balance);
12574 + #ifdef REISERQUOTA_DEBUG
12575 +- reiserfs_debug(th->t_super, REISERFS_DEBUG_CODE,
12576 ++ if (inode)
12577 ++ reiserfs_debug(th->t_super, REISERFS_DEBUG_CODE,
12578 + "reiserquota insert_item(): freeing %u id=%u type=%c",
12579 + quota_bytes, inode->i_uid, head2type(ih));
12580 + #endif
12581 +diff --git a/fs/reiserfs/super.c b/fs/reiserfs/super.c
12582 +index 6280efeceb0a..de5eda33c92a 100644
12583 +--- a/fs/reiserfs/super.c
12584 ++++ b/fs/reiserfs/super.c
12585 +@@ -1954,7 +1954,7 @@ static int reiserfs_fill_super(struct super_block *s, void *data, int silent)
12586 + if (!sbi->s_jdev) {
12587 + SWARN(silent, s, "", "Cannot allocate memory for "
12588 + "journal device name");
12589 +- goto error;
12590 ++ goto error_unlocked;
12591 + }
12592 + }
12593 + #ifdef CONFIG_QUOTA
12594 +diff --git a/fs/udf/super.c b/fs/udf/super.c
12595 +index 6fd0f14e9dd2..1676a175cd7a 100644
12596 +--- a/fs/udf/super.c
12597 ++++ b/fs/udf/super.c
12598 +@@ -2469,17 +2469,29 @@ static unsigned int udf_count_free_table(struct super_block *sb,
12599 + static unsigned int udf_count_free(struct super_block *sb)
12600 + {
12601 + unsigned int accum = 0;
12602 +- struct udf_sb_info *sbi;
12603 ++ struct udf_sb_info *sbi = UDF_SB(sb);
12604 + struct udf_part_map *map;
12605 ++ unsigned int part = sbi->s_partition;
12606 ++ int ptype = sbi->s_partmaps[part].s_partition_type;
12607 ++
12608 ++ if (ptype == UDF_METADATA_MAP25) {
12609 ++ part = sbi->s_partmaps[part].s_type_specific.s_metadata.
12610 ++ s_phys_partition_ref;
12611 ++ } else if (ptype == UDF_VIRTUAL_MAP15 || ptype == UDF_VIRTUAL_MAP20) {
12612 ++ /*
12613 ++ * Filesystems with VAT are append-only and we cannot write to
12614 ++ * them. Let's just report 0 here.
12615 ++ */
12616 ++ return 0;
12617 ++ }
12618 +
12619 +- sbi = UDF_SB(sb);
12620 + if (sbi->s_lvid_bh) {
12621 + struct logicalVolIntegrityDesc *lvid =
12622 + (struct logicalVolIntegrityDesc *)
12623 + sbi->s_lvid_bh->b_data;
12624 +- if (le32_to_cpu(lvid->numOfPartitions) > sbi->s_partition) {
12625 ++ if (le32_to_cpu(lvid->numOfPartitions) > part) {
12626 + accum = le32_to_cpu(
12627 +- lvid->freeSpaceTable[sbi->s_partition]);
12628 ++ lvid->freeSpaceTable[part]);
12629 + if (accum == 0xFFFFFFFF)
12630 + accum = 0;
12631 + }
12632 +@@ -2488,7 +2500,7 @@ static unsigned int udf_count_free(struct super_block *sb)
12633 + if (accum)
12634 + return accum;
12635 +
12636 +- map = &sbi->s_partmaps[sbi->s_partition];
12637 ++ map = &sbi->s_partmaps[part];
12638 + if (map->s_partition_flags & UDF_PART_FLAG_UNALLOC_BITMAP) {
12639 + accum += udf_count_free_bitmap(sb,
12640 + map->s_uspace.s_bitmap);
12641 +diff --git a/include/linux/dmaengine.h b/include/linux/dmaengine.h
12642 +index 0647f436f88c..50128c36f0b4 100644
12643 +--- a/include/linux/dmaengine.h
12644 ++++ b/include/linux/dmaengine.h
12645 +@@ -686,6 +686,7 @@ struct dma_filter {
12646 + * @fill_align: alignment shift for memset operations
12647 + * @dev_id: unique device ID
12648 + * @dev: struct device reference for dma mapping api
12649 ++ * @owner: owner module (automatically set based on the provided dev)
12650 + * @src_addr_widths: bit mask of src addr widths the device supports
12651 + * Width is specified in bytes, e.g. for a device supporting
12652 + * a width of 4 the mask should have BIT(4) set.
12653 +@@ -749,6 +750,7 @@ struct dma_device {
12654 +
12655 + int dev_id;
12656 + struct device *dev;
12657 ++ struct module *owner;
12658 +
12659 + u32 src_addr_widths;
12660 + u32 dst_addr_widths;
12661 +diff --git a/include/linux/list_nulls.h b/include/linux/list_nulls.h
12662 +index 3ef96743db8d..1ecd35664e0d 100644
12663 +--- a/include/linux/list_nulls.h
12664 ++++ b/include/linux/list_nulls.h
12665 +@@ -72,10 +72,10 @@ static inline void hlist_nulls_add_head(struct hlist_nulls_node *n,
12666 + struct hlist_nulls_node *first = h->first;
12667 +
12668 + n->next = first;
12669 +- n->pprev = &h->first;
12670 ++ WRITE_ONCE(n->pprev, &h->first);
12671 + h->first = n;
12672 + if (!is_a_nulls(first))
12673 +- first->pprev = &n->next;
12674 ++ WRITE_ONCE(first->pprev, &n->next);
12675 + }
12676 +
12677 + static inline void __hlist_nulls_del(struct hlist_nulls_node *n)
12678 +@@ -85,13 +85,13 @@ static inline void __hlist_nulls_del(struct hlist_nulls_node *n)
12679 +
12680 + WRITE_ONCE(*pprev, next);
12681 + if (!is_a_nulls(next))
12682 +- next->pprev = pprev;
12683 ++ WRITE_ONCE(next->pprev, pprev);
12684 + }
12685 +
12686 + static inline void hlist_nulls_del(struct hlist_nulls_node *n)
12687 + {
12688 + __hlist_nulls_del(n);
12689 +- n->pprev = LIST_POISON2;
12690 ++ WRITE_ONCE(n->pprev, LIST_POISON2);
12691 + }
12692 +
12693 + /**
12694 +diff --git a/include/linux/rculist_nulls.h b/include/linux/rculist_nulls.h
12695 +index 61974c4c566b..90f2e2232c6d 100644
12696 +--- a/include/linux/rculist_nulls.h
12697 ++++ b/include/linux/rculist_nulls.h
12698 +@@ -34,7 +34,7 @@ static inline void hlist_nulls_del_init_rcu(struct hlist_nulls_node *n)
12699 + {
12700 + if (!hlist_nulls_unhashed(n)) {
12701 + __hlist_nulls_del(n);
12702 +- n->pprev = NULL;
12703 ++ WRITE_ONCE(n->pprev, NULL);
12704 + }
12705 + }
12706 +
12707 +@@ -66,7 +66,7 @@ static inline void hlist_nulls_del_init_rcu(struct hlist_nulls_node *n)
12708 + static inline void hlist_nulls_del_rcu(struct hlist_nulls_node *n)
12709 + {
12710 + __hlist_nulls_del(n);
12711 +- n->pprev = LIST_POISON2;
12712 ++ WRITE_ONCE(n->pprev, LIST_POISON2);
12713 + }
12714 +
12715 + /**
12716 +@@ -94,10 +94,10 @@ static inline void hlist_nulls_add_head_rcu(struct hlist_nulls_node *n,
12717 + struct hlist_nulls_node *first = h->first;
12718 +
12719 + n->next = first;
12720 +- n->pprev = &h->first;
12721 ++ WRITE_ONCE(n->pprev, &h->first);
12722 + rcu_assign_pointer(hlist_nulls_first_rcu(h), n);
12723 + if (!is_a_nulls(first))
12724 +- first->pprev = &n->next;
12725 ++ WRITE_ONCE(first->pprev, &n->next);
12726 + }
12727 +
12728 + /**
12729 +diff --git a/include/media/v4l2-device.h b/include/media/v4l2-device.h
12730 +index b330e4a08a6b..40840fec337c 100644
12731 +--- a/include/media/v4l2-device.h
12732 ++++ b/include/media/v4l2-device.h
12733 +@@ -372,7 +372,7 @@ static inline void v4l2_subdev_notify(struct v4l2_subdev *sd,
12734 + struct v4l2_subdev *__sd; \
12735 + \
12736 + __v4l2_device_call_subdevs_p(v4l2_dev, __sd, \
12737 +- !(grpid) || __sd->grp_id == (grpid), o, f , \
12738 ++ (grpid) == 0 || __sd->grp_id == (grpid), o, f , \
12739 + ##args); \
12740 + } while (0)
12741 +
12742 +@@ -404,7 +404,7 @@ static inline void v4l2_subdev_notify(struct v4l2_subdev *sd,
12743 + ({ \
12744 + struct v4l2_subdev *__sd; \
12745 + __v4l2_device_call_subdevs_until_err_p(v4l2_dev, __sd, \
12746 +- !(grpid) || __sd->grp_id == (grpid), o, f , \
12747 ++ (grpid) == 0 || __sd->grp_id == (grpid), o, f , \
12748 + ##args); \
12749 + })
12750 +
12751 +@@ -432,8 +432,8 @@ static inline void v4l2_subdev_notify(struct v4l2_subdev *sd,
12752 + struct v4l2_subdev *__sd; \
12753 + \
12754 + __v4l2_device_call_subdevs_p(v4l2_dev, __sd, \
12755 +- !(grpmsk) || (__sd->grp_id & (grpmsk)), o, f , \
12756 +- ##args); \
12757 ++ (grpmsk) == 0 || (__sd->grp_id & (grpmsk)), o, \
12758 ++ f , ##args); \
12759 + } while (0)
12760 +
12761 + /**
12762 +@@ -463,8 +463,8 @@ static inline void v4l2_subdev_notify(struct v4l2_subdev *sd,
12763 + ({ \
12764 + struct v4l2_subdev *__sd; \
12765 + __v4l2_device_call_subdevs_until_err_p(v4l2_dev, __sd, \
12766 +- !(grpmsk) || (__sd->grp_id & (grpmsk)), o, f , \
12767 +- ##args); \
12768 ++ (grpmsk) == 0 || (__sd->grp_id & (grpmsk)), o, \
12769 ++ f , ##args); \
12770 + })
12771 +
12772 +
12773 +diff --git a/kernel/bpf/inode.c b/kernel/bpf/inode.c
12774 +index dc9d7ac8228d..c04815bb15cc 100644
12775 +--- a/kernel/bpf/inode.c
12776 ++++ b/kernel/bpf/inode.c
12777 +@@ -198,6 +198,7 @@ static void *map_seq_next(struct seq_file *m, void *v, loff_t *pos)
12778 + void *key = map_iter(m)->key;
12779 + void *prev_key;
12780 +
12781 ++ (*pos)++;
12782 + if (map_iter(m)->done)
12783 + return NULL;
12784 +
12785 +@@ -210,8 +211,6 @@ static void *map_seq_next(struct seq_file *m, void *v, loff_t *pos)
12786 + map_iter(m)->done = true;
12787 + return NULL;
12788 + }
12789 +-
12790 +- ++(*pos);
12791 + return key;
12792 + }
12793 +
12794 +diff --git a/kernel/cpu.c b/kernel/cpu.c
12795 +index 8d6b8b5493f9..2d850eaaf82e 100644
12796 +--- a/kernel/cpu.c
12797 ++++ b/kernel/cpu.c
12798 +@@ -493,8 +493,7 @@ static int bringup_wait_for_ap(unsigned int cpu)
12799 + if (WARN_ON_ONCE((!cpu_online(cpu))))
12800 + return -ECANCELED;
12801 +
12802 +- /* Unpark the stopper thread and the hotplug thread of the target cpu */
12803 +- stop_machine_unpark(cpu);
12804 ++ /* Unpark the hotplug thread of the target cpu */
12805 + kthread_unpark(st->thread);
12806 +
12807 + /*
12808 +@@ -1048,8 +1047,8 @@ void notify_cpu_starting(unsigned int cpu)
12809 +
12810 + /*
12811 + * Called from the idle task. Wake up the controlling task which brings the
12812 +- * stopper and the hotplug thread of the upcoming CPU up and then delegates
12813 +- * the rest of the online bringup to the hotplug thread.
12814 ++ * hotplug thread of the upcoming CPU up and then delegates the rest of the
12815 ++ * online bringup to the hotplug thread.
12816 + */
12817 + void cpuhp_online_idle(enum cpuhp_state state)
12818 + {
12819 +@@ -1059,6 +1058,12 @@ void cpuhp_online_idle(enum cpuhp_state state)
12820 + if (state != CPUHP_AP_ONLINE_IDLE)
12821 + return;
12822 +
12823 ++ /*
12824 ++ * Unpart the stopper thread before we start the idle loop (and start
12825 ++ * scheduling); this ensures the stopper task is always available.
12826 ++ */
12827 ++ stop_machine_unpark(smp_processor_id());
12828 ++
12829 + st->state = CPUHP_AP_ONLINE_IDLE;
12830 + complete_ap_thread(st, true);
12831 + }
12832 +diff --git a/kernel/module.c b/kernel/module.c
12833 +index 70a75a7216ab..20fc0efc679c 100644
12834 +--- a/kernel/module.c
12835 ++++ b/kernel/module.c
12836 +@@ -2980,9 +2980,7 @@ static int setup_load_info(struct load_info *info, int flags)
12837 +
12838 + /* Try to find a name early so we can log errors with a module name */
12839 + info->index.info = find_sec(info, ".modinfo");
12840 +- if (!info->index.info)
12841 +- info->name = "(missing .modinfo section)";
12842 +- else
12843 ++ if (info->index.info)
12844 + info->name = get_modinfo(info, "name");
12845 +
12846 + /* Find internal symbols and strings. */
12847 +@@ -2997,14 +2995,15 @@ static int setup_load_info(struct load_info *info, int flags)
12848 + }
12849 +
12850 + if (info->index.sym == 0) {
12851 +- pr_warn("%s: module has no symbols (stripped?)\n", info->name);
12852 ++ pr_warn("%s: module has no symbols (stripped?)\n",
12853 ++ info->name ?: "(missing .modinfo section or name field)");
12854 + return -ENOEXEC;
12855 + }
12856 +
12857 + info->index.mod = find_sec(info, ".gnu.linkonce.this_module");
12858 + if (!info->index.mod) {
12859 + pr_warn("%s: No module found in object\n",
12860 +- info->name ?: "(missing .modinfo name field)");
12861 ++ info->name ?: "(missing .modinfo section or name field)");
12862 + return -ENOEXEC;
12863 + }
12864 + /* This is temporary: point mod into copy of data. */
12865 +diff --git a/kernel/trace/ftrace.c b/kernel/trace/ftrace.c
12866 +index 53795237e975..0c379cd40bea 100644
12867 +--- a/kernel/trace/ftrace.c
12868 ++++ b/kernel/trace/ftrace.c
12869 +@@ -6525,9 +6525,10 @@ static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
12870 + struct trace_array *tr = m->private;
12871 + struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_pids);
12872 +
12873 +- if (v == FTRACE_NO_PIDS)
12874 ++ if (v == FTRACE_NO_PIDS) {
12875 ++ (*pos)++;
12876 + return NULL;
12877 +-
12878 ++ }
12879 + return trace_pid_next(pid_list, v, pos);
12880 + }
12881 +
12882 +diff --git a/kernel/trace/trace_events_trigger.c b/kernel/trace/trace_events_trigger.c
12883 +index b05d1b6a6291..9300e8bbf08a 100644
12884 +--- a/kernel/trace/trace_events_trigger.c
12885 ++++ b/kernel/trace/trace_events_trigger.c
12886 +@@ -115,9 +115,10 @@ static void *trigger_next(struct seq_file *m, void *t, loff_t *pos)
12887 + {
12888 + struct trace_event_file *event_file = event_file_data(m->private);
12889 +
12890 +- if (t == SHOW_AVAILABLE_TRIGGERS)
12891 ++ if (t == SHOW_AVAILABLE_TRIGGERS) {
12892 ++ (*pos)++;
12893 + return NULL;
12894 +-
12895 ++ }
12896 + return seq_list_next(t, &event_file->triggers, pos);
12897 + }
12898 +
12899 +diff --git a/kernel/trace/trace_stat.c b/kernel/trace/trace_stat.c
12900 +index 75bf1bcb4a8a..92b76f9e25ed 100644
12901 +--- a/kernel/trace/trace_stat.c
12902 ++++ b/kernel/trace/trace_stat.c
12903 +@@ -278,18 +278,22 @@ static int tracing_stat_init(void)
12904 +
12905 + d_tracing = tracing_init_dentry();
12906 + if (IS_ERR(d_tracing))
12907 +- return 0;
12908 ++ return -ENODEV;
12909 +
12910 + stat_dir = tracefs_create_dir("trace_stat", d_tracing);
12911 +- if (!stat_dir)
12912 ++ if (!stat_dir) {
12913 + pr_warn("Could not create tracefs 'trace_stat' entry\n");
12914 ++ return -ENOMEM;
12915 ++ }
12916 + return 0;
12917 + }
12918 +
12919 + static int init_stat_file(struct stat_session *session)
12920 + {
12921 +- if (!stat_dir && tracing_stat_init())
12922 +- return -ENODEV;
12923 ++ int ret;
12924 ++
12925 ++ if (!stat_dir && (ret = tracing_stat_init()))
12926 ++ return ret;
12927 +
12928 + session->file = tracefs_create_file(session->ts->name, 0644,
12929 + stat_dir,
12930 +@@ -302,7 +306,7 @@ static int init_stat_file(struct stat_session *session)
12931 + int register_stat_tracer(struct tracer_stat *trace)
12932 + {
12933 + struct stat_session *session, *node;
12934 +- int ret;
12935 ++ int ret = -EINVAL;
12936 +
12937 + if (!trace)
12938 + return -EINVAL;
12939 +@@ -313,17 +317,15 @@ int register_stat_tracer(struct tracer_stat *trace)
12940 + /* Already registered? */
12941 + mutex_lock(&all_stat_sessions_mutex);
12942 + list_for_each_entry(node, &all_stat_sessions, session_list) {
12943 +- if (node->ts == trace) {
12944 +- mutex_unlock(&all_stat_sessions_mutex);
12945 +- return -EINVAL;
12946 +- }
12947 ++ if (node->ts == trace)
12948 ++ goto out;
12949 + }
12950 +- mutex_unlock(&all_stat_sessions_mutex);
12951 +
12952 ++ ret = -ENOMEM;
12953 + /* Init the session */
12954 + session = kzalloc(sizeof(*session), GFP_KERNEL);
12955 + if (!session)
12956 +- return -ENOMEM;
12957 ++ goto out;
12958 +
12959 + session->ts = trace;
12960 + INIT_LIST_HEAD(&session->session_list);
12961 +@@ -332,15 +334,16 @@ int register_stat_tracer(struct tracer_stat *trace)
12962 + ret = init_stat_file(session);
12963 + if (ret) {
12964 + destroy_session(session);
12965 +- return ret;
12966 ++ goto out;
12967 + }
12968 +
12969 ++ ret = 0;
12970 + /* Register */
12971 +- mutex_lock(&all_stat_sessions_mutex);
12972 + list_add_tail(&session->session_list, &all_stat_sessions);
12973 ++ out:
12974 + mutex_unlock(&all_stat_sessions_mutex);
12975 +
12976 +- return 0;
12977 ++ return ret;
12978 + }
12979 +
12980 + void unregister_stat_tracer(struct tracer_stat *trace)
12981 +diff --git a/kernel/watchdog.c b/kernel/watchdog.c
12982 +index bbc4940f21af..6d60701dc636 100644
12983 +--- a/kernel/watchdog.c
12984 ++++ b/kernel/watchdog.c
12985 +@@ -161,6 +161,8 @@ static void lockup_detector_update_enable(void)
12986 +
12987 + #ifdef CONFIG_SOFTLOCKUP_DETECTOR
12988 +
12989 ++#define SOFTLOCKUP_RESET ULONG_MAX
12990 ++
12991 + /* Global variables, exported for sysctl */
12992 + unsigned int __read_mostly softlockup_panic =
12993 + CONFIG_BOOTPARAM_SOFTLOCKUP_PANIC_VALUE;
12994 +@@ -267,7 +269,7 @@ notrace void touch_softlockup_watchdog_sched(void)
12995 + * Preemption can be enabled. It doesn't matter which CPU's timestamp
12996 + * gets zeroed here, so use the raw_ operation.
12997 + */
12998 +- raw_cpu_write(watchdog_touch_ts, 0);
12999 ++ raw_cpu_write(watchdog_touch_ts, SOFTLOCKUP_RESET);
13000 + }
13001 +
13002 + notrace void touch_softlockup_watchdog(void)
13003 +@@ -291,14 +293,14 @@ void touch_all_softlockup_watchdogs(void)
13004 + * the softlockup check.
13005 + */
13006 + for_each_cpu(cpu, &watchdog_allowed_mask)
13007 +- per_cpu(watchdog_touch_ts, cpu) = 0;
13008 ++ per_cpu(watchdog_touch_ts, cpu) = SOFTLOCKUP_RESET;
13009 + wq_watchdog_touch(-1);
13010 + }
13011 +
13012 + void touch_softlockup_watchdog_sync(void)
13013 + {
13014 + __this_cpu_write(softlockup_touch_sync, true);
13015 +- __this_cpu_write(watchdog_touch_ts, 0);
13016 ++ __this_cpu_write(watchdog_touch_ts, SOFTLOCKUP_RESET);
13017 + }
13018 +
13019 + static int is_softlockup(unsigned long touch_ts)
13020 +@@ -376,7 +378,7 @@ static enum hrtimer_restart watchdog_timer_fn(struct hrtimer *hrtimer)
13021 + /* .. and repeat */
13022 + hrtimer_forward_now(hrtimer, ns_to_ktime(sample_period));
13023 +
13024 +- if (touch_ts == 0) {
13025 ++ if (touch_ts == SOFTLOCKUP_RESET) {
13026 + if (unlikely(__this_cpu_read(softlockup_touch_sync))) {
13027 + /*
13028 + * If the time stamp was touched atomically
13029 +diff --git a/lib/scatterlist.c b/lib/scatterlist.c
13030 +index 8c3036c37ba0..60e7eca2f4be 100644
13031 +--- a/lib/scatterlist.c
13032 ++++ b/lib/scatterlist.c
13033 +@@ -305,7 +305,7 @@ int __sg_alloc_table(struct sg_table *table, unsigned int nents,
13034 + if (prv)
13035 + table->nents = ++table->orig_nents;
13036 +
13037 +- return -ENOMEM;
13038 ++ return -ENOMEM;
13039 + }
13040 +
13041 + sg_init_table(sg, alloc_size);
13042 +diff --git a/net/core/dev.c b/net/core/dev.c
13043 +index 1c0224e8fc78..c1a3baf16957 100644
13044 +--- a/net/core/dev.c
13045 ++++ b/net/core/dev.c
13046 +@@ -4306,14 +4306,14 @@ static u32 netif_receive_generic_xdp(struct sk_buff *skb,
13047 + /* Reinjected packets coming from act_mirred or similar should
13048 + * not get XDP generic processing.
13049 + */
13050 +- if (skb_cloned(skb) || skb_is_tc_redirected(skb))
13051 ++ if (skb_is_tc_redirected(skb))
13052 + return XDP_PASS;
13053 +
13054 + /* XDP packets must be linear and must have sufficient headroom
13055 + * of XDP_PACKET_HEADROOM bytes. This is the guarantee that also
13056 + * native XDP provides, thus we need to do it here as well.
13057 + */
13058 +- if (skb_is_nonlinear(skb) ||
13059 ++ if (skb_cloned(skb) || skb_is_nonlinear(skb) ||
13060 + skb_headroom(skb) < XDP_PACKET_HEADROOM) {
13061 + int hroom = XDP_PACKET_HEADROOM - skb_headroom(skb);
13062 + int troom = skb->tail + skb->data_len - skb->end;
13063 +diff --git a/net/core/filter.c b/net/core/filter.c
13064 +index 9daf1a4118b5..40b3af05c883 100644
13065 +--- a/net/core/filter.c
13066 ++++ b/net/core/filter.c
13067 +@@ -3207,7 +3207,7 @@ static int __bpf_tx_xdp_map(struct net_device *dev_rx, void *fwd,
13068 + return err;
13069 + }
13070 + default:
13071 +- break;
13072 ++ return -EBADRQC;
13073 + }
13074 + return 0;
13075 + }
13076 +diff --git a/net/dsa/tag_qca.c b/net/dsa/tag_qca.c
13077 +index 66a952118dfd..9c0dd31d4445 100644
13078 +--- a/net/dsa/tag_qca.c
13079 ++++ b/net/dsa/tag_qca.c
13080 +@@ -41,7 +41,7 @@ static struct sk_buff *qca_tag_xmit(struct sk_buff *skb, struct net_device *dev)
13081 + struct dsa_port *dp = dsa_slave_to_port(dev);
13082 + u16 *phdr, hdr;
13083 +
13084 +- if (skb_cow_head(skb, 0) < 0)
13085 ++ if (skb_cow_head(skb, QCA_HDR_LEN) < 0)
13086 + return NULL;
13087 +
13088 + skb_push(skb, QCA_HDR_LEN);
13089 +diff --git a/net/netfilter/nft_tunnel.c b/net/netfilter/nft_tunnel.c
13090 +index e5444f3ff43f..5e66042ac346 100644
13091 +--- a/net/netfilter/nft_tunnel.c
13092 ++++ b/net/netfilter/nft_tunnel.c
13093 +@@ -218,8 +218,9 @@ static int nft_tunnel_obj_vxlan_init(const struct nlattr *attr,
13094 + }
13095 +
13096 + static const struct nla_policy nft_tunnel_opts_erspan_policy[NFTA_TUNNEL_KEY_ERSPAN_MAX + 1] = {
13097 ++ [NFTA_TUNNEL_KEY_ERSPAN_VERSION] = { .type = NLA_U32 },
13098 + [NFTA_TUNNEL_KEY_ERSPAN_V1_INDEX] = { .type = NLA_U32 },
13099 +- [NFTA_TUNNEL_KEY_ERSPAN_V2_DIR] = { .type = NLA_U8 },
13100 ++ [NFTA_TUNNEL_KEY_ERSPAN_V2_DIR] = { .type = NLA_U8 },
13101 + [NFTA_TUNNEL_KEY_ERSPAN_V2_HWID] = { .type = NLA_U8 },
13102 + };
13103 +
13104 +diff --git a/net/sched/cls_flower.c b/net/sched/cls_flower.c
13105 +index 22415311f324..c006d3b89ba3 100644
13106 +--- a/net/sched/cls_flower.c
13107 ++++ b/net/sched/cls_flower.c
13108 +@@ -486,6 +486,7 @@ static const struct nla_policy fl_policy[TCA_FLOWER_MAX + 1] = {
13109 + [TCA_FLOWER_KEY_ENC_IP_TTL_MASK] = { .type = NLA_U8 },
13110 + [TCA_FLOWER_KEY_ENC_OPTS] = { .type = NLA_NESTED },
13111 + [TCA_FLOWER_KEY_ENC_OPTS_MASK] = { .type = NLA_NESTED },
13112 ++ [TCA_FLOWER_FLAGS] = { .type = NLA_U32 },
13113 + };
13114 +
13115 + static const struct nla_policy
13116 +diff --git a/net/sched/cls_matchall.c b/net/sched/cls_matchall.c
13117 +index 40be745db357..74863b0ff694 100644
13118 +--- a/net/sched/cls_matchall.c
13119 ++++ b/net/sched/cls_matchall.c
13120 +@@ -137,6 +137,7 @@ static void *mall_get(struct tcf_proto *tp, u32 handle)
13121 + static const struct nla_policy mall_policy[TCA_MATCHALL_MAX + 1] = {
13122 + [TCA_MATCHALL_UNSPEC] = { .type = NLA_UNSPEC },
13123 + [TCA_MATCHALL_CLASSID] = { .type = NLA_U32 },
13124 ++ [TCA_MATCHALL_FLAGS] = { .type = NLA_U32 },
13125 + };
13126 +
13127 + static int mall_set_parms(struct net *net, struct tcf_proto *tp,
13128 +diff --git a/net/smc/smc_diag.c b/net/smc/smc_diag.c
13129 +index 371b4cf31fcd..2379a02c319d 100644
13130 +--- a/net/smc/smc_diag.c
13131 ++++ b/net/smc/smc_diag.c
13132 +@@ -38,16 +38,15 @@ static void smc_diag_msg_common_fill(struct smc_diag_msg *r, struct sock *sk)
13133 + {
13134 + struct smc_sock *smc = smc_sk(sk);
13135 +
13136 ++ memset(r, 0, sizeof(*r));
13137 + r->diag_family = sk->sk_family;
13138 ++ sock_diag_save_cookie(sk, r->id.idiag_cookie);
13139 + if (!smc->clcsock)
13140 + return;
13141 + r->id.idiag_sport = htons(smc->clcsock->sk->sk_num);
13142 + r->id.idiag_dport = smc->clcsock->sk->sk_dport;
13143 + r->id.idiag_if = smc->clcsock->sk->sk_bound_dev_if;
13144 +- sock_diag_save_cookie(sk, r->id.idiag_cookie);
13145 + if (sk->sk_protocol == SMCPROTO_SMC) {
13146 +- memset(&r->id.idiag_src, 0, sizeof(r->id.idiag_src));
13147 +- memset(&r->id.idiag_dst, 0, sizeof(r->id.idiag_dst));
13148 + r->id.idiag_src[0] = smc->clcsock->sk->sk_rcv_saddr;
13149 + r->id.idiag_dst[0] = smc->clcsock->sk->sk_daddr;
13150 + #if IS_ENABLED(CONFIG_IPV6)
13151 +diff --git a/scripts/Kconfig.include b/scripts/Kconfig.include
13152 +index 3b2861f47709..79455ad6b386 100644
13153 +--- a/scripts/Kconfig.include
13154 ++++ b/scripts/Kconfig.include
13155 +@@ -20,7 +20,7 @@ success = $(if-success,$(1),y,n)
13156 +
13157 + # $(cc-option,<flag>)
13158 + # Return y if the compiler supports <flag>, n otherwise
13159 +-cc-option = $(success,$(CC) -Werror $(CLANG_FLAGS) $(1) -E -x c /dev/null -o /dev/null)
13160 ++cc-option = $(success,$(CC) -Werror $(CLANG_FLAGS) $(1) -S -x c /dev/null -o /dev/null)
13161 +
13162 + # $(ld-option,<flag>)
13163 + # Return y if the linker supports <flag>, n otherwise
13164 +diff --git a/scripts/kconfig/confdata.c b/scripts/kconfig/confdata.c
13165 +index 0dde19cf7486..2caf5fac102a 100644
13166 +--- a/scripts/kconfig/confdata.c
13167 ++++ b/scripts/kconfig/confdata.c
13168 +@@ -1314,7 +1314,7 @@ bool conf_set_all_new_symbols(enum conf_def_mode mode)
13169 +
13170 + sym_calc_value(csym);
13171 + if (mode == def_random)
13172 +- has_changed = randomize_choice_values(csym);
13173 ++ has_changed |= randomize_choice_values(csym);
13174 + else {
13175 + set_all_choice_values(csym);
13176 + has_changed = true;
13177 +diff --git a/security/selinux/avc.c b/security/selinux/avc.c
13178 +index 5de18a6d5c3f..d52be7b9f08c 100644
13179 +--- a/security/selinux/avc.c
13180 ++++ b/security/selinux/avc.c
13181 +@@ -496,7 +496,7 @@ static inline int avc_xperms_audit(struct selinux_state *state,
13182 + if (likely(!audited))
13183 + return 0;
13184 + return slow_avc_audit(state, ssid, tsid, tclass, requested,
13185 +- audited, denied, result, ad, 0);
13186 ++ audited, denied, result, ad);
13187 + }
13188 +
13189 + static void avc_node_free(struct rcu_head *rhead)
13190 +@@ -689,40 +689,37 @@ static struct avc_node *avc_insert(struct selinux_avc *avc,
13191 + struct avc_node *pos, *node = NULL;
13192 + int hvalue;
13193 + unsigned long flag;
13194 ++ spinlock_t *lock;
13195 ++ struct hlist_head *head;
13196 +
13197 + if (avc_latest_notif_update(avc, avd->seqno, 1))
13198 +- goto out;
13199 ++ return NULL;
13200 +
13201 + node = avc_alloc_node(avc);
13202 +- if (node) {
13203 +- struct hlist_head *head;
13204 +- spinlock_t *lock;
13205 +- int rc = 0;
13206 +-
13207 +- hvalue = avc_hash(ssid, tsid, tclass);
13208 +- avc_node_populate(node, ssid, tsid, tclass, avd);
13209 +- rc = avc_xperms_populate(node, xp_node);
13210 +- if (rc) {
13211 +- kmem_cache_free(avc_node_cachep, node);
13212 +- return NULL;
13213 +- }
13214 +- head = &avc->avc_cache.slots[hvalue];
13215 +- lock = &avc->avc_cache.slots_lock[hvalue];
13216 ++ if (!node)
13217 ++ return NULL;
13218 +
13219 +- spin_lock_irqsave(lock, flag);
13220 +- hlist_for_each_entry(pos, head, list) {
13221 +- if (pos->ae.ssid == ssid &&
13222 +- pos->ae.tsid == tsid &&
13223 +- pos->ae.tclass == tclass) {
13224 +- avc_node_replace(avc, node, pos);
13225 +- goto found;
13226 +- }
13227 ++ avc_node_populate(node, ssid, tsid, tclass, avd);
13228 ++ if (avc_xperms_populate(node, xp_node)) {
13229 ++ avc_node_kill(avc, node);
13230 ++ return NULL;
13231 ++ }
13232 ++
13233 ++ hvalue = avc_hash(ssid, tsid, tclass);
13234 ++ head = &avc->avc_cache.slots[hvalue];
13235 ++ lock = &avc->avc_cache.slots_lock[hvalue];
13236 ++ spin_lock_irqsave(lock, flag);
13237 ++ hlist_for_each_entry(pos, head, list) {
13238 ++ if (pos->ae.ssid == ssid &&
13239 ++ pos->ae.tsid == tsid &&
13240 ++ pos->ae.tclass == tclass) {
13241 ++ avc_node_replace(avc, node, pos);
13242 ++ goto found;
13243 + }
13244 +- hlist_add_head_rcu(&node->list, head);
13245 +-found:
13246 +- spin_unlock_irqrestore(lock, flag);
13247 + }
13248 +-out:
13249 ++ hlist_add_head_rcu(&node->list, head);
13250 ++found:
13251 ++ spin_unlock_irqrestore(lock, flag);
13252 + return node;
13253 + }
13254 +
13255 +@@ -766,8 +763,7 @@ static void avc_audit_post_callback(struct audit_buffer *ab, void *a)
13256 + noinline int slow_avc_audit(struct selinux_state *state,
13257 + u32 ssid, u32 tsid, u16 tclass,
13258 + u32 requested, u32 audited, u32 denied, int result,
13259 +- struct common_audit_data *a,
13260 +- unsigned int flags)
13261 ++ struct common_audit_data *a)
13262 + {
13263 + struct common_audit_data stack_data;
13264 + struct selinux_audit_data sad;
13265 +@@ -777,17 +773,6 @@ noinline int slow_avc_audit(struct selinux_state *state,
13266 + a->type = LSM_AUDIT_DATA_NONE;
13267 + }
13268 +
13269 +- /*
13270 +- * When in a RCU walk do the audit on the RCU retry. This is because
13271 +- * the collection of the dname in an inode audit message is not RCU
13272 +- * safe. Note this may drop some audits when the situation changes
13273 +- * during retry. However this is logically just as if the operation
13274 +- * happened a little later.
13275 +- */
13276 +- if ((a->type == LSM_AUDIT_DATA_INODE) &&
13277 +- (flags & MAY_NOT_BLOCK))
13278 +- return -ECHILD;
13279 +-
13280 + sad.tclass = tclass;
13281 + sad.requested = requested;
13282 + sad.ssid = ssid;
13283 +@@ -860,16 +845,14 @@ static int avc_update_node(struct selinux_avc *avc,
13284 + /*
13285 + * If we are in a non-blocking code path, e.g. VFS RCU walk,
13286 + * then we must not add permissions to a cache entry
13287 +- * because we cannot safely audit the denial. Otherwise,
13288 ++ * because we will not audit the denial. Otherwise,
13289 + * during the subsequent blocking retry (e.g. VFS ref walk), we
13290 + * will find the permissions already granted in the cache entry
13291 + * and won't audit anything at all, leading to silent denials in
13292 + * permissive mode that only appear when in enforcing mode.
13293 + *
13294 +- * See the corresponding handling in slow_avc_audit(), and the
13295 +- * logic in selinux_inode_follow_link and selinux_inode_permission
13296 +- * for the VFS MAY_NOT_BLOCK flag, which is transliterated into
13297 +- * AVC_NONBLOCKING for avc_has_perm_noaudit().
13298 ++ * See the corresponding handling of MAY_NOT_BLOCK in avc_audit()
13299 ++ * and selinux_inode_permission().
13300 + */
13301 + if (flags & AVC_NONBLOCKING)
13302 + return 0;
13303 +@@ -913,7 +896,7 @@ static int avc_update_node(struct selinux_avc *avc,
13304 + if (orig->ae.xp_node) {
13305 + rc = avc_xperms_populate(node, orig->ae.xp_node);
13306 + if (rc) {
13307 +- kmem_cache_free(avc_node_cachep, node);
13308 ++ avc_node_kill(avc, node);
13309 + goto out_unlock;
13310 + }
13311 + }
13312 +diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
13313 +index 040c843968dc..c574285966f9 100644
13314 +--- a/security/selinux/hooks.c
13315 ++++ b/security/selinux/hooks.c
13316 +@@ -3171,8 +3171,7 @@ static int selinux_inode_follow_link(struct dentry *dentry, struct inode *inode,
13317 +
13318 + static noinline int audit_inode_permission(struct inode *inode,
13319 + u32 perms, u32 audited, u32 denied,
13320 +- int result,
13321 +- unsigned flags)
13322 ++ int result)
13323 + {
13324 + struct common_audit_data ad;
13325 + struct inode_security_struct *isec = inode->i_security;
13326 +@@ -3183,7 +3182,7 @@ static noinline int audit_inode_permission(struct inode *inode,
13327 +
13328 + rc = slow_avc_audit(&selinux_state,
13329 + current_sid(), isec->sid, isec->sclass, perms,
13330 +- audited, denied, result, &ad, flags);
13331 ++ audited, denied, result, &ad);
13332 + if (rc)
13333 + return rc;
13334 + return 0;
13335 +@@ -3230,7 +3229,11 @@ static int selinux_inode_permission(struct inode *inode, int mask)
13336 + if (likely(!audited))
13337 + return rc;
13338 +
13339 +- rc2 = audit_inode_permission(inode, perms, audited, denied, rc, flags);
13340 ++ /* fall back to ref-walk if we have to generate audit */
13341 ++ if (flags & MAY_NOT_BLOCK)
13342 ++ return -ECHILD;
13343 ++
13344 ++ rc2 = audit_inode_permission(inode, perms, audited, denied, rc);
13345 + if (rc2)
13346 + return rc2;
13347 + return rc;
13348 +diff --git a/security/selinux/include/avc.h b/security/selinux/include/avc.h
13349 +index 74ea50977c20..cf4cc3ef959b 100644
13350 +--- a/security/selinux/include/avc.h
13351 ++++ b/security/selinux/include/avc.h
13352 +@@ -100,8 +100,7 @@ static inline u32 avc_audit_required(u32 requested,
13353 + int slow_avc_audit(struct selinux_state *state,
13354 + u32 ssid, u32 tsid, u16 tclass,
13355 + u32 requested, u32 audited, u32 denied, int result,
13356 +- struct common_audit_data *a,
13357 +- unsigned flags);
13358 ++ struct common_audit_data *a);
13359 +
13360 + /**
13361 + * avc_audit - Audit the granting or denial of permissions.
13362 +@@ -135,9 +134,12 @@ static inline int avc_audit(struct selinux_state *state,
13363 + audited = avc_audit_required(requested, avd, result, 0, &denied);
13364 + if (likely(!audited))
13365 + return 0;
13366 ++ /* fall back to ref-walk if we have to generate audit */
13367 ++ if (flags & MAY_NOT_BLOCK)
13368 ++ return -ECHILD;
13369 + return slow_avc_audit(state, ssid, tsid, tclass,
13370 + requested, audited, denied, result,
13371 +- a, flags);
13372 ++ a);
13373 + }
13374 +
13375 + #define AVC_STRICT 1 /* Ignore permissive mode. */
13376 +diff --git a/sound/core/control.c b/sound/core/control.c
13377 +index 649d3217590e..d1312f14d78f 100644
13378 +--- a/sound/core/control.c
13379 ++++ b/sound/core/control.c
13380 +@@ -1468,8 +1468,9 @@ static int call_tlv_handler(struct snd_ctl_file *file, int op_flag,
13381 + if (kctl->tlv.c == NULL)
13382 + return -ENXIO;
13383 +
13384 +- /* When locked, this is unavailable. */
13385 +- if (vd->owner != NULL && vd->owner != file)
13386 ++ /* Write and command operations are not allowed for locked element. */
13387 ++ if (op_flag != SNDRV_CTL_TLV_OP_READ &&
13388 ++ vd->owner != NULL && vd->owner != file)
13389 + return -EPERM;
13390 +
13391 + return kctl->tlv.c(kctl, op_flag, size, buf);
13392 +diff --git a/sound/pci/hda/patch_conexant.c b/sound/pci/hda/patch_conexant.c
13393 +index 5500dd437b44..78bb96263bc2 100644
13394 +--- a/sound/pci/hda/patch_conexant.c
13395 ++++ b/sound/pci/hda/patch_conexant.c
13396 +@@ -935,6 +935,7 @@ static const struct snd_pci_quirk cxt5066_fixups[] = {
13397 + SND_PCI_QUIRK(0x17aa, 0x215f, "Lenovo T510", CXT_PINCFG_LENOVO_TP410),
13398 + SND_PCI_QUIRK(0x17aa, 0x21ce, "Lenovo T420", CXT_PINCFG_LENOVO_TP410),
13399 + SND_PCI_QUIRK(0x17aa, 0x21cf, "Lenovo T520", CXT_PINCFG_LENOVO_TP410),
13400 ++ SND_PCI_QUIRK(0x17aa, 0x21d2, "Lenovo T420s", CXT_PINCFG_LENOVO_TP410),
13401 + SND_PCI_QUIRK(0x17aa, 0x21da, "Lenovo X220", CXT_PINCFG_LENOVO_TP410),
13402 + SND_PCI_QUIRK(0x17aa, 0x21db, "Lenovo X220-tablet", CXT_PINCFG_LENOVO_TP410),
13403 + SND_PCI_QUIRK(0x17aa, 0x38af, "Lenovo IdeaPad Z560", CXT_FIXUP_MUTE_LED_EAPD),
13404 +diff --git a/sound/pci/hda/patch_hdmi.c b/sound/pci/hda/patch_hdmi.c
13405 +index c827a2a89cc3..c67fadd5aae5 100644
13406 +--- a/sound/pci/hda/patch_hdmi.c
13407 ++++ b/sound/pci/hda/patch_hdmi.c
13408 +@@ -2604,9 +2604,12 @@ static int alloc_intel_hdmi(struct hda_codec *codec)
13409 + /* parse and post-process for Intel codecs */
13410 + static int parse_intel_hdmi(struct hda_codec *codec)
13411 + {
13412 +- int err;
13413 ++ int err, retries = 3;
13414 ++
13415 ++ do {
13416 ++ err = hdmi_parse_codec(codec);
13417 ++ } while (err < 0 && retries--);
13418 +
13419 +- err = hdmi_parse_codec(codec);
13420 + if (err < 0) {
13421 + generic_spec_free(codec);
13422 + return err;
13423 +diff --git a/sound/sh/aica.c b/sound/sh/aica.c
13424 +index ad3f71358486..69ac44b33560 100644
13425 +--- a/sound/sh/aica.c
13426 ++++ b/sound/sh/aica.c
13427 +@@ -117,10 +117,10 @@ static void spu_memset(u32 toi, u32 what, int length)
13428 + }
13429 +
13430 + /* spu_memload - write to SPU address space */
13431 +-static void spu_memload(u32 toi, void *from, int length)
13432 ++static void spu_memload(u32 toi, const void *from, int length)
13433 + {
13434 + unsigned long flags;
13435 +- u32 *froml = from;
13436 ++ const u32 *froml = from;
13437 + u32 __iomem *to = (u32 __iomem *) (SPU_MEMORY_BASE + toi);
13438 + int i;
13439 + u32 val;
13440 +diff --git a/sound/sh/sh_dac_audio.c b/sound/sh/sh_dac_audio.c
13441 +index 834b2574786f..6251b5e1b64a 100644
13442 +--- a/sound/sh/sh_dac_audio.c
13443 ++++ b/sound/sh/sh_dac_audio.c
13444 +@@ -190,7 +190,6 @@ static int snd_sh_dac_pcm_copy(struct snd_pcm_substream *substream,
13445 + {
13446 + /* channel is not used (interleaved data) */
13447 + struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
13448 +- struct snd_pcm_runtime *runtime = substream->runtime;
13449 +
13450 + if (copy_from_user_toio(chip->data_buffer + pos, src, count))
13451 + return -EFAULT;
13452 +@@ -210,7 +209,6 @@ static int snd_sh_dac_pcm_copy_kernel(struct snd_pcm_substream *substream,
13453 + {
13454 + /* channel is not used (interleaved data) */
13455 + struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
13456 +- struct snd_pcm_runtime *runtime = substream->runtime;
13457 +
13458 + memcpy_toio(chip->data_buffer + pos, src, count);
13459 + chip->buffer_end = chip->data_buffer + pos + count;
13460 +@@ -229,7 +227,6 @@ static int snd_sh_dac_pcm_silence(struct snd_pcm_substream *substream,
13461 + {
13462 + /* channel is not used (interleaved data) */
13463 + struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
13464 +- struct snd_pcm_runtime *runtime = substream->runtime;
13465 +
13466 + memset_io(chip->data_buffer + pos, 0, count);
13467 + chip->buffer_end = chip->data_buffer + pos + count;
13468 +diff --git a/sound/soc/atmel/Kconfig b/sound/soc/atmel/Kconfig
13469 +index 64b784e96f84..dad778e5884b 100644
13470 +--- a/sound/soc/atmel/Kconfig
13471 ++++ b/sound/soc/atmel/Kconfig
13472 +@@ -25,6 +25,8 @@ config SND_ATMEL_SOC_DMA
13473 +
13474 + config SND_ATMEL_SOC_SSC_DMA
13475 + tristate
13476 ++ select SND_ATMEL_SOC_DMA
13477 ++ select SND_ATMEL_SOC_PDC
13478 +
13479 + config SND_ATMEL_SOC_SSC
13480 + tristate
13481 +diff --git a/sound/usb/usx2y/usX2Yhwdep.c b/sound/usb/usx2y/usX2Yhwdep.c
13482 +index c1dd9a7b48df..36b345970364 100644
13483 +--- a/sound/usb/usx2y/usX2Yhwdep.c
13484 ++++ b/sound/usb/usx2y/usX2Yhwdep.c
13485 +@@ -131,7 +131,7 @@ static int snd_usX2Y_hwdep_dsp_status(struct snd_hwdep *hw,
13486 + info->num_dsps = 2; // 0: Prepad Data, 1: FPGA Code
13487 + if (us428->chip_status & USX2Y_STAT_CHIP_INIT)
13488 + info->chip_ready = 1;
13489 +- info->version = USX2Y_DRIVER_VERSION;
13490 ++ info->version = USX2Y_DRIVER_VERSION;
13491 + return 0;
13492 + }
13493 +
13494 +diff --git a/tools/lib/api/fs/fs.c b/tools/lib/api/fs/fs.c
13495 +index 7aba8243a0e7..bd021a0eeef8 100644
13496 +--- a/tools/lib/api/fs/fs.c
13497 ++++ b/tools/lib/api/fs/fs.c
13498 +@@ -210,6 +210,7 @@ static bool fs__env_override(struct fs *fs)
13499 + size_t name_len = strlen(fs->name);
13500 + /* name + "_PATH" + '\0' */
13501 + char upper_name[name_len + 5 + 1];
13502 ++
13503 + memcpy(upper_name, fs->name, name_len);
13504 + mem_toupper(upper_name, name_len);
13505 + strcpy(&upper_name[name_len], "_PATH");
13506 +@@ -219,7 +220,8 @@ static bool fs__env_override(struct fs *fs)
13507 + return false;
13508 +
13509 + fs->found = true;
13510 +- strncpy(fs->path, override_path, sizeof(fs->path));
13511 ++ strncpy(fs->path, override_path, sizeof(fs->path) - 1);
13512 ++ fs->path[sizeof(fs->path) - 1] = '\0';
13513 + return true;
13514 + }
13515 +
13516 +diff --git a/tools/objtool/arch/x86/lib/x86-opcode-map.txt b/tools/objtool/arch/x86/lib/x86-opcode-map.txt
13517 +index 0a0e9112f284..5cb9f009f2be 100644
13518 +--- a/tools/objtool/arch/x86/lib/x86-opcode-map.txt
13519 ++++ b/tools/objtool/arch/x86/lib/x86-opcode-map.txt
13520 +@@ -909,7 +909,7 @@ EndTable
13521 +
13522 + GrpTable: Grp3_2
13523 + 0: TEST Ev,Iz
13524 +-1:
13525 ++1: TEST Ev,Iz
13526 + 2: NOT Ev
13527 + 3: NEG Ev
13528 + 4: MUL rAX,Ev
13529 +diff --git a/tools/testing/selftests/bpf/test_select_reuseport.c b/tools/testing/selftests/bpf/test_select_reuseport.c
13530 +index 75646d9b34aa..cdbbdab2725f 100644
13531 +--- a/tools/testing/selftests/bpf/test_select_reuseport.c
13532 ++++ b/tools/testing/selftests/bpf/test_select_reuseport.c
13533 +@@ -30,7 +30,7 @@
13534 + #define REUSEPORT_ARRAY_SIZE 32
13535 +
13536 + static int result_map, tmp_index_ovr_map, linum_map, data_check_map;
13537 +-static enum result expected_results[NR_RESULTS];
13538 ++static __u32 expected_results[NR_RESULTS];
13539 + static int sk_fds[REUSEPORT_ARRAY_SIZE];
13540 + static int reuseport_array, outer_map;
13541 + static int select_by_skb_data_prog;
13542 +@@ -610,7 +610,19 @@ static void setup_per_test(int type, unsigned short family, bool inany)
13543 +
13544 + static void cleanup_per_test(void)
13545 + {
13546 +- int i, err;
13547 ++ int i, err, zero = 0;
13548 ++
13549 ++ memset(expected_results, 0, sizeof(expected_results));
13550 ++
13551 ++ for (i = 0; i < NR_RESULTS; i++) {
13552 ++ err = bpf_map_update_elem(result_map, &i, &zero, BPF_ANY);
13553 ++ RET_IF(err, "reset elem in result_map",
13554 ++ "i:%u err:%d errno:%d\n", i, err, errno);
13555 ++ }
13556 ++
13557 ++ err = bpf_map_update_elem(linum_map, &zero, &zero, BPF_ANY);
13558 ++ RET_IF(err, "reset line number in linum_map", "err:%d errno:%d\n",
13559 ++ err, errno);
13560 +
13561 + for (i = 0; i < REUSEPORT_ARRAY_SIZE; i++)
13562 + close(sk_fds[i]);
13563 +diff --git a/tools/testing/selftests/size/get_size.c b/tools/testing/selftests/size/get_size.c
13564 +index d4b59ab979a0..f55943b6d1e2 100644
13565 +--- a/tools/testing/selftests/size/get_size.c
13566 ++++ b/tools/testing/selftests/size/get_size.c
13567 +@@ -12,23 +12,35 @@
13568 + * own execution. It also attempts to have as few dependencies
13569 + * on kernel features as possible.
13570 + *
13571 +- * It should be statically linked, with startup libs avoided.
13572 +- * It uses no library calls, and only the following 3 syscalls:
13573 ++ * It should be statically linked, with startup libs avoided. It uses
13574 ++ * no library calls except the syscall() function for the following 3
13575 ++ * syscalls:
13576 + * sysinfo(), write(), and _exit()
13577 + *
13578 + * For output, it avoids printf (which in some C libraries
13579 + * has large external dependencies) by implementing it's own
13580 + * number output and print routines, and using __builtin_strlen()
13581 ++ *
13582 ++ * The test may crash if any of the above syscalls fails because in some
13583 ++ * libc implementations (e.g. the GNU C Library) errno is saved in
13584 ++ * thread-local storage, which does not get initialized due to avoiding
13585 ++ * startup libs.
13586 + */
13587 +
13588 + #include <sys/sysinfo.h>
13589 + #include <unistd.h>
13590 ++#include <sys/syscall.h>
13591 +
13592 + #define STDOUT_FILENO 1
13593 +
13594 + static int print(const char *s)
13595 + {
13596 +- return write(STDOUT_FILENO, s, __builtin_strlen(s));
13597 ++ size_t len = 0;
13598 ++
13599 ++ while (s[len] != '\0')
13600 ++ len++;
13601 ++
13602 ++ return syscall(SYS_write, STDOUT_FILENO, s, len);
13603 + }
13604 +
13605 + static inline char *num_to_str(unsigned long num, char *buf, int len)
13606 +@@ -80,12 +92,12 @@ void _start(void)
13607 + print("TAP version 13\n");
13608 + print("# Testing system size.\n");
13609 +
13610 +- ccode = sysinfo(&info);
13611 ++ ccode = syscall(SYS_sysinfo, &info);
13612 + if (ccode < 0) {
13613 + print("not ok 1");
13614 + print(test_name);
13615 + print(" ---\n reason: \"could not get sysinfo\"\n ...\n");
13616 +- _exit(ccode);
13617 ++ syscall(SYS_exit, ccode);
13618 + }
13619 + print("ok 1");
13620 + print(test_name);
13621 +@@ -101,5 +113,5 @@ void _start(void)
13622 + print(" ...\n");
13623 + print("1..1\n");
13624 +
13625 +- _exit(0);
13626 ++ syscall(SYS_exit, 0);
13627 + }
13628 +diff --git a/tools/usb/usbip/src/usbip_network.c b/tools/usb/usbip/src/usbip_network.c
13629 +index 8ffcd47d9638..902f55208e23 100644
13630 +--- a/tools/usb/usbip/src/usbip_network.c
13631 ++++ b/tools/usb/usbip/src/usbip_network.c
13632 +@@ -62,39 +62,39 @@ void usbip_setup_port_number(char *arg)
13633 + info("using port %d (\"%s\")", usbip_port, usbip_port_string);
13634 + }
13635 +
13636 +-void usbip_net_pack_uint32_t(int pack, uint32_t *num)
13637 ++uint32_t usbip_net_pack_uint32_t(int pack, uint32_t num)
13638 + {
13639 + uint32_t i;
13640 +
13641 + if (pack)
13642 +- i = htonl(*num);
13643 ++ i = htonl(num);
13644 + else
13645 +- i = ntohl(*num);
13646 ++ i = ntohl(num);
13647 +
13648 +- *num = i;
13649 ++ return i;
13650 + }
13651 +
13652 +-void usbip_net_pack_uint16_t(int pack, uint16_t *num)
13653 ++uint16_t usbip_net_pack_uint16_t(int pack, uint16_t num)
13654 + {
13655 + uint16_t i;
13656 +
13657 + if (pack)
13658 +- i = htons(*num);
13659 ++ i = htons(num);
13660 + else
13661 +- i = ntohs(*num);
13662 ++ i = ntohs(num);
13663 +
13664 +- *num = i;
13665 ++ return i;
13666 + }
13667 +
13668 + void usbip_net_pack_usb_device(int pack, struct usbip_usb_device *udev)
13669 + {
13670 +- usbip_net_pack_uint32_t(pack, &udev->busnum);
13671 +- usbip_net_pack_uint32_t(pack, &udev->devnum);
13672 +- usbip_net_pack_uint32_t(pack, &udev->speed);
13673 ++ udev->busnum = usbip_net_pack_uint32_t(pack, udev->busnum);
13674 ++ udev->devnum = usbip_net_pack_uint32_t(pack, udev->devnum);
13675 ++ udev->speed = usbip_net_pack_uint32_t(pack, udev->speed);
13676 +
13677 +- usbip_net_pack_uint16_t(pack, &udev->idVendor);
13678 +- usbip_net_pack_uint16_t(pack, &udev->idProduct);
13679 +- usbip_net_pack_uint16_t(pack, &udev->bcdDevice);
13680 ++ udev->idVendor = usbip_net_pack_uint16_t(pack, udev->idVendor);
13681 ++ udev->idProduct = usbip_net_pack_uint16_t(pack, udev->idProduct);
13682 ++ udev->bcdDevice = usbip_net_pack_uint16_t(pack, udev->bcdDevice);
13683 + }
13684 +
13685 + void usbip_net_pack_usb_interface(int pack __attribute__((unused)),
13686 +@@ -141,6 +141,14 @@ ssize_t usbip_net_send(int sockfd, void *buff, size_t bufflen)
13687 + return usbip_net_xmit(sockfd, buff, bufflen, 1);
13688 + }
13689 +
13690 ++static inline void usbip_net_pack_op_common(int pack,
13691 ++ struct op_common *op_common)
13692 ++{
13693 ++ op_common->version = usbip_net_pack_uint16_t(pack, op_common->version);
13694 ++ op_common->code = usbip_net_pack_uint16_t(pack, op_common->code);
13695 ++ op_common->status = usbip_net_pack_uint32_t(pack, op_common->status);
13696 ++}
13697 ++
13698 + int usbip_net_send_op_common(int sockfd, uint32_t code, uint32_t status)
13699 + {
13700 + struct op_common op_common;
13701 +@@ -152,7 +160,7 @@ int usbip_net_send_op_common(int sockfd, uint32_t code, uint32_t status)
13702 + op_common.code = code;
13703 + op_common.status = status;
13704 +
13705 +- PACK_OP_COMMON(1, &op_common);
13706 ++ usbip_net_pack_op_common(1, &op_common);
13707 +
13708 + rc = usbip_net_send(sockfd, &op_common, sizeof(op_common));
13709 + if (rc < 0) {
13710 +@@ -176,7 +184,7 @@ int usbip_net_recv_op_common(int sockfd, uint16_t *code, int *status)
13711 + goto err;
13712 + }
13713 +
13714 +- PACK_OP_COMMON(0, &op_common);
13715 ++ usbip_net_pack_op_common(0, &op_common);
13716 +
13717 + if (op_common.version != USBIP_VERSION) {
13718 + err("USBIP Kernel and tool version mismatch: %d %d:",
13719 +diff --git a/tools/usb/usbip/src/usbip_network.h b/tools/usb/usbip/src/usbip_network.h
13720 +index 555215eae43e..83b4c5344f72 100644
13721 +--- a/tools/usb/usbip/src/usbip_network.h
13722 ++++ b/tools/usb/usbip/src/usbip_network.h
13723 +@@ -32,12 +32,6 @@ struct op_common {
13724 +
13725 + } __attribute__((packed));
13726 +
13727 +-#define PACK_OP_COMMON(pack, op_common) do {\
13728 +- usbip_net_pack_uint16_t(pack, &(op_common)->version);\
13729 +- usbip_net_pack_uint16_t(pack, &(op_common)->code);\
13730 +- usbip_net_pack_uint32_t(pack, &(op_common)->status);\
13731 +-} while (0)
13732 +-
13733 + /* ---------------------------------------------------------------------- */
13734 + /* Dummy Code */
13735 + #define OP_UNSPEC 0x00
13736 +@@ -163,11 +157,11 @@ struct op_devlist_reply_extra {
13737 + } while (0)
13738 +
13739 + #define PACK_OP_DEVLIST_REPLY(pack, reply) do {\
13740 +- usbip_net_pack_uint32_t(pack, &(reply)->ndev);\
13741 ++ (reply)->ndev = usbip_net_pack_uint32_t(pack, (reply)->ndev);\
13742 + } while (0)
13743 +
13744 +-void usbip_net_pack_uint32_t(int pack, uint32_t *num);
13745 +-void usbip_net_pack_uint16_t(int pack, uint16_t *num);
13746 ++uint32_t usbip_net_pack_uint32_t(int pack, uint32_t num);
13747 ++uint16_t usbip_net_pack_uint16_t(int pack, uint16_t num);
13748 + void usbip_net_pack_usb_device(int pack, struct usbip_usb_device *udev);
13749 + void usbip_net_pack_usb_interface(int pack, struct usbip_usb_interface *uinf);
13750 +