2 * Copyright (c) 1993 The Regents of the University of California.
3 * Copyright (c) 2003 Peter Wemm.
4 * Copyright (c) 2008 The DragonFly Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 4. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * $FreeBSD: src/sys/amd64/amd64/support.S,v 1.127 2007/05/23 08:33:04 kib Exp $
36 #include <machine/asmacros.h>
37 #include <machine/pmap.h>
47 * void bzero(void *buf, u_int len)
70 movnti %rax,(%rdi,%rdx)
71 movnti %rax,8(%rdi,%rdx)
72 movnti %rax,16(%rdi,%rdx)
73 movnti %rax,24(%rdi,%rdx)
82 cld /* compare forwards */
97 * bcopy(src, dst, cnt)
99 * ws@tools.de (Wolfgang Solfrank, TooLs GmbH) +49-228-985800
101 ENTRY(generic_bcopy) /* generic_bcopy is bcopy without FPU */
102 ENTRY(ovbcopy) /* our bcopy doesn't use the FPU, so ovbcopy is the same */
109 cmpq %rcx,%rax /* overlapping && src < dst? */
112 shrq $3,%rcx /* copy by 64-bit words */
113 cld /* nope, copy forwards */
117 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 */
141 movq $0x200,%rax /* the manual says that bit 10 must be set to 1 */
142 movq %rax,%dr7 /* disable all breapoints first */
152 * Note: memcpy does not support overlapping copies
156 shrq $3,%rcx /* copy by 64-bit words */
157 cld /* copy forwards */
161 andq $7,%rcx /* any bytes left? */
167 * pagecopy(%rdi=from, %rsi=to)
170 movq $-PAGE_SIZE,%rax
175 prefetchnta (%rdi,%rax)
179 movq (%rdi,%rdx),%rax
180 movnti %rax,(%rsi,%rdx)
181 movq 8(%rdi,%rdx),%rax
182 movnti %rax,8(%rsi,%rdx)
183 movq 16(%rdi,%rdx),%rax
184 movnti %rax,16(%rsi,%rdx)
185 movq 24(%rdi,%rdx),%rax
186 movnti %rax,24(%rsi,%rdx)
192 /* fillw(pat, base, cnt) */
193 /* %rdi,%rsi, %rdx */
203 /*****************************************************************************/
204 /* copyout and fubyte family */
205 /*****************************************************************************/
207 * Access user memory from inside the kernel. These routines should be
208 * the only places that do this.
210 * These routines set curpcb->onfault for the time they execute. When a
211 * protection violation occurs inside the functions, the trap handler
212 * returns to *curpcb->onfault instead of the function.
216 * copyout(from_kernel, to_user, len) - MP SAFE
220 movq PCPU(curthread),%rax
221 movq TD_PCB(%rax), %rax
222 movq $copyout_fault,PCB_ONFAULT(%rax)
223 testq %rdx,%rdx /* anything to do? */
227 * Check explicitly for non-user addresses. If 486 write protection
228 * is being used, this check is essential because we are in kernel
229 * mode so the h/w does not provide any protection against writing
234 * First, prevent address wrapping.
240 * XXX STOP USING VM_MAX_USER_ADDRESS.
241 * It is an end address, not a max, so every time it is used correctly it
242 * looks like there is an off by one error, and of course it caused an off
243 * by one error in several places.
245 movq $VM_MAX_USER_ADDRESS,%rcx
250 /* bcopy(%rsi, %rdi, %rdx) */
264 movq PCPU(curthread),%rdx
265 movq TD_PCB(%rdx), %rdx
266 movq %rax,PCB_ONFAULT(%rdx)
271 movq PCPU(curthread),%rdx
272 movq TD_PCB(%rdx), %rdx
273 movq $0,PCB_ONFAULT(%rdx)
278 * copyin(from_user, to_kernel, len) - MP SAFE
282 movq PCPU(curthread),%rax
283 movq TD_PCB(%rax), %rax
284 movq $copyin_fault,PCB_ONFAULT(%rax)
285 testq %rdx,%rdx /* anything to do? */
289 * make sure address is valid
294 movq $VM_MAX_USER_ADDRESS,%rcx
301 shrq $3,%rcx /* copy longword-wise */
306 andb $7,%cl /* copy remaining bytes */
312 movq PCPU(curthread),%rdx
313 movq TD_PCB(%rdx), %rdx
314 movq %rax,PCB_ONFAULT(%rdx)
319 movq PCPU(curthread),%rdx
320 movq TD_PCB(%rdx), %rdx
321 movq $0,PCB_ONFAULT(%rdx)
326 * casuword32. Compare and set user integer. Returns -1 or the current value.
327 * dst = %rdi, old = %rsi, new = %rdx
330 movq PCPU(curthread),%rcx
331 movq TD_PCB(%rcx), %rcx
332 movq $fusufault,PCB_ONFAULT(%rcx)
334 movq $VM_MAX_USER_ADDRESS-4,%rax
335 cmpq %rax,%rdi /* verify address is valid */
338 movl %esi,%eax /* old */
342 cmpxchgl %edx,(%rdi) /* new = %edx */
345 * The old value is in %eax. If the store succeeded it will be the
346 * value we expected (old) from before the store, otherwise it will
347 * be the current value.
350 movq PCPU(curthread),%rcx
351 movq TD_PCB(%rcx), %rcx
352 movq $0,PCB_ONFAULT(%rcx)
356 * casuword. Compare and set user word. Returns -1 or the current value.
357 * dst = %rdi, old = %rsi, new = %rdx
360 movq PCPU(curthread),%rcx
361 movq TD_PCB(%rcx), %rcx
362 movq $fusufault,PCB_ONFAULT(%rcx)
364 movq $VM_MAX_USER_ADDRESS-4,%rax
365 cmpq %rax,%rdi /* verify address is valid */
368 movq %rsi,%rax /* old */
372 cmpxchgq %rdx,(%rdi) /* new = %rdx */
375 * The old value is in %eax. If the store succeeded it will be the
376 * value we expected (old) from before the store, otherwise it will
377 * be the current value.
380 movq PCPU(curthread),%rcx
381 movq TD_PCB(%rcx), %rcx
382 movq $fusufault,PCB_ONFAULT(%rcx)
383 movq $0,PCB_ONFAULT(%rcx)
387 * Fetch (load) a 64-bit word, a 32-bit word, a 16-bit word, or an 8-bit
388 * byte from user memory. All these functions are MPSAFE.
394 movq PCPU(curthread),%rcx
395 movq TD_PCB(%rcx), %rcx
396 movq $fusufault,PCB_ONFAULT(%rcx)
398 movq $VM_MAX_USER_ADDRESS-8,%rax
399 cmpq %rax,%rdi /* verify address is valid */
403 movq $0,PCB_ONFAULT(%rcx)
407 movq PCPU(curthread),%rcx
408 movq TD_PCB(%rcx), %rcx
409 movq $fusufault,PCB_ONFAULT(%rcx)
411 movq $VM_MAX_USER_ADDRESS-4,%rax
412 cmpq %rax,%rdi /* verify address is valid */
416 movq $0,PCB_ONFAULT(%rcx)
420 * fuswintr() and suswintr() are specialized variants of fuword16() and
421 * suword16(), respectively. They are called from the profiling code,
422 * potentially at interrupt time. If they fail, that's okay; good things
423 * will happen later. They always fail for now, until the trap code is
424 * able to deal with this.
432 movq PCPU(curthread),%rcx
433 movq TD_PCB(%rcx), %rcx
434 movq $fusufault,PCB_ONFAULT(%rcx)
436 movq $VM_MAX_USER_ADDRESS-2,%rax
441 movq $0,PCB_ONFAULT(%rcx)
445 movq PCPU(curthread),%rcx
446 movq TD_PCB(%rcx), %rcx
447 movq $fusufault,PCB_ONFAULT(%rcx)
449 movq $VM_MAX_USER_ADDRESS-1,%rax
454 movq $0,PCB_ONFAULT(%rcx)
459 movq PCPU(curthread),%rcx
461 movq TD_PCB(%rcx), %rcx
462 movq %rax,PCB_ONFAULT(%rcx)
467 * Store a 64-bit word, a 32-bit word, a 16-bit word, or an 8-bit byte to
468 * user memory. All these functions are MPSAFE.
469 * addr = %rdi, value = %rsi
473 movq PCPU(curthread),%rcx
474 movq TD_PCB(%rcx), %rcx
475 movq $fusufault,PCB_ONFAULT(%rcx)
477 movq $VM_MAX_USER_ADDRESS-8,%rax
478 cmpq %rax,%rdi /* verify address validity */
483 movq PCPU(curthread),%rcx
484 movq TD_PCB(%rcx), %rcx
485 movq %rax,PCB_ONFAULT(%rcx)
489 movq PCPU(curthread),%rcx
490 movq TD_PCB(%rcx), %rcx
491 movq $fusufault,PCB_ONFAULT(%rcx)
493 movq $VM_MAX_USER_ADDRESS-4,%rax
494 cmpq %rax,%rdi /* verify address validity */
499 movq PCPU(curthread),%rcx
500 movq TD_PCB(%rcx), %rcx
501 movq %rax,PCB_ONFAULT(%rcx)
505 movq PCPU(curthread),%rcx
506 movq TD_PCB(%rcx), %rcx
507 movq $fusufault,PCB_ONFAULT(%rcx)
509 movq $VM_MAX_USER_ADDRESS-2,%rax
510 cmpq %rax,%rdi /* verify address validity */
515 movq PCPU(curthread),%rcx /* restore trashed register */
516 movq TD_PCB(%rcx), %rcx
517 movq %rax,PCB_ONFAULT(%rcx)
521 movq PCPU(curthread),%rcx
522 movq TD_PCB(%rcx), %rcx
523 movq $fusufault,PCB_ONFAULT(%rcx)
525 movq $VM_MAX_USER_ADDRESS-1,%rax
526 cmpq %rax,%rdi /* verify address validity */
532 movq PCPU(curthread),%rcx /* restore trashed register */
533 movq TD_PCB(%rcx), %rcx
534 movq %rax,PCB_ONFAULT(%rcx)
538 * copyinstr(from, to, maxlen, int *lencopied) - MP SAFE
539 * %rdi, %rsi, %rdx, %rcx
541 * copy a string from from to to, stop when a 0 character is reached.
542 * return ENAMETOOLONG if string is longer than maxlen, and
543 * EFAULT on protection violations. If lencopied is non-zero,
544 * return the actual length in *lencopied.
547 movq %rdx,%r8 /* %r8 = maxlen */
548 movq %rcx,%r9 /* %r9 = *len */
549 xchgq %rdi,%rsi /* %rdi = from, %rsi = to */
550 movq PCPU(curthread),%rcx
551 movq TD_PCB(%rcx), %rcx
552 movq $cpystrflt,PCB_ONFAULT(%rcx)
554 movq $VM_MAX_USER_ADDRESS,%rax
556 /* make sure 'from' is within bounds */
560 /* restrict maxlen to <= VM_MAX_USER_ADDRESS-from */
578 /* Success -- 0 byte reached */
583 /* rdx is zero - return ENAMETOOLONG or EFAULT */
584 movq $VM_MAX_USER_ADDRESS,%rax
588 movq $ENAMETOOLONG,%rax
595 /* set *lencopied and return %eax */
596 movq PCPU(curthread),%rcx
597 movq TD_PCB(%rcx), %rcx
598 movq $0,PCB_ONFAULT(%rcx)
609 * copystr(from, to, maxlen, int *lencopied) - MP SAFE
610 * %rdi, %rsi, %rdx, %rcx
613 movq %rdx,%r8 /* %r8 = maxlen */
626 /* Success -- 0 byte reached */
631 /* rdx is zero -- return ENAMETOOLONG */
632 movq $ENAMETOOLONG,%rax
638 /* set *lencopied and return %rax */
645 * Handling of special x86_64 registers and descriptor tables etc
648 /* void lgdt(struct region_descriptor *rdp); */
650 /* reload the descriptor table */
653 /* flush the prefetch q */
660 movl %eax,%fs /* Beware, use wrmsr to set 64 bit base */
661 movl %eax,%gs /* Beware, use wrmsr to set 64 bit base */
664 /* reload code selector by turning return into intersegmental return */
671 /*****************************************************************************/
672 /* setjump, longjump */
673 /*****************************************************************************/
676 movq %rbx,0(%rdi) /* save rbx */
677 movq %rsp,8(%rdi) /* save rsp */
678 movq %rbp,16(%rdi) /* save rbp */
679 movq %r12,24(%rdi) /* save r12 */
680 movq %r13,32(%rdi) /* save r13 */
681 movq %r14,40(%rdi) /* save r14 */
682 movq %r15,48(%rdi) /* save r15 */
683 movq 0(%rsp),%rdx /* get rta */
684 movq %rdx,56(%rdi) /* save rip */
685 xorl %eax,%eax /* return(0); */
689 movq 0(%rdi),%rbx /* restore rbx */
690 movq 8(%rdi),%rsp /* restore rsp */
691 movq 16(%rdi),%rbp /* restore rbp */
692 movq 24(%rdi),%r12 /* restore r12 */
693 movq 32(%rdi),%r13 /* restore r13 */
694 movq 40(%rdi),%r14 /* restore r14 */
695 movq 48(%rdi),%r15 /* restore r15 */
696 movq 56(%rdi),%rdx /* get rta */
697 movq %rdx,0(%rsp) /* put in return frame */
698 xorl %eax,%eax /* return(1); */
703 * Support for reading MSRs in the safe manner.
706 /* int rdmsr_safe(u_int msr, uint64_t *data) */
707 movq PCPU(curthread),%r8
708 movq TD_PCB(%r8), %r8
709 movq $msr_onfault,PCB_ONFAULT(%r8)
711 rdmsr /* Read MSR pointed by %ecx. Returns
712 hi byte in edx, lo in %eax */
713 salq $32,%rdx /* sign-shift %rdx left */
714 movl %eax,%eax /* zero-extend %eax -> %rax */
718 movq %rax,PCB_ONFAULT(%r8)
722 * MSR operations fault handler
726 movq PCPU(curthread),%r8
727 movq TD_PCB(%r8), %r8
728 movq $0,PCB_ONFAULT(%r8)
733 * Support for BB-profiling (gcc -a). The kernbb program will extract
734 * the data from the kernel.
744 NON_GPROF_ENTRY(__bb_init_func)