2 * Copyright (c) 2018-2019 The FreeBSD Foundation
3 * Copyright (c) 2003 Peter Wemm.
4 * Copyright (c) 1993 The Regents of the University of California.
7 * Portions of this software were developed by
8 * Konstantin Belousov <kib@FreeBSD.org> under sponsorship from
9 * the FreeBSD Foundation.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 #include <machine/asmacros.h>
41 #include <machine/specialreg.h>
42 #include <machine/pmap.h>
51 movl $PAGE_SIZE/8,%ecx
70 * pagecopy(%rdi=from, %rsi=to)
74 movl $PAGE_SIZE/8,%ecx
92 * The loop takes 29 bytes. Ensure that it doesn't cross a 32-byte
97 movnti %rax,(%rdi,%rdx)
98 movnti %rax,8(%rdi,%rdx)
99 movnti %rax,16(%rdi,%rdx)
100 movnti %rax,24(%rdi,%rdx)
109 * memcmpy(b1, b2, len)
121 movzbl (%rdi,%rcx,1),%eax
122 movzbl (%rsi,%rcx,1),%r8d
128 movzbl (%rdi,%rcx,1),%eax
129 movzbl (%rsi,%rcx,1),%r8d
135 movzbl (%rdi,%rcx,1),%eax
136 movzbl (%rsi,%rcx,1),%r8d
142 movzbl (%rdi,%rcx,1),%eax
143 movzbl (%rsi,%rcx,1),%r8d
203 * memmove(dst, src, cnt)
208 * Register state at entry is supposed to be as follows:
213 * The macro possibly clobbers the above and: rcx, r8, r9, r10
214 * It does not clobber rax nor r11.
216 .macro MEMMOVE erms overlap begin end
220 * For sizes 0..32 all data is read before it is written, so there
221 * is no correctness issue with direction of copying.
229 cmpq %rcx,%r8 /* overlapping && src < dst? */
260 movq -16(%rsi,%rcx),%r9
261 movq -8(%rsi,%rcx),%r10
264 movq %r9,-16(%rdi,%rcx)
265 movq %r10,-8(%rdi,%rcx)
273 movq -8(%rsi,%rcx),%r8
275 movq %r8,-8(%rdi,%rcx,)
283 movl -4(%rsi,%rcx),%r8d
285 movl %r8d,-4(%rdi,%rcx)
293 movzwl -2(%rsi,%rcx),%r8d
295 movw %r8w,-2(%rdi,%rcx)
316 shrq $3,%rcx /* copy by 64-bit words */
320 andl $7,%ecx /* any bytes left? */
331 leaq -16(%rdx,%rcx),%rdx
333 leaq 16(%rdi,%rcx),%rdi
334 leaq 16(%rsi,%rcx),%rsi
342 shrq $3,%rcx /* copy by 64-bit words */
348 andl $7,%ecx /* any bytes left? */
363 leaq -8(%rdi,%rcx),%rdi
364 leaq -8(%rsi,%rcx),%rsi
438 leaq -1(%rdi,%rcx),%rdi
439 leaq -1(%rsi,%rcx),%rsi
444 leaq -8(%rdi,%rcx),%rdi
445 leaq -8(%rsi,%rcx),%rsi
470 MEMMOVE erms=0 overlap=1 begin=MEMMOVE_BEGIN end=MEMMOVE_END
474 MEMMOVE erms=1 overlap=1 begin=MEMMOVE_BEGIN end=MEMMOVE_END
478 * memcpy(dst, src, len)
481 * Note: memcpy does not support overlapping copies
484 MEMMOVE erms=0 overlap=0 begin=MEMMOVE_BEGIN end=MEMMOVE_END
488 MEMMOVE erms=1 overlap=0 begin=MEMMOVE_BEGIN end=MEMMOVE_END
492 * memset(dst, c, len)
500 movabs $0x0101010101010101,%r10
520 movq %r10,-16(%rdi,%rcx)
521 movq %r10,-8(%rdi,%rcx)
531 movq %r10,-16(%rdi,%rcx)
532 movq %r10,-8(%rdi,%rcx)
540 movq %r10,-8(%rdi,%rcx)
548 movl %r10d,-4(%rdi,%rcx)
556 movw %r10w,-2(%rdi,%rcx)
589 movq %r10,-8(%rdi,%rdx)
599 leaq -16(%rcx,%r8),%rcx
601 leaq 16(%rdi,%r8),%rdi
613 /* fillw(pat, base, cnt) */
614 /* %rdi,%rsi, %rdx */
626 /*****************************************************************************/
627 /* copyout and fubyte family */
628 /*****************************************************************************/
630 * Access user memory from inside the kernel. These routines should be
631 * the only places that do this.
633 * These routines set curpcb->pcb_onfault for the time they execute. When a
634 * protection violation occurs inside the functions, the trap handler
635 * returns to *curpcb->pcb_onfault instead of the function.
638 .macro SMAP_DISABLE smap
645 .macro SMAP_ENABLE smap
651 .macro COPYINOUT_BEGIN
655 movq %rax,PCB_ONFAULT(%r11)
659 .macro COPYINOUT_SMAP_END
665 * copyout(from_kernel, to_user, len)
668 .macro COPYOUT smap erms
670 movq PCPU(CURPCB),%r11
671 movq $copy_fault,PCB_ONFAULT(%r11)
674 * Check explicitly for non-user addresses. If 486 write protection
675 * is being used, this check is essential because we are in kernel
676 * mode so the h/w does not provide any protection against writing
681 * First, prevent address wrapping.
687 * XXX STOP USING VM_MAXUSER_ADDRESS.
688 * It is an end address, not a max, so every time it is used correctly it
689 * looks like there is an off by one error, and of course it caused an off
690 * by one error in several places.
692 movq $VM_MAXUSER_ADDRESS,%rcx
697 * Set return value to zero. Remaining failure mode goes through
703 * Set up arguments for MEMMOVE.
713 MEMMOVE erms=\erms overlap=0 begin=COPYINOUT_BEGIN end=COPYINOUT_SMAP_END
715 MEMMOVE erms=\erms overlap=0 begin=COPYINOUT_BEGIN end=COPYINOUT_END
720 ENTRY(copyout_nosmap_std)
721 COPYOUT smap=0 erms=0
722 END(copyout_nosmap_std)
724 ENTRY(copyout_smap_std)
725 COPYOUT smap=1 erms=0
726 END(copyout_smap_std)
728 ENTRY(copyout_nosmap_erms)
729 COPYOUT smap=0 erms=1
730 END(copyout_nosmap_erms)
732 ENTRY(copyout_smap_erms)
733 COPYOUT smap=1 erms=1
734 END(copyout_smap_erms)
737 * copyin(from_user, to_kernel, len)
740 .macro COPYIN smap erms
742 movq PCPU(CURPCB),%r11
743 movq $copy_fault,PCB_ONFAULT(%r11)
746 * make sure address is valid
751 movq $VM_MAXUSER_ADDRESS,%rcx
764 MEMMOVE erms=\erms overlap=0 begin=COPYINOUT_BEGIN end=COPYINOUT_SMAP_END
766 MEMMOVE erms=\erms overlap=0 begin=COPYINOUT_BEGIN end=COPYINOUT_END
771 ENTRY(copyin_nosmap_std)
773 END(copyin_nosmap_std)
775 ENTRY(copyin_smap_std)
779 ENTRY(copyin_nosmap_erms)
781 END(copyin_nosmap_erms)
783 ENTRY(copyin_smap_erms)
785 END(copyin_smap_erms)
788 /* Trap entry clears PSL.AC */
790 movq $0,PCB_ONFAULT(%r11)
796 * casueword32. Compare and set user integer. Returns -1 on fault,
797 * 0 if access was successful. Old value is written to *oldp.
798 * dst = %rdi, old = %esi, oldp = %rdx, new = %ecx
800 ENTRY(casueword32_nosmap)
802 movq PCPU(CURPCB),%r8
803 movq $fusufault,PCB_ONFAULT(%r8)
805 movq $VM_MAXUSER_ADDRESS-4,%rax
806 cmpq %rax,%rdi /* verify address is valid */
809 movl %esi,%eax /* old */
813 cmpxchgl %ecx,(%rdi) /* new = %ecx */
817 * The old value is in %eax. If the store succeeded it will be the
818 * value we expected (old) from before the store, otherwise it will
819 * be the current value. Save %eax into %esi to prepare the return
824 movq %rax,PCB_ONFAULT(%r8)
827 * Access the oldp after the pcb_onfault is cleared, to correctly
828 * catch corrupted pointer.
830 movl %esi,(%rdx) /* oldp = %rdx */
834 END(casueword32_nosmap)
836 ENTRY(casueword32_smap)
838 movq PCPU(CURPCB),%r8
839 movq $fusufault,PCB_ONFAULT(%r8)
841 movq $VM_MAXUSER_ADDRESS-4,%rax
842 cmpq %rax,%rdi /* verify address is valid */
845 movl %esi,%eax /* old */
850 cmpxchgl %ecx,(%rdi) /* new = %ecx */
855 * The old value is in %eax. If the store succeeded it will be the
856 * value we expected (old) from before the store, otherwise it will
857 * be the current value. Save %eax into %esi to prepare the return
862 movq %rax,PCB_ONFAULT(%r8)
865 * Access the oldp after the pcb_onfault is cleared, to correctly
866 * catch corrupted pointer.
868 movl %esi,(%rdx) /* oldp = %rdx */
872 END(casueword32_smap)
875 * casueword. Compare and set user long. Returns -1 on fault,
876 * 0 if access was successful. Old value is written to *oldp.
877 * dst = %rdi, old = %rsi, oldp = %rdx, new = %rcx
879 ENTRY(casueword_nosmap)
881 movq PCPU(CURPCB),%r8
882 movq $fusufault,PCB_ONFAULT(%r8)
884 movq $VM_MAXUSER_ADDRESS-4,%rax
885 cmpq %rax,%rdi /* verify address is valid */
888 movq %rsi,%rax /* old */
892 cmpxchgq %rcx,(%rdi) /* new = %rcx */
896 * The old value is in %rax. If the store succeeded it will be the
897 * value we expected (old) from before the store, otherwise it will
898 * be the current value.
902 movq %rax,PCB_ONFAULT(%r8)
907 END(casueword_nosmap)
909 ENTRY(casueword_smap)
911 movq PCPU(CURPCB),%r8
912 movq $fusufault,PCB_ONFAULT(%r8)
914 movq $VM_MAXUSER_ADDRESS-4,%rax
915 cmpq %rax,%rdi /* verify address is valid */
918 movq %rsi,%rax /* old */
923 cmpxchgq %rcx,(%rdi) /* new = %rcx */
928 * The old value is in %rax. If the store succeeded it will be the
929 * value we expected (old) from before the store, otherwise it will
930 * be the current value.
934 movq %rax,PCB_ONFAULT(%r8)
942 * Fetch (load) a 64-bit word, a 32-bit word, a 16-bit word, or an 8-bit
943 * byte from user memory.
944 * addr = %rdi, valp = %rsi
947 ENTRY(fueword_nosmap)
949 movq PCPU(CURPCB),%rcx
950 movq $fusufault,PCB_ONFAULT(%rcx)
952 movq $VM_MAXUSER_ADDRESS-8,%rax
953 cmpq %rax,%rdi /* verify address is valid */
958 movq %rax,PCB_ONFAULT(%rcx)
966 movq PCPU(CURPCB),%rcx
967 movq $fusufault,PCB_ONFAULT(%rcx)
969 movq $VM_MAXUSER_ADDRESS-8,%rax
970 cmpq %rax,%rdi /* verify address is valid */
977 movq %rax,PCB_ONFAULT(%rcx)
983 ENTRY(fueword32_nosmap)
985 movq PCPU(CURPCB),%rcx
986 movq $fusufault,PCB_ONFAULT(%rcx)
988 movq $VM_MAXUSER_ADDRESS-4,%rax
989 cmpq %rax,%rdi /* verify address is valid */
994 movq %rax,PCB_ONFAULT(%rcx)
998 END(fueword32_nosmap)
1000 ENTRY(fueword32_smap)
1002 movq PCPU(CURPCB),%rcx
1003 movq $fusufault,PCB_ONFAULT(%rcx)
1005 movq $VM_MAXUSER_ADDRESS-4,%rax
1006 cmpq %rax,%rdi /* verify address is valid */
1013 movq %rax,PCB_ONFAULT(%rcx)
1019 ENTRY(fuword16_nosmap)
1021 movq PCPU(CURPCB),%rcx
1022 movq $fusufault,PCB_ONFAULT(%rcx)
1024 movq $VM_MAXUSER_ADDRESS-2,%rax
1029 movq $0,PCB_ONFAULT(%rcx)
1032 END(fuword16_nosmap)
1034 ENTRY(fuword16_smap)
1036 movq PCPU(CURPCB),%rcx
1037 movq $fusufault,PCB_ONFAULT(%rcx)
1039 movq $VM_MAXUSER_ADDRESS-2,%rax
1046 movq $0,PCB_ONFAULT(%rcx)
1051 ENTRY(fubyte_nosmap)
1053 movq PCPU(CURPCB),%rcx
1054 movq $fusufault,PCB_ONFAULT(%rcx)
1056 movq $VM_MAXUSER_ADDRESS-1,%rax
1061 movq $0,PCB_ONFAULT(%rcx)
1068 movq PCPU(CURPCB),%rcx
1069 movq $fusufault,PCB_ONFAULT(%rcx)
1071 movq $VM_MAXUSER_ADDRESS-1,%rax
1078 movq $0,PCB_ONFAULT(%rcx)
1084 * Store a 64-bit word, a 32-bit word, a 16-bit word, or an 8-bit byte to
1086 * addr = %rdi, value = %rsi
1088 ENTRY(suword_nosmap)
1090 movq PCPU(CURPCB),%rcx
1091 movq $fusufault,PCB_ONFAULT(%rcx)
1093 movq $VM_MAXUSER_ADDRESS-8,%rax
1094 cmpq %rax,%rdi /* verify address validity */
1099 movq %rax,PCB_ONFAULT(%rcx)
1106 movq PCPU(CURPCB),%rcx
1107 movq $fusufault,PCB_ONFAULT(%rcx)
1109 movq $VM_MAXUSER_ADDRESS-8,%rax
1110 cmpq %rax,%rdi /* verify address validity */
1117 movq %rax,PCB_ONFAULT(%rcx)
1122 ENTRY(suword32_nosmap)
1124 movq PCPU(CURPCB),%rcx
1125 movq $fusufault,PCB_ONFAULT(%rcx)
1127 movq $VM_MAXUSER_ADDRESS-4,%rax
1128 cmpq %rax,%rdi /* verify address validity */
1133 movq %rax,PCB_ONFAULT(%rcx)
1136 END(suword32_nosmap)
1138 ENTRY(suword32_smap)
1140 movq PCPU(CURPCB),%rcx
1141 movq $fusufault,PCB_ONFAULT(%rcx)
1143 movq $VM_MAXUSER_ADDRESS-4,%rax
1144 cmpq %rax,%rdi /* verify address validity */
1151 movq %rax,PCB_ONFAULT(%rcx)
1156 ENTRY(suword16_nosmap)
1158 movq PCPU(CURPCB),%rcx
1159 movq $fusufault,PCB_ONFAULT(%rcx)
1161 movq $VM_MAXUSER_ADDRESS-2,%rax
1162 cmpq %rax,%rdi /* verify address validity */
1167 movq %rax,PCB_ONFAULT(%rcx)
1170 END(suword16_nosmap)
1172 ENTRY(suword16_smap)
1174 movq PCPU(CURPCB),%rcx
1175 movq $fusufault,PCB_ONFAULT(%rcx)
1177 movq $VM_MAXUSER_ADDRESS-2,%rax
1178 cmpq %rax,%rdi /* verify address validity */
1185 movq %rax,PCB_ONFAULT(%rcx)
1190 ENTRY(subyte_nosmap)
1192 movq PCPU(CURPCB),%rcx
1193 movq $fusufault,PCB_ONFAULT(%rcx)
1195 movq $VM_MAXUSER_ADDRESS-1,%rax
1196 cmpq %rax,%rdi /* verify address validity */
1202 movq %rax,PCB_ONFAULT(%rcx)
1209 movq PCPU(CURPCB),%rcx
1210 movq $fusufault,PCB_ONFAULT(%rcx)
1212 movq $VM_MAXUSER_ADDRESS-1,%rax
1213 cmpq %rax,%rdi /* verify address validity */
1221 movq %rax,PCB_ONFAULT(%rcx)
1227 /* Fault entry clears PSL.AC */
1229 movq PCPU(CURPCB),%rcx
1231 movq %rax,PCB_ONFAULT(%rcx)
1237 * copyinstr(from, to, maxlen, int *lencopied)
1238 * %rdi, %rsi, %rdx, %rcx
1240 * copy a string from 'from' to 'to', stop when a 0 character is reached.
1241 * return ENAMETOOLONG if string is longer than maxlen, and
1242 * EFAULT on protection violations. If lencopied is non-zero,
1243 * return the actual length in *lencopied.
1245 .macro COPYINSTR smap
1247 movq %rdx,%r8 /* %r8 = maxlen */
1248 movq PCPU(CURPCB),%r9
1249 movq $cpystrflt,PCB_ONFAULT(%r9)
1251 movq $VM_MAXUSER_ADDRESS,%rax
1253 /* make sure 'from' is within bounds */
1259 /* restrict maxlen to <= VM_MAXUSER_ADDRESS-from */
1267 jz copyinstr_toolong
1269 jz copyinstr_toolong_smap
1281 /* Success -- 0 byte reached */
1285 /* set *lencopied and return %eax */
1286 movq %rax,PCB_ONFAULT(%r9)
1303 ENTRY(copyinstr_nosmap)
1305 END(copyinstr_nosmap)
1307 ENTRY(copyinstr_smap)
1312 /* Fault entry clears PSL.AC */
1315 /* set *lencopied and return %eax */
1316 movq $0,PCB_ONFAULT(%r9)
1326 copyinstr_toolong_smap:
1329 /* rdx is zero - return ENAMETOOLONG or EFAULT */
1330 movq $VM_MAXUSER_ADDRESS,%rax
1333 movl $ENAMETOOLONG,%eax
1337 * copystr(from, to, maxlen, int *lencopied)
1338 * %rdi, %rsi, %rdx, %rcx
1342 movq %rdx,%r8 /* %r8 = maxlen */
1355 /* Success -- 0 byte reached */
1361 /* set *lencopied and return %rax */
1368 /* rdx is zero -- return ENAMETOOLONG */
1369 movl $ENAMETOOLONG,%eax
1374 * Handling of special amd64 registers and descriptor tables etc
1376 /* void lgdt(struct region_descriptor *rdp); */
1378 /* reload the descriptor table */
1381 /* flush the prefetch q */
1388 movl %eax,%fs /* Beware, use wrmsr to set 64 bit base */
1392 /* reload code selector by turning return into intersegmental return */
1400 /*****************************************************************************/
1401 /* setjump, longjump */
1402 /*****************************************************************************/
1405 movq %rbx,0(%rdi) /* save rbx */
1406 movq %rsp,8(%rdi) /* save rsp */
1407 movq %rbp,16(%rdi) /* save rbp */
1408 movq %r12,24(%rdi) /* save r12 */
1409 movq %r13,32(%rdi) /* save r13 */
1410 movq %r14,40(%rdi) /* save r14 */
1411 movq %r15,48(%rdi) /* save r15 */
1412 movq 0(%rsp),%rdx /* get rta */
1413 movq %rdx,56(%rdi) /* save rip */
1414 xorl %eax,%eax /* return(0); */
1419 movq 0(%rdi),%rbx /* restore rbx */
1420 movq 8(%rdi),%rsp /* restore rsp */
1421 movq 16(%rdi),%rbp /* restore rbp */
1422 movq 24(%rdi),%r12 /* restore r12 */
1423 movq 32(%rdi),%r13 /* restore r13 */
1424 movq 40(%rdi),%r14 /* restore r14 */
1425 movq 48(%rdi),%r15 /* restore r15 */
1426 movq 56(%rdi),%rdx /* get rta */
1427 movq %rdx,0(%rsp) /* put in return frame */
1428 xorl %eax,%eax /* return(1); */
1434 * Support for reading MSRs in the safe manner. (Instead of panic on #gp,
1438 /* int rdmsr_safe(u_int msr, uint64_t *data) */
1440 movq PCPU(CURPCB),%r8
1441 movq $msr_onfault,PCB_ONFAULT(%r8)
1443 rdmsr /* Read MSR pointed by %ecx. Returns
1444 hi byte in edx, lo in %eax */
1445 salq $32,%rdx /* sign-shift %rdx left */
1446 movl %eax,%eax /* zero-extend %eax -> %rax */
1450 movq %rax,PCB_ONFAULT(%r8)
1455 * Support for writing MSRs in the safe manner. (Instead of panic on #gp,
1459 /* int wrmsr_safe(u_int msr, uint64_t data) */
1461 movq PCPU(CURPCB),%r8
1462 movq $msr_onfault,PCB_ONFAULT(%r8)
1467 wrmsr /* Write MSR pointed by %ecx. Accepts
1468 hi byte in edx, lo in %eax. */
1470 movq %rax,PCB_ONFAULT(%r8)
1475 * MSR operations fault handler
1479 movq $0,PCB_ONFAULT(%r8)
1485 * void pmap_pti_pcid_invalidate(uint64_t ucr3, uint64_t kcr3);
1486 * Invalidates address space addressed by ucr3, then returns to kcr3.
1487 * Done in assembler to ensure no other memory accesses happen while
1491 ENTRY(pmap_pti_pcid_invalidate)
1494 movq %rdi,%cr3 /* to user page table */
1495 movq %rsi,%cr3 /* back to kernel */
1500 * void pmap_pti_pcid_invlpg(uint64_t ucr3, uint64_t kcr3, vm_offset_t va);
1501 * Invalidates virtual address va in address space ucr3, then returns to kcr3.
1504 ENTRY(pmap_pti_pcid_invlpg)
1507 movq %rdi,%cr3 /* to user page table */
1509 movq %rsi,%cr3 /* back to kernel */
1514 * void pmap_pti_pcid_invlrng(uint64_t ucr3, uint64_t kcr3, vm_offset_t sva,
1516 * Invalidates virtual addresses between sva and eva in address space ucr3,
1517 * then returns to kcr3.
1520 ENTRY(pmap_pti_pcid_invlrng)
1523 movq %rdi,%cr3 /* to user page table */
1525 addq $PAGE_SIZE,%rdx
1528 movq %rsi,%cr3 /* back to kernel */
1533 .macro ibrs_seq_label l
1536 .macro ibrs_call_label l
1539 .macro ibrs_seq count
1542 ibrs_call_label %(ll)
1544 ibrs_seq_label %(ll)
1550 /* all callers already saved %rax, %rdx, and %rcx */
1551 ENTRY(handle_ibrs_entry)
1552 cmpb $0,hw_ibrs_active(%rip)
1554 movl $MSR_IA32_SPEC_CTRL,%ecx
1556 orl $(IA32_SPEC_CTRL_IBRS|IA32_SPEC_CTRL_STIBP),%eax
1557 orl $(IA32_SPEC_CTRL_IBRS|IA32_SPEC_CTRL_STIBP)>>32,%edx
1559 movb $1,PCPU(IBPB_SET)
1560 testl $CPUID_STDEXT_SMEP,cpu_stdext_feature(%rip)
1564 END(handle_ibrs_entry)
1566 ENTRY(handle_ibrs_exit)
1567 cmpb $0,PCPU(IBPB_SET)
1569 movl $MSR_IA32_SPEC_CTRL,%ecx
1571 andl $~(IA32_SPEC_CTRL_IBRS|IA32_SPEC_CTRL_STIBP),%eax
1572 andl $~((IA32_SPEC_CTRL_IBRS|IA32_SPEC_CTRL_STIBP)>>32),%edx
1574 movb $0,PCPU(IBPB_SET)
1576 END(handle_ibrs_exit)
1578 /* registers-neutral version, but needs stack */
1579 ENTRY(handle_ibrs_exit_rs)
1580 cmpb $0,PCPU(IBPB_SET)
1585 movl $MSR_IA32_SPEC_CTRL,%ecx
1587 andl $~(IA32_SPEC_CTRL_IBRS|IA32_SPEC_CTRL_STIBP),%eax
1588 andl $~((IA32_SPEC_CTRL_IBRS|IA32_SPEC_CTRL_STIBP)>>32),%edx
1593 movb $0,PCPU(IBPB_SET)
1595 END(handle_ibrs_exit_rs)
1600 * Flush L1D cache. Load enough of the data from the kernel text
1601 * to flush existing L1D content.
1603 * N.B. The function does not follow ABI calling conventions, it corrupts %rbx.
1604 * The vmm.ko caller expects that only %rax, %rdx, %rbx, %rcx, %r9, and %rflags
1605 * registers are clobbered. The NMI handler caller only needs %r13 preserved.
1608 #define L1D_FLUSH_SIZE (64 * 1024)
1610 movq $-L1D_FLUSH_SIZE, %rcx
1612 * pass 1: Preload TLB.
1613 * Kernel text is mapped using superpages. TLB preload is
1614 * done for the benefit of older CPUs which split 2M page
1615 * into 4k TLB entries.
1617 1: movb L1D_FLUSH_SIZE(%r9, %rcx), %al
1618 addq $PAGE_SIZE, %rcx
1622 movq $-L1D_FLUSH_SIZE, %rcx
1623 /* pass 2: Read each cache line. */
1624 2: movb L1D_FLUSH_SIZE(%r9, %rcx), %al
1629 #undef L1D_FLUSH_SIZE
1632 ENTRY(flush_l1d_sw_abi)
1637 END(flush_l1d_sw_abi)
1639 ENTRY(mds_handler_void)
1641 END(mds_handler_void)
1643 ENTRY(mds_handler_verw)
1649 END(mds_handler_verw)
1651 ENTRY(mds_handler_ivb)
1660 1: movq PCPU(MDS_BUF), %rdx
1661 movdqa %xmm0, PCPU(MDS_TMP)
1670 2: movntdq %xmm0, (%rdx)
1676 movdqa PCPU(MDS_TMP),%xmm0
1684 END(mds_handler_ivb)
1686 ENTRY(mds_handler_bdw)
1697 1: movq PCPU(MDS_BUF), %rbx
1698 movdqa %xmm0, PCPU(MDS_TMP)
1704 2: movntdq %xmm0, (%rbx)
1713 movdqa PCPU(MDS_TMP),%xmm0
1723 END(mds_handler_bdw)
1725 ENTRY(mds_handler_skl_sse)
1735 1: movq PCPU(MDS_BUF), %rdi
1736 movq PCPU(MDS_BUF64), %rdx
1737 movdqa %xmm0, PCPU(MDS_TMP)
1744 2: clflushopt 5376(%rdi, %rax, 8)
1754 movdqa PCPU(MDS_TMP), %xmm0
1763 END(mds_handler_skl_sse)
1765 ENTRY(mds_handler_skl_avx)
1775 1: movq PCPU(MDS_BUF), %rdi
1776 movq PCPU(MDS_BUF64), %rdx
1777 vmovdqa %ymm0, PCPU(MDS_TMP)
1778 vpxor %ymm0, %ymm0, %ymm0
1781 vorpd (%rdx), %ymm0, %ymm0
1782 vorpd (%rdx), %ymm0, %ymm0
1784 2: clflushopt 5376(%rdi, %rax, 8)
1794 vmovdqa PCPU(MDS_TMP), %ymm0
1803 END(mds_handler_skl_avx)
1805 ENTRY(mds_handler_skl_avx512)
1815 1: movq PCPU(MDS_BUF), %rdi
1816 movq PCPU(MDS_BUF64), %rdx
1817 vmovdqa64 %zmm0, PCPU(MDS_TMP)
1818 vpxord %zmm0, %zmm0, %zmm0
1821 vorpd (%rdx), %zmm0, %zmm0
1822 vorpd (%rdx), %zmm0, %zmm0
1824 2: clflushopt 5376(%rdi, %rax, 8)
1834 vmovdqa64 PCPU(MDS_TMP), %zmm0
1843 END(mds_handler_skl_avx512)
1845 ENTRY(mds_handler_silvermont)
1854 1: movq PCPU(MDS_BUF), %rdx
1855 movdqa %xmm0, PCPU(MDS_TMP)
1859 2: movntdq %xmm0, (%rdx)
1865 movdqa PCPU(MDS_TMP),%xmm0
1873 END(mds_handler_silvermont)