4a5090ce9606c646d58564223dd016b09123da24
[sbcl.git] / src / runtime / x86-assem.S
1 /*
2  * very-low-level utilities for runtime support
3  */
4
5 /*
6  * This software is part of the SBCL system. See the README file for
7  * more information.
8  *
9  * This software is derived from the CMU CL system, which was
10  * written at Carnegie Mellon University and released into the
11  * public domain. The software is in the public domain and is
12  * provided with absolutely no warranty. See the COPYING and CREDITS
13  * files for more information.
14  */
15 \f
16 #define LANGUAGE_ASSEMBLY
17 #include "sbcl.h"
18 #include "validate.h"
19 #include "genesis/closure.h"
20 #include "genesis/funcallable-instance.h"
21 #include "genesis/fdefn.h"
22 #include "genesis/static-symbols.h"
23 #include "genesis/symbol.h"
24 #include "genesis/thread.h"
25         
26 /* Minimize conditionalization for different OS naming schemes. 
27  *
28  * (As of sbcl-0.8.10, this seems no longer to be much of an issue, 
29  * since everyone has converged on ELF. If this generality really 
30  * turns out not to matter, perhaps it's just clutter we could get
31  * rid of? -- WHN 2004-04-18)
32  *
33  * (Except Win32, which is unlikely ever to be ELF, sorry. -- AB 2005-12-08)
34  */
35 #if defined __linux__  || defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ || defined __sun
36 #define GNAME(var) var
37 #else
38 #define GNAME(var) _##var
39 #endif
40
41 /* Get the right type of alignment. Linux, FreeBSD and NetBSD (but not OpenBSD)
42  * want alignment in bytes. 
43  *
44  * (As in the GNAME() definitions above, as of sbcl-0.8.10, this seems 
45  * no longer to be much of an issue, since everyone has converged on
46  * the same value. If this generality really turns out not to 
47  * matter any more, perhaps it's just clutter we could get
48  * rid of? -- WHN 2004-04-18)
49  */
50 #if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__sun) || defined(LISP_FEATURE_WIN32)
51 #define align_4byte     4
52 #define align_8byte     8
53 #define align_16byte    16
54 #else
55 #define align_4byte     2
56 #define align_8byte     3
57 #define align_16byte    4       
58 #endif                  
59
60 /*
61  * The assembler used for win32 doesn't like .type or .size directives,
62  * so we want to conditionally kill them out. So let's wrap them in macros
63  * that are defined to be no-ops on win32. Hopefully this still works on
64  * other platforms.
65  */
66 #if !defined(LISP_FEATURE_WIN32) && !defined(LISP_FEATURE_DARWIN)
67 #define TYPE(name) .type name,@function
68 #define SIZE(name) .size name,.-name
69 #else
70 #define TYPE(name)
71 #define SIZE(name)
72 #endif
73
74 /*
75  * x86/darwin (as of MacOS X 10.4.5) doesn't reliably file signal
76  * handlers (SIGTRAP or Mach exception handlers) for 0xCC, wo we have
77  * to use ud2 instead. ud2 is an undefined opcode, #x0b0f, or
78  * 0F 0B in low-endian notation, that causes SIGILL to fire. We check
79  * for this instruction in the SIGILL handler and if we see it, we
80  * advance the EIP by two bytes to skip over ud2 instruction and
81  * call sigtrap_handler. */
82 #if defined(LISP_FEATURE_DARWIN)
83 #define END()
84 #define TRAP ud2
85 #else
86 #define END() .end
87 #define TRAP int3
88 #endif
89
90         .text
91         .globl  GNAME(all_threads)
92 \f
93 /*
94  * A call to call_into_c preserves esi, edi, and ebp.   
95  * (The C function will preserve ebx, esi, edi, and ebp across its
96  * function call, but we trash ebx ourselves by using it to save the
97  * return Lisp address.)
98  *
99  * Return values are in eax and maybe edx for quads, or st(0) for
100  * floats.
101  *
102  * This should work for Lisp calls C calls Lisp calls C..
103  *
104  * FIXME & OAOOM: This duplicates call-out in src/compiler/x86/c-call.lisp,
105  * so if you tweak this, change that too!
106  */
107         .text
108         .align  align_16byte,0x90
109         .globl GNAME(call_into_c)
110         TYPE(GNAME(call_into_c))
111 GNAME(call_into_c):
112 /* Save the return Lisp address in ebx. */
113         popl    %ebx
114
115 /* Setup the NPX for C */
116         fstp    %st(0)
117         fstp    %st(0)
118         fstp    %st(0)
119         fstp    %st(0)
120         fstp    %st(0)
121         fstp    %st(0)
122         fstp    %st(0)
123         fstp    %st(0)
124
125         call    *%eax             # normal callout using Lisp stack
126         movl    %eax,%ecx         # remember integer return value
127
128 /* Check for a return FP value. */
129         fxam
130         fnstsw  %ax
131         andl    $0x4500,%eax
132         cmpl    $0x4100,%eax
133         jne     Lfp_rtn_value
134
135 /* The return value is in eax, or eax,edx? */
136 /* Set up the NPX stack for Lisp. */
137         fldz                    # Ensure no regs are empty.
138         fldz
139         fldz
140         fldz
141         fldz
142         fldz
143         fldz
144         fldz
145
146 /* Restore the return value. */
147         movl    %ecx,%eax       # maybe return value
148
149 /* Return. */
150         jmp     *%ebx
151
152 Lfp_rtn_value:
153 /* The return result is in st(0). */
154 /* Set up the NPX stack for Lisp, placing the result in st(0). */
155         fldz                    # Ensure no regs are empty.
156         fldz
157         fldz
158         fldz
159         fldz
160         fldz
161         fldz
162         fxch    %st(7)          # Move the result back to st(0).
163
164 /* We don't need to restore eax, because the result is in st(0). */
165
166 /* Return. FIXME: It would be nice to restructure this to use RET. */   
167         jmp     *%ebx
168
169         SIZE(GNAME(call_into_c))
170
171 \f
172         .text   
173         .globl GNAME(call_into_lisp_first_time)
174         TYPE(GNAME(call_into_lisp_first_time))
175                 
176 /* The *ALIEN-STACK* pointer is set up on the first call_into_lisp when
177  * the stack changes.  We don't worry too much about saving registers 
178  * here, because we never expect to return from the initial call to lisp 
179  * anyway */
180         
181         .align  align_16byte,0x90
182 GNAME(call_into_lisp_first_time):
183         pushl   %ebp            # Save old frame pointer.
184         movl    %esp,%ebp       # Establish new frame.
185 #ifndef LISP_FEATURE_WIN32
186         movl    %esp,ALIEN_STACK + SYMBOL_VALUE_OFFSET
187         movl    GNAME(all_threads),%eax
188         /* pthread machinery takes care of this for other threads */
189         movl    THREAD_CONTROL_STACK_END_OFFSET(%eax) ,%esp
190 #else
191 /* Win32 -really- doesn't like you switching stacks out from under it. */
192         movl    GNAME(all_threads),%eax
193 #endif
194         jmp     Lstack
195 \f
196         .text   
197         .globl GNAME(call_into_lisp)
198         TYPE(GNAME(call_into_lisp))
199                 
200 /* The C conventions require that ebx, esi, edi, and ebp be preserved
201  * across function calls. */
202         
203         .align  align_16byte,0x90
204 GNAME(call_into_lisp):
205         pushl   %ebp            # Save old frame pointer.
206         movl    %esp,%ebp       # Establish new frame.
207 Lstack:
208 /* Save the NPX state */
209         fwait                   # Catch any pending NPX exceptions.
210         subl    $108,%esp       # Make room for the NPX state.
211         fnsave  (%esp)          # save and reset NPX
212
213         movl    (%esp),%eax     # Load NPX control word.
214         andl    $0xfffff2ff,%eax        # Set rounding mode to nearest.
215         orl     $0x00000200,%eax        # Set precision to 64 bits.  (53-bit mantissa)
216         pushl   %eax
217         fldcw   (%esp)          # Recover modes.
218         popl    %eax
219
220         fldz                    # Ensure no FP regs are empty.
221         fldz
222         fldz
223         fldz
224         fldz
225         fldz
226         fldz
227         fldz
228         
229 /* Save C regs: ebx esi edi. */
230         pushl   %ebx
231         pushl   %esi
232         pushl   %edi
233         
234 /* Clear descriptor regs. */
235         xorl    %eax,%eax       # lexenv
236         xorl    %ebx,%ebx       # available
237         xorl    %ecx,%ecx       # arg count
238         xorl    %edx,%edx       # first arg
239         xorl    %edi,%edi       # second arg
240         xorl    %esi,%esi       # third arg
241
242 /* no longer in function call */
243         movl    %esp,%ebx       # remember current stack
244         pushl   %ebx            # Save entry stack on (maybe) new stack.
245
246         /* Establish Lisp args. */
247         movl     8(%ebp),%eax   # lexenv?
248         movl    12(%ebp),%ebx   # address of arg vec
249         movl    16(%ebp),%ecx   # num args
250         shll    $2,%ecx         # Make num args into fixnum.
251         cmpl    $0,%ecx
252         je      Ldone
253         movl    (%ebx),%edx     # arg0
254         cmpl    $4,%ecx
255         je      Ldone
256         movl    4(%ebx),%edi    # arg1
257         cmpl    $8,%ecx
258         je      Ldone
259         movl    8(%ebx),%esi    # arg2
260 Ldone:  
261         /* Registers eax, ecx, edx, edi, and esi are now live. */
262
263 #ifdef LISP_FEATURE_WIN32
264         /* Establish an SEH frame. */
265 #ifdef LISP_FEATURE_SB_THREAD
266         /* FIXME: need to save BSP here. */
267 #error "need to save BSP here, but don't know how yet."
268 #else
269         pushl   BINDING_STACK_POINTER + SYMBOL_VALUE_OFFSET
270 #endif
271         pushl   $GNAME(exception_handler_wrapper)
272         pushl   %fs:0
273         movl    %esp, %fs:0
274 #endif
275
276         /* Alloc new frame. */
277         mov     %esp,%ebx       # The current sp marks start of new frame.
278         push    %ebp            # fp in save location S0
279         sub     $8,%esp         # Ensure 3 slots are allocated, one above.
280         mov     %ebx,%ebp       # Switch to new frame.
281
282         call    *CLOSURE_FUN_OFFSET(%eax)
283         
284         /* If the function returned multiple values, it will return to
285            this point.  Lose them */
286         jnc     LsingleValue
287         mov     %ebx, %esp
288 LsingleValue:
289         /* A singled value function returns here */
290
291 #ifdef LISP_FEATURE_WIN32
292         /* Remove our SEH frame. */
293         popl    %fs:0
294         add     $8, %esp
295 #endif
296
297 /* Restore the stack, in case there was a stack change. */
298         popl    %esp            # c-sp
299
300 /* Restore C regs: ebx esi edi. */
301         popl    %edi
302         popl    %esi
303         popl    %ebx
304
305 /* Restore the NPX state. */
306         frstor  (%esp)
307         addl    $108, %esp
308         
309         popl    %ebp            # c-sp
310         movl    %edx,%eax       # c-val
311         ret
312         SIZE(GNAME(call_into_lisp))
313 \f
314 /* support for saving and restoring the NPX state from C */
315         .text
316         .globl  GNAME(fpu_save)
317         TYPE(GNAME(fpu_save))
318         .align  2,0x90
319 GNAME(fpu_save):
320         movl    4(%esp),%eax
321         fnsave  (%eax)          # Save the NPX state. (resets NPX)
322         ret
323         SIZE(GNAME(fpu_save))
324
325         .globl  GNAME(fpu_restore)
326         TYPE(GNAME(fpu_restore))
327         .align  2,0x90
328 GNAME(fpu_restore):
329         movl    4(%esp),%eax
330         frstor  (%eax)          # Restore the NPX state.
331         ret
332         SIZE(GNAME(fpu_restore))
333 \f
334 /*
335  * the undefined-function trampoline
336  */
337         .text
338         .align  align_4byte,0x90
339         .globl GNAME(undefined_tramp)
340         TYPE(GNAME(undefined_tramp))
341         .byte   0, 0, 0, SIMPLE_FUN_HEADER_WIDETAG
342 GNAME(undefined_tramp):
343         TRAP
344         .byte   trap_Error
345         .byte   2
346         .byte   UNDEFINED_FUN_ERROR
347         .byte   sc_DescriptorReg # eax in the Descriptor-reg SC
348         ret
349         SIZE(GNAME(undefined_tramp))
350
351 /*
352  * the closure trampoline
353  */
354         .text
355         .align  align_4byte,0x90
356         .globl GNAME(closure_tramp)
357         TYPE(GNAME(closure_tramp))
358         .byte   0, 0, 0, SIMPLE_FUN_HEADER_WIDETAG
359 GNAME(closure_tramp):
360         movl    FDEFN_FUN_OFFSET(%eax),%eax
361         /* FIXME: The '*' after "jmp" in the next line is from PVE's
362          * patch posted to the CMU CL mailing list Oct 6, 1999. It looks
363          * reasonable, and it certainly seems as though if CMU CL needs it,
364          * SBCL needs it too, but I haven't actually verified that it's
365          * right. It would be good to find a way to force the flow of
366          * control through here to test it. */
367         jmp     *CLOSURE_FUN_OFFSET(%eax)
368         SIZE(GNAME(closure_tramp))
369
370         .text
371         .align  align_4byte,0x90
372         .globl GNAME(funcallable_instance_tramp)
373         TYPE(GNAME(funcallable_instance_tramp))
374 GNAME(funcallable_instance_tramp):
375         movl    FUNCALLABLE_INSTANCE_FUNCTION_OFFSET(%eax),%eax 
376         /* KLUDGE: on this platform, whatever kind of function is in %rax
377          * now, the first word of it contains the address to jump to. */
378         jmp     *CLOSURE_FUN_OFFSET(%eax)
379         SIZE(GNAME(funcallable_instance_tramp))
380         
381 /*
382  * fun-end breakpoint magic
383  */
384         .text
385         .globl  GNAME(fun_end_breakpoint_guts)
386         .align  align_4byte
387 GNAME(fun_end_breakpoint_guts):
388         /* Multiple Value return */
389         jc      multiple_value_return
390         /* Single value return: The eventual return will now use the
391            multiple values return convention but with a return values
392            count of one. */
393         movl    %esp,%ebx       # Setup ebx - the ofp.
394         subl    $4,%esp         # Allocate one stack slot for the return value
395         movl    $4,%ecx         # Setup ecx for one return value.
396         movl    $(NIL),%edi     # default second value
397         movl    $(NIL),%esi     # default third value
398                 
399 multiple_value_return:
400         
401         .globl GNAME(fun_end_breakpoint_trap)
402 GNAME(fun_end_breakpoint_trap):
403         TRAP
404         .byte   trap_FunEndBreakpoint
405         hlt                     # We should never return here.
406
407         .globl GNAME(fun_end_breakpoint_end)
408 GNAME(fun_end_breakpoint_end):
409
410 \f
411         .globl  GNAME(do_pending_interrupt)
412         TYPE(GNAME(do_pending_interrupt))
413         .align  align_4byte,0x90
414 GNAME(do_pending_interrupt):
415         TRAP
416         .byte   trap_PendingInterrupt
417         ret
418         SIZE(GNAME(do_pending_interrupt))
419 \f
420 /* Allocate bytes and return the start of the allocated space
421  * in the specified destination register.
422  *
423  * In the general case the size will be in the destination register.
424  *
425  * All registers must be preserved except the destination.
426  * The C conventions will preserve ebx, esi, edi, and ebp.
427  * So only eax, ecx, and edx need special care here.
428  *
429  * ALLOC factors out the logic of calling alloc(): stack alignment, etc.
430  *
431  * DEFINE_ALLOC_TO_FOO defines an alloction routine.
432  */
433
434 #ifdef LISP_FEATURE_DARWIN
435 #define ALLOC(size)                                             \
436         pushl   %ebp;              /* Save EBP               */ \
437         movl    %esp,%ebp;         /* Save ESP to EBP        */ \
438         pushl   $0;                /* Reserve space for arg  */ \
439         andl    $0xfffffff0,%esp;  /* Align stack to 16bytes */ \
440         movl    size, (%esp);      /* Argument to alloc      */ \
441         call    GNAME(alloc);                                   \
442         movl    %ebp,%esp;         /* Restore ESP from EBP   */ \
443         popl    %ebp;              /* Restore EBP            */
444 #else
445 #define ALLOC(size)                                             \
446         pushl   size;              /* Argument to alloc      */ \
447         call    GNAME(alloc);                                   \
448         addl    $4,%esp;           /* Pop argument           */
449 #endif
450
451 #define DEFINE_ALLOC_TO_EAX(name,size)                          \
452         .globl  GNAME(name);                                    \
453         TYPE(GNAME(name));                                      \
454         .align  align_4byte,0x90;                               \
455 GNAME(name):                                                    \
456         pushl   %ecx;              /* Save ECX and EDX       */ \
457         pushl   %edx;                                           \
458         ALLOC(size)                                             \
459         popl    %edx;              /* Restore ECX and EDX    */ \
460         popl    %ecx;                                           \
461         ret;                                                    \
462         SIZE(GNAME(name))
463
464 #define DEFINE_ALLOC_TO_ECX(name,size)                          \
465         .globl  GNAME(name);                                    \
466         TYPE(GNAME(name));                                      \
467         .align  align_4byte,0x90;                               \
468 GNAME(name):                                                    \
469         pushl   %eax;              /* Save EAX and EDX       */ \
470         pushl   %edx;                                           \
471         ALLOC(size)                                             \
472         movl    %eax,%ecx;         /* Result to destination  */ \
473         popl    %edx;                                           \
474         popl    %eax;                                           \
475         ret;                                                    \
476         SIZE(GNAME(name))
477         
478 #define DEFINE_ALLOC_TO_EDX(name,size)                          \
479         .globl  GNAME(name);                                    \
480         TYPE(GNAME(name));                                      \
481         .align  align_4byte,0x90;                               \
482 GNAME(name):                                                    \
483         pushl   %eax;               /* Save EAX and ECX      */ \
484         pushl   %ecx;                                           \
485         ALLOC(size)                                             \
486         movl    %eax,%edx;          /* Restore EAX and ECX   */ \
487         popl    %ecx;                                           \
488         popl    %eax;                                           \
489         ret;                                                    \
490         SIZE(GNAME(name))
491
492 #define DEFINE_ALLOC_TO_REG(name,reg,size)                      \
493         .globl  GNAME(name);                                    \
494         TYPE(GNAME(name));                                      \
495         .align  align_4byte,0x90;                               \
496 GNAME(name):                                                    \
497         pushl   %eax;              /* Save EAX, ECX, and EDX */ \
498         pushl   %ecx;                                           \
499         pushl   %edx;                                           \
500         ALLOC(size)                                             \
501         movl    %eax,reg;          /* Restore them           */ \
502         popl    %edx;                                           \
503         popl    %ecx;                                           \
504         popl    %eax;                                           \
505         ret;                                                    \
506         SIZE(GNAME(name))
507
508 DEFINE_ALLOC_TO_EAX(alloc_to_eax,%eax)
509 DEFINE_ALLOC_TO_EAX(alloc_8_to_eax,$8)
510 DEFINE_ALLOC_TO_EAX(alloc_16_to_eax,$16)
511
512 DEFINE_ALLOC_TO_ECX(alloc_to_ecx,%ecx)
513 DEFINE_ALLOC_TO_ECX(alloc_8_to_ecx,$8)
514 DEFINE_ALLOC_TO_ECX(alloc_16_to_ecx,$16)
515
516 DEFINE_ALLOC_TO_EDX(alloc_to_edx,%edx)
517 DEFINE_ALLOC_TO_EDX(alloc_8_to_edx,$8)
518 DEFINE_ALLOC_TO_EDX(alloc_16_to_edx,$16)
519
520 DEFINE_ALLOC_TO_REG(alloc_to_ebx,%ebx,%ebx)
521 DEFINE_ALLOC_TO_REG(alloc_8_to_ebx,%ebx,$8)
522 DEFINE_ALLOC_TO_REG(alloc_16_to_ebx,%ebx,$16)
523
524 DEFINE_ALLOC_TO_REG(alloc_to_esi,%esi,%esi)
525 DEFINE_ALLOC_TO_REG(alloc_8_to_esi,%esi,$8)
526 DEFINE_ALLOC_TO_REG(alloc_16_to_esi,%esi,$16)
527
528 DEFINE_ALLOC_TO_REG(alloc_to_edi,%edi,%edi)
529 DEFINE_ALLOC_TO_REG(alloc_8_to_edi,%edi,$8)
530 DEFINE_ALLOC_TO_REG(alloc_16_to_edi,%edi,$16)
531
532 /* Called from lisp when an inline allocation overflows.
533  * Every register except the result needs to be preserved.
534  * We depend on C to preserve ebx, esi, edi, and ebp.
535  * But where necessary must save eax, ecx, edx. */
536
537 #ifdef LISP_FEATURE_SB_THREAD
538 #define START_REGION %fs:THREAD_ALLOC_REGION_OFFSET
539 #else
540 #define START_REGION GNAME(boxed_region)
541 #endif
542
543 #define ALLOC_OVERFLOW(size)                                    \
544         /* Calculate the size for the allocation. */            \
545         subl    START_REGION,size;                              \
546         ALLOC(size)
547
548 /* This routine handles an overflow with eax=crfp+size. So the
549    size=eax-crfp. */
550         .align  align_4byte
551         .globl  GNAME(alloc_overflow_eax)
552         TYPE(GNAME(alloc_overflow_eax))
553 GNAME(alloc_overflow_eax):
554         pushl   %ecx            # Save ecx
555         pushl   %edx            # Save edx
556         ALLOC_OVERFLOW(%eax)
557         popl    %edx    # Restore edx.
558         popl    %ecx    # Restore ecx.
559         ret
560         SIZE(GNAME(alloc_overflow_eax))
561
562         .align  align_4byte
563         .globl  GNAME(alloc_overflow_ecx)
564         TYPE(GNAME(alloc_overflow_ecx))
565 GNAME(alloc_overflow_ecx):
566         pushl   %eax            # Save eax
567         pushl   %edx            # Save edx
568         ALLOC_OVERFLOW(%ecx)
569         movl    %eax,%ecx       # setup the destination.
570         popl    %edx    # Restore edx.
571         popl    %eax    # Restore eax.
572         ret
573         SIZE(GNAME(alloc_overflow_ecx))
574
575         .align  align_4byte
576         .globl  GNAME(alloc_overflow_edx)
577         TYPE(GNAME(alloc_overflow_edx))
578 GNAME(alloc_overflow_edx):
579         pushl   %eax            # Save eax
580         pushl   %ecx            # Save ecx
581         ALLOC_OVERFLOW(%edx)
582         movl    %eax,%edx       # setup the destination.
583         popl    %ecx    # Restore ecx.
584         popl    %eax    # Restore eax.
585         ret
586         SIZE(GNAME(alloc_overflow_edx))
587
588 /* This routine handles an overflow with ebx=crfp+size. So the
589    size=ebx-crfp. */
590         .align  align_4byte
591         .globl  GNAME(alloc_overflow_ebx)
592         TYPE(GNAME(alloc_overflow_ebx))
593 GNAME(alloc_overflow_ebx):
594         pushl   %eax            # Save eax
595         pushl   %ecx            # Save ecx
596         pushl   %edx            # Save edx
597         ALLOC_OVERFLOW(%ebx)
598         movl    %eax,%ebx       # setup the destination.
599         popl    %edx    # Restore edx.
600         popl    %ecx    # Restore ecx.
601         popl    %eax    # Restore eax.
602         ret
603         SIZE(GNAME(alloc_overflow_ebx))
604
605 /* This routine handles an overflow with esi=crfp+size. So the
606    size=esi-crfp. */
607         .align  align_4byte
608         .globl  GNAME(alloc_overflow_esi)
609         TYPE(GNAME(alloc_overflow_esi))
610 GNAME(alloc_overflow_esi):
611         pushl   %eax            # Save eax
612         pushl   %ecx            # Save ecx
613         pushl   %edx            # Save edx
614         ALLOC_OVERFLOW(%esi)
615         movl    %eax,%esi       # setup the destination.
616         popl    %edx    # Restore edx.
617         popl    %ecx    # Restore ecx.
618         popl    %eax    # Restore eax.
619         ret
620         SIZE(GNAME(alloc_overflow_esi))
621
622         .align  align_4byte
623         .globl  GNAME(alloc_overflow_edi)
624         TYPE(GNAME(alloc_overflow_edi))
625 GNAME(alloc_overflow_edi):
626         pushl   %eax            # Save eax
627         pushl   %ecx            # Save ecx
628         pushl   %edx            # Save edx
629         ALLOC_OVERFLOW(%edi)
630         movl    %eax,%edi       # setup the destination.
631         popl    %edx    # Restore edx.
632         popl    %ecx    # Restore ecx.
633         popl    %eax    # Restore eax.
634         ret
635         SIZE(GNAME(alloc_overflow_edi))
636
637
638 #ifdef LISP_FEATURE_WIN32
639         /* The guts of the exception-handling system doesn't use
640          * frame pointers, which manages to throw off backtraces
641          * rather badly.  So here we grab the (known-good) EBP
642          * and EIP from the exception context and use it to fake
643          * up a stack frame which will skip over the system SEH
644          * code. */
645         .align  align_4byte
646         .globl  GNAME(exception_handler_wrapper)
647         TYPE(GNAME(exception_handler_wrapper))
648 GNAME(exception_handler_wrapper):
649         /* Context layout is: */
650         /* 7 dwords before FSA. (0x1c) */
651         /* 8 dwords and 0x50 bytes in the FSA. (0x70/0x8c) */
652         /* 4 dwords segregs. (0x10/0x9c) */
653         /* 6 dwords non-stack GPRs. (0x18/0xb4) */
654         /* EBP (at 0xb4) */
655         /* EIP (at 0xb8) */
656 #define CONTEXT_EBP_OFFSET 0xb4
657 #define CONTEXT_EIP_OFFSET 0xb8
658         /* some other stuff we don't care about. */
659         pushl   %ebp
660         movl    0x10(%esp), %ebp        /* context */
661         pushl   CONTEXT_EIP_OFFSET(%ebp)
662         pushl   CONTEXT_EBP_OFFSET(%ebp)
663         movl    %esp, %ebp
664         pushl   0x1c(%esp)
665         pushl   0x1c(%esp)
666         pushl   0x1c(%esp)
667         pushl   0x1c(%esp)
668         call    GNAME(handle_exception)
669         lea     8(%ebp), %esp
670         popl    %ebp
671         ret
672         SIZE(GNAME(exception_handler_wrapper))
673 #endif
674
675 #ifdef LISP_FEATURE_DARWIN
676         .align align_4byte
677         .globl GNAME(call_into_lisp_tramp)
678         TYPE(GNAME(call_into_lisp_tramp))
679 GNAME(call_into_lisp_tramp):
680         /* 1. build the stack frame from the block that's pointed to by ECX
681            2. free the block
682            3. set ECX to 0
683            4. call the function via call_into_lisp
684         */
685         pushl   0(%ecx)          /* return address */
686
687         pushl   %ebp
688         movl    %esp, %ebp
689
690         pushl   32(%ecx)         /* eflags */
691         pushl   28(%ecx)         /* EAX */
692         pushl   20(%ecx)         /* ECX */
693         pushl   16(%ecx)         /* EDX */
694         pushl   24(%ecx)         /* EBX */
695         pushl   $0                /* popal is going to ignore esp */
696         pushl   %ebp              /* is this right?? */
697         pushl   12(%ecx)         /* ESI */
698         pushl   8(%ecx)          /* EDI */
699         pushl   $0                /* args for call_into_lisp */
700         pushl   $0
701         pushl   4(%ecx)          /* function to call */
702
703         /* free our save block */
704         pushl   %ecx              /* reserve sufficient space on stack for args */
705         pushl   %ecx
706         andl    $0xfffffff0, %esp  /* align stack */
707         movl    $0x40, 4(%esp)
708         movl    %ecx, (%esp)
709         call    GNAME(os_invalidate)
710
711         /* call call_into_lisp */
712         leal    -48(%ebp), %esp
713         call    GNAME(call_into_lisp)
714
715         /* Clean up our mess */
716         leal    -36(%ebp), %esp
717         popal
718         popfl
719         leave
720         ret
721         
722         SIZE(call_into_lisp_tramp)
723 #endif
724         
725         .align  align_4byte,0x90
726         .globl  GNAME(post_signal_tramp)
727         TYPE(GNAME(post_signal_tramp))
728 GNAME(post_signal_tramp):
729         /* this is notionally the second half of a function whose first half
730          * doesn't exist.  This is where call_into_lisp returns when called 
731          * using return_to_lisp_function */
732         addl $12,%esp   /* clear call_into_lisp args from stack */
733         popal           /* restore registers */
734         popfl
735 #ifdef LISP_FEATURE_DARWIN
736         /* skip two padding words */
737         addl $8,%esp
738 #endif
739         leave
740         ret
741         SIZE(GNAME(post_signal_tramp))
742
743
744         /* fast_bzero implementations and code to detect which implementation
745          * to use.
746          */
747 \f
748         .globl GNAME(fast_bzero_pointer)
749         .data
750         .align  align_4byte
751 GNAME(fast_bzero_pointer):
752         /* Variable containing a pointer to the bzero function to use.
753          * Initially points to a basic function.  Change this variable
754          * to fast_bzero_detect if OS supports SSE.  */
755         .long GNAME(fast_bzero_base)
756 \f
757         .text
758         .align  align_8byte,0x90
759         .globl GNAME(fast_bzero)
760         TYPE(GNAME(fast_bzero))
761 GNAME(fast_bzero):        
762         /* Indirect function call */
763         jmp *GNAME(fast_bzero_pointer)
764         SIZE(GNAME(fast_bzero))
765         
766 \f      
767         .text
768         .align  align_8byte,0x90
769         .globl GNAME(fast_bzero_detect)
770         TYPE(GNAME(fast_bzero_detect))
771 GNAME(fast_bzero_detect):
772         /* Decide whether to use SSE, MMX or REP version */
773         push %eax /* CPUID uses EAX-EDX */
774         push %ebx
775         push %ecx
776         push %edx
777         mov $1, %eax
778         cpuid
779         test $0x04000000, %edx    /* SSE2 needed for MOVNTDQ */
780         jnz Lsse2
781         /* Originally there was another case here for using the
782          * MOVNTQ instruction for processors that supported MMX but
783          * not SSE2. This turned out to be a loss especially on
784          * Athlons (where this instruction is apparently microcoded
785          * somewhat slowly). So for simplicity revert to REP STOSL
786          * for all non-SSE2 processors.
787          */
788 Lbase:
789         movl $(GNAME(fast_bzero_base)), GNAME(fast_bzero_pointer)
790         jmp Lrestore
791 Lsse2:
792         movl $(GNAME(fast_bzero_sse)), GNAME(fast_bzero_pointer)
793         jmp Lrestore
794         
795 Lrestore:
796         pop %edx
797         pop %ecx
798         pop %ebx
799         pop %eax
800         jmp *GNAME(fast_bzero_pointer)
801         
802         SIZE(GNAME(fast_bzero_detect))
803         
804 \f
805         .text
806         .align  align_8byte,0x90
807         .globl GNAME(fast_bzero_sse)
808         TYPE(GNAME(fast_bzero_sse))
809         
810 GNAME(fast_bzero_sse):
811         /* A fast routine for zero-filling blocks of memory that are
812          * guaranteed to start and end at a 4096-byte aligned address.
813          */        
814         push %esi                 /* Save temporary registers */
815         push %edi
816         mov 16(%esp), %esi        /* Parameter: amount of bytes to fill */
817         mov 12(%esp), %edi        /* Parameter: start address */
818         shr $6, %esi              /* Amount of 64-byte blocks to copy */
819         jz Lend_sse               /* If none, stop */
820         movups %xmm7, -16(%esp)   /* Save XMM register */
821         xorps  %xmm7, %xmm7       /* Zero the XMM register */
822         jmp Lloop_sse
823         .align align_16byte
824 Lloop_sse:
825
826         /* Copy the 16 zeroes from xmm7 to memory, 4 times. MOVNTDQ is the
827          * non-caching double-quadword moving variant, i.e. the memory areas
828          * we're touching are not fetched into the L1 cache, since we're just
829          * going to overwrite the memory soon anyway.
830          */
831         movntdq %xmm7, 0(%edi)
832         movntdq %xmm7, 16(%edi)
833         movntdq %xmm7, 32(%edi)
834         movntdq %xmm7, 48(%edi)
835  
836         add $64, %edi /* Advance pointer */
837         dec %esi      /* Decrement 64-byte block count */
838         jnz Lloop_sse
839         movups -16(%esp), %xmm7 /* Restore the XMM register */
840         sfence        /* Ensure that weakly ordered writes are flushed. */
841 Lend_sse:
842         mov 12(%esp), %esi      /* Parameter: start address */
843         prefetcht0 0(%esi)      /* Prefetch the start of the block into cache,
844                                  * since it's likely to be used immediately. */
845         pop %edi      /* Restore temp registers */
846         pop %esi
847         ret
848         SIZE(GNAME(fast_bzero_sse))
849                 
850 \f
851         .text
852         .align  align_8byte,0x90
853         .globl GNAME(fast_bzero_base)
854         TYPE(GNAME(fast_bzero_base))
855         
856 GNAME(fast_bzero_base):
857         /* A fast routine for zero-filling blocks of memory that are
858          * guaranteed to start and end at a 4096-byte aligned address.
859          */        
860         push %eax                 /* Save temporary registers */
861         push %ecx
862         push %edi
863         mov 20(%esp), %ecx        /* Parameter: amount of bytes to fill */
864         mov 16(%esp), %edi        /* Parameter: start address */
865         xor %eax, %eax            /* Zero EAX */
866         shr $2, %ecx              /* Amount of 4-byte blocks to copy */
867         jz  Lend_base
868
869         rep
870         stosl                     /* Store EAX to *EDI, ECX times, incrementing
871                                    * EDI by 4 after each store */
872         
873 Lend_base:        
874         pop %edi                  /* Restore temp registers */
875         pop %ecx
876         pop %eax
877         ret
878         SIZE(GNAME(fast_bzero_base))
879         
880 \f       
881         END()