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)
79 movnti %rax,(%rdi,%rdx)
80 movnti %rax,8(%rdi,%rdx)
81 movnti %rax,16(%rdi,%rdx)
82 movnti %rax,24(%rdi,%rdx)
91 cld /* compare forwards */
106 * bcopy(src, dst, cnt)
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 remainder by 32-bit words */
149 * Note: memcpy does not support overlapping copies
153 shrq $3,%rcx /* copy by 64-bit words */
154 cld /* copy forwards */
158 andq $7,%rcx /* any bytes left? */
164 * pagecopy(%rdi=from, %rsi=to)
167 movq $-PAGE_SIZE,%rax
172 prefetchnta (%rdi,%rax)
176 movq (%rdi,%rdx),%rax
177 movnti %rax,(%rsi,%rdx)
178 movq 8(%rdi,%rdx),%rax
179 movnti %rax,8(%rsi,%rdx)
180 movq 16(%rdi,%rdx),%rax
181 movnti %rax,16(%rsi,%rdx)
182 movq 24(%rdi,%rdx),%rax
183 movnti %rax,24(%rsi,%rdx)
189 /* fillw(pat, base, cnt) */
190 /* %rdi,%rsi, %rdx */
200 /*****************************************************************************/
201 /* copyout and fubyte family */
202 /*****************************************************************************/
204 * Access user memory from inside the kernel. These routines should be
205 * the only places that do this.
207 * These routines set curpcb->onfault for the time they execute. When a
208 * protection violation occurs inside the functions, the trap handler
209 * returns to *curpcb->onfault instead of the function.
213 * copyout(from_kernel, to_user, len) - MP SAFE
217 movq PCPU(CURPCB),%rax
218 movq $copyout_fault,PCB_ONFAULT(%rax)
219 testq %rdx,%rdx /* anything to do? */
223 * Check explicitly for non-user addresses. If 486 write protection
224 * is being used, this check is essential because we are in kernel
225 * mode so the h/w does not provide any protection against writing
230 * First, prevent address wrapping.
236 * XXX STOP USING VM_MAXUSER_ADDRESS.
237 * It is an end address, not a max, so every time it is used correctly it
238 * looks like there is an off by one error, and of course it caused an off
239 * by one error in several places.
241 movq $VM_MAXUSER_ADDRESS,%rcx
246 /* bcopy(%rsi, %rdi, %rdx) */
260 movq PCPU(CURPCB),%rdx
261 movq %rax,PCB_ONFAULT(%rdx)
266 movq PCPU(CURPCB),%rdx
267 movq $0,PCB_ONFAULT(%rdx)
272 * copyin(from_user, to_kernel, len) - MP SAFE
276 movq PCPU(CURPCB),%rax
277 movq $copyin_fault,PCB_ONFAULT(%rax)
278 testq %rdx,%rdx /* anything to do? */
282 * make sure address is valid
287 movq $VM_MAXUSER_ADDRESS,%rcx
294 shrq $3,%rcx /* copy longword-wise */
299 andb $7,%cl /* copy remaining bytes */
305 movq PCPU(CURPCB),%rdx
306 movq %rax,PCB_ONFAULT(%rdx)
311 movq PCPU(CURPCB),%rdx
312 movq $0,PCB_ONFAULT(%rdx)
317 * casuword32. Compare and set user integer. Returns -1 or the current value.
318 * dst = %rdi, old = %rsi, new = %rdx
321 movq PCPU(CURPCB),%rcx
322 movq $fusufault,PCB_ONFAULT(%rcx)
324 movq $VM_MAXUSER_ADDRESS-4,%rax
325 cmpq %rax,%rdi /* verify address is valid */
328 movl %esi,%eax /* old */
332 cmpxchgl %edx,(%rdi) /* new = %edx */
335 * The old value is in %eax. If the store succeeded it will be the
336 * value we expected (old) from before the store, otherwise it will
337 * be the current value.
340 movq PCPU(CURPCB),%rcx
341 movq $0,PCB_ONFAULT(%rcx)
345 * casuword. Compare and set user word. Returns -1 or the current value.
346 * dst = %rdi, old = %rsi, new = %rdx
349 movq PCPU(CURPCB),%rcx
350 movq $fusufault,PCB_ONFAULT(%rcx)
352 movq $VM_MAXUSER_ADDRESS-4,%rax
353 cmpq %rax,%rdi /* verify address is valid */
356 movq %rsi, %rax /* old */
360 cmpxchgq %rdx, (%rdi) /* new = %rdx */
363 * The old value is in %eax. If the store succeeded it will be the
364 * value we expected (old) from before the store, otherwise it will
365 * be the current value.
368 movq PCPU(CURPCB),%rcx
369 movq $fusufault,PCB_ONFAULT(%rcx)
370 movq $0,PCB_ONFAULT(%rcx)
374 * Fetch (load) a 64-bit word, a 32-bit word, a 16-bit word, or an 8-bit
375 * byte from user memory. All these functions are MPSAFE.
381 movq PCPU(CURPCB),%rcx
382 movq $fusufault,PCB_ONFAULT(%rcx)
384 movq $VM_MAXUSER_ADDRESS-8,%rax
385 cmpq %rax,%rdi /* verify address is valid */
389 movq $0,PCB_ONFAULT(%rcx)
393 movq PCPU(CURPCB),%rcx
394 movq $fusufault,PCB_ONFAULT(%rcx)
396 movq $VM_MAXUSER_ADDRESS-4,%rax
397 cmpq %rax,%rdi /* verify address is valid */
401 movq $0,PCB_ONFAULT(%rcx)
405 * fuswintr() and suswintr() are specialized variants of fuword16() and
406 * suword16(), respectively. They are called from the profiling code,
407 * potentially at interrupt time. If they fail, that's okay; good things
408 * will happen later. They always fail for now, until the trap code is
409 * able to deal with this.
417 movq PCPU(CURPCB),%rcx
418 movq $fusufault,PCB_ONFAULT(%rcx)
420 movq $VM_MAXUSER_ADDRESS-2,%rax
425 movq $0,PCB_ONFAULT(%rcx)
429 movq PCPU(CURPCB),%rcx
430 movq $fusufault,PCB_ONFAULT(%rcx)
432 movq $VM_MAXUSER_ADDRESS-1,%rax
437 movq $0,PCB_ONFAULT(%rcx)
442 movq PCPU(CURPCB),%rcx
444 movq %rax,PCB_ONFAULT(%rcx)
449 * Store a 64-bit word, a 32-bit word, a 16-bit word, or an 8-bit byte to
450 * user memory. All these functions are MPSAFE.
451 * addr = %rdi, value = %rsi
455 movq PCPU(CURPCB),%rcx
456 movq $fusufault,PCB_ONFAULT(%rcx)
458 movq $VM_MAXUSER_ADDRESS-8,%rax
459 cmpq %rax,%rdi /* verify address validity */
464 movq PCPU(CURPCB),%rcx
465 movq %rax,PCB_ONFAULT(%rcx)
469 movq PCPU(CURPCB),%rcx
470 movq $fusufault,PCB_ONFAULT(%rcx)
472 movq $VM_MAXUSER_ADDRESS-4,%rax
473 cmpq %rax,%rdi /* verify address validity */
478 movq PCPU(CURPCB),%rcx
479 movq %rax,PCB_ONFAULT(%rcx)
483 movq PCPU(CURPCB),%rcx
484 movq $fusufault,PCB_ONFAULT(%rcx)
486 movq $VM_MAXUSER_ADDRESS-2,%rax
487 cmpq %rax,%rdi /* verify address validity */
492 movq PCPU(CURPCB),%rcx /* restore trashed register */
493 movq %rax,PCB_ONFAULT(%rcx)
497 movq PCPU(CURPCB),%rcx
498 movq $fusufault,PCB_ONFAULT(%rcx)
500 movq $VM_MAXUSER_ADDRESS-1,%rax
501 cmpq %rax,%rdi /* verify address validity */
507 movq PCPU(CURPCB),%rcx /* restore trashed register */
508 movq %rax,PCB_ONFAULT(%rcx)
512 * copyinstr(from, to, maxlen, int *lencopied) - MP SAFE
513 * %rdi, %rsi, %rdx, %rcx
515 * copy a string from from to to, stop when a 0 character is reached.
516 * return ENAMETOOLONG if string is longer than maxlen, and
517 * EFAULT on protection violations. If lencopied is non-zero,
518 * return the actual length in *lencopied.
521 movq %rdx, %r8 /* %r8 = maxlen */
522 movq %rcx, %r9 /* %r9 = *len */
523 xchgq %rdi, %rsi /* %rdi = from, %rsi = to */
524 movq PCPU(CURPCB),%rcx
525 movq $cpystrflt,PCB_ONFAULT(%rcx)
527 movq $VM_MAXUSER_ADDRESS,%rax
529 /* make sure 'from' is within bounds */
533 /* restrict maxlen to <= VM_MAXUSER_ADDRESS-from */
551 /* Success -- 0 byte reached */
556 /* rdx is zero - return ENAMETOOLONG or EFAULT */
557 movq $VM_MAXUSER_ADDRESS,%rax
561 movq $ENAMETOOLONG,%rax
568 /* set *lencopied and return %eax */
569 movq PCPU(CURPCB),%rcx
570 movq $0,PCB_ONFAULT(%rcx)
581 * copystr(from, to, maxlen, int *lencopied) - MP SAFE
582 * %rdi, %rsi, %rdx, %rcx
585 movq %rdx, %r8 /* %r8 = maxlen */
598 /* Success -- 0 byte reached */
603 /* rdx is zero -- return ENAMETOOLONG */
604 movq $ENAMETOOLONG,%rax
610 /* set *lencopied and return %rax */
617 * Handling of special amd64 registers and descriptor tables etc
620 /* void lgdt(struct region_descriptor *rdp); */
622 /* reload the descriptor table */
625 /* flush the prefetch q */
632 movl %eax,%fs /* Beware, use wrmsr to set 64 bit base */
636 /* reload code selector by turning return into intersegmental return */
643 /*****************************************************************************/
644 /* setjump, longjump */
645 /*****************************************************************************/
648 movq %rbx,0(%rdi) /* save rbx */
649 movq %rsp,8(%rdi) /* save rsp */
650 movq %rbp,16(%rdi) /* save rbp */
651 movq %r12,24(%rdi) /* save r12 */
652 movq %r13,32(%rdi) /* save r13 */
653 movq %r14,40(%rdi) /* save r14 */
654 movq %r15,48(%rdi) /* save r15 */
655 movq 0(%rsp),%rdx /* get rta */
656 movq %rdx,56(%rdi) /* save rip */
657 xorl %eax,%eax /* return(0); */
661 movq 0(%rdi),%rbx /* restore rbx */
662 movq 8(%rdi),%rsp /* restore rsp */
663 movq 16(%rdi),%rbp /* restore rbp */
664 movq 24(%rdi),%r12 /* restore r12 */
665 movq 32(%rdi),%r13 /* restore r13 */
666 movq 40(%rdi),%r14 /* restore r14 */
667 movq 48(%rdi),%r15 /* restore r15 */
668 movq 56(%rdi),%rdx /* get rta */
669 movq %rdx,0(%rsp) /* put in return frame */
670 xorl %eax,%eax /* return(1); */
675 * Support for BB-profiling (gcc -a). The kernbb program will extract
676 * the data from the kernel.
686 NON_GPROF_ENTRY(__bb_init_func)