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.15 2006/10/23 15:42:45 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
60 .long asm_generic_bcopy
64 /* fillw(pat, base, cnt) */
77 * void bcopy(const void *s, void *d, size_t count)
79 * Normal bcopy() vector, an optimized bcopy may be installed in
85 movl 4+8(%esp),%esi /* caddr_t from */
86 movl 8+8(%esp),%edi /* caddr_t to */
87 movl 12+8(%esp),%ecx /* size_t len */
94 * Generic (integer-only) bcopy() vector.
99 movl 4+8(%esp),%esi /* caddr_t from */
100 movl 8+8(%esp),%edi /* caddr_t to */
101 movl 12+8(%esp),%ecx /* size_t len */
102 call asm_generic_bcopy
110 movl 4+8(%esp),%esi /* caddr_t from */
111 movl 8+8(%esp),%edi /* caddr_t to */
112 movl 12+8(%esp),%ecx /* size_t len */
119 * void *memcpy(void *d, const void *s, size_t count)
121 * Note: memcpy does not have to support overlapping copies.
123 * Note: (d, s) arguments reversed from bcopy, and memcpy() returns d
124 * while bcopy() returns void.
139 * A stack-based on-fault routine is used for more complex PCB_ONFAULT
140 * situations (such as memcpy/bcopy/bzero). In this case the on-fault
141 * routine must be pushed on the stack.
146 /*****************************************************************************/
147 /* copyout and fubyte family */
148 /*****************************************************************************/
150 * Access user memory from inside the kernel. These routines and possibly
151 * the math- and DOS emulators should be the only places that do this.
153 * We have to access the memory with user's permissions, so use a segment
154 * selector with RPL 3. For writes to user space we have to additionally
155 * check the PTE for write permission, because the 386 does not check
156 * write permissions when we are executing with EPL 0. The 486 does check
157 * this if the WP bit is set in CR0, so we can use a simpler version here.
159 * These routines set curpcb->onfault for the time they execute. When a
160 * protection violation occurs inside the functions, the trap handler
161 * returns to *curpcb->onfault instead of the function.
165 * copyout(from_kernel, to_user, len) - MP SAFE (if not I386_CPU)
168 movl PCPU(curthread),%eax
169 movl TD_PCB(%eax),%eax
173 pushl $copyout_fault2
174 movl $stack_onfault,PCB_ONFAULT(%eax)
177 movl 12+16(%esp),%ebx
178 testl %ebx,%ebx /* anything to do? */
182 * Check explicitly for non-user addresses. If 486 write protection
183 * is being used, this check is essential because we are in kernel
184 * mode so the h/w does not provide any protection against writing
189 * First, prevent address wrapping.
195 * XXX STOP USING VM_MAXUSER_ADDRESS.
196 * It is an end address, not a max, so every time it is used correctly it
197 * looks like there is an off by one error, and of course it caused an off
198 * by one error in several places.
200 cmpl $VM_MAXUSER_ADDRESS,%eax
203 #if defined(I386_CPU)
205 #if defined(I486_CPU) || defined(I586_CPU) || defined(I686_CPU)
206 cmpl $CPUCLASS_386,cpu_class
210 * We have to check each PTE for user write permission.
211 * The checking may cause a page fault, so it is important to set
212 * up everything for return via copyout_fault before here.
214 /* compute number of pages */
219 shrl $IDXSHIFT+2,%ecx
222 /* compute PTE offset for start address */
228 /* check PTE for each page */
229 leal PTmap(%edx),%eax
232 testb $PG_V,PTmap(%eax) /* PTE page must be valid */
235 andb $PG_V|PG_RW|PG_U,%al /* page must be valid and user writable */
236 cmpb $PG_V|PG_RW|PG_U,%al
240 /* simulate a trap */
245 call trapwrite /* trapwrite(addr) */
250 testl %eax,%eax /* if not ok, return EFAULT */
256 jnz 1b /* check next page */
257 #endif /* I386_CPU */
260 * Convert copyout to memcpy_vector(dest:%edi, src:%esi, conut:%ecx)
271 movl PCPU(curthread),%edx
273 movl TD_PCB(%edx),%edx
277 movl %eax,PCB_ONFAULT(%edx)
282 addl $4,%esp /* skip pushed copyout_fault vector */
287 movl PCPU(curthread),%edx
288 movl TD_PCB(%edx),%edx
289 movl $0,PCB_ONFAULT(%edx)
294 * copyin(from_user, to_kernel, len) - MP SAFE
298 movl PCPU(curthread),%eax
299 movl TD_PCB(%eax),%eax
303 movl $stack_onfault,PCB_ONFAULT(%eax)
304 movl 4+12(%esp),%esi /* caddr_t from */
305 movl 8+12(%esp),%edi /* caddr_t to */
306 movl 12+12(%esp),%ecx /* size_t len */
309 * make sure address is valid
314 cmpl $VM_MAXUSER_ADDRESS,%edx
318 * Call memcpy(destination:%edi, source:%esi, bytes:%ecx)
323 * return 0 (no error)
326 movl PCPU(curthread),%edx
328 movl TD_PCB(%edx),%edx
331 movl %eax,PCB_ONFAULT(%edx)
339 addl $4,%esp /* skip pushed copyin_fault vector */
343 movl PCPU(curthread),%edx
344 movl TD_PCB(%edx),%edx
345 movl $0,PCB_ONFAULT(%edx)
350 * fu{byte,sword,word} - MP SAFE
352 * Fetch a byte (sword, word) from user memory
355 movl PCPU(curthread),%ecx
356 movl TD_PCB(%ecx),%ecx
357 movl $fusufault,PCB_ONFAULT(%ecx)
358 movl 4(%esp),%edx /* from */
360 cmpl $VM_MAXUSER_ADDRESS-4,%edx /* verify address is valid */
364 movl $0,PCB_ONFAULT(%ecx)
368 * These two routines are called from the profiling code, potentially
369 * at interrupt time. If they fail, that's okay, good things will
370 * happen later. Fail all the time for now - until the trap code is
371 * able to deal with this.
382 movl PCPU(curthread),%ecx
383 movl TD_PCB(%ecx),%ecx
384 movl $fusufault,PCB_ONFAULT(%ecx)
387 cmpl $VM_MAXUSER_ADDRESS-2,%edx
391 movl $0,PCB_ONFAULT(%ecx)
398 movl PCPU(curthread),%ecx
399 movl TD_PCB(%ecx),%ecx
400 movl $fusufault,PCB_ONFAULT(%ecx)
403 cmpl $VM_MAXUSER_ADDRESS-1,%edx
407 movl $0,PCB_ONFAULT(%ecx)
412 movl PCPU(curthread),%ecx
413 movl TD_PCB(%ecx),%ecx
415 movl %eax,PCB_ONFAULT(%ecx)
420 * su{byte,sword,word} - MP SAFE (if not I386_CPU)
422 * Write a byte (word, longword) to user memory
425 movl PCPU(curthread),%ecx
426 movl TD_PCB(%ecx),%ecx
427 movl $fusufault,PCB_ONFAULT(%ecx)
430 #if defined(I386_CPU)
432 #if defined(I486_CPU) || defined(I586_CPU) || defined(I686_CPU)
433 cmpl $CPUCLASS_386,cpu_class
434 jne 2f /* we only have to set the right segment selector */
435 #endif /* I486_CPU || I586_CPU || I686_CPU */
437 /* XXX - page boundary crossing is still not handled */
442 leal PTmap(%edx),%ecx
445 testb $PG_V,PTmap(%ecx) /* PTE page must be valid */
448 andb $PG_V|PG_RW|PG_U,%dl /* page must be valid and user writable */
449 cmpb $PG_V|PG_RW|PG_U,%dl
453 /* simulate a trap */
456 popl %edx /* remove junk parameter from stack */
464 cmpl $VM_MAXUSER_ADDRESS-4,%edx /* verify address validity */
470 movl PCPU(curthread),%ecx
471 movl TD_PCB(%ecx),%ecx
472 movl %eax,PCB_ONFAULT(%ecx)
476 * susword - MP SAFE (if not I386_CPU)
479 movl PCPU(curthread),%ecx
480 movl TD_PCB(%ecx),%ecx
481 movl $fusufault,PCB_ONFAULT(%ecx)
484 #if defined(I386_CPU)
486 #if defined(I486_CPU) || defined(I586_CPU) || defined(I686_CPU)
487 cmpl $CPUCLASS_386,cpu_class
489 #endif /* I486_CPU || I586_CPU || I686_CPU */
491 /* XXX - page boundary crossing is still not handled */
496 leal PTmap(%edx),%ecx
499 testb $PG_V,PTmap(%ecx) /* PTE page must be valid */
502 andb $PG_V|PG_RW|PG_U,%dl /* page must be valid and user writable */
503 cmpb $PG_V|PG_RW|PG_U,%dl
507 /* simulate a trap */
510 popl %edx /* remove junk parameter from stack */
518 cmpl $VM_MAXUSER_ADDRESS-2,%edx /* verify address validity */
524 movl PCPU(curthread),%ecx /* restore trashed register */
525 movl TD_PCB(%ecx),%ecx
526 movl %eax,PCB_ONFAULT(%ecx)
530 * su[i]byte - MP SAFE (if not I386_CPU)
534 movl PCPU(curthread),%ecx
535 movl TD_PCB(%ecx),%ecx
536 movl $fusufault,PCB_ONFAULT(%ecx)
539 #if defined(I386_CPU)
541 #if defined(I486_CPU) || defined(I586_CPU) || defined(I686_CPU)
542 cmpl $CPUCLASS_386,cpu_class
544 #endif /* I486_CPU || I586_CPU || I686_CPU */
550 leal PTmap(%edx),%ecx
553 testb $PG_V,PTmap(%ecx) /* PTE page must be valid */
556 andb $PG_V|PG_RW|PG_U,%dl /* page must be valid and user writable */
557 cmpb $PG_V|PG_RW|PG_U,%dl
561 /* simulate a trap */
564 popl %edx /* remove junk parameter from stack */
572 cmpl $VM_MAXUSER_ADDRESS-1,%edx /* verify address validity */
578 movl PCPU(curthread),%ecx /* restore trashed register */
579 movl TD_PCB(%ecx),%ecx
580 movl %eax,PCB_ONFAULT(%ecx)
584 * copyinstr(from, to, maxlen, int *lencopied) - MP SAFE
586 * copy a string from from to to, stop when a 0 character is reached.
587 * return ENAMETOOLONG if string is longer than maxlen, and
588 * EFAULT on protection violations. If lencopied is non-zero,
589 * return the actual length in *lencopied.
594 movl PCPU(curthread),%ecx
595 movl TD_PCB(%ecx),%ecx
596 movl $cpystrflt,PCB_ONFAULT(%ecx)
598 movl 12(%esp),%esi /* %esi = from */
599 movl 16(%esp),%edi /* %edi = to */
600 movl 20(%esp),%edx /* %edx = maxlen */
602 movl $VM_MAXUSER_ADDRESS,%eax
604 /* make sure 'from' is within bounds */
608 /* restrict maxlen to <= VM_MAXUSER_ADDRESS-from */
626 /* Success -- 0 byte reached */
631 /* edx is zero - return ENAMETOOLONG or EFAULT */
632 cmpl $VM_MAXUSER_ADDRESS,%esi
635 movl $ENAMETOOLONG,%eax
642 /* set *lencopied and return %eax */
643 movl PCPU(curthread),%ecx
644 movl TD_PCB(%ecx),%ecx
645 movl $0,PCB_ONFAULT(%ecx)
659 * copystr(from, to, maxlen, int *lencopied) - MP SAFE
665 movl 12(%esp),%esi /* %esi = from */
666 movl 16(%esp),%edi /* %edi = to */
667 movl 20(%esp),%edx /* %edx = maxlen */
678 /* Success -- 0 byte reached */
683 /* edx is zero -- return ENAMETOOLONG */
684 movl $ENAMETOOLONG,%eax
687 /* set *lencopied and return %eax */
709 cld /* compare forwards */
728 * Handling of special 386 registers and descriptor tables etc
730 /* void lgdt(struct region_descriptor *rdp); */
732 /* reload the descriptor table */
736 /* flush the prefetch q */
740 /* reload "stale" selectors */
749 /* reload code selector by turning return into intersegmental return */
756 * void lidt(struct region_descriptor *rdp);
764 * void lldt(u_short sel)
771 * void ltr(u_short sel)
777 /* ssdtosd(*ssdp,*sdp) */
814 /* void load_cr3(caddr_t cr3) */
816 #if defined(SWTCH_OPTIM_STATS)
817 incl _tlb_flush_count
828 /* void load_cr4(caddr_t cr4) */
834 /* void reset_dbregs() */
837 movl %eax,%dr7 /* disable all breapoints first */
845 /*****************************************************************************/
846 /* setjump, longjump */
847 /*****************************************************************************/
851 movl %ebx,(%eax) /* save ebx */
852 movl %esp,4(%eax) /* save esp */
853 movl %ebp,8(%eax) /* save ebp */
854 movl %esi,12(%eax) /* save esi */
855 movl %edi,16(%eax) /* save edi */
856 movl (%esp),%edx /* get rta */
857 movl %edx,20(%eax) /* save eip */
858 xorl %eax,%eax /* return(0); */
863 movl (%eax),%ebx /* restore ebx */
864 movl 4(%eax),%esp /* restore esp */
865 movl 8(%eax),%ebp /* restore ebp */
866 movl 12(%eax),%esi /* restore esi */
867 movl 16(%eax),%edi /* restore edi */
868 movl 20(%eax),%edx /* get rta */
869 movl %edx,(%esp) /* put in return frame */
870 xorl %eax,%eax /* return(1); */
875 * Support for BB-profiling (gcc -a). The kernbb program will extract
876 * the data from the kernel.
886 NON_GPROF_ENTRY(__bb_init_func)
892 .byte 0xc3 /* avoid macro for `ret' */