Merge branch 'BigIntegerParse'
[mono.git] / mono / mini / mini-amd64.h
1 #ifndef __MONO_MINI_AMD64_H__
2 #define __MONO_MINI_AMD64_H__
3
4 #include <mono/arch/amd64/amd64-codegen.h>
5 #include <mono/utils/mono-sigcontext.h>
6 #include <mono/utils/mono-context.h>
7 #include <glib.h>
8
9 #ifdef __native_client_codegen__
10 #define kNaClAlignmentAMD64 32
11 #define kNaClAlignmentMaskAMD64 (kNaClAlignmentAMD64 - 1)
12
13 /* TODO: use kamd64NaClLengthOfCallImm    */
14 /* temporarily using kNaClAlignmentAMD64 so padding in */
15 /* image-writer.c doesn't happen                       */
16 #define kNaClLengthOfCallImm kNaClAlignmentAMD64
17
18 int is_nacl_call_reg_sequence (guint8* code);
19 void amd64_nacl_clear_legacy_prefix_tag ();
20 void amd64_nacl_tag_legacy_prefix (guint8* code);
21 void amd64_nacl_tag_rex (guint8* code);
22 guint8* amd64_nacl_get_legacy_prefix_tag ();
23 guint8* amd64_nacl_get_rex_tag ();
24 void amd64_nacl_instruction_pre ();
25 void amd64_nacl_instruction_post (guint8 **start, guint8 **end);
26 void amd64_nacl_membase_handler (guint8** code, gint8 basereg, gint32 offset, gint8 dreg);
27 #endif
28
29 #ifdef HOST_WIN32
30 #include <windows.h>
31 /* use SIG* defines if possible */
32 #ifdef HAVE_SIGNAL_H
33 #include <signal.h>
34 #endif
35
36
37 /* sigcontext surrogate */
38 struct sigcontext {
39         guint64 eax;
40         guint64 ebx;
41         guint64 ecx;
42         guint64 edx;
43         guint64 ebp;
44         guint64 esp;
45     guint64 esi;
46         guint64 edi;
47         guint64 eip;
48 };
49
50 typedef void (* MonoW32ExceptionHandler) (int _dummy, EXCEPTION_POINTERS *info, void *context);
51 void win32_seh_init(void);
52 void win32_seh_cleanup(void);
53 void win32_seh_set_handler(int type, MonoW32ExceptionHandler handler);
54
55 #ifndef SIGFPE
56 #define SIGFPE 4
57 #endif
58
59 #ifndef SIGILL
60 #define SIGILL 8
61 #endif
62
63 #ifndef SIGSEGV
64 #define SIGSEGV 11
65 #endif
66
67 LONG CALLBACK seh_handler(EXCEPTION_POINTERS* ep);
68
69 #endif /* HOST_WIN32 */
70
71 #ifdef sun    // Solaris x86
72 #  undef SIGSEGV_ON_ALTSTACK
73 #  define MONO_ARCH_NOMAP32BIT
74
75 struct sigcontext {
76         unsigned short gs, __gsh;
77         unsigned short fs, __fsh;
78         unsigned short es, __esh;
79         unsigned short ds, __dsh;
80         unsigned long edi;
81         unsigned long esi;
82         unsigned long ebp;
83         unsigned long esp;
84         unsigned long ebx;
85         unsigned long edx;
86         unsigned long ecx;
87         unsigned long eax;
88         unsigned long trapno;
89         unsigned long err;
90         unsigned long eip;
91         unsigned short cs, __csh;
92         unsigned long eflags;
93         unsigned long esp_at_signal;
94         unsigned short ss, __ssh;
95         unsigned long fpstate[95];
96       unsigned long filler[5];
97 };
98 #endif  // sun, Solaris x86
99
100 #ifndef DISABLE_SIMD
101 #define MONO_ARCH_SIMD_INTRINSICS 1
102 #define MONO_ARCH_NEED_SIMD_BANK 1
103 #define MONO_ARCH_USE_SHARED_FP_SIMD_BANK 1
104 #endif
105
106
107
108 #if defined(__APPLE__)
109 #define MONO_ARCH_SIGNAL_STACK_SIZE MINSIGSTKSZ
110 #else
111 #define MONO_ARCH_SIGNAL_STACK_SIZE (16 * 1024)
112 #endif
113
114 #define MONO_ARCH_HAVE_RESTORE_STACK_SUPPORT 1
115
116 #define MONO_ARCH_CPU_SPEC amd64_desc
117
118 #define MONO_MAX_IREGS 16
119
120 #define MONO_MAX_FREGS AMD64_XMM_NREG
121
122 #define MONO_ARCH_FP_RETURN_REG AMD64_XMM0
123
124 /* xmm15 is reserved for use by some opcodes */
125 #define MONO_ARCH_CALLEE_FREGS 0x7fff
126 #define MONO_ARCH_CALLEE_SAVED_FREGS 0
127
128 #define MONO_MAX_XREGS MONO_MAX_FREGS
129
130 #define MONO_ARCH_CALLEE_XREGS 0x7fff
131 #define MONO_ARCH_CALLEE_SAVED_XREGS 0
132
133
134 #define MONO_ARCH_CALLEE_REGS AMD64_CALLEE_REGS
135 #define MONO_ARCH_CALLEE_SAVED_REGS AMD64_CALLEE_SAVED_REGS
136
137 #define MONO_ARCH_USE_FPSTACK FALSE
138 #define MONO_ARCH_FPSTACK_SIZE 0
139
140 #define MONO_ARCH_INST_FIXED_REG(desc) ((desc == '\0') ? -1 : ((desc == 'i' ? -1 : ((desc == 'a') ? AMD64_RAX : ((desc == 's') ? AMD64_RCX : ((desc == 'd') ? AMD64_RDX : -1))))))
141
142 /* RDX is clobbered by the opcode implementation before accessing sreg2 */
143 #define MONO_ARCH_INST_SREG2_MASK(ins) (((ins [MONO_INST_CLOB] == 'a') || (ins [MONO_INST_CLOB] == 'd')) ? (1 << AMD64_RDX) : 0)
144
145 #define MONO_ARCH_INST_IS_REGPAIR(desc) FALSE
146 #define MONO_ARCH_INST_REGPAIR_REG2(desc,hreg1) (-1)
147
148 #define MONO_ARCH_FRAME_ALIGNMENT 16
149
150 /* fixme: align to 16byte instead of 32byte (we align to 32byte to get 
151  * reproduceable results for benchmarks */
152 #define MONO_ARCH_CODE_ALIGNMENT 32
153
154 #define MONO_ARCH_RETREG1 X86_EAX
155 #define MONO_ARCH_RETREG2 X86_EDX
156
157 /*This is the max size of the locals area of a given frame. I think 1MB is a safe default for now*/
158 #define MONO_ARCH_MAX_FRAME_SIZE 0x100000
159
160 struct MonoLMF {
161         /* 
162          * If the lowest bit is set, then this LMF has the rip field set. Otherwise,
163          * the rip field is not set, and the rsp field points to the stack location where
164          * the caller ip is saved.
165          * If the second lowest bit is set, then this is a MonoLMFExt structure, and
166          * the other fields are not valid.
167          * If the third lowest bit is set, then this is a MonoLMFTramp structure.
168          */
169         gpointer    previous_lmf;
170 #ifdef HOST_WIN32
171         gpointer    lmf_addr;
172 #endif
173 #if defined(__default_codegen__) || defined(HOST_WIN32)
174         guint64     rip;
175 #elif defined(__native_client_codegen__)
176         /* On 64-bit compilers, default alignment is 8 for this field, */
177         /* this allows the structure to match for 32-bit compilers.    */
178         guint64     rip __attribute__ ((aligned(8)));
179 #endif
180         guint64     rbx;
181         guint64     rbp;
182         guint64     rsp;
183         guint64     r12;
184         guint64     r13;
185         guint64     r14;
186         guint64     r15;
187 #ifdef HOST_WIN32
188         guint64     rdi;
189         guint64     rsi;
190 #endif
191 };
192
193 /* LMF structure used by the JIT trampolines */
194 typedef struct {
195         struct MonoLMF lmf;
196         guint64 *regs;
197         gpointer lmf_addr;
198 } MonoLMFTramp;
199
200 typedef struct MonoCompileArch {
201         gint32 localloc_offset;
202         gint32 reg_save_area_offset;
203         gint32 stack_alloc_size;
204         gint32 sp_fp_offset;
205         gboolean omit_fp, omit_fp_computed, no_pushes;
206         gpointer cinfo;
207         gint32 async_point_count;
208         gpointer vret_addr_loc;
209 #ifdef HOST_WIN32
210         gpointer        unwindinfo;
211 #endif
212         gpointer seq_point_info_var;
213         gpointer ss_trigger_page_var;
214         gpointer lmf_var;
215 } MonoCompileArch;
216
217 #define MONO_CONTEXT_SET_LLVM_EXC_REG(ctx, exc) do { (ctx)->rax = (gsize)exc; } while (0)
218
219 #define MONO_ARCH_INIT_TOP_LMF_ENTRY(lmf)
220
221 #ifdef _MSC_VER
222
223 #define MONO_INIT_CONTEXT_FROM_FUNC(ctx, start_func) do { \
224     guint64 stackptr; \
225         mono_arch_flush_register_windows (); \
226         stackptr = ((guint64)_AddressOfReturnAddress () - sizeof (void*));\
227         MONO_CONTEXT_SET_IP ((ctx), (start_func)); \
228         MONO_CONTEXT_SET_BP ((ctx), stackptr); \
229         MONO_CONTEXT_SET_SP ((ctx), stackptr); \
230 } while (0)
231
232 #else
233
234 /* 
235  * __builtin_frame_address () is broken on some older gcc versions in the presence of
236  * frame pointer elimination, see bug #82095.
237  */
238 #define MONO_INIT_CONTEXT_FROM_FUNC(ctx,start_func) do {        \
239         int tmp; \
240         guint64 stackptr = (guint64)&tmp; \
241                 mono_arch_flush_register_windows ();    \
242                 MONO_CONTEXT_SET_IP ((ctx), (start_func));      \
243                 MONO_CONTEXT_SET_BP ((ctx), stackptr);  \
244                 MONO_CONTEXT_SET_SP ((ctx), stackptr);  \
245         } while (0)
246
247 #endif
248
249 /*
250  * some icalls like mono_array_new_va needs to be called using a different 
251  * calling convention.
252  */
253 #define MONO_ARCH_VARARG_ICALLS 1
254
255 #if !defined( HOST_WIN32 ) && !defined(__native_client__) && !defined(__native_client_codegen__)
256
257 #define MONO_ARCH_USE_SIGACTION 1
258
259 #ifdef HAVE_WORKING_SIGALTSTACK
260
261 #define MONO_ARCH_SIGSEGV_ON_ALTSTACK
262
263 #endif
264
265 #endif /* !HOST_WIN32 && !__native_client__ */
266
267 #if defined (__APPLE__)
268
269 #define MONO_ARCH_NOMAP32BIT
270
271 #elif defined (__NetBSD__)
272
273 #define REG_RAX 14
274 #define REG_RCX 3
275 #define REG_RDX 2
276 #define REG_RBX 13
277 #define REG_RSP 24
278 #define REG_RBP 12
279 #define REG_RSI 1
280 #define REG_RDI 0
281 #define REG_R8 4
282 #define REG_R9 5
283 #define REG_R10 6
284 #define REG_R11 7
285 #define REG_R12 8
286 #define REG_R13 9
287 #define REG_R14 10
288 #define REG_R15 11
289 #define REG_RIP 21
290
291 #define MONO_ARCH_NOMAP32BIT
292
293 #elif defined (__OpenBSD__)
294
295 #define MONO_ARCH_NOMAP32BIT
296
297 #elif defined (__DragonFly__)
298
299 #define MONO_ARCH_NOMAP32BIT
300
301 #elif defined (__FreeBSD__)
302
303 #define REG_RAX 7
304 #define REG_RCX 4
305 #define REG_RDX 3
306 #define REG_RBX 8
307 #define REG_RSP 23
308 #define REG_RBP 9
309 #define REG_RSI 2
310 #define REG_RDI 1
311 #define REG_R8  5
312 #define REG_R9  6
313 #define REG_R10 10
314 #define REG_R11 11
315 #define REG_R12 12
316 #define REG_R13 13
317 #define REG_R14 14
318 #define REG_R15 15
319 #define REG_RIP 20
320
321 /* 
322  * FreeBSD does not have MAP_32BIT, so code allocated by the code manager might not have a
323  * 32 bit address.
324  */
325 #define MONO_ARCH_NOMAP32BIT
326
327 #endif /* __FreeBSD__ */
328
329 #ifdef HOST_WIN32
330 #define MONO_AMD64_ARG_REG1 AMD64_RCX
331 #define MONO_AMD64_ARG_REG2 AMD64_RDX
332 #else
333 #define MONO_AMD64_ARG_REG1 AMD64_RDI
334 #define MONO_AMD64_ARG_REG2 AMD64_RSI
335 #endif
336
337 #define MONO_ARCH_NO_EMULATE_LONG_SHIFT_OPS
338 #define MONO_ARCH_NO_EMULATE_LONG_MUL_OPTS
339
340 #define MONO_ARCH_EMULATE_CONV_R8_UN    1
341 #define MONO_ARCH_EMULATE_FREM 1
342 #define MONO_ARCH_HAVE_IS_INT_OVERFLOW 1
343
344 #define MONO_ARCH_ENABLE_REGALLOC_IN_EH_BLOCKS 1
345 #define MONO_ARCH_ENABLE_MONO_LMF_VAR 1
346 #define MONO_ARCH_HAVE_INVALIDATE_METHOD 1
347 #define MONO_ARCH_HAVE_CREATE_DELEGATE_TRAMPOLINE 1
348 #define MONO_ARCH_HAVE_ATOMIC_ADD 1
349 #define MONO_ARCH_HAVE_ATOMIC_EXCHANGE 1
350 #define MONO_ARCH_HAVE_ATOMIC_CAS 1
351 #define MONO_ARCH_HAVE_FULL_AOT_TRAMPOLINES 1
352 #define MONO_ARCH_HAVE_IMT 1
353 #define MONO_ARCH_HAVE_TLS_GET (mono_amd64_have_tls_get ())
354 #define MONO_ARCH_IMT_REG AMD64_R10
355 #define MONO_ARCH_IMT_SCRATCH_REG AMD64_R11
356 #define MONO_ARCH_VTABLE_REG MONO_AMD64_ARG_REG1
357 /*
358  * We use r10 for the imt/rgctx register rather than r11 because r11 is
359  * used by the trampoline as a scratch register and hence might be
360  * clobbered across method call boundaries.
361  */
362 #define MONO_ARCH_RGCTX_REG MONO_ARCH_IMT_REG
363 #define MONO_ARCH_HAVE_CMOV_OPS 1
364 #define MONO_ARCH_HAVE_NOTIFY_PENDING_EXC 1
365 #define MONO_ARCH_HAVE_EXCEPTIONS_INIT 1
366 #define MONO_ARCH_ENABLE_GLOBAL_RA 1
367 #define MONO_ARCH_HAVE_GENERALIZED_IMT_THUNK 1
368 #define MONO_ARCH_HAVE_LIVERANGE_OPS 1
369 #define MONO_ARCH_HAVE_XP_UNWIND 1
370 #define MONO_ARCH_HAVE_SIGCTX_TO_MONOCTX 1
371 #if !defined(HOST_WIN32)
372 #define MONO_ARCH_MONITOR_OBJECT_REG MONO_AMD64_ARG_REG1
373 #endif
374 #define MONO_ARCH_HAVE_GET_TRAMPOLINES 1
375
376 #define MONO_ARCH_AOT_SUPPORTED 1
377 #if !defined( HOST_WIN32 ) && !defined( __native_client__ )
378 #define MONO_ARCH_SOFT_DEBUG_SUPPORTED 1
379 #endif
380
381 #if !defined(HOST_WIN32) || defined(__sun)
382 #define MONO_ARCH_ENABLE_MONITOR_IL_FASTPATH 1
383 #endif
384
385 #define MONO_ARCH_SUPPORT_TASKLETS 1
386
387 #ifndef HOST_WIN32
388 #define MONO_AMD64_NO_PUSHES 1
389 #endif
390
391 #define MONO_ARCH_GSHARED_SUPPORTED 1
392 #define MONO_ARCH_DYN_CALL_SUPPORTED 1
393 #define MONO_ARCH_DYN_CALL_PARAM_AREA 0
394
395 #define MONO_ARCH_HAVE_LLVM_IMT_TRAMPOLINE 1
396 #define MONO_ARCH_LLVM_SUPPORTED 1
397 #define MONO_ARCH_THIS_AS_FIRST_ARG 1
398 #define MONO_ARCH_HAVE_HANDLER_BLOCK_GUARD 1
399 #define MONO_ARCH_HAVE_CARD_TABLE_WBARRIER 1
400 #define MONO_ARCH_HAVE_SETUP_RESUME_FROM_SIGNAL_HANDLER_CTX 1
401 #define MONO_ARCH_GC_MAPS_SUPPORTED 1
402 #define MONO_ARCH_HAVE_CONTEXT_SET_INT_REG 1
403 #define MONO_ARCH_HAVE_SETUP_ASYNC_CALLBACK 1
404 #define MONO_ARCH_HAVE_CREATE_LLVM_NATIVE_THUNK 1
405 #define MONO_ARCH_HAVE_OP_TAIL_CALL 1
406 #define MONO_ARCH_HAVE_TRANSLATE_TLS_OFFSET 1
407
408 #if defined(TARGET_OSX) || defined(__linux__)
409 #define MONO_ARCH_HAVE_TLS_GET_REG 1
410 #endif
411
412 /* Used for optimization, not complete */
413 #define MONO_ARCH_IS_OP_MEMBASE(opcode) ((opcode) == OP_X86_PUSH_MEMBASE)
414
415 #define MONO_ARCH_EMIT_BOUNDS_CHECK(cfg, array_reg, offset, index_reg) do { \
416             MonoInst *inst; \
417             MONO_INST_NEW ((cfg), inst, OP_AMD64_ICOMPARE_MEMBASE_REG); \
418             inst->inst_basereg = array_reg; \
419             inst->inst_offset = offset; \
420             inst->sreg2 = index_reg; \
421             MONO_ADD_INS ((cfg)->cbb, inst); \
422             MONO_EMIT_NEW_COND_EXC (cfg, LE_UN, "IndexOutOfRangeException"); \
423        } while (0)
424
425 void 
426 mono_amd64_patch (unsigned char* code, gpointer target) MONO_INTERNAL;
427
428 void
429 mono_amd64_throw_exception (guint64 dummy1, guint64 dummy2, guint64 dummy3, guint64 dummy4,
430                                                         guint64 dummy5, guint64 dummy6,
431                                                         mgreg_t *regs, mgreg_t rip,
432                                                         MonoObject *exc, gboolean rethrow) MONO_INTERNAL;
433
434 void
435 mono_amd64_throw_corlib_exception (guint64 dummy1, guint64 dummy2, guint64 dummy3, guint64 dummy4,
436                                                                    guint64 dummy5, guint64 dummy6,
437                                                                    mgreg_t *regs, mgreg_t rip,
438                                                                    guint32 ex_token_index, gint64 pc_offset) MONO_INTERNAL;
439
440 guint64
441 mono_amd64_get_original_ip (void) MONO_INTERNAL;
442
443 guint8*
444 mono_amd64_emit_tls_get (guint8* code, int dreg, int tls_offset) MONO_INTERNAL;
445
446 gboolean
447 mono_amd64_have_tls_get (void) MONO_INTERNAL;
448
449 GSList*
450 mono_amd64_get_exception_trampolines (gboolean aot) MONO_INTERNAL;
451
452 int
453 mono_amd64_get_tls_gs_offset (void) MONO_INTERNAL;
454
455 typedef struct {
456         guint8 *address;
457         guint8 saved_byte;
458 } MonoBreakpointInfo;
459
460 extern MonoBreakpointInfo mono_breakpoint_info [MONO_BREAKPOINT_ARRAY_SIZE];
461
462 #ifdef HOST_WIN32
463
464 void mono_arch_unwindinfo_add_push_nonvol (gpointer* monoui, gpointer codebegin, gpointer nextip, guchar reg );
465 void mono_arch_unwindinfo_add_set_fpreg (gpointer* monoui, gpointer codebegin, gpointer nextip, guchar reg );
466 void mono_arch_unwindinfo_add_alloc_stack (gpointer* monoui, gpointer codebegin, gpointer nextip, guint size );
467 guint mono_arch_unwindinfo_get_size (gpointer monoui);
468 void mono_arch_unwindinfo_install_unwind_info (gpointer* monoui, gpointer code, guint code_size);
469
470 #define MONO_ARCH_HAVE_UNWIND_TABLE 1
471 #endif
472
473 #endif /* __MONO_MINI_AMD64_H__ */  
474