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 * 3. 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 $
34 #include <machine/asmacros.h>
35 #include <machine/pmap.h>
44 * bzero(ptr:%rdi, bytes:%rsi)
46 * Using rep stosq is 70% faster than a %rax loop and almost as fast as
47 * a %xmm0 loop on a modern intel cpu.
49 * Do not use non-termportal instructions here as we do not know the caller's
69 * Using rep stosq is nearly as fast as using %xmm0 on a modern intel cpu,
70 * and about 70% faster than a %rax loop.
72 * Do not use non-termportal instructions here as we do not know the caller's
76 movq $PAGE_SIZE>>3,%rcx
85 * bcmp(ptr:%rdi, ptr:%rsi, bytes:%rdx)
90 cld /* compare forwards */
106 * bcopy(src:%rdi, dst:%rsi, cnt:%rdx)
108 * ws@tools.de (Wolfgang Solfrank, TooLs GmbH) +49-228-985800
116 cmpq %rcx,%rax /* overlapping && src < dst? */
119 shrq $3,%rcx /* copy by 64-bit words */
120 cld /* nope, copy forwards */
124 andq $7,%rcx /* any bytes left? */
131 addq %rcx,%rdi /* copy backwards */
135 andq $7,%rcx /* any fractional bytes? */
139 movq %rdx,%rcx /* copy by 32-bit words */
150 movq $0x200,%rax /* the manual says that bit 10 must be set to 1 */
151 movq %rax,%dr7 /* disable all breapoints first */
162 * memcpy(dst:%rdi, src:%rsi, bytes:%rdx)
164 * Note: memcpy does not support overlapping copies
169 shrq $3,%rcx /* copy by 64-bit words */
170 cld /* copy forwards */
174 andq $7,%rcx /* any bytes left? */
181 /* fillw(pat, base, cnt) */
182 /* %rdi,%rsi, %rdx */
193 /*****************************************************************************/
194 /* copyout and fubyte family */
195 /*****************************************************************************/
197 * Access user memory from inside the kernel. These routines should be
198 * the only places that do this.
200 * These routines set curpcb->onfault for the time they execute. When a
201 * protection violation occurs inside the functions, the trap handler
202 * returns to *curpcb->onfault instead of the function.
206 * std_copyout(from_kernel, to_user, len) - MP SAFE
210 movq PCPU(curthread),%rax
211 movq TD_PCB(%rax), %rax
212 movq $copyout_fault,PCB_ONFAULT(%rax)
213 movq %rsp,PCB_ONFAULT_SP(%rax)
214 testq %rdx,%rdx /* anything to do? */
218 * Check explicitly for non-user addresses. If 486 write protection
219 * is being used, this check is essential because we are in kernel
220 * mode so the h/w does not provide any protection against writing
225 * First, prevent address wrapping.
231 * XXX STOP USING VM_MAX_USER_ADDRESS.
232 * It is an end address, not a max, so every time it is used correctly it
233 * looks like there is an off by one error, and of course it caused an off
234 * by one error in several places.
236 movq $VM_MAX_USER_ADDRESS,%rcx
241 /* bcopy(%rsi, %rdi, %rdx) */
255 movq PCPU(curthread),%rdx
256 movq TD_PCB(%rdx), %rdx
257 movq %rax,PCB_ONFAULT(%rdx)
262 movq PCPU(curthread),%rdx
263 movq TD_PCB(%rdx), %rdx
264 movq $0,PCB_ONFAULT(%rdx)
270 * std_copyin(from_user, to_kernel, len) - MP SAFE
274 movq PCPU(curthread),%rax
275 movq TD_PCB(%rax), %rax
276 movq $copyin_fault,PCB_ONFAULT(%rax)
277 movq %rsp,PCB_ONFAULT_SP(%rax)
278 testq %rdx,%rdx /* anything to do? */
282 * make sure address is valid
287 movq $VM_MAX_USER_ADDRESS,%rcx
294 shrq $3,%rcx /* copy longword-wise */
299 andb $7,%cl /* copy remaining bytes */
305 movq PCPU(curthread),%rdx
306 movq TD_PCB(%rdx), %rdx
307 movq %rax,PCB_ONFAULT(%rdx)
312 movq PCPU(curthread),%rdx
313 movq TD_PCB(%rdx), %rdx
314 movq $0,PCB_ONFAULT(%rdx)
320 * casu32 - Compare and set user integer. Returns -1 or the current value.
321 * dst = %rdi, old = %rsi, new = %rdx
324 movq PCPU(curthread),%rcx
325 movq TD_PCB(%rcx), %rcx
326 movq $fusufault,PCB_ONFAULT(%rcx)
327 movq %rsp,PCB_ONFAULT_SP(%rcx)
329 movq $VM_MAX_USER_ADDRESS-4,%rax
330 cmpq %rax,%rdi /* verify address is valid */
333 movl %esi,%eax /* old */
335 cmpxchgl %edx,(%rdi) /* new = %edx */
338 * The old value is in %eax. If the store succeeded it will be the
339 * value we expected (old) from before the store, otherwise it will
340 * be the current value.
343 movq PCPU(curthread),%rcx
344 movq TD_PCB(%rcx), %rcx
345 movq $0,PCB_ONFAULT(%rcx)
350 * swapu32 - Swap int in user space. ptr = %rdi, val = %rsi
353 movq PCPU(curthread),%rcx
354 movq TD_PCB(%rcx), %rcx
355 movq $fusufault,PCB_ONFAULT(%rcx)
356 movq %rsp,PCB_ONFAULT_SP(%rcx)
358 movq $VM_MAX_USER_ADDRESS-4,%rax
359 cmpq %rax,%rdi /* verify address is valid */
362 movq %rsi,%rax /* old */
366 * The old value is in %rax. If the store succeeded it will be the
367 * value we expected (old) from before the store, otherwise it will
368 * be the current value.
371 movq PCPU(curthread),%rcx
372 movq TD_PCB(%rcx), %rcx
373 movq $0,PCB_ONFAULT(%rcx)
378 * casu64 - Compare and set user word. Returns -1 or the current value.
379 * dst = %rdi, old = %rsi, new = %rdx
382 movq PCPU(curthread),%rcx
383 movq TD_PCB(%rcx), %rcx
384 movq $fusufault,PCB_ONFAULT(%rcx)
385 movq %rsp,PCB_ONFAULT_SP(%rcx)
387 movq $VM_MAX_USER_ADDRESS-8,%rax
388 cmpq %rax,%rdi /* verify address is valid */
391 movq %rsi,%rax /* old */
393 cmpxchgq %rdx,(%rdi) /* new = %rdx */
396 * The old value is in %rax. If the store succeeded it will be the
397 * value we expected (old) from before the store, otherwise it will
398 * be the current value.
401 movq PCPU(curthread),%rcx
402 movq TD_PCB(%rcx), %rcx
403 movq $0,PCB_ONFAULT(%rcx)
408 * swapu64 - Swap long in user space. ptr = %rdi, val = %rsi
411 movq PCPU(curthread),%rcx
412 movq TD_PCB(%rcx), %rcx
413 movq $fusufault,PCB_ONFAULT(%rcx)
414 movq %rsp,PCB_ONFAULT_SP(%rcx)
416 movq $VM_MAX_USER_ADDRESS-8,%rax
417 cmpq %rax,%rdi /* verify address is valid */
420 movq %rsi,%rax /* old */
424 * The old value is in %rax. If the store succeeded it will be the
425 * value we expected (old) from before the store, otherwise it will
426 * be the current value.
429 movq PCPU(curthread),%rcx
430 movq TD_PCB(%rcx), %rcx
431 movq $0,PCB_ONFAULT(%rcx)
436 * Fetch (load) a 64-bit word, a 32-bit word, a 16-bit word, or an 8-bit
437 * byte from user memory. All these functions are MPSAFE.
442 movq PCPU(curthread),%rcx
443 movq TD_PCB(%rcx), %rcx
444 movq $fusufault,PCB_ONFAULT(%rcx)
445 movq %rsp,PCB_ONFAULT_SP(%rcx)
447 movq $VM_MAX_USER_ADDRESS-8,%rax
448 cmpq %rax,%rdi /* verify address is valid */
452 movq $0,PCB_ONFAULT(%rcx)
457 movq PCPU(curthread),%rcx
458 movq TD_PCB(%rcx), %rcx
459 movq $fusufault,PCB_ONFAULT(%rcx)
460 movq %rsp,PCB_ONFAULT_SP(%rcx)
462 movq $VM_MAX_USER_ADDRESS-4,%rax
463 cmpq %rax,%rdi /* verify address is valid */
467 movq $0,PCB_ONFAULT(%rcx)
472 movq PCPU(curthread),%rcx
473 movq TD_PCB(%rcx), %rcx
474 movq $fusufault,PCB_ONFAULT(%rcx)
475 movq %rsp,PCB_ONFAULT_SP(%rcx)
477 movq $VM_MAX_USER_ADDRESS-1,%rax
482 movq $0,PCB_ONFAULT(%rcx)
487 movq PCPU(curthread),%rcx
489 movq TD_PCB(%rcx), %rcx
490 movq %rax,PCB_ONFAULT(%rcx)
496 * Store a 64-bit word, a 32-bit word, a 16-bit word, or an 8-bit byte to
497 * user memory. All these functions are MPSAFE.
499 * addr = %rdi, value = %rsi
504 movq PCPU(curthread),%rcx
505 movq TD_PCB(%rcx), %rcx
506 movq $fusufault,PCB_ONFAULT(%rcx)
507 movq %rsp,PCB_ONFAULT_SP(%rcx)
509 movq $VM_MAX_USER_ADDRESS-8,%rax
510 cmpq %rax,%rdi /* verify address validity */
515 movq PCPU(curthread),%rcx
516 movq TD_PCB(%rcx), %rcx
517 movq %rax,PCB_ONFAULT(%rcx)
525 movq PCPU(curthread),%rcx
526 movq TD_PCB(%rcx), %rcx
527 movq $fusufault,PCB_ONFAULT(%rcx)
528 movq %rsp,PCB_ONFAULT_SP(%rcx)
530 movq $VM_MAX_USER_ADDRESS-4,%rax
531 cmpq %rax,%rdi /* verify address validity */
536 movq PCPU(curthread),%rcx
537 movq TD_PCB(%rcx), %rcx
538 movq %rax,PCB_ONFAULT(%rcx)
543 movq PCPU(curthread),%rcx
544 movq TD_PCB(%rcx), %rcx
545 movq $fusufault,PCB_ONFAULT(%rcx)
546 movq %rsp,PCB_ONFAULT_SP(%rcx)
548 movq $VM_MAX_USER_ADDRESS-1,%rax
549 cmpq %rax,%rdi /* verify address validity */
555 movq PCPU(curthread),%rcx /* restore trashed register */
556 movq TD_PCB(%rcx), %rcx
557 movq %rax,PCB_ONFAULT(%rcx)
562 * std_copyinstr(from, to, maxlen, int *lencopied) - MP SAFE
563 * %rdi, %rsi, %rdx, %rcx
565 * copy a string from from to to, stop when a 0 character is reached.
566 * return ENAMETOOLONG if string is longer than maxlen, and
567 * EFAULT on protection violations. If lencopied is non-zero,
568 * return the actual length in *lencopied.
571 movq %rdx,%r8 /* %r8 = maxlen */
572 movq %rcx,%r9 /* %r9 = *len */
573 xchgq %rdi,%rsi /* %rdi = from, %rsi = to */
574 movq PCPU(curthread),%rcx
575 movq TD_PCB(%rcx), %rcx
576 movq $cpystrflt,PCB_ONFAULT(%rcx)
577 movq %rsp,PCB_ONFAULT_SP(%rcx)
579 movq $VM_MAX_USER_ADDRESS,%rax
581 /* make sure 'from' is within bounds */
585 /* restrict maxlen to <= VM_MAX_USER_ADDRESS-from */
603 /* Success -- 0 byte reached */
608 /* rdx is zero - return ENAMETOOLONG or EFAULT */
609 movq $VM_MAX_USER_ADDRESS,%rax
613 movq $ENAMETOOLONG,%rax
620 /* set *lencopied and return %eax */
621 movq PCPU(curthread),%rcx
622 movq TD_PCB(%rcx), %rcx
623 movq $0,PCB_ONFAULT(%rcx)
634 * copystr(from, to, maxlen, int *lencopied) - MP SAFE
635 * %rdi, %rsi, %rdx, %rcx
638 movq %rdx,%r8 /* %r8 = maxlen */
651 /* Success -- 0 byte reached */
656 /* rdx is zero -- return ENAMETOOLONG */
657 movq $ENAMETOOLONG,%rax
663 /* set *lencopied and return %rax */
671 * Handling of special x86_64 registers and descriptor tables etc
674 /* void lgdt(struct region_descriptor *rdp); */
676 /* reload the descriptor table */
679 /* flush the prefetch q */
686 movl %eax,%fs /* Beware, use wrmsr to set 64 bit base */
687 movl %eax,%gs /* Beware, use wrmsr to set 64 bit base */
690 /* reload code selector by turning return into intersegmental return */
698 /*****************************************************************************/
699 /* setjmp, longjmp */
700 /*****************************************************************************/
703 movq %rbx,0(%rdi) /* save rbx */
704 movq %rsp,8(%rdi) /* save rsp */
705 movq %rbp,16(%rdi) /* save rbp */
706 movq %r12,24(%rdi) /* save r12 */
707 movq %r13,32(%rdi) /* save r13 */
708 movq %r14,40(%rdi) /* save r14 */
709 movq %r15,48(%rdi) /* save r15 */
710 movq 0(%rsp),%rdx /* get rta */
711 movq %rdx,56(%rdi) /* save rip */
712 xorl %eax,%eax /* return(0); */
717 movq 0(%rdi),%rbx /* restore rbx */
718 movq 8(%rdi),%rsp /* restore rsp */
719 movq 16(%rdi),%rbp /* restore rbp */
720 movq 24(%rdi),%r12 /* restore r12 */
721 movq 32(%rdi),%r13 /* restore r13 */
722 movq 40(%rdi),%r14 /* restore r14 */
723 movq 48(%rdi),%r15 /* restore r15 */
724 movq 56(%rdi),%rdx /* get rta */
725 movq %rdx,0(%rsp) /* put in return frame */
726 xorl %eax,%eax /* return(1); */
732 * Support for reading MSRs in the safe manner.
735 /* int rdmsr_safe(u_int msr, uint64_t *data) */
736 movq PCPU(curthread),%r8
737 movq TD_PCB(%r8), %r8
738 movq $msr_onfault,PCB_ONFAULT(%r8)
739 movq %rsp,PCB_ONFAULT_SP(%r8)
741 rdmsr /* Read MSR pointed by %ecx. Returns
742 hi byte in edx, lo in %eax */
743 salq $32,%rdx /* sign-shift %rdx left */
744 movl %eax,%eax /* zero-extend %eax -> %rax */
748 movq %rax,PCB_ONFAULT(%r8)
753 * Support for writing MSRs in the safe manner.
756 /* int wrmsr_safe(u_int msr, uint64_t data) */
757 movq PCPU(curthread),%r8
758 movq TD_PCB(%r8), %r8
759 movq $msr_onfault,PCB_ONFAULT(%r8)
760 movq %rsp,PCB_ONFAULT_SP(%r8)
765 wrmsr /* Write MSR pointed by %ecx. Accepts
766 hi byte in edx, lo in %eax. */
768 movq %rax,PCB_ONFAULT(%r8)
773 * MSR operations fault handler
777 movq PCPU(curthread),%r8
778 movq TD_PCB(%r8), %r8
779 movq $0,PCB_ONFAULT(%r8)