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