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)
133 /* fillw(pat, base, cnt) */
154 cmpl %ecx,%eax /* overlapping && src < dst? */
156 cld /* nope, copy forwards */
165 addl %ecx,%edi /* copy backwards. */
179 * bcopy(src, dst, cnt)
180 * ws@tools.de (Wolfgang Solfrank, TooLs GmbH) +49-228-985800
193 cmpl %ecx,%eax /* overlapping && src < dst? */
196 shrl $2,%ecx /* copy by 32-bit words */
197 cld /* nope, copy forwards */
201 andl $3,%ecx /* any bytes left? */
211 addl %ecx,%edi /* copy backwards */
215 andl $3,%ecx /* any fractional bytes? */
219 movl 16(%ebp),%ecx /* copy remainder by 32-bit words */
233 * Note: memcpy does not support overlapping copies
242 shrl $2,%ecx /* copy by 32-bit words */
243 cld /* nope, copy forwards */
247 andl $3,%ecx /* any bytes left? */
255 /*****************************************************************************/
256 /* copyout and fubyte family */
257 /*****************************************************************************/
259 * Access user memory from inside the kernel. These routines and possibly
260 * the math- and DOS emulators should be the only places that do this.
262 * We have to access the memory with user's permissions, so use a segment
263 * selector with RPL 3. For writes to user space we have to additionally
264 * check the PTE for write permission, because the 386 does not check
265 * write permissions when we are executing with EPL 0. The 486 does check
266 * this if the WP bit is set in CR0, so we can use a simpler version here.
268 * These routines set curpcb->pcb_onfault for the time they execute. When a
269 * protection violation occurs inside the functions, the trap handler
270 * returns to *curpcb->pcb_onfault instead of the function.
274 * copyout(from_kernel, to_user, len) - MP SAFE
277 movl PCPU(CURPCB),%eax
278 movl $copyout_fault,PCB_ONFAULT(%eax)
285 testl %ebx,%ebx /* anything to do? */
289 * Check explicitly for non-user addresses. If 486 write protection
290 * is being used, this check is essential because we are in kernel
291 * mode so the h/w does not provide any protection against writing
296 * First, prevent address wrapping.
302 * XXX STOP USING VM_MAXUSER_ADDRESS.
303 * It is an end address, not a max, so every time it is used correctly it
304 * looks like there is an off by one error, and of course it caused an off
305 * by one error in several places.
307 cmpl $VM_MAXUSER_ADDRESS,%eax
310 /* bcopy(%esi, %edi, %ebx) */
327 movl PCPU(CURPCB),%edx
328 movl %eax,PCB_ONFAULT(%edx)
337 movl PCPU(CURPCB),%edx
338 movl $0,PCB_ONFAULT(%edx)
343 * copyin(from_user, to_kernel, len) - MP SAFE
346 movl PCPU(CURPCB),%eax
347 movl $copyin_fault,PCB_ONFAULT(%eax)
350 movl 12(%esp),%esi /* caddr_t from */
351 movl 16(%esp),%edi /* caddr_t to */
352 movl 20(%esp),%ecx /* size_t len */
355 * make sure address is valid
360 cmpl $VM_MAXUSER_ADDRESS,%edx
364 shrl $2,%ecx /* copy longword-wise */
369 andb $3,%cl /* copy remaining bytes */
376 movl PCPU(CURPCB),%edx
377 movl %eax,PCB_ONFAULT(%edx)
385 movl PCPU(CURPCB),%edx
386 movl $0,PCB_ONFAULT(%edx)
391 * casueword. Compare and set user word. Returns -1 on fault,
392 * 0 on non-faulting access. The current value is in *oldp.
394 ALTENTRY(casueword32)
396 movl PCPU(CURPCB),%ecx
397 movl $fusufault,PCB_ONFAULT(%ecx)
398 movl 4(%esp),%edx /* dst */
399 movl 8(%esp),%eax /* old */
400 movl 16(%esp),%ecx /* new */
402 cmpl $VM_MAXUSER_ADDRESS-4,%edx /* verify address is valid */
408 cmpxchgl %ecx,(%edx) /* Compare and set. */
411 * The old value is in %eax. If the store succeeded it will be the
412 * value we expected (old) from before the store, otherwise it will
413 * be the current value.
416 movl PCPU(CURPCB),%ecx
417 movl $0,PCB_ONFAULT(%ecx)
418 movl 12(%esp),%edx /* oldp */
426 * Fetch (load) a 32-bit word, a 16-bit word, or an 8-bit byte from user
432 movl PCPU(CURPCB),%ecx
433 movl $fusufault,PCB_ONFAULT(%ecx)
434 movl 4(%esp),%edx /* from */
436 cmpl $VM_MAXUSER_ADDRESS-4,%edx /* verify address is valid */
440 movl $0,PCB_ONFAULT(%ecx)
449 * fuswintr() and suswintr() are specialized variants of fuword16() and
450 * suword16(), respectively. They are called from the profiling code,
451 * potentially at interrupt time. If they fail, that's okay; good things
452 * will happen later. They always fail for now, until the trap code is
453 * able to deal with this.
463 movl PCPU(CURPCB),%ecx
464 movl $fusufault,PCB_ONFAULT(%ecx)
467 cmpl $VM_MAXUSER_ADDRESS-2,%edx
471 movl $0,PCB_ONFAULT(%ecx)
476 movl PCPU(CURPCB),%ecx
477 movl $fusufault,PCB_ONFAULT(%ecx)
480 cmpl $VM_MAXUSER_ADDRESS-1,%edx
484 movl $0,PCB_ONFAULT(%ecx)
490 movl PCPU(CURPCB),%ecx
492 movl %eax,PCB_ONFAULT(%ecx)
497 * Store a 32-bit word, a 16-bit word, or an 8-bit byte to user memory.
498 * All these functions are MPSAFE.
503 movl PCPU(CURPCB),%ecx
504 movl $fusufault,PCB_ONFAULT(%ecx)
507 cmpl $VM_MAXUSER_ADDRESS-4,%edx /* verify address validity */
513 movl PCPU(CURPCB),%ecx
514 movl %eax,PCB_ONFAULT(%ecx)
520 movl PCPU(CURPCB),%ecx
521 movl $fusufault,PCB_ONFAULT(%ecx)
524 cmpl $VM_MAXUSER_ADDRESS-2,%edx /* verify address validity */
530 movl PCPU(CURPCB),%ecx /* restore trashed register */
531 movl %eax,PCB_ONFAULT(%ecx)
536 movl PCPU(CURPCB),%ecx
537 movl $fusufault,PCB_ONFAULT(%ecx)
540 cmpl $VM_MAXUSER_ADDRESS-1,%edx /* verify address validity */
546 movl PCPU(CURPCB),%ecx /* restore trashed register */
547 movl %eax,PCB_ONFAULT(%ecx)
552 * copyinstr(from, to, maxlen, int *lencopied) - MP SAFE
554 * copy a string from from to to, stop when a 0 character is reached.
555 * return ENAMETOOLONG if string is longer than maxlen, and
556 * EFAULT on protection violations. If lencopied is non-zero,
557 * return the actual length in *lencopied.
562 movl PCPU(CURPCB),%ecx
563 movl $cpystrflt,PCB_ONFAULT(%ecx)
565 movl 12(%esp),%esi /* %esi = from */
566 movl 16(%esp),%edi /* %edi = to */
567 movl 20(%esp),%edx /* %edx = maxlen */
569 movl $VM_MAXUSER_ADDRESS,%eax
571 /* make sure 'from' is within bounds */
575 /* restrict maxlen to <= VM_MAXUSER_ADDRESS-from */
593 /* Success -- 0 byte reached */
598 /* edx is zero - return ENAMETOOLONG or EFAULT */
599 cmpl $VM_MAXUSER_ADDRESS,%esi
602 movl $ENAMETOOLONG,%eax
609 /* set *lencopied and return %eax */
610 movl PCPU(CURPCB),%ecx
611 movl $0,PCB_ONFAULT(%ecx)
625 * copystr(from, to, maxlen, int *lencopied) - MP SAFE
631 movl 12(%esp),%esi /* %esi = from */
632 movl 16(%esp),%edi /* %edi = to */
633 movl 20(%esp),%edx /* %edx = maxlen */
644 /* Success -- 0 byte reached */
649 /* edx is zero -- return ENAMETOOLONG */
650 movl $ENAMETOOLONG,%eax
653 /* set *lencopied and return %eax */
675 cld /* compare forwards */
693 * Handling of special 386 registers and descriptor tables etc
695 /* void lgdt(struct region_descriptor *rdp); */
697 /* reload the descriptor table */
701 /* flush the prefetch q */
705 /* reload "stale" selectors */
714 /* reload code selector by turning return into intersegmental return */
722 /* ssdtosd(*ssdp,*sdp) */
744 /* void reset_dbregs() */
747 movl %eax,%dr7 /* disable all breakpoints first */
756 /*****************************************************************************/
757 /* setjump, longjump */
758 /*****************************************************************************/
762 movl %ebx,(%eax) /* save ebx */
763 movl %esp,4(%eax) /* save esp */
764 movl %ebp,8(%eax) /* save ebp */
765 movl %esi,12(%eax) /* save esi */
766 movl %edi,16(%eax) /* save edi */
767 movl (%esp),%edx /* get rta */
768 movl %edx,20(%eax) /* save eip */
769 xorl %eax,%eax /* return(0); */
775 movl (%eax),%ebx /* restore ebx */
776 movl 4(%eax),%esp /* restore esp */
777 movl 8(%eax),%ebp /* restore ebp */
778 movl 12(%eax),%esi /* restore esi */
779 movl 16(%eax),%edi /* restore edi */
780 movl 20(%eax),%edx /* get rta */
781 movl %edx,(%esp) /* put in return frame */
782 xorl %eax,%eax /* return(1); */
788 * Support for reading MSRs in the safe manner.
791 /* int rdmsr_safe(u_int msr, uint64_t *data) */
792 movl PCPU(CURPCB),%ecx
793 movl $msr_onfault,PCB_ONFAULT(%ecx)
802 movl PCPU(CURPCB),%ecx
803 movl %eax,PCB_ONFAULT(%ecx)
808 * Support for writing MSRs in the safe manner.
811 /* int wrmsr_safe(u_int msr, uint64_t data) */
812 movl PCPU(CURPCB),%ecx
813 movl $msr_onfault,PCB_ONFAULT(%ecx)
821 movl PCPU(CURPCB),%ecx
822 movl %eax,PCB_ONFAULT(%ecx)
827 * MSR operations fault handler
831 movl PCPU(CURPCB),%ecx
832 movl $0,PCB_ONFAULT(%ecx)