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
85 * memcmpy(b1, b2, len)
120 movq -8(%rdi,%rdx),%r8
121 movq -8(%rsi,%rdx),%r9
132 movl -4(%rdi,%rdx),%r8d
133 movl -4(%rsi,%rdx),%r9d
144 movzwl -2(%rdi,%rdx),%r8d
145 movzwl -2(%rsi,%rdx),%r9d
162 movq -16(%rdi,%rdx),%r8
163 movq -16(%rsi,%rdx),%r9
166 movq -8(%rdi,%rdx),%r8
167 movq -8(%rsi,%rdx),%r9
199 * Mismatch was found.
201 * Before we compute it we narrow down the range (16 -> 8 -> 4 bytes).
218 leaq -8(%rdi,%rdx),%rdi
219 leaq -8(%rsi,%rdx),%rsi
223 leaq -16(%rdi,%rdx),%rdi
224 leaq -16(%rsi,%rdx),%rsi
233 leaq -4(%rdi,%rdx),%rdi
234 leaq -4(%rsi,%rdx),%rsi
247 * We have up to 4 bytes to inspect.
274 * memmove(dst, src, cnt)
279 * Register state at entry is supposed to be as follows:
284 * The macro possibly clobbers the above and: rcx, r8, r9, r10
285 * It does not clobber rax nor r11.
287 .macro MEMMOVE erms overlap begin end
291 * For sizes 0..32 all data is read before it is written, so there
292 * is no correctness issue with direction of copying.
300 cmpq %rcx,%r8 /* overlapping && src < dst? */
332 movq -16(%rsi,%rcx),%r9
333 movq -8(%rsi,%rcx),%r10
336 movq %r9,-16(%rdi,%rcx)
337 movq %r10,-8(%rdi,%rcx)
345 movq -8(%rsi,%rcx),%r8
347 movq %r8,-8(%rdi,%rcx,)
355 movl -4(%rsi,%rcx),%r8d
357 movl %r8d,-4(%rdi,%rcx)
365 movzwl -2(%rsi,%rcx),%r8d
367 movw %r8w,-2(%rdi,%rcx)
388 shrq $3,%rcx /* copy by 64-bit words */
392 andl $7,%ecx /* any bytes left? */
403 leaq -16(%rdx,%rcx),%rdx
405 leaq 16(%rdi,%rcx),%rdi
406 leaq 16(%rsi,%rcx),%rsi
414 shrq $3,%rcx /* copy by 64-bit words */
420 andl $7,%ecx /* any bytes left? */
435 leaq -8(%rdi,%rcx),%rdi
436 leaq -8(%rsi,%rcx),%rsi
511 leaq -1(%rdi,%rcx),%rdi
512 leaq -1(%rsi,%rcx),%rsi
517 leaq -8(%rdi,%rcx),%rdi
518 leaq -8(%rsi,%rcx),%rsi
543 MEMMOVE erms=0 overlap=1 begin=MEMMOVE_BEGIN end=MEMMOVE_END
547 MEMMOVE erms=1 overlap=1 begin=MEMMOVE_BEGIN end=MEMMOVE_END
551 * memcpy(dst, src, len)
554 * Note: memcpy does not support overlapping copies
557 MEMMOVE erms=0 overlap=0 begin=MEMMOVE_BEGIN end=MEMMOVE_END
561 MEMMOVE erms=1 overlap=0 begin=MEMMOVE_BEGIN end=MEMMOVE_END
565 * memset(dst, c, len)
573 movabs $0x0101010101010101,%r10
594 movq %r10,-16(%rdi,%rcx)
595 movq %r10,-8(%rdi,%rcx)
605 movq %r10,-16(%rdi,%rcx)
606 movq %r10,-8(%rdi,%rcx)
614 movq %r10,-8(%rdi,%rcx)
622 movl %r10d,-4(%rdi,%rcx)
630 movw %r10w,-2(%rdi,%rcx)
663 movq %r10,-8(%rdi,%rdx)
673 leaq -16(%rcx,%r8),%rcx
675 leaq 16(%rdi,%r8),%rdi
687 /* fillw(pat, base, cnt) */
688 /* %rdi,%rsi, %rdx */
704 * Uses the ((x - 0x01....01) & ~x & 0x80....80) trick.
706 * 0x01....01 is replaced with 0x0 - 0x01....01 so that it can be added
709 * For a description see either:
710 * - "Hacker's Delight" by Henry S. Warren, Jr.
711 * - "Optimizing subroutines in assembly language: An optimization guide for x86 platforms"
714 * The latter contains a 32-bit variant of the same algorithm coded in assembly for i386.
718 movabsq $0xfefefefefefefeff,%r8
719 movabsq $0x8080808080808080,%r9
727 * Handle misaligned reads: align to 8 and fill
728 * the spurious bytes.
760 leaq (%rcx,%rdi),%rax
766 /*****************************************************************************/
767 /* copyout and fubyte family */
768 /*****************************************************************************/
770 * Access user memory from inside the kernel. These routines should be
771 * the only places that do this.
773 * These routines set curpcb->pcb_onfault for the time they execute. When a
774 * protection violation occurs inside the functions, the trap handler
775 * returns to *curpcb->pcb_onfault instead of the function.
778 .macro SMAP_DISABLE smap
785 .macro SMAP_ENABLE smap
791 .macro COPYINOUT_BEGIN
795 movq %rax,PCB_ONFAULT(%r11)
799 .macro COPYINOUT_SMAP_END
805 * copyout(from_kernel, to_user, len)
808 .macro COPYOUT smap erms
810 movq PCPU(CURPCB),%r11
811 movq $copy_fault,PCB_ONFAULT(%r11)
814 * Check explicitly for non-user addresses.
815 * First, prevent address wrapping.
821 * XXX STOP USING VM_MAXUSER_ADDRESS.
822 * It is an end address, not a max, so every time it is used correctly it
823 * looks like there is an off by one error, and of course it caused an off
824 * by one error in several places.
826 movq $VM_MAXUSER_ADDRESS,%rcx
831 * Set return value to zero. Remaining failure mode goes through
837 * Set up arguments for MEMMOVE.
847 MEMMOVE erms=\erms overlap=0 begin=COPYINOUT_BEGIN end=COPYINOUT_SMAP_END
849 MEMMOVE erms=\erms overlap=0 begin=COPYINOUT_BEGIN end=COPYINOUT_END
854 ENTRY(copyout_nosmap_std)
855 COPYOUT smap=0 erms=0
856 END(copyout_nosmap_std)
858 ENTRY(copyout_smap_std)
859 COPYOUT smap=1 erms=0
860 END(copyout_smap_std)
862 ENTRY(copyout_nosmap_erms)
863 COPYOUT smap=0 erms=1
864 END(copyout_nosmap_erms)
866 ENTRY(copyout_smap_erms)
867 COPYOUT smap=1 erms=1
868 END(copyout_smap_erms)
871 * copyin(from_user, to_kernel, len)
874 .macro COPYIN smap erms
876 movq PCPU(CURPCB),%r11
877 movq $copy_fault,PCB_ONFAULT(%r11)
880 * make sure address is valid
885 movq $VM_MAXUSER_ADDRESS,%rcx
898 MEMMOVE erms=\erms overlap=0 begin=COPYINOUT_BEGIN end=COPYINOUT_SMAP_END
900 MEMMOVE erms=\erms overlap=0 begin=COPYINOUT_BEGIN end=COPYINOUT_END
905 ENTRY(copyin_nosmap_std)
907 END(copyin_nosmap_std)
909 ENTRY(copyin_smap_std)
913 ENTRY(copyin_nosmap_erms)
915 END(copyin_nosmap_erms)
917 ENTRY(copyin_smap_erms)
919 END(copyin_smap_erms)
922 /* Trap entry clears PSL.AC */
924 movq $0,PCB_ONFAULT(%r11)
930 * casueword32. Compare and set user integer. Returns -1 on fault,
931 * 0 if access was successful. Old value is written to *oldp.
932 * dst = %rdi, old = %esi, oldp = %rdx, new = %ecx
934 ENTRY(casueword32_nosmap)
936 movq PCPU(CURPCB),%r8
937 movq $fusufault,PCB_ONFAULT(%r8)
939 movq $VM_MAXUSER_ADDRESS-4,%rax
940 cmpq %rax,%rdi /* verify address is valid */
943 movl %esi,%eax /* old */
947 cmpxchgl %ecx,(%rdi) /* new = %ecx */
951 * The old value is in %eax. If the store succeeded it will be the
952 * value we expected (old) from before the store, otherwise it will
953 * be the current value. Save %eax into %esi to prepare the return
958 movq %rax,PCB_ONFAULT(%r8)
961 * Access the oldp after the pcb_onfault is cleared, to correctly
962 * catch corrupted pointer.
964 movl %esi,(%rdx) /* oldp = %rdx */
968 END(casueword32_nosmap)
970 ENTRY(casueword32_smap)
972 movq PCPU(CURPCB),%r8
973 movq $fusufault,PCB_ONFAULT(%r8)
975 movq $VM_MAXUSER_ADDRESS-4,%rax
976 cmpq %rax,%rdi /* verify address is valid */
979 movl %esi,%eax /* old */
984 cmpxchgl %ecx,(%rdi) /* new = %ecx */
989 * The old value is in %eax. If the store succeeded it will be the
990 * value we expected (old) from before the store, otherwise it will
991 * be the current value. Save %eax into %esi to prepare the return
996 movq %rax,PCB_ONFAULT(%r8)
999 * Access the oldp after the pcb_onfault is cleared, to correctly
1000 * catch corrupted pointer.
1002 movl %esi,(%rdx) /* oldp = %rdx */
1006 END(casueword32_smap)
1009 * casueword. Compare and set user long. Returns -1 on fault,
1010 * 0 if access was successful. Old value is written to *oldp.
1011 * dst = %rdi, old = %rsi, oldp = %rdx, new = %rcx
1013 ENTRY(casueword_nosmap)
1015 movq PCPU(CURPCB),%r8
1016 movq $fusufault,PCB_ONFAULT(%r8)
1018 movq $VM_MAXUSER_ADDRESS-4,%rax
1019 cmpq %rax,%rdi /* verify address is valid */
1022 movq %rsi,%rax /* old */
1026 cmpxchgq %rcx,(%rdi) /* new = %rcx */
1030 * The old value is in %rax. If the store succeeded it will be the
1031 * value we expected (old) from before the store, otherwise it will
1032 * be the current value.
1036 movq %rax,PCB_ONFAULT(%r8)
1041 END(casueword_nosmap)
1043 ENTRY(casueword_smap)
1045 movq PCPU(CURPCB),%r8
1046 movq $fusufault,PCB_ONFAULT(%r8)
1048 movq $VM_MAXUSER_ADDRESS-4,%rax
1049 cmpq %rax,%rdi /* verify address is valid */
1052 movq %rsi,%rax /* old */
1057 cmpxchgq %rcx,(%rdi) /* new = %rcx */
1062 * The old value is in %rax. If the store succeeded it will be the
1063 * value we expected (old) from before the store, otherwise it will
1064 * be the current value.
1068 movq %rax,PCB_ONFAULT(%r8)
1076 * Fetch (load) a 64-bit word, a 32-bit word, a 16-bit word, or an 8-bit
1077 * byte from user memory.
1078 * addr = %rdi, valp = %rsi
1081 ENTRY(fueword_nosmap)
1083 movq PCPU(CURPCB),%rcx
1084 movq $fusufault,PCB_ONFAULT(%rcx)
1086 movq $VM_MAXUSER_ADDRESS-8,%rax
1087 cmpq %rax,%rdi /* verify address is valid */
1092 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 is valid */
1111 movq %rax,PCB_ONFAULT(%rcx)
1117 ENTRY(fueword32_nosmap)
1119 movq PCPU(CURPCB),%rcx
1120 movq $fusufault,PCB_ONFAULT(%rcx)
1122 movq $VM_MAXUSER_ADDRESS-4,%rax
1123 cmpq %rax,%rdi /* verify address is valid */
1128 movq %rax,PCB_ONFAULT(%rcx)
1132 END(fueword32_nosmap)
1134 ENTRY(fueword32_smap)
1136 movq PCPU(CURPCB),%rcx
1137 movq $fusufault,PCB_ONFAULT(%rcx)
1139 movq $VM_MAXUSER_ADDRESS-4,%rax
1140 cmpq %rax,%rdi /* verify address is valid */
1147 movq %rax,PCB_ONFAULT(%rcx)
1153 ENTRY(fuword16_nosmap)
1155 movq PCPU(CURPCB),%rcx
1156 movq $fusufault,PCB_ONFAULT(%rcx)
1158 movq $VM_MAXUSER_ADDRESS-2,%rax
1163 movq $0,PCB_ONFAULT(%rcx)
1166 END(fuword16_nosmap)
1168 ENTRY(fuword16_smap)
1170 movq PCPU(CURPCB),%rcx
1171 movq $fusufault,PCB_ONFAULT(%rcx)
1173 movq $VM_MAXUSER_ADDRESS-2,%rax
1180 movq $0,PCB_ONFAULT(%rcx)
1185 ENTRY(fubyte_nosmap)
1187 movq PCPU(CURPCB),%rcx
1188 movq $fusufault,PCB_ONFAULT(%rcx)
1190 movq $VM_MAXUSER_ADDRESS-1,%rax
1195 movq $0,PCB_ONFAULT(%rcx)
1202 movq PCPU(CURPCB),%rcx
1203 movq $fusufault,PCB_ONFAULT(%rcx)
1205 movq $VM_MAXUSER_ADDRESS-1,%rax
1212 movq $0,PCB_ONFAULT(%rcx)
1218 * Store a 64-bit word, a 32-bit word, a 16-bit word, or an 8-bit byte to
1220 * addr = %rdi, value = %rsi
1222 ENTRY(suword_nosmap)
1224 movq PCPU(CURPCB),%rcx
1225 movq $fusufault,PCB_ONFAULT(%rcx)
1227 movq $VM_MAXUSER_ADDRESS-8,%rax
1228 cmpq %rax,%rdi /* verify address validity */
1233 movq %rax,PCB_ONFAULT(%rcx)
1240 movq PCPU(CURPCB),%rcx
1241 movq $fusufault,PCB_ONFAULT(%rcx)
1243 movq $VM_MAXUSER_ADDRESS-8,%rax
1244 cmpq %rax,%rdi /* verify address validity */
1251 movq %rax,PCB_ONFAULT(%rcx)
1256 ENTRY(suword32_nosmap)
1258 movq PCPU(CURPCB),%rcx
1259 movq $fusufault,PCB_ONFAULT(%rcx)
1261 movq $VM_MAXUSER_ADDRESS-4,%rax
1262 cmpq %rax,%rdi /* verify address validity */
1267 movq %rax,PCB_ONFAULT(%rcx)
1270 END(suword32_nosmap)
1272 ENTRY(suword32_smap)
1274 movq PCPU(CURPCB),%rcx
1275 movq $fusufault,PCB_ONFAULT(%rcx)
1277 movq $VM_MAXUSER_ADDRESS-4,%rax
1278 cmpq %rax,%rdi /* verify address validity */
1285 movq %rax,PCB_ONFAULT(%rcx)
1290 ENTRY(suword16_nosmap)
1292 movq PCPU(CURPCB),%rcx
1293 movq $fusufault,PCB_ONFAULT(%rcx)
1295 movq $VM_MAXUSER_ADDRESS-2,%rax
1296 cmpq %rax,%rdi /* verify address validity */
1301 movq %rax,PCB_ONFAULT(%rcx)
1304 END(suword16_nosmap)
1306 ENTRY(suword16_smap)
1308 movq PCPU(CURPCB),%rcx
1309 movq $fusufault,PCB_ONFAULT(%rcx)
1311 movq $VM_MAXUSER_ADDRESS-2,%rax
1312 cmpq %rax,%rdi /* verify address validity */
1319 movq %rax,PCB_ONFAULT(%rcx)
1324 ENTRY(subyte_nosmap)
1326 movq PCPU(CURPCB),%rcx
1327 movq $fusufault,PCB_ONFAULT(%rcx)
1329 movq $VM_MAXUSER_ADDRESS-1,%rax
1330 cmpq %rax,%rdi /* verify address validity */
1336 movq %rax,PCB_ONFAULT(%rcx)
1343 movq PCPU(CURPCB),%rcx
1344 movq $fusufault,PCB_ONFAULT(%rcx)
1346 movq $VM_MAXUSER_ADDRESS-1,%rax
1347 cmpq %rax,%rdi /* verify address validity */
1355 movq %rax,PCB_ONFAULT(%rcx)
1361 /* Fault entry clears PSL.AC */
1363 movq PCPU(CURPCB),%rcx
1365 movq %rax,PCB_ONFAULT(%rcx)
1371 * copyinstr(from, to, maxlen, int *lencopied)
1372 * %rdi, %rsi, %rdx, %rcx
1374 * copy a string from 'from' to 'to', stop when a 0 character is reached.
1375 * return ENAMETOOLONG if string is longer than maxlen, and
1376 * EFAULT on protection violations. If lencopied is non-zero,
1377 * return the actual length in *lencopied.
1379 .macro COPYINSTR smap
1381 movq %rdx,%r8 /* %r8 = maxlen */
1382 movq PCPU(CURPCB),%r9
1383 movq $cpystrflt,PCB_ONFAULT(%r9)
1385 movq $VM_MAXUSER_ADDRESS,%rax
1387 /* make sure 'from' is within bounds */
1393 /* restrict maxlen to <= VM_MAXUSER_ADDRESS-from */
1401 jz copyinstr_toolong
1403 jz copyinstr_toolong_smap
1415 /* Success -- 0 byte reached */
1419 /* set *lencopied and return %eax */
1420 movq %rax,PCB_ONFAULT(%r9)
1437 ENTRY(copyinstr_nosmap)
1439 END(copyinstr_nosmap)
1441 ENTRY(copyinstr_smap)
1446 /* Fault entry clears PSL.AC */
1449 /* set *lencopied and return %eax */
1450 movq $0,PCB_ONFAULT(%r9)
1460 copyinstr_toolong_smap:
1463 /* rdx is zero - return ENAMETOOLONG or EFAULT */
1464 movq $VM_MAXUSER_ADDRESS,%rax
1467 movl $ENAMETOOLONG,%eax
1471 * Handling of special amd64 registers and descriptor tables etc
1473 /* void lgdt(struct region_descriptor *rdp); */
1475 /* reload the descriptor table */
1478 /* flush the prefetch q */
1485 movl %eax,%fs /* Beware, use wrmsr to set 64 bit base */
1489 /* reload code selector by turning return into intersegmental return */
1497 /*****************************************************************************/
1498 /* setjump, longjump */
1499 /*****************************************************************************/
1502 movq %rbx,0(%rdi) /* save rbx */
1503 movq %rsp,8(%rdi) /* save rsp */
1504 movq %rbp,16(%rdi) /* save rbp */
1505 movq %r12,24(%rdi) /* save r12 */
1506 movq %r13,32(%rdi) /* save r13 */
1507 movq %r14,40(%rdi) /* save r14 */
1508 movq %r15,48(%rdi) /* save r15 */
1509 movq 0(%rsp),%rdx /* get rta */
1510 movq %rdx,56(%rdi) /* save rip */
1511 xorl %eax,%eax /* return(0); */
1516 movq 0(%rdi),%rbx /* restore rbx */
1517 movq 8(%rdi),%rsp /* restore rsp */
1518 movq 16(%rdi),%rbp /* restore rbp */
1519 movq 24(%rdi),%r12 /* restore r12 */
1520 movq 32(%rdi),%r13 /* restore r13 */
1521 movq 40(%rdi),%r14 /* restore r14 */
1522 movq 48(%rdi),%r15 /* restore r15 */
1523 movq 56(%rdi),%rdx /* get rta */
1524 movq %rdx,0(%rsp) /* put in return frame */
1525 xorl %eax,%eax /* return(1); */
1531 * Support for reading MSRs in the safe manner. (Instead of panic on #gp,
1535 /* int rdmsr_safe(u_int msr, uint64_t *data) */
1537 movq PCPU(CURPCB),%r8
1538 movq $msr_onfault,PCB_ONFAULT(%r8)
1540 rdmsr /* Read MSR pointed by %ecx. Returns
1541 hi byte in edx, lo in %eax */
1542 salq $32,%rdx /* sign-shift %rdx left */
1543 movl %eax,%eax /* zero-extend %eax -> %rax */
1547 movq %rax,PCB_ONFAULT(%r8)
1552 * Support for writing MSRs in the safe manner. (Instead of panic on #gp,
1556 /* int wrmsr_safe(u_int msr, uint64_t data) */
1558 movq PCPU(CURPCB),%r8
1559 movq $msr_onfault,PCB_ONFAULT(%r8)
1564 wrmsr /* Write MSR pointed by %ecx. Accepts
1565 hi byte in edx, lo in %eax. */
1567 movq %rax,PCB_ONFAULT(%r8)
1572 * MSR operations fault handler
1576 movq $0,PCB_ONFAULT(%r8)
1582 * void pmap_pti_pcid_invalidate(uint64_t ucr3, uint64_t kcr3);
1583 * Invalidates address space addressed by ucr3, then returns to kcr3.
1584 * Done in assembler to ensure no other memory accesses happen while
1588 ENTRY(pmap_pti_pcid_invalidate)
1591 movq %rdi,%cr3 /* to user page table */
1592 movq %rsi,%cr3 /* back to kernel */
1597 * void pmap_pti_pcid_invlpg(uint64_t ucr3, uint64_t kcr3, vm_offset_t va);
1598 * Invalidates virtual address va in address space ucr3, then returns to kcr3.
1601 ENTRY(pmap_pti_pcid_invlpg)
1604 movq %rdi,%cr3 /* to user page table */
1606 movq %rsi,%cr3 /* back to kernel */
1611 * void pmap_pti_pcid_invlrng(uint64_t ucr3, uint64_t kcr3, vm_offset_t sva,
1613 * Invalidates virtual addresses between sva and eva in address space ucr3,
1614 * then returns to kcr3.
1617 ENTRY(pmap_pti_pcid_invlrng)
1620 movq %rdi,%cr3 /* to user page table */
1622 addq $PAGE_SIZE,%rdx
1625 movq %rsi,%cr3 /* back to kernel */
1630 .macro rsb_seq_label l
1633 .macro rsb_call_label l
1636 .macro rsb_seq count
1639 rsb_call_label %(ll)
1651 /* all callers already saved %rax, %rdx, and %rcx */
1652 ENTRY(handle_ibrs_entry)
1653 cmpb $0,hw_ibrs_ibpb_active(%rip)
1655 movl $MSR_IA32_SPEC_CTRL,%ecx
1657 orl $(IA32_SPEC_CTRL_IBRS|IA32_SPEC_CTRL_STIBP),%eax
1658 orl $(IA32_SPEC_CTRL_IBRS|IA32_SPEC_CTRL_STIBP)>>32,%edx
1660 movb $1,PCPU(IBPB_SET)
1661 testl $CPUID_STDEXT_SMEP,cpu_stdext_feature(%rip)
1664 END(handle_ibrs_entry)
1666 ENTRY(handle_ibrs_exit)
1667 cmpb $0,PCPU(IBPB_SET)
1669 movl $MSR_IA32_SPEC_CTRL,%ecx
1671 andl $~(IA32_SPEC_CTRL_IBRS|IA32_SPEC_CTRL_STIBP),%eax
1672 andl $~((IA32_SPEC_CTRL_IBRS|IA32_SPEC_CTRL_STIBP)>>32),%edx
1674 movb $0,PCPU(IBPB_SET)
1676 END(handle_ibrs_exit)
1678 /* registers-neutral version, but needs stack */
1679 ENTRY(handle_ibrs_exit_rs)
1680 cmpb $0,PCPU(IBPB_SET)
1685 movl $MSR_IA32_SPEC_CTRL,%ecx
1687 andl $~(IA32_SPEC_CTRL_IBRS|IA32_SPEC_CTRL_STIBP),%eax
1688 andl $~((IA32_SPEC_CTRL_IBRS|IA32_SPEC_CTRL_STIBP)>>32),%edx
1693 movb $0,PCPU(IBPB_SET)
1695 END(handle_ibrs_exit_rs)
1700 * Flush L1D cache. Load enough of the data from the kernel text
1701 * to flush existing L1D content.
1703 * N.B. The function does not follow ABI calling conventions, it corrupts %rbx.
1704 * The vmm.ko caller expects that only %rax, %rdx, %rbx, %rcx, %r9, and %rflags
1705 * registers are clobbered. The NMI handler caller only needs %r13 and %r15
1709 #define L1D_FLUSH_SIZE (64 * 1024)
1711 movq $-L1D_FLUSH_SIZE, %rcx
1713 * pass 1: Preload TLB.
1714 * Kernel text is mapped using superpages. TLB preload is
1715 * done for the benefit of older CPUs which split 2M page
1716 * into 4k TLB entries.
1718 1: movb L1D_FLUSH_SIZE(%r9, %rcx), %al
1719 addq $PAGE_SIZE, %rcx
1723 movq $-L1D_FLUSH_SIZE, %rcx
1724 /* pass 2: Read each cache line. */
1725 2: movb L1D_FLUSH_SIZE(%r9, %rcx), %al
1730 #undef L1D_FLUSH_SIZE
1733 ENTRY(flush_l1d_sw_abi)
1738 END(flush_l1d_sw_abi)
1740 ENTRY(mds_handler_void)
1742 END(mds_handler_void)
1744 ENTRY(mds_handler_verw)
1750 END(mds_handler_verw)
1752 ENTRY(mds_handler_ivb)
1761 1: movq PCPU(MDS_BUF), %rdx
1762 movdqa %xmm0, PCPU(MDS_TMP)
1771 2: movntdq %xmm0, (%rdx)
1777 movdqa PCPU(MDS_TMP),%xmm0
1785 END(mds_handler_ivb)
1787 ENTRY(mds_handler_bdw)
1798 1: movq PCPU(MDS_BUF), %rbx
1799 movdqa %xmm0, PCPU(MDS_TMP)
1805 2: movntdq %xmm0, (%rbx)
1814 movdqa PCPU(MDS_TMP),%xmm0
1824 END(mds_handler_bdw)
1826 ENTRY(mds_handler_skl_sse)
1836 1: movq PCPU(MDS_BUF), %rdi
1837 movq PCPU(MDS_BUF64), %rdx
1838 movdqa %xmm0, PCPU(MDS_TMP)
1845 2: clflushopt 5376(%rdi, %rax, 8)
1855 movdqa PCPU(MDS_TMP), %xmm0
1864 END(mds_handler_skl_sse)
1866 ENTRY(mds_handler_skl_avx)
1876 1: movq PCPU(MDS_BUF), %rdi
1877 movq PCPU(MDS_BUF64), %rdx
1878 vmovdqa %ymm0, PCPU(MDS_TMP)
1879 vpxor %ymm0, %ymm0, %ymm0
1882 vorpd (%rdx), %ymm0, %ymm0
1883 vorpd (%rdx), %ymm0, %ymm0
1885 2: clflushopt 5376(%rdi, %rax, 8)
1895 vmovdqa PCPU(MDS_TMP), %ymm0
1904 END(mds_handler_skl_avx)
1906 ENTRY(mds_handler_skl_avx512)
1916 1: movq PCPU(MDS_BUF), %rdi
1917 movq PCPU(MDS_BUF64), %rdx
1918 vmovdqa64 %zmm0, PCPU(MDS_TMP)
1919 vpxord %zmm0, %zmm0, %zmm0
1922 vorpd (%rdx), %zmm0, %zmm0
1923 vorpd (%rdx), %zmm0, %zmm0
1925 2: clflushopt 5376(%rdi, %rax, 8)
1935 vmovdqa64 PCPU(MDS_TMP), %zmm0
1944 END(mds_handler_skl_avx512)
1946 ENTRY(mds_handler_silvermont)
1955 1: movq PCPU(MDS_BUF), %rdx
1956 movdqa %xmm0, PCPU(MDS_TMP)
1960 2: movntdq %xmm0, (%rdx)
1966 movdqa PCPU(MDS_TMP),%xmm0
1974 END(mds_handler_silvermont)