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
17 #include "genesis/config.h"
20 #include "genesis/closure.h"
21 #include "genesis/funcallable-instance.h"
22 #include "genesis/fdefn.h"
23 #include "genesis/static-symbols.h"
24 #include "genesis/symbol.h"
25 #include "genesis/thread.h"
27 /* Minimize conditionalization for different OS naming schemes. */
28 #if defined __linux__ || defined __FreeBSD__ || defined __OpenBSD__ || defined __NetBSD__ || defined __sun || defined _WIN64
29 #define GNAME(var) var
31 #define GNAME(var) _##var
34 /* Get the right type of alignment. Linux, FreeBSD and OpenBSD
35 * want alignment in bytes. */
36 #if defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined __NetBSD__ || defined(__sun) || defined _WIN64
39 #define align_16byte 16
40 #define align_32byte 32
41 #define align_page 32768
45 #define align_16byte 4
50 * The assembler used for win32 doesn't like .type or .size directives,
51 * so we want to conditionally kill them out. So let's wrap them in macros
52 * that are defined to be no-ops on win32. Hopefully this still works on
55 #if !defined(LISP_FEATURE_WIN32) && !defined(LISP_FEATURE_DARWIN)
56 #define TYPE(name) .type name,@function
57 #define SIZE(name) .size name,.-name
58 #define DOLLAR(name) $(name)
65 * x86/darwin (as of MacOS X 10.4.5) doesn't reliably fire signal
66 * handlers (SIGTRAP or Mach exception handlers) for 0xCC, wo we have
67 * to use ud2 instead. ud2 is an undefined opcode, #x0b0f, or
68 * 0F 0B in low-endian notation, that causes SIGILL to fire. We check
69 * for this instruction in the SIGILL handler and if we see it, we
70 * advance the EIP by two bytes to skip over ud2 instruction and
71 * call sigtrap_handler. */
72 #if defined(LISP_FEATURE_UD2_BREAKPOINTS)
79 * More Apple assembler hacks
82 #if defined(LISP_FEATURE_DARWIN)
83 /* global symbol x86-64 sym(%rip) hack:*/
84 #define GSYM(name) name(%rip)
87 #define GSYM(name) $name
93 .globl GNAME(all_threads)
97 /* From lower to higher-numbered addresses, the stack contains
98 * return address, arg 0, arg 1, arg 2 ...
99 * rax contains the address of the function to call
100 * Lisp expects return value in rax, whic is already consistent with C
101 * XXXX correct floating point handling is unimplemented so far
102 * Based on comments cleaned from x86-assem.S, we believe that
103 * Lisp is expecting us to preserve rsi, rdi, rsp (no idea about r8-15)
106 .align align_16byte,0x90
107 .globl GNAME(call_into_c)
108 TYPE(GNAME(call_into_c))
110 push %rbp # Save old frame pointer.
111 mov %rsp,%rbp # Establish new frame.
113 push %rsi # args are going in here
126 SIZE(GNAME(call_into_c))
130 .globl GNAME(call_into_lisp_first_time)
131 TYPE(GNAME(call_into_lisp_first_time))
133 /* We don't worry too much about saving registers
134 * here, because we never expect to return from the initial call to lisp
137 .align align_16byte,0x90
138 GNAME(call_into_lisp_first_time):
139 push %rbp # Save old frame pointer.
140 mov %rsp,%rbp # Establish new frame.
141 #if defined(LISP_FEATURE_DARWIN)
142 movq GSYM(GNAME(all_threads)),%rax
144 movq GNAME(all_threads),%rax
146 mov THREAD_CONTROL_STACK_END_OFFSET(%rax) ,%rsp
150 .globl GNAME(call_into_lisp)
151 TYPE(GNAME(call_into_lisp))
154 * amd64 calling convention: C expects that
155 * arguments go in rdi rsi rdx rcx r8 r9
156 * return values in rax rdx
157 * callee saves rbp rbx r12-15 if it uses them
159 #ifdef LISP_FEATURE_WIN32
160 # define SUPPORT_FOMIT_FRAME_POINTER
162 .align align_16byte,0x90
163 GNAME(call_into_lisp):
164 #ifdef SUPPORT_FOMIT_FRAME_POINTER
167 push %rbp # Save old frame pointer.
168 mov %rsp,%rbp # Establish new frame.
170 #ifdef SUPPORT_FOMIT_FRAME_POINTER
171 /* If called through call_into_lisp_first_time, %r15 becomes invalid
172 * here, but we will not return in that case. */
176 /* FIXME x86 saves FPU state here */
177 push %rbx # these regs are callee-saved according to C
178 push %r12 # so must be preserved and restored when
179 push %r13 # the lisp function returns
183 mov %rsp,%rbx # remember current stack
184 push %rbx # Save entry stack on (maybe) new stack.
186 push %rdi # args from C
189 #ifdef LISP_FEATURE_SB_THREAD
190 # ifdef SUPPORT_FOMIT_FRAME_POINTER
193 call GNAME(carry_frame_pointer)
197 #ifdef LISP_FEATURE_GCC_TLS
199 movq GNAME(current_thread)@TPOFF(%rax), %r12
201 #ifdef LISP_FEATURE_DARWIN
202 mov GSYM(GNAME(specials)),%rdi
206 call GNAME(pthread_getspecific)
211 pop %rbx # arg vector
212 pop %rax # function ptr/lexenv
214 xor %rdx,%rdx # clear any descriptor registers
215 xor %rdi,%rdi # that we can't be sure we'll
216 xor %rsi,%rsi # initialise properly. XX do r8-r15 too?
217 shl $(N_FIXNUM_TAG_BITS),%rcx # (fixnumize num-args)
220 mov 0(%rbx),%rdx # arg0
223 mov 8(%rbx),%rdi # arg1
226 mov 16(%rbx),%rsi # arg2
228 /* Registers rax, rcx, rdx, rdi, and rsi are now live. */
229 xor %rbx,%rbx # available
231 /* Alloc new frame. */
232 push %rbp # Dummy for return address
233 push %rbp # fp in save location S1
234 mov %rsp,%rbp # The current sp marks start of new frame.
235 sub $8,%rsp # Ensure 3 slots are allocated, two above.
238 call *CLOSURE_FUN_OFFSET(%rax)
240 /* If the function returned multiple values, it will return to
241 this point. Lose them */
246 /* Restore the stack, in case there was a stack change. */
256 /* FIXME Restore the NPX state. */
258 mov %rdx,%rax # c-val
259 #ifdef SUPPORT_FOMIT_FRAME_POINTER
260 mov %r15,%rbp # orig rbp
262 add $8,%rsp # no need for saved (overridden) rbp
267 SIZE(GNAME(call_into_lisp))
269 /* support for saving and restoring the NPX state from C */
271 .globl GNAME(fpu_save)
272 TYPE(GNAME(fpu_save))
273 .align align_16byte,0x90
275 fnsave (%rdi) # Save the NPX state. (resets NPX)
277 SIZE(GNAME(fpu_save))
279 .globl GNAME(fpu_restore)
280 TYPE(GNAME(fpu_restore))
281 .align align_16byte,0x90
283 frstor (%rdi) # Restore the NPX state.
285 SIZE(GNAME(fpu_restore))
288 * the undefined-function trampoline
291 .align align_16byte,0x90
292 .globl GNAME(undefined_tramp)
293 TYPE(GNAME(undefined_tramp))
294 GNAME(undefined_tramp):
295 pop 8(%rbp) # Save return PC for backtrace.
299 .byte UNDEFINED_FUN_ERROR
300 .byte sc_DescriptorReg # eax in the Descriptor-reg SC
302 SIZE(GNAME(undefined_tramp))
304 /* KLUDGE: FIND-ESCAPED-FRAME (SYS:SRC;CODE;DEBUG-INT.LISP) needs
305 * to know the name of the function immediately following the
306 * undefined-function trampoline. */
308 /* Our call-site does not take care of caller-saved xmm registers, so it
309 * falls to us spill them beforing hopping into C.
311 * We simply save all of them.
313 * (But for the sake of completeness, here is my understanding of the specs:)
315 * argument passing xmm0-7 xmm0-3
316 * caller-saved xmm8-15 xmm4-5
317 * callee-saved - xmm6-15
321 #define stkxmmsave(n) movaps %xmm##n, n*16(%rsp)
322 #define stkxmmload(n) movaps n*16(%rsp), %xmm##n
323 #define map_all_xmm(op) \
324 op(0);op(1);op(2);op(3);op(4);op(5);op(6);op(7); \
325 op(8);op(9);op(10);op(11);op(12);op(13);op(14);op(15);
328 .align align_16byte,0x90
329 .globl GNAME(alloc_tramp)
330 TYPE(GNAME(alloc_tramp))
333 push %rbp # Save old frame pointer.
334 mov %rsp,%rbp # Establish new frame.
337 map_all_xmm(stkxmmsave)
361 map_all_xmm(stkxmmload)
365 SIZE(GNAME(alloc_tramp))
369 * the closure trampoline
372 .align align_16byte,0x90
373 .globl GNAME(closure_tramp)
374 TYPE(GNAME(closure_tramp))
375 GNAME(closure_tramp):
376 mov FDEFN_FUN_OFFSET(%rax),%rax
377 /* FIXME: The '*' after "jmp" in the next line is from PVE's
378 * patch posted to the CMU CL mailing list Oct 6, 1999. It looks
379 * reasonable, and it certainly seems as though if CMU CL needs it,
380 * SBCL needs it too, but I haven't actually verified that it's
381 * right. It would be good to find a way to force the flow of
382 * control through here to test it. */
383 jmp *CLOSURE_FUN_OFFSET(%rax)
384 SIZE(GNAME(closure_tramp))
387 .align align_16byte,0x90
388 .globl GNAME(funcallable_instance_tramp)
389 #if !defined(LISP_FEATURE_DARWIN)
390 TYPE(GNAME(funcallable_instance_tramp))
392 GNAME(funcallable_instance_tramp):
393 mov FUNCALLABLE_INSTANCE_FUNCTION_OFFSET(%rax),%rax
394 /* KLUDGE: on this platform, whatever kind of function is in %rax
395 * now, the first word of it contains the address to jump to. */
396 jmp *CLOSURE_FUN_OFFSET(%rax)
397 #if !defined(LISP_FEATURE_DARWIN)
398 SIZE(GNAME(funcallable_instance_tramp))
401 * fun-end breakpoint magic
405 * For an explanation of the magic involved in function-end
406 * breakpoints, see the implementation in ppc-assem.S.
410 .globl GNAME(fun_end_breakpoint_guts)
412 GNAME(fun_end_breakpoint_guts):
413 /* Multiple Value return */
414 jc multiple_value_return
415 /* Single value return: The eventual return will now use the
416 multiple values return convention but with a return values
418 mov %rsp,%rbx # Setup ebx - the ofp.
419 sub $8,%rsp # Allocate one stack slot for the return value
420 mov $8,%rcx # Setup ecx for one return value.
421 #if defined(LISP_FEATURE_DARWIN)
422 mov GSYM(NIL),%rdi # default second value
423 mov GSYM(NIL),%rsi # default third value
425 mov $NIL,%rdi # default second value
426 mov $NIL,%rsi # default third value
428 multiple_value_return:
430 .globl GNAME(fun_end_breakpoint_trap)
431 .align align_16byte,0x90
432 GNAME(fun_end_breakpoint_trap):
434 .byte trap_FunEndBreakpoint
435 hlt # We should never return here.
437 .globl GNAME(fun_end_breakpoint_end)
438 GNAME(fun_end_breakpoint_end):
441 .globl GNAME(do_pending_interrupt)
442 TYPE(GNAME(do_pending_interrupt))
443 .align align_16byte,0x90
444 GNAME(do_pending_interrupt):
446 .byte trap_PendingInterrupt
448 SIZE(GNAME(do_pending_interrupt))
450 .globl GNAME(post_signal_tramp)
451 TYPE(GNAME(post_signal_tramp))
452 .align align_16byte,0x90
453 GNAME(post_signal_tramp):
454 /* this is notionally the second half of a function whose first half
455 * doesn't exist. This is where call_into_lisp returns when called
456 * using return_to_lisp_function */
467 /* skip RBP and RSP */
475 SIZE(GNAME(post_signal_tramp))
478 .align align_16byte,0x90
479 .globl GNAME(fast_bzero)
480 TYPE(GNAME(fast_bzero))
482 #ifdef LISP_FEATURE_WIN32
484 #define redsave(reg,off) movups reg,-off(%rsp)
485 #define redrestore(reg,off) movups -off(%rsp),reg
488 #define redsave(reg,off)
489 #define redrestore(reg,off)
493 /* A fast routine for zero-filling blocks of memory that are
494 * guaranteed to start and end at a 4096-byte aligned address.
496 shr $6, %rsi /* Amount of 64-byte blocks to copy */
497 jz Lend /* If none, stop */
498 mov %rsi, %rcx /* Save start address */
500 xorps %xmmreg, %xmmreg /* Zero the XMM register */
505 /* Copy the 16 zeroes from xmm7 to memory, 4 times. MOVNTDQ is the
506 * non-caching double-quadword moving variant, i.e. the memory areas
507 * we're touching are not fetched into the L1 cache, since we're just
508 * going to overwrite the memory soon anyway.
510 movntdq %xmmreg, 0(%rdi)
511 movntdq %xmmreg, 16(%rdi)
512 movntdq %xmmreg, 32(%rdi)
513 movntdq %xmmreg, 48(%rdi)
515 add $64, %rdi /* Advance pointer */
516 dec %rsi /* Decrement 64-byte block count */
518 mfence /* Ensure that the writes are globally visible, since
519 * MOVNTDQ is weakly ordered */
520 redrestore(%xmmreg,16)
521 prefetcht0 0(%rcx) /* Prefetch the start of the block into cache,
522 * since it's likely to be used immediately. */
525 SIZE(GNAME(fast_bzero))
528 /* When LISP_FEATURE_C_STACK_IS_CONTROL_STACK, we cannot safely scrub
529 * the control stack from C, largely due to not knowing where the
530 * active stack frame ends. On such platforms, we reimplement the
531 * core scrubbing logic in assembly, in this case here:
534 .align align_16byte,0x90
535 .globl GNAME(arch_scrub_control_stack)
536 TYPE(GNAME(arch_scrub_control_stack))
537 GNAME(arch_scrub_control_stack):
538 /* We are passed three parameters:
539 * A (struct thread *) in RDI,
540 * the address of the guard page in RSI, and
541 * the address of the hard guard page in RDX.
542 * We may trash RAX, RCX, and R8-R11 with impunity.
543 * [RSP] is our return address, [RSP-8] is the first
544 * stack slot to scrub. */
546 /* We start by setting up our scrub pointer in RAX, our
547 * guard page upper bound in R8, and our hard guard
548 * page upper bound in R9. */
550 #ifdef LISP_FEATURE_DARWIN
551 mov GSYM(GNAME(os_vm_page_size)),%r9
553 mov os_vm_page_size,%r9
558 /* Now we begin our main scrub loop. */
561 /* If we're about to scrub the hard guard page, exit. */
563 jae ascs_check_guard_page
567 ascs_check_guard_page:
568 /* If we're about to scrub the guard page, and the guard
569 * page is protected, exit. */
574 cmpq $(NIL), THREAD_CONTROL_STACK_GUARD_PAGE_PROTECTED_OFFSET(%rdi)
577 /* Clear memory backwards to the start of the (4KiB) page */
584 /* If we're about to hit the hard guard page, exit. */
588 /* If the next (previous?) 4KiB page contains a non-zero
589 * word, continue scrubbing. */
599 SIZE(GNAME(arch_scrub_control_stack))