2 * Copyright (c) 2003 Peter Wemm.
3 * Copyright (c) 1993 The Regents of the University of California.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <machine/asmacros.h>
36 #include <machine/specialreg.h>
37 #include <machine/pmap.h>
45 * void bzero(void *buf, u_int len)
67 movq $PAGE_SIZE/8,%rcx
83 * The loop takes 29 bytes. Ensure that it doesn't cross a 32-byte
88 movnti %rax,(%rdi,%rdx)
89 movnti %rax,8(%rdi,%rdx)
90 movnti %rax,16(%rdi,%rdx)
91 movnti %rax,24(%rdi,%rdx)
108 movzbl (%rdi,%rcx,1),%eax
109 movzbl (%rsi,%rcx,1),%r8d
142 * bcopy(src, dst, cnt)
144 * ws@tools.de (Wolfgang Solfrank, TooLs GmbH) +49-228-985800
153 cmpq %rcx,%rax /* overlapping && src < dst? */
156 shrq $3,%rcx /* copy by 64-bit words */
160 andq $7,%rcx /* any bytes left? */
168 addq %rcx,%rdi /* copy backwards */
172 andq $7,%rcx /* any fractional bytes? */
176 movq %rdx,%rcx /* copy remainder by 32-bit words */
188 * memmove(dst, src, cnt)
190 * Adapted from bcopy written by:
191 * ws@tools.de (Wolfgang Solfrank, TooLs GmbH) +49-228-985800
200 cmpq %rcx,%rax /* overlapping && src < dst? */
203 shrq $3,%rcx /* copy by 64-bit words */
207 andq $7,%rcx /* any bytes left? */
216 addq %rcx,%rdi /* copy backwards */
220 andq $7,%rcx /* any fractional bytes? */
224 movq %rdx,%rcx /* copy remainder by 32-bit words */
237 * memcpy(dst, src, len)
240 * Note: memcpy does not support overlapping copies
246 shrq $3,%rcx /* copy by 64-bit words */
250 andq $7,%rcx /* any bytes left? */
258 * memset(dst, c, len)
265 movabs $0x0101010101010101,%rax
280 * pagecopy(%rdi=from, %rsi=to)
284 movq $PAGE_SIZE/8,%rcx
294 /* fillw(pat, base, cnt) */
295 /* %rdi,%rsi, %rdx */
307 /*****************************************************************************/
308 /* copyout and fubyte family */
309 /*****************************************************************************/
311 * Access user memory from inside the kernel. These routines should be
312 * the only places that do this.
314 * These routines set curpcb->pcb_onfault for the time they execute. When a
315 * protection violation occurs inside the functions, the trap handler
316 * returns to *curpcb->pcb_onfault instead of the function.
320 * copyout(from_kernel, to_user, len)
325 movq PCPU(CURPCB),%rax
326 movq $copyout_fault,PCB_ONFAULT(%rax)
327 testq %rdx,%rdx /* anything to do? */
331 * Check explicitly for non-user addresses. This check is essential
332 * because it prevents usermode from writing into the kernel. We do
333 * not verify anywhere else that the user did not specify a rogue
337 * First, prevent address wrapping.
343 * XXX STOP USING VM_MAXUSER_ADDRESS.
344 * It is an end address, not a max, so every time it is used correctly it
345 * looks like there is an off by one error, and of course it caused an off
346 * by one error in several places.
348 movq $VM_MAXUSER_ADDRESS,%rcx
353 /* bcopy(%rsi, %rdi, %rdx) */
366 movq PCPU(CURPCB),%rdx
367 movq %rax,PCB_ONFAULT(%rdx)
373 movq PCPU(CURPCB),%rdx
374 movq $0,PCB_ONFAULT(%rdx)
381 * copyin(from_user, to_kernel, len)
386 movq PCPU(CURPCB),%rax
387 movq $copyin_fault,PCB_ONFAULT(%rax)
388 testq %rdx,%rdx /* anything to do? */
392 * make sure address is valid
397 movq $VM_MAXUSER_ADDRESS,%rcx
404 shrq $3,%rcx /* copy longword-wise */
408 andb $7,%cl /* copy remaining bytes */
414 movq PCPU(CURPCB),%rdx
415 movq %rax,PCB_ONFAULT(%rdx)
421 movq PCPU(CURPCB),%rdx
422 movq $0,PCB_ONFAULT(%rdx)
429 * casueword32. Compare and set user integer. Returns -1 on fault,
430 * 0 if access was successful. Old value is written to *oldp.
431 * dst = %rdi, old = %esi, oldp = %rdx, new = %ecx
435 movq PCPU(CURPCB),%r8
436 movq $fusufault,PCB_ONFAULT(%r8)
438 movq $VM_MAXUSER_ADDRESS-4,%rax
439 cmpq %rax,%rdi /* verify address is valid */
442 movl %esi,%eax /* old */
446 cmpxchgl %ecx,(%rdi) /* new = %ecx */
449 * The old value is in %eax. If the store succeeded it will be the
450 * value we expected (old) from before the store, otherwise it will
451 * be the current value. Save %eax into %esi to prepare the return
456 movq %rax,PCB_ONFAULT(%r8)
459 * Access the oldp after the pcb_onfault is cleared, to correctly
460 * catch corrupted pointer.
462 movl %esi,(%rdx) /* oldp = %rdx */
468 * casueword. Compare and set user long. Returns -1 on fault,
469 * 0 if access was successful. Old value is written to *oldp.
470 * dst = %rdi, old = %rsi, oldp = %rdx, new = %rcx
474 movq PCPU(CURPCB),%r8
475 movq $fusufault,PCB_ONFAULT(%r8)
477 movq $VM_MAXUSER_ADDRESS-4,%rax
478 cmpq %rax,%rdi /* verify address is valid */
481 movq %rsi,%rax /* old */
485 cmpxchgq %rcx,(%rdi) /* new = %rcx */
488 * The old value is in %rax. If the store succeeded it will be the
489 * value we expected (old) from before the store, otherwise it will
490 * be the current value.
494 movq %rax,PCB_ONFAULT(%r8)
501 * Fetch (load) a 64-bit word, a 32-bit word, a 16-bit word, or an 8-bit
502 * byte from user memory.
503 * addr = %rdi, valp = %rsi
509 movq PCPU(CURPCB),%rcx
510 movq $fusufault,PCB_ONFAULT(%rcx)
512 movq $VM_MAXUSER_ADDRESS-8,%rax
513 cmpq %rax,%rdi /* verify address is valid */
518 movq %rax,PCB_ONFAULT(%rcx)
527 movq PCPU(CURPCB),%rcx
528 movq $fusufault,PCB_ONFAULT(%rcx)
530 movq $VM_MAXUSER_ADDRESS-4,%rax
531 cmpq %rax,%rdi /* verify address is valid */
536 movq %rax,PCB_ONFAULT(%rcx)
544 movq PCPU(CURPCB),%rcx
545 movq $fusufault,PCB_ONFAULT(%rcx)
547 movq $VM_MAXUSER_ADDRESS-2,%rax
552 movq $0,PCB_ONFAULT(%rcx)
559 movq PCPU(CURPCB),%rcx
560 movq $fusufault,PCB_ONFAULT(%rcx)
562 movq $VM_MAXUSER_ADDRESS-1,%rax
567 movq $0,PCB_ONFAULT(%rcx)
574 movq PCPU(CURPCB),%rcx
576 movq %rax,PCB_ONFAULT(%rcx)
582 * Store a 64-bit word, a 32-bit word, a 16-bit word, or an 8-bit byte to
584 * addr = %rdi, value = %rsi
589 movq PCPU(CURPCB),%rcx
590 movq $fusufault,PCB_ONFAULT(%rcx)
592 movq $VM_MAXUSER_ADDRESS-8,%rax
593 cmpq %rax,%rdi /* verify address validity */
598 movq PCPU(CURPCB),%rcx
599 movq %rax,PCB_ONFAULT(%rcx)
607 movq PCPU(CURPCB),%rcx
608 movq $fusufault,PCB_ONFAULT(%rcx)
610 movq $VM_MAXUSER_ADDRESS-4,%rax
611 cmpq %rax,%rdi /* verify address validity */
616 movq PCPU(CURPCB),%rcx
617 movq %rax,PCB_ONFAULT(%rcx)
624 movq PCPU(CURPCB),%rcx
625 movq $fusufault,PCB_ONFAULT(%rcx)
627 movq $VM_MAXUSER_ADDRESS-2,%rax
628 cmpq %rax,%rdi /* verify address validity */
633 movq PCPU(CURPCB),%rcx /* restore trashed register */
634 movq %rax,PCB_ONFAULT(%rcx)
641 movq PCPU(CURPCB),%rcx
642 movq $fusufault,PCB_ONFAULT(%rcx)
644 movq $VM_MAXUSER_ADDRESS-1,%rax
645 cmpq %rax,%rdi /* verify address validity */
651 movq PCPU(CURPCB),%rcx /* restore trashed register */
652 movq %rax,PCB_ONFAULT(%rcx)
658 * copyinstr(from, to, maxlen, int *lencopied)
659 * %rdi, %rsi, %rdx, %rcx
661 * copy a string from 'from' to 'to', stop when a 0 character is reached.
662 * return ENAMETOOLONG if string is longer than maxlen, and
663 * EFAULT on protection violations. If lencopied is non-zero,
664 * return the actual length in *lencopied.
668 movq %rdx,%r8 /* %r8 = maxlen */
669 movq %rcx,%r9 /* %r9 = *len */
670 xchgq %rdi,%rsi /* %rdi = from, %rsi = to */
671 movq PCPU(CURPCB),%rcx
672 movq $cpystrflt,PCB_ONFAULT(%rcx)
674 movq $VM_MAXUSER_ADDRESS,%rax
676 /* make sure 'from' is within bounds */
680 /* restrict maxlen to <= VM_MAXUSER_ADDRESS-from */
697 /* Success -- 0 byte reached */
702 /* rdx is zero - return ENAMETOOLONG or EFAULT */
703 movq $VM_MAXUSER_ADDRESS,%rax
707 movq $ENAMETOOLONG,%rax
714 /* set *lencopied and return %eax */
715 movq PCPU(CURPCB),%rcx
716 movq $0,PCB_ONFAULT(%rcx)
728 * copystr(from, to, maxlen, int *lencopied)
729 * %rdi, %rsi, %rdx, %rcx
733 movq %rdx,%r8 /* %r8 = maxlen */
745 /* Success -- 0 byte reached */
750 /* rdx is zero -- return ENAMETOOLONG */
751 movq $ENAMETOOLONG,%rax
757 /* set *lencopied and return %rax */
766 * Handling of special amd64 registers and descriptor tables etc
768 /* void lgdt(struct region_descriptor *rdp); */
770 /* reload the descriptor table */
773 /* flush the prefetch q */
780 movl %eax,%fs /* Beware, use wrmsr to set 64 bit base */
784 /* reload code selector by turning return into intersegmental return */
792 /*****************************************************************************/
793 /* setjump, longjump */
794 /*****************************************************************************/
797 movq %rbx,0(%rdi) /* save rbx */
798 movq %rsp,8(%rdi) /* save rsp */
799 movq %rbp,16(%rdi) /* save rbp */
800 movq %r12,24(%rdi) /* save r12 */
801 movq %r13,32(%rdi) /* save r13 */
802 movq %r14,40(%rdi) /* save r14 */
803 movq %r15,48(%rdi) /* save r15 */
804 movq 0(%rsp),%rdx /* get rta */
805 movq %rdx,56(%rdi) /* save rip */
806 xorl %eax,%eax /* return(0); */
811 movq 0(%rdi),%rbx /* restore rbx */
812 movq 8(%rdi),%rsp /* restore rsp */
813 movq 16(%rdi),%rbp /* restore rbp */
814 movq 24(%rdi),%r12 /* restore r12 */
815 movq 32(%rdi),%r13 /* restore r13 */
816 movq 40(%rdi),%r14 /* restore r14 */
817 movq 48(%rdi),%r15 /* restore r15 */
818 movq 56(%rdi),%rdx /* get rta */
819 movq %rdx,0(%rsp) /* put in return frame */
820 xorl %eax,%eax /* return(1); */
826 * Support for reading MSRs in the safe manner. (Instead of panic on #gp,
830 /* int rdmsr_safe(u_int msr, uint64_t *data) */
832 movq PCPU(CURPCB),%r8
833 movq $msr_onfault,PCB_ONFAULT(%r8)
835 rdmsr /* Read MSR pointed by %ecx. Returns
836 hi byte in edx, lo in %eax */
837 salq $32,%rdx /* sign-shift %rdx left */
838 movl %eax,%eax /* zero-extend %eax -> %rax */
842 movq %rax,PCB_ONFAULT(%r8)
847 * Support for writing MSRs in the safe manner. (Instead of panic on #gp,
851 /* int wrmsr_safe(u_int msr, uint64_t data) */
853 movq PCPU(CURPCB),%r8
854 movq $msr_onfault,PCB_ONFAULT(%r8)
859 wrmsr /* Write MSR pointed by %ecx. Accepts
860 hi byte in edx, lo in %eax. */
862 movq %rax,PCB_ONFAULT(%r8)
867 * MSR operations fault handler
871 movq $0,PCB_ONFAULT(%r8)
877 * void pmap_pti_pcid_invalidate(uint64_t ucr3, uint64_t kcr3);
878 * Invalidates address space addressed by ucr3, then returns to kcr3.
879 * Done in assembler to ensure no other memory accesses happen while
883 ENTRY(pmap_pti_pcid_invalidate)
886 movq %rdi,%cr3 /* to user page table */
887 movq %rsi,%cr3 /* back to kernel */
892 * void pmap_pti_pcid_invlpg(uint64_t ucr3, uint64_t kcr3, vm_offset_t va);
893 * Invalidates virtual address va in address space ucr3, then returns to kcr3.
896 ENTRY(pmap_pti_pcid_invlpg)
899 movq %rdi,%cr3 /* to user page table */
901 movq %rsi,%cr3 /* back to kernel */
906 * void pmap_pti_pcid_invlrng(uint64_t ucr3, uint64_t kcr3, vm_offset_t sva,
908 * Invalidates virtual addresses between sva and eva in address space ucr3,
909 * then returns to kcr3.
912 ENTRY(pmap_pti_pcid_invlrng)
915 movq %rdi,%cr3 /* to user page table */
920 movq %rsi,%cr3 /* back to kernel */
925 .macro ibrs_seq_label l
928 .macro ibrs_call_label l
931 .macro ibrs_seq count
934 ibrs_call_label %(ll)
942 /* all callers already saved %rax, %rdx, and %rcx */
943 ENTRY(handle_ibrs_entry)
944 cmpb $0,hw_ibrs_active(%rip)
946 movl $MSR_IA32_SPEC_CTRL,%ecx
948 orl $(IA32_SPEC_CTRL_IBRS|IA32_SPEC_CTRL_STIBP),%eax
949 orl $(IA32_SPEC_CTRL_IBRS|IA32_SPEC_CTRL_STIBP)>>32,%edx
951 movb $1,PCPU(IBPB_SET)
952 testl $CPUID_STDEXT_SMEP,cpu_stdext_feature(%rip)
956 END(handle_ibrs_entry)
958 ENTRY(handle_ibrs_exit)
959 cmpb $0,PCPU(IBPB_SET)
961 movl $MSR_IA32_SPEC_CTRL,%ecx
963 andl $~(IA32_SPEC_CTRL_IBRS|IA32_SPEC_CTRL_STIBP),%eax
964 andl $~((IA32_SPEC_CTRL_IBRS|IA32_SPEC_CTRL_STIBP)>>32),%edx
966 movb $0,PCPU(IBPB_SET)
968 END(handle_ibrs_exit)
970 /* registers-neutral version, but needs stack */
971 ENTRY(handle_ibrs_exit_rs)
972 cmpb $0,PCPU(IBPB_SET)
977 movl $MSR_IA32_SPEC_CTRL,%ecx
979 andl $~(IA32_SPEC_CTRL_IBRS|IA32_SPEC_CTRL_STIBP),%eax
980 andl $~((IA32_SPEC_CTRL_IBRS|IA32_SPEC_CTRL_STIBP)>>32),%edx
985 movb $0,PCPU(IBPB_SET)
987 END(handle_ibrs_exit_rs)