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.21 2008/05/08 01:21:04 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
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_MAX_USER_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_MAX_USER_ADDRESS,%eax
204 * Convert copyout to memcpy_vector(dest:%edi, src:%esi, count:%ecx)
214 movl PCPU(curthread),%edx
216 movl TD_PCB(%edx),%edx
220 movl %eax,PCB_ONFAULT(%edx)
225 addl $4,%esp /* skip pushed copyout_fault vector */
230 movl PCPU(curthread),%edx
231 movl TD_PCB(%edx),%edx
232 movl $0,PCB_ONFAULT(%edx)
237 * copyin(from_user, to_kernel, len) - MP SAFE
241 movl PCPU(curthread),%eax
242 movl TD_PCB(%eax),%eax
246 movl $stack_onfault,PCB_ONFAULT(%eax)
247 movl 4+12(%esp),%esi /* caddr_t from */
248 movl 8+12(%esp),%edi /* caddr_t to */
249 movl 12+12(%esp),%ecx /* size_t len */
252 * make sure address is valid
257 cmpl $VM_MAX_USER_ADDRESS,%edx
261 * Call memcpy(destination:%edi, source:%esi, bytes:%ecx)
266 * return 0 (no error)
269 movl PCPU(curthread),%edx
271 movl TD_PCB(%edx),%edx
274 movl %eax,PCB_ONFAULT(%edx)
282 addl $4,%esp /* skip pushed copyin_fault vector */
286 movl PCPU(curthread),%edx
287 movl TD_PCB(%edx),%edx
288 movl $0,PCB_ONFAULT(%edx)
293 * casuword. Compare and set user word. Returns -1 or the current value.
297 movl PCPU(curthread),%ecx
298 movl TD_PCB(%ecx),%ecx
299 movl $fusufault,PCB_ONFAULT(%ecx)
300 movl 4(%esp),%edx /* dst */
301 movl 8(%esp),%eax /* old */
302 movl 12(%esp),%ecx /* new */
304 cmpl $VM_MAX_USER_ADDRESS-4,%edx /* verify address is valid */
310 cmpxchgl %ecx,(%edx) /* Compare and set. */
313 * The old value is in %eax. If the store succeeded it will be the
314 * value we expected (old) from before the store, otherwise it will
315 * be the current value.
318 movl PCPU(curthread),%ecx
319 movl TD_PCB(%ecx),%ecx
320 movl $fusufault,PCB_ONFAULT(%ecx)
321 movl $0,PCB_ONFAULT(%ecx)
326 * fu{byte,sword,word} - MP SAFE
328 * Fetch a byte (sword, word) from user memory
331 movl PCPU(curthread),%ecx
332 movl TD_PCB(%ecx),%ecx
333 movl $fusufault,PCB_ONFAULT(%ecx)
334 movl 4(%esp),%edx /* from */
336 cmpl $VM_MAX_USER_ADDRESS-4,%edx /* verify address is valid */
340 movl $0,PCB_ONFAULT(%ecx)
347 movl PCPU(curthread),%ecx
348 movl TD_PCB(%ecx),%ecx
349 movl $fusufault,PCB_ONFAULT(%ecx)
352 cmpl $VM_MAX_USER_ADDRESS-2,%edx
356 movl $0,PCB_ONFAULT(%ecx)
363 movl PCPU(curthread),%ecx
364 movl TD_PCB(%ecx),%ecx
365 movl $fusufault,PCB_ONFAULT(%ecx)
368 cmpl $VM_MAX_USER_ADDRESS-1,%edx
372 movl $0,PCB_ONFAULT(%ecx)
377 movl PCPU(curthread),%ecx
378 movl TD_PCB(%ecx),%ecx
380 movl %eax,PCB_ONFAULT(%ecx)
385 * su{byte,sword,word} - MP SAFE
387 * Write a byte (word, longword) to user memory
390 movl PCPU(curthread),%ecx
391 movl TD_PCB(%ecx),%ecx
392 movl $fusufault,PCB_ONFAULT(%ecx)
395 cmpl $VM_MAX_USER_ADDRESS-4,%edx /* verify address validity */
401 movl PCPU(curthread),%ecx
402 movl TD_PCB(%ecx),%ecx
403 movl %eax,PCB_ONFAULT(%ecx)
410 movl PCPU(curthread),%ecx
411 movl TD_PCB(%ecx),%ecx
412 movl $fusufault,PCB_ONFAULT(%ecx)
415 cmpl $VM_MAX_USER_ADDRESS-2,%edx /* verify address validity */
421 movl PCPU(curthread),%ecx /* restore trashed register */
422 movl TD_PCB(%ecx),%ecx
423 movl %eax,PCB_ONFAULT(%ecx)
430 movl PCPU(curthread),%ecx
431 movl TD_PCB(%ecx),%ecx
432 movl $fusufault,PCB_ONFAULT(%ecx)
435 cmpl $VM_MAX_USER_ADDRESS-1,%edx /* verify address validity */
441 movl PCPU(curthread),%ecx /* restore trashed register */
442 movl TD_PCB(%ecx),%ecx
443 movl %eax,PCB_ONFAULT(%ecx)
447 * copyinstr(from, to, maxlen, int *lencopied) - MP SAFE
449 * copy a string from from to to, stop when a 0 character is reached.
450 * return ENAMETOOLONG if string is longer than maxlen, and
451 * EFAULT on protection violations. If lencopied is non-zero,
452 * return the actual length in *lencopied.
457 movl PCPU(curthread),%ecx
458 movl TD_PCB(%ecx),%ecx
459 movl $cpystrflt,PCB_ONFAULT(%ecx)
461 movl 12(%esp),%esi /* %esi = from */
462 movl 16(%esp),%edi /* %edi = to */
463 movl 20(%esp),%edx /* %edx = maxlen */
465 movl $VM_MAX_USER_ADDRESS,%eax
467 /* make sure 'from' is within bounds */
471 /* restrict maxlen to <= VM_MAX_USER_ADDRESS-from */
489 /* Success -- 0 byte reached */
494 /* edx is zero - return ENAMETOOLONG or EFAULT */
495 cmpl $VM_MAX_USER_ADDRESS,%esi
498 movl $ENAMETOOLONG,%eax
505 /* set *lencopied and return %eax */
506 movl PCPU(curthread),%ecx
507 movl TD_PCB(%ecx),%ecx
508 movl $0,PCB_ONFAULT(%ecx)
522 * copystr(from, to, maxlen, int *lencopied) - MP SAFE
528 movl 12(%esp),%esi /* %esi = from */
529 movl 16(%esp),%edi /* %edi = to */
530 movl 20(%esp),%edx /* %edx = maxlen */
541 /* Success -- 0 byte reached */
546 /* edx is zero -- return ENAMETOOLONG */
547 movl $ENAMETOOLONG,%eax
550 /* set *lencopied and return %eax */
572 cld /* compare forwards */
591 * Handling of special 386 registers and descriptor tables etc
593 /* void lgdt(struct region_descriptor *rdp); */
595 /* reload the descriptor table */
599 /* flush the prefetch q */
603 /* reload "stale" selectors */
613 /* reload code selector by turning return into intersegmental return */
620 * void lidt(struct region_descriptor *rdp);
628 * void lldt(u_short sel)
635 * void ltr(u_short sel)
641 /* ssdtosd(*ssdp,*sdp) */
678 /* void load_cr3(caddr_t cr3) */
680 #if defined(SWTCH_OPTIM_STATS)
681 incl _tlb_flush_count
692 /* void load_cr4(caddr_t cr4) */
698 /* void reset_dbregs() */
701 movl %eax,%dr7 /* disable all breapoints first */
709 /*****************************************************************************/
710 /* setjump, longjump */
711 /*****************************************************************************/
715 movl %ebx,(%eax) /* save ebx */
716 movl %esp,4(%eax) /* save esp */
717 movl %ebp,8(%eax) /* save ebp */
718 movl %esi,12(%eax) /* save esi */
719 movl %edi,16(%eax) /* save edi */
720 movl (%esp),%edx /* get rta */
721 movl %edx,20(%eax) /* save eip */
722 xorl %eax,%eax /* return(0); */
727 movl (%eax),%ebx /* restore ebx */
728 movl 4(%eax),%esp /* restore esp */
729 movl 8(%eax),%ebp /* restore ebp */
730 movl 12(%eax),%esi /* restore esi */
731 movl 16(%eax),%edi /* restore edi */
732 movl 20(%eax),%edx /* get rta */
733 movl %edx,(%esp) /* put in return frame */
734 xorl %eax,%eax /* return(1); */
739 * Support for BB-profiling (gcc -a). The kernbb program will extract
740 * the data from the kernel.
750 NON_GPROF_ENTRY(__bb_init_func)
756 .byte 0xc3 /* avoid macro for `ret' */