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
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)
923 testl $CPUID_STDEXT_SMAP,cpu_stdext_feature(%rip)
926 1: movq $0,PCB_ONFAULT(%r11)
932 * casueword32. Compare and set user integer. Returns -1 on fault,
933 * 0 if access was successful. Old value is written to *oldp.
934 * dst = %rdi, old = %esi, oldp = %rdx, new = %ecx
936 ENTRY(casueword32_nosmap)
938 movq PCPU(CURPCB),%r8
939 movq $fusufault,PCB_ONFAULT(%r8)
941 movq $VM_MAXUSER_ADDRESS-4,%rax
942 cmpq %rax,%rdi /* verify address is valid */
945 movl %esi,%eax /* old */
949 cmpxchgl %ecx,(%rdi) /* new = %ecx */
953 * The old value is in %eax. If the store succeeded it will be the
954 * value we expected (old) from before the store, otherwise it will
955 * be the current value. Save %eax into %esi to prepare the return
960 movq %rax,PCB_ONFAULT(%r8)
963 * Access the oldp after the pcb_onfault is cleared, to correctly
964 * catch corrupted pointer.
966 movl %esi,(%rdx) /* oldp = %rdx */
970 END(casueword32_nosmap)
972 ENTRY(casueword32_smap)
974 movq PCPU(CURPCB),%r8
975 movq $fusufault,PCB_ONFAULT(%r8)
977 movq $VM_MAXUSER_ADDRESS-4,%rax
978 cmpq %rax,%rdi /* verify address is valid */
981 movl %esi,%eax /* old */
986 cmpxchgl %ecx,(%rdi) /* new = %ecx */
991 * The old value is in %eax. If the store succeeded it will be the
992 * value we expected (old) from before the store, otherwise it will
993 * be the current value. Save %eax into %esi to prepare the return
998 movq %rax,PCB_ONFAULT(%r8)
1001 * Access the oldp after the pcb_onfault is cleared, to correctly
1002 * catch corrupted pointer.
1004 movl %esi,(%rdx) /* oldp = %rdx */
1008 END(casueword32_smap)
1011 * casueword. Compare and set user long. Returns -1 on fault,
1012 * 0 if access was successful. Old value is written to *oldp.
1013 * dst = %rdi, old = %rsi, oldp = %rdx, new = %rcx
1015 ENTRY(casueword_nosmap)
1017 movq PCPU(CURPCB),%r8
1018 movq $fusufault,PCB_ONFAULT(%r8)
1020 movq $VM_MAXUSER_ADDRESS-4,%rax
1021 cmpq %rax,%rdi /* verify address is valid */
1024 movq %rsi,%rax /* old */
1028 cmpxchgq %rcx,(%rdi) /* new = %rcx */
1032 * The old value is in %rax. If the store succeeded it will be the
1033 * value we expected (old) from before the store, otherwise it will
1034 * be the current value.
1038 movq %rax,PCB_ONFAULT(%r8)
1043 END(casueword_nosmap)
1045 ENTRY(casueword_smap)
1047 movq PCPU(CURPCB),%r8
1048 movq $fusufault,PCB_ONFAULT(%r8)
1050 movq $VM_MAXUSER_ADDRESS-4,%rax
1051 cmpq %rax,%rdi /* verify address is valid */
1054 movq %rsi,%rax /* old */
1059 cmpxchgq %rcx,(%rdi) /* new = %rcx */
1064 * The old value is in %rax. If the store succeeded it will be the
1065 * value we expected (old) from before the store, otherwise it will
1066 * be the current value.
1070 movq %rax,PCB_ONFAULT(%r8)
1078 * Fetch (load) a 64-bit word, a 32-bit word, a 16-bit word, or an 8-bit
1079 * byte from user memory.
1080 * addr = %rdi, valp = %rsi
1083 ENTRY(fueword_nosmap)
1085 movq PCPU(CURPCB),%rcx
1086 movq $fusufault,PCB_ONFAULT(%rcx)
1088 movq $VM_MAXUSER_ADDRESS-8,%rax
1089 cmpq %rax,%rdi /* verify address is valid */
1094 movq %rax,PCB_ONFAULT(%rcx)
1102 movq PCPU(CURPCB),%rcx
1103 movq $fusufault,PCB_ONFAULT(%rcx)
1105 movq $VM_MAXUSER_ADDRESS-8,%rax
1106 cmpq %rax,%rdi /* verify address is valid */
1113 movq %rax,PCB_ONFAULT(%rcx)
1119 ENTRY(fueword32_nosmap)
1121 movq PCPU(CURPCB),%rcx
1122 movq $fusufault,PCB_ONFAULT(%rcx)
1124 movq $VM_MAXUSER_ADDRESS-4,%rax
1125 cmpq %rax,%rdi /* verify address is valid */
1130 movq %rax,PCB_ONFAULT(%rcx)
1134 END(fueword32_nosmap)
1136 ENTRY(fueword32_smap)
1138 movq PCPU(CURPCB),%rcx
1139 movq $fusufault,PCB_ONFAULT(%rcx)
1141 movq $VM_MAXUSER_ADDRESS-4,%rax
1142 cmpq %rax,%rdi /* verify address is valid */
1149 movq %rax,PCB_ONFAULT(%rcx)
1155 ENTRY(fuword16_nosmap)
1157 movq PCPU(CURPCB),%rcx
1158 movq $fusufault,PCB_ONFAULT(%rcx)
1160 movq $VM_MAXUSER_ADDRESS-2,%rax
1165 movq $0,PCB_ONFAULT(%rcx)
1168 END(fuword16_nosmap)
1170 ENTRY(fuword16_smap)
1172 movq PCPU(CURPCB),%rcx
1173 movq $fusufault,PCB_ONFAULT(%rcx)
1175 movq $VM_MAXUSER_ADDRESS-2,%rax
1182 movq $0,PCB_ONFAULT(%rcx)
1187 ENTRY(fubyte_nosmap)
1189 movq PCPU(CURPCB),%rcx
1190 movq $fusufault,PCB_ONFAULT(%rcx)
1192 movq $VM_MAXUSER_ADDRESS-1,%rax
1197 movq $0,PCB_ONFAULT(%rcx)
1204 movq PCPU(CURPCB),%rcx
1205 movq $fusufault,PCB_ONFAULT(%rcx)
1207 movq $VM_MAXUSER_ADDRESS-1,%rax
1214 movq $0,PCB_ONFAULT(%rcx)
1220 * Store a 64-bit word, a 32-bit word, a 16-bit word, or an 8-bit byte to
1222 * addr = %rdi, value = %rsi
1224 ENTRY(suword_nosmap)
1226 movq PCPU(CURPCB),%rcx
1227 movq $fusufault,PCB_ONFAULT(%rcx)
1229 movq $VM_MAXUSER_ADDRESS-8,%rax
1230 cmpq %rax,%rdi /* verify address validity */
1235 movq %rax,PCB_ONFAULT(%rcx)
1242 movq PCPU(CURPCB),%rcx
1243 movq $fusufault,PCB_ONFAULT(%rcx)
1245 movq $VM_MAXUSER_ADDRESS-8,%rax
1246 cmpq %rax,%rdi /* verify address validity */
1253 movq %rax,PCB_ONFAULT(%rcx)
1258 ENTRY(suword32_nosmap)
1260 movq PCPU(CURPCB),%rcx
1261 movq $fusufault,PCB_ONFAULT(%rcx)
1263 movq $VM_MAXUSER_ADDRESS-4,%rax
1264 cmpq %rax,%rdi /* verify address validity */
1269 movq %rax,PCB_ONFAULT(%rcx)
1272 END(suword32_nosmap)
1274 ENTRY(suword32_smap)
1276 movq PCPU(CURPCB),%rcx
1277 movq $fusufault,PCB_ONFAULT(%rcx)
1279 movq $VM_MAXUSER_ADDRESS-4,%rax
1280 cmpq %rax,%rdi /* verify address validity */
1287 movq %rax,PCB_ONFAULT(%rcx)
1292 ENTRY(suword16_nosmap)
1294 movq PCPU(CURPCB),%rcx
1295 movq $fusufault,PCB_ONFAULT(%rcx)
1297 movq $VM_MAXUSER_ADDRESS-2,%rax
1298 cmpq %rax,%rdi /* verify address validity */
1303 movq %rax,PCB_ONFAULT(%rcx)
1306 END(suword16_nosmap)
1308 ENTRY(suword16_smap)
1310 movq PCPU(CURPCB),%rcx
1311 movq $fusufault,PCB_ONFAULT(%rcx)
1313 movq $VM_MAXUSER_ADDRESS-2,%rax
1314 cmpq %rax,%rdi /* verify address validity */
1321 movq %rax,PCB_ONFAULT(%rcx)
1326 ENTRY(subyte_nosmap)
1328 movq PCPU(CURPCB),%rcx
1329 movq $fusufault,PCB_ONFAULT(%rcx)
1331 movq $VM_MAXUSER_ADDRESS-1,%rax
1332 cmpq %rax,%rdi /* verify address validity */
1338 movq %rax,PCB_ONFAULT(%rcx)
1345 movq PCPU(CURPCB),%rcx
1346 movq $fusufault,PCB_ONFAULT(%rcx)
1348 movq $VM_MAXUSER_ADDRESS-1,%rax
1349 cmpq %rax,%rdi /* verify address validity */
1357 movq %rax,PCB_ONFAULT(%rcx)
1364 testl $CPUID_STDEXT_SMAP,cpu_stdext_feature(%rip)
1367 1: movq PCPU(CURPCB),%rcx
1369 movq %rax,PCB_ONFAULT(%rcx)
1375 * copyinstr(from, to, maxlen, int *lencopied)
1376 * %rdi, %rsi, %rdx, %rcx
1378 * copy a string from 'from' to 'to', stop when a 0 character is reached.
1379 * return ENAMETOOLONG if string is longer than maxlen, and
1380 * EFAULT on protection violations. If lencopied is non-zero,
1381 * return the actual length in *lencopied.
1383 .macro COPYINSTR smap
1385 movq %rdx,%r8 /* %r8 = maxlen */
1386 movq PCPU(CURPCB),%r9
1387 movq $cpystrflt,PCB_ONFAULT(%r9)
1389 movq $VM_MAXUSER_ADDRESS,%rax
1391 /* make sure 'from' is within bounds */
1397 /* restrict maxlen to <= VM_MAXUSER_ADDRESS-from */
1405 jz copyinstr_toolong
1407 jz copyinstr_toolong_smap
1419 /* Success -- 0 byte reached */
1423 /* set *lencopied and return %eax */
1424 movq %rax,PCB_ONFAULT(%r9)
1441 ENTRY(copyinstr_nosmap)
1443 END(copyinstr_nosmap)
1445 ENTRY(copyinstr_smap)
1450 testl $CPUID_STDEXT_SMAP,cpu_stdext_feature(%rip)
1453 1: movl $EFAULT,%eax
1455 /* set *lencopied and return %eax */
1456 movq $0,PCB_ONFAULT(%r9)
1466 copyinstr_toolong_smap:
1469 /* rdx is zero - return ENAMETOOLONG or EFAULT */
1470 movq $VM_MAXUSER_ADDRESS,%rax
1473 movl $ENAMETOOLONG,%eax
1477 * Handling of special amd64 registers and descriptor tables etc
1479 /* void lgdt(struct region_descriptor *rdp); */
1481 /* reload the descriptor table */
1484 /* flush the prefetch q */
1491 movl %eax,%fs /* Beware, use wrmsr to set 64 bit base */
1495 /* reload code selector by turning return into intersegmental return */
1503 /*****************************************************************************/
1504 /* setjump, longjump */
1505 /*****************************************************************************/
1508 movq %rbx,0(%rdi) /* save rbx */
1509 movq %rsp,8(%rdi) /* save rsp */
1510 movq %rbp,16(%rdi) /* save rbp */
1511 movq %r12,24(%rdi) /* save r12 */
1512 movq %r13,32(%rdi) /* save r13 */
1513 movq %r14,40(%rdi) /* save r14 */
1514 movq %r15,48(%rdi) /* save r15 */
1515 movq 0(%rsp),%rdx /* get rta */
1516 movq %rdx,56(%rdi) /* save rip */
1517 xorl %eax,%eax /* return(0); */
1522 movq 0(%rdi),%rbx /* restore rbx */
1523 movq 8(%rdi),%rsp /* restore rsp */
1524 movq 16(%rdi),%rbp /* restore rbp */
1525 movq 24(%rdi),%r12 /* restore r12 */
1526 movq 32(%rdi),%r13 /* restore r13 */
1527 movq 40(%rdi),%r14 /* restore r14 */
1528 movq 48(%rdi),%r15 /* restore r15 */
1529 movq 56(%rdi),%rdx /* get rta */
1530 movq %rdx,0(%rsp) /* put in return frame */
1531 xorl %eax,%eax /* return(1); */
1537 * Support for reading MSRs in the safe manner. (Instead of panic on #gp,
1541 /* int rdmsr_safe(u_int msr, uint64_t *data) */
1543 movq PCPU(CURPCB),%r8
1544 movq $msr_onfault,PCB_ONFAULT(%r8)
1546 rdmsr /* Read MSR pointed by %ecx. Returns
1547 hi byte in edx, lo in %eax */
1548 salq $32,%rdx /* sign-shift %rdx left */
1549 movl %eax,%eax /* zero-extend %eax -> %rax */
1553 movq %rax,PCB_ONFAULT(%r8)
1558 * Support for writing MSRs in the safe manner. (Instead of panic on #gp,
1562 /* int wrmsr_safe(u_int msr, uint64_t data) */
1564 movq PCPU(CURPCB),%r8
1565 movq $msr_onfault,PCB_ONFAULT(%r8)
1570 wrmsr /* Write MSR pointed by %ecx. Accepts
1571 hi byte in edx, lo in %eax. */
1573 movq %rax,PCB_ONFAULT(%r8)
1578 * MSR operations fault handler
1582 movq $0,PCB_ONFAULT(%r8)
1588 * void pmap_pti_pcid_invalidate(uint64_t ucr3, uint64_t kcr3);
1589 * Invalidates address space addressed by ucr3, then returns to kcr3.
1590 * Done in assembler to ensure no other memory accesses happen while
1594 ENTRY(pmap_pti_pcid_invalidate)
1597 movq %rdi,%cr3 /* to user page table */
1598 movq %rsi,%cr3 /* back to kernel */
1603 * void pmap_pti_pcid_invlpg(uint64_t ucr3, uint64_t kcr3, vm_offset_t va);
1604 * Invalidates virtual address va in address space ucr3, then returns to kcr3.
1607 ENTRY(pmap_pti_pcid_invlpg)
1610 movq %rdi,%cr3 /* to user page table */
1612 movq %rsi,%cr3 /* back to kernel */
1617 * void pmap_pti_pcid_invlrng(uint64_t ucr3, uint64_t kcr3, vm_offset_t sva,
1619 * Invalidates virtual addresses between sva and eva in address space ucr3,
1620 * then returns to kcr3.
1623 ENTRY(pmap_pti_pcid_invlrng)
1626 movq %rdi,%cr3 /* to user page table */
1628 addq $PAGE_SIZE,%rdx
1631 movq %rsi,%cr3 /* back to kernel */
1636 .macro rsb_seq_label l
1639 .macro rsb_call_label l
1642 .macro rsb_seq count
1645 rsb_call_label %(ll)
1657 /* all callers already saved %rax, %rdx, and %rcx */
1658 ENTRY(handle_ibrs_entry)
1659 cmpb $0,hw_ibrs_ibpb_active(%rip)
1661 movl $MSR_IA32_SPEC_CTRL,%ecx
1663 orl $(IA32_SPEC_CTRL_IBRS|IA32_SPEC_CTRL_STIBP),%eax
1664 orl $(IA32_SPEC_CTRL_IBRS|IA32_SPEC_CTRL_STIBP)>>32,%edx
1666 movb $1,PCPU(IBPB_SET)
1667 testl $CPUID_STDEXT_SMEP,cpu_stdext_feature(%rip)
1670 END(handle_ibrs_entry)
1672 ENTRY(handle_ibrs_exit)
1673 cmpb $0,PCPU(IBPB_SET)
1675 movl $MSR_IA32_SPEC_CTRL,%ecx
1677 andl $~(IA32_SPEC_CTRL_IBRS|IA32_SPEC_CTRL_STIBP),%eax
1678 andl $~((IA32_SPEC_CTRL_IBRS|IA32_SPEC_CTRL_STIBP)>>32),%edx
1680 movb $0,PCPU(IBPB_SET)
1682 END(handle_ibrs_exit)
1684 /* registers-neutral version, but needs stack */
1685 ENTRY(handle_ibrs_exit_rs)
1686 cmpb $0,PCPU(IBPB_SET)
1691 movl $MSR_IA32_SPEC_CTRL,%ecx
1693 andl $~(IA32_SPEC_CTRL_IBRS|IA32_SPEC_CTRL_STIBP),%eax
1694 andl $~((IA32_SPEC_CTRL_IBRS|IA32_SPEC_CTRL_STIBP)>>32),%edx
1699 movb $0,PCPU(IBPB_SET)
1701 END(handle_ibrs_exit_rs)
1706 * Flush L1D cache. Load enough of the data from the kernel text
1707 * to flush existing L1D content.
1709 * N.B. The function does not follow ABI calling conventions, it corrupts %rbx.
1710 * The vmm.ko caller expects that only %rax, %rdx, %rbx, %rcx, %r9, and %rflags
1711 * registers are clobbered. The NMI handler caller only needs %r13 and %r15
1715 #define L1D_FLUSH_SIZE (64 * 1024)
1717 movq $-L1D_FLUSH_SIZE, %rcx
1719 * pass 1: Preload TLB.
1720 * Kernel text is mapped using superpages. TLB preload is
1721 * done for the benefit of older CPUs which split 2M page
1722 * into 4k TLB entries.
1724 1: movb L1D_FLUSH_SIZE(%r9, %rcx), %al
1725 addq $PAGE_SIZE, %rcx
1729 movq $-L1D_FLUSH_SIZE, %rcx
1730 /* pass 2: Read each cache line. */
1731 2: movb L1D_FLUSH_SIZE(%r9, %rcx), %al
1736 #undef L1D_FLUSH_SIZE
1739 ENTRY(flush_l1d_sw_abi)
1744 END(flush_l1d_sw_abi)
1746 ENTRY(mds_handler_void)
1748 END(mds_handler_void)
1750 ENTRY(mds_handler_verw)
1756 END(mds_handler_verw)
1758 ENTRY(mds_handler_ivb)
1767 1: movq PCPU(MDS_BUF), %rdx
1768 movdqa %xmm0, PCPU(MDS_TMP)
1777 2: movntdq %xmm0, (%rdx)
1783 movdqa PCPU(MDS_TMP),%xmm0
1791 END(mds_handler_ivb)
1793 ENTRY(mds_handler_bdw)
1804 1: movq PCPU(MDS_BUF), %rbx
1805 movdqa %xmm0, PCPU(MDS_TMP)
1811 2: movntdq %xmm0, (%rbx)
1820 movdqa PCPU(MDS_TMP),%xmm0
1830 END(mds_handler_bdw)
1832 ENTRY(mds_handler_skl_sse)
1842 1: movq PCPU(MDS_BUF), %rdi
1843 movq PCPU(MDS_BUF64), %rdx
1844 movdqa %xmm0, PCPU(MDS_TMP)
1851 2: clflushopt 5376(%rdi, %rax, 8)
1861 movdqa PCPU(MDS_TMP), %xmm0
1870 END(mds_handler_skl_sse)
1872 ENTRY(mds_handler_skl_avx)
1882 1: movq PCPU(MDS_BUF), %rdi
1883 movq PCPU(MDS_BUF64), %rdx
1884 vmovdqa %ymm0, PCPU(MDS_TMP)
1885 vpxor %ymm0, %ymm0, %ymm0
1888 vorpd (%rdx), %ymm0, %ymm0
1889 vorpd (%rdx), %ymm0, %ymm0
1891 2: clflushopt 5376(%rdi, %rax, 8)
1901 vmovdqa PCPU(MDS_TMP), %ymm0
1910 END(mds_handler_skl_avx)
1912 ENTRY(mds_handler_skl_avx512)
1922 1: movq PCPU(MDS_BUF), %rdi
1923 movq PCPU(MDS_BUF64), %rdx
1924 vmovdqa64 %zmm0, PCPU(MDS_TMP)
1925 vpxord %zmm0, %zmm0, %zmm0
1928 vorpd (%rdx), %zmm0, %zmm0
1929 vorpd (%rdx), %zmm0, %zmm0
1931 2: clflushopt 5376(%rdi, %rax, 8)
1941 vmovdqa64 PCPU(MDS_TMP), %zmm0
1950 END(mds_handler_skl_avx512)
1952 ENTRY(mds_handler_silvermont)
1961 1: movq PCPU(MDS_BUF), %rdx
1962 movdqa %xmm0, PCPU(MDS_TMP)
1966 2: movntdq %xmm0, (%rdx)
1972 movdqa PCPU(MDS_TMP),%xmm0
1980 END(mds_handler_silvermont)