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 */
816 * The old value is in %eax. If the store succeeded it will be the
817 * value we expected (old) from before the store, otherwise it will
818 * be the current value. Save %eax into %esi to prepare the return
823 movq %rax,PCB_ONFAULT(%r8)
826 * Access the oldp after the pcb_onfault is cleared, to correctly
827 * catch corrupted pointer.
829 movl %esi,(%rdx) /* oldp = %rdx */
832 END(casueword32_nosmap)
834 ENTRY(casueword32_smap)
836 movq PCPU(CURPCB),%r8
837 movq $fusufault,PCB_ONFAULT(%r8)
839 movq $VM_MAXUSER_ADDRESS-4,%rax
840 cmpq %rax,%rdi /* verify address is valid */
843 movl %esi,%eax /* old */
848 cmpxchgl %ecx,(%rdi) /* new = %ecx */
852 * The old value is in %eax. If the store succeeded it will be the
853 * value we expected (old) from before the store, otherwise it will
854 * be the current value. Save %eax into %esi to prepare the return
859 movq %rax,PCB_ONFAULT(%r8)
862 * Access the oldp after the pcb_onfault is cleared, to correctly
863 * catch corrupted pointer.
865 movl %esi,(%rdx) /* oldp = %rdx */
868 END(casueword32_smap)
871 * casueword. Compare and set user long. Returns -1 on fault,
872 * 0 if access was successful. Old value is written to *oldp.
873 * dst = %rdi, old = %rsi, oldp = %rdx, new = %rcx
875 ENTRY(casueword_nosmap)
877 movq PCPU(CURPCB),%r8
878 movq $fusufault,PCB_ONFAULT(%r8)
880 movq $VM_MAXUSER_ADDRESS-4,%rax
881 cmpq %rax,%rdi /* verify address is valid */
884 movq %rsi,%rax /* old */
888 cmpxchgq %rcx,(%rdi) /* new = %rcx */
891 * The old value is in %rax. If the store succeeded it will be the
892 * value we expected (old) from before the store, otherwise it will
893 * be the current value.
897 movq %rax,PCB_ONFAULT(%r8)
901 END(casueword_nosmap)
903 ENTRY(casueword_smap)
905 movq PCPU(CURPCB),%r8
906 movq $fusufault,PCB_ONFAULT(%r8)
908 movq $VM_MAXUSER_ADDRESS-4,%rax
909 cmpq %rax,%rdi /* verify address is valid */
912 movq %rsi,%rax /* old */
917 cmpxchgq %rcx,(%rdi) /* new = %rcx */
921 * The old value is in %rax. If the store succeeded it will be the
922 * value we expected (old) from before the store, otherwise it will
923 * be the current value.
927 movq %rax,PCB_ONFAULT(%r8)
934 * Fetch (load) a 64-bit word, a 32-bit word, a 16-bit word, or an 8-bit
935 * byte from user memory.
936 * addr = %rdi, valp = %rsi
939 ENTRY(fueword_nosmap)
941 movq PCPU(CURPCB),%rcx
942 movq $fusufault,PCB_ONFAULT(%rcx)
944 movq $VM_MAXUSER_ADDRESS-8,%rax
945 cmpq %rax,%rdi /* verify address is valid */
950 movq %rax,PCB_ONFAULT(%rcx)
958 movq PCPU(CURPCB),%rcx
959 movq $fusufault,PCB_ONFAULT(%rcx)
961 movq $VM_MAXUSER_ADDRESS-8,%rax
962 cmpq %rax,%rdi /* verify address is valid */
969 movq %rax,PCB_ONFAULT(%rcx)
975 ENTRY(fueword32_nosmap)
977 movq PCPU(CURPCB),%rcx
978 movq $fusufault,PCB_ONFAULT(%rcx)
980 movq $VM_MAXUSER_ADDRESS-4,%rax
981 cmpq %rax,%rdi /* verify address is valid */
986 movq %rax,PCB_ONFAULT(%rcx)
990 END(fueword32_nosmap)
992 ENTRY(fueword32_smap)
994 movq PCPU(CURPCB),%rcx
995 movq $fusufault,PCB_ONFAULT(%rcx)
997 movq $VM_MAXUSER_ADDRESS-4,%rax
998 cmpq %rax,%rdi /* verify address is valid */
1005 movq %rax,PCB_ONFAULT(%rcx)
1011 ENTRY(fuword16_nosmap)
1013 movq PCPU(CURPCB),%rcx
1014 movq $fusufault,PCB_ONFAULT(%rcx)
1016 movq $VM_MAXUSER_ADDRESS-2,%rax
1021 movq $0,PCB_ONFAULT(%rcx)
1024 END(fuword16_nosmap)
1026 ENTRY(fuword16_smap)
1028 movq PCPU(CURPCB),%rcx
1029 movq $fusufault,PCB_ONFAULT(%rcx)
1031 movq $VM_MAXUSER_ADDRESS-2,%rax
1038 movq $0,PCB_ONFAULT(%rcx)
1043 ENTRY(fubyte_nosmap)
1045 movq PCPU(CURPCB),%rcx
1046 movq $fusufault,PCB_ONFAULT(%rcx)
1048 movq $VM_MAXUSER_ADDRESS-1,%rax
1053 movq $0,PCB_ONFAULT(%rcx)
1060 movq PCPU(CURPCB),%rcx
1061 movq $fusufault,PCB_ONFAULT(%rcx)
1063 movq $VM_MAXUSER_ADDRESS-1,%rax
1070 movq $0,PCB_ONFAULT(%rcx)
1076 * Store a 64-bit word, a 32-bit word, a 16-bit word, or an 8-bit byte to
1078 * addr = %rdi, value = %rsi
1080 ENTRY(suword_nosmap)
1082 movq PCPU(CURPCB),%rcx
1083 movq $fusufault,PCB_ONFAULT(%rcx)
1085 movq $VM_MAXUSER_ADDRESS-8,%rax
1086 cmpq %rax,%rdi /* verify address validity */
1091 movq %rax,PCB_ONFAULT(%rcx)
1098 movq PCPU(CURPCB),%rcx
1099 movq $fusufault,PCB_ONFAULT(%rcx)
1101 movq $VM_MAXUSER_ADDRESS-8,%rax
1102 cmpq %rax,%rdi /* verify address validity */
1109 movq %rax,PCB_ONFAULT(%rcx)
1114 ENTRY(suword32_nosmap)
1116 movq PCPU(CURPCB),%rcx
1117 movq $fusufault,PCB_ONFAULT(%rcx)
1119 movq $VM_MAXUSER_ADDRESS-4,%rax
1120 cmpq %rax,%rdi /* verify address validity */
1125 movq %rax,PCB_ONFAULT(%rcx)
1128 END(suword32_nosmap)
1130 ENTRY(suword32_smap)
1132 movq PCPU(CURPCB),%rcx
1133 movq $fusufault,PCB_ONFAULT(%rcx)
1135 movq $VM_MAXUSER_ADDRESS-4,%rax
1136 cmpq %rax,%rdi /* verify address validity */
1143 movq %rax,PCB_ONFAULT(%rcx)
1148 ENTRY(suword16_nosmap)
1150 movq PCPU(CURPCB),%rcx
1151 movq $fusufault,PCB_ONFAULT(%rcx)
1153 movq $VM_MAXUSER_ADDRESS-2,%rax
1154 cmpq %rax,%rdi /* verify address validity */
1159 movq %rax,PCB_ONFAULT(%rcx)
1162 END(suword16_nosmap)
1164 ENTRY(suword16_smap)
1166 movq PCPU(CURPCB),%rcx
1167 movq $fusufault,PCB_ONFAULT(%rcx)
1169 movq $VM_MAXUSER_ADDRESS-2,%rax
1170 cmpq %rax,%rdi /* verify address validity */
1177 movq %rax,PCB_ONFAULT(%rcx)
1182 ENTRY(subyte_nosmap)
1184 movq PCPU(CURPCB),%rcx
1185 movq $fusufault,PCB_ONFAULT(%rcx)
1187 movq $VM_MAXUSER_ADDRESS-1,%rax
1188 cmpq %rax,%rdi /* verify address validity */
1194 movq %rax,PCB_ONFAULT(%rcx)
1201 movq PCPU(CURPCB),%rcx
1202 movq $fusufault,PCB_ONFAULT(%rcx)
1204 movq $VM_MAXUSER_ADDRESS-1,%rax
1205 cmpq %rax,%rdi /* verify address validity */
1213 movq %rax,PCB_ONFAULT(%rcx)
1219 /* Fault entry clears PSL.AC */
1221 movq PCPU(CURPCB),%rcx
1223 movq %rax,PCB_ONFAULT(%rcx)
1229 * copyinstr(from, to, maxlen, int *lencopied)
1230 * %rdi, %rsi, %rdx, %rcx
1232 * copy a string from 'from' to 'to', stop when a 0 character is reached.
1233 * return ENAMETOOLONG if string is longer than maxlen, and
1234 * EFAULT on protection violations. If lencopied is non-zero,
1235 * return the actual length in *lencopied.
1237 .macro COPYINSTR smap
1239 movq %rdx,%r8 /* %r8 = maxlen */
1240 movq PCPU(CURPCB),%r9
1241 movq $cpystrflt,PCB_ONFAULT(%r9)
1243 movq $VM_MAXUSER_ADDRESS,%rax
1245 /* make sure 'from' is within bounds */
1251 /* restrict maxlen to <= VM_MAXUSER_ADDRESS-from */
1259 jz copyinstr_toolong
1261 jz copyinstr_toolong_smap
1273 /* Success -- 0 byte reached */
1277 /* set *lencopied and return %eax */
1278 movq %rax,PCB_ONFAULT(%r9)
1295 ENTRY(copyinstr_nosmap)
1297 END(copyinstr_nosmap)
1299 ENTRY(copyinstr_smap)
1304 /* Fault entry clears PSL.AC */
1307 /* set *lencopied and return %eax */
1308 movq $0,PCB_ONFAULT(%r9)
1318 copyinstr_toolong_smap:
1321 /* rdx is zero - return ENAMETOOLONG or EFAULT */
1322 movq $VM_MAXUSER_ADDRESS,%rax
1325 movl $ENAMETOOLONG,%eax
1329 * copystr(from, to, maxlen, int *lencopied)
1330 * %rdi, %rsi, %rdx, %rcx
1334 movq %rdx,%r8 /* %r8 = maxlen */
1347 /* Success -- 0 byte reached */
1353 /* set *lencopied and return %rax */
1360 /* rdx is zero -- return ENAMETOOLONG */
1361 movl $ENAMETOOLONG,%eax
1366 * Handling of special amd64 registers and descriptor tables etc
1368 /* void lgdt(struct region_descriptor *rdp); */
1370 /* reload the descriptor table */
1373 /* flush the prefetch q */
1380 movl %eax,%fs /* Beware, use wrmsr to set 64 bit base */
1384 /* reload code selector by turning return into intersegmental return */
1392 /*****************************************************************************/
1393 /* setjump, longjump */
1394 /*****************************************************************************/
1397 movq %rbx,0(%rdi) /* save rbx */
1398 movq %rsp,8(%rdi) /* save rsp */
1399 movq %rbp,16(%rdi) /* save rbp */
1400 movq %r12,24(%rdi) /* save r12 */
1401 movq %r13,32(%rdi) /* save r13 */
1402 movq %r14,40(%rdi) /* save r14 */
1403 movq %r15,48(%rdi) /* save r15 */
1404 movq 0(%rsp),%rdx /* get rta */
1405 movq %rdx,56(%rdi) /* save rip */
1406 xorl %eax,%eax /* return(0); */
1411 movq 0(%rdi),%rbx /* restore rbx */
1412 movq 8(%rdi),%rsp /* restore rsp */
1413 movq 16(%rdi),%rbp /* restore rbp */
1414 movq 24(%rdi),%r12 /* restore r12 */
1415 movq 32(%rdi),%r13 /* restore r13 */
1416 movq 40(%rdi),%r14 /* restore r14 */
1417 movq 48(%rdi),%r15 /* restore r15 */
1418 movq 56(%rdi),%rdx /* get rta */
1419 movq %rdx,0(%rsp) /* put in return frame */
1420 xorl %eax,%eax /* return(1); */
1426 * Support for reading MSRs in the safe manner. (Instead of panic on #gp,
1430 /* int rdmsr_safe(u_int msr, uint64_t *data) */
1432 movq PCPU(CURPCB),%r8
1433 movq $msr_onfault,PCB_ONFAULT(%r8)
1435 rdmsr /* Read MSR pointed by %ecx. Returns
1436 hi byte in edx, lo in %eax */
1437 salq $32,%rdx /* sign-shift %rdx left */
1438 movl %eax,%eax /* zero-extend %eax -> %rax */
1442 movq %rax,PCB_ONFAULT(%r8)
1447 * Support for writing MSRs in the safe manner. (Instead of panic on #gp,
1451 /* int wrmsr_safe(u_int msr, uint64_t data) */
1453 movq PCPU(CURPCB),%r8
1454 movq $msr_onfault,PCB_ONFAULT(%r8)
1459 wrmsr /* Write MSR pointed by %ecx. Accepts
1460 hi byte in edx, lo in %eax. */
1462 movq %rax,PCB_ONFAULT(%r8)
1467 * MSR operations fault handler
1471 movq $0,PCB_ONFAULT(%r8)
1477 * void pmap_pti_pcid_invalidate(uint64_t ucr3, uint64_t kcr3);
1478 * Invalidates address space addressed by ucr3, then returns to kcr3.
1479 * Done in assembler to ensure no other memory accesses happen while
1483 ENTRY(pmap_pti_pcid_invalidate)
1486 movq %rdi,%cr3 /* to user page table */
1487 movq %rsi,%cr3 /* back to kernel */
1492 * void pmap_pti_pcid_invlpg(uint64_t ucr3, uint64_t kcr3, vm_offset_t va);
1493 * Invalidates virtual address va in address space ucr3, then returns to kcr3.
1496 ENTRY(pmap_pti_pcid_invlpg)
1499 movq %rdi,%cr3 /* to user page table */
1501 movq %rsi,%cr3 /* back to kernel */
1506 * void pmap_pti_pcid_invlrng(uint64_t ucr3, uint64_t kcr3, vm_offset_t sva,
1508 * Invalidates virtual addresses between sva and eva in address space ucr3,
1509 * then returns to kcr3.
1512 ENTRY(pmap_pti_pcid_invlrng)
1515 movq %rdi,%cr3 /* to user page table */
1517 addq $PAGE_SIZE,%rdx
1520 movq %rsi,%cr3 /* back to kernel */
1525 .macro ibrs_seq_label l
1528 .macro ibrs_call_label l
1531 .macro ibrs_seq count
1534 ibrs_call_label %(ll)
1536 ibrs_seq_label %(ll)
1542 /* all callers already saved %rax, %rdx, and %rcx */
1543 ENTRY(handle_ibrs_entry)
1544 cmpb $0,hw_ibrs_active(%rip)
1546 movl $MSR_IA32_SPEC_CTRL,%ecx
1548 orl $(IA32_SPEC_CTRL_IBRS|IA32_SPEC_CTRL_STIBP),%eax
1549 orl $(IA32_SPEC_CTRL_IBRS|IA32_SPEC_CTRL_STIBP)>>32,%edx
1551 movb $1,PCPU(IBPB_SET)
1552 testl $CPUID_STDEXT_SMEP,cpu_stdext_feature(%rip)
1556 END(handle_ibrs_entry)
1558 ENTRY(handle_ibrs_exit)
1559 cmpb $0,PCPU(IBPB_SET)
1561 movl $MSR_IA32_SPEC_CTRL,%ecx
1563 andl $~(IA32_SPEC_CTRL_IBRS|IA32_SPEC_CTRL_STIBP),%eax
1564 andl $~((IA32_SPEC_CTRL_IBRS|IA32_SPEC_CTRL_STIBP)>>32),%edx
1566 movb $0,PCPU(IBPB_SET)
1568 END(handle_ibrs_exit)
1570 /* registers-neutral version, but needs stack */
1571 ENTRY(handle_ibrs_exit_rs)
1572 cmpb $0,PCPU(IBPB_SET)
1577 movl $MSR_IA32_SPEC_CTRL,%ecx
1579 andl $~(IA32_SPEC_CTRL_IBRS|IA32_SPEC_CTRL_STIBP),%eax
1580 andl $~((IA32_SPEC_CTRL_IBRS|IA32_SPEC_CTRL_STIBP)>>32),%edx
1585 movb $0,PCPU(IBPB_SET)
1587 END(handle_ibrs_exit_rs)
1592 * Flush L1D cache. Load enough of the data from the kernel text
1593 * to flush existing L1D content.
1595 * N.B. The function does not follow ABI calling conventions, it corrupts %rbx.
1596 * The vmm.ko caller expects that only %rax, %rdx, %rbx, %rcx, %r9, and %rflags
1597 * registers are clobbered. The NMI handler caller only needs %r13 preserved.
1600 #define L1D_FLUSH_SIZE (64 * 1024)
1602 movq $-L1D_FLUSH_SIZE, %rcx
1604 * pass 1: Preload TLB.
1605 * Kernel text is mapped using superpages. TLB preload is
1606 * done for the benefit of older CPUs which split 2M page
1607 * into 4k TLB entries.
1609 1: movb L1D_FLUSH_SIZE(%r9, %rcx), %al
1610 addq $PAGE_SIZE, %rcx
1614 movq $-L1D_FLUSH_SIZE, %rcx
1615 /* pass 2: Read each cache line. */
1616 2: movb L1D_FLUSH_SIZE(%r9, %rcx), %al
1621 #undef L1D_FLUSH_SIZE
1624 ENTRY(flush_l1d_sw_abi)
1629 END(flush_l1d_sw_abi)
1631 ENTRY(mds_handler_void)
1633 END(mds_handler_void)
1635 ENTRY(mds_handler_verw)
1641 END(mds_handler_verw)
1643 ENTRY(mds_handler_ivb)
1652 1: movq PCPU(MDS_BUF), %rdx
1653 movdqa %xmm0, PCPU(MDS_TMP)
1662 2: movntdq %xmm0, (%rdx)
1668 movdqa PCPU(MDS_TMP),%xmm0
1676 END(mds_handler_ivb)
1678 ENTRY(mds_handler_bdw)
1689 1: movq PCPU(MDS_BUF), %rbx
1690 movdqa %xmm0, PCPU(MDS_TMP)
1696 2: movntdq %xmm0, (%rbx)
1705 movdqa PCPU(MDS_TMP),%xmm0
1715 END(mds_handler_bdw)
1717 ENTRY(mds_handler_skl_sse)
1727 1: movq PCPU(MDS_BUF), %rdi
1728 movq PCPU(MDS_BUF64), %rdx
1729 movdqa %xmm0, PCPU(MDS_TMP)
1736 2: clflushopt 5376(%rdi, %rax, 8)
1746 movdqa PCPU(MDS_TMP), %xmm0
1755 END(mds_handler_skl_sse)
1757 ENTRY(mds_handler_skl_avx)
1767 1: movq PCPU(MDS_BUF), %rdi
1768 movq PCPU(MDS_BUF64), %rdx
1769 vmovdqa %ymm0, PCPU(MDS_TMP)
1770 vpxor %ymm0, %ymm0, %ymm0
1773 vorpd (%rdx), %ymm0, %ymm0
1774 vorpd (%rdx), %ymm0, %ymm0
1776 2: clflushopt 5376(%rdi, %rax, 8)
1786 vmovdqa PCPU(MDS_TMP), %ymm0
1795 END(mds_handler_skl_avx)
1797 ENTRY(mds_handler_skl_avx512)
1807 1: movq PCPU(MDS_BUF), %rdi
1808 movq PCPU(MDS_BUF64), %rdx
1809 vmovdqa64 %zmm0, PCPU(MDS_TMP)
1810 vpxord %zmm0, %zmm0, %zmm0
1813 vorpd (%rdx), %zmm0, %zmm0
1814 vorpd (%rdx), %zmm0, %zmm0
1816 2: clflushopt 5376(%rdi, %rax, 8)
1826 vmovdqa64 PCPU(MDS_TMP), %zmm0
1835 END(mds_handler_skl_avx512)
1837 ENTRY(mds_handler_silvermont)
1846 1: movq PCPU(MDS_BUF), %rdx
1847 movdqa %xmm0, PCPU(MDS_TMP)
1851 2: movntdq %xmm0, (%rdx)
1857 movdqa PCPU(MDS_TMP),%xmm0
1865 END(mds_handler_silvermont)