2 * Copyright (c) 2003 Peter Wemm.
3 * Copyright (c) 1993 The Regents of the University of California.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 4. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <machine/asmacros.h>
36 #include <machine/intr_machdep.h>
37 #include <machine/pmap.h>
42 .globl intrcnt, eintrcnt
44 .space INTRCNT_COUNT * 8
47 .globl intrnames, eintrnames
49 .space INTRCNT_COUNT * (MAXCOMLEN + 1)
56 * void bzero(void *buf, u_int len)
80 movnti %rax,(%rdi,%rdx)
81 movnti %rax,8(%rdi,%rdx)
82 movnti %rax,16(%rdi,%rdx)
83 movnti %rax,24(%rdi,%rdx)
93 cld /* compare forwards */
109 * bcopy(src, dst, cnt)
111 * ws@tools.de (Wolfgang Solfrank, TooLs GmbH) +49-228-985800
119 cmpq %rcx,%rax /* overlapping && src < dst? */
122 shrq $3,%rcx /* copy by 64-bit words */
123 cld /* nope, copy forwards */
127 andq $7,%rcx /* any bytes left? */
134 addq %rcx,%rdi /* copy backwards */
138 andq $7,%rcx /* any fractional bytes? */
142 movq %rdx,%rcx /* copy remainder by 32-bit words */
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? */
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)
195 /* fillw(pat, base, cnt) */
196 /* %rdi,%rsi, %rdx */
207 /*****************************************************************************/
208 /* copyout and fubyte family */
209 /*****************************************************************************/
211 * Access user memory from inside the kernel. These routines should be
212 * the only places that do this.
214 * These routines set curpcb->onfault for the time they execute. When a
215 * protection violation occurs inside the functions, the trap handler
216 * returns to *curpcb->onfault instead of the function.
220 * copyout(from_kernel, to_user, len) - MP SAFE
224 movq PCPU(CURPCB),%rax
225 movq $copyout_fault,PCB_ONFAULT(%rax)
226 testq %rdx,%rdx /* anything to do? */
230 * Check explicitly for non-user addresses. If 486 write protection
231 * is being used, this check is essential because we are in kernel
232 * mode so the h/w does not provide any protection against writing
237 * First, prevent address wrapping.
243 * XXX STOP USING VM_MAXUSER_ADDRESS.
244 * It is an end address, not a max, so every time it is used correctly it
245 * looks like there is an off by one error, and of course it caused an off
246 * by one error in several places.
248 movq $VM_MAXUSER_ADDRESS,%rcx
253 /* bcopy(%rsi, %rdi, %rdx) */
267 movq PCPU(CURPCB),%rdx
268 movq %rax,PCB_ONFAULT(%rdx)
273 movq PCPU(CURPCB),%rdx
274 movq $0,PCB_ONFAULT(%rdx)
280 * copyin(from_user, to_kernel, len) - MP SAFE
284 movq PCPU(CURPCB),%rax
285 movq $copyin_fault,PCB_ONFAULT(%rax)
286 testq %rdx,%rdx /* anything to do? */
290 * make sure address is valid
295 movq $VM_MAXUSER_ADDRESS,%rcx
302 shrq $3,%rcx /* copy longword-wise */
307 andb $7,%cl /* copy remaining bytes */
313 movq PCPU(CURPCB),%rdx
314 movq %rax,PCB_ONFAULT(%rdx)
319 movq PCPU(CURPCB),%rdx
320 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(CURPCB),%rcx
331 movq $fusufault,PCB_ONFAULT(%rcx)
333 movq $VM_MAXUSER_ADDRESS-4,%rax
334 cmpq %rax,%rdi /* verify address is valid */
337 movl %esi,%eax /* old */
341 cmpxchgl %edx,(%rdi) /* new = %edx */
344 * The old value is in %eax. If the store succeeded it will be the
345 * value we expected (old) from before the store, otherwise it will
346 * be the current value.
349 movq PCPU(CURPCB),%rcx
350 movq $0,PCB_ONFAULT(%rcx)
355 * casuword. Compare and set user word. Returns -1 or the current value.
356 * dst = %rdi, old = %rsi, new = %rdx
359 movq PCPU(CURPCB),%rcx
360 movq $fusufault,PCB_ONFAULT(%rcx)
362 movq $VM_MAXUSER_ADDRESS-4,%rax
363 cmpq %rax,%rdi /* verify address is valid */
366 movq %rsi,%rax /* old */
370 cmpxchgq %rdx,(%rdi) /* new = %rdx */
373 * The old value is in %eax. If the store succeeded it will be the
374 * value we expected (old) from before the store, otherwise it will
375 * be the current value.
378 movq PCPU(CURPCB),%rcx
379 movq $fusufault,PCB_ONFAULT(%rcx)
380 movq $0,PCB_ONFAULT(%rcx)
385 * Fetch (load) a 64-bit word, a 32-bit word, a 16-bit word, or an 8-bit
386 * byte from user memory. All these functions are MPSAFE.
392 movq PCPU(CURPCB),%rcx
393 movq $fusufault,PCB_ONFAULT(%rcx)
395 movq $VM_MAXUSER_ADDRESS-8,%rax
396 cmpq %rax,%rdi /* verify address is valid */
400 movq $0,PCB_ONFAULT(%rcx)
406 movq PCPU(CURPCB),%rcx
407 movq $fusufault,PCB_ONFAULT(%rcx)
409 movq $VM_MAXUSER_ADDRESS-4,%rax
410 cmpq %rax,%rdi /* verify address is valid */
414 movq $0,PCB_ONFAULT(%rcx)
419 * fuswintr() and suswintr() are specialized variants of fuword16() and
420 * suword16(), respectively. They are called from the profiling code,
421 * potentially at interrupt time. If they fail, that's okay; good things
422 * will happen later. They always fail for now, until the trap code is
423 * able to deal with this.
433 movq PCPU(CURPCB),%rcx
434 movq $fusufault,PCB_ONFAULT(%rcx)
436 movq $VM_MAXUSER_ADDRESS-2,%rax
441 movq $0,PCB_ONFAULT(%rcx)
446 movq PCPU(CURPCB),%rcx
447 movq $fusufault,PCB_ONFAULT(%rcx)
449 movq $VM_MAXUSER_ADDRESS-1,%rax
454 movq $0,PCB_ONFAULT(%rcx)
460 movq PCPU(CURPCB),%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.
469 * addr = %rdi, value = %rsi
473 movq PCPU(CURPCB),%rcx
474 movq $fusufault,PCB_ONFAULT(%rcx)
476 movq $VM_MAXUSER_ADDRESS-8,%rax
477 cmpq %rax,%rdi /* verify address validity */
482 movq PCPU(CURPCB),%rcx
483 movq %rax,PCB_ONFAULT(%rcx)
489 movq PCPU(CURPCB),%rcx
490 movq $fusufault,PCB_ONFAULT(%rcx)
492 movq $VM_MAXUSER_ADDRESS-4,%rax
493 cmpq %rax,%rdi /* verify address validity */
498 movq PCPU(CURPCB),%rcx
499 movq %rax,PCB_ONFAULT(%rcx)
504 movq PCPU(CURPCB),%rcx
505 movq $fusufault,PCB_ONFAULT(%rcx)
507 movq $VM_MAXUSER_ADDRESS-2,%rax
508 cmpq %rax,%rdi /* verify address validity */
513 movq PCPU(CURPCB),%rcx /* restore trashed register */
514 movq %rax,PCB_ONFAULT(%rcx)
519 movq PCPU(CURPCB),%rcx
520 movq $fusufault,PCB_ONFAULT(%rcx)
522 movq $VM_MAXUSER_ADDRESS-1,%rax
523 cmpq %rax,%rdi /* verify address validity */
529 movq PCPU(CURPCB),%rcx /* restore trashed register */
530 movq %rax,PCB_ONFAULT(%rcx)
535 * copyinstr(from, to, maxlen, int *lencopied) - MP SAFE
536 * %rdi, %rsi, %rdx, %rcx
538 * copy a string from from to to, stop when a 0 character is reached.
539 * return ENAMETOOLONG if string is longer than maxlen, and
540 * EFAULT on protection violations. If lencopied is non-zero,
541 * return the actual length in *lencopied.
544 movq %rdx,%r8 /* %r8 = maxlen */
545 movq %rcx,%r9 /* %r9 = *len */
546 xchgq %rdi,%rsi /* %rdi = from, %rsi = to */
547 movq PCPU(CURPCB),%rcx
548 movq $cpystrflt,PCB_ONFAULT(%rcx)
550 movq $VM_MAXUSER_ADDRESS,%rax
552 /* make sure 'from' is within bounds */
556 /* restrict maxlen to <= VM_MAXUSER_ADDRESS-from */
574 /* Success -- 0 byte reached */
579 /* rdx is zero - return ENAMETOOLONG or EFAULT */
580 movq $VM_MAXUSER_ADDRESS,%rax
584 movq $ENAMETOOLONG,%rax
591 /* set *lencopied and return %eax */
592 movq PCPU(CURPCB),%rcx
593 movq $0,PCB_ONFAULT(%rcx)
604 * copystr(from, to, maxlen, int *lencopied) - MP SAFE
605 * %rdi, %rsi, %rdx, %rcx
608 movq %rdx,%r8 /* %r8 = maxlen */
621 /* Success -- 0 byte reached */
626 /* rdx is zero -- return ENAMETOOLONG */
627 movq $ENAMETOOLONG,%rax
633 /* set *lencopied and return %rax */
641 * Handling of special amd64 registers and descriptor tables etc
644 /* void lgdt(struct region_descriptor *rdp); */
646 /* reload the descriptor table */
649 /* flush the prefetch q */
656 movl %eax,%fs /* Beware, use wrmsr to set 64 bit base */
660 /* reload code selector by turning return into intersegmental return */
668 /*****************************************************************************/
669 /* setjump, longjump */
670 /*****************************************************************************/
673 movq %rbx,0(%rdi) /* save rbx */
674 movq %rsp,8(%rdi) /* save rsp */
675 movq %rbp,16(%rdi) /* save rbp */
676 movq %r12,24(%rdi) /* save r12 */
677 movq %r13,32(%rdi) /* save r13 */
678 movq %r14,40(%rdi) /* save r14 */
679 movq %r15,48(%rdi) /* save r15 */
680 movq 0(%rsp),%rdx /* get rta */
681 movq %rdx,56(%rdi) /* save rip */
682 xorl %eax,%eax /* return(0); */
687 movq 0(%rdi),%rbx /* restore rbx */
688 movq 8(%rdi),%rsp /* restore rsp */
689 movq 16(%rdi),%rbp /* restore rbp */
690 movq 24(%rdi),%r12 /* restore r12 */
691 movq 32(%rdi),%r13 /* restore r13 */
692 movq 40(%rdi),%r14 /* restore r14 */
693 movq 48(%rdi),%r15 /* restore r15 */
694 movq 56(%rdi),%rdx /* get rta */
695 movq %rdx,0(%rsp) /* put in return frame */
696 xorl %eax,%eax /* return(1); */
702 * Support for BB-profiling (gcc -a). The kernbb program will extract
703 * the data from the kernel.
713 NON_GPROF_ENTRY(__bb_init_func)
721 * Support for reading MSRs in the safe manner.
724 /* int rdmsr_safe(u_int msr, uint64_t *data) */
725 movq PCPU(CURPCB),%r8
726 movq $msr_onfault,PCB_ONFAULT(%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 cltq /* sign-extend %eax -> %rax */
735 movq %rax,PCB_ONFAULT(%r8)
739 * Support for writing MSRs in the safe manner.
742 /* int wrmsr_safe(u_int msr, uint64_t data) */
743 movq PCPU(CURPCB),%r8
744 movq $msr_onfault,PCB_ONFAULT(%r8)
749 wrmsr /* Write MSR pointed by %ecx. Accepts
750 hi byte in edx, lo in %eax. */
752 movq %rax,PCB_ONFAULT(%r8)
756 * MSR operations fault handler
760 movq $0,PCB_ONFAULT(%r8)