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_base/isa/intr_machdep.h>
38 #include <machine/pmap.h>
48 * void bzero(void *buf, u_int len)
71 movnti %rax,(%rdi,%rdx)
72 movnti %rax,8(%rdi,%rdx)
73 movnti %rax,16(%rdi,%rdx)
74 movnti %rax,24(%rdi,%rdx)
83 cld /* compare forwards */
98 * bcopy(src, dst, cnt)
100 * ws@tools.de (Wolfgang Solfrank, TooLs GmbH) +49-228-985800
102 ENTRY(generic_bcopy) /* generic_bcopy is bcopy without FPU */
103 ENTRY(ovbcopy) /* our bcopy doesn't use the FPU, so ovbcopy is the same */
110 cmpq %rcx,%rax /* overlapping && src < dst? */
113 shrq $3,%rcx /* copy by 64-bit words */
114 cld /* nope, copy forwards */
118 andq $7,%rcx /* any bytes left? */
125 addq %rcx,%rdi /* copy backwards */
129 andq $7,%rcx /* any fractional bytes? */
133 movq %rdx,%rcx /* copy remainder by 32-bit words */
142 movq $0x200,%rax /* the manual says that bit 10 must be set to 1 */
143 movq %rax,%dr7 /* disable all breapoints first */
153 * Note: memcpy does not support overlapping copies
157 shrq $3,%rcx /* copy by 64-bit words */
158 cld /* copy forwards */
162 andq $7,%rcx /* any bytes left? */
168 * pagecopy(%rdi=from, %rsi=to)
171 movq $-PAGE_SIZE,%rax
176 prefetchnta (%rdi,%rax)
180 movq (%rdi,%rdx),%rax
181 movnti %rax,(%rsi,%rdx)
182 movq 8(%rdi,%rdx),%rax
183 movnti %rax,8(%rsi,%rdx)
184 movq 16(%rdi,%rdx),%rax
185 movnti %rax,16(%rsi,%rdx)
186 movq 24(%rdi,%rdx),%rax
187 movnti %rax,24(%rsi,%rdx)
193 /* fillw(pat, base, cnt) */
194 /* %rdi,%rsi, %rdx */
204 /*****************************************************************************/
205 /* copyout and fubyte family */
206 /*****************************************************************************/
208 * Access user memory from inside the kernel. These routines should be
209 * the only places that do this.
211 * These routines set curpcb->onfault for the time they execute. When a
212 * protection violation occurs inside the functions, the trap handler
213 * returns to *curpcb->onfault instead of the function.
217 * copyout(from_kernel, to_user, len) - MP SAFE
221 movq PCPU(curthread),%rax
222 movq TD_PCB(%rax), %rax
223 movq $copyout_fault,PCB_ONFAULT(%rax)
224 testq %rdx,%rdx /* anything to do? */
228 * Check explicitly for non-user addresses. If 486 write protection
229 * is being used, this check is essential because we are in kernel
230 * mode so the h/w does not provide any protection against writing
235 * First, prevent address wrapping.
241 * XXX STOP USING VM_MAX_USER_ADDRESS.
242 * It is an end address, not a max, so every time it is used correctly it
243 * looks like there is an off by one error, and of course it caused an off
244 * by one error in several places.
246 movq $VM_MAX_USER_ADDRESS,%rcx
251 /* bcopy(%rsi, %rdi, %rdx) */
265 movq PCPU(curthread),%rdx
266 movq TD_PCB(%rdx), %rdx
267 movq %rax,PCB_ONFAULT(%rdx)
272 movq PCPU(curthread),%rdx
273 movq TD_PCB(%rdx), %rdx
274 movq $0,PCB_ONFAULT(%rdx)
279 * copyin(from_user, to_kernel, len) - MP SAFE
283 movq PCPU(curthread),%rax
284 movq TD_PCB(%rax), %rax
285 movq $copyin_fault,PCB_ONFAULT(%rax)
286 testq %rdx,%rdx /* anything to do? */
290 * make sure address is valid
295 movq $VM_MAX_USER_ADDRESS,%rcx
302 shrq $3,%rcx /* copy longword-wise */
307 andb $7,%cl /* copy remaining bytes */
313 movq PCPU(curthread),%rdx
314 movq TD_PCB(%rdx), %rdx
315 movq %rax,PCB_ONFAULT(%rdx)
320 movq PCPU(curthread),%rdx
321 movq TD_PCB(%rdx), %rdx
322 movq $0,PCB_ONFAULT(%rdx)
327 * casuword32. Compare and set user integer. Returns -1 or the current value.
328 * dst = %rdi, old = %rsi, new = %rdx
331 movq PCPU(curthread),%rcx
332 movq TD_PCB(%rcx), %rcx
333 movq $fusufault,PCB_ONFAULT(%rcx)
335 movq $VM_MAX_USER_ADDRESS-4,%rax
336 cmpq %rax,%rdi /* verify address is valid */
339 movl %esi,%eax /* old */
343 cmpxchgl %edx,(%rdi) /* new = %edx */
346 * The old value is in %eax. If the store succeeded it will be the
347 * value we expected (old) from before the store, otherwise it will
348 * be the current value.
351 movq PCPU(curthread),%rcx
352 movq TD_PCB(%rcx), %rcx
353 movq $0,PCB_ONFAULT(%rcx)
357 * casuword. Compare and set user word. Returns -1 or the current value.
358 * dst = %rdi, old = %rsi, new = %rdx
361 movq PCPU(curthread),%rcx
362 movq TD_PCB(%rcx), %rcx
363 movq $fusufault,PCB_ONFAULT(%rcx)
365 movq $VM_MAX_USER_ADDRESS-4,%rax
366 cmpq %rax,%rdi /* verify address is valid */
369 movq %rsi,%rax /* old */
373 cmpxchgq %rdx,(%rdi) /* new = %rdx */
376 * The old value is in %eax. If the store succeeded it will be the
377 * value we expected (old) from before the store, otherwise it will
378 * be the current value.
381 movq PCPU(curthread),%rcx
382 movq TD_PCB(%rcx), %rcx
383 movq $fusufault,PCB_ONFAULT(%rcx)
384 movq $0,PCB_ONFAULT(%rcx)
388 * Fetch (load) a 64-bit word, a 32-bit word, a 16-bit word, or an 8-bit
389 * byte from user memory. All these functions are MPSAFE.
395 movq PCPU(curthread),%rcx
396 movq TD_PCB(%rcx), %rcx
397 movq $fusufault,PCB_ONFAULT(%rcx)
399 movq $VM_MAX_USER_ADDRESS-8,%rax
400 cmpq %rax,%rdi /* verify address is valid */
404 movq $0,PCB_ONFAULT(%rcx)
408 movq PCPU(curthread),%rcx
409 movq TD_PCB(%rcx), %rcx
410 movq $fusufault,PCB_ONFAULT(%rcx)
412 movq $VM_MAX_USER_ADDRESS-4,%rax
413 cmpq %rax,%rdi /* verify address is valid */
417 movq $0,PCB_ONFAULT(%rcx)
421 * fuswintr() and suswintr() are specialized variants of fuword16() and
422 * suword16(), respectively. They are called from the profiling code,
423 * potentially at interrupt time. If they fail, that's okay; good things
424 * will happen later. They always fail for now, until the trap code is
425 * able to deal with this.
433 movq PCPU(curthread),%rcx
434 movq TD_PCB(%rcx), %rcx
435 movq $fusufault,PCB_ONFAULT(%rcx)
437 movq $VM_MAX_USER_ADDRESS-2,%rax
442 movq $0,PCB_ONFAULT(%rcx)
446 movq PCPU(curthread),%rcx
447 movq TD_PCB(%rcx), %rcx
448 movq $fusufault,PCB_ONFAULT(%rcx)
450 movq $VM_MAX_USER_ADDRESS-1,%rax
455 movq $0,PCB_ONFAULT(%rcx)
460 movq PCPU(curthread),%rcx
462 movq TD_PCB(%rcx), %rcx
463 movq %rax,PCB_ONFAULT(%rcx)
468 * Store a 64-bit word, a 32-bit word, a 16-bit word, or an 8-bit byte to
469 * user memory. All these functions are MPSAFE.
470 * addr = %rdi, value = %rsi
474 movq PCPU(curthread),%rcx
475 movq TD_PCB(%rcx), %rcx
476 movq $fusufault,PCB_ONFAULT(%rcx)
478 movq $VM_MAX_USER_ADDRESS-8,%rax
479 cmpq %rax,%rdi /* verify address validity */
484 movq PCPU(curthread),%rcx
485 movq TD_PCB(%rcx), %rcx
486 movq %rax,PCB_ONFAULT(%rcx)
490 movq PCPU(curthread),%rcx
491 movq TD_PCB(%rcx), %rcx
492 movq $fusufault,PCB_ONFAULT(%rcx)
494 movq $VM_MAX_USER_ADDRESS-4,%rax
495 cmpq %rax,%rdi /* verify address validity */
500 movq PCPU(curthread),%rcx
501 movq TD_PCB(%rcx), %rcx
502 movq %rax,PCB_ONFAULT(%rcx)
506 movq PCPU(curthread),%rcx
507 movq TD_PCB(%rcx), %rcx
508 movq $fusufault,PCB_ONFAULT(%rcx)
510 movq $VM_MAX_USER_ADDRESS-2,%rax
511 cmpq %rax,%rdi /* verify address validity */
516 movq PCPU(curthread),%rcx /* restore trashed register */
517 movq TD_PCB(%rcx), %rcx
518 movq %rax,PCB_ONFAULT(%rcx)
522 movq PCPU(curthread),%rcx
523 movq TD_PCB(%rcx), %rcx
524 movq $fusufault,PCB_ONFAULT(%rcx)
526 movq $VM_MAX_USER_ADDRESS-1,%rax
527 cmpq %rax,%rdi /* verify address validity */
533 movq PCPU(curthread),%rcx /* restore trashed register */
534 movq TD_PCB(%rcx), %rcx
535 movq %rax,PCB_ONFAULT(%rcx)
539 * copyinstr(from, to, maxlen, int *lencopied) - MP SAFE
540 * %rdi, %rsi, %rdx, %rcx
542 * copy a string from from to to, stop when a 0 character is reached.
543 * return ENAMETOOLONG if string is longer than maxlen, and
544 * EFAULT on protection violations. If lencopied is non-zero,
545 * return the actual length in *lencopied.
548 movq %rdx,%r8 /* %r8 = maxlen */
549 movq %rcx,%r9 /* %r9 = *len */
550 xchgq %rdi,%rsi /* %rdi = from, %rsi = to */
551 movq PCPU(curthread),%rcx
552 movq TD_PCB(%rcx), %rcx
553 movq $cpystrflt,PCB_ONFAULT(%rcx)
555 movq $VM_MAX_USER_ADDRESS,%rax
557 /* make sure 'from' is within bounds */
561 /* restrict maxlen to <= VM_MAX_USER_ADDRESS-from */
579 /* Success -- 0 byte reached */
584 /* rdx is zero - return ENAMETOOLONG or EFAULT */
585 movq $VM_MAX_USER_ADDRESS,%rax
589 movq $ENAMETOOLONG,%rax
596 /* set *lencopied and return %eax */
597 movq PCPU(curthread),%rcx
598 movq TD_PCB(%rcx), %rcx
599 movq $0,PCB_ONFAULT(%rcx)
610 * copystr(from, to, maxlen, int *lencopied) - MP SAFE
611 * %rdi, %rsi, %rdx, %rcx
614 movq %rdx,%r8 /* %r8 = maxlen */
627 /* Success -- 0 byte reached */
632 /* rdx is zero -- return ENAMETOOLONG */
633 movq $ENAMETOOLONG,%rax
639 /* set *lencopied and return %rax */
646 * Handling of special x86_64 registers and descriptor tables etc
649 /* void lgdt(struct region_descriptor *rdp); */
651 /* reload the descriptor table */
654 /* flush the prefetch q */
661 movl %eax,%fs /* Beware, use wrmsr to set 64 bit base */
662 movl %eax,%gs /* Beware, use wrmsr to set 64 bit base */
665 /* reload code selector by turning return into intersegmental return */
672 /*****************************************************************************/
673 /* setjump, longjump */
674 /*****************************************************************************/
677 movq %rbx,0(%rdi) /* save rbx */
678 movq %rsp,8(%rdi) /* save rsp */
679 movq %rbp,16(%rdi) /* save rbp */
680 movq %r12,24(%rdi) /* save r12 */
681 movq %r13,32(%rdi) /* save r13 */
682 movq %r14,40(%rdi) /* save r14 */
683 movq %r15,48(%rdi) /* save r15 */
684 movq 0(%rsp),%rdx /* get rta */
685 movq %rdx,56(%rdi) /* save rip */
686 xorl %eax,%eax /* return(0); */
690 movq 0(%rdi),%rbx /* restore rbx */
691 movq 8(%rdi),%rsp /* restore rsp */
692 movq 16(%rdi),%rbp /* restore rbp */
693 movq 24(%rdi),%r12 /* restore r12 */
694 movq 32(%rdi),%r13 /* restore r13 */
695 movq 40(%rdi),%r14 /* restore r14 */
696 movq 48(%rdi),%r15 /* restore r15 */
697 movq 56(%rdi),%rdx /* get rta */
698 movq %rdx,0(%rsp) /* put in return frame */
699 xorl %eax,%eax /* return(1); */
704 * Support for BB-profiling (gcc -a). The kernbb program will extract
705 * the data from the kernel.
715 NON_GPROF_ENTRY(__bb_init_func)