2 * Copyright (c) 2004 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * $DragonFly: src/sys/platform/pc32/i386/bcopy.s,v 1.6 2004/07/16 05:48:29 dillon Exp $
37 * bcopy(source:%esi, target:%edi, count:%ecx)
39 * note: esi, edi, eax, ecx, and edx may be destroyed
44 #include <machine/asmacros.h>
45 #include <machine/cputypes.h>
46 #include <machine/pmap.h>
47 #include <machine/specialreg.h>
54 * bcopyb() is a 'dumb' byte-granular bcopy. It is only used by
55 * devices which need to bcopy device-mapped memory which cannot
56 * otherwise handle 16 or 32 bit ops.
67 cmpl %ecx,%eax /* overlapping && src < dst? */
69 cld /* nope, copy forwards */
78 addl %ecx,%edi /* copy backwards. */
92 * If memcpy/bcopy is called as part of a copyin or copyout, the
93 * on-fault routine is set up to do a 'ret'. We have to restore
94 * %ebx and return to the copyin/copyout fault handler.
98 addl $4,%esp /* skip normal return vector */
99 ret /* return to copyin/copyout fault handler */
102 * GENERIC BCOPY() - COPY DIRECTION CHECK AND FORWARDS COPY
104 * Reasonably optimal on all modern machines.
108 ENTRY(asm_generic_memcpy) /* memcpy() entry point use optimal copy */
110 pushl $generic_onfault
114 ENTRY(asm_generic_bcopy)
116 pushl $generic_onfault
117 cmpl %esi,%edi /* if (edi < esi) fwd copy ok */
120 cmpl %esi,%edi /* if (edi < esi + count) do bkwrds copy */
159 * GENERIC_BCOPY() - BACKWARDS COPY
203 * MMX BCOPY() - COPY DIRECTION CHECK AND FORWARDS COPY
205 * note: esi, edi, eax, ecx, and edx are allowed to be destroyed.
207 * In order for the kernel to be able to use the FPU:
209 * (1) The kernel may not already be using the fpu.
211 * (2) If the fpu is owned by the application, we must save
212 * its state. If the fpu is not owned by the application
213 * the application's saved fp state may already exist
216 * (3) We cannot allow the kernel to overwrite the application's
217 * FPU state with our own, so we make sure the application's
218 * FPU state has been saved and then point TD_SAVEFPU at a
219 * temporary fpu save area in the globaldata structure.
223 * If gd_npxthread is not NULL we must save the application's
224 * current FP state to the current save area and then NULL
225 * out gd_npxthread to interlock against new interruptions
226 * changing the FP state further.
228 * If gd_npxthread is NULL the FP unit is in a known 'safe'
229 * state and may be used once the new save area is installed.
231 * race(1): If an interrupt occurs just prior to calling fxsave
232 * all that happens is that fxsave gets a npxdna trap, restores
233 * the app's environment, and immediately traps, restores,
234 * and saves it again.
236 * race(2): No interrupt can safely occur after we NULL-out
237 * npxthread until we fninit, because the kernel assumes that
238 * the FP unit is in a safe state when npxthread is NULL. It's
239 * more convenient to use a cli sequence here (it is not
240 * considered to be in the critical path), but a critical
241 * section would also work.
243 * race(3): The FP unit is in a known state (because npxthread
244 * was either previously NULL or we saved and init'd and made
245 * it NULL). This is true even if we are preempted and the
246 * preempting thread uses the FP unit, because it will be
247 * fninit's again on return. ANY STATE WE SAVE TO THE FPU MAY
248 * BE DESTROYED BY PREEMPTION WHILE NPXTHREAD IS NULL! However,
249 * an interrupt occuring inbetween clts and the setting of
250 * gd_npxthread may set the TS bit again and cause the next
251 * npxdna() to panic when it sees a non-NULL gd_npxthread.
253 * We can safely set TD_SAVEFPU to point to a new uninitialized
254 * save area and then set GD_NPXTHREAD to non-NULL. If an
255 * interrupt occurs after we set GD_NPXTHREAD, all that happens
256 * is that the safe FP state gets saved and restored. We do not
257 * need to fninit again.
259 * We can safely clts after setting up the new save-area, before
260 * installing gd_npxthread, even if we get preempted just after
261 * calling clts. This is because the FP unit will be in a safe
262 * state while gd_npxthread is NULL. Setting gd_npxthread will
263 * simply lock-in that safe-state. Calling clts saves
264 * unnecessary trap overhead since we are about to use the FP
265 * unit anyway and don't need to 'restore' any state prior to
268 * MMX+XMM (SSE2): Typical on Athlons, later P4s. 128 bit media insn.
269 * MMX: Typical on XPs and P3s. 64 bit media insn.
272 #define MMX_SAVE_BLOCK(missfunc) \
275 movl MYCPU,%eax ; /* EAX = MYCPU */ \
276 btsl $1,GD_FPU_LOCK(%eax) ; \
280 movl GD_CURTHREAD(%eax),%edx ; /* EDX = CURTHREAD */ \
281 movl TD_SAVEFPU(%edx),%ebx ; /* save app save area */\
282 addl $TDPRI_CRIT,TD_PRI(%edx) ; \
283 cmpl $0,GD_NPXTHREAD(%eax) ; \
285 fxsave 0(%ebx) ; /* race(1) */ \
286 movl $0,GD_NPXTHREAD(%eax) ; /* interlock intr */ \
288 fninit ; /* race(2) */ \
290 leal GD_SAVEFPU(%eax),%ecx ; \
291 movl %ecx,TD_SAVEFPU(%edx) ; \
293 movl %edx,GD_NPXTHREAD(%eax) ; /* race(3) */ \
294 subl $TDPRI_CRIT,TD_PRI(%edx) ; /* crit_exit() */ \
295 cmpl $0,GD_REQFLAGS(%eax) ; \
297 cmpl $TDPRI_CRIT,TD_PRI(%edx) ; \
299 call lwkt_yield_quick ; \
300 /* note: eax,ecx,edx destroyed */ \
303 movl $mmx_onfault,(%esp) ; \
306 * When restoring the application's FP state we must first clear
307 * npxthread to prevent further saves, then restore the pointer
308 * to the app's save area. We do not have to (and should not)
309 * restore the app's FP state now. Note that we do not have to
310 * call fninit because our use of the FP guarentees that it is in
311 * a 'safe' state (at least for kernel use).
313 * NOTE: it is not usually safe to mess with CR0 outside of a
314 * critical section, because TS may get set by a preemptive
315 * interrupt. However, we *can* race a load/set-ts/store against
316 * an interrupt doing the same thing.
319 #define MMX_RESTORE_BLOCK \
323 #define MMX_RESTORE_BLOCK2 \
325 movl GD_CURTHREAD(%ecx),%edx ; \
326 movl $0,GD_NPXTHREAD(%ecx) ; \
327 movl %ebx,TD_SAVEFPU(%edx) ; \
332 movl $0,GD_FPU_LOCK(%ecx)
335 * xmm/mmx_onfault routine. Restore the fpu state, skip the normal
336 * return vector, and return to the caller's on-fault routine
337 * (which was pushed on the callers stack just before he called us)
346 * MXX entry points - only support 64 bit media instructions
349 ENTRY(asm_mmx_memcpy) /* memcpy() entry point use optimal copy */
350 MMX_SAVE_BLOCK(asm_generic_memcpy)
355 MMX_SAVE_BLOCK(asm_generic_bcopy)
356 cmpl %esi,%edi /* if (edi < esi) fwd copy ok */
359 cmpl %esi,%edi /* if (edi < esi + count) do bkwrds copy */
365 * XMM entry points - support 128 bit media instructions
368 ENTRY(asm_xmm_memcpy) /* memcpy() entry point use optimal copy */
369 MMX_SAVE_BLOCK(asm_generic_memcpy)
374 MMX_SAVE_BLOCK(asm_generic_bcopy)
375 cmpl %esi,%edi /* if (edi < esi) fwd copy ok */
378 cmpl %esi,%edi /* if (edi < esi + count) do bkwrds copy */
382 movl %esi,%eax /* skip xmm if the data is not aligned */
393 movdqa 16(%esi),%xmm1
394 movdqa 32(%esi),%xmm2
395 movdqa 48(%esi),%xmm3
396 movdqa 64(%esi),%xmm4
397 movdqa 80(%esi),%xmm5
398 movdqa 96(%esi),%xmm6
399 movdqa 112(%esi),%xmm7
400 /*prefetchnta 128(%esi) 3dNOW */
404 * movdqa or movntdq can be used.
407 movdqa %xmm1,16(%edi)
408 movdqa %xmm2,32(%edi)
409 movdqa %xmm3,48(%edi)
410 movdqa %xmm4,64(%edi)
411 movdqa %xmm5,80(%edi)
412 movdqa %xmm6,96(%edi)
413 movdqa %xmm7,112(%edi)
431 /*prefetchnta 128(%esi) 3dNOW */
455 * GENERIC_BCOPY() - BACKWARDS COPY
457 * Don't bother using xmm optimizations, just stick with mmx.
474 /*prefetchnta -128(%esi)*/