2 * very-low-level utilities for runtime support
6 * This software is part of the SBCL system. See the README file for
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.
16 #define LANGUAGE_ASSEMBLY
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"
26 /* Minimize conditionalization for different OS naming schemes.
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)
33 * (Except Win32, which is unlikely ever to be ELF, sorry. -- AB 2005-12-08)
35 #if defined __linux__ || defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ || defined __sun
36 #define GNAME(var) var
38 #define GNAME(var) _##var
41 /* Get the right type of alignment. Linux, FreeBSD and NetBSD (but not OpenBSD)
42 * want alignment in bytes.
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)
50 #if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__sun) || defined(LISP_FEATURE_WIN32)
53 #define align_16byte 16
57 #define align_16byte 4
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
66 #if !defined(LISP_FEATURE_WIN32) && !defined(LISP_FEATURE_DARWIN)
67 #define TYPE(name) .type name,@function
68 #define SIZE(name) .size name,.-name
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)
91 .globl GNAME(all_threads)
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.)
99 * Return values are in eax and maybe edx for quads, or st(0) for
102 * This should work for Lisp calls C calls Lisp calls C..
105 .align align_16byte,0x90
106 .globl GNAME(call_into_c)
107 TYPE(GNAME(call_into_c))
109 /* Save the return Lisp address in ebx. */
112 /* Setup the NPX for C */
122 #ifdef LISP_FEATURE_WIN32
126 #ifdef LISP_FEATURE_DARWIN
127 andl $0xfffffff0,%esp # align stack to 16-byte boundary before calling C
129 call *%eax # normal callout using Lisp stack
131 movl %eax,%ecx # remember integer return value
133 /* Check for a return FP value. */
140 /* The return value is in eax, or eax,edx? */
141 /* Set up the NPX stack for Lisp. */
142 fldz # Ensure no regs are empty.
151 /* Restore the return value. */
152 movl %ecx,%eax # maybe return value
158 /* The return result is in st(0). */
159 /* Set up the NPX stack for Lisp, placing the result in st(0). */
160 fldz # Ensure no regs are empty.
167 fxch %st(7) # Move the result back to st(0).
169 /* We don't need to restore eax, because the result is in st(0). */
174 SIZE(GNAME(call_into_c))
178 .globl GNAME(call_into_lisp_first_time)
179 TYPE(GNAME(call_into_lisp_first_time))
181 /* The *ALIEN-STACK* pointer is set up on the first call_into_lisp when
182 * the stack changes. We don't worry too much about saving registers
183 * here, because we never expect to return from the initial call to lisp
186 .align align_16byte,0x90
187 GNAME(call_into_lisp_first_time):
188 pushl %ebp # Save old frame pointer.
189 movl %esp,%ebp # Establish new frame.
190 #ifndef LISP_FEATURE_WIN32
191 movl %esp,ALIEN_STACK + SYMBOL_VALUE_OFFSET
192 movl GNAME(all_threads),%eax
193 movl THREAD_CONTROL_STACK_START_OFFSET(%eax) ,%esp
194 /* don't think too hard about what happens if we get interrupted
196 addl $(THREAD_CONTROL_STACK_SIZE),%esp
198 /* Win32 -really- doesn't like you switching stacks out from under it. */
199 movl GNAME(all_threads),%eax
204 .globl GNAME(call_into_lisp)
205 TYPE(GNAME(call_into_lisp))
207 /* The C conventions require that ebx, esi, edi, and ebp be preserved
208 * across function calls. */
210 .align align_16byte,0x90
211 GNAME(call_into_lisp):
212 pushl %ebp # Save old frame pointer.
213 movl %esp,%ebp # Establish new frame.
215 /* Save the NPX state */
216 fwait # Catch any pending NPX exceptions.
217 subl $108,%esp # Make room for the NPX state.
218 fnsave (%esp) # save and reset NPX
220 movl (%esp),%eax # Load NPX control word.
221 andl $0xfffff2ff,%eax # Set rounding mode to nearest.
222 orl $0x00000200,%eax # Set precision to 64 bits. (53-bit mantissa)
224 fldcw (%esp) # Recover modes.
227 fldz # Ensure no FP regs are empty.
236 /* Save C regs: ebx esi edi. */
241 /* Clear descriptor regs. */
242 xorl %eax,%eax # lexenv
243 xorl %ebx,%ebx # available
244 xorl %ecx,%ecx # arg count
245 xorl %edx,%edx # first arg
246 xorl %edi,%edi # second arg
247 xorl %esi,%esi # third arg
249 /* no longer in function call */
250 movl %esp,%ebx # remember current stack
251 pushl %ebx # Save entry stack on (maybe) new stack.
253 /* Establish Lisp args. */
254 movl 8(%ebp),%eax # lexenv?
255 movl 12(%ebp),%ebx # address of arg vec
256 movl 16(%ebp),%ecx # num args
257 shll $2,%ecx # Make num args into fixnum.
260 movl (%ebx),%edx # arg0
263 movl 4(%ebx),%edi # arg1
266 movl 8(%ebx),%esi # arg2
268 /* Registers eax, ecx, edx, edi, and esi are now live. */
270 #ifdef LISP_FEATURE_WIN32
271 /* Establish an SEH frame. */
272 #ifdef LISP_FEATURE_SB_THREAD
273 /* FIXME: need to save BSP here. */
274 #error "need to save BSP here, but don't know how yet."
276 pushl BINDING_STACK_POINTER + SYMBOL_VALUE_OFFSET
278 pushl $GNAME(exception_handler_wrapper)
283 /* Alloc new frame. */
284 mov %esp,%ebx # The current sp marks start of new frame.
285 push %ebp # fp in save location S0
286 sub $8,%esp # Ensure 3 slots are allocated, one above.
287 mov %ebx,%ebp # Switch to new frame.
289 call *CLOSURE_FUN_OFFSET(%eax)
291 /* If the function returned multiple values, it will return to
292 this point. Lose them */
296 /* A singled value function returns here */
298 #ifdef LISP_FEATURE_WIN32
299 /* Remove our SEH frame. */
304 /* Restore the stack, in case there was a stack change. */
307 /* Restore C regs: ebx esi edi. */
312 /* Restore the NPX state. */
317 movl %edx,%eax # c-val
319 SIZE(GNAME(call_into_lisp))
321 /* support for saving and restoring the NPX state from C */
323 .globl GNAME(fpu_save)
324 TYPE(GNAME(fpu_save))
328 fnsave (%eax) # Save the NPX state. (resets NPX)
330 SIZE(GNAME(fpu_save))
332 .globl GNAME(fpu_restore)
333 TYPE(GNAME(fpu_restore))
337 frstor (%eax) # Restore the NPX state.
339 SIZE(GNAME(fpu_restore))
342 * the undefined-function trampoline
345 .align align_4byte,0x90
346 .globl GNAME(undefined_tramp)
347 TYPE(GNAME(undefined_tramp))
348 .byte 0, 0, 0, SIMPLE_FUN_HEADER_WIDETAG
349 GNAME(undefined_tramp):
353 .byte UNDEFINED_FUN_ERROR
354 .byte sc_DescriptorReg # eax in the Descriptor-reg SC
356 SIZE(GNAME(undefined_tramp))
359 * the closure trampoline
362 .align align_4byte,0x90
363 .globl GNAME(closure_tramp)
364 TYPE(GNAME(closure_tramp))
365 .byte 0, 0, 0, SIMPLE_FUN_HEADER_WIDETAG
366 GNAME(closure_tramp):
367 movl FDEFN_FUN_OFFSET(%eax),%eax
368 /* FIXME: The '*' after "jmp" in the next line is from PVE's
369 * patch posted to the CMU CL mailing list Oct 6, 1999. It looks
370 * reasonable, and it certainly seems as though if CMU CL needs it,
371 * SBCL needs it too, but I haven't actually verified that it's
372 * right. It would be good to find a way to force the flow of
373 * control through here to test it. */
374 jmp *CLOSURE_FUN_OFFSET(%eax)
375 SIZE(GNAME(closure_tramp))
378 .align align_4byte,0x90
379 .globl GNAME(funcallable_instance_tramp)
380 TYPE(GNAME(funcallable_instance_tramp))
381 GNAME(funcallable_instance_tramp):
382 movl FUNCALLABLE_INSTANCE_FUNCTION_OFFSET(%eax),%eax
383 /* KLUDGE: on this platform, whatever kind of function is in %rax
384 * now, the first word of it contains the address to jump to. */
385 jmp *CLOSURE_FUN_OFFSET(%eax)
386 SIZE(GNAME(funcallable_instance_tramp))
389 * fun-end breakpoint magic
392 .globl GNAME(fun_end_breakpoint_guts)
394 GNAME(fun_end_breakpoint_guts):
395 /* Multiple Value return */
396 jc multiple_value_return
397 /* Single value return: The eventual return will now use the
398 multiple values return convention but with a return values
400 movl %esp,%ebx # Setup ebx - the ofp.
401 subl $4,%esp # Allocate one stack slot for the return value
402 movl $4,%ecx # Setup ecx for one return value.
403 movl $(NIL),%edi # default second value
404 movl $(NIL),%esi # default third value
406 multiple_value_return:
408 .globl GNAME(fun_end_breakpoint_trap)
409 GNAME(fun_end_breakpoint_trap):
411 .byte trap_FunEndBreakpoint
412 hlt # We should never return here.
414 .globl GNAME(fun_end_breakpoint_end)
415 GNAME(fun_end_breakpoint_end):
418 .globl GNAME(do_pending_interrupt)
419 TYPE(GNAME(do_pending_interrupt))
420 .align align_4byte,0x90
421 GNAME(do_pending_interrupt):
423 .byte trap_PendingInterrupt
425 SIZE(GNAME(do_pending_interrupt))
429 * Allocate bytes and return the start of the allocated space
430 * in the specified destination register.
432 * In the general case the size will be in the destination register.
434 * All registers must be preserved except the destination.
435 * The C conventions will preserve ebx, esi, edi, and ebp.
436 * So only eax, ecx, and edx need special care here.
439 .globl GNAME(alloc_to_eax)
440 TYPE(GNAME(alloc_to_eax))
441 .align align_4byte,0x90
443 pushl %ecx # Save ecx and edx as C could destroy them.
445 pushl %eax # Push the size.
447 addl $4,%esp # Pop the size arg.
448 popl %edx # Restore ecx and edx.
451 SIZE(GNAME(alloc_to_eax))
453 .globl GNAME(alloc_8_to_eax)
454 TYPE(GNAME(alloc_8_to_eax))
455 .align align_4byte,0x90
456 GNAME(alloc_8_to_eax):
457 pushl %ecx # Save ecx and edx as C could destroy them.
459 pushl $8 # Push the size.
461 addl $4,%esp # Pop the size arg.
462 popl %edx # Restore ecx and edx.
465 SIZE(GNAME(alloc_8_to_eax))
467 .globl GNAME(alloc_8_to_eax)
468 TYPE(GNAME(alloc_8_to_eax))
469 .align align_4byte,0x90
471 .globl GNAME(alloc_16_to_eax)
472 TYPE(GNAME(alloc_16_to_eax))
473 .align align_4byte,0x90
474 GNAME(alloc_16_to_eax):
475 pushl %ecx # Save ecx and edx as C could destroy them.
477 pushl $16 # Push the size.
479 addl $4,%esp # Pop the size arg.
480 popl %edx # Restore ecx and edx.
483 SIZE(GNAME(alloc_16_to_eax))
485 .globl GNAME(alloc_to_ecx)
486 TYPE(GNAME(alloc_to_ecx))
487 .align align_4byte,0x90
489 pushl %eax # Save eax and edx as C could destroy them.
491 pushl %ecx # Push the size.
493 addl $4,%esp # Pop the size arg.
494 movl %eax,%ecx # Set up the destination.
495 popl %edx # Restore eax and edx.
498 SIZE(GNAME(alloc_to_ecx))
500 .globl GNAME(alloc_8_to_ecx)
501 TYPE(GNAME(alloc_8_to_ecx))
502 .align align_4byte,0x90
503 GNAME(alloc_8_to_ecx):
504 pushl %eax # Save eax and edx as C could destroy them.
506 pushl $8 # Push the size.
508 addl $4,%esp # Pop the size arg.
509 movl %eax,%ecx # Set up the destination.
510 popl %edx # Restore eax and edx.
513 SIZE(GNAME(alloc_8_to_ecx))
515 .globl GNAME(alloc_16_to_ecx)
516 TYPE(GNAME(alloc_16_to_ecx))
517 .align align_4byte,0x90
518 GNAME(alloc_16_to_ecx):
519 pushl %eax # Save eax and edx as C could destroy them.
521 pushl $16 # Push the size.
523 addl $4,%esp # Pop the size arg.
524 movl %eax,%ecx # Set up the destination.
525 popl %edx # Restore eax and edx.
528 SIZE(GNAME(alloc_16_to_ecx))
531 .globl GNAME(alloc_to_edx)
532 TYPE(GNAME(alloc_to_edx))
533 .align align_4byte,0x90
535 pushl %eax # Save eax and ecx as C could destroy them.
537 pushl %edx # Push the size.
539 addl $4,%esp # Pop the size arg.
540 movl %eax,%edx # Set up the destination.
541 popl %ecx # Restore eax and ecx.
544 SIZE(GNAME(alloc_to_edx))
546 .globl GNAME(alloc_8_to_edx)
547 TYPE(GNAME(alloc_8_to_edx))
548 .align align_4byte,0x90
549 GNAME(alloc_8_to_edx):
550 pushl %eax # Save eax and ecx as C could destroy them.
552 pushl $8 # Push the size.
554 addl $4,%esp # Pop the size arg.
555 movl %eax,%edx # Set up the destination.
556 popl %ecx # Restore eax and ecx.
559 SIZE(GNAME(alloc_8_to_edx))
561 .globl GNAME(alloc_16_to_edx)
562 TYPE(GNAME(alloc_16_to_edx))
563 .align align_4byte,0x90
564 GNAME(alloc_16_to_edx):
565 pushl %eax # Save eax and ecx as C could destroy them.
567 pushl $16 # Push the size.
569 addl $4,%esp # Pop the size arg.
570 movl %eax,%edx # Set up the destination.
571 popl %ecx # Restore eax and ecx.
574 SIZE(GNAME(alloc_16_to_edx))
578 .globl GNAME(alloc_to_ebx)
579 TYPE(GNAME(alloc_to_ebx))
580 .align align_4byte,0x90
582 pushl %eax # Save eax, ecx, and edx as C could destroy them.
585 pushl %ebx # Push the size.
587 addl $4,%esp # Pop the size arg.
588 movl %eax,%ebx # Set up the destination.
589 popl %edx # Restore eax, ecx and edx.
593 SIZE(GNAME(alloc_to_ebx))
595 .globl GNAME(alloc_8_to_ebx)
596 TYPE(GNAME(alloc_8_to_ebx))
597 .align align_4byte,0x90
598 GNAME(alloc_8_to_ebx):
599 pushl %eax # Save eax, ecx, and edx as C could destroy them.
602 pushl $8 # Push the size.
604 addl $4,%esp # Pop the size arg.
605 movl %eax,%ebx # Set up the destination.
606 popl %edx # Restore eax, ecx and edx.
610 SIZE(GNAME(alloc_8_to_ebx))
612 .globl GNAME(alloc_16_to_ebx)
613 TYPE(GNAME(alloc_16_to_ebx))
614 .align align_4byte,0x90
615 GNAME(alloc_16_to_ebx):
616 pushl %eax # Save eax, ecx, and edx as C could destroy them.
619 pushl $16 # Push the size
621 addl $4,%esp # pop the size arg.
622 movl %eax,%ebx # setup the destination.
623 popl %edx # Restore eax, ecx and edx.
627 SIZE(GNAME(alloc_16_to_ebx))
631 .globl GNAME(alloc_to_esi)
632 TYPE(GNAME(alloc_to_esi))
633 .align align_4byte,0x90
635 pushl %eax # Save eax, ecx, and edx as C could destroy them.
638 pushl %esi # Push the size
640 addl $4,%esp # pop the size arg.
641 movl %eax,%esi # setup the destination.
642 popl %edx # Restore eax, ecx and edx.
646 SIZE(GNAME(alloc_to_esi))
648 .globl GNAME(alloc_8_to_esi)
649 TYPE(GNAME(alloc_8_to_esi))
650 .align align_4byte,0x90
651 GNAME(alloc_8_to_esi):
652 pushl %eax # Save eax, ecx, and edx as C could destroy them.
655 pushl $8 # Push the size
657 addl $4,%esp # pop the size arg.
658 movl %eax,%esi # setup the destination.
659 popl %edx # Restore eax, ecx and edx.
663 SIZE(GNAME(alloc_8_to_esi))
665 .globl GNAME(alloc_16_to_esi)
666 TYPE(GNAME(alloc_16_to_esi))
667 .align align_4byte,0x90
668 GNAME(alloc_16_to_esi):
669 pushl %eax # Save eax, ecx, and edx as C could destroy them.
672 pushl $16 # Push the size
674 addl $4,%esp # pop the size arg.
675 movl %eax,%esi # setup the destination.
676 popl %edx # Restore eax, ecx and edx.
680 SIZE(GNAME(alloc_16_to_esi))
683 .globl GNAME(alloc_to_edi)
684 TYPE(GNAME(alloc_to_edi))
685 .align align_4byte,0x90
687 pushl %eax # Save eax, ecx, and edx as C could destroy them.
690 pushl %edi # Push the size
692 addl $4,%esp # pop the size arg.
693 movl %eax,%edi # setup the destination.
694 popl %edx # Restore eax, ecx and edx.
698 SIZE(GNAME(alloc_to_edi))
700 .globl GNAME(alloc_8_to_edi)
701 TYPE(GNAME(alloc_8_to_edi))
702 .align align_4byte,0x90
703 GNAME(alloc_8_to_edi):
704 pushl %eax # Save eax, ecx, and edx as C could destroy them.
707 pushl $8 # Push the size
709 addl $4,%esp # pop the size arg.
710 movl %eax,%edi # setup the destination.
711 popl %edx # Restore eax, ecx and edx.
715 SIZE(GNAME(alloc_8_to_edi))
717 .globl GNAME(alloc_16_to_edi)
718 TYPE(GNAME(alloc_16_to_edi))
719 .align align_4byte,0x90
720 GNAME(alloc_16_to_edi):
721 pushl %eax # Save eax, ecx, and edx as C could destroy them.
724 pushl $16 # Push the size
726 addl $4,%esp # pop the size arg.
727 movl %eax,%edi # setup the destination.
728 popl %edx # Restore eax, ecx and edx.
732 SIZE(GNAME(alloc_16_to_edi))
735 /* Called from lisp when an inline allocation overflows.
736 Every register except the result needs to be preserved.
737 We depend on C to preserve ebx, esi, edi, and ebp.
738 But where necessary must save eax, ecx, edx. */
740 #ifdef LISP_FEATURE_SB_THREAD
741 #define START_REGION %fs:THREAD_ALLOC_REGION_OFFSET
743 #define START_REGION GNAME(boxed_region)
746 /* This routine handles an overflow with eax=crfp+size. So the
749 .globl GNAME(alloc_overflow_eax)
750 TYPE(GNAME(alloc_overflow_eax))
751 GNAME(alloc_overflow_eax):
752 pushl %ecx # Save ecx
753 pushl %edx # Save edx
754 /* Calculate the size for the allocation. */
755 subl START_REGION,%eax
756 pushl %eax # Push the size
758 addl $4,%esp # pop the size arg.
759 popl %edx # Restore edx.
760 popl %ecx # Restore ecx.
762 SIZE(GNAME(alloc_overflow_eax))
765 .globl GNAME(alloc_overflow_ecx)
766 TYPE(GNAME(alloc_overflow_ecx))
767 GNAME(alloc_overflow_ecx):
768 pushl %eax # Save eax
769 pushl %edx # Save edx
770 /* Calculate the size for the allocation. */
771 subl START_REGION,%ecx
772 pushl %ecx # Push the size
774 addl $4,%esp # pop the size arg.
775 movl %eax,%ecx # setup the destination.
776 popl %edx # Restore edx.
777 popl %eax # Restore eax.
779 SIZE(GNAME(alloc_overflow_ecx))
782 .globl GNAME(alloc_overflow_edx)
783 TYPE(GNAME(alloc_overflow_edx))
784 GNAME(alloc_overflow_edx):
785 pushl %eax # Save eax
786 pushl %ecx # Save ecx
787 /* Calculate the size for the allocation. */
788 subl START_REGION,%edx
789 pushl %edx # Push the size
791 addl $4,%esp # pop the size arg.
792 movl %eax,%edx # setup the destination.
793 popl %ecx # Restore ecx.
794 popl %eax # Restore eax.
796 SIZE(GNAME(alloc_overflow_edx))
798 /* This routine handles an overflow with ebx=crfp+size. So the
801 .globl GNAME(alloc_overflow_ebx)
802 TYPE(GNAME(alloc_overflow_ebx))
803 GNAME(alloc_overflow_ebx):
804 pushl %eax # Save eax
805 pushl %ecx # Save ecx
806 pushl %edx # Save edx
807 /* Calculate the size for the allocation. */
808 subl START_REGION,%ebx
809 pushl %ebx # Push the size
811 addl $4,%esp # pop the size arg.
812 movl %eax,%ebx # setup the destination.
813 popl %edx # Restore edx.
814 popl %ecx # Restore ecx.
815 popl %eax # Restore eax.
817 SIZE(GNAME(alloc_overflow_ebx))
819 /* This routine handles an overflow with esi=crfp+size. So the
822 .globl GNAME(alloc_overflow_esi)
823 TYPE(GNAME(alloc_overflow_esi))
824 GNAME(alloc_overflow_esi):
825 pushl %eax # Save eax
826 pushl %ecx # Save ecx
827 pushl %edx # Save edx
828 /* Calculate the size for the allocation. */
829 subl START_REGION,%esi
830 pushl %esi # Push the size
832 addl $4,%esp # pop the size arg.
833 movl %eax,%esi # setup the destination.
834 popl %edx # Restore edx.
835 popl %ecx # Restore ecx.
836 popl %eax # Restore eax.
838 SIZE(GNAME(alloc_overflow_esi))
841 .globl GNAME(alloc_overflow_edi)
842 TYPE(GNAME(alloc_overflow_edi))
843 GNAME(alloc_overflow_edi):
844 pushl %eax # Save eax
845 pushl %ecx # Save ecx
846 pushl %edx # Save edx
847 /* Calculate the size for the allocation. */
848 subl START_REGION,%edi
849 pushl %edi # Push the size
851 addl $4,%esp # pop the size arg.
852 movl %eax,%edi # setup the destination.
853 popl %edx # Restore edx.
854 popl %ecx # Restore ecx.
855 popl %eax # Restore eax.
857 SIZE(GNAME(alloc_overflow_edi))
860 #ifdef LISP_FEATURE_WIN32
861 /* The guts of the exception-handling system doesn't use
862 * frame pointers, which manages to throw off backtraces
863 * rather badly. So here we grab the (known-good) EBP
864 * and EIP from the exception context and use it to fake
865 * up a stack frame which will skip over the system SEH
868 .globl GNAME(exception_handler_wrapper)
869 TYPE(GNAME(exception_handler_wrapper))
870 GNAME(exception_handler_wrapper):
871 /* Context layout is: */
872 /* 7 dwords before FSA. (0x1c) */
873 /* 8 dwords and 0x50 bytes in the FSA. (0x70/0x8c) */
874 /* 4 dwords segregs. (0x10/0x9c) */
875 /* 6 dwords non-stack GPRs. (0x18/0xb4) */
878 #define CONTEXT_EBP_OFFSET 0xb4
879 #define CONTEXT_EIP_OFFSET 0xb8
880 /* some other stuff we don't care about. */
882 movl 0x10(%esp), %ebp /* context */
883 pushl CONTEXT_EIP_OFFSET(%ebp)
884 pushl CONTEXT_EBP_OFFSET(%ebp)
890 call GNAME(handle_exception)
894 SIZE(GNAME(exception_handler_wrapper))
897 #ifdef LISP_FEATURE_DARWIN
899 .globl GNAME(call_into_lisp_tramp)
900 TYPE(GNAME(call_into_lisp_tramp))
901 GNAME(call_into_lisp_tramp):
902 /* 1. build the stack frame from the block that's pointed to by ECX
905 4. call the function via call_into_lisp
907 pushl 0(%ecx) /* return address */
912 pushl 32(%ecx) /* eflags */
913 pushl 28(%ecx) /* EAX */
914 pushl 20(%ecx) /* ECX */
915 pushl 16(%ecx) /* EDX */
916 pushl 24(%ecx) /* EBX */
917 pushl $0 /* popal is going to ignore esp */
918 pushl %ebp /* is this right?? */
919 pushl 12(%ecx) /* ESI */
920 pushl 8(%ecx) /* EDI */
921 pushl $0 /* args for call_into_lisp */
923 pushl 4(%ecx) /* function to call */
925 /* free our save block */
926 pushl %ecx /* reserve sufficient space on stack for args */
928 andl $0xfffffff0, %esp /* align stack */
931 call GNAME(os_invalidate)
933 /* call call_into_lisp */
935 call GNAME(call_into_lisp)
937 /* Clean up our mess */
944 SIZE(call_into_lisp_tramp)
947 .align align_4byte,0x90
948 .globl GNAME(post_signal_tramp)
949 TYPE(GNAME(post_signal_tramp))
950 GNAME(post_signal_tramp):
951 /* this is notionally the second half of a function whose first half
952 * doesn't exist. This is where call_into_lisp returns when called
953 * using return_to_lisp_function */
954 addl $12,%esp /* clear call_into_lisp args from stack */
955 popal /* restore registers */
957 #ifdef LISP_FEATURE_DARWIN
958 /* skip two padding words */
963 SIZE(GNAME(post_signal_tramp))
966 /* fast_bzero implementations and code to detect which implementation
970 .globl GNAME(fast_bzero_pointer)
973 GNAME(fast_bzero_pointer):
974 /* Variable containing a pointer to the bzero function to use.
975 * Initially points to a basic function. Change this variable
976 * to fast_bzero_detect if OS supports SSE. */
977 .long GNAME(fast_bzero_base)
980 .align align_8byte,0x90
981 .globl GNAME(fast_bzero)
982 TYPE(GNAME(fast_bzero))
984 /* Indirect function call */
985 jmp *GNAME(fast_bzero_pointer)
986 SIZE(GNAME(fast_bzero))
990 .align align_8byte,0x90
991 .globl GNAME(fast_bzero_detect)
992 TYPE(GNAME(fast_bzero_detect))
993 GNAME(fast_bzero_detect):
994 /* Decide whether to use SSE, MMX or REP version */
995 push %eax /* CPUID uses EAX-EDX */
1001 test $0x04000000, %edx /* SSE2 needed for MOVNTDQ */
1003 /* Originally there was another case here for using the
1004 * MOVNTQ instruction for processors that supported MMX but
1005 * not SSE2. This turned out to be a loss especially on
1006 * Athlons (where this instruction is apparently microcoded
1007 * somewhat slowly). So for simplicity revert to REP STOSL
1008 * for all non-SSE2 processors.
1011 movl $(GNAME(fast_bzero_base)), GNAME(fast_bzero_pointer)
1014 movl $(GNAME(fast_bzero_sse)), GNAME(fast_bzero_pointer)
1022 jmp *GNAME(fast_bzero_pointer)
1024 SIZE(GNAME(fast_bzero_detect))
1028 .align align_8byte,0x90
1029 .globl GNAME(fast_bzero_sse)
1030 TYPE(GNAME(fast_bzero_sse))
1032 GNAME(fast_bzero_sse):
1033 /* A fast routine for zero-filling blocks of memory that are
1034 * guaranteed to start and end at a 4096-byte aligned address.
1036 push %esi /* Save temporary registers */
1038 mov 16(%esp), %esi /* Parameter: amount of bytes to fill */
1039 mov 12(%esp), %edi /* Parameter: start address */
1040 shr $6, %esi /* Amount of 64-byte blocks to copy */
1041 jz Lend_sse /* If none, stop */
1042 movups %xmm7, -16(%esp) /* Save XMM register */
1043 xorps %xmm7, %xmm7 /* Zero the XMM register */
1048 /* Copy the 16 zeroes from xmm7 to memory, 4 times. MOVNTDQ is the
1049 * non-caching double-quadword moving variant, i.e. the memory areas
1050 * we're touching are not fetched into the L1 cache, since we're just
1051 * going to overwrite the memory soon anyway.
1053 movntdq %xmm7, 0(%edi)
1054 movntdq %xmm7, 16(%edi)
1055 movntdq %xmm7, 32(%edi)
1056 movntdq %xmm7, 48(%edi)
1058 add $64, %edi /* Advance pointer */
1059 dec %esi /* Decrement 64-byte block count */
1061 movups -16(%esp), %xmm7 /* Restore the XMM register */
1062 sfence /* Ensure that weakly ordered writes are flushed. */
1064 mov 12(%esp), %esi /* Parameter: start address */
1065 prefetcht0 0(%esi) /* Prefetch the start of the block into cache,
1066 * since it's likely to be used immediately. */
1067 pop %edi /* Restore temp registers */
1070 SIZE(GNAME(fast_bzero_sse))
1074 .align align_8byte,0x90
1075 .globl GNAME(fast_bzero_base)
1076 TYPE(GNAME(fast_bzero_base))
1078 GNAME(fast_bzero_base):
1079 /* A fast routine for zero-filling blocks of memory that are
1080 * guaranteed to start and end at a 4096-byte aligned address.
1082 push %eax /* Save temporary registers */
1085 mov 20(%esp), %ecx /* Parameter: amount of bytes to fill */
1086 mov 16(%esp), %edi /* Parameter: start address */
1087 xor %eax, %eax /* Zero EAX */
1088 shr $2, %ecx /* Amount of 4-byte blocks to copy */
1090 cld /* Set direction of STOSL to increment */
1093 stosl /* Store EAX to *EDI, ECX times, incrementing
1094 * EDI by 4 after each store */
1097 pop %edi /* Restore temp registers */
1101 SIZE(GNAME(fast_bzero_base))