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)
68 movq $PAGE_SIZE/8,%rcx
80 cld /* compare forwards */
97 * bcopy(src, dst, cnt)
99 * ws@tools.de (Wolfgang Solfrank, TooLs GmbH) +49-228-985800
108 cmpq %rcx,%rax /* overlapping && src < dst? */
111 shrq $3,%rcx /* copy by 64-bit words */
112 cld /* nope, copy forwards */
116 andq $7,%rcx /* any bytes left? */
124 addq %rcx,%rdi /* copy backwards */
128 andq $7,%rcx /* any fractional bytes? */
132 movq %rdx,%rcx /* copy remainder by 32-bit words */
144 * Note: memcpy does not support overlapping copies
150 shrq $3,%rcx /* copy by 64-bit words */
151 cld /* copy forwards */
155 andq $7,%rcx /* any bytes left? */
163 * pagecopy(%rdi=from, %rsi=to)
167 movq $-PAGE_SIZE,%rax
172 prefetchnta (%rdi,%rax)
176 movq (%rdi,%rdx),%rax
177 movnti %rax,(%rsi,%rdx)
178 movq 8(%rdi,%rdx),%rax
179 movnti %rax,8(%rsi,%rdx)
180 movq 16(%rdi,%rdx),%rax
181 movnti %rax,16(%rsi,%rdx)
182 movq 24(%rdi,%rdx),%rax
183 movnti %rax,24(%rsi,%rdx)
191 /* fillw(pat, base, cnt) */
192 /* %rdi,%rsi, %rdx */
205 /*****************************************************************************/
206 /* copyout and fubyte family */
207 /*****************************************************************************/
209 * Access user memory from inside the kernel. These routines should be
210 * the only places that do this.
212 * These routines set curpcb->pcb_onfault for the time they execute. When a
213 * protection violation occurs inside the functions, the trap handler
214 * returns to *curpcb->pcb_onfault instead of the function.
218 * copyout(from_kernel, to_user, len) - MP SAFE
223 movq PCPU(CURPCB),%rax
224 movq $copyout_fault,PCB_ONFAULT(%rax)
225 testq %rdx,%rdx /* anything to do? */
229 * Check explicitly for non-user addresses. If 486 write protection
230 * is being used, this check is essential because we are in kernel
231 * mode so the h/w does not provide any protection against writing
236 * First, prevent address wrapping.
242 * XXX STOP USING VM_MAXUSER_ADDRESS.
243 * It is an end address, not a max, so every time it is used correctly it
244 * looks like there is an off by one error, and of course it caused an off
245 * by one error in several places.
247 movq $VM_MAXUSER_ADDRESS,%rcx
252 /* bcopy(%rsi, %rdi, %rdx) */
266 movq PCPU(CURPCB),%rdx
267 movq %rax,PCB_ONFAULT(%rdx)
273 movq PCPU(CURPCB),%rdx
274 movq $0,PCB_ONFAULT(%rdx)
281 * copyin(from_user, to_kernel, len) - MP SAFE
286 movq PCPU(CURPCB),%rax
287 movq $copyin_fault,PCB_ONFAULT(%rax)
288 testq %rdx,%rdx /* anything to do? */
292 * make sure address is valid
297 movq $VM_MAXUSER_ADDRESS,%rcx
304 shrq $3,%rcx /* copy longword-wise */
309 andb $7,%cl /* copy remaining bytes */
315 movq PCPU(CURPCB),%rdx
316 movq %rax,PCB_ONFAULT(%rdx)
322 movq PCPU(CURPCB),%rdx
323 movq $0,PCB_ONFAULT(%rdx)
330 * casueword32. Compare and set user integer. Returns -1 on fault,
331 * 0 if access was successful. Old value is written to *oldp.
332 * dst = %rdi, old = %esi, oldp = %rdx, new = %ecx
336 movq PCPU(CURPCB),%r8
337 movq $fusufault,PCB_ONFAULT(%r8)
339 movq $VM_MAXUSER_ADDRESS-4,%rax
340 cmpq %rax,%rdi /* verify address is valid */
343 movl %esi,%eax /* old */
347 cmpxchgl %ecx,(%rdi) /* new = %ecx */
350 * The old value is in %eax. If the store succeeded it will be the
351 * value we expected (old) from before the store, otherwise it will
352 * be the current value. Save %eax into %esi to prepare the return
357 movq %rax,PCB_ONFAULT(%r8)
360 * Access the oldp after the pcb_onfault is cleared, to correctly
361 * catch corrupted pointer.
363 movl %esi,(%rdx) /* oldp = %rdx */
369 * casueword. Compare and set user long. Returns -1 on fault,
370 * 0 if access was successful. Old value is written to *oldp.
371 * dst = %rdi, old = %rsi, oldp = %rdx, new = %rcx
375 movq PCPU(CURPCB),%r8
376 movq $fusufault,PCB_ONFAULT(%r8)
378 movq $VM_MAXUSER_ADDRESS-4,%rax
379 cmpq %rax,%rdi /* verify address is valid */
382 movq %rsi,%rax /* old */
386 cmpxchgq %rcx,(%rdi) /* new = %rcx */
389 * The old value is in %rax. If the store succeeded it will be the
390 * value we expected (old) from before the store, otherwise it will
391 * be the current value.
395 movq %rax,PCB_ONFAULT(%r8)
402 * Fetch (load) a 64-bit word, a 32-bit word, a 16-bit word, or an 8-bit
403 * byte from user memory.
404 * addr = %rdi, valp = %rsi
410 movq PCPU(CURPCB),%rcx
411 movq $fusufault,PCB_ONFAULT(%rcx)
413 movq $VM_MAXUSER_ADDRESS-8,%rax
414 cmpq %rax,%rdi /* verify address is valid */
419 movq %rax,PCB_ONFAULT(%rcx)
428 movq PCPU(CURPCB),%rcx
429 movq $fusufault,PCB_ONFAULT(%rcx)
431 movq $VM_MAXUSER_ADDRESS-4,%rax
432 cmpq %rax,%rdi /* verify address is valid */
437 movq %rax,PCB_ONFAULT(%rcx)
444 * fuswintr() and suswintr() are specialized variants of fuword16() and
445 * suword16(), respectively. They are called from the profiling code,
446 * potentially at interrupt time. If they fail, that's okay; good things
447 * will happen later. They always fail for now, until the trap code is
448 * able to deal with this.
459 movq PCPU(CURPCB),%rcx
460 movq $fusufault,PCB_ONFAULT(%rcx)
462 movq $VM_MAXUSER_ADDRESS-2,%rax
467 movq $0,PCB_ONFAULT(%rcx)
474 movq PCPU(CURPCB),%rcx
475 movq $fusufault,PCB_ONFAULT(%rcx)
477 movq $VM_MAXUSER_ADDRESS-1,%rax
482 movq $0,PCB_ONFAULT(%rcx)
489 movq PCPU(CURPCB),%rcx
491 movq %rax,PCB_ONFAULT(%rcx)
497 * Store a 64-bit word, a 32-bit word, a 16-bit word, or an 8-bit byte to
498 * user memory. All these functions are MPSAFE.
499 * addr = %rdi, value = %rsi
504 movq PCPU(CURPCB),%rcx
505 movq $fusufault,PCB_ONFAULT(%rcx)
507 movq $VM_MAXUSER_ADDRESS-8,%rax
508 cmpq %rax,%rdi /* verify address validity */
513 movq PCPU(CURPCB),%rcx
514 movq %rax,PCB_ONFAULT(%rcx)
522 movq PCPU(CURPCB),%rcx
523 movq $fusufault,PCB_ONFAULT(%rcx)
525 movq $VM_MAXUSER_ADDRESS-4,%rax
526 cmpq %rax,%rdi /* verify address validity */
531 movq PCPU(CURPCB),%rcx
532 movq %rax,PCB_ONFAULT(%rcx)
539 movq PCPU(CURPCB),%rcx
540 movq $fusufault,PCB_ONFAULT(%rcx)
542 movq $VM_MAXUSER_ADDRESS-2,%rax
543 cmpq %rax,%rdi /* verify address validity */
548 movq PCPU(CURPCB),%rcx /* restore trashed register */
549 movq %rax,PCB_ONFAULT(%rcx)
556 movq PCPU(CURPCB),%rcx
557 movq $fusufault,PCB_ONFAULT(%rcx)
559 movq $VM_MAXUSER_ADDRESS-1,%rax
560 cmpq %rax,%rdi /* verify address validity */
566 movq PCPU(CURPCB),%rcx /* restore trashed register */
567 movq %rax,PCB_ONFAULT(%rcx)
573 * copyinstr(from, to, maxlen, int *lencopied) - MP SAFE
574 * %rdi, %rsi, %rdx, %rcx
576 * copy a string from from to to, stop when a 0 character is reached.
577 * return ENAMETOOLONG if string is longer than maxlen, and
578 * EFAULT on protection violations. If lencopied is non-zero,
579 * return the actual length in *lencopied.
583 movq %rdx,%r8 /* %r8 = maxlen */
584 movq %rcx,%r9 /* %r9 = *len */
585 xchgq %rdi,%rsi /* %rdi = from, %rsi = to */
586 movq PCPU(CURPCB),%rcx
587 movq $cpystrflt,PCB_ONFAULT(%rcx)
589 movq $VM_MAXUSER_ADDRESS,%rax
591 /* make sure 'from' is within bounds */
595 /* restrict maxlen to <= VM_MAXUSER_ADDRESS-from */
613 /* Success -- 0 byte reached */
618 /* rdx is zero - return ENAMETOOLONG or EFAULT */
619 movq $VM_MAXUSER_ADDRESS,%rax
623 movq $ENAMETOOLONG,%rax
630 /* set *lencopied and return %eax */
631 movq PCPU(CURPCB),%rcx
632 movq $0,PCB_ONFAULT(%rcx)
644 * copystr(from, to, maxlen, int *lencopied) - MP SAFE
645 * %rdi, %rsi, %rdx, %rcx
649 movq %rdx,%r8 /* %r8 = maxlen */
662 /* Success -- 0 byte reached */
667 /* rdx is zero -- return ENAMETOOLONG */
668 movq $ENAMETOOLONG,%rax
674 /* set *lencopied and return %rax */
683 * Handling of special amd64 registers and descriptor tables etc
686 /* void lgdt(struct region_descriptor *rdp); */
688 /* reload the descriptor table */
691 /* flush the prefetch q */
698 movl %eax,%fs /* Beware, use wrmsr to set 64 bit base */
702 /* reload code selector by turning return into intersegmental return */
710 /*****************************************************************************/
711 /* setjump, longjump */
712 /*****************************************************************************/
715 movq %rbx,0(%rdi) /* save rbx */
716 movq %rsp,8(%rdi) /* save rsp */
717 movq %rbp,16(%rdi) /* save rbp */
718 movq %r12,24(%rdi) /* save r12 */
719 movq %r13,32(%rdi) /* save r13 */
720 movq %r14,40(%rdi) /* save r14 */
721 movq %r15,48(%rdi) /* save r15 */
722 movq 0(%rsp),%rdx /* get rta */
723 movq %rdx,56(%rdi) /* save rip */
724 xorl %eax,%eax /* return(0); */
729 movq 0(%rdi),%rbx /* restore rbx */
730 movq 8(%rdi),%rsp /* restore rsp */
731 movq 16(%rdi),%rbp /* restore rbp */
732 movq 24(%rdi),%r12 /* restore r12 */
733 movq 32(%rdi),%r13 /* restore r13 */
734 movq 40(%rdi),%r14 /* restore r14 */
735 movq 48(%rdi),%r15 /* restore r15 */
736 movq 56(%rdi),%rdx /* get rta */
737 movq %rdx,0(%rsp) /* put in return frame */
738 xorl %eax,%eax /* return(1); */
744 * Support for reading MSRs in the safe manner.
747 /* int rdmsr_safe(u_int msr, uint64_t *data) */
749 movq PCPU(CURPCB),%r8
750 movq $msr_onfault,PCB_ONFAULT(%r8)
752 rdmsr /* Read MSR pointed by %ecx. Returns
753 hi byte in edx, lo in %eax */
754 salq $32,%rdx /* sign-shift %rdx left */
755 movl %eax,%eax /* zero-extend %eax -> %rax */
759 movq %rax,PCB_ONFAULT(%r8)
764 * Support for writing MSRs in the safe manner.
767 /* int wrmsr_safe(u_int msr, uint64_t data) */
769 movq PCPU(CURPCB),%r8
770 movq $msr_onfault,PCB_ONFAULT(%r8)
775 wrmsr /* Write MSR pointed by %ecx. Accepts
776 hi byte in edx, lo in %eax. */
778 movq %rax,PCB_ONFAULT(%r8)
783 * MSR operations fault handler
787 movq $0,PCB_ONFAULT(%r8)