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)
119 * bcopy(src, dst, cnt)
121 * ws@tools.de (Wolfgang Solfrank, TooLs GmbH) +49-228-985800
130 cmpq %rcx,%rax /* overlapping && src < dst? */
133 shrq $3,%rcx /* copy by 64-bit words */
137 andq $7,%rcx /* any bytes left? */
145 addq %rcx,%rdi /* copy backwards */
149 andq $7,%rcx /* any fractional bytes? */
153 movq %rdx,%rcx /* copy remainder by 32-bit words */
165 * memmove(dst, src, cnt)
168 * ws@tools.de (Wolfgang Solfrank, TooLs GmbH) +49-228-985800
177 cmpq %rcx,%rax /* overlapping && src < dst? */
180 shrq $3,%rcx /* copy by 64-bit words */
184 andq $7,%rcx /* any bytes left? */
193 addq %rcx,%rdi /* copy backwards */
197 andq $7,%rcx /* any fractional bytes? */
201 movq %rdx,%rcx /* copy remainder by 32-bit words */
214 * memcpy(dst, src, len)
217 * Note: memcpy does not support overlapping copies
223 shrq $3,%rcx /* copy by 64-bit words */
227 andq $7,%rcx /* any bytes left? */
235 * memset(dst, c, len)
256 * pagecopy(%rdi=from, %rsi=to)
260 movq $-PAGE_SIZE,%rax
265 prefetchnta (%rdi,%rax)
269 movq (%rdi,%rdx),%rax
270 movnti %rax,(%rsi,%rdx)
271 movq 8(%rdi,%rdx),%rax
272 movnti %rax,8(%rsi,%rdx)
273 movq 16(%rdi,%rdx),%rax
274 movnti %rax,16(%rsi,%rdx)
275 movq 24(%rdi,%rdx),%rax
276 movnti %rax,24(%rsi,%rdx)
284 /* fillw(pat, base, cnt) */
285 /* %rdi,%rsi, %rdx */
297 /*****************************************************************************/
298 /* copyout and fubyte family */
299 /*****************************************************************************/
301 * Access user memory from inside the kernel. These routines should be
302 * the only places that do this.
304 * These routines set curpcb->pcb_onfault for the time they execute. When a
305 * protection violation occurs inside the functions, the trap handler
306 * returns to *curpcb->pcb_onfault instead of the function.
310 * copyout(from_kernel, to_user, len)
315 movq PCPU(CURPCB),%rax
316 movq $copyout_fault,PCB_ONFAULT(%rax)
317 testq %rdx,%rdx /* anything to do? */
321 * Check explicitly for non-user addresses. This check is essential
322 * because it prevents usermode from writing into the kernel. We do
323 * not verify anywhere else that the user did not specify a rogue
327 * First, prevent address wrapping.
333 * XXX STOP USING VM_MAXUSER_ADDRESS.
334 * It is an end address, not a max, so every time it is used correctly it
335 * looks like there is an off by one error, and of course it caused an off
336 * by one error in several places.
338 movq $VM_MAXUSER_ADDRESS,%rcx
343 /* bcopy(%rsi, %rdi, %rdx) */
356 movq PCPU(CURPCB),%rdx
357 movq %rax,PCB_ONFAULT(%rdx)
363 movq PCPU(CURPCB),%rdx
364 movq $0,PCB_ONFAULT(%rdx)
371 * copyin(from_user, to_kernel, len)
376 movq PCPU(CURPCB),%rax
377 movq $copyin_fault,PCB_ONFAULT(%rax)
378 testq %rdx,%rdx /* anything to do? */
382 * make sure address is valid
387 movq $VM_MAXUSER_ADDRESS,%rcx
394 shrq $3,%rcx /* copy longword-wise */
398 andb $7,%cl /* copy remaining bytes */
404 movq PCPU(CURPCB),%rdx
405 movq %rax,PCB_ONFAULT(%rdx)
411 movq PCPU(CURPCB),%rdx
412 movq $0,PCB_ONFAULT(%rdx)
419 * casueword32. Compare and set user integer. Returns -1 on fault,
420 * 0 if access was successful. Old value is written to *oldp.
421 * dst = %rdi, old = %esi, oldp = %rdx, new = %ecx
425 movq PCPU(CURPCB),%r8
426 movq $fusufault,PCB_ONFAULT(%r8)
428 movq $VM_MAXUSER_ADDRESS-4,%rax
429 cmpq %rax,%rdi /* verify address is valid */
432 movl %esi,%eax /* old */
436 cmpxchgl %ecx,(%rdi) /* new = %ecx */
439 * The old value is in %eax. If the store succeeded it will be the
440 * value we expected (old) from before the store, otherwise it will
441 * be the current value. Save %eax into %esi to prepare the return
446 movq %rax,PCB_ONFAULT(%r8)
449 * Access the oldp after the pcb_onfault is cleared, to correctly
450 * catch corrupted pointer.
452 movl %esi,(%rdx) /* oldp = %rdx */
458 * casueword. Compare and set user long. Returns -1 on fault,
459 * 0 if access was successful. Old value is written to *oldp.
460 * dst = %rdi, old = %rsi, oldp = %rdx, new = %rcx
464 movq PCPU(CURPCB),%r8
465 movq $fusufault,PCB_ONFAULT(%r8)
467 movq $VM_MAXUSER_ADDRESS-4,%rax
468 cmpq %rax,%rdi /* verify address is valid */
471 movq %rsi,%rax /* old */
475 cmpxchgq %rcx,(%rdi) /* new = %rcx */
478 * The old value is in %rax. If the store succeeded it will be the
479 * value we expected (old) from before the store, otherwise it will
480 * be the current value.
484 movq %rax,PCB_ONFAULT(%r8)
491 * Fetch (load) a 64-bit word, a 32-bit word, a 16-bit word, or an 8-bit
492 * byte from user memory.
493 * addr = %rdi, valp = %rsi
499 movq PCPU(CURPCB),%rcx
500 movq $fusufault,PCB_ONFAULT(%rcx)
502 movq $VM_MAXUSER_ADDRESS-8,%rax
503 cmpq %rax,%rdi /* verify address is valid */
508 movq %rax,PCB_ONFAULT(%rcx)
517 movq PCPU(CURPCB),%rcx
518 movq $fusufault,PCB_ONFAULT(%rcx)
520 movq $VM_MAXUSER_ADDRESS-4,%rax
521 cmpq %rax,%rdi /* verify address is valid */
526 movq %rax,PCB_ONFAULT(%rcx)
534 movq PCPU(CURPCB),%rcx
535 movq $fusufault,PCB_ONFAULT(%rcx)
537 movq $VM_MAXUSER_ADDRESS-2,%rax
542 movq $0,PCB_ONFAULT(%rcx)
549 movq PCPU(CURPCB),%rcx
550 movq $fusufault,PCB_ONFAULT(%rcx)
552 movq $VM_MAXUSER_ADDRESS-1,%rax
557 movq $0,PCB_ONFAULT(%rcx)
564 movq PCPU(CURPCB),%rcx
566 movq %rax,PCB_ONFAULT(%rcx)
572 * Store a 64-bit word, a 32-bit word, a 16-bit word, or an 8-bit byte to
574 * addr = %rdi, value = %rsi
579 movq PCPU(CURPCB),%rcx
580 movq $fusufault,PCB_ONFAULT(%rcx)
582 movq $VM_MAXUSER_ADDRESS-8,%rax
583 cmpq %rax,%rdi /* verify address validity */
588 movq PCPU(CURPCB),%rcx
589 movq %rax,PCB_ONFAULT(%rcx)
597 movq PCPU(CURPCB),%rcx
598 movq $fusufault,PCB_ONFAULT(%rcx)
600 movq $VM_MAXUSER_ADDRESS-4,%rax
601 cmpq %rax,%rdi /* verify address validity */
606 movq PCPU(CURPCB),%rcx
607 movq %rax,PCB_ONFAULT(%rcx)
614 movq PCPU(CURPCB),%rcx
615 movq $fusufault,PCB_ONFAULT(%rcx)
617 movq $VM_MAXUSER_ADDRESS-2,%rax
618 cmpq %rax,%rdi /* verify address validity */
623 movq PCPU(CURPCB),%rcx /* restore trashed register */
624 movq %rax,PCB_ONFAULT(%rcx)
631 movq PCPU(CURPCB),%rcx
632 movq $fusufault,PCB_ONFAULT(%rcx)
634 movq $VM_MAXUSER_ADDRESS-1,%rax
635 cmpq %rax,%rdi /* verify address validity */
641 movq PCPU(CURPCB),%rcx /* restore trashed register */
642 movq %rax,PCB_ONFAULT(%rcx)
648 * copyinstr(from, to, maxlen, int *lencopied)
649 * %rdi, %rsi, %rdx, %rcx
651 * copy a string from 'from' to 'to', stop when a 0 character is reached.
652 * return ENAMETOOLONG if string is longer than maxlen, and
653 * EFAULT on protection violations. If lencopied is non-zero,
654 * return the actual length in *lencopied.
658 movq %rdx,%r8 /* %r8 = maxlen */
659 movq %rcx,%r9 /* %r9 = *len */
660 xchgq %rdi,%rsi /* %rdi = from, %rsi = to */
661 movq PCPU(CURPCB),%rcx
662 movq $cpystrflt,PCB_ONFAULT(%rcx)
664 movq $VM_MAXUSER_ADDRESS,%rax
666 /* make sure 'from' is within bounds */
670 /* restrict maxlen to <= VM_MAXUSER_ADDRESS-from */
687 /* Success -- 0 byte reached */
692 /* rdx is zero - return ENAMETOOLONG or EFAULT */
693 movq $VM_MAXUSER_ADDRESS,%rax
697 movq $ENAMETOOLONG,%rax
704 /* set *lencopied and return %eax */
705 movq PCPU(CURPCB),%rcx
706 movq $0,PCB_ONFAULT(%rcx)
718 * copystr(from, to, maxlen, int *lencopied)
719 * %rdi, %rsi, %rdx, %rcx
723 movq %rdx,%r8 /* %r8 = maxlen */
735 /* Success -- 0 byte reached */
740 /* rdx is zero -- return ENAMETOOLONG */
741 movq $ENAMETOOLONG,%rax
747 /* set *lencopied and return %rax */
756 * Handling of special amd64 registers and descriptor tables etc
758 /* void lgdt(struct region_descriptor *rdp); */
760 /* reload the descriptor table */
763 /* flush the prefetch q */
770 movl %eax,%fs /* Beware, use wrmsr to set 64 bit base */
774 /* reload code selector by turning return into intersegmental return */
782 /*****************************************************************************/
783 /* setjump, longjump */
784 /*****************************************************************************/
787 movq %rbx,0(%rdi) /* save rbx */
788 movq %rsp,8(%rdi) /* save rsp */
789 movq %rbp,16(%rdi) /* save rbp */
790 movq %r12,24(%rdi) /* save r12 */
791 movq %r13,32(%rdi) /* save r13 */
792 movq %r14,40(%rdi) /* save r14 */
793 movq %r15,48(%rdi) /* save r15 */
794 movq 0(%rsp),%rdx /* get rta */
795 movq %rdx,56(%rdi) /* save rip */
796 xorl %eax,%eax /* return(0); */
801 movq 0(%rdi),%rbx /* restore rbx */
802 movq 8(%rdi),%rsp /* restore rsp */
803 movq 16(%rdi),%rbp /* restore rbp */
804 movq 24(%rdi),%r12 /* restore r12 */
805 movq 32(%rdi),%r13 /* restore r13 */
806 movq 40(%rdi),%r14 /* restore r14 */
807 movq 48(%rdi),%r15 /* restore r15 */
808 movq 56(%rdi),%rdx /* get rta */
809 movq %rdx,0(%rsp) /* put in return frame */
810 xorl %eax,%eax /* return(1); */
816 * Support for reading MSRs in the safe manner. (Instead of panic on #gp,
820 /* int rdmsr_safe(u_int msr, uint64_t *data) */
822 movq PCPU(CURPCB),%r8
823 movq $msr_onfault,PCB_ONFAULT(%r8)
825 rdmsr /* Read MSR pointed by %ecx. Returns
826 hi byte in edx, lo in %eax */
827 salq $32,%rdx /* sign-shift %rdx left */
828 movl %eax,%eax /* zero-extend %eax -> %rax */
832 movq %rax,PCB_ONFAULT(%r8)
837 * Support for writing MSRs in the safe manner. (Instead of panic on #gp,
841 /* int wrmsr_safe(u_int msr, uint64_t data) */
843 movq PCPU(CURPCB),%r8
844 movq $msr_onfault,PCB_ONFAULT(%r8)
849 wrmsr /* Write MSR pointed by %ecx. Accepts
850 hi byte in edx, lo in %eax. */
852 movq %rax,PCB_ONFAULT(%r8)
857 * MSR operations fault handler
861 movq $0,PCB_ONFAULT(%r8)
867 * void pmap_pti_pcid_invalidate(uint64_t ucr3, uint64_t kcr3);
868 * Invalidates address space addressed by ucr3, then returns to kcr3.
869 * Done in assembler to ensure no other memory accesses happen while
873 ENTRY(pmap_pti_pcid_invalidate)
876 movq %rdi,%cr3 /* to user page table */
877 movq %rsi,%cr3 /* back to kernel */
882 * void pmap_pti_pcid_invlpg(uint64_t ucr3, uint64_t kcr3, vm_offset_t va);
883 * Invalidates virtual address va in address space ucr3, then returns to kcr3.
886 ENTRY(pmap_pti_pcid_invlpg)
889 movq %rdi,%cr3 /* to user page table */
891 movq %rsi,%cr3 /* back to kernel */
896 * void pmap_pti_pcid_invlrng(uint64_t ucr3, uint64_t kcr3, vm_offset_t sva,
898 * Invalidates virtual addresses between sva and eva in address space ucr3,
899 * then returns to kcr3.
902 ENTRY(pmap_pti_pcid_invlrng)
905 movq %rdi,%cr3 /* to user page table */
910 movq %rsi,%cr3 /* back to kernel */
915 .macro ibrs_seq_label l
918 .macro ibrs_call_label l
921 .macro ibrs_seq count
924 ibrs_call_label %(ll)
932 /* all callers already saved %rax, %rdx, and %rcx */
933 ENTRY(handle_ibrs_entry)
934 cmpb $0,hw_ibrs_active(%rip)
936 movl $MSR_IA32_SPEC_CTRL,%ecx
937 movl $(IA32_SPEC_CTRL_IBRS|IA32_SPEC_CTRL_STIBP),%eax
938 movl $(IA32_SPEC_CTRL_IBRS|IA32_SPEC_CTRL_STIBP)>>32,%edx
940 movb $1,PCPU(IBPB_SET)
941 testl $CPUID_STDEXT_SMEP,cpu_stdext_feature(%rip)
945 END(handle_ibrs_entry)
947 ENTRY(handle_ibrs_exit)
948 cmpb $0,PCPU(IBPB_SET)
950 movl $MSR_IA32_SPEC_CTRL,%ecx
954 movb $0,PCPU(IBPB_SET)
956 END(handle_ibrs_exit)
958 /* registers-neutral version, but needs stack */
959 ENTRY(handle_ibrs_exit_rs)
960 cmpb $0,PCPU(IBPB_SET)
965 movl $MSR_IA32_SPEC_CTRL,%ecx
972 movb $0,PCPU(IBPB_SET)
974 END(handle_ibrs_exit_rs)