2 * Copyright (c) 1993 The Regents of the University of California.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <machine/asmacros.h>
35 #include <machine/cputypes.h>
36 #include <machine/intr_machdep.h>
37 #include <machine/pmap.h>
38 #include <machine/specialreg.h>
48 * void bzero(void *buf, u_int len)
134 /* fillw(pat, base, cnt) */
155 cmpl %ecx,%eax /* overlapping && src < dst? */
157 cld /* nope, copy forwards */
166 addl %ecx,%edi /* copy backwards. */
180 * bcopy(src, dst, cnt)
181 * ws@tools.de (Wolfgang Solfrank, TooLs GmbH) +49-228-985800
194 cmpl %ecx,%eax /* overlapping && src < dst? */
197 shrl $2,%ecx /* copy by 32-bit words */
198 cld /* nope, copy forwards */
202 andl $3,%ecx /* any bytes left? */
212 addl %ecx,%edi /* copy backwards */
216 andl $3,%ecx /* any fractional bytes? */
220 movl 16(%ebp),%ecx /* copy remainder by 32-bit words */
234 * Note: memcpy does not support overlapping copies
243 shrl $2,%ecx /* copy by 32-bit words */
244 cld /* nope, copy forwards */
248 andl $3,%ecx /* any bytes left? */
256 /*****************************************************************************/
257 /* copyout and fubyte family */
258 /*****************************************************************************/
260 * Access user memory from inside the kernel. These routines and possibly
261 * the math- and DOS emulators should be the only places that do this.
263 * We have to access the memory with user's permissions, so use a segment
264 * selector with RPL 3. For writes to user space we have to additionally
265 * check the PTE for write permission, because the 386 does not check
266 * write permissions when we are executing with EPL 0. The 486 does check
267 * this if the WP bit is set in CR0, so we can use a simpler version here.
269 * These routines set curpcb->pcb_onfault for the time they execute. When a
270 * protection violation occurs inside the functions, the trap handler
271 * returns to *curpcb->pcb_onfault instead of the function.
275 * copyout(from_kernel, to_user, len) - MP SAFE
278 movl PCPU(CURPCB),%eax
279 movl $copyout_fault,PCB_ONFAULT(%eax)
286 testl %ebx,%ebx /* anything to do? */
290 * Check explicitly for non-user addresses. If 486 write protection
291 * is being used, this check is essential because we are in kernel
292 * mode so the h/w does not provide any protection against writing
297 * First, prevent address wrapping.
303 * XXX STOP USING VM_MAXUSER_ADDRESS.
304 * It is an end address, not a max, so every time it is used correctly it
305 * looks like there is an off by one error, and of course it caused an off
306 * by one error in several places.
308 cmpl $VM_MAXUSER_ADDRESS,%eax
311 /* bcopy(%esi, %edi, %ebx) */
328 movl PCPU(CURPCB),%edx
329 movl %eax,PCB_ONFAULT(%edx)
338 movl PCPU(CURPCB),%edx
339 movl $0,PCB_ONFAULT(%edx)
344 * copyin(from_user, to_kernel, len) - MP SAFE
347 movl PCPU(CURPCB),%eax
348 movl $copyin_fault,PCB_ONFAULT(%eax)
351 movl 12(%esp),%esi /* caddr_t from */
352 movl 16(%esp),%edi /* caddr_t to */
353 movl 20(%esp),%ecx /* size_t len */
356 * make sure address is valid
361 cmpl $VM_MAXUSER_ADDRESS,%edx
365 shrl $2,%ecx /* copy longword-wise */
370 andb $3,%cl /* copy remaining bytes */
377 movl PCPU(CURPCB),%edx
378 movl %eax,PCB_ONFAULT(%edx)
386 movl PCPU(CURPCB),%edx
387 movl $0,PCB_ONFAULT(%edx)
392 * casueword. Compare and set user word. Returns -1 on fault,
393 * 0 on non-faulting access. The current value is in *oldp.
395 ALTENTRY(casueword32)
397 movl PCPU(CURPCB),%ecx
398 movl $fusufault,PCB_ONFAULT(%ecx)
399 movl 4(%esp),%edx /* dst */
400 movl 8(%esp),%eax /* old */
401 movl 16(%esp),%ecx /* new */
403 cmpl $VM_MAXUSER_ADDRESS-4,%edx /* verify address is valid */
409 cmpxchgl %ecx,(%edx) /* Compare and set. */
412 * The old value is in %eax. If the store succeeded it will be the
413 * value we expected (old) from before the store, otherwise it will
414 * be the current value.
417 movl PCPU(CURPCB),%ecx
418 movl $0,PCB_ONFAULT(%ecx)
419 movl 12(%esp),%edx /* oldp */
427 * Fetch (load) a 32-bit word, a 16-bit word, or an 8-bit byte from user
433 movl PCPU(CURPCB),%ecx
434 movl $fusufault,PCB_ONFAULT(%ecx)
435 movl 4(%esp),%edx /* from */
437 cmpl $VM_MAXUSER_ADDRESS-4,%edx /* verify address is valid */
441 movl $0,PCB_ONFAULT(%ecx)
450 * fuswintr() and suswintr() are specialized variants of fuword16() and
451 * suword16(), respectively. They are called from the profiling code,
452 * potentially at interrupt time. If they fail, that's okay; good things
453 * will happen later. They always fail for now, until the trap code is
454 * able to deal with this.
464 movl PCPU(CURPCB),%ecx
465 movl $fusufault,PCB_ONFAULT(%ecx)
468 cmpl $VM_MAXUSER_ADDRESS-2,%edx
472 movl $0,PCB_ONFAULT(%ecx)
477 movl PCPU(CURPCB),%ecx
478 movl $fusufault,PCB_ONFAULT(%ecx)
481 cmpl $VM_MAXUSER_ADDRESS-1,%edx
485 movl $0,PCB_ONFAULT(%ecx)
491 movl PCPU(CURPCB),%ecx
493 movl %eax,PCB_ONFAULT(%ecx)
498 * Store a 32-bit word, a 16-bit word, or an 8-bit byte to user memory.
499 * All these functions are MPSAFE.
504 movl PCPU(CURPCB),%ecx
505 movl $fusufault,PCB_ONFAULT(%ecx)
508 cmpl $VM_MAXUSER_ADDRESS-4,%edx /* verify address validity */
514 movl PCPU(CURPCB),%ecx
515 movl %eax,PCB_ONFAULT(%ecx)
521 movl PCPU(CURPCB),%ecx
522 movl $fusufault,PCB_ONFAULT(%ecx)
525 cmpl $VM_MAXUSER_ADDRESS-2,%edx /* verify address validity */
531 movl PCPU(CURPCB),%ecx /* restore trashed register */
532 movl %eax,PCB_ONFAULT(%ecx)
537 movl PCPU(CURPCB),%ecx
538 movl $fusufault,PCB_ONFAULT(%ecx)
541 cmpl $VM_MAXUSER_ADDRESS-1,%edx /* verify address validity */
547 movl PCPU(CURPCB),%ecx /* restore trashed register */
548 movl %eax,PCB_ONFAULT(%ecx)
553 * copyinstr(from, to, maxlen, int *lencopied) - MP SAFE
555 * copy a string from from to to, stop when a 0 character is reached.
556 * return ENAMETOOLONG if string is longer than maxlen, and
557 * EFAULT on protection violations. If lencopied is non-zero,
558 * return the actual length in *lencopied.
563 movl PCPU(CURPCB),%ecx
564 movl $cpystrflt,PCB_ONFAULT(%ecx)
566 movl 12(%esp),%esi /* %esi = from */
567 movl 16(%esp),%edi /* %edi = to */
568 movl 20(%esp),%edx /* %edx = maxlen */
570 movl $VM_MAXUSER_ADDRESS,%eax
572 /* make sure 'from' is within bounds */
576 /* restrict maxlen to <= VM_MAXUSER_ADDRESS-from */
594 /* Success -- 0 byte reached */
599 /* edx is zero - return ENAMETOOLONG or EFAULT */
600 cmpl $VM_MAXUSER_ADDRESS,%esi
603 movl $ENAMETOOLONG,%eax
610 /* set *lencopied and return %eax */
611 movl PCPU(CURPCB),%ecx
612 movl $0,PCB_ONFAULT(%ecx)
626 * copystr(from, to, maxlen, int *lencopied) - MP SAFE
632 movl 12(%esp),%esi /* %esi = from */
633 movl 16(%esp),%edi /* %edi = to */
634 movl 20(%esp),%edx /* %edx = maxlen */
645 /* Success -- 0 byte reached */
650 /* edx is zero -- return ENAMETOOLONG */
651 movl $ENAMETOOLONG,%eax
654 /* set *lencopied and return %eax */
676 cld /* compare forwards */
694 * Handling of special 386 registers and descriptor tables etc
696 /* void lgdt(struct region_descriptor *rdp); */
699 /* reload the descriptor table */
704 /* flush the prefetch q */
708 /* reload "stale" selectors */
717 /* reload code selector by turning return into intersegmental return */
725 /* ssdtosd(*ssdp,*sdp) */
747 /* void reset_dbregs() */
750 movl %eax,%dr7 /* disable all breakpoints first */
759 /*****************************************************************************/
760 /* setjump, longjump */
761 /*****************************************************************************/
765 movl %ebx,(%eax) /* save ebx */
766 movl %esp,4(%eax) /* save esp */
767 movl %ebp,8(%eax) /* save ebp */
768 movl %esi,12(%eax) /* save esi */
769 movl %edi,16(%eax) /* save edi */
770 movl (%esp),%edx /* get rta */
771 movl %edx,20(%eax) /* save eip */
772 xorl %eax,%eax /* return(0); */
778 movl (%eax),%ebx /* restore ebx */
779 movl 4(%eax),%esp /* restore esp */
780 movl 8(%eax),%ebp /* restore ebp */
781 movl 12(%eax),%esi /* restore esi */
782 movl 16(%eax),%edi /* restore edi */
783 movl 20(%eax),%edx /* get rta */
784 movl %edx,(%esp) /* put in return frame */
785 xorl %eax,%eax /* return(1); */
791 * Support for reading MSRs in the safe manner.
794 /* int rdmsr_safe(u_int msr, uint64_t *data) */
795 movl PCPU(CURPCB),%ecx
796 movl $msr_onfault,PCB_ONFAULT(%ecx)
805 movl PCPU(CURPCB),%ecx
806 movl %eax,PCB_ONFAULT(%ecx)
811 * Support for writing MSRs in the safe manner.
814 /* int wrmsr_safe(u_int msr, uint64_t data) */
815 movl PCPU(CURPCB),%ecx
816 movl $msr_onfault,PCB_ONFAULT(%ecx)
824 movl PCPU(CURPCB),%ecx
825 movl %eax,PCB_ONFAULT(%ecx)
830 * MSR operations fault handler
834 movl PCPU(CURPCB),%ecx
835 movl $0,PCB_ONFAULT(%ecx)