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 $
34 #include <machine/asmacros.h>
35 #include <machine/pmap.h>
45 * void bzero(void *buf, size_t len)
68 movq %rax,(%rdi,%rdx) /* movnti */
69 movq %rax,8(%rdi,%rdx) /* movnti */
70 movq %rax,16(%rdi,%rdx) /* movnti */
71 movq %rax,24(%rdi,%rdx) /* movnti */
80 cld /* compare forwards */
95 * bcopy(src, dst, cnt)
97 * ws@tools.de (Wolfgang Solfrank, TooLs GmbH) +49-228-985800
99 ENTRY(generic_bcopy) /* generic_bcopy is bcopy without FPU */
100 ENTRY(ovbcopy) /* our bcopy doesn't use the FPU, so ovbcopy is the same */
107 cmpq %rcx,%rax /* overlapping && src < dst? */
110 shrq $3,%rcx /* copy by 64-bit words */
111 cld /* nope, copy forwards */
115 andq $7,%rcx /* any bytes left? */
122 addq %rcx,%rdi /* copy backwards */
126 andq $7,%rcx /* any fractional bytes? */
130 movq %rdx,%rcx /* copy remainder by 32-bit words */
139 movq $0x200,%rax /* the manual says that bit 10 must be set to 1 */
140 movq %rax,%dr7 /* disable all breapoints first */
150 * Note: memcpy does not support overlapping copies
154 shrq $3,%rcx /* copy by 64-bit words */
155 cld /* copy forwards */
159 andq $7,%rcx /* any bytes left? */
165 * pagecopy(%rdi=from, %rsi=to)
168 movq $-PAGE_SIZE,%rax
173 /*prefetchnta (%rdi,%rax)*/
177 movq (%rdi,%rdx),%rax
178 movq %rax,(%rsi,%rdx) /* movnti */
179 movq 8(%rdi,%rdx),%rax
180 movq %rax,8(%rsi,%rdx) /* movnti */
181 movq 16(%rdi,%rdx),%rax
182 movq %rax,16(%rsi,%rdx) /* movnti */
183 movq 24(%rdi,%rdx),%rax
184 movq %rax,24(%rsi,%rdx) /* movnti */
190 /* fillw(pat, base, cnt) */
191 /* %rdi,%rsi, %rdx */
201 /*****************************************************************************/
202 /* copyout and fubyte family */
203 /*****************************************************************************/
205 * Access user memory from inside the kernel. These routines should be
206 * the only places that do this.
208 * These routines set curpcb->onfault for the time they execute. When a
209 * protection violation occurs inside the functions, the trap handler
210 * returns to *curpcb->onfault instead of the function.
214 * copyout(from_kernel, to_user, len) - MP SAFE
218 movq PCPU(curthread),%rax
219 movq TD_PCB(%rax), %rax
220 movq $copyout_fault,PCB_ONFAULT(%rax)
221 movq %rsp,PCB_ONFAULT_SP(%rax)
222 testq %rdx,%rdx /* anything to do? */
226 * Check explicitly for non-user addresses. If 486 write protection
227 * is being used, this check is essential because we are in kernel
228 * mode so the h/w does not provide any protection against writing
233 * First, prevent address wrapping.
239 * XXX STOP USING VM_MAX_USER_ADDRESS.
240 * It is an end address, not a max, so every time it is used correctly it
241 * looks like there is an off by one error, and of course it caused an off
242 * by one error in several places.
244 movq $VM_MAX_USER_ADDRESS,%rcx
249 /* bcopy(%rsi, %rdi, %rdx) */
263 movq PCPU(curthread),%rdx
264 movq TD_PCB(%rdx), %rdx
265 movq %rax,PCB_ONFAULT(%rdx)
270 movq PCPU(curthread),%rdx
271 movq TD_PCB(%rdx), %rdx
272 movq $0,PCB_ONFAULT(%rdx)
277 * copyin(from_user, to_kernel, len) - MP SAFE
281 movq PCPU(curthread),%rax
282 movq TD_PCB(%rax), %rax
283 movq $copyin_fault,PCB_ONFAULT(%rax)
284 movq %rsp,PCB_ONFAULT_SP(%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)
333 movq %rsp,PCB_ONFAULT_SP(%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)
364 movq %rsp,PCB_ONFAULT_SP(%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 $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)
398 movq %rsp,PCB_ONFAULT_SP(%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)
412 movq %rsp,PCB_ONFAULT_SP(%rcx)
414 movq $VM_MAX_USER_ADDRESS-4,%rax
415 cmpq %rax,%rdi /* verify address is valid */
419 movq $0,PCB_ONFAULT(%rcx)
423 * fuswintr() and suswintr() are specialized variants of fuword16() and
424 * suword16(), respectively. They are called from the profiling code,
425 * potentially at interrupt time. If they fail, that's okay; good things
426 * will happen later. They always fail for now, until the trap code is
427 * able to deal with this.
435 movq PCPU(curthread),%rcx
436 movq TD_PCB(%rcx), %rcx
437 movq $fusufault,PCB_ONFAULT(%rcx)
438 movq %rsp,PCB_ONFAULT_SP(%rcx)
440 movq $VM_MAX_USER_ADDRESS-2,%rax
445 movq $0,PCB_ONFAULT(%rcx)
449 movq PCPU(curthread),%rcx
450 movq TD_PCB(%rcx), %rcx
451 movq $fusufault,PCB_ONFAULT(%rcx)
452 movq %rsp,PCB_ONFAULT_SP(%rcx)
454 movq $VM_MAX_USER_ADDRESS-1,%rax
459 movq $0,PCB_ONFAULT(%rcx)
464 movq PCPU(curthread),%rcx
466 movq TD_PCB(%rcx), %rcx
467 movq %rax,PCB_ONFAULT(%rcx)
472 * Store a 64-bit word, a 32-bit word, a 16-bit word, or an 8-bit byte to
473 * user memory. All these functions are MPSAFE.
475 * addr = %rdi, value = %rsi
481 movq PCPU(curthread),%rcx
482 movq TD_PCB(%rcx), %rcx
483 movq $fusufault,PCB_ONFAULT(%rcx)
484 movq %rsp,PCB_ONFAULT_SP(%rcx)
486 movq $VM_MAX_USER_ADDRESS-8,%rax
487 cmpq %rax,%rdi /* verify address validity */
492 movq PCPU(curthread),%rcx
493 movq TD_PCB(%rcx), %rcx
494 movq %rax,PCB_ONFAULT(%rcx)
501 movq PCPU(curthread),%rcx
502 movq TD_PCB(%rcx), %rcx
503 movq $fusufault,PCB_ONFAULT(%rcx)
504 movq %rsp,PCB_ONFAULT_SP(%rcx)
506 movq $VM_MAX_USER_ADDRESS-4,%rax
507 cmpq %rax,%rdi /* verify address validity */
512 movq PCPU(curthread),%rcx
513 movq TD_PCB(%rcx), %rcx
514 movq %rax,PCB_ONFAULT(%rcx)
518 movq PCPU(curthread),%rcx
519 movq TD_PCB(%rcx), %rcx
520 movq $fusufault,PCB_ONFAULT(%rcx)
521 movq %rsp,PCB_ONFAULT_SP(%rcx)
523 movq $VM_MAX_USER_ADDRESS-2,%rax
524 cmpq %rax,%rdi /* verify address validity */
529 movq PCPU(curthread),%rcx /* restore trashed register */
530 movq TD_PCB(%rcx), %rcx
531 movq %rax,PCB_ONFAULT(%rcx)
535 movq PCPU(curthread),%rcx
536 movq TD_PCB(%rcx), %rcx
537 movq $fusufault,PCB_ONFAULT(%rcx)
538 movq %rsp,PCB_ONFAULT_SP(%rcx)
540 movq $VM_MAX_USER_ADDRESS-1,%rax
541 cmpq %rax,%rdi /* verify address validity */
547 movq PCPU(curthread),%rcx /* restore trashed register */
548 movq TD_PCB(%rcx), %rcx
549 movq %rax,PCB_ONFAULT(%rcx)
553 * copyinstr(from, to, maxlen, int *lencopied) - MP SAFE
554 * %rdi, %rsi, %rdx, %rcx
556 * copy a string from from to to, stop when a 0 character is reached.
557 * return ENAMETOOLONG if string is longer than maxlen, and
558 * EFAULT on protection violations. If lencopied is non-zero,
559 * return the actual length in *lencopied.
562 movq %rdx,%r8 /* %r8 = maxlen */
563 movq %rcx,%r9 /* %r9 = *len */
564 xchgq %rdi,%rsi /* %rdi = from, %rsi = to */
565 movq PCPU(curthread),%rcx
566 movq TD_PCB(%rcx), %rcx
567 movq $cpystrflt,PCB_ONFAULT(%rcx)
568 movq %rsp,PCB_ONFAULT_SP(%rcx)
570 movq $VM_MAX_USER_ADDRESS,%rax
572 /* make sure 'from' is within bounds */
576 /* restrict maxlen to <= VM_MAX_USER_ADDRESS-from */
594 /* Success -- 0 byte reached */
599 /* rdx is zero - return ENAMETOOLONG or EFAULT */
600 movq $VM_MAX_USER_ADDRESS,%rax
604 movq $ENAMETOOLONG,%rax
611 /* set *lencopied and return %eax */
612 movq PCPU(curthread),%rcx
613 movq TD_PCB(%rcx), %rcx
614 movq $0,PCB_ONFAULT(%rcx)
625 * copystr(from, to, maxlen, int *lencopied) - MP SAFE
626 * %rdi, %rsi, %rdx, %rcx
629 movq %rdx,%r8 /* %r8 = maxlen */
642 /* Success -- 0 byte reached */
647 /* rdx is zero -- return ENAMETOOLONG */
648 movq $ENAMETOOLONG,%rax
654 /* set *lencopied and return %rax */
661 * Handling of special x86_64 registers and descriptor tables etc
664 /* void lgdt(struct region_descriptor *rdp); */
666 /* reload the descriptor table */
669 /* flush the prefetch q */
676 movl %eax,%fs /* Beware, use wrmsr to set 64 bit base */
677 movl %eax,%gs /* Beware, use wrmsr to set 64 bit base */
680 /* reload code selector by turning return into intersegmental return */
687 /*****************************************************************************/
688 /* setjump, longjump */
689 /*****************************************************************************/
692 movq %rbx,0(%rdi) /* save rbx */
693 movq %rsp,8(%rdi) /* save rsp */
694 movq %rbp,16(%rdi) /* save rbp */
695 movq %r12,24(%rdi) /* save r12 */
696 movq %r13,32(%rdi) /* save r13 */
697 movq %r14,40(%rdi) /* save r14 */
698 movq %r15,48(%rdi) /* save r15 */
699 movq 0(%rsp),%rdx /* get rta */
700 movq %rdx,56(%rdi) /* save rip */
701 xorl %eax,%eax /* return(0); */
705 movq 0(%rdi),%rbx /* restore rbx */
706 movq 8(%rdi),%rsp /* restore rsp */
707 movq 16(%rdi),%rbp /* restore rbp */
708 movq 24(%rdi),%r12 /* restore r12 */
709 movq 32(%rdi),%r13 /* restore r13 */
710 movq 40(%rdi),%r14 /* restore r14 */
711 movq 48(%rdi),%r15 /* restore r15 */
712 movq 56(%rdi),%rdx /* get rta */
713 movq %rdx,0(%rsp) /* put in return frame */
714 xorl %eax,%eax /* return(1); */
719 * Support for reading MSRs in the safe manner.
722 /* int rdmsr_safe(u_int msr, uint64_t *data) */
723 movq PCPU(curthread),%r8
724 movq TD_PCB(%r8), %r8
725 movq $msr_onfault,PCB_ONFAULT(%r8)
726 movq %rsp,PCB_ONFAULT_SP(%r8)
728 rdmsr /* Read MSR pointed by %ecx. Returns
729 hi byte in edx, lo in %eax */
730 salq $32,%rdx /* sign-shift %rdx left */
731 movl %eax,%eax /* zero-extend %eax -> %rax */
735 movq %rax,PCB_ONFAULT(%r8)
739 * MSR operations fault handler
743 movq PCPU(curthread),%r8
744 movq TD_PCB(%r8), %r8
745 movq $0,PCB_ONFAULT(%r8)
750 * Support for BB-profiling (gcc -a). The kernbb program will extract
751 * the data from the kernel.
761 NON_GPROF_ENTRY(__bb_init_func)