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 * casuptr. Compare and set user pointer. 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 movq %rsi, %rax /* old */
332 cmpxchgq %rdx, (%rdi) /* new = %rdx */
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 $fusufault,PCB_ONFAULT(%rcx)
342 movq $0,PCB_ONFAULT(%rcx)
346 * Fetch (load) a 64-bit word, a 32-bit word, a 16-bit word, or an 8-bit
347 * byte from user memory. All these functions are MPSAFE.
353 movq PCPU(CURPCB),%rcx
354 movq $fusufault,PCB_ONFAULT(%rcx)
356 movq $VM_MAXUSER_ADDRESS-8,%rax
357 cmpq %rax,%rdi /* verify address is valid */
361 movq $0,PCB_ONFAULT(%rcx)
365 movq PCPU(CURPCB),%rcx
366 movq $fusufault,PCB_ONFAULT(%rcx)
368 movq $VM_MAXUSER_ADDRESS-4,%rax
369 cmpq %rax,%rdi /* verify address is valid */
373 movq $0,PCB_ONFAULT(%rcx)
377 * fuswintr() and suswintr() are specialized variants of fuword16() and
378 * suword16(), respectively. They are called from the profiling code,
379 * potentially at interrupt time. If they fail, that's okay; good things
380 * will happen later. They always fail for now, until the trap code is
381 * able to deal with this.
389 movq PCPU(CURPCB),%rcx
390 movq $fusufault,PCB_ONFAULT(%rcx)
392 movq $VM_MAXUSER_ADDRESS-2,%rax
397 movq $0,PCB_ONFAULT(%rcx)
401 movq PCPU(CURPCB),%rcx
402 movq $fusufault,PCB_ONFAULT(%rcx)
404 movq $VM_MAXUSER_ADDRESS-1,%rax
409 movq $0,PCB_ONFAULT(%rcx)
414 movq PCPU(CURPCB),%rcx
416 movq %rax,PCB_ONFAULT(%rcx)
421 * Store a 64-bit word, a 32-bit word, a 16-bit word, or an 8-bit byte to
422 * user memory. All these functions are MPSAFE.
423 * addr = %rdi, value = %rsi
427 movq PCPU(CURPCB),%rcx
428 movq $fusufault,PCB_ONFAULT(%rcx)
430 movq $VM_MAXUSER_ADDRESS-8,%rax
431 cmpq %rax,%rdi /* verify address validity */
436 movq PCPU(CURPCB),%rcx
437 movq %rax,PCB_ONFAULT(%rcx)
441 movq PCPU(CURPCB),%rcx
442 movq $fusufault,PCB_ONFAULT(%rcx)
444 movq $VM_MAXUSER_ADDRESS-4,%rax
445 cmpq %rax,%rdi /* verify address validity */
450 movq PCPU(CURPCB),%rcx
451 movq %rax,PCB_ONFAULT(%rcx)
455 movq PCPU(CURPCB),%rcx
456 movq $fusufault,PCB_ONFAULT(%rcx)
458 movq $VM_MAXUSER_ADDRESS-2,%rax
459 cmpq %rax,%rdi /* verify address validity */
464 movq PCPU(CURPCB),%rcx /* restore trashed register */
465 movq %rax,PCB_ONFAULT(%rcx)
469 movq PCPU(CURPCB),%rcx
470 movq $fusufault,PCB_ONFAULT(%rcx)
472 movq $VM_MAXUSER_ADDRESS-1,%rax
473 cmpq %rax,%rdi /* verify address validity */
479 movq PCPU(CURPCB),%rcx /* restore trashed register */
480 movq %rax,PCB_ONFAULT(%rcx)
484 * copyinstr(from, to, maxlen, int *lencopied) - MP SAFE
485 * %rdi, %rsi, %rdx, %rcx
487 * copy a string from from to to, stop when a 0 character is reached.
488 * return ENAMETOOLONG if string is longer than maxlen, and
489 * EFAULT on protection violations. If lencopied is non-zero,
490 * return the actual length in *lencopied.
493 movq %rdx, %r8 /* %r8 = maxlen */
494 movq %rcx, %r9 /* %r9 = *len */
495 xchgq %rdi, %rsi /* %rdi = from, %rsi = to */
496 movq PCPU(CURPCB),%rcx
497 movq $cpystrflt,PCB_ONFAULT(%rcx)
499 movq $VM_MAXUSER_ADDRESS,%rax
501 /* make sure 'from' is within bounds */
505 /* restrict maxlen to <= VM_MAXUSER_ADDRESS-from */
523 /* Success -- 0 byte reached */
528 /* rdx is zero - return ENAMETOOLONG or EFAULT */
529 movq $VM_MAXUSER_ADDRESS,%rax
533 movq $ENAMETOOLONG,%rax
540 /* set *lencopied and return %eax */
541 movq PCPU(CURPCB),%rcx
542 movq $0,PCB_ONFAULT(%rcx)
553 * copystr(from, to, maxlen, int *lencopied) - MP SAFE
554 * %rdi, %rsi, %rdx, %rcx
557 movq %rdx, %r8 /* %r8 = maxlen */
570 /* Success -- 0 byte reached */
575 /* rdx is zero -- return ENAMETOOLONG */
576 movq $ENAMETOOLONG,%rax
582 /* set *lencopied and return %rax */
589 * Handling of special amd64 registers and descriptor tables etc
592 /* void lgdt(struct region_descriptor *rdp); */
594 /* reload the descriptor table */
597 /* flush the prefetch q */
604 movl %eax,%fs /* Beware, use wrmsr to set 64 bit base */
608 /* reload code selector by turning return into intersegmental return */
615 /*****************************************************************************/
616 /* setjump, longjump */
617 /*****************************************************************************/
620 movq %rbx,0(%rdi) /* save rbx */
621 movq %rsp,8(%rdi) /* save rsp */
622 movq %rbp,16(%rdi) /* save rbp */
623 movq %r12,24(%rdi) /* save r12 */
624 movq %r13,32(%rdi) /* save r13 */
625 movq %r14,40(%rdi) /* save r14 */
626 movq %r15,48(%rdi) /* save r15 */
627 movq 0(%rsp),%rdx /* get rta */
628 movq %rdx,56(%rdi) /* save rip */
629 xorl %eax,%eax /* return(0); */
633 movq 0(%rdi),%rbx /* restore rbx */
634 movq 8(%rdi),%rsp /* restore rsp */
635 movq 16(%rdi),%rbp /* restore rbp */
636 movq 24(%rdi),%r12 /* restore r12 */
637 movq 32(%rdi),%r13 /* restore r13 */
638 movq 40(%rdi),%r14 /* restore r14 */
639 movq 48(%rdi),%r15 /* restore r15 */
640 movq 56(%rdi),%rdx /* get rta */
641 movq %rdx,0(%rsp) /* put in return frame */
642 xorl %eax,%eax /* return(1); */
647 * Support for BB-profiling (gcc -a). The kernbb program will extract
648 * the data from the kernel.
658 NON_GPROF_ENTRY(__bb_init_func)