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
54 #define align_page 4096
58 #define align_16byte 4
63 * The assembler used for win32 doesn't like .type or .size directives,
64 * so we want to conditionally kill them out. So let's wrap them in macros
65 * that are defined to be no-ops on win32. Hopefully this still works on
68 #if !defined(LISP_FEATURE_WIN32) && !defined(LISP_FEATURE_DARWIN)
69 #define TYPE(name) .type name,@function
70 #define SIZE(name) .size name,.-name
76 /* Helper macros for access to thread-locals slots for both OS types:
77 * ------------------------------------------------------------------------
80 * ================== __________
81 * | Win32 %FS base | ----> | | 0
82 * ================== | | 1
84 * TLS slots start here> |XXXXXXXX| e10 = TEB_STATIC_TLS_SLOTS_OFFSET
88 * TLS ends here> ,- |XXXXXXXX| e4f = TEB_STATIC_TLS_SLOTS_OFFSET+63
90 * | ---------- "os_address" ----.
92 * | big blob of SBCL-specific thread-local data |
93 * | |----------------------------------------| <--'
94 * | | CONTROL, BINDING, ALIEN STACK |
96 * ================== | |----------------------------------------|
97 * | Linux %FS base | -->| | FFI stack pointer |
98 * ================== | | (extra page for mprotect) |
99 * \ |----------------------------------------|
100 * (union p_t_d) -----> \-> | struct thread { | dynamic_values[0] |
103 * [tls data begins] | } | ... | <-
104 * [declared end of p_t_d] |----------------------------------------| . |
106 * . | [TLS_SIZE-1] | <-|
107 * [tls data actually ends] |----------------------------------------| |
109 * . |----------------------------------------| |
110 * . | struct nonpointer_thread_data { } | |
111 * . ------------------------------------------ |
112 * [blob actually ends] |
116 * ______________________ /
117 * | struct symbol { | /
119 * | fixnum tls_index; // fixnum value relative to union /
120 * | } | (< TLS_SIZE = 4096)
121 * ---------------------|
123 #ifdef LISP_FEATURE_WIN32
124 # define TEB_STATIC_TLS_SLOTS_OFFSET 0xE10
125 # define TEB_SBCL_THREAD_BASE_OFFSET (TEB_STATIC_TLS_SLOTS_OFFSET+(63*4))
126 # define SBCL_THREAD_BASE_EA %fs:TEB_SBCL_THREAD_BASE_OFFSET
127 # define MAYBE_FS(addr) addr
128 # define LoadTlSymbolValueAddress(symbol,reg) ; \
129 movl SBCL_THREAD_BASE_EA, reg ; \
130 addl (symbol+SYMBOL_TLS_INDEX_OFFSET), reg ;
131 # define LoadCurrentThreadSlot(offset,reg); \
132 movl SBCL_THREAD_BASE_EA, reg ; \
133 movl offset(reg), reg ;
134 #elif defined(LISP_FEATURE_LINUX) || defined(LISP_FEATURE_SUNOS) || defined(LISP_FEATURE_FREEBSD)
135 /* see comment in arch_os_thread_init */
136 # define SBCL_THREAD_BASE_EA %fs:THREAD_SELFPTR_OFFSET
137 # define MAYBE_FS(addr) addr
139 /* perhaps there's an OS out there that actually supports %fs without
140 * jumping through hoops, so just in case, here a default definition: */
141 # define SBCL_THREAD_BASE_EA $0
142 # define MAYBE_FS(addr) %fs:addr
145 /* gas can't parse 4096LU; redefine */
146 #if BACKEND_PAGE_BYTES == 4096
147 # undef BACKEND_PAGE_BYTES
148 # define BACKEND_PAGE_BYTES 4096
149 #elif BACKEND_PAGE_BYTES == 32768
150 # undef BACKEND_PAGE_BYTES
151 # define BACKEND_PAGE_BYTES 32768
153 # error BACKEND_PAGE_BYTES mismatch
156 /* OAOOM because we don't have the C headers here */
157 #define THREAD_CSP_PAGE_SIZE BACKEND_PAGE_BYTES
159 /* the CSP page sits right before the thread */
160 #define THREAD_SAVED_CSP_OFFSET (-THREAD_CSP_PAGE_SIZE)
163 * x86/darwin (as of MacOS X 10.4.5) doesn't reliably file signal
164 * handlers (SIGTRAP or Mach exception handlers) for 0xCC, wo we have
165 * to use ud2 instead. ud2 is an undefined opcode, #x0b0f, or
166 * 0F 0B in low-endian notation, that causes SIGILL to fire. We check
167 * for this instruction in the SIGILL handler and if we see it, we
168 * advance the EIP by two bytes to skip over ud2 instruction and
169 * call sigtrap_handler. */
170 #if defined(LISP_FEATURE_UD2_BREAKPOINTS)
179 .globl GNAME(all_threads)
182 * A call to call_into_c preserves esi, edi, and ebp.
183 * (The C function will preserve ebx, esi, edi, and ebp across its
184 * function call, but we trash ebx ourselves by using it to save the
185 * return Lisp address.)
187 * Return values are in eax and maybe edx for quads, or st(0) for
190 * This should work for Lisp calls C calls Lisp calls C..
192 * FIXME & OAOOM: This duplicates call-out in src/compiler/x86/c-call.lisp,
193 * so if you tweak this, change that too!
196 * Note on sections specific to LISP_FEATURE_SB_SAFEPOINT:
198 * The code below is essential to safepoint-based garbage collection,
199 * and several details need to be considered for correct implementation.
201 * The stack spilling approach:
202 * On SB-SAFEPOINT platforms, the CALL-OUT vop is defined to spill all
203 * live Lisp TNs to the stack to provide information for conservative
204 * GC cooperatively (avoiding the need to retrieve register values
205 * from POSIX signal contexts or Windows GetThreadContext()).
207 * Finding the SP at all:
208 * The main remaining value needed by GC is the stack pointer (SP) at
209 * the moment of entering the foreign function. For this purpose, a
210 * thread-local field for the SP is used. Two stores to that field
211 * are done for each C call, one to save the SP before calling out and
212 * and one to undo that store afterwards.
214 * Stores as synchronization points:
215 * These two stores delimit the C call: While the SP is set, our
216 * thread is known not to run Lisp code: During GC, memory protection
217 * ensures that no thread proceeds across stores.
219 * The return PC issue:
220 * (Note that CALL-OUT has, in principle, two versions: Inline
221 * assembly in the VOP -or- alternatively the out-of-line version you
222 * are currently reading. In reality, safepoint builds currently
223 * lack the inline code entirely.)
225 * Both versions need to take special care with the return PC:
226 * - In the inline version of the code (if it existed), the two stores
227 * would be done directly in the CALL-OUT vop. In that theoretical
228 * implementation, there is a time interval between return of the
229 * actual C call and a second SP store during which the return
230 * address might not be on the stack anymore.
231 * - In this out-of-line version, the stores are done during
232 * call_into_c's frame, but an equivalent problem arises: In order
233 * to present the stack of arguments as our foreign function expects
234 * them, call_into_c has to pop the Lisp return address into a
235 * register first; this register has to be preserved by GENCGC
236 * separately: our return address is not in the stack anymore.
237 * In both case, stack scanning alone is not sufficient to pin
238 * the return address, and we communicate it to GC explicitly
239 * in addition to the SP.
241 * Note on look-alike accessor macros with vastly different behaviour:
242 * THREAD_PC_AROUND_FOREIGN_CALL_OFFSET is an "ordinary" field of the
243 * struct thread, whereas THREAD_SAVED_CSP_OFFSET is a synchronization
244 * point on a potentially write-protected page.
248 .align align_16byte,0x90
249 .globl GNAME(call_into_c)
250 TYPE(GNAME(call_into_c))
252 /* Save the return Lisp address in ebx. */
255 /* Setup the NPX for C */
256 /* The VOP says regarding CLD: "Clear out DF: Darwin, Windows,
257 * and Solaris at least require this, and it should not hurt
258 * others either." call_into_c didn't have it, but better safe than
270 #ifdef LISP_FEATURE_SB_SAFEPOINT
271 /* enter safe region: store SP and return PC */
272 movl SBCL_THREAD_BASE_EA,%edi
273 movl %esp,MAYBE_FS(THREAD_SAVED_CSP_OFFSET(%edi))
274 movl %ebx,MAYBE_FS(THREAD_PC_AROUND_FOREIGN_CALL_OFFSET(%edi))
277 /* foreign call, preserving ESI, EDI, and EBX */
278 call *%eax # normal callout using Lisp stack
279 /* return values now in eax/edx OR st(0) */
281 #ifdef LISP_FEATURE_SB_SAFEPOINT
282 /* leave region: clear the SP! (Also unpin the return PC.) */
284 movl %ecx,MAYBE_FS(THREAD_SAVED_CSP_OFFSET(%edi))
285 movl %ecx,MAYBE_FS(THREAD_PC_AROUND_FOREIGN_CALL_OFFSET(%edi))
288 movl %eax,%ecx # remember integer return value
290 /* Check for a return FP value. */
297 /* The return value is in eax, or eax,edx? */
298 /* Set up the NPX stack for Lisp. */
299 fldz # Ensure no regs are empty.
308 /* Restore the return value. */
309 movl %ecx,%eax # maybe return value
315 /* The return result is in st(0). */
316 /* Set up the NPX stack for Lisp, placing the result in st(0). */
317 fldz # Ensure no regs are empty.
324 fxch %st(7) # Move the result back to st(0).
326 /* We don't need to restore eax, because the result is in st(0). */
328 /* Return. FIXME: It would be nice to restructure this to use RET. */
331 SIZE(GNAME(call_into_c))
335 .globl GNAME(call_into_lisp_first_time)
336 TYPE(GNAME(call_into_lisp_first_time))
338 /* We don't worry too much about saving registers
339 * here, because we never expect to return from the initial call to lisp
342 .align align_16byte,0x90
343 GNAME(call_into_lisp_first_time):
344 pushl %ebp # Save old frame pointer.
345 movl %esp,%ebp # Establish new frame.
346 #ifndef LISP_FEATURE_WIN32
347 movl GNAME(all_threads),%eax
348 /* pthread machinery takes care of this for other threads */
349 movl THREAD_CONTROL_STACK_END_OFFSET(%eax) ,%esp
351 /* Win32 -really- doesn't like you switching stacks out from under it. */
352 movl GNAME(all_threads),%eax
357 .globl GNAME(call_into_lisp)
358 TYPE(GNAME(call_into_lisp))
360 /* The C conventions require that ebx, esi, edi, and ebp be preserved
361 * across function calls. */
363 .align align_16byte,0x90
364 GNAME(call_into_lisp):
365 pushl %ebp # Save old frame pointer.
366 movl %esp,%ebp # Establish new frame.
369 /* Save the NPX state */
370 fwait # Catch any pending NPX exceptions.
371 subl $108,%esp # Make room for the NPX state.
372 fnsave (%esp) # save and reset NPX
374 movl (%esp),%eax # Load NPX control word.
375 andl $0xfffff2ff,%eax # Set rounding mode to nearest.
376 orl $0x00000200,%eax # Set precision to 64 bits. (53-bit mantissa)
378 fldcw (%esp) # Recover modes.
381 fldz # Ensure no FP regs are empty.
390 /* Save C regs: ebx esi edi. */
395 /* Clear descriptor regs. */
396 xorl %eax,%eax # lexenv
397 xorl %ebx,%ebx # available
398 xorl %ecx,%ecx # arg count
399 xorl %edx,%edx # first arg
400 xorl %edi,%edi # second arg
401 xorl %esi,%esi # third arg
403 /* no longer in function call */
404 movl %esp,%ebx # remember current stack
405 pushl %ebx # Save entry stack on (maybe) new stack.
407 /* Establish Lisp args. */
408 movl 8(%ebp),%eax # lexenv?
409 movl 12(%ebp),%ebx # address of arg vec
410 movl 16(%ebp),%ecx # num args
411 shll $2,%ecx # Make num args into fixnum.
414 movl (%ebx),%edx # arg0
417 movl 4(%ebx),%edi # arg1
420 movl 8(%ebx),%esi # arg2
422 /* Registers eax, ecx, edx, edi, and esi are now live. */
424 #ifdef LISP_FEATURE_WIN32
425 /* Establish an SEH frame. */
426 #ifdef LISP_FEATURE_SB_THREAD
427 /* Save binding stack pointer */
430 movl SBCL_THREAD_BASE_EA, %eax
431 movl THREAD_BINDING_STACK_POINTER_OFFSET(%eax), %eax
435 pushl BINDING_STACK_POINTER + SYMBOL_VALUE_OFFSET
437 pushl $GNAME(exception_handler_wrapper)
442 /* Alloc new frame. */
443 push %ebp # Dummy for return address
444 push %ebp # fp in save location S1
445 mov %esp,%ebp # The current sp marks start of new frame.
446 sub $4,%esp # Ensure 3 slots are allocated, two above.
448 call *CLOSURE_FUN_OFFSET(%eax)
450 /* If the function returned multiple values, it will return to
451 this point. Lose them */
455 /* A singled value function returns here */
457 #ifdef LISP_FEATURE_WIN32
458 /* Remove our SEH frame. */
464 /* Restore the stack, in case there was a stack change. */
467 /* Restore C regs: ebx esi edi. */
472 /* Restore the NPX state. */
477 movl %edx,%eax # c-val
479 SIZE(GNAME(call_into_lisp))
481 /* support for saving and restoring the NPX state from C */
483 .globl GNAME(fpu_save)
484 TYPE(GNAME(fpu_save))
488 fnsave (%eax) # Save the NPX state. (resets NPX)
490 SIZE(GNAME(fpu_save))
492 .globl GNAME(fpu_restore)
493 TYPE(GNAME(fpu_restore))
497 frstor (%eax) # Restore the NPX state.
499 SIZE(GNAME(fpu_restore))
502 * the undefined-function trampoline
505 .align align_16byte,0x90
506 .globl GNAME(undefined_tramp)
507 TYPE(GNAME(undefined_tramp))
508 .byte 0, 0, 0, SIMPLE_FUN_HEADER_WIDETAG
509 GNAME(undefined_tramp):
510 pop 4(%ebp) # Save return PC for backtrace.
514 .byte UNDEFINED_FUN_ERROR
515 .byte sc_DescriptorReg # eax in the Descriptor-reg SC
517 SIZE(GNAME(undefined_tramp))
519 /* KLUDGE: FIND-ESCAPED-FRAME (SYS:SRC;CODE;DEBUG-INT.LISP) needs
520 * to know the name of the function immediately following the
521 * undefined-function trampoline. */
524 * the closure trampoline
527 .align align_16byte,0x90
528 .globl GNAME(closure_tramp)
529 TYPE(GNAME(closure_tramp))
530 .byte 0, 0, 0, SIMPLE_FUN_HEADER_WIDETAG
531 GNAME(closure_tramp):
532 movl FDEFN_FUN_OFFSET(%eax),%eax
533 /* FIXME: The '*' after "jmp" in the next line is from PVE's
534 * patch posted to the CMU CL mailing list Oct 6, 1999. It looks
535 * reasonable, and it certainly seems as though if CMU CL needs it,
536 * SBCL needs it too, but I haven't actually verified that it's
537 * right. It would be good to find a way to force the flow of
538 * control through here to test it. */
539 jmp *CLOSURE_FUN_OFFSET(%eax)
540 SIZE(GNAME(closure_tramp))
543 .align align_16byte,0x90
544 .globl GNAME(funcallable_instance_tramp)
545 TYPE(GNAME(funcallable_instance_tramp))
546 GNAME(funcallable_instance_tramp):
547 movl FUNCALLABLE_INSTANCE_FUNCTION_OFFSET(%eax),%eax
548 /* KLUDGE: on this platform, whatever kind of function is in %rax
549 * now, the first word of it contains the address to jump to. */
550 jmp *CLOSURE_FUN_OFFSET(%eax)
551 SIZE(GNAME(funcallable_instance_tramp))
554 * fun-end breakpoint magic
558 * For an explanation of the magic involved in function-end
559 * breakpoints, see the implementation in ppc-assem.S.
563 .globl GNAME(fun_end_breakpoint_guts)
565 GNAME(fun_end_breakpoint_guts):
566 /* Multiple Value return */
567 jc multiple_value_return
568 /* Single value return: The eventual return will now use the
569 multiple values return convention but with a return values
571 movl %esp,%ebx # Setup ebx - the ofp.
572 subl $4,%esp # Allocate one stack slot for the return value
573 movl $4,%ecx # Setup ecx for one return value.
574 movl $(NIL),%edi # default second value
575 movl $(NIL),%esi # default third value
577 multiple_value_return:
579 .globl GNAME(fun_end_breakpoint_trap)
580 GNAME(fun_end_breakpoint_trap):
582 .byte trap_FunEndBreakpoint
583 hlt # We should never return here.
585 .globl GNAME(fun_end_breakpoint_end)
586 GNAME(fun_end_breakpoint_end):
589 .globl GNAME(do_pending_interrupt)
590 TYPE(GNAME(do_pending_interrupt))
591 .align align_16byte,0x90
592 GNAME(do_pending_interrupt):
594 .byte trap_PendingInterrupt
596 SIZE(GNAME(do_pending_interrupt))
598 /* Allocate bytes and return the start of the allocated space
599 * in the specified destination register.
601 * In the general case the size will be in the destination register.
603 * All registers must be preserved except the destination.
604 * The C conventions will preserve ebx, esi, edi, and ebp.
605 * So only eax, ecx, and edx need special care here.
607 * ALLOC factors out the logic of calling alloc(): stack alignment, etc.
609 * DEFINE_ALLOC_TO_FOO defines an alloction routine.
612 #ifdef LISP_FEATURE_DARWIN
613 #define ALLOC(size) \
614 pushl %ebp; /* Save EBP */ \
615 movl %esp,%ebp; /* Save ESP to EBP */ \
616 pushl $0; /* Reserve space for arg */ \
617 andl $0xfffffff0,%esp; /* Align stack to 16bytes */ \
618 movl size, (%esp); /* Argument to alloc */ \
620 movl %ebp,%esp; /* Restore ESP from EBP */ \
621 popl %ebp; /* Restore EBP */
623 #define ALLOC(size) \
624 pushl size; /* Argument to alloc */ \
626 addl $4,%esp; /* Pop argument */
629 #define DEFINE_ALLOC_TO_EAX(name,size) \
630 .globl GNAME(name); \
632 .align align_16byte,0x90; \
634 pushl %ecx; /* Save ECX and EDX */ \
637 popl %edx; /* Restore ECX and EDX */ \
642 #define DEFINE_ALLOC_TO_ECX(name,size) \
643 .globl GNAME(name); \
645 .align align_16byte,0x90; \
647 pushl %eax; /* Save EAX and EDX */ \
650 movl %eax,%ecx; /* Result to destination */ \
656 #define DEFINE_ALLOC_TO_EDX(name,size) \
657 .globl GNAME(name); \
659 .align align_16byte,0x90; \
661 pushl %eax; /* Save EAX and ECX */ \
664 movl %eax,%edx; /* Restore EAX and ECX */ \
670 #define DEFINE_ALLOC_TO_REG(name,reg,size) \
671 .globl GNAME(name); \
673 .align align_16byte,0x90; \
675 pushl %eax; /* Save EAX, ECX, and EDX */ \
679 movl %eax,reg; /* Restore them */ \
686 DEFINE_ALLOC_TO_EAX(alloc_to_eax,%eax)
687 DEFINE_ALLOC_TO_EAX(alloc_8_to_eax,$8)
688 DEFINE_ALLOC_TO_EAX(alloc_16_to_eax,$16)
690 DEFINE_ALLOC_TO_ECX(alloc_to_ecx,%ecx)
691 DEFINE_ALLOC_TO_ECX(alloc_8_to_ecx,$8)
692 DEFINE_ALLOC_TO_ECX(alloc_16_to_ecx,$16)
694 DEFINE_ALLOC_TO_EDX(alloc_to_edx,%edx)
695 DEFINE_ALLOC_TO_EDX(alloc_8_to_edx,$8)
696 DEFINE_ALLOC_TO_EDX(alloc_16_to_edx,$16)
698 DEFINE_ALLOC_TO_REG(alloc_to_ebx,%ebx,%ebx)
699 DEFINE_ALLOC_TO_REG(alloc_8_to_ebx,%ebx,$8)
700 DEFINE_ALLOC_TO_REG(alloc_16_to_ebx,%ebx,$16)
702 DEFINE_ALLOC_TO_REG(alloc_to_esi,%esi,%esi)
703 DEFINE_ALLOC_TO_REG(alloc_8_to_esi,%esi,$8)
704 DEFINE_ALLOC_TO_REG(alloc_16_to_esi,%esi,$16)
706 DEFINE_ALLOC_TO_REG(alloc_to_edi,%edi,%edi)
707 DEFINE_ALLOC_TO_REG(alloc_8_to_edi,%edi,$8)
708 DEFINE_ALLOC_TO_REG(alloc_16_to_edi,%edi,$16)
710 /* Called from lisp when an inline allocation overflows.
711 * Every register except the result needs to be preserved.
712 * We depend on C to preserve ebx, esi, edi, and ebp.
713 * But where necessary must save eax, ecx, edx. */
715 #ifdef LISP_FEATURE_SB_THREAD
716 #define START_REGION %fs:THREAD_ALLOC_REGION_OFFSET
718 #define START_REGION GNAME(boxed_region)
721 #if defined(LISP_FEATURE_SB_THREAD) && defined(LISP_FEATURE_WIN32)
722 #define ALLOC_OVERFLOW(size,scratch) \
723 movl SBCL_THREAD_BASE_EA, scratch; \
724 /* Calculate the size for the allocation. */ \
725 subl THREAD_ALLOC_REGION_OFFSET(scratch),size; \
728 #define ALLOC_OVERFLOW(size,scratch) \
729 /* Calculate the size for the allocation. */ \
730 subl START_REGION,size; \
734 /* This routine handles an overflow with eax=crfp+size. So the
737 .globl GNAME(alloc_overflow_eax)
738 TYPE(GNAME(alloc_overflow_eax))
739 GNAME(alloc_overflow_eax):
740 pushl %ecx # Save ecx
741 pushl %edx # Save edx
742 ALLOC_OVERFLOW(%eax,%edx)
743 popl %edx # Restore edx.
744 popl %ecx # Restore ecx.
746 SIZE(GNAME(alloc_overflow_eax))
749 .globl GNAME(alloc_overflow_ecx)
750 TYPE(GNAME(alloc_overflow_ecx))
751 GNAME(alloc_overflow_ecx):
752 pushl %eax # Save eax
753 pushl %edx # Save edx
754 ALLOC_OVERFLOW(%ecx,%edx)
755 movl %eax,%ecx # setup the destination.
756 popl %edx # Restore edx.
757 popl %eax # Restore eax.
759 SIZE(GNAME(alloc_overflow_ecx))
762 .globl GNAME(alloc_overflow_edx)
763 TYPE(GNAME(alloc_overflow_edx))
764 GNAME(alloc_overflow_edx):
765 pushl %eax # Save eax
766 pushl %ecx # Save ecx
767 ALLOC_OVERFLOW(%edx,%ecx)
768 movl %eax,%edx # setup the destination.
769 popl %ecx # Restore ecx.
770 popl %eax # Restore eax.
772 SIZE(GNAME(alloc_overflow_edx))
774 /* This routine handles an overflow with ebx=crfp+size. So the
777 .globl GNAME(alloc_overflow_ebx)
778 TYPE(GNAME(alloc_overflow_ebx))
779 GNAME(alloc_overflow_ebx):
780 pushl %eax # Save eax
781 pushl %ecx # Save ecx
782 pushl %edx # Save edx
783 ALLOC_OVERFLOW(%ebx,%edx)
784 movl %eax,%ebx # setup the destination.
785 popl %edx # Restore edx.
786 popl %ecx # Restore ecx.
787 popl %eax # Restore eax.
789 SIZE(GNAME(alloc_overflow_ebx))
791 /* This routine handles an overflow with esi=crfp+size. So the
794 .globl GNAME(alloc_overflow_esi)
795 TYPE(GNAME(alloc_overflow_esi))
796 GNAME(alloc_overflow_esi):
797 pushl %eax # Save eax
798 pushl %ecx # Save ecx
799 pushl %edx # Save edx
800 ALLOC_OVERFLOW(%esi,%edx)
801 movl %eax,%esi # setup the destination.
802 popl %edx # Restore edx.
803 popl %ecx # Restore ecx.
804 popl %eax # Restore eax.
806 SIZE(GNAME(alloc_overflow_esi))
809 .globl GNAME(alloc_overflow_edi)
810 TYPE(GNAME(alloc_overflow_edi))
811 GNAME(alloc_overflow_edi):
812 pushl %eax # Save eax
813 pushl %ecx # Save ecx
814 pushl %edx # Save edx
815 ALLOC_OVERFLOW(%edi,%edx)
816 movl %eax,%edi # setup the destination.
817 popl %edx # Restore edx.
818 popl %ecx # Restore ecx.
819 popl %eax # Restore eax.
821 SIZE(GNAME(alloc_overflow_edi))
824 #ifdef LISP_FEATURE_WIN32
825 /* The guts of the exception-handling system doesn't use
826 * frame pointers, which manages to throw off backtraces
827 * rather badly. So here we grab the (known-good) EBP
828 * and EIP from the exception context and use it to fake
829 * up a stack frame which will skip over the system SEH
832 .globl GNAME(exception_handler_wrapper)
833 TYPE(GNAME(exception_handler_wrapper))
834 GNAME(exception_handler_wrapper):
835 /* Context layout is: */
836 /* 7 dwords before FSA. (0x1c) */
837 /* 8 dwords and 0x50 bytes in the FSA. (0x70/0x8c) */
838 /* 4 dwords segregs. (0x10/0x9c) */
839 /* 6 dwords non-stack GPRs. (0x18/0xb4) */
842 #define CONTEXT_EBP_OFFSET 0xb4
843 #define CONTEXT_EIP_OFFSET 0xb8
844 /* some other stuff we don't care about. */
846 movl 0x10(%esp), %ebp /* context */
847 pushl CONTEXT_EIP_OFFSET(%ebp)
848 pushl CONTEXT_EBP_OFFSET(%ebp)
854 call GNAME(handle_exception)
858 SIZE(GNAME(exception_handler_wrapper))
861 #ifdef LISP_FEATURE_DARWIN
863 .globl GNAME(call_into_lisp_tramp)
864 TYPE(GNAME(call_into_lisp_tramp))
865 GNAME(call_into_lisp_tramp):
866 /* 1. build the stack frame from the block that's pointed to by ECX
869 4. call the function via call_into_lisp
871 pushl 0(%ecx) /* return address */
876 pushl 32(%ecx) /* eflags */
877 pushl 28(%ecx) /* EAX */
878 pushl 20(%ecx) /* ECX */
879 pushl 16(%ecx) /* EDX */
880 pushl 24(%ecx) /* EBX */
881 pushl $0 /* popal is going to ignore esp */
882 pushl %ebp /* is this right?? */
883 pushl 12(%ecx) /* ESI */
884 pushl 8(%ecx) /* EDI */
885 pushl $0 /* args for call_into_lisp */
887 pushl 4(%ecx) /* function to call */
889 /* free our save block */
890 pushl %ecx /* reserve sufficient space on stack for args */
892 andl $0xfffffff0, %esp /* align stack */
895 call GNAME(os_invalidate)
897 /* call call_into_lisp */
899 call GNAME(call_into_lisp)
901 /* Clean up our mess */
908 SIZE(call_into_lisp_tramp)
911 .align align_16byte,0x90
912 .globl GNAME(post_signal_tramp)
913 TYPE(GNAME(post_signal_tramp))
914 GNAME(post_signal_tramp):
915 /* this is notionally the second half of a function whose first half
916 * doesn't exist. This is where call_into_lisp returns when called
917 * using return_to_lisp_function */
918 addl $12,%esp /* clear call_into_lisp args from stack */
919 popal /* restore registers */
921 #ifdef LISP_FEATURE_DARWIN
922 /* skip two padding words */
927 SIZE(GNAME(post_signal_tramp))
930 /* fast_bzero implementations and code to detect which implementation
934 .globl GNAME(fast_bzero_pointer)
937 GNAME(fast_bzero_pointer):
938 /* Variable containing a pointer to the bzero function to use.
939 * Initially points to a basic function. Change this variable
940 * to fast_bzero_detect if OS supports SSE. */
941 .long GNAME(fast_bzero_base)
944 .align align_16byte,0x90
945 .globl GNAME(fast_bzero)
946 TYPE(GNAME(fast_bzero))
948 /* Indirect function call */
949 jmp *GNAME(fast_bzero_pointer)
950 SIZE(GNAME(fast_bzero))
954 .align align_16byte,0x90
955 .globl GNAME(fast_bzero_detect)
956 TYPE(GNAME(fast_bzero_detect))
957 GNAME(fast_bzero_detect):
958 /* Decide whether to use SSE, MMX or REP version */
959 push %eax /* CPUID uses EAX-EDX */
965 test $0x04000000, %edx /* SSE2 needed for MOVNTDQ */
967 /* Originally there was another case here for using the
968 * MOVNTQ instruction for processors that supported MMX but
969 * not SSE2. This turned out to be a loss especially on
970 * Athlons (where this instruction is apparently microcoded
971 * somewhat slowly). So for simplicity revert to REP STOSL
972 * for all non-SSE2 processors.
975 movl $(GNAME(fast_bzero_base)), GNAME(fast_bzero_pointer)
978 movl $(GNAME(fast_bzero_sse)), GNAME(fast_bzero_pointer)
986 jmp *GNAME(fast_bzero_pointer)
988 SIZE(GNAME(fast_bzero_detect))
992 .align align_16byte,0x90
993 .globl GNAME(fast_bzero_sse)
994 TYPE(GNAME(fast_bzero_sse))
996 GNAME(fast_bzero_sse):
997 /* A fast routine for zero-filling blocks of memory that are
998 * guaranteed to start and end at a 4096-byte aligned address.
1000 push %esi /* Save temporary registers */
1002 mov 16(%esp), %esi /* Parameter: amount of bytes to fill */
1003 mov 12(%esp), %edi /* Parameter: start address */
1004 shr $6, %esi /* Amount of 64-byte blocks to copy */
1005 jz Lend_sse /* If none, stop */
1006 movups %xmm7, -16(%esp) /* Save XMM register */
1007 xorps %xmm7, %xmm7 /* Zero the XMM register */
1012 /* Copy the 16 zeroes from xmm7 to memory, 4 times. MOVNTDQ is the
1013 * non-caching double-quadword moving variant, i.e. the memory areas
1014 * we're touching are not fetched into the L1 cache, since we're just
1015 * going to overwrite the memory soon anyway.
1017 movntdq %xmm7, 0(%edi)
1018 movntdq %xmm7, 16(%edi)
1019 movntdq %xmm7, 32(%edi)
1020 movntdq %xmm7, 48(%edi)
1022 add $64, %edi /* Advance pointer */
1023 dec %esi /* Decrement 64-byte block count */
1025 movups -16(%esp), %xmm7 /* Restore the XMM register */
1026 sfence /* Ensure that weakly ordered writes are flushed. */
1028 mov 12(%esp), %esi /* Parameter: start address */
1029 prefetcht0 0(%esi) /* Prefetch the start of the block into cache,
1030 * since it's likely to be used immediately. */
1031 pop %edi /* Restore temp registers */
1034 SIZE(GNAME(fast_bzero_sse))
1038 .align align_16byte,0x90
1039 .globl GNAME(fast_bzero_base)
1040 TYPE(GNAME(fast_bzero_base))
1042 GNAME(fast_bzero_base):
1043 /* A fast routine for zero-filling blocks of memory that are
1044 * guaranteed to start and end at a 4096-byte aligned address.
1046 push %eax /* Save temporary registers */
1049 mov 20(%esp), %ecx /* Parameter: amount of bytes to fill */
1050 mov 16(%esp), %edi /* Parameter: start address */
1051 xor %eax, %eax /* Zero EAX */
1052 shr $2, %ecx /* Amount of 4-byte blocks to copy */
1056 stosl /* Store EAX to *EDI, ECX times, incrementing
1057 * EDI by 4 after each store */
1060 pop %edi /* Restore temp registers */
1064 SIZE(GNAME(fast_bzero_base))
1067 /* When LISP_FEATURE_C_STACK_IS_CONTROL_STACK, we cannot safely scrub
1068 * the control stack from C, largely due to not knowing where the
1069 * active stack frame ends. On such platforms, we reimplement the
1070 * core scrubbing logic in assembly, in this case here:
1073 .align align_16byte,0x90
1074 .globl GNAME(arch_scrub_control_stack)
1075 TYPE(GNAME(arch_scrub_control_stack))
1076 GNAME(arch_scrub_control_stack):
1077 /* We are passed three parameters:
1078 * A (struct thread *) at [ESP+4],
1079 * the address of the guard page at [ESP+8], and
1080 * the address of the hard guard page at [ESP+12].
1081 * We may trash EAX, ECX, and EDX with impunity.
1082 * [ESP] is our return address, [ESP-4] is the first
1083 * stack slot to scrub. */
1085 /* We start by setting up our scrub pointer in EAX, our
1086 * guard page upper bound in ECX, and our hard guard
1087 * page upper bound in EDX. */
1089 mov GNAME(os_vm_page_size),%edx
1094 /* We need to do a memory operation relative to the
1095 * thread pointer, so put it in %ecx and our guard
1096 * page upper bound in 4(%esp). */
1099 /* Now we begin our main scrub loop. */
1102 /* If we're about to scrub the hard guard page, exit. */
1104 jae ascs_check_guard_page
1108 ascs_check_guard_page:
1109 /* If we're about to scrub the guard page, and the guard
1110 * page is protected, exit. */
1115 cmpl $(NIL), THREAD_CONTROL_STACK_GUARD_PAGE_PROTECTED_OFFSET(%ecx)
1118 /* Clear memory backwards to the start of the (4KiB) page */
1125 /* If we're about to hit the hard guard page, exit. */
1129 /* If the next (previous?) 4KiB page contains a non-zero
1130 * word, continue scrubbing. */
1140 SIZE(GNAME(arch_scrub_control_stack))