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 * Note: memcpy does not support overlapping copies
171 shrq $3,%rcx /* copy by 64-bit words */
175 andq $7,%rcx /* any bytes left? */
183 * pagecopy(%rdi=from, %rsi=to)
187 movq $-PAGE_SIZE,%rax
192 prefetchnta (%rdi,%rax)
196 movq (%rdi,%rdx),%rax
197 movnti %rax,(%rsi,%rdx)
198 movq 8(%rdi,%rdx),%rax
199 movnti %rax,8(%rsi,%rdx)
200 movq 16(%rdi,%rdx),%rax
201 movnti %rax,16(%rsi,%rdx)
202 movq 24(%rdi,%rdx),%rax
203 movnti %rax,24(%rsi,%rdx)
211 /* fillw(pat, base, cnt) */
212 /* %rdi,%rsi, %rdx */
224 /*****************************************************************************/
225 /* copyout and fubyte family */
226 /*****************************************************************************/
228 * Access user memory from inside the kernel. These routines should be
229 * the only places that do this.
231 * These routines set curpcb->pcb_onfault for the time they execute. When a
232 * protection violation occurs inside the functions, the trap handler
233 * returns to *curpcb->pcb_onfault instead of the function.
237 * copyout(from_kernel, to_user, len)
242 movq PCPU(CURPCB),%rax
243 movq $copyout_fault,PCB_ONFAULT(%rax)
244 testq %rdx,%rdx /* anything to do? */
248 * Check explicitly for non-user addresses. This check is essential
249 * because it prevents usermode from writing into the kernel. We do
250 * not verify anywhere else that the user did not specify a rogue
254 * First, prevent address wrapping.
260 * XXX STOP USING VM_MAXUSER_ADDRESS.
261 * It is an end address, not a max, so every time it is used correctly it
262 * looks like there is an off by one error, and of course it caused an off
263 * by one error in several places.
265 movq $VM_MAXUSER_ADDRESS,%rcx
270 /* bcopy(%rsi, %rdi, %rdx) */
283 movq PCPU(CURPCB),%rdx
284 movq %rax,PCB_ONFAULT(%rdx)
290 movq PCPU(CURPCB),%rdx
291 movq $0,PCB_ONFAULT(%rdx)
298 * copyin(from_user, to_kernel, len)
303 movq PCPU(CURPCB),%rax
304 movq $copyin_fault,PCB_ONFAULT(%rax)
305 testq %rdx,%rdx /* anything to do? */
309 * make sure address is valid
314 movq $VM_MAXUSER_ADDRESS,%rcx
321 shrq $3,%rcx /* copy longword-wise */
325 andb $7,%cl /* copy remaining bytes */
331 movq PCPU(CURPCB),%rdx
332 movq %rax,PCB_ONFAULT(%rdx)
338 movq PCPU(CURPCB),%rdx
339 movq $0,PCB_ONFAULT(%rdx)
346 * casueword32. Compare and set user integer. Returns -1 on fault,
347 * 0 if access was successful. Old value is written to *oldp.
348 * dst = %rdi, old = %esi, oldp = %rdx, new = %ecx
352 movq PCPU(CURPCB),%r8
353 movq $fusufault,PCB_ONFAULT(%r8)
355 movq $VM_MAXUSER_ADDRESS-4,%rax
356 cmpq %rax,%rdi /* verify address is valid */
359 movl %esi,%eax /* old */
363 cmpxchgl %ecx,(%rdi) /* new = %ecx */
366 * The old value is in %eax. If the store succeeded it will be the
367 * value we expected (old) from before the store, otherwise it will
368 * be the current value. Save %eax into %esi to prepare the return
373 movq %rax,PCB_ONFAULT(%r8)
376 * Access the oldp after the pcb_onfault is cleared, to correctly
377 * catch corrupted pointer.
379 movl %esi,(%rdx) /* oldp = %rdx */
385 * casueword. Compare and set user long. Returns -1 on fault,
386 * 0 if access was successful. Old value is written to *oldp.
387 * dst = %rdi, old = %rsi, oldp = %rdx, new = %rcx
391 movq PCPU(CURPCB),%r8
392 movq $fusufault,PCB_ONFAULT(%r8)
394 movq $VM_MAXUSER_ADDRESS-4,%rax
395 cmpq %rax,%rdi /* verify address is valid */
398 movq %rsi,%rax /* old */
402 cmpxchgq %rcx,(%rdi) /* new = %rcx */
405 * The old value is in %rax. If the store succeeded it will be the
406 * value we expected (old) from before the store, otherwise it will
407 * be the current value.
411 movq %rax,PCB_ONFAULT(%r8)
418 * Fetch (load) a 64-bit word, a 32-bit word, a 16-bit word, or an 8-bit
419 * byte from user memory.
420 * addr = %rdi, valp = %rsi
426 movq PCPU(CURPCB),%rcx
427 movq $fusufault,PCB_ONFAULT(%rcx)
429 movq $VM_MAXUSER_ADDRESS-8,%rax
430 cmpq %rax,%rdi /* verify address is valid */
435 movq %rax,PCB_ONFAULT(%rcx)
444 movq PCPU(CURPCB),%rcx
445 movq $fusufault,PCB_ONFAULT(%rcx)
447 movq $VM_MAXUSER_ADDRESS-4,%rax
448 cmpq %rax,%rdi /* verify address is valid */
453 movq %rax,PCB_ONFAULT(%rcx)
460 * fuswintr() and suswintr() are specialized variants of fuword16() and
461 * suword16(), respectively. They are called from the profiling code,
462 * potentially at interrupt time. If they fail, that's okay; good things
463 * will happen later. They always fail for now, until the trap code is
464 * able to deal with this.
475 movq PCPU(CURPCB),%rcx
476 movq $fusufault,PCB_ONFAULT(%rcx)
478 movq $VM_MAXUSER_ADDRESS-2,%rax
483 movq $0,PCB_ONFAULT(%rcx)
490 movq PCPU(CURPCB),%rcx
491 movq $fusufault,PCB_ONFAULT(%rcx)
493 movq $VM_MAXUSER_ADDRESS-1,%rax
498 movq $0,PCB_ONFAULT(%rcx)
505 movq PCPU(CURPCB),%rcx
507 movq %rax,PCB_ONFAULT(%rcx)
513 * Store a 64-bit word, a 32-bit word, a 16-bit word, or an 8-bit byte to
515 * addr = %rdi, value = %rsi
520 movq PCPU(CURPCB),%rcx
521 movq $fusufault,PCB_ONFAULT(%rcx)
523 movq $VM_MAXUSER_ADDRESS-8,%rax
524 cmpq %rax,%rdi /* verify address validity */
529 movq PCPU(CURPCB),%rcx
530 movq %rax,PCB_ONFAULT(%rcx)
538 movq PCPU(CURPCB),%rcx
539 movq $fusufault,PCB_ONFAULT(%rcx)
541 movq $VM_MAXUSER_ADDRESS-4,%rax
542 cmpq %rax,%rdi /* verify address validity */
547 movq PCPU(CURPCB),%rcx
548 movq %rax,PCB_ONFAULT(%rcx)
555 movq PCPU(CURPCB),%rcx
556 movq $fusufault,PCB_ONFAULT(%rcx)
558 movq $VM_MAXUSER_ADDRESS-2,%rax
559 cmpq %rax,%rdi /* verify address validity */
564 movq PCPU(CURPCB),%rcx /* restore trashed register */
565 movq %rax,PCB_ONFAULT(%rcx)
572 movq PCPU(CURPCB),%rcx
573 movq $fusufault,PCB_ONFAULT(%rcx)
575 movq $VM_MAXUSER_ADDRESS-1,%rax
576 cmpq %rax,%rdi /* verify address validity */
582 movq PCPU(CURPCB),%rcx /* restore trashed register */
583 movq %rax,PCB_ONFAULT(%rcx)
589 * copyinstr(from, to, maxlen, int *lencopied)
590 * %rdi, %rsi, %rdx, %rcx
592 * copy a string from 'from' to 'to', stop when a 0 character is reached.
593 * return ENAMETOOLONG if string is longer than maxlen, and
594 * EFAULT on protection violations. If lencopied is non-zero,
595 * return the actual length in *lencopied.
599 movq %rdx,%r8 /* %r8 = maxlen */
600 movq %rcx,%r9 /* %r9 = *len */
601 xchgq %rdi,%rsi /* %rdi = from, %rsi = to */
602 movq PCPU(CURPCB),%rcx
603 movq $cpystrflt,PCB_ONFAULT(%rcx)
605 movq $VM_MAXUSER_ADDRESS,%rax
607 /* make sure 'from' is within bounds */
611 /* restrict maxlen to <= VM_MAXUSER_ADDRESS-from */
628 /* Success -- 0 byte reached */
633 /* rdx is zero - return ENAMETOOLONG or EFAULT */
634 movq $VM_MAXUSER_ADDRESS,%rax
638 movq $ENAMETOOLONG,%rax
645 /* set *lencopied and return %eax */
646 movq PCPU(CURPCB),%rcx
647 movq $0,PCB_ONFAULT(%rcx)
659 * copystr(from, to, maxlen, int *lencopied)
660 * %rdi, %rsi, %rdx, %rcx
664 movq %rdx,%r8 /* %r8 = maxlen */
676 /* Success -- 0 byte reached */
681 /* rdx is zero -- return ENAMETOOLONG */
682 movq $ENAMETOOLONG,%rax
688 /* set *lencopied and return %rax */
697 * Handling of special amd64 registers and descriptor tables etc
699 /* void lgdt(struct region_descriptor *rdp); */
701 /* reload the descriptor table */
704 /* flush the prefetch q */
711 movl %eax,%fs /* Beware, use wrmsr to set 64 bit base */
715 /* reload code selector by turning return into intersegmental return */
723 /*****************************************************************************/
724 /* setjump, longjump */
725 /*****************************************************************************/
728 movq %rbx,0(%rdi) /* save rbx */
729 movq %rsp,8(%rdi) /* save rsp */
730 movq %rbp,16(%rdi) /* save rbp */
731 movq %r12,24(%rdi) /* save r12 */
732 movq %r13,32(%rdi) /* save r13 */
733 movq %r14,40(%rdi) /* save r14 */
734 movq %r15,48(%rdi) /* save r15 */
735 movq 0(%rsp),%rdx /* get rta */
736 movq %rdx,56(%rdi) /* save rip */
737 xorl %eax,%eax /* return(0); */
742 movq 0(%rdi),%rbx /* restore rbx */
743 movq 8(%rdi),%rsp /* restore rsp */
744 movq 16(%rdi),%rbp /* restore rbp */
745 movq 24(%rdi),%r12 /* restore r12 */
746 movq 32(%rdi),%r13 /* restore r13 */
747 movq 40(%rdi),%r14 /* restore r14 */
748 movq 48(%rdi),%r15 /* restore r15 */
749 movq 56(%rdi),%rdx /* get rta */
750 movq %rdx,0(%rsp) /* put in return frame */
751 xorl %eax,%eax /* return(1); */
757 * Support for reading MSRs in the safe manner. (Instead of panic on #gp,
761 /* int rdmsr_safe(u_int msr, uint64_t *data) */
763 movq PCPU(CURPCB),%r8
764 movq $msr_onfault,PCB_ONFAULT(%r8)
766 rdmsr /* Read MSR pointed by %ecx. Returns
767 hi byte in edx, lo in %eax */
768 salq $32,%rdx /* sign-shift %rdx left */
769 movl %eax,%eax /* zero-extend %eax -> %rax */
773 movq %rax,PCB_ONFAULT(%r8)
778 * Support for writing MSRs in the safe manner. (Instead of panic on #gp,
782 /* int wrmsr_safe(u_int msr, uint64_t data) */
784 movq PCPU(CURPCB),%r8
785 movq $msr_onfault,PCB_ONFAULT(%r8)
790 wrmsr /* Write MSR pointed by %ecx. Accepts
791 hi byte in edx, lo in %eax. */
793 movq %rax,PCB_ONFAULT(%r8)
798 * MSR operations fault handler
802 movq $0,PCB_ONFAULT(%r8)
808 * void pmap_pti_pcid_invalidate(uint64_t ucr3, uint64_t kcr3);
809 * Invalidates address space addressed by ucr3, then returns to kcr3.
810 * Done in assembler to ensure no other memory accesses happen while
814 ENTRY(pmap_pti_pcid_invalidate)
817 movq %rdi,%cr3 /* to user page table */
818 movq %rsi,%cr3 /* back to kernel */
823 * void pmap_pti_pcid_invlpg(uint64_t ucr3, uint64_t kcr3, vm_offset_t va);
824 * Invalidates virtual address va in address space ucr3, then returns to kcr3.
827 ENTRY(pmap_pti_pcid_invlpg)
830 movq %rdi,%cr3 /* to user page table */
832 movq %rsi,%cr3 /* back to kernel */
837 * void pmap_pti_pcid_invlrng(uint64_t ucr3, uint64_t kcr3, vm_offset_t sva,
839 * Invalidates virtual addresses between sva and eva in address space ucr3,
840 * then returns to kcr3.
843 ENTRY(pmap_pti_pcid_invlrng)
846 movq %rdi,%cr3 /* to user page table */
851 movq %rsi,%cr3 /* back to kernel */
856 .macro ibrs_seq_label l
859 .macro ibrs_call_label l
862 .macro ibrs_seq count
865 ibrs_call_label %(ll)
873 /* all callers already saved %rax, %rdx, and %rcx */
874 ENTRY(handle_ibrs_entry)
875 cmpb $0,hw_ibrs_active(%rip)
877 movl $MSR_IA32_SPEC_CTRL,%ecx
878 movl $(IA32_SPEC_CTRL_IBRS|IA32_SPEC_CTRL_STIBP),%eax
879 movl $(IA32_SPEC_CTRL_IBRS|IA32_SPEC_CTRL_STIBP)>>32,%edx
881 movb $1,PCPU(IBPB_SET)
882 testl $CPUID_STDEXT_SMEP,cpu_stdext_feature(%rip)
886 END(handle_ibrs_entry)
888 ENTRY(handle_ibrs_exit)
889 cmpb $0,PCPU(IBPB_SET)
891 movl $MSR_IA32_SPEC_CTRL,%ecx
895 movb $0,PCPU(IBPB_SET)
897 END(handle_ibrs_exit)
899 /* registers-neutral version, but needs stack */
900 ENTRY(handle_ibrs_exit_rs)
901 cmpb $0,PCPU(IBPB_SET)
906 movl $MSR_IA32_SPEC_CTRL,%ecx
913 movb $0,PCPU(IBPB_SET)
915 END(handle_ibrs_exit_rs)