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