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