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, size_t len)
70 movq %rax,(%rdi,%rdx) /* movnti */
71 movq %rax,8(%rdi,%rdx) /* movnti */
72 movq %rax,16(%rdi,%rdx) /* movnti */
73 movq %rax,24(%rdi,%rdx) /* movnti */
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 movq %rax,(%rsi,%rdx) /* movnti */
181 movq 8(%rdi,%rdx),%rax
182 movq %rax,8(%rsi,%rdx) /* movnti */
183 movq 16(%rdi,%rdx),%rax
184 movq %rax,16(%rsi,%rdx) /* movnti */
185 movq 24(%rdi,%rdx),%rax
186 movq %rax,24(%rsi,%rdx) /* movnti */
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.
470 * addr = %rdi, value = %rsi
476 movq PCPU(curthread),%rcx
477 movq TD_PCB(%rcx), %rcx
478 movq $fusufault,PCB_ONFAULT(%rcx)
480 movq $VM_MAX_USER_ADDRESS-8,%rax
481 cmpq %rax,%rdi /* verify address validity */
486 movq PCPU(curthread),%rcx
487 movq TD_PCB(%rcx), %rcx
488 movq %rax,PCB_ONFAULT(%rcx)
495 movq PCPU(curthread),%rcx
496 movq TD_PCB(%rcx), %rcx
497 movq $fusufault,PCB_ONFAULT(%rcx)
499 movq $VM_MAX_USER_ADDRESS-4,%rax
500 cmpq %rax,%rdi /* verify address validity */
505 movq PCPU(curthread),%rcx
506 movq TD_PCB(%rcx), %rcx
507 movq %rax,PCB_ONFAULT(%rcx)
511 movq PCPU(curthread),%rcx
512 movq TD_PCB(%rcx), %rcx
513 movq $fusufault,PCB_ONFAULT(%rcx)
515 movq $VM_MAX_USER_ADDRESS-2,%rax
516 cmpq %rax,%rdi /* verify address validity */
521 movq PCPU(curthread),%rcx /* restore trashed register */
522 movq TD_PCB(%rcx), %rcx
523 movq %rax,PCB_ONFAULT(%rcx)
527 movq PCPU(curthread),%rcx
528 movq TD_PCB(%rcx), %rcx
529 movq $fusufault,PCB_ONFAULT(%rcx)
531 movq $VM_MAX_USER_ADDRESS-1,%rax
532 cmpq %rax,%rdi /* verify address validity */
538 movq PCPU(curthread),%rcx /* restore trashed register */
539 movq TD_PCB(%rcx), %rcx
540 movq %rax,PCB_ONFAULT(%rcx)
544 * copyinstr(from, to, maxlen, int *lencopied) - MP SAFE
545 * %rdi, %rsi, %rdx, %rcx
547 * copy a string from from to to, stop when a 0 character is reached.
548 * return ENAMETOOLONG if string is longer than maxlen, and
549 * EFAULT on protection violations. If lencopied is non-zero,
550 * return the actual length in *lencopied.
553 movq %rdx,%r8 /* %r8 = maxlen */
554 movq %rcx,%r9 /* %r9 = *len */
555 xchgq %rdi,%rsi /* %rdi = from, %rsi = to */
556 movq PCPU(curthread),%rcx
557 movq TD_PCB(%rcx), %rcx
558 movq $cpystrflt,PCB_ONFAULT(%rcx)
560 movq $VM_MAX_USER_ADDRESS,%rax
562 /* make sure 'from' is within bounds */
566 /* restrict maxlen to <= VM_MAX_USER_ADDRESS-from */
584 /* Success -- 0 byte reached */
589 /* rdx is zero - return ENAMETOOLONG or EFAULT */
590 movq $VM_MAX_USER_ADDRESS,%rax
594 movq $ENAMETOOLONG,%rax
601 /* set *lencopied and return %eax */
602 movq PCPU(curthread),%rcx
603 movq TD_PCB(%rcx), %rcx
604 movq $0,PCB_ONFAULT(%rcx)
615 * copystr(from, to, maxlen, int *lencopied) - MP SAFE
616 * %rdi, %rsi, %rdx, %rcx
619 movq %rdx,%r8 /* %r8 = maxlen */
632 /* Success -- 0 byte reached */
637 /* rdx is zero -- return ENAMETOOLONG */
638 movq $ENAMETOOLONG,%rax
644 /* set *lencopied and return %rax */
651 * Handling of special x86_64 registers and descriptor tables etc
654 /* void lgdt(struct region_descriptor *rdp); */
656 /* reload the descriptor table */
659 /* flush the prefetch q */
666 movl %eax,%fs /* Beware, use wrmsr to set 64 bit base */
667 movl %eax,%gs /* Beware, use wrmsr to set 64 bit base */
670 /* reload code selector by turning return into intersegmental return */
677 /*****************************************************************************/
678 /* setjump, longjump */
679 /*****************************************************************************/
682 movq %rbx,0(%rdi) /* save rbx */
683 movq %rsp,8(%rdi) /* save rsp */
684 movq %rbp,16(%rdi) /* save rbp */
685 movq %r12,24(%rdi) /* save r12 */
686 movq %r13,32(%rdi) /* save r13 */
687 movq %r14,40(%rdi) /* save r14 */
688 movq %r15,48(%rdi) /* save r15 */
689 movq 0(%rsp),%rdx /* get rta */
690 movq %rdx,56(%rdi) /* save rip */
691 xorl %eax,%eax /* return(0); */
695 movq 0(%rdi),%rbx /* restore rbx */
696 movq 8(%rdi),%rsp /* restore rsp */
697 movq 16(%rdi),%rbp /* restore rbp */
698 movq 24(%rdi),%r12 /* restore r12 */
699 movq 32(%rdi),%r13 /* restore r13 */
700 movq 40(%rdi),%r14 /* restore r14 */
701 movq 48(%rdi),%r15 /* restore r15 */
702 movq 56(%rdi),%rdx /* get rta */
703 movq %rdx,0(%rsp) /* put in return frame */
704 xorl %eax,%eax /* return(1); */
709 * Support for reading MSRs in the safe manner.
712 /* int rdmsr_safe(u_int msr, uint64_t *data) */
713 movq PCPU(curthread),%r8
714 movq TD_PCB(%r8), %r8
715 movq $msr_onfault,PCB_ONFAULT(%r8)
717 rdmsr /* Read MSR pointed by %ecx. Returns
718 hi byte in edx, lo in %eax */
719 salq $32,%rdx /* sign-shift %rdx left */
720 movl %eax,%eax /* zero-extend %eax -> %rax */
724 movq %rax,PCB_ONFAULT(%r8)
728 * MSR operations fault handler
732 movq PCPU(curthread),%r8
733 movq TD_PCB(%r8), %r8
734 movq $0,PCB_ONFAULT(%r8)
739 * Support for BB-profiling (gcc -a). The kernbb program will extract
740 * the data from the kernel.
750 NON_GPROF_ENTRY(__bb_init_func)