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