37fcafaf9faa648e959e2d98af0c442db66ad88b
[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 /* 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         TRAP
342         .byte   trap_Error
343         .byte   2
344         .byte   UNDEFINED_FUN_ERROR
345         .byte   sc_DescriptorReg # eax in the Descriptor-reg SC
346         ret
347         SIZE(GNAME(undefined_tramp))
348
349 /*
350  * the closure trampoline
351  */
352         .text
353         .align  align_16byte,0x90
354         .globl GNAME(closure_tramp)
355         TYPE(GNAME(closure_tramp))
356         .byte   0, 0, 0, SIMPLE_FUN_HEADER_WIDETAG
357 GNAME(closure_tramp):
358         movl    FDEFN_FUN_OFFSET(%eax),%eax
359         /* FIXME: The '*' after "jmp" in the next line is from PVE's
360          * patch posted to the CMU CL mailing list Oct 6, 1999. It looks
361          * reasonable, and it certainly seems as though if CMU CL needs it,
362          * SBCL needs it too, but I haven't actually verified that it's
363          * right. It would be good to find a way to force the flow of
364          * control through here to test it. */
365         jmp     *CLOSURE_FUN_OFFSET(%eax)
366         SIZE(GNAME(closure_tramp))
367
368         .text
369         .align  align_16byte,0x90
370         .globl GNAME(funcallable_instance_tramp)
371         TYPE(GNAME(funcallable_instance_tramp))
372 GNAME(funcallable_instance_tramp):
373         movl    FUNCALLABLE_INSTANCE_FUNCTION_OFFSET(%eax),%eax 
374         /* KLUDGE: on this platform, whatever kind of function is in %rax
375          * now, the first word of it contains the address to jump to. */
376         jmp     *CLOSURE_FUN_OFFSET(%eax)
377         SIZE(GNAME(funcallable_instance_tramp))
378         
379 /*
380  * fun-end breakpoint magic
381  */
382         .text
383         .globl  GNAME(fun_end_breakpoint_guts)
384         .align  align_16byte
385 GNAME(fun_end_breakpoint_guts):
386         /* Multiple Value return */
387         jc      multiple_value_return
388         /* Single value return: The eventual return will now use the
389            multiple values return convention but with a return values
390            count of one. */
391         movl    %esp,%ebx       # Setup ebx - the ofp.
392         subl    $4,%esp         # Allocate one stack slot for the return value
393         movl    $4,%ecx         # Setup ecx for one return value.
394         movl    $(NIL),%edi     # default second value
395         movl    $(NIL),%esi     # default third value
396                 
397 multiple_value_return:
398         
399         .globl GNAME(fun_end_breakpoint_trap)
400 GNAME(fun_end_breakpoint_trap):
401         TRAP
402         .byte   trap_FunEndBreakpoint
403         hlt                     # We should never return here.
404
405         .globl GNAME(fun_end_breakpoint_end)
406 GNAME(fun_end_breakpoint_end):
407
408 \f
409         .globl  GNAME(do_pending_interrupt)
410         TYPE(GNAME(do_pending_interrupt))
411         .align  align_16byte,0x90
412 GNAME(do_pending_interrupt):
413         TRAP
414         .byte   trap_PendingInterrupt
415         ret
416         SIZE(GNAME(do_pending_interrupt))
417 \f
418 /* Allocate bytes and return the start of the allocated space
419  * in the specified destination register.
420  *
421  * In the general case the size will be in the destination register.
422  *
423  * All registers must be preserved except the destination.
424  * The C conventions will preserve ebx, esi, edi, and ebp.
425  * So only eax, ecx, and edx need special care here.
426  *
427  * ALLOC factors out the logic of calling alloc(): stack alignment, etc.
428  *
429  * DEFINE_ALLOC_TO_FOO defines an alloction routine.
430  */
431
432 #ifdef LISP_FEATURE_DARWIN
433 #define ALLOC(size)                                             \
434         pushl   %ebp;              /* Save EBP               */ \
435         movl    %esp,%ebp;         /* Save ESP to EBP        */ \
436         pushl   $0;                /* Reserve space for arg  */ \
437         andl    $0xfffffff0,%esp;  /* Align stack to 16bytes */ \
438         movl    size, (%esp);      /* Argument to alloc      */ \
439         call    GNAME(alloc);                                   \
440         movl    %ebp,%esp;         /* Restore ESP from EBP   */ \
441         popl    %ebp;              /* Restore EBP            */
442 #else
443 #define ALLOC(size)                                             \
444         pushl   size;              /* Argument to alloc      */ \
445         call    GNAME(alloc);                                   \
446         addl    $4,%esp;           /* Pop argument           */
447 #endif
448
449 #define DEFINE_ALLOC_TO_EAX(name,size)                          \
450         .globl  GNAME(name);                                    \
451         TYPE(GNAME(name));                                      \
452         .align  align_16byte,0x90;                              \
453 GNAME(name):                                                    \
454         pushl   %ecx;              /* Save ECX and EDX       */ \
455         pushl   %edx;                                           \
456         ALLOC(size)                                             \
457         popl    %edx;              /* Restore ECX and EDX    */ \
458         popl    %ecx;                                           \
459         ret;                                                    \
460         SIZE(GNAME(name))
461
462 #define DEFINE_ALLOC_TO_ECX(name,size)                          \
463         .globl  GNAME(name);                                    \
464         TYPE(GNAME(name));                                      \
465         .align  align_16byte,0x90;                              \
466 GNAME(name):                                                    \
467         pushl   %eax;              /* Save EAX and EDX       */ \
468         pushl   %edx;                                           \
469         ALLOC(size)                                             \
470         movl    %eax,%ecx;         /* Result to destination  */ \
471         popl    %edx;                                           \
472         popl    %eax;                                           \
473         ret;                                                    \
474         SIZE(GNAME(name))
475         
476 #define DEFINE_ALLOC_TO_EDX(name,size)                          \
477         .globl  GNAME(name);                                    \
478         TYPE(GNAME(name));                                      \
479         .align  align_16byte,0x90;                              \
480 GNAME(name):                                                    \
481         pushl   %eax;               /* Save EAX and ECX      */ \
482         pushl   %ecx;                                           \
483         ALLOC(size)                                             \
484         movl    %eax,%edx;          /* Restore EAX and ECX   */ \
485         popl    %ecx;                                           \
486         popl    %eax;                                           \
487         ret;                                                    \
488         SIZE(GNAME(name))
489
490 #define DEFINE_ALLOC_TO_REG(name,reg,size)                      \
491         .globl  GNAME(name);                                    \
492         TYPE(GNAME(name));                                      \
493         .align  align_16byte,0x90;                              \
494 GNAME(name):                                                    \
495         pushl   %eax;              /* Save EAX, ECX, and EDX */ \
496         pushl   %ecx;                                           \
497         pushl   %edx;                                           \
498         ALLOC(size)                                             \
499         movl    %eax,reg;          /* Restore them           */ \
500         popl    %edx;                                           \
501         popl    %ecx;                                           \
502         popl    %eax;                                           \
503         ret;                                                    \
504         SIZE(GNAME(name))
505
506 DEFINE_ALLOC_TO_EAX(alloc_to_eax,%eax)
507 DEFINE_ALLOC_TO_EAX(alloc_8_to_eax,$8)
508 DEFINE_ALLOC_TO_EAX(alloc_16_to_eax,$16)
509
510 DEFINE_ALLOC_TO_ECX(alloc_to_ecx,%ecx)
511 DEFINE_ALLOC_TO_ECX(alloc_8_to_ecx,$8)
512 DEFINE_ALLOC_TO_ECX(alloc_16_to_ecx,$16)
513
514 DEFINE_ALLOC_TO_EDX(alloc_to_edx,%edx)
515 DEFINE_ALLOC_TO_EDX(alloc_8_to_edx,$8)
516 DEFINE_ALLOC_TO_EDX(alloc_16_to_edx,$16)
517
518 DEFINE_ALLOC_TO_REG(alloc_to_ebx,%ebx,%ebx)
519 DEFINE_ALLOC_TO_REG(alloc_8_to_ebx,%ebx,$8)
520 DEFINE_ALLOC_TO_REG(alloc_16_to_ebx,%ebx,$16)
521
522 DEFINE_ALLOC_TO_REG(alloc_to_esi,%esi,%esi)
523 DEFINE_ALLOC_TO_REG(alloc_8_to_esi,%esi,$8)
524 DEFINE_ALLOC_TO_REG(alloc_16_to_esi,%esi,$16)
525
526 DEFINE_ALLOC_TO_REG(alloc_to_edi,%edi,%edi)
527 DEFINE_ALLOC_TO_REG(alloc_8_to_edi,%edi,$8)
528 DEFINE_ALLOC_TO_REG(alloc_16_to_edi,%edi,$16)
529
530 /* Called from lisp when an inline allocation overflows.
531  * Every register except the result needs to be preserved.
532  * We depend on C to preserve ebx, esi, edi, and ebp.
533  * But where necessary must save eax, ecx, edx. */
534
535 #ifdef LISP_FEATURE_SB_THREAD
536 #define START_REGION %fs:THREAD_ALLOC_REGION_OFFSET
537 #else
538 #define START_REGION GNAME(boxed_region)
539 #endif
540
541 #define ALLOC_OVERFLOW(size)                                    \
542         /* Calculate the size for the allocation. */            \
543         subl    START_REGION,size;                              \
544         ALLOC(size)
545
546 /* This routine handles an overflow with eax=crfp+size. So the
547    size=eax-crfp. */
548         .align  align_16byte
549         .globl  GNAME(alloc_overflow_eax)
550         TYPE(GNAME(alloc_overflow_eax))
551 GNAME(alloc_overflow_eax):
552         pushl   %ecx            # Save ecx
553         pushl   %edx            # Save edx
554         ALLOC_OVERFLOW(%eax)
555         popl    %edx    # Restore edx.
556         popl    %ecx    # Restore ecx.
557         ret
558         SIZE(GNAME(alloc_overflow_eax))
559
560         .align  align_16byte
561         .globl  GNAME(alloc_overflow_ecx)
562         TYPE(GNAME(alloc_overflow_ecx))
563 GNAME(alloc_overflow_ecx):
564         pushl   %eax            # Save eax
565         pushl   %edx            # Save edx
566         ALLOC_OVERFLOW(%ecx)
567         movl    %eax,%ecx       # setup the destination.
568         popl    %edx    # Restore edx.
569         popl    %eax    # Restore eax.
570         ret
571         SIZE(GNAME(alloc_overflow_ecx))
572
573         .align  align_16byte
574         .globl  GNAME(alloc_overflow_edx)
575         TYPE(GNAME(alloc_overflow_edx))
576 GNAME(alloc_overflow_edx):
577         pushl   %eax            # Save eax
578         pushl   %ecx            # Save ecx
579         ALLOC_OVERFLOW(%edx)
580         movl    %eax,%edx       # setup the destination.
581         popl    %ecx    # Restore ecx.
582         popl    %eax    # Restore eax.
583         ret
584         SIZE(GNAME(alloc_overflow_edx))
585
586 /* This routine handles an overflow with ebx=crfp+size. So the
587    size=ebx-crfp. */
588         .align  align_16byte
589         .globl  GNAME(alloc_overflow_ebx)
590         TYPE(GNAME(alloc_overflow_ebx))
591 GNAME(alloc_overflow_ebx):
592         pushl   %eax            # Save eax
593         pushl   %ecx            # Save ecx
594         pushl   %edx            # Save edx
595         ALLOC_OVERFLOW(%ebx)
596         movl    %eax,%ebx       # setup the destination.
597         popl    %edx    # Restore edx.
598         popl    %ecx    # Restore ecx.
599         popl    %eax    # Restore eax.
600         ret
601         SIZE(GNAME(alloc_overflow_ebx))
602
603 /* This routine handles an overflow with esi=crfp+size. So the
604    size=esi-crfp. */
605         .align  align_16byte
606         .globl  GNAME(alloc_overflow_esi)
607         TYPE(GNAME(alloc_overflow_esi))
608 GNAME(alloc_overflow_esi):
609         pushl   %eax            # Save eax
610         pushl   %ecx            # Save ecx
611         pushl   %edx            # Save edx
612         ALLOC_OVERFLOW(%esi)
613         movl    %eax,%esi       # setup the destination.
614         popl    %edx    # Restore edx.
615         popl    %ecx    # Restore ecx.
616         popl    %eax    # Restore eax.
617         ret
618         SIZE(GNAME(alloc_overflow_esi))
619
620         .align  align_16byte
621         .globl  GNAME(alloc_overflow_edi)
622         TYPE(GNAME(alloc_overflow_edi))
623 GNAME(alloc_overflow_edi):
624         pushl   %eax            # Save eax
625         pushl   %ecx            # Save ecx
626         pushl   %edx            # Save edx
627         ALLOC_OVERFLOW(%edi)
628         movl    %eax,%edi       # setup the destination.
629         popl    %edx    # Restore edx.
630         popl    %ecx    # Restore ecx.
631         popl    %eax    # Restore eax.
632         ret
633         SIZE(GNAME(alloc_overflow_edi))
634
635
636 #ifdef LISP_FEATURE_WIN32
637         /* The guts of the exception-handling system doesn't use
638          * frame pointers, which manages to throw off backtraces
639          * rather badly.  So here we grab the (known-good) EBP
640          * and EIP from the exception context and use it to fake
641          * up a stack frame which will skip over the system SEH
642          * code. */
643         .align  align_16byte
644         .globl  GNAME(exception_handler_wrapper)
645         TYPE(GNAME(exception_handler_wrapper))
646 GNAME(exception_handler_wrapper):
647         /* Context layout is: */
648         /* 7 dwords before FSA. (0x1c) */
649         /* 8 dwords and 0x50 bytes in the FSA. (0x70/0x8c) */
650         /* 4 dwords segregs. (0x10/0x9c) */
651         /* 6 dwords non-stack GPRs. (0x18/0xb4) */
652         /* EBP (at 0xb4) */
653         /* EIP (at 0xb8) */
654 #define CONTEXT_EBP_OFFSET 0xb4
655 #define CONTEXT_EIP_OFFSET 0xb8
656         /* some other stuff we don't care about. */
657         pushl   %ebp
658         movl    0x10(%esp), %ebp        /* context */
659         pushl   CONTEXT_EIP_OFFSET(%ebp)
660         pushl   CONTEXT_EBP_OFFSET(%ebp)
661         movl    %esp, %ebp
662         pushl   0x1c(%esp)
663         pushl   0x1c(%esp)
664         pushl   0x1c(%esp)
665         pushl   0x1c(%esp)
666         call    GNAME(handle_exception)
667         lea     8(%ebp), %esp
668         popl    %ebp
669         ret
670         SIZE(GNAME(exception_handler_wrapper))
671 #endif
672
673 #ifdef LISP_FEATURE_DARWIN
674         .align align_16byte
675         .globl GNAME(call_into_lisp_tramp)
676         TYPE(GNAME(call_into_lisp_tramp))
677 GNAME(call_into_lisp_tramp):
678         /* 1. build the stack frame from the block that's pointed to by ECX
679            2. free the block
680            3. set ECX to 0
681            4. call the function via call_into_lisp
682         */
683         pushl   0(%ecx)          /* return address */
684
685         pushl   %ebp
686         movl    %esp, %ebp
687
688         pushl   32(%ecx)         /* eflags */
689         pushl   28(%ecx)         /* EAX */
690         pushl   20(%ecx)         /* ECX */
691         pushl   16(%ecx)         /* EDX */
692         pushl   24(%ecx)         /* EBX */
693         pushl   $0                /* popal is going to ignore esp */
694         pushl   %ebp              /* is this right?? */
695         pushl   12(%ecx)         /* ESI */
696         pushl   8(%ecx)          /* EDI */
697         pushl   $0                /* args for call_into_lisp */
698         pushl   $0
699         pushl   4(%ecx)          /* function to call */
700
701         /* free our save block */
702         pushl   %ecx              /* reserve sufficient space on stack for args */
703         pushl   %ecx
704         andl    $0xfffffff0, %esp  /* align stack */
705         movl    $0x40, 4(%esp)
706         movl    %ecx, (%esp)
707         call    GNAME(os_invalidate)
708
709         /* call call_into_lisp */
710         leal    -48(%ebp), %esp
711         call    GNAME(call_into_lisp)
712
713         /* Clean up our mess */
714         leal    -36(%ebp), %esp
715         popal
716         popfl
717         leave
718         ret
719         
720         SIZE(call_into_lisp_tramp)
721 #endif
722         
723         .align  align_16byte,0x90
724         .globl  GNAME(post_signal_tramp)
725         TYPE(GNAME(post_signal_tramp))
726 GNAME(post_signal_tramp):
727         /* this is notionally the second half of a function whose first half
728          * doesn't exist.  This is where call_into_lisp returns when called 
729          * using return_to_lisp_function */
730         addl $12,%esp   /* clear call_into_lisp args from stack */
731         popal           /* restore registers */
732         popfl
733 #ifdef LISP_FEATURE_DARWIN
734         /* skip two padding words */
735         addl $8,%esp
736 #endif
737         leave
738         ret
739         SIZE(GNAME(post_signal_tramp))
740
741
742         /* fast_bzero implementations and code to detect which implementation
743          * to use.
744          */
745 \f
746         .globl GNAME(fast_bzero_pointer)
747         .data
748         .align  align_16byte
749 GNAME(fast_bzero_pointer):
750         /* Variable containing a pointer to the bzero function to use.
751          * Initially points to a basic function.  Change this variable
752          * to fast_bzero_detect if OS supports SSE.  */
753         .long GNAME(fast_bzero_base)
754 \f
755         .text
756         .align  align_16byte,0x90
757         .globl GNAME(fast_bzero)
758         TYPE(GNAME(fast_bzero))
759 GNAME(fast_bzero):        
760         /* Indirect function call */
761         jmp *GNAME(fast_bzero_pointer)
762         SIZE(GNAME(fast_bzero))
763         
764 \f      
765         .text
766         .align  align_16byte,0x90
767         .globl GNAME(fast_bzero_detect)
768         TYPE(GNAME(fast_bzero_detect))
769 GNAME(fast_bzero_detect):
770         /* Decide whether to use SSE, MMX or REP version */
771         push %eax /* CPUID uses EAX-EDX */
772         push %ebx
773         push %ecx
774         push %edx
775         mov $1, %eax
776         cpuid
777         test $0x04000000, %edx    /* SSE2 needed for MOVNTDQ */
778         jnz Lsse2
779         /* Originally there was another case here for using the
780          * MOVNTQ instruction for processors that supported MMX but
781          * not SSE2. This turned out to be a loss especially on
782          * Athlons (where this instruction is apparently microcoded
783          * somewhat slowly). So for simplicity revert to REP STOSL
784          * for all non-SSE2 processors.
785          */
786 Lbase:
787         movl $(GNAME(fast_bzero_base)), GNAME(fast_bzero_pointer)
788         jmp Lrestore
789 Lsse2:
790         movl $(GNAME(fast_bzero_sse)), GNAME(fast_bzero_pointer)
791         jmp Lrestore
792         
793 Lrestore:
794         pop %edx
795         pop %ecx
796         pop %ebx
797         pop %eax
798         jmp *GNAME(fast_bzero_pointer)
799         
800         SIZE(GNAME(fast_bzero_detect))
801         
802 \f
803         .text
804         .align  align_16byte,0x90
805         .globl GNAME(fast_bzero_sse)
806         TYPE(GNAME(fast_bzero_sse))
807         
808 GNAME(fast_bzero_sse):
809         /* A fast routine for zero-filling blocks of memory that are
810          * guaranteed to start and end at a 4096-byte aligned address.
811          */        
812         push %esi                 /* Save temporary registers */
813         push %edi
814         mov 16(%esp), %esi        /* Parameter: amount of bytes to fill */
815         mov 12(%esp), %edi        /* Parameter: start address */
816         shr $6, %esi              /* Amount of 64-byte blocks to copy */
817         jz Lend_sse               /* If none, stop */
818         movups %xmm7, -16(%esp)   /* Save XMM register */
819         xorps  %xmm7, %xmm7       /* Zero the XMM register */
820         jmp Lloop_sse
821         .align align_16byte
822 Lloop_sse:
823
824         /* Copy the 16 zeroes from xmm7 to memory, 4 times. MOVNTDQ is the
825          * non-caching double-quadword moving variant, i.e. the memory areas
826          * we're touching are not fetched into the L1 cache, since we're just
827          * going to overwrite the memory soon anyway.
828          */
829         movntdq %xmm7, 0(%edi)
830         movntdq %xmm7, 16(%edi)
831         movntdq %xmm7, 32(%edi)
832         movntdq %xmm7, 48(%edi)
833  
834         add $64, %edi /* Advance pointer */
835         dec %esi      /* Decrement 64-byte block count */
836         jnz Lloop_sse
837         movups -16(%esp), %xmm7 /* Restore the XMM register */
838         sfence        /* Ensure that weakly ordered writes are flushed. */
839 Lend_sse:
840         mov 12(%esp), %esi      /* Parameter: start address */
841         prefetcht0 0(%esi)      /* Prefetch the start of the block into cache,
842                                  * since it's likely to be used immediately. */
843         pop %edi      /* Restore temp registers */
844         pop %esi
845         ret
846         SIZE(GNAME(fast_bzero_sse))
847                 
848 \f
849         .text
850         .align  align_16byte,0x90
851         .globl GNAME(fast_bzero_base)
852         TYPE(GNAME(fast_bzero_base))
853         
854 GNAME(fast_bzero_base):
855         /* A fast routine for zero-filling blocks of memory that are
856          * guaranteed to start and end at a 4096-byte aligned address.
857          */        
858         push %eax                 /* Save temporary registers */
859         push %ecx
860         push %edi
861         mov 20(%esp), %ecx        /* Parameter: amount of bytes to fill */
862         mov 16(%esp), %edi        /* Parameter: start address */
863         xor %eax, %eax            /* Zero EAX */
864         shr $2, %ecx              /* Amount of 4-byte blocks to copy */
865         jz  Lend_base
866
867         rep
868         stosl                     /* Store EAX to *EDI, ECX times, incrementing
869                                    * EDI by 4 after each store */
870         
871 Lend_base:        
872         pop %edi                  /* Restore temp registers */
873         pop %ecx
874         pop %eax
875         ret
876         SIZE(GNAME(fast_bzero_base))
877         
878 \f       
879         END()