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>
43 .globl intrcnt, eintrcnt
45 .space INTRCNT_COUNT * 8
48 .globl intrnames, eintrnames
50 .space INTRCNT_COUNT * (MAXCOMLEN + 1)
57 * void bzero(void *buf, u_int len)
81 movnti %rax,(%rdi,%rdx)
82 movnti %rax,8(%rdi,%rdx)
83 movnti %rax,16(%rdi,%rdx)
84 movnti %rax,24(%rdi,%rdx)
94 cld /* compare forwards */
110 * bcopy(src, dst, cnt)
112 * ws@tools.de (Wolfgang Solfrank, TooLs GmbH) +49-228-985800
120 cmpq %rcx,%rax /* overlapping && src < dst? */
123 shrq $3,%rcx /* copy by 64-bit words */
124 cld /* nope, copy forwards */
128 andq $7,%rcx /* any bytes left? */
135 addq %rcx,%rdi /* copy backwards */
139 andq $7,%rcx /* any fractional bytes? */
143 movq %rdx,%rcx /* copy remainder by 32-bit words */
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? */
170 * pagecopy(%rdi=from, %rsi=to)
173 movq $-PAGE_SIZE,%rax
178 prefetchnta (%rdi,%rax)
182 movq (%rdi,%rdx),%rax
183 movnti %rax,(%rsi,%rdx)
184 movq 8(%rdi,%rdx),%rax
185 movnti %rax,8(%rsi,%rdx)
186 movq 16(%rdi,%rdx),%rax
187 movnti %rax,16(%rsi,%rdx)
188 movq 24(%rdi,%rdx),%rax
189 movnti %rax,24(%rsi,%rdx)
196 /* fillw(pat, base, cnt) */
197 /* %rdi,%rsi, %rdx */
208 /*****************************************************************************/
209 /* copyout and fubyte family */
210 /*****************************************************************************/
212 * Access user memory from inside the kernel. These routines should be
213 * the only places that do this.
215 * These routines set curpcb->onfault for the time they execute. When a
216 * protection violation occurs inside the functions, the trap handler
217 * returns to *curpcb->onfault instead of the function.
221 * copyout(from_kernel, to_user, len) - MP SAFE
225 movq PCPU(CURPCB),%rax
226 movq $copyout_fault,PCB_ONFAULT(%rax)
227 testq %rdx,%rdx /* anything to do? */
231 * Check explicitly for non-user addresses. If 486 write protection
232 * is being used, this check is essential because we are in kernel
233 * mode so the h/w does not provide any protection against writing
238 * First, prevent address wrapping.
244 * XXX STOP USING VM_MAXUSER_ADDRESS.
245 * It is an end address, not a max, so every time it is used correctly it
246 * looks like there is an off by one error, and of course it caused an off
247 * by one error in several places.
249 movq $VM_MAXUSER_ADDRESS,%rcx
254 /* bcopy(%rsi, %rdi, %rdx) */
268 movq PCPU(CURPCB),%rdx
269 movq %rax,PCB_ONFAULT(%rdx)
274 movq PCPU(CURPCB),%rdx
275 movq $0,PCB_ONFAULT(%rdx)
281 * copyin(from_user, to_kernel, len) - MP SAFE
285 movq PCPU(CURPCB),%rax
286 movq $copyin_fault,PCB_ONFAULT(%rax)
287 testq %rdx,%rdx /* anything to do? */
291 * make sure address is valid
296 movq $VM_MAXUSER_ADDRESS,%rcx
303 shrq $3,%rcx /* copy longword-wise */
308 andb $7,%cl /* copy remaining bytes */
314 movq PCPU(CURPCB),%rdx
315 movq %rax,PCB_ONFAULT(%rdx)
320 movq PCPU(CURPCB),%rdx
321 movq $0,PCB_ONFAULT(%rdx)
327 * casuword32. Compare and set user integer. Returns -1 or the current value.
328 * dst = %rdi, old = %rsi, new = %rdx
331 movq PCPU(CURPCB),%rcx
332 movq $fusufault,PCB_ONFAULT(%rcx)
334 movq $VM_MAXUSER_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(CURPCB),%rcx
351 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(CURPCB),%rcx
361 movq $fusufault,PCB_ONFAULT(%rcx)
363 movq $VM_MAXUSER_ADDRESS-4,%rax
364 cmpq %rax,%rdi /* verify address is valid */
367 movq %rsi,%rax /* old */
371 cmpxchgq %rdx,(%rdi) /* new = %rdx */
374 * The old value is in %eax. If the store succeeded it will be the
375 * value we expected (old) from before the store, otherwise it will
376 * be the current value.
379 movq PCPU(CURPCB),%rcx
380 movq $fusufault,PCB_ONFAULT(%rcx)
381 movq $0,PCB_ONFAULT(%rcx)
386 * Fetch (load) a 64-bit word, a 32-bit word, a 16-bit word, or an 8-bit
387 * byte from user memory. All these functions are MPSAFE.
393 movq PCPU(CURPCB),%rcx
394 movq $fusufault,PCB_ONFAULT(%rcx)
396 movq $VM_MAXUSER_ADDRESS-8,%rax
397 cmpq %rax,%rdi /* verify address is valid */
401 movq $0,PCB_ONFAULT(%rcx)
407 movq PCPU(CURPCB),%rcx
408 movq $fusufault,PCB_ONFAULT(%rcx)
410 movq $VM_MAXUSER_ADDRESS-4,%rax
411 cmpq %rax,%rdi /* verify address is valid */
415 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.
434 movq PCPU(CURPCB),%rcx
435 movq $fusufault,PCB_ONFAULT(%rcx)
437 movq $VM_MAXUSER_ADDRESS-2,%rax
442 movq $0,PCB_ONFAULT(%rcx)
447 movq PCPU(CURPCB),%rcx
448 movq $fusufault,PCB_ONFAULT(%rcx)
450 movq $VM_MAXUSER_ADDRESS-1,%rax
455 movq $0,PCB_ONFAULT(%rcx)
461 movq PCPU(CURPCB),%rcx
463 movq %rax,PCB_ONFAULT(%rcx)
468 * Store a 64-bit word, a 32-bit word, a 16-bit word, or an 8-bit byte to
469 * user memory. All these functions are MPSAFE.
470 * addr = %rdi, value = %rsi
474 movq PCPU(CURPCB),%rcx
475 movq $fusufault,PCB_ONFAULT(%rcx)
477 movq $VM_MAXUSER_ADDRESS-8,%rax
478 cmpq %rax,%rdi /* verify address validity */
483 movq PCPU(CURPCB),%rcx
484 movq %rax,PCB_ONFAULT(%rcx)
490 movq PCPU(CURPCB),%rcx
491 movq $fusufault,PCB_ONFAULT(%rcx)
493 movq $VM_MAXUSER_ADDRESS-4,%rax
494 cmpq %rax,%rdi /* verify address validity */
499 movq PCPU(CURPCB),%rcx
500 movq %rax,PCB_ONFAULT(%rcx)
505 movq PCPU(CURPCB),%rcx
506 movq $fusufault,PCB_ONFAULT(%rcx)
508 movq $VM_MAXUSER_ADDRESS-2,%rax
509 cmpq %rax,%rdi /* verify address validity */
514 movq PCPU(CURPCB),%rcx /* restore trashed register */
515 movq %rax,PCB_ONFAULT(%rcx)
520 movq PCPU(CURPCB),%rcx
521 movq $fusufault,PCB_ONFAULT(%rcx)
523 movq $VM_MAXUSER_ADDRESS-1,%rax
524 cmpq %rax,%rdi /* verify address validity */
530 movq PCPU(CURPCB),%rcx /* restore trashed register */
531 movq %rax,PCB_ONFAULT(%rcx)
536 * copyinstr(from, to, maxlen, int *lencopied) - MP SAFE
537 * %rdi, %rsi, %rdx, %rcx
539 * copy a string from from to to, stop when a 0 character is reached.
540 * return ENAMETOOLONG if string is longer than maxlen, and
541 * EFAULT on protection violations. If lencopied is non-zero,
542 * return the actual length in *lencopied.
545 movq %rdx,%r8 /* %r8 = maxlen */
546 movq %rcx,%r9 /* %r9 = *len */
547 xchgq %rdi,%rsi /* %rdi = from, %rsi = to */
548 movq PCPU(CURPCB),%rcx
549 movq $cpystrflt,PCB_ONFAULT(%rcx)
551 movq $VM_MAXUSER_ADDRESS,%rax
553 /* make sure 'from' is within bounds */
557 /* restrict maxlen to <= VM_MAXUSER_ADDRESS-from */
575 /* Success -- 0 byte reached */
580 /* rdx is zero - return ENAMETOOLONG or EFAULT */
581 movq $VM_MAXUSER_ADDRESS,%rax
585 movq $ENAMETOOLONG,%rax
592 /* set *lencopied and return %eax */
593 movq PCPU(CURPCB),%rcx
594 movq $0,PCB_ONFAULT(%rcx)
605 * copystr(from, to, maxlen, int *lencopied) - MP SAFE
606 * %rdi, %rsi, %rdx, %rcx
609 movq %rdx,%r8 /* %r8 = maxlen */
622 /* Success -- 0 byte reached */
627 /* rdx is zero -- return ENAMETOOLONG */
628 movq $ENAMETOOLONG,%rax
634 /* set *lencopied and return %rax */
642 * Handling of special amd64 registers and descriptor tables etc
645 /* void lgdt(struct region_descriptor *rdp); */
647 /* reload the descriptor table */
650 /* flush the prefetch q */
657 movl %eax,%fs /* Beware, use wrmsr to set 64 bit base */
661 /* reload code selector by turning return into intersegmental return */
669 /*****************************************************************************/
670 /* setjump, longjump */
671 /*****************************************************************************/
674 movq %rbx,0(%rdi) /* save rbx */
675 movq %rsp,8(%rdi) /* save rsp */
676 movq %rbp,16(%rdi) /* save rbp */
677 movq %r12,24(%rdi) /* save r12 */
678 movq %r13,32(%rdi) /* save r13 */
679 movq %r14,40(%rdi) /* save r14 */
680 movq %r15,48(%rdi) /* save r15 */
681 movq 0(%rsp),%rdx /* get rta */
682 movq %rdx,56(%rdi) /* save rip */
683 xorl %eax,%eax /* return(0); */
688 movq 0(%rdi),%rbx /* restore rbx */
689 movq 8(%rdi),%rsp /* restore rsp */
690 movq 16(%rdi),%rbp /* restore rbp */
691 movq 24(%rdi),%r12 /* restore r12 */
692 movq 32(%rdi),%r13 /* restore r13 */
693 movq 40(%rdi),%r14 /* restore r14 */
694 movq 48(%rdi),%r15 /* restore r15 */
695 movq 56(%rdi),%rdx /* get rta */
696 movq %rdx,0(%rsp) /* put in return frame */
697 xorl %eax,%eax /* return(1); */
703 * Support for BB-profiling (gcc -a). The kernbb program will extract
704 * the data from the kernel.
714 NON_GPROF_ENTRY(__bb_init_func)
722 * Support for reading MSRs in the safe manner.
725 /* int rdmsr_safe(u_int msr, uint64_t *data) */
726 movq PCPU(CURPCB),%r8
727 movq $msr_onfault,PCB_ONFAULT(%r8)
729 rdmsr /* Read MSR pointed by %ecx. Returns
730 hi byte in edx, lo in %eax */
731 salq $32,%rdx /* sign-shift %rdx left */
732 movl %eax,%eax /* zero-extend %eax -> %rax */
736 movq %rax,PCB_ONFAULT(%r8)
740 * Support for writing MSRs in the safe manner.
743 /* int wrmsr_safe(u_int msr, uint64_t data) */
744 movq PCPU(CURPCB),%r8
745 movq $msr_onfault,PCB_ONFAULT(%r8)
750 wrmsr /* Write MSR pointed by %ecx. Accepts
751 hi byte in edx, lo in %eax. */
753 movq %rax,PCB_ONFAULT(%r8)
757 * MSR operations fault handler
761 movq $0,PCB_ONFAULT(%r8)