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/pmap.h>
44 * void bzero(void *buf, u_int len)
67 movq $PAGE_SIZE/8,%rcx
79 cld /* compare forwards */
96 * bcopy(src, dst, cnt)
98 * ws@tools.de (Wolfgang Solfrank, TooLs GmbH) +49-228-985800
107 cmpq %rcx,%rax /* overlapping && src < dst? */
110 shrq $3,%rcx /* copy by 64-bit words */
111 cld /* nope, copy forwards */
115 andq $7,%rcx /* any bytes left? */
123 addq %rcx,%rdi /* copy backwards */
127 andq $7,%rcx /* any fractional bytes? */
131 movq %rdx,%rcx /* copy remainder by 32-bit words */
143 * Note: memcpy does not support overlapping copies
149 shrq $3,%rcx /* copy by 64-bit words */
150 cld /* copy forwards */
154 andq $7,%rcx /* any bytes left? */
162 * pagecopy(%rdi=from, %rsi=to)
166 movq $-PAGE_SIZE,%rax
171 prefetchnta (%rdi,%rax)
175 movq (%rdi,%rdx),%rax
176 movnti %rax,(%rsi,%rdx)
177 movq 8(%rdi,%rdx),%rax
178 movnti %rax,8(%rsi,%rdx)
179 movq 16(%rdi,%rdx),%rax
180 movnti %rax,16(%rsi,%rdx)
181 movq 24(%rdi,%rdx),%rax
182 movnti %rax,24(%rsi,%rdx)
190 /* fillw(pat, base, cnt) */
191 /* %rdi,%rsi, %rdx */
204 /*****************************************************************************/
205 /* copyout and fubyte family */
206 /*****************************************************************************/
208 * Access user memory from inside the kernel. These routines should be
209 * the only places that do this.
211 * These routines set curpcb->pcb_onfault for the time they execute. When a
212 * protection violation occurs inside the functions, the trap handler
213 * returns to *curpcb->pcb_onfault instead of the function.
217 * copyout(from_kernel, to_user, len) - MP SAFE
222 movq PCPU(CURPCB),%rax
223 movq $copyout_fault,PCB_ONFAULT(%rax)
224 testq %rdx,%rdx /* anything to do? */
228 * Check explicitly for non-user addresses. If 486 write protection
229 * is being used, this check is essential because we are in kernel
230 * mode so the h/w does not provide any protection against writing
235 * First, prevent address wrapping.
241 * XXX STOP USING VM_MAXUSER_ADDRESS.
242 * It is an end address, not a max, so every time it is used correctly it
243 * looks like there is an off by one error, and of course it caused an off
244 * by one error in several places.
246 movq $VM_MAXUSER_ADDRESS,%rcx
251 /* bcopy(%rsi, %rdi, %rdx) */
265 movq PCPU(CURPCB),%rdx
266 movq %rax,PCB_ONFAULT(%rdx)
272 movq PCPU(CURPCB),%rdx
273 movq $0,PCB_ONFAULT(%rdx)
280 * copyin(from_user, to_kernel, len) - MP SAFE
285 movq PCPU(CURPCB),%rax
286 movq $copyin_fault,PCB_ONFAULT(%rax)
287 testq %rdx,%rdx /* anything to do? */
291 * make sure address is valid
296 movq $VM_MAXUSER_ADDRESS,%rcx
303 shrq $3,%rcx /* copy longword-wise */
308 andb $7,%cl /* copy remaining bytes */
314 movq PCPU(CURPCB),%rdx
315 movq %rax,PCB_ONFAULT(%rdx)
321 movq PCPU(CURPCB),%rdx
322 movq $0,PCB_ONFAULT(%rdx)
329 * casueword32. Compare and set user integer. Returns -1 on fault,
330 * 0 if access was successful. Old value is written to *oldp.
331 * dst = %rdi, old = %esi, oldp = %rdx, new = %ecx
335 movq PCPU(CURPCB),%r8
336 movq $fusufault,PCB_ONFAULT(%r8)
338 movq $VM_MAXUSER_ADDRESS-4,%rax
339 cmpq %rax,%rdi /* verify address is valid */
342 movl %esi,%eax /* old */
346 cmpxchgl %ecx,(%rdi) /* new = %ecx */
349 * The old value is in %eax. If the store succeeded it will be the
350 * value we expected (old) from before the store, otherwise it will
351 * be the current value. Save %eax into %esi to prepare the return
356 movq %rax,PCB_ONFAULT(%r8)
359 * Access the oldp after the pcb_onfault is cleared, to correctly
360 * catch corrupted pointer.
362 movl %esi,(%rdx) /* oldp = %rdx */
368 * casueword. Compare and set user long. Returns -1 on fault,
369 * 0 if access was successful. Old value is written to *oldp.
370 * dst = %rdi, old = %rsi, oldp = %rdx, new = %rcx
374 movq PCPU(CURPCB),%r8
375 movq $fusufault,PCB_ONFAULT(%r8)
377 movq $VM_MAXUSER_ADDRESS-4,%rax
378 cmpq %rax,%rdi /* verify address is valid */
381 movq %rsi,%rax /* old */
385 cmpxchgq %rcx,(%rdi) /* new = %rcx */
388 * The old value is in %rax. If the store succeeded it will be the
389 * value we expected (old) from before the store, otherwise it will
390 * be the current value.
394 movq %rax,PCB_ONFAULT(%r8)
401 * Fetch (load) a 64-bit word, a 32-bit word, a 16-bit word, or an 8-bit
402 * byte from user memory.
403 * addr = %rdi, valp = %rsi
409 movq PCPU(CURPCB),%rcx
410 movq $fusufault,PCB_ONFAULT(%rcx)
412 movq $VM_MAXUSER_ADDRESS-8,%rax
413 cmpq %rax,%rdi /* verify address is valid */
418 movq %rax,PCB_ONFAULT(%rcx)
427 movq PCPU(CURPCB),%rcx
428 movq $fusufault,PCB_ONFAULT(%rcx)
430 movq $VM_MAXUSER_ADDRESS-4,%rax
431 cmpq %rax,%rdi /* verify address is valid */
436 movq %rax,PCB_ONFAULT(%rcx)
443 * fuswintr() and suswintr() are specialized variants of fuword16() and
444 * suword16(), respectively. They are called from the profiling code,
445 * potentially at interrupt time. If they fail, that's okay; good things
446 * will happen later. They always fail for now, until the trap code is
447 * able to deal with this.
458 movq PCPU(CURPCB),%rcx
459 movq $fusufault,PCB_ONFAULT(%rcx)
461 movq $VM_MAXUSER_ADDRESS-2,%rax
466 movq $0,PCB_ONFAULT(%rcx)
473 movq PCPU(CURPCB),%rcx
474 movq $fusufault,PCB_ONFAULT(%rcx)
476 movq $VM_MAXUSER_ADDRESS-1,%rax
481 movq $0,PCB_ONFAULT(%rcx)
488 movq PCPU(CURPCB),%rcx
490 movq %rax,PCB_ONFAULT(%rcx)
496 * Store a 64-bit word, a 32-bit word, a 16-bit word, or an 8-bit byte to
497 * user memory. All these functions are MPSAFE.
498 * addr = %rdi, value = %rsi
503 movq PCPU(CURPCB),%rcx
504 movq $fusufault,PCB_ONFAULT(%rcx)
506 movq $VM_MAXUSER_ADDRESS-8,%rax
507 cmpq %rax,%rdi /* verify address validity */
512 movq PCPU(CURPCB),%rcx
513 movq %rax,PCB_ONFAULT(%rcx)
521 movq PCPU(CURPCB),%rcx
522 movq $fusufault,PCB_ONFAULT(%rcx)
524 movq $VM_MAXUSER_ADDRESS-4,%rax
525 cmpq %rax,%rdi /* verify address validity */
530 movq PCPU(CURPCB),%rcx
531 movq %rax,PCB_ONFAULT(%rcx)
538 movq PCPU(CURPCB),%rcx
539 movq $fusufault,PCB_ONFAULT(%rcx)
541 movq $VM_MAXUSER_ADDRESS-2,%rax
542 cmpq %rax,%rdi /* verify address validity */
547 movq PCPU(CURPCB),%rcx /* restore trashed register */
548 movq %rax,PCB_ONFAULT(%rcx)
555 movq PCPU(CURPCB),%rcx
556 movq $fusufault,PCB_ONFAULT(%rcx)
558 movq $VM_MAXUSER_ADDRESS-1,%rax
559 cmpq %rax,%rdi /* verify address validity */
565 movq PCPU(CURPCB),%rcx /* restore trashed register */
566 movq %rax,PCB_ONFAULT(%rcx)
572 * copyinstr(from, to, maxlen, int *lencopied) - MP SAFE
573 * %rdi, %rsi, %rdx, %rcx
575 * copy a string from from to to, stop when a 0 character is reached.
576 * return ENAMETOOLONG if string is longer than maxlen, and
577 * EFAULT on protection violations. If lencopied is non-zero,
578 * return the actual length in *lencopied.
582 movq %rdx,%r8 /* %r8 = maxlen */
583 movq %rcx,%r9 /* %r9 = *len */
584 xchgq %rdi,%rsi /* %rdi = from, %rsi = to */
585 movq PCPU(CURPCB),%rcx
586 movq $cpystrflt,PCB_ONFAULT(%rcx)
588 movq $VM_MAXUSER_ADDRESS,%rax
590 /* make sure 'from' is within bounds */
594 /* restrict maxlen to <= VM_MAXUSER_ADDRESS-from */
612 /* Success -- 0 byte reached */
617 /* rdx is zero - return ENAMETOOLONG or EFAULT */
618 movq $VM_MAXUSER_ADDRESS,%rax
622 movq $ENAMETOOLONG,%rax
629 /* set *lencopied and return %eax */
630 movq PCPU(CURPCB),%rcx
631 movq $0,PCB_ONFAULT(%rcx)
643 * copystr(from, to, maxlen, int *lencopied) - MP SAFE
644 * %rdi, %rsi, %rdx, %rcx
648 movq %rdx,%r8 /* %r8 = maxlen */
661 /* Success -- 0 byte reached */
666 /* rdx is zero -- return ENAMETOOLONG */
667 movq $ENAMETOOLONG,%rax
673 /* set *lencopied and return %rax */
682 * Handling of special amd64 registers and descriptor tables etc
685 /* void lgdt(struct region_descriptor *rdp); */
687 /* reload the descriptor table */
690 /* flush the prefetch q */
697 movl %eax,%fs /* Beware, use wrmsr to set 64 bit base */
701 /* reload code selector by turning return into intersegmental return */
709 /*****************************************************************************/
710 /* setjump, longjump */
711 /*****************************************************************************/
714 movq %rbx,0(%rdi) /* save rbx */
715 movq %rsp,8(%rdi) /* save rsp */
716 movq %rbp,16(%rdi) /* save rbp */
717 movq %r12,24(%rdi) /* save r12 */
718 movq %r13,32(%rdi) /* save r13 */
719 movq %r14,40(%rdi) /* save r14 */
720 movq %r15,48(%rdi) /* save r15 */
721 movq 0(%rsp),%rdx /* get rta */
722 movq %rdx,56(%rdi) /* save rip */
723 xorl %eax,%eax /* return(0); */
728 movq 0(%rdi),%rbx /* restore rbx */
729 movq 8(%rdi),%rsp /* restore rsp */
730 movq 16(%rdi),%rbp /* restore rbp */
731 movq 24(%rdi),%r12 /* restore r12 */
732 movq 32(%rdi),%r13 /* restore r13 */
733 movq 40(%rdi),%r14 /* restore r14 */
734 movq 48(%rdi),%r15 /* restore r15 */
735 movq 56(%rdi),%rdx /* get rta */
736 movq %rdx,0(%rsp) /* put in return frame */
737 xorl %eax,%eax /* return(1); */
743 * Support for reading MSRs in the safe manner.
746 /* int rdmsr_safe(u_int msr, uint64_t *data) */
748 movq PCPU(CURPCB),%r8
749 movq $msr_onfault,PCB_ONFAULT(%r8)
751 rdmsr /* Read MSR pointed by %ecx. Returns
752 hi byte in edx, lo in %eax */
753 salq $32,%rdx /* sign-shift %rdx left */
754 movl %eax,%eax /* zero-extend %eax -> %rax */
758 movq %rax,PCB_ONFAULT(%r8)
763 * Support for writing MSRs in the safe manner.
766 /* int wrmsr_safe(u_int msr, uint64_t data) */
768 movq PCPU(CURPCB),%r8
769 movq $msr_onfault,PCB_ONFAULT(%r8)
774 wrmsr /* Write MSR pointed by %ecx. Accepts
775 hi byte in edx, lo in %eax. */
777 movq %rax,PCB_ONFAULT(%r8)
782 * MSR operations fault handler
786 movq $0,PCB_ONFAULT(%r8)