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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * $FreeBSD: src/sys/i386/i386/support.s,v 1.67.2.5 2001/08/15 01:23:50 peter Exp $
34 * $DragonFly: src/sys/platform/pc32/i386/support.s,v 1.12 2004/04/30 09:44:16 dillon Exp $
39 #include <machine/asmacros.h>
40 #include <machine/cputypes.h>
41 #include <machine/pmap.h>
42 #include <machine/specialreg.h>
52 .long asm_generic_memcpy
56 .long asm_generic_bcopy
64 .long asm_generic_bcopy
68 /* fillw(pat, base, cnt) */
81 * void bcopy(const void *s, void *d, size_t count)
83 * Normal bcopy() vector, an optimized bcopy may be installed in
89 movl 4+8(%esp),%esi /* caddr_t from */
90 movl 8+8(%esp),%edi /* caddr_t to */
91 movl 12+8(%esp),%ecx /* size_t len */
98 * Generic (integer-only) bcopy() vector.
103 movl 4+8(%esp),%esi /* caddr_t from */
104 movl 8+8(%esp),%edi /* caddr_t to */
105 movl 12+8(%esp),%ecx /* size_t len */
106 call asm_generic_bcopy
114 movl 4+8(%esp),%esi /* caddr_t from */
115 movl 8+8(%esp),%edi /* caddr_t to */
116 movl 12+8(%esp),%ecx /* size_t len */
123 * void *memcpy(void *d, const void *s, size_t count)
125 * Note: memcpy does not have to support overlapping copies.
127 * Note: (d, s) arguments reversed from bcopy, and memcpy() returns d
128 * while bcopy() returns void.
143 * A stack-based on-fault routine is used for more complex PCB_ONFAULT
144 * situations (such as memcpy/bcopy/bzero). In this case the on-fault
145 * routine must be pushed on the stack.
150 /*****************************************************************************/
151 /* copyout and fubyte family */
152 /*****************************************************************************/
154 * Access user memory from inside the kernel. These routines and possibly
155 * the math- and DOS emulators should be the only places that do this.
157 * We have to access the memory with user's permissions, so use a segment
158 * selector with RPL 3. For writes to user space we have to additionally
159 * check the PTE for write permission, because the 386 does not check
160 * write permissions when we are executing with EPL 0. The 486 does check
161 * this if the WP bit is set in CR0, so we can use a simpler version here.
163 * These routines set curpcb->onfault for the time they execute. When a
164 * protection violation occurs inside the functions, the trap handler
165 * returns to *curpcb->onfault instead of the function.
169 * copyout(from_kernel, to_user, len) - MP SAFE (if not I386_CPU)
172 movl PCPU(curthread),%eax
173 movl TD_PCB(%eax),%eax
177 pushl $copyout_fault2
178 movl $stack_onfault,PCB_ONFAULT(%eax)
181 movl 12+16(%esp),%ebx
182 testl %ebx,%ebx /* anything to do? */
186 * Check explicitly for non-user addresses. If 486 write protection
187 * is being used, this check is essential because we are in kernel
188 * mode so the h/w does not provide any protection against writing
193 * First, prevent address wrapping.
199 * XXX STOP USING VM_MAXUSER_ADDRESS.
200 * It is an end address, not a max, so every time it is used correctly it
201 * looks like there is an off by one error, and of course it caused an off
202 * by one error in several places.
204 cmpl $VM_MAXUSER_ADDRESS,%eax
207 #if defined(I386_CPU)
209 #if defined(I486_CPU) || defined(I586_CPU) || defined(I686_CPU)
210 cmpl $CPUCLASS_386,cpu_class
214 * We have to check each PTE for user write permission.
215 * The checking may cause a page fault, so it is important to set
216 * up everything for return via copyout_fault before here.
218 /* compute number of pages */
223 shrl $IDXSHIFT+2,%ecx
226 /* compute PTE offset for start address */
232 /* check PTE for each page */
233 leal PTmap(%edx),%eax
236 testb $PG_V,PTmap(%eax) /* PTE page must be valid */
239 andb $PG_V|PG_RW|PG_U,%al /* page must be valid and user writable */
240 cmpb $PG_V|PG_RW|PG_U,%al
244 /* simulate a trap */
249 call trapwrite /* trapwrite(addr) */
254 testl %eax,%eax /* if not ok, return EFAULT */
260 jnz 1b /* check next page */
261 #endif /* I386_CPU */
264 * Convert copyout to memcpy_vector(dest:%edi, src:%esi, conut:%ecx)
275 movl PCPU(curthread),%edx
277 movl TD_PCB(%edx),%edx
281 movl %eax,PCB_ONFAULT(%edx)
286 addl $4,%esp /* skip pushed copyout_fault vector */
291 movl PCPU(curthread),%edx
292 movl TD_PCB(%edx),%edx
293 movl $0,PCB_ONFAULT(%edx)
298 * copyin(from_user, to_kernel, len) - MP SAFE
302 movl PCPU(curthread),%eax
303 movl TD_PCB(%eax),%eax
307 movl $stack_onfault,PCB_ONFAULT(%eax)
308 movl 4+12(%esp),%esi /* caddr_t from */
309 movl 8+12(%esp),%edi /* caddr_t to */
310 movl 12+12(%esp),%ecx /* size_t len */
313 * make sure address is valid
318 cmpl $VM_MAXUSER_ADDRESS,%edx
322 * Call memcpy(destination:%edi, source:%esi, bytes:%ecx)
327 * return 0 (no error)
330 movl PCPU(curthread),%edx
332 movl TD_PCB(%edx),%edx
335 movl %eax,PCB_ONFAULT(%edx)
343 addl $4,%esp /* skip pushed copyin_fault vector */
347 movl PCPU(curthread),%edx
348 movl TD_PCB(%edx),%edx
349 movl $0,PCB_ONFAULT(%edx)
354 * fu{byte,sword,word} - MP SAFE
356 * Fetch a byte (sword, word) from user memory
359 movl PCPU(curthread),%ecx
360 movl TD_PCB(%ecx),%ecx
361 movl $fusufault,PCB_ONFAULT(%ecx)
362 movl 4(%esp),%edx /* from */
364 cmpl $VM_MAXUSER_ADDRESS-4,%edx /* verify address is valid */
368 movl $0,PCB_ONFAULT(%ecx)
372 * These two routines are called from the profiling code, potentially
373 * at interrupt time. If they fail, that's okay, good things will
374 * happen later. Fail all the time for now - until the trap code is
375 * able to deal with this.
386 movl PCPU(curthread),%ecx
387 movl TD_PCB(%ecx),%ecx
388 movl $fusufault,PCB_ONFAULT(%ecx)
391 cmpl $VM_MAXUSER_ADDRESS-2,%edx
395 movl $0,PCB_ONFAULT(%ecx)
402 movl PCPU(curthread),%ecx
403 movl TD_PCB(%ecx),%ecx
404 movl $fusufault,PCB_ONFAULT(%ecx)
407 cmpl $VM_MAXUSER_ADDRESS-1,%edx
411 movl $0,PCB_ONFAULT(%ecx)
416 movl PCPU(curthread),%ecx
417 movl TD_PCB(%ecx),%ecx
419 movl %eax,PCB_ONFAULT(%ecx)
424 * su{byte,sword,word} - MP SAFE (if not I386_CPU)
426 * Write a byte (word, longword) to user memory
429 movl PCPU(curthread),%ecx
430 movl TD_PCB(%ecx),%ecx
431 movl $fusufault,PCB_ONFAULT(%ecx)
434 #if defined(I386_CPU)
436 #if defined(I486_CPU) || defined(I586_CPU) || defined(I686_CPU)
437 cmpl $CPUCLASS_386,cpu_class
438 jne 2f /* we only have to set the right segment selector */
439 #endif /* I486_CPU || I586_CPU || I686_CPU */
441 /* XXX - page boundary crossing is still not handled */
446 leal PTmap(%edx),%ecx
449 testb $PG_V,PTmap(%ecx) /* PTE page must be valid */
452 andb $PG_V|PG_RW|PG_U,%dl /* page must be valid and user writable */
453 cmpb $PG_V|PG_RW|PG_U,%dl
457 /* simulate a trap */
460 popl %edx /* remove junk parameter from stack */
468 cmpl $VM_MAXUSER_ADDRESS-4,%edx /* verify address validity */
474 movl PCPU(curthread),%ecx
475 movl TD_PCB(%ecx),%ecx
476 movl %eax,PCB_ONFAULT(%ecx)
480 * susword - MP SAFE (if not I386_CPU)
483 movl PCPU(curthread),%ecx
484 movl TD_PCB(%ecx),%ecx
485 movl $fusufault,PCB_ONFAULT(%ecx)
488 #if defined(I386_CPU)
490 #if defined(I486_CPU) || defined(I586_CPU) || defined(I686_CPU)
491 cmpl $CPUCLASS_386,cpu_class
493 #endif /* I486_CPU || I586_CPU || I686_CPU */
495 /* XXX - page boundary crossing is still not handled */
500 leal PTmap(%edx),%ecx
503 testb $PG_V,PTmap(%ecx) /* PTE page must be valid */
506 andb $PG_V|PG_RW|PG_U,%dl /* page must be valid and user writable */
507 cmpb $PG_V|PG_RW|PG_U,%dl
511 /* simulate a trap */
514 popl %edx /* remove junk parameter from stack */
522 cmpl $VM_MAXUSER_ADDRESS-2,%edx /* verify address validity */
528 movl PCPU(curthread),%ecx /* restore trashed register */
529 movl TD_PCB(%ecx),%ecx
530 movl %eax,PCB_ONFAULT(%ecx)
534 * su[i]byte - MP SAFE (if not I386_CPU)
538 movl PCPU(curthread),%ecx
539 movl TD_PCB(%ecx),%ecx
540 movl $fusufault,PCB_ONFAULT(%ecx)
543 #if defined(I386_CPU)
545 #if defined(I486_CPU) || defined(I586_CPU) || defined(I686_CPU)
546 cmpl $CPUCLASS_386,cpu_class
548 #endif /* I486_CPU || I586_CPU || I686_CPU */
554 leal PTmap(%edx),%ecx
557 testb $PG_V,PTmap(%ecx) /* PTE page must be valid */
560 andb $PG_V|PG_RW|PG_U,%dl /* page must be valid and user writable */
561 cmpb $PG_V|PG_RW|PG_U,%dl
565 /* simulate a trap */
568 popl %edx /* remove junk parameter from stack */
576 cmpl $VM_MAXUSER_ADDRESS-1,%edx /* verify address validity */
582 movl PCPU(curthread),%ecx /* restore trashed register */
583 movl TD_PCB(%ecx),%ecx
584 movl %eax,PCB_ONFAULT(%ecx)
588 * copyinstr(from, to, maxlen, int *lencopied) - MP SAFE
590 * copy a string from from to to, stop when a 0 character is reached.
591 * return ENAMETOOLONG if string is longer than maxlen, and
592 * EFAULT on protection violations. If lencopied is non-zero,
593 * return the actual length in *lencopied.
598 movl PCPU(curthread),%ecx
599 movl TD_PCB(%ecx),%ecx
600 movl $cpystrflt,PCB_ONFAULT(%ecx)
602 movl 12(%esp),%esi /* %esi = from */
603 movl 16(%esp),%edi /* %edi = to */
604 movl 20(%esp),%edx /* %edx = maxlen */
606 movl $VM_MAXUSER_ADDRESS,%eax
608 /* make sure 'from' is within bounds */
612 /* restrict maxlen to <= VM_MAXUSER_ADDRESS-from */
630 /* Success -- 0 byte reached */
635 /* edx is zero - return ENAMETOOLONG or EFAULT */
636 cmpl $VM_MAXUSER_ADDRESS,%esi
639 movl $ENAMETOOLONG,%eax
646 /* set *lencopied and return %eax */
647 movl PCPU(curthread),%ecx
648 movl TD_PCB(%ecx),%ecx
649 movl $0,PCB_ONFAULT(%ecx)
663 * copystr(from, to, maxlen, int *lencopied) - MP SAFE
669 movl 12(%esp),%esi /* %esi = from */
670 movl 16(%esp),%edi /* %edi = to */
671 movl 20(%esp),%edx /* %edx = maxlen */
682 /* Success -- 0 byte reached */
687 /* edx is zero -- return ENAMETOOLONG */
688 movl $ENAMETOOLONG,%eax
691 /* set *lencopied and return %eax */
713 cld /* compare forwards */
732 * Handling of special 386 registers and descriptor tables etc
734 /* void lgdt(struct region_descriptor *rdp); */
736 /* reload the descriptor table */
740 /* flush the prefetch q */
744 /* reload "stale" selectors */
753 /* reload code selector by turning return into intersegmental return */
760 * void lidt(struct region_descriptor *rdp);
768 * void lldt(u_short sel)
775 * void ltr(u_short sel)
781 /* ssdtosd(*ssdp,*sdp) */
818 /* void load_cr3(caddr_t cr3) */
820 #if defined(SWTCH_OPTIM_STATS)
821 incl _tlb_flush_count
832 /* void load_cr4(caddr_t cr4) */
838 /* void reset_dbregs() */
841 movl %eax,%dr7 /* disable all breapoints first */
849 /*****************************************************************************/
850 /* setjump, longjump */
851 /*****************************************************************************/
855 movl %ebx,(%eax) /* save ebx */
856 movl %esp,4(%eax) /* save esp */
857 movl %ebp,8(%eax) /* save ebp */
858 movl %esi,12(%eax) /* save esi */
859 movl %edi,16(%eax) /* save edi */
860 movl (%esp),%edx /* get rta */
861 movl %edx,20(%eax) /* save eip */
862 xorl %eax,%eax /* return(0); */
867 movl (%eax),%ebx /* restore ebx */
868 movl 4(%eax),%esp /* restore esp */
869 movl 8(%eax),%ebp /* restore ebp */
870 movl 12(%eax),%esi /* restore esi */
871 movl 16(%eax),%edi /* restore edi */
872 movl 20(%eax),%edx /* get rta */
873 movl %edx,(%esp) /* put in return frame */
874 xorl %eax,%eax /* return(1); */
879 * Support for BB-profiling (gcc -a). The kernbb program will extract
880 * the data from the kernel.
890 NON_GPROF_ENTRY(__bb_init_func)
896 .byte 0xc3 /* avoid macro for `ret' */