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 $
36 #include <machine/asmacros.h>
37 #include <machine/cputypes.h>
38 #include <machine/pmap.h>
39 #include <machine/specialreg.h>
49 .long asm_generic_memcpy
53 .long asm_generic_bcopy
57 .long asm_generic_bcopy
61 /* fillw(pat, base, cnt) */
74 * void bcopy(const void *s, void *d, size_t count)
76 * Normal bcopy() vector, an optimized bcopy may be installed in
82 movl 4+8(%esp),%esi /* caddr_t from */
83 movl 8+8(%esp),%edi /* caddr_t to */
84 movl 12+8(%esp),%ecx /* size_t len */
91 * Generic (integer-only) bcopy() vector.
96 movl 4+8(%esp),%esi /* caddr_t from */
97 movl 8+8(%esp),%edi /* caddr_t to */
98 movl 12+8(%esp),%ecx /* size_t len */
99 call asm_generic_bcopy
107 movl 4+8(%esp),%esi /* caddr_t from */
108 movl 8+8(%esp),%edi /* caddr_t to */
109 movl 12+8(%esp),%ecx /* size_t len */
116 * void *memcpy(void *d, const void *s, size_t count)
118 * Note: memcpy does not have to support overlapping copies.
120 * Note: (d, s) arguments reversed from bcopy, and memcpy() returns d
121 * while bcopy() returns void.
136 * A stack-based on-fault routine is used for more complex PCB_ONFAULT
137 * situations (such as memcpy/bcopy/bzero). In this case the on-fault
138 * routine must be pushed on the stack.
143 /*****************************************************************************/
144 /* copyout and fubyte family */
145 /*****************************************************************************/
147 * Access user memory from inside the kernel. These routines and possibly
148 * the math- and DOS emulators should be the only places that do this.
150 * We have to access the memory with user's permissions, so use a segment
151 * selector with RPL 3. For writes to user space we have to additionally
152 * check the PTE for write permission, because the 386 does not check
153 * write permissions when we are executing with EPL 0. The 486 does check
154 * this if the WP bit is set in CR0, so we can use a simpler version here.
156 * These routines set curpcb->onfault for the time they execute. When a
157 * protection violation occurs inside the functions, the trap handler
158 * returns to *curpcb->onfault instead of the function.
162 * copyout(from_kernel, to_user, len) - MP SAFE
165 movl PCPU(curthread),%eax
166 movl TD_PCB(%eax),%eax
170 pushl $copyout_fault2
171 movl $stack_onfault,PCB_ONFAULT(%eax)
174 movl 12+16(%esp),%ebx
175 testl %ebx,%ebx /* anything to do? */
179 * Check explicitly for non-user addresses. If 486 write protection
180 * is being used, this check is essential because we are in kernel
181 * mode so the h/w does not provide any protection against writing
186 * First, prevent address wrapping.
192 * XXX STOP USING VM_MAX_USER_ADDRESS.
193 * It is an end address, not a max, so every time it is used correctly it
194 * looks like there is an off by one error, and of course it caused an off
195 * by one error in several places.
197 cmpl $VM_MAX_USER_ADDRESS,%eax
201 * Convert copyout to memcpy_vector(dest:%edi, src:%esi, count:%ecx)
211 movl PCPU(curthread),%edx
213 movl TD_PCB(%edx),%edx
217 movl %eax,PCB_ONFAULT(%edx)
222 addl $4,%esp /* skip pushed copyout_fault vector */
227 movl PCPU(curthread),%edx
228 movl TD_PCB(%edx),%edx
229 movl $0,PCB_ONFAULT(%edx)
234 * copyin(from_user, to_kernel, len) - MP SAFE
238 movl PCPU(curthread),%eax
239 movl TD_PCB(%eax),%eax
243 movl $stack_onfault,PCB_ONFAULT(%eax)
244 movl 4+12(%esp),%esi /* caddr_t from */
245 movl 8+12(%esp),%edi /* caddr_t to */
246 movl 12+12(%esp),%ecx /* size_t len */
249 * make sure address is valid
254 cmpl $VM_MAX_USER_ADDRESS,%edx
258 * Call memcpy(destination:%edi, source:%esi, bytes:%ecx)
263 * return 0 (no error)
266 movl PCPU(curthread),%edx
268 movl TD_PCB(%edx),%edx
271 movl %eax,PCB_ONFAULT(%edx)
279 addl $4,%esp /* skip pushed copyin_fault vector */
283 movl PCPU(curthread),%edx
284 movl TD_PCB(%edx),%edx
285 movl $0,PCB_ONFAULT(%edx)
290 * casuword. Compare and set user word. Returns -1 or the current value.
294 movl PCPU(curthread),%ecx
295 movl TD_PCB(%ecx),%ecx
296 movl $fusufault,PCB_ONFAULT(%ecx)
297 movl 4(%esp),%edx /* dst */
298 movl 8(%esp),%eax /* old */
299 movl 12(%esp),%ecx /* new */
301 cmpl $VM_MAX_USER_ADDRESS-4,%edx /* verify address is valid */
307 cmpxchgl %ecx,(%edx) /* Compare and set. */
310 * The old value is in %eax. If the store succeeded it will be the
311 * value we expected (old) from before the store, otherwise it will
312 * be the current value.
315 movl PCPU(curthread),%ecx
316 movl TD_PCB(%ecx),%ecx
317 movl $fusufault,PCB_ONFAULT(%ecx)
318 movl $0,PCB_ONFAULT(%ecx)
323 * fu{byte,sword,word} - MP SAFE
325 * Fetch a byte (sword, word) from user memory
328 movl PCPU(curthread),%ecx
329 movl TD_PCB(%ecx),%ecx
330 movl $fusufault,PCB_ONFAULT(%ecx)
331 movl 4(%esp),%edx /* from */
333 cmpl $VM_MAX_USER_ADDRESS-4,%edx /* verify address is valid */
337 movl $0,PCB_ONFAULT(%ecx)
344 movl PCPU(curthread),%ecx
345 movl TD_PCB(%ecx),%ecx
346 movl $fusufault,PCB_ONFAULT(%ecx)
349 cmpl $VM_MAX_USER_ADDRESS-2,%edx
353 movl $0,PCB_ONFAULT(%ecx)
360 movl PCPU(curthread),%ecx
361 movl TD_PCB(%ecx),%ecx
362 movl $fusufault,PCB_ONFAULT(%ecx)
365 cmpl $VM_MAX_USER_ADDRESS-1,%edx
369 movl $0,PCB_ONFAULT(%ecx)
374 movl PCPU(curthread),%ecx
375 movl TD_PCB(%ecx),%ecx
377 movl %eax,PCB_ONFAULT(%ecx)
382 * su{byte,sword,word,word32} - MP SAFE
384 * Write a long to user memory
387 movl PCPU(curthread),%ecx
388 movl TD_PCB(%ecx),%ecx
389 movl $fusufault,PCB_ONFAULT(%ecx)
392 cmpl $VM_MAX_USER_ADDRESS-4,%edx /* verify address validity */
398 movl PCPU(curthread),%ecx
399 movl TD_PCB(%ecx),%ecx
400 movl %eax,PCB_ONFAULT(%ecx)
404 * Write an integer to user memory
407 movl PCPU(curthread),%ecx
408 movl TD_PCB(%ecx),%ecx
409 movl $fusufault,PCB_ONFAULT(%ecx)
412 cmpl $VM_MAX_USER_ADDRESS-4,%edx /* verify address validity */
418 movl PCPU(curthread),%ecx
419 movl TD_PCB(%ecx),%ecx
420 movl %eax,PCB_ONFAULT(%ecx)
427 movl PCPU(curthread),%ecx
428 movl TD_PCB(%ecx),%ecx
429 movl $fusufault,PCB_ONFAULT(%ecx)
432 cmpl $VM_MAX_USER_ADDRESS-2,%edx /* verify address validity */
438 movl PCPU(curthread),%ecx /* restore trashed register */
439 movl TD_PCB(%ecx),%ecx
440 movl %eax,PCB_ONFAULT(%ecx)
447 movl PCPU(curthread),%ecx
448 movl TD_PCB(%ecx),%ecx
449 movl $fusufault,PCB_ONFAULT(%ecx)
452 cmpl $VM_MAX_USER_ADDRESS-1,%edx /* verify address validity */
458 movl PCPU(curthread),%ecx /* restore trashed register */
459 movl TD_PCB(%ecx),%ecx
460 movl %eax,PCB_ONFAULT(%ecx)
464 * copyinstr(from, to, maxlen, int *lencopied) - MP SAFE
466 * copy a string from from to to, stop when a 0 character is reached.
467 * return ENAMETOOLONG if string is longer than maxlen, and
468 * EFAULT on protection violations. If lencopied is non-zero,
469 * return the actual length in *lencopied.
474 movl PCPU(curthread),%ecx
475 movl TD_PCB(%ecx),%ecx
476 movl $cpystrflt,PCB_ONFAULT(%ecx)
478 movl 12(%esp),%esi /* %esi = from */
479 movl 16(%esp),%edi /* %edi = to */
480 movl 20(%esp),%edx /* %edx = maxlen */
482 movl $VM_MAX_USER_ADDRESS,%eax
484 /* make sure 'from' is within bounds */
488 /* restrict maxlen to <= VM_MAX_USER_ADDRESS-from */
506 /* Success -- 0 byte reached */
511 /* edx is zero - return ENAMETOOLONG or EFAULT */
512 cmpl $VM_MAX_USER_ADDRESS,%esi
515 movl $ENAMETOOLONG,%eax
522 /* set *lencopied and return %eax */
523 movl PCPU(curthread),%ecx
524 movl TD_PCB(%ecx),%ecx
525 movl $0,PCB_ONFAULT(%ecx)
539 * copystr(from, to, maxlen, int *lencopied) - MP SAFE
545 movl 12(%esp),%esi /* %esi = from */
546 movl 16(%esp),%edi /* %edi = to */
547 movl 20(%esp),%edx /* %edx = maxlen */
558 /* Success -- 0 byte reached */
563 /* edx is zero -- return ENAMETOOLONG */
564 movl $ENAMETOOLONG,%eax
567 /* set *lencopied and return %eax */
589 cld /* compare forwards */
608 * Handling of special 386 registers and descriptor tables etc
610 /* void lgdt(struct region_descriptor *rdp); */
612 /* reload the descriptor table */
616 /* flush the prefetch q */
620 /* reload "stale" selectors */
630 /* reload code selector by turning return into intersegmental return */
637 * void lidt(struct region_descriptor *rdp);
645 * void lldt(u_short sel)
652 * void ltr(u_short sel)
658 /* ssdtosd(*ssdp,*sdp) */
695 /* void load_cr3(caddr_t cr3) */
697 #if defined(SWTCH_OPTIM_STATS)
698 incl _tlb_flush_count
709 /* void load_cr4(caddr_t cr4) */
715 /* void reset_dbregs() */
718 movl %eax,%dr7 /* disable all breapoints first */
726 /*****************************************************************************/
727 /* setjump, longjump */
728 /*****************************************************************************/
732 movl %ebx,(%eax) /* save ebx */
733 movl %esp,4(%eax) /* save esp */
734 movl %ebp,8(%eax) /* save ebp */
735 movl %esi,12(%eax) /* save esi */
736 movl %edi,16(%eax) /* save edi */
737 movl (%esp),%edx /* get rta */
738 movl %edx,20(%eax) /* save eip */
739 xorl %eax,%eax /* return(0); */
744 movl (%eax),%ebx /* restore ebx */
745 movl 4(%eax),%esp /* restore esp */
746 movl 8(%eax),%ebp /* restore ebp */
747 movl 12(%eax),%esi /* restore esi */
748 movl 16(%eax),%edi /* restore edi */
749 movl 20(%eax),%edx /* get rta */
750 movl %edx,(%esp) /* put in return frame */
751 xorl %eax,%eax /* return(1); */
756 * Support for reading MSRs in the safe manner.
759 /* int rdmsr_safe(u_int msr, uint64_t *data) */
760 movl PCPU(curthread),%ecx
761 movl TD_PCB(%ecx), %ecx
762 movl $msr_onfault,PCB_ONFAULT(%ecx)
771 movl PCPU(curthread),%ecx
772 movl TD_PCB(%ecx), %ecx
773 movl %eax,PCB_ONFAULT(%ecx)
778 * MSR operations fault handler
782 movl PCPU(curthread),%ecx
783 movl TD_PCB(%ecx), %ecx
784 movl $0,PCB_ONFAULT(%ecx)
789 * Support for BB-profiling (gcc -a). The kernbb program will extract
790 * the data from the kernel.
800 NON_GPROF_ENTRY(__bb_init_func)
806 .byte 0xc3 /* avoid macro for `ret' */