Fix unwinding of C -> Haskell FFI calls with -threaded
[ghc.git] / includes / rts / Constants.h
1 /* ----------------------------------------------------------------------------
2 *
3 * (c) The GHC Team, 1998-2009
4 *
5 * Constants
6 *
7 * NOTE: this information is used by both the compiler and the RTS.
8 * Some of it is tweakable, and some of it must be kept up to date
9 * with various other parts of the system.
10 *
11 * Constants which are derived automatically from other definitions in
12 * the system (eg. structure sizes) are generated into the file
13 * DerivedConstants.h by a C program (mkDerivedConstantsHdr).
14 *
15 * To understand the structure of the RTS headers, see the wiki:
16 * http://ghc.haskell.org/trac/ghc/wiki/Commentary/SourceTree/Includes
17 *
18 * -------------------------------------------------------------------------- */
19
20 #pragma once
21
22 /* -----------------------------------------------------------------------------
23 Minimum closure sizes
24
25 This is the minimum number of words in the payload of a
26 heap-allocated closure, so that the closure has enough room to be
27 overwritten with a forwarding pointer during garbage collection.
28 -------------------------------------------------------------------------- */
29
30 #define MIN_PAYLOAD_SIZE 1
31
32 /* -----------------------------------------------------------------------------
33 Constants to do with specialised closure types.
34 -------------------------------------------------------------------------- */
35
36 /* We have some pre-compiled selector thunks defined in rts/StgStdThunks.hc.
37 * This constant defines the highest selectee index that we can replace with a
38 * reference to the pre-compiled code.
39 */
40
41 #define MAX_SPEC_SELECTEE_SIZE 15
42
43 /* Vector-apply thunks. These thunks just push their free variables
44 * on the stack and enter the first one. They're a bit like PAPs, but
45 * don't have a dynamic size. We've pre-compiled a few to save
46 * space.
47 */
48
49 #define MAX_SPEC_AP_SIZE 7
50
51 /* Specialised FUN/THUNK/CONSTR closure types */
52
53 #define MAX_SPEC_THUNK_SIZE 2
54 #define MAX_SPEC_FUN_SIZE 2
55 #define MAX_SPEC_CONSTR_SIZE 2
56
57 /* Range of built-in table of static small int-like and char-like closures.
58 *
59 * NB. This corresponds with the number of actual INTLIKE/CHARLIKE
60 * closures defined in rts/StgMiscClosures.cmm.
61 */
62 #define MAX_INTLIKE 16
63 #define MIN_INTLIKE (-16)
64
65 #define MAX_CHARLIKE 255
66 #define MIN_CHARLIKE 0
67
68 /* Each byte in the card table for an StgMutaArrPtrs covers
69 * (1<<MUT_ARR_PTRS_CARD_BITS) elements in the array. To find a good
70 * value for this, I used the benchmarks nofib/gc/hash,
71 * nofib/gc/graph, and nofib/gc/gc_bench.
72 */
73 #define MUT_ARR_PTRS_CARD_BITS 7
74
75 /* -----------------------------------------------------------------------------
76 STG Registers.
77
78 Note that in MachRegs.h we define how many of these registers are
79 *real* machine registers, and not just offsets in the Register Table.
80 -------------------------------------------------------------------------- */
81
82 #define MAX_VANILLA_REG 10
83 #define MAX_FLOAT_REG 6
84 #define MAX_DOUBLE_REG 6
85 #define MAX_LONG_REG 1
86 #define MAX_XMM_REG 6
87
88 /* -----------------------------------------------------------------------------
89 Semi-Tagging constants
90
91 Old Comments about this stuff:
92
93 Tags for indirection nodes and ``other'' (probably unevaluated) nodes;
94 normal-form values of algebraic data types will have tags 0, 1, ...
95
96 @INFO_IND_TAG@ is different from @INFO_OTHER_TAG@ just so we can count
97 how often we bang into indirection nodes; that's all. (WDP 95/11)
98
99 ToDo: find out if we need any of this.
100 -------------------------------------------------------------------------- */
101
102 #define INFO_OTHER_TAG (-1)
103 #define INFO_IND_TAG (-2)
104 #define INFO_FIRST_TAG 0
105
106 /* -----------------------------------------------------------------------------
107 How much C stack to reserve for local temporaries when in the STG
108 world. Used in StgCRun.c.
109 -------------------------------------------------------------------------- */
110
111 #define RESERVED_C_STACK_BYTES (2048 * SIZEOF_LONG)
112
113 /* -----------------------------------------------------------------------------
114 How large is the stack frame saved by StgRun?
115 world. Used in StgCRun.c.
116
117 The size has to be enough to save the registers (see StgCRun)
118 plus padding if the result is not 16 byte aligned.
119 See the Note [Stack Alignment on X86] in StgCRun.c for details.
120
121 -------------------------------------------------------------------------- */
122 #if defined(x86_64_HOST_ARCH)
123 # if defined(mingw32_HOST_OS)
124 # define STG_RUN_STACK_FRAME_SIZE 144
125 # else
126 # define STG_RUN_STACK_FRAME_SIZE 48
127 # endif
128 #endif
129
130 /* -----------------------------------------------------------------------------
131 StgRun related labels shared between StgCRun.c and StgStartup.cmm.
132 -------------------------------------------------------------------------- */
133
134 #if defined(LEADING_UNDERSCORE)
135 #define STG_RUN "_StgRun"
136 #define STG_RUN_JMP _StgRunJmp
137 #define STG_RETURN "_StgReturn"
138 #else
139 #define STG_RUN "StgRun"
140 #define STG_RUN_JMP StgRunJmp
141 #define STG_RETURN "StgReturn"
142 #endif
143
144 /* -----------------------------------------------------------------------------
145 How much Haskell stack space to reserve for the saving of registers
146 etc. in the case of a stack/heap overflow.
147
148 This must be large enough to accommodate the largest stack frame
149 pushed in one of the heap check fragments in HeapStackCheck.hc
150 (ie. currently the generic heap checks - 3 words for StgRetDyn,
151 18 words for the saved registers, see StgMacros.h).
152 -------------------------------------------------------------------------- */
153
154 #define RESERVED_STACK_WORDS 21
155
156 /* -----------------------------------------------------------------------------
157 The limit on the size of the stack check performed when we enter an
158 AP_STACK, in words. See raiseAsync() and bug #1466.
159 -------------------------------------------------------------------------- */
160
161 #define AP_STACK_SPLIM 1024
162
163 /* -----------------------------------------------------------------------------
164 Storage manager constants
165 -------------------------------------------------------------------------- */
166
167 /* The size of a block (2^BLOCK_SHIFT bytes) */
168 #define BLOCK_SHIFT 12
169
170 /* The size of a megablock (2^MBLOCK_SHIFT bytes) */
171 #define MBLOCK_SHIFT 20
172
173 /* -----------------------------------------------------------------------------
174 Bitmap/size fields (used in info tables)
175 -------------------------------------------------------------------------- */
176
177 /* In a 32-bit bitmap field, we use 5 bits for the size, and 27 bits
178 * for the bitmap. If the bitmap requires more than 27 bits, then we
179 * store it in a separate array, and leave a pointer in the bitmap
180 * field. On a 64-bit machine, the sizes are extended accordingly.
181 */
182 #if SIZEOF_VOID_P == 4
183 #define BITMAP_SIZE_MASK 0x1f
184 #define BITMAP_BITS_SHIFT 5
185 #elif SIZEOF_VOID_P == 8
186 #define BITMAP_SIZE_MASK 0x3f
187 #define BITMAP_BITS_SHIFT 6
188 #else
189 #error unknown SIZEOF_VOID_P
190 #endif
191
192 /* -----------------------------------------------------------------------------
193 Lag/Drag/Void constants
194 -------------------------------------------------------------------------- */
195
196 /*
197 An LDV word is divided into 3 parts: state bits (LDV_STATE_MASK), creation
198 time bits (LDV_CREATE_MASK), and last use time bits (LDV_LAST_MASK).
199 */
200 #if SIZEOF_VOID_P == 8
201 #define LDV_SHIFT 30
202 #define LDV_STATE_MASK 0x1000000000000000
203 #define LDV_CREATE_MASK 0x0FFFFFFFC0000000
204 #define LDV_LAST_MASK 0x000000003FFFFFFF
205 #define LDV_STATE_CREATE 0x0000000000000000
206 #define LDV_STATE_USE 0x1000000000000000
207 #else
208 #define LDV_SHIFT 15
209 #define LDV_STATE_MASK 0x40000000
210 #define LDV_CREATE_MASK 0x3FFF8000
211 #define LDV_LAST_MASK 0x00007FFF
212 #define LDV_STATE_CREATE 0x00000000
213 #define LDV_STATE_USE 0x40000000
214 #endif /* SIZEOF_VOID_P */
215
216 /* -----------------------------------------------------------------------------
217 TSO related constants
218 -------------------------------------------------------------------------- */
219
220 /*
221 * Constants for the what_next field of a TSO, which indicates how it
222 * is to be run.
223 */
224 #define ThreadRunGHC 1 /* return to address on top of stack */
225 #define ThreadInterpret 2 /* interpret this thread */
226 #define ThreadKilled 3 /* thread has died, don't run it */
227 #define ThreadComplete 4 /* thread has finished */
228
229 /*
230 * Constants for the why_blocked field of a TSO
231 * NB. keep these in sync with GHC/Conc/Sync.hs: threadStatus
232 */
233 #define NotBlocked 0
234 #define BlockedOnMVar 1
235 #define BlockedOnMVarRead 14 /* TODO: renumber me, see #9003 */
236 #define BlockedOnBlackHole 2
237 #define BlockedOnRead 3
238 #define BlockedOnWrite 4
239 #define BlockedOnDelay 5
240 #define BlockedOnSTM 6
241
242 /* Win32 only: */
243 #define BlockedOnDoProc 7
244
245 /* Only relevant for THREADED_RTS: */
246 #define BlockedOnCCall 10
247 #define BlockedOnCCall_Interruptible 11
248 /* same as above but permit killing the worker thread */
249
250 /* Involved in a message sent to tso->msg_cap */
251 #define BlockedOnMsgThrowTo 12
252
253 /* The thread is not on any run queues, but can be woken up
254 by tryWakeupThread() */
255 #define ThreadMigrating 13
256
257 /* WARNING WARNING top number is BlockedOnMVarRead 14, not 13!! */
258
259 /*
260 * These constants are returned to the scheduler by a thread that has
261 * stopped for one reason or another. See typedef StgThreadReturnCode
262 * in TSO.h.
263 */
264 #define HeapOverflow 1 /* might also be StackOverflow */
265 #define StackOverflow 2
266 #define ThreadYielding 3
267 #define ThreadBlocked 4
268 #define ThreadFinished 5
269
270 /*
271 * Flags for the tso->flags field.
272 */
273
274 /*
275 * TSO_LOCKED is set when a TSO is locked to a particular Capability.
276 */
277 #define TSO_LOCKED 2
278
279 /*
280 * TSO_BLOCKEX: the TSO is blocking exceptions
281 *
282 * TSO_INTERRUPTIBLE: the TSO can be interrupted if it blocks
283 * interruptibly (eg. with BlockedOnMVar).
284 *
285 * TSO_STOPPED_ON_BREAKPOINT: the thread is currently stopped in a breakpoint
286 */
287 #define TSO_BLOCKEX 4
288 #define TSO_INTERRUPTIBLE 8
289 #define TSO_STOPPED_ON_BREAKPOINT 16
290
291 /*
292 * Used by the sanity checker to check whether TSOs are on the correct
293 * mutable list.
294 */
295 #define TSO_MARKED 64
296
297 /*
298 * Used to communicate between stackSqueeze() and
299 * threadStackOverflow() that a thread's stack was squeezed and the
300 * stack may not need to be expanded.
301 */
302 #define TSO_SQUEEZED 128
303
304 /*
305 * Enables the AllocationLimitExceeded exception when the thread's
306 * allocation limit goes negative.
307 */
308 #define TSO_ALLOC_LIMIT 256
309
310 /*
311 * The number of times we spin in a spin lock before yielding (see
312 * #3758). To tune this value, use the benchmark in #3758: run the
313 * server with -N2 and the client both on a dual-core. Also make sure
314 * that the chosen value doesn't slow down any of the parallel
315 * benchmarks in nofib/parallel.
316 */
317 #define SPIN_COUNT 1000
318
319 /* -----------------------------------------------------------------------------
320 Spare workers per Capability in the threaded RTS
321
322 No more than MAX_SPARE_WORKERS will be kept in the thread pool
323 associated with each Capability.
324 -------------------------------------------------------------------------- */
325
326 #define MAX_SPARE_WORKERS 6
327
328 /*
329 * The maximum number of NUMA nodes we support. This is a fixed limit so that
330 * we can have static arrays of this size in the RTS for speed.
331 */
332 #define MAX_NUMA_NODES 16