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 LISP_FEATURE_FREEBSD || defined __OpenBSD__ || defined __NetBSD__ || defined __sun || defined _WIN64 || defined __DragonFly__
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(LISP_FEATURE_FREEBSD) || defined(__OpenBSD__) || defined __NetBSD__ || defined(__sun) || defined _WIN64 || defined(__DragonFly__)
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)
86 #define GSYM(name) $name
91 .globl GNAME(all_threads)
95 /* From lower to higher-numbered addresses, the stack contains
96 * return address, arg 0, arg 1, arg 2 ...
97 * rax contains the address of the function to call
98 * Lisp expects return value in rax, which is already consistent with C
99 * XXXX correct floating point handling is unimplemented so far
100 * Based on comments cleaned from x86-assem.S, we believe that
101 * Lisp is expecting us to preserve rsi, rdi, rsp (no idea about r8-15)
104 .align align_16byte,0x90
105 .globl GNAME(call_into_c)
106 TYPE(GNAME(call_into_c))
108 push %rbp # Save old frame pointer.
109 mov %rsp,%rbp # Establish new frame.
111 push %rsi # args are going in here
124 SIZE(GNAME(call_into_c))
128 .globl GNAME(call_into_lisp_first_time)
129 TYPE(GNAME(call_into_lisp_first_time))
131 /* We don't worry too much about saving registers
132 * here, because we never expect to return from the initial call to lisp
135 .align align_16byte,0x90
136 GNAME(call_into_lisp_first_time):
137 push %rbp # Save old frame pointer.
138 mov %rsp,%rbp # Establish new frame.
139 #if defined(LISP_FEATURE_DARWIN)
140 movq GSYM(GNAME(all_threads)),%rax
142 movq GNAME(all_threads),%rax
144 mov THREAD_CONTROL_STACK_END_OFFSET(%rax) ,%rsp
148 .globl GNAME(call_into_lisp)
149 TYPE(GNAME(call_into_lisp))
152 * amd64 calling convention: C expects that
153 * arguments go in rdi rsi rdx rcx r8 r9
154 * return values in rax rdx
155 * callee saves rbp rbx r12-15 if it uses them
157 #ifdef LISP_FEATURE_WIN32
158 # define SUPPORT_FOMIT_FRAME_POINTER
160 .align align_16byte,0x90
161 GNAME(call_into_lisp):
162 #ifdef SUPPORT_FOMIT_FRAME_POINTER
165 push %rbp # Save old frame pointer.
166 mov %rsp,%rbp # Establish new frame.
168 #ifdef SUPPORT_FOMIT_FRAME_POINTER
169 /* If called through call_into_lisp_first_time, %r15 becomes invalid
170 * here, but we will not return in that case. */
174 /* FIXME x86 saves FPU state here */
175 push %rbx # these regs are callee-saved according to C
176 push %r12 # so must be preserved and restored when
177 push %r13 # the lisp function returns
181 mov %rsp,%rbx # remember current stack
182 push %rbx # Save entry stack on (maybe) new stack.
184 push %rdi # args from C
187 #ifdef LISP_FEATURE_SB_THREAD
188 # ifdef SUPPORT_FOMIT_FRAME_POINTER
191 call GNAME(carry_frame_pointer)
195 #ifdef LISP_FEATURE_GCC_TLS
197 movq GNAME(current_thread)@TPOFF(%rax), %r12
199 #ifdef LISP_FEATURE_DARWIN
200 mov GSYM(GNAME(specials)),%rdi
204 call GNAME(pthread_getspecific)
209 pop %rbx # arg vector
210 pop %rax # function ptr/lexenv
212 xor %rdx,%rdx # clear any descriptor registers
213 xor %rdi,%rdi # that we can't be sure we'll
214 xor %rsi,%rsi # initialise properly. XX do r8-r15 too?
216 # It's tempting to think 'cmov' for these assignments, but don't:
217 # cmov does a memory cycle whether or not it moves, succumbing to
218 # a classic buffer overrun bug if argv[] is "badly" placed.
220 mov 0(%rbx),%rdx # arg0
223 mov 8(%rbx),%rdi # arg1
226 mov 16(%rbx),%rsi # arg2
228 shl $(N_FIXNUM_TAG_BITS),%rcx # (fixnumize num-args)
229 /* Registers rax, rcx, rdx, rdi, and rsi are now live. */
230 xor %rbx,%rbx # available
232 /* Alloc new frame. */
233 push %rbp # Dummy for return address
234 push %rbp # fp in save location S1
235 mov %rsp,%rbp # The current sp marks start of new frame.
236 sub $8,%rsp # Ensure 3 slots are allocated, two above.
239 call *CLOSURE_FUN_OFFSET(%rax)
241 /* If the function returned multiple values, it will return to
242 this point. Lose them */
247 /* Restore the stack, in case there was a stack change. */
257 /* FIXME Restore the NPX state. */
259 mov %rdx,%rax # c-val
260 #ifdef SUPPORT_FOMIT_FRAME_POINTER
261 mov %r15,%rbp # orig rbp
263 add $8,%rsp # no need for saved (overridden) rbp
268 SIZE(GNAME(call_into_lisp))
270 /* support for saving and restoring the NPX state from C */
272 .globl GNAME(fpu_save)
273 TYPE(GNAME(fpu_save))
274 .align align_16byte,0x90
276 fnsave (%rdi) # Save the NPX state. (resets NPX)
278 SIZE(GNAME(fpu_save))
280 .globl GNAME(fpu_restore)
281 TYPE(GNAME(fpu_restore))
282 .align align_16byte,0x90
284 frstor (%rdi) # Restore the NPX state.
286 SIZE(GNAME(fpu_restore))
289 * the undefined-function trampoline
292 .align align_16byte,0x90
293 .globl GNAME(undefined_tramp)
294 TYPE(GNAME(undefined_tramp))
295 GNAME(undefined_tramp):
296 pop 8(%rbp) # Save return PC for backtrace.
300 .byte UNDEFINED_FUN_ERROR
301 .byte sc_DescriptorReg # eax in the Descriptor-reg SC
303 SIZE(GNAME(undefined_tramp))
306 .align align_16byte,0x90
307 .globl GNAME(undefined_alien_function)
308 TYPE(GNAME(undefined_alien_function))
309 GNAME(undefined_alien_function):
310 pop 8(%rbp) # Save return PC for backtrace.
314 .byte UNDEFINED_ALIEN_FUN_ERROR
316 FIXME: make independt of the encoding changes. */
321 SIZE(GNAME(undefined_alien_function))
323 /* KLUDGE: FIND-ESCAPED-FRAME (SYS:SRC;CODE;DEBUG-INT.LISP) needs
324 * to know the name of the function immediately following the
325 * undefined-function trampoline. */
327 /* Our call-site does not take care of caller-saved xmm registers, so it
328 * falls to us spill them beforing hopping into C.
330 * We simply save all of them.
332 * (But for the sake of completeness, here is my understanding of the specs:)
334 * argument passing xmm0-7 xmm0-3
335 * caller-saved xmm8-15 xmm4-5
336 * callee-saved - xmm6-15
340 #define stkxmmsave(n) movaps %xmm##n, n*16(%rsp)
341 #define stkxmmload(n) movaps n*16(%rsp), %xmm##n
342 #define map_all_xmm(op) \
343 op(0);op(1);op(2);op(3);op(4);op(5);op(6);op(7); \
344 op(8);op(9);op(10);op(11);op(12);op(13);op(14);op(15);
347 .align align_16byte,0x90
348 .globl GNAME(alloc_tramp)
349 TYPE(GNAME(alloc_tramp))
352 push %rbp # Save old frame pointer.
353 mov %rsp,%rbp # Establish new frame.
356 map_all_xmm(stkxmmsave)
357 # Doing rax twice is to maintain 16-byte stack alignment.
368 # r12 through r15 are callee-saved
381 pop %rax # corresponds to "extra" push
382 map_all_xmm(stkxmmload)
386 SIZE(GNAME(alloc_tramp))
390 * the closure trampoline
393 .align align_16byte,0x90
394 .globl GNAME(closure_tramp)
395 TYPE(GNAME(closure_tramp))
396 GNAME(closure_tramp):
397 mov FDEFN_FUN_OFFSET(%rax),%rax
398 /* FIXME: The '*' after "jmp" in the next line is from PVE's
399 * patch posted to the CMU CL mailing list Oct 6, 1999. It looks
400 * reasonable, and it certainly seems as though if CMU CL needs it,
401 * SBCL needs it too, but I haven't actually verified that it's
402 * right. It would be good to find a way to force the flow of
403 * control through here to test it. */
404 jmp *CLOSURE_FUN_OFFSET(%rax)
405 SIZE(GNAME(closure_tramp))
408 .align align_16byte,0x90
409 .globl GNAME(funcallable_instance_tramp)
410 #if !defined(LISP_FEATURE_DARWIN)
411 TYPE(GNAME(funcallable_instance_tramp))
413 GNAME(funcallable_instance_tramp):
414 mov FUNCALLABLE_INSTANCE_FUNCTION_OFFSET(%rax),%rax
415 /* KLUDGE: on this platform, whatever kind of function is in %rax
416 * now, the first word of it contains the address to jump to. */
417 jmp *CLOSURE_FUN_OFFSET(%rax)
418 #if !defined(LISP_FEATURE_DARWIN)
419 SIZE(GNAME(funcallable_instance_tramp))
422 * fun-end breakpoint magic
426 * For an explanation of the magic involved in function-end
427 * breakpoints, see the implementation in ppc-assem.S.
431 .globl GNAME(fun_end_breakpoint_guts)
433 GNAME(fun_end_breakpoint_guts):
434 /* Multiple Value return */
435 jc multiple_value_return
436 /* Single value return: The eventual return will now use the
437 multiple values return convention but with a return values
439 mov %rsp,%rbx # Setup ebx - the ofp.
440 sub $8,%rsp # Allocate one stack slot for the return value
441 mov $(1 << N_FIXNUM_TAG_BITS),%rcx # Setup ecx for one return value.
442 mov GSYM(NIL),%rdi # default second value
443 mov GSYM(NIL),%rsi # default third value
444 multiple_value_return:
446 .globl GNAME(fun_end_breakpoint_trap)
447 .align align_16byte,0x90
448 GNAME(fun_end_breakpoint_trap):
450 .byte trap_FunEndBreakpoint
451 hlt # We should never return here.
453 .globl GNAME(fun_end_breakpoint_end)
454 GNAME(fun_end_breakpoint_end):
457 .globl GNAME(do_pending_interrupt)
458 TYPE(GNAME(do_pending_interrupt))
459 .align align_16byte,0x90
460 GNAME(do_pending_interrupt):
462 .byte trap_PendingInterrupt
464 SIZE(GNAME(do_pending_interrupt))
466 .globl GNAME(post_signal_tramp)
467 TYPE(GNAME(post_signal_tramp))
468 .align align_16byte,0x90
469 GNAME(post_signal_tramp):
470 /* this is notionally the second half of a function whose first half
471 * doesn't exist. This is where call_into_lisp returns when called
472 * using return_to_lisp_function */
483 /* skip RBP and RSP */
491 SIZE(GNAME(post_signal_tramp))
493 /* When LISP_FEATURE_C_STACK_IS_CONTROL_STACK, we cannot safely scrub
494 * the control stack from C, largely due to not knowing where the
495 * active stack frame ends. On such platforms, we reimplement the
496 * core scrubbing logic in assembly, in this case here:
499 .align align_16byte,0x90
500 .globl GNAME(arch_scrub_control_stack)
501 TYPE(GNAME(arch_scrub_control_stack))
502 GNAME(arch_scrub_control_stack):
503 /* We are passed three parameters:
504 * A (struct thread *) in RDI,
505 * the address of the guard page in RSI, and
506 * the address of the hard guard page in RDX.
507 * We may trash RAX, RCX, and R8-R11 with impunity.
508 * [RSP] is our return address, [RSP-8] is the first
509 * stack slot to scrub. */
511 /* We start by setting up our scrub pointer in RAX, our
512 * guard page upper bound in R8, and our hard guard
513 * page upper bound in R9. */
515 #ifdef LISP_FEATURE_DARWIN
516 mov GSYM(GNAME(os_vm_page_size)),%r9
518 mov os_vm_page_size,%r9
523 /* Now we begin our main scrub loop. */
526 /* If we're about to scrub the hard guard page, exit. */
528 jae ascs_check_guard_page
532 ascs_check_guard_page:
533 /* If we're about to scrub the guard page, and the guard
534 * page is protected, exit. */
539 cmpq $(NIL), THREAD_CONTROL_STACK_GUARD_PAGE_PROTECTED_OFFSET(%rdi)
542 /* Clear memory backwards to the start of the (4KiB) page */
549 /* If we're about to hit the hard guard page, exit. */
553 /* If the next (previous?) 4KiB page contains a non-zero
554 * word, continue scrubbing. */
564 SIZE(GNAME(arch_scrub_control_stack))