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)
66 movq $PAGE_SIZE/8,%rcx
82 * The loop takes 29 bytes. Ensure that it doesn't cross a 32-byte
87 movnti %rax,(%rdi,%rdx)
88 movnti %rax,8(%rdi,%rdx)
89 movnti %rax,16(%rdi,%rdx)
90 movnti %rax,24(%rdi,%rdx)
118 * bcopy(src, dst, cnt)
120 * ws@tools.de (Wolfgang Solfrank, TooLs GmbH) +49-228-985800
129 cmpq %rcx,%rax /* overlapping && src < dst? */
132 shrq $3,%rcx /* copy by 64-bit words */
136 andq $7,%rcx /* any bytes left? */
144 addq %rcx,%rdi /* copy backwards */
148 andq $7,%rcx /* any fractional bytes? */
152 movq %rdx,%rcx /* copy remainder by 32-bit words */
164 * Note: memcpy does not support overlapping copies
170 shrq $3,%rcx /* copy by 64-bit words */
174 andq $7,%rcx /* any bytes left? */
182 * pagecopy(%rdi=from, %rsi=to)
186 movq $-PAGE_SIZE,%rax
191 prefetchnta (%rdi,%rax)
195 movq (%rdi,%rdx),%rax
196 movnti %rax,(%rsi,%rdx)
197 movq 8(%rdi,%rdx),%rax
198 movnti %rax,8(%rsi,%rdx)
199 movq 16(%rdi,%rdx),%rax
200 movnti %rax,16(%rsi,%rdx)
201 movq 24(%rdi,%rdx),%rax
202 movnti %rax,24(%rsi,%rdx)
210 /* fillw(pat, base, cnt) */
211 /* %rdi,%rsi, %rdx */
223 /*****************************************************************************/
224 /* copyout and fubyte family */
225 /*****************************************************************************/
227 * Access user memory from inside the kernel. These routines should be
228 * the only places that do this.
230 * These routines set curpcb->pcb_onfault for the time they execute. When a
231 * protection violation occurs inside the functions, the trap handler
232 * returns to *curpcb->pcb_onfault instead of the function.
236 * copyout(from_kernel, to_user, len)
241 movq PCPU(CURPCB),%rax
242 movq $copyout_fault,PCB_ONFAULT(%rax)
243 testq %rdx,%rdx /* anything to do? */
247 * Check explicitly for non-user addresses. This check is essential
248 * because it prevents usermode from writing into the kernel. We do
249 * not verify anywhere else that the user did not specify a rogue
253 * First, prevent address wrapping.
259 * XXX STOP USING VM_MAXUSER_ADDRESS.
260 * It is an end address, not a max, so every time it is used correctly it
261 * looks like there is an off by one error, and of course it caused an off
262 * by one error in several places.
264 movq $VM_MAXUSER_ADDRESS,%rcx
269 /* bcopy(%rsi, %rdi, %rdx) */
282 movq PCPU(CURPCB),%rdx
283 movq %rax,PCB_ONFAULT(%rdx)
289 movq PCPU(CURPCB),%rdx
290 movq $0,PCB_ONFAULT(%rdx)
297 * copyin(from_user, to_kernel, len)
302 movq PCPU(CURPCB),%rax
303 movq $copyin_fault,PCB_ONFAULT(%rax)
304 testq %rdx,%rdx /* anything to do? */
308 * make sure address is valid
313 movq $VM_MAXUSER_ADDRESS,%rcx
320 shrq $3,%rcx /* copy longword-wise */
324 andb $7,%cl /* copy remaining bytes */
330 movq PCPU(CURPCB),%rdx
331 movq %rax,PCB_ONFAULT(%rdx)
337 movq PCPU(CURPCB),%rdx
338 movq $0,PCB_ONFAULT(%rdx)
345 * casueword32. Compare and set user integer. Returns -1 on fault,
346 * 0 if access was successful. Old value is written to *oldp.
347 * dst = %rdi, old = %esi, oldp = %rdx, new = %ecx
351 movq PCPU(CURPCB),%r8
352 movq $fusufault,PCB_ONFAULT(%r8)
354 movq $VM_MAXUSER_ADDRESS-4,%rax
355 cmpq %rax,%rdi /* verify address is valid */
358 movl %esi,%eax /* old */
362 cmpxchgl %ecx,(%rdi) /* new = %ecx */
365 * The old value is in %eax. If the store succeeded it will be the
366 * value we expected (old) from before the store, otherwise it will
367 * be the current value. Save %eax into %esi to prepare the return
372 movq %rax,PCB_ONFAULT(%r8)
375 * Access the oldp after the pcb_onfault is cleared, to correctly
376 * catch corrupted pointer.
378 movl %esi,(%rdx) /* oldp = %rdx */
384 * casueword. Compare and set user long. Returns -1 on fault,
385 * 0 if access was successful. Old value is written to *oldp.
386 * dst = %rdi, old = %rsi, oldp = %rdx, new = %rcx
390 movq PCPU(CURPCB),%r8
391 movq $fusufault,PCB_ONFAULT(%r8)
393 movq $VM_MAXUSER_ADDRESS-4,%rax
394 cmpq %rax,%rdi /* verify address is valid */
397 movq %rsi,%rax /* old */
401 cmpxchgq %rcx,(%rdi) /* new = %rcx */
404 * The old value is in %rax. If the store succeeded it will be the
405 * value we expected (old) from before the store, otherwise it will
406 * be the current value.
410 movq %rax,PCB_ONFAULT(%r8)
417 * Fetch (load) a 64-bit word, a 32-bit word, a 16-bit word, or an 8-bit
418 * byte from user memory.
419 * addr = %rdi, valp = %rsi
425 movq PCPU(CURPCB),%rcx
426 movq $fusufault,PCB_ONFAULT(%rcx)
428 movq $VM_MAXUSER_ADDRESS-8,%rax
429 cmpq %rax,%rdi /* verify address is valid */
434 movq %rax,PCB_ONFAULT(%rcx)
443 movq PCPU(CURPCB),%rcx
444 movq $fusufault,PCB_ONFAULT(%rcx)
446 movq $VM_MAXUSER_ADDRESS-4,%rax
447 cmpq %rax,%rdi /* verify address is valid */
452 movq %rax,PCB_ONFAULT(%rcx)
459 * fuswintr() and suswintr() are specialized variants of fuword16() and
460 * suword16(), respectively. They are called from the profiling code,
461 * potentially at interrupt time. If they fail, that's okay; good things
462 * will happen later. They always fail for now, until the trap code is
463 * able to deal with this.
474 movq PCPU(CURPCB),%rcx
475 movq $fusufault,PCB_ONFAULT(%rcx)
477 movq $VM_MAXUSER_ADDRESS-2,%rax
482 movq $0,PCB_ONFAULT(%rcx)
489 movq PCPU(CURPCB),%rcx
490 movq $fusufault,PCB_ONFAULT(%rcx)
492 movq $VM_MAXUSER_ADDRESS-1,%rax
497 movq $0,PCB_ONFAULT(%rcx)
504 movq PCPU(CURPCB),%rcx
506 movq %rax,PCB_ONFAULT(%rcx)
512 * Store a 64-bit word, a 32-bit word, a 16-bit word, or an 8-bit byte to
514 * addr = %rdi, value = %rsi
519 movq PCPU(CURPCB),%rcx
520 movq $fusufault,PCB_ONFAULT(%rcx)
522 movq $VM_MAXUSER_ADDRESS-8,%rax
523 cmpq %rax,%rdi /* verify address validity */
528 movq PCPU(CURPCB),%rcx
529 movq %rax,PCB_ONFAULT(%rcx)
537 movq PCPU(CURPCB),%rcx
538 movq $fusufault,PCB_ONFAULT(%rcx)
540 movq $VM_MAXUSER_ADDRESS-4,%rax
541 cmpq %rax,%rdi /* verify address validity */
546 movq PCPU(CURPCB),%rcx
547 movq %rax,PCB_ONFAULT(%rcx)
554 movq PCPU(CURPCB),%rcx
555 movq $fusufault,PCB_ONFAULT(%rcx)
557 movq $VM_MAXUSER_ADDRESS-2,%rax
558 cmpq %rax,%rdi /* verify address validity */
563 movq PCPU(CURPCB),%rcx /* restore trashed register */
564 movq %rax,PCB_ONFAULT(%rcx)
571 movq PCPU(CURPCB),%rcx
572 movq $fusufault,PCB_ONFAULT(%rcx)
574 movq $VM_MAXUSER_ADDRESS-1,%rax
575 cmpq %rax,%rdi /* verify address validity */
581 movq PCPU(CURPCB),%rcx /* restore trashed register */
582 movq %rax,PCB_ONFAULT(%rcx)
588 * copyinstr(from, to, maxlen, int *lencopied)
589 * %rdi, %rsi, %rdx, %rcx
591 * copy a string from 'from' to 'to', stop when a 0 character is reached.
592 * return ENAMETOOLONG if string is longer than maxlen, and
593 * EFAULT on protection violations. If lencopied is non-zero,
594 * return the actual length in *lencopied.
598 movq %rdx,%r8 /* %r8 = maxlen */
599 movq %rcx,%r9 /* %r9 = *len */
600 xchgq %rdi,%rsi /* %rdi = from, %rsi = to */
601 movq PCPU(CURPCB),%rcx
602 movq $cpystrflt,PCB_ONFAULT(%rcx)
604 movq $VM_MAXUSER_ADDRESS,%rax
606 /* make sure 'from' is within bounds */
610 /* restrict maxlen to <= VM_MAXUSER_ADDRESS-from */
627 /* Success -- 0 byte reached */
632 /* rdx is zero - return ENAMETOOLONG or EFAULT */
633 movq $VM_MAXUSER_ADDRESS,%rax
637 movq $ENAMETOOLONG,%rax
644 /* set *lencopied and return %eax */
645 movq PCPU(CURPCB),%rcx
646 movq $0,PCB_ONFAULT(%rcx)
658 * copystr(from, to, maxlen, int *lencopied)
659 * %rdi, %rsi, %rdx, %rcx
663 movq %rdx,%r8 /* %r8 = maxlen */
675 /* Success -- 0 byte reached */
680 /* rdx is zero -- return ENAMETOOLONG */
681 movq $ENAMETOOLONG,%rax
687 /* set *lencopied and return %rax */
696 * Handling of special amd64 registers and descriptor tables etc
698 /* void lgdt(struct region_descriptor *rdp); */
700 /* reload the descriptor table */
703 /* flush the prefetch q */
710 movl %eax,%fs /* Beware, use wrmsr to set 64 bit base */
714 /* reload code selector by turning return into intersegmental return */
722 /*****************************************************************************/
723 /* setjump, longjump */
724 /*****************************************************************************/
727 movq %rbx,0(%rdi) /* save rbx */
728 movq %rsp,8(%rdi) /* save rsp */
729 movq %rbp,16(%rdi) /* save rbp */
730 movq %r12,24(%rdi) /* save r12 */
731 movq %r13,32(%rdi) /* save r13 */
732 movq %r14,40(%rdi) /* save r14 */
733 movq %r15,48(%rdi) /* save r15 */
734 movq 0(%rsp),%rdx /* get rta */
735 movq %rdx,56(%rdi) /* save rip */
736 xorl %eax,%eax /* return(0); */
741 movq 0(%rdi),%rbx /* restore rbx */
742 movq 8(%rdi),%rsp /* restore rsp */
743 movq 16(%rdi),%rbp /* restore rbp */
744 movq 24(%rdi),%r12 /* restore r12 */
745 movq 32(%rdi),%r13 /* restore r13 */
746 movq 40(%rdi),%r14 /* restore r14 */
747 movq 48(%rdi),%r15 /* restore r15 */
748 movq 56(%rdi),%rdx /* get rta */
749 movq %rdx,0(%rsp) /* put in return frame */
750 xorl %eax,%eax /* return(1); */
756 * Support for reading MSRs in the safe manner. (Instead of panic on #gp,
760 /* int rdmsr_safe(u_int msr, uint64_t *data) */
762 movq PCPU(CURPCB),%r8
763 movq $msr_onfault,PCB_ONFAULT(%r8)
765 rdmsr /* Read MSR pointed by %ecx. Returns
766 hi byte in edx, lo in %eax */
767 salq $32,%rdx /* sign-shift %rdx left */
768 movl %eax,%eax /* zero-extend %eax -> %rax */
772 movq %rax,PCB_ONFAULT(%r8)
777 * Support for writing MSRs in the safe manner. (Instead of panic on #gp,
781 /* int wrmsr_safe(u_int msr, uint64_t data) */
783 movq PCPU(CURPCB),%r8
784 movq $msr_onfault,PCB_ONFAULT(%r8)
789 wrmsr /* Write MSR pointed by %ecx. Accepts
790 hi byte in edx, lo in %eax. */
792 movq %rax,PCB_ONFAULT(%r8)
797 * MSR operations fault handler
801 movq $0,PCB_ONFAULT(%r8)