1.0.16.31: --control-stack-size runtime argument
[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         mov     4(%rsp),%rax
252         fnsave  (%rax)          # Save the NPX state. (resets NPX)
253         ret
254         SIZE(GNAME(fpu_save))
255
256         .globl  GNAME(fpu_restore)
257         TYPE(GNAME(fpu_restore))
258         .align  2,0x90
259 GNAME(fpu_restore):
260         mov     4(%rsp),%rax
261         frstor  (%rax)          # Restore the NPX state.
262         ret
263         SIZE(GNAME(fpu_restore))
264 \f
265 /*
266  * the undefined-function trampoline
267  */
268         .text
269         .align  align_16byte,0x90
270         .globl  GNAME(undefined_tramp)
271         TYPE(GNAME(undefined_tramp))
272 GNAME(undefined_tramp):
273         TRAP
274         .byte   trap_Error
275         .byte   2
276         .byte   UNDEFINED_FUN_ERROR
277         .byte   sc_DescriptorReg # eax in the Descriptor-reg SC
278         ret
279         SIZE(GNAME(undefined_tramp))
280
281
282         .text
283         .align  align_16byte,0x90
284         .globl  GNAME(alloc_tramp)
285         TYPE(GNAME(alloc_tramp))
286 GNAME(alloc_tramp):
287         push    %rbp            # Save old frame pointer.
288         mov     %rsp,%rbp       # Establish new frame.
289         push    %rax
290         push    %rcx
291         push    %rdx
292         push    %rsi
293         push    %rdi
294         push    %r8
295         push    %r9
296         push    %r10
297         push    %r11
298         mov     16(%rbp),%rdi   
299         call    GNAME(alloc)
300         mov     %rax,16(%rbp)
301         pop     %r11
302         pop     %r10
303         pop     %r9
304         pop     %r8
305         pop     %rdi
306         pop     %rsi
307         pop     %rdx
308         pop     %rcx
309         pop     %rax
310         pop     %rbp
311         ret
312         SIZE(GNAME(alloc_tramp))
313
314                 
315 /*
316  * the closure trampoline
317  */
318         .text
319         .align  align_16byte,0x90
320         .globl  GNAME(closure_tramp)
321         TYPE(GNAME(closure_tramp))
322 GNAME(closure_tramp):
323         mov     FDEFN_FUN_OFFSET(%rax),%rax
324         /* FIXME: The '*' after "jmp" in the next line is from PVE's
325          * patch posted to the CMU CL mailing list Oct 6, 1999. It looks
326          * reasonable, and it certainly seems as though if CMU CL needs it,
327          * SBCL needs it too, but I haven't actually verified that it's
328          * right. It would be good to find a way to force the flow of
329          * control through here to test it. */
330         jmp     *CLOSURE_FUN_OFFSET(%rax)
331         SIZE(GNAME(closure_tramp))
332
333         .text
334         .align  align_16byte,0x90
335         .globl  GNAME(funcallable_instance_tramp)
336 #if !defined(LISP_FEATURE_DARWIN)
337         .type   GNAME(funcallable_instance_tramp),@function
338 #endif
339         GNAME(funcallable_instance_tramp):
340         mov     FUNCALLABLE_INSTANCE_FUNCTION_OFFSET(%rax),%rax
341         /* KLUDGE: on this platform, whatever kind of function is in %rax
342          * now, the first word of it contains the address to jump to. */
343         jmp     *CLOSURE_FUN_OFFSET(%rax)
344 #if !defined(LISP_FEATURE_DARWIN)
345         .size   GNAME(funcallable_instance_tramp), .-GNAME(funcallable_instance_tramp)
346 #endif
347 /*
348  * fun-end breakpoint magic
349  */
350         .text
351         .globl  GNAME(fun_end_breakpoint_guts)
352         .align  align_16byte
353 GNAME(fun_end_breakpoint_guts):
354         /* Multiple Value return */
355         jc      multiple_value_return
356         /* Single value return: The eventual return will now use the
357            multiple values return convention but with a return values
358            count of one. */
359         mov     %rsp,%rbx       # Setup ebx - the ofp.
360         sub     $8,%rsp         # Allocate one stack slot for the return value
361         mov     $8,%rcx         # Setup ecx for one return value.
362 #if defined(LISP_FEATURE_DARWIN)
363         mov     GSYM(NIL),%rdi  # default second value
364         mov     GSYM(NIL),%rsi  # default third value
365 #else
366         mov     $NIL,%rdi       # default second value
367         mov     $NIL,%rsi       # default third value
368 #endif
369 multiple_value_return:
370         
371         .globl  GNAME(fun_end_breakpoint_trap)
372 GNAME(fun_end_breakpoint_trap):
373         TRAP
374         .byte   trap_FunEndBreakpoint
375         hlt                     # We should never return here.
376
377         .globl  GNAME(fun_end_breakpoint_end)
378 GNAME(fun_end_breakpoint_end):
379
380 \f
381         .globl  GNAME(do_pending_interrupt)
382         TYPE(GNAME(do_pending_interrupt))
383         .align  align_16byte,0x90
384 GNAME(do_pending_interrupt):
385         TRAP
386         .byte   trap_PendingInterrupt
387         ret
388         SIZE(GNAME(do_pending_interrupt))
389 \f
390         .globl  GNAME(post_signal_tramp)
391         TYPE(GNAME(post_signal_tramp))
392         .align  align_16byte,0x90
393 GNAME(post_signal_tramp):
394         /* this is notionally the second half of a function whose first half
395          * doesn't exist.  This is where call_into_lisp returns when called 
396          * using return_to_lisp_function */
397         popq %r15
398         popq %r14
399         popq %r13
400         popq %r12
401         popq %r11
402         popq %r10
403         popq %r9
404         popq %r8
405         popq %rdi
406         popq %rsi
407         /* skip RBP and RSP */
408         popq %rbx
409         popq %rdx
410         popq %rcx
411         popq %rax
412         popfq
413         leave
414         ret
415         SIZE(GNAME(post_signal_tramp))
416 \f
417         .text
418         .align  align_16byte,0x90
419         .globl  GNAME(fast_bzero)
420         TYPE(GNAME(fast_bzero))
421         
422 GNAME(fast_bzero):
423         /* A fast routine for zero-filling blocks of memory that are
424          * guaranteed to start and end at a 4096-byte aligned address.
425          */
426         shr $6, %rsi              /* Amount of 64-byte blocks to copy */
427         jz Lend                   /* If none, stop */
428         mov %rsi, %rcx            /* Save start address */
429         movups %xmm7, -16(%rsp)   /* Save XMM register */
430         xorps  %xmm7, %xmm7       /* Zero the XMM register */
431         jmp Lloop
432         .align align_16byte                 
433 Lloop:
434
435         /* Copy the 16 zeroes from xmm7 to memory, 4 times. MOVNTDQ is the
436          * non-caching double-quadword moving variant, i.e. the memory areas
437          * we're touching are not fetched into the L1 cache, since we're just
438          * going to overwrite the memory soon anyway.
439          */
440         movntdq %xmm7, 0(%rdi)
441         movntdq %xmm7, 16(%rdi)
442         movntdq %xmm7, 32(%rdi)
443         movntdq %xmm7, 48(%rdi)
444
445         add $64, %rdi  /* Advance pointer */
446         dec %rsi       /* Decrement 64-byte block count */
447         jnz Lloop
448         mfence         /* Ensure that the writes are globally visible, since
449                         * MOVNTDQ is weakly ordered */
450         movups -16(%rsp), %xmm7 /* Restore the XMM register */
451         prefetcht0 0(%rcx)      /* Prefetch the start of the block into cache,
452                                  * since it's likely to be used immediately. */
453 Lend:        
454         ret
455         SIZE(GNAME(fast_bzero))
456
457         END()