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 * 3. 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
32 #include <machine/asmacros.h>
33 #include <machine/cputypes.h>
34 #include <machine/pmap.h>
35 #include <machine/specialreg.h>
45 * void bzero(void *buf, u_int len)
72 * The loop takes 14 bytes. Ensure that it doesn't cross a 16-byte
138 /* fillw(pat, base, cnt) */
159 cmpl %ecx,%eax /* overlapping && src < dst? */
161 cld /* nope, copy forwards */
170 addl %ecx,%edi /* copy backwards. */
184 * bcopy(src, dst, cnt)
185 * ws@tools.de (Wolfgang Solfrank, TooLs GmbH) +49-228-985800
198 cmpl %ecx,%eax /* overlapping && src < dst? */
201 shrl $2,%ecx /* copy by 32-bit words */
202 cld /* nope, copy forwards */
206 andl $3,%ecx /* any bytes left? */
216 addl %ecx,%edi /* copy backwards */
220 andl $3,%ecx /* any fractional bytes? */
224 movl 16(%ebp),%ecx /* copy remainder by 32-bit words */
238 * Note: memcpy does not support overlapping copies
247 shrl $2,%ecx /* copy by 32-bit words */
248 cld /* nope, copy forwards */
252 andl $3,%ecx /* any bytes left? */
260 /*****************************************************************************/
261 /* copyout and fubyte family */
262 /*****************************************************************************/
264 * Access user memory from inside the kernel. These routines and possibly
265 * the math- and DOS emulators should be the only places that do this.
267 * We have to access the memory with user's permissions, so use a segment
268 * selector with RPL 3. For writes to user space we have to additionally
269 * check the PTE for write permission, because the 386 does not check
270 * write permissions when we are executing with EPL 0. The 486 does check
271 * this if the WP bit is set in CR0, so we can use a simpler version here.
273 * These routines set curpcb->pcb_onfault for the time they execute. When a
274 * protection violation occurs inside the functions, the trap handler
275 * returns to *curpcb->pcb_onfault instead of the function.
279 * copyout(from_kernel, to_user, len) - MP SAFE
282 movl PCPU(CURPCB),%eax
283 movl $copyout_fault,PCB_ONFAULT(%eax)
290 testl %ebx,%ebx /* anything to do? */
294 * Check explicitly for non-user addresses. If 486 write protection
295 * is being used, this check is essential because we are in kernel
296 * mode so the h/w does not provide any protection against writing
301 * First, prevent address wrapping.
307 * XXX STOP USING VM_MAXUSER_ADDRESS.
308 * It is an end address, not a max, so every time it is used correctly it
309 * looks like there is an off by one error, and of course it caused an off
310 * by one error in several places.
312 cmpl $VM_MAXUSER_ADDRESS,%eax
315 /* bcopy(%esi, %edi, %ebx) */
332 movl PCPU(CURPCB),%edx
333 movl %eax,PCB_ONFAULT(%edx)
342 movl PCPU(CURPCB),%edx
343 movl $0,PCB_ONFAULT(%edx)
348 * copyin(from_user, to_kernel, len) - MP SAFE
351 movl PCPU(CURPCB),%eax
352 movl $copyin_fault,PCB_ONFAULT(%eax)
355 movl 12(%esp),%esi /* caddr_t from */
356 movl 16(%esp),%edi /* caddr_t to */
357 movl 20(%esp),%ecx /* size_t len */
360 * make sure address is valid
365 cmpl $VM_MAXUSER_ADDRESS,%edx
369 shrl $2,%ecx /* copy longword-wise */
374 andb $3,%cl /* copy remaining bytes */
381 movl PCPU(CURPCB),%edx
382 movl %eax,PCB_ONFAULT(%edx)
390 movl PCPU(CURPCB),%edx
391 movl $0,PCB_ONFAULT(%edx)
396 * casueword. Compare and set user word. Returns -1 on fault,
397 * 0 on non-faulting access. The current value is in *oldp.
399 ALTENTRY(casueword32)
401 movl PCPU(CURPCB),%ecx
402 movl $fusufault,PCB_ONFAULT(%ecx)
403 movl 4(%esp),%edx /* dst */
404 movl 8(%esp),%eax /* old */
405 movl 16(%esp),%ecx /* new */
407 cmpl $VM_MAXUSER_ADDRESS-4,%edx /* verify address is valid */
413 cmpxchgl %ecx,(%edx) /* Compare and set. */
416 * The old value is in %eax. If the store succeeded it will be the
417 * value we expected (old) from before the store, otherwise it will
418 * be the current value.
421 movl PCPU(CURPCB),%ecx
422 movl $0,PCB_ONFAULT(%ecx)
423 movl 12(%esp),%edx /* oldp */
431 * Fetch (load) a 32-bit word, a 16-bit word, or an 8-bit byte from user
437 movl PCPU(CURPCB),%ecx
438 movl $fusufault,PCB_ONFAULT(%ecx)
439 movl 4(%esp),%edx /* from */
441 cmpl $VM_MAXUSER_ADDRESS-4,%edx /* verify address is valid */
445 movl $0,PCB_ONFAULT(%ecx)
454 * fuswintr() and suswintr() are specialized variants of fuword16() and
455 * suword16(), respectively. They are called from the profiling code,
456 * potentially at interrupt time. If they fail, that's okay; good things
457 * will happen later. They always fail for now, until the trap code is
458 * able to deal with this.
468 movl PCPU(CURPCB),%ecx
469 movl $fusufault,PCB_ONFAULT(%ecx)
472 cmpl $VM_MAXUSER_ADDRESS-2,%edx
476 movl $0,PCB_ONFAULT(%ecx)
481 movl PCPU(CURPCB),%ecx
482 movl $fusufault,PCB_ONFAULT(%ecx)
485 cmpl $VM_MAXUSER_ADDRESS-1,%edx
489 movl $0,PCB_ONFAULT(%ecx)
495 movl PCPU(CURPCB),%ecx
497 movl %eax,PCB_ONFAULT(%ecx)
502 * Store a 32-bit word, a 16-bit word, or an 8-bit byte to user memory.
503 * All these functions are MPSAFE.
508 movl PCPU(CURPCB),%ecx
509 movl $fusufault,PCB_ONFAULT(%ecx)
512 cmpl $VM_MAXUSER_ADDRESS-4,%edx /* verify address validity */
518 movl PCPU(CURPCB),%ecx
519 movl %eax,PCB_ONFAULT(%ecx)
525 movl PCPU(CURPCB),%ecx
526 movl $fusufault,PCB_ONFAULT(%ecx)
529 cmpl $VM_MAXUSER_ADDRESS-2,%edx /* verify address validity */
535 movl PCPU(CURPCB),%ecx /* restore trashed register */
536 movl %eax,PCB_ONFAULT(%ecx)
541 movl PCPU(CURPCB),%ecx
542 movl $fusufault,PCB_ONFAULT(%ecx)
545 cmpl $VM_MAXUSER_ADDRESS-1,%edx /* verify address validity */
551 movl PCPU(CURPCB),%ecx /* restore trashed register */
552 movl %eax,PCB_ONFAULT(%ecx)
557 * copyinstr(from, to, maxlen, int *lencopied) - MP SAFE
559 * copy a string from from to to, stop when a 0 character is reached.
560 * return ENAMETOOLONG if string is longer than maxlen, and
561 * EFAULT on protection violations. If lencopied is non-zero,
562 * return the actual length in *lencopied.
567 movl PCPU(CURPCB),%ecx
568 movl $cpystrflt,PCB_ONFAULT(%ecx)
570 movl 12(%esp),%esi /* %esi = from */
571 movl 16(%esp),%edi /* %edi = to */
572 movl 20(%esp),%edx /* %edx = maxlen */
574 movl $VM_MAXUSER_ADDRESS,%eax
576 /* make sure 'from' is within bounds */
580 /* restrict maxlen to <= VM_MAXUSER_ADDRESS-from */
598 /* Success -- 0 byte reached */
603 /* edx is zero - return ENAMETOOLONG or EFAULT */
604 cmpl $VM_MAXUSER_ADDRESS,%esi
607 movl $ENAMETOOLONG,%eax
614 /* set *lencopied and return %eax */
615 movl PCPU(CURPCB),%ecx
616 movl $0,PCB_ONFAULT(%ecx)
630 * copystr(from, to, maxlen, int *lencopied) - MP SAFE
636 movl 12(%esp),%esi /* %esi = from */
637 movl 16(%esp),%edi /* %edi = to */
638 movl 20(%esp),%edx /* %edx = maxlen */
649 /* Success -- 0 byte reached */
654 /* edx is zero -- return ENAMETOOLONG */
655 movl $ENAMETOOLONG,%eax
658 /* set *lencopied and return %eax */
680 cld /* compare forwards */
698 * Handling of special 386 registers and descriptor tables etc
700 /* void lgdt(struct region_descriptor *rdp); */
702 /* reload the descriptor table */
706 /* flush the prefetch q */
710 /* reload "stale" selectors */
719 /* reload code selector by turning return into intersegmental return */
727 /* ssdtosd(*ssdp,*sdp) */
749 /* void reset_dbregs() */
752 movl %eax,%dr7 /* disable all breakpoints first */
761 /*****************************************************************************/
762 /* setjump, longjump */
763 /*****************************************************************************/
767 movl %ebx,(%eax) /* save ebx */
768 movl %esp,4(%eax) /* save esp */
769 movl %ebp,8(%eax) /* save ebp */
770 movl %esi,12(%eax) /* save esi */
771 movl %edi,16(%eax) /* save edi */
772 movl (%esp),%edx /* get rta */
773 movl %edx,20(%eax) /* save eip */
774 xorl %eax,%eax /* return(0); */
780 movl (%eax),%ebx /* restore ebx */
781 movl 4(%eax),%esp /* restore esp */
782 movl 8(%eax),%ebp /* restore ebp */
783 movl 12(%eax),%esi /* restore esi */
784 movl 16(%eax),%edi /* restore edi */
785 movl 20(%eax),%edx /* get rta */
786 movl %edx,(%esp) /* put in return frame */
787 xorl %eax,%eax /* return(1); */
793 * Support for reading MSRs in the safe manner.
796 /* int rdmsr_safe(u_int msr, uint64_t *data) */
797 movl PCPU(CURPCB),%ecx
798 movl $msr_onfault,PCB_ONFAULT(%ecx)
807 movl PCPU(CURPCB),%ecx
808 movl %eax,PCB_ONFAULT(%ecx)
813 * Support for writing MSRs in the safe manner.
816 /* int wrmsr_safe(u_int msr, uint64_t data) */
817 movl PCPU(CURPCB),%ecx
818 movl $msr_onfault,PCB_ONFAULT(%ecx)
826 movl PCPU(CURPCB),%ecx
827 movl %eax,PCB_ONFAULT(%ecx)
832 * MSR operations fault handler
836 movl PCPU(CURPCB),%ecx
837 movl $0,PCB_ONFAULT(%ecx)