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/pmap.h>
44 * 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)
103 cld /* compare forwards */
120 * bcopy(src, dst, cnt)
122 * ws@tools.de (Wolfgang Solfrank, TooLs GmbH) +49-228-985800
131 cmpq %rcx,%rax /* overlapping && src < dst? */
134 shrq $3,%rcx /* copy by 64-bit words */
135 cld /* nope, copy forwards */
139 andq $7,%rcx /* any bytes left? */
147 addq %rcx,%rdi /* copy backwards */
151 andq $7,%rcx /* any fractional bytes? */
155 movq %rdx,%rcx /* copy remainder by 32-bit words */
167 * Note: memcpy does not support overlapping copies
173 shrq $3,%rcx /* copy by 64-bit words */
174 cld /* copy forwards */
178 andq $7,%rcx /* any bytes left? */
186 * pagecopy(%rdi=from, %rsi=to)
190 movq $-PAGE_SIZE,%rax
195 prefetchnta (%rdi,%rax)
199 movq (%rdi,%rdx),%rax
200 movnti %rax,(%rsi,%rdx)
201 movq 8(%rdi,%rdx),%rax
202 movnti %rax,8(%rsi,%rdx)
203 movq 16(%rdi,%rdx),%rax
204 movnti %rax,16(%rsi,%rdx)
205 movq 24(%rdi,%rdx),%rax
206 movnti %rax,24(%rsi,%rdx)
214 /* fillw(pat, base, cnt) */
215 /* %rdi,%rsi, %rdx */
228 /*****************************************************************************/
229 /* copyout and fubyte family */
230 /*****************************************************************************/
232 * Access user memory from inside the kernel. These routines should be
233 * the only places that do this.
235 * These routines set curpcb->pcb_onfault for the time they execute. When a
236 * protection violation occurs inside the functions, the trap handler
237 * returns to *curpcb->pcb_onfault instead of the function.
241 * copyout(from_kernel, to_user, len) - MP SAFE
246 movq PCPU(CURPCB),%rax
247 movq $copyout_fault,PCB_ONFAULT(%rax)
248 testq %rdx,%rdx /* anything to do? */
252 * Check explicitly for non-user addresses. If 486 write protection
253 * is being used, this check is essential because we are in kernel
254 * mode so the h/w does not provide any protection against writing
259 * First, prevent address wrapping.
265 * XXX STOP USING VM_MAXUSER_ADDRESS.
266 * It is an end address, not a max, so every time it is used correctly it
267 * looks like there is an off by one error, and of course it caused an off
268 * by one error in several places.
270 movq $VM_MAXUSER_ADDRESS,%rcx
275 /* bcopy(%rsi, %rdi, %rdx) */
289 movq PCPU(CURPCB),%rdx
290 movq %rax,PCB_ONFAULT(%rdx)
296 movq PCPU(CURPCB),%rdx
297 movq $0,PCB_ONFAULT(%rdx)
304 * copyin(from_user, to_kernel, len) - MP SAFE
309 movq PCPU(CURPCB),%rax
310 movq $copyin_fault,PCB_ONFAULT(%rax)
311 testq %rdx,%rdx /* anything to do? */
315 * make sure address is valid
320 movq $VM_MAXUSER_ADDRESS,%rcx
327 shrq $3,%rcx /* copy longword-wise */
332 andb $7,%cl /* copy remaining bytes */
338 movq PCPU(CURPCB),%rdx
339 movq %rax,PCB_ONFAULT(%rdx)
345 movq PCPU(CURPCB),%rdx
346 movq $0,PCB_ONFAULT(%rdx)
353 * casueword32. Compare and set user integer. Returns -1 on fault,
354 * 0 if access was successful. Old value is written to *oldp.
355 * dst = %rdi, old = %esi, oldp = %rdx, new = %ecx
359 movq PCPU(CURPCB),%r8
360 movq $fusufault,PCB_ONFAULT(%r8)
362 movq $VM_MAXUSER_ADDRESS-4,%rax
363 cmpq %rax,%rdi /* verify address is valid */
366 movl %esi,%eax /* old */
370 cmpxchgl %ecx,(%rdi) /* new = %ecx */
373 * The old value is in %eax. If the store succeeded it will be the
374 * value we expected (old) from before the store, otherwise it will
375 * be the current value. Save %eax into %esi to prepare the return
380 movq %rax,PCB_ONFAULT(%r8)
383 * Access the oldp after the pcb_onfault is cleared, to correctly
384 * catch corrupted pointer.
386 movl %esi,(%rdx) /* oldp = %rdx */
392 * casueword. Compare and set user long. Returns -1 on fault,
393 * 0 if access was successful. Old value is written to *oldp.
394 * dst = %rdi, old = %rsi, oldp = %rdx, new = %rcx
398 movq PCPU(CURPCB),%r8
399 movq $fusufault,PCB_ONFAULT(%r8)
401 movq $VM_MAXUSER_ADDRESS-4,%rax
402 cmpq %rax,%rdi /* verify address is valid */
405 movq %rsi,%rax /* old */
409 cmpxchgq %rcx,(%rdi) /* new = %rcx */
412 * The old value is in %rax. If the store succeeded it will be the
413 * value we expected (old) from before the store, otherwise it will
414 * be the current value.
418 movq %rax,PCB_ONFAULT(%r8)
425 * Fetch (load) a 64-bit word, a 32-bit word, a 16-bit word, or an 8-bit
426 * byte from user memory.
427 * addr = %rdi, valp = %rsi
433 movq PCPU(CURPCB),%rcx
434 movq $fusufault,PCB_ONFAULT(%rcx)
436 movq $VM_MAXUSER_ADDRESS-8,%rax
437 cmpq %rax,%rdi /* verify address is valid */
442 movq %rax,PCB_ONFAULT(%rcx)
451 movq PCPU(CURPCB),%rcx
452 movq $fusufault,PCB_ONFAULT(%rcx)
454 movq $VM_MAXUSER_ADDRESS-4,%rax
455 cmpq %rax,%rdi /* verify address is valid */
460 movq %rax,PCB_ONFAULT(%rcx)
467 * fuswintr() and suswintr() are specialized variants of fuword16() and
468 * suword16(), respectively. They are called from the profiling code,
469 * potentially at interrupt time. If they fail, that's okay; good things
470 * will happen later. They always fail for now, until the trap code is
471 * able to deal with this.
482 movq PCPU(CURPCB),%rcx
483 movq $fusufault,PCB_ONFAULT(%rcx)
485 movq $VM_MAXUSER_ADDRESS-2,%rax
490 movq $0,PCB_ONFAULT(%rcx)
497 movq PCPU(CURPCB),%rcx
498 movq $fusufault,PCB_ONFAULT(%rcx)
500 movq $VM_MAXUSER_ADDRESS-1,%rax
505 movq $0,PCB_ONFAULT(%rcx)
512 movq PCPU(CURPCB),%rcx
514 movq %rax,PCB_ONFAULT(%rcx)
520 * Store a 64-bit word, a 32-bit word, a 16-bit word, or an 8-bit byte to
521 * user memory. All these functions are MPSAFE.
522 * addr = %rdi, value = %rsi
527 movq PCPU(CURPCB),%rcx
528 movq $fusufault,PCB_ONFAULT(%rcx)
530 movq $VM_MAXUSER_ADDRESS-8,%rax
531 cmpq %rax,%rdi /* verify address validity */
536 movq PCPU(CURPCB),%rcx
537 movq %rax,PCB_ONFAULT(%rcx)
545 movq PCPU(CURPCB),%rcx
546 movq $fusufault,PCB_ONFAULT(%rcx)
548 movq $VM_MAXUSER_ADDRESS-4,%rax
549 cmpq %rax,%rdi /* verify address validity */
554 movq PCPU(CURPCB),%rcx
555 movq %rax,PCB_ONFAULT(%rcx)
562 movq PCPU(CURPCB),%rcx
563 movq $fusufault,PCB_ONFAULT(%rcx)
565 movq $VM_MAXUSER_ADDRESS-2,%rax
566 cmpq %rax,%rdi /* verify address validity */
571 movq PCPU(CURPCB),%rcx /* restore trashed register */
572 movq %rax,PCB_ONFAULT(%rcx)
579 movq PCPU(CURPCB),%rcx
580 movq $fusufault,PCB_ONFAULT(%rcx)
582 movq $VM_MAXUSER_ADDRESS-1,%rax
583 cmpq %rax,%rdi /* verify address validity */
589 movq PCPU(CURPCB),%rcx /* restore trashed register */
590 movq %rax,PCB_ONFAULT(%rcx)
596 * copyinstr(from, to, maxlen, int *lencopied) - MP SAFE
597 * %rdi, %rsi, %rdx, %rcx
599 * copy a string from from to to, stop when a 0 character is reached.
600 * return ENAMETOOLONG if string is longer than maxlen, and
601 * EFAULT on protection violations. If lencopied is non-zero,
602 * return the actual length in *lencopied.
606 movq %rdx,%r8 /* %r8 = maxlen */
607 movq %rcx,%r9 /* %r9 = *len */
608 xchgq %rdi,%rsi /* %rdi = from, %rsi = to */
609 movq PCPU(CURPCB),%rcx
610 movq $cpystrflt,PCB_ONFAULT(%rcx)
612 movq $VM_MAXUSER_ADDRESS,%rax
614 /* make sure 'from' is within bounds */
618 /* restrict maxlen to <= VM_MAXUSER_ADDRESS-from */
636 /* Success -- 0 byte reached */
641 /* rdx is zero - return ENAMETOOLONG or EFAULT */
642 movq $VM_MAXUSER_ADDRESS,%rax
646 movq $ENAMETOOLONG,%rax
653 /* set *lencopied and return %eax */
654 movq PCPU(CURPCB),%rcx
655 movq $0,PCB_ONFAULT(%rcx)
667 * copystr(from, to, maxlen, int *lencopied) - MP SAFE
668 * %rdi, %rsi, %rdx, %rcx
672 movq %rdx,%r8 /* %r8 = maxlen */
685 /* Success -- 0 byte reached */
690 /* rdx is zero -- return ENAMETOOLONG */
691 movq $ENAMETOOLONG,%rax
697 /* set *lencopied and return %rax */
706 * Handling of special amd64 registers and descriptor tables etc
709 /* void lgdt(struct region_descriptor *rdp); */
711 /* reload the descriptor table */
714 /* flush the prefetch q */
721 movl %eax,%fs /* Beware, use wrmsr to set 64 bit base */
725 /* reload code selector by turning return into intersegmental return */
733 /*****************************************************************************/
734 /* setjump, longjump */
735 /*****************************************************************************/
738 movq %rbx,0(%rdi) /* save rbx */
739 movq %rsp,8(%rdi) /* save rsp */
740 movq %rbp,16(%rdi) /* save rbp */
741 movq %r12,24(%rdi) /* save r12 */
742 movq %r13,32(%rdi) /* save r13 */
743 movq %r14,40(%rdi) /* save r14 */
744 movq %r15,48(%rdi) /* save r15 */
745 movq 0(%rsp),%rdx /* get rta */
746 movq %rdx,56(%rdi) /* save rip */
747 xorl %eax,%eax /* return(0); */
752 movq 0(%rdi),%rbx /* restore rbx */
753 movq 8(%rdi),%rsp /* restore rsp */
754 movq 16(%rdi),%rbp /* restore rbp */
755 movq 24(%rdi),%r12 /* restore r12 */
756 movq 32(%rdi),%r13 /* restore r13 */
757 movq 40(%rdi),%r14 /* restore r14 */
758 movq 48(%rdi),%r15 /* restore r15 */
759 movq 56(%rdi),%rdx /* get rta */
760 movq %rdx,0(%rsp) /* put in return frame */
761 xorl %eax,%eax /* return(1); */
767 * Support for reading MSRs in the safe manner.
770 /* int rdmsr_safe(u_int msr, uint64_t *data) */
772 movq PCPU(CURPCB),%r8
773 movq $msr_onfault,PCB_ONFAULT(%r8)
775 rdmsr /* Read MSR pointed by %ecx. Returns
776 hi byte in edx, lo in %eax */
777 salq $32,%rdx /* sign-shift %rdx left */
778 movl %eax,%eax /* zero-extend %eax -> %rax */
782 movq %rax,PCB_ONFAULT(%r8)
787 * Support for writing MSRs in the safe manner.
790 /* int wrmsr_safe(u_int msr, uint64_t data) */
792 movq PCPU(CURPCB),%r8
793 movq $msr_onfault,PCB_ONFAULT(%r8)
798 wrmsr /* Write MSR pointed by %ecx. Accepts
799 hi byte in edx, lo in %eax. */
801 movq %rax,PCB_ONFAULT(%r8)
806 * MSR operations fault handler
810 movq $0,PCB_ONFAULT(%r8)