This source file includes following definitions.
- kern_pcid
- user_pcid
- build_cr3
- build_cr3_noflush
- nmi_uaccess_okay
- cr4_init_shadow
- __cr4_set
- cr4_set_bits_irqsoff
- cr4_clear_bits_irqsoff
- cr4_set_bits
- cr4_clear_bits
- cr4_toggle_bits_irqsoff
- cr4_read_shadow
- invalidate_other_asid
- cr4_set_bits_and_update_boot
- invalidate_user_asid
- __native_flush_tlb
- __native_flush_tlb_global
- __native_flush_tlb_one_user
- __flush_tlb_all
- __flush_tlb_one_kernel
- flush_tlb_page
- inc_mm_tlb_gen
- arch_tlbbatch_add_mm
1
2 #ifndef _ASM_X86_TLBFLUSH_H
3 #define _ASM_X86_TLBFLUSH_H
4
5 #include <linux/mm.h>
6 #include <linux/sched.h>
7
8 #include <asm/processor.h>
9 #include <asm/cpufeature.h>
10 #include <asm/special_insns.h>
11 #include <asm/smp.h>
12 #include <asm/invpcid.h>
13 #include <asm/pti.h>
14 #include <asm/processor-flags.h>
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 #define CR3_HW_ASID_BITS 12
46
47
48
49
50
51 #ifdef CONFIG_PAGE_TABLE_ISOLATION
52 # define PTI_CONSUMED_PCID_BITS 1
53 #else
54 # define PTI_CONSUMED_PCID_BITS 0
55 #endif
56
57 #define CR3_AVAIL_PCID_BITS (X86_CR3_PCID_BITS - PTI_CONSUMED_PCID_BITS)
58
59
60
61
62
63
64 #define MAX_ASID_AVAILABLE ((1 << CR3_AVAIL_PCID_BITS) - 2)
65
66
67
68
69
70 #define TLB_NR_DYN_ASIDS 6
71
72
73
74
75 static inline u16 kern_pcid(u16 asid)
76 {
77 VM_WARN_ON_ONCE(asid > MAX_ASID_AVAILABLE);
78
79 #ifdef CONFIG_PAGE_TABLE_ISOLATION
80
81
82
83
84 BUILD_BUG_ON(TLB_NR_DYN_ASIDS >= (1 << X86_CR3_PTI_PCID_USER_BIT));
85
86
87
88
89
90 VM_WARN_ON_ONCE(asid & (1 << X86_CR3_PTI_PCID_USER_BIT));
91 #endif
92
93
94
95
96
97
98
99
100
101
102
103
104
105 return asid + 1;
106 }
107
108
109
110
111 static inline u16 user_pcid(u16 asid)
112 {
113 u16 ret = kern_pcid(asid);
114 #ifdef CONFIG_PAGE_TABLE_ISOLATION
115 ret |= 1 << X86_CR3_PTI_PCID_USER_BIT;
116 #endif
117 return ret;
118 }
119
120 struct pgd_t;
121 static inline unsigned long build_cr3(pgd_t *pgd, u16 asid)
122 {
123 if (static_cpu_has(X86_FEATURE_PCID)) {
124 return __sme_pa(pgd) | kern_pcid(asid);
125 } else {
126 VM_WARN_ON_ONCE(asid != 0);
127 return __sme_pa(pgd);
128 }
129 }
130
131 static inline unsigned long build_cr3_noflush(pgd_t *pgd, u16 asid)
132 {
133 VM_WARN_ON_ONCE(asid > MAX_ASID_AVAILABLE);
134
135
136
137
138
139 VM_WARN_ON_ONCE(!boot_cpu_has(X86_FEATURE_PCID));
140 return __sme_pa(pgd) | kern_pcid(asid) | CR3_NOFLUSH;
141 }
142
143 #ifdef CONFIG_PARAVIRT
144 #include <asm/paravirt.h>
145 #else
146 #define __flush_tlb() __native_flush_tlb()
147 #define __flush_tlb_global() __native_flush_tlb_global()
148 #define __flush_tlb_one_user(addr) __native_flush_tlb_one_user(addr)
149 #endif
150
151 struct tlb_context {
152 u64 ctx_id;
153 u64 tlb_gen;
154 };
155
156 struct tlb_state {
157
158
159
160
161
162
163
164
165
166
167
168 struct mm_struct *loaded_mm;
169
170 #define LOADED_MM_SWITCHING ((struct mm_struct *)1UL)
171
172
173 union {
174 struct mm_struct *last_user_mm;
175 unsigned long last_user_mm_ibpb;
176 };
177
178 u16 loaded_mm_asid;
179 u16 next_asid;
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196 bool is_lazy;
197
198
199
200
201
202
203
204
205
206
207 bool invalidate_other;
208
209
210
211
212
213
214 unsigned short user_pcid_flush_mask;
215
216
217
218
219
220 unsigned long cr4;
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241 struct tlb_context ctxs[TLB_NR_DYN_ASIDS];
242 };
243 DECLARE_PER_CPU_SHARED_ALIGNED(struct tlb_state, cpu_tlbstate);
244
245
246
247
248
249
250
251
252 static inline bool nmi_uaccess_okay(void)
253 {
254 struct mm_struct *loaded_mm = this_cpu_read(cpu_tlbstate.loaded_mm);
255 struct mm_struct *current_mm = current->mm;
256
257 VM_WARN_ON_ONCE(!loaded_mm);
258
259
260
261
262
263
264
265
266
267
268
269 if (loaded_mm != current_mm)
270 return false;
271
272 VM_WARN_ON_ONCE(current_mm->pgd != __va(read_cr3_pa()));
273
274 return true;
275 }
276
277 #define nmi_uaccess_okay nmi_uaccess_okay
278
279
280 static inline void cr4_init_shadow(void)
281 {
282 this_cpu_write(cpu_tlbstate.cr4, __read_cr4());
283 }
284
285 static inline void __cr4_set(unsigned long cr4)
286 {
287 lockdep_assert_irqs_disabled();
288 this_cpu_write(cpu_tlbstate.cr4, cr4);
289 __write_cr4(cr4);
290 }
291
292
293 static inline void cr4_set_bits_irqsoff(unsigned long mask)
294 {
295 unsigned long cr4;
296
297 cr4 = this_cpu_read(cpu_tlbstate.cr4);
298 if ((cr4 | mask) != cr4)
299 __cr4_set(cr4 | mask);
300 }
301
302
303 static inline void cr4_clear_bits_irqsoff(unsigned long mask)
304 {
305 unsigned long cr4;
306
307 cr4 = this_cpu_read(cpu_tlbstate.cr4);
308 if ((cr4 & ~mask) != cr4)
309 __cr4_set(cr4 & ~mask);
310 }
311
312
313 static inline void cr4_set_bits(unsigned long mask)
314 {
315 unsigned long flags;
316
317 local_irq_save(flags);
318 cr4_set_bits_irqsoff(mask);
319 local_irq_restore(flags);
320 }
321
322
323 static inline void cr4_clear_bits(unsigned long mask)
324 {
325 unsigned long flags;
326
327 local_irq_save(flags);
328 cr4_clear_bits_irqsoff(mask);
329 local_irq_restore(flags);
330 }
331
332 static inline void cr4_toggle_bits_irqsoff(unsigned long mask)
333 {
334 unsigned long cr4;
335
336 cr4 = this_cpu_read(cpu_tlbstate.cr4);
337 __cr4_set(cr4 ^ mask);
338 }
339
340
341 static inline unsigned long cr4_read_shadow(void)
342 {
343 return this_cpu_read(cpu_tlbstate.cr4);
344 }
345
346
347
348
349 static inline void invalidate_other_asid(void)
350 {
351 this_cpu_write(cpu_tlbstate.invalidate_other, true);
352 }
353
354
355
356
357
358
359
360 extern unsigned long mmu_cr4_features;
361 extern u32 *trampoline_cr4_features;
362
363 static inline void cr4_set_bits_and_update_boot(unsigned long mask)
364 {
365 mmu_cr4_features |= mask;
366 if (trampoline_cr4_features)
367 *trampoline_cr4_features = mmu_cr4_features;
368 cr4_set_bits(mask);
369 }
370
371 extern void initialize_tlbstate_and_flush(void);
372
373
374
375
376
377
378
379 static inline void invalidate_user_asid(u16 asid)
380 {
381
382 if (!IS_ENABLED(CONFIG_PAGE_TABLE_ISOLATION))
383 return;
384
385
386
387
388
389 if (!cpu_feature_enabled(X86_FEATURE_PCID))
390 return;
391
392 if (!static_cpu_has(X86_FEATURE_PTI))
393 return;
394
395 __set_bit(kern_pcid(asid),
396 (unsigned long *)this_cpu_ptr(&cpu_tlbstate.user_pcid_flush_mask));
397 }
398
399
400
401
402 static inline void __native_flush_tlb(void)
403 {
404
405
406
407
408
409 WARN_ON_ONCE(preemptible());
410
411 invalidate_user_asid(this_cpu_read(cpu_tlbstate.loaded_mm_asid));
412
413
414 native_write_cr3(__native_read_cr3());
415 }
416
417
418
419
420 static inline void __native_flush_tlb_global(void)
421 {
422 unsigned long cr4, flags;
423
424 if (static_cpu_has(X86_FEATURE_INVPCID)) {
425
426
427
428
429
430
431 invpcid_flush_all();
432 return;
433 }
434
435
436
437
438
439
440 raw_local_irq_save(flags);
441
442 cr4 = this_cpu_read(cpu_tlbstate.cr4);
443
444 native_write_cr4(cr4 ^ X86_CR4_PGE);
445
446 native_write_cr4(cr4);
447
448 raw_local_irq_restore(flags);
449 }
450
451
452
453
454 static inline void __native_flush_tlb_one_user(unsigned long addr)
455 {
456 u32 loaded_mm_asid = this_cpu_read(cpu_tlbstate.loaded_mm_asid);
457
458 asm volatile("invlpg (%0)" ::"r" (addr) : "memory");
459
460 if (!static_cpu_has(X86_FEATURE_PTI))
461 return;
462
463
464
465
466
467 if (!this_cpu_has(X86_FEATURE_INVPCID_SINGLE))
468 invalidate_user_asid(loaded_mm_asid);
469 else
470 invpcid_flush_one(user_pcid(loaded_mm_asid), addr);
471 }
472
473
474
475
476 static inline void __flush_tlb_all(void)
477 {
478
479
480
481
482 VM_WARN_ON_ONCE(preemptible());
483
484 if (boot_cpu_has(X86_FEATURE_PGE)) {
485 __flush_tlb_global();
486 } else {
487
488
489
490 __flush_tlb();
491 }
492 }
493
494
495
496
497 static inline void __flush_tlb_one_kernel(unsigned long addr)
498 {
499 count_vm_tlb_event(NR_TLB_LOCAL_FLUSH_ONE);
500
501
502
503
504
505
506
507
508
509
510
511
512 __flush_tlb_one_user(addr);
513
514 if (!static_cpu_has(X86_FEATURE_PTI))
515 return;
516
517
518
519
520
521
522
523 invalidate_other_asid();
524 }
525
526 #define TLB_FLUSH_ALL -1UL
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541 struct flush_tlb_info {
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558 struct mm_struct *mm;
559 unsigned long start;
560 unsigned long end;
561 u64 new_tlb_gen;
562 unsigned int stride_shift;
563 bool freed_tables;
564 };
565
566 #define local_flush_tlb() __flush_tlb()
567
568 #define flush_tlb_mm(mm) \
569 flush_tlb_mm_range(mm, 0UL, TLB_FLUSH_ALL, 0UL, true)
570
571 #define flush_tlb_range(vma, start, end) \
572 flush_tlb_mm_range((vma)->vm_mm, start, end, \
573 ((vma)->vm_flags & VM_HUGETLB) \
574 ? huge_page_shift(hstate_vma(vma)) \
575 : PAGE_SHIFT, false)
576
577 extern void flush_tlb_all(void);
578 extern void flush_tlb_mm_range(struct mm_struct *mm, unsigned long start,
579 unsigned long end, unsigned int stride_shift,
580 bool freed_tables);
581 extern void flush_tlb_kernel_range(unsigned long start, unsigned long end);
582
583 static inline void flush_tlb_page(struct vm_area_struct *vma, unsigned long a)
584 {
585 flush_tlb_mm_range(vma->vm_mm, a, a + PAGE_SIZE, PAGE_SHIFT, false);
586 }
587
588 void native_flush_tlb_others(const struct cpumask *cpumask,
589 const struct flush_tlb_info *info);
590
591 static inline u64 inc_mm_tlb_gen(struct mm_struct *mm)
592 {
593
594
595
596
597
598
599 return atomic64_inc_return(&mm->context.tlb_gen);
600 }
601
602 static inline void arch_tlbbatch_add_mm(struct arch_tlbflush_unmap_batch *batch,
603 struct mm_struct *mm)
604 {
605 inc_mm_tlb_gen(mm);
606 cpumask_or(&batch->cpumask, &batch->cpumask, mm_cpumask(mm));
607 }
608
609 extern void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch);
610
611 #ifndef CONFIG_PARAVIRT
612 #define flush_tlb_others(mask, info) \
613 native_flush_tlb_others(mask, info)
614
615 #define paravirt_tlb_remove_table(tlb, page) \
616 tlb_remove_page(tlb, (void *)(page))
617 #endif
618
619 #endif