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__ /* (but *not* OpenBSD) */
29 #define GNAME(var) var
31 #define GNAME(var) _##var
34 /* Get the right type of alignment. Linux and FreeBSD (but not OpenBSD)
35 * want alignment in bytes. */
36 #if defined(__linux__) || defined(__FreeBSD__)
39 #define align_16byte 16
40 #define align_32byte 32
44 #define align_16byte 4
48 * The assembler used for win32 doesn't like .type or .size directives,
49 * so we want to conditionally kill them out. So let's wrap them in macros
50 * that are defined to be no-ops on win32. Hopefully this still works on
53 #if !defined(LISP_FEATURE_WIN32) && !defined(LISP_FEATURE_DARWIN)
54 #define TYPE(name) .type name,@function
55 #define SIZE(name) .size name,.-name
56 #define DOLLAR(name) $(name)
63 * x86/darwin (as of MacOS X 10.4.5) doesn't reliably fire signal
64 * handlers (SIGTRAP or Mach exception handlers) for 0xCC, wo we have
65 * to use ud2 instead. ud2 is an undefined opcode, #x0b0f, or
66 * 0F 0B in low-endian notation, that causes SIGILL to fire. We check
67 * for this instruction in the SIGILL handler and if we see it, we
68 * advance the EIP by two bytes to skip over ud2 instruction and
69 * call sigtrap_handler. */
70 #if defined(LISP_FEATURE_DARWIN)
77 * More Apple assembler hacks
80 #if defined(LISP_FEATURE_DARWIN)
81 /* global symbol x86-64 sym(%rip) hack:*/
82 #define GSYM(name) name(%rip)
85 #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, whic 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 /* ABI requires that the direction flag be clear on function
111 push %rbp # Save old frame pointer.
112 mov %rsp,%rbp # Establish new frame.
114 push %rsi # args are going in here
127 SIZE(GNAME(call_into_c))
131 .globl GNAME(call_into_lisp_first_time)
132 TYPE(GNAME(call_into_lisp_first_time))
134 /* The *ALIEN-STACK* pointer is set up on the first call_into_lisp when
135 * the stack changes. We don't worry too much about saving registers
136 * here, because we never expect to return from the initial call to lisp
139 .align align_16byte,0x90
140 GNAME(call_into_lisp_first_time):
141 push %rbp # Save old frame pointer.
142 mov %rsp,%rbp # Establish new frame.
143 mov %rsp,ALIEN_STACK + SYMBOL_VALUE_OFFSET
144 #if defined(LISP_FEATURE_DARWIN)
145 movq GSYM(GNAME(all_threads)),%rax
147 movq GNAME(all_threads),%rax
149 mov THREAD_CONTROL_STACK_START_OFFSET(%rax) ,%rsp
150 /* don't think too hard about what happens if we get interrupted
152 add $(THREAD_CONTROL_STACK_SIZE)-16,%rsp
156 .globl GNAME(call_into_lisp)
157 TYPE(GNAME(call_into_lisp))
160 * amd64 calling convention: C expects that
161 * arguments go in rdi rsi rdx rcx r8 r9
162 * return values in rax rdx
163 * callee saves rbp rbx r12-15 if it uses them
166 .align align_16byte,0x90
167 GNAME(call_into_lisp):
168 push %rbp # Save old frame pointer.
169 mov %rsp,%rbp # Establish new frame.
171 /* FIXME x86 saves FPU state here */
172 push %rbx # these regs are callee-saved according to C
173 push %r12 # so must be preserved and restored when
174 push %r13 # the lisp function returns
178 mov %rsp,%rbx # remember current stack
179 push %rbx # Save entry stack on (maybe) new stack.
181 push %rdi # args from C
184 #ifdef LISP_FEATURE_SB_THREAD
186 call pthread_getspecific
190 pop %rbx # arg vector
191 pop %rax # function ptr/lexenv
193 xor %rdx,%rdx # clear any descriptor registers
194 xor %rdi,%rdi # that we can't be sure we'll
195 xor %rsi,%rsi # initialise properly. XX do r8-r15 too?
196 shl $3,%rcx # (fixnumize num-args)
199 mov 0(%rbx),%rdx # arg0
202 mov 8(%rbx),%rdi # arg1
205 mov 16(%rbx),%rsi # arg2
207 /* Registers rax, rcx, rdx, rdi, and rsi are now live. */
208 xor %rbx,%rbx # available
210 /* Alloc new frame. */
211 mov %rsp,%rbx # The current sp marks start of new frame.
212 push %rbp # fp in save location S0
213 sub $16,%rsp # Ensure 3 slots are allocated, one above.
214 mov %rbx,%rbp # Switch to new frame.
217 call *CLOSURE_FUN_OFFSET(%rax)
219 /* If the function returned multiple values, it will return to
220 this point. Lose them */
225 /* Restore the stack, in case there was a stack change. */
235 /* ABI requires that the direction flag be clear on function
239 /* FIXME Restore the NPX state. */
241 /* return value is already in rax where lisp expects it */
244 SIZE(GNAME(call_into_lisp))
246 /* support for saving and restoring the NPX state from C */
248 .globl GNAME(fpu_save)
249 TYPE(GNAME(fpu_save))
253 fnsave (%rax) # Save the NPX state. (resets NPX)
255 SIZE(GNAME(fpu_save))
257 .globl GNAME(fpu_restore)
258 TYPE(GNAME(fpu_restore))
262 frstor (%rax) # Restore the NPX state.
264 SIZE(GNAME(fpu_restore))
267 * the undefined-function trampoline
270 .align align_16byte,0x90
271 .globl GNAME(undefined_tramp)
272 TYPE(GNAME(undefined_tramp))
273 GNAME(undefined_tramp):
277 .byte UNDEFINED_FUN_ERROR
278 .byte sc_DescriptorReg # eax in the Descriptor-reg SC
280 SIZE(GNAME(undefined_tramp))
284 .align align_16byte,0x90
285 .globl GNAME(alloc_tramp)
286 TYPE(GNAME(alloc_tramp))
288 push %rbp # Save old frame pointer.
289 mov %rsp,%rbp # Establish new frame.
313 SIZE(GNAME(alloc_tramp))
317 * the closure trampoline
320 .align align_16byte,0x90
321 .globl GNAME(closure_tramp)
322 TYPE(GNAME(closure_tramp))
323 GNAME(closure_tramp):
324 mov FDEFN_FUN_OFFSET(%rax),%rax
325 /* FIXME: The '*' after "jmp" in the next line is from PVE's
326 * patch posted to the CMU CL mailing list Oct 6, 1999. It looks
327 * reasonable, and it certainly seems as though if CMU CL needs it,
328 * SBCL needs it too, but I haven't actually verified that it's
329 * right. It would be good to find a way to force the flow of
330 * control through here to test it. */
331 jmp *CLOSURE_FUN_OFFSET(%rax)
332 SIZE(GNAME(closure_tramp))
335 .align align_16byte,0x90
336 .globl GNAME(funcallable_instance_tramp)
337 #if !defined(LISP_FEATURE_DARWIN)
338 .type GNAME(funcallable_instance_tramp),@function
340 GNAME(funcallable_instance_tramp):
341 mov FUNCALLABLE_INSTANCE_FUNCTION_OFFSET(%rax),%rax
342 /* KLUDGE: on this platform, whatever kind of function is in %rax
343 * now, the first word of it contains the address to jump to. */
344 jmp *CLOSURE_FUN_OFFSET(%rax)
345 #if !defined(LISP_FEATURE_DARWIN)
346 .size GNAME(funcallable_instance_tramp), .-GNAME(funcallable_instance_tramp)
349 * fun-end breakpoint magic
352 .globl GNAME(fun_end_breakpoint_guts)
354 GNAME(fun_end_breakpoint_guts):
355 /* Multiple Value return */
356 jc multiple_value_return
357 /* Single value return: The eventual return will now use the
358 multiple values return convention but with a return values
360 mov %rsp,%rbx # Setup ebx - the ofp.
361 sub $8,%rsp # Allocate one stack slot for the return value
362 mov $8,%rcx # Setup ecx for one return value.
363 #if defined(LISP_FEATURE_DARWIN)
364 mov GSYM(NIL),%rdi # default second value
365 mov GSYM(NIL),%rsi # default third value
367 mov $NIL,%rdi # default second value
368 mov $NIL,%rsi # default third value
370 multiple_value_return:
372 .globl GNAME(fun_end_breakpoint_trap)
373 GNAME(fun_end_breakpoint_trap):
375 .byte trap_FunEndBreakpoint
376 hlt # We should never return here.
378 .globl GNAME(fun_end_breakpoint_end)
379 GNAME(fun_end_breakpoint_end):
382 .globl GNAME(do_pending_interrupt)
383 TYPE(GNAME(do_pending_interrupt))
384 .align align_16byte,0x90
385 GNAME(do_pending_interrupt):
387 .byte trap_PendingInterrupt
389 SIZE(GNAME(do_pending_interrupt))
391 .globl GNAME(post_signal_tramp)
392 TYPE(GNAME(post_signal_tramp))
393 .align align_16byte,0x90
394 GNAME(post_signal_tramp):
395 /* this is notionally the second half of a function whose first half
396 * doesn't exist. This is where call_into_lisp returns when called
397 * using return_to_lisp_function */
408 /* skip RBP and RSP */
416 SIZE(GNAME(post_signal_tramp))
419 .align align_16byte,0x90
420 .globl GNAME(fast_bzero)
421 TYPE(GNAME(fast_bzero))
424 /* A fast routine for zero-filling blocks of memory that are
425 * guaranteed to start and end at a 4096-byte aligned address.
427 shr $6, %rsi /* Amount of 64-byte blocks to copy */
428 jz Lend /* If none, stop */
429 mov %rsi, %rcx /* Save start address */
430 movups %xmm7, -16(%rsp) /* Save XMM register */
431 xorps %xmm7, %xmm7 /* Zero the XMM register */
436 /* Copy the 16 zeroes from xmm7 to memory, 4 times. MOVNTDQ is the
437 * non-caching double-quadword moving variant, i.e. the memory areas
438 * we're touching are not fetched into the L1 cache, since we're just
439 * going to overwrite the memory soon anyway.
441 movntdq %xmm7, 0(%rdi)
442 movntdq %xmm7, 16(%rdi)
443 movntdq %xmm7, 32(%rdi)
444 movntdq %xmm7, 48(%rdi)
446 add $64, %rdi /* Advance pointer */
447 dec %rsi /* Decrement 64-byte block count */
449 mfence /* Ensure that the writes are globally visible, since
450 * MOVNTDQ is weakly ordered */
451 movups -16(%rsp), %xmm7 /* Restore the XMM register */
452 prefetcht0 0(%rcx) /* Prefetch the start of the block into cache,
453 * since it's likely to be used immediately. */
456 SIZE(GNAME(fast_bzero))