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/pmap.h>
37 #include <machine/specialreg.h>
47 * void bzero(void *buf, u_int len)
74 * The loop takes 14 bytes. Ensure that it doesn't cross a 16-byte
140 /* fillw(pat, base, cnt) */
161 cmpl %ecx,%eax /* overlapping && src < dst? */
163 cld /* nope, copy forwards */
172 addl %ecx,%edi /* copy backwards. */
186 * bcopy(src, dst, cnt)
187 * ws@tools.de (Wolfgang Solfrank, TooLs GmbH) +49-228-985800
200 cmpl %ecx,%eax /* overlapping && src < dst? */
203 shrl $2,%ecx /* copy by 32-bit words */
204 cld /* nope, copy forwards */
208 andl $3,%ecx /* any bytes left? */
218 addl %ecx,%edi /* copy backwards */
222 andl $3,%ecx /* any fractional bytes? */
226 movl 16(%ebp),%ecx /* copy remainder by 32-bit words */
240 * Note: memcpy does not support overlapping copies
249 shrl $2,%ecx /* copy by 32-bit words */
250 cld /* nope, copy forwards */
254 andl $3,%ecx /* any bytes left? */
262 /*****************************************************************************/
263 /* copyout and fubyte family */
264 /*****************************************************************************/
266 * Access user memory from inside the kernel. These routines and possibly
267 * the math- and DOS emulators should be the only places that do this.
269 * We have to access the memory with user's permissions, so use a segment
270 * selector with RPL 3. For writes to user space we have to additionally
271 * check the PTE for write permission, because the 386 does not check
272 * write permissions when we are executing with EPL 0. The 486 does check
273 * this if the WP bit is set in CR0, so we can use a simpler version here.
275 * These routines set curpcb->pcb_onfault for the time they execute. When a
276 * protection violation occurs inside the functions, the trap handler
277 * returns to *curpcb->pcb_onfault instead of the function.
281 * copyout(from_kernel, to_user, len) - MP SAFE
284 movl PCPU(CURPCB),%eax
285 movl $copyout_fault,PCB_ONFAULT(%eax)
292 testl %ebx,%ebx /* anything to do? */
296 * Check explicitly for non-user addresses. If 486 write protection
297 * is being used, this check is essential because we are in kernel
298 * mode so the h/w does not provide any protection against writing
303 * First, prevent address wrapping.
309 * XXX STOP USING VM_MAXUSER_ADDRESS.
310 * It is an end address, not a max, so every time it is used correctly it
311 * looks like there is an off by one error, and of course it caused an off
312 * by one error in several places.
314 cmpl $VM_MAXUSER_ADDRESS,%eax
317 /* bcopy(%esi, %edi, %ebx) */
334 movl PCPU(CURPCB),%edx
335 movl %eax,PCB_ONFAULT(%edx)
344 movl PCPU(CURPCB),%edx
345 movl $0,PCB_ONFAULT(%edx)
350 * copyin(from_user, to_kernel, len) - MP SAFE
353 movl PCPU(CURPCB),%eax
354 movl $copyin_fault,PCB_ONFAULT(%eax)
357 movl 12(%esp),%esi /* caddr_t from */
358 movl 16(%esp),%edi /* caddr_t to */
359 movl 20(%esp),%ecx /* size_t len */
362 * make sure address is valid
367 cmpl $VM_MAXUSER_ADDRESS,%edx
371 shrl $2,%ecx /* copy longword-wise */
376 andb $3,%cl /* copy remaining bytes */
383 movl PCPU(CURPCB),%edx
384 movl %eax,PCB_ONFAULT(%edx)
392 movl PCPU(CURPCB),%edx
393 movl $0,PCB_ONFAULT(%edx)
398 * casueword. Compare and set user word. Returns -1 on fault,
399 * 0 on non-faulting access. The current value is in *oldp.
401 ALTENTRY(casueword32)
403 movl PCPU(CURPCB),%ecx
404 movl $fusufault,PCB_ONFAULT(%ecx)
405 movl 4(%esp),%edx /* dst */
406 movl 8(%esp),%eax /* old */
407 movl 16(%esp),%ecx /* new */
409 cmpl $VM_MAXUSER_ADDRESS-4,%edx /* verify address is valid */
415 cmpxchgl %ecx,(%edx) /* Compare and set. */
418 * The old value is in %eax. If the store succeeded it will be the
419 * value we expected (old) from before the store, otherwise it will
420 * be the current value.
423 movl PCPU(CURPCB),%ecx
424 movl $0,PCB_ONFAULT(%ecx)
425 movl 12(%esp),%edx /* oldp */
433 * Fetch (load) a 32-bit word, a 16-bit word, or an 8-bit byte from user
439 movl PCPU(CURPCB),%ecx
440 movl $fusufault,PCB_ONFAULT(%ecx)
441 movl 4(%esp),%edx /* from */
443 cmpl $VM_MAXUSER_ADDRESS-4,%edx /* verify address is valid */
447 movl $0,PCB_ONFAULT(%ecx)
456 * fuswintr() and suswintr() are specialized variants of fuword16() and
457 * suword16(), respectively. They are called from the profiling code,
458 * potentially at interrupt time. If they fail, that's okay; good things
459 * will happen later. They always fail for now, until the trap code is
460 * able to deal with this.
470 movl PCPU(CURPCB),%ecx
471 movl $fusufault,PCB_ONFAULT(%ecx)
474 cmpl $VM_MAXUSER_ADDRESS-2,%edx
478 movl $0,PCB_ONFAULT(%ecx)
483 movl PCPU(CURPCB),%ecx
484 movl $fusufault,PCB_ONFAULT(%ecx)
487 cmpl $VM_MAXUSER_ADDRESS-1,%edx
491 movl $0,PCB_ONFAULT(%ecx)
497 movl PCPU(CURPCB),%ecx
499 movl %eax,PCB_ONFAULT(%ecx)
504 * Store a 32-bit word, a 16-bit word, or an 8-bit byte to user memory.
505 * All these functions are MPSAFE.
510 movl PCPU(CURPCB),%ecx
511 movl $fusufault,PCB_ONFAULT(%ecx)
514 cmpl $VM_MAXUSER_ADDRESS-4,%edx /* verify address validity */
520 movl PCPU(CURPCB),%ecx
521 movl %eax,PCB_ONFAULT(%ecx)
527 movl PCPU(CURPCB),%ecx
528 movl $fusufault,PCB_ONFAULT(%ecx)
531 cmpl $VM_MAXUSER_ADDRESS-2,%edx /* verify address validity */
537 movl PCPU(CURPCB),%ecx /* restore trashed register */
538 movl %eax,PCB_ONFAULT(%ecx)
543 movl PCPU(CURPCB),%ecx
544 movl $fusufault,PCB_ONFAULT(%ecx)
547 cmpl $VM_MAXUSER_ADDRESS-1,%edx /* verify address validity */
553 movl PCPU(CURPCB),%ecx /* restore trashed register */
554 movl %eax,PCB_ONFAULT(%ecx)
559 * copyinstr(from, to, maxlen, int *lencopied) - MP SAFE
561 * copy a string from from to to, stop when a 0 character is reached.
562 * return ENAMETOOLONG if string is longer than maxlen, and
563 * EFAULT on protection violations. If lencopied is non-zero,
564 * return the actual length in *lencopied.
569 movl PCPU(CURPCB),%ecx
570 movl $cpystrflt,PCB_ONFAULT(%ecx)
572 movl 12(%esp),%esi /* %esi = from */
573 movl 16(%esp),%edi /* %edi = to */
574 movl 20(%esp),%edx /* %edx = maxlen */
576 movl $VM_MAXUSER_ADDRESS,%eax
578 /* make sure 'from' is within bounds */
582 /* restrict maxlen to <= VM_MAXUSER_ADDRESS-from */
600 /* Success -- 0 byte reached */
605 /* edx is zero - return ENAMETOOLONG or EFAULT */
606 cmpl $VM_MAXUSER_ADDRESS,%esi
609 movl $ENAMETOOLONG,%eax
616 /* set *lencopied and return %eax */
617 movl PCPU(CURPCB),%ecx
618 movl $0,PCB_ONFAULT(%ecx)
632 * copystr(from, to, maxlen, int *lencopied) - MP SAFE
638 movl 12(%esp),%esi /* %esi = from */
639 movl 16(%esp),%edi /* %edi = to */
640 movl 20(%esp),%edx /* %edx = maxlen */
651 /* Success -- 0 byte reached */
656 /* edx is zero -- return ENAMETOOLONG */
657 movl $ENAMETOOLONG,%eax
660 /* set *lencopied and return %eax */
682 cld /* compare forwards */
700 * Handling of special 386 registers and descriptor tables etc
702 /* void lgdt(struct region_descriptor *rdp); */
704 /* reload the descriptor table */
708 /* flush the prefetch q */
712 /* reload "stale" selectors */
721 /* reload code selector by turning return into intersegmental return */
729 /* ssdtosd(*ssdp,*sdp) */
751 /* void reset_dbregs() */
754 movl %eax,%dr7 /* disable all breakpoints first */
763 /*****************************************************************************/
764 /* setjump, longjump */
765 /*****************************************************************************/
769 movl %ebx,(%eax) /* save ebx */
770 movl %esp,4(%eax) /* save esp */
771 movl %ebp,8(%eax) /* save ebp */
772 movl %esi,12(%eax) /* save esi */
773 movl %edi,16(%eax) /* save edi */
774 movl (%esp),%edx /* get rta */
775 movl %edx,20(%eax) /* save eip */
776 xorl %eax,%eax /* return(0); */
782 movl (%eax),%ebx /* restore ebx */
783 movl 4(%eax),%esp /* restore esp */
784 movl 8(%eax),%ebp /* restore ebp */
785 movl 12(%eax),%esi /* restore esi */
786 movl 16(%eax),%edi /* restore edi */
787 movl 20(%eax),%edx /* get rta */
788 movl %edx,(%esp) /* put in return frame */
789 xorl %eax,%eax /* return(1); */
795 * Support for reading MSRs in the safe manner.
798 /* int rdmsr_safe(u_int msr, uint64_t *data) */
799 movl PCPU(CURPCB),%ecx
800 movl $msr_onfault,PCB_ONFAULT(%ecx)
809 movl PCPU(CURPCB),%ecx
810 movl %eax,PCB_ONFAULT(%ecx)
815 * Support for writing MSRs in the safe manner.
818 /* int wrmsr_safe(u_int msr, uint64_t data) */
819 movl PCPU(CURPCB),%ecx
820 movl $msr_onfault,PCB_ONFAULT(%ecx)
828 movl PCPU(CURPCB),%ecx
829 movl %eax,PCB_ONFAULT(%ecx)
834 * MSR operations fault handler
838 movl PCPU(CURPCB),%ecx
839 movl $0,PCB_ONFAULT(%ecx)