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 $
32 * $DragonFly: src/sys/platform/pc64/amd64/support.s,v 1.2 2008/08/29 17:07:10 dillon Exp $
37 #include <machine/asmacros.h>
38 #include <machine/intr_machdep.h>
39 #include <machine/pmap.h>
44 .globl intrcnt, eintrcnt
46 .space INTRCNT_COUNT * 8
49 .globl intrnames, eintrnames
51 .space INTRCNT_COUNT * (MAXCOMLEN + 1)
58 * void bzero(void *buf, u_int len)
81 movnti %rax,(%rdi,%rdx)
82 movnti %rax,8(%rdi,%rdx)
83 movnti %rax,16(%rdi,%rdx)
84 movnti %rax,24(%rdi,%rdx)
93 cld /* compare forwards */
108 * bcopy(src, dst, cnt)
110 * ws@tools.de (Wolfgang Solfrank, TooLs GmbH) +49-228-985800
112 ENTRY(generic_bcopy) /* generic_bcopy is bcopy without FPU */
113 ENTRY(ovbcopy) /* our bcopy doesn't use the FPU, so ovbcopy is the same */
120 cmpq %rcx,%rax /* overlapping && src < dst? */
123 shrq $3,%rcx /* copy by 64-bit words */
124 cld /* nope, copy forwards */
128 andq $7,%rcx /* any bytes left? */
135 addq %rcx,%rdi /* copy backwards */
139 andq $7,%rcx /* any fractional bytes? */
143 movq %rdx,%rcx /* copy remainder by 32-bit words */
152 movq $0x200,%rax /* the manual says that bit 10 must be set to 1 */
153 movq %rax,%dr7 /* disable all breapoints first */
163 * Note: memcpy does not support overlapping copies
167 shrq $3,%rcx /* copy by 64-bit words */
168 cld /* copy forwards */
172 andq $7,%rcx /* any bytes left? */
178 * pagecopy(%rdi=from, %rsi=to)
181 movq $-PAGE_SIZE,%rax
186 prefetchnta (%rdi,%rax)
190 movq (%rdi,%rdx),%rax
191 movnti %rax,(%rsi,%rdx)
192 movq 8(%rdi,%rdx),%rax
193 movnti %rax,8(%rsi,%rdx)
194 movq 16(%rdi,%rdx),%rax
195 movnti %rax,16(%rsi,%rdx)
196 movq 24(%rdi,%rdx),%rax
197 movnti %rax,24(%rsi,%rdx)
203 /* fillw(pat, base, cnt) */
204 /* %rdi,%rsi, %rdx */
214 /*****************************************************************************/
215 /* copyout and fubyte family */
216 /*****************************************************************************/
218 * Access user memory from inside the kernel. These routines should be
219 * the only places that do this.
221 * These routines set curpcb->onfault for the time they execute. When a
222 * protection violation occurs inside the functions, the trap handler
223 * returns to *curpcb->onfault instead of the function.
227 * copyout(from_kernel, to_user, len) - MP SAFE
231 movq PCPU(curthread),%rax
232 movq TD_PCB(%rax), %rax
233 movq $copyout_fault,PCB_ONFAULT(%rax)
234 testq %rdx,%rdx /* anything to do? */
238 * Check explicitly for non-user addresses. If 486 write protection
239 * is being used, this check is essential because we are in kernel
240 * mode so the h/w does not provide any protection against writing
245 * First, prevent address wrapping.
251 * XXX STOP USING VM_MAX_USER_ADDRESS.
252 * It is an end address, not a max, so every time it is used correctly it
253 * looks like there is an off by one error, and of course it caused an off
254 * by one error in several places.
256 movq $VM_MAX_USER_ADDRESS,%rcx
261 /* bcopy(%rsi, %rdi, %rdx) */
275 movq PCPU(curthread),%rdx
276 movq TD_PCB(%rdx), %rdx
277 movq %rax,PCB_ONFAULT(%rdx)
282 movq PCPU(curthread),%rdx
283 movq TD_PCB(%rdx), %rdx
284 movq $0,PCB_ONFAULT(%rdx)
289 * copyin(from_user, to_kernel, len) - MP SAFE
293 movq PCPU(curthread),%rax
294 movq TD_PCB(%rax), %rax
295 movq $copyin_fault,PCB_ONFAULT(%rax)
296 testq %rdx,%rdx /* anything to do? */
300 * make sure address is valid
305 movq $VM_MAX_USER_ADDRESS,%rcx
312 shrq $3,%rcx /* copy longword-wise */
317 andb $7,%cl /* copy remaining bytes */
323 movq PCPU(curthread),%rdx
324 movq TD_PCB(%rdx), %rdx
325 movq %rax,PCB_ONFAULT(%rdx)
330 movq PCPU(curthread),%rdx
331 movq TD_PCB(%rdx), %rdx
332 movq $0,PCB_ONFAULT(%rdx)
337 * casuword32. Compare and set user integer. Returns -1 or the current value.
338 * dst = %rdi, old = %rsi, new = %rdx
341 movq PCPU(curthread),%rcx
342 movq TD_PCB(%rcx), %rcx
343 movq $fusufault,PCB_ONFAULT(%rcx)
345 movq $VM_MAX_USER_ADDRESS-4,%rax
346 cmpq %rax,%rdi /* verify address is valid */
349 movl %esi,%eax /* old */
353 cmpxchgl %edx,(%rdi) /* new = %edx */
356 * The old value is in %eax. If the store succeeded it will be the
357 * value we expected (old) from before the store, otherwise it will
358 * be the current value.
361 movq PCPU(curthread),%rcx
362 movq TD_PCB(%rcx), %rcx
363 movq $0,PCB_ONFAULT(%rcx)
367 * casuword. Compare and set user word. Returns -1 or the current value.
368 * dst = %rdi, old = %rsi, new = %rdx
371 movq PCPU(curthread),%rcx
372 movq TD_PCB(%rcx), %rcx
373 movq $fusufault,PCB_ONFAULT(%rcx)
375 movq $VM_MAX_USER_ADDRESS-4,%rax
376 cmpq %rax,%rdi /* verify address is valid */
379 movq %rsi,%rax /* old */
383 cmpxchgq %rdx,(%rdi) /* new = %rdx */
386 * The old value is in %eax. If the store succeeded it will be the
387 * value we expected (old) from before the store, otherwise it will
388 * be the current value.
391 movq PCPU(curthread),%rcx
392 movq TD_PCB(%rcx), %rcx
393 movq $fusufault,PCB_ONFAULT(%rcx)
394 movq $0,PCB_ONFAULT(%rcx)
398 * Fetch (load) a 64-bit word, a 32-bit word, a 16-bit word, or an 8-bit
399 * byte from user memory. All these functions are MPSAFE.
405 movq PCPU(curthread),%rcx
406 movq TD_PCB(%rcx), %rcx
407 movq $fusufault,PCB_ONFAULT(%rcx)
409 movq $VM_MAX_USER_ADDRESS-8,%rax
410 cmpq %rax,%rdi /* verify address is valid */
414 movq $0,PCB_ONFAULT(%rcx)
418 movq PCPU(curthread),%rcx
419 movq TD_PCB(%rcx), %rcx
420 movq $fusufault,PCB_ONFAULT(%rcx)
422 movq $VM_MAX_USER_ADDRESS-4,%rax
423 cmpq %rax,%rdi /* verify address is valid */
427 movq $0,PCB_ONFAULT(%rcx)
431 * fuswintr() and suswintr() are specialized variants of fuword16() and
432 * suword16(), respectively. They are called from the profiling code,
433 * potentially at interrupt time. If they fail, that's okay; good things
434 * will happen later. They always fail for now, until the trap code is
435 * able to deal with this.
443 movq PCPU(curthread),%rcx
444 movq TD_PCB(%rcx), %rcx
445 movq $fusufault,PCB_ONFAULT(%rcx)
447 movq $VM_MAX_USER_ADDRESS-2,%rax
452 movq $0,PCB_ONFAULT(%rcx)
456 movq PCPU(curthread),%rcx
457 movq TD_PCB(%rcx), %rcx
458 movq $fusufault,PCB_ONFAULT(%rcx)
460 movq $VM_MAX_USER_ADDRESS-1,%rax
465 movq $0,PCB_ONFAULT(%rcx)
470 movq PCPU(curthread),%rcx
472 movq TD_PCB(%rcx), %rcx
473 movq %rax,PCB_ONFAULT(%rcx)
478 * Store a 64-bit word, a 32-bit word, a 16-bit word, or an 8-bit byte to
479 * user memory. All these functions are MPSAFE.
480 * addr = %rdi, value = %rsi
484 movq PCPU(curthread),%rcx
485 movq TD_PCB(%rcx), %rcx
486 movq $fusufault,PCB_ONFAULT(%rcx)
488 movq $VM_MAX_USER_ADDRESS-8,%rax
489 cmpq %rax,%rdi /* verify address validity */
494 movq PCPU(curthread),%rcx
495 movq TD_PCB(%rcx), %rcx
496 movq %rax,PCB_ONFAULT(%rcx)
500 movq PCPU(curthread),%rcx
501 movq TD_PCB(%rcx), %rcx
502 movq $fusufault,PCB_ONFAULT(%rcx)
504 movq $VM_MAX_USER_ADDRESS-4,%rax
505 cmpq %rax,%rdi /* verify address validity */
510 movq PCPU(curthread),%rcx
511 movq TD_PCB(%rcx), %rcx
512 movq %rax,PCB_ONFAULT(%rcx)
516 movq PCPU(curthread),%rcx
517 movq TD_PCB(%rcx), %rcx
518 movq $fusufault,PCB_ONFAULT(%rcx)
520 movq $VM_MAX_USER_ADDRESS-2,%rax
521 cmpq %rax,%rdi /* verify address validity */
526 movq PCPU(curthread),%rcx /* restore trashed register */
527 movq TD_PCB(%rcx), %rcx
528 movq %rax,PCB_ONFAULT(%rcx)
532 movq PCPU(curthread),%rcx
533 movq TD_PCB(%rcx), %rcx
534 movq $fusufault,PCB_ONFAULT(%rcx)
536 movq $VM_MAX_USER_ADDRESS-1,%rax
537 cmpq %rax,%rdi /* verify address validity */
543 movq PCPU(curthread),%rcx /* restore trashed register */
544 movq TD_PCB(%rcx), %rcx
545 movq %rax,PCB_ONFAULT(%rcx)
549 * copyinstr(from, to, maxlen, int *lencopied) - MP SAFE
550 * %rdi, %rsi, %rdx, %rcx
552 * copy a string from from to to, stop when a 0 character is reached.
553 * return ENAMETOOLONG if string is longer than maxlen, and
554 * EFAULT on protection violations. If lencopied is non-zero,
555 * return the actual length in *lencopied.
558 movq %rdx,%r8 /* %r8 = maxlen */
559 movq %rcx,%r9 /* %r9 = *len */
560 xchgq %rdi,%rsi /* %rdi = from, %rsi = to */
561 movq PCPU(curthread),%rcx
562 movq TD_PCB(%rcx), %rcx
563 movq $cpystrflt,PCB_ONFAULT(%rcx)
565 movq $VM_MAX_USER_ADDRESS,%rax
567 /* make sure 'from' is within bounds */
571 /* restrict maxlen to <= VM_MAX_USER_ADDRESS-from */
589 /* Success -- 0 byte reached */
594 /* rdx is zero - return ENAMETOOLONG or EFAULT */
595 movq $VM_MAX_USER_ADDRESS,%rax
599 movq $ENAMETOOLONG,%rax
606 /* set *lencopied and return %eax */
607 movq PCPU(curthread),%rcx
608 movq TD_PCB(%rcx), %rcx
609 movq $0,PCB_ONFAULT(%rcx)
620 * copystr(from, to, maxlen, int *lencopied) - MP SAFE
621 * %rdi, %rsi, %rdx, %rcx
624 movq %rdx,%r8 /* %r8 = maxlen */
637 /* Success -- 0 byte reached */
642 /* rdx is zero -- return ENAMETOOLONG */
643 movq $ENAMETOOLONG,%rax
649 /* set *lencopied and return %rax */
656 * Handling of special amd64 registers and descriptor tables etc
659 /* void lgdt(struct region_descriptor *rdp); */
661 /* reload the descriptor table */
664 /* flush the prefetch q */
671 movl %eax,%fs /* Beware, use wrmsr to set 64 bit base */
672 movl %eax,%gs /* Beware, use wrmsr to set 64 bit base */
675 /* reload code selector by turning return into intersegmental return */
682 /*****************************************************************************/
683 /* setjump, longjump */
684 /*****************************************************************************/
687 movq %rbx,0(%rdi) /* save rbx */
688 movq %rsp,8(%rdi) /* save rsp */
689 movq %rbp,16(%rdi) /* save rbp */
690 movq %r12,24(%rdi) /* save r12 */
691 movq %r13,32(%rdi) /* save r13 */
692 movq %r14,40(%rdi) /* save r14 */
693 movq %r15,48(%rdi) /* save r15 */
694 movq 0(%rsp),%rdx /* get rta */
695 movq %rdx,56(%rdi) /* save rip */
696 xorl %eax,%eax /* return(0); */
700 movq 0(%rdi),%rbx /* restore rbx */
701 movq 8(%rdi),%rsp /* restore rsp */
702 movq 16(%rdi),%rbp /* restore rbp */
703 movq 24(%rdi),%r12 /* restore r12 */
704 movq 32(%rdi),%r13 /* restore r13 */
705 movq 40(%rdi),%r14 /* restore r14 */
706 movq 48(%rdi),%r15 /* restore r15 */
707 movq 56(%rdi),%rdx /* get rta */
708 movq %rdx,0(%rsp) /* put in return frame */
709 xorl %eax,%eax /* return(1); */
714 * Support for BB-profiling (gcc -a). The kernbb program will extract
715 * the data from the kernel.
725 NON_GPROF_ENTRY(__bb_init_func)