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)
69 movnti %rax,(%rdi,%rdx)
70 movnti %rax,8(%rdi,%rdx)
71 movnti %rax,16(%rdi,%rdx)
72 movnti %rax,24(%rdi,%rdx)
82 cld /* compare forwards */
98 * bcopy(src, dst, cnt)
100 * 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? */
123 addq %rcx,%rdi /* copy backwards */
127 andq $7,%rcx /* any fractional bytes? */
131 movq %rdx,%rcx /* copy remainder by 32-bit words */
142 * Note: memcpy does not support overlapping copies
146 shrq $3,%rcx /* copy by 64-bit words */
147 cld /* copy forwards */
151 andq $7,%rcx /* any bytes left? */
158 * pagecopy(%rdi=from, %rsi=to)
161 movq $-PAGE_SIZE,%rax
166 prefetchnta (%rdi,%rax)
170 movq (%rdi,%rdx),%rax
171 movnti %rax,(%rsi,%rdx)
172 movq 8(%rdi,%rdx),%rax
173 movnti %rax,8(%rsi,%rdx)
174 movq 16(%rdi,%rdx),%rax
175 movnti %rax,16(%rsi,%rdx)
176 movq 24(%rdi,%rdx),%rax
177 movnti %rax,24(%rsi,%rdx)
184 /* fillw(pat, base, cnt) */
185 /* %rdi,%rsi, %rdx */
196 /*****************************************************************************/
197 /* copyout and fubyte family */
198 /*****************************************************************************/
200 * Access user memory from inside the kernel. These routines should be
201 * the only places that do this.
203 * These routines set curpcb->pcb_onfault for the time they execute. When a
204 * protection violation occurs inside the functions, the trap handler
205 * returns to *curpcb->pcb_onfault instead of the function.
209 * copyout(from_kernel, to_user, len) - MP SAFE
213 movq PCPU(CURPCB),%rax
214 movq $copyout_fault,PCB_ONFAULT(%rax)
215 testq %rdx,%rdx /* anything to do? */
219 * Check explicitly for non-user addresses. If 486 write protection
220 * is being used, this check is essential because we are in kernel
221 * mode so the h/w does not provide any protection against writing
226 * First, prevent address wrapping.
232 * XXX STOP USING VM_MAXUSER_ADDRESS.
233 * It is an end address, not a max, so every time it is used correctly it
234 * looks like there is an off by one error, and of course it caused an off
235 * by one error in several places.
237 movq $VM_MAXUSER_ADDRESS,%rcx
242 /* bcopy(%rsi, %rdi, %rdx) */
256 movq PCPU(CURPCB),%rdx
257 movq %rax,PCB_ONFAULT(%rdx)
262 movq PCPU(CURPCB),%rdx
263 movq $0,PCB_ONFAULT(%rdx)
269 * copyin(from_user, to_kernel, len) - MP SAFE
273 movq PCPU(CURPCB),%rax
274 movq $copyin_fault,PCB_ONFAULT(%rax)
275 testq %rdx,%rdx /* anything to do? */
279 * make sure address is valid
284 movq $VM_MAXUSER_ADDRESS,%rcx
291 shrq $3,%rcx /* copy longword-wise */
296 andb $7,%cl /* copy remaining bytes */
302 movq PCPU(CURPCB),%rdx
303 movq %rax,PCB_ONFAULT(%rdx)
308 movq PCPU(CURPCB),%rdx
309 movq $0,PCB_ONFAULT(%rdx)
315 * casuword32. Compare and set user integer. Returns -1 or the current value.
316 * dst = %rdi, old = %rsi, new = %rdx
319 movq PCPU(CURPCB),%rcx
320 movq $fusufault,PCB_ONFAULT(%rcx)
322 movq $VM_MAXUSER_ADDRESS-4,%rax
323 cmpq %rax,%rdi /* verify address is valid */
326 movl %esi,%eax /* old */
330 cmpxchgl %edx,(%rdi) /* new = %edx */
333 * The old value is in %eax. If the store succeeded it will be the
334 * value we expected (old) from before the store, otherwise it will
335 * be the current value.
338 movq PCPU(CURPCB),%rcx
339 movq $0,PCB_ONFAULT(%rcx)
344 * casuword. Compare and set user word. Returns -1 or the current value.
345 * dst = %rdi, old = %rsi, new = %rdx
348 movq PCPU(CURPCB),%rcx
349 movq $fusufault,PCB_ONFAULT(%rcx)
351 movq $VM_MAXUSER_ADDRESS-4,%rax
352 cmpq %rax,%rdi /* verify address is valid */
355 movq %rsi,%rax /* old */
359 cmpxchgq %rdx,(%rdi) /* new = %rdx */
362 * The old value is in %eax. If the store succeeded it will be the
363 * value we expected (old) from before the store, otherwise it will
364 * be the current value.
367 movq PCPU(CURPCB),%rcx
368 movq $fusufault,PCB_ONFAULT(%rcx)
369 movq $0,PCB_ONFAULT(%rcx)
374 * Fetch (load) a 64-bit word, a 32-bit word, a 16-bit word, or an 8-bit
375 * byte from user memory. All these functions are MPSAFE.
381 movq PCPU(CURPCB),%rcx
382 movq $fusufault,PCB_ONFAULT(%rcx)
384 movq $VM_MAXUSER_ADDRESS-8,%rax
385 cmpq %rax,%rdi /* verify address is valid */
389 movq $0,PCB_ONFAULT(%rcx)
395 movq PCPU(CURPCB),%rcx
396 movq $fusufault,PCB_ONFAULT(%rcx)
398 movq $VM_MAXUSER_ADDRESS-4,%rax
399 cmpq %rax,%rdi /* verify address is valid */
403 movq $0,PCB_ONFAULT(%rcx)
408 * fuswintr() and suswintr() are specialized variants of fuword16() and
409 * suword16(), respectively. They are called from the profiling code,
410 * potentially at interrupt time. If they fail, that's okay; good things
411 * will happen later. They always fail for now, until the trap code is
412 * able to deal with this.
422 movq PCPU(CURPCB),%rcx
423 movq $fusufault,PCB_ONFAULT(%rcx)
425 movq $VM_MAXUSER_ADDRESS-2,%rax
430 movq $0,PCB_ONFAULT(%rcx)
435 movq PCPU(CURPCB),%rcx
436 movq $fusufault,PCB_ONFAULT(%rcx)
438 movq $VM_MAXUSER_ADDRESS-1,%rax
443 movq $0,PCB_ONFAULT(%rcx)
449 movq PCPU(CURPCB),%rcx
451 movq %rax,PCB_ONFAULT(%rcx)
456 * Store a 64-bit word, a 32-bit word, a 16-bit word, or an 8-bit byte to
457 * user memory. All these functions are MPSAFE.
458 * addr = %rdi, value = %rsi
462 movq PCPU(CURPCB),%rcx
463 movq $fusufault,PCB_ONFAULT(%rcx)
465 movq $VM_MAXUSER_ADDRESS-8,%rax
466 cmpq %rax,%rdi /* verify address validity */
471 movq PCPU(CURPCB),%rcx
472 movq %rax,PCB_ONFAULT(%rcx)
478 movq PCPU(CURPCB),%rcx
479 movq $fusufault,PCB_ONFAULT(%rcx)
481 movq $VM_MAXUSER_ADDRESS-4,%rax
482 cmpq %rax,%rdi /* verify address validity */
487 movq PCPU(CURPCB),%rcx
488 movq %rax,PCB_ONFAULT(%rcx)
493 movq PCPU(CURPCB),%rcx
494 movq $fusufault,PCB_ONFAULT(%rcx)
496 movq $VM_MAXUSER_ADDRESS-2,%rax
497 cmpq %rax,%rdi /* verify address validity */
502 movq PCPU(CURPCB),%rcx /* restore trashed register */
503 movq %rax,PCB_ONFAULT(%rcx)
508 movq PCPU(CURPCB),%rcx
509 movq $fusufault,PCB_ONFAULT(%rcx)
511 movq $VM_MAXUSER_ADDRESS-1,%rax
512 cmpq %rax,%rdi /* verify address validity */
518 movq PCPU(CURPCB),%rcx /* restore trashed register */
519 movq %rax,PCB_ONFAULT(%rcx)
524 * copyinstr(from, to, maxlen, int *lencopied) - MP SAFE
525 * %rdi, %rsi, %rdx, %rcx
527 * copy a string from from to to, stop when a 0 character is reached.
528 * return ENAMETOOLONG if string is longer than maxlen, and
529 * EFAULT on protection violations. If lencopied is non-zero,
530 * return the actual length in *lencopied.
533 movq %rdx,%r8 /* %r8 = maxlen */
534 movq %rcx,%r9 /* %r9 = *len */
535 xchgq %rdi,%rsi /* %rdi = from, %rsi = to */
536 movq PCPU(CURPCB),%rcx
537 movq $cpystrflt,PCB_ONFAULT(%rcx)
539 movq $VM_MAXUSER_ADDRESS,%rax
541 /* make sure 'from' is within bounds */
545 /* restrict maxlen to <= VM_MAXUSER_ADDRESS-from */
563 /* Success -- 0 byte reached */
568 /* rdx is zero - return ENAMETOOLONG or EFAULT */
569 movq $VM_MAXUSER_ADDRESS,%rax
573 movq $ENAMETOOLONG,%rax
580 /* set *lencopied and return %eax */
581 movq PCPU(CURPCB),%rcx
582 movq $0,PCB_ONFAULT(%rcx)
593 * copystr(from, to, maxlen, int *lencopied) - MP SAFE
594 * %rdi, %rsi, %rdx, %rcx
597 movq %rdx,%r8 /* %r8 = maxlen */
610 /* Success -- 0 byte reached */
615 /* rdx is zero -- return ENAMETOOLONG */
616 movq $ENAMETOOLONG,%rax
622 /* set *lencopied and return %rax */
630 * Handling of special amd64 registers and descriptor tables etc
633 /* void lgdt(struct region_descriptor *rdp); */
635 /* reload the descriptor table */
638 /* flush the prefetch q */
645 movl %eax,%fs /* Beware, use wrmsr to set 64 bit base */
649 /* reload code selector by turning return into intersegmental return */
657 /*****************************************************************************/
658 /* setjump, longjump */
659 /*****************************************************************************/
662 movq %rbx,0(%rdi) /* save rbx */
663 movq %rsp,8(%rdi) /* save rsp */
664 movq %rbp,16(%rdi) /* save rbp */
665 movq %r12,24(%rdi) /* save r12 */
666 movq %r13,32(%rdi) /* save r13 */
667 movq %r14,40(%rdi) /* save r14 */
668 movq %r15,48(%rdi) /* save r15 */
669 movq 0(%rsp),%rdx /* get rta */
670 movq %rdx,56(%rdi) /* save rip */
671 xorl %eax,%eax /* return(0); */
676 movq 0(%rdi),%rbx /* restore rbx */
677 movq 8(%rdi),%rsp /* restore rsp */
678 movq 16(%rdi),%rbp /* restore rbp */
679 movq 24(%rdi),%r12 /* restore r12 */
680 movq 32(%rdi),%r13 /* restore r13 */
681 movq 40(%rdi),%r14 /* restore r14 */
682 movq 48(%rdi),%r15 /* restore r15 */
683 movq 56(%rdi),%rdx /* get rta */
684 movq %rdx,0(%rsp) /* put in return frame */
685 xorl %eax,%eax /* return(1); */
691 * Support for reading MSRs in the safe manner.
694 /* int rdmsr_safe(u_int msr, uint64_t *data) */
695 movq PCPU(CURPCB),%r8
696 movq $msr_onfault,PCB_ONFAULT(%r8)
698 rdmsr /* Read MSR pointed by %ecx. Returns
699 hi byte in edx, lo in %eax */
700 salq $32,%rdx /* sign-shift %rdx left */
701 movl %eax,%eax /* zero-extend %eax -> %rax */
705 movq %rax,PCB_ONFAULT(%r8)
709 * Support for writing MSRs in the safe manner.
712 /* int wrmsr_safe(u_int msr, uint64_t data) */
713 movq PCPU(CURPCB),%r8
714 movq $msr_onfault,PCB_ONFAULT(%r8)
719 wrmsr /* Write MSR pointed by %ecx. Accepts
720 hi byte in edx, lo in %eax. */
722 movq %rax,PCB_ONFAULT(%r8)
726 * MSR operations fault handler
730 movq $0,PCB_ONFAULT(%r8)