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_base/isa/intr_machdep.h>
39 #include <machine/pmap.h>
49 * void bzero(void *buf, u_int len)
72 movnti %rax,(%rdi,%rdx)
73 movnti %rax,8(%rdi,%rdx)
74 movnti %rax,16(%rdi,%rdx)
75 movnti %rax,24(%rdi,%rdx)
84 cld /* compare forwards */
99 * bcopy(src, dst, cnt)
101 * ws@tools.de (Wolfgang Solfrank, TooLs GmbH) +49-228-985800
103 ENTRY(generic_bcopy) /* generic_bcopy is bcopy without FPU */
104 ENTRY(ovbcopy) /* our bcopy doesn't use the FPU, so ovbcopy is the same */
111 cmpq %rcx,%rax /* overlapping && src < dst? */
114 shrq $3,%rcx /* copy by 64-bit words */
115 cld /* nope, copy forwards */
119 andq $7,%rcx /* any bytes left? */
126 addq %rcx,%rdi /* copy backwards */
130 andq $7,%rcx /* any fractional bytes? */
134 movq %rdx,%rcx /* copy remainder by 32-bit words */
143 movq $0x200,%rax /* the manual says that bit 10 must be set to 1 */
144 movq %rax,%dr7 /* disable all breapoints first */
154 * Note: memcpy does not support overlapping copies
158 shrq $3,%rcx /* copy by 64-bit words */
159 cld /* copy forwards */
163 andq $7,%rcx /* any bytes left? */
169 * pagecopy(%rdi=from, %rsi=to)
172 movq $-PAGE_SIZE,%rax
177 prefetchnta (%rdi,%rax)
181 movq (%rdi,%rdx),%rax
182 movnti %rax,(%rsi,%rdx)
183 movq 8(%rdi,%rdx),%rax
184 movnti %rax,8(%rsi,%rdx)
185 movq 16(%rdi,%rdx),%rax
186 movnti %rax,16(%rsi,%rdx)
187 movq 24(%rdi,%rdx),%rax
188 movnti %rax,24(%rsi,%rdx)
194 /* fillw(pat, base, cnt) */
195 /* %rdi,%rsi, %rdx */
205 /*****************************************************************************/
206 /* copyout and fubyte family */
207 /*****************************************************************************/
209 * Access user memory from inside the kernel. These routines should be
210 * the only places that do this.
212 * These routines set curpcb->onfault for the time they execute. When a
213 * protection violation occurs inside the functions, the trap handler
214 * returns to *curpcb->onfault instead of the function.
218 * copyout(from_kernel, to_user, len) - MP SAFE
222 movq PCPU(curthread),%rax
223 movq TD_PCB(%rax), %rax
224 movq $copyout_fault,PCB_ONFAULT(%rax)
225 testq %rdx,%rdx /* anything to do? */
229 * Check explicitly for non-user addresses. If 486 write protection
230 * is being used, this check is essential because we are in kernel
231 * mode so the h/w does not provide any protection against writing
236 * First, prevent address wrapping.
242 * XXX STOP USING VM_MAX_USER_ADDRESS.
243 * It is an end address, not a max, so every time it is used correctly it
244 * looks like there is an off by one error, and of course it caused an off
245 * by one error in several places.
247 movq $VM_MAX_USER_ADDRESS,%rcx
252 /* bcopy(%rsi, %rdi, %rdx) */
266 movq PCPU(curthread),%rdx
267 movq TD_PCB(%rdx), %rdx
268 movq %rax,PCB_ONFAULT(%rdx)
273 movq PCPU(curthread),%rdx
274 movq TD_PCB(%rdx), %rdx
275 movq $0,PCB_ONFAULT(%rdx)
280 * copyin(from_user, to_kernel, len) - MP SAFE
284 movq PCPU(curthread),%rax
285 movq TD_PCB(%rax), %rax
286 movq $copyin_fault,PCB_ONFAULT(%rax)
287 testq %rdx,%rdx /* anything to do? */
291 * make sure address is valid
296 movq $VM_MAX_USER_ADDRESS,%rcx
303 shrq $3,%rcx /* copy longword-wise */
308 andb $7,%cl /* copy remaining bytes */
314 movq PCPU(curthread),%rdx
315 movq TD_PCB(%rdx), %rdx
316 movq %rax,PCB_ONFAULT(%rdx)
321 movq PCPU(curthread),%rdx
322 movq TD_PCB(%rdx), %rdx
323 movq $0,PCB_ONFAULT(%rdx)
328 * casuword32. Compare and set user integer. Returns -1 or the current value.
329 * dst = %rdi, old = %rsi, new = %rdx
332 movq PCPU(curthread),%rcx
333 movq TD_PCB(%rcx), %rcx
334 movq $fusufault,PCB_ONFAULT(%rcx)
336 movq $VM_MAX_USER_ADDRESS-4,%rax
337 cmpq %rax,%rdi /* verify address is valid */
340 movl %esi,%eax /* old */
344 cmpxchgl %edx,(%rdi) /* new = %edx */
347 * The old value is in %eax. If the store succeeded it will be the
348 * value we expected (old) from before the store, otherwise it will
349 * be the current value.
352 movq PCPU(curthread),%rcx
353 movq TD_PCB(%rcx), %rcx
354 movq $0,PCB_ONFAULT(%rcx)
358 * casuword. Compare and set user word. Returns -1 or the current value.
359 * dst = %rdi, old = %rsi, new = %rdx
362 movq PCPU(curthread),%rcx
363 movq TD_PCB(%rcx), %rcx
364 movq $fusufault,PCB_ONFAULT(%rcx)
366 movq $VM_MAX_USER_ADDRESS-4,%rax
367 cmpq %rax,%rdi /* verify address is valid */
370 movq %rsi,%rax /* old */
374 cmpxchgq %rdx,(%rdi) /* new = %rdx */
377 * The old value is in %eax. If the store succeeded it will be the
378 * value we expected (old) from before the store, otherwise it will
379 * be the current value.
382 movq PCPU(curthread),%rcx
383 movq TD_PCB(%rcx), %rcx
384 movq $fusufault,PCB_ONFAULT(%rcx)
385 movq $0,PCB_ONFAULT(%rcx)
389 * Fetch (load) a 64-bit word, a 32-bit word, a 16-bit word, or an 8-bit
390 * byte from user memory. All these functions are MPSAFE.
396 movq PCPU(curthread),%rcx
397 movq TD_PCB(%rcx), %rcx
398 movq $fusufault,PCB_ONFAULT(%rcx)
400 movq $VM_MAX_USER_ADDRESS-8,%rax
401 cmpq %rax,%rdi /* verify address is valid */
405 movq $0,PCB_ONFAULT(%rcx)
409 movq PCPU(curthread),%rcx
410 movq TD_PCB(%rcx), %rcx
411 movq $fusufault,PCB_ONFAULT(%rcx)
413 movq $VM_MAX_USER_ADDRESS-4,%rax
414 cmpq %rax,%rdi /* verify address is valid */
418 movq $0,PCB_ONFAULT(%rcx)
422 * fuswintr() and suswintr() are specialized variants of fuword16() and
423 * suword16(), respectively. They are called from the profiling code,
424 * potentially at interrupt time. If they fail, that's okay; good things
425 * will happen later. They always fail for now, until the trap code is
426 * able to deal with this.
434 movq PCPU(curthread),%rcx
435 movq TD_PCB(%rcx), %rcx
436 movq $fusufault,PCB_ONFAULT(%rcx)
438 movq $VM_MAX_USER_ADDRESS-2,%rax
443 movq $0,PCB_ONFAULT(%rcx)
447 movq PCPU(curthread),%rcx
448 movq TD_PCB(%rcx), %rcx
449 movq $fusufault,PCB_ONFAULT(%rcx)
451 movq $VM_MAX_USER_ADDRESS-1,%rax
456 movq $0,PCB_ONFAULT(%rcx)
461 movq PCPU(curthread),%rcx
463 movq TD_PCB(%rcx), %rcx
464 movq %rax,PCB_ONFAULT(%rcx)
469 * Store a 64-bit word, a 32-bit word, a 16-bit word, or an 8-bit byte to
470 * user memory. All these functions are MPSAFE.
471 * addr = %rdi, value = %rsi
475 movq PCPU(curthread),%rcx
476 movq TD_PCB(%rcx), %rcx
477 movq $fusufault,PCB_ONFAULT(%rcx)
479 movq $VM_MAX_USER_ADDRESS-8,%rax
480 cmpq %rax,%rdi /* verify address validity */
485 movq PCPU(curthread),%rcx
486 movq TD_PCB(%rcx), %rcx
487 movq %rax,PCB_ONFAULT(%rcx)
491 movq PCPU(curthread),%rcx
492 movq TD_PCB(%rcx), %rcx
493 movq $fusufault,PCB_ONFAULT(%rcx)
495 movq $VM_MAX_USER_ADDRESS-4,%rax
496 cmpq %rax,%rdi /* verify address validity */
501 movq PCPU(curthread),%rcx
502 movq TD_PCB(%rcx), %rcx
503 movq %rax,PCB_ONFAULT(%rcx)
507 movq PCPU(curthread),%rcx
508 movq TD_PCB(%rcx), %rcx
509 movq $fusufault,PCB_ONFAULT(%rcx)
511 movq $VM_MAX_USER_ADDRESS-2,%rax
512 cmpq %rax,%rdi /* verify address validity */
517 movq PCPU(curthread),%rcx /* restore trashed register */
518 movq TD_PCB(%rcx), %rcx
519 movq %rax,PCB_ONFAULT(%rcx)
523 movq PCPU(curthread),%rcx
524 movq TD_PCB(%rcx), %rcx
525 movq $fusufault,PCB_ONFAULT(%rcx)
527 movq $VM_MAX_USER_ADDRESS-1,%rax
528 cmpq %rax,%rdi /* verify address validity */
534 movq PCPU(curthread),%rcx /* restore trashed register */
535 movq TD_PCB(%rcx), %rcx
536 movq %rax,PCB_ONFAULT(%rcx)
540 * copyinstr(from, to, maxlen, int *lencopied) - MP SAFE
541 * %rdi, %rsi, %rdx, %rcx
543 * copy a string from from to to, stop when a 0 character is reached.
544 * return ENAMETOOLONG if string is longer than maxlen, and
545 * EFAULT on protection violations. If lencopied is non-zero,
546 * return the actual length in *lencopied.
549 movq %rdx,%r8 /* %r8 = maxlen */
550 movq %rcx,%r9 /* %r9 = *len */
551 xchgq %rdi,%rsi /* %rdi = from, %rsi = to */
552 movq PCPU(curthread),%rcx
553 movq TD_PCB(%rcx), %rcx
554 movq $cpystrflt,PCB_ONFAULT(%rcx)
556 movq $VM_MAX_USER_ADDRESS,%rax
558 /* make sure 'from' is within bounds */
562 /* restrict maxlen to <= VM_MAX_USER_ADDRESS-from */
580 /* Success -- 0 byte reached */
585 /* rdx is zero - return ENAMETOOLONG or EFAULT */
586 movq $VM_MAX_USER_ADDRESS,%rax
590 movq $ENAMETOOLONG,%rax
597 /* set *lencopied and return %eax */
598 movq PCPU(curthread),%rcx
599 movq TD_PCB(%rcx), %rcx
600 movq $0,PCB_ONFAULT(%rcx)
611 * copystr(from, to, maxlen, int *lencopied) - MP SAFE
612 * %rdi, %rsi, %rdx, %rcx
615 movq %rdx,%r8 /* %r8 = maxlen */
628 /* Success -- 0 byte reached */
633 /* rdx is zero -- return ENAMETOOLONG */
634 movq $ENAMETOOLONG,%rax
640 /* set *lencopied and return %rax */
647 * Handling of special amd64 registers and descriptor tables etc
650 /* void lgdt(struct region_descriptor *rdp); */
652 /* reload the descriptor table */
655 /* flush the prefetch q */
662 movl %eax,%fs /* Beware, use wrmsr to set 64 bit base */
663 movl %eax,%gs /* Beware, use wrmsr to set 64 bit base */
666 /* reload code selector by turning return into intersegmental return */
673 /*****************************************************************************/
674 /* setjump, longjump */
675 /*****************************************************************************/
678 movq %rbx,0(%rdi) /* save rbx */
679 movq %rsp,8(%rdi) /* save rsp */
680 movq %rbp,16(%rdi) /* save rbp */
681 movq %r12,24(%rdi) /* save r12 */
682 movq %r13,32(%rdi) /* save r13 */
683 movq %r14,40(%rdi) /* save r14 */
684 movq %r15,48(%rdi) /* save r15 */
685 movq 0(%rsp),%rdx /* get rta */
686 movq %rdx,56(%rdi) /* save rip */
687 xorl %eax,%eax /* return(0); */
691 movq 0(%rdi),%rbx /* restore rbx */
692 movq 8(%rdi),%rsp /* restore rsp */
693 movq 16(%rdi),%rbp /* restore rbp */
694 movq 24(%rdi),%r12 /* restore r12 */
695 movq 32(%rdi),%r13 /* restore r13 */
696 movq 40(%rdi),%r14 /* restore r14 */
697 movq 48(%rdi),%r15 /* restore r15 */
698 movq 56(%rdi),%rdx /* get rta */
699 movq %rdx,0(%rsp) /* put in return frame */
700 xorl %eax,%eax /* return(1); */
705 * Support for BB-profiling (gcc -a). The kernbb program will extract
706 * the data from the kernel.
716 NON_GPROF_ENTRY(__bb_init_func)