Gentoo Archives: gentoo-commits

From: Mike Pagano <mpagano@g.o>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] proj/linux-patches:4.9 commit in: /
Date: Fri, 14 Feb 2020 23:36:55
Message-Id: 1581723390.69b6a1f5f90590939aee5275da1056be38d5c1e2.mpagano@gentoo
1 commit: 69b6a1f5f90590939aee5275da1056be38d5c1e2
2 Author: Mike Pagano <mpagano <AT> gentoo <DOT> org>
3 AuthorDate: Fri Feb 14 23:36:30 2020 +0000
4 Commit: Mike Pagano <mpagano <AT> gentoo <DOT> org>
5 CommitDate: Fri Feb 14 23:36:30 2020 +0000
6 URL: https://gitweb.gentoo.org/proj/linux-patches.git/commit/?id=69b6a1f5
7
8 Linux patch 4.9.214
9
10 Signed-off-by: Mike Pagano <mpagano <AT> gentoo.org>
11
12 0000_README | 4 +
13 1213_linux-4.9.214.patch | 11817 +++++++++++++++++++++++++++++++++++++++++++++
14 2 files changed, 11821 insertions(+)
15
16 diff --git a/0000_README b/0000_README
17 index 5b98293..bc32b07 100644
18 --- a/0000_README
19 +++ b/0000_README
20 @@ -895,6 +895,10 @@ Patch: 1212_linux-4.9.213.patch
21 From: http://www.kernel.org
22 Desc: Linux 4.9.213
23
24 +Patch: 1213_linux-4.9.214.patch
25 +From: http://www.kernel.org
26 +Desc: Linux 4.9.214
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/1213_linux-4.9.214.patch b/1213_linux-4.9.214.patch
33 new file mode 100644
34 index 0000000..dda82d4
35 --- /dev/null
36 +++ b/1213_linux-4.9.214.patch
37 @@ -0,0 +1,11817 @@
38 +diff --git a/Makefile b/Makefile
39 +index de79c801abcd..9a6aa41a9ec1 100644
40 +--- a/Makefile
41 ++++ b/Makefile
42 +@@ -1,6 +1,6 @@
43 + VERSION = 4
44 + PATCHLEVEL = 9
45 +-SUBLEVEL = 213
46 ++SUBLEVEL = 214
47 + EXTRAVERSION =
48 + NAME = Roaring Lionus
49 +
50 +diff --git a/arch/arc/boot/dts/axs10x_mb.dtsi b/arch/arc/boot/dts/axs10x_mb.dtsi
51 +index d6c1bbc98ac3..15698b3e490f 100644
52 +--- a/arch/arc/boot/dts/axs10x_mb.dtsi
53 ++++ b/arch/arc/boot/dts/axs10x_mb.dtsi
54 +@@ -63,6 +63,7 @@
55 + interrupt-names = "macirq";
56 + phy-mode = "rgmii";
57 + snps,pbl = < 32 >;
58 ++ snps,multicast-filter-bins = <256>;
59 + clocks = <&apbclk>;
60 + clock-names = "stmmaceth";
61 + max-speed = <100>;
62 +diff --git a/arch/arm/boot/dts/sama5d3.dtsi b/arch/arm/boot/dts/sama5d3.dtsi
63 +index 4c84d333fc7e..33c0d2668934 100644
64 +--- a/arch/arm/boot/dts/sama5d3.dtsi
65 ++++ b/arch/arm/boot/dts/sama5d3.dtsi
66 +@@ -1109,49 +1109,49 @@
67 + usart0_clk: usart0_clk {
68 + #clock-cells = <0>;
69 + reg = <12>;
70 +- atmel,clk-output-range = <0 66000000>;
71 ++ atmel,clk-output-range = <0 83000000>;
72 + };
73 +
74 + usart1_clk: usart1_clk {
75 + #clock-cells = <0>;
76 + reg = <13>;
77 +- atmel,clk-output-range = <0 66000000>;
78 ++ atmel,clk-output-range = <0 83000000>;
79 + };
80 +
81 + usart2_clk: usart2_clk {
82 + #clock-cells = <0>;
83 + reg = <14>;
84 +- atmel,clk-output-range = <0 66000000>;
85 ++ atmel,clk-output-range = <0 83000000>;
86 + };
87 +
88 + usart3_clk: usart3_clk {
89 + #clock-cells = <0>;
90 + reg = <15>;
91 +- atmel,clk-output-range = <0 66000000>;
92 ++ atmel,clk-output-range = <0 83000000>;
93 + };
94 +
95 + uart0_clk: uart0_clk {
96 + #clock-cells = <0>;
97 + reg = <16>;
98 +- atmel,clk-output-range = <0 66000000>;
99 ++ atmel,clk-output-range = <0 83000000>;
100 + };
101 +
102 + twi0_clk: twi0_clk {
103 + reg = <18>;
104 + #clock-cells = <0>;
105 +- atmel,clk-output-range = <0 16625000>;
106 ++ atmel,clk-output-range = <0 41500000>;
107 + };
108 +
109 + twi1_clk: twi1_clk {
110 + #clock-cells = <0>;
111 + reg = <19>;
112 +- atmel,clk-output-range = <0 16625000>;
113 ++ atmel,clk-output-range = <0 41500000>;
114 + };
115 +
116 + twi2_clk: twi2_clk {
117 + #clock-cells = <0>;
118 + reg = <20>;
119 +- atmel,clk-output-range = <0 16625000>;
120 ++ atmel,clk-output-range = <0 41500000>;
121 + };
122 +
123 + mci0_clk: mci0_clk {
124 +@@ -1167,19 +1167,19 @@
125 + spi0_clk: spi0_clk {
126 + #clock-cells = <0>;
127 + reg = <24>;
128 +- atmel,clk-output-range = <0 133000000>;
129 ++ atmel,clk-output-range = <0 166000000>;
130 + };
131 +
132 + spi1_clk: spi1_clk {
133 + #clock-cells = <0>;
134 + reg = <25>;
135 +- atmel,clk-output-range = <0 133000000>;
136 ++ atmel,clk-output-range = <0 166000000>;
137 + };
138 +
139 + tcb0_clk: tcb0_clk {
140 + #clock-cells = <0>;
141 + reg = <26>;
142 +- atmel,clk-output-range = <0 133000000>;
143 ++ atmel,clk-output-range = <0 166000000>;
144 + };
145 +
146 + pwm_clk: pwm_clk {
147 +@@ -1190,7 +1190,7 @@
148 + adc_clk: adc_clk {
149 + #clock-cells = <0>;
150 + reg = <29>;
151 +- atmel,clk-output-range = <0 66000000>;
152 ++ atmel,clk-output-range = <0 83000000>;
153 + };
154 +
155 + dma0_clk: dma0_clk {
156 +@@ -1221,13 +1221,13 @@
157 + ssc0_clk: ssc0_clk {
158 + #clock-cells = <0>;
159 + reg = <38>;
160 +- atmel,clk-output-range = <0 66000000>;
161 ++ atmel,clk-output-range = <0 83000000>;
162 + };
163 +
164 + ssc1_clk: ssc1_clk {
165 + #clock-cells = <0>;
166 + reg = <39>;
167 +- atmel,clk-output-range = <0 66000000>;
168 ++ atmel,clk-output-range = <0 83000000>;
169 + };
170 +
171 + sha_clk: sha_clk {
172 +diff --git a/arch/arm/boot/dts/sama5d3_can.dtsi b/arch/arm/boot/dts/sama5d3_can.dtsi
173 +index c5a3772741bf..0fac79f75c06 100644
174 +--- a/arch/arm/boot/dts/sama5d3_can.dtsi
175 ++++ b/arch/arm/boot/dts/sama5d3_can.dtsi
176 +@@ -37,13 +37,13 @@
177 + can0_clk: can0_clk {
178 + #clock-cells = <0>;
179 + reg = <40>;
180 +- atmel,clk-output-range = <0 66000000>;
181 ++ atmel,clk-output-range = <0 83000000>;
182 + };
183 +
184 + can1_clk: can1_clk {
185 + #clock-cells = <0>;
186 + reg = <41>;
187 +- atmel,clk-output-range = <0 66000000>;
188 ++ atmel,clk-output-range = <0 83000000>;
189 + };
190 + };
191 + };
192 +diff --git a/arch/arm/boot/dts/sama5d3_tcb1.dtsi b/arch/arm/boot/dts/sama5d3_tcb1.dtsi
193 +index 801f9745e82f..b80dbc45a3c2 100644
194 +--- a/arch/arm/boot/dts/sama5d3_tcb1.dtsi
195 ++++ b/arch/arm/boot/dts/sama5d3_tcb1.dtsi
196 +@@ -23,6 +23,7 @@
197 + tcb1_clk: tcb1_clk {
198 + #clock-cells = <0>;
199 + reg = <27>;
200 ++ atmel,clk-output-range = <0 166000000>;
201 + };
202 + };
203 + };
204 +diff --git a/arch/arm/boot/dts/sama5d3_uart.dtsi b/arch/arm/boot/dts/sama5d3_uart.dtsi
205 +index 2511d748867b..71818c7bfb67 100644
206 +--- a/arch/arm/boot/dts/sama5d3_uart.dtsi
207 ++++ b/arch/arm/boot/dts/sama5d3_uart.dtsi
208 +@@ -42,13 +42,13 @@
209 + uart0_clk: uart0_clk {
210 + #clock-cells = <0>;
211 + reg = <16>;
212 +- atmel,clk-output-range = <0 66000000>;
213 ++ atmel,clk-output-range = <0 83000000>;
214 + };
215 +
216 + uart1_clk: uart1_clk {
217 + #clock-cells = <0>;
218 + reg = <17>;
219 +- atmel,clk-output-range = <0 66000000>;
220 ++ atmel,clk-output-range = <0 83000000>;
221 + };
222 + };
223 + };
224 +diff --git a/arch/arm/mach-tegra/sleep-tegra30.S b/arch/arm/mach-tegra/sleep-tegra30.S
225 +index 16e5ff03383c..91b3f06e5425 100644
226 +--- a/arch/arm/mach-tegra/sleep-tegra30.S
227 ++++ b/arch/arm/mach-tegra/sleep-tegra30.S
228 +@@ -382,6 +382,14 @@ _pll_m_c_x_done:
229 + pll_locked r1, r0, CLK_RESET_PLLC_BASE
230 + pll_locked r1, r0, CLK_RESET_PLLX_BASE
231 +
232 ++ tegra_get_soc_id TEGRA_APB_MISC_BASE, r1
233 ++ cmp r1, #TEGRA30
234 ++ beq 1f
235 ++ ldr r1, [r0, #CLK_RESET_PLLP_BASE]
236 ++ bic r1, r1, #(1<<31) @ disable PllP bypass
237 ++ str r1, [r0, #CLK_RESET_PLLP_BASE]
238 ++1:
239 ++
240 + mov32 r7, TEGRA_TMRUS_BASE
241 + ldr r1, [r7]
242 + add r1, r1, #LOCK_DELAY
243 +@@ -641,7 +649,10 @@ tegra30_switch_cpu_to_clk32k:
244 + str r0, [r4, #PMC_PLLP_WB0_OVERRIDE]
245 +
246 + /* disable PLLP, PLLA, PLLC and PLLX */
247 ++ tegra_get_soc_id TEGRA_APB_MISC_BASE, r1
248 ++ cmp r1, #TEGRA30
249 + ldr r0, [r5, #CLK_RESET_PLLP_BASE]
250 ++ orrne r0, r0, #(1 << 31) @ enable PllP bypass on fast cluster
251 + bic r0, r0, #(1 << 30)
252 + str r0, [r5, #CLK_RESET_PLLP_BASE]
253 + ldr r0, [r5, #CLK_RESET_PLLA_BASE]
254 +diff --git a/arch/powerpc/Kconfig b/arch/powerpc/Kconfig
255 +index fa8f2aa88189..f529d3d9d88d 100644
256 +--- a/arch/powerpc/Kconfig
257 ++++ b/arch/powerpc/Kconfig
258 +@@ -85,6 +85,7 @@ config PPC
259 + select BINFMT_ELF
260 + select ARCH_HAS_ELF_RANDOMIZE
261 + select OF
262 ++ select OF_DMA_DEFAULT_COHERENT if !NOT_COHERENT_CACHE
263 + select OF_EARLY_FLATTREE
264 + select OF_RESERVED_MEM
265 + select HAVE_FTRACE_MCOUNT_RECORD
266 +diff --git a/arch/powerpc/boot/4xx.c b/arch/powerpc/boot/4xx.c
267 +index 9d3bd4c45a24..1c4354f922fd 100644
268 +--- a/arch/powerpc/boot/4xx.c
269 ++++ b/arch/powerpc/boot/4xx.c
270 +@@ -232,7 +232,7 @@ void ibm4xx_denali_fixup_memsize(void)
271 + dpath = 8; /* 64 bits */
272 +
273 + /* get address pins (rows) */
274 +- val = SDRAM0_READ(DDR0_42);
275 ++ val = SDRAM0_READ(DDR0_42);
276 +
277 + row = DDR_GET_VAL(val, DDR_APIN, DDR_APIN_SHIFT);
278 + if (row > max_row)
279 +diff --git a/arch/powerpc/kvm/book3s_hv.c b/arch/powerpc/kvm/book3s_hv.c
280 +index e840f943cd2c..5cf1392dff96 100644
281 +--- a/arch/powerpc/kvm/book3s_hv.c
282 ++++ b/arch/powerpc/kvm/book3s_hv.c
283 +@@ -1766,7 +1766,7 @@ static struct kvm_vcpu *kvmppc_core_vcpu_create_hv(struct kvm *kvm,
284 + mutex_unlock(&kvm->lock);
285 +
286 + if (!vcore)
287 +- goto free_vcpu;
288 ++ goto uninit_vcpu;
289 +
290 + spin_lock(&vcore->lock);
291 + ++vcore->num_threads;
292 +@@ -1782,6 +1782,8 @@ static struct kvm_vcpu *kvmppc_core_vcpu_create_hv(struct kvm *kvm,
293 +
294 + return vcpu;
295 +
296 ++uninit_vcpu:
297 ++ kvm_vcpu_uninit(vcpu);
298 + free_vcpu:
299 + kmem_cache_free(kvm_vcpu_cache, vcpu);
300 + out:
301 +diff --git a/arch/powerpc/kvm/book3s_pr.c b/arch/powerpc/kvm/book3s_pr.c
302 +index e0d88d0890aa..8172021bcee6 100644
303 +--- a/arch/powerpc/kvm/book3s_pr.c
304 ++++ b/arch/powerpc/kvm/book3s_pr.c
305 +@@ -1482,10 +1482,12 @@ static struct kvm_vcpu *kvmppc_core_vcpu_create_pr(struct kvm *kvm,
306 +
307 + err = kvmppc_mmu_init(vcpu);
308 + if (err < 0)
309 +- goto uninit_vcpu;
310 ++ goto free_shared_page;
311 +
312 + return vcpu;
313 +
314 ++free_shared_page:
315 ++ free_page((unsigned long)vcpu->arch.shared);
316 + uninit_vcpu:
317 + kvm_vcpu_uninit(vcpu);
318 + free_shadow_vcpu:
319 +diff --git a/arch/powerpc/platforms/pseries/hotplug-memory.c b/arch/powerpc/platforms/pseries/hotplug-memory.c
320 +index 6c12b02f4a61..eee45b9220e0 100644
321 +--- a/arch/powerpc/platforms/pseries/hotplug-memory.c
322 ++++ b/arch/powerpc/platforms/pseries/hotplug-memory.c
323 +@@ -398,8 +398,10 @@ static bool lmb_is_removable(struct of_drconf_cell *lmb)
324 +
325 + for (i = 0; i < scns_per_block; i++) {
326 + pfn = PFN_DOWN(phys_addr);
327 +- if (!pfn_present(pfn))
328 ++ if (!pfn_present(pfn)) {
329 ++ phys_addr += MIN_MEMORY_BLOCK_SIZE;
330 + continue;
331 ++ }
332 +
333 + rc &= is_mem_section_removable(pfn, PAGES_PER_SECTION);
334 + phys_addr += MIN_MEMORY_BLOCK_SIZE;
335 +diff --git a/arch/powerpc/platforms/pseries/iommu.c b/arch/powerpc/platforms/pseries/iommu.c
336 +index 0024e451bb36..c0f094c96cd6 100644
337 +--- a/arch/powerpc/platforms/pseries/iommu.c
338 ++++ b/arch/powerpc/platforms/pseries/iommu.c
339 +@@ -167,10 +167,10 @@ static unsigned long tce_get_pseries(struct iommu_table *tbl, long index)
340 + return be64_to_cpu(*tcep);
341 + }
342 +
343 +-static void tce_free_pSeriesLP(struct iommu_table*, long, long);
344 ++static void tce_free_pSeriesLP(unsigned long liobn, long, long);
345 + static void tce_freemulti_pSeriesLP(struct iommu_table*, long, long);
346 +
347 +-static int tce_build_pSeriesLP(struct iommu_table *tbl, long tcenum,
348 ++static int tce_build_pSeriesLP(unsigned long liobn, long tcenum, long tceshift,
349 + long npages, unsigned long uaddr,
350 + enum dma_data_direction direction,
351 + unsigned long attrs)
352 +@@ -181,25 +181,25 @@ static int tce_build_pSeriesLP(struct iommu_table *tbl, long tcenum,
353 + int ret = 0;
354 + long tcenum_start = tcenum, npages_start = npages;
355 +
356 +- rpn = __pa(uaddr) >> TCE_SHIFT;
357 ++ rpn = __pa(uaddr) >> tceshift;
358 + proto_tce = TCE_PCI_READ;
359 + if (direction != DMA_TO_DEVICE)
360 + proto_tce |= TCE_PCI_WRITE;
361 +
362 + while (npages--) {
363 +- tce = proto_tce | (rpn & TCE_RPN_MASK) << TCE_RPN_SHIFT;
364 +- rc = plpar_tce_put((u64)tbl->it_index, (u64)tcenum << 12, tce);
365 ++ tce = proto_tce | (rpn & TCE_RPN_MASK) << tceshift;
366 ++ rc = plpar_tce_put((u64)liobn, (u64)tcenum << tceshift, tce);
367 +
368 + if (unlikely(rc == H_NOT_ENOUGH_RESOURCES)) {
369 + ret = (int)rc;
370 +- tce_free_pSeriesLP(tbl, tcenum_start,
371 ++ tce_free_pSeriesLP(liobn, tcenum_start,
372 + (npages_start - (npages + 1)));
373 + break;
374 + }
375 +
376 + if (rc && printk_ratelimit()) {
377 + printk("tce_build_pSeriesLP: plpar_tce_put failed. rc=%lld\n", rc);
378 +- printk("\tindex = 0x%llx\n", (u64)tbl->it_index);
379 ++ printk("\tindex = 0x%llx\n", (u64)liobn);
380 + printk("\ttcenum = 0x%llx\n", (u64)tcenum);
381 + printk("\ttce val = 0x%llx\n", tce );
382 + dump_stack();
383 +@@ -228,7 +228,8 @@ static int tce_buildmulti_pSeriesLP(struct iommu_table *tbl, long tcenum,
384 + unsigned long flags;
385 +
386 + if ((npages == 1) || !firmware_has_feature(FW_FEATURE_MULTITCE)) {
387 +- return tce_build_pSeriesLP(tbl, tcenum, npages, uaddr,
388 ++ return tce_build_pSeriesLP(tbl->it_index, tcenum,
389 ++ tbl->it_page_shift, npages, uaddr,
390 + direction, attrs);
391 + }
392 +
393 +@@ -244,8 +245,9 @@ static int tce_buildmulti_pSeriesLP(struct iommu_table *tbl, long tcenum,
394 + /* If allocation fails, fall back to the loop implementation */
395 + if (!tcep) {
396 + local_irq_restore(flags);
397 +- return tce_build_pSeriesLP(tbl, tcenum, npages, uaddr,
398 +- direction, attrs);
399 ++ return tce_build_pSeriesLP(tbl->it_index, tcenum,
400 ++ tbl->it_page_shift,
401 ++ npages, uaddr, direction, attrs);
402 + }
403 + __this_cpu_write(tce_page, tcep);
404 + }
405 +@@ -296,16 +298,16 @@ static int tce_buildmulti_pSeriesLP(struct iommu_table *tbl, long tcenum,
406 + return ret;
407 + }
408 +
409 +-static void tce_free_pSeriesLP(struct iommu_table *tbl, long tcenum, long npages)
410 ++static void tce_free_pSeriesLP(unsigned long liobn, long tcenum, long npages)
411 + {
412 + u64 rc;
413 +
414 + while (npages--) {
415 +- rc = plpar_tce_put((u64)tbl->it_index, (u64)tcenum << 12, 0);
416 ++ rc = plpar_tce_put((u64)liobn, (u64)tcenum << 12, 0);
417 +
418 + if (rc && printk_ratelimit()) {
419 + printk("tce_free_pSeriesLP: plpar_tce_put failed. rc=%lld\n", rc);
420 +- printk("\tindex = 0x%llx\n", (u64)tbl->it_index);
421 ++ printk("\tindex = 0x%llx\n", (u64)liobn);
422 + printk("\ttcenum = 0x%llx\n", (u64)tcenum);
423 + dump_stack();
424 + }
425 +@@ -320,7 +322,7 @@ static void tce_freemulti_pSeriesLP(struct iommu_table *tbl, long tcenum, long n
426 + u64 rc;
427 +
428 + if (!firmware_has_feature(FW_FEATURE_MULTITCE))
429 +- return tce_free_pSeriesLP(tbl, tcenum, npages);
430 ++ return tce_free_pSeriesLP(tbl->it_index, tcenum, npages);
431 +
432 + rc = plpar_tce_stuff((u64)tbl->it_index, (u64)tcenum << 12, 0, npages);
433 +
434 +@@ -435,6 +437,19 @@ static int tce_setrange_multi_pSeriesLP(unsigned long start_pfn,
435 + u64 rc = 0;
436 + long l, limit;
437 +
438 ++ if (!firmware_has_feature(FW_FEATURE_MULTITCE)) {
439 ++ unsigned long tceshift = be32_to_cpu(maprange->tce_shift);
440 ++ unsigned long dmastart = (start_pfn << PAGE_SHIFT) +
441 ++ be64_to_cpu(maprange->dma_base);
442 ++ unsigned long tcenum = dmastart >> tceshift;
443 ++ unsigned long npages = num_pfn << PAGE_SHIFT >> tceshift;
444 ++ void *uaddr = __va(start_pfn << PAGE_SHIFT);
445 ++
446 ++ return tce_build_pSeriesLP(be32_to_cpu(maprange->liobn),
447 ++ tcenum, tceshift, npages, (unsigned long) uaddr,
448 ++ DMA_BIDIRECTIONAL, 0);
449 ++ }
450 ++
451 + local_irq_disable(); /* to protect tcep and the page behind it */
452 + tcep = __this_cpu_read(tce_page);
453 +
454 +diff --git a/arch/sparc/include/uapi/asm/ipcbuf.h b/arch/sparc/include/uapi/asm/ipcbuf.h
455 +index 66013b4fe10d..58da9c4addb2 100644
456 +--- a/arch/sparc/include/uapi/asm/ipcbuf.h
457 ++++ b/arch/sparc/include/uapi/asm/ipcbuf.h
458 +@@ -14,19 +14,19 @@
459 +
460 + struct ipc64_perm
461 + {
462 +- __kernel_key_t key;
463 +- __kernel_uid_t uid;
464 +- __kernel_gid_t gid;
465 +- __kernel_uid_t cuid;
466 +- __kernel_gid_t cgid;
467 ++ __kernel_key_t key;
468 ++ __kernel_uid32_t uid;
469 ++ __kernel_gid32_t gid;
470 ++ __kernel_uid32_t cuid;
471 ++ __kernel_gid32_t cgid;
472 + #ifndef __arch64__
473 +- unsigned short __pad0;
474 ++ unsigned short __pad0;
475 + #endif
476 +- __kernel_mode_t mode;
477 +- unsigned short __pad1;
478 +- unsigned short seq;
479 +- unsigned long long __unused1;
480 +- unsigned long long __unused2;
481 ++ __kernel_mode_t mode;
482 ++ unsigned short __pad1;
483 ++ unsigned short seq;
484 ++ unsigned long long __unused1;
485 ++ unsigned long long __unused2;
486 + };
487 +
488 + #endif /* __SPARC_IPCBUF_H */
489 +diff --git a/arch/x86/kernel/cpu/tsx.c b/arch/x86/kernel/cpu/tsx.c
490 +index 3e20d322bc98..032509adf9de 100644
491 +--- a/arch/x86/kernel/cpu/tsx.c
492 ++++ b/arch/x86/kernel/cpu/tsx.c
493 +@@ -115,11 +115,12 @@ void __init tsx_init(void)
494 + tsx_disable();
495 +
496 + /*
497 +- * tsx_disable() will change the state of the
498 +- * RTM CPUID bit. Clear it here since it is now
499 +- * expected to be not set.
500 ++ * tsx_disable() will change the state of the RTM and HLE CPUID
501 ++ * bits. Clear them here since they are now expected to be not
502 ++ * set.
503 + */
504 + setup_clear_cpu_cap(X86_FEATURE_RTM);
505 ++ setup_clear_cpu_cap(X86_FEATURE_HLE);
506 + } else if (tsx_ctrl_state == TSX_CTRL_ENABLE) {
507 +
508 + /*
509 +@@ -131,10 +132,10 @@ void __init tsx_init(void)
510 + tsx_enable();
511 +
512 + /*
513 +- * tsx_enable() will change the state of the
514 +- * RTM CPUID bit. Force it here since it is now
515 +- * expected to be set.
516 ++ * tsx_enable() will change the state of the RTM and HLE CPUID
517 ++ * bits. Force them here since they are now expected to be set.
518 + */
519 + setup_force_cpu_cap(X86_FEATURE_RTM);
520 ++ setup_force_cpu_cap(X86_FEATURE_HLE);
521 + }
522 + }
523 +diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c
524 +index 660c35f854f8..c456a9dbade8 100644
525 +--- a/arch/x86/kvm/emulate.c
526 ++++ b/arch/x86/kvm/emulate.c
527 +@@ -21,6 +21,7 @@
528 + */
529 +
530 + #include <linux/kvm_host.h>
531 ++#include <linux/nospec.h>
532 + #include "kvm_cache_regs.h"
533 + #include <asm/kvm_emulate.h>
534 + #include <linux/stringify.h>
535 +@@ -5053,16 +5054,28 @@ int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
536 + ctxt->ad_bytes = def_ad_bytes ^ 6;
537 + break;
538 + case 0x26: /* ES override */
539 ++ has_seg_override = true;
540 ++ ctxt->seg_override = VCPU_SREG_ES;
541 ++ break;
542 + case 0x2e: /* CS override */
543 ++ has_seg_override = true;
544 ++ ctxt->seg_override = VCPU_SREG_CS;
545 ++ break;
546 + case 0x36: /* SS override */
547 ++ has_seg_override = true;
548 ++ ctxt->seg_override = VCPU_SREG_SS;
549 ++ break;
550 + case 0x3e: /* DS override */
551 + has_seg_override = true;
552 +- ctxt->seg_override = (ctxt->b >> 3) & 3;
553 ++ ctxt->seg_override = VCPU_SREG_DS;
554 + break;
555 + case 0x64: /* FS override */
556 ++ has_seg_override = true;
557 ++ ctxt->seg_override = VCPU_SREG_FS;
558 ++ break;
559 + case 0x65: /* GS override */
560 + has_seg_override = true;
561 +- ctxt->seg_override = ctxt->b & 7;
562 ++ ctxt->seg_override = VCPU_SREG_GS;
563 + break;
564 + case 0x40 ... 0x4f: /* REX */
565 + if (mode != X86EMUL_MODE_PROT64)
566 +@@ -5146,10 +5159,15 @@ done_prefixes:
567 + }
568 + break;
569 + case Escape:
570 +- if (ctxt->modrm > 0xbf)
571 +- opcode = opcode.u.esc->high[ctxt->modrm - 0xc0];
572 +- else
573 ++ if (ctxt->modrm > 0xbf) {
574 ++ size_t size = ARRAY_SIZE(opcode.u.esc->high);
575 ++ u32 index = array_index_nospec(
576 ++ ctxt->modrm - 0xc0, size);
577 ++
578 ++ opcode = opcode.u.esc->high[index];
579 ++ } else {
580 + opcode = opcode.u.esc->op[(ctxt->modrm >> 3) & 7];
581 ++ }
582 + break;
583 + case InstrDual:
584 + if ((ctxt->modrm >> 6) == 3)
585 +diff --git a/arch/x86/kvm/hyperv.c b/arch/x86/kvm/hyperv.c
586 +index 42b1c83741c8..5e837c96e93f 100644
587 +--- a/arch/x86/kvm/hyperv.c
588 ++++ b/arch/x86/kvm/hyperv.c
589 +@@ -28,6 +28,7 @@
590 +
591 + #include <linux/kvm_host.h>
592 + #include <linux/highmem.h>
593 ++#include <linux/nospec.h>
594 + #include <asm/apicdef.h>
595 + #include <trace/events/kvm.h>
596 +
597 +@@ -719,11 +720,12 @@ static int kvm_hv_msr_get_crash_data(struct kvm_vcpu *vcpu,
598 + u32 index, u64 *pdata)
599 + {
600 + struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
601 ++ size_t size = ARRAY_SIZE(hv->hv_crash_param);
602 +
603 +- if (WARN_ON_ONCE(index >= ARRAY_SIZE(hv->hv_crash_param)))
604 ++ if (WARN_ON_ONCE(index >= size))
605 + return -EINVAL;
606 +
607 +- *pdata = hv->hv_crash_param[index];
608 ++ *pdata = hv->hv_crash_param[array_index_nospec(index, size)];
609 + return 0;
610 + }
611 +
612 +@@ -762,11 +764,12 @@ static int kvm_hv_msr_set_crash_data(struct kvm_vcpu *vcpu,
613 + u32 index, u64 data)
614 + {
615 + struct kvm_hv *hv = &vcpu->kvm->arch.hyperv;
616 ++ size_t size = ARRAY_SIZE(hv->hv_crash_param);
617 +
618 +- if (WARN_ON_ONCE(index >= ARRAY_SIZE(hv->hv_crash_param)))
619 ++ if (WARN_ON_ONCE(index >= size))
620 + return -EINVAL;
621 +
622 +- hv->hv_crash_param[index] = data;
623 ++ hv->hv_crash_param[array_index_nospec(index, size)] = data;
624 + return 0;
625 + }
626 +
627 +diff --git a/arch/x86/kvm/ioapic.c b/arch/x86/kvm/ioapic.c
628 +index 5f810bb80802..aa34b16e62c2 100644
629 +--- a/arch/x86/kvm/ioapic.c
630 ++++ b/arch/x86/kvm/ioapic.c
631 +@@ -36,6 +36,7 @@
632 + #include <linux/io.h>
633 + #include <linux/slab.h>
634 + #include <linux/export.h>
635 ++#include <linux/nospec.h>
636 + #include <asm/processor.h>
637 + #include <asm/page.h>
638 + #include <asm/current.h>
639 +@@ -73,13 +74,14 @@ static unsigned long ioapic_read_indirect(struct kvm_ioapic *ioapic,
640 + default:
641 + {
642 + u32 redir_index = (ioapic->ioregsel - 0x10) >> 1;
643 +- u64 redir_content;
644 ++ u64 redir_content = ~0ULL;
645 +
646 +- if (redir_index < IOAPIC_NUM_PINS)
647 +- redir_content =
648 +- ioapic->redirtbl[redir_index].bits;
649 +- else
650 +- redir_content = ~0ULL;
651 ++ if (redir_index < IOAPIC_NUM_PINS) {
652 ++ u32 index = array_index_nospec(
653 ++ redir_index, IOAPIC_NUM_PINS);
654 ++
655 ++ redir_content = ioapic->redirtbl[index].bits;
656 ++ }
657 +
658 + result = (ioapic->ioregsel & 0x1) ?
659 + (redir_content >> 32) & 0xffffffff :
660 +@@ -299,6 +301,7 @@ static void ioapic_write_indirect(struct kvm_ioapic *ioapic, u32 val)
661 + ioapic_debug("change redir index %x val %x\n", index, val);
662 + if (index >= IOAPIC_NUM_PINS)
663 + return;
664 ++ index = array_index_nospec(index, IOAPIC_NUM_PINS);
665 + e = &ioapic->redirtbl[index];
666 + mask_before = e->fields.mask;
667 + /* Preserve read-only fields */
668 +diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c
669 +index cf32533225bb..caa17f8d4221 100644
670 +--- a/arch/x86/kvm/lapic.c
671 ++++ b/arch/x86/kvm/lapic.c
672 +@@ -28,6 +28,7 @@
673 + #include <linux/export.h>
674 + #include <linux/math64.h>
675 + #include <linux/slab.h>
676 ++#include <linux/nospec.h>
677 + #include <asm/processor.h>
678 + #include <asm/msr.h>
679 + #include <asm/page.h>
680 +@@ -1587,15 +1588,20 @@ int kvm_lapic_reg_write(struct kvm_lapic *apic, u32 reg, u32 val)
681 + case APIC_LVTTHMR:
682 + case APIC_LVTPC:
683 + case APIC_LVT1:
684 +- case APIC_LVTERR:
685 ++ case APIC_LVTERR: {
686 + /* TODO: Check vector */
687 ++ size_t size;
688 ++ u32 index;
689 ++
690 + if (!kvm_apic_sw_enabled(apic))
691 + val |= APIC_LVT_MASKED;
692 +-
693 +- val &= apic_lvt_mask[(reg - APIC_LVTT) >> 4];
694 ++ size = ARRAY_SIZE(apic_lvt_mask);
695 ++ index = array_index_nospec(
696 ++ (reg - APIC_LVTT) >> 4, size);
697 ++ val &= apic_lvt_mask[index];
698 + kvm_lapic_set_reg(apic, reg, val);
699 +-
700 + break;
701 ++ }
702 +
703 + case APIC_LVTT:
704 + if (!kvm_apic_sw_enabled(apic))
705 +diff --git a/arch/x86/kvm/mtrr.c b/arch/x86/kvm/mtrr.c
706 +index 0149ac59c273..3e3016411020 100644
707 +--- a/arch/x86/kvm/mtrr.c
708 ++++ b/arch/x86/kvm/mtrr.c
709 +@@ -17,6 +17,7 @@
710 + */
711 +
712 + #include <linux/kvm_host.h>
713 ++#include <linux/nospec.h>
714 + #include <asm/mtrr.h>
715 +
716 + #include "cpuid.h"
717 +@@ -202,11 +203,15 @@ static bool fixed_msr_to_seg_unit(u32 msr, int *seg, int *unit)
718 + break;
719 + case MSR_MTRRfix16K_80000 ... MSR_MTRRfix16K_A0000:
720 + *seg = 1;
721 +- *unit = msr - MSR_MTRRfix16K_80000;
722 ++ *unit = array_index_nospec(
723 ++ msr - MSR_MTRRfix16K_80000,
724 ++ MSR_MTRRfix16K_A0000 - MSR_MTRRfix16K_80000 + 1);
725 + break;
726 + case MSR_MTRRfix4K_C0000 ... MSR_MTRRfix4K_F8000:
727 + *seg = 2;
728 +- *unit = msr - MSR_MTRRfix4K_C0000;
729 ++ *unit = array_index_nospec(
730 ++ msr - MSR_MTRRfix4K_C0000,
731 ++ MSR_MTRRfix4K_F8000 - MSR_MTRRfix4K_C0000 + 1);
732 + break;
733 + default:
734 + return false;
735 +diff --git a/arch/x86/kvm/pmu.h b/arch/x86/kvm/pmu.h
736 +index f96e1f962587..fbf3d25af765 100644
737 +--- a/arch/x86/kvm/pmu.h
738 ++++ b/arch/x86/kvm/pmu.h
739 +@@ -1,6 +1,8 @@
740 + #ifndef __KVM_X86_PMU_H
741 + #define __KVM_X86_PMU_H
742 +
743 ++#include <linux/nospec.h>
744 ++
745 + #define vcpu_to_pmu(vcpu) (&(vcpu)->arch.pmu)
746 + #define pmu_to_vcpu(pmu) (container_of((pmu), struct kvm_vcpu, arch.pmu))
747 + #define pmc_to_pmu(pmc) (&(pmc)->vcpu->arch.pmu)
748 +@@ -80,8 +82,12 @@ static inline bool pmc_is_enabled(struct kvm_pmc *pmc)
749 + static inline struct kvm_pmc *get_gp_pmc(struct kvm_pmu *pmu, u32 msr,
750 + u32 base)
751 + {
752 +- if (msr >= base && msr < base + pmu->nr_arch_gp_counters)
753 +- return &pmu->gp_counters[msr - base];
754 ++ if (msr >= base && msr < base + pmu->nr_arch_gp_counters) {
755 ++ u32 index = array_index_nospec(msr - base,
756 ++ pmu->nr_arch_gp_counters);
757 ++
758 ++ return &pmu->gp_counters[index];
759 ++ }
760 +
761 + return NULL;
762 + }
763 +@@ -91,8 +97,12 @@ static inline struct kvm_pmc *get_fixed_pmc(struct kvm_pmu *pmu, u32 msr)
764 + {
765 + int base = MSR_CORE_PERF_FIXED_CTR0;
766 +
767 +- if (msr >= base && msr < base + pmu->nr_arch_fixed_counters)
768 +- return &pmu->fixed_counters[msr - base];
769 ++ if (msr >= base && msr < base + pmu->nr_arch_fixed_counters) {
770 ++ u32 index = array_index_nospec(msr - base,
771 ++ pmu->nr_arch_fixed_counters);
772 ++
773 ++ return &pmu->fixed_counters[index];
774 ++ }
775 +
776 + return NULL;
777 + }
778 +diff --git a/arch/x86/kvm/pmu_intel.c b/arch/x86/kvm/pmu_intel.c
779 +index 2729131fe9bf..84ae4dd261ca 100644
780 +--- a/arch/x86/kvm/pmu_intel.c
781 ++++ b/arch/x86/kvm/pmu_intel.c
782 +@@ -87,10 +87,14 @@ static unsigned intel_find_arch_event(struct kvm_pmu *pmu,
783 +
784 + static unsigned intel_find_fixed_event(int idx)
785 + {
786 +- if (idx >= ARRAY_SIZE(fixed_pmc_events))
787 ++ u32 event;
788 ++ size_t size = ARRAY_SIZE(fixed_pmc_events);
789 ++
790 ++ if (idx >= size)
791 + return PERF_COUNT_HW_MAX;
792 +
793 +- return intel_arch_events[fixed_pmc_events[idx]].event_type;
794 ++ event = fixed_pmc_events[array_index_nospec(idx, size)];
795 ++ return intel_arch_events[event].event_type;
796 + }
797 +
798 + /* check if a PMC is enabled by comparing it with globl_ctrl bits. */
799 +@@ -131,15 +135,19 @@ static struct kvm_pmc *intel_msr_idx_to_pmc(struct kvm_vcpu *vcpu,
800 + struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
801 + bool fixed = idx & (1u << 30);
802 + struct kvm_pmc *counters;
803 ++ unsigned int num_counters;
804 +
805 + idx &= ~(3u << 30);
806 +- if (!fixed && idx >= pmu->nr_arch_gp_counters)
807 +- return NULL;
808 +- if (fixed && idx >= pmu->nr_arch_fixed_counters)
809 ++ if (fixed) {
810 ++ counters = pmu->fixed_counters;
811 ++ num_counters = pmu->nr_arch_fixed_counters;
812 ++ } else {
813 ++ counters = pmu->gp_counters;
814 ++ num_counters = pmu->nr_arch_gp_counters;
815 ++ }
816 ++ if (idx >= num_counters)
817 + return NULL;
818 +- counters = fixed ? pmu->fixed_counters : pmu->gp_counters;
819 +-
820 +- return &counters[idx];
821 ++ return &counters[array_index_nospec(idx, num_counters)];
822 + }
823 +
824 + static bool intel_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr)
825 +diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c
826 +index f76caa03f4f8..67cdb08a736f 100644
827 +--- a/arch/x86/kvm/vmx.c
828 ++++ b/arch/x86/kvm/vmx.c
829 +@@ -7653,8 +7653,10 @@ static int handle_vmread(struct kvm_vcpu *vcpu)
830 + /* _system ok, as nested_vmx_check_permission verified cpl=0 */
831 + if (kvm_write_guest_virt_system(vcpu, gva, &field_value,
832 + (is_long_mode(vcpu) ? 8 : 4),
833 +- &e))
834 ++ &e)) {
835 + kvm_inject_page_fault(vcpu, &e);
836 ++ return 1;
837 ++ }
838 + }
839 +
840 + nested_vmx_succeed(vcpu);
841 +diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
842 +new file mode 100644
843 +index 000000000000..3791ce8d269e
844 +--- /dev/null
845 ++++ b/arch/x86/kvm/vmx/vmx.c
846 +@@ -0,0 +1,8033 @@
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 ++ if (cpu_has_vmx_ept_5levels() && (cpuid_maxphyaddr(vcpu) > 48))
3818 ++ return 5;
3819 ++ return 4;
3820 ++}
3821 ++
3822 ++u64 construct_eptp(struct kvm_vcpu *vcpu, unsigned long root_hpa)
3823 ++{
3824 ++ u64 eptp = VMX_EPTP_MT_WB;
3825 ++
3826 ++ eptp |= (get_ept_level(vcpu) == 5) ? VMX_EPTP_PWL_5 : VMX_EPTP_PWL_4;
3827 ++
3828 ++ if (enable_ept_ad_bits &&
3829 ++ (!is_guest_mode(vcpu) || nested_ept_ad_enabled(vcpu)))
3830 ++ eptp |= VMX_EPTP_AD_ENABLE_BIT;
3831 ++ eptp |= (root_hpa & PAGE_MASK);
3832 ++
3833 ++ return eptp;
3834 ++}
3835 ++
3836 ++void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
3837 ++{
3838 ++ struct kvm *kvm = vcpu->kvm;
3839 ++ bool update_guest_cr3 = true;
3840 ++ unsigned long guest_cr3;
3841 ++ u64 eptp;
3842 ++
3843 ++ guest_cr3 = cr3;
3844 ++ if (enable_ept) {
3845 ++ eptp = construct_eptp(vcpu, cr3);
3846 ++ vmcs_write64(EPT_POINTER, eptp);
3847 ++
3848 ++ if (kvm_x86_ops->tlb_remote_flush) {
3849 ++ spin_lock(&to_kvm_vmx(kvm)->ept_pointer_lock);
3850 ++ to_vmx(vcpu)->ept_pointer = eptp;
3851 ++ to_kvm_vmx(kvm)->ept_pointers_match
3852 ++ = EPT_POINTERS_CHECK;
3853 ++ spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
3854 ++ }
3855 ++
3856 ++ /* Loading vmcs02.GUEST_CR3 is handled by nested VM-Enter. */
3857 ++ if (is_guest_mode(vcpu))
3858 ++ update_guest_cr3 = false;
3859 ++ else if (!enable_unrestricted_guest && !is_paging(vcpu))
3860 ++ guest_cr3 = to_kvm_vmx(kvm)->ept_identity_map_addr;
3861 ++ else if (test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
3862 ++ guest_cr3 = vcpu->arch.cr3;
3863 ++ else /* vmcs01.GUEST_CR3 is already up-to-date. */
3864 ++ update_guest_cr3 = false;
3865 ++ ept_load_pdptrs(vcpu);
3866 ++ }
3867 ++
3868 ++ if (update_guest_cr3)
3869 ++ vmcs_writel(GUEST_CR3, guest_cr3);
3870 ++}
3871 ++
3872 ++int vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
3873 ++{
3874 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
3875 ++ /*
3876 ++ * Pass through host's Machine Check Enable value to hw_cr4, which
3877 ++ * is in force while we are in guest mode. Do not let guests control
3878 ++ * this bit, even if host CR4.MCE == 0.
3879 ++ */
3880 ++ unsigned long hw_cr4;
3881 ++
3882 ++ hw_cr4 = (cr4_read_shadow() & X86_CR4_MCE) | (cr4 & ~X86_CR4_MCE);
3883 ++ if (enable_unrestricted_guest)
3884 ++ hw_cr4 |= KVM_VM_CR4_ALWAYS_ON_UNRESTRICTED_GUEST;
3885 ++ else if (vmx->rmode.vm86_active)
3886 ++ hw_cr4 |= KVM_RMODE_VM_CR4_ALWAYS_ON;
3887 ++ else
3888 ++ hw_cr4 |= KVM_PMODE_VM_CR4_ALWAYS_ON;
3889 ++
3890 ++ if (!boot_cpu_has(X86_FEATURE_UMIP) && vmx_umip_emulated()) {
3891 ++ if (cr4 & X86_CR4_UMIP) {
3892 ++ secondary_exec_controls_setbit(vmx, SECONDARY_EXEC_DESC);
3893 ++ hw_cr4 &= ~X86_CR4_UMIP;
3894 ++ } else if (!is_guest_mode(vcpu) ||
3895 ++ !nested_cpu_has2(get_vmcs12(vcpu), SECONDARY_EXEC_DESC)) {
3896 ++ secondary_exec_controls_clearbit(vmx, SECONDARY_EXEC_DESC);
3897 ++ }
3898 ++ }
3899 ++
3900 ++ if (cr4 & X86_CR4_VMXE) {
3901 ++ /*
3902 ++ * To use VMXON (and later other VMX instructions), a guest
3903 ++ * must first be able to turn on cr4.VMXE (see handle_vmon()).
3904 ++ * So basically the check on whether to allow nested VMX
3905 ++ * is here. We operate under the default treatment of SMM,
3906 ++ * so VMX cannot be enabled under SMM.
3907 ++ */
3908 ++ if (!nested_vmx_allowed(vcpu) || is_smm(vcpu))
3909 ++ return 1;
3910 ++ }
3911 ++
3912 ++ if (vmx->nested.vmxon && !nested_cr4_valid(vcpu, cr4))
3913 ++ return 1;
3914 ++
3915 ++ vcpu->arch.cr4 = cr4;
3916 ++
3917 ++ if (!enable_unrestricted_guest) {
3918 ++ if (enable_ept) {
3919 ++ if (!is_paging(vcpu)) {
3920 ++ hw_cr4 &= ~X86_CR4_PAE;
3921 ++ hw_cr4 |= X86_CR4_PSE;
3922 ++ } else if (!(cr4 & X86_CR4_PAE)) {
3923 ++ hw_cr4 &= ~X86_CR4_PAE;
3924 ++ }
3925 ++ }
3926 ++
3927 ++ /*
3928 ++ * SMEP/SMAP/PKU is disabled if CPU is in non-paging mode in
3929 ++ * hardware. To emulate this behavior, SMEP/SMAP/PKU needs
3930 ++ * to be manually disabled when guest switches to non-paging
3931 ++ * mode.
3932 ++ *
3933 ++ * If !enable_unrestricted_guest, the CPU is always running
3934 ++ * with CR0.PG=1 and CR4 needs to be modified.
3935 ++ * If enable_unrestricted_guest, the CPU automatically
3936 ++ * disables SMEP/SMAP/PKU when the guest sets CR0.PG=0.
3937 ++ */
3938 ++ if (!is_paging(vcpu))
3939 ++ hw_cr4 &= ~(X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE);
3940 ++ }
3941 ++
3942 ++ vmcs_writel(CR4_READ_SHADOW, cr4);
3943 ++ vmcs_writel(GUEST_CR4, hw_cr4);
3944 ++ return 0;
3945 ++}
3946 ++
3947 ++void vmx_get_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg)
3948 ++{
3949 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
3950 ++ u32 ar;
3951 ++
3952 ++ if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
3953 ++ *var = vmx->rmode.segs[seg];
3954 ++ if (seg == VCPU_SREG_TR
3955 ++ || var->selector == vmx_read_guest_seg_selector(vmx, seg))
3956 ++ return;
3957 ++ var->base = vmx_read_guest_seg_base(vmx, seg);
3958 ++ var->selector = vmx_read_guest_seg_selector(vmx, seg);
3959 ++ return;
3960 ++ }
3961 ++ var->base = vmx_read_guest_seg_base(vmx, seg);
3962 ++ var->limit = vmx_read_guest_seg_limit(vmx, seg);
3963 ++ var->selector = vmx_read_guest_seg_selector(vmx, seg);
3964 ++ ar = vmx_read_guest_seg_ar(vmx, seg);
3965 ++ var->unusable = (ar >> 16) & 1;
3966 ++ var->type = ar & 15;
3967 ++ var->s = (ar >> 4) & 1;
3968 ++ var->dpl = (ar >> 5) & 3;
3969 ++ /*
3970 ++ * Some userspaces do not preserve unusable property. Since usable
3971 ++ * segment has to be present according to VMX spec we can use present
3972 ++ * property to amend userspace bug by making unusable segment always
3973 ++ * nonpresent. vmx_segment_access_rights() already marks nonpresent
3974 ++ * segment as unusable.
3975 ++ */
3976 ++ var->present = !var->unusable;
3977 ++ var->avl = (ar >> 12) & 1;
3978 ++ var->l = (ar >> 13) & 1;
3979 ++ var->db = (ar >> 14) & 1;
3980 ++ var->g = (ar >> 15) & 1;
3981 ++}
3982 ++
3983 ++static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg)
3984 ++{
3985 ++ struct kvm_segment s;
3986 ++
3987 ++ if (to_vmx(vcpu)->rmode.vm86_active) {
3988 ++ vmx_get_segment(vcpu, &s, seg);
3989 ++ return s.base;
3990 ++ }
3991 ++ return vmx_read_guest_seg_base(to_vmx(vcpu), seg);
3992 ++}
3993 ++
3994 ++int vmx_get_cpl(struct kvm_vcpu *vcpu)
3995 ++{
3996 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
3997 ++
3998 ++ if (unlikely(vmx->rmode.vm86_active))
3999 ++ return 0;
4000 ++ else {
4001 ++ int ar = vmx_read_guest_seg_ar(vmx, VCPU_SREG_SS);
4002 ++ return VMX_AR_DPL(ar);
4003 ++ }
4004 ++}
4005 ++
4006 ++static u32 vmx_segment_access_rights(struct kvm_segment *var)
4007 ++{
4008 ++ u32 ar;
4009 ++
4010 ++ if (var->unusable || !var->present)
4011 ++ ar = 1 << 16;
4012 ++ else {
4013 ++ ar = var->type & 15;
4014 ++ ar |= (var->s & 1) << 4;
4015 ++ ar |= (var->dpl & 3) << 5;
4016 ++ ar |= (var->present & 1) << 7;
4017 ++ ar |= (var->avl & 1) << 12;
4018 ++ ar |= (var->l & 1) << 13;
4019 ++ ar |= (var->db & 1) << 14;
4020 ++ ar |= (var->g & 1) << 15;
4021 ++ }
4022 ++
4023 ++ return ar;
4024 ++}
4025 ++
4026 ++void vmx_set_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg)
4027 ++{
4028 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
4029 ++ const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
4030 ++
4031 ++ vmx_segment_cache_clear(vmx);
4032 ++
4033 ++ if (vmx->rmode.vm86_active && seg != VCPU_SREG_LDTR) {
4034 ++ vmx->rmode.segs[seg] = *var;
4035 ++ if (seg == VCPU_SREG_TR)
4036 ++ vmcs_write16(sf->selector, var->selector);
4037 ++ else if (var->s)
4038 ++ fix_rmode_seg(seg, &vmx->rmode.segs[seg]);
4039 ++ goto out;
4040 ++ }
4041 ++
4042 ++ vmcs_writel(sf->base, var->base);
4043 ++ vmcs_write32(sf->limit, var->limit);
4044 ++ vmcs_write16(sf->selector, var->selector);
4045 ++
4046 ++ /*
4047 ++ * Fix the "Accessed" bit in AR field of segment registers for older
4048 ++ * qemu binaries.
4049 ++ * IA32 arch specifies that at the time of processor reset the
4050 ++ * "Accessed" bit in the AR field of segment registers is 1. And qemu
4051 ++ * is setting it to 0 in the userland code. This causes invalid guest
4052 ++ * state vmexit when "unrestricted guest" mode is turned on.
4053 ++ * Fix for this setup issue in cpu_reset is being pushed in the qemu
4054 ++ * tree. Newer qemu binaries with that qemu fix would not need this
4055 ++ * kvm hack.
4056 ++ */
4057 ++ if (enable_unrestricted_guest && (seg != VCPU_SREG_LDTR))
4058 ++ var->type |= 0x1; /* Accessed */
4059 ++
4060 ++ vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var));
4061 ++
4062 ++out:
4063 ++ vmx->emulation_required = emulation_required(vcpu);
4064 ++}
4065 ++
4066 ++static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
4067 ++{
4068 ++ u32 ar = vmx_read_guest_seg_ar(to_vmx(vcpu), VCPU_SREG_CS);
4069 ++
4070 ++ *db = (ar >> 14) & 1;
4071 ++ *l = (ar >> 13) & 1;
4072 ++}
4073 ++
4074 ++static void vmx_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
4075 ++{
4076 ++ dt->size = vmcs_read32(GUEST_IDTR_LIMIT);
4077 ++ dt->address = vmcs_readl(GUEST_IDTR_BASE);
4078 ++}
4079 ++
4080 ++static void vmx_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
4081 ++{
4082 ++ vmcs_write32(GUEST_IDTR_LIMIT, dt->size);
4083 ++ vmcs_writel(GUEST_IDTR_BASE, dt->address);
4084 ++}
4085 ++
4086 ++static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
4087 ++{
4088 ++ dt->size = vmcs_read32(GUEST_GDTR_LIMIT);
4089 ++ dt->address = vmcs_readl(GUEST_GDTR_BASE);
4090 ++}
4091 ++
4092 ++static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
4093 ++{
4094 ++ vmcs_write32(GUEST_GDTR_LIMIT, dt->size);
4095 ++ vmcs_writel(GUEST_GDTR_BASE, dt->address);
4096 ++}
4097 ++
4098 ++static bool rmode_segment_valid(struct kvm_vcpu *vcpu, int seg)
4099 ++{
4100 ++ struct kvm_segment var;
4101 ++ u32 ar;
4102 ++
4103 ++ vmx_get_segment(vcpu, &var, seg);
4104 ++ var.dpl = 0x3;
4105 ++ if (seg == VCPU_SREG_CS)
4106 ++ var.type = 0x3;
4107 ++ ar = vmx_segment_access_rights(&var);
4108 ++
4109 ++ if (var.base != (var.selector << 4))
4110 ++ return false;
4111 ++ if (var.limit != 0xffff)
4112 ++ return false;
4113 ++ if (ar != 0xf3)
4114 ++ return false;
4115 ++
4116 ++ return true;
4117 ++}
4118 ++
4119 ++static bool code_segment_valid(struct kvm_vcpu *vcpu)
4120 ++{
4121 ++ struct kvm_segment cs;
4122 ++ unsigned int cs_rpl;
4123 ++
4124 ++ vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
4125 ++ cs_rpl = cs.selector & SEGMENT_RPL_MASK;
4126 ++
4127 ++ if (cs.unusable)
4128 ++ return false;
4129 ++ if (~cs.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_ACCESSES_MASK))
4130 ++ return false;
4131 ++ if (!cs.s)
4132 ++ return false;
4133 ++ if (cs.type & VMX_AR_TYPE_WRITEABLE_MASK) {
4134 ++ if (cs.dpl > cs_rpl)
4135 ++ return false;
4136 ++ } else {
4137 ++ if (cs.dpl != cs_rpl)
4138 ++ return false;
4139 ++ }
4140 ++ if (!cs.present)
4141 ++ return false;
4142 ++
4143 ++ /* TODO: Add Reserved field check, this'll require a new member in the kvm_segment_field structure */
4144 ++ return true;
4145 ++}
4146 ++
4147 ++static bool stack_segment_valid(struct kvm_vcpu *vcpu)
4148 ++{
4149 ++ struct kvm_segment ss;
4150 ++ unsigned int ss_rpl;
4151 ++
4152 ++ vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
4153 ++ ss_rpl = ss.selector & SEGMENT_RPL_MASK;
4154 ++
4155 ++ if (ss.unusable)
4156 ++ return true;
4157 ++ if (ss.type != 3 && ss.type != 7)
4158 ++ return false;
4159 ++ if (!ss.s)
4160 ++ return false;
4161 ++ if (ss.dpl != ss_rpl) /* DPL != RPL */
4162 ++ return false;
4163 ++ if (!ss.present)
4164 ++ return false;
4165 ++
4166 ++ return true;
4167 ++}
4168 ++
4169 ++static bool data_segment_valid(struct kvm_vcpu *vcpu, int seg)
4170 ++{
4171 ++ struct kvm_segment var;
4172 ++ unsigned int rpl;
4173 ++
4174 ++ vmx_get_segment(vcpu, &var, seg);
4175 ++ rpl = var.selector & SEGMENT_RPL_MASK;
4176 ++
4177 ++ if (var.unusable)
4178 ++ return true;
4179 ++ if (!var.s)
4180 ++ return false;
4181 ++ if (!var.present)
4182 ++ return false;
4183 ++ if (~var.type & (VMX_AR_TYPE_CODE_MASK|VMX_AR_TYPE_WRITEABLE_MASK)) {
4184 ++ if (var.dpl < rpl) /* DPL < RPL */
4185 ++ return false;
4186 ++ }
4187 ++
4188 ++ /* TODO: Add other members to kvm_segment_field to allow checking for other access
4189 ++ * rights flags
4190 ++ */
4191 ++ return true;
4192 ++}
4193 ++
4194 ++static bool tr_valid(struct kvm_vcpu *vcpu)
4195 ++{
4196 ++ struct kvm_segment tr;
4197 ++
4198 ++ vmx_get_segment(vcpu, &tr, VCPU_SREG_TR);
4199 ++
4200 ++ if (tr.unusable)
4201 ++ return false;
4202 ++ if (tr.selector & SEGMENT_TI_MASK) /* TI = 1 */
4203 ++ return false;
4204 ++ if (tr.type != 3 && tr.type != 11) /* TODO: Check if guest is in IA32e mode */
4205 ++ return false;
4206 ++ if (!tr.present)
4207 ++ return false;
4208 ++
4209 ++ return true;
4210 ++}
4211 ++
4212 ++static bool ldtr_valid(struct kvm_vcpu *vcpu)
4213 ++{
4214 ++ struct kvm_segment ldtr;
4215 ++
4216 ++ vmx_get_segment(vcpu, &ldtr, VCPU_SREG_LDTR);
4217 ++
4218 ++ if (ldtr.unusable)
4219 ++ return true;
4220 ++ if (ldtr.selector & SEGMENT_TI_MASK) /* TI = 1 */
4221 ++ return false;
4222 ++ if (ldtr.type != 2)
4223 ++ return false;
4224 ++ if (!ldtr.present)
4225 ++ return false;
4226 ++
4227 ++ return true;
4228 ++}
4229 ++
4230 ++static bool cs_ss_rpl_check(struct kvm_vcpu *vcpu)
4231 ++{
4232 ++ struct kvm_segment cs, ss;
4233 ++
4234 ++ vmx_get_segment(vcpu, &cs, VCPU_SREG_CS);
4235 ++ vmx_get_segment(vcpu, &ss, VCPU_SREG_SS);
4236 ++
4237 ++ return ((cs.selector & SEGMENT_RPL_MASK) ==
4238 ++ (ss.selector & SEGMENT_RPL_MASK));
4239 ++}
4240 ++
4241 ++/*
4242 ++ * Check if guest state is valid. Returns true if valid, false if
4243 ++ * not.
4244 ++ * We assume that registers are always usable
4245 ++ */
4246 ++static bool guest_state_valid(struct kvm_vcpu *vcpu)
4247 ++{
4248 ++ if (enable_unrestricted_guest)
4249 ++ return true;
4250 ++
4251 ++ /* real mode guest state checks */
4252 ++ if (!is_protmode(vcpu) || (vmx_get_rflags(vcpu) & X86_EFLAGS_VM)) {
4253 ++ if (!rmode_segment_valid(vcpu, VCPU_SREG_CS))
4254 ++ return false;
4255 ++ if (!rmode_segment_valid(vcpu, VCPU_SREG_SS))
4256 ++ return false;
4257 ++ if (!rmode_segment_valid(vcpu, VCPU_SREG_DS))
4258 ++ return false;
4259 ++ if (!rmode_segment_valid(vcpu, VCPU_SREG_ES))
4260 ++ return false;
4261 ++ if (!rmode_segment_valid(vcpu, VCPU_SREG_FS))
4262 ++ return false;
4263 ++ if (!rmode_segment_valid(vcpu, VCPU_SREG_GS))
4264 ++ return false;
4265 ++ } else {
4266 ++ /* protected mode guest state checks */
4267 ++ if (!cs_ss_rpl_check(vcpu))
4268 ++ return false;
4269 ++ if (!code_segment_valid(vcpu))
4270 ++ return false;
4271 ++ if (!stack_segment_valid(vcpu))
4272 ++ return false;
4273 ++ if (!data_segment_valid(vcpu, VCPU_SREG_DS))
4274 ++ return false;
4275 ++ if (!data_segment_valid(vcpu, VCPU_SREG_ES))
4276 ++ return false;
4277 ++ if (!data_segment_valid(vcpu, VCPU_SREG_FS))
4278 ++ return false;
4279 ++ if (!data_segment_valid(vcpu, VCPU_SREG_GS))
4280 ++ return false;
4281 ++ if (!tr_valid(vcpu))
4282 ++ return false;
4283 ++ if (!ldtr_valid(vcpu))
4284 ++ return false;
4285 ++ }
4286 ++ /* TODO:
4287 ++ * - Add checks on RIP
4288 ++ * - Add checks on RFLAGS
4289 ++ */
4290 ++
4291 ++ return true;
4292 ++}
4293 ++
4294 ++static int init_rmode_tss(struct kvm *kvm)
4295 ++{
4296 ++ gfn_t fn;
4297 ++ u16 data = 0;
4298 ++ int idx, r;
4299 ++
4300 ++ idx = srcu_read_lock(&kvm->srcu);
4301 ++ fn = to_kvm_vmx(kvm)->tss_addr >> PAGE_SHIFT;
4302 ++ r = kvm_clear_guest_page(kvm, fn, 0, PAGE_SIZE);
4303 ++ if (r < 0)
4304 ++ goto out;
4305 ++ data = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE;
4306 ++ r = kvm_write_guest_page(kvm, fn++, &data,
4307 ++ TSS_IOPB_BASE_OFFSET, sizeof(u16));
4308 ++ if (r < 0)
4309 ++ goto out;
4310 ++ r = kvm_clear_guest_page(kvm, fn++, 0, PAGE_SIZE);
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 ++ data = ~0;
4317 ++ r = kvm_write_guest_page(kvm, fn, &data,
4318 ++ RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1,
4319 ++ sizeof(u8));
4320 ++out:
4321 ++ srcu_read_unlock(&kvm->srcu, idx);
4322 ++ return r;
4323 ++}
4324 ++
4325 ++static int init_rmode_identity_map(struct kvm *kvm)
4326 ++{
4327 ++ struct kvm_vmx *kvm_vmx = to_kvm_vmx(kvm);
4328 ++ int i, idx, r = 0;
4329 ++ kvm_pfn_t identity_map_pfn;
4330 ++ u32 tmp;
4331 ++
4332 ++ /* Protect kvm_vmx->ept_identity_pagetable_done. */
4333 ++ mutex_lock(&kvm->slots_lock);
4334 ++
4335 ++ if (likely(kvm_vmx->ept_identity_pagetable_done))
4336 ++ goto out2;
4337 ++
4338 ++ if (!kvm_vmx->ept_identity_map_addr)
4339 ++ kvm_vmx->ept_identity_map_addr = VMX_EPT_IDENTITY_PAGETABLE_ADDR;
4340 ++ identity_map_pfn = kvm_vmx->ept_identity_map_addr >> PAGE_SHIFT;
4341 ++
4342 ++ r = __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT,
4343 ++ kvm_vmx->ept_identity_map_addr, PAGE_SIZE);
4344 ++ if (r < 0)
4345 ++ goto out2;
4346 ++
4347 ++ idx = srcu_read_lock(&kvm->srcu);
4348 ++ r = kvm_clear_guest_page(kvm, identity_map_pfn, 0, PAGE_SIZE);
4349 ++ if (r < 0)
4350 ++ goto out;
4351 ++ /* Set up identity-mapping pagetable for EPT in real mode */
4352 ++ for (i = 0; i < PT32_ENT_PER_PAGE; i++) {
4353 ++ tmp = (i << 22) + (_PAGE_PRESENT | _PAGE_RW | _PAGE_USER |
4354 ++ _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_PSE);
4355 ++ r = kvm_write_guest_page(kvm, identity_map_pfn,
4356 ++ &tmp, i * sizeof(tmp), sizeof(tmp));
4357 ++ if (r < 0)
4358 ++ goto out;
4359 ++ }
4360 ++ kvm_vmx->ept_identity_pagetable_done = true;
4361 ++
4362 ++out:
4363 ++ srcu_read_unlock(&kvm->srcu, idx);
4364 ++
4365 ++out2:
4366 ++ mutex_unlock(&kvm->slots_lock);
4367 ++ return r;
4368 ++}
4369 ++
4370 ++static void seg_setup(int seg)
4371 ++{
4372 ++ const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg];
4373 ++ unsigned int ar;
4374 ++
4375 ++ vmcs_write16(sf->selector, 0);
4376 ++ vmcs_writel(sf->base, 0);
4377 ++ vmcs_write32(sf->limit, 0xffff);
4378 ++ ar = 0x93;
4379 ++ if (seg == VCPU_SREG_CS)
4380 ++ ar |= 0x08; /* code segment */
4381 ++
4382 ++ vmcs_write32(sf->ar_bytes, ar);
4383 ++}
4384 ++
4385 ++static int alloc_apic_access_page(struct kvm *kvm)
4386 ++{
4387 ++ struct page *page;
4388 ++ int r = 0;
4389 ++
4390 ++ mutex_lock(&kvm->slots_lock);
4391 ++ if (kvm->arch.apic_access_page_done)
4392 ++ goto out;
4393 ++ r = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
4394 ++ APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
4395 ++ if (r)
4396 ++ goto out;
4397 ++
4398 ++ page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
4399 ++ if (is_error_page(page)) {
4400 ++ r = -EFAULT;
4401 ++ goto out;
4402 ++ }
4403 ++
4404 ++ /*
4405 ++ * Do not pin the page in memory, so that memory hot-unplug
4406 ++ * is able to migrate it.
4407 ++ */
4408 ++ put_page(page);
4409 ++ kvm->arch.apic_access_page_done = true;
4410 ++out:
4411 ++ mutex_unlock(&kvm->slots_lock);
4412 ++ return r;
4413 ++}
4414 ++
4415 ++int allocate_vpid(void)
4416 ++{
4417 ++ int vpid;
4418 ++
4419 ++ if (!enable_vpid)
4420 ++ return 0;
4421 ++ spin_lock(&vmx_vpid_lock);
4422 ++ vpid = find_first_zero_bit(vmx_vpid_bitmap, VMX_NR_VPIDS);
4423 ++ if (vpid < VMX_NR_VPIDS)
4424 ++ __set_bit(vpid, vmx_vpid_bitmap);
4425 ++ else
4426 ++ vpid = 0;
4427 ++ spin_unlock(&vmx_vpid_lock);
4428 ++ return vpid;
4429 ++}
4430 ++
4431 ++void free_vpid(int vpid)
4432 ++{
4433 ++ if (!enable_vpid || vpid == 0)
4434 ++ return;
4435 ++ spin_lock(&vmx_vpid_lock);
4436 ++ __clear_bit(vpid, vmx_vpid_bitmap);
4437 ++ spin_unlock(&vmx_vpid_lock);
4438 ++}
4439 ++
4440 ++static __always_inline void vmx_disable_intercept_for_msr(unsigned long *msr_bitmap,
4441 ++ u32 msr, int type)
4442 ++{
4443 ++ int f = sizeof(unsigned long);
4444 ++
4445 ++ if (!cpu_has_vmx_msr_bitmap())
4446 ++ return;
4447 ++
4448 ++ if (static_branch_unlikely(&enable_evmcs))
4449 ++ evmcs_touch_msr_bitmap();
4450 ++
4451 ++ /*
4452 ++ * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4453 ++ * have the write-low and read-high bitmap offsets the wrong way round.
4454 ++ * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4455 ++ */
4456 ++ if (msr <= 0x1fff) {
4457 ++ if (type & MSR_TYPE_R)
4458 ++ /* read-low */
4459 ++ __clear_bit(msr, msr_bitmap + 0x000 / f);
4460 ++
4461 ++ if (type & MSR_TYPE_W)
4462 ++ /* write-low */
4463 ++ __clear_bit(msr, msr_bitmap + 0x800 / f);
4464 ++
4465 ++ } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4466 ++ msr &= 0x1fff;
4467 ++ if (type & MSR_TYPE_R)
4468 ++ /* read-high */
4469 ++ __clear_bit(msr, msr_bitmap + 0x400 / f);
4470 ++
4471 ++ if (type & MSR_TYPE_W)
4472 ++ /* write-high */
4473 ++ __clear_bit(msr, msr_bitmap + 0xc00 / f);
4474 ++
4475 ++ }
4476 ++}
4477 ++
4478 ++static __always_inline void vmx_enable_intercept_for_msr(unsigned long *msr_bitmap,
4479 ++ u32 msr, int type)
4480 ++{
4481 ++ int f = sizeof(unsigned long);
4482 ++
4483 ++ if (!cpu_has_vmx_msr_bitmap())
4484 ++ return;
4485 ++
4486 ++ if (static_branch_unlikely(&enable_evmcs))
4487 ++ evmcs_touch_msr_bitmap();
4488 ++
4489 ++ /*
4490 ++ * See Intel PRM Vol. 3, 20.6.9 (MSR-Bitmap Address). Early manuals
4491 ++ * have the write-low and read-high bitmap offsets the wrong way round.
4492 ++ * We can control MSRs 0x00000000-0x00001fff and 0xc0000000-0xc0001fff.
4493 ++ */
4494 ++ if (msr <= 0x1fff) {
4495 ++ if (type & MSR_TYPE_R)
4496 ++ /* read-low */
4497 ++ __set_bit(msr, msr_bitmap + 0x000 / f);
4498 ++
4499 ++ if (type & MSR_TYPE_W)
4500 ++ /* write-low */
4501 ++ __set_bit(msr, msr_bitmap + 0x800 / f);
4502 ++
4503 ++ } else if ((msr >= 0xc0000000) && (msr <= 0xc0001fff)) {
4504 ++ msr &= 0x1fff;
4505 ++ if (type & MSR_TYPE_R)
4506 ++ /* read-high */
4507 ++ __set_bit(msr, msr_bitmap + 0x400 / f);
4508 ++
4509 ++ if (type & MSR_TYPE_W)
4510 ++ /* write-high */
4511 ++ __set_bit(msr, msr_bitmap + 0xc00 / f);
4512 ++
4513 ++ }
4514 ++}
4515 ++
4516 ++static __always_inline void vmx_set_intercept_for_msr(unsigned long *msr_bitmap,
4517 ++ u32 msr, int type, bool value)
4518 ++{
4519 ++ if (value)
4520 ++ vmx_enable_intercept_for_msr(msr_bitmap, msr, type);
4521 ++ else
4522 ++ vmx_disable_intercept_for_msr(msr_bitmap, msr, type);
4523 ++}
4524 ++
4525 ++static u8 vmx_msr_bitmap_mode(struct kvm_vcpu *vcpu)
4526 ++{
4527 ++ u8 mode = 0;
4528 ++
4529 ++ if (cpu_has_secondary_exec_ctrls() &&
4530 ++ (secondary_exec_controls_get(to_vmx(vcpu)) &
4531 ++ SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE)) {
4532 ++ mode |= MSR_BITMAP_MODE_X2APIC;
4533 ++ if (enable_apicv && kvm_vcpu_apicv_active(vcpu))
4534 ++ mode |= MSR_BITMAP_MODE_X2APIC_APICV;
4535 ++ }
4536 ++
4537 ++ return mode;
4538 ++}
4539 ++
4540 ++static void vmx_update_msr_bitmap_x2apic(unsigned long *msr_bitmap,
4541 ++ u8 mode)
4542 ++{
4543 ++ int msr;
4544 ++
4545 ++ for (msr = 0x800; msr <= 0x8ff; msr += BITS_PER_LONG) {
4546 ++ unsigned word = msr / BITS_PER_LONG;
4547 ++ msr_bitmap[word] = (mode & MSR_BITMAP_MODE_X2APIC_APICV) ? 0 : ~0;
4548 ++ msr_bitmap[word + (0x800 / sizeof(long))] = ~0;
4549 ++ }
4550 ++
4551 ++ if (mode & MSR_BITMAP_MODE_X2APIC) {
4552 ++ /*
4553 ++ * TPR reads and writes can be virtualized even if virtual interrupt
4554 ++ * delivery is not in use.
4555 ++ */
4556 ++ vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TASKPRI), MSR_TYPE_RW);
4557 ++ if (mode & MSR_BITMAP_MODE_X2APIC_APICV) {
4558 ++ vmx_enable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_TMCCT), MSR_TYPE_R);
4559 ++ vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_EOI), MSR_TYPE_W);
4560 ++ vmx_disable_intercept_for_msr(msr_bitmap, X2APIC_MSR(APIC_SELF_IPI), MSR_TYPE_W);
4561 ++ }
4562 ++ }
4563 ++}
4564 ++
4565 ++void vmx_update_msr_bitmap(struct kvm_vcpu *vcpu)
4566 ++{
4567 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
4568 ++ unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
4569 ++ u8 mode = vmx_msr_bitmap_mode(vcpu);
4570 ++ u8 changed = mode ^ vmx->msr_bitmap_mode;
4571 ++
4572 ++ if (!changed)
4573 ++ return;
4574 ++
4575 ++ if (changed & (MSR_BITMAP_MODE_X2APIC | MSR_BITMAP_MODE_X2APIC_APICV))
4576 ++ vmx_update_msr_bitmap_x2apic(msr_bitmap, mode);
4577 ++
4578 ++ vmx->msr_bitmap_mode = mode;
4579 ++}
4580 ++
4581 ++void pt_update_intercept_for_msr(struct vcpu_vmx *vmx)
4582 ++{
4583 ++ unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap;
4584 ++ bool flag = !(vmx->pt_desc.guest.ctl & RTIT_CTL_TRACEEN);
4585 ++ u32 i;
4586 ++
4587 ++ vmx_set_intercept_for_msr(msr_bitmap, MSR_IA32_RTIT_STATUS,
4588 ++ MSR_TYPE_RW, flag);
4589 ++ vmx_set_intercept_for_msr(msr_bitmap, MSR_IA32_RTIT_OUTPUT_BASE,
4590 ++ MSR_TYPE_RW, flag);
4591 ++ vmx_set_intercept_for_msr(msr_bitmap, MSR_IA32_RTIT_OUTPUT_MASK,
4592 ++ MSR_TYPE_RW, flag);
4593 ++ vmx_set_intercept_for_msr(msr_bitmap, MSR_IA32_RTIT_CR3_MATCH,
4594 ++ MSR_TYPE_RW, flag);
4595 ++ for (i = 0; i < vmx->pt_desc.addr_range; i++) {
4596 ++ vmx_set_intercept_for_msr(msr_bitmap,
4597 ++ MSR_IA32_RTIT_ADDR0_A + i * 2, MSR_TYPE_RW, flag);
4598 ++ vmx_set_intercept_for_msr(msr_bitmap,
4599 ++ MSR_IA32_RTIT_ADDR0_B + i * 2, MSR_TYPE_RW, flag);
4600 ++ }
4601 ++}
4602 ++
4603 ++static bool vmx_get_enable_apicv(struct kvm *kvm)
4604 ++{
4605 ++ return enable_apicv;
4606 ++}
4607 ++
4608 ++static bool vmx_guest_apic_has_interrupt(struct kvm_vcpu *vcpu)
4609 ++{
4610 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
4611 ++ void *vapic_page;
4612 ++ u32 vppr;
4613 ++ int rvi;
4614 ++
4615 ++ if (WARN_ON_ONCE(!is_guest_mode(vcpu)) ||
4616 ++ !nested_cpu_has_vid(get_vmcs12(vcpu)) ||
4617 ++ WARN_ON_ONCE(!vmx->nested.virtual_apic_map.gfn))
4618 ++ return false;
4619 ++
4620 ++ rvi = vmx_get_rvi();
4621 ++
4622 ++ vapic_page = vmx->nested.virtual_apic_map.hva;
4623 ++ vppr = *((u32 *)(vapic_page + APIC_PROCPRI));
4624 ++
4625 ++ return ((rvi & 0xf0) > (vppr & 0xf0));
4626 ++}
4627 ++
4628 ++static inline bool kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
4629 ++ bool nested)
4630 ++{
4631 ++#ifdef CONFIG_SMP
4632 ++ int pi_vec = nested ? POSTED_INTR_NESTED_VECTOR : POSTED_INTR_VECTOR;
4633 ++
4634 ++ if (vcpu->mode == IN_GUEST_MODE) {
4635 ++ /*
4636 ++ * The vector of interrupt to be delivered to vcpu had
4637 ++ * been set in PIR before this function.
4638 ++ *
4639 ++ * Following cases will be reached in this block, and
4640 ++ * we always send a notification event in all cases as
4641 ++ * explained below.
4642 ++ *
4643 ++ * Case 1: vcpu keeps in non-root mode. Sending a
4644 ++ * notification event posts the interrupt to vcpu.
4645 ++ *
4646 ++ * Case 2: vcpu exits to root mode and is still
4647 ++ * runnable. PIR will be synced to vIRR before the
4648 ++ * next vcpu entry. Sending a notification event in
4649 ++ * this case has no effect, as vcpu is not in root
4650 ++ * mode.
4651 ++ *
4652 ++ * Case 3: vcpu exits to root mode and is blocked.
4653 ++ * vcpu_block() has already synced PIR to vIRR and
4654 ++ * never blocks vcpu if vIRR is not cleared. Therefore,
4655 ++ * a blocked vcpu here does not wait for any requested
4656 ++ * interrupts in PIR, and sending a notification event
4657 ++ * which has no effect is safe here.
4658 ++ */
4659 ++
4660 ++ apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
4661 ++ return true;
4662 ++ }
4663 ++#endif
4664 ++ return false;
4665 ++}
4666 ++
4667 ++static int vmx_deliver_nested_posted_interrupt(struct kvm_vcpu *vcpu,
4668 ++ int vector)
4669 ++{
4670 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
4671 ++
4672 ++ if (is_guest_mode(vcpu) &&
4673 ++ vector == vmx->nested.posted_intr_nv) {
4674 ++ /*
4675 ++ * If a posted intr is not recognized by hardware,
4676 ++ * we will accomplish it in the next vmentry.
4677 ++ */
4678 ++ vmx->nested.pi_pending = true;
4679 ++ kvm_make_request(KVM_REQ_EVENT, vcpu);
4680 ++ /* the PIR and ON have been set by L1. */
4681 ++ if (!kvm_vcpu_trigger_posted_interrupt(vcpu, true))
4682 ++ kvm_vcpu_kick(vcpu);
4683 ++ return 0;
4684 ++ }
4685 ++ return -1;
4686 ++}
4687 ++/*
4688 ++ * Send interrupt to vcpu via posted interrupt way.
4689 ++ * 1. If target vcpu is running(non-root mode), send posted interrupt
4690 ++ * notification to vcpu and hardware will sync PIR to vIRR atomically.
4691 ++ * 2. If target vcpu isn't running(root mode), kick it to pick up the
4692 ++ * interrupt from PIR in next vmentry.
4693 ++ */
4694 ++static void vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu, int vector)
4695 ++{
4696 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
4697 ++ int r;
4698 ++
4699 ++ r = vmx_deliver_nested_posted_interrupt(vcpu, vector);
4700 ++ if (!r)
4701 ++ return;
4702 ++
4703 ++ if (pi_test_and_set_pir(vector, &vmx->pi_desc))
4704 ++ return;
4705 ++
4706 ++ /* If a previous notification has sent the IPI, nothing to do. */
4707 ++ if (pi_test_and_set_on(&vmx->pi_desc))
4708 ++ return;
4709 ++
4710 ++ if (!kvm_vcpu_trigger_posted_interrupt(vcpu, false))
4711 ++ kvm_vcpu_kick(vcpu);
4712 ++}
4713 ++
4714 ++/*
4715 ++ * Set up the vmcs's constant host-state fields, i.e., host-state fields that
4716 ++ * will not change in the lifetime of the guest.
4717 ++ * Note that host-state that does change is set elsewhere. E.g., host-state
4718 ++ * that is set differently for each CPU is set in vmx_vcpu_load(), not here.
4719 ++ */
4720 ++void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
4721 ++{
4722 ++ u32 low32, high32;
4723 ++ unsigned long tmpl;
4724 ++ unsigned long cr0, cr3, cr4;
4725 ++
4726 ++ cr0 = read_cr0();
4727 ++ WARN_ON(cr0 & X86_CR0_TS);
4728 ++ vmcs_writel(HOST_CR0, cr0); /* 22.2.3 */
4729 ++
4730 ++ /*
4731 ++ * Save the most likely value for this task's CR3 in the VMCS.
4732 ++ * We can't use __get_current_cr3_fast() because we're not atomic.
4733 ++ */
4734 ++ cr3 = __read_cr3();
4735 ++ vmcs_writel(HOST_CR3, cr3); /* 22.2.3 FIXME: shadow tables */
4736 ++ vmx->loaded_vmcs->host_state.cr3 = cr3;
4737 ++
4738 ++ /* Save the most likely value for this task's CR4 in the VMCS. */
4739 ++ cr4 = cr4_read_shadow();
4740 ++ vmcs_writel(HOST_CR4, cr4); /* 22.2.3, 22.2.5 */
4741 ++ vmx->loaded_vmcs->host_state.cr4 = cr4;
4742 ++
4743 ++ vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */
4744 ++#ifdef CONFIG_X86_64
4745 ++ /*
4746 ++ * Load null selectors, so we can avoid reloading them in
4747 ++ * vmx_prepare_switch_to_host(), in case userspace uses
4748 ++ * the null selectors too (the expected case).
4749 ++ */
4750 ++ vmcs_write16(HOST_DS_SELECTOR, 0);
4751 ++ vmcs_write16(HOST_ES_SELECTOR, 0);
4752 ++#else
4753 ++ vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
4754 ++ vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
4755 ++#endif
4756 ++ vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
4757 ++ vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */
4758 ++
4759 ++ vmcs_writel(HOST_IDTR_BASE, host_idt_base); /* 22.2.4 */
4760 ++
4761 ++ vmcs_writel(HOST_RIP, (unsigned long)vmx_vmexit); /* 22.2.5 */
4762 ++
4763 ++ rdmsr(MSR_IA32_SYSENTER_CS, low32, high32);
4764 ++ vmcs_write32(HOST_IA32_SYSENTER_CS, low32);
4765 ++ rdmsrl(MSR_IA32_SYSENTER_EIP, tmpl);
4766 ++ vmcs_writel(HOST_IA32_SYSENTER_EIP, tmpl); /* 22.2.3 */
4767 ++
4768 ++ if (vmcs_config.vmexit_ctrl & VM_EXIT_LOAD_IA32_PAT) {
4769 ++ rdmsr(MSR_IA32_CR_PAT, low32, high32);
4770 ++ vmcs_write64(HOST_IA32_PAT, low32 | ((u64) high32 << 32));
4771 ++ }
4772 ++
4773 ++ if (cpu_has_load_ia32_efer())
4774 ++ vmcs_write64(HOST_IA32_EFER, host_efer);
4775 ++}
4776 ++
4777 ++void set_cr4_guest_host_mask(struct vcpu_vmx *vmx)
4778 ++{
4779 ++ vmx->vcpu.arch.cr4_guest_owned_bits = KVM_CR4_GUEST_OWNED_BITS;
4780 ++ if (enable_ept)
4781 ++ vmx->vcpu.arch.cr4_guest_owned_bits |= X86_CR4_PGE;
4782 ++ if (is_guest_mode(&vmx->vcpu))
4783 ++ vmx->vcpu.arch.cr4_guest_owned_bits &=
4784 ++ ~get_vmcs12(&vmx->vcpu)->cr4_guest_host_mask;
4785 ++ vmcs_writel(CR4_GUEST_HOST_MASK, ~vmx->vcpu.arch.cr4_guest_owned_bits);
4786 ++}
4787 ++
4788 ++u32 vmx_pin_based_exec_ctrl(struct vcpu_vmx *vmx)
4789 ++{
4790 ++ u32 pin_based_exec_ctrl = vmcs_config.pin_based_exec_ctrl;
4791 ++
4792 ++ if (!kvm_vcpu_apicv_active(&vmx->vcpu))
4793 ++ pin_based_exec_ctrl &= ~PIN_BASED_POSTED_INTR;
4794 ++
4795 ++ if (!enable_vnmi)
4796 ++ pin_based_exec_ctrl &= ~PIN_BASED_VIRTUAL_NMIS;
4797 ++
4798 ++ if (!enable_preemption_timer)
4799 ++ pin_based_exec_ctrl &= ~PIN_BASED_VMX_PREEMPTION_TIMER;
4800 ++
4801 ++ return pin_based_exec_ctrl;
4802 ++}
4803 ++
4804 ++static void vmx_refresh_apicv_exec_ctrl(struct kvm_vcpu *vcpu)
4805 ++{
4806 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
4807 ++
4808 ++ pin_controls_set(vmx, vmx_pin_based_exec_ctrl(vmx));
4809 ++ if (cpu_has_secondary_exec_ctrls()) {
4810 ++ if (kvm_vcpu_apicv_active(vcpu))
4811 ++ secondary_exec_controls_setbit(vmx,
4812 ++ SECONDARY_EXEC_APIC_REGISTER_VIRT |
4813 ++ SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
4814 ++ else
4815 ++ secondary_exec_controls_clearbit(vmx,
4816 ++ SECONDARY_EXEC_APIC_REGISTER_VIRT |
4817 ++ SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
4818 ++ }
4819 ++
4820 ++ if (cpu_has_vmx_msr_bitmap())
4821 ++ vmx_update_msr_bitmap(vcpu);
4822 ++}
4823 ++
4824 ++u32 vmx_exec_control(struct vcpu_vmx *vmx)
4825 ++{
4826 ++ u32 exec_control = vmcs_config.cpu_based_exec_ctrl;
4827 ++
4828 ++ if (vmx->vcpu.arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)
4829 ++ exec_control &= ~CPU_BASED_MOV_DR_EXITING;
4830 ++
4831 ++ if (!cpu_need_tpr_shadow(&vmx->vcpu)) {
4832 ++ exec_control &= ~CPU_BASED_TPR_SHADOW;
4833 ++#ifdef CONFIG_X86_64
4834 ++ exec_control |= CPU_BASED_CR8_STORE_EXITING |
4835 ++ CPU_BASED_CR8_LOAD_EXITING;
4836 ++#endif
4837 ++ }
4838 ++ if (!enable_ept)
4839 ++ exec_control |= CPU_BASED_CR3_STORE_EXITING |
4840 ++ CPU_BASED_CR3_LOAD_EXITING |
4841 ++ CPU_BASED_INVLPG_EXITING;
4842 ++ if (kvm_mwait_in_guest(vmx->vcpu.kvm))
4843 ++ exec_control &= ~(CPU_BASED_MWAIT_EXITING |
4844 ++ CPU_BASED_MONITOR_EXITING);
4845 ++ if (kvm_hlt_in_guest(vmx->vcpu.kvm))
4846 ++ exec_control &= ~CPU_BASED_HLT_EXITING;
4847 ++ return exec_control;
4848 ++}
4849 ++
4850 ++
4851 ++static void vmx_compute_secondary_exec_control(struct vcpu_vmx *vmx)
4852 ++{
4853 ++ struct kvm_vcpu *vcpu = &vmx->vcpu;
4854 ++
4855 ++ u32 exec_control = vmcs_config.cpu_based_2nd_exec_ctrl;
4856 ++
4857 ++ if (pt_mode == PT_MODE_SYSTEM)
4858 ++ exec_control &= ~(SECONDARY_EXEC_PT_USE_GPA | SECONDARY_EXEC_PT_CONCEAL_VMX);
4859 ++ if (!cpu_need_virtualize_apic_accesses(vcpu))
4860 ++ exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
4861 ++ if (vmx->vpid == 0)
4862 ++ exec_control &= ~SECONDARY_EXEC_ENABLE_VPID;
4863 ++ if (!enable_ept) {
4864 ++ exec_control &= ~SECONDARY_EXEC_ENABLE_EPT;
4865 ++ enable_unrestricted_guest = 0;
4866 ++ }
4867 ++ if (!enable_unrestricted_guest)
4868 ++ exec_control &= ~SECONDARY_EXEC_UNRESTRICTED_GUEST;
4869 ++ if (kvm_pause_in_guest(vmx->vcpu.kvm))
4870 ++ exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
4871 ++ if (!kvm_vcpu_apicv_active(vcpu))
4872 ++ exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
4873 ++ SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
4874 ++ exec_control &= ~SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
4875 ++
4876 ++ /* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP,
4877 ++ * in vmx_set_cr4. */
4878 ++ exec_control &= ~SECONDARY_EXEC_DESC;
4879 ++
4880 ++ /* SECONDARY_EXEC_SHADOW_VMCS is enabled when L1 executes VMPTRLD
4881 ++ (handle_vmptrld).
4882 ++ We can NOT enable shadow_vmcs here because we don't have yet
4883 ++ a current VMCS12
4884 ++ */
4885 ++ exec_control &= ~SECONDARY_EXEC_SHADOW_VMCS;
4886 ++
4887 ++ if (!enable_pml)
4888 ++ exec_control &= ~SECONDARY_EXEC_ENABLE_PML;
4889 ++
4890 ++ if (vmx_xsaves_supported()) {
4891 ++ /* Exposing XSAVES only when XSAVE is exposed */
4892 ++ bool xsaves_enabled =
4893 ++ guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
4894 ++ guest_cpuid_has(vcpu, X86_FEATURE_XSAVES);
4895 ++
4896 ++ vcpu->arch.xsaves_enabled = xsaves_enabled;
4897 ++
4898 ++ if (!xsaves_enabled)
4899 ++ exec_control &= ~SECONDARY_EXEC_XSAVES;
4900 ++
4901 ++ if (nested) {
4902 ++ if (xsaves_enabled)
4903 ++ vmx->nested.msrs.secondary_ctls_high |=
4904 ++ SECONDARY_EXEC_XSAVES;
4905 ++ else
4906 ++ vmx->nested.msrs.secondary_ctls_high &=
4907 ++ ~SECONDARY_EXEC_XSAVES;
4908 ++ }
4909 ++ }
4910 ++
4911 ++ if (vmx_rdtscp_supported()) {
4912 ++ bool rdtscp_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP);
4913 ++ if (!rdtscp_enabled)
4914 ++ exec_control &= ~SECONDARY_EXEC_RDTSCP;
4915 ++
4916 ++ if (nested) {
4917 ++ if (rdtscp_enabled)
4918 ++ vmx->nested.msrs.secondary_ctls_high |=
4919 ++ SECONDARY_EXEC_RDTSCP;
4920 ++ else
4921 ++ vmx->nested.msrs.secondary_ctls_high &=
4922 ++ ~SECONDARY_EXEC_RDTSCP;
4923 ++ }
4924 ++ }
4925 ++
4926 ++ if (vmx_invpcid_supported()) {
4927 ++ /* Exposing INVPCID only when PCID is exposed */
4928 ++ bool invpcid_enabled =
4929 ++ guest_cpuid_has(vcpu, X86_FEATURE_INVPCID) &&
4930 ++ guest_cpuid_has(vcpu, X86_FEATURE_PCID);
4931 ++
4932 ++ if (!invpcid_enabled) {
4933 ++ exec_control &= ~SECONDARY_EXEC_ENABLE_INVPCID;
4934 ++ guest_cpuid_clear(vcpu, X86_FEATURE_INVPCID);
4935 ++ }
4936 ++
4937 ++ if (nested) {
4938 ++ if (invpcid_enabled)
4939 ++ vmx->nested.msrs.secondary_ctls_high |=
4940 ++ SECONDARY_EXEC_ENABLE_INVPCID;
4941 ++ else
4942 ++ vmx->nested.msrs.secondary_ctls_high &=
4943 ++ ~SECONDARY_EXEC_ENABLE_INVPCID;
4944 ++ }
4945 ++ }
4946 ++
4947 ++ if (vmx_rdrand_supported()) {
4948 ++ bool rdrand_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDRAND);
4949 ++ if (rdrand_enabled)
4950 ++ exec_control &= ~SECONDARY_EXEC_RDRAND_EXITING;
4951 ++
4952 ++ if (nested) {
4953 ++ if (rdrand_enabled)
4954 ++ vmx->nested.msrs.secondary_ctls_high |=
4955 ++ SECONDARY_EXEC_RDRAND_EXITING;
4956 ++ else
4957 ++ vmx->nested.msrs.secondary_ctls_high &=
4958 ++ ~SECONDARY_EXEC_RDRAND_EXITING;
4959 ++ }
4960 ++ }
4961 ++
4962 ++ if (vmx_rdseed_supported()) {
4963 ++ bool rdseed_enabled = guest_cpuid_has(vcpu, X86_FEATURE_RDSEED);
4964 ++ if (rdseed_enabled)
4965 ++ exec_control &= ~SECONDARY_EXEC_RDSEED_EXITING;
4966 ++
4967 ++ if (nested) {
4968 ++ if (rdseed_enabled)
4969 ++ vmx->nested.msrs.secondary_ctls_high |=
4970 ++ SECONDARY_EXEC_RDSEED_EXITING;
4971 ++ else
4972 ++ vmx->nested.msrs.secondary_ctls_high &=
4973 ++ ~SECONDARY_EXEC_RDSEED_EXITING;
4974 ++ }
4975 ++ }
4976 ++
4977 ++ if (vmx_waitpkg_supported()) {
4978 ++ bool waitpkg_enabled =
4979 ++ guest_cpuid_has(vcpu, X86_FEATURE_WAITPKG);
4980 ++
4981 ++ if (!waitpkg_enabled)
4982 ++ exec_control &= ~SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE;
4983 ++
4984 ++ if (nested) {
4985 ++ if (waitpkg_enabled)
4986 ++ vmx->nested.msrs.secondary_ctls_high |=
4987 ++ SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE;
4988 ++ else
4989 ++ vmx->nested.msrs.secondary_ctls_high &=
4990 ++ ~SECONDARY_EXEC_ENABLE_USR_WAIT_PAUSE;
4991 ++ }
4992 ++ }
4993 ++
4994 ++ vmx->secondary_exec_control = exec_control;
4995 ++}
4996 ++
4997 ++static void ept_set_mmio_spte_mask(void)
4998 ++{
4999 ++ /*
5000 ++ * EPT Misconfigurations can be generated if the value of bits 2:0
5001 ++ * of an EPT paging-structure entry is 110b (write/execute).
5002 ++ */
5003 ++ kvm_mmu_set_mmio_spte_mask(VMX_EPT_RWX_MASK,
5004 ++ VMX_EPT_MISCONFIG_WX_VALUE, 0);
5005 ++}
5006 ++
5007 ++#define VMX_XSS_EXIT_BITMAP 0
5008 ++
5009 ++/*
5010 ++ * Noting that the initialization of Guest-state Area of VMCS is in
5011 ++ * vmx_vcpu_reset().
5012 ++ */
5013 ++static void init_vmcs(struct vcpu_vmx *vmx)
5014 ++{
5015 ++ if (nested)
5016 ++ nested_vmx_set_vmcs_shadowing_bitmap();
5017 ++
5018 ++ if (cpu_has_vmx_msr_bitmap())
5019 ++ vmcs_write64(MSR_BITMAP, __pa(vmx->vmcs01.msr_bitmap));
5020 ++
5021 ++ vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */
5022 ++
5023 ++ /* Control */
5024 ++ pin_controls_set(vmx, vmx_pin_based_exec_ctrl(vmx));
5025 ++
5026 ++ exec_controls_set(vmx, vmx_exec_control(vmx));
5027 ++
5028 ++ if (cpu_has_secondary_exec_ctrls()) {
5029 ++ vmx_compute_secondary_exec_control(vmx);
5030 ++ secondary_exec_controls_set(vmx, vmx->secondary_exec_control);
5031 ++ }
5032 ++
5033 ++ if (kvm_vcpu_apicv_active(&vmx->vcpu)) {
5034 ++ vmcs_write64(EOI_EXIT_BITMAP0, 0);
5035 ++ vmcs_write64(EOI_EXIT_BITMAP1, 0);
5036 ++ vmcs_write64(EOI_EXIT_BITMAP2, 0);
5037 ++ vmcs_write64(EOI_EXIT_BITMAP3, 0);
5038 ++
5039 ++ vmcs_write16(GUEST_INTR_STATUS, 0);
5040 ++
5041 ++ vmcs_write16(POSTED_INTR_NV, POSTED_INTR_VECTOR);
5042 ++ vmcs_write64(POSTED_INTR_DESC_ADDR, __pa((&vmx->pi_desc)));
5043 ++ }
5044 ++
5045 ++ if (!kvm_pause_in_guest(vmx->vcpu.kvm)) {
5046 ++ vmcs_write32(PLE_GAP, ple_gap);
5047 ++ vmx->ple_window = ple_window;
5048 ++ vmx->ple_window_dirty = true;
5049 ++ }
5050 ++
5051 ++ vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0);
5052 ++ vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0);
5053 ++ vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */
5054 ++
5055 ++ vmcs_write16(HOST_FS_SELECTOR, 0); /* 22.2.4 */
5056 ++ vmcs_write16(HOST_GS_SELECTOR, 0); /* 22.2.4 */
5057 ++ vmx_set_constant_host_state(vmx);
5058 ++ vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */
5059 ++ vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */
5060 ++
5061 ++ if (cpu_has_vmx_vmfunc())
5062 ++ vmcs_write64(VM_FUNCTION_CONTROL, 0);
5063 ++
5064 ++ vmcs_write32(VM_EXIT_MSR_STORE_COUNT, 0);
5065 ++ vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, 0);
5066 ++ vmcs_write64(VM_EXIT_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.host.val));
5067 ++ vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, 0);
5068 ++ vmcs_write64(VM_ENTRY_MSR_LOAD_ADDR, __pa(vmx->msr_autoload.guest.val));
5069 ++
5070 ++ if (vmcs_config.vmentry_ctrl & VM_ENTRY_LOAD_IA32_PAT)
5071 ++ vmcs_write64(GUEST_IA32_PAT, vmx->vcpu.arch.pat);
5072 ++
5073 ++ vm_exit_controls_set(vmx, vmx_vmexit_ctrl());
5074 ++
5075 ++ /* 22.2.1, 20.8.1 */
5076 ++ vm_entry_controls_set(vmx, vmx_vmentry_ctrl());
5077 ++
5078 ++ vmx->vcpu.arch.cr0_guest_owned_bits = X86_CR0_TS;
5079 ++ vmcs_writel(CR0_GUEST_HOST_MASK, ~X86_CR0_TS);
5080 ++
5081 ++ set_cr4_guest_host_mask(vmx);
5082 ++
5083 ++ if (vmx->vpid != 0)
5084 ++ vmcs_write16(VIRTUAL_PROCESSOR_ID, vmx->vpid);
5085 ++
5086 ++ if (vmx_xsaves_supported())
5087 ++ vmcs_write64(XSS_EXIT_BITMAP, VMX_XSS_EXIT_BITMAP);
5088 ++
5089 ++ if (enable_pml) {
5090 ++ vmcs_write64(PML_ADDRESS, page_to_phys(vmx->pml_pg));
5091 ++ vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
5092 ++ }
5093 ++
5094 ++ if (cpu_has_vmx_encls_vmexit())
5095 ++ vmcs_write64(ENCLS_EXITING_BITMAP, -1ull);
5096 ++
5097 ++ if (pt_mode == PT_MODE_HOST_GUEST) {
5098 ++ memset(&vmx->pt_desc, 0, sizeof(vmx->pt_desc));
5099 ++ /* Bit[6~0] are forced to 1, writes are ignored. */
5100 ++ vmx->pt_desc.guest.output_mask = 0x7F;
5101 ++ vmcs_write64(GUEST_IA32_RTIT_CTL, 0);
5102 ++ }
5103 ++}
5104 ++
5105 ++static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
5106 ++{
5107 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
5108 ++ struct msr_data apic_base_msr;
5109 ++ u64 cr0;
5110 ++
5111 ++ vmx->rmode.vm86_active = 0;
5112 ++ vmx->spec_ctrl = 0;
5113 ++
5114 ++ vmx->msr_ia32_umwait_control = 0;
5115 ++
5116 ++ vcpu->arch.microcode_version = 0x100000000ULL;
5117 ++ vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val();
5118 ++ vmx->hv_deadline_tsc = -1;
5119 ++ kvm_set_cr8(vcpu, 0);
5120 ++
5121 ++ if (!init_event) {
5122 ++ apic_base_msr.data = APIC_DEFAULT_PHYS_BASE |
5123 ++ MSR_IA32_APICBASE_ENABLE;
5124 ++ if (kvm_vcpu_is_reset_bsp(vcpu))
5125 ++ apic_base_msr.data |= MSR_IA32_APICBASE_BSP;
5126 ++ apic_base_msr.host_initiated = true;
5127 ++ kvm_set_apic_base(vcpu, &apic_base_msr);
5128 ++ }
5129 ++
5130 ++ vmx_segment_cache_clear(vmx);
5131 ++
5132 ++ seg_setup(VCPU_SREG_CS);
5133 ++ vmcs_write16(GUEST_CS_SELECTOR, 0xf000);
5134 ++ vmcs_writel(GUEST_CS_BASE, 0xffff0000ul);
5135 ++
5136 ++ seg_setup(VCPU_SREG_DS);
5137 ++ seg_setup(VCPU_SREG_ES);
5138 ++ seg_setup(VCPU_SREG_FS);
5139 ++ seg_setup(VCPU_SREG_GS);
5140 ++ seg_setup(VCPU_SREG_SS);
5141 ++
5142 ++ vmcs_write16(GUEST_TR_SELECTOR, 0);
5143 ++ vmcs_writel(GUEST_TR_BASE, 0);
5144 ++ vmcs_write32(GUEST_TR_LIMIT, 0xffff);
5145 ++ vmcs_write32(GUEST_TR_AR_BYTES, 0x008b);
5146 ++
5147 ++ vmcs_write16(GUEST_LDTR_SELECTOR, 0);
5148 ++ vmcs_writel(GUEST_LDTR_BASE, 0);
5149 ++ vmcs_write32(GUEST_LDTR_LIMIT, 0xffff);
5150 ++ vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082);
5151 ++
5152 ++ if (!init_event) {
5153 ++ vmcs_write32(GUEST_SYSENTER_CS, 0);
5154 ++ vmcs_writel(GUEST_SYSENTER_ESP, 0);
5155 ++ vmcs_writel(GUEST_SYSENTER_EIP, 0);
5156 ++ vmcs_write64(GUEST_IA32_DEBUGCTL, 0);
5157 ++ }
5158 ++
5159 ++ kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
5160 ++ kvm_rip_write(vcpu, 0xfff0);
5161 ++
5162 ++ vmcs_writel(GUEST_GDTR_BASE, 0);
5163 ++ vmcs_write32(GUEST_GDTR_LIMIT, 0xffff);
5164 ++
5165 ++ vmcs_writel(GUEST_IDTR_BASE, 0);
5166 ++ vmcs_write32(GUEST_IDTR_LIMIT, 0xffff);
5167 ++
5168 ++ vmcs_write32(GUEST_ACTIVITY_STATE, GUEST_ACTIVITY_ACTIVE);
5169 ++ vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0);
5170 ++ vmcs_writel(GUEST_PENDING_DBG_EXCEPTIONS, 0);
5171 ++ if (kvm_mpx_supported())
5172 ++ vmcs_write64(GUEST_BNDCFGS, 0);
5173 ++
5174 ++ setup_msrs(vmx);
5175 ++
5176 ++ vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */
5177 ++
5178 ++ if (cpu_has_vmx_tpr_shadow() && !init_event) {
5179 ++ vmcs_write64(VIRTUAL_APIC_PAGE_ADDR, 0);
5180 ++ if (cpu_need_tpr_shadow(vcpu))
5181 ++ vmcs_write64(VIRTUAL_APIC_PAGE_ADDR,
5182 ++ __pa(vcpu->arch.apic->regs));
5183 ++ vmcs_write32(TPR_THRESHOLD, 0);
5184 ++ }
5185 ++
5186 ++ kvm_make_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu);
5187 ++
5188 ++ cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
5189 ++ vmx->vcpu.arch.cr0 = cr0;
5190 ++ vmx_set_cr0(vcpu, cr0); /* enter rmode */
5191 ++ vmx_set_cr4(vcpu, 0);
5192 ++ vmx_set_efer(vcpu, 0);
5193 ++
5194 ++ update_exception_bitmap(vcpu);
5195 ++
5196 ++ vpid_sync_context(vmx->vpid);
5197 ++ if (init_event)
5198 ++ vmx_clear_hlt(vcpu);
5199 ++}
5200 ++
5201 ++static void enable_irq_window(struct kvm_vcpu *vcpu)
5202 ++{
5203 ++ exec_controls_setbit(to_vmx(vcpu), CPU_BASED_INTR_WINDOW_EXITING);
5204 ++}
5205 ++
5206 ++static void enable_nmi_window(struct kvm_vcpu *vcpu)
5207 ++{
5208 ++ if (!enable_vnmi ||
5209 ++ vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_STI) {
5210 ++ enable_irq_window(vcpu);
5211 ++ return;
5212 ++ }
5213 ++
5214 ++ exec_controls_setbit(to_vmx(vcpu), CPU_BASED_NMI_WINDOW_EXITING);
5215 ++}
5216 ++
5217 ++static void vmx_inject_irq(struct kvm_vcpu *vcpu)
5218 ++{
5219 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
5220 ++ uint32_t intr;
5221 ++ int irq = vcpu->arch.interrupt.nr;
5222 ++
5223 ++ trace_kvm_inj_virq(irq);
5224 ++
5225 ++ ++vcpu->stat.irq_injections;
5226 ++ if (vmx->rmode.vm86_active) {
5227 ++ int inc_eip = 0;
5228 ++ if (vcpu->arch.interrupt.soft)
5229 ++ inc_eip = vcpu->arch.event_exit_inst_len;
5230 ++ kvm_inject_realmode_interrupt(vcpu, irq, inc_eip);
5231 ++ return;
5232 ++ }
5233 ++ intr = irq | INTR_INFO_VALID_MASK;
5234 ++ if (vcpu->arch.interrupt.soft) {
5235 ++ intr |= INTR_TYPE_SOFT_INTR;
5236 ++ vmcs_write32(VM_ENTRY_INSTRUCTION_LEN,
5237 ++ vmx->vcpu.arch.event_exit_inst_len);
5238 ++ } else
5239 ++ intr |= INTR_TYPE_EXT_INTR;
5240 ++ vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, intr);
5241 ++
5242 ++ vmx_clear_hlt(vcpu);
5243 ++}
5244 ++
5245 ++static void vmx_inject_nmi(struct kvm_vcpu *vcpu)
5246 ++{
5247 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
5248 ++
5249 ++ if (!enable_vnmi) {
5250 ++ /*
5251 ++ * Tracking the NMI-blocked state in software is built upon
5252 ++ * finding the next open IRQ window. This, in turn, depends on
5253 ++ * well-behaving guests: They have to keep IRQs disabled at
5254 ++ * least as long as the NMI handler runs. Otherwise we may
5255 ++ * cause NMI nesting, maybe breaking the guest. But as this is
5256 ++ * highly unlikely, we can live with the residual risk.
5257 ++ */
5258 ++ vmx->loaded_vmcs->soft_vnmi_blocked = 1;
5259 ++ vmx->loaded_vmcs->vnmi_blocked_time = 0;
5260 ++ }
5261 ++
5262 ++ ++vcpu->stat.nmi_injections;
5263 ++ vmx->loaded_vmcs->nmi_known_unmasked = false;
5264 ++
5265 ++ if (vmx->rmode.vm86_active) {
5266 ++ kvm_inject_realmode_interrupt(vcpu, NMI_VECTOR, 0);
5267 ++ return;
5268 ++ }
5269 ++
5270 ++ vmcs_write32(VM_ENTRY_INTR_INFO_FIELD,
5271 ++ INTR_TYPE_NMI_INTR | INTR_INFO_VALID_MASK | NMI_VECTOR);
5272 ++
5273 ++ vmx_clear_hlt(vcpu);
5274 ++}
5275 ++
5276 ++bool vmx_get_nmi_mask(struct kvm_vcpu *vcpu)
5277 ++{
5278 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
5279 ++ bool masked;
5280 ++
5281 ++ if (!enable_vnmi)
5282 ++ return vmx->loaded_vmcs->soft_vnmi_blocked;
5283 ++ if (vmx->loaded_vmcs->nmi_known_unmasked)
5284 ++ return false;
5285 ++ masked = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & GUEST_INTR_STATE_NMI;
5286 ++ vmx->loaded_vmcs->nmi_known_unmasked = !masked;
5287 ++ return masked;
5288 ++}
5289 ++
5290 ++void vmx_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
5291 ++{
5292 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
5293 ++
5294 ++ if (!enable_vnmi) {
5295 ++ if (vmx->loaded_vmcs->soft_vnmi_blocked != masked) {
5296 ++ vmx->loaded_vmcs->soft_vnmi_blocked = masked;
5297 ++ vmx->loaded_vmcs->vnmi_blocked_time = 0;
5298 ++ }
5299 ++ } else {
5300 ++ vmx->loaded_vmcs->nmi_known_unmasked = !masked;
5301 ++ if (masked)
5302 ++ vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
5303 ++ GUEST_INTR_STATE_NMI);
5304 ++ else
5305 ++ vmcs_clear_bits(GUEST_INTERRUPTIBILITY_INFO,
5306 ++ GUEST_INTR_STATE_NMI);
5307 ++ }
5308 ++}
5309 ++
5310 ++static int vmx_nmi_allowed(struct kvm_vcpu *vcpu)
5311 ++{
5312 ++ if (to_vmx(vcpu)->nested.nested_run_pending)
5313 ++ return 0;
5314 ++
5315 ++ if (!enable_vnmi &&
5316 ++ to_vmx(vcpu)->loaded_vmcs->soft_vnmi_blocked)
5317 ++ return 0;
5318 ++
5319 ++ return !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5320 ++ (GUEST_INTR_STATE_MOV_SS | GUEST_INTR_STATE_STI
5321 ++ | GUEST_INTR_STATE_NMI));
5322 ++}
5323 ++
5324 ++static int vmx_interrupt_allowed(struct kvm_vcpu *vcpu)
5325 ++{
5326 ++ return (!to_vmx(vcpu)->nested.nested_run_pending &&
5327 ++ vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) &&
5328 ++ !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) &
5329 ++ (GUEST_INTR_STATE_STI | GUEST_INTR_STATE_MOV_SS));
5330 ++}
5331 ++
5332 ++static int vmx_set_tss_addr(struct kvm *kvm, unsigned int addr)
5333 ++{
5334 ++ int ret;
5335 ++
5336 ++ if (enable_unrestricted_guest)
5337 ++ return 0;
5338 ++
5339 ++ ret = x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, addr,
5340 ++ PAGE_SIZE * 3);
5341 ++ if (ret)
5342 ++ return ret;
5343 ++ to_kvm_vmx(kvm)->tss_addr = addr;
5344 ++ return init_rmode_tss(kvm);
5345 ++}
5346 ++
5347 ++static int vmx_set_identity_map_addr(struct kvm *kvm, u64 ident_addr)
5348 ++{
5349 ++ to_kvm_vmx(kvm)->ept_identity_map_addr = ident_addr;
5350 ++ return 0;
5351 ++}
5352 ++
5353 ++static bool rmode_exception(struct kvm_vcpu *vcpu, int vec)
5354 ++{
5355 ++ switch (vec) {
5356 ++ case BP_VECTOR:
5357 ++ /*
5358 ++ * Update instruction length as we may reinject the exception
5359 ++ * from user space while in guest debugging mode.
5360 ++ */
5361 ++ to_vmx(vcpu)->vcpu.arch.event_exit_inst_len =
5362 ++ vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
5363 ++ if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
5364 ++ return false;
5365 ++ /* fall through */
5366 ++ case DB_VECTOR:
5367 ++ if (vcpu->guest_debug &
5368 ++ (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
5369 ++ return false;
5370 ++ /* fall through */
5371 ++ case DE_VECTOR:
5372 ++ case OF_VECTOR:
5373 ++ case BR_VECTOR:
5374 ++ case UD_VECTOR:
5375 ++ case DF_VECTOR:
5376 ++ case SS_VECTOR:
5377 ++ case GP_VECTOR:
5378 ++ case MF_VECTOR:
5379 ++ return true;
5380 ++ break;
5381 ++ }
5382 ++ return false;
5383 ++}
5384 ++
5385 ++static int handle_rmode_exception(struct kvm_vcpu *vcpu,
5386 ++ int vec, u32 err_code)
5387 ++{
5388 ++ /*
5389 ++ * Instruction with address size override prefix opcode 0x67
5390 ++ * Cause the #SS fault with 0 error code in VM86 mode.
5391 ++ */
5392 ++ if (((vec == GP_VECTOR) || (vec == SS_VECTOR)) && err_code == 0) {
5393 ++ if (kvm_emulate_instruction(vcpu, 0)) {
5394 ++ if (vcpu->arch.halt_request) {
5395 ++ vcpu->arch.halt_request = 0;
5396 ++ return kvm_vcpu_halt(vcpu);
5397 ++ }
5398 ++ return 1;
5399 ++ }
5400 ++ return 0;
5401 ++ }
5402 ++
5403 ++ /*
5404 ++ * Forward all other exceptions that are valid in real mode.
5405 ++ * FIXME: Breaks guest debugging in real mode, needs to be fixed with
5406 ++ * the required debugging infrastructure rework.
5407 ++ */
5408 ++ kvm_queue_exception(vcpu, vec);
5409 ++ return 1;
5410 ++}
5411 ++
5412 ++/*
5413 ++ * Trigger machine check on the host. We assume all the MSRs are already set up
5414 ++ * by the CPU and that we still run on the same CPU as the MCE occurred on.
5415 ++ * We pass a fake environment to the machine check handler because we want
5416 ++ * the guest to be always treated like user space, no matter what context
5417 ++ * it used internally.
5418 ++ */
5419 ++static void kvm_machine_check(void)
5420 ++{
5421 ++#if defined(CONFIG_X86_MCE) && defined(CONFIG_X86_64)
5422 ++ struct pt_regs regs = {
5423 ++ .cs = 3, /* Fake ring 3 no matter what the guest ran on */
5424 ++ .flags = X86_EFLAGS_IF,
5425 ++ };
5426 ++
5427 ++ do_machine_check(&regs, 0);
5428 ++#endif
5429 ++}
5430 ++
5431 ++static int handle_machine_check(struct kvm_vcpu *vcpu)
5432 ++{
5433 ++ /* handled by vmx_vcpu_run() */
5434 ++ return 1;
5435 ++}
5436 ++
5437 ++static int handle_exception_nmi(struct kvm_vcpu *vcpu)
5438 ++{
5439 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
5440 ++ struct kvm_run *kvm_run = vcpu->run;
5441 ++ u32 intr_info, ex_no, error_code;
5442 ++ unsigned long cr2, rip, dr6;
5443 ++ u32 vect_info;
5444 ++
5445 ++ vect_info = vmx->idt_vectoring_info;
5446 ++ intr_info = vmx->exit_intr_info;
5447 ++
5448 ++ if (is_machine_check(intr_info) || is_nmi(intr_info))
5449 ++ return 1; /* handled by handle_exception_nmi_irqoff() */
5450 ++
5451 ++ if (is_invalid_opcode(intr_info))
5452 ++ return handle_ud(vcpu);
5453 ++
5454 ++ error_code = 0;
5455 ++ if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
5456 ++ error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE);
5457 ++
5458 ++ if (!vmx->rmode.vm86_active && is_gp_fault(intr_info)) {
5459 ++ WARN_ON_ONCE(!enable_vmware_backdoor);
5460 ++
5461 ++ /*
5462 ++ * VMware backdoor emulation on #GP interception only handles
5463 ++ * IN{S}, OUT{S}, and RDPMC, none of which generate a non-zero
5464 ++ * error code on #GP.
5465 ++ */
5466 ++ if (error_code) {
5467 ++ kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
5468 ++ return 1;
5469 ++ }
5470 ++ return kvm_emulate_instruction(vcpu, EMULTYPE_VMWARE_GP);
5471 ++ }
5472 ++
5473 ++ /*
5474 ++ * The #PF with PFEC.RSVD = 1 indicates the guest is accessing
5475 ++ * MMIO, it is better to report an internal error.
5476 ++ * See the comments in vmx_handle_exit.
5477 ++ */
5478 ++ if ((vect_info & VECTORING_INFO_VALID_MASK) &&
5479 ++ !(is_page_fault(intr_info) && !(error_code & PFERR_RSVD_MASK))) {
5480 ++ vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
5481 ++ vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_SIMUL_EX;
5482 ++ vcpu->run->internal.ndata = 3;
5483 ++ vcpu->run->internal.data[0] = vect_info;
5484 ++ vcpu->run->internal.data[1] = intr_info;
5485 ++ vcpu->run->internal.data[2] = error_code;
5486 ++ return 0;
5487 ++ }
5488 ++
5489 ++ if (is_page_fault(intr_info)) {
5490 ++ cr2 = vmcs_readl(EXIT_QUALIFICATION);
5491 ++ /* EPT won't cause page fault directly */
5492 ++ WARN_ON_ONCE(!vcpu->arch.apf.host_apf_reason && enable_ept);
5493 ++ return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
5494 ++ }
5495 ++
5496 ++ ex_no = intr_info & INTR_INFO_VECTOR_MASK;
5497 ++
5498 ++ if (vmx->rmode.vm86_active && rmode_exception(vcpu, ex_no))
5499 ++ return handle_rmode_exception(vcpu, ex_no, error_code);
5500 ++
5501 ++ switch (ex_no) {
5502 ++ case AC_VECTOR:
5503 ++ kvm_queue_exception_e(vcpu, AC_VECTOR, error_code);
5504 ++ return 1;
5505 ++ case DB_VECTOR:
5506 ++ dr6 = vmcs_readl(EXIT_QUALIFICATION);
5507 ++ if (!(vcpu->guest_debug &
5508 ++ (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))) {
5509 ++ vcpu->arch.dr6 &= ~DR_TRAP_BITS;
5510 ++ vcpu->arch.dr6 |= dr6 | DR6_RTM;
5511 ++ if (is_icebp(intr_info))
5512 ++ WARN_ON(!skip_emulated_instruction(vcpu));
5513 ++
5514 ++ kvm_queue_exception(vcpu, DB_VECTOR);
5515 ++ return 1;
5516 ++ }
5517 ++ kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1;
5518 ++ kvm_run->debug.arch.dr7 = vmcs_readl(GUEST_DR7);
5519 ++ /* fall through */
5520 ++ case BP_VECTOR:
5521 ++ /*
5522 ++ * Update instruction length as we may reinject #BP from
5523 ++ * user space while in guest debugging mode. Reading it for
5524 ++ * #DB as well causes no harm, it is not used in that case.
5525 ++ */
5526 ++ vmx->vcpu.arch.event_exit_inst_len =
5527 ++ vmcs_read32(VM_EXIT_INSTRUCTION_LEN);
5528 ++ kvm_run->exit_reason = KVM_EXIT_DEBUG;
5529 ++ rip = kvm_rip_read(vcpu);
5530 ++ kvm_run->debug.arch.pc = vmcs_readl(GUEST_CS_BASE) + rip;
5531 ++ kvm_run->debug.arch.exception = ex_no;
5532 ++ break;
5533 ++ default:
5534 ++ kvm_run->exit_reason = KVM_EXIT_EXCEPTION;
5535 ++ kvm_run->ex.exception = ex_no;
5536 ++ kvm_run->ex.error_code = error_code;
5537 ++ break;
5538 ++ }
5539 ++ return 0;
5540 ++}
5541 ++
5542 ++static __always_inline int handle_external_interrupt(struct kvm_vcpu *vcpu)
5543 ++{
5544 ++ ++vcpu->stat.irq_exits;
5545 ++ return 1;
5546 ++}
5547 ++
5548 ++static int handle_triple_fault(struct kvm_vcpu *vcpu)
5549 ++{
5550 ++ vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
5551 ++ vcpu->mmio_needed = 0;
5552 ++ return 0;
5553 ++}
5554 ++
5555 ++static int handle_io(struct kvm_vcpu *vcpu)
5556 ++{
5557 ++ unsigned long exit_qualification;
5558 ++ int size, in, string;
5559 ++ unsigned port;
5560 ++
5561 ++ exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5562 ++ string = (exit_qualification & 16) != 0;
5563 ++
5564 ++ ++vcpu->stat.io_exits;
5565 ++
5566 ++ if (string)
5567 ++ return kvm_emulate_instruction(vcpu, 0);
5568 ++
5569 ++ port = exit_qualification >> 16;
5570 ++ size = (exit_qualification & 7) + 1;
5571 ++ in = (exit_qualification & 8) != 0;
5572 ++
5573 ++ return kvm_fast_pio(vcpu, size, port, in);
5574 ++}
5575 ++
5576 ++static void
5577 ++vmx_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
5578 ++{
5579 ++ /*
5580 ++ * Patch in the VMCALL instruction:
5581 ++ */
5582 ++ hypercall[0] = 0x0f;
5583 ++ hypercall[1] = 0x01;
5584 ++ hypercall[2] = 0xc1;
5585 ++}
5586 ++
5587 ++/* called to set cr0 as appropriate for a mov-to-cr0 exit. */
5588 ++static int handle_set_cr0(struct kvm_vcpu *vcpu, unsigned long val)
5589 ++{
5590 ++ if (is_guest_mode(vcpu)) {
5591 ++ struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5592 ++ unsigned long orig_val = val;
5593 ++
5594 ++ /*
5595 ++ * We get here when L2 changed cr0 in a way that did not change
5596 ++ * any of L1's shadowed bits (see nested_vmx_exit_handled_cr),
5597 ++ * but did change L0 shadowed bits. So we first calculate the
5598 ++ * effective cr0 value that L1 would like to write into the
5599 ++ * hardware. It consists of the L2-owned bits from the new
5600 ++ * value combined with the L1-owned bits from L1's guest_cr0.
5601 ++ */
5602 ++ val = (val & ~vmcs12->cr0_guest_host_mask) |
5603 ++ (vmcs12->guest_cr0 & vmcs12->cr0_guest_host_mask);
5604 ++
5605 ++ if (!nested_guest_cr0_valid(vcpu, val))
5606 ++ return 1;
5607 ++
5608 ++ if (kvm_set_cr0(vcpu, val))
5609 ++ return 1;
5610 ++ vmcs_writel(CR0_READ_SHADOW, orig_val);
5611 ++ return 0;
5612 ++ } else {
5613 ++ if (to_vmx(vcpu)->nested.vmxon &&
5614 ++ !nested_host_cr0_valid(vcpu, val))
5615 ++ return 1;
5616 ++
5617 ++ return kvm_set_cr0(vcpu, val);
5618 ++ }
5619 ++}
5620 ++
5621 ++static int handle_set_cr4(struct kvm_vcpu *vcpu, unsigned long val)
5622 ++{
5623 ++ if (is_guest_mode(vcpu)) {
5624 ++ struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
5625 ++ unsigned long orig_val = val;
5626 ++
5627 ++ /* analogously to handle_set_cr0 */
5628 ++ val = (val & ~vmcs12->cr4_guest_host_mask) |
5629 ++ (vmcs12->guest_cr4 & vmcs12->cr4_guest_host_mask);
5630 ++ if (kvm_set_cr4(vcpu, val))
5631 ++ return 1;
5632 ++ vmcs_writel(CR4_READ_SHADOW, orig_val);
5633 ++ return 0;
5634 ++ } else
5635 ++ return kvm_set_cr4(vcpu, val);
5636 ++}
5637 ++
5638 ++static int handle_desc(struct kvm_vcpu *vcpu)
5639 ++{
5640 ++ WARN_ON(!(vcpu->arch.cr4 & X86_CR4_UMIP));
5641 ++ return kvm_emulate_instruction(vcpu, 0);
5642 ++}
5643 ++
5644 ++static int handle_cr(struct kvm_vcpu *vcpu)
5645 ++{
5646 ++ unsigned long exit_qualification, val;
5647 ++ int cr;
5648 ++ int reg;
5649 ++ int err;
5650 ++ int ret;
5651 ++
5652 ++ exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5653 ++ cr = exit_qualification & 15;
5654 ++ reg = (exit_qualification >> 8) & 15;
5655 ++ switch ((exit_qualification >> 4) & 3) {
5656 ++ case 0: /* mov to cr */
5657 ++ val = kvm_register_readl(vcpu, reg);
5658 ++ trace_kvm_cr_write(cr, val);
5659 ++ switch (cr) {
5660 ++ case 0:
5661 ++ err = handle_set_cr0(vcpu, val);
5662 ++ return kvm_complete_insn_gp(vcpu, err);
5663 ++ case 3:
5664 ++ WARN_ON_ONCE(enable_unrestricted_guest);
5665 ++ err = kvm_set_cr3(vcpu, val);
5666 ++ return kvm_complete_insn_gp(vcpu, err);
5667 ++ case 4:
5668 ++ err = handle_set_cr4(vcpu, val);
5669 ++ return kvm_complete_insn_gp(vcpu, err);
5670 ++ case 8: {
5671 ++ u8 cr8_prev = kvm_get_cr8(vcpu);
5672 ++ u8 cr8 = (u8)val;
5673 ++ err = kvm_set_cr8(vcpu, cr8);
5674 ++ ret = kvm_complete_insn_gp(vcpu, err);
5675 ++ if (lapic_in_kernel(vcpu))
5676 ++ return ret;
5677 ++ if (cr8_prev <= cr8)
5678 ++ return ret;
5679 ++ /*
5680 ++ * TODO: we might be squashing a
5681 ++ * KVM_GUESTDBG_SINGLESTEP-triggered
5682 ++ * KVM_EXIT_DEBUG here.
5683 ++ */
5684 ++ vcpu->run->exit_reason = KVM_EXIT_SET_TPR;
5685 ++ return 0;
5686 ++ }
5687 ++ }
5688 ++ break;
5689 ++ case 2: /* clts */
5690 ++ WARN_ONCE(1, "Guest should always own CR0.TS");
5691 ++ vmx_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
5692 ++ trace_kvm_cr_write(0, kvm_read_cr0(vcpu));
5693 ++ return kvm_skip_emulated_instruction(vcpu);
5694 ++ case 1: /*mov from cr*/
5695 ++ switch (cr) {
5696 ++ case 3:
5697 ++ WARN_ON_ONCE(enable_unrestricted_guest);
5698 ++ val = kvm_read_cr3(vcpu);
5699 ++ kvm_register_write(vcpu, reg, val);
5700 ++ trace_kvm_cr_read(cr, val);
5701 ++ return kvm_skip_emulated_instruction(vcpu);
5702 ++ case 8:
5703 ++ val = kvm_get_cr8(vcpu);
5704 ++ kvm_register_write(vcpu, reg, val);
5705 ++ trace_kvm_cr_read(cr, val);
5706 ++ return kvm_skip_emulated_instruction(vcpu);
5707 ++ }
5708 ++ break;
5709 ++ case 3: /* lmsw */
5710 ++ val = (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f;
5711 ++ trace_kvm_cr_write(0, (kvm_read_cr0(vcpu) & ~0xful) | val);
5712 ++ kvm_lmsw(vcpu, val);
5713 ++
5714 ++ return kvm_skip_emulated_instruction(vcpu);
5715 ++ default:
5716 ++ break;
5717 ++ }
5718 ++ vcpu->run->exit_reason = 0;
5719 ++ vcpu_unimpl(vcpu, "unhandled control register: op %d cr %d\n",
5720 ++ (int)(exit_qualification >> 4) & 3, cr);
5721 ++ return 0;
5722 ++}
5723 ++
5724 ++static int handle_dr(struct kvm_vcpu *vcpu)
5725 ++{
5726 ++ unsigned long exit_qualification;
5727 ++ int dr, dr7, reg;
5728 ++
5729 ++ exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5730 ++ dr = exit_qualification & DEBUG_REG_ACCESS_NUM;
5731 ++
5732 ++ /* First, if DR does not exist, trigger UD */
5733 ++ if (!kvm_require_dr(vcpu, dr))
5734 ++ return 1;
5735 ++
5736 ++ /* Do not handle if the CPL > 0, will trigger GP on re-entry */
5737 ++ if (!kvm_require_cpl(vcpu, 0))
5738 ++ return 1;
5739 ++ dr7 = vmcs_readl(GUEST_DR7);
5740 ++ if (dr7 & DR7_GD) {
5741 ++ /*
5742 ++ * As the vm-exit takes precedence over the debug trap, we
5743 ++ * need to emulate the latter, either for the host or the
5744 ++ * guest debugging itself.
5745 ++ */
5746 ++ if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
5747 ++ vcpu->run->debug.arch.dr6 = vcpu->arch.dr6;
5748 ++ vcpu->run->debug.arch.dr7 = dr7;
5749 ++ vcpu->run->debug.arch.pc = kvm_get_linear_rip(vcpu);
5750 ++ vcpu->run->debug.arch.exception = DB_VECTOR;
5751 ++ vcpu->run->exit_reason = KVM_EXIT_DEBUG;
5752 ++ return 0;
5753 ++ } else {
5754 ++ vcpu->arch.dr6 &= ~DR_TRAP_BITS;
5755 ++ vcpu->arch.dr6 |= DR6_BD | DR6_RTM;
5756 ++ kvm_queue_exception(vcpu, DB_VECTOR);
5757 ++ return 1;
5758 ++ }
5759 ++ }
5760 ++
5761 ++ if (vcpu->guest_debug == 0) {
5762 ++ exec_controls_clearbit(to_vmx(vcpu), CPU_BASED_MOV_DR_EXITING);
5763 ++
5764 ++ /*
5765 ++ * No more DR vmexits; force a reload of the debug registers
5766 ++ * and reenter on this instruction. The next vmexit will
5767 ++ * retrieve the full state of the debug registers.
5768 ++ */
5769 ++ vcpu->arch.switch_db_regs |= KVM_DEBUGREG_WONT_EXIT;
5770 ++ return 1;
5771 ++ }
5772 ++
5773 ++ reg = DEBUG_REG_ACCESS_REG(exit_qualification);
5774 ++ if (exit_qualification & TYPE_MOV_FROM_DR) {
5775 ++ unsigned long val;
5776 ++
5777 ++ if (kvm_get_dr(vcpu, dr, &val))
5778 ++ return 1;
5779 ++ kvm_register_write(vcpu, reg, val);
5780 ++ } else
5781 ++ if (kvm_set_dr(vcpu, dr, kvm_register_readl(vcpu, reg)))
5782 ++ return 1;
5783 ++
5784 ++ return kvm_skip_emulated_instruction(vcpu);
5785 ++}
5786 ++
5787 ++static u64 vmx_get_dr6(struct kvm_vcpu *vcpu)
5788 ++{
5789 ++ return vcpu->arch.dr6;
5790 ++}
5791 ++
5792 ++static void vmx_set_dr6(struct kvm_vcpu *vcpu, unsigned long val)
5793 ++{
5794 ++}
5795 ++
5796 ++static void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu)
5797 ++{
5798 ++ get_debugreg(vcpu->arch.db[0], 0);
5799 ++ get_debugreg(vcpu->arch.db[1], 1);
5800 ++ get_debugreg(vcpu->arch.db[2], 2);
5801 ++ get_debugreg(vcpu->arch.db[3], 3);
5802 ++ get_debugreg(vcpu->arch.dr6, 6);
5803 ++ vcpu->arch.dr7 = vmcs_readl(GUEST_DR7);
5804 ++
5805 ++ vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_WONT_EXIT;
5806 ++ exec_controls_setbit(to_vmx(vcpu), CPU_BASED_MOV_DR_EXITING);
5807 ++}
5808 ++
5809 ++static void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val)
5810 ++{
5811 ++ vmcs_writel(GUEST_DR7, val);
5812 ++}
5813 ++
5814 ++static int handle_tpr_below_threshold(struct kvm_vcpu *vcpu)
5815 ++{
5816 ++ kvm_apic_update_ppr(vcpu);
5817 ++ return 1;
5818 ++}
5819 ++
5820 ++static int handle_interrupt_window(struct kvm_vcpu *vcpu)
5821 ++{
5822 ++ exec_controls_clearbit(to_vmx(vcpu), CPU_BASED_INTR_WINDOW_EXITING);
5823 ++
5824 ++ kvm_make_request(KVM_REQ_EVENT, vcpu);
5825 ++
5826 ++ ++vcpu->stat.irq_window_exits;
5827 ++ return 1;
5828 ++}
5829 ++
5830 ++static int handle_vmcall(struct kvm_vcpu *vcpu)
5831 ++{
5832 ++ return kvm_emulate_hypercall(vcpu);
5833 ++}
5834 ++
5835 ++static int handle_invd(struct kvm_vcpu *vcpu)
5836 ++{
5837 ++ return kvm_emulate_instruction(vcpu, 0);
5838 ++}
5839 ++
5840 ++static int handle_invlpg(struct kvm_vcpu *vcpu)
5841 ++{
5842 ++ unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5843 ++
5844 ++ kvm_mmu_invlpg(vcpu, exit_qualification);
5845 ++ return kvm_skip_emulated_instruction(vcpu);
5846 ++}
5847 ++
5848 ++static int handle_rdpmc(struct kvm_vcpu *vcpu)
5849 ++{
5850 ++ int err;
5851 ++
5852 ++ err = kvm_rdpmc(vcpu);
5853 ++ return kvm_complete_insn_gp(vcpu, err);
5854 ++}
5855 ++
5856 ++static int handle_wbinvd(struct kvm_vcpu *vcpu)
5857 ++{
5858 ++ return kvm_emulate_wbinvd(vcpu);
5859 ++}
5860 ++
5861 ++static int handle_xsetbv(struct kvm_vcpu *vcpu)
5862 ++{
5863 ++ u64 new_bv = kvm_read_edx_eax(vcpu);
5864 ++ u32 index = kvm_rcx_read(vcpu);
5865 ++
5866 ++ if (kvm_set_xcr(vcpu, index, new_bv) == 0)
5867 ++ return kvm_skip_emulated_instruction(vcpu);
5868 ++ return 1;
5869 ++}
5870 ++
5871 ++static int handle_apic_access(struct kvm_vcpu *vcpu)
5872 ++{
5873 ++ if (likely(fasteoi)) {
5874 ++ unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5875 ++ int access_type, offset;
5876 ++
5877 ++ access_type = exit_qualification & APIC_ACCESS_TYPE;
5878 ++ offset = exit_qualification & APIC_ACCESS_OFFSET;
5879 ++ /*
5880 ++ * Sane guest uses MOV to write EOI, with written value
5881 ++ * not cared. So make a short-circuit here by avoiding
5882 ++ * heavy instruction emulation.
5883 ++ */
5884 ++ if ((access_type == TYPE_LINEAR_APIC_INST_WRITE) &&
5885 ++ (offset == APIC_EOI)) {
5886 ++ kvm_lapic_set_eoi(vcpu);
5887 ++ return kvm_skip_emulated_instruction(vcpu);
5888 ++ }
5889 ++ }
5890 ++ return kvm_emulate_instruction(vcpu, 0);
5891 ++}
5892 ++
5893 ++static int handle_apic_eoi_induced(struct kvm_vcpu *vcpu)
5894 ++{
5895 ++ unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5896 ++ int vector = exit_qualification & 0xff;
5897 ++
5898 ++ /* EOI-induced VM exit is trap-like and thus no need to adjust IP */
5899 ++ kvm_apic_set_eoi_accelerated(vcpu, vector);
5900 ++ return 1;
5901 ++}
5902 ++
5903 ++static int handle_apic_write(struct kvm_vcpu *vcpu)
5904 ++{
5905 ++ unsigned long exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5906 ++ u32 offset = exit_qualification & 0xfff;
5907 ++
5908 ++ /* APIC-write VM exit is trap-like and thus no need to adjust IP */
5909 ++ kvm_apic_write_nodecode(vcpu, offset);
5910 ++ return 1;
5911 ++}
5912 ++
5913 ++static int handle_task_switch(struct kvm_vcpu *vcpu)
5914 ++{
5915 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
5916 ++ unsigned long exit_qualification;
5917 ++ bool has_error_code = false;
5918 ++ u32 error_code = 0;
5919 ++ u16 tss_selector;
5920 ++ int reason, type, idt_v, idt_index;
5921 ++
5922 ++ idt_v = (vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK);
5923 ++ idt_index = (vmx->idt_vectoring_info & VECTORING_INFO_VECTOR_MASK);
5924 ++ type = (vmx->idt_vectoring_info & VECTORING_INFO_TYPE_MASK);
5925 ++
5926 ++ exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5927 ++
5928 ++ reason = (u32)exit_qualification >> 30;
5929 ++ if (reason == TASK_SWITCH_GATE && idt_v) {
5930 ++ switch (type) {
5931 ++ case INTR_TYPE_NMI_INTR:
5932 ++ vcpu->arch.nmi_injected = false;
5933 ++ vmx_set_nmi_mask(vcpu, true);
5934 ++ break;
5935 ++ case INTR_TYPE_EXT_INTR:
5936 ++ case INTR_TYPE_SOFT_INTR:
5937 ++ kvm_clear_interrupt_queue(vcpu);
5938 ++ break;
5939 ++ case INTR_TYPE_HARD_EXCEPTION:
5940 ++ if (vmx->idt_vectoring_info &
5941 ++ VECTORING_INFO_DELIVER_CODE_MASK) {
5942 ++ has_error_code = true;
5943 ++ error_code =
5944 ++ vmcs_read32(IDT_VECTORING_ERROR_CODE);
5945 ++ }
5946 ++ /* fall through */
5947 ++ case INTR_TYPE_SOFT_EXCEPTION:
5948 ++ kvm_clear_exception_queue(vcpu);
5949 ++ break;
5950 ++ default:
5951 ++ break;
5952 ++ }
5953 ++ }
5954 ++ tss_selector = exit_qualification;
5955 ++
5956 ++ if (!idt_v || (type != INTR_TYPE_HARD_EXCEPTION &&
5957 ++ type != INTR_TYPE_EXT_INTR &&
5958 ++ type != INTR_TYPE_NMI_INTR))
5959 ++ WARN_ON(!skip_emulated_instruction(vcpu));
5960 ++
5961 ++ /*
5962 ++ * TODO: What about debug traps on tss switch?
5963 ++ * Are we supposed to inject them and update dr6?
5964 ++ */
5965 ++ return kvm_task_switch(vcpu, tss_selector,
5966 ++ type == INTR_TYPE_SOFT_INTR ? idt_index : -1,
5967 ++ reason, has_error_code, error_code);
5968 ++}
5969 ++
5970 ++static int handle_ept_violation(struct kvm_vcpu *vcpu)
5971 ++{
5972 ++ unsigned long exit_qualification;
5973 ++ gpa_t gpa;
5974 ++ u64 error_code;
5975 ++
5976 ++ exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
5977 ++
5978 ++ /*
5979 ++ * EPT violation happened while executing iret from NMI,
5980 ++ * "blocked by NMI" bit has to be set before next VM entry.
5981 ++ * There are errata that may cause this bit to not be set:
5982 ++ * AAK134, BY25.
5983 ++ */
5984 ++ if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
5985 ++ enable_vnmi &&
5986 ++ (exit_qualification & INTR_INFO_UNBLOCK_NMI))
5987 ++ vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO, GUEST_INTR_STATE_NMI);
5988 ++
5989 ++ gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
5990 ++ trace_kvm_page_fault(gpa, exit_qualification);
5991 ++
5992 ++ /* Is it a read fault? */
5993 ++ error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
5994 ++ ? PFERR_USER_MASK : 0;
5995 ++ /* Is it a write fault? */
5996 ++ error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
5997 ++ ? PFERR_WRITE_MASK : 0;
5998 ++ /* Is it a fetch fault? */
5999 ++ error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
6000 ++ ? PFERR_FETCH_MASK : 0;
6001 ++ /* ept page table entry is present? */
6002 ++ error_code |= (exit_qualification &
6003 ++ (EPT_VIOLATION_READABLE | EPT_VIOLATION_WRITABLE |
6004 ++ EPT_VIOLATION_EXECUTABLE))
6005 ++ ? PFERR_PRESENT_MASK : 0;
6006 ++
6007 ++ error_code |= (exit_qualification & 0x100) != 0 ?
6008 ++ PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
6009 ++
6010 ++ vcpu->arch.exit_qualification = exit_qualification;
6011 ++ return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
6012 ++}
6013 ++
6014 ++static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
6015 ++{
6016 ++ gpa_t gpa;
6017 ++
6018 ++ /*
6019 ++ * A nested guest cannot optimize MMIO vmexits, because we have an
6020 ++ * nGPA here instead of the required GPA.
6021 ++ */
6022 ++ gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
6023 ++ if (!is_guest_mode(vcpu) &&
6024 ++ !kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
6025 ++ trace_kvm_fast_mmio(gpa);
6026 ++ return kvm_skip_emulated_instruction(vcpu);
6027 ++ }
6028 ++
6029 ++ return kvm_mmu_page_fault(vcpu, gpa, PFERR_RSVD_MASK, NULL, 0);
6030 ++}
6031 ++
6032 ++static int handle_nmi_window(struct kvm_vcpu *vcpu)
6033 ++{
6034 ++ WARN_ON_ONCE(!enable_vnmi);
6035 ++ exec_controls_clearbit(to_vmx(vcpu), CPU_BASED_NMI_WINDOW_EXITING);
6036 ++ ++vcpu->stat.nmi_window_exits;
6037 ++ kvm_make_request(KVM_REQ_EVENT, vcpu);
6038 ++
6039 ++ return 1;
6040 ++}
6041 ++
6042 ++static int handle_invalid_guest_state(struct kvm_vcpu *vcpu)
6043 ++{
6044 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
6045 ++ bool intr_window_requested;
6046 ++ unsigned count = 130;
6047 ++
6048 ++ /*
6049 ++ * We should never reach the point where we are emulating L2
6050 ++ * due to invalid guest state as that means we incorrectly
6051 ++ * allowed a nested VMEntry with an invalid vmcs12.
6052 ++ */
6053 ++ WARN_ON_ONCE(vmx->emulation_required && vmx->nested.nested_run_pending);
6054 ++
6055 ++ intr_window_requested = exec_controls_get(vmx) &
6056 ++ CPU_BASED_INTR_WINDOW_EXITING;
6057 ++
6058 ++ while (vmx->emulation_required && count-- != 0) {
6059 ++ if (intr_window_requested && vmx_interrupt_allowed(vcpu))
6060 ++ return handle_interrupt_window(&vmx->vcpu);
6061 ++
6062 ++ if (kvm_test_request(KVM_REQ_EVENT, vcpu))
6063 ++ return 1;
6064 ++
6065 ++ if (!kvm_emulate_instruction(vcpu, 0))
6066 ++ return 0;
6067 ++
6068 ++ if (vmx->emulation_required && !vmx->rmode.vm86_active &&
6069 ++ vcpu->arch.exception.pending) {
6070 ++ vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6071 ++ vcpu->run->internal.suberror =
6072 ++ KVM_INTERNAL_ERROR_EMULATION;
6073 ++ vcpu->run->internal.ndata = 0;
6074 ++ return 0;
6075 ++ }
6076 ++
6077 ++ if (vcpu->arch.halt_request) {
6078 ++ vcpu->arch.halt_request = 0;
6079 ++ return kvm_vcpu_halt(vcpu);
6080 ++ }
6081 ++
6082 ++ /*
6083 ++ * Note, return 1 and not 0, vcpu_run() is responsible for
6084 ++ * morphing the pending signal into the proper return code.
6085 ++ */
6086 ++ if (signal_pending(current))
6087 ++ return 1;
6088 ++
6089 ++ if (need_resched())
6090 ++ schedule();
6091 ++ }
6092 ++
6093 ++ return 1;
6094 ++}
6095 ++
6096 ++static void grow_ple_window(struct kvm_vcpu *vcpu)
6097 ++{
6098 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
6099 ++ unsigned int old = vmx->ple_window;
6100 ++
6101 ++ vmx->ple_window = __grow_ple_window(old, ple_window,
6102 ++ ple_window_grow,
6103 ++ ple_window_max);
6104 ++
6105 ++ if (vmx->ple_window != old) {
6106 ++ vmx->ple_window_dirty = true;
6107 ++ trace_kvm_ple_window_update(vcpu->vcpu_id,
6108 ++ vmx->ple_window, old);
6109 ++ }
6110 ++}
6111 ++
6112 ++static void shrink_ple_window(struct kvm_vcpu *vcpu)
6113 ++{
6114 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
6115 ++ unsigned int old = vmx->ple_window;
6116 ++
6117 ++ vmx->ple_window = __shrink_ple_window(old, ple_window,
6118 ++ ple_window_shrink,
6119 ++ ple_window);
6120 ++
6121 ++ if (vmx->ple_window != old) {
6122 ++ vmx->ple_window_dirty = true;
6123 ++ trace_kvm_ple_window_update(vcpu->vcpu_id,
6124 ++ vmx->ple_window, old);
6125 ++ }
6126 ++}
6127 ++
6128 ++/*
6129 ++ * Handler for POSTED_INTERRUPT_WAKEUP_VECTOR.
6130 ++ */
6131 ++static void wakeup_handler(void)
6132 ++{
6133 ++ struct kvm_vcpu *vcpu;
6134 ++ int cpu = smp_processor_id();
6135 ++
6136 ++ spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6137 ++ list_for_each_entry(vcpu, &per_cpu(blocked_vcpu_on_cpu, cpu),
6138 ++ blocked_vcpu_list) {
6139 ++ struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
6140 ++
6141 ++ if (pi_test_on(pi_desc) == 1)
6142 ++ kvm_vcpu_kick(vcpu);
6143 ++ }
6144 ++ spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
6145 ++}
6146 ++
6147 ++static void vmx_enable_tdp(void)
6148 ++{
6149 ++ kvm_mmu_set_mask_ptes(VMX_EPT_READABLE_MASK,
6150 ++ enable_ept_ad_bits ? VMX_EPT_ACCESS_BIT : 0ull,
6151 ++ enable_ept_ad_bits ? VMX_EPT_DIRTY_BIT : 0ull,
6152 ++ 0ull, VMX_EPT_EXECUTABLE_MASK,
6153 ++ cpu_has_vmx_ept_execute_only() ? 0ull : VMX_EPT_READABLE_MASK,
6154 ++ VMX_EPT_RWX_MASK, 0ull);
6155 ++
6156 ++ ept_set_mmio_spte_mask();
6157 ++ kvm_enable_tdp();
6158 ++}
6159 ++
6160 ++/*
6161 ++ * Indicate a busy-waiting vcpu in spinlock. We do not enable the PAUSE
6162 ++ * exiting, so only get here on cpu with PAUSE-Loop-Exiting.
6163 ++ */
6164 ++static int handle_pause(struct kvm_vcpu *vcpu)
6165 ++{
6166 ++ if (!kvm_pause_in_guest(vcpu->kvm))
6167 ++ grow_ple_window(vcpu);
6168 ++
6169 ++ /*
6170 ++ * Intel sdm vol3 ch-25.1.3 says: The "PAUSE-loop exiting"
6171 ++ * VM-execution control is ignored if CPL > 0. OTOH, KVM
6172 ++ * never set PAUSE_EXITING and just set PLE if supported,
6173 ++ * so the vcpu must be CPL=0 if it gets a PAUSE exit.
6174 ++ */
6175 ++ kvm_vcpu_on_spin(vcpu, true);
6176 ++ return kvm_skip_emulated_instruction(vcpu);
6177 ++}
6178 ++
6179 ++static int handle_nop(struct kvm_vcpu *vcpu)
6180 ++{
6181 ++ return kvm_skip_emulated_instruction(vcpu);
6182 ++}
6183 ++
6184 ++static int handle_mwait(struct kvm_vcpu *vcpu)
6185 ++{
6186 ++ printk_once(KERN_WARNING "kvm: MWAIT instruction emulated as NOP!\n");
6187 ++ return handle_nop(vcpu);
6188 ++}
6189 ++
6190 ++static int handle_invalid_op(struct kvm_vcpu *vcpu)
6191 ++{
6192 ++ kvm_queue_exception(vcpu, UD_VECTOR);
6193 ++ return 1;
6194 ++}
6195 ++
6196 ++static int handle_monitor_trap(struct kvm_vcpu *vcpu)
6197 ++{
6198 ++ return 1;
6199 ++}
6200 ++
6201 ++static int handle_monitor(struct kvm_vcpu *vcpu)
6202 ++{
6203 ++ printk_once(KERN_WARNING "kvm: MONITOR instruction emulated as NOP!\n");
6204 ++ return handle_nop(vcpu);
6205 ++}
6206 ++
6207 ++static int handle_invpcid(struct kvm_vcpu *vcpu)
6208 ++{
6209 ++ u32 vmx_instruction_info;
6210 ++ unsigned long type;
6211 ++ bool pcid_enabled;
6212 ++ gva_t gva;
6213 ++ struct x86_exception e;
6214 ++ unsigned i;
6215 ++ unsigned long roots_to_free = 0;
6216 ++ struct {
6217 ++ u64 pcid;
6218 ++ u64 gla;
6219 ++ } operand;
6220 ++
6221 ++ if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
6222 ++ kvm_queue_exception(vcpu, UD_VECTOR);
6223 ++ return 1;
6224 ++ }
6225 ++
6226 ++ vmx_instruction_info = vmcs_read32(VMX_INSTRUCTION_INFO);
6227 ++ type = kvm_register_readl(vcpu, (vmx_instruction_info >> 28) & 0xf);
6228 ++
6229 ++ if (type > 3) {
6230 ++ kvm_inject_gp(vcpu, 0);
6231 ++ return 1;
6232 ++ }
6233 ++
6234 ++ /* According to the Intel instruction reference, the memory operand
6235 ++ * is read even if it isn't needed (e.g., for type==all)
6236 ++ */
6237 ++ if (get_vmx_mem_address(vcpu, vmcs_readl(EXIT_QUALIFICATION),
6238 ++ vmx_instruction_info, false,
6239 ++ sizeof(operand), &gva))
6240 ++ return 1;
6241 ++
6242 ++ if (kvm_read_guest_virt(vcpu, gva, &operand, sizeof(operand), &e)) {
6243 ++ kvm_inject_page_fault(vcpu, &e);
6244 ++ return 1;
6245 ++ }
6246 ++
6247 ++ if (operand.pcid >> 12 != 0) {
6248 ++ kvm_inject_gp(vcpu, 0);
6249 ++ return 1;
6250 ++ }
6251 ++
6252 ++ pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
6253 ++
6254 ++ switch (type) {
6255 ++ case INVPCID_TYPE_INDIV_ADDR:
6256 ++ if ((!pcid_enabled && (operand.pcid != 0)) ||
6257 ++ is_noncanonical_address(operand.gla, vcpu)) {
6258 ++ kvm_inject_gp(vcpu, 0);
6259 ++ return 1;
6260 ++ }
6261 ++ kvm_mmu_invpcid_gva(vcpu, operand.gla, operand.pcid);
6262 ++ return kvm_skip_emulated_instruction(vcpu);
6263 ++
6264 ++ case INVPCID_TYPE_SINGLE_CTXT:
6265 ++ if (!pcid_enabled && (operand.pcid != 0)) {
6266 ++ kvm_inject_gp(vcpu, 0);
6267 ++ return 1;
6268 ++ }
6269 ++
6270 ++ if (kvm_get_active_pcid(vcpu) == operand.pcid) {
6271 ++ kvm_mmu_sync_roots(vcpu);
6272 ++ kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
6273 ++ }
6274 ++
6275 ++ for (i = 0; i < KVM_MMU_NUM_PREV_ROOTS; i++)
6276 ++ if (kvm_get_pcid(vcpu, vcpu->arch.mmu->prev_roots[i].cr3)
6277 ++ == operand.pcid)
6278 ++ roots_to_free |= KVM_MMU_ROOT_PREVIOUS(i);
6279 ++
6280 ++ kvm_mmu_free_roots(vcpu, vcpu->arch.mmu, roots_to_free);
6281 ++ /*
6282 ++ * If neither the current cr3 nor any of the prev_roots use the
6283 ++ * given PCID, then nothing needs to be done here because a
6284 ++ * resync will happen anyway before switching to any other CR3.
6285 ++ */
6286 ++
6287 ++ return kvm_skip_emulated_instruction(vcpu);
6288 ++
6289 ++ case INVPCID_TYPE_ALL_NON_GLOBAL:
6290 ++ /*
6291 ++ * Currently, KVM doesn't mark global entries in the shadow
6292 ++ * page tables, so a non-global flush just degenerates to a
6293 ++ * global flush. If needed, we could optimize this later by
6294 ++ * keeping track of global entries in shadow page tables.
6295 ++ */
6296 ++
6297 ++ /* fall-through */
6298 ++ case INVPCID_TYPE_ALL_INCL_GLOBAL:
6299 ++ kvm_mmu_unload(vcpu);
6300 ++ return kvm_skip_emulated_instruction(vcpu);
6301 ++
6302 ++ default:
6303 ++ BUG(); /* We have already checked above that type <= 3 */
6304 ++ }
6305 ++}
6306 ++
6307 ++static int handle_pml_full(struct kvm_vcpu *vcpu)
6308 ++{
6309 ++ unsigned long exit_qualification;
6310 ++
6311 ++ trace_kvm_pml_full(vcpu->vcpu_id);
6312 ++
6313 ++ exit_qualification = vmcs_readl(EXIT_QUALIFICATION);
6314 ++
6315 ++ /*
6316 ++ * PML buffer FULL happened while executing iret from NMI,
6317 ++ * "blocked by NMI" bit has to be set before next VM entry.
6318 ++ */
6319 ++ if (!(to_vmx(vcpu)->idt_vectoring_info & VECTORING_INFO_VALID_MASK) &&
6320 ++ enable_vnmi &&
6321 ++ (exit_qualification & INTR_INFO_UNBLOCK_NMI))
6322 ++ vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
6323 ++ GUEST_INTR_STATE_NMI);
6324 ++
6325 ++ /*
6326 ++ * PML buffer already flushed at beginning of VMEXIT. Nothing to do
6327 ++ * here.., and there's no userspace involvement needed for PML.
6328 ++ */
6329 ++ return 1;
6330 ++}
6331 ++
6332 ++static int handle_preemption_timer(struct kvm_vcpu *vcpu)
6333 ++{
6334 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
6335 ++
6336 ++ if (!vmx->req_immediate_exit &&
6337 ++ !unlikely(vmx->loaded_vmcs->hv_timer_soft_disabled))
6338 ++ kvm_lapic_expired_hv_timer(vcpu);
6339 ++
6340 ++ return 1;
6341 ++}
6342 ++
6343 ++/*
6344 ++ * When nested=0, all VMX instruction VM Exits filter here. The handlers
6345 ++ * are overwritten by nested_vmx_setup() when nested=1.
6346 ++ */
6347 ++static int handle_vmx_instruction(struct kvm_vcpu *vcpu)
6348 ++{
6349 ++ kvm_queue_exception(vcpu, UD_VECTOR);
6350 ++ return 1;
6351 ++}
6352 ++
6353 ++static int handle_encls(struct kvm_vcpu *vcpu)
6354 ++{
6355 ++ /*
6356 ++ * SGX virtualization is not yet supported. There is no software
6357 ++ * enable bit for SGX, so we have to trap ENCLS and inject a #UD
6358 ++ * to prevent the guest from executing ENCLS.
6359 ++ */
6360 ++ kvm_queue_exception(vcpu, UD_VECTOR);
6361 ++ return 1;
6362 ++}
6363 ++
6364 ++/*
6365 ++ * The exit handlers return 1 if the exit was handled fully and guest execution
6366 ++ * may resume. Otherwise they set the kvm_run parameter to indicate what needs
6367 ++ * to be done to userspace and return 0.
6368 ++ */
6369 ++static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {
6370 ++ [EXIT_REASON_EXCEPTION_NMI] = handle_exception_nmi,
6371 ++ [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt,
6372 ++ [EXIT_REASON_TRIPLE_FAULT] = handle_triple_fault,
6373 ++ [EXIT_REASON_NMI_WINDOW] = handle_nmi_window,
6374 ++ [EXIT_REASON_IO_INSTRUCTION] = handle_io,
6375 ++ [EXIT_REASON_CR_ACCESS] = handle_cr,
6376 ++ [EXIT_REASON_DR_ACCESS] = handle_dr,
6377 ++ [EXIT_REASON_CPUID] = kvm_emulate_cpuid,
6378 ++ [EXIT_REASON_MSR_READ] = kvm_emulate_rdmsr,
6379 ++ [EXIT_REASON_MSR_WRITE] = kvm_emulate_wrmsr,
6380 ++ [EXIT_REASON_INTERRUPT_WINDOW] = handle_interrupt_window,
6381 ++ [EXIT_REASON_HLT] = kvm_emulate_halt,
6382 ++ [EXIT_REASON_INVD] = handle_invd,
6383 ++ [EXIT_REASON_INVLPG] = handle_invlpg,
6384 ++ [EXIT_REASON_RDPMC] = handle_rdpmc,
6385 ++ [EXIT_REASON_VMCALL] = handle_vmcall,
6386 ++ [EXIT_REASON_VMCLEAR] = handle_vmx_instruction,
6387 ++ [EXIT_REASON_VMLAUNCH] = handle_vmx_instruction,
6388 ++ [EXIT_REASON_VMPTRLD] = handle_vmx_instruction,
6389 ++ [EXIT_REASON_VMPTRST] = handle_vmx_instruction,
6390 ++ [EXIT_REASON_VMREAD] = handle_vmx_instruction,
6391 ++ [EXIT_REASON_VMRESUME] = handle_vmx_instruction,
6392 ++ [EXIT_REASON_VMWRITE] = handle_vmx_instruction,
6393 ++ [EXIT_REASON_VMOFF] = handle_vmx_instruction,
6394 ++ [EXIT_REASON_VMON] = handle_vmx_instruction,
6395 ++ [EXIT_REASON_TPR_BELOW_THRESHOLD] = handle_tpr_below_threshold,
6396 ++ [EXIT_REASON_APIC_ACCESS] = handle_apic_access,
6397 ++ [EXIT_REASON_APIC_WRITE] = handle_apic_write,
6398 ++ [EXIT_REASON_EOI_INDUCED] = handle_apic_eoi_induced,
6399 ++ [EXIT_REASON_WBINVD] = handle_wbinvd,
6400 ++ [EXIT_REASON_XSETBV] = handle_xsetbv,
6401 ++ [EXIT_REASON_TASK_SWITCH] = handle_task_switch,
6402 ++ [EXIT_REASON_MCE_DURING_VMENTRY] = handle_machine_check,
6403 ++ [EXIT_REASON_GDTR_IDTR] = handle_desc,
6404 ++ [EXIT_REASON_LDTR_TR] = handle_desc,
6405 ++ [EXIT_REASON_EPT_VIOLATION] = handle_ept_violation,
6406 ++ [EXIT_REASON_EPT_MISCONFIG] = handle_ept_misconfig,
6407 ++ [EXIT_REASON_PAUSE_INSTRUCTION] = handle_pause,
6408 ++ [EXIT_REASON_MWAIT_INSTRUCTION] = handle_mwait,
6409 ++ [EXIT_REASON_MONITOR_TRAP_FLAG] = handle_monitor_trap,
6410 ++ [EXIT_REASON_MONITOR_INSTRUCTION] = handle_monitor,
6411 ++ [EXIT_REASON_INVEPT] = handle_vmx_instruction,
6412 ++ [EXIT_REASON_INVVPID] = handle_vmx_instruction,
6413 ++ [EXIT_REASON_RDRAND] = handle_invalid_op,
6414 ++ [EXIT_REASON_RDSEED] = handle_invalid_op,
6415 ++ [EXIT_REASON_PML_FULL] = handle_pml_full,
6416 ++ [EXIT_REASON_INVPCID] = handle_invpcid,
6417 ++ [EXIT_REASON_VMFUNC] = handle_vmx_instruction,
6418 ++ [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer,
6419 ++ [EXIT_REASON_ENCLS] = handle_encls,
6420 ++};
6421 ++
6422 ++static const int kvm_vmx_max_exit_handlers =
6423 ++ ARRAY_SIZE(kvm_vmx_exit_handlers);
6424 ++
6425 ++static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
6426 ++{
6427 ++ *info1 = vmcs_readl(EXIT_QUALIFICATION);
6428 ++ *info2 = vmcs_read32(VM_EXIT_INTR_INFO);
6429 ++}
6430 ++
6431 ++static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx)
6432 ++{
6433 ++ if (vmx->pml_pg) {
6434 ++ __free_page(vmx->pml_pg);
6435 ++ vmx->pml_pg = NULL;
6436 ++ }
6437 ++}
6438 ++
6439 ++static void vmx_flush_pml_buffer(struct kvm_vcpu *vcpu)
6440 ++{
6441 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
6442 ++ u64 *pml_buf;
6443 ++ u16 pml_idx;
6444 ++
6445 ++ pml_idx = vmcs_read16(GUEST_PML_INDEX);
6446 ++
6447 ++ /* Do nothing if PML buffer is empty */
6448 ++ if (pml_idx == (PML_ENTITY_NUM - 1))
6449 ++ return;
6450 ++
6451 ++ /* PML index always points to next available PML buffer entity */
6452 ++ if (pml_idx >= PML_ENTITY_NUM)
6453 ++ pml_idx = 0;
6454 ++ else
6455 ++ pml_idx++;
6456 ++
6457 ++ pml_buf = page_address(vmx->pml_pg);
6458 ++ for (; pml_idx < PML_ENTITY_NUM; pml_idx++) {
6459 ++ u64 gpa;
6460 ++
6461 ++ gpa = pml_buf[pml_idx];
6462 ++ WARN_ON(gpa & (PAGE_SIZE - 1));
6463 ++ kvm_vcpu_mark_page_dirty(vcpu, gpa >> PAGE_SHIFT);
6464 ++ }
6465 ++
6466 ++ /* reset PML index */
6467 ++ vmcs_write16(GUEST_PML_INDEX, PML_ENTITY_NUM - 1);
6468 ++}
6469 ++
6470 ++/*
6471 ++ * Flush all vcpus' PML buffer and update logged GPAs to dirty_bitmap.
6472 ++ * Called before reporting dirty_bitmap to userspace.
6473 ++ */
6474 ++static void kvm_flush_pml_buffers(struct kvm *kvm)
6475 ++{
6476 ++ int i;
6477 ++ struct kvm_vcpu *vcpu;
6478 ++ /*
6479 ++ * We only need to kick vcpu out of guest mode here, as PML buffer
6480 ++ * is flushed at beginning of all VMEXITs, and it's obvious that only
6481 ++ * vcpus running in guest are possible to have unflushed GPAs in PML
6482 ++ * buffer.
6483 ++ */
6484 ++ kvm_for_each_vcpu(i, vcpu, kvm)
6485 ++ kvm_vcpu_kick(vcpu);
6486 ++}
6487 ++
6488 ++static void vmx_dump_sel(char *name, uint32_t sel)
6489 ++{
6490 ++ pr_err("%s sel=0x%04x, attr=0x%05x, limit=0x%08x, base=0x%016lx\n",
6491 ++ name, vmcs_read16(sel),
6492 ++ vmcs_read32(sel + GUEST_ES_AR_BYTES - GUEST_ES_SELECTOR),
6493 ++ vmcs_read32(sel + GUEST_ES_LIMIT - GUEST_ES_SELECTOR),
6494 ++ vmcs_readl(sel + GUEST_ES_BASE - GUEST_ES_SELECTOR));
6495 ++}
6496 ++
6497 ++static void vmx_dump_dtsel(char *name, uint32_t limit)
6498 ++{
6499 ++ pr_err("%s limit=0x%08x, base=0x%016lx\n",
6500 ++ name, vmcs_read32(limit),
6501 ++ vmcs_readl(limit + GUEST_GDTR_BASE - GUEST_GDTR_LIMIT));
6502 ++}
6503 ++
6504 ++void dump_vmcs(void)
6505 ++{
6506 ++ u32 vmentry_ctl, vmexit_ctl;
6507 ++ u32 cpu_based_exec_ctrl, pin_based_exec_ctrl, secondary_exec_control;
6508 ++ unsigned long cr4;
6509 ++ u64 efer;
6510 ++ int i, n;
6511 ++
6512 ++ if (!dump_invalid_vmcs) {
6513 ++ pr_warn_ratelimited("set kvm_intel.dump_invalid_vmcs=1 to dump internal KVM state.\n");
6514 ++ return;
6515 ++ }
6516 ++
6517 ++ vmentry_ctl = vmcs_read32(VM_ENTRY_CONTROLS);
6518 ++ vmexit_ctl = vmcs_read32(VM_EXIT_CONTROLS);
6519 ++ cpu_based_exec_ctrl = vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
6520 ++ pin_based_exec_ctrl = vmcs_read32(PIN_BASED_VM_EXEC_CONTROL);
6521 ++ cr4 = vmcs_readl(GUEST_CR4);
6522 ++ efer = vmcs_read64(GUEST_IA32_EFER);
6523 ++ secondary_exec_control = 0;
6524 ++ if (cpu_has_secondary_exec_ctrls())
6525 ++ secondary_exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL);
6526 ++
6527 ++ pr_err("*** Guest State ***\n");
6528 ++ pr_err("CR0: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
6529 ++ vmcs_readl(GUEST_CR0), vmcs_readl(CR0_READ_SHADOW),
6530 ++ vmcs_readl(CR0_GUEST_HOST_MASK));
6531 ++ pr_err("CR4: actual=0x%016lx, shadow=0x%016lx, gh_mask=%016lx\n",
6532 ++ cr4, vmcs_readl(CR4_READ_SHADOW), vmcs_readl(CR4_GUEST_HOST_MASK));
6533 ++ pr_err("CR3 = 0x%016lx\n", vmcs_readl(GUEST_CR3));
6534 ++ if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT) &&
6535 ++ (cr4 & X86_CR4_PAE) && !(efer & EFER_LMA))
6536 ++ {
6537 ++ pr_err("PDPTR0 = 0x%016llx PDPTR1 = 0x%016llx\n",
6538 ++ vmcs_read64(GUEST_PDPTR0), vmcs_read64(GUEST_PDPTR1));
6539 ++ pr_err("PDPTR2 = 0x%016llx PDPTR3 = 0x%016llx\n",
6540 ++ vmcs_read64(GUEST_PDPTR2), vmcs_read64(GUEST_PDPTR3));
6541 ++ }
6542 ++ pr_err("RSP = 0x%016lx RIP = 0x%016lx\n",
6543 ++ vmcs_readl(GUEST_RSP), vmcs_readl(GUEST_RIP));
6544 ++ pr_err("RFLAGS=0x%08lx DR7 = 0x%016lx\n",
6545 ++ vmcs_readl(GUEST_RFLAGS), vmcs_readl(GUEST_DR7));
6546 ++ pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
6547 ++ vmcs_readl(GUEST_SYSENTER_ESP),
6548 ++ vmcs_read32(GUEST_SYSENTER_CS), vmcs_readl(GUEST_SYSENTER_EIP));
6549 ++ vmx_dump_sel("CS: ", GUEST_CS_SELECTOR);
6550 ++ vmx_dump_sel("DS: ", GUEST_DS_SELECTOR);
6551 ++ vmx_dump_sel("SS: ", GUEST_SS_SELECTOR);
6552 ++ vmx_dump_sel("ES: ", GUEST_ES_SELECTOR);
6553 ++ vmx_dump_sel("FS: ", GUEST_FS_SELECTOR);
6554 ++ vmx_dump_sel("GS: ", GUEST_GS_SELECTOR);
6555 ++ vmx_dump_dtsel("GDTR:", GUEST_GDTR_LIMIT);
6556 ++ vmx_dump_sel("LDTR:", GUEST_LDTR_SELECTOR);
6557 ++ vmx_dump_dtsel("IDTR:", GUEST_IDTR_LIMIT);
6558 ++ vmx_dump_sel("TR: ", GUEST_TR_SELECTOR);
6559 ++ if ((vmexit_ctl & (VM_EXIT_SAVE_IA32_PAT | VM_EXIT_SAVE_IA32_EFER)) ||
6560 ++ (vmentry_ctl & (VM_ENTRY_LOAD_IA32_PAT | VM_ENTRY_LOAD_IA32_EFER)))
6561 ++ pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
6562 ++ efer, vmcs_read64(GUEST_IA32_PAT));
6563 ++ pr_err("DebugCtl = 0x%016llx DebugExceptions = 0x%016lx\n",
6564 ++ vmcs_read64(GUEST_IA32_DEBUGCTL),
6565 ++ vmcs_readl(GUEST_PENDING_DBG_EXCEPTIONS));
6566 ++ if (cpu_has_load_perf_global_ctrl() &&
6567 ++ vmentry_ctl & VM_ENTRY_LOAD_IA32_PERF_GLOBAL_CTRL)
6568 ++ pr_err("PerfGlobCtl = 0x%016llx\n",
6569 ++ vmcs_read64(GUEST_IA32_PERF_GLOBAL_CTRL));
6570 ++ if (vmentry_ctl & VM_ENTRY_LOAD_BNDCFGS)
6571 ++ pr_err("BndCfgS = 0x%016llx\n", vmcs_read64(GUEST_BNDCFGS));
6572 ++ pr_err("Interruptibility = %08x ActivityState = %08x\n",
6573 ++ vmcs_read32(GUEST_INTERRUPTIBILITY_INFO),
6574 ++ vmcs_read32(GUEST_ACTIVITY_STATE));
6575 ++ if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY)
6576 ++ pr_err("InterruptStatus = %04x\n",
6577 ++ vmcs_read16(GUEST_INTR_STATUS));
6578 ++
6579 ++ pr_err("*** Host State ***\n");
6580 ++ pr_err("RIP = 0x%016lx RSP = 0x%016lx\n",
6581 ++ vmcs_readl(HOST_RIP), vmcs_readl(HOST_RSP));
6582 ++ pr_err("CS=%04x SS=%04x DS=%04x ES=%04x FS=%04x GS=%04x TR=%04x\n",
6583 ++ vmcs_read16(HOST_CS_SELECTOR), vmcs_read16(HOST_SS_SELECTOR),
6584 ++ vmcs_read16(HOST_DS_SELECTOR), vmcs_read16(HOST_ES_SELECTOR),
6585 ++ vmcs_read16(HOST_FS_SELECTOR), vmcs_read16(HOST_GS_SELECTOR),
6586 ++ vmcs_read16(HOST_TR_SELECTOR));
6587 ++ pr_err("FSBase=%016lx GSBase=%016lx TRBase=%016lx\n",
6588 ++ vmcs_readl(HOST_FS_BASE), vmcs_readl(HOST_GS_BASE),
6589 ++ vmcs_readl(HOST_TR_BASE));
6590 ++ pr_err("GDTBase=%016lx IDTBase=%016lx\n",
6591 ++ vmcs_readl(HOST_GDTR_BASE), vmcs_readl(HOST_IDTR_BASE));
6592 ++ pr_err("CR0=%016lx CR3=%016lx CR4=%016lx\n",
6593 ++ vmcs_readl(HOST_CR0), vmcs_readl(HOST_CR3),
6594 ++ vmcs_readl(HOST_CR4));
6595 ++ pr_err("Sysenter RSP=%016lx CS:RIP=%04x:%016lx\n",
6596 ++ vmcs_readl(HOST_IA32_SYSENTER_ESP),
6597 ++ vmcs_read32(HOST_IA32_SYSENTER_CS),
6598 ++ vmcs_readl(HOST_IA32_SYSENTER_EIP));
6599 ++ if (vmexit_ctl & (VM_EXIT_LOAD_IA32_PAT | VM_EXIT_LOAD_IA32_EFER))
6600 ++ pr_err("EFER = 0x%016llx PAT = 0x%016llx\n",
6601 ++ vmcs_read64(HOST_IA32_EFER),
6602 ++ vmcs_read64(HOST_IA32_PAT));
6603 ++ if (cpu_has_load_perf_global_ctrl() &&
6604 ++ vmexit_ctl & VM_EXIT_LOAD_IA32_PERF_GLOBAL_CTRL)
6605 ++ pr_err("PerfGlobCtl = 0x%016llx\n",
6606 ++ vmcs_read64(HOST_IA32_PERF_GLOBAL_CTRL));
6607 ++
6608 ++ pr_err("*** Control State ***\n");
6609 ++ pr_err("PinBased=%08x CPUBased=%08x SecondaryExec=%08x\n",
6610 ++ pin_based_exec_ctrl, cpu_based_exec_ctrl, secondary_exec_control);
6611 ++ pr_err("EntryControls=%08x ExitControls=%08x\n", vmentry_ctl, vmexit_ctl);
6612 ++ pr_err("ExceptionBitmap=%08x PFECmask=%08x PFECmatch=%08x\n",
6613 ++ vmcs_read32(EXCEPTION_BITMAP),
6614 ++ vmcs_read32(PAGE_FAULT_ERROR_CODE_MASK),
6615 ++ vmcs_read32(PAGE_FAULT_ERROR_CODE_MATCH));
6616 ++ pr_err("VMEntry: intr_info=%08x errcode=%08x ilen=%08x\n",
6617 ++ vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
6618 ++ vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE),
6619 ++ vmcs_read32(VM_ENTRY_INSTRUCTION_LEN));
6620 ++ pr_err("VMExit: intr_info=%08x errcode=%08x ilen=%08x\n",
6621 ++ vmcs_read32(VM_EXIT_INTR_INFO),
6622 ++ vmcs_read32(VM_EXIT_INTR_ERROR_CODE),
6623 ++ vmcs_read32(VM_EXIT_INSTRUCTION_LEN));
6624 ++ pr_err(" reason=%08x qualification=%016lx\n",
6625 ++ vmcs_read32(VM_EXIT_REASON), vmcs_readl(EXIT_QUALIFICATION));
6626 ++ pr_err("IDTVectoring: info=%08x errcode=%08x\n",
6627 ++ vmcs_read32(IDT_VECTORING_INFO_FIELD),
6628 ++ vmcs_read32(IDT_VECTORING_ERROR_CODE));
6629 ++ pr_err("TSC Offset = 0x%016llx\n", vmcs_read64(TSC_OFFSET));
6630 ++ if (secondary_exec_control & SECONDARY_EXEC_TSC_SCALING)
6631 ++ pr_err("TSC Multiplier = 0x%016llx\n",
6632 ++ vmcs_read64(TSC_MULTIPLIER));
6633 ++ if (cpu_based_exec_ctrl & CPU_BASED_TPR_SHADOW) {
6634 ++ if (secondary_exec_control & SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY) {
6635 ++ u16 status = vmcs_read16(GUEST_INTR_STATUS);
6636 ++ pr_err("SVI|RVI = %02x|%02x ", status >> 8, status & 0xff);
6637 ++ }
6638 ++ pr_cont("TPR Threshold = 0x%02x\n", vmcs_read32(TPR_THRESHOLD));
6639 ++ if (secondary_exec_control & SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES)
6640 ++ pr_err("APIC-access addr = 0x%016llx ", vmcs_read64(APIC_ACCESS_ADDR));
6641 ++ pr_cont("virt-APIC addr = 0x%016llx\n", vmcs_read64(VIRTUAL_APIC_PAGE_ADDR));
6642 ++ }
6643 ++ if (pin_based_exec_ctrl & PIN_BASED_POSTED_INTR)
6644 ++ pr_err("PostedIntrVec = 0x%02x\n", vmcs_read16(POSTED_INTR_NV));
6645 ++ if ((secondary_exec_control & SECONDARY_EXEC_ENABLE_EPT))
6646 ++ pr_err("EPT pointer = 0x%016llx\n", vmcs_read64(EPT_POINTER));
6647 ++ n = vmcs_read32(CR3_TARGET_COUNT);
6648 ++ for (i = 0; i + 1 < n; i += 4)
6649 ++ pr_err("CR3 target%u=%016lx target%u=%016lx\n",
6650 ++ i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2),
6651 ++ i + 1, vmcs_readl(CR3_TARGET_VALUE0 + i * 2 + 2));
6652 ++ if (i < n)
6653 ++ pr_err("CR3 target%u=%016lx\n",
6654 ++ i, vmcs_readl(CR3_TARGET_VALUE0 + i * 2));
6655 ++ if (secondary_exec_control & SECONDARY_EXEC_PAUSE_LOOP_EXITING)
6656 ++ pr_err("PLE Gap=%08x Window=%08x\n",
6657 ++ vmcs_read32(PLE_GAP), vmcs_read32(PLE_WINDOW));
6658 ++ if (secondary_exec_control & SECONDARY_EXEC_ENABLE_VPID)
6659 ++ pr_err("Virtual processor ID = 0x%04x\n",
6660 ++ vmcs_read16(VIRTUAL_PROCESSOR_ID));
6661 ++}
6662 ++
6663 ++/*
6664 ++ * The guest has exited. See if we can fix it or if we need userspace
6665 ++ * assistance.
6666 ++ */
6667 ++static int vmx_handle_exit(struct kvm_vcpu *vcpu,
6668 ++ enum exit_fastpath_completion exit_fastpath)
6669 ++{
6670 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
6671 ++ u32 exit_reason = vmx->exit_reason;
6672 ++ u32 vectoring_info = vmx->idt_vectoring_info;
6673 ++
6674 ++ trace_kvm_exit(exit_reason, vcpu, KVM_ISA_VMX);
6675 ++
6676 ++ /*
6677 ++ * Flush logged GPAs PML buffer, this will make dirty_bitmap more
6678 ++ * updated. Another good is, in kvm_vm_ioctl_get_dirty_log, before
6679 ++ * querying dirty_bitmap, we only need to kick all vcpus out of guest
6680 ++ * mode as if vcpus is in root mode, the PML buffer must has been
6681 ++ * flushed already.
6682 ++ */
6683 ++ if (enable_pml)
6684 ++ vmx_flush_pml_buffer(vcpu);
6685 ++
6686 ++ /* If guest state is invalid, start emulating */
6687 ++ if (vmx->emulation_required)
6688 ++ return handle_invalid_guest_state(vcpu);
6689 ++
6690 ++ if (is_guest_mode(vcpu) && nested_vmx_exit_reflected(vcpu, exit_reason))
6691 ++ return nested_vmx_reflect_vmexit(vcpu, exit_reason);
6692 ++
6693 ++ if (exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) {
6694 ++ dump_vmcs();
6695 ++ vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
6696 ++ vcpu->run->fail_entry.hardware_entry_failure_reason
6697 ++ = exit_reason;
6698 ++ return 0;
6699 ++ }
6700 ++
6701 ++ if (unlikely(vmx->fail)) {
6702 ++ dump_vmcs();
6703 ++ vcpu->run->exit_reason = KVM_EXIT_FAIL_ENTRY;
6704 ++ vcpu->run->fail_entry.hardware_entry_failure_reason
6705 ++ = vmcs_read32(VM_INSTRUCTION_ERROR);
6706 ++ return 0;
6707 ++ }
6708 ++
6709 ++ /*
6710 ++ * Note:
6711 ++ * Do not try to fix EXIT_REASON_EPT_MISCONFIG if it caused by
6712 ++ * delivery event since it indicates guest is accessing MMIO.
6713 ++ * The vm-exit can be triggered again after return to guest that
6714 ++ * will cause infinite loop.
6715 ++ */
6716 ++ if ((vectoring_info & VECTORING_INFO_VALID_MASK) &&
6717 ++ (exit_reason != EXIT_REASON_EXCEPTION_NMI &&
6718 ++ exit_reason != EXIT_REASON_EPT_VIOLATION &&
6719 ++ exit_reason != EXIT_REASON_PML_FULL &&
6720 ++ exit_reason != EXIT_REASON_TASK_SWITCH)) {
6721 ++ vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6722 ++ vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_DELIVERY_EV;
6723 ++ vcpu->run->internal.ndata = 3;
6724 ++ vcpu->run->internal.data[0] = vectoring_info;
6725 ++ vcpu->run->internal.data[1] = exit_reason;
6726 ++ vcpu->run->internal.data[2] = vcpu->arch.exit_qualification;
6727 ++ if (exit_reason == EXIT_REASON_EPT_MISCONFIG) {
6728 ++ vcpu->run->internal.ndata++;
6729 ++ vcpu->run->internal.data[3] =
6730 ++ vmcs_read64(GUEST_PHYSICAL_ADDRESS);
6731 ++ }
6732 ++ return 0;
6733 ++ }
6734 ++
6735 ++ if (unlikely(!enable_vnmi &&
6736 ++ vmx->loaded_vmcs->soft_vnmi_blocked)) {
6737 ++ if (vmx_interrupt_allowed(vcpu)) {
6738 ++ vmx->loaded_vmcs->soft_vnmi_blocked = 0;
6739 ++ } else if (vmx->loaded_vmcs->vnmi_blocked_time > 1000000000LL &&
6740 ++ vcpu->arch.nmi_pending) {
6741 ++ /*
6742 ++ * This CPU don't support us in finding the end of an
6743 ++ * NMI-blocked window if the guest runs with IRQs
6744 ++ * disabled. So we pull the trigger after 1 s of
6745 ++ * futile waiting, but inform the user about this.
6746 ++ */
6747 ++ printk(KERN_WARNING "%s: Breaking out of NMI-blocked "
6748 ++ "state on VCPU %d after 1 s timeout\n",
6749 ++ __func__, vcpu->vcpu_id);
6750 ++ vmx->loaded_vmcs->soft_vnmi_blocked = 0;
6751 ++ }
6752 ++ }
6753 ++
6754 ++ if (exit_fastpath == EXIT_FASTPATH_SKIP_EMUL_INS) {
6755 ++ kvm_skip_emulated_instruction(vcpu);
6756 ++ return 1;
6757 ++ } else if (exit_reason < kvm_vmx_max_exit_handlers
6758 ++ && kvm_vmx_exit_handlers[exit_reason]) {
6759 ++#ifdef CONFIG_RETPOLINE
6760 ++ if (exit_reason == EXIT_REASON_MSR_WRITE)
6761 ++ return kvm_emulate_wrmsr(vcpu);
6762 ++ else if (exit_reason == EXIT_REASON_PREEMPTION_TIMER)
6763 ++ return handle_preemption_timer(vcpu);
6764 ++ else if (exit_reason == EXIT_REASON_INTERRUPT_WINDOW)
6765 ++ return handle_interrupt_window(vcpu);
6766 ++ else if (exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT)
6767 ++ return handle_external_interrupt(vcpu);
6768 ++ else if (exit_reason == EXIT_REASON_HLT)
6769 ++ return kvm_emulate_halt(vcpu);
6770 ++ else if (exit_reason == EXIT_REASON_EPT_MISCONFIG)
6771 ++ return handle_ept_misconfig(vcpu);
6772 ++#endif
6773 ++ return kvm_vmx_exit_handlers[exit_reason](vcpu);
6774 ++ } else {
6775 ++ vcpu_unimpl(vcpu, "vmx: unexpected exit reason 0x%x\n",
6776 ++ exit_reason);
6777 ++ dump_vmcs();
6778 ++ vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6779 ++ vcpu->run->internal.suberror =
6780 ++ KVM_INTERNAL_ERROR_UNEXPECTED_EXIT_REASON;
6781 ++ vcpu->run->internal.ndata = 1;
6782 ++ vcpu->run->internal.data[0] = exit_reason;
6783 ++ return 0;
6784 ++ }
6785 ++}
6786 ++
6787 ++/*
6788 ++ * Software based L1D cache flush which is used when microcode providing
6789 ++ * the cache control MSR is not loaded.
6790 ++ *
6791 ++ * The L1D cache is 32 KiB on Nehalem and later microarchitectures, but to
6792 ++ * flush it is required to read in 64 KiB because the replacement algorithm
6793 ++ * is not exactly LRU. This could be sized at runtime via topology
6794 ++ * information but as all relevant affected CPUs have 32KiB L1D cache size
6795 ++ * there is no point in doing so.
6796 ++ */
6797 ++static void vmx_l1d_flush(struct kvm_vcpu *vcpu)
6798 ++{
6799 ++ int size = PAGE_SIZE << L1D_CACHE_ORDER;
6800 ++
6801 ++ /*
6802 ++ * This code is only executed when the the flush mode is 'cond' or
6803 ++ * 'always'
6804 ++ */
6805 ++ if (static_branch_likely(&vmx_l1d_flush_cond)) {
6806 ++ bool flush_l1d;
6807 ++
6808 ++ /*
6809 ++ * Clear the per-vcpu flush bit, it gets set again
6810 ++ * either from vcpu_run() or from one of the unsafe
6811 ++ * VMEXIT handlers.
6812 ++ */
6813 ++ flush_l1d = vcpu->arch.l1tf_flush_l1d;
6814 ++ vcpu->arch.l1tf_flush_l1d = false;
6815 ++
6816 ++ /*
6817 ++ * Clear the per-cpu flush bit, it gets set again from
6818 ++ * the interrupt handlers.
6819 ++ */
6820 ++ flush_l1d |= kvm_get_cpu_l1tf_flush_l1d();
6821 ++ kvm_clear_cpu_l1tf_flush_l1d();
6822 ++
6823 ++ if (!flush_l1d)
6824 ++ return;
6825 ++ }
6826 ++
6827 ++ vcpu->stat.l1d_flush++;
6828 ++
6829 ++ if (static_cpu_has(X86_FEATURE_FLUSH_L1D)) {
6830 ++ wrmsrl(MSR_IA32_FLUSH_CMD, L1D_FLUSH);
6831 ++ return;
6832 ++ }
6833 ++
6834 ++ asm volatile(
6835 ++ /* First ensure the pages are in the TLB */
6836 ++ "xorl %%eax, %%eax\n"
6837 ++ ".Lpopulate_tlb:\n\t"
6838 ++ "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
6839 ++ "addl $4096, %%eax\n\t"
6840 ++ "cmpl %%eax, %[size]\n\t"
6841 ++ "jne .Lpopulate_tlb\n\t"
6842 ++ "xorl %%eax, %%eax\n\t"
6843 ++ "cpuid\n\t"
6844 ++ /* Now fill the cache */
6845 ++ "xorl %%eax, %%eax\n"
6846 ++ ".Lfill_cache:\n"
6847 ++ "movzbl (%[flush_pages], %%" _ASM_AX "), %%ecx\n\t"
6848 ++ "addl $64, %%eax\n\t"
6849 ++ "cmpl %%eax, %[size]\n\t"
6850 ++ "jne .Lfill_cache\n\t"
6851 ++ "lfence\n"
6852 ++ :: [flush_pages] "r" (vmx_l1d_flush_pages),
6853 ++ [size] "r" (size)
6854 ++ : "eax", "ebx", "ecx", "edx");
6855 ++}
6856 ++
6857 ++static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
6858 ++{
6859 ++ struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
6860 ++ int tpr_threshold;
6861 ++
6862 ++ if (is_guest_mode(vcpu) &&
6863 ++ nested_cpu_has(vmcs12, CPU_BASED_TPR_SHADOW))
6864 ++ return;
6865 ++
6866 ++ tpr_threshold = (irr == -1 || tpr < irr) ? 0 : irr;
6867 ++ if (is_guest_mode(vcpu))
6868 ++ to_vmx(vcpu)->nested.l1_tpr_threshold = tpr_threshold;
6869 ++ else
6870 ++ vmcs_write32(TPR_THRESHOLD, tpr_threshold);
6871 ++}
6872 ++
6873 ++void vmx_set_virtual_apic_mode(struct kvm_vcpu *vcpu)
6874 ++{
6875 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
6876 ++ u32 sec_exec_control;
6877 ++
6878 ++ if (!lapic_in_kernel(vcpu))
6879 ++ return;
6880 ++
6881 ++ if (!flexpriority_enabled &&
6882 ++ !cpu_has_vmx_virtualize_x2apic_mode())
6883 ++ return;
6884 ++
6885 ++ /* Postpone execution until vmcs01 is the current VMCS. */
6886 ++ if (is_guest_mode(vcpu)) {
6887 ++ vmx->nested.change_vmcs01_virtual_apic_mode = true;
6888 ++ return;
6889 ++ }
6890 ++
6891 ++ sec_exec_control = secondary_exec_controls_get(vmx);
6892 ++ sec_exec_control &= ~(SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
6893 ++ SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE);
6894 ++
6895 ++ switch (kvm_get_apic_mode(vcpu)) {
6896 ++ case LAPIC_MODE_INVALID:
6897 ++ WARN_ONCE(true, "Invalid local APIC state");
6898 ++ case LAPIC_MODE_DISABLED:
6899 ++ break;
6900 ++ case LAPIC_MODE_XAPIC:
6901 ++ if (flexpriority_enabled) {
6902 ++ sec_exec_control |=
6903 ++ SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES;
6904 ++ vmx_flush_tlb(vcpu, true);
6905 ++ }
6906 ++ break;
6907 ++ case LAPIC_MODE_X2APIC:
6908 ++ if (cpu_has_vmx_virtualize_x2apic_mode())
6909 ++ sec_exec_control |=
6910 ++ SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE;
6911 ++ break;
6912 ++ }
6913 ++ secondary_exec_controls_set(vmx, sec_exec_control);
6914 ++
6915 ++ vmx_update_msr_bitmap(vcpu);
6916 ++}
6917 ++
6918 ++static void vmx_set_apic_access_page_addr(struct kvm_vcpu *vcpu, hpa_t hpa)
6919 ++{
6920 ++ if (!is_guest_mode(vcpu)) {
6921 ++ vmcs_write64(APIC_ACCESS_ADDR, hpa);
6922 ++ vmx_flush_tlb(vcpu, true);
6923 ++ }
6924 ++}
6925 ++
6926 ++static void vmx_hwapic_isr_update(struct kvm_vcpu *vcpu, int max_isr)
6927 ++{
6928 ++ u16 status;
6929 ++ u8 old;
6930 ++
6931 ++ if (max_isr == -1)
6932 ++ max_isr = 0;
6933 ++
6934 ++ status = vmcs_read16(GUEST_INTR_STATUS);
6935 ++ old = status >> 8;
6936 ++ if (max_isr != old) {
6937 ++ status &= 0xff;
6938 ++ status |= max_isr << 8;
6939 ++ vmcs_write16(GUEST_INTR_STATUS, status);
6940 ++ }
6941 ++}
6942 ++
6943 ++static void vmx_set_rvi(int vector)
6944 ++{
6945 ++ u16 status;
6946 ++ u8 old;
6947 ++
6948 ++ if (vector == -1)
6949 ++ vector = 0;
6950 ++
6951 ++ status = vmcs_read16(GUEST_INTR_STATUS);
6952 ++ old = (u8)status & 0xff;
6953 ++ if ((u8)vector != old) {
6954 ++ status &= ~0xff;
6955 ++ status |= (u8)vector;
6956 ++ vmcs_write16(GUEST_INTR_STATUS, status);
6957 ++ }
6958 ++}
6959 ++
6960 ++static void vmx_hwapic_irr_update(struct kvm_vcpu *vcpu, int max_irr)
6961 ++{
6962 ++ /*
6963 ++ * When running L2, updating RVI is only relevant when
6964 ++ * vmcs12 virtual-interrupt-delivery enabled.
6965 ++ * However, it can be enabled only when L1 also
6966 ++ * intercepts external-interrupts and in that case
6967 ++ * we should not update vmcs02 RVI but instead intercept
6968 ++ * interrupt. Therefore, do nothing when running L2.
6969 ++ */
6970 ++ if (!is_guest_mode(vcpu))
6971 ++ vmx_set_rvi(max_irr);
6972 ++}
6973 ++
6974 ++static int vmx_sync_pir_to_irr(struct kvm_vcpu *vcpu)
6975 ++{
6976 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
6977 ++ int max_irr;
6978 ++ bool max_irr_updated;
6979 ++
6980 ++ WARN_ON(!vcpu->arch.apicv_active);
6981 ++ if (pi_test_on(&vmx->pi_desc)) {
6982 ++ pi_clear_on(&vmx->pi_desc);
6983 ++ /*
6984 ++ * IOMMU can write to PID.ON, so the barrier matters even on UP.
6985 ++ * But on x86 this is just a compiler barrier anyway.
6986 ++ */
6987 ++ smp_mb__after_atomic();
6988 ++ max_irr_updated =
6989 ++ kvm_apic_update_irr(vcpu, vmx->pi_desc.pir, &max_irr);
6990 ++
6991 ++ /*
6992 ++ * If we are running L2 and L1 has a new pending interrupt
6993 ++ * which can be injected, we should re-evaluate
6994 ++ * what should be done with this new L1 interrupt.
6995 ++ * If L1 intercepts external-interrupts, we should
6996 ++ * exit from L2 to L1. Otherwise, interrupt should be
6997 ++ * delivered directly to L2.
6998 ++ */
6999 ++ if (is_guest_mode(vcpu) && max_irr_updated) {
7000 ++ if (nested_exit_on_intr(vcpu))
7001 ++ kvm_vcpu_exiting_guest_mode(vcpu);
7002 ++ else
7003 ++ kvm_make_request(KVM_REQ_EVENT, vcpu);
7004 ++ }
7005 ++ } else {
7006 ++ max_irr = kvm_lapic_find_highest_irr(vcpu);
7007 ++ }
7008 ++ vmx_hwapic_irr_update(vcpu, max_irr);
7009 ++ return max_irr;
7010 ++}
7011 ++
7012 ++static bool vmx_dy_apicv_has_pending_interrupt(struct kvm_vcpu *vcpu)
7013 ++{
7014 ++ struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
7015 ++
7016 ++ return pi_test_on(pi_desc) ||
7017 ++ (pi_test_sn(pi_desc) && !pi_is_pir_empty(pi_desc));
7018 ++}
7019 ++
7020 ++static void vmx_load_eoi_exitmap(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap)
7021 ++{
7022 ++ if (!kvm_vcpu_apicv_active(vcpu))
7023 ++ return;
7024 ++
7025 ++ vmcs_write64(EOI_EXIT_BITMAP0, eoi_exit_bitmap[0]);
7026 ++ vmcs_write64(EOI_EXIT_BITMAP1, eoi_exit_bitmap[1]);
7027 ++ vmcs_write64(EOI_EXIT_BITMAP2, eoi_exit_bitmap[2]);
7028 ++ vmcs_write64(EOI_EXIT_BITMAP3, eoi_exit_bitmap[3]);
7029 ++}
7030 ++
7031 ++static void vmx_apicv_post_state_restore(struct kvm_vcpu *vcpu)
7032 ++{
7033 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
7034 ++
7035 ++ pi_clear_on(&vmx->pi_desc);
7036 ++ memset(vmx->pi_desc.pir, 0, sizeof(vmx->pi_desc.pir));
7037 ++}
7038 ++
7039 ++static void handle_exception_nmi_irqoff(struct vcpu_vmx *vmx)
7040 ++{
7041 ++ vmx->exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
7042 ++
7043 ++ /* if exit due to PF check for async PF */
7044 ++ if (is_page_fault(vmx->exit_intr_info))
7045 ++ vmx->vcpu.arch.apf.host_apf_reason = kvm_read_and_reset_pf_reason();
7046 ++
7047 ++ /* Handle machine checks before interrupts are enabled */
7048 ++ if (is_machine_check(vmx->exit_intr_info))
7049 ++ kvm_machine_check();
7050 ++
7051 ++ /* We need to handle NMIs before interrupts are enabled */
7052 ++ if (is_nmi(vmx->exit_intr_info)) {
7053 ++ kvm_before_interrupt(&vmx->vcpu);
7054 ++ asm("int $2");
7055 ++ kvm_after_interrupt(&vmx->vcpu);
7056 ++ }
7057 ++}
7058 ++
7059 ++static void handle_external_interrupt_irqoff(struct kvm_vcpu *vcpu)
7060 ++{
7061 ++ unsigned int vector;
7062 ++ unsigned long entry;
7063 ++#ifdef CONFIG_X86_64
7064 ++ unsigned long tmp;
7065 ++#endif
7066 ++ gate_desc *desc;
7067 ++ u32 intr_info;
7068 ++
7069 ++ intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
7070 ++ if (WARN_ONCE(!is_external_intr(intr_info),
7071 ++ "KVM: unexpected VM-Exit interrupt info: 0x%x", intr_info))
7072 ++ return;
7073 ++
7074 ++ vector = intr_info & INTR_INFO_VECTOR_MASK;
7075 ++ desc = (gate_desc *)host_idt_base + vector;
7076 ++ entry = gate_offset(desc);
7077 ++
7078 ++ kvm_before_interrupt(vcpu);
7079 ++
7080 ++ asm volatile(
7081 ++#ifdef CONFIG_X86_64
7082 ++ "mov %%" _ASM_SP ", %[sp]\n\t"
7083 ++ "and $0xfffffffffffffff0, %%" _ASM_SP "\n\t"
7084 ++ "push $%c[ss]\n\t"
7085 ++ "push %[sp]\n\t"
7086 ++#endif
7087 ++ "pushf\n\t"
7088 ++ __ASM_SIZE(push) " $%c[cs]\n\t"
7089 ++ CALL_NOSPEC
7090 ++ :
7091 ++#ifdef CONFIG_X86_64
7092 ++ [sp]"=&r"(tmp),
7093 ++#endif
7094 ++ ASM_CALL_CONSTRAINT
7095 ++ :
7096 ++ THUNK_TARGET(entry),
7097 ++ [ss]"i"(__KERNEL_DS),
7098 ++ [cs]"i"(__KERNEL_CS)
7099 ++ );
7100 ++
7101 ++ kvm_after_interrupt(vcpu);
7102 ++}
7103 ++STACK_FRAME_NON_STANDARD(handle_external_interrupt_irqoff);
7104 ++
7105 ++static void vmx_handle_exit_irqoff(struct kvm_vcpu *vcpu,
7106 ++ enum exit_fastpath_completion *exit_fastpath)
7107 ++{
7108 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
7109 ++
7110 ++ if (vmx->exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT)
7111 ++ handle_external_interrupt_irqoff(vcpu);
7112 ++ else if (vmx->exit_reason == EXIT_REASON_EXCEPTION_NMI)
7113 ++ handle_exception_nmi_irqoff(vmx);
7114 ++ else if (!is_guest_mode(vcpu) &&
7115 ++ vmx->exit_reason == EXIT_REASON_MSR_WRITE)
7116 ++ *exit_fastpath = handle_fastpath_set_msr_irqoff(vcpu);
7117 ++}
7118 ++
7119 ++static bool vmx_has_emulated_msr(int index)
7120 ++{
7121 ++ switch (index) {
7122 ++ case MSR_IA32_SMBASE:
7123 ++ /*
7124 ++ * We cannot do SMM unless we can run the guest in big
7125 ++ * real mode.
7126 ++ */
7127 ++ return enable_unrestricted_guest || emulate_invalid_guest_state;
7128 ++ case MSR_IA32_VMX_BASIC ... MSR_IA32_VMX_VMFUNC:
7129 ++ return nested;
7130 ++ case MSR_AMD64_VIRT_SPEC_CTRL:
7131 ++ /* This is AMD only. */
7132 ++ return false;
7133 ++ default:
7134 ++ return true;
7135 ++ }
7136 ++}
7137 ++
7138 ++static bool vmx_pt_supported(void)
7139 ++{
7140 ++ return pt_mode == PT_MODE_HOST_GUEST;
7141 ++}
7142 ++
7143 ++static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
7144 ++{
7145 ++ u32 exit_intr_info;
7146 ++ bool unblock_nmi;
7147 ++ u8 vector;
7148 ++ bool idtv_info_valid;
7149 ++
7150 ++ idtv_info_valid = vmx->idt_vectoring_info & VECTORING_INFO_VALID_MASK;
7151 ++
7152 ++ if (enable_vnmi) {
7153 ++ if (vmx->loaded_vmcs->nmi_known_unmasked)
7154 ++ return;
7155 ++ /*
7156 ++ * Can't use vmx->exit_intr_info since we're not sure what
7157 ++ * the exit reason is.
7158 ++ */
7159 ++ exit_intr_info = vmcs_read32(VM_EXIT_INTR_INFO);
7160 ++ unblock_nmi = (exit_intr_info & INTR_INFO_UNBLOCK_NMI) != 0;
7161 ++ vector = exit_intr_info & INTR_INFO_VECTOR_MASK;
7162 ++ /*
7163 ++ * SDM 3: 27.7.1.2 (September 2008)
7164 ++ * Re-set bit "block by NMI" before VM entry if vmexit caused by
7165 ++ * a guest IRET fault.
7166 ++ * SDM 3: 23.2.2 (September 2008)
7167 ++ * Bit 12 is undefined in any of the following cases:
7168 ++ * If the VM exit sets the valid bit in the IDT-vectoring
7169 ++ * information field.
7170 ++ * If the VM exit is due to a double fault.
7171 ++ */
7172 ++ if ((exit_intr_info & INTR_INFO_VALID_MASK) && unblock_nmi &&
7173 ++ vector != DF_VECTOR && !idtv_info_valid)
7174 ++ vmcs_set_bits(GUEST_INTERRUPTIBILITY_INFO,
7175 ++ GUEST_INTR_STATE_NMI);
7176 ++ else
7177 ++ vmx->loaded_vmcs->nmi_known_unmasked =
7178 ++ !(vmcs_read32(GUEST_INTERRUPTIBILITY_INFO)
7179 ++ & GUEST_INTR_STATE_NMI);
7180 ++ } else if (unlikely(vmx->loaded_vmcs->soft_vnmi_blocked))
7181 ++ vmx->loaded_vmcs->vnmi_blocked_time +=
7182 ++ ktime_to_ns(ktime_sub(ktime_get(),
7183 ++ vmx->loaded_vmcs->entry_time));
7184 ++}
7185 ++
7186 ++static void __vmx_complete_interrupts(struct kvm_vcpu *vcpu,
7187 ++ u32 idt_vectoring_info,
7188 ++ int instr_len_field,
7189 ++ int error_code_field)
7190 ++{
7191 ++ u8 vector;
7192 ++ int type;
7193 ++ bool idtv_info_valid;
7194 ++
7195 ++ idtv_info_valid = idt_vectoring_info & VECTORING_INFO_VALID_MASK;
7196 ++
7197 ++ vcpu->arch.nmi_injected = false;
7198 ++ kvm_clear_exception_queue(vcpu);
7199 ++ kvm_clear_interrupt_queue(vcpu);
7200 ++
7201 ++ if (!idtv_info_valid)
7202 ++ return;
7203 ++
7204 ++ kvm_make_request(KVM_REQ_EVENT, vcpu);
7205 ++
7206 ++ vector = idt_vectoring_info & VECTORING_INFO_VECTOR_MASK;
7207 ++ type = idt_vectoring_info & VECTORING_INFO_TYPE_MASK;
7208 ++
7209 ++ switch (type) {
7210 ++ case INTR_TYPE_NMI_INTR:
7211 ++ vcpu->arch.nmi_injected = true;
7212 ++ /*
7213 ++ * SDM 3: 27.7.1.2 (September 2008)
7214 ++ * Clear bit "block by NMI" before VM entry if a NMI
7215 ++ * delivery faulted.
7216 ++ */
7217 ++ vmx_set_nmi_mask(vcpu, false);
7218 ++ break;
7219 ++ case INTR_TYPE_SOFT_EXCEPTION:
7220 ++ vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
7221 ++ /* fall through */
7222 ++ case INTR_TYPE_HARD_EXCEPTION:
7223 ++ if (idt_vectoring_info & VECTORING_INFO_DELIVER_CODE_MASK) {
7224 ++ u32 err = vmcs_read32(error_code_field);
7225 ++ kvm_requeue_exception_e(vcpu, vector, err);
7226 ++ } else
7227 ++ kvm_requeue_exception(vcpu, vector);
7228 ++ break;
7229 ++ case INTR_TYPE_SOFT_INTR:
7230 ++ vcpu->arch.event_exit_inst_len = vmcs_read32(instr_len_field);
7231 ++ /* fall through */
7232 ++ case INTR_TYPE_EXT_INTR:
7233 ++ kvm_queue_interrupt(vcpu, vector, type == INTR_TYPE_SOFT_INTR);
7234 ++ break;
7235 ++ default:
7236 ++ break;
7237 ++ }
7238 ++}
7239 ++
7240 ++static void vmx_complete_interrupts(struct vcpu_vmx *vmx)
7241 ++{
7242 ++ __vmx_complete_interrupts(&vmx->vcpu, vmx->idt_vectoring_info,
7243 ++ VM_EXIT_INSTRUCTION_LEN,
7244 ++ IDT_VECTORING_ERROR_CODE);
7245 ++}
7246 ++
7247 ++static void vmx_cancel_injection(struct kvm_vcpu *vcpu)
7248 ++{
7249 ++ __vmx_complete_interrupts(vcpu,
7250 ++ vmcs_read32(VM_ENTRY_INTR_INFO_FIELD),
7251 ++ VM_ENTRY_INSTRUCTION_LEN,
7252 ++ VM_ENTRY_EXCEPTION_ERROR_CODE);
7253 ++
7254 ++ vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0);
7255 ++}
7256 ++
7257 ++static void atomic_switch_perf_msrs(struct vcpu_vmx *vmx)
7258 ++{
7259 ++ int i, nr_msrs;
7260 ++ struct perf_guest_switch_msr *msrs;
7261 ++
7262 ++ msrs = perf_guest_get_msrs(&nr_msrs);
7263 ++
7264 ++ if (!msrs)
7265 ++ return;
7266 ++
7267 ++ for (i = 0; i < nr_msrs; i++)
7268 ++ if (msrs[i].host == msrs[i].guest)
7269 ++ clear_atomic_switch_msr(vmx, msrs[i].msr);
7270 ++ else
7271 ++ add_atomic_switch_msr(vmx, msrs[i].msr, msrs[i].guest,
7272 ++ msrs[i].host, false);
7273 ++}
7274 ++
7275 ++static void atomic_switch_umwait_control_msr(struct vcpu_vmx *vmx)
7276 ++{
7277 ++ u32 host_umwait_control;
7278 ++
7279 ++ if (!vmx_has_waitpkg(vmx))
7280 ++ return;
7281 ++
7282 ++ host_umwait_control = get_umwait_control_msr();
7283 ++
7284 ++ if (vmx->msr_ia32_umwait_control != host_umwait_control)
7285 ++ add_atomic_switch_msr(vmx, MSR_IA32_UMWAIT_CONTROL,
7286 ++ vmx->msr_ia32_umwait_control,
7287 ++ host_umwait_control, false);
7288 ++ else
7289 ++ clear_atomic_switch_msr(vmx, MSR_IA32_UMWAIT_CONTROL);
7290 ++}
7291 ++
7292 ++static void vmx_update_hv_timer(struct kvm_vcpu *vcpu)
7293 ++{
7294 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
7295 ++ u64 tscl;
7296 ++ u32 delta_tsc;
7297 ++
7298 ++ if (vmx->req_immediate_exit) {
7299 ++ vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, 0);
7300 ++ vmx->loaded_vmcs->hv_timer_soft_disabled = false;
7301 ++ } else if (vmx->hv_deadline_tsc != -1) {
7302 ++ tscl = rdtsc();
7303 ++ if (vmx->hv_deadline_tsc > tscl)
7304 ++ /* set_hv_timer ensures the delta fits in 32-bits */
7305 ++ delta_tsc = (u32)((vmx->hv_deadline_tsc - tscl) >>
7306 ++ cpu_preemption_timer_multi);
7307 ++ else
7308 ++ delta_tsc = 0;
7309 ++
7310 ++ vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, delta_tsc);
7311 ++ vmx->loaded_vmcs->hv_timer_soft_disabled = false;
7312 ++ } else if (!vmx->loaded_vmcs->hv_timer_soft_disabled) {
7313 ++ vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, -1);
7314 ++ vmx->loaded_vmcs->hv_timer_soft_disabled = true;
7315 ++ }
7316 ++}
7317 ++
7318 ++void vmx_update_host_rsp(struct vcpu_vmx *vmx, unsigned long host_rsp)
7319 ++{
7320 ++ if (unlikely(host_rsp != vmx->loaded_vmcs->host_state.rsp)) {
7321 ++ vmx->loaded_vmcs->host_state.rsp = host_rsp;
7322 ++ vmcs_writel(HOST_RSP, host_rsp);
7323 ++ }
7324 ++}
7325 ++
7326 ++bool __vmx_vcpu_run(struct vcpu_vmx *vmx, unsigned long *regs, bool launched);
7327 ++
7328 ++static void vmx_vcpu_run(struct kvm_vcpu *vcpu)
7329 ++{
7330 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
7331 ++ unsigned long cr3, cr4;
7332 ++
7333 ++ /* Record the guest's net vcpu time for enforced NMI injections. */
7334 ++ if (unlikely(!enable_vnmi &&
7335 ++ vmx->loaded_vmcs->soft_vnmi_blocked))
7336 ++ vmx->loaded_vmcs->entry_time = ktime_get();
7337 ++
7338 ++ /* Don't enter VMX if guest state is invalid, let the exit handler
7339 ++ start emulation until we arrive back to a valid state */
7340 ++ if (vmx->emulation_required)
7341 ++ return;
7342 ++
7343 ++ if (vmx->ple_window_dirty) {
7344 ++ vmx->ple_window_dirty = false;
7345 ++ vmcs_write32(PLE_WINDOW, vmx->ple_window);
7346 ++ }
7347 ++
7348 ++ if (vmx->nested.need_vmcs12_to_shadow_sync)
7349 ++ nested_sync_vmcs12_to_shadow(vcpu);
7350 ++
7351 ++ if (kvm_register_is_dirty(vcpu, VCPU_REGS_RSP))
7352 ++ vmcs_writel(GUEST_RSP, vcpu->arch.regs[VCPU_REGS_RSP]);
7353 ++ if (kvm_register_is_dirty(vcpu, VCPU_REGS_RIP))
7354 ++ vmcs_writel(GUEST_RIP, vcpu->arch.regs[VCPU_REGS_RIP]);
7355 ++
7356 ++ cr3 = __get_current_cr3_fast();
7357 ++ if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
7358 ++ vmcs_writel(HOST_CR3, cr3);
7359 ++ vmx->loaded_vmcs->host_state.cr3 = cr3;
7360 ++ }
7361 ++
7362 ++ cr4 = cr4_read_shadow();
7363 ++ if (unlikely(cr4 != vmx->loaded_vmcs->host_state.cr4)) {
7364 ++ vmcs_writel(HOST_CR4, cr4);
7365 ++ vmx->loaded_vmcs->host_state.cr4 = cr4;
7366 ++ }
7367 ++
7368 ++ /* When single-stepping over STI and MOV SS, we must clear the
7369 ++ * corresponding interruptibility bits in the guest state. Otherwise
7370 ++ * vmentry fails as it then expects bit 14 (BS) in pending debug
7371 ++ * exceptions being set, but that's not correct for the guest debugging
7372 ++ * case. */
7373 ++ if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
7374 ++ vmx_set_interrupt_shadow(vcpu, 0);
7375 ++
7376 ++ kvm_load_guest_xsave_state(vcpu);
7377 ++
7378 ++ if (static_cpu_has(X86_FEATURE_PKU) &&
7379 ++ kvm_read_cr4_bits(vcpu, X86_CR4_PKE) &&
7380 ++ vcpu->arch.pkru != vmx->host_pkru)
7381 ++ __write_pkru(vcpu->arch.pkru);
7382 ++
7383 ++ pt_guest_enter(vmx);
7384 ++
7385 ++ atomic_switch_perf_msrs(vmx);
7386 ++ atomic_switch_umwait_control_msr(vmx);
7387 ++
7388 ++ if (enable_preemption_timer)
7389 ++ vmx_update_hv_timer(vcpu);
7390 ++
7391 ++ if (lapic_in_kernel(vcpu) &&
7392 ++ vcpu->arch.apic->lapic_timer.timer_advance_ns)
7393 ++ kvm_wait_lapic_expire(vcpu);
7394 ++
7395 ++ /*
7396 ++ * If this vCPU has touched SPEC_CTRL, restore the guest's value if
7397 ++ * it's non-zero. Since vmentry is serialising on affected CPUs, there
7398 ++ * is no need to worry about the conditional branch over the wrmsr
7399 ++ * being speculatively taken.
7400 ++ */
7401 ++ x86_spec_ctrl_set_guest(vmx->spec_ctrl, 0);
7402 ++
7403 ++ /* L1D Flush includes CPU buffer clear to mitigate MDS */
7404 ++ if (static_branch_unlikely(&vmx_l1d_should_flush))
7405 ++ vmx_l1d_flush(vcpu);
7406 ++ else if (static_branch_unlikely(&mds_user_clear))
7407 ++ mds_clear_cpu_buffers();
7408 ++
7409 ++ if (vcpu->arch.cr2 != read_cr2())
7410 ++ write_cr2(vcpu->arch.cr2);
7411 ++
7412 ++ vmx->fail = __vmx_vcpu_run(vmx, (unsigned long *)&vcpu->arch.regs,
7413 ++ vmx->loaded_vmcs->launched);
7414 ++
7415 ++ vcpu->arch.cr2 = read_cr2();
7416 ++
7417 ++ /*
7418 ++ * We do not use IBRS in the kernel. If this vCPU has used the
7419 ++ * SPEC_CTRL MSR it may have left it on; save the value and
7420 ++ * turn it off. This is much more efficient than blindly adding
7421 ++ * it to the atomic save/restore list. Especially as the former
7422 ++ * (Saving guest MSRs on vmexit) doesn't even exist in KVM.
7423 ++ *
7424 ++ * For non-nested case:
7425 ++ * If the L01 MSR bitmap does not intercept the MSR, then we need to
7426 ++ * save it.
7427 ++ *
7428 ++ * For nested case:
7429 ++ * If the L02 MSR bitmap does not intercept the MSR, then we need to
7430 ++ * save it.
7431 ++ */
7432 ++ if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL)))
7433 ++ vmx->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL);
7434 ++
7435 ++ x86_spec_ctrl_restore_host(vmx->spec_ctrl, 0);
7436 ++
7437 ++ /* All fields are clean at this point */
7438 ++ if (static_branch_unlikely(&enable_evmcs))
7439 ++ current_evmcs->hv_clean_fields |=
7440 ++ HV_VMX_ENLIGHTENED_CLEAN_FIELD_ALL;
7441 ++
7442 ++ if (static_branch_unlikely(&enable_evmcs))
7443 ++ current_evmcs->hv_vp_id = vcpu->arch.hyperv.vp_index;
7444 ++
7445 ++ /* MSR_IA32_DEBUGCTLMSR is zeroed on vmexit. Restore it if needed */
7446 ++ if (vmx->host_debugctlmsr)
7447 ++ update_debugctlmsr(vmx->host_debugctlmsr);
7448 ++
7449 ++#ifndef CONFIG_X86_64
7450 ++ /*
7451 ++ * The sysexit path does not restore ds/es, so we must set them to
7452 ++ * a reasonable value ourselves.
7453 ++ *
7454 ++ * We can't defer this to vmx_prepare_switch_to_host() since that
7455 ++ * function may be executed in interrupt context, which saves and
7456 ++ * restore segments around it, nullifying its effect.
7457 ++ */
7458 ++ loadsegment(ds, __USER_DS);
7459 ++ loadsegment(es, __USER_DS);
7460 ++#endif
7461 ++
7462 ++ vcpu->arch.regs_avail = ~((1 << VCPU_REGS_RIP) | (1 << VCPU_REGS_RSP)
7463 ++ | (1 << VCPU_EXREG_RFLAGS)
7464 ++ | (1 << VCPU_EXREG_PDPTR)
7465 ++ | (1 << VCPU_EXREG_SEGMENTS)
7466 ++ | (1 << VCPU_EXREG_CR3));
7467 ++ vcpu->arch.regs_dirty = 0;
7468 ++
7469 ++ pt_guest_exit(vmx);
7470 ++
7471 ++ /*
7472 ++ * eager fpu is enabled if PKEY is supported and CR4 is switched
7473 ++ * back on host, so it is safe to read guest PKRU from current
7474 ++ * XSAVE.
7475 ++ */
7476 ++ if (static_cpu_has(X86_FEATURE_PKU) &&
7477 ++ kvm_read_cr4_bits(vcpu, X86_CR4_PKE)) {
7478 ++ vcpu->arch.pkru = rdpkru();
7479 ++ if (vcpu->arch.pkru != vmx->host_pkru)
7480 ++ __write_pkru(vmx->host_pkru);
7481 ++ }
7482 ++
7483 ++ kvm_load_host_xsave_state(vcpu);
7484 ++
7485 ++ vmx->nested.nested_run_pending = 0;
7486 ++ vmx->idt_vectoring_info = 0;
7487 ++
7488 ++ vmx->exit_reason = vmx->fail ? 0xdead : vmcs_read32(VM_EXIT_REASON);
7489 ++ if ((u16)vmx->exit_reason == EXIT_REASON_MCE_DURING_VMENTRY)
7490 ++ kvm_machine_check();
7491 ++
7492 ++ if (vmx->fail || (vmx->exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY))
7493 ++ return;
7494 ++
7495 ++ vmx->loaded_vmcs->launched = 1;
7496 ++ vmx->idt_vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD);
7497 ++
7498 ++ vmx_recover_nmi_blocking(vmx);
7499 ++ vmx_complete_interrupts(vmx);
7500 ++}
7501 ++
7502 ++static struct kvm *vmx_vm_alloc(void)
7503 ++{
7504 ++ struct kvm_vmx *kvm_vmx = __vmalloc(sizeof(struct kvm_vmx),
7505 ++ GFP_KERNEL_ACCOUNT | __GFP_ZERO,
7506 ++ PAGE_KERNEL);
7507 ++ return &kvm_vmx->kvm;
7508 ++}
7509 ++
7510 ++static void vmx_vm_free(struct kvm *kvm)
7511 ++{
7512 ++ kfree(kvm->arch.hyperv.hv_pa_pg);
7513 ++ vfree(to_kvm_vmx(kvm));
7514 ++}
7515 ++
7516 ++static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
7517 ++{
7518 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
7519 ++
7520 ++ if (enable_pml)
7521 ++ vmx_destroy_pml_buffer(vmx);
7522 ++ free_vpid(vmx->vpid);
7523 ++ nested_vmx_free_vcpu(vcpu);
7524 ++ free_loaded_vmcs(vmx->loaded_vmcs);
7525 ++ kvm_vcpu_uninit(vcpu);
7526 ++ kmem_cache_free(x86_fpu_cache, vmx->vcpu.arch.user_fpu);
7527 ++ kmem_cache_free(x86_fpu_cache, vmx->vcpu.arch.guest_fpu);
7528 ++ kmem_cache_free(kvm_vcpu_cache, vmx);
7529 ++}
7530 ++
7531 ++static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
7532 ++{
7533 ++ int err;
7534 ++ struct vcpu_vmx *vmx;
7535 ++ unsigned long *msr_bitmap;
7536 ++ int i, cpu;
7537 ++
7538 ++ BUILD_BUG_ON_MSG(offsetof(struct vcpu_vmx, vcpu) != 0,
7539 ++ "struct kvm_vcpu must be at offset 0 for arch usercopy region");
7540 ++
7541 ++ vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL_ACCOUNT);
7542 ++ if (!vmx)
7543 ++ return ERR_PTR(-ENOMEM);
7544 ++
7545 ++ vmx->vcpu.arch.user_fpu = kmem_cache_zalloc(x86_fpu_cache,
7546 ++ GFP_KERNEL_ACCOUNT);
7547 ++ if (!vmx->vcpu.arch.user_fpu) {
7548 ++ printk(KERN_ERR "kvm: failed to allocate kvm userspace's fpu\n");
7549 ++ err = -ENOMEM;
7550 ++ goto free_partial_vcpu;
7551 ++ }
7552 ++
7553 ++ vmx->vcpu.arch.guest_fpu = kmem_cache_zalloc(x86_fpu_cache,
7554 ++ GFP_KERNEL_ACCOUNT);
7555 ++ if (!vmx->vcpu.arch.guest_fpu) {
7556 ++ printk(KERN_ERR "kvm: failed to allocate vcpu's fpu\n");
7557 ++ err = -ENOMEM;
7558 ++ goto free_user_fpu;
7559 ++ }
7560 ++
7561 ++ vmx->vpid = allocate_vpid();
7562 ++
7563 ++ err = kvm_vcpu_init(&vmx->vcpu, kvm, id);
7564 ++ if (err)
7565 ++ goto free_vcpu;
7566 ++
7567 ++ err = -ENOMEM;
7568 ++
7569 ++ /*
7570 ++ * If PML is turned on, failure on enabling PML just results in failure
7571 ++ * of creating the vcpu, therefore we can simplify PML logic (by
7572 ++ * avoiding dealing with cases, such as enabling PML partially on vcpus
7573 ++ * for the guest), etc.
7574 ++ */
7575 ++ if (enable_pml) {
7576 ++ vmx->pml_pg = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
7577 ++ if (!vmx->pml_pg)
7578 ++ goto uninit_vcpu;
7579 ++ }
7580 ++
7581 ++ BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) != NR_SHARED_MSRS);
7582 ++
7583 ++ for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
7584 ++ u32 index = vmx_msr_index[i];
7585 ++ u32 data_low, data_high;
7586 ++ int j = vmx->nmsrs;
7587 ++
7588 ++ if (rdmsr_safe(index, &data_low, &data_high) < 0)
7589 ++ continue;
7590 ++ if (wrmsr_safe(index, data_low, data_high) < 0)
7591 ++ continue;
7592 ++
7593 ++ vmx->guest_msrs[j].index = i;
7594 ++ vmx->guest_msrs[j].data = 0;
7595 ++ switch (index) {
7596 ++ case MSR_IA32_TSX_CTRL:
7597 ++ /*
7598 ++ * No need to pass TSX_CTRL_CPUID_CLEAR through, so
7599 ++ * let's avoid changing CPUID bits under the host
7600 ++ * kernel's feet.
7601 ++ */
7602 ++ vmx->guest_msrs[j].mask = ~(u64)TSX_CTRL_CPUID_CLEAR;
7603 ++ break;
7604 ++ default:
7605 ++ vmx->guest_msrs[j].mask = -1ull;
7606 ++ break;
7607 ++ }
7608 ++ ++vmx->nmsrs;
7609 ++ }
7610 ++
7611 ++ err = alloc_loaded_vmcs(&vmx->vmcs01);
7612 ++ if (err < 0)
7613 ++ goto free_pml;
7614 ++
7615 ++ msr_bitmap = vmx->vmcs01.msr_bitmap;
7616 ++ vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_TSC, MSR_TYPE_R);
7617 ++ vmx_disable_intercept_for_msr(msr_bitmap, MSR_FS_BASE, MSR_TYPE_RW);
7618 ++ vmx_disable_intercept_for_msr(msr_bitmap, MSR_GS_BASE, MSR_TYPE_RW);
7619 ++ vmx_disable_intercept_for_msr(msr_bitmap, MSR_KERNEL_GS_BASE, MSR_TYPE_RW);
7620 ++ vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW);
7621 ++ vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW);
7622 ++ vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW);
7623 ++ if (kvm_cstate_in_guest(kvm)) {
7624 ++ vmx_disable_intercept_for_msr(msr_bitmap, MSR_CORE_C1_RES, MSR_TYPE_R);
7625 ++ vmx_disable_intercept_for_msr(msr_bitmap, MSR_CORE_C3_RESIDENCY, MSR_TYPE_R);
7626 ++ vmx_disable_intercept_for_msr(msr_bitmap, MSR_CORE_C6_RESIDENCY, MSR_TYPE_R);
7627 ++ vmx_disable_intercept_for_msr(msr_bitmap, MSR_CORE_C7_RESIDENCY, MSR_TYPE_R);
7628 ++ }
7629 ++ vmx->msr_bitmap_mode = 0;
7630 ++
7631 ++ vmx->loaded_vmcs = &vmx->vmcs01;
7632 ++ cpu = get_cpu();
7633 ++ vmx_vcpu_load(&vmx->vcpu, cpu);
7634 ++ vmx->vcpu.cpu = cpu;
7635 ++ init_vmcs(vmx);
7636 ++ vmx_vcpu_put(&vmx->vcpu);
7637 ++ put_cpu();
7638 ++ if (cpu_need_virtualize_apic_accesses(&vmx->vcpu)) {
7639 ++ err = alloc_apic_access_page(kvm);
7640 ++ if (err)
7641 ++ goto free_vmcs;
7642 ++ }
7643 ++
7644 ++ if (enable_ept && !enable_unrestricted_guest) {
7645 ++ err = init_rmode_identity_map(kvm);
7646 ++ if (err)
7647 ++ goto free_vmcs;
7648 ++ }
7649 ++
7650 ++ if (nested)
7651 ++ nested_vmx_setup_ctls_msrs(&vmx->nested.msrs,
7652 ++ vmx_capability.ept,
7653 ++ kvm_vcpu_apicv_active(&vmx->vcpu));
7654 ++ else
7655 ++ memset(&vmx->nested.msrs, 0, sizeof(vmx->nested.msrs));
7656 ++
7657 ++ vmx->nested.posted_intr_nv = -1;
7658 ++ vmx->nested.current_vmptr = -1ull;
7659 ++
7660 ++ vmx->msr_ia32_feature_control_valid_bits = FEATURE_CONTROL_LOCKED;
7661 ++
7662 ++ /*
7663 ++ * Enforce invariant: pi_desc.nv is always either POSTED_INTR_VECTOR
7664 ++ * or POSTED_INTR_WAKEUP_VECTOR.
7665 ++ */
7666 ++ vmx->pi_desc.nv = POSTED_INTR_VECTOR;
7667 ++ vmx->pi_desc.sn = 1;
7668 ++
7669 ++ vmx->ept_pointer = INVALID_PAGE;
7670 ++
7671 ++ return &vmx->vcpu;
7672 ++
7673 ++free_vmcs:
7674 ++ free_loaded_vmcs(vmx->loaded_vmcs);
7675 ++free_pml:
7676 ++ vmx_destroy_pml_buffer(vmx);
7677 ++uninit_vcpu:
7678 ++ kvm_vcpu_uninit(&vmx->vcpu);
7679 ++free_vcpu:
7680 ++ free_vpid(vmx->vpid);
7681 ++ kmem_cache_free(x86_fpu_cache, vmx->vcpu.arch.guest_fpu);
7682 ++free_user_fpu:
7683 ++ kmem_cache_free(x86_fpu_cache, vmx->vcpu.arch.user_fpu);
7684 ++free_partial_vcpu:
7685 ++ kmem_cache_free(kvm_vcpu_cache, vmx);
7686 ++ return ERR_PTR(err);
7687 ++}
7688 ++
7689 ++#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"
7690 ++#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"
7691 ++
7692 ++static int vmx_vm_init(struct kvm *kvm)
7693 ++{
7694 ++ spin_lock_init(&to_kvm_vmx(kvm)->ept_pointer_lock);
7695 ++
7696 ++ if (!ple_gap)
7697 ++ kvm->arch.pause_in_guest = true;
7698 ++
7699 ++ if (boot_cpu_has(X86_BUG_L1TF) && enable_ept) {
7700 ++ switch (l1tf_mitigation) {
7701 ++ case L1TF_MITIGATION_OFF:
7702 ++ case L1TF_MITIGATION_FLUSH_NOWARN:
7703 ++ /* 'I explicitly don't care' is set */
7704 ++ break;
7705 ++ case L1TF_MITIGATION_FLUSH:
7706 ++ case L1TF_MITIGATION_FLUSH_NOSMT:
7707 ++ case L1TF_MITIGATION_FULL:
7708 ++ /*
7709 ++ * Warn upon starting the first VM in a potentially
7710 ++ * insecure environment.
7711 ++ */
7712 ++ if (sched_smt_active())
7713 ++ pr_warn_once(L1TF_MSG_SMT);
7714 ++ if (l1tf_vmx_mitigation == VMENTER_L1D_FLUSH_NEVER)
7715 ++ pr_warn_once(L1TF_MSG_L1D);
7716 ++ break;
7717 ++ case L1TF_MITIGATION_FULL_FORCE:
7718 ++ /* Flush is enforced */
7719 ++ break;
7720 ++ }
7721 ++ }
7722 ++ return 0;
7723 ++}
7724 ++
7725 ++static int __init vmx_check_processor_compat(void)
7726 ++{
7727 ++ struct vmcs_config vmcs_conf;
7728 ++ struct vmx_capability vmx_cap;
7729 ++
7730 ++ if (setup_vmcs_config(&vmcs_conf, &vmx_cap) < 0)
7731 ++ return -EIO;
7732 ++ if (nested)
7733 ++ nested_vmx_setup_ctls_msrs(&vmcs_conf.nested, vmx_cap.ept,
7734 ++ enable_apicv);
7735 ++ if (memcmp(&vmcs_config, &vmcs_conf, sizeof(struct vmcs_config)) != 0) {
7736 ++ printk(KERN_ERR "kvm: CPU %d feature inconsistency!\n",
7737 ++ smp_processor_id());
7738 ++ return -EIO;
7739 ++ }
7740 ++ return 0;
7741 ++}
7742 ++
7743 ++static u64 vmx_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
7744 ++{
7745 ++ u8 cache;
7746 ++ u64 ipat = 0;
7747 ++
7748 ++ /* For VT-d and EPT combination
7749 ++ * 1. MMIO: always map as UC
7750 ++ * 2. EPT with VT-d:
7751 ++ * a. VT-d without snooping control feature: can't guarantee the
7752 ++ * result, try to trust guest.
7753 ++ * b. VT-d with snooping control feature: snooping control feature of
7754 ++ * VT-d engine can guarantee the cache correctness. Just set it
7755 ++ * to WB to keep consistent with host. So the same as item 3.
7756 ++ * 3. EPT without VT-d: always map as WB and set IPAT=1 to keep
7757 ++ * consistent with host MTRR
7758 ++ */
7759 ++ if (is_mmio) {
7760 ++ cache = MTRR_TYPE_UNCACHABLE;
7761 ++ goto exit;
7762 ++ }
7763 ++
7764 ++ if (!kvm_arch_has_noncoherent_dma(vcpu->kvm)) {
7765 ++ ipat = VMX_EPT_IPAT_BIT;
7766 ++ cache = MTRR_TYPE_WRBACK;
7767 ++ goto exit;
7768 ++ }
7769 ++
7770 ++ if (kvm_read_cr0(vcpu) & X86_CR0_CD) {
7771 ++ ipat = VMX_EPT_IPAT_BIT;
7772 ++ if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
7773 ++ cache = MTRR_TYPE_WRBACK;
7774 ++ else
7775 ++ cache = MTRR_TYPE_UNCACHABLE;
7776 ++ goto exit;
7777 ++ }
7778 ++
7779 ++ cache = kvm_mtrr_get_guest_memory_type(vcpu, gfn);
7780 ++
7781 ++exit:
7782 ++ return (cache << VMX_EPT_MT_EPTE_SHIFT) | ipat;
7783 ++}
7784 ++
7785 ++static int vmx_get_lpage_level(void)
7786 ++{
7787 ++ if (enable_ept && !cpu_has_vmx_ept_1g_page())
7788 ++ return PT_DIRECTORY_LEVEL;
7789 ++ else
7790 ++ /* For shadow and EPT supported 1GB page */
7791 ++ return PT_PDPE_LEVEL;
7792 ++}
7793 ++
7794 ++static void vmcs_set_secondary_exec_control(struct vcpu_vmx *vmx)
7795 ++{
7796 ++ /*
7797 ++ * These bits in the secondary execution controls field
7798 ++ * are dynamic, the others are mostly based on the hypervisor
7799 ++ * architecture and the guest's CPUID. Do not touch the
7800 ++ * dynamic bits.
7801 ++ */
7802 ++ u32 mask =
7803 ++ SECONDARY_EXEC_SHADOW_VMCS |
7804 ++ SECONDARY_EXEC_VIRTUALIZE_X2APIC_MODE |
7805 ++ SECONDARY_EXEC_VIRTUALIZE_APIC_ACCESSES |
7806 ++ SECONDARY_EXEC_DESC;
7807 ++
7808 ++ u32 new_ctl = vmx->secondary_exec_control;
7809 ++ u32 cur_ctl = secondary_exec_controls_get(vmx);
7810 ++
7811 ++ secondary_exec_controls_set(vmx, (new_ctl & ~mask) | (cur_ctl & mask));
7812 ++}
7813 ++
7814 ++/*
7815 ++ * Generate MSR_IA32_VMX_CR{0,4}_FIXED1 according to CPUID. Only set bits
7816 ++ * (indicating "allowed-1") if they are supported in the guest's CPUID.
7817 ++ */
7818 ++static void nested_vmx_cr_fixed1_bits_update(struct kvm_vcpu *vcpu)
7819 ++{
7820 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
7821 ++ struct kvm_cpuid_entry2 *entry;
7822 ++
7823 ++ vmx->nested.msrs.cr0_fixed1 = 0xffffffff;
7824 ++ vmx->nested.msrs.cr4_fixed1 = X86_CR4_PCE;
7825 ++
7826 ++#define cr4_fixed1_update(_cr4_mask, _reg, _cpuid_mask) do { \
7827 ++ if (entry && (entry->_reg & (_cpuid_mask))) \
7828 ++ vmx->nested.msrs.cr4_fixed1 |= (_cr4_mask); \
7829 ++} while (0)
7830 ++
7831 ++ entry = kvm_find_cpuid_entry(vcpu, 0x1, 0);
7832 ++ cr4_fixed1_update(X86_CR4_VME, edx, bit(X86_FEATURE_VME));
7833 ++ cr4_fixed1_update(X86_CR4_PVI, edx, bit(X86_FEATURE_VME));
7834 ++ cr4_fixed1_update(X86_CR4_TSD, edx, bit(X86_FEATURE_TSC));
7835 ++ cr4_fixed1_update(X86_CR4_DE, edx, bit(X86_FEATURE_DE));
7836 ++ cr4_fixed1_update(X86_CR4_PSE, edx, bit(X86_FEATURE_PSE));
7837 ++ cr4_fixed1_update(X86_CR4_PAE, edx, bit(X86_FEATURE_PAE));
7838 ++ cr4_fixed1_update(X86_CR4_MCE, edx, bit(X86_FEATURE_MCE));
7839 ++ cr4_fixed1_update(X86_CR4_PGE, edx, bit(X86_FEATURE_PGE));
7840 ++ cr4_fixed1_update(X86_CR4_OSFXSR, edx, bit(X86_FEATURE_FXSR));
7841 ++ cr4_fixed1_update(X86_CR4_OSXMMEXCPT, edx, bit(X86_FEATURE_XMM));
7842 ++ cr4_fixed1_update(X86_CR4_VMXE, ecx, bit(X86_FEATURE_VMX));
7843 ++ cr4_fixed1_update(X86_CR4_SMXE, ecx, bit(X86_FEATURE_SMX));
7844 ++ cr4_fixed1_update(X86_CR4_PCIDE, ecx, bit(X86_FEATURE_PCID));
7845 ++ cr4_fixed1_update(X86_CR4_OSXSAVE, ecx, bit(X86_FEATURE_XSAVE));
7846 ++
7847 ++ entry = kvm_find_cpuid_entry(vcpu, 0x7, 0);
7848 ++ cr4_fixed1_update(X86_CR4_FSGSBASE, ebx, bit(X86_FEATURE_FSGSBASE));
7849 ++ cr4_fixed1_update(X86_CR4_SMEP, ebx, bit(X86_FEATURE_SMEP));
7850 ++ cr4_fixed1_update(X86_CR4_SMAP, ebx, bit(X86_FEATURE_SMAP));
7851 ++ cr4_fixed1_update(X86_CR4_PKE, ecx, bit(X86_FEATURE_PKU));
7852 ++ cr4_fixed1_update(X86_CR4_UMIP, ecx, bit(X86_FEATURE_UMIP));
7853 ++ cr4_fixed1_update(X86_CR4_LA57, ecx, bit(X86_FEATURE_LA57));
7854 ++
7855 ++#undef cr4_fixed1_update
7856 ++}
7857 ++
7858 ++static void nested_vmx_entry_exit_ctls_update(struct kvm_vcpu *vcpu)
7859 ++{
7860 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
7861 ++
7862 ++ if (kvm_mpx_supported()) {
7863 ++ bool mpx_enabled = guest_cpuid_has(vcpu, X86_FEATURE_MPX);
7864 ++
7865 ++ if (mpx_enabled) {
7866 ++ vmx->nested.msrs.entry_ctls_high |= VM_ENTRY_LOAD_BNDCFGS;
7867 ++ vmx->nested.msrs.exit_ctls_high |= VM_EXIT_CLEAR_BNDCFGS;
7868 ++ } else {
7869 ++ vmx->nested.msrs.entry_ctls_high &= ~VM_ENTRY_LOAD_BNDCFGS;
7870 ++ vmx->nested.msrs.exit_ctls_high &= ~VM_EXIT_CLEAR_BNDCFGS;
7871 ++ }
7872 ++ }
7873 ++}
7874 ++
7875 ++static void update_intel_pt_cfg(struct kvm_vcpu *vcpu)
7876 ++{
7877 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
7878 ++ struct kvm_cpuid_entry2 *best = NULL;
7879 ++ int i;
7880 ++
7881 ++ for (i = 0; i < PT_CPUID_LEAVES; i++) {
7882 ++ best = kvm_find_cpuid_entry(vcpu, 0x14, i);
7883 ++ if (!best)
7884 ++ return;
7885 ++ vmx->pt_desc.caps[CPUID_EAX + i*PT_CPUID_REGS_NUM] = best->eax;
7886 ++ vmx->pt_desc.caps[CPUID_EBX + i*PT_CPUID_REGS_NUM] = best->ebx;
7887 ++ vmx->pt_desc.caps[CPUID_ECX + i*PT_CPUID_REGS_NUM] = best->ecx;
7888 ++ vmx->pt_desc.caps[CPUID_EDX + i*PT_CPUID_REGS_NUM] = best->edx;
7889 ++ }
7890 ++
7891 ++ /* Get the number of configurable Address Ranges for filtering */
7892 ++ vmx->pt_desc.addr_range = intel_pt_validate_cap(vmx->pt_desc.caps,
7893 ++ PT_CAP_num_address_ranges);
7894 ++
7895 ++ /* Initialize and clear the no dependency bits */
7896 ++ vmx->pt_desc.ctl_bitmask = ~(RTIT_CTL_TRACEEN | RTIT_CTL_OS |
7897 ++ RTIT_CTL_USR | RTIT_CTL_TSC_EN | RTIT_CTL_DISRETC);
7898 ++
7899 ++ /*
7900 ++ * If CPUID.(EAX=14H,ECX=0):EBX[0]=1 CR3Filter can be set otherwise
7901 ++ * will inject an #GP
7902 ++ */
7903 ++ if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_cr3_filtering))
7904 ++ vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_CR3EN;
7905 ++
7906 ++ /*
7907 ++ * If CPUID.(EAX=14H,ECX=0):EBX[1]=1 CYCEn, CycThresh and
7908 ++ * PSBFreq can be set
7909 ++ */
7910 ++ if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_psb_cyc))
7911 ++ vmx->pt_desc.ctl_bitmask &= ~(RTIT_CTL_CYCLEACC |
7912 ++ RTIT_CTL_CYC_THRESH | RTIT_CTL_PSB_FREQ);
7913 ++
7914 ++ /*
7915 ++ * If CPUID.(EAX=14H,ECX=0):EBX[3]=1 MTCEn BranchEn and
7916 ++ * MTCFreq can be set
7917 ++ */
7918 ++ if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_mtc))
7919 ++ vmx->pt_desc.ctl_bitmask &= ~(RTIT_CTL_MTC_EN |
7920 ++ RTIT_CTL_BRANCH_EN | RTIT_CTL_MTC_RANGE);
7921 ++
7922 ++ /* If CPUID.(EAX=14H,ECX=0):EBX[4]=1 FUPonPTW and PTWEn can be set */
7923 ++ if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_ptwrite))
7924 ++ vmx->pt_desc.ctl_bitmask &= ~(RTIT_CTL_FUP_ON_PTW |
7925 ++ RTIT_CTL_PTW_EN);
7926 ++
7927 ++ /* If CPUID.(EAX=14H,ECX=0):EBX[5]=1 PwrEvEn can be set */
7928 ++ if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_power_event_trace))
7929 ++ vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_PWR_EVT_EN;
7930 ++
7931 ++ /* If CPUID.(EAX=14H,ECX=0):ECX[0]=1 ToPA can be set */
7932 ++ if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_topa_output))
7933 ++ vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_TOPA;
7934 ++
7935 ++ /* If CPUID.(EAX=14H,ECX=0):ECX[3]=1 FabircEn can be set */
7936 ++ if (intel_pt_validate_cap(vmx->pt_desc.caps, PT_CAP_output_subsys))
7937 ++ vmx->pt_desc.ctl_bitmask &= ~RTIT_CTL_FABRIC_EN;
7938 ++
7939 ++ /* unmask address range configure area */
7940 ++ for (i = 0; i < vmx->pt_desc.addr_range; i++)
7941 ++ vmx->pt_desc.ctl_bitmask &= ~(0xfULL << (32 + i * 4));
7942 ++}
7943 ++
7944 ++static void vmx_cpuid_update(struct kvm_vcpu *vcpu)
7945 ++{
7946 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
7947 ++
7948 ++ /* xsaves_enabled is recomputed in vmx_compute_secondary_exec_control(). */
7949 ++ vcpu->arch.xsaves_enabled = false;
7950 ++
7951 ++ if (cpu_has_secondary_exec_ctrls()) {
7952 ++ vmx_compute_secondary_exec_control(vmx);
7953 ++ vmcs_set_secondary_exec_control(vmx);
7954 ++ }
7955 ++
7956 ++ if (nested_vmx_allowed(vcpu))
7957 ++ to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
7958 ++ FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX |
7959 ++ FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX;
7960 ++ else
7961 ++ to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
7962 ++ ~(FEATURE_CONTROL_VMXON_ENABLED_INSIDE_SMX |
7963 ++ FEATURE_CONTROL_VMXON_ENABLED_OUTSIDE_SMX);
7964 ++
7965 ++ if (nested_vmx_allowed(vcpu)) {
7966 ++ nested_vmx_cr_fixed1_bits_update(vcpu);
7967 ++ nested_vmx_entry_exit_ctls_update(vcpu);
7968 ++ }
7969 ++
7970 ++ if (boot_cpu_has(X86_FEATURE_INTEL_PT) &&
7971 ++ guest_cpuid_has(vcpu, X86_FEATURE_INTEL_PT))
7972 ++ update_intel_pt_cfg(vcpu);
7973 ++
7974 ++ if (boot_cpu_has(X86_FEATURE_RTM)) {
7975 ++ struct shared_msr_entry *msr;
7976 ++ msr = find_msr_entry(vmx, MSR_IA32_TSX_CTRL);
7977 ++ if (msr) {
7978 ++ bool enabled = guest_cpuid_has(vcpu, X86_FEATURE_RTM);
7979 ++ vmx_set_guest_msr(vmx, msr, enabled ? 0 : TSX_CTRL_RTM_DISABLE);
7980 ++ }
7981 ++ }
7982 ++}
7983 ++
7984 ++static void vmx_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
7985 ++{
7986 ++ if (func == 1 && nested)
7987 ++ entry->ecx |= bit(X86_FEATURE_VMX);
7988 ++}
7989 ++
7990 ++static void vmx_request_immediate_exit(struct kvm_vcpu *vcpu)
7991 ++{
7992 ++ to_vmx(vcpu)->req_immediate_exit = true;
7993 ++}
7994 ++
7995 ++static int vmx_check_intercept(struct kvm_vcpu *vcpu,
7996 ++ struct x86_instruction_info *info,
7997 ++ enum x86_intercept_stage stage)
7998 ++{
7999 ++ struct vmcs12 *vmcs12 = get_vmcs12(vcpu);
8000 ++ struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
8001 ++
8002 ++ /*
8003 ++ * RDPID causes #UD if disabled through secondary execution controls.
8004 ++ * Because it is marked as EmulateOnUD, we need to intercept it here.
8005 ++ */
8006 ++ if (info->intercept == x86_intercept_rdtscp &&
8007 ++ !nested_cpu_has2(vmcs12, SECONDARY_EXEC_RDTSCP)) {
8008 ++ ctxt->exception.vector = UD_VECTOR;
8009 ++ ctxt->exception.error_code_valid = false;
8010 ++ return X86EMUL_PROPAGATE_FAULT;
8011 ++ }
8012 ++
8013 ++ /* TODO: check more intercepts... */
8014 ++ return X86EMUL_CONTINUE;
8015 ++}
8016 ++
8017 ++#ifdef CONFIG_X86_64
8018 ++/* (a << shift) / divisor, return 1 if overflow otherwise 0 */
8019 ++static inline int u64_shl_div_u64(u64 a, unsigned int shift,
8020 ++ u64 divisor, u64 *result)
8021 ++{
8022 ++ u64 low = a << shift, high = a >> (64 - shift);
8023 ++
8024 ++ /* To avoid the overflow on divq */
8025 ++ if (high >= divisor)
8026 ++ return 1;
8027 ++
8028 ++ /* Low hold the result, high hold rem which is discarded */
8029 ++ asm("divq %2\n\t" : "=a" (low), "=d" (high) :
8030 ++ "rm" (divisor), "0" (low), "1" (high));
8031 ++ *result = low;
8032 ++
8033 ++ return 0;
8034 ++}
8035 ++
8036 ++static int vmx_set_hv_timer(struct kvm_vcpu *vcpu, u64 guest_deadline_tsc,
8037 ++ bool *expired)
8038 ++{
8039 ++ struct vcpu_vmx *vmx;
8040 ++ u64 tscl, guest_tscl, delta_tsc, lapic_timer_advance_cycles;
8041 ++ struct kvm_timer *ktimer = &vcpu->arch.apic->lapic_timer;
8042 ++
8043 ++ if (kvm_mwait_in_guest(vcpu->kvm) ||
8044 ++ kvm_can_post_timer_interrupt(vcpu))
8045 ++ return -EOPNOTSUPP;
8046 ++
8047 ++ vmx = to_vmx(vcpu);
8048 ++ tscl = rdtsc();
8049 ++ guest_tscl = kvm_read_l1_tsc(vcpu, tscl);
8050 ++ delta_tsc = max(guest_deadline_tsc, guest_tscl) - guest_tscl;
8051 ++ lapic_timer_advance_cycles = nsec_to_cycles(vcpu,
8052 ++ ktimer->timer_advance_ns);
8053 ++
8054 ++ if (delta_tsc > lapic_timer_advance_cycles)
8055 ++ delta_tsc -= lapic_timer_advance_cycles;
8056 ++ else
8057 ++ delta_tsc = 0;
8058 ++
8059 ++ /* Convert to host delta tsc if tsc scaling is enabled */
8060 ++ if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio &&
8061 ++ delta_tsc && u64_shl_div_u64(delta_tsc,
8062 ++ kvm_tsc_scaling_ratio_frac_bits,
8063 ++ vcpu->arch.tsc_scaling_ratio, &delta_tsc))
8064 ++ return -ERANGE;
8065 ++
8066 ++ /*
8067 ++ * If the delta tsc can't fit in the 32 bit after the multi shift,
8068 ++ * we can't use the preemption timer.
8069 ++ * It's possible that it fits on later vmentries, but checking
8070 ++ * on every vmentry is costly so we just use an hrtimer.
8071 ++ */
8072 ++ if (delta_tsc >> (cpu_preemption_timer_multi + 32))
8073 ++ return -ERANGE;
8074 ++
8075 ++ vmx->hv_deadline_tsc = tscl + delta_tsc;
8076 ++ *expired = !delta_tsc;
8077 ++ return 0;
8078 ++}
8079 ++
8080 ++static void vmx_cancel_hv_timer(struct kvm_vcpu *vcpu)
8081 ++{
8082 ++ to_vmx(vcpu)->hv_deadline_tsc = -1;
8083 ++}
8084 ++#endif
8085 ++
8086 ++static void vmx_sched_in(struct kvm_vcpu *vcpu, int cpu)
8087 ++{
8088 ++ if (!kvm_pause_in_guest(vcpu->kvm))
8089 ++ shrink_ple_window(vcpu);
8090 ++}
8091 ++
8092 ++static void vmx_slot_enable_log_dirty(struct kvm *kvm,
8093 ++ struct kvm_memory_slot *slot)
8094 ++{
8095 ++ kvm_mmu_slot_leaf_clear_dirty(kvm, slot);
8096 ++ kvm_mmu_slot_largepage_remove_write_access(kvm, slot);
8097 ++}
8098 ++
8099 ++static void vmx_slot_disable_log_dirty(struct kvm *kvm,
8100 ++ struct kvm_memory_slot *slot)
8101 ++{
8102 ++ kvm_mmu_slot_set_dirty(kvm, slot);
8103 ++}
8104 ++
8105 ++static void vmx_flush_log_dirty(struct kvm *kvm)
8106 ++{
8107 ++ kvm_flush_pml_buffers(kvm);
8108 ++}
8109 ++
8110 ++static int vmx_write_pml_buffer(struct kvm_vcpu *vcpu)
8111 ++{
8112 ++ struct vmcs12 *vmcs12;
8113 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
8114 ++ gpa_t gpa, dst;
8115 ++
8116 ++ if (is_guest_mode(vcpu)) {
8117 ++ WARN_ON_ONCE(vmx->nested.pml_full);
8118 ++
8119 ++ /*
8120 ++ * Check if PML is enabled for the nested guest.
8121 ++ * Whether eptp bit 6 is set is already checked
8122 ++ * as part of A/D emulation.
8123 ++ */
8124 ++ vmcs12 = get_vmcs12(vcpu);
8125 ++ if (!nested_cpu_has_pml(vmcs12))
8126 ++ return 0;
8127 ++
8128 ++ if (vmcs12->guest_pml_index >= PML_ENTITY_NUM) {
8129 ++ vmx->nested.pml_full = true;
8130 ++ return 1;
8131 ++ }
8132 ++
8133 ++ gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS) & ~0xFFFull;
8134 ++ dst = vmcs12->pml_address + sizeof(u64) * vmcs12->guest_pml_index;
8135 ++
8136 ++ if (kvm_write_guest_page(vcpu->kvm, gpa_to_gfn(dst), &gpa,
8137 ++ offset_in_page(dst), sizeof(gpa)))
8138 ++ return 0;
8139 ++
8140 ++ vmcs12->guest_pml_index--;
8141 ++ }
8142 ++
8143 ++ return 0;
8144 ++}
8145 ++
8146 ++static void vmx_enable_log_dirty_pt_masked(struct kvm *kvm,
8147 ++ struct kvm_memory_slot *memslot,
8148 ++ gfn_t offset, unsigned long mask)
8149 ++{
8150 ++ kvm_mmu_clear_dirty_pt_masked(kvm, memslot, offset, mask);
8151 ++}
8152 ++
8153 ++static void __pi_post_block(struct kvm_vcpu *vcpu)
8154 ++{
8155 ++ struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
8156 ++ struct pi_desc old, new;
8157 ++ unsigned int dest;
8158 ++
8159 ++ do {
8160 ++ old.control = new.control = pi_desc->control;
8161 ++ WARN(old.nv != POSTED_INTR_WAKEUP_VECTOR,
8162 ++ "Wakeup handler not enabled while the VCPU is blocked\n");
8163 ++
8164 ++ dest = cpu_physical_id(vcpu->cpu);
8165 ++
8166 ++ if (x2apic_enabled())
8167 ++ new.ndst = dest;
8168 ++ else
8169 ++ new.ndst = (dest << 8) & 0xFF00;
8170 ++
8171 ++ /* set 'NV' to 'notification vector' */
8172 ++ new.nv = POSTED_INTR_VECTOR;
8173 ++ } while (cmpxchg64(&pi_desc->control, old.control,
8174 ++ new.control) != old.control);
8175 ++
8176 ++ if (!WARN_ON_ONCE(vcpu->pre_pcpu == -1)) {
8177 ++ spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
8178 ++ list_del(&vcpu->blocked_vcpu_list);
8179 ++ spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
8180 ++ vcpu->pre_pcpu = -1;
8181 ++ }
8182 ++}
8183 ++
8184 ++/*
8185 ++ * This routine does the following things for vCPU which is going
8186 ++ * to be blocked if VT-d PI is enabled.
8187 ++ * - Store the vCPU to the wakeup list, so when interrupts happen
8188 ++ * we can find the right vCPU to wake up.
8189 ++ * - Change the Posted-interrupt descriptor as below:
8190 ++ * 'NDST' <-- vcpu->pre_pcpu
8191 ++ * 'NV' <-- POSTED_INTR_WAKEUP_VECTOR
8192 ++ * - If 'ON' is set during this process, which means at least one
8193 ++ * interrupt is posted for this vCPU, we cannot block it, in
8194 ++ * this case, return 1, otherwise, return 0.
8195 ++ *
8196 ++ */
8197 ++static int pi_pre_block(struct kvm_vcpu *vcpu)
8198 ++{
8199 ++ unsigned int dest;
8200 ++ struct pi_desc old, new;
8201 ++ struct pi_desc *pi_desc = vcpu_to_pi_desc(vcpu);
8202 ++
8203 ++ if (!kvm_arch_has_assigned_device(vcpu->kvm) ||
8204 ++ !irq_remapping_cap(IRQ_POSTING_CAP) ||
8205 ++ !kvm_vcpu_apicv_active(vcpu))
8206 ++ return 0;
8207 ++
8208 ++ WARN_ON(irqs_disabled());
8209 ++ local_irq_disable();
8210 ++ if (!WARN_ON_ONCE(vcpu->pre_pcpu != -1)) {
8211 ++ vcpu->pre_pcpu = vcpu->cpu;
8212 ++ spin_lock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
8213 ++ list_add_tail(&vcpu->blocked_vcpu_list,
8214 ++ &per_cpu(blocked_vcpu_on_cpu,
8215 ++ vcpu->pre_pcpu));
8216 ++ spin_unlock(&per_cpu(blocked_vcpu_on_cpu_lock, vcpu->pre_pcpu));
8217 ++ }
8218 ++
8219 ++ do {
8220 ++ old.control = new.control = pi_desc->control;
8221 ++
8222 ++ WARN((pi_desc->sn == 1),
8223 ++ "Warning: SN field of posted-interrupts "
8224 ++ "is set before blocking\n");
8225 ++
8226 ++ /*
8227 ++ * Since vCPU can be preempted during this process,
8228 ++ * vcpu->cpu could be different with pre_pcpu, we
8229 ++ * need to set pre_pcpu as the destination of wakeup
8230 ++ * notification event, then we can find the right vCPU
8231 ++ * to wakeup in wakeup handler if interrupts happen
8232 ++ * when the vCPU is in blocked state.
8233 ++ */
8234 ++ dest = cpu_physical_id(vcpu->pre_pcpu);
8235 ++
8236 ++ if (x2apic_enabled())
8237 ++ new.ndst = dest;
8238 ++ else
8239 ++ new.ndst = (dest << 8) & 0xFF00;
8240 ++
8241 ++ /* set 'NV' to 'wakeup vector' */
8242 ++ new.nv = POSTED_INTR_WAKEUP_VECTOR;
8243 ++ } while (cmpxchg64(&pi_desc->control, old.control,
8244 ++ new.control) != old.control);
8245 ++
8246 ++ /* We should not block the vCPU if an interrupt is posted for it. */
8247 ++ if (pi_test_on(pi_desc) == 1)
8248 ++ __pi_post_block(vcpu);
8249 ++
8250 ++ local_irq_enable();
8251 ++ return (vcpu->pre_pcpu == -1);
8252 ++}
8253 ++
8254 ++static int vmx_pre_block(struct kvm_vcpu *vcpu)
8255 ++{
8256 ++ if (pi_pre_block(vcpu))
8257 ++ return 1;
8258 ++
8259 ++ if (kvm_lapic_hv_timer_in_use(vcpu))
8260 ++ kvm_lapic_switch_to_sw_timer(vcpu);
8261 ++
8262 ++ return 0;
8263 ++}
8264 ++
8265 ++static void pi_post_block(struct kvm_vcpu *vcpu)
8266 ++{
8267 ++ if (vcpu->pre_pcpu == -1)
8268 ++ return;
8269 ++
8270 ++ WARN_ON(irqs_disabled());
8271 ++ local_irq_disable();
8272 ++ __pi_post_block(vcpu);
8273 ++ local_irq_enable();
8274 ++}
8275 ++
8276 ++static void vmx_post_block(struct kvm_vcpu *vcpu)
8277 ++{
8278 ++ if (kvm_x86_ops->set_hv_timer)
8279 ++ kvm_lapic_switch_to_hv_timer(vcpu);
8280 ++
8281 ++ pi_post_block(vcpu);
8282 ++}
8283 ++
8284 ++/*
8285 ++ * vmx_update_pi_irte - set IRTE for Posted-Interrupts
8286 ++ *
8287 ++ * @kvm: kvm
8288 ++ * @host_irq: host irq of the interrupt
8289 ++ * @guest_irq: gsi of the interrupt
8290 ++ * @set: set or unset PI
8291 ++ * returns 0 on success, < 0 on failure
8292 ++ */
8293 ++static int vmx_update_pi_irte(struct kvm *kvm, unsigned int host_irq,
8294 ++ uint32_t guest_irq, bool set)
8295 ++{
8296 ++ struct kvm_kernel_irq_routing_entry *e;
8297 ++ struct kvm_irq_routing_table *irq_rt;
8298 ++ struct kvm_lapic_irq irq;
8299 ++ struct kvm_vcpu *vcpu;
8300 ++ struct vcpu_data vcpu_info;
8301 ++ int idx, ret = 0;
8302 ++
8303 ++ if (!kvm_arch_has_assigned_device(kvm) ||
8304 ++ !irq_remapping_cap(IRQ_POSTING_CAP) ||
8305 ++ !kvm_vcpu_apicv_active(kvm->vcpus[0]))
8306 ++ return 0;
8307 ++
8308 ++ idx = srcu_read_lock(&kvm->irq_srcu);
8309 ++ irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
8310 ++ if (guest_irq >= irq_rt->nr_rt_entries ||
8311 ++ hlist_empty(&irq_rt->map[guest_irq])) {
8312 ++ pr_warn_once("no route for guest_irq %u/%u (broken user space?)\n",
8313 ++ guest_irq, irq_rt->nr_rt_entries);
8314 ++ goto out;
8315 ++ }
8316 ++
8317 ++ hlist_for_each_entry(e, &irq_rt->map[guest_irq], link) {
8318 ++ if (e->type != KVM_IRQ_ROUTING_MSI)
8319 ++ continue;
8320 ++ /*
8321 ++ * VT-d PI cannot support posting multicast/broadcast
8322 ++ * interrupts to a vCPU, we still use interrupt remapping
8323 ++ * for these kind of interrupts.
8324 ++ *
8325 ++ * For lowest-priority interrupts, we only support
8326 ++ * those with single CPU as the destination, e.g. user
8327 ++ * configures the interrupts via /proc/irq or uses
8328 ++ * irqbalance to make the interrupts single-CPU.
8329 ++ *
8330 ++ * We will support full lowest-priority interrupt later.
8331 ++ *
8332 ++ * In addition, we can only inject generic interrupts using
8333 ++ * the PI mechanism, refuse to route others through it.
8334 ++ */
8335 ++
8336 ++ kvm_set_msi_irq(kvm, e, &irq);
8337 ++ if (!kvm_intr_is_single_vcpu(kvm, &irq, &vcpu) ||
8338 ++ !kvm_irq_is_postable(&irq)) {
8339 ++ /*
8340 ++ * Make sure the IRTE is in remapped mode if
8341 ++ * we don't handle it in posted mode.
8342 ++ */
8343 ++ ret = irq_set_vcpu_affinity(host_irq, NULL);
8344 ++ if (ret < 0) {
8345 ++ printk(KERN_INFO
8346 ++ "failed to back to remapped mode, irq: %u\n",
8347 ++ host_irq);
8348 ++ goto out;
8349 ++ }
8350 ++
8351 ++ continue;
8352 ++ }
8353 ++
8354 ++ vcpu_info.pi_desc_addr = __pa(vcpu_to_pi_desc(vcpu));
8355 ++ vcpu_info.vector = irq.vector;
8356 ++
8357 ++ trace_kvm_pi_irte_update(host_irq, vcpu->vcpu_id, e->gsi,
8358 ++ vcpu_info.vector, vcpu_info.pi_desc_addr, set);
8359 ++
8360 ++ if (set)
8361 ++ ret = irq_set_vcpu_affinity(host_irq, &vcpu_info);
8362 ++ else
8363 ++ ret = irq_set_vcpu_affinity(host_irq, NULL);
8364 ++
8365 ++ if (ret < 0) {
8366 ++ printk(KERN_INFO "%s: failed to update PI IRTE\n",
8367 ++ __func__);
8368 ++ goto out;
8369 ++ }
8370 ++ }
8371 ++
8372 ++ ret = 0;
8373 ++out:
8374 ++ srcu_read_unlock(&kvm->irq_srcu, idx);
8375 ++ return ret;
8376 ++}
8377 ++
8378 ++static void vmx_setup_mce(struct kvm_vcpu *vcpu)
8379 ++{
8380 ++ if (vcpu->arch.mcg_cap & MCG_LMCE_P)
8381 ++ to_vmx(vcpu)->msr_ia32_feature_control_valid_bits |=
8382 ++ FEATURE_CONTROL_LMCE;
8383 ++ else
8384 ++ to_vmx(vcpu)->msr_ia32_feature_control_valid_bits &=
8385 ++ ~FEATURE_CONTROL_LMCE;
8386 ++}
8387 ++
8388 ++static int vmx_smi_allowed(struct kvm_vcpu *vcpu)
8389 ++{
8390 ++ /* we need a nested vmexit to enter SMM, postpone if run is pending */
8391 ++ if (to_vmx(vcpu)->nested.nested_run_pending)
8392 ++ return 0;
8393 ++ return 1;
8394 ++}
8395 ++
8396 ++static int vmx_pre_enter_smm(struct kvm_vcpu *vcpu, char *smstate)
8397 ++{
8398 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
8399 ++
8400 ++ vmx->nested.smm.guest_mode = is_guest_mode(vcpu);
8401 ++ if (vmx->nested.smm.guest_mode)
8402 ++ nested_vmx_vmexit(vcpu, -1, 0, 0);
8403 ++
8404 ++ vmx->nested.smm.vmxon = vmx->nested.vmxon;
8405 ++ vmx->nested.vmxon = false;
8406 ++ vmx_clear_hlt(vcpu);
8407 ++ return 0;
8408 ++}
8409 ++
8410 ++static int vmx_pre_leave_smm(struct kvm_vcpu *vcpu, const char *smstate)
8411 ++{
8412 ++ struct vcpu_vmx *vmx = to_vmx(vcpu);
8413 ++ int ret;
8414 ++
8415 ++ if (vmx->nested.smm.vmxon) {
8416 ++ vmx->nested.vmxon = true;
8417 ++ vmx->nested.smm.vmxon = false;
8418 ++ }
8419 ++
8420 ++ if (vmx->nested.smm.guest_mode) {
8421 ++ ret = nested_vmx_enter_non_root_mode(vcpu, false);
8422 ++ if (ret)
8423 ++ return ret;
8424 ++
8425 ++ vmx->nested.smm.guest_mode = false;
8426 ++ }
8427 ++ return 0;
8428 ++}
8429 ++
8430 ++static int enable_smi_window(struct kvm_vcpu *vcpu)
8431 ++{
8432 ++ return 0;
8433 ++}
8434 ++
8435 ++static bool vmx_need_emulation_on_page_fault(struct kvm_vcpu *vcpu)
8436 ++{
8437 ++ return false;
8438 ++}
8439 ++
8440 ++static bool vmx_apic_init_signal_blocked(struct kvm_vcpu *vcpu)
8441 ++{
8442 ++ return to_vmx(vcpu)->nested.vmxon;
8443 ++}
8444 ++
8445 ++static __init int hardware_setup(void)
8446 ++{
8447 ++ unsigned long host_bndcfgs;
8448 ++ struct desc_ptr dt;
8449 ++ int r, i;
8450 ++
8451 ++ rdmsrl_safe(MSR_EFER, &host_efer);
8452 ++
8453 ++ store_idt(&dt);
8454 ++ host_idt_base = dt.address;
8455 ++
8456 ++ for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i)
8457 ++ kvm_define_shared_msr(i, vmx_msr_index[i]);
8458 ++
8459 ++ if (setup_vmcs_config(&vmcs_config, &vmx_capability) < 0)
8460 ++ return -EIO;
8461 ++
8462 ++ if (boot_cpu_has(X86_FEATURE_NX))
8463 ++ kvm_enable_efer_bits(EFER_NX);
8464 ++
8465 ++ if (boot_cpu_has(X86_FEATURE_MPX)) {
8466 ++ rdmsrl(MSR_IA32_BNDCFGS, host_bndcfgs);
8467 ++ WARN_ONCE(host_bndcfgs, "KVM: BNDCFGS in host will be lost");
8468 ++ }
8469 ++
8470 ++ if (!cpu_has_vmx_vpid() || !cpu_has_vmx_invvpid() ||
8471 ++ !(cpu_has_vmx_invvpid_single() || cpu_has_vmx_invvpid_global()))
8472 ++ enable_vpid = 0;
8473 ++
8474 ++ if (!cpu_has_vmx_ept() ||
8475 ++ !cpu_has_vmx_ept_4levels() ||
8476 ++ !cpu_has_vmx_ept_mt_wb() ||
8477 ++ !cpu_has_vmx_invept_global())
8478 ++ enable_ept = 0;
8479 ++
8480 ++ if (!cpu_has_vmx_ept_ad_bits() || !enable_ept)
8481 ++ enable_ept_ad_bits = 0;
8482 ++
8483 ++ if (!cpu_has_vmx_unrestricted_guest() || !enable_ept)
8484 ++ enable_unrestricted_guest = 0;
8485 ++
8486 ++ if (!cpu_has_vmx_flexpriority())
8487 ++ flexpriority_enabled = 0;
8488 ++
8489 ++ if (!cpu_has_virtual_nmis())
8490 ++ enable_vnmi = 0;
8491 ++
8492 ++ /*
8493 ++ * set_apic_access_page_addr() is used to reload apic access
8494 ++ * page upon invalidation. No need to do anything if not
8495 ++ * using the APIC_ACCESS_ADDR VMCS field.
8496 ++ */
8497 ++ if (!flexpriority_enabled)
8498 ++ kvm_x86_ops->set_apic_access_page_addr = NULL;
8499 ++
8500 ++ if (!cpu_has_vmx_tpr_shadow())
8501 ++ kvm_x86_ops->update_cr8_intercept = NULL;
8502 ++
8503 ++ if (enable_ept && !cpu_has_vmx_ept_2m_page())
8504 ++ kvm_disable_largepages();
8505 ++
8506 ++#if IS_ENABLED(CONFIG_HYPERV)
8507 ++ if (ms_hyperv.nested_features & HV_X64_NESTED_GUEST_MAPPING_FLUSH
8508 ++ && enable_ept) {
8509 ++ kvm_x86_ops->tlb_remote_flush = hv_remote_flush_tlb;
8510 ++ kvm_x86_ops->tlb_remote_flush_with_range =
8511 ++ hv_remote_flush_tlb_with_range;
8512 ++ }
8513 ++#endif
8514 ++
8515 ++ if (!cpu_has_vmx_ple()) {
8516 ++ ple_gap = 0;
8517 ++ ple_window = 0;
8518 ++ ple_window_grow = 0;
8519 ++ ple_window_max = 0;
8520 ++ ple_window_shrink = 0;
8521 ++ }
8522 ++
8523 ++ if (!cpu_has_vmx_apicv()) {
8524 ++ enable_apicv = 0;
8525 ++ kvm_x86_ops->sync_pir_to_irr = NULL;
8526 ++ }
8527 ++
8528 ++ if (cpu_has_vmx_tsc_scaling()) {
8529 ++ kvm_has_tsc_control = true;
8530 ++ kvm_max_tsc_scaling_ratio = KVM_VMX_TSC_MULTIPLIER_MAX;
8531 ++ kvm_tsc_scaling_ratio_frac_bits = 48;
8532 ++ }
8533 ++
8534 ++ set_bit(0, vmx_vpid_bitmap); /* 0 is reserved for host */
8535 ++
8536 ++ if (enable_ept)
8537 ++ vmx_enable_tdp();
8538 ++ else
8539 ++ kvm_disable_tdp();
8540 ++
8541 ++ /*
8542 ++ * Only enable PML when hardware supports PML feature, and both EPT
8543 ++ * and EPT A/D bit features are enabled -- PML depends on them to work.
8544 ++ */
8545 ++ if (!enable_ept || !enable_ept_ad_bits || !cpu_has_vmx_pml())
8546 ++ enable_pml = 0;
8547 ++
8548 ++ if (!enable_pml) {
8549 ++ kvm_x86_ops->slot_enable_log_dirty = NULL;
8550 ++ kvm_x86_ops->slot_disable_log_dirty = NULL;
8551 ++ kvm_x86_ops->flush_log_dirty = NULL;
8552 ++ kvm_x86_ops->enable_log_dirty_pt_masked = NULL;
8553 ++ }
8554 ++
8555 ++ if (!cpu_has_vmx_preemption_timer())
8556 ++ enable_preemption_timer = false;
8557 ++
8558 ++ if (enable_preemption_timer) {
8559 ++ u64 use_timer_freq = 5000ULL * 1000 * 1000;
8560 ++ u64 vmx_msr;
8561 ++
8562 ++ rdmsrl(MSR_IA32_VMX_MISC, vmx_msr);
8563 ++ cpu_preemption_timer_multi =
8564 ++ vmx_msr & VMX_MISC_PREEMPTION_TIMER_RATE_MASK;
8565 ++
8566 ++ if (tsc_khz)
8567 ++ use_timer_freq = (u64)tsc_khz * 1000;
8568 ++ use_timer_freq >>= cpu_preemption_timer_multi;
8569 ++
8570 ++ /*
8571 ++ * KVM "disables" the preemption timer by setting it to its max
8572 ++ * value. Don't use the timer if it might cause spurious exits
8573 ++ * at a rate faster than 0.1 Hz (of uninterrupted guest time).
8574 ++ */
8575 ++ if (use_timer_freq > 0xffffffffu / 10)
8576 ++ enable_preemption_timer = false;
8577 ++ }
8578 ++
8579 ++ if (!enable_preemption_timer) {
8580 ++ kvm_x86_ops->set_hv_timer = NULL;
8581 ++ kvm_x86_ops->cancel_hv_timer = NULL;
8582 ++ kvm_x86_ops->request_immediate_exit = __kvm_request_immediate_exit;
8583 ++ }
8584 ++
8585 ++ kvm_set_posted_intr_wakeup_handler(wakeup_handler);
8586 ++
8587 ++ kvm_mce_cap_supported |= MCG_LMCE_P;
8588 ++
8589 ++ if (pt_mode != PT_MODE_SYSTEM && pt_mode != PT_MODE_HOST_GUEST)
8590 ++ return -EINVAL;
8591 ++ if (!enable_ept || !cpu_has_vmx_intel_pt())
8592 ++ pt_mode = PT_MODE_SYSTEM;
8593 ++
8594 ++ if (nested) {
8595 ++ nested_vmx_setup_ctls_msrs(&vmcs_config.nested,
8596 ++ vmx_capability.ept, enable_apicv);
8597 ++
8598 ++ r = nested_vmx_hardware_setup(kvm_vmx_exit_handlers);
8599 ++ if (r)
8600 ++ return r;
8601 ++ }
8602 ++
8603 ++ r = alloc_kvm_area();
8604 ++ if (r)
8605 ++ nested_vmx_hardware_unsetup();
8606 ++ return r;
8607 ++}
8608 ++
8609 ++static __exit void hardware_unsetup(void)
8610 ++{
8611 ++ if (nested)
8612 ++ nested_vmx_hardware_unsetup();
8613 ++
8614 ++ free_kvm_area();
8615 ++}
8616 ++
8617 ++static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
8618 ++ .cpu_has_kvm_support = cpu_has_kvm_support,
8619 ++ .disabled_by_bios = vmx_disabled_by_bios,
8620 ++ .hardware_setup = hardware_setup,
8621 ++ .hardware_unsetup = hardware_unsetup,
8622 ++ .check_processor_compatibility = vmx_check_processor_compat,
8623 ++ .hardware_enable = hardware_enable,
8624 ++ .hardware_disable = hardware_disable,
8625 ++ .cpu_has_accelerated_tpr = report_flexpriority,
8626 ++ .has_emulated_msr = vmx_has_emulated_msr,
8627 ++
8628 ++ .vm_init = vmx_vm_init,
8629 ++ .vm_alloc = vmx_vm_alloc,
8630 ++ .vm_free = vmx_vm_free,
8631 ++
8632 ++ .vcpu_create = vmx_create_vcpu,
8633 ++ .vcpu_free = vmx_free_vcpu,
8634 ++ .vcpu_reset = vmx_vcpu_reset,
8635 ++
8636 ++ .prepare_guest_switch = vmx_prepare_switch_to_guest,
8637 ++ .vcpu_load = vmx_vcpu_load,
8638 ++ .vcpu_put = vmx_vcpu_put,
8639 ++
8640 ++ .update_bp_intercept = update_exception_bitmap,
8641 ++ .get_msr_feature = vmx_get_msr_feature,
8642 ++ .get_msr = vmx_get_msr,
8643 ++ .set_msr = vmx_set_msr,
8644 ++ .get_segment_base = vmx_get_segment_base,
8645 ++ .get_segment = vmx_get_segment,
8646 ++ .set_segment = vmx_set_segment,
8647 ++ .get_cpl = vmx_get_cpl,
8648 ++ .get_cs_db_l_bits = vmx_get_cs_db_l_bits,
8649 ++ .decache_cr0_guest_bits = vmx_decache_cr0_guest_bits,
8650 ++ .decache_cr4_guest_bits = vmx_decache_cr4_guest_bits,
8651 ++ .set_cr0 = vmx_set_cr0,
8652 ++ .set_cr3 = vmx_set_cr3,
8653 ++ .set_cr4 = vmx_set_cr4,
8654 ++ .set_efer = vmx_set_efer,
8655 ++ .get_idt = vmx_get_idt,
8656 ++ .set_idt = vmx_set_idt,
8657 ++ .get_gdt = vmx_get_gdt,
8658 ++ .set_gdt = vmx_set_gdt,
8659 ++ .get_dr6 = vmx_get_dr6,
8660 ++ .set_dr6 = vmx_set_dr6,
8661 ++ .set_dr7 = vmx_set_dr7,
8662 ++ .sync_dirty_debug_regs = vmx_sync_dirty_debug_regs,
8663 ++ .cache_reg = vmx_cache_reg,
8664 ++ .get_rflags = vmx_get_rflags,
8665 ++ .set_rflags = vmx_set_rflags,
8666 ++
8667 ++ .tlb_flush = vmx_flush_tlb,
8668 ++ .tlb_flush_gva = vmx_flush_tlb_gva,
8669 ++
8670 ++ .run = vmx_vcpu_run,
8671 ++ .handle_exit = vmx_handle_exit,
8672 ++ .skip_emulated_instruction = skip_emulated_instruction,
8673 ++ .set_interrupt_shadow = vmx_set_interrupt_shadow,
8674 ++ .get_interrupt_shadow = vmx_get_interrupt_shadow,
8675 ++ .patch_hypercall = vmx_patch_hypercall,
8676 ++ .set_irq = vmx_inject_irq,
8677 ++ .set_nmi = vmx_inject_nmi,
8678 ++ .queue_exception = vmx_queue_exception,
8679 ++ .cancel_injection = vmx_cancel_injection,
8680 ++ .interrupt_allowed = vmx_interrupt_allowed,
8681 ++ .nmi_allowed = vmx_nmi_allowed,
8682 ++ .get_nmi_mask = vmx_get_nmi_mask,
8683 ++ .set_nmi_mask = vmx_set_nmi_mask,
8684 ++ .enable_nmi_window = enable_nmi_window,
8685 ++ .enable_irq_window = enable_irq_window,
8686 ++ .update_cr8_intercept = update_cr8_intercept,
8687 ++ .set_virtual_apic_mode = vmx_set_virtual_apic_mode,
8688 ++ .set_apic_access_page_addr = vmx_set_apic_access_page_addr,
8689 ++ .get_enable_apicv = vmx_get_enable_apicv,
8690 ++ .refresh_apicv_exec_ctrl = vmx_refresh_apicv_exec_ctrl,
8691 ++ .load_eoi_exitmap = vmx_load_eoi_exitmap,
8692 ++ .apicv_post_state_restore = vmx_apicv_post_state_restore,
8693 ++ .hwapic_irr_update = vmx_hwapic_irr_update,
8694 ++ .hwapic_isr_update = vmx_hwapic_isr_update,
8695 ++ .guest_apic_has_interrupt = vmx_guest_apic_has_interrupt,
8696 ++ .sync_pir_to_irr = vmx_sync_pir_to_irr,
8697 ++ .deliver_posted_interrupt = vmx_deliver_posted_interrupt,
8698 ++ .dy_apicv_has_pending_interrupt = vmx_dy_apicv_has_pending_interrupt,
8699 ++
8700 ++ .set_tss_addr = vmx_set_tss_addr,
8701 ++ .set_identity_map_addr = vmx_set_identity_map_addr,
8702 ++ .get_tdp_level = get_ept_level,
8703 ++ .get_mt_mask = vmx_get_mt_mask,
8704 ++
8705 ++ .get_exit_info = vmx_get_exit_info,
8706 ++
8707 ++ .get_lpage_level = vmx_get_lpage_level,
8708 ++
8709 ++ .cpuid_update = vmx_cpuid_update,
8710 ++
8711 ++ .rdtscp_supported = vmx_rdtscp_supported,
8712 ++ .invpcid_supported = vmx_invpcid_supported,
8713 ++
8714 ++ .set_supported_cpuid = vmx_set_supported_cpuid,
8715 ++
8716 ++ .has_wbinvd_exit = cpu_has_vmx_wbinvd_exit,
8717 ++
8718 ++ .read_l1_tsc_offset = vmx_read_l1_tsc_offset,
8719 ++ .write_l1_tsc_offset = vmx_write_l1_tsc_offset,
8720 ++
8721 ++ .set_tdp_cr3 = vmx_set_cr3,
8722 ++
8723 ++ .check_intercept = vmx_check_intercept,
8724 ++ .handle_exit_irqoff = vmx_handle_exit_irqoff,
8725 ++ .mpx_supported = vmx_mpx_supported,
8726 ++ .xsaves_supported = vmx_xsaves_supported,
8727 ++ .umip_emulated = vmx_umip_emulated,
8728 ++ .pt_supported = vmx_pt_supported,
8729 ++
8730 ++ .request_immediate_exit = vmx_request_immediate_exit,
8731 ++
8732 ++ .sched_in = vmx_sched_in,
8733 ++
8734 ++ .slot_enable_log_dirty = vmx_slot_enable_log_dirty,
8735 ++ .slot_disable_log_dirty = vmx_slot_disable_log_dirty,
8736 ++ .flush_log_dirty = vmx_flush_log_dirty,
8737 ++ .enable_log_dirty_pt_masked = vmx_enable_log_dirty_pt_masked,
8738 ++ .write_log_dirty = vmx_write_pml_buffer,
8739 ++
8740 ++ .pre_block = vmx_pre_block,
8741 ++ .post_block = vmx_post_block,
8742 ++
8743 ++ .pmu_ops = &intel_pmu_ops,
8744 ++
8745 ++ .update_pi_irte = vmx_update_pi_irte,
8746 ++
8747 ++#ifdef CONFIG_X86_64
8748 ++ .set_hv_timer = vmx_set_hv_timer,
8749 ++ .cancel_hv_timer = vmx_cancel_hv_timer,
8750 ++#endif
8751 ++
8752 ++ .setup_mce = vmx_setup_mce,
8753 ++
8754 ++ .smi_allowed = vmx_smi_allowed,
8755 ++ .pre_enter_smm = vmx_pre_enter_smm,
8756 ++ .pre_leave_smm = vmx_pre_leave_smm,
8757 ++ .enable_smi_window = enable_smi_window,
8758 ++
8759 ++ .check_nested_events = NULL,
8760 ++ .get_nested_state = NULL,
8761 ++ .set_nested_state = NULL,
8762 ++ .get_vmcs12_pages = NULL,
8763 ++ .nested_enable_evmcs = NULL,
8764 ++ .nested_get_evmcs_version = NULL,
8765 ++ .need_emulation_on_page_fault = vmx_need_emulation_on_page_fault,
8766 ++ .apic_init_signal_blocked = vmx_apic_init_signal_blocked,
8767 ++};
8768 ++
8769 ++static void vmx_cleanup_l1d_flush(void)
8770 ++{
8771 ++ if (vmx_l1d_flush_pages) {
8772 ++ free_pages((unsigned long)vmx_l1d_flush_pages, L1D_CACHE_ORDER);
8773 ++ vmx_l1d_flush_pages = NULL;
8774 ++ }
8775 ++ /* Restore state so sysfs ignores VMX */
8776 ++ l1tf_vmx_mitigation = VMENTER_L1D_FLUSH_AUTO;
8777 ++}
8778 ++
8779 ++static void vmx_exit(void)
8780 ++{
8781 ++#ifdef CONFIG_KEXEC_CORE
8782 ++ RCU_INIT_POINTER(crash_vmclear_loaded_vmcss, NULL);
8783 ++ synchronize_rcu();
8784 ++#endif
8785 ++
8786 ++ kvm_exit();
8787 ++
8788 ++#if IS_ENABLED(CONFIG_HYPERV)
8789 ++ if (static_branch_unlikely(&enable_evmcs)) {
8790 ++ int cpu;
8791 ++ struct hv_vp_assist_page *vp_ap;
8792 ++ /*
8793 ++ * Reset everything to support using non-enlightened VMCS
8794 ++ * access later (e.g. when we reload the module with
8795 ++ * enlightened_vmcs=0)
8796 ++ */
8797 ++ for_each_online_cpu(cpu) {
8798 ++ vp_ap = hv_get_vp_assist_page(cpu);
8799 ++
8800 ++ if (!vp_ap)
8801 ++ continue;
8802 ++
8803 ++ vp_ap->nested_control.features.directhypercall = 0;
8804 ++ vp_ap->current_nested_vmcs = 0;
8805 ++ vp_ap->enlighten_vmentry = 0;
8806 ++ }
8807 ++
8808 ++ static_branch_disable(&enable_evmcs);
8809 ++ }
8810 ++#endif
8811 ++ vmx_cleanup_l1d_flush();
8812 ++}
8813 ++module_exit(vmx_exit);
8814 ++
8815 ++static int __init vmx_init(void)
8816 ++{
8817 ++ int r;
8818 ++
8819 ++#if IS_ENABLED(CONFIG_HYPERV)
8820 ++ /*
8821 ++ * Enlightened VMCS usage should be recommended and the host needs
8822 ++ * to support eVMCS v1 or above. We can also disable eVMCS support
8823 ++ * with module parameter.
8824 ++ */
8825 ++ if (enlightened_vmcs &&
8826 ++ ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED &&
8827 ++ (ms_hyperv.nested_features & HV_X64_ENLIGHTENED_VMCS_VERSION) >=
8828 ++ KVM_EVMCS_VERSION) {
8829 ++ int cpu;
8830 ++
8831 ++ /* Check that we have assist pages on all online CPUs */
8832 ++ for_each_online_cpu(cpu) {
8833 ++ if (!hv_get_vp_assist_page(cpu)) {
8834 ++ enlightened_vmcs = false;
8835 ++ break;
8836 ++ }
8837 ++ }
8838 ++
8839 ++ if (enlightened_vmcs) {
8840 ++ pr_info("KVM: vmx: using Hyper-V Enlightened VMCS\n");
8841 ++ static_branch_enable(&enable_evmcs);
8842 ++ }
8843 ++
8844 ++ if (ms_hyperv.nested_features & HV_X64_NESTED_DIRECT_FLUSH)
8845 ++ vmx_x86_ops.enable_direct_tlbflush
8846 ++ = hv_enable_direct_tlbflush;
8847 ++
8848 ++ } else {
8849 ++ enlightened_vmcs = false;
8850 ++ }
8851 ++#endif
8852 ++
8853 ++ r = kvm_init(&vmx_x86_ops, sizeof(struct vcpu_vmx),
8854 ++ __alignof__(struct vcpu_vmx), THIS_MODULE);
8855 ++ if (r)
8856 ++ return r;
8857 ++
8858 ++ /*
8859 ++ * Must be called after kvm_init() so enable_ept is properly set
8860 ++ * up. Hand the parameter mitigation value in which was stored in
8861 ++ * the pre module init parser. If no parameter was given, it will
8862 ++ * contain 'auto' which will be turned into the default 'cond'
8863 ++ * mitigation mode.
8864 ++ */
8865 ++ r = vmx_setup_l1d_flush(vmentry_l1d_flush_param);
8866 ++ if (r) {
8867 ++ vmx_exit();
8868 ++ return r;
8869 ++ }
8870 ++
8871 ++#ifdef CONFIG_KEXEC_CORE
8872 ++ rcu_assign_pointer(crash_vmclear_loaded_vmcss,
8873 ++ crash_vmclear_local_loaded_vmcss);
8874 ++#endif
8875 ++ vmx_check_vmcs12_offsets();
8876 ++
8877 ++ return 0;
8878 ++}
8879 ++module_init(vmx_init);
8880 +diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
8881 +index c9c533370e88..43aabd72019b 100644
8882 +--- a/arch/x86/kvm/x86.c
8883 ++++ b/arch/x86/kvm/x86.c
8884 +@@ -54,6 +54,7 @@
8885 + #include <linux/pvclock_gtod.h>
8886 + #include <linux/kvm_irqfd.h>
8887 + #include <linux/irqbypass.h>
8888 ++#include <linux/nospec.h>
8889 + #include <trace/events/kvm.h>
8890 +
8891 + #include <asm/debugreg.h>
8892 +@@ -889,9 +890,11 @@ static u64 kvm_dr6_fixed(struct kvm_vcpu *vcpu)
8893 +
8894 + static int __kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
8895 + {
8896 ++ size_t size = ARRAY_SIZE(vcpu->arch.db);
8897 ++
8898 + switch (dr) {
8899 + case 0 ... 3:
8900 +- vcpu->arch.db[dr] = val;
8901 ++ vcpu->arch.db[array_index_nospec(dr, size)] = val;
8902 + if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
8903 + vcpu->arch.eff_db[dr] = val;
8904 + break;
8905 +@@ -928,9 +931,11 @@ EXPORT_SYMBOL_GPL(kvm_set_dr);
8906 +
8907 + int kvm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *val)
8908 + {
8909 ++ size_t size = ARRAY_SIZE(vcpu->arch.db);
8910 ++
8911 + switch (dr) {
8912 + case 0 ... 3:
8913 +- *val = vcpu->arch.db[dr];
8914 ++ *val = vcpu->arch.db[array_index_nospec(dr, size)];
8915 + break;
8916 + case 4:
8917 + /* fall through */
8918 +@@ -2125,7 +2130,10 @@ static int set_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 data)
8919 + default:
8920 + if (msr >= MSR_IA32_MC0_CTL &&
8921 + msr < MSR_IA32_MCx_CTL(bank_num)) {
8922 +- u32 offset = msr - MSR_IA32_MC0_CTL;
8923 ++ u32 offset = array_index_nospec(
8924 ++ msr - MSR_IA32_MC0_CTL,
8925 ++ MSR_IA32_MCx_CTL(bank_num) - MSR_IA32_MC0_CTL);
8926 ++
8927 + /* only 0 or all 1s can be written to IA32_MCi_CTL
8928 + * some Linux kernels though clear bit 10 in bank 4 to
8929 + * workaround a BIOS/GART TBL issue on AMD K8s, ignore
8930 +@@ -2493,7 +2501,10 @@ static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
8931 + default:
8932 + if (msr >= MSR_IA32_MC0_CTL &&
8933 + msr < MSR_IA32_MCx_CTL(bank_num)) {
8934 +- u32 offset = msr - MSR_IA32_MC0_CTL;
8935 ++ u32 offset = array_index_nospec(
8936 ++ msr - MSR_IA32_MC0_CTL,
8937 ++ MSR_IA32_MCx_CTL(bank_num) - MSR_IA32_MC0_CTL);
8938 ++
8939 + data = vcpu->arch.mce_banks[offset];
8940 + break;
8941 + }
8942 +@@ -6121,14 +6132,12 @@ static void kvm_set_mmio_spte_mask(void)
8943 + /* Set the present bit. */
8944 + mask |= 1ull;
8945 +
8946 +-#ifdef CONFIG_X86_64
8947 + /*
8948 + * If reserved bit is not supported, clear the present bit to disable
8949 + * mmio page fault.
8950 + */
8951 + if (maxphyaddr == 52)
8952 + mask &= ~1ull;
8953 +-#endif
8954 +
8955 + kvm_mmu_set_mmio_spte_mask(mask);
8956 + }
8957 +@@ -7798,7 +7807,7 @@ void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
8958 + kvm_mmu_unload(vcpu);
8959 + vcpu_put(vcpu);
8960 +
8961 +- kvm_x86_ops->vcpu_free(vcpu);
8962 ++ kvm_arch_vcpu_free(vcpu);
8963 + }
8964 +
8965 + void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
8966 +diff --git a/crypto/algapi.c b/crypto/algapi.c
8967 +index 5c098ffa7d3d..9e5b24329b41 100644
8968 +--- a/crypto/algapi.c
8969 ++++ b/crypto/algapi.c
8970 +@@ -652,11 +652,9 @@ EXPORT_SYMBOL_GPL(crypto_grab_spawn);
8971 +
8972 + void crypto_drop_spawn(struct crypto_spawn *spawn)
8973 + {
8974 +- if (!spawn->alg)
8975 +- return;
8976 +-
8977 + down_write(&crypto_alg_sem);
8978 +- list_del(&spawn->list);
8979 ++ if (spawn->alg)
8980 ++ list_del(&spawn->list);
8981 + up_write(&crypto_alg_sem);
8982 + }
8983 + EXPORT_SYMBOL_GPL(crypto_drop_spawn);
8984 +@@ -664,22 +662,16 @@ EXPORT_SYMBOL_GPL(crypto_drop_spawn);
8985 + static struct crypto_alg *crypto_spawn_alg(struct crypto_spawn *spawn)
8986 + {
8987 + struct crypto_alg *alg;
8988 +- struct crypto_alg *alg2;
8989 +
8990 + down_read(&crypto_alg_sem);
8991 + alg = spawn->alg;
8992 +- alg2 = alg;
8993 +- if (alg2)
8994 +- alg2 = crypto_mod_get(alg2);
8995 +- up_read(&crypto_alg_sem);
8996 +-
8997 +- if (!alg2) {
8998 +- if (alg)
8999 +- crypto_shoot_alg(alg);
9000 +- return ERR_PTR(-EAGAIN);
9001 ++ if (alg && !crypto_mod_get(alg)) {
9002 ++ alg->cra_flags |= CRYPTO_ALG_DYING;
9003 ++ alg = NULL;
9004 + }
9005 ++ up_read(&crypto_alg_sem);
9006 +
9007 +- return alg;
9008 ++ return alg ?: ERR_PTR(-EAGAIN);
9009 + }
9010 +
9011 + struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn, u32 type,
9012 +diff --git a/crypto/api.c b/crypto/api.c
9013 +index abf53e67e3d8..b273b3a726a9 100644
9014 +--- a/crypto/api.c
9015 ++++ b/crypto/api.c
9016 +@@ -355,13 +355,12 @@ static unsigned int crypto_ctxsize(struct crypto_alg *alg, u32 type, u32 mask)
9017 + return len;
9018 + }
9019 +
9020 +-void crypto_shoot_alg(struct crypto_alg *alg)
9021 ++static void crypto_shoot_alg(struct crypto_alg *alg)
9022 + {
9023 + down_write(&crypto_alg_sem);
9024 + alg->cra_flags |= CRYPTO_ALG_DYING;
9025 + up_write(&crypto_alg_sem);
9026 + }
9027 +-EXPORT_SYMBOL_GPL(crypto_shoot_alg);
9028 +
9029 + struct crypto_tfm *__crypto_alloc_tfm(struct crypto_alg *alg, u32 type,
9030 + u32 mask)
9031 +diff --git a/crypto/internal.h b/crypto/internal.h
9032 +index 7eefcdb00227..6184c4226a8f 100644
9033 +--- a/crypto/internal.h
9034 ++++ b/crypto/internal.h
9035 +@@ -87,7 +87,6 @@ void crypto_alg_tested(const char *name, int err);
9036 + void crypto_remove_spawns(struct crypto_alg *alg, struct list_head *list,
9037 + struct crypto_alg *nalg);
9038 + void crypto_remove_final(struct list_head *list);
9039 +-void crypto_shoot_alg(struct crypto_alg *alg);
9040 + struct crypto_tfm *__crypto_alloc_tfm(struct crypto_alg *alg, u32 type,
9041 + u32 mask);
9042 + void *crypto_create_tfm(struct crypto_alg *alg,
9043 +diff --git a/crypto/pcrypt.c b/crypto/pcrypt.c
9044 +index 1348541da463..85082574c515 100644
9045 +--- a/crypto/pcrypt.c
9046 ++++ b/crypto/pcrypt.c
9047 +@@ -130,7 +130,6 @@ static void pcrypt_aead_done(struct crypto_async_request *areq, int err)
9048 + struct padata_priv *padata = pcrypt_request_padata(preq);
9049 +
9050 + padata->info = err;
9051 +- req->base.flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
9052 +
9053 + padata_do_serial(padata);
9054 + }
9055 +diff --git a/drivers/clk/tegra/clk-tegra-periph.c b/drivers/clk/tegra/clk-tegra-periph.c
9056 +index 4ce4e7fb1124..d9c1f229c644 100644
9057 +--- a/drivers/clk/tegra/clk-tegra-periph.c
9058 ++++ b/drivers/clk/tegra/clk-tegra-periph.c
9059 +@@ -797,7 +797,11 @@ static struct tegra_periph_init_data gate_clks[] = {
9060 + GATE("vcp", "clk_m", 29, 0, tegra_clk_vcp, 0),
9061 + GATE("apbdma", "clk_m", 34, 0, tegra_clk_apbdma, 0),
9062 + GATE("kbc", "clk_32k", 36, TEGRA_PERIPH_ON_APB | TEGRA_PERIPH_NO_RESET, tegra_clk_kbc, 0),
9063 +- GATE("fuse", "clk_m", 39, TEGRA_PERIPH_ON_APB, tegra_clk_fuse, 0),
9064 ++ /*
9065 ++ * Critical for RAM re-repair operation, which must occur on resume
9066 ++ * from LP1 system suspend and as part of CCPLEX cluster switching.
9067 ++ */
9068 ++ GATE("fuse", "clk_m", 39, TEGRA_PERIPH_ON_APB, tegra_clk_fuse, CLK_IS_CRITICAL),
9069 + GATE("fuse_burn", "clk_m", 39, TEGRA_PERIPH_ON_APB, tegra_clk_fuse_burn, 0),
9070 + GATE("kfuse", "clk_m", 40, TEGRA_PERIPH_ON_APB, tegra_clk_kfuse, 0),
9071 + GATE("apbif", "clk_m", 107, TEGRA_PERIPH_ON_APB, tegra_clk_apbif, 0),
9072 +diff --git a/drivers/crypto/atmel-aes.c b/drivers/crypto/atmel-aes.c
9073 +index e3d40a8dfffb..915253b9c912 100644
9074 +--- a/drivers/crypto/atmel-aes.c
9075 ++++ b/drivers/crypto/atmel-aes.c
9076 +@@ -87,7 +87,6 @@
9077 + struct atmel_aes_caps {
9078 + bool has_dualbuff;
9079 + bool has_cfb64;
9080 +- bool has_ctr32;
9081 + bool has_gcm;
9082 + u32 max_burst_size;
9083 + };
9084 +@@ -923,8 +922,9 @@ static int atmel_aes_ctr_transfer(struct atmel_aes_dev *dd)
9085 + struct atmel_aes_ctr_ctx *ctx = atmel_aes_ctr_ctx_cast(dd->ctx);
9086 + struct ablkcipher_request *req = ablkcipher_request_cast(dd->areq);
9087 + struct scatterlist *src, *dst;
9088 +- u32 ctr, blocks;
9089 + size_t datalen;
9090 ++ u32 ctr;
9091 ++ u16 blocks, start, end;
9092 + bool use_dma, fragmented = false;
9093 +
9094 + /* Check for transfer completion. */
9095 +@@ -936,27 +936,17 @@ static int atmel_aes_ctr_transfer(struct atmel_aes_dev *dd)
9096 + datalen = req->nbytes - ctx->offset;
9097 + blocks = DIV_ROUND_UP(datalen, AES_BLOCK_SIZE);
9098 + ctr = be32_to_cpu(ctx->iv[3]);
9099 +- if (dd->caps.has_ctr32) {
9100 +- /* Check 32bit counter overflow. */
9101 +- u32 start = ctr;
9102 +- u32 end = start + blocks - 1;
9103 +-
9104 +- if (end < start) {
9105 +- ctr |= 0xffffffff;
9106 +- datalen = AES_BLOCK_SIZE * -start;
9107 +- fragmented = true;
9108 +- }
9109 +- } else {
9110 +- /* Check 16bit counter overflow. */
9111 +- u16 start = ctr & 0xffff;
9112 +- u16 end = start + (u16)blocks - 1;
9113 +-
9114 +- if (blocks >> 16 || end < start) {
9115 +- ctr |= 0xffff;
9116 +- datalen = AES_BLOCK_SIZE * (0x10000-start);
9117 +- fragmented = true;
9118 +- }
9119 ++
9120 ++ /* Check 16bit counter overflow. */
9121 ++ start = ctr & 0xffff;
9122 ++ end = start + blocks - 1;
9123 ++
9124 ++ if (blocks >> 16 || end < start) {
9125 ++ ctr |= 0xffff;
9126 ++ datalen = AES_BLOCK_SIZE * (0x10000 - start);
9127 ++ fragmented = true;
9128 + }
9129 ++
9130 + use_dma = (datalen >= ATMEL_AES_DMA_THRESHOLD);
9131 +
9132 + /* Jump to offset. */
9133 +@@ -1926,7 +1916,6 @@ static void atmel_aes_get_cap(struct atmel_aes_dev *dd)
9134 + {
9135 + dd->caps.has_dualbuff = 0;
9136 + dd->caps.has_cfb64 = 0;
9137 +- dd->caps.has_ctr32 = 0;
9138 + dd->caps.has_gcm = 0;
9139 + dd->caps.max_burst_size = 1;
9140 +
9141 +@@ -1935,14 +1924,12 @@ static void atmel_aes_get_cap(struct atmel_aes_dev *dd)
9142 + case 0x500:
9143 + dd->caps.has_dualbuff = 1;
9144 + dd->caps.has_cfb64 = 1;
9145 +- dd->caps.has_ctr32 = 1;
9146 + dd->caps.has_gcm = 1;
9147 + dd->caps.max_burst_size = 4;
9148 + break;
9149 + case 0x200:
9150 + dd->caps.has_dualbuff = 1;
9151 + dd->caps.has_cfb64 = 1;
9152 +- dd->caps.has_ctr32 = 1;
9153 + dd->caps.has_gcm = 1;
9154 + dd->caps.max_burst_size = 4;
9155 + break;
9156 +diff --git a/drivers/crypto/picoxcell_crypto.c b/drivers/crypto/picoxcell_crypto.c
9157 +index 47576098831f..b3ea6d60c458 100644
9158 +--- a/drivers/crypto/picoxcell_crypto.c
9159 ++++ b/drivers/crypto/picoxcell_crypto.c
9160 +@@ -1632,6 +1632,11 @@ static bool spacc_is_compatible(struct platform_device *pdev,
9161 + return false;
9162 + }
9163 +
9164 ++static void spacc_tasklet_kill(void *data)
9165 ++{
9166 ++ tasklet_kill(data);
9167 ++}
9168 ++
9169 + static int spacc_probe(struct platform_device *pdev)
9170 + {
9171 + int i, err, ret = -EINVAL;
9172 +@@ -1674,6 +1679,14 @@ static int spacc_probe(struct platform_device *pdev)
9173 + return -ENXIO;
9174 + }
9175 +
9176 ++ tasklet_init(&engine->complete, spacc_spacc_complete,
9177 ++ (unsigned long)engine);
9178 ++
9179 ++ ret = devm_add_action(&pdev->dev, spacc_tasklet_kill,
9180 ++ &engine->complete);
9181 ++ if (ret)
9182 ++ return ret;
9183 ++
9184 + if (devm_request_irq(&pdev->dev, irq->start, spacc_spacc_irq, 0,
9185 + engine->name, engine)) {
9186 + dev_err(engine->dev, "failed to request IRQ\n");
9187 +@@ -1736,8 +1749,6 @@ static int spacc_probe(struct platform_device *pdev)
9188 + INIT_LIST_HEAD(&engine->completed);
9189 + INIT_LIST_HEAD(&engine->in_progress);
9190 + engine->in_flight = 0;
9191 +- tasklet_init(&engine->complete, spacc_spacc_complete,
9192 +- (unsigned long)engine);
9193 +
9194 + platform_set_drvdata(pdev, engine);
9195 +
9196 +diff --git a/drivers/gpu/drm/atmel-hlcdc/atmel_hlcdc_crtc.c b/drivers/gpu/drm/atmel-hlcdc/atmel_hlcdc_crtc.c
9197 +index 9b17a66cf0e1..aa54a6a2ad1d 100644
9198 +--- a/drivers/gpu/drm/atmel-hlcdc/atmel_hlcdc_crtc.c
9199 ++++ b/drivers/gpu/drm/atmel-hlcdc/atmel_hlcdc_crtc.c
9200 +@@ -81,7 +81,11 @@ static void atmel_hlcdc_crtc_mode_set_nofb(struct drm_crtc *c)
9201 + struct videomode vm;
9202 + unsigned long prate;
9203 + unsigned int cfg;
9204 +- int div;
9205 ++ int div, ret;
9206 ++
9207 ++ ret = clk_prepare_enable(crtc->dc->hlcdc->sys_clk);
9208 ++ if (ret)
9209 ++ return;
9210 +
9211 + vm.vfront_porch = adj->crtc_vsync_start - adj->crtc_vdisplay;
9212 + vm.vback_porch = adj->crtc_vtotal - adj->crtc_vsync_end;
9213 +@@ -140,6 +144,8 @@ static void atmel_hlcdc_crtc_mode_set_nofb(struct drm_crtc *c)
9214 + ATMEL_HLCDC_VSPSU | ATMEL_HLCDC_VSPHO |
9215 + ATMEL_HLCDC_GUARDTIME_MASK | ATMEL_HLCDC_MODE_MASK,
9216 + cfg);
9217 ++
9218 ++ clk_disable_unprepare(crtc->dc->hlcdc->sys_clk);
9219 + }
9220 +
9221 + static bool atmel_hlcdc_crtc_mode_fixup(struct drm_crtc *c,
9222 +diff --git a/drivers/infiniband/core/addr.c b/drivers/infiniband/core/addr.c
9223 +index 1baa25e82bdd..f7d23c1081dc 100644
9224 +--- a/drivers/infiniband/core/addr.c
9225 ++++ b/drivers/infiniband/core/addr.c
9226 +@@ -141,7 +141,7 @@ int ib_nl_handle_ip_res_resp(struct sk_buff *skb,
9227 + if (ib_nl_is_good_ip_resp(nlh))
9228 + ib_nl_process_good_ip_rsep(nlh);
9229 +
9230 +- return skb->len;
9231 ++ return 0;
9232 + }
9233 +
9234 + static int ib_nl_ip_send_msg(struct rdma_dev_addr *dev_addr,
9235 +diff --git a/drivers/infiniband/core/sa_query.c b/drivers/infiniband/core/sa_query.c
9236 +index 5879a06ada93..1c459725d64e 100644
9237 +--- a/drivers/infiniband/core/sa_query.c
9238 ++++ b/drivers/infiniband/core/sa_query.c
9239 +@@ -848,7 +848,7 @@ int ib_nl_handle_set_timeout(struct sk_buff *skb,
9240 + }
9241 +
9242 + settimeout_out:
9243 +- return skb->len;
9244 ++ return 0;
9245 + }
9246 +
9247 + static inline int ib_nl_is_good_resolve_resp(const struct nlmsghdr *nlh)
9248 +@@ -920,7 +920,7 @@ int ib_nl_handle_resolve_resp(struct sk_buff *skb,
9249 + }
9250 +
9251 + resp_out:
9252 +- return skb->len;
9253 ++ return 0;
9254 + }
9255 +
9256 + static void free_sm_ah(struct kref *kref)
9257 +diff --git a/drivers/infiniband/hw/mlx5/gsi.c b/drivers/infiniband/hw/mlx5/gsi.c
9258 +index 79e6309460dc..262c18b2f525 100644
9259 +--- a/drivers/infiniband/hw/mlx5/gsi.c
9260 ++++ b/drivers/infiniband/hw/mlx5/gsi.c
9261 +@@ -507,8 +507,7 @@ int mlx5_ib_gsi_post_send(struct ib_qp *qp, struct ib_send_wr *wr,
9262 + ret = ib_post_send(tx_qp, &cur_wr.wr, bad_wr);
9263 + if (ret) {
9264 + /* Undo the effect of adding the outstanding wr */
9265 +- gsi->outstanding_pi = (gsi->outstanding_pi - 1) %
9266 +- gsi->cap.max_send_wr;
9267 ++ gsi->outstanding_pi--;
9268 + goto err;
9269 + }
9270 + spin_unlock_irqrestore(&gsi->lock, flags);
9271 +diff --git a/drivers/md/dm.c b/drivers/md/dm.c
9272 +index 36e6221fabab..dd154027adc9 100644
9273 +--- a/drivers/md/dm.c
9274 ++++ b/drivers/md/dm.c
9275 +@@ -1457,7 +1457,6 @@ void dm_init_md_queue(struct mapped_device *md)
9276 + * - must do so here (in alloc_dev callchain) before queue is used
9277 + */
9278 + md->queue->queuedata = md;
9279 +- md->queue->backing_dev_info.congested_data = md;
9280 + }
9281 +
9282 + void dm_init_normal_md_queue(struct mapped_device *md)
9283 +@@ -1468,6 +1467,7 @@ void dm_init_normal_md_queue(struct mapped_device *md)
9284 + /*
9285 + * Initialize aspects of queue that aren't relevant for blk-mq
9286 + */
9287 ++ md->queue->backing_dev_info.congested_data = md;
9288 + md->queue->backing_dev_info.congested_fn = dm_any_congested;
9289 + blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
9290 + }
9291 +@@ -1555,6 +1555,12 @@ static struct mapped_device *alloc_dev(int minor)
9292 + goto bad;
9293 +
9294 + dm_init_md_queue(md);
9295 ++ /*
9296 ++ * default to bio-based required ->make_request_fn until DM
9297 ++ * table is loaded and md->type established. If request-based
9298 ++ * table is loaded: blk-mq will override accordingly.
9299 ++ */
9300 ++ blk_queue_make_request(md->queue, dm_make_request);
9301 +
9302 + md->disk = alloc_disk_node(1, numa_node_id);
9303 + if (!md->disk)
9304 +@@ -1853,7 +1859,6 @@ int dm_setup_md_queue(struct mapped_device *md, struct dm_table *t)
9305 + case DM_TYPE_BIO_BASED:
9306 + case DM_TYPE_DAX_BIO_BASED:
9307 + dm_init_normal_md_queue(md);
9308 +- blk_queue_make_request(md->queue, dm_make_request);
9309 + /*
9310 + * DM handles splitting bios as needed. Free the bio_split bioset
9311 + * since it won't be used (saves 1 process per bio-based DM device).
9312 +diff --git a/drivers/md/persistent-data/dm-space-map-common.c b/drivers/md/persistent-data/dm-space-map-common.c
9313 +index 306d2e4502c4..22729fd92a1b 100644
9314 +--- a/drivers/md/persistent-data/dm-space-map-common.c
9315 ++++ b/drivers/md/persistent-data/dm-space-map-common.c
9316 +@@ -382,6 +382,33 @@ int sm_ll_find_free_block(struct ll_disk *ll, dm_block_t begin,
9317 + return -ENOSPC;
9318 + }
9319 +
9320 ++int sm_ll_find_common_free_block(struct ll_disk *old_ll, struct ll_disk *new_ll,
9321 ++ dm_block_t begin, dm_block_t end, dm_block_t *b)
9322 ++{
9323 ++ int r;
9324 ++ uint32_t count;
9325 ++
9326 ++ do {
9327 ++ r = sm_ll_find_free_block(new_ll, begin, new_ll->nr_blocks, b);
9328 ++ if (r)
9329 ++ break;
9330 ++
9331 ++ /* double check this block wasn't used in the old transaction */
9332 ++ if (*b >= old_ll->nr_blocks)
9333 ++ count = 0;
9334 ++ else {
9335 ++ r = sm_ll_lookup(old_ll, *b, &count);
9336 ++ if (r)
9337 ++ break;
9338 ++
9339 ++ if (count)
9340 ++ begin = *b + 1;
9341 ++ }
9342 ++ } while (count);
9343 ++
9344 ++ return r;
9345 ++}
9346 ++
9347 + static int sm_ll_mutate(struct ll_disk *ll, dm_block_t b,
9348 + int (*mutator)(void *context, uint32_t old, uint32_t *new),
9349 + void *context, enum allocation_event *ev)
9350 +diff --git a/drivers/md/persistent-data/dm-space-map-common.h b/drivers/md/persistent-data/dm-space-map-common.h
9351 +index b3078d5eda0c..8de63ce39bdd 100644
9352 +--- a/drivers/md/persistent-data/dm-space-map-common.h
9353 ++++ b/drivers/md/persistent-data/dm-space-map-common.h
9354 +@@ -109,6 +109,8 @@ int sm_ll_lookup_bitmap(struct ll_disk *ll, dm_block_t b, uint32_t *result);
9355 + int sm_ll_lookup(struct ll_disk *ll, dm_block_t b, uint32_t *result);
9356 + int sm_ll_find_free_block(struct ll_disk *ll, dm_block_t begin,
9357 + dm_block_t end, dm_block_t *result);
9358 ++int sm_ll_find_common_free_block(struct ll_disk *old_ll, struct ll_disk *new_ll,
9359 ++ dm_block_t begin, dm_block_t end, dm_block_t *result);
9360 + int sm_ll_insert(struct ll_disk *ll, dm_block_t b, uint32_t ref_count, enum allocation_event *ev);
9361 + int sm_ll_inc(struct ll_disk *ll, dm_block_t b, enum allocation_event *ev);
9362 + int sm_ll_dec(struct ll_disk *ll, dm_block_t b, enum allocation_event *ev);
9363 +diff --git a/drivers/md/persistent-data/dm-space-map-disk.c b/drivers/md/persistent-data/dm-space-map-disk.c
9364 +index 32adf6b4a9c7..bf4c5e2ccb6f 100644
9365 +--- a/drivers/md/persistent-data/dm-space-map-disk.c
9366 ++++ b/drivers/md/persistent-data/dm-space-map-disk.c
9367 +@@ -167,8 +167,10 @@ static int sm_disk_new_block(struct dm_space_map *sm, dm_block_t *b)
9368 + enum allocation_event ev;
9369 + struct sm_disk *smd = container_of(sm, struct sm_disk, sm);
9370 +
9371 +- /* FIXME: we should loop round a couple of times */
9372 +- r = sm_ll_find_free_block(&smd->old_ll, smd->begin, smd->old_ll.nr_blocks, b);
9373 ++ /*
9374 ++ * Any block we allocate has to be free in both the old and current ll.
9375 ++ */
9376 ++ r = sm_ll_find_common_free_block(&smd->old_ll, &smd->ll, smd->begin, smd->ll.nr_blocks, b);
9377 + if (r)
9378 + return r;
9379 +
9380 +diff --git a/drivers/md/persistent-data/dm-space-map-metadata.c b/drivers/md/persistent-data/dm-space-map-metadata.c
9381 +index 1d29771af380..967d8f2a731f 100644
9382 +--- a/drivers/md/persistent-data/dm-space-map-metadata.c
9383 ++++ b/drivers/md/persistent-data/dm-space-map-metadata.c
9384 +@@ -447,7 +447,10 @@ static int sm_metadata_new_block_(struct dm_space_map *sm, dm_block_t *b)
9385 + enum allocation_event ev;
9386 + struct sm_metadata *smm = container_of(sm, struct sm_metadata, sm);
9387 +
9388 +- r = sm_ll_find_free_block(&smm->old_ll, smm->begin, smm->old_ll.nr_blocks, b);
9389 ++ /*
9390 ++ * Any block we allocate has to be free in both the old and current ll.
9391 ++ */
9392 ++ r = sm_ll_find_common_free_block(&smm->old_ll, &smm->ll, smm->begin, smm->ll.nr_blocks, b);
9393 + if (r)
9394 + return r;
9395 +
9396 +diff --git a/drivers/media/rc/iguanair.c b/drivers/media/rc/iguanair.c
9397 +index 25470395c43f..246795c31553 100644
9398 +--- a/drivers/media/rc/iguanair.c
9399 ++++ b/drivers/media/rc/iguanair.c
9400 +@@ -430,7 +430,7 @@ static int iguanair_probe(struct usb_interface *intf,
9401 + int ret, pipein, pipeout;
9402 + struct usb_host_interface *idesc;
9403 +
9404 +- idesc = intf->altsetting;
9405 ++ idesc = intf->cur_altsetting;
9406 + if (idesc->desc.bNumEndpoints < 2)
9407 + return -ENODEV;
9408 +
9409 +diff --git a/drivers/media/usb/uvc/uvc_driver.c b/drivers/media/usb/uvc/uvc_driver.c
9410 +index 7c375b6dd318..9803135f2e59 100644
9411 +--- a/drivers/media/usb/uvc/uvc_driver.c
9412 ++++ b/drivers/media/usb/uvc/uvc_driver.c
9413 +@@ -1411,6 +1411,11 @@ static int uvc_scan_chain_forward(struct uvc_video_chain *chain,
9414 + break;
9415 + if (forward == prev)
9416 + continue;
9417 ++ if (forward->chain.next || forward->chain.prev) {
9418 ++ uvc_trace(UVC_TRACE_DESCR, "Found reference to "
9419 ++ "entity %d already in chain.\n", forward->id);
9420 ++ return -EINVAL;
9421 ++ }
9422 +
9423 + switch (UVC_ENTITY_TYPE(forward)) {
9424 + case UVC_VC_EXTENSION_UNIT:
9425 +@@ -1492,6 +1497,13 @@ static int uvc_scan_chain_backward(struct uvc_video_chain *chain,
9426 + return -1;
9427 + }
9428 +
9429 ++ if (term->chain.next || term->chain.prev) {
9430 ++ uvc_trace(UVC_TRACE_DESCR, "Found reference to "
9431 ++ "entity %d already in chain.\n",
9432 ++ term->id);
9433 ++ return -EINVAL;
9434 ++ }
9435 ++
9436 + if (uvc_trace_param & UVC_TRACE_PROBE)
9437 + printk(" %d", term->id);
9438 +
9439 +diff --git a/drivers/media/v4l2-core/videobuf-dma-sg.c b/drivers/media/v4l2-core/videobuf-dma-sg.c
9440 +index b6189a4958c5..025822bc6941 100644
9441 +--- a/drivers/media/v4l2-core/videobuf-dma-sg.c
9442 ++++ b/drivers/media/v4l2-core/videobuf-dma-sg.c
9443 +@@ -352,8 +352,11 @@ int videobuf_dma_free(struct videobuf_dmabuf *dma)
9444 + BUG_ON(dma->sglen);
9445 +
9446 + if (dma->pages) {
9447 +- for (i = 0; i < dma->nr_pages; i++)
9448 ++ for (i = 0; i < dma->nr_pages; i++) {
9449 ++ if (dma->direction == DMA_FROM_DEVICE)
9450 ++ set_page_dirty_lock(dma->pages[i]);
9451 + put_page(dma->pages[i]);
9452 ++ }
9453 + kfree(dma->pages);
9454 + dma->pages = NULL;
9455 + }
9456 +diff --git a/drivers/mfd/da9062-core.c b/drivers/mfd/da9062-core.c
9457 +index 8f873866ea60..86aab322da33 100644
9458 +--- a/drivers/mfd/da9062-core.c
9459 ++++ b/drivers/mfd/da9062-core.c
9460 +@@ -142,7 +142,7 @@ static const struct mfd_cell da9062_devs[] = {
9461 + .name = "da9062-watchdog",
9462 + .num_resources = ARRAY_SIZE(da9062_wdt_resources),
9463 + .resources = da9062_wdt_resources,
9464 +- .of_compatible = "dlg,da9062-wdt",
9465 ++ .of_compatible = "dlg,da9062-watchdog",
9466 + },
9467 + {
9468 + .name = "da9062-thermal",
9469 +diff --git a/drivers/mfd/dln2.c b/drivers/mfd/dln2.c
9470 +index 704e189ca162..95d0f2df0ad4 100644
9471 +--- a/drivers/mfd/dln2.c
9472 ++++ b/drivers/mfd/dln2.c
9473 +@@ -729,6 +729,8 @@ static int dln2_probe(struct usb_interface *interface,
9474 + const struct usb_device_id *usb_id)
9475 + {
9476 + struct usb_host_interface *hostif = interface->cur_altsetting;
9477 ++ struct usb_endpoint_descriptor *epin;
9478 ++ struct usb_endpoint_descriptor *epout;
9479 + struct device *dev = &interface->dev;
9480 + struct dln2_dev *dln2;
9481 + int ret;
9482 +@@ -738,12 +740,19 @@ static int dln2_probe(struct usb_interface *interface,
9483 + hostif->desc.bNumEndpoints < 2)
9484 + return -ENODEV;
9485 +
9486 ++ epin = &hostif->endpoint[0].desc;
9487 ++ epout = &hostif->endpoint[1].desc;
9488 ++ if (!usb_endpoint_is_bulk_out(epout))
9489 ++ return -ENODEV;
9490 ++ if (!usb_endpoint_is_bulk_in(epin))
9491 ++ return -ENODEV;
9492 ++
9493 + dln2 = kzalloc(sizeof(*dln2), GFP_KERNEL);
9494 + if (!dln2)
9495 + return -ENOMEM;
9496 +
9497 +- dln2->ep_out = hostif->endpoint[0].desc.bEndpointAddress;
9498 +- dln2->ep_in = hostif->endpoint[1].desc.bEndpointAddress;
9499 ++ dln2->ep_out = epout->bEndpointAddress;
9500 ++ dln2->ep_in = epin->bEndpointAddress;
9501 + dln2->usb_dev = usb_get_dev(interface_to_usbdev(interface));
9502 + dln2->interface = interface;
9503 + usb_set_intfdata(interface, dln2);
9504 +diff --git a/drivers/mfd/rn5t618.c b/drivers/mfd/rn5t618.c
9505 +index ee94080e1cbb..dd20c3e32352 100644
9506 +--- a/drivers/mfd/rn5t618.c
9507 ++++ b/drivers/mfd/rn5t618.c
9508 +@@ -32,6 +32,7 @@ static bool rn5t618_volatile_reg(struct device *dev, unsigned int reg)
9509 + case RN5T618_WATCHDOGCNT:
9510 + case RN5T618_DCIRQ:
9511 + case RN5T618_ILIMDATAH ... RN5T618_AIN0DATAL:
9512 ++ case RN5T618_ADCCNT3:
9513 + case RN5T618_IR_ADC1 ... RN5T618_IR_ADC3:
9514 + case RN5T618_IR_GPR:
9515 + case RN5T618_IR_GPF:
9516 +diff --git a/drivers/mmc/host/mmc_spi.c b/drivers/mmc/host/mmc_spi.c
9517 +index c2df68e958b3..279d5da6e54b 100644
9518 +--- a/drivers/mmc/host/mmc_spi.c
9519 ++++ b/drivers/mmc/host/mmc_spi.c
9520 +@@ -1157,17 +1157,22 @@ static void mmc_spi_initsequence(struct mmc_spi_host *host)
9521 + * SPI protocol. Another is that when chipselect is released while
9522 + * the card returns BUSY status, the clock must issue several cycles
9523 + * with chipselect high before the card will stop driving its output.
9524 ++ *
9525 ++ * SPI_CS_HIGH means "asserted" here. In some cases like when using
9526 ++ * GPIOs for chip select, SPI_CS_HIGH is set but this will be logically
9527 ++ * inverted by gpiolib, so if we want to ascertain to drive it high
9528 ++ * we should toggle the default with an XOR as we do here.
9529 + */
9530 +- host->spi->mode |= SPI_CS_HIGH;
9531 ++ host->spi->mode ^= SPI_CS_HIGH;
9532 + if (spi_setup(host->spi) != 0) {
9533 + /* Just warn; most cards work without it. */
9534 + dev_warn(&host->spi->dev,
9535 + "can't change chip-select polarity\n");
9536 +- host->spi->mode &= ~SPI_CS_HIGH;
9537 ++ host->spi->mode ^= SPI_CS_HIGH;
9538 + } else {
9539 + mmc_spi_readbytes(host, 18);
9540 +
9541 +- host->spi->mode &= ~SPI_CS_HIGH;
9542 ++ host->spi->mode ^= SPI_CS_HIGH;
9543 + if (spi_setup(host->spi) != 0) {
9544 + /* Wot, we can't get the same setup we had before? */
9545 + dev_err(&host->spi->dev,
9546 +diff --git a/drivers/mtd/ubi/fastmap.c b/drivers/mtd/ubi/fastmap.c
9547 +index b44c8d348e78..e7b177c61642 100644
9548 +--- a/drivers/mtd/ubi/fastmap.c
9549 ++++ b/drivers/mtd/ubi/fastmap.c
9550 +@@ -73,7 +73,7 @@ static int self_check_seen(struct ubi_device *ubi, unsigned long *seen)
9551 + return 0;
9552 +
9553 + for (pnum = 0; pnum < ubi->peb_count; pnum++) {
9554 +- if (test_bit(pnum, seen) && ubi->lookuptbl[pnum]) {
9555 ++ if (!test_bit(pnum, seen) && ubi->lookuptbl[pnum]) {
9556 + ubi_err(ubi, "self-check failed for PEB %d, fastmap didn't see it", pnum);
9557 + ret = -EINVAL;
9558 + }
9559 +@@ -1127,7 +1127,7 @@ static int ubi_write_fastmap(struct ubi_device *ubi,
9560 + struct rb_node *tmp_rb;
9561 + int ret, i, j, free_peb_count, used_peb_count, vol_count;
9562 + int scrub_peb_count, erase_peb_count;
9563 +- unsigned long *seen_pebs = NULL;
9564 ++ unsigned long *seen_pebs;
9565 +
9566 + fm_raw = ubi->fm_buf;
9567 + memset(ubi->fm_buf, 0, ubi->fm_size);
9568 +@@ -1141,7 +1141,7 @@ static int ubi_write_fastmap(struct ubi_device *ubi,
9569 + dvbuf = new_fm_vbuf(ubi, UBI_FM_DATA_VOLUME_ID);
9570 + if (!dvbuf) {
9571 + ret = -ENOMEM;
9572 +- goto out_kfree;
9573 ++ goto out_free_avbuf;
9574 + }
9575 +
9576 + avhdr = ubi_get_vid_hdr(avbuf);
9577 +@@ -1150,7 +1150,7 @@ static int ubi_write_fastmap(struct ubi_device *ubi,
9578 + seen_pebs = init_seen(ubi);
9579 + if (IS_ERR(seen_pebs)) {
9580 + ret = PTR_ERR(seen_pebs);
9581 +- goto out_kfree;
9582 ++ goto out_free_dvbuf;
9583 + }
9584 +
9585 + spin_lock(&ubi->volumes_lock);
9586 +@@ -1318,7 +1318,7 @@ static int ubi_write_fastmap(struct ubi_device *ubi,
9587 + ret = ubi_io_write_vid_hdr(ubi, new_fm->e[0]->pnum, avbuf);
9588 + if (ret) {
9589 + ubi_err(ubi, "unable to write vid_hdr to fastmap SB!");
9590 +- goto out_kfree;
9591 ++ goto out_free_seen;
9592 + }
9593 +
9594 + for (i = 0; i < new_fm->used_blocks; i++) {
9595 +@@ -1340,7 +1340,7 @@ static int ubi_write_fastmap(struct ubi_device *ubi,
9596 + if (ret) {
9597 + ubi_err(ubi, "unable to write vid_hdr to PEB %i!",
9598 + new_fm->e[i]->pnum);
9599 +- goto out_kfree;
9600 ++ goto out_free_seen;
9601 + }
9602 + }
9603 +
9604 +@@ -1350,7 +1350,7 @@ static int ubi_write_fastmap(struct ubi_device *ubi,
9605 + if (ret) {
9606 + ubi_err(ubi, "unable to write fastmap to PEB %i!",
9607 + new_fm->e[i]->pnum);
9608 +- goto out_kfree;
9609 ++ goto out_free_seen;
9610 + }
9611 + }
9612 +
9613 +@@ -1360,10 +1360,13 @@ static int ubi_write_fastmap(struct ubi_device *ubi,
9614 + ret = self_check_seen(ubi, seen_pebs);
9615 + dbg_bld("fastmap written!");
9616 +
9617 +-out_kfree:
9618 +- ubi_free_vid_buf(avbuf);
9619 +- ubi_free_vid_buf(dvbuf);
9620 ++out_free_seen:
9621 + free_seen(seen_pebs);
9622 ++out_free_dvbuf:
9623 ++ ubi_free_vid_buf(dvbuf);
9624 ++out_free_avbuf:
9625 ++ ubi_free_vid_buf(avbuf);
9626 ++
9627 + out:
9628 + return ret;
9629 + }
9630 +diff --git a/drivers/net/bonding/bond_alb.c b/drivers/net/bonding/bond_alb.c
9631 +index 91d8a48e53c3..9834d28d52e8 100644
9632 +--- a/drivers/net/bonding/bond_alb.c
9633 ++++ b/drivers/net/bonding/bond_alb.c
9634 +@@ -1371,26 +1371,31 @@ int bond_alb_xmit(struct sk_buff *skb, struct net_device *bond_dev)
9635 + bool do_tx_balance = true;
9636 + u32 hash_index = 0;
9637 + const u8 *hash_start = NULL;
9638 +- struct ipv6hdr *ip6hdr;
9639 +
9640 + skb_reset_mac_header(skb);
9641 + eth_data = eth_hdr(skb);
9642 +
9643 + switch (ntohs(skb->protocol)) {
9644 + case ETH_P_IP: {
9645 +- const struct iphdr *iph = ip_hdr(skb);
9646 ++ const struct iphdr *iph;
9647 +
9648 + if (ether_addr_equal_64bits(eth_data->h_dest, mac_bcast) ||
9649 +- (iph->daddr == ip_bcast) ||
9650 +- (iph->protocol == IPPROTO_IGMP)) {
9651 ++ (!pskb_network_may_pull(skb, sizeof(*iph)))) {
9652 ++ do_tx_balance = false;
9653 ++ break;
9654 ++ }
9655 ++ iph = ip_hdr(skb);
9656 ++ if (iph->daddr == ip_bcast || iph->protocol == IPPROTO_IGMP) {
9657 + do_tx_balance = false;
9658 + break;
9659 + }
9660 + hash_start = (char *)&(iph->daddr);
9661 + hash_size = sizeof(iph->daddr);
9662 +- }
9663 + break;
9664 +- case ETH_P_IPV6:
9665 ++ }
9666 ++ case ETH_P_IPV6: {
9667 ++ const struct ipv6hdr *ip6hdr;
9668 ++
9669 + /* IPv6 doesn't really use broadcast mac address, but leave
9670 + * that here just in case.
9671 + */
9672 +@@ -1407,7 +1412,11 @@ int bond_alb_xmit(struct sk_buff *skb, struct net_device *bond_dev)
9673 + break;
9674 + }
9675 +
9676 +- /* Additianally, DAD probes should not be tx-balanced as that
9677 ++ if (!pskb_network_may_pull(skb, sizeof(*ip6hdr))) {
9678 ++ do_tx_balance = false;
9679 ++ break;
9680 ++ }
9681 ++ /* Additionally, DAD probes should not be tx-balanced as that
9682 + * will lead to false positives for duplicate addresses and
9683 + * prevent address configuration from working.
9684 + */
9685 +@@ -1417,17 +1426,26 @@ int bond_alb_xmit(struct sk_buff *skb, struct net_device *bond_dev)
9686 + break;
9687 + }
9688 +
9689 +- hash_start = (char *)&(ipv6_hdr(skb)->daddr);
9690 +- hash_size = sizeof(ipv6_hdr(skb)->daddr);
9691 ++ hash_start = (char *)&ip6hdr->daddr;
9692 ++ hash_size = sizeof(ip6hdr->daddr);
9693 + break;
9694 +- case ETH_P_IPX:
9695 +- if (ipx_hdr(skb)->ipx_checksum != IPX_NO_CHECKSUM) {
9696 ++ }
9697 ++ case ETH_P_IPX: {
9698 ++ const struct ipxhdr *ipxhdr;
9699 ++
9700 ++ if (pskb_network_may_pull(skb, sizeof(*ipxhdr))) {
9701 ++ do_tx_balance = false;
9702 ++ break;
9703 ++ }
9704 ++ ipxhdr = (struct ipxhdr *)skb_network_header(skb);
9705 ++
9706 ++ if (ipxhdr->ipx_checksum != IPX_NO_CHECKSUM) {
9707 + /* something is wrong with this packet */
9708 + do_tx_balance = false;
9709 + break;
9710 + }
9711 +
9712 +- if (ipx_hdr(skb)->ipx_type != IPX_TYPE_NCP) {
9713 ++ if (ipxhdr->ipx_type != IPX_TYPE_NCP) {
9714 + /* The only protocol worth balancing in
9715 + * this family since it has an "ARP" like
9716 + * mechanism
9717 +@@ -1436,9 +1454,11 @@ int bond_alb_xmit(struct sk_buff *skb, struct net_device *bond_dev)
9718 + break;
9719 + }
9720 +
9721 ++ eth_data = eth_hdr(skb);
9722 + hash_start = (char *)eth_data->h_dest;
9723 + hash_size = ETH_ALEN;
9724 + break;
9725 ++ }
9726 + case ETH_P_ARP:
9727 + do_tx_balance = false;
9728 + if (bond_info->rlb_enabled)
9729 +diff --git a/drivers/net/ethernet/broadcom/bcmsysport.c b/drivers/net/ethernet/broadcom/bcmsysport.c
9730 +index e3b41af65d18..6519dd33c7ca 100644
9731 +--- a/drivers/net/ethernet/broadcom/bcmsysport.c
9732 ++++ b/drivers/net/ethernet/broadcom/bcmsysport.c
9733 +@@ -1983,6 +1983,9 @@ static int bcm_sysport_resume(struct device *d)
9734 +
9735 + umac_reset(priv);
9736 +
9737 ++ /* Disable the UniMAC RX/TX */
9738 ++ umac_enable_set(priv, CMD_RX_EN | CMD_TX_EN, 0);
9739 ++
9740 + /* We may have been suspended and never received a WOL event that
9741 + * would turn off MPD detection, take care of that now
9742 + */
9743 +diff --git a/drivers/net/ethernet/dec/tulip/dmfe.c b/drivers/net/ethernet/dec/tulip/dmfe.c
9744 +index 8ed0fd8b1dda..74cb9b3c2f41 100644
9745 +--- a/drivers/net/ethernet/dec/tulip/dmfe.c
9746 ++++ b/drivers/net/ethernet/dec/tulip/dmfe.c
9747 +@@ -2225,15 +2225,16 @@ static int __init dmfe_init_module(void)
9748 + if (cr6set)
9749 + dmfe_cr6_user_set = cr6set;
9750 +
9751 +- switch(mode) {
9752 +- case DMFE_10MHF:
9753 ++ switch (mode) {
9754 ++ case DMFE_10MHF:
9755 + case DMFE_100MHF:
9756 + case DMFE_10MFD:
9757 + case DMFE_100MFD:
9758 + case DMFE_1M_HPNA:
9759 + dmfe_media_mode = mode;
9760 + break;
9761 +- default:dmfe_media_mode = DMFE_AUTO;
9762 ++ default:
9763 ++ dmfe_media_mode = DMFE_AUTO;
9764 + break;
9765 + }
9766 +
9767 +diff --git a/drivers/net/ethernet/dec/tulip/uli526x.c b/drivers/net/ethernet/dec/tulip/uli526x.c
9768 +index e750b5ddc0fb..5f79e2731b76 100644
9769 +--- a/drivers/net/ethernet/dec/tulip/uli526x.c
9770 ++++ b/drivers/net/ethernet/dec/tulip/uli526x.c
9771 +@@ -1813,8 +1813,8 @@ static int __init uli526x_init_module(void)
9772 + if (cr6set)
9773 + uli526x_cr6_user_set = cr6set;
9774 +
9775 +- switch (mode) {
9776 +- case ULI526X_10MHF:
9777 ++ switch (mode) {
9778 ++ case ULI526X_10MHF:
9779 + case ULI526X_100MHF:
9780 + case ULI526X_10MFD:
9781 + case ULI526X_100MFD:
9782 +diff --git a/drivers/net/ethernet/smsc/smc911x.c b/drivers/net/ethernet/smsc/smc911x.c
9783 +index 323b3ac16bc0..d0cf971aa4eb 100644
9784 +--- a/drivers/net/ethernet/smsc/smc911x.c
9785 ++++ b/drivers/net/ethernet/smsc/smc911x.c
9786 +@@ -948,7 +948,7 @@ static void smc911x_phy_configure(struct work_struct *work)
9787 + if (lp->ctl_rspeed != 100)
9788 + my_ad_caps &= ~(ADVERTISE_100BASE4|ADVERTISE_100FULL|ADVERTISE_100HALF);
9789 +
9790 +- if (!lp->ctl_rfduplx)
9791 ++ if (!lp->ctl_rfduplx)
9792 + my_ad_caps &= ~(ADVERTISE_100FULL|ADVERTISE_10FULL);
9793 +
9794 + /* Update our Auto-Neg Advertisement Register */
9795 +diff --git a/drivers/net/gtp.c b/drivers/net/gtp.c
9796 +index 5077c69eb652..a9e8a7356c41 100644
9797 +--- a/drivers/net/gtp.c
9798 ++++ b/drivers/net/gtp.c
9799 +@@ -784,11 +784,13 @@ static int gtp_hashtable_new(struct gtp_dev *gtp, int hsize)
9800 + {
9801 + int i;
9802 +
9803 +- gtp->addr_hash = kmalloc(sizeof(struct hlist_head) * hsize, GFP_KERNEL);
9804 ++ gtp->addr_hash = kmalloc(sizeof(struct hlist_head) * hsize,
9805 ++ GFP_KERNEL | __GFP_NOWARN);
9806 + if (gtp->addr_hash == NULL)
9807 + return -ENOMEM;
9808 +
9809 +- gtp->tid_hash = kmalloc(sizeof(struct hlist_head) * hsize, GFP_KERNEL);
9810 ++ gtp->tid_hash = kmalloc(sizeof(struct hlist_head) * hsize,
9811 ++ GFP_KERNEL | __GFP_NOWARN);
9812 + if (gtp->tid_hash == NULL)
9813 + goto err1;
9814 +
9815 +diff --git a/drivers/net/ppp/ppp_async.c b/drivers/net/ppp/ppp_async.c
9816 +index 9c889e0303dd..cef40de1bd05 100644
9817 +--- a/drivers/net/ppp/ppp_async.c
9818 ++++ b/drivers/net/ppp/ppp_async.c
9819 +@@ -878,15 +878,15 @@ ppp_async_input(struct asyncppp *ap, const unsigned char *buf,
9820 + skb = dev_alloc_skb(ap->mru + PPP_HDRLEN + 2);
9821 + if (!skb)
9822 + goto nomem;
9823 +- ap->rpkt = skb;
9824 +- }
9825 +- if (skb->len == 0) {
9826 +- /* Try to get the payload 4-byte aligned.
9827 +- * This should match the
9828 +- * PPP_ALLSTATIONS/PPP_UI/compressed tests in
9829 +- * process_input_packet, but we do not have
9830 +- * enough chars here to test buf[1] and buf[2].
9831 +- */
9832 ++ ap->rpkt = skb;
9833 ++ }
9834 ++ if (skb->len == 0) {
9835 ++ /* Try to get the payload 4-byte aligned.
9836 ++ * This should match the
9837 ++ * PPP_ALLSTATIONS/PPP_UI/compressed tests in
9838 ++ * process_input_packet, but we do not have
9839 ++ * enough chars here to test buf[1] and buf[2].
9840 ++ */
9841 + if (buf[0] != PPP_ALLSTATIONS)
9842 + skb_reserve(skb, 2 + (buf[0] & 1));
9843 + }
9844 +diff --git a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/usb.c b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/usb.c
9845 +index 05df9d8f76e9..31727f34381f 100644
9846 +--- a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/usb.c
9847 ++++ b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/usb.c
9848 +@@ -438,6 +438,7 @@ fail:
9849 + usb_free_urb(req->urb);
9850 + list_del(q->next);
9851 + }
9852 ++ kfree(reqs);
9853 + return NULL;
9854 +
9855 + }
9856 +diff --git a/drivers/net/wireless/marvell/libertas/cfg.c b/drivers/net/wireless/marvell/libertas/cfg.c
9857 +index 3eab802c7d3f..ece6d72cf90c 100644
9858 +--- a/drivers/net/wireless/marvell/libertas/cfg.c
9859 ++++ b/drivers/net/wireless/marvell/libertas/cfg.c
9860 +@@ -1859,6 +1859,8 @@ static int lbs_ibss_join_existing(struct lbs_private *priv,
9861 + rates_max = rates_eid[1];
9862 + if (rates_max > MAX_RATES) {
9863 + lbs_deb_join("invalid rates");
9864 ++ rcu_read_unlock();
9865 ++ ret = -EINVAL;
9866 + goto out;
9867 + }
9868 + rates = cmd.bss.rates;
9869 +diff --git a/drivers/net/wireless/marvell/mwifiex/scan.c b/drivers/net/wireless/marvell/mwifiex/scan.c
9870 +index 828c6f5eb83c..5fde2e2f1fea 100644
9871 +--- a/drivers/net/wireless/marvell/mwifiex/scan.c
9872 ++++ b/drivers/net/wireless/marvell/mwifiex/scan.c
9873 +@@ -2878,6 +2878,13 @@ mwifiex_cmd_append_vsie_tlv(struct mwifiex_private *priv,
9874 + vs_param_set->header.len =
9875 + cpu_to_le16((((u16) priv->vs_ie[id].ie[1])
9876 + & 0x00FF) + 2);
9877 ++ if (le16_to_cpu(vs_param_set->header.len) >
9878 ++ MWIFIEX_MAX_VSIE_LEN) {
9879 ++ mwifiex_dbg(priv->adapter, ERROR,
9880 ++ "Invalid param length!\n");
9881 ++ break;
9882 ++ }
9883 ++
9884 + memcpy(vs_param_set->ie, priv->vs_ie[id].ie,
9885 + le16_to_cpu(vs_param_set->header.len));
9886 + *buffer += le16_to_cpu(vs_param_set->header.len) +
9887 +diff --git a/drivers/net/wireless/marvell/mwifiex/sta_ioctl.c b/drivers/net/wireless/marvell/mwifiex/sta_ioctl.c
9888 +index be3be7a63cf0..f2d10ba19920 100644
9889 +--- a/drivers/net/wireless/marvell/mwifiex/sta_ioctl.c
9890 ++++ b/drivers/net/wireless/marvell/mwifiex/sta_ioctl.c
9891 +@@ -274,6 +274,7 @@ static int mwifiex_process_country_ie(struct mwifiex_private *priv,
9892 +
9893 + if (country_ie_len >
9894 + (IEEE80211_COUNTRY_STRING_LEN + MWIFIEX_MAX_TRIPLET_802_11D)) {
9895 ++ rcu_read_unlock();
9896 + mwifiex_dbg(priv->adapter, ERROR,
9897 + "11D: country_ie_len overflow!, deauth AP\n");
9898 + return -EINVAL;
9899 +diff --git a/drivers/net/wireless/marvell/mwifiex/wmm.c b/drivers/net/wireless/marvell/mwifiex/wmm.c
9900 +index 9843560e784f..c93fcafbcc7a 100644
9901 +--- a/drivers/net/wireless/marvell/mwifiex/wmm.c
9902 ++++ b/drivers/net/wireless/marvell/mwifiex/wmm.c
9903 +@@ -980,6 +980,10 @@ int mwifiex_ret_wmm_get_status(struct mwifiex_private *priv,
9904 + "WMM Parameter Set Count: %d\n",
9905 + wmm_param_ie->qos_info_bitmap & mask);
9906 +
9907 ++ if (wmm_param_ie->vend_hdr.len + 2 >
9908 ++ sizeof(struct ieee_types_wmm_parameter))
9909 ++ break;
9910 ++
9911 + memcpy((u8 *) &priv->curr_bss_params.bss_descriptor.
9912 + wmm_ie, wmm_param_ie,
9913 + wmm_param_ie->vend_hdr.len + 2);
9914 +diff --git a/drivers/nfc/pn544/pn544.c b/drivers/nfc/pn544/pn544.c
9915 +index 12e819ddf17a..3afc53ff7369 100644
9916 +--- a/drivers/nfc/pn544/pn544.c
9917 ++++ b/drivers/nfc/pn544/pn544.c
9918 +@@ -704,7 +704,7 @@ static int pn544_hci_check_presence(struct nfc_hci_dev *hdev,
9919 + target->nfcid1_len != 10)
9920 + return -EOPNOTSUPP;
9921 +
9922 +- return nfc_hci_send_cmd(hdev, NFC_HCI_RF_READER_A_GATE,
9923 ++ return nfc_hci_send_cmd(hdev, NFC_HCI_RF_READER_A_GATE,
9924 + PN544_RF_READER_CMD_ACTIVATE_NEXT,
9925 + target->nfcid1, target->nfcid1_len, NULL);
9926 + } else if (target->supported_protocols & (NFC_PROTO_JEWEL_MASK |
9927 +diff --git a/drivers/of/Kconfig b/drivers/of/Kconfig
9928 +index ba7b034b2b91..6b8646db110c 100644
9929 +--- a/drivers/of/Kconfig
9930 ++++ b/drivers/of/Kconfig
9931 +@@ -112,4 +112,8 @@ config OF_OVERLAY
9932 + config OF_NUMA
9933 + bool
9934 +
9935 ++config OF_DMA_DEFAULT_COHERENT
9936 ++ # arches should select this if DMA is coherent by default for OF devices
9937 ++ bool
9938 ++
9939 + endif # OF
9940 +diff --git a/drivers/of/address.c b/drivers/of/address.c
9941 +index 72914cdfce2a..37619bb2c97a 100644
9942 +--- a/drivers/of/address.c
9943 ++++ b/drivers/of/address.c
9944 +@@ -896,12 +896,16 @@ EXPORT_SYMBOL_GPL(of_dma_get_range);
9945 + * @np: device node
9946 + *
9947 + * It returns true if "dma-coherent" property was found
9948 +- * for this device in DT.
9949 ++ * for this device in the DT, or if DMA is coherent by
9950 ++ * default for OF devices on the current platform.
9951 + */
9952 + bool of_dma_is_coherent(struct device_node *np)
9953 + {
9954 + struct device_node *node = of_node_get(np);
9955 +
9956 ++ if (IS_ENABLED(CONFIG_OF_DMA_DEFAULT_COHERENT))
9957 ++ return true;
9958 ++
9959 + while (node) {
9960 + if (of_property_read_bool(node, "dma-coherent")) {
9961 + of_node_put(node);
9962 +diff --git a/drivers/pci/host/pci-keystone-dw.c b/drivers/pci/host/pci-keystone-dw.c
9963 +index 9397c4667106..f011a8780ff5 100644
9964 +--- a/drivers/pci/host/pci-keystone-dw.c
9965 ++++ b/drivers/pci/host/pci-keystone-dw.c
9966 +@@ -502,7 +502,7 @@ void ks_dw_pcie_initiate_link_train(struct keystone_pcie *ks_pcie)
9967 + /* Disable Link training */
9968 + val = ks_dw_app_readl(ks_pcie, CMD_STATUS);
9969 + val &= ~LTSSM_EN_VAL;
9970 +- ks_dw_app_writel(ks_pcie, CMD_STATUS, LTSSM_EN_VAL | val);
9971 ++ ks_dw_app_writel(ks_pcie, CMD_STATUS, val);
9972 +
9973 + /* Initiate Link Training */
9974 + val = ks_dw_app_readl(ks_pcie, CMD_STATUS);
9975 +diff --git a/drivers/pci/setup-bus.c b/drivers/pci/setup-bus.c
9976 +index f30ca75b5b6c..e631636a0aa5 100644
9977 +--- a/drivers/pci/setup-bus.c
9978 ++++ b/drivers/pci/setup-bus.c
9979 +@@ -1833,12 +1833,18 @@ again:
9980 + /* restore size and flags */
9981 + list_for_each_entry(fail_res, &fail_head, list) {
9982 + struct resource *res = fail_res->res;
9983 ++ int idx;
9984 +
9985 + res->start = fail_res->start;
9986 + res->end = fail_res->end;
9987 + res->flags = fail_res->flags;
9988 +- if (fail_res->dev->subordinate)
9989 +- res->flags = 0;
9990 ++
9991 ++ if (pci_is_bridge(fail_res->dev)) {
9992 ++ idx = res - &fail_res->dev->resource[0];
9993 ++ if (idx >= PCI_BRIDGE_RESOURCES &&
9994 ++ idx <= PCI_BRIDGE_RESOURCE_END)
9995 ++ res->flags = 0;
9996 ++ }
9997 + }
9998 + free_list(&fail_head);
9999 +
10000 +@@ -1904,12 +1910,18 @@ again:
10001 + /* restore size and flags */
10002 + list_for_each_entry(fail_res, &fail_head, list) {
10003 + struct resource *res = fail_res->res;
10004 ++ int idx;
10005 +
10006 + res->start = fail_res->start;
10007 + res->end = fail_res->end;
10008 + res->flags = fail_res->flags;
10009 +- if (fail_res->dev->subordinate)
10010 +- res->flags = 0;
10011 ++
10012 ++ if (pci_is_bridge(fail_res->dev)) {
10013 ++ idx = res - &fail_res->dev->resource[0];
10014 ++ if (idx >= PCI_BRIDGE_RESOURCES &&
10015 ++ idx <= PCI_BRIDGE_RESOURCE_END)
10016 ++ res->flags = 0;
10017 ++ }
10018 + }
10019 + free_list(&fail_head);
10020 +
10021 +diff --git a/drivers/pinctrl/sh-pfc/pfc-r8a7778.c b/drivers/pinctrl/sh-pfc/pfc-r8a7778.c
10022 +index 18ef7042b3d1..771689a41dbf 100644
10023 +--- a/drivers/pinctrl/sh-pfc/pfc-r8a7778.c
10024 ++++ b/drivers/pinctrl/sh-pfc/pfc-r8a7778.c
10025 +@@ -2324,7 +2324,7 @@ static const struct pinmux_cfg_reg pinmux_config_regs[] = {
10026 + FN_ATAG0_A, 0, FN_REMOCON_B, 0,
10027 + /* IP0_11_8 [4] */
10028 + FN_SD1_DAT2_A, FN_MMC_D2, 0, FN_BS,
10029 +- FN_ATADIR0_A, 0, FN_SDSELF_B, 0,
10030 ++ FN_ATADIR0_A, 0, FN_SDSELF_A, 0,
10031 + FN_PWM4_B, 0, 0, 0,
10032 + 0, 0, 0, 0,
10033 + /* IP0_7_5 [3] */
10034 +@@ -2366,7 +2366,7 @@ static const struct pinmux_cfg_reg pinmux_config_regs[] = {
10035 + FN_TS_SDAT0_A, 0, 0, 0,
10036 + 0, 0, 0, 0,
10037 + /* IP1_10_8 [3] */
10038 +- FN_SD1_CLK_B, FN_MMC_D6, 0, FN_A24,
10039 ++ FN_SD1_CD_A, FN_MMC_D6, 0, FN_A24,
10040 + FN_DREQ1_A, 0, FN_HRX0_B, FN_TS_SPSYNC0_A,
10041 + /* IP1_7_5 [3] */
10042 + FN_A23, FN_HTX0_B, FN_TX2_B, FN_DACK2_A,
10043 +diff --git a/drivers/power/supply/ltc2941-battery-gauge.c b/drivers/power/supply/ltc2941-battery-gauge.c
10044 +index 4adf2ba021ce..043de9d039d5 100644
10045 +--- a/drivers/power/supply/ltc2941-battery-gauge.c
10046 ++++ b/drivers/power/supply/ltc2941-battery-gauge.c
10047 +@@ -364,7 +364,7 @@ static int ltc294x_i2c_remove(struct i2c_client *client)
10048 + {
10049 + struct ltc294x_info *info = i2c_get_clientdata(client);
10050 +
10051 +- cancel_delayed_work(&info->work);
10052 ++ cancel_delayed_work_sync(&info->work);
10053 + power_supply_unregister(info->supply);
10054 + return 0;
10055 + }
10056 +diff --git a/drivers/rtc/rtc-cmos.c b/drivers/rtc/rtc-cmos.c
10057 +index c554e529fc4e..b962dbe51750 100644
10058 +--- a/drivers/rtc/rtc-cmos.c
10059 ++++ b/drivers/rtc/rtc-cmos.c
10060 +@@ -730,7 +730,7 @@ cmos_do_probe(struct device *dev, struct resource *ports, int rtc_irq)
10061 + rtc_cmos_int_handler = cmos_interrupt;
10062 +
10063 + retval = request_irq(rtc_irq, rtc_cmos_int_handler,
10064 +- IRQF_SHARED, dev_name(&cmos_rtc.rtc->dev),
10065 ++ 0, dev_name(&cmos_rtc.rtc->dev),
10066 + cmos_rtc.rtc);
10067 + if (retval < 0) {
10068 + dev_dbg(dev, "IRQ %d is already in use\n", rtc_irq);
10069 +diff --git a/drivers/rtc/rtc-hym8563.c b/drivers/rtc/rtc-hym8563.c
10070 +index e5ad527cb75e..a8c2d38b2411 100644
10071 +--- a/drivers/rtc/rtc-hym8563.c
10072 ++++ b/drivers/rtc/rtc-hym8563.c
10073 +@@ -105,7 +105,7 @@ static int hym8563_rtc_read_time(struct device *dev, struct rtc_time *tm)
10074 +
10075 + if (!hym8563->valid) {
10076 + dev_warn(&client->dev, "no valid clock/calendar values available\n");
10077 +- return -EPERM;
10078 ++ return -EINVAL;
10079 + }
10080 +
10081 + ret = i2c_smbus_read_i2c_block_data(client, HYM8563_SEC, 7, buf);
10082 +diff --git a/drivers/scsi/csiostor/csio_scsi.c b/drivers/scsi/csiostor/csio_scsi.c
10083 +index 894d97e4ace5..5db57671fa28 100644
10084 +--- a/drivers/scsi/csiostor/csio_scsi.c
10085 ++++ b/drivers/scsi/csiostor/csio_scsi.c
10086 +@@ -1383,7 +1383,7 @@ csio_device_reset(struct device *dev,
10087 + return -EINVAL;
10088 +
10089 + /* Delete NPIV lnodes */
10090 +- csio_lnodes_exit(hw, 1);
10091 ++ csio_lnodes_exit(hw, 1);
10092 +
10093 + /* Block upper IOs */
10094 + csio_lnodes_block_request(hw);
10095 +diff --git a/drivers/scsi/megaraid/megaraid_sas_base.c b/drivers/scsi/megaraid/megaraid_sas_base.c
10096 +index 10ae624dd266..9fa6a560b162 100644
10097 +--- a/drivers/scsi/megaraid/megaraid_sas_base.c
10098 ++++ b/drivers/scsi/megaraid/megaraid_sas_base.c
10099 +@@ -3978,7 +3978,8 @@ dcmd_timeout_ocr_possible(struct megasas_instance *instance) {
10100 + if (!instance->ctrl_context)
10101 + return KILL_ADAPTER;
10102 + else if (instance->unload ||
10103 +- test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags))
10104 ++ test_bit(MEGASAS_FUSION_OCR_NOT_POSSIBLE,
10105 ++ &instance->reset_flags))
10106 + return IGNORE_TIMEOUT;
10107 + else
10108 + return INITIATE_OCR;
10109 +diff --git a/drivers/scsi/megaraid/megaraid_sas_fusion.c b/drivers/scsi/megaraid/megaraid_sas_fusion.c
10110 +index fe1a20973e47..874e5a7f7998 100644
10111 +--- a/drivers/scsi/megaraid/megaraid_sas_fusion.c
10112 ++++ b/drivers/scsi/megaraid/megaraid_sas_fusion.c
10113 +@@ -3438,6 +3438,7 @@ int megasas_reset_fusion(struct Scsi_Host *shost, int reason)
10114 + if (instance->requestorId && !instance->skip_heartbeat_timer_del)
10115 + del_timer_sync(&instance->sriov_heartbeat_timer);
10116 + set_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
10117 ++ set_bit(MEGASAS_FUSION_OCR_NOT_POSSIBLE, &instance->reset_flags);
10118 + atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_POLLING);
10119 + instance->instancet->disable_intr(instance);
10120 + msleep(1000);
10121 +@@ -3594,7 +3595,7 @@ fail_kill_adapter:
10122 + atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
10123 + }
10124 + out:
10125 +- clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
10126 ++ clear_bit(MEGASAS_FUSION_OCR_NOT_POSSIBLE, &instance->reset_flags);
10127 + mutex_unlock(&instance->reset_mutex);
10128 + return retval;
10129 + }
10130 +diff --git a/drivers/scsi/megaraid/megaraid_sas_fusion.h b/drivers/scsi/megaraid/megaraid_sas_fusion.h
10131 +index e3bee04c1eb1..034653d93365 100644
10132 +--- a/drivers/scsi/megaraid/megaraid_sas_fusion.h
10133 ++++ b/drivers/scsi/megaraid/megaraid_sas_fusion.h
10134 +@@ -93,6 +93,7 @@ enum MR_RAID_FLAGS_IO_SUB_TYPE {
10135 +
10136 + #define MEGASAS_FP_CMD_LEN 16
10137 + #define MEGASAS_FUSION_IN_RESET 0
10138 ++#define MEGASAS_FUSION_OCR_NOT_POSSIBLE 1
10139 + #define THRESHOLD_REPLY_COUNT 50
10140 + #define JBOD_MAPS_COUNT 2
10141 +
10142 +diff --git a/drivers/scsi/qla2xxx/qla_mbx.c b/drivers/scsi/qla2xxx/qla_mbx.c
10143 +index bf29ad454118..9bbea4223917 100644
10144 +--- a/drivers/scsi/qla2xxx/qla_mbx.c
10145 ++++ b/drivers/scsi/qla2xxx/qla_mbx.c
10146 +@@ -5723,9 +5723,8 @@ qla2x00_dump_mctp_data(scsi_qla_host_t *vha, dma_addr_t req_dma, uint32_t addr,
10147 + mcp->mb[7] = LSW(MSD(req_dma));
10148 + mcp->mb[8] = MSW(addr);
10149 + /* Setting RAM ID to valid */
10150 +- mcp->mb[10] |= BIT_7;
10151 + /* For MCTP RAM ID is 0x40 */
10152 +- mcp->mb[10] |= 0x40;
10153 ++ mcp->mb[10] = BIT_7 | 0x40;
10154 +
10155 + mcp->out_mb |= MBX_10|MBX_8|MBX_7|MBX_6|MBX_5|MBX_4|MBX_3|MBX_2|MBX_1|
10156 + MBX_0;
10157 +diff --git a/drivers/scsi/qla2xxx/qla_nx.c b/drivers/scsi/qla2xxx/qla_nx.c
10158 +index 54380b434b30..104e13ae3428 100644
10159 +--- a/drivers/scsi/qla2xxx/qla_nx.c
10160 ++++ b/drivers/scsi/qla2xxx/qla_nx.c
10161 +@@ -1600,8 +1600,7 @@ qla82xx_get_bootld_offset(struct qla_hw_data *ha)
10162 + return (u8 *)&ha->hablob->fw->data[offset];
10163 + }
10164 +
10165 +-static __le32
10166 +-qla82xx_get_fw_size(struct qla_hw_data *ha)
10167 ++static u32 qla82xx_get_fw_size(struct qla_hw_data *ha)
10168 + {
10169 + struct qla82xx_uri_data_desc *uri_desc = NULL;
10170 +
10171 +@@ -1612,7 +1611,7 @@ qla82xx_get_fw_size(struct qla_hw_data *ha)
10172 + return cpu_to_le32(uri_desc->size);
10173 + }
10174 +
10175 +- return cpu_to_le32(*(u32 *)&ha->hablob->fw->data[FW_SIZE_OFFSET]);
10176 ++ return get_unaligned_le32(&ha->hablob->fw->data[FW_SIZE_OFFSET]);
10177 + }
10178 +
10179 + static u8 *
10180 +@@ -1803,7 +1802,7 @@ qla82xx_fw_load_from_blob(struct qla_hw_data *ha)
10181 + }
10182 +
10183 + flashaddr = FLASH_ADDR_START;
10184 +- size = (__force u32)qla82xx_get_fw_size(ha) / 8;
10185 ++ size = qla82xx_get_fw_size(ha) / 8;
10186 + ptr64 = (u64 *)qla82xx_get_fw_offs(ha);
10187 +
10188 + for (i = 0; i < size; i++) {
10189 +diff --git a/drivers/scsi/qla4xxx/ql4_os.c b/drivers/scsi/qla4xxx/ql4_os.c
10190 +index f714d5f917d1..3fda5836aac6 100644
10191 +--- a/drivers/scsi/qla4xxx/ql4_os.c
10192 ++++ b/drivers/scsi/qla4xxx/ql4_os.c
10193 +@@ -4150,7 +4150,7 @@ static void qla4xxx_mem_free(struct scsi_qla_host *ha)
10194 + dma_free_coherent(&ha->pdev->dev, ha->queues_len, ha->queues,
10195 + ha->queues_dma);
10196 +
10197 +- if (ha->fw_dump)
10198 ++ if (ha->fw_dump)
10199 + vfree(ha->fw_dump);
10200 +
10201 + ha->queues_len = 0;
10202 +diff --git a/drivers/scsi/ufs/ufshcd.c b/drivers/scsi/ufs/ufshcd.c
10203 +index 094e879af121..394df57894e6 100644
10204 +--- a/drivers/scsi/ufs/ufshcd.c
10205 ++++ b/drivers/scsi/ufs/ufshcd.c
10206 +@@ -5347,7 +5347,8 @@ static int ufshcd_probe_hba(struct ufs_hba *hba)
10207 + ufshcd_init_icc_levels(hba);
10208 +
10209 + /* Add required well known logical units to scsi mid layer */
10210 +- if (ufshcd_scsi_add_wlus(hba))
10211 ++ ret = ufshcd_scsi_add_wlus(hba);
10212 ++ if (ret)
10213 + goto out;
10214 +
10215 + scsi_scan_host(hba->host);
10216 +diff --git a/drivers/usb/gadget/function/f_ecm.c b/drivers/usb/gadget/function/f_ecm.c
10217 +index dc99ed94f03d..8e3e44382785 100644
10218 +--- a/drivers/usb/gadget/function/f_ecm.c
10219 ++++ b/drivers/usb/gadget/function/f_ecm.c
10220 +@@ -56,6 +56,7 @@ struct f_ecm {
10221 + struct usb_ep *notify;
10222 + struct usb_request *notify_req;
10223 + u8 notify_state;
10224 ++ atomic_t notify_count;
10225 + bool is_open;
10226 +
10227 + /* FIXME is_open needs some irq-ish locking
10228 +@@ -384,7 +385,7 @@ static void ecm_do_notify(struct f_ecm *ecm)
10229 + int status;
10230 +
10231 + /* notification already in flight? */
10232 +- if (!req)
10233 ++ if (atomic_read(&ecm->notify_count))
10234 + return;
10235 +
10236 + event = req->buf;
10237 +@@ -424,10 +425,10 @@ static void ecm_do_notify(struct f_ecm *ecm)
10238 + event->bmRequestType = 0xA1;
10239 + event->wIndex = cpu_to_le16(ecm->ctrl_id);
10240 +
10241 +- ecm->notify_req = NULL;
10242 ++ atomic_inc(&ecm->notify_count);
10243 + status = usb_ep_queue(ecm->notify, req, GFP_ATOMIC);
10244 + if (status < 0) {
10245 +- ecm->notify_req = req;
10246 ++ atomic_dec(&ecm->notify_count);
10247 + DBG(cdev, "notify --> %d\n", status);
10248 + }
10249 + }
10250 +@@ -452,17 +453,19 @@ static void ecm_notify_complete(struct usb_ep *ep, struct usb_request *req)
10251 + switch (req->status) {
10252 + case 0:
10253 + /* no fault */
10254 ++ atomic_dec(&ecm->notify_count);
10255 + break;
10256 + case -ECONNRESET:
10257 + case -ESHUTDOWN:
10258 ++ atomic_set(&ecm->notify_count, 0);
10259 + ecm->notify_state = ECM_NOTIFY_NONE;
10260 + break;
10261 + default:
10262 + DBG(cdev, "event %02x --> %d\n",
10263 + event->bNotificationType, req->status);
10264 ++ atomic_dec(&ecm->notify_count);
10265 + break;
10266 + }
10267 +- ecm->notify_req = req;
10268 + ecm_do_notify(ecm);
10269 + }
10270 +
10271 +@@ -909,6 +912,11 @@ static void ecm_unbind(struct usb_configuration *c, struct usb_function *f)
10272 +
10273 + usb_free_all_descriptors(f);
10274 +
10275 ++ if (atomic_read(&ecm->notify_count)) {
10276 ++ usb_ep_dequeue(ecm->notify, ecm->notify_req);
10277 ++ atomic_set(&ecm->notify_count, 0);
10278 ++ }
10279 ++
10280 + kfree(ecm->notify_req->buf);
10281 + usb_ep_free_request(ecm->notify, ecm->notify_req);
10282 + }
10283 +diff --git a/drivers/usb/gadget/function/f_ncm.c b/drivers/usb/gadget/function/f_ncm.c
10284 +index 639603722709..6399923239e7 100644
10285 +--- a/drivers/usb/gadget/function/f_ncm.c
10286 ++++ b/drivers/usb/gadget/function/f_ncm.c
10287 +@@ -57,6 +57,7 @@ struct f_ncm {
10288 + struct usb_ep *notify;
10289 + struct usb_request *notify_req;
10290 + u8 notify_state;
10291 ++ atomic_t notify_count;
10292 + bool is_open;
10293 +
10294 + const struct ndp_parser_opts *parser_opts;
10295 +@@ -552,7 +553,7 @@ static void ncm_do_notify(struct f_ncm *ncm)
10296 + int status;
10297 +
10298 + /* notification already in flight? */
10299 +- if (!req)
10300 ++ if (atomic_read(&ncm->notify_count))
10301 + return;
10302 +
10303 + event = req->buf;
10304 +@@ -592,7 +593,8 @@ static void ncm_do_notify(struct f_ncm *ncm)
10305 + event->bmRequestType = 0xA1;
10306 + event->wIndex = cpu_to_le16(ncm->ctrl_id);
10307 +
10308 +- ncm->notify_req = NULL;
10309 ++ atomic_inc(&ncm->notify_count);
10310 ++
10311 + /*
10312 + * In double buffering if there is a space in FIFO,
10313 + * completion callback can be called right after the call,
10314 +@@ -602,7 +604,7 @@ static void ncm_do_notify(struct f_ncm *ncm)
10315 + status = usb_ep_queue(ncm->notify, req, GFP_ATOMIC);
10316 + spin_lock(&ncm->lock);
10317 + if (status < 0) {
10318 +- ncm->notify_req = req;
10319 ++ atomic_dec(&ncm->notify_count);
10320 + DBG(cdev, "notify --> %d\n", status);
10321 + }
10322 + }
10323 +@@ -637,17 +639,19 @@ static void ncm_notify_complete(struct usb_ep *ep, struct usb_request *req)
10324 + case 0:
10325 + VDBG(cdev, "Notification %02x sent\n",
10326 + event->bNotificationType);
10327 ++ atomic_dec(&ncm->notify_count);
10328 + break;
10329 + case -ECONNRESET:
10330 + case -ESHUTDOWN:
10331 ++ atomic_set(&ncm->notify_count, 0);
10332 + ncm->notify_state = NCM_NOTIFY_NONE;
10333 + break;
10334 + default:
10335 + DBG(cdev, "event %02x --> %d\n",
10336 + event->bNotificationType, req->status);
10337 ++ atomic_dec(&ncm->notify_count);
10338 + break;
10339 + }
10340 +- ncm->notify_req = req;
10341 + ncm_do_notify(ncm);
10342 + spin_unlock(&ncm->lock);
10343 + }
10344 +@@ -1639,6 +1643,11 @@ static void ncm_unbind(struct usb_configuration *c, struct usb_function *f)
10345 + ncm_string_defs[0].id = 0;
10346 + usb_free_all_descriptors(f);
10347 +
10348 ++ if (atomic_read(&ncm->notify_count)) {
10349 ++ usb_ep_dequeue(ncm->notify, ncm->notify_req);
10350 ++ atomic_set(&ncm->notify_count, 0);
10351 ++ }
10352 ++
10353 + kfree(ncm->notify_req->buf);
10354 + usb_ep_free_request(ncm->notify, ncm->notify_req);
10355 + }
10356 +diff --git a/drivers/usb/gadget/legacy/cdc2.c b/drivers/usb/gadget/legacy/cdc2.c
10357 +index 51c08682de84..5ee25beb52f0 100644
10358 +--- a/drivers/usb/gadget/legacy/cdc2.c
10359 ++++ b/drivers/usb/gadget/legacy/cdc2.c
10360 +@@ -229,7 +229,7 @@ static struct usb_composite_driver cdc_driver = {
10361 + .name = "g_cdc",
10362 + .dev = &device_desc,
10363 + .strings = dev_strings,
10364 +- .max_speed = USB_SPEED_HIGH,
10365 ++ .max_speed = USB_SPEED_SUPER,
10366 + .bind = cdc_bind,
10367 + .unbind = cdc_unbind,
10368 + };
10369 +diff --git a/drivers/usb/gadget/legacy/g_ffs.c b/drivers/usb/gadget/legacy/g_ffs.c
10370 +index 6da7316f8e87..54ee4e31645b 100644
10371 +--- a/drivers/usb/gadget/legacy/g_ffs.c
10372 ++++ b/drivers/usb/gadget/legacy/g_ffs.c
10373 +@@ -153,7 +153,7 @@ static struct usb_composite_driver gfs_driver = {
10374 + .name = DRIVER_NAME,
10375 + .dev = &gfs_dev_desc,
10376 + .strings = gfs_dev_strings,
10377 +- .max_speed = USB_SPEED_HIGH,
10378 ++ .max_speed = USB_SPEED_SUPER,
10379 + .bind = gfs_bind,
10380 + .unbind = gfs_unbind,
10381 + };
10382 +diff --git a/drivers/usb/gadget/legacy/multi.c b/drivers/usb/gadget/legacy/multi.c
10383 +index a70a406580ea..3b7fc5c7e9c3 100644
10384 +--- a/drivers/usb/gadget/legacy/multi.c
10385 ++++ b/drivers/usb/gadget/legacy/multi.c
10386 +@@ -486,7 +486,7 @@ static struct usb_composite_driver multi_driver = {
10387 + .name = "g_multi",
10388 + .dev = &device_desc,
10389 + .strings = dev_strings,
10390 +- .max_speed = USB_SPEED_HIGH,
10391 ++ .max_speed = USB_SPEED_SUPER,
10392 + .bind = multi_bind,
10393 + .unbind = multi_unbind,
10394 + .needs_serial = 1,
10395 +diff --git a/drivers/usb/gadget/legacy/ncm.c b/drivers/usb/gadget/legacy/ncm.c
10396 +index 0aba68253e3d..2fb4a847dd52 100644
10397 +--- a/drivers/usb/gadget/legacy/ncm.c
10398 ++++ b/drivers/usb/gadget/legacy/ncm.c
10399 +@@ -203,7 +203,7 @@ static struct usb_composite_driver ncm_driver = {
10400 + .name = "g_ncm",
10401 + .dev = &device_desc,
10402 + .strings = dev_strings,
10403 +- .max_speed = USB_SPEED_HIGH,
10404 ++ .max_speed = USB_SPEED_SUPER,
10405 + .bind = gncm_bind,
10406 + .unbind = gncm_unbind,
10407 + };
10408 +diff --git a/fs/btrfs/ctree.c b/fs/btrfs/ctree.c
10409 +index 305deb6e59c3..b5ebb43b1824 100644
10410 +--- a/fs/btrfs/ctree.c
10411 ++++ b/fs/btrfs/ctree.c
10412 +@@ -331,26 +331,6 @@ struct tree_mod_elem {
10413 + struct tree_mod_root old_root;
10414 + };
10415 +
10416 +-static inline void tree_mod_log_read_lock(struct btrfs_fs_info *fs_info)
10417 +-{
10418 +- read_lock(&fs_info->tree_mod_log_lock);
10419 +-}
10420 +-
10421 +-static inline void tree_mod_log_read_unlock(struct btrfs_fs_info *fs_info)
10422 +-{
10423 +- read_unlock(&fs_info->tree_mod_log_lock);
10424 +-}
10425 +-
10426 +-static inline void tree_mod_log_write_lock(struct btrfs_fs_info *fs_info)
10427 +-{
10428 +- write_lock(&fs_info->tree_mod_log_lock);
10429 +-}
10430 +-
10431 +-static inline void tree_mod_log_write_unlock(struct btrfs_fs_info *fs_info)
10432 +-{
10433 +- write_unlock(&fs_info->tree_mod_log_lock);
10434 +-}
10435 +-
10436 + /*
10437 + * Pull a new tree mod seq number for our operation.
10438 + */
10439 +@@ -370,14 +350,12 @@ static inline u64 btrfs_inc_tree_mod_seq(struct btrfs_fs_info *fs_info)
10440 + u64 btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info,
10441 + struct seq_list *elem)
10442 + {
10443 +- tree_mod_log_write_lock(fs_info);
10444 +- spin_lock(&fs_info->tree_mod_seq_lock);
10445 ++ write_lock(&fs_info->tree_mod_log_lock);
10446 + if (!elem->seq) {
10447 + elem->seq = btrfs_inc_tree_mod_seq(fs_info);
10448 + list_add_tail(&elem->list, &fs_info->tree_mod_seq_list);
10449 + }
10450 +- spin_unlock(&fs_info->tree_mod_seq_lock);
10451 +- tree_mod_log_write_unlock(fs_info);
10452 ++ write_unlock(&fs_info->tree_mod_log_lock);
10453 +
10454 + return elem->seq;
10455 + }
10456 +@@ -396,7 +374,7 @@ void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info,
10457 + if (!seq_putting)
10458 + return;
10459 +
10460 +- spin_lock(&fs_info->tree_mod_seq_lock);
10461 ++ write_lock(&fs_info->tree_mod_log_lock);
10462 + list_del(&elem->list);
10463 + elem->seq = 0;
10464 +
10465 +@@ -407,19 +385,17 @@ void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info,
10466 + * blocker with lower sequence number exists, we
10467 + * cannot remove anything from the log
10468 + */
10469 +- spin_unlock(&fs_info->tree_mod_seq_lock);
10470 ++ write_unlock(&fs_info->tree_mod_log_lock);
10471 + return;
10472 + }
10473 + min_seq = cur_elem->seq;
10474 + }
10475 + }
10476 +- spin_unlock(&fs_info->tree_mod_seq_lock);
10477 +
10478 + /*
10479 + * anything that's lower than the lowest existing (read: blocked)
10480 + * sequence number can be removed from the tree.
10481 + */
10482 +- tree_mod_log_write_lock(fs_info);
10483 + tm_root = &fs_info->tree_mod_log;
10484 + for (node = rb_first(tm_root); node; node = next) {
10485 + next = rb_next(node);
10486 +@@ -429,7 +405,7 @@ void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info,
10487 + rb_erase(node, tm_root);
10488 + kfree(tm);
10489 + }
10490 +- tree_mod_log_write_unlock(fs_info);
10491 ++ write_unlock(&fs_info->tree_mod_log_lock);
10492 + }
10493 +
10494 + /*
10495 +@@ -440,7 +416,7 @@ void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info,
10496 + * for root replace operations, or the logical address of the affected
10497 + * block for all other operations.
10498 + *
10499 +- * Note: must be called with write lock (tree_mod_log_write_lock).
10500 ++ * Note: must be called with write lock for fs_info::tree_mod_log_lock.
10501 + */
10502 + static noinline int
10503 + __tree_mod_log_insert(struct btrfs_fs_info *fs_info, struct tree_mod_elem *tm)
10504 +@@ -480,7 +456,7 @@ __tree_mod_log_insert(struct btrfs_fs_info *fs_info, struct tree_mod_elem *tm)
10505 + * Determines if logging can be omitted. Returns 1 if it can. Otherwise, it
10506 + * returns zero with the tree_mod_log_lock acquired. The caller must hold
10507 + * this until all tree mod log insertions are recorded in the rb tree and then
10508 +- * call tree_mod_log_write_unlock() to release.
10509 ++ * write unlock fs_info::tree_mod_log_lock.
10510 + */
10511 + static inline int tree_mod_dont_log(struct btrfs_fs_info *fs_info,
10512 + struct extent_buffer *eb) {
10513 +@@ -490,9 +466,9 @@ static inline int tree_mod_dont_log(struct btrfs_fs_info *fs_info,
10514 + if (eb && btrfs_header_level(eb) == 0)
10515 + return 1;
10516 +
10517 +- tree_mod_log_write_lock(fs_info);
10518 ++ write_lock(&fs_info->tree_mod_log_lock);
10519 + if (list_empty(&(fs_info)->tree_mod_seq_list)) {
10520 +- tree_mod_log_write_unlock(fs_info);
10521 ++ write_unlock(&fs_info->tree_mod_log_lock);
10522 + return 1;
10523 + }
10524 +
10525 +@@ -556,7 +532,7 @@ tree_mod_log_insert_key(struct btrfs_fs_info *fs_info,
10526 + }
10527 +
10528 + ret = __tree_mod_log_insert(fs_info, tm);
10529 +- tree_mod_log_write_unlock(fs_info);
10530 ++ write_unlock(&eb->fs_info->tree_mod_log_lock);
10531 + if (ret)
10532 + kfree(tm);
10533 +
10534 +@@ -620,7 +596,7 @@ tree_mod_log_insert_move(struct btrfs_fs_info *fs_info,
10535 + ret = __tree_mod_log_insert(fs_info, tm);
10536 + if (ret)
10537 + goto free_tms;
10538 +- tree_mod_log_write_unlock(fs_info);
10539 ++ write_unlock(&eb->fs_info->tree_mod_log_lock);
10540 + kfree(tm_list);
10541 +
10542 + return 0;
10543 +@@ -631,7 +607,7 @@ free_tms:
10544 + kfree(tm_list[i]);
10545 + }
10546 + if (locked)
10547 +- tree_mod_log_write_unlock(fs_info);
10548 ++ write_unlock(&eb->fs_info->tree_mod_log_lock);
10549 + kfree(tm_list);
10550 + kfree(tm);
10551 +
10552 +@@ -712,7 +688,7 @@ tree_mod_log_insert_root(struct btrfs_fs_info *fs_info,
10553 + if (!ret)
10554 + ret = __tree_mod_log_insert(fs_info, tm);
10555 +
10556 +- tree_mod_log_write_unlock(fs_info);
10557 ++ write_unlock(&fs_info->tree_mod_log_lock);
10558 + if (ret)
10559 + goto free_tms;
10560 + kfree(tm_list);
10561 +@@ -739,7 +715,7 @@ __tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq,
10562 + struct tree_mod_elem *cur = NULL;
10563 + struct tree_mod_elem *found = NULL;
10564 +
10565 +- tree_mod_log_read_lock(fs_info);
10566 ++ read_lock(&fs_info->tree_mod_log_lock);
10567 + tm_root = &fs_info->tree_mod_log;
10568 + node = tm_root->rb_node;
10569 + while (node) {
10570 +@@ -767,7 +743,7 @@ __tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq,
10571 + break;
10572 + }
10573 + }
10574 +- tree_mod_log_read_unlock(fs_info);
10575 ++ read_unlock(&fs_info->tree_mod_log_lock);
10576 +
10577 + return found;
10578 + }
10579 +@@ -848,7 +824,7 @@ tree_mod_log_eb_copy(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
10580 + goto free_tms;
10581 + }
10582 +
10583 +- tree_mod_log_write_unlock(fs_info);
10584 ++ write_unlock(&fs_info->tree_mod_log_lock);
10585 + kfree(tm_list);
10586 +
10587 + return 0;
10588 +@@ -860,7 +836,7 @@ free_tms:
10589 + kfree(tm_list[i]);
10590 + }
10591 + if (locked)
10592 +- tree_mod_log_write_unlock(fs_info);
10593 ++ write_unlock(&fs_info->tree_mod_log_lock);
10594 + kfree(tm_list);
10595 +
10596 + return ret;
10597 +@@ -920,7 +896,7 @@ tree_mod_log_free_eb(struct btrfs_fs_info *fs_info, struct extent_buffer *eb)
10598 + goto free_tms;
10599 +
10600 + ret = __tree_mod_log_free_eb(fs_info, tm_list, nritems);
10601 +- tree_mod_log_write_unlock(fs_info);
10602 ++ write_unlock(&eb->fs_info->tree_mod_log_lock);
10603 + if (ret)
10604 + goto free_tms;
10605 + kfree(tm_list);
10606 +@@ -1271,7 +1247,7 @@ __tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
10607 + unsigned long p_size = sizeof(struct btrfs_key_ptr);
10608 +
10609 + n = btrfs_header_nritems(eb);
10610 +- tree_mod_log_read_lock(fs_info);
10611 ++ read_lock(&fs_info->tree_mod_log_lock);
10612 + while (tm && tm->seq >= time_seq) {
10613 + /*
10614 + * all the operations are recorded with the operator used for
10615 +@@ -1326,7 +1302,7 @@ __tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
10616 + if (tm->logical != first_tm->logical)
10617 + break;
10618 + }
10619 +- tree_mod_log_read_unlock(fs_info);
10620 ++ read_unlock(&fs_info->tree_mod_log_lock);
10621 + btrfs_set_header_nritems(eb, n);
10622 + }
10623 +
10624 +diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
10625 +index a423c36bcd72..2bc37d03d407 100644
10626 +--- a/fs/btrfs/ctree.h
10627 ++++ b/fs/btrfs/ctree.h
10628 +@@ -851,14 +851,12 @@ struct btrfs_fs_info {
10629 + struct list_head delayed_iputs;
10630 + struct mutex cleaner_delayed_iput_mutex;
10631 +
10632 +- /* this protects tree_mod_seq_list */
10633 +- spinlock_t tree_mod_seq_lock;
10634 + atomic64_t tree_mod_seq;
10635 +- struct list_head tree_mod_seq_list;
10636 +
10637 +- /* this protects tree_mod_log */
10638 ++ /* this protects tree_mod_log and tree_mod_seq_list */
10639 + rwlock_t tree_mod_log_lock;
10640 + struct rb_root tree_mod_log;
10641 ++ struct list_head tree_mod_seq_list;
10642 +
10643 + atomic_t nr_async_submits;
10644 + atomic_t async_submit_draining;
10645 +diff --git a/fs/btrfs/delayed-ref.c b/fs/btrfs/delayed-ref.c
10646 +index 74c17db75201..c1ca4ce11e69 100644
10647 +--- a/fs/btrfs/delayed-ref.c
10648 ++++ b/fs/btrfs/delayed-ref.c
10649 +@@ -279,7 +279,7 @@ void btrfs_merge_delayed_refs(struct btrfs_trans_handle *trans,
10650 + if (head->is_data)
10651 + return;
10652 +
10653 +- spin_lock(&fs_info->tree_mod_seq_lock);
10654 ++ read_lock(&fs_info->tree_mod_log_lock);
10655 + if (!list_empty(&fs_info->tree_mod_seq_list)) {
10656 + struct seq_list *elem;
10657 +
10658 +@@ -287,7 +287,7 @@ void btrfs_merge_delayed_refs(struct btrfs_trans_handle *trans,
10659 + struct seq_list, list);
10660 + seq = elem->seq;
10661 + }
10662 +- spin_unlock(&fs_info->tree_mod_seq_lock);
10663 ++ read_unlock(&fs_info->tree_mod_log_lock);
10664 +
10665 + ref = list_first_entry(&head->ref_list, struct btrfs_delayed_ref_node,
10666 + list);
10667 +@@ -315,7 +315,7 @@ int btrfs_check_delayed_seq(struct btrfs_fs_info *fs_info,
10668 + struct seq_list *elem;
10669 + int ret = 0;
10670 +
10671 +- spin_lock(&fs_info->tree_mod_seq_lock);
10672 ++ read_lock(&fs_info->tree_mod_log_lock);
10673 + if (!list_empty(&fs_info->tree_mod_seq_list)) {
10674 + elem = list_first_entry(&fs_info->tree_mod_seq_list,
10675 + struct seq_list, list);
10676 +@@ -329,7 +329,7 @@ int btrfs_check_delayed_seq(struct btrfs_fs_info *fs_info,
10677 + }
10678 + }
10679 +
10680 +- spin_unlock(&fs_info->tree_mod_seq_lock);
10681 ++ read_unlock(&fs_info->tree_mod_log_lock);
10682 + return ret;
10683 + }
10684 +
10685 +diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
10686 +index b37519241eb1..e3524ecce3d7 100644
10687 +--- a/fs/btrfs/disk-io.c
10688 ++++ b/fs/btrfs/disk-io.c
10689 +@@ -2104,7 +2104,7 @@ static void free_root_extent_buffers(struct btrfs_root *root)
10690 + }
10691 +
10692 + /* helper to cleanup tree roots */
10693 +-static void free_root_pointers(struct btrfs_fs_info *info, int chunk_root)
10694 ++static void free_root_pointers(struct btrfs_fs_info *info, bool free_chunk_root)
10695 + {
10696 + free_root_extent_buffers(info->tree_root);
10697 +
10698 +@@ -2113,7 +2113,7 @@ static void free_root_pointers(struct btrfs_fs_info *info, int chunk_root)
10699 + free_root_extent_buffers(info->csum_root);
10700 + free_root_extent_buffers(info->quota_root);
10701 + free_root_extent_buffers(info->uuid_root);
10702 +- if (chunk_root)
10703 ++ if (free_chunk_root)
10704 + free_root_extent_buffers(info->chunk_root);
10705 + free_root_extent_buffers(info->free_space_root);
10706 + }
10707 +@@ -2519,7 +2519,6 @@ int open_ctree(struct super_block *sb,
10708 + spin_lock_init(&fs_info->delayed_iput_lock);
10709 + spin_lock_init(&fs_info->defrag_inodes_lock);
10710 + spin_lock_init(&fs_info->free_chunk_lock);
10711 +- spin_lock_init(&fs_info->tree_mod_seq_lock);
10712 + spin_lock_init(&fs_info->super_lock);
10713 + spin_lock_init(&fs_info->qgroup_op_lock);
10714 + spin_lock_init(&fs_info->buffer_lock);
10715 +@@ -3136,7 +3135,7 @@ fail_block_groups:
10716 + btrfs_free_block_groups(fs_info);
10717 +
10718 + fail_tree_roots:
10719 +- free_root_pointers(fs_info, 1);
10720 ++ free_root_pointers(fs_info, true);
10721 + invalidate_inode_pages2(fs_info->btree_inode->i_mapping);
10722 +
10723 + fail_sb_buffer:
10724 +@@ -3165,7 +3164,7 @@ recovery_tree_root:
10725 + if (!btrfs_test_opt(tree_root->fs_info, USEBACKUPROOT))
10726 + goto fail_tree_roots;
10727 +
10728 +- free_root_pointers(fs_info, 0);
10729 ++ free_root_pointers(fs_info, false);
10730 +
10731 + /* don't use the log in recovery mode, it won't be valid */
10732 + btrfs_set_super_log_root(disk_super, 0);
10733 +@@ -3862,7 +3861,7 @@ void close_ctree(struct btrfs_root *root)
10734 + btrfs_stop_all_workers(fs_info);
10735 +
10736 + clear_bit(BTRFS_FS_OPEN, &fs_info->flags);
10737 +- free_root_pointers(fs_info, 1);
10738 ++ free_root_pointers(fs_info, true);
10739 +
10740 + iput(fs_info->btree_inode);
10741 +
10742 +diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c
10743 +index 37a28e2369b9..1372d3e5d90b 100644
10744 +--- a/fs/btrfs/extent_io.c
10745 ++++ b/fs/btrfs/extent_io.c
10746 +@@ -4060,6 +4060,14 @@ retry:
10747 + */
10748 + scanned = 1;
10749 + index = 0;
10750 ++
10751 ++ /*
10752 ++ * If we're looping we could run into a page that is locked by a
10753 ++ * writer and that writer could be waiting on writeback for a
10754 ++ * page in our current bio, and thus deadlock, so flush the
10755 ++ * write bio here.
10756 ++ */
10757 ++ flush_write_bio(data);
10758 + goto retry;
10759 + }
10760 +
10761 +diff --git a/fs/btrfs/tests/btrfs-tests.c b/fs/btrfs/tests/btrfs-tests.c
10762 +index bf62ad919a95..9edc2674b8a7 100644
10763 +--- a/fs/btrfs/tests/btrfs-tests.c
10764 ++++ b/fs/btrfs/tests/btrfs-tests.c
10765 +@@ -112,7 +112,6 @@ struct btrfs_fs_info *btrfs_alloc_dummy_fs_info(void)
10766 + spin_lock_init(&fs_info->qgroup_op_lock);
10767 + spin_lock_init(&fs_info->super_lock);
10768 + spin_lock_init(&fs_info->fs_roots_radix_lock);
10769 +- spin_lock_init(&fs_info->tree_mod_seq_lock);
10770 + mutex_init(&fs_info->qgroup_ioctl_lock);
10771 + mutex_init(&fs_info->qgroup_rescan_lock);
10772 + rwlock_init(&fs_info->tree_mod_log_lock);
10773 +diff --git a/fs/btrfs/transaction.c b/fs/btrfs/transaction.c
10774 +index fd6c74662e9a..31df020634cd 100644
10775 +--- a/fs/btrfs/transaction.c
10776 ++++ b/fs/btrfs/transaction.c
10777 +@@ -1917,6 +1917,14 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
10778 + struct btrfs_transaction *prev_trans = NULL;
10779 + int ret;
10780 +
10781 ++ /*
10782 ++ * Some places just start a transaction to commit it. We need to make
10783 ++ * sure that if this commit fails that the abort code actually marks the
10784 ++ * transaction as failed, so set trans->dirty to make the abort code do
10785 ++ * the right thing.
10786 ++ */
10787 ++ trans->dirty = true;
10788 ++
10789 + /* Stop the commit early if ->aborted is set */
10790 + if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
10791 + ret = cur_trans->aborted;
10792 +diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c
10793 +index 7ee573cddde9..f79682937faf 100644
10794 +--- a/fs/btrfs/tree-log.c
10795 ++++ b/fs/btrfs/tree-log.c
10796 +@@ -4443,13 +4443,8 @@ static int btrfs_log_trailing_hole(struct btrfs_trans_handle *trans,
10797 + struct btrfs_file_extent_item);
10798 +
10799 + if (btrfs_file_extent_type(leaf, extent) ==
10800 +- BTRFS_FILE_EXTENT_INLINE) {
10801 +- len = btrfs_file_extent_inline_len(leaf,
10802 +- path->slots[0],
10803 +- extent);
10804 +- ASSERT(len == i_size);
10805 ++ BTRFS_FILE_EXTENT_INLINE)
10806 + return 0;
10807 +- }
10808 +
10809 + len = btrfs_file_extent_num_bytes(leaf, extent);
10810 + /* Last extent goes beyond i_size, no need to log a hole. */
10811 +diff --git a/fs/cifs/smb2pdu.c b/fs/cifs/smb2pdu.c
10812 +index 5255deac86b2..e8dc28dbe563 100644
10813 +--- a/fs/cifs/smb2pdu.c
10814 ++++ b/fs/cifs/smb2pdu.c
10815 +@@ -247,9 +247,14 @@ smb2_reconnect(__le16 smb2_command, struct cifs_tcon *tcon)
10816 + */
10817 + mutex_lock(&tcon->ses->session_mutex);
10818 + rc = cifs_negotiate_protocol(0, tcon->ses);
10819 +- if (!rc && tcon->ses->need_reconnect)
10820 ++ if (!rc && tcon->ses->need_reconnect) {
10821 + rc = cifs_setup_session(0, tcon->ses, nls_codepage);
10822 +-
10823 ++ if ((rc == -EACCES) && !tcon->retry) {
10824 ++ rc = -EHOSTDOWN;
10825 ++ mutex_unlock(&tcon->ses->session_mutex);
10826 ++ goto failed;
10827 ++ }
10828 ++ }
10829 + if (rc || !tcon->need_reconnect) {
10830 + mutex_unlock(&tcon->ses->session_mutex);
10831 + goto out;
10832 +@@ -291,6 +296,7 @@ out:
10833 + case SMB2_SET_INFO:
10834 + rc = -EAGAIN;
10835 + }
10836 ++failed:
10837 + unload_nls(nls_codepage);
10838 + return rc;
10839 + }
10840 +diff --git a/fs/ext2/super.c b/fs/ext2/super.c
10841 +index 6fcb29b393d3..186912c9bf56 100644
10842 +--- a/fs/ext2/super.c
10843 ++++ b/fs/ext2/super.c
10844 +@@ -1047,9 +1047,9 @@ static int ext2_fill_super(struct super_block *sb, void *data, int silent)
10845 +
10846 + if (EXT2_BLOCKS_PER_GROUP(sb) == 0)
10847 + goto cantfind_ext2;
10848 +- sbi->s_groups_count = ((le32_to_cpu(es->s_blocks_count) -
10849 +- le32_to_cpu(es->s_first_data_block) - 1)
10850 +- / EXT2_BLOCKS_PER_GROUP(sb)) + 1;
10851 ++ sbi->s_groups_count = ((le32_to_cpu(es->s_blocks_count) -
10852 ++ le32_to_cpu(es->s_first_data_block) - 1)
10853 ++ / EXT2_BLOCKS_PER_GROUP(sb)) + 1;
10854 + db_count = (sbi->s_groups_count + EXT2_DESC_PER_BLOCK(sb) - 1) /
10855 + EXT2_DESC_PER_BLOCK(sb);
10856 + sbi->s_group_desc = kmalloc (db_count * sizeof (struct buffer_head *), GFP_KERNEL);
10857 +diff --git a/fs/ext4/page-io.c b/fs/ext4/page-io.c
10858 +index 0094923e5ebf..94f60f9d57fd 100644
10859 +--- a/fs/ext4/page-io.c
10860 ++++ b/fs/ext4/page-io.c
10861 +@@ -469,16 +469,25 @@ int ext4_bio_write_page(struct ext4_io_submit *io,
10862 + nr_to_submit) {
10863 + gfp_t gfp_flags = GFP_NOFS;
10864 +
10865 ++ /*
10866 ++ * Since bounce page allocation uses a mempool, we can only use
10867 ++ * a waiting mask (i.e. request guaranteed allocation) on the
10868 ++ * first page of the bio. Otherwise it can deadlock.
10869 ++ */
10870 ++ if (io->io_bio)
10871 ++ gfp_flags = GFP_NOWAIT | __GFP_NOWARN;
10872 + retry_encrypt:
10873 + data_page = fscrypt_encrypt_page(inode, page, gfp_flags);
10874 + if (IS_ERR(data_page)) {
10875 + ret = PTR_ERR(data_page);
10876 +- if (ret == -ENOMEM && wbc->sync_mode == WB_SYNC_ALL) {
10877 +- if (io->io_bio) {
10878 ++ if (ret == -ENOMEM &&
10879 ++ (io->io_bio || wbc->sync_mode == WB_SYNC_ALL)) {
10880 ++ gfp_flags = GFP_NOFS;
10881 ++ if (io->io_bio)
10882 + ext4_io_submit(io);
10883 +- congestion_wait(BLK_RW_ASYNC, HZ/50);
10884 +- }
10885 +- gfp_flags |= __GFP_NOFAIL;
10886 ++ else
10887 ++ gfp_flags |= __GFP_NOFAIL;
10888 ++ congestion_wait(BLK_RW_ASYNC, HZ/50);
10889 + goto retry_encrypt;
10890 + }
10891 + data_page = NULL;
10892 +diff --git a/fs/nfs/Kconfig b/fs/nfs/Kconfig
10893 +index b1daeafbea92..c3428767332c 100644
10894 +--- a/fs/nfs/Kconfig
10895 ++++ b/fs/nfs/Kconfig
10896 +@@ -89,7 +89,7 @@ config NFS_V4
10897 + config NFS_SWAP
10898 + bool "Provide swap over NFS support"
10899 + default n
10900 +- depends on NFS_FS
10901 ++ depends on NFS_FS && SWAP
10902 + select SUNRPC_SWAP
10903 + help
10904 + This option enables swapon to work on files located on NFS mounts.
10905 +diff --git a/fs/nfs/callback_proc.c b/fs/nfs/callback_proc.c
10906 +index 9d7537446260..0d4a56c77a1a 100644
10907 +--- a/fs/nfs/callback_proc.c
10908 ++++ b/fs/nfs/callback_proc.c
10909 +@@ -419,7 +419,7 @@ static bool referring_call_exists(struct nfs_client *clp,
10910 + uint32_t nrclists,
10911 + struct referring_call_list *rclists)
10912 + {
10913 +- bool status = 0;
10914 ++ bool status = false;
10915 + int i, j;
10916 + struct nfs4_session *session;
10917 + struct nfs4_slot_table *tbl;
10918 +diff --git a/fs/nfs/dir.c b/fs/nfs/dir.c
10919 +index 1e5321d1ed22..c2665d920cf8 100644
10920 +--- a/fs/nfs/dir.c
10921 ++++ b/fs/nfs/dir.c
10922 +@@ -57,7 +57,7 @@ static void nfs_readdir_clear_array(struct page*);
10923 + const struct file_operations nfs_dir_operations = {
10924 + .llseek = nfs_llseek_dir,
10925 + .read = generic_read_dir,
10926 +- .iterate_shared = nfs_readdir,
10927 ++ .iterate = nfs_readdir,
10928 + .open = nfs_opendir,
10929 + .release = nfs_closedir,
10930 + .fsync = nfs_fsync_dir,
10931 +@@ -145,7 +145,6 @@ struct nfs_cache_array_entry {
10932 + };
10933 +
10934 + struct nfs_cache_array {
10935 +- atomic_t refcount;
10936 + int size;
10937 + int eof_index;
10938 + u64 last_cookie;
10939 +@@ -170,6 +169,17 @@ typedef struct {
10940 + unsigned int eof:1;
10941 + } nfs_readdir_descriptor_t;
10942 +
10943 ++static
10944 ++void nfs_readdir_init_array(struct page *page)
10945 ++{
10946 ++ struct nfs_cache_array *array;
10947 ++
10948 ++ array = kmap_atomic(page);
10949 ++ memset(array, 0, sizeof(struct nfs_cache_array));
10950 ++ array->eof_index = -1;
10951 ++ kunmap_atomic(array);
10952 ++}
10953 ++
10954 + /*
10955 + * The caller is responsible for calling nfs_readdir_release_array(page)
10956 + */
10957 +@@ -201,20 +211,12 @@ void nfs_readdir_clear_array(struct page *page)
10958 + int i;
10959 +
10960 + array = kmap_atomic(page);
10961 +- if (atomic_dec_and_test(&array->refcount))
10962 +- for (i = 0; i < array->size; i++)
10963 +- kfree(array->array[i].string.name);
10964 ++ for (i = 0; i < array->size; i++)
10965 ++ kfree(array->array[i].string.name);
10966 ++ array->size = 0;
10967 + kunmap_atomic(array);
10968 + }
10969 +
10970 +-static bool grab_page(struct page *page)
10971 +-{
10972 +- struct nfs_cache_array *array = kmap_atomic(page);
10973 +- bool res = atomic_inc_not_zero(&array->refcount);
10974 +- kunmap_atomic(array);
10975 +- return res;
10976 +-}
10977 +-
10978 + /*
10979 + * the caller is responsible for freeing qstr.name
10980 + * when called by nfs_readdir_add_to_array, the strings will be freed in
10981 +@@ -287,7 +289,7 @@ int nfs_readdir_search_for_pos(struct nfs_cache_array *array, nfs_readdir_descri
10982 + desc->cache_entry_index = index;
10983 + return 0;
10984 + out_eof:
10985 +- desc->eof = 1;
10986 ++ desc->eof = true;
10987 + return -EBADCOOKIE;
10988 + }
10989 +
10990 +@@ -341,7 +343,7 @@ int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, nfs_readdir_des
10991 + if (array->eof_index >= 0) {
10992 + status = -EBADCOOKIE;
10993 + if (*desc->dir_cookie == array->last_cookie)
10994 +- desc->eof = 1;
10995 ++ desc->eof = true;
10996 + }
10997 + out:
10998 + return status;
10999 +@@ -653,6 +655,8 @@ int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page,
11000 + int status = -ENOMEM;
11001 + unsigned int array_size = ARRAY_SIZE(pages);
11002 +
11003 ++ nfs_readdir_init_array(page);
11004 ++
11005 + entry.prev_cookie = 0;
11006 + entry.cookie = desc->last_cookie;
11007 + entry.eof = 0;
11008 +@@ -673,9 +677,8 @@ int nfs_readdir_xdr_to_array(nfs_readdir_descriptor_t *desc, struct page *page,
11009 + status = PTR_ERR(array);
11010 + goto out_label_free;
11011 + }
11012 +- memset(array, 0, sizeof(struct nfs_cache_array));
11013 +- atomic_set(&array->refcount, 1);
11014 +- array->eof_index = -1;
11015 ++
11016 ++ array = kmap(page);
11017 +
11018 + status = nfs_readdir_alloc_pages(pages, array_size);
11019 + if (status < 0)
11020 +@@ -730,6 +733,7 @@ int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page* page)
11021 + unlock_page(page);
11022 + return 0;
11023 + error:
11024 ++ nfs_readdir_clear_array(page);
11025 + unlock_page(page);
11026 + return ret;
11027 + }
11028 +@@ -737,7 +741,6 @@ int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page* page)
11029 + static
11030 + void cache_page_release(nfs_readdir_descriptor_t *desc)
11031 + {
11032 +- nfs_readdir_clear_array(desc->page);
11033 + put_page(desc->page);
11034 + desc->page = NULL;
11035 + }
11036 +@@ -745,33 +748,34 @@ void cache_page_release(nfs_readdir_descriptor_t *desc)
11037 + static
11038 + struct page *get_cache_page(nfs_readdir_descriptor_t *desc)
11039 + {
11040 +- struct page *page;
11041 +-
11042 +- for (;;) {
11043 +- page = read_cache_page(desc->file->f_mapping,
11044 ++ return read_cache_page(desc->file->f_mapping,
11045 + desc->page_index, (filler_t *)nfs_readdir_filler, desc);
11046 +- if (IS_ERR(page) || grab_page(page))
11047 +- break;
11048 +- put_page(page);
11049 +- }
11050 +- return page;
11051 + }
11052 +
11053 + /*
11054 + * Returns 0 if desc->dir_cookie was found on page desc->page_index
11055 ++ * and locks the page to prevent removal from the page cache.
11056 + */
11057 + static
11058 +-int find_cache_page(nfs_readdir_descriptor_t *desc)
11059 ++int find_and_lock_cache_page(nfs_readdir_descriptor_t *desc)
11060 + {
11061 + int res;
11062 +
11063 + desc->page = get_cache_page(desc);
11064 + if (IS_ERR(desc->page))
11065 + return PTR_ERR(desc->page);
11066 +-
11067 +- res = nfs_readdir_search_array(desc);
11068 ++ res = lock_page_killable(desc->page);
11069 + if (res != 0)
11070 +- cache_page_release(desc);
11071 ++ goto error;
11072 ++ res = -EAGAIN;
11073 ++ if (desc->page->mapping != NULL) {
11074 ++ res = nfs_readdir_search_array(desc);
11075 ++ if (res == 0)
11076 ++ return 0;
11077 ++ }
11078 ++ unlock_page(desc->page);
11079 ++error:
11080 ++ cache_page_release(desc);
11081 + return res;
11082 + }
11083 +
11084 +@@ -786,7 +790,7 @@ int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
11085 + desc->last_cookie = 0;
11086 + }
11087 + do {
11088 +- res = find_cache_page(desc);
11089 ++ res = find_and_lock_cache_page(desc);
11090 + } while (res == -EAGAIN);
11091 + return res;
11092 + }
11093 +@@ -815,7 +819,7 @@ int nfs_do_filldir(nfs_readdir_descriptor_t *desc)
11094 + ent = &array->array[i];
11095 + if (!dir_emit(desc->ctx, ent->string.name, ent->string.len,
11096 + nfs_compat_user_ino64(ent->ino), ent->d_type)) {
11097 +- desc->eof = 1;
11098 ++ desc->eof = true;
11099 + break;
11100 + }
11101 + desc->ctx->pos++;
11102 +@@ -827,11 +831,10 @@ int nfs_do_filldir(nfs_readdir_descriptor_t *desc)
11103 + ctx->duped = 1;
11104 + }
11105 + if (array->eof_index >= 0)
11106 +- desc->eof = 1;
11107 ++ desc->eof = true;
11108 +
11109 + nfs_readdir_release_array(desc->page);
11110 + out:
11111 +- cache_page_release(desc);
11112 + dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n",
11113 + (unsigned long long)*desc->dir_cookie, res);
11114 + return res;
11115 +@@ -877,13 +880,13 @@ int uncached_readdir(nfs_readdir_descriptor_t *desc)
11116 +
11117 + status = nfs_do_filldir(desc);
11118 +
11119 ++ out_release:
11120 ++ nfs_readdir_clear_array(desc->page);
11121 ++ cache_page_release(desc);
11122 + out:
11123 + dfprintk(DIRCACHE, "NFS: %s: returns %d\n",
11124 + __func__, status);
11125 + return status;
11126 +- out_release:
11127 +- cache_page_release(desc);
11128 +- goto out;
11129 + }
11130 +
11131 + /* The file offset position represents the dirent entry number. A
11132 +@@ -928,7 +931,7 @@ static int nfs_readdir(struct file *file, struct dir_context *ctx)
11133 + if (res == -EBADCOOKIE) {
11134 + res = 0;
11135 + /* This means either end of directory */
11136 +- if (*desc->dir_cookie && desc->eof == 0) {
11137 ++ if (*desc->dir_cookie && !desc->eof) {
11138 + /* Or that the server has 'lost' a cookie */
11139 + res = uncached_readdir(desc);
11140 + if (res == 0)
11141 +@@ -948,6 +951,8 @@ static int nfs_readdir(struct file *file, struct dir_context *ctx)
11142 + break;
11143 +
11144 + res = nfs_do_filldir(desc);
11145 ++ unlock_page(desc->page);
11146 ++ cache_page_release(desc);
11147 + if (res < 0)
11148 + break;
11149 + } while (!desc->eof);
11150 +@@ -960,11 +965,13 @@ out:
11151 +
11152 + static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence)
11153 + {
11154 ++ struct inode *inode = file_inode(filp);
11155 + struct nfs_open_dir_context *dir_ctx = filp->private_data;
11156 +
11157 + dfprintk(FILE, "NFS: llseek dir(%pD2, %lld, %d)\n",
11158 + filp, offset, whence);
11159 +
11160 ++ inode_lock(inode);
11161 + switch (whence) {
11162 + case 1:
11163 + offset += filp->f_pos;
11164 +@@ -972,13 +979,16 @@ static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence)
11165 + if (offset >= 0)
11166 + break;
11167 + default:
11168 +- return -EINVAL;
11169 ++ offset = -EINVAL;
11170 ++ goto out;
11171 + }
11172 + if (offset != filp->f_pos) {
11173 + filp->f_pos = offset;
11174 + dir_ctx->dir_cookie = 0;
11175 + dir_ctx->duped = 0;
11176 + }
11177 ++out:
11178 ++ inode_unlock(inode);
11179 + return offset;
11180 + }
11181 +
11182 +diff --git a/fs/nfs/nfs4client.c b/fs/nfs/nfs4client.c
11183 +index 1ec6dd4f3e2e..3ee60c533217 100644
11184 +--- a/fs/nfs/nfs4client.c
11185 ++++ b/fs/nfs/nfs4client.c
11186 +@@ -847,7 +847,7 @@ nfs4_find_client_sessionid(struct net *net, const struct sockaddr *addr,
11187 +
11188 + spin_lock(&nn->nfs_client_lock);
11189 + list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) {
11190 +- if (nfs4_cb_match_client(addr, clp, minorversion) == false)
11191 ++ if (!nfs4_cb_match_client(addr, clp, minorversion))
11192 + continue;
11193 +
11194 + if (!nfs4_has_session(clp))
11195 +diff --git a/fs/nfs/nfs4proc.c b/fs/nfs/nfs4proc.c
11196 +index ca4249ae644f..632d3c3f8dfb 100644
11197 +--- a/fs/nfs/nfs4proc.c
11198 ++++ b/fs/nfs/nfs4proc.c
11199 +@@ -2916,6 +2916,11 @@ static struct nfs4_state *nfs4_do_open(struct inode *dir,
11200 + exception.retry = 1;
11201 + continue;
11202 + }
11203 ++ if (status == -NFS4ERR_EXPIRED) {
11204 ++ nfs4_schedule_lease_recovery(server->nfs_client);
11205 ++ exception.retry = 1;
11206 ++ continue;
11207 ++ }
11208 + if (status == -EAGAIN) {
11209 + /* We must have found a delegation */
11210 + exception.retry = 1;
11211 +diff --git a/fs/nfs/pnfs.c b/fs/nfs/pnfs.c
11212 +index 0e008db16b16..c3abf92adfb7 100644
11213 +--- a/fs/nfs/pnfs.c
11214 ++++ b/fs/nfs/pnfs.c
11215 +@@ -1436,7 +1436,7 @@ pnfs_lseg_range_match(const struct pnfs_layout_range *ls_range,
11216 + if ((range->iomode == IOMODE_RW &&
11217 + ls_range->iomode != IOMODE_RW) ||
11218 + (range->iomode != ls_range->iomode &&
11219 +- strict_iomode == true) ||
11220 ++ strict_iomode) ||
11221 + !pnfs_lseg_range_intersecting(ls_range, range))
11222 + return 0;
11223 +
11224 +diff --git a/fs/nfsd/nfs4layouts.c b/fs/nfsd/nfs4layouts.c
11225 +index 64813697f4c4..f6cc2fddb78b 100644
11226 +--- a/fs/nfsd/nfs4layouts.c
11227 ++++ b/fs/nfsd/nfs4layouts.c
11228 +@@ -680,7 +680,7 @@ nfsd4_cb_layout_done(struct nfsd4_callback *cb, struct rpc_task *task)
11229 +
11230 + /* Client gets 2 lease periods to return it */
11231 + cutoff = ktime_add_ns(task->tk_start,
11232 +- nn->nfsd4_lease * NSEC_PER_SEC * 2);
11233 ++ (u64)nn->nfsd4_lease * NSEC_PER_SEC * 2);
11234 +
11235 + if (ktime_before(now, cutoff)) {
11236 + rpc_delay(task, HZ/100); /* 10 mili-seconds */
11237 +diff --git a/fs/nfsd/nfs4state.c b/fs/nfsd/nfs4state.c
11238 +index db4bd70b62d0..4509c76716e3 100644
11239 +--- a/fs/nfsd/nfs4state.c
11240 ++++ b/fs/nfsd/nfs4state.c
11241 +@@ -6034,7 +6034,7 @@ nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
11242 + }
11243 +
11244 + if (fl_flags & FL_SLEEP) {
11245 +- nbl->nbl_time = jiffies;
11246 ++ nbl->nbl_time = get_seconds();
11247 + spin_lock(&nn->blocked_locks_lock);
11248 + list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
11249 + list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
11250 +diff --git a/fs/nfsd/state.h b/fs/nfsd/state.h
11251 +index 133d8bf62a5c..7872b1ead885 100644
11252 +--- a/fs/nfsd/state.h
11253 ++++ b/fs/nfsd/state.h
11254 +@@ -591,7 +591,7 @@ static inline bool nfsd4_stateid_generation_after(stateid_t *a, stateid_t *b)
11255 + struct nfsd4_blocked_lock {
11256 + struct list_head nbl_list;
11257 + struct list_head nbl_lru;
11258 +- unsigned long nbl_time;
11259 ++ time_t nbl_time;
11260 + struct file_lock nbl_lock;
11261 + struct knfsd_fh nbl_fh;
11262 + struct nfsd4_callback nbl_cb;
11263 +diff --git a/fs/ubifs/file.c b/fs/ubifs/file.c
11264 +index b4fbeefba246..5ef0d1d60743 100644
11265 +--- a/fs/ubifs/file.c
11266 ++++ b/fs/ubifs/file.c
11267 +@@ -721,6 +721,7 @@ static int ubifs_do_bulk_read(struct ubifs_info *c, struct bu_info *bu,
11268 + int err, page_idx, page_cnt, ret = 0, n = 0;
11269 + int allocate = bu->buf ? 0 : 1;
11270 + loff_t isize;
11271 ++ gfp_t ra_gfp_mask = readahead_gfp_mask(mapping) & ~__GFP_FS;
11272 +
11273 + err = ubifs_tnc_get_bu_keys(c, bu);
11274 + if (err)
11275 +@@ -782,8 +783,9 @@ static int ubifs_do_bulk_read(struct ubifs_info *c, struct bu_info *bu,
11276 +
11277 + if (page_offset > end_index)
11278 + break;
11279 +- page = find_or_create_page(mapping, page_offset,
11280 +- GFP_NOFS | __GFP_COLD);
11281 ++ page = pagecache_get_page(mapping, page_offset,
11282 ++ FGP_LOCK|FGP_ACCESSED|FGP_CREAT|FGP_NOWAIT,
11283 ++ ra_gfp_mask);
11284 + if (!page)
11285 + break;
11286 + if (!PageUptodate(page))
11287 +diff --git a/include/media/v4l2-rect.h b/include/media/v4l2-rect.h
11288 +index d2125f0cc7cd..1584c760b993 100644
11289 +--- a/include/media/v4l2-rect.h
11290 ++++ b/include/media/v4l2-rect.h
11291 +@@ -75,10 +75,10 @@ static inline void v4l2_rect_map_inside(struct v4l2_rect *r,
11292 + r->left = boundary->left;
11293 + if (r->top < boundary->top)
11294 + r->top = boundary->top;
11295 +- if (r->left + r->width > boundary->width)
11296 +- r->left = boundary->width - r->width;
11297 +- if (r->top + r->height > boundary->height)
11298 +- r->top = boundary->height - r->height;
11299 ++ if (r->left + r->width > boundary->left + boundary->width)
11300 ++ r->left = boundary->left + boundary->width - r->width;
11301 ++ if (r->top + r->height > boundary->top + boundary->height)
11302 ++ r->top = boundary->top + boundary->height - r->height;
11303 + }
11304 +
11305 + /**
11306 +diff --git a/kernel/events/core.c b/kernel/events/core.c
11307 +index 64ace5e9af2a..97b90faceb97 100644
11308 +--- a/kernel/events/core.c
11309 ++++ b/kernel/events/core.c
11310 +@@ -5303,7 +5303,15 @@ accounting:
11311 + */
11312 + user_lock_limit *= num_online_cpus();
11313 +
11314 +- user_locked = atomic_long_read(&user->locked_vm) + user_extra;
11315 ++ user_locked = atomic_long_read(&user->locked_vm);
11316 ++
11317 ++ /*
11318 ++ * sysctl_perf_event_mlock may have changed, so that
11319 ++ * user->locked_vm > user_lock_limit
11320 ++ */
11321 ++ if (user_locked > user_lock_limit)
11322 ++ user_locked = user_lock_limit;
11323 ++ user_locked += user_extra;
11324 +
11325 + if (user_locked > user_lock_limit)
11326 + extra = user_locked - user_lock_limit;
11327 +diff --git a/kernel/time/clocksource.c b/kernel/time/clocksource.c
11328 +index 7e4fad75acaa..2924ff544c9e 100644
11329 +--- a/kernel/time/clocksource.c
11330 ++++ b/kernel/time/clocksource.c
11331 +@@ -272,8 +272,15 @@ static void clocksource_watchdog(unsigned long data)
11332 + next_cpu = cpumask_next(raw_smp_processor_id(), cpu_online_mask);
11333 + if (next_cpu >= nr_cpu_ids)
11334 + next_cpu = cpumask_first(cpu_online_mask);
11335 +- watchdog_timer.expires += WATCHDOG_INTERVAL;
11336 +- add_timer_on(&watchdog_timer, next_cpu);
11337 ++
11338 ++ /*
11339 ++ * Arm timer if not already pending: could race with concurrent
11340 ++ * pair clocksource_stop_watchdog() clocksource_start_watchdog().
11341 ++ */
11342 ++ if (!timer_pending(&watchdog_timer)) {
11343 ++ watchdog_timer.expires += WATCHDOG_INTERVAL;
11344 ++ add_timer_on(&watchdog_timer, next_cpu);
11345 ++ }
11346 + out:
11347 + spin_unlock(&watchdog_lock);
11348 + }
11349 +diff --git a/lib/test_kasan.c b/lib/test_kasan.c
11350 +index 4ba4cbe169a8..6e76a448867d 100644
11351 +--- a/lib/test_kasan.c
11352 ++++ b/lib/test_kasan.c
11353 +@@ -124,6 +124,7 @@ static noinline void __init kmalloc_oob_krealloc_more(void)
11354 + if (!ptr1 || !ptr2) {
11355 + pr_err("Allocation failed\n");
11356 + kfree(ptr1);
11357 ++ kfree(ptr2);
11358 + return;
11359 + }
11360 +
11361 +diff --git a/net/hsr/hsr_slave.c b/net/hsr/hsr_slave.c
11362 +index f5b60388d02f..4ff6e02d8b73 100644
11363 +--- a/net/hsr/hsr_slave.c
11364 ++++ b/net/hsr/hsr_slave.c
11365 +@@ -31,6 +31,8 @@ static rx_handler_result_t hsr_handle_frame(struct sk_buff **pskb)
11366 +
11367 + rcu_read_lock(); /* hsr->node_db, hsr->ports */
11368 + port = hsr_port_get_rcu(skb->dev);
11369 ++ if (!port)
11370 ++ goto finish_pass;
11371 +
11372 + if (hsr_addr_is_self(port->hsr, eth_hdr(skb)->h_source)) {
11373 + /* Directly kill frames sent by ourselves */
11374 +diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c
11375 +index 6e25524c6a74..02918e0d635e 100644
11376 +--- a/net/ipv4/tcp.c
11377 ++++ b/net/ipv4/tcp.c
11378 +@@ -2298,9 +2298,11 @@ int tcp_disconnect(struct sock *sk, int flags)
11379 + tp->snd_ssthresh = TCP_INFINITE_SSTHRESH;
11380 + tp->snd_cwnd_cnt = 0;
11381 + tp->window_clamp = 0;
11382 ++ tp->delivered = 0;
11383 + tcp_set_ca_state(sk, TCP_CA_Open);
11384 + tp->is_sack_reneg = 0;
11385 + tcp_clear_retrans(tp);
11386 ++ tp->total_retrans = 0;
11387 + inet_csk_delack_init(sk);
11388 + /* Initialize rcv_mss to TCP_MIN_MSS to avoid division by 0
11389 + * issue in __tcp_select_window()
11390 +@@ -2312,8 +2314,12 @@ int tcp_disconnect(struct sock *sk, int flags)
11391 + dst_release(sk->sk_rx_dst);
11392 + sk->sk_rx_dst = NULL;
11393 + tcp_saved_syn_free(tp);
11394 ++ tp->segs_in = 0;
11395 ++ tp->segs_out = 0;
11396 + tp->bytes_acked = 0;
11397 + tp->bytes_received = 0;
11398 ++ tp->data_segs_in = 0;
11399 ++ tp->data_segs_out = 0;
11400 +
11401 + WARN_ON(inet->inet_num && !icsk->icsk_bind_hash);
11402 +
11403 +diff --git a/net/rxrpc/ar-internal.h b/net/rxrpc/ar-internal.h
11404 +index f60e35576526..d6a64771463f 100644
11405 +--- a/net/rxrpc/ar-internal.h
11406 ++++ b/net/rxrpc/ar-internal.h
11407 +@@ -401,6 +401,7 @@ enum rxrpc_call_flag {
11408 + RXRPC_CALL_SEND_PING, /* A ping will need to be sent */
11409 + RXRPC_CALL_PINGING, /* Ping in process */
11410 + RXRPC_CALL_RETRANS_TIMEOUT, /* Retransmission due to timeout occurred */
11411 ++ RXRPC_CALL_DISCONNECTED, /* The call has been disconnected */
11412 + };
11413 +
11414 + /*
11415 +diff --git a/net/rxrpc/call_object.c b/net/rxrpc/call_object.c
11416 +index 1ed18d8c9c9f..88bcd146142f 100644
11417 +--- a/net/rxrpc/call_object.c
11418 ++++ b/net/rxrpc/call_object.c
11419 +@@ -464,7 +464,7 @@ void rxrpc_release_call(struct rxrpc_sock *rx, struct rxrpc_call *call)
11420 +
11421 + _debug("RELEASE CALL %p (%d CONN %p)", call, call->debug_id, conn);
11422 +
11423 +- if (conn)
11424 ++ if (conn && !test_bit(RXRPC_CALL_DISCONNECTED, &call->flags))
11425 + rxrpc_disconnect_call(call);
11426 +
11427 + for (i = 0; i < RXRPC_RXTX_BUFF_SIZE; i++) {
11428 +@@ -539,6 +539,7 @@ static void rxrpc_rcu_destroy_call(struct rcu_head *rcu)
11429 + {
11430 + struct rxrpc_call *call = container_of(rcu, struct rxrpc_call, rcu);
11431 +
11432 ++ rxrpc_put_connection(call->conn);
11433 + rxrpc_put_peer(call->peer);
11434 + kfree(call->rxtx_buffer);
11435 + kfree(call->rxtx_annotations);
11436 +@@ -560,7 +561,6 @@ void rxrpc_cleanup_call(struct rxrpc_call *call)
11437 +
11438 + ASSERTCMP(call->state, ==, RXRPC_CALL_COMPLETE);
11439 + ASSERT(test_bit(RXRPC_CALL_RELEASED, &call->flags));
11440 +- ASSERTCMP(call->conn, ==, NULL);
11441 +
11442 + /* Clean up the Rx/Tx buffer */
11443 + for (i = 0; i < RXRPC_RXTX_BUFF_SIZE; i++)
11444 +diff --git a/net/rxrpc/conn_client.c b/net/rxrpc/conn_client.c
11445 +index 0fce919bf47d..dd41733f182a 100644
11446 +--- a/net/rxrpc/conn_client.c
11447 ++++ b/net/rxrpc/conn_client.c
11448 +@@ -736,9 +736,9 @@ void rxrpc_disconnect_client_call(struct rxrpc_call *call)
11449 + struct rxrpc_channel *chan = &conn->channels[channel];
11450 +
11451 + trace_rxrpc_client(conn, channel, rxrpc_client_chan_disconnect);
11452 +- call->conn = NULL;
11453 +
11454 + spin_lock(&conn->channel_lock);
11455 ++ set_bit(RXRPC_CALL_DISCONNECTED, &call->flags);
11456 +
11457 + /* Calls that have never actually been assigned a channel can simply be
11458 + * discarded. If the conn didn't get used either, it will follow
11459 +@@ -828,7 +828,6 @@ out:
11460 + spin_unlock(&rxrpc_client_conn_cache_lock);
11461 + out_2:
11462 + spin_unlock(&conn->channel_lock);
11463 +- rxrpc_put_connection(conn);
11464 + _leave("");
11465 + return;
11466 +
11467 +diff --git a/net/rxrpc/conn_object.c b/net/rxrpc/conn_object.c
11468 +index e1e83af47866..e7c89b978587 100644
11469 +--- a/net/rxrpc/conn_object.c
11470 ++++ b/net/rxrpc/conn_object.c
11471 +@@ -211,9 +211,8 @@ void rxrpc_disconnect_call(struct rxrpc_call *call)
11472 + __rxrpc_disconnect_call(conn, call);
11473 + spin_unlock(&conn->channel_lock);
11474 +
11475 +- call->conn = NULL;
11476 ++ set_bit(RXRPC_CALL_DISCONNECTED, &call->flags);
11477 + conn->idle_timestamp = jiffies;
11478 +- rxrpc_put_connection(conn);
11479 + }
11480 +
11481 + /*
11482 +diff --git a/net/rxrpc/input.c b/net/rxrpc/input.c
11483 +index a4380e182e6c..f0ccc6a04c7a 100644
11484 +--- a/net/rxrpc/input.c
11485 ++++ b/net/rxrpc/input.c
11486 +@@ -582,8 +582,7 @@ ack:
11487 + immediate_ack, true,
11488 + rxrpc_propose_ack_input_data);
11489 +
11490 +- if (sp->hdr.seq == READ_ONCE(call->rx_hard_ack) + 1)
11491 +- rxrpc_notify_socket(call);
11492 ++ rxrpc_notify_socket(call);
11493 + _leave(" [queued]");
11494 + }
11495 +
11496 +diff --git a/net/rxrpc/output.c b/net/rxrpc/output.c
11497 +index 64389f493bb2..d568c96f5262 100644
11498 +--- a/net/rxrpc/output.c
11499 ++++ b/net/rxrpc/output.c
11500 +@@ -96,7 +96,7 @@ static size_t rxrpc_fill_out_ack(struct rxrpc_call *call,
11501 + */
11502 + int rxrpc_send_ack_packet(struct rxrpc_call *call, bool ping)
11503 + {
11504 +- struct rxrpc_connection *conn = NULL;
11505 ++ struct rxrpc_connection *conn;
11506 + struct rxrpc_ack_buffer *pkt;
11507 + struct msghdr msg;
11508 + struct kvec iov[2];
11509 +@@ -106,18 +106,14 @@ int rxrpc_send_ack_packet(struct rxrpc_call *call, bool ping)
11510 + int ret;
11511 + u8 reason;
11512 +
11513 +- spin_lock_bh(&call->lock);
11514 +- if (call->conn)
11515 +- conn = rxrpc_get_connection_maybe(call->conn);
11516 +- spin_unlock_bh(&call->lock);
11517 +- if (!conn)
11518 ++ if (test_bit(RXRPC_CALL_DISCONNECTED, &call->flags))
11519 + return -ECONNRESET;
11520 +
11521 + pkt = kzalloc(sizeof(*pkt), GFP_KERNEL);
11522 +- if (!pkt) {
11523 +- rxrpc_put_connection(conn);
11524 ++ if (!pkt)
11525 + return -ENOMEM;
11526 +- }
11527 ++
11528 ++ conn = call->conn;
11529 +
11530 + msg.msg_name = &call->peer->srx.transport;
11531 + msg.msg_namelen = call->peer->srx.transport_len;
11532 +@@ -204,7 +200,6 @@ int rxrpc_send_ack_packet(struct rxrpc_call *call, bool ping)
11533 + }
11534 +
11535 + out:
11536 +- rxrpc_put_connection(conn);
11537 + kfree(pkt);
11538 + return ret;
11539 + }
11540 +@@ -214,20 +209,18 @@ out:
11541 + */
11542 + int rxrpc_send_abort_packet(struct rxrpc_call *call)
11543 + {
11544 +- struct rxrpc_connection *conn = NULL;
11545 ++ struct rxrpc_connection *conn;
11546 + struct rxrpc_abort_buffer pkt;
11547 + struct msghdr msg;
11548 + struct kvec iov[1];
11549 + rxrpc_serial_t serial;
11550 + int ret;
11551 +
11552 +- spin_lock_bh(&call->lock);
11553 +- if (call->conn)
11554 +- conn = rxrpc_get_connection_maybe(call->conn);
11555 +- spin_unlock_bh(&call->lock);
11556 +- if (!conn)
11557 ++ if (test_bit(RXRPC_CALL_DISCONNECTED, &call->flags))
11558 + return -ECONNRESET;
11559 +
11560 ++ conn = call->conn;
11561 ++
11562 + msg.msg_name = &call->peer->srx.transport;
11563 + msg.msg_namelen = call->peer->srx.transport_len;
11564 + msg.msg_control = NULL;
11565 +@@ -255,7 +248,6 @@ int rxrpc_send_abort_packet(struct rxrpc_call *call)
11566 + ret = kernel_sendmsg(conn->params.local->socket,
11567 + &msg, iov, 1, sizeof(pkt));
11568 +
11569 +- rxrpc_put_connection(conn);
11570 + return ret;
11571 + }
11572 +
11573 +diff --git a/net/sched/cls_rsvp.h b/net/sched/cls_rsvp.h
11574 +index 322438fb3ffc..8673f9817f91 100644
11575 +--- a/net/sched/cls_rsvp.h
11576 ++++ b/net/sched/cls_rsvp.h
11577 +@@ -455,10 +455,8 @@ static u32 gen_tunnel(struct rsvp_head *data)
11578 +
11579 + static const struct nla_policy rsvp_policy[TCA_RSVP_MAX + 1] = {
11580 + [TCA_RSVP_CLASSID] = { .type = NLA_U32 },
11581 +- [TCA_RSVP_DST] = { .type = NLA_BINARY,
11582 +- .len = RSVP_DST_LEN * sizeof(u32) },
11583 +- [TCA_RSVP_SRC] = { .type = NLA_BINARY,
11584 +- .len = RSVP_DST_LEN * sizeof(u32) },
11585 ++ [TCA_RSVP_DST] = { .len = RSVP_DST_LEN * sizeof(u32) },
11586 ++ [TCA_RSVP_SRC] = { .len = RSVP_DST_LEN * sizeof(u32) },
11587 + [TCA_RSVP_PINFO] = { .len = sizeof(struct tc_rsvp_pinfo) },
11588 + };
11589 +
11590 +diff --git a/net/sched/cls_tcindex.c b/net/sched/cls_tcindex.c
11591 +index db80a6440f37..3e1695b66e31 100644
11592 +--- a/net/sched/cls_tcindex.c
11593 ++++ b/net/sched/cls_tcindex.c
11594 +@@ -301,12 +301,31 @@ tcindex_set_parms(struct net *net, struct tcf_proto *tp, unsigned long base,
11595 + cp->fall_through = p->fall_through;
11596 + cp->tp = tp;
11597 +
11598 ++ if (tb[TCA_TCINDEX_HASH])
11599 ++ cp->hash = nla_get_u32(tb[TCA_TCINDEX_HASH]);
11600 ++
11601 ++ if (tb[TCA_TCINDEX_MASK])
11602 ++ cp->mask = nla_get_u16(tb[TCA_TCINDEX_MASK]);
11603 ++
11604 ++ if (tb[TCA_TCINDEX_SHIFT])
11605 ++ cp->shift = nla_get_u32(tb[TCA_TCINDEX_SHIFT]);
11606 ++
11607 ++ if (!cp->hash) {
11608 ++ /* Hash not specified, use perfect hash if the upper limit
11609 ++ * of the hashing index is below the threshold.
11610 ++ */
11611 ++ if ((cp->mask >> cp->shift) < PERFECT_HASH_THRESHOLD)
11612 ++ cp->hash = (cp->mask >> cp->shift) + 1;
11613 ++ else
11614 ++ cp->hash = DEFAULT_HASH_SIZE;
11615 ++ }
11616 ++
11617 + if (p->perfect) {
11618 + int i;
11619 +
11620 + if (tcindex_alloc_perfect_hash(cp) < 0)
11621 + goto errout;
11622 +- for (i = 0; i < cp->hash; i++)
11623 ++ for (i = 0; i < min(cp->hash, p->hash); i++)
11624 + cp->perfect[i].res = p->perfect[i].res;
11625 + balloc = 1;
11626 + }
11627 +@@ -321,15 +340,6 @@ tcindex_set_parms(struct net *net, struct tcf_proto *tp, unsigned long base,
11628 + if (old_r)
11629 + cr.res = r->res;
11630 +
11631 +- if (tb[TCA_TCINDEX_HASH])
11632 +- cp->hash = nla_get_u32(tb[TCA_TCINDEX_HASH]);
11633 +-
11634 +- if (tb[TCA_TCINDEX_MASK])
11635 +- cp->mask = nla_get_u16(tb[TCA_TCINDEX_MASK]);
11636 +-
11637 +- if (tb[TCA_TCINDEX_SHIFT])
11638 +- cp->shift = nla_get_u32(tb[TCA_TCINDEX_SHIFT]);
11639 +-
11640 + err = -EBUSY;
11641 +
11642 + /* Hash already allocated, make sure that we still meet the
11643 +@@ -347,16 +357,6 @@ tcindex_set_parms(struct net *net, struct tcf_proto *tp, unsigned long base,
11644 + if (tb[TCA_TCINDEX_FALL_THROUGH])
11645 + cp->fall_through = nla_get_u32(tb[TCA_TCINDEX_FALL_THROUGH]);
11646 +
11647 +- if (!cp->hash) {
11648 +- /* Hash not specified, use perfect hash if the upper limit
11649 +- * of the hashing index is below the threshold.
11650 +- */
11651 +- if ((cp->mask >> cp->shift) < PERFECT_HASH_THRESHOLD)
11652 +- cp->hash = (cp->mask >> cp->shift) + 1;
11653 +- else
11654 +- cp->hash = DEFAULT_HASH_SIZE;
11655 +- }
11656 +-
11657 + if (!cp->perfect && !cp->h)
11658 + cp->alloc_hash = cp->hash;
11659 +
11660 +diff --git a/net/sunrpc/auth_gss/svcauth_gss.c b/net/sunrpc/auth_gss/svcauth_gss.c
11661 +index b4b68c6e3f8b..d7775ca2fbb9 100644
11662 +--- a/net/sunrpc/auth_gss/svcauth_gss.c
11663 ++++ b/net/sunrpc/auth_gss/svcauth_gss.c
11664 +@@ -1180,6 +1180,7 @@ static int gss_proxy_save_rsc(struct cache_detail *cd,
11665 + dprintk("RPC: No creds found!\n");
11666 + goto out;
11667 + } else {
11668 ++ struct timespec64 boot;
11669 +
11670 + /* steal creds */
11671 + rsci.cred = ud->creds;
11672 +@@ -1200,6 +1201,9 @@ static int gss_proxy_save_rsc(struct cache_detail *cd,
11673 + &expiry, GFP_KERNEL);
11674 + if (status)
11675 + goto out;
11676 ++
11677 ++ getboottime64(&boot);
11678 ++ expiry -= boot.tv_sec;
11679 + }
11680 +
11681 + rsci.h.expiry_time = expiry;
11682 +diff --git a/sound/drivers/dummy.c b/sound/drivers/dummy.c
11683 +index 172dacd925f5..c182341c1714 100644
11684 +--- a/sound/drivers/dummy.c
11685 ++++ b/sound/drivers/dummy.c
11686 +@@ -925,7 +925,7 @@ static void print_formats(struct snd_dummy *dummy,
11687 + {
11688 + int i;
11689 +
11690 +- for (i = 0; i < SNDRV_PCM_FORMAT_LAST; i++) {
11691 ++ for (i = 0; i <= SNDRV_PCM_FORMAT_LAST; i++) {
11692 + if (dummy->pcm_hw.formats & (1ULL << i))
11693 + snd_iprintf(buffer, " %s", snd_pcm_format_name(i));
11694 + }
11695 +diff --git a/sound/soc/qcom/apq8016_sbc.c b/sound/soc/qcom/apq8016_sbc.c
11696 +index 754742018515..3d91cef3704a 100644
11697 +--- a/sound/soc/qcom/apq8016_sbc.c
11698 ++++ b/sound/soc/qcom/apq8016_sbc.c
11699 +@@ -128,7 +128,8 @@ static struct apq8016_sbc_data *apq8016_sbc_parse_of(struct snd_soc_card *card)
11700 + link->codec_of_node = of_parse_phandle(codec, "sound-dai", 0);
11701 + if (!link->codec_of_node) {
11702 + dev_err(card->dev, "error getting codec phandle\n");
11703 +- return ERR_PTR(-EINVAL);
11704 ++ ret = -EINVAL;
11705 ++ goto error;
11706 + }
11707 +
11708 + ret = snd_soc_of_get_dai_name(cpu, &link->cpu_dai_name);
11709 +diff --git a/sound/soc/soc-pcm.c b/sound/soc/soc-pcm.c
11710 +index 635b22fa1101..280bb5cab87f 100644
11711 +--- a/sound/soc/soc-pcm.c
11712 ++++ b/sound/soc/soc-pcm.c
11713 +@@ -2137,42 +2137,81 @@ int dpcm_be_dai_trigger(struct snd_soc_pcm_runtime *fe, int stream,
11714 + }
11715 + EXPORT_SYMBOL_GPL(dpcm_be_dai_trigger);
11716 +
11717 ++static int dpcm_dai_trigger_fe_be(struct snd_pcm_substream *substream,
11718 ++ int cmd, bool fe_first)
11719 ++{
11720 ++ struct snd_soc_pcm_runtime *fe = substream->private_data;
11721 ++ int ret;
11722 ++
11723 ++ /* call trigger on the frontend before the backend. */
11724 ++ if (fe_first) {
11725 ++ dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
11726 ++ fe->dai_link->name, cmd);
11727 ++
11728 ++ ret = soc_pcm_trigger(substream, cmd);
11729 ++ if (ret < 0)
11730 ++ return ret;
11731 ++
11732 ++ ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
11733 ++ return ret;
11734 ++ }
11735 ++
11736 ++ /* call trigger on the frontend after the backend. */
11737 ++ ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
11738 ++ if (ret < 0)
11739 ++ return ret;
11740 ++
11741 ++ dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
11742 ++ fe->dai_link->name, cmd);
11743 ++
11744 ++ ret = soc_pcm_trigger(substream, cmd);
11745 ++
11746 ++ return ret;
11747 ++}
11748 ++
11749 + static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
11750 + {
11751 + struct snd_soc_pcm_runtime *fe = substream->private_data;
11752 +- int stream = substream->stream, ret;
11753 ++ int stream = substream->stream;
11754 ++ int ret = 0;
11755 + enum snd_soc_dpcm_trigger trigger = fe->dai_link->trigger[stream];
11756 +
11757 + fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
11758 +
11759 + switch (trigger) {
11760 + case SND_SOC_DPCM_TRIGGER_PRE:
11761 +- /* call trigger on the frontend before the backend. */
11762 +-
11763 +- dev_dbg(fe->dev, "ASoC: pre trigger FE %s cmd %d\n",
11764 +- fe->dai_link->name, cmd);
11765 +-
11766 +- ret = soc_pcm_trigger(substream, cmd);
11767 +- if (ret < 0) {
11768 +- dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
11769 +- goto out;
11770 ++ switch (cmd) {
11771 ++ case SNDRV_PCM_TRIGGER_START:
11772 ++ case SNDRV_PCM_TRIGGER_RESUME:
11773 ++ case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
11774 ++ ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
11775 ++ break;
11776 ++ case SNDRV_PCM_TRIGGER_STOP:
11777 ++ case SNDRV_PCM_TRIGGER_SUSPEND:
11778 ++ case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
11779 ++ ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
11780 ++ break;
11781 ++ default:
11782 ++ ret = -EINVAL;
11783 ++ break;
11784 + }
11785 +-
11786 +- ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
11787 + break;
11788 + case SND_SOC_DPCM_TRIGGER_POST:
11789 +- /* call trigger on the frontend after the backend. */
11790 +-
11791 +- ret = dpcm_be_dai_trigger(fe, substream->stream, cmd);
11792 +- if (ret < 0) {
11793 +- dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
11794 +- goto out;
11795 ++ switch (cmd) {
11796 ++ case SNDRV_PCM_TRIGGER_START:
11797 ++ case SNDRV_PCM_TRIGGER_RESUME:
11798 ++ case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
11799 ++ ret = dpcm_dai_trigger_fe_be(substream, cmd, false);
11800 ++ break;
11801 ++ case SNDRV_PCM_TRIGGER_STOP:
11802 ++ case SNDRV_PCM_TRIGGER_SUSPEND:
11803 ++ case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
11804 ++ ret = dpcm_dai_trigger_fe_be(substream, cmd, true);
11805 ++ break;
11806 ++ default:
11807 ++ ret = -EINVAL;
11808 ++ break;
11809 + }
11810 +-
11811 +- dev_dbg(fe->dev, "ASoC: post trigger FE %s cmd %d\n",
11812 +- fe->dai_link->name, cmd);
11813 +-
11814 +- ret = soc_pcm_trigger(substream, cmd);
11815 + break;
11816 + case SND_SOC_DPCM_TRIGGER_BESPOKE:
11817 + /* bespoke trigger() - handles both FE and BEs */
11818 +@@ -2181,10 +2220,6 @@ static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
11819 + fe->dai_link->name, cmd);
11820 +
11821 + ret = soc_pcm_bespoke_trigger(substream, cmd);
11822 +- if (ret < 0) {
11823 +- dev_err(fe->dev,"ASoC: trigger FE failed %d\n", ret);
11824 +- goto out;
11825 +- }
11826 + break;
11827 + default:
11828 + dev_err(fe->dev, "ASoC: invalid trigger cmd %d for %s\n", cmd,
11829 +@@ -2193,6 +2228,12 @@ static int dpcm_fe_dai_do_trigger(struct snd_pcm_substream *substream, int cmd)
11830 + goto out;
11831 + }
11832 +
11833 ++ if (ret < 0) {
11834 ++ dev_err(fe->dev, "ASoC: trigger FE cmd: %d failed: %d\n",
11835 ++ cmd, ret);
11836 ++ goto out;
11837 ++ }
11838 ++
11839 + switch (cmd) {
11840 + case SNDRV_PCM_TRIGGER_START:
11841 + case SNDRV_PCM_TRIGGER_RESUME:
11842 +diff --git a/tools/power/acpi/Makefile.config b/tools/power/acpi/Makefile.config
11843 +index a1883bbb0144..fb5559f9819a 100644
11844 +--- a/tools/power/acpi/Makefile.config
11845 ++++ b/tools/power/acpi/Makefile.config
11846 +@@ -18,7 +18,7 @@ include $(srctree)/../../scripts/Makefile.include
11847 +
11848 + OUTPUT=$(srctree)/
11849 + ifeq ("$(origin O)", "command line")
11850 +- OUTPUT := $(O)/power/acpi/
11851 ++ OUTPUT := $(O)/tools/power/acpi/
11852 + endif
11853 + #$(info Determined 'OUTPUT' to be $(OUTPUT))
11854 +