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 * 4. 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/intr_machdep.h>
37 #include <machine/pmap.h>
45 * void bzero(void *buf, u_int len)
72 movnti %rax,(%rdi,%rdx)
73 movnti %rax,8(%rdi,%rdx)
74 movnti %rax,16(%rdi,%rdx)
75 movnti %rax,24(%rdi,%rdx)
87 cld /* compare forwards */
104 * bcopy(src, dst, cnt)
106 * ws@tools.de (Wolfgang Solfrank, TooLs GmbH) +49-228-985800
115 cmpq %rcx,%rax /* overlapping && src < dst? */
118 shrq $3,%rcx /* copy by 64-bit words */
119 cld /* nope, copy forwards */
123 andq $7,%rcx /* any bytes left? */
131 addq %rcx,%rdi /* copy backwards */
135 andq $7,%rcx /* any fractional bytes? */
139 movq %rdx,%rcx /* copy remainder by 32-bit words */
151 * Note: memcpy does not support overlapping copies
156 shrq $3,%rcx /* copy by 64-bit words */
157 cld /* copy forwards */
161 andq $7,%rcx /* any bytes left? */
169 * pagecopy(%rdi=from, %rsi=to)
173 movq $-PAGE_SIZE,%rax
178 prefetchnta (%rdi,%rax)
182 movq (%rdi,%rdx),%rax
183 movnti %rax,(%rsi,%rdx)
184 movq 8(%rdi,%rdx),%rax
185 movnti %rax,8(%rsi,%rdx)
186 movq 16(%rdi,%rdx),%rax
187 movnti %rax,16(%rsi,%rdx)
188 movq 24(%rdi,%rdx),%rax
189 movnti %rax,24(%rsi,%rdx)
197 /* fillw(pat, base, cnt) */
198 /* %rdi,%rsi, %rdx */
211 /*****************************************************************************/
212 /* copyout and fubyte family */
213 /*****************************************************************************/
215 * Access user memory from inside the kernel. These routines should be
216 * the only places that do this.
218 * These routines set curpcb->pcb_onfault for the time they execute. When a
219 * protection violation occurs inside the functions, the trap handler
220 * returns to *curpcb->pcb_onfault instead of the function.
224 * copyout(from_kernel, to_user, len) - MP SAFE
229 movq PCPU(CURPCB),%rax
230 movq $copyout_fault,PCB_ONFAULT(%rax)
231 testq %rdx,%rdx /* anything to do? */
235 * Check explicitly for non-user addresses. If 486 write protection
236 * is being used, this check is essential because we are in kernel
237 * mode so the h/w does not provide any protection against writing
242 * First, prevent address wrapping.
248 * XXX STOP USING VM_MAXUSER_ADDRESS.
249 * It is an end address, not a max, so every time it is used correctly it
250 * looks like there is an off by one error, and of course it caused an off
251 * by one error in several places.
253 movq $VM_MAXUSER_ADDRESS,%rcx
258 /* bcopy(%rsi, %rdi, %rdx) */
272 movq PCPU(CURPCB),%rdx
273 movq %rax,PCB_ONFAULT(%rdx)
279 movq PCPU(CURPCB),%rdx
280 movq $0,PCB_ONFAULT(%rdx)
287 * copyin(from_user, to_kernel, len) - MP SAFE
292 movq PCPU(CURPCB),%rax
293 movq $copyin_fault,PCB_ONFAULT(%rax)
294 testq %rdx,%rdx /* anything to do? */
298 * make sure address is valid
303 movq $VM_MAXUSER_ADDRESS,%rcx
310 shrq $3,%rcx /* copy longword-wise */
315 andb $7,%cl /* copy remaining bytes */
321 movq PCPU(CURPCB),%rdx
322 movq %rax,PCB_ONFAULT(%rdx)
328 movq PCPU(CURPCB),%rdx
329 movq $0,PCB_ONFAULT(%rdx)
336 * casueword32. Compare and set user integer. Returns -1 on fault,
337 * 0 if access was successful. Old value is written to *oldp.
338 * dst = %rdi, old = %esi, oldp = %rdx, new = %ecx
342 movq PCPU(CURPCB),%r8
343 movq $fusufault,PCB_ONFAULT(%r8)
345 movq $VM_MAXUSER_ADDRESS-4,%rax
346 cmpq %rax,%rdi /* verify address is valid */
349 movl %esi,%eax /* old */
353 cmpxchgl %ecx,(%rdi) /* new = %ecx */
356 * The old value is in %eax. If the store succeeded it will be the
357 * value we expected (old) from before the store, otherwise it will
358 * be the current value. Save %eax into %esi to prepare the return
363 movq %rax,PCB_ONFAULT(%r8)
366 * Access the oldp after the pcb_onfault is cleared, to correctly
367 * catch corrupted pointer.
369 movl %esi,(%rdx) /* oldp = %rdx */
375 * casueword. Compare and set user long. Returns -1 on fault,
376 * 0 if access was successful. Old value is written to *oldp.
377 * dst = %rdi, old = %rsi, oldp = %rdx, new = %rcx
381 movq PCPU(CURPCB),%r8
382 movq $fusufault,PCB_ONFAULT(%r8)
384 movq $VM_MAXUSER_ADDRESS-4,%rax
385 cmpq %rax,%rdi /* verify address is valid */
388 movq %rsi,%rax /* old */
392 cmpxchgq %rcx,(%rdi) /* new = %rcx */
395 * The old value is in %rax. If the store succeeded it will be the
396 * value we expected (old) from before the store, otherwise it will
397 * be the current value.
401 movq %rax,PCB_ONFAULT(%r8)
408 * Fetch (load) a 64-bit word, a 32-bit word, a 16-bit word, or an 8-bit
409 * byte from user memory.
410 * addr = %rdi, valp = %rsi
416 movq PCPU(CURPCB),%rcx
417 movq $fusufault,PCB_ONFAULT(%rcx)
419 movq $VM_MAXUSER_ADDRESS-8,%rax
420 cmpq %rax,%rdi /* verify address is valid */
425 movq %rax,PCB_ONFAULT(%rcx)
434 movq PCPU(CURPCB),%rcx
435 movq $fusufault,PCB_ONFAULT(%rcx)
437 movq $VM_MAXUSER_ADDRESS-4,%rax
438 cmpq %rax,%rdi /* verify address is valid */
443 movq %rax,PCB_ONFAULT(%rcx)
450 * fuswintr() and suswintr() are specialized variants of fuword16() and
451 * suword16(), respectively. They are called from the profiling code,
452 * potentially at interrupt time. If they fail, that's okay; good things
453 * will happen later. They always fail for now, until the trap code is
454 * able to deal with this.
465 movq PCPU(CURPCB),%rcx
466 movq $fusufault,PCB_ONFAULT(%rcx)
468 movq $VM_MAXUSER_ADDRESS-2,%rax
473 movq $0,PCB_ONFAULT(%rcx)
480 movq PCPU(CURPCB),%rcx
481 movq $fusufault,PCB_ONFAULT(%rcx)
483 movq $VM_MAXUSER_ADDRESS-1,%rax
488 movq $0,PCB_ONFAULT(%rcx)
495 movq PCPU(CURPCB),%rcx
497 movq %rax,PCB_ONFAULT(%rcx)
503 * Store a 64-bit word, a 32-bit word, a 16-bit word, or an 8-bit byte to
504 * user memory. All these functions are MPSAFE.
505 * addr = %rdi, value = %rsi
510 movq PCPU(CURPCB),%rcx
511 movq $fusufault,PCB_ONFAULT(%rcx)
513 movq $VM_MAXUSER_ADDRESS-8,%rax
514 cmpq %rax,%rdi /* verify address validity */
519 movq PCPU(CURPCB),%rcx
520 movq %rax,PCB_ONFAULT(%rcx)
528 movq PCPU(CURPCB),%rcx
529 movq $fusufault,PCB_ONFAULT(%rcx)
531 movq $VM_MAXUSER_ADDRESS-4,%rax
532 cmpq %rax,%rdi /* verify address validity */
537 movq PCPU(CURPCB),%rcx
538 movq %rax,PCB_ONFAULT(%rcx)
545 movq PCPU(CURPCB),%rcx
546 movq $fusufault,PCB_ONFAULT(%rcx)
548 movq $VM_MAXUSER_ADDRESS-2,%rax
549 cmpq %rax,%rdi /* verify address validity */
554 movq PCPU(CURPCB),%rcx /* restore trashed register */
555 movq %rax,PCB_ONFAULT(%rcx)
562 movq PCPU(CURPCB),%rcx
563 movq $fusufault,PCB_ONFAULT(%rcx)
565 movq $VM_MAXUSER_ADDRESS-1,%rax
566 cmpq %rax,%rdi /* verify address validity */
572 movq PCPU(CURPCB),%rcx /* restore trashed register */
573 movq %rax,PCB_ONFAULT(%rcx)
579 * copyinstr(from, to, maxlen, int *lencopied) - MP SAFE
580 * %rdi, %rsi, %rdx, %rcx
582 * copy a string from from to to, stop when a 0 character is reached.
583 * return ENAMETOOLONG if string is longer than maxlen, and
584 * EFAULT on protection violations. If lencopied is non-zero,
585 * return the actual length in *lencopied.
589 movq %rdx,%r8 /* %r8 = maxlen */
590 movq %rcx,%r9 /* %r9 = *len */
591 xchgq %rdi,%rsi /* %rdi = from, %rsi = to */
592 movq PCPU(CURPCB),%rcx
593 movq $cpystrflt,PCB_ONFAULT(%rcx)
595 movq $VM_MAXUSER_ADDRESS,%rax
597 /* make sure 'from' is within bounds */
601 /* restrict maxlen to <= VM_MAXUSER_ADDRESS-from */
619 /* Success -- 0 byte reached */
624 /* rdx is zero - return ENAMETOOLONG or EFAULT */
625 movq $VM_MAXUSER_ADDRESS,%rax
629 movq $ENAMETOOLONG,%rax
636 /* set *lencopied and return %eax */
637 movq PCPU(CURPCB),%rcx
638 movq $0,PCB_ONFAULT(%rcx)
650 * copystr(from, to, maxlen, int *lencopied) - MP SAFE
651 * %rdi, %rsi, %rdx, %rcx
655 movq %rdx,%r8 /* %r8 = maxlen */
668 /* Success -- 0 byte reached */
673 /* rdx is zero -- return ENAMETOOLONG */
674 movq $ENAMETOOLONG,%rax
680 /* set *lencopied and return %rax */
689 * Handling of special amd64 registers and descriptor tables etc
692 /* void lgdt(struct region_descriptor *rdp); */
694 /* reload the descriptor table */
697 /* flush the prefetch q */
704 movl %eax,%fs /* Beware, use wrmsr to set 64 bit base */
708 /* reload code selector by turning return into intersegmental return */
716 /*****************************************************************************/
717 /* setjump, longjump */
718 /*****************************************************************************/
721 movq %rbx,0(%rdi) /* save rbx */
722 movq %rsp,8(%rdi) /* save rsp */
723 movq %rbp,16(%rdi) /* save rbp */
724 movq %r12,24(%rdi) /* save r12 */
725 movq %r13,32(%rdi) /* save r13 */
726 movq %r14,40(%rdi) /* save r14 */
727 movq %r15,48(%rdi) /* save r15 */
728 movq 0(%rsp),%rdx /* get rta */
729 movq %rdx,56(%rdi) /* save rip */
730 xorl %eax,%eax /* return(0); */
735 movq 0(%rdi),%rbx /* restore rbx */
736 movq 8(%rdi),%rsp /* restore rsp */
737 movq 16(%rdi),%rbp /* restore rbp */
738 movq 24(%rdi),%r12 /* restore r12 */
739 movq 32(%rdi),%r13 /* restore r13 */
740 movq 40(%rdi),%r14 /* restore r14 */
741 movq 48(%rdi),%r15 /* restore r15 */
742 movq 56(%rdi),%rdx /* get rta */
743 movq %rdx,0(%rsp) /* put in return frame */
744 xorl %eax,%eax /* return(1); */
750 * Support for reading MSRs in the safe manner.
753 /* int rdmsr_safe(u_int msr, uint64_t *data) */
755 movq PCPU(CURPCB),%r8
756 movq $msr_onfault,PCB_ONFAULT(%r8)
758 rdmsr /* Read MSR pointed by %ecx. Returns
759 hi byte in edx, lo in %eax */
760 salq $32,%rdx /* sign-shift %rdx left */
761 movl %eax,%eax /* zero-extend %eax -> %rax */
765 movq %rax,PCB_ONFAULT(%r8)
770 * Support for writing MSRs in the safe manner.
773 /* int wrmsr_safe(u_int msr, uint64_t data) */
775 movq PCPU(CURPCB),%r8
776 movq $msr_onfault,PCB_ONFAULT(%r8)
781 wrmsr /* Write MSR pointed by %ecx. Accepts
782 hi byte in edx, lo in %eax. */
784 movq %rax,PCB_ONFAULT(%r8)
789 * MSR operations fault handler
793 movq $0,PCB_ONFAULT(%r8)