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.
675 * First, prevent address wrapping.
681 * XXX STOP USING VM_MAXUSER_ADDRESS.
682 * It is an end address, not a max, so every time it is used correctly it
683 * looks like there is an off by one error, and of course it caused an off
684 * by one error in several places.
686 movq $VM_MAXUSER_ADDRESS,%rcx
691 * Set return value to zero. Remaining failure mode goes through
697 * Set up arguments for MEMMOVE.
707 MEMMOVE erms=\erms overlap=0 begin=COPYINOUT_BEGIN end=COPYINOUT_SMAP_END
709 MEMMOVE erms=\erms overlap=0 begin=COPYINOUT_BEGIN end=COPYINOUT_END
714 ENTRY(copyout_nosmap_std)
715 COPYOUT smap=0 erms=0
716 END(copyout_nosmap_std)
718 ENTRY(copyout_smap_std)
719 COPYOUT smap=1 erms=0
720 END(copyout_smap_std)
722 ENTRY(copyout_nosmap_erms)
723 COPYOUT smap=0 erms=1
724 END(copyout_nosmap_erms)
726 ENTRY(copyout_smap_erms)
727 COPYOUT smap=1 erms=1
728 END(copyout_smap_erms)
731 * copyin(from_user, to_kernel, len)
734 .macro COPYIN smap erms
736 movq PCPU(CURPCB),%r11
737 movq $copy_fault,PCB_ONFAULT(%r11)
740 * make sure address is valid
745 movq $VM_MAXUSER_ADDRESS,%rcx
758 MEMMOVE erms=\erms overlap=0 begin=COPYINOUT_BEGIN end=COPYINOUT_SMAP_END
760 MEMMOVE erms=\erms overlap=0 begin=COPYINOUT_BEGIN end=COPYINOUT_END
765 ENTRY(copyin_nosmap_std)
767 END(copyin_nosmap_std)
769 ENTRY(copyin_smap_std)
773 ENTRY(copyin_nosmap_erms)
775 END(copyin_nosmap_erms)
777 ENTRY(copyin_smap_erms)
779 END(copyin_smap_erms)
782 /* Trap entry clears PSL.AC */
784 movq $0,PCB_ONFAULT(%r11)
790 * casueword32. Compare and set user integer. Returns -1 on fault,
791 * 0 if access was successful. Old value is written to *oldp.
792 * dst = %rdi, old = %esi, oldp = %rdx, new = %ecx
794 ENTRY(casueword32_nosmap)
796 movq PCPU(CURPCB),%r8
797 movq $fusufault,PCB_ONFAULT(%r8)
799 movq $VM_MAXUSER_ADDRESS-4,%rax
800 cmpq %rax,%rdi /* verify address is valid */
803 movl %esi,%eax /* old */
807 cmpxchgl %ecx,(%rdi) /* new = %ecx */
811 * The old value is in %eax. If the store succeeded it will be the
812 * value we expected (old) from before the store, otherwise it will
813 * be the current value. Save %eax into %esi to prepare the return
818 movq %rax,PCB_ONFAULT(%r8)
821 * Access the oldp after the pcb_onfault is cleared, to correctly
822 * catch corrupted pointer.
824 movl %esi,(%rdx) /* oldp = %rdx */
828 END(casueword32_nosmap)
830 ENTRY(casueword32_smap)
832 movq PCPU(CURPCB),%r8
833 movq $fusufault,PCB_ONFAULT(%r8)
835 movq $VM_MAXUSER_ADDRESS-4,%rax
836 cmpq %rax,%rdi /* verify address is valid */
839 movl %esi,%eax /* old */
844 cmpxchgl %ecx,(%rdi) /* new = %ecx */
849 * The old value is in %eax. If the store succeeded it will be the
850 * value we expected (old) from before the store, otherwise it will
851 * be the current value. Save %eax into %esi to prepare the return
856 movq %rax,PCB_ONFAULT(%r8)
859 * Access the oldp after the pcb_onfault is cleared, to correctly
860 * catch corrupted pointer.
862 movl %esi,(%rdx) /* oldp = %rdx */
866 END(casueword32_smap)
869 * casueword. Compare and set user long. Returns -1 on fault,
870 * 0 if access was successful. Old value is written to *oldp.
871 * dst = %rdi, old = %rsi, oldp = %rdx, new = %rcx
873 ENTRY(casueword_nosmap)
875 movq PCPU(CURPCB),%r8
876 movq $fusufault,PCB_ONFAULT(%r8)
878 movq $VM_MAXUSER_ADDRESS-4,%rax
879 cmpq %rax,%rdi /* verify address is valid */
882 movq %rsi,%rax /* old */
886 cmpxchgq %rcx,(%rdi) /* new = %rcx */
890 * The old value is in %rax. If the store succeeded it will be the
891 * value we expected (old) from before the store, otherwise it will
892 * be the current value.
896 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 */
922 * The old value is in %rax. If the store succeeded it will be the
923 * value we expected (old) from before the store, otherwise it will
924 * be the current value.
928 movq %rax,PCB_ONFAULT(%r8)
936 * Fetch (load) a 64-bit word, a 32-bit word, a 16-bit word, or an 8-bit
937 * byte from user memory.
938 * addr = %rdi, valp = %rsi
941 ENTRY(fueword_nosmap)
943 movq PCPU(CURPCB),%rcx
944 movq $fusufault,PCB_ONFAULT(%rcx)
946 movq $VM_MAXUSER_ADDRESS-8,%rax
947 cmpq %rax,%rdi /* verify address is valid */
952 movq %rax,PCB_ONFAULT(%rcx)
960 movq PCPU(CURPCB),%rcx
961 movq $fusufault,PCB_ONFAULT(%rcx)
963 movq $VM_MAXUSER_ADDRESS-8,%rax
964 cmpq %rax,%rdi /* verify address is valid */
971 movq %rax,PCB_ONFAULT(%rcx)
977 ENTRY(fueword32_nosmap)
979 movq PCPU(CURPCB),%rcx
980 movq $fusufault,PCB_ONFAULT(%rcx)
982 movq $VM_MAXUSER_ADDRESS-4,%rax
983 cmpq %rax,%rdi /* verify address is valid */
988 movq %rax,PCB_ONFAULT(%rcx)
992 END(fueword32_nosmap)
994 ENTRY(fueword32_smap)
996 movq PCPU(CURPCB),%rcx
997 movq $fusufault,PCB_ONFAULT(%rcx)
999 movq $VM_MAXUSER_ADDRESS-4,%rax
1000 cmpq %rax,%rdi /* verify address is valid */
1007 movq %rax,PCB_ONFAULT(%rcx)
1013 ENTRY(fuword16_nosmap)
1015 movq PCPU(CURPCB),%rcx
1016 movq $fusufault,PCB_ONFAULT(%rcx)
1018 movq $VM_MAXUSER_ADDRESS-2,%rax
1023 movq $0,PCB_ONFAULT(%rcx)
1026 END(fuword16_nosmap)
1028 ENTRY(fuword16_smap)
1030 movq PCPU(CURPCB),%rcx
1031 movq $fusufault,PCB_ONFAULT(%rcx)
1033 movq $VM_MAXUSER_ADDRESS-2,%rax
1040 movq $0,PCB_ONFAULT(%rcx)
1045 ENTRY(fubyte_nosmap)
1047 movq PCPU(CURPCB),%rcx
1048 movq $fusufault,PCB_ONFAULT(%rcx)
1050 movq $VM_MAXUSER_ADDRESS-1,%rax
1055 movq $0,PCB_ONFAULT(%rcx)
1062 movq PCPU(CURPCB),%rcx
1063 movq $fusufault,PCB_ONFAULT(%rcx)
1065 movq $VM_MAXUSER_ADDRESS-1,%rax
1072 movq $0,PCB_ONFAULT(%rcx)
1078 * Store a 64-bit word, a 32-bit word, a 16-bit word, or an 8-bit byte to
1080 * addr = %rdi, value = %rsi
1082 ENTRY(suword_nosmap)
1084 movq PCPU(CURPCB),%rcx
1085 movq $fusufault,PCB_ONFAULT(%rcx)
1087 movq $VM_MAXUSER_ADDRESS-8,%rax
1088 cmpq %rax,%rdi /* verify address validity */
1093 movq %rax,PCB_ONFAULT(%rcx)
1100 movq PCPU(CURPCB),%rcx
1101 movq $fusufault,PCB_ONFAULT(%rcx)
1103 movq $VM_MAXUSER_ADDRESS-8,%rax
1104 cmpq %rax,%rdi /* verify address validity */
1111 movq %rax,PCB_ONFAULT(%rcx)
1116 ENTRY(suword32_nosmap)
1118 movq PCPU(CURPCB),%rcx
1119 movq $fusufault,PCB_ONFAULT(%rcx)
1121 movq $VM_MAXUSER_ADDRESS-4,%rax
1122 cmpq %rax,%rdi /* verify address validity */
1127 movq %rax,PCB_ONFAULT(%rcx)
1130 END(suword32_nosmap)
1132 ENTRY(suword32_smap)
1134 movq PCPU(CURPCB),%rcx
1135 movq $fusufault,PCB_ONFAULT(%rcx)
1137 movq $VM_MAXUSER_ADDRESS-4,%rax
1138 cmpq %rax,%rdi /* verify address validity */
1145 movq %rax,PCB_ONFAULT(%rcx)
1150 ENTRY(suword16_nosmap)
1152 movq PCPU(CURPCB),%rcx
1153 movq $fusufault,PCB_ONFAULT(%rcx)
1155 movq $VM_MAXUSER_ADDRESS-2,%rax
1156 cmpq %rax,%rdi /* verify address validity */
1161 movq %rax,PCB_ONFAULT(%rcx)
1164 END(suword16_nosmap)
1166 ENTRY(suword16_smap)
1168 movq PCPU(CURPCB),%rcx
1169 movq $fusufault,PCB_ONFAULT(%rcx)
1171 movq $VM_MAXUSER_ADDRESS-2,%rax
1172 cmpq %rax,%rdi /* verify address validity */
1179 movq %rax,PCB_ONFAULT(%rcx)
1184 ENTRY(subyte_nosmap)
1186 movq PCPU(CURPCB),%rcx
1187 movq $fusufault,PCB_ONFAULT(%rcx)
1189 movq $VM_MAXUSER_ADDRESS-1,%rax
1190 cmpq %rax,%rdi /* verify address validity */
1196 movq %rax,PCB_ONFAULT(%rcx)
1203 movq PCPU(CURPCB),%rcx
1204 movq $fusufault,PCB_ONFAULT(%rcx)
1206 movq $VM_MAXUSER_ADDRESS-1,%rax
1207 cmpq %rax,%rdi /* verify address validity */
1215 movq %rax,PCB_ONFAULT(%rcx)
1221 /* Fault entry clears PSL.AC */
1223 movq PCPU(CURPCB),%rcx
1225 movq %rax,PCB_ONFAULT(%rcx)
1231 * copyinstr(from, to, maxlen, int *lencopied)
1232 * %rdi, %rsi, %rdx, %rcx
1234 * copy a string from 'from' to 'to', stop when a 0 character is reached.
1235 * return ENAMETOOLONG if string is longer than maxlen, and
1236 * EFAULT on protection violations. If lencopied is non-zero,
1237 * return the actual length in *lencopied.
1239 .macro COPYINSTR smap
1241 movq %rdx,%r8 /* %r8 = maxlen */
1242 movq PCPU(CURPCB),%r9
1243 movq $cpystrflt,PCB_ONFAULT(%r9)
1245 movq $VM_MAXUSER_ADDRESS,%rax
1247 /* make sure 'from' is within bounds */
1253 /* restrict maxlen to <= VM_MAXUSER_ADDRESS-from */
1261 jz copyinstr_toolong
1263 jz copyinstr_toolong_smap
1275 /* Success -- 0 byte reached */
1279 /* set *lencopied and return %eax */
1280 movq %rax,PCB_ONFAULT(%r9)
1297 ENTRY(copyinstr_nosmap)
1299 END(copyinstr_nosmap)
1301 ENTRY(copyinstr_smap)
1306 /* Fault entry clears PSL.AC */
1309 /* set *lencopied and return %eax */
1310 movq $0,PCB_ONFAULT(%r9)
1320 copyinstr_toolong_smap:
1323 /* rdx is zero - return ENAMETOOLONG or EFAULT */
1324 movq $VM_MAXUSER_ADDRESS,%rax
1327 movl $ENAMETOOLONG,%eax
1331 * copystr(from, to, maxlen, int *lencopied)
1332 * %rdi, %rsi, %rdx, %rcx
1336 movq %rdx,%r8 /* %r8 = maxlen */
1349 /* Success -- 0 byte reached */
1355 /* set *lencopied and return %rax */
1362 /* rdx is zero -- return ENAMETOOLONG */
1363 movl $ENAMETOOLONG,%eax
1368 * Handling of special amd64 registers and descriptor tables etc
1370 /* void lgdt(struct region_descriptor *rdp); */
1372 /* reload the descriptor table */
1375 /* flush the prefetch q */
1382 movl %eax,%fs /* Beware, use wrmsr to set 64 bit base */
1386 /* reload code selector by turning return into intersegmental return */
1394 /*****************************************************************************/
1395 /* setjump, longjump */
1396 /*****************************************************************************/
1399 movq %rbx,0(%rdi) /* save rbx */
1400 movq %rsp,8(%rdi) /* save rsp */
1401 movq %rbp,16(%rdi) /* save rbp */
1402 movq %r12,24(%rdi) /* save r12 */
1403 movq %r13,32(%rdi) /* save r13 */
1404 movq %r14,40(%rdi) /* save r14 */
1405 movq %r15,48(%rdi) /* save r15 */
1406 movq 0(%rsp),%rdx /* get rta */
1407 movq %rdx,56(%rdi) /* save rip */
1408 xorl %eax,%eax /* return(0); */
1413 movq 0(%rdi),%rbx /* restore rbx */
1414 movq 8(%rdi),%rsp /* restore rsp */
1415 movq 16(%rdi),%rbp /* restore rbp */
1416 movq 24(%rdi),%r12 /* restore r12 */
1417 movq 32(%rdi),%r13 /* restore r13 */
1418 movq 40(%rdi),%r14 /* restore r14 */
1419 movq 48(%rdi),%r15 /* restore r15 */
1420 movq 56(%rdi),%rdx /* get rta */
1421 movq %rdx,0(%rsp) /* put in return frame */
1422 xorl %eax,%eax /* return(1); */
1428 * Support for reading MSRs in the safe manner. (Instead of panic on #gp,
1432 /* int rdmsr_safe(u_int msr, uint64_t *data) */
1434 movq PCPU(CURPCB),%r8
1435 movq $msr_onfault,PCB_ONFAULT(%r8)
1437 rdmsr /* Read MSR pointed by %ecx. Returns
1438 hi byte in edx, lo in %eax */
1439 salq $32,%rdx /* sign-shift %rdx left */
1440 movl %eax,%eax /* zero-extend %eax -> %rax */
1444 movq %rax,PCB_ONFAULT(%r8)
1449 * Support for writing MSRs in the safe manner. (Instead of panic on #gp,
1453 /* int wrmsr_safe(u_int msr, uint64_t data) */
1455 movq PCPU(CURPCB),%r8
1456 movq $msr_onfault,PCB_ONFAULT(%r8)
1461 wrmsr /* Write MSR pointed by %ecx. Accepts
1462 hi byte in edx, lo in %eax. */
1464 movq %rax,PCB_ONFAULT(%r8)
1469 * MSR operations fault handler
1473 movq $0,PCB_ONFAULT(%r8)
1479 * void pmap_pti_pcid_invalidate(uint64_t ucr3, uint64_t kcr3);
1480 * Invalidates address space addressed by ucr3, then returns to kcr3.
1481 * Done in assembler to ensure no other memory accesses happen while
1485 ENTRY(pmap_pti_pcid_invalidate)
1488 movq %rdi,%cr3 /* to user page table */
1489 movq %rsi,%cr3 /* back to kernel */
1494 * void pmap_pti_pcid_invlpg(uint64_t ucr3, uint64_t kcr3, vm_offset_t va);
1495 * Invalidates virtual address va in address space ucr3, then returns to kcr3.
1498 ENTRY(pmap_pti_pcid_invlpg)
1501 movq %rdi,%cr3 /* to user page table */
1503 movq %rsi,%cr3 /* back to kernel */
1508 * void pmap_pti_pcid_invlrng(uint64_t ucr3, uint64_t kcr3, vm_offset_t sva,
1510 * Invalidates virtual addresses between sva and eva in address space ucr3,
1511 * then returns to kcr3.
1514 ENTRY(pmap_pti_pcid_invlrng)
1517 movq %rdi,%cr3 /* to user page table */
1519 addq $PAGE_SIZE,%rdx
1522 movq %rsi,%cr3 /* back to kernel */
1527 .macro ibrs_seq_label l
1530 .macro ibrs_call_label l
1533 .macro ibrs_seq count
1536 ibrs_call_label %(ll)
1538 ibrs_seq_label %(ll)
1544 /* all callers already saved %rax, %rdx, and %rcx */
1545 ENTRY(handle_ibrs_entry)
1546 cmpb $0,hw_ibrs_active(%rip)
1548 movl $MSR_IA32_SPEC_CTRL,%ecx
1550 orl $(IA32_SPEC_CTRL_IBRS|IA32_SPEC_CTRL_STIBP),%eax
1551 orl $(IA32_SPEC_CTRL_IBRS|IA32_SPEC_CTRL_STIBP)>>32,%edx
1553 movb $1,PCPU(IBPB_SET)
1554 testl $CPUID_STDEXT_SMEP,cpu_stdext_feature(%rip)
1558 END(handle_ibrs_entry)
1560 ENTRY(handle_ibrs_exit)
1561 cmpb $0,PCPU(IBPB_SET)
1563 movl $MSR_IA32_SPEC_CTRL,%ecx
1565 andl $~(IA32_SPEC_CTRL_IBRS|IA32_SPEC_CTRL_STIBP),%eax
1566 andl $~((IA32_SPEC_CTRL_IBRS|IA32_SPEC_CTRL_STIBP)>>32),%edx
1568 movb $0,PCPU(IBPB_SET)
1570 END(handle_ibrs_exit)
1572 /* registers-neutral version, but needs stack */
1573 ENTRY(handle_ibrs_exit_rs)
1574 cmpb $0,PCPU(IBPB_SET)
1579 movl $MSR_IA32_SPEC_CTRL,%ecx
1581 andl $~(IA32_SPEC_CTRL_IBRS|IA32_SPEC_CTRL_STIBP),%eax
1582 andl $~((IA32_SPEC_CTRL_IBRS|IA32_SPEC_CTRL_STIBP)>>32),%edx
1587 movb $0,PCPU(IBPB_SET)
1589 END(handle_ibrs_exit_rs)
1594 * Flush L1D cache. Load enough of the data from the kernel text
1595 * to flush existing L1D content.
1597 * N.B. The function does not follow ABI calling conventions, it corrupts %rbx.
1598 * The vmm.ko caller expects that only %rax, %rdx, %rbx, %rcx, %r9, and %rflags
1599 * registers are clobbered. The NMI handler caller only needs %r13 preserved.
1602 #define L1D_FLUSH_SIZE (64 * 1024)
1604 movq $-L1D_FLUSH_SIZE, %rcx
1606 * pass 1: Preload TLB.
1607 * Kernel text is mapped using superpages. TLB preload is
1608 * done for the benefit of older CPUs which split 2M page
1609 * into 4k TLB entries.
1611 1: movb L1D_FLUSH_SIZE(%r9, %rcx), %al
1612 addq $PAGE_SIZE, %rcx
1616 movq $-L1D_FLUSH_SIZE, %rcx
1617 /* pass 2: Read each cache line. */
1618 2: movb L1D_FLUSH_SIZE(%r9, %rcx), %al
1623 #undef L1D_FLUSH_SIZE
1626 ENTRY(flush_l1d_sw_abi)
1631 END(flush_l1d_sw_abi)
1633 ENTRY(mds_handler_void)
1635 END(mds_handler_void)
1637 ENTRY(mds_handler_verw)
1643 END(mds_handler_verw)
1645 ENTRY(mds_handler_ivb)
1654 1: movq PCPU(MDS_BUF), %rdx
1655 movdqa %xmm0, PCPU(MDS_TMP)
1664 2: movntdq %xmm0, (%rdx)
1670 movdqa PCPU(MDS_TMP),%xmm0
1678 END(mds_handler_ivb)
1680 ENTRY(mds_handler_bdw)
1691 1: movq PCPU(MDS_BUF), %rbx
1692 movdqa %xmm0, PCPU(MDS_TMP)
1698 2: movntdq %xmm0, (%rbx)
1707 movdqa PCPU(MDS_TMP),%xmm0
1717 END(mds_handler_bdw)
1719 ENTRY(mds_handler_skl_sse)
1729 1: movq PCPU(MDS_BUF), %rdi
1730 movq PCPU(MDS_BUF64), %rdx
1731 movdqa %xmm0, PCPU(MDS_TMP)
1738 2: clflushopt 5376(%rdi, %rax, 8)
1748 movdqa PCPU(MDS_TMP), %xmm0
1757 END(mds_handler_skl_sse)
1759 ENTRY(mds_handler_skl_avx)
1769 1: movq PCPU(MDS_BUF), %rdi
1770 movq PCPU(MDS_BUF64), %rdx
1771 vmovdqa %ymm0, PCPU(MDS_TMP)
1772 vpxor %ymm0, %ymm0, %ymm0
1775 vorpd (%rdx), %ymm0, %ymm0
1776 vorpd (%rdx), %ymm0, %ymm0
1778 2: clflushopt 5376(%rdi, %rax, 8)
1788 vmovdqa PCPU(MDS_TMP), %ymm0
1797 END(mds_handler_skl_avx)
1799 ENTRY(mds_handler_skl_avx512)
1809 1: movq PCPU(MDS_BUF), %rdi
1810 movq PCPU(MDS_BUF64), %rdx
1811 vmovdqa64 %zmm0, PCPU(MDS_TMP)
1812 vpxord %zmm0, %zmm0, %zmm0
1815 vorpd (%rdx), %zmm0, %zmm0
1816 vorpd (%rdx), %zmm0, %zmm0
1818 2: clflushopt 5376(%rdi, %rax, 8)
1828 vmovdqa64 PCPU(MDS_TMP), %zmm0
1837 END(mds_handler_skl_avx512)
1839 ENTRY(mds_handler_silvermont)
1848 1: movq PCPU(MDS_BUF), %rdx
1849 movdqa %xmm0, PCPU(MDS_TMP)
1853 2: movntdq %xmm0, (%rdx)
1859 movdqa PCPU(MDS_TMP),%xmm0
1867 END(mds_handler_silvermont)