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>
46 movq $PAGE_SIZE/8,%rcx
55 * pagecopy(%rdi=from, %rsi=to)
59 movq $PAGE_SIZE/8,%rcx
77 * The loop takes 29 bytes. Ensure that it doesn't cross a 32-byte
82 movnti %rax,(%rdi,%rdx)
83 movnti %rax,8(%rdi,%rdx)
84 movnti %rax,16(%rdi,%rdx)
85 movnti %rax,24(%rdi,%rdx)
94 * memmove(dst, src, cnt)
96 * Adapted from bcopy written by:
97 * ws@tools.de (Wolfgang Solfrank, TooLs GmbH) +49-228-985800
106 cmpq %rcx,%rax /* overlapping && src < dst? */
109 shrq $3,%rcx /* copy by 64-bit words */
113 andq $7,%rcx /* any bytes left? */
127 addq %rcx,%rdi /* copy backwards */
131 andq $7,%rcx /* any fractional bytes? */
135 movq %rdx,%rcx /* copy remainder by 32-bit words */
148 * memcpy(dst, src, len)
151 * Note: memcpy does not support overlapping copies
157 shrq $3,%rcx /* copy by 64-bit words */
161 andq $7,%rcx /* any bytes left? */
173 * memset(dst, c, len)
181 movabs $0x0101010101010101,%rax
200 /* fillw(pat, base, cnt) */
201 /* %rdi,%rsi, %rdx */
213 /*****************************************************************************/
214 /* copyout and fubyte family */
215 /*****************************************************************************/
217 * Access user memory from inside the kernel. These routines should be
218 * the only places that do this.
220 * These routines set curpcb->pcb_onfault for the time they execute. When a
221 * protection violation occurs inside the functions, the trap handler
222 * returns to *curpcb->pcb_onfault instead of the function.
226 * copyout(from_kernel, to_user, len)
231 movq PCPU(CURPCB),%rax
232 movq $copyout_fault,PCB_ONFAULT(%rax)
233 testq %rdx,%rdx /* anything to do? */
237 * Check explicitly for non-user addresses. This check is essential
238 * because it prevents usermode from writing into the kernel. We do
239 * not verify anywhere else that the user did not specify a rogue
243 * First, prevent address wrapping.
249 * XXX STOP USING VM_MAXUSER_ADDRESS.
250 * It is an end address, not a max, so every time it is used correctly it
251 * looks like there is an off by one error, and of course it caused an off
252 * by one error in several places.
254 movq $VM_MAXUSER_ADDRESS,%rcx
259 /* bcopy(%rsi, %rdi, %rdx) */
273 movq PCPU(CURPCB),%rdx
274 movq %rax,PCB_ONFAULT(%rdx)
280 movq PCPU(CURPCB),%rdx
281 movq $0,PCB_ONFAULT(%rdx)
288 * copyin(from_user, to_kernel, len)
293 movq PCPU(CURPCB),%rax
294 movq $copyin_fault,PCB_ONFAULT(%rax)
295 testq %rdx,%rdx /* anything to do? */
299 * make sure address is valid
304 movq $VM_MAXUSER_ADDRESS,%rcx
311 shrq $3,%rcx /* copy longword-wise */
315 andb $7,%cl /* copy remaining bytes */
322 movq PCPU(CURPCB),%rdx
323 movq %rax,PCB_ONFAULT(%rdx)
329 movq PCPU(CURPCB),%rdx
330 movq $0,PCB_ONFAULT(%rdx)
337 * casueword32. Compare and set user integer. Returns -1 on fault,
338 * 0 if access was successful. Old value is written to *oldp.
339 * dst = %rdi, old = %esi, oldp = %rdx, new = %ecx
343 movq PCPU(CURPCB),%r8
344 movq $fusufault,PCB_ONFAULT(%r8)
346 movq $VM_MAXUSER_ADDRESS-4,%rax
347 cmpq %rax,%rdi /* verify address is valid */
350 movl %esi,%eax /* old */
354 cmpxchgl %ecx,(%rdi) /* new = %ecx */
357 * The old value is in %eax. If the store succeeded it will be the
358 * value we expected (old) from before the store, otherwise it will
359 * be the current value. Save %eax into %esi to prepare the return
364 movq %rax,PCB_ONFAULT(%r8)
367 * Access the oldp after the pcb_onfault is cleared, to correctly
368 * catch corrupted pointer.
370 movl %esi,(%rdx) /* oldp = %rdx */
376 * casueword. Compare and set user long. Returns -1 on fault,
377 * 0 if access was successful. Old value is written to *oldp.
378 * dst = %rdi, old = %rsi, oldp = %rdx, new = %rcx
382 movq PCPU(CURPCB),%r8
383 movq $fusufault,PCB_ONFAULT(%r8)
385 movq $VM_MAXUSER_ADDRESS-4,%rax
386 cmpq %rax,%rdi /* verify address is valid */
389 movq %rsi,%rax /* old */
393 cmpxchgq %rcx,(%rdi) /* new = %rcx */
396 * The old value is in %rax. If the store succeeded it will be the
397 * value we expected (old) from before the store, otherwise it will
398 * be the current value.
402 movq %rax,PCB_ONFAULT(%r8)
409 * Fetch (load) a 64-bit word, a 32-bit word, a 16-bit word, or an 8-bit
410 * byte from user memory.
411 * addr = %rdi, valp = %rsi
417 movq PCPU(CURPCB),%rcx
418 movq $fusufault,PCB_ONFAULT(%rcx)
420 movq $VM_MAXUSER_ADDRESS-8,%rax
421 cmpq %rax,%rdi /* verify address is valid */
426 movq %rax,PCB_ONFAULT(%rcx)
435 movq PCPU(CURPCB),%rcx
436 movq $fusufault,PCB_ONFAULT(%rcx)
438 movq $VM_MAXUSER_ADDRESS-4,%rax
439 cmpq %rax,%rdi /* verify address is valid */
444 movq %rax,PCB_ONFAULT(%rcx)
452 movq PCPU(CURPCB),%rcx
453 movq $fusufault,PCB_ONFAULT(%rcx)
455 movq $VM_MAXUSER_ADDRESS-2,%rax
460 movq $0,PCB_ONFAULT(%rcx)
467 movq PCPU(CURPCB),%rcx
468 movq $fusufault,PCB_ONFAULT(%rcx)
470 movq $VM_MAXUSER_ADDRESS-1,%rax
475 movq $0,PCB_ONFAULT(%rcx)
482 movq PCPU(CURPCB),%rcx
484 movq %rax,PCB_ONFAULT(%rcx)
490 * Store a 64-bit word, a 32-bit word, a 16-bit word, or an 8-bit byte to
492 * addr = %rdi, value = %rsi
497 movq PCPU(CURPCB),%rcx
498 movq $fusufault,PCB_ONFAULT(%rcx)
500 movq $VM_MAXUSER_ADDRESS-8,%rax
501 cmpq %rax,%rdi /* verify address validity */
506 movq PCPU(CURPCB),%rcx
507 movq %rax,PCB_ONFAULT(%rcx)
515 movq PCPU(CURPCB),%rcx
516 movq $fusufault,PCB_ONFAULT(%rcx)
518 movq $VM_MAXUSER_ADDRESS-4,%rax
519 cmpq %rax,%rdi /* verify address validity */
524 movq PCPU(CURPCB),%rcx
525 movq %rax,PCB_ONFAULT(%rcx)
532 movq PCPU(CURPCB),%rcx
533 movq $fusufault,PCB_ONFAULT(%rcx)
535 movq $VM_MAXUSER_ADDRESS-2,%rax
536 cmpq %rax,%rdi /* verify address validity */
541 movq PCPU(CURPCB),%rcx /* restore trashed register */
542 movq %rax,PCB_ONFAULT(%rcx)
549 movq PCPU(CURPCB),%rcx
550 movq $fusufault,PCB_ONFAULT(%rcx)
552 movq $VM_MAXUSER_ADDRESS-1,%rax
553 cmpq %rax,%rdi /* verify address validity */
559 movq PCPU(CURPCB),%rcx /* restore trashed register */
560 movq %rax,PCB_ONFAULT(%rcx)
566 * copyinstr(from, to, maxlen, int *lencopied)
567 * %rdi, %rsi, %rdx, %rcx
569 * copy a string from 'from' to 'to', stop when a 0 character is reached.
570 * return ENAMETOOLONG if string is longer than maxlen, and
571 * EFAULT on protection violations. If lencopied is non-zero,
572 * return the actual length in *lencopied.
576 movq %rdx,%r8 /* %r8 = maxlen */
577 movq %rcx,%r9 /* %r9 = *len */
578 xchgq %rdi,%rsi /* %rdi = from, %rsi = to */
579 movq PCPU(CURPCB),%rcx
580 movq $cpystrflt,PCB_ONFAULT(%rcx)
582 movq $VM_MAXUSER_ADDRESS,%rax
584 /* make sure 'from' is within bounds */
588 /* restrict maxlen to <= VM_MAXUSER_ADDRESS-from */
605 /* Success -- 0 byte reached */
610 /* rdx is zero - return ENAMETOOLONG or EFAULT */
611 movq $VM_MAXUSER_ADDRESS,%rax
615 movq $ENAMETOOLONG,%rax
622 /* set *lencopied and return %eax */
623 movq PCPU(CURPCB),%rcx
624 movq $0,PCB_ONFAULT(%rcx)
636 * copystr(from, to, maxlen, int *lencopied)
637 * %rdi, %rsi, %rdx, %rcx
641 movq %rdx,%r8 /* %r8 = maxlen */
653 /* Success -- 0 byte reached */
658 /* rdx is zero -- return ENAMETOOLONG */
659 movq $ENAMETOOLONG,%rax
665 /* set *lencopied and return %rax */
674 * Handling of special amd64 registers and descriptor tables etc
676 /* void lgdt(struct region_descriptor *rdp); */
678 /* reload the descriptor table */
681 /* flush the prefetch q */
688 movl %eax,%fs /* Beware, use wrmsr to set 64 bit base */
692 /* reload code selector by turning return into intersegmental return */
700 /*****************************************************************************/
701 /* setjump, longjump */
702 /*****************************************************************************/
705 movq %rbx,0(%rdi) /* save rbx */
706 movq %rsp,8(%rdi) /* save rsp */
707 movq %rbp,16(%rdi) /* save rbp */
708 movq %r12,24(%rdi) /* save r12 */
709 movq %r13,32(%rdi) /* save r13 */
710 movq %r14,40(%rdi) /* save r14 */
711 movq %r15,48(%rdi) /* save r15 */
712 movq 0(%rsp),%rdx /* get rta */
713 movq %rdx,56(%rdi) /* save rip */
714 xorl %eax,%eax /* return(0); */
719 movq 0(%rdi),%rbx /* restore rbx */
720 movq 8(%rdi),%rsp /* restore rsp */
721 movq 16(%rdi),%rbp /* restore rbp */
722 movq 24(%rdi),%r12 /* restore r12 */
723 movq 32(%rdi),%r13 /* restore r13 */
724 movq 40(%rdi),%r14 /* restore r14 */
725 movq 48(%rdi),%r15 /* restore r15 */
726 movq 56(%rdi),%rdx /* get rta */
727 movq %rdx,0(%rsp) /* put in return frame */
728 xorl %eax,%eax /* return(1); */
734 * Support for reading MSRs in the safe manner. (Instead of panic on #gp,
738 /* int rdmsr_safe(u_int msr, uint64_t *data) */
740 movq PCPU(CURPCB),%r8
741 movq $msr_onfault,PCB_ONFAULT(%r8)
743 rdmsr /* Read MSR pointed by %ecx. Returns
744 hi byte in edx, lo in %eax */
745 salq $32,%rdx /* sign-shift %rdx left */
746 movl %eax,%eax /* zero-extend %eax -> %rax */
750 movq %rax,PCB_ONFAULT(%r8)
755 * Support for writing MSRs in the safe manner. (Instead of panic on #gp,
759 /* int wrmsr_safe(u_int msr, uint64_t data) */
761 movq PCPU(CURPCB),%r8
762 movq $msr_onfault,PCB_ONFAULT(%r8)
767 wrmsr /* Write MSR pointed by %ecx. Accepts
768 hi byte in edx, lo in %eax. */
770 movq %rax,PCB_ONFAULT(%r8)
775 * MSR operations fault handler
779 movq $0,PCB_ONFAULT(%r8)
785 * void pmap_pti_pcid_invalidate(uint64_t ucr3, uint64_t kcr3);
786 * Invalidates address space addressed by ucr3, then returns to kcr3.
787 * Done in assembler to ensure no other memory accesses happen while
791 ENTRY(pmap_pti_pcid_invalidate)
794 movq %rdi,%cr3 /* to user page table */
795 movq %rsi,%cr3 /* back to kernel */
800 * void pmap_pti_pcid_invlpg(uint64_t ucr3, uint64_t kcr3, vm_offset_t va);
801 * Invalidates virtual address va in address space ucr3, then returns to kcr3.
804 ENTRY(pmap_pti_pcid_invlpg)
807 movq %rdi,%cr3 /* to user page table */
809 movq %rsi,%cr3 /* back to kernel */
814 * void pmap_pti_pcid_invlrng(uint64_t ucr3, uint64_t kcr3, vm_offset_t sva,
816 * Invalidates virtual addresses between sva and eva in address space ucr3,
817 * then returns to kcr3.
820 ENTRY(pmap_pti_pcid_invlrng)
823 movq %rdi,%cr3 /* to user page table */
828 movq %rsi,%cr3 /* back to kernel */
833 .macro ibrs_seq_label l
836 .macro ibrs_call_label l
839 .macro ibrs_seq count
842 ibrs_call_label %(ll)
850 /* all callers already saved %rax, %rdx, and %rcx */
851 ENTRY(handle_ibrs_entry)
852 cmpb $0,hw_ibrs_active(%rip)
854 movl $MSR_IA32_SPEC_CTRL,%ecx
856 orl $(IA32_SPEC_CTRL_IBRS|IA32_SPEC_CTRL_STIBP),%eax
857 orl $(IA32_SPEC_CTRL_IBRS|IA32_SPEC_CTRL_STIBP)>>32,%edx
859 movb $1,PCPU(IBPB_SET)
860 testl $CPUID_STDEXT_SMEP,cpu_stdext_feature(%rip)
864 END(handle_ibrs_entry)
866 ENTRY(handle_ibrs_exit)
867 cmpb $0,PCPU(IBPB_SET)
869 movl $MSR_IA32_SPEC_CTRL,%ecx
871 andl $~(IA32_SPEC_CTRL_IBRS|IA32_SPEC_CTRL_STIBP),%eax
872 andl $~((IA32_SPEC_CTRL_IBRS|IA32_SPEC_CTRL_STIBP)>>32),%edx
874 movb $0,PCPU(IBPB_SET)
876 END(handle_ibrs_exit)
878 /* registers-neutral version, but needs stack */
879 ENTRY(handle_ibrs_exit_rs)
880 cmpb $0,PCPU(IBPB_SET)
885 movl $MSR_IA32_SPEC_CTRL,%ecx
887 andl $~(IA32_SPEC_CTRL_IBRS|IA32_SPEC_CTRL_STIBP),%eax
888 andl $~((IA32_SPEC_CTRL_IBRS|IA32_SPEC_CTRL_STIBP)>>32),%edx
893 movb $0,PCPU(IBPB_SET)
895 END(handle_ibrs_exit_rs)