5f1ff8b2dc04d27ceb3851b70d208b5d0a409b7f
[sbcl.git] / src / runtime / x86-64-assem.S
1 /*
2  * very-low-level utilities for runtime support
3  */
4
5 /*
6  * This software is part of the SBCL system. See the README file for
7  * more information.
8  *
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.
14  */
15 \f
16 #define LANGUAGE_ASSEMBLY
17 #include "genesis/config.h"
18 #include "validate.h"
19 #include "sbcl.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"
26         
27 /* Minimize conditionalization for different OS naming schemes. */
28 #if defined __linux__  || defined __FreeBSD__ /* (but *not* OpenBSD) */
29 #define GNAME(var) var
30 #else
31 #define GNAME(var) _##var
32 #endif
33
34 /* Get the right type of alignment. Linux and FreeBSD (but not OpenBSD)
35  * want alignment in bytes. */
36 #if defined(__linux__) || defined(__FreeBSD__)
37 #define align_4byte     4
38 #define align_8byte     8
39 #define align_16byte    16
40 #define align_32byte    32
41 #else
42 #define align_4byte     2
43 #define align_8byte     3
44 #define align_16byte    4       
45 #endif                  
46
47 /*
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
51  * other platforms.
52  */
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)
57 #else
58 #define TYPE(name)
59 #define SIZE(name)
60 #endif
61
62 /*
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)
71 #define TRAP ud2
72 #else
73 #define TRAP int3
74 #endif
75
76 /*
77  * More Apple assembler hacks
78  */
79
80 #if defined(LISP_FEATURE_DARWIN)
81 /* global symbol x86-64 sym(%rip) hack:*/
82 #define GSYM(name) name(%rip)
83 #define END()
84 #else
85 #define GSYM(name) $name
86 #define END() .end
87 #endif
88
89         
90         .text
91         .globl  GNAME(all_threads)
92         
93         
94 \f
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)
102  */
103         .text
104         .align  align_16byte,0x90
105         .globl  GNAME(call_into_c)
106         TYPE(GNAME(call_into_c))
107 GNAME(call_into_c):
108         push    %rbp            # Save old frame pointer.
109         mov     %rsp,%rbp       # Establish new frame.
110
111         push    %rsi            # args are going in here
112         push    %rdi
113         mov     16(%rbp),%rdi
114         mov     24(%rbp),%rsi
115         mov     32(%rbp),%rdx
116         mov     40(%rbp),%rcx
117         mov     48(%rbp),%rcx
118         mov     56(%rbp),%r8
119         mov     64(%rbp),%r9
120         call    *%rax
121         mov     %rbp,%rsp
122         pop     %rbp
123         ret
124         SIZE(GNAME(call_into_c))
125
126 \f
127         .text   
128         .globl  GNAME(call_into_lisp_first_time)
129         TYPE(GNAME(call_into_lisp_first_time))
130                 
131 /* The *ALIEN-STACK* pointer is set up on the first call_into_lisp when
132  * the stack changes.  We don't worry too much about saving registers 
133  * here, because we never expect to return from the initial call to lisp 
134  * anyway */
135         
136         .align  align_16byte,0x90
137 GNAME(call_into_lisp_first_time):
138         push    %rbp            # Save old frame pointer.
139         mov     %rsp,%rbp       # Establish new frame.
140         mov     %rsp,ALIEN_STACK + SYMBOL_VALUE_OFFSET
141 #if defined(LISP_FEATURE_DARWIN)
142         movq    GSYM(GNAME(all_threads)),%rax
143 #else
144         movq    GNAME(all_threads),%rax
145 #endif
146         mov     THREAD_CONTROL_STACK_END_OFFSET(%rax) ,%rsp
147         jmp     Lstack
148 \f
149         .text   
150         .globl  GNAME(call_into_lisp)
151         TYPE(GNAME(call_into_lisp))
152                 
153 /*
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
158  */
159         
160         .align  align_16byte,0x90
161 GNAME(call_into_lisp):
162         push    %rbp            # Save old frame pointer.
163         mov     %rsp,%rbp       # Establish new frame.
164 Lstack:
165         /* FIXME x86 saves FPU state here */
166         push    %rbx    # these regs are callee-saved according to C
167         push    %r12    # so must be preserved and restored when 
168         push    %r13    # the lisp function returns
169         push    %r14    #
170         push    %r15    #
171
172         mov     %rsp,%rbx       # remember current stack
173         push    %rbx            # Save entry stack on (maybe) new stack.
174
175         push    %rdi    # args from C
176         push    %rsi    #
177         push    %rdx    #
178 #ifdef LISP_FEATURE_SB_THREAD
179 #ifdef LISP_FEATURE_GCC_TLS
180         movq    %fs:0, %rax
181         movq    GNAME(current_thread)@TPOFF(%rax), %r12
182 #else
183 #ifdef LISP_FEATURE_DARWIN
184         mov     GSYM(GNAME(specials)),%rdi
185 #else
186         mov     specials,%rdi
187 #endif
188         call    GNAME(pthread_getspecific)
189         mov     %rax,%r12
190 #endif
191 #endif
192         pop     %rcx    # num args
193         pop     %rbx    # arg vector
194         pop     %rax    # function ptr/lexenv
195
196         xor     %rdx,%rdx       # clear any descriptor registers 
197         xor     %rdi,%rdi       # that we can't be sure we'll 
198         xor     %rsi,%rsi       # initialise properly.  XX do r8-r15 too?
199         shl     $3,%rcx         # (fixnumize num-args)
200         cmp     $0,%rcx
201         je      Ldone
202         mov     0(%rbx),%rdx    # arg0
203         cmp     $8,%rcx
204         je      Ldone
205         mov     8(%rbx),%rdi    # arg1
206         cmp     $16,%rcx
207         je      Ldone
208         mov     16(%rbx),%rsi   # arg2
209 Ldone:  
210         /* Registers rax, rcx, rdx, rdi, and rsi are now live. */
211         xor     %rbx,%rbx       # available
212
213         /* Alloc new frame. */
214         mov     %rsp,%rbx       # The current sp marks start of new frame.
215         push    %rbp            # fp in save location S0
216         sub     $16,%rsp        # Ensure 3 slots are allocated, one above.
217         mov     %rbx,%rbp       # Switch to new frame.
218
219 Lcall:
220         call    *CLOSURE_FUN_OFFSET(%rax)
221         
222         /* If the function returned multiple values, it will return to
223            this point.  Lose them */
224         jnc     LsingleValue    
225         mov     %rbx, %rsp
226 LsingleValue:   
227
228 /* Restore the stack, in case there was a stack change. */
229         pop     %rsp            # c-sp
230
231 /* Restore C regs */
232         pop     %r15
233         pop     %r14
234         pop     %r13
235         pop     %r12
236         pop     %rbx
237
238 /* FIXME Restore the NPX state. */
239
240         /* return value is already in rax where lisp expects it */
241         leave
242         ret
243         SIZE(GNAME(call_into_lisp))
244 \f
245 /* support for saving and restoring the NPX state from C */
246         .text
247         .globl  GNAME(fpu_save)
248         TYPE(GNAME(fpu_save))
249         .align  2,0x90
250 GNAME(fpu_save):
251         fnsave  (%rdi)          # Save the NPX state. (resets NPX)
252         ret
253         SIZE(GNAME(fpu_save))
254
255         .globl  GNAME(fpu_restore)
256         TYPE(GNAME(fpu_restore))
257         .align  2,0x90
258 GNAME(fpu_restore):
259         frstor  (%rdi)          # Restore the NPX state.
260         ret
261         SIZE(GNAME(fpu_restore))
262 \f
263 /*
264  * the undefined-function trampoline
265  */
266         .text
267         .align  align_16byte,0x90
268         .globl  GNAME(undefined_tramp)
269         TYPE(GNAME(undefined_tramp))
270 GNAME(undefined_tramp):
271         TRAP
272         .byte   trap_Error
273         .byte   2
274         .byte   UNDEFINED_FUN_ERROR
275         .byte   sc_DescriptorReg # eax in the Descriptor-reg SC
276         ret
277         SIZE(GNAME(undefined_tramp))
278
279
280         .text
281         .align  align_16byte,0x90
282         .globl  GNAME(alloc_tramp)
283         TYPE(GNAME(alloc_tramp))
284 GNAME(alloc_tramp):
285         push    %rbp            # Save old frame pointer.
286         mov     %rsp,%rbp       # Establish new frame.
287         push    %rax
288         push    %rcx
289         push    %rdx
290         push    %rsi
291         push    %rdi
292         push    %r8
293         push    %r9
294         push    %r10
295         push    %r11
296         mov     16(%rbp),%rdi   
297         call    GNAME(alloc)
298         mov     %rax,16(%rbp)
299         pop     %r11
300         pop     %r10
301         pop     %r9
302         pop     %r8
303         pop     %rdi
304         pop     %rsi
305         pop     %rdx
306         pop     %rcx
307         pop     %rax
308         pop     %rbp
309         ret
310         SIZE(GNAME(alloc_tramp))
311
312                 
313 /*
314  * the closure trampoline
315  */
316         .text
317         .align  align_16byte,0x90
318         .globl  GNAME(closure_tramp)
319         TYPE(GNAME(closure_tramp))
320 GNAME(closure_tramp):
321         mov     FDEFN_FUN_OFFSET(%rax),%rax
322         /* FIXME: The '*' after "jmp" in the next line is from PVE's
323          * patch posted to the CMU CL mailing list Oct 6, 1999. It looks
324          * reasonable, and it certainly seems as though if CMU CL needs it,
325          * SBCL needs it too, but I haven't actually verified that it's
326          * right. It would be good to find a way to force the flow of
327          * control through here to test it. */
328         jmp     *CLOSURE_FUN_OFFSET(%rax)
329         SIZE(GNAME(closure_tramp))
330
331         .text
332         .align  align_16byte,0x90
333         .globl  GNAME(funcallable_instance_tramp)
334 #if !defined(LISP_FEATURE_DARWIN)
335         .type   GNAME(funcallable_instance_tramp),@function
336 #endif
337         GNAME(funcallable_instance_tramp):
338         mov     FUNCALLABLE_INSTANCE_FUNCTION_OFFSET(%rax),%rax
339         /* KLUDGE: on this platform, whatever kind of function is in %rax
340          * now, the first word of it contains the address to jump to. */
341         jmp     *CLOSURE_FUN_OFFSET(%rax)
342 #if !defined(LISP_FEATURE_DARWIN)
343         .size   GNAME(funcallable_instance_tramp), .-GNAME(funcallable_instance_tramp)
344 #endif
345 /*
346  * fun-end breakpoint magic
347  */
348         .text
349         .globl  GNAME(fun_end_breakpoint_guts)
350         .align  align_16byte
351 GNAME(fun_end_breakpoint_guts):
352         /* Multiple Value return */
353         jc      multiple_value_return
354         /* Single value return: The eventual return will now use the
355            multiple values return convention but with a return values
356            count of one. */
357         mov     %rsp,%rbx       # Setup ebx - the ofp.
358         sub     $8,%rsp         # Allocate one stack slot for the return value
359         mov     $8,%rcx         # Setup ecx for one return value.
360 #if defined(LISP_FEATURE_DARWIN)
361         mov     GSYM(NIL),%rdi  # default second value
362         mov     GSYM(NIL),%rsi  # default third value
363 #else
364         mov     $NIL,%rdi       # default second value
365         mov     $NIL,%rsi       # default third value
366 #endif
367 multiple_value_return:
368         
369         .globl  GNAME(fun_end_breakpoint_trap)
370 GNAME(fun_end_breakpoint_trap):
371         TRAP
372         .byte   trap_FunEndBreakpoint
373         hlt                     # We should never return here.
374
375         .globl  GNAME(fun_end_breakpoint_end)
376 GNAME(fun_end_breakpoint_end):
377
378 \f
379         .globl  GNAME(do_pending_interrupt)
380         TYPE(GNAME(do_pending_interrupt))
381         .align  align_16byte,0x90
382 GNAME(do_pending_interrupt):
383         TRAP
384         .byte   trap_PendingInterrupt
385         ret
386         SIZE(GNAME(do_pending_interrupt))
387 \f
388         .globl  GNAME(post_signal_tramp)
389         TYPE(GNAME(post_signal_tramp))
390         .align  align_16byte,0x90
391 GNAME(post_signal_tramp):
392         /* this is notionally the second half of a function whose first half
393          * doesn't exist.  This is where call_into_lisp returns when called 
394          * using return_to_lisp_function */
395         popq %r15
396         popq %r14
397         popq %r13
398         popq %r12
399         popq %r11
400         popq %r10
401         popq %r9
402         popq %r8
403         popq %rdi
404         popq %rsi
405         /* skip RBP and RSP */
406         popq %rbx
407         popq %rdx
408         popq %rcx
409         popq %rax
410         popfq
411         leave
412         ret
413         SIZE(GNAME(post_signal_tramp))
414 \f
415         .text
416         .align  align_16byte,0x90
417         .globl  GNAME(fast_bzero)
418         TYPE(GNAME(fast_bzero))
419         
420 GNAME(fast_bzero):
421         /* A fast routine for zero-filling blocks of memory that are
422          * guaranteed to start and end at a 4096-byte aligned address.
423          */
424         shr $6, %rsi              /* Amount of 64-byte blocks to copy */
425         jz Lend                   /* If none, stop */
426         mov %rsi, %rcx            /* Save start address */
427         movups %xmm7, -16(%rsp)   /* Save XMM register */
428         xorps  %xmm7, %xmm7       /* Zero the XMM register */
429         jmp Lloop
430         .align align_16byte                 
431 Lloop:
432
433         /* Copy the 16 zeroes from xmm7 to memory, 4 times. MOVNTDQ is the
434          * non-caching double-quadword moving variant, i.e. the memory areas
435          * we're touching are not fetched into the L1 cache, since we're just
436          * going to overwrite the memory soon anyway.
437          */
438         movntdq %xmm7, 0(%rdi)
439         movntdq %xmm7, 16(%rdi)
440         movntdq %xmm7, 32(%rdi)
441         movntdq %xmm7, 48(%rdi)
442
443         add $64, %rdi  /* Advance pointer */
444         dec %rsi       /* Decrement 64-byte block count */
445         jnz Lloop
446         mfence         /* Ensure that the writes are globally visible, since
447                         * MOVNTDQ is weakly ordered */
448         movups -16(%rsp), %xmm7 /* Restore the XMM register */
449         prefetcht0 0(%rcx)      /* Prefetch the start of the block into cache,
450                                  * since it's likely to be used immediately. */
451 Lend:        
452         ret
453         SIZE(GNAME(fast_bzero))
454
455         END()