[sgen] MONO_GC_DEBUG option for checking mark bit consistency.
[mono.git] / mono / metadata / sgen-gc.c
1 /*
2  * sgen-gc.c: Simple generational GC.
3  *
4  * Author:
5  *      Paolo Molaro (lupus@ximian.com)
6  *  Rodrigo Kumpera (kumpera@gmail.com)
7  *
8  * Copyright 2005-2011 Novell, Inc (http://www.novell.com)
9  * Copyright 2011 Xamarin Inc (http://www.xamarin.com)
10  *
11  * Thread start/stop adapted from Boehm's GC:
12  * Copyright (c) 1994 by Xerox Corporation.  All rights reserved.
13  * Copyright (c) 1996 by Silicon Graphics.  All rights reserved.
14  * Copyright (c) 1998 by Fergus Henderson.  All rights reserved.
15  * Copyright (c) 2000-2004 by Hewlett-Packard Company.  All rights reserved.
16  * Copyright 2001-2003 Ximian, Inc
17  * Copyright 2003-2010 Novell, Inc.
18  * Copyright 2011 Xamarin, Inc.
19  * Copyright (C) 2012 Xamarin Inc
20  *
21  * This library is free software; you can redistribute it and/or
22  * modify it under the terms of the GNU Library General Public
23  * License 2.0 as published by the Free Software Foundation;
24  *
25  * This library is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
28  * Library General Public License for more details.
29  *
30  * You should have received a copy of the GNU Library General Public
31  * License 2.0 along with this library; if not, write to the Free
32  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33  *
34  * Important: allocation provides always zeroed memory, having to do
35  * a memset after allocation is deadly for performance.
36  * Memory usage at startup is currently as follows:
37  * 64 KB pinned space
38  * 64 KB internal space
39  * size of nursery
40  * We should provide a small memory config with half the sizes
41  *
42  * We currently try to make as few mono assumptions as possible:
43  * 1) 2-word header with no GC pointers in it (first vtable, second to store the
44  *    forwarding ptr)
45  * 2) gc descriptor is the second word in the vtable (first word in the class)
46  * 3) 8 byte alignment is the minimum and enough (not true for special structures (SIMD), FIXME)
47  * 4) there is a function to get an object's size and the number of
48  *    elements in an array.
49  * 5) we know the special way bounds are allocated for complex arrays
50  * 6) we know about proxies and how to treat them when domains are unloaded
51  *
52  * Always try to keep stack usage to a minimum: no recursive behaviour
53  * and no large stack allocs.
54  *
55  * General description.
56  * Objects are initially allocated in a nursery using a fast bump-pointer technique.
57  * When the nursery is full we start a nursery collection: this is performed with a
58  * copying GC.
59  * When the old generation is full we start a copying GC of the old generation as well:
60  * this will be changed to mark&sweep with copying when fragmentation becomes to severe
61  * in the future.  Maybe we'll even do both during the same collection like IMMIX.
62  *
63  * The things that complicate this description are:
64  * *) pinned objects: we can't move them so we need to keep track of them
65  * *) no precise info of the thread stacks and registers: we need to be able to
66  *    quickly find the objects that may be referenced conservatively and pin them
67  *    (this makes the first issues more important)
68  * *) large objects are too expensive to be dealt with using copying GC: we handle them
69  *    with mark/sweep during major collections
70  * *) some objects need to not move even if they are small (interned strings, Type handles):
71  *    we use mark/sweep for them, too: they are not allocated in the nursery, but inside
72  *    PinnedChunks regions
73  */
74
75 /*
76  * TODO:
77
78  *) we could have a function pointer in MonoClass to implement
79   customized write barriers for value types
80
81  *) investigate the stuff needed to advance a thread to a GC-safe
82   point (single-stepping, read from unmapped memory etc) and implement it.
83   This would enable us to inline allocations and write barriers, for example,
84   or at least parts of them, like the write barrier checks.
85   We may need this also for handling precise info on stacks, even simple things
86   as having uninitialized data on the stack and having to wait for the prolog
87   to zero it. Not an issue for the last frame that we scan conservatively.
88   We could always not trust the value in the slots anyway.
89
90  *) modify the jit to save info about references in stack locations:
91   this can be done just for locals as a start, so that at least
92   part of the stack is handled precisely.
93
94  *) test/fix endianess issues
95
96  *) Implement a card table as the write barrier instead of remembered
97     sets?  Card tables are not easy to implement with our current
98     memory layout.  We have several different kinds of major heap
99     objects: Small objects in regular blocks, small objects in pinned
100     chunks and LOS objects.  If we just have a pointer we have no way
101     to tell which kind of object it points into, therefore we cannot
102     know where its card table is.  The least we have to do to make
103     this happen is to get rid of write barriers for indirect stores.
104     (See next item)
105
106  *) Get rid of write barriers for indirect stores.  We can do this by
107     telling the GC to wbarrier-register an object once we do an ldloca
108     or ldelema on it, and to unregister it once it's not used anymore
109     (it can only travel downwards on the stack).  The problem with
110     unregistering is that it needs to happen eventually no matter
111     what, even if exceptions are thrown, the thread aborts, etc.
112     Rodrigo suggested that we could do only the registering part and
113     let the collector find out (pessimistically) when it's safe to
114     unregister, namely when the stack pointer of the thread that
115     registered the object is higher than it was when the registering
116     happened.  This might make for a good first implementation to get
117     some data on performance.
118
119  *) Some sort of blacklist support?  Blacklists is a concept from the
120     Boehm GC: if during a conservative scan we find pointers to an
121     area which we might use as heap, we mark that area as unusable, so
122     pointer retention by random pinning pointers is reduced.
123
124  *) experiment with max small object size (very small right now - 2kb,
125     because it's tied to the max freelist size)
126
127   *) add an option to mmap the whole heap in one chunk: it makes for many
128      simplifications in the checks (put the nursery at the top and just use a single
129      check for inclusion/exclusion): the issue this has is that on 32 bit systems it's
130      not flexible (too much of the address space may be used by default or we can't
131      increase the heap as needed) and we'd need a race-free mechanism to return memory
132      back to the system (mprotect(PROT_NONE) will still keep the memory allocated if it
133      was written to, munmap is needed, but the following mmap may not find the same segment
134      free...)
135
136  *) memzero the major fragments after restarting the world and optionally a smaller
137     chunk at a time
138
139  *) investigate having fragment zeroing threads
140
141  *) separate locks for finalization and other minor stuff to reduce
142     lock contention
143
144  *) try a different copying order to improve memory locality
145
146  *) a thread abort after a store but before the write barrier will
147     prevent the write barrier from executing
148
149  *) specialized dynamically generated markers/copiers
150
151  *) Dynamically adjust TLAB size to the number of threads.  If we have
152     too many threads that do allocation, we might need smaller TLABs,
153     and we might get better performance with larger TLABs if we only
154     have a handful of threads.  We could sum up the space left in all
155     assigned TLABs and if that's more than some percentage of the
156     nursery size, reduce the TLAB size.
157
158  *) Explore placing unreachable objects on unused nursery memory.
159         Instead of memset'ng a region to zero, place an int[] covering it.
160         A good place to start is add_nursery_frag. The tricky thing here is
161         placing those objects atomically outside of a collection.
162
163  *) Allocation should use asymmetric Dekker synchronization:
164         http://blogs.oracle.com/dave/resource/Asymmetric-Dekker-Synchronization.txt
165         This should help weak consistency archs.
166  */
167 #include "config.h"
168 #ifdef HAVE_SGEN_GC
169
170 #ifdef __MACH__
171 #undef _XOPEN_SOURCE
172 #define _XOPEN_SOURCE
173 #define _DARWIN_C_SOURCE
174 #endif
175
176 #ifdef HAVE_UNISTD_H
177 #include <unistd.h>
178 #endif
179 #ifdef HAVE_PTHREAD_H
180 #include <pthread.h>
181 #endif
182 #ifdef HAVE_SEMAPHORE_H
183 #include <semaphore.h>
184 #endif
185 #include <stdio.h>
186 #include <string.h>
187 #include <signal.h>
188 #include <errno.h>
189 #include <assert.h>
190
191 #include "metadata/sgen-gc.h"
192 #include "metadata/metadata-internals.h"
193 #include "metadata/class-internals.h"
194 #include "metadata/gc-internal.h"
195 #include "metadata/object-internals.h"
196 #include "metadata/threads.h"
197 #include "metadata/sgen-cardtable.h"
198 #include "metadata/sgen-ssb.h"
199 #include "metadata/sgen-protocol.h"
200 #include "metadata/sgen-archdep.h"
201 #include "metadata/sgen-bridge.h"
202 #include "metadata/sgen-memory-governor.h"
203 #include "metadata/sgen-hash-table.h"
204 #include "metadata/mono-gc.h"
205 #include "metadata/method-builder.h"
206 #include "metadata/profiler-private.h"
207 #include "metadata/monitor.h"
208 #include "metadata/threadpool-internals.h"
209 #include "metadata/mempool-internals.h"
210 #include "metadata/marshal.h"
211 #include "metadata/runtime.h"
212 #include "metadata/sgen-cardtable.h"
213 #include "metadata/sgen-pinning.h"
214 #include "metadata/sgen-workers.h"
215 #include "utils/mono-mmap.h"
216 #include "utils/mono-time.h"
217 #include "utils/mono-semaphore.h"
218 #include "utils/mono-counters.h"
219 #include "utils/mono-proclib.h"
220 #include "utils/mono-memory-model.h"
221 #include "utils/mono-logger-internal.h"
222 #include "utils/dtrace.h"
223
224 #include <mono/utils/mono-logger-internal.h>
225 #include <mono/utils/memcheck.h>
226
227 #if defined(__MACH__)
228 #include "utils/mach-support.h"
229 #endif
230
231 #define OPDEF(a,b,c,d,e,f,g,h,i,j) \
232         a = i,
233
234 enum {
235 #include "mono/cil/opcode.def"
236         CEE_LAST
237 };
238
239 #undef OPDEF
240
241 #undef pthread_create
242 #undef pthread_join
243 #undef pthread_detach
244
245 /*
246  * ######################################################################
247  * ########  Types and constants used by the GC.
248  * ######################################################################
249  */
250
251 /* 0 means not initialized, 1 is initialized, -1 means in progress */
252 static int gc_initialized = 0;
253 /* If set, check if we need to do something every X allocations */
254 gboolean has_per_allocation_action;
255 /* If set, do a heap check every X allocation */
256 guint32 verify_before_allocs = 0;
257 /* If set, do a minor collection before every X allocation */
258 guint32 collect_before_allocs = 0;
259 /* If set, do a whole heap check before each collection */
260 static gboolean whole_heap_check_before_collection = FALSE;
261 /* If set, do a heap consistency check before each minor collection */
262 static gboolean consistency_check_at_minor_collection = FALSE;
263 /* If set, check whether mark bits are consistent after major collections */
264 static gboolean check_mark_bits_after_major_collection = FALSE;
265 /* If set, do a few checks when the concurrent collector is used */
266 static gboolean do_concurrent_checks = FALSE;
267 /* If set, check that there are no references to the domain left at domain unload */
268 static gboolean xdomain_checks = FALSE;
269 /* If not null, dump the heap after each collection into this file */
270 static FILE *heap_dump_file = NULL;
271 /* If set, mark stacks conservatively, even if precise marking is possible */
272 static gboolean conservative_stack_mark = FALSE;
273 /* If set, do a plausibility check on the scan_starts before and after
274    each collection */
275 static gboolean do_scan_starts_check = FALSE;
276 static gboolean nursery_collection_is_parallel = FALSE;
277 static gboolean disable_minor_collections = FALSE;
278 static gboolean disable_major_collections = FALSE;
279 gboolean do_pin_stats = FALSE;
280 static gboolean do_verify_nursery = FALSE;
281 static gboolean do_dump_nursery_content = FALSE;
282
283 #ifdef HEAVY_STATISTICS
284 long long stat_objects_alloced_degraded = 0;
285 long long stat_bytes_alloced_degraded = 0;
286
287 long long stat_copy_object_called_nursery = 0;
288 long long stat_objects_copied_nursery = 0;
289 long long stat_copy_object_called_major = 0;
290 long long stat_objects_copied_major = 0;
291
292 long long stat_scan_object_called_nursery = 0;
293 long long stat_scan_object_called_major = 0;
294
295 long long stat_slots_allocated_in_vain;
296
297 long long stat_nursery_copy_object_failed_from_space = 0;
298 long long stat_nursery_copy_object_failed_forwarded = 0;
299 long long stat_nursery_copy_object_failed_pinned = 0;
300 long long stat_nursery_copy_object_failed_to_space = 0;
301
302 static int stat_wbarrier_set_field = 0;
303 static int stat_wbarrier_set_arrayref = 0;
304 static int stat_wbarrier_arrayref_copy = 0;
305 static int stat_wbarrier_generic_store = 0;
306 static int stat_wbarrier_set_root = 0;
307 static int stat_wbarrier_value_copy = 0;
308 static int stat_wbarrier_object_copy = 0;
309 #endif
310
311 int stat_minor_gcs = 0;
312 int stat_major_gcs = 0;
313
314 static long long stat_pinned_objects = 0;
315
316 static long long time_minor_pre_collection_fragment_clear = 0;
317 static long long time_minor_pinning = 0;
318 static long long time_minor_scan_remsets = 0;
319 static long long time_minor_scan_pinned = 0;
320 static long long time_minor_scan_registered_roots = 0;
321 static long long time_minor_scan_thread_data = 0;
322 static long long time_minor_finish_gray_stack = 0;
323 static long long time_minor_fragment_creation = 0;
324
325 static long long time_major_pre_collection_fragment_clear = 0;
326 static long long time_major_pinning = 0;
327 static long long time_major_scan_pinned = 0;
328 static long long time_major_scan_registered_roots = 0;
329 static long long time_major_scan_thread_data = 0;
330 static long long time_major_scan_alloc_pinned = 0;
331 static long long time_major_scan_finalized = 0;
332 static long long time_major_scan_big_objects = 0;
333 static long long time_major_finish_gray_stack = 0;
334 static long long time_major_free_bigobjs = 0;
335 static long long time_major_los_sweep = 0;
336 static long long time_major_sweep = 0;
337 static long long time_major_fragment_creation = 0;
338
339 int gc_debug_level = 0;
340 FILE* gc_debug_file;
341
342 /*
343 void
344 mono_gc_flush_info (void)
345 {
346         fflush (gc_debug_file);
347 }
348 */
349
350 #define TV_DECLARE SGEN_TV_DECLARE
351 #define TV_GETTIME SGEN_TV_GETTIME
352 #define TV_ELAPSED SGEN_TV_ELAPSED
353 #define TV_ELAPSED_MS SGEN_TV_ELAPSED_MS
354
355 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
356
357 NurseryClearPolicy nursery_clear_policy = CLEAR_AT_TLAB_CREATION;
358
359 #define object_is_forwarded     SGEN_OBJECT_IS_FORWARDED
360 #define object_is_pinned        SGEN_OBJECT_IS_PINNED
361 #define pin_object              SGEN_PIN_OBJECT
362 #define unpin_object            SGEN_UNPIN_OBJECT
363
364 #define ptr_in_nursery sgen_ptr_in_nursery
365
366 #define LOAD_VTABLE     SGEN_LOAD_VTABLE
367
368 static const char*
369 safe_name (void* obj)
370 {
371         MonoVTable *vt = (MonoVTable*)LOAD_VTABLE (obj);
372         return vt->klass->name;
373 }
374
375 #define safe_object_get_size    sgen_safe_object_get_size
376
377 const char*
378 sgen_safe_name (void* obj)
379 {
380         return safe_name (obj);
381 }
382
383 /*
384  * ######################################################################
385  * ########  Global data.
386  * ######################################################################
387  */
388 LOCK_DECLARE (gc_mutex);
389 static int gc_disabled = 0;
390
391 static gboolean use_cardtable;
392
393 #define SCAN_START_SIZE SGEN_SCAN_START_SIZE
394
395 static mword pagesize = 4096;
396 int degraded_mode = 0;
397
398 static mword bytes_pinned_from_failed_allocation = 0;
399
400 GCMemSection *nursery_section = NULL;
401 static mword lowest_heap_address = ~(mword)0;
402 static mword highest_heap_address = 0;
403
404 LOCK_DECLARE (sgen_interruption_mutex);
405 static LOCK_DECLARE (pin_queue_mutex);
406
407 #define LOCK_PIN_QUEUE mono_mutex_lock (&pin_queue_mutex)
408 #define UNLOCK_PIN_QUEUE mono_mutex_unlock (&pin_queue_mutex)
409
410 typedef struct _FinalizeReadyEntry FinalizeReadyEntry;
411 struct _FinalizeReadyEntry {
412         FinalizeReadyEntry *next;
413         void *object;
414 };
415
416 typedef struct _EphemeronLinkNode EphemeronLinkNode;
417
418 struct _EphemeronLinkNode {
419         EphemeronLinkNode *next;
420         char *array;
421 };
422
423 typedef struct {
424        void *key;
425        void *value;
426 } Ephemeron;
427
428 int current_collection_generation = -1;
429 volatile gboolean concurrent_collection_in_progress = FALSE;
430
431 /* objects that are ready to be finalized */
432 static FinalizeReadyEntry *fin_ready_list = NULL;
433 static FinalizeReadyEntry *critical_fin_list = NULL;
434
435 static EphemeronLinkNode *ephemeron_list;
436
437 /* registered roots: the key to the hash is the root start address */
438 /* 
439  * Different kinds of roots are kept separate to speed up pin_from_roots () for example.
440  */
441 SgenHashTable roots_hash [ROOT_TYPE_NUM] = {
442         SGEN_HASH_TABLE_INIT (INTERNAL_MEM_ROOTS_TABLE, INTERNAL_MEM_ROOT_RECORD, sizeof (RootRecord), mono_aligned_addr_hash, NULL),
443         SGEN_HASH_TABLE_INIT (INTERNAL_MEM_ROOTS_TABLE, INTERNAL_MEM_ROOT_RECORD, sizeof (RootRecord), mono_aligned_addr_hash, NULL),
444         SGEN_HASH_TABLE_INIT (INTERNAL_MEM_ROOTS_TABLE, INTERNAL_MEM_ROOT_RECORD, sizeof (RootRecord), mono_aligned_addr_hash, NULL)
445 };
446 static mword roots_size = 0; /* amount of memory in the root set */
447
448 #define GC_ROOT_NUM 32
449 typedef struct {
450         int count;              /* must be the first field */
451         void *objects [GC_ROOT_NUM];
452         int root_types [GC_ROOT_NUM];
453         uintptr_t extra_info [GC_ROOT_NUM];
454 } GCRootReport;
455
456 static void
457 notify_gc_roots (GCRootReport *report)
458 {
459         if (!report->count)
460                 return;
461         mono_profiler_gc_roots (report->count, report->objects, report->root_types, report->extra_info);
462         report->count = 0;
463 }
464
465 static void
466 add_profile_gc_root (GCRootReport *report, void *object, int rtype, uintptr_t extra_info)
467 {
468         if (report->count == GC_ROOT_NUM)
469                 notify_gc_roots (report);
470         report->objects [report->count] = object;
471         report->root_types [report->count] = rtype;
472         report->extra_info [report->count++] = (uintptr_t)((MonoVTable*)LOAD_VTABLE (object))->klass;
473 }
474
475 MonoNativeTlsKey thread_info_key;
476
477 #ifdef HAVE_KW_THREAD
478 __thread SgenThreadInfo *sgen_thread_info;
479 __thread gpointer *store_remset_buffer;
480 __thread long store_remset_buffer_index;
481 __thread char *stack_end;
482 __thread long *store_remset_buffer_index_addr;
483 #endif
484
485 /* The size of a TLAB */
486 /* The bigger the value, the less often we have to go to the slow path to allocate a new 
487  * one, but the more space is wasted by threads not allocating much memory.
488  * FIXME: Tune this.
489  * FIXME: Make this self-tuning for each thread.
490  */
491 guint32 tlab_size = (1024 * 4);
492
493 #define MAX_SMALL_OBJ_SIZE      SGEN_MAX_SMALL_OBJ_SIZE
494
495 /* Functions supplied by the runtime to be called by the GC */
496 static MonoGCCallbacks gc_callbacks;
497
498 #define ALLOC_ALIGN             SGEN_ALLOC_ALIGN
499 #define ALLOC_ALIGN_BITS        SGEN_ALLOC_ALIGN_BITS
500
501 #define ALIGN_UP                SGEN_ALIGN_UP
502
503 #define MOVED_OBJECTS_NUM 64
504 static void *moved_objects [MOVED_OBJECTS_NUM];
505 static int moved_objects_idx = 0;
506
507 /* Vtable of the objects used to fill out nursery fragments before a collection */
508 static MonoVTable *array_fill_vtable;
509
510 #ifdef SGEN_DEBUG_INTERNAL_ALLOC
511 MonoNativeThreadId main_gc_thread = NULL;
512 #endif
513
514 /*Object was pinned during the current collection*/
515 static mword objects_pinned;
516
517 /*
518  * ######################################################################
519  * ########  Macros and function declarations.
520  * ######################################################################
521  */
522
523 inline static void*
524 align_pointer (void *ptr)
525 {
526         mword p = (mword)ptr;
527         p += sizeof (gpointer) - 1;
528         p &= ~ (sizeof (gpointer) - 1);
529         return (void*)p;
530 }
531
532 typedef SgenGrayQueue GrayQueue;
533
534 /* forward declarations */
535 static void scan_thread_data (void *start_nursery, void *end_nursery, gboolean precise, GrayQueue *queue);
536 static void scan_from_registered_roots (char *addr_start, char *addr_end, int root_type, ScanCopyContext ctx);
537 static void scan_finalizer_entries (FinalizeReadyEntry *list, ScanCopyContext ctx);
538 static void report_finalizer_roots (void);
539 static void report_registered_roots (void);
540
541 static void pin_from_roots (void *start_nursery, void *end_nursery, GrayQueue *queue);
542 static int pin_objects_from_addresses (GCMemSection *section, void **start, void **end, void *start_nursery, void *end_nursery, ScanCopyContext ctx);
543 static void finish_gray_stack (char *start_addr, char *end_addr, int generation, GrayQueue *queue);
544
545 void mono_gc_scan_for_specific_ref (MonoObject *key, gboolean precise);
546
547
548 static void init_stats (void);
549
550 static int mark_ephemerons_in_range (char *start, char *end, ScanCopyContext ctx);
551 static void clear_unreachable_ephemerons (char *start, char *end, ScanCopyContext ctx);
552 static void null_ephemerons_for_domain (MonoDomain *domain);
553
554 SgenObjectOperations current_object_ops;
555 SgenMajorCollector major_collector;
556 SgenMinorCollector sgen_minor_collector;
557 static GrayQueue gray_queue;
558 static GrayQueue remember_major_objects_gray_queue;
559
560 static SgenRemeberedSet remset;
561
562 /* The gray queue to use from the main collection thread. */
563 #define WORKERS_DISTRIBUTE_GRAY_QUEUE   (&gray_queue)
564
565 /*
566  * The gray queue a worker job must use.  If we're not parallel or
567  * concurrent, we use the main gray queue.
568  */
569 static SgenGrayQueue*
570 sgen_workers_get_job_gray_queue (WorkerData *worker_data)
571 {
572         return worker_data ? &worker_data->private_gray_queue : WORKERS_DISTRIBUTE_GRAY_QUEUE;
573 }
574
575 static gboolean have_non_collection_major_object_remembers = FALSE;
576
577 gboolean
578 sgen_remember_major_object_for_concurrent_mark (char *obj)
579 {
580         if (!major_collector.is_concurrent)
581                 return FALSE;
582
583         g_assert (current_collection_generation == GENERATION_NURSERY || current_collection_generation == -1);
584
585         if (!concurrent_collection_in_progress)
586                 return FALSE;
587
588         GRAY_OBJECT_ENQUEUE (&remember_major_objects_gray_queue, obj);
589
590         if (current_collection_generation != GENERATION_NURSERY) {
591                 /*
592                  * This happens when the mutator allocates large or
593                  * pinned objects or when allocating in degraded
594                  * mode.
595                  */
596                 have_non_collection_major_object_remembers = TRUE;
597         }
598
599         return TRUE;
600 }
601
602 static void
603 gray_queue_redirect (SgenGrayQueue *queue)
604 {
605         gboolean wake = FALSE;
606
607
608         for (;;) {
609                 GrayQueueSection *section = sgen_gray_object_dequeue_section (queue);
610                 if (!section)
611                         break;
612                 sgen_section_gray_queue_enqueue (queue->alloc_prepare_data, section);
613                 wake = TRUE;
614         }
615
616         if (wake) {
617                 g_assert (concurrent_collection_in_progress ||
618                                 (current_collection_generation == GENERATION_OLD && major_collector.is_parallel));
619                 if (sgen_workers_have_started ()) {
620                         sgen_workers_wake_up_all ();
621                 } else {
622                         if (concurrent_collection_in_progress)
623                                 g_assert (current_collection_generation == -1);
624                 }
625         }
626 }
627
628 static void
629 redirect_major_object_remembers (void)
630 {
631         gray_queue_redirect (&remember_major_objects_gray_queue);
632         have_non_collection_major_object_remembers = FALSE;
633 }
634
635 static gboolean
636 is_xdomain_ref_allowed (gpointer *ptr, char *obj, MonoDomain *domain)
637 {
638         MonoObject *o = (MonoObject*)(obj);
639         MonoObject *ref = (MonoObject*)*(ptr);
640         int offset = (char*)(ptr) - (char*)o;
641
642         if (o->vtable->klass == mono_defaults.thread_class && offset == G_STRUCT_OFFSET (MonoThread, internal_thread))
643                 return TRUE;
644         if (o->vtable->klass == mono_defaults.internal_thread_class && offset == G_STRUCT_OFFSET (MonoInternalThread, current_appcontext))
645                 return TRUE;
646         if (mono_class_has_parent_fast (o->vtable->klass, mono_defaults.real_proxy_class) &&
647                         offset == G_STRUCT_OFFSET (MonoRealProxy, unwrapped_server))
648                 return TRUE;
649         /* Thread.cached_culture_info */
650         if (!strcmp (ref->vtable->klass->name_space, "System.Globalization") &&
651                         !strcmp (ref->vtable->klass->name, "CultureInfo") &&
652                         !strcmp(o->vtable->klass->name_space, "System") &&
653                         !strcmp(o->vtable->klass->name, "Object[]"))
654                 return TRUE;
655         /*
656          *  at System.IO.MemoryStream.InternalConstructor (byte[],int,int,bool,bool) [0x0004d] in /home/schani/Work/novell/trunk/mcs/class/corlib/System.IO/MemoryStream.cs:121
657          * at System.IO.MemoryStream..ctor (byte[]) [0x00017] in /home/schani/Work/novell/trunk/mcs/class/corlib/System.IO/MemoryStream.cs:81
658          * at (wrapper remoting-invoke-with-check) System.IO.MemoryStream..ctor (byte[]) <IL 0x00020, 0xffffffff>
659          * at System.Runtime.Remoting.Messaging.CADMethodCallMessage.GetArguments () [0x0000d] in /home/schani/Work/novell/trunk/mcs/class/corlib/System.Runtime.Remoting.Messaging/CADMessages.cs:327
660          * at System.Runtime.Remoting.Messaging.MethodCall..ctor (System.Runtime.Remoting.Messaging.CADMethodCallMessage) [0x00017] in /home/schani/Work/novell/trunk/mcs/class/corlib/System.Runtime.Remoting.Messaging/MethodCall.cs:87
661          * at System.AppDomain.ProcessMessageInDomain (byte[],System.Runtime.Remoting.Messaging.CADMethodCallMessage,byte[]&,System.Runtime.Remoting.Messaging.CADMethodReturnMessage&) [0x00018] in /home/schani/Work/novell/trunk/mcs/class/corlib/System/AppDomain.cs:1213
662          * at (wrapper remoting-invoke-with-check) System.AppDomain.ProcessMessageInDomain (byte[],System.Runtime.Remoting.Messaging.CADMethodCallMessage,byte[]&,System.Runtime.Remoting.Messaging.CADMethodReturnMessage&) <IL 0x0003d, 0xffffffff>
663          * at System.Runtime.Remoting.Channels.CrossAppDomainSink.ProcessMessageInDomain (byte[],System.Runtime.Remoting.Messaging.CADMethodCallMessage) [0x00008] in /home/schani/Work/novell/trunk/mcs/class/corlib/System.Runtime.Remoting.Channels/CrossAppDomainChannel.cs:198
664          * at (wrapper runtime-invoke) object.runtime_invoke_CrossAppDomainSink/ProcessMessageRes_object_object (object,intptr,intptr,intptr) <IL 0x0004c, 0xffffffff>
665          */
666         if (!strcmp (ref->vtable->klass->name_space, "System") &&
667                         !strcmp (ref->vtable->klass->name, "Byte[]") &&
668                         !strcmp (o->vtable->klass->name_space, "System.IO") &&
669                         !strcmp (o->vtable->klass->name, "MemoryStream"))
670                 return TRUE;
671         /* append_job() in threadpool.c */
672         if (!strcmp (ref->vtable->klass->name_space, "System.Runtime.Remoting.Messaging") &&
673                         !strcmp (ref->vtable->klass->name, "AsyncResult") &&
674                         !strcmp (o->vtable->klass->name_space, "System") &&
675                         !strcmp (o->vtable->klass->name, "Object[]") &&
676                         mono_thread_pool_is_queue_array ((MonoArray*) o))
677                 return TRUE;
678         return FALSE;
679 }
680
681 static void
682 check_reference_for_xdomain (gpointer *ptr, char *obj, MonoDomain *domain)
683 {
684         MonoObject *o = (MonoObject*)(obj);
685         MonoObject *ref = (MonoObject*)*(ptr);
686         int offset = (char*)(ptr) - (char*)o;
687         MonoClass *class;
688         MonoClassField *field;
689         char *str;
690
691         if (!ref || ref->vtable->domain == domain)
692                 return;
693         if (is_xdomain_ref_allowed (ptr, obj, domain))
694                 return;
695
696         field = NULL;
697         for (class = o->vtable->klass; class; class = class->parent) {
698                 int i;
699
700                 for (i = 0; i < class->field.count; ++i) {
701                         if (class->fields[i].offset == offset) {
702                                 field = &class->fields[i];
703                                 break;
704                         }
705                 }
706                 if (field)
707                         break;
708         }
709
710         if (ref->vtable->klass == mono_defaults.string_class)
711                 str = mono_string_to_utf8 ((MonoString*)ref);
712         else
713                 str = NULL;
714         g_print ("xdomain reference in %p (%s.%s) at offset %d (%s) to %p (%s.%s) (%s)  -  pointed to by:\n",
715                         o, o->vtable->klass->name_space, o->vtable->klass->name,
716                         offset, field ? field->name : "",
717                         ref, ref->vtable->klass->name_space, ref->vtable->klass->name, str ? str : "");
718         mono_gc_scan_for_specific_ref (o, TRUE);
719         if (str)
720                 g_free (str);
721 }
722
723 #undef HANDLE_PTR
724 #define HANDLE_PTR(ptr,obj)     check_reference_for_xdomain ((ptr), (obj), domain)
725
726 static void
727 scan_object_for_xdomain_refs (char *start, mword size, void *data)
728 {
729         MonoDomain *domain = ((MonoObject*)start)->vtable->domain;
730
731         #include "sgen-scan-object.h"
732 }
733
734 static gboolean scan_object_for_specific_ref_precise = TRUE;
735
736 #undef HANDLE_PTR
737 #define HANDLE_PTR(ptr,obj) do {                \
738         if ((MonoObject*)*(ptr) == key) {       \
739         g_print ("found ref to %p in object %p (%s) at offset %td\n",   \
740                         key, (obj), safe_name ((obj)), ((char*)(ptr) - (char*)(obj))); \
741         }                                                               \
742         } while (0)
743
744 static void
745 scan_object_for_specific_ref (char *start, MonoObject *key)
746 {
747         char *forwarded;
748
749         if ((forwarded = SGEN_OBJECT_IS_FORWARDED (start)))
750                 start = forwarded;
751
752         if (scan_object_for_specific_ref_precise) {
753                 #include "sgen-scan-object.h"
754         } else {
755                 mword *words = (mword*)start;
756                 size_t size = safe_object_get_size ((MonoObject*)start);
757                 int i;
758                 for (i = 0; i < size / sizeof (mword); ++i) {
759                         if (words [i] == (mword)key) {
760                                 g_print ("found possible ref to %p in object %p (%s) at offset %td\n",
761                                                 key, start, safe_name (start), i * sizeof (mword));
762                         }
763                 }
764         }
765 }
766
767 void
768 sgen_scan_area_with_callback (char *start, char *end, IterateObjectCallbackFunc callback, void *data, gboolean allow_flags)
769 {
770         while (start < end) {
771                 size_t size;
772                 char *obj;
773
774                 if (!*(void**)start) {
775                         start += sizeof (void*); /* should be ALLOC_ALIGN, really */
776                         continue;
777                 }
778
779                 if (allow_flags) {
780                         if (!(obj = SGEN_OBJECT_IS_FORWARDED (start)))
781                                 obj = start;
782                 } else {
783                         obj = start;
784                 }
785
786                 size = ALIGN_UP (safe_object_get_size ((MonoObject*)obj));
787
788                 if ((MonoVTable*)SGEN_LOAD_VTABLE (obj) != array_fill_vtable)
789                         callback (obj, size, data);
790
791                 start += size;
792         }
793 }
794
795 static void
796 scan_object_for_specific_ref_callback (char *obj, size_t size, MonoObject *key)
797 {
798         scan_object_for_specific_ref (obj, key);
799 }
800
801 static void
802 check_root_obj_specific_ref (RootRecord *root, MonoObject *key, MonoObject *obj)
803 {
804         if (key != obj)
805                 return;
806         g_print ("found ref to %p in root record %p\n", key, root);
807 }
808
809 static MonoObject *check_key = NULL;
810 static RootRecord *check_root = NULL;
811
812 static void
813 check_root_obj_specific_ref_from_marker (void **obj)
814 {
815         check_root_obj_specific_ref (check_root, check_key, *obj);
816 }
817
818 static void
819 scan_roots_for_specific_ref (MonoObject *key, int root_type)
820 {
821         void **start_root;
822         RootRecord *root;
823         check_key = key;
824
825         SGEN_HASH_TABLE_FOREACH (&roots_hash [root_type], start_root, root) {
826                 mword desc = root->root_desc;
827
828                 check_root = root;
829
830                 switch (desc & ROOT_DESC_TYPE_MASK) {
831                 case ROOT_DESC_BITMAP:
832                         desc >>= ROOT_DESC_TYPE_SHIFT;
833                         while (desc) {
834                                 if (desc & 1)
835                                         check_root_obj_specific_ref (root, key, *start_root);
836                                 desc >>= 1;
837                                 start_root++;
838                         }
839                         return;
840                 case ROOT_DESC_COMPLEX: {
841                         gsize *bitmap_data = sgen_get_complex_descriptor_bitmap (desc);
842                         int bwords = (*bitmap_data) - 1;
843                         void **start_run = start_root;
844                         bitmap_data++;
845                         while (bwords-- > 0) {
846                                 gsize bmap = *bitmap_data++;
847                                 void **objptr = start_run;
848                                 while (bmap) {
849                                         if (bmap & 1)
850                                                 check_root_obj_specific_ref (root, key, *objptr);
851                                         bmap >>= 1;
852                                         ++objptr;
853                                 }
854                                 start_run += GC_BITS_PER_WORD;
855                         }
856                         break;
857                 }
858                 case ROOT_DESC_USER: {
859                         MonoGCRootMarkFunc marker = sgen_get_user_descriptor_func (desc);
860                         marker (start_root, check_root_obj_specific_ref_from_marker);
861                         break;
862                 }
863                 case ROOT_DESC_RUN_LEN:
864                         g_assert_not_reached ();
865                 default:
866                         g_assert_not_reached ();
867                 }
868         } SGEN_HASH_TABLE_FOREACH_END;
869
870         check_key = NULL;
871         check_root = NULL;
872 }
873
874 void
875 mono_gc_scan_for_specific_ref (MonoObject *key, gboolean precise)
876 {
877         void **ptr;
878         RootRecord *root;
879
880         scan_object_for_specific_ref_precise = precise;
881
882         sgen_scan_area_with_callback (nursery_section->data, nursery_section->end_data,
883                         (IterateObjectCallbackFunc)scan_object_for_specific_ref_callback, key, TRUE);
884
885         major_collector.iterate_objects (TRUE, TRUE, (IterateObjectCallbackFunc)scan_object_for_specific_ref_callback, key);
886
887         sgen_los_iterate_objects ((IterateObjectCallbackFunc)scan_object_for_specific_ref_callback, key);
888
889         scan_roots_for_specific_ref (key, ROOT_TYPE_NORMAL);
890         scan_roots_for_specific_ref (key, ROOT_TYPE_WBARRIER);
891
892         SGEN_HASH_TABLE_FOREACH (&roots_hash [ROOT_TYPE_PINNED], ptr, root) {
893                 while (ptr < (void**)root->end_root) {
894                         check_root_obj_specific_ref (root, *ptr, key);
895                         ++ptr;
896                 }
897         } SGEN_HASH_TABLE_FOREACH_END;
898 }
899
900 static gboolean
901 need_remove_object_for_domain (char *start, MonoDomain *domain)
902 {
903         if (mono_object_domain (start) == domain) {
904                 SGEN_LOG (4, "Need to cleanup object %p", start);
905                 binary_protocol_cleanup (start, (gpointer)LOAD_VTABLE (start), safe_object_get_size ((MonoObject*)start));
906                 return TRUE;
907         }
908         return FALSE;
909 }
910
911 static void
912 process_object_for_domain_clearing (char *start, MonoDomain *domain)
913 {
914         GCVTable *vt = (GCVTable*)LOAD_VTABLE (start);
915         if (vt->klass == mono_defaults.internal_thread_class)
916                 g_assert (mono_object_domain (start) == mono_get_root_domain ());
917         /* The object could be a proxy for an object in the domain
918            we're deleting. */
919         if (mono_class_has_parent_fast (vt->klass, mono_defaults.real_proxy_class)) {
920                 MonoObject *server = ((MonoRealProxy*)start)->unwrapped_server;
921
922                 /* The server could already have been zeroed out, so
923                    we need to check for that, too. */
924                 if (server && (!LOAD_VTABLE (server) || mono_object_domain (server) == domain)) {
925                         SGEN_LOG (4, "Cleaning up remote pointer in %p to object %p", start, server);
926                         ((MonoRealProxy*)start)->unwrapped_server = NULL;
927                 }
928         }
929 }
930
931 static MonoDomain *check_domain = NULL;
932
933 static void
934 check_obj_not_in_domain (void **o)
935 {
936         g_assert (((MonoObject*)(*o))->vtable->domain != check_domain);
937 }
938
939 static void
940 scan_for_registered_roots_in_domain (MonoDomain *domain, int root_type)
941 {
942         void **start_root;
943         RootRecord *root;
944         check_domain = domain;
945         SGEN_HASH_TABLE_FOREACH (&roots_hash [root_type], start_root, root) {
946                 mword desc = root->root_desc;
947
948                 /* The MonoDomain struct is allowed to hold
949                    references to objects in its own domain. */
950                 if (start_root == (void**)domain)
951                         continue;
952
953                 switch (desc & ROOT_DESC_TYPE_MASK) {
954                 case ROOT_DESC_BITMAP:
955                         desc >>= ROOT_DESC_TYPE_SHIFT;
956                         while (desc) {
957                                 if ((desc & 1) && *start_root)
958                                         check_obj_not_in_domain (*start_root);
959                                 desc >>= 1;
960                                 start_root++;
961                         }
962                         break;
963                 case ROOT_DESC_COMPLEX: {
964                         gsize *bitmap_data = sgen_get_complex_descriptor_bitmap (desc);
965                         int bwords = (*bitmap_data) - 1;
966                         void **start_run = start_root;
967                         bitmap_data++;
968                         while (bwords-- > 0) {
969                                 gsize bmap = *bitmap_data++;
970                                 void **objptr = start_run;
971                                 while (bmap) {
972                                         if ((bmap & 1) && *objptr)
973                                                 check_obj_not_in_domain (*objptr);
974                                         bmap >>= 1;
975                                         ++objptr;
976                                 }
977                                 start_run += GC_BITS_PER_WORD;
978                         }
979                         break;
980                 }
981                 case ROOT_DESC_USER: {
982                         MonoGCRootMarkFunc marker = sgen_get_user_descriptor_func (desc);
983                         marker (start_root, check_obj_not_in_domain);
984                         break;
985                 }
986                 case ROOT_DESC_RUN_LEN:
987                         g_assert_not_reached ();
988                 default:
989                         g_assert_not_reached ();
990                 }
991         } SGEN_HASH_TABLE_FOREACH_END;
992
993         check_domain = NULL;
994 }
995
996 static void
997 check_for_xdomain_refs (void)
998 {
999         LOSObject *bigobj;
1000
1001         sgen_scan_area_with_callback (nursery_section->data, nursery_section->end_data,
1002                         (IterateObjectCallbackFunc)scan_object_for_xdomain_refs, NULL, FALSE);
1003
1004         major_collector.iterate_objects (TRUE, TRUE, (IterateObjectCallbackFunc)scan_object_for_xdomain_refs, NULL);
1005
1006         for (bigobj = los_object_list; bigobj; bigobj = bigobj->next)
1007                 scan_object_for_xdomain_refs (bigobj->data, sgen_los_object_size (bigobj), NULL);
1008 }
1009
1010 static gboolean
1011 clear_domain_process_object (char *obj, MonoDomain *domain)
1012 {
1013         gboolean remove;
1014
1015         process_object_for_domain_clearing (obj, domain);
1016         remove = need_remove_object_for_domain (obj, domain);
1017
1018         if (remove && ((MonoObject*)obj)->synchronisation) {
1019                 void **dislink = mono_monitor_get_object_monitor_weak_link ((MonoObject*)obj);
1020                 if (dislink)
1021                         sgen_register_disappearing_link (NULL, dislink, FALSE, TRUE);
1022         }
1023
1024         return remove;
1025 }
1026
1027 static void
1028 clear_domain_process_minor_object_callback (char *obj, size_t size, MonoDomain *domain)
1029 {
1030         if (clear_domain_process_object (obj, domain))
1031                 memset (obj, 0, size);
1032 }
1033
1034 static void
1035 clear_domain_process_major_object_callback (char *obj, size_t size, MonoDomain *domain)
1036 {
1037         clear_domain_process_object (obj, domain);
1038 }
1039
1040 static void
1041 clear_domain_free_major_non_pinned_object_callback (char *obj, size_t size, MonoDomain *domain)
1042 {
1043         if (need_remove_object_for_domain (obj, domain))
1044                 major_collector.free_non_pinned_object (obj, size);
1045 }
1046
1047 static void
1048 clear_domain_free_major_pinned_object_callback (char *obj, size_t size, MonoDomain *domain)
1049 {
1050         if (need_remove_object_for_domain (obj, domain))
1051                 major_collector.free_pinned_object (obj, size);
1052 }
1053
1054 /*
1055  * When appdomains are unloaded we can easily remove objects that have finalizers,
1056  * but all the others could still be present in random places on the heap.
1057  * We need a sweep to get rid of them even though it's going to be costly
1058  * with big heaps.
1059  * The reason we need to remove them is because we access the vtable and class
1060  * structures to know the object size and the reference bitmap: once the domain is
1061  * unloaded the point to random memory.
1062  */
1063 void
1064 mono_gc_clear_domain (MonoDomain * domain)
1065 {
1066         LOSObject *bigobj, *prev;
1067         int i;
1068
1069         LOCK_GC;
1070
1071         sgen_process_fin_stage_entries ();
1072         sgen_process_dislink_stage_entries ();
1073
1074         sgen_clear_nursery_fragments ();
1075
1076         if (xdomain_checks && domain != mono_get_root_domain ()) {
1077                 scan_for_registered_roots_in_domain (domain, ROOT_TYPE_NORMAL);
1078                 scan_for_registered_roots_in_domain (domain, ROOT_TYPE_WBARRIER);
1079                 check_for_xdomain_refs ();
1080         }
1081
1082         /*Ephemerons and dislinks must be processed before LOS since they might end up pointing
1083         to memory returned to the OS.*/
1084         null_ephemerons_for_domain (domain);
1085
1086         for (i = GENERATION_NURSERY; i < GENERATION_MAX; ++i)
1087                 sgen_null_links_for_domain (domain, i);
1088
1089         for (i = GENERATION_NURSERY; i < GENERATION_MAX; ++i)
1090                 sgen_remove_finalizers_for_domain (domain, i);
1091
1092         sgen_scan_area_with_callback (nursery_section->data, nursery_section->end_data,
1093                         (IterateObjectCallbackFunc)clear_domain_process_minor_object_callback, domain, FALSE);
1094
1095         /* We need two passes over major and large objects because
1096            freeing such objects might give their memory back to the OS
1097            (in the case of large objects) or obliterate its vtable
1098            (pinned objects with major-copying or pinned and non-pinned
1099            objects with major-mark&sweep), but we might need to
1100            dereference a pointer from an object to another object if
1101            the first object is a proxy. */
1102         major_collector.iterate_objects (TRUE, TRUE, (IterateObjectCallbackFunc)clear_domain_process_major_object_callback, domain);
1103         for (bigobj = los_object_list; bigobj; bigobj = bigobj->next)
1104                 clear_domain_process_object (bigobj->data, domain);
1105
1106         prev = NULL;
1107         for (bigobj = los_object_list; bigobj;) {
1108                 if (need_remove_object_for_domain (bigobj->data, domain)) {
1109                         LOSObject *to_free = bigobj;
1110                         if (prev)
1111                                 prev->next = bigobj->next;
1112                         else
1113                                 los_object_list = bigobj->next;
1114                         bigobj = bigobj->next;
1115                         SGEN_LOG (4, "Freeing large object %p", bigobj->data);
1116                         sgen_los_free_object (to_free);
1117                         continue;
1118                 }
1119                 prev = bigobj;
1120                 bigobj = bigobj->next;
1121         }
1122         major_collector.iterate_objects (TRUE, FALSE, (IterateObjectCallbackFunc)clear_domain_free_major_non_pinned_object_callback, domain);
1123         major_collector.iterate_objects (FALSE, TRUE, (IterateObjectCallbackFunc)clear_domain_free_major_pinned_object_callback, domain);
1124
1125         if (G_UNLIKELY (do_pin_stats)) {
1126                 if (domain == mono_get_root_domain ())
1127                         sgen_pin_stats_print_class_stats ();
1128         }
1129
1130         UNLOCK_GC;
1131 }
1132
1133 /*
1134  * sgen_add_to_global_remset:
1135  *
1136  *   The global remset contains locations which point into newspace after
1137  * a minor collection. This can happen if the objects they point to are pinned.
1138  *
1139  * LOCKING: If called from a parallel collector, the global remset
1140  * lock must be held.  For serial collectors that is not necessary.
1141  */
1142 void
1143 sgen_add_to_global_remset (gpointer ptr, gpointer obj)
1144 {
1145         remset.record_pointer (ptr);
1146
1147         if (G_UNLIKELY (MONO_GC_GLOBAL_REMSET_ADD_ENABLED ())) {
1148                 MonoVTable *vt = (MonoVTable*)LOAD_VTABLE (obj);
1149                 MONO_GC_GLOBAL_REMSET_ADD ((mword)ptr, (mword)obj, sgen_safe_object_get_size (obj),
1150                                 vt->klass->name_space, vt->klass->name);
1151         }
1152 }
1153
1154 /*
1155  * sgen_drain_gray_stack:
1156  *
1157  *   Scan objects in the gray stack until the stack is empty. This should be called
1158  * frequently after each object is copied, to achieve better locality and cache
1159  * usage.
1160  */
1161 gboolean
1162 sgen_drain_gray_stack (int max_objs, ScanCopyContext ctx)
1163 {
1164         char *obj;
1165         ScanObjectFunc scan_func = ctx.scan_func;
1166         GrayQueue *queue = ctx.queue;
1167
1168         if (max_objs == -1) {
1169                 for (;;) {
1170                         GRAY_OBJECT_DEQUEUE (queue, obj);
1171                         if (!obj)
1172                                 return TRUE;
1173                         SGEN_LOG (9, "Precise gray object scan %p (%s)", obj, safe_name (obj));
1174                         scan_func (obj, queue);
1175                 }
1176         } else {
1177                 int i;
1178
1179                 do {
1180                         for (i = 0; i != max_objs; ++i) {
1181                                 GRAY_OBJECT_DEQUEUE (queue, obj);
1182                                 if (!obj)
1183                                         return TRUE;
1184                                 SGEN_LOG (9, "Precise gray object scan %p (%s)", obj, safe_name (obj));
1185                                 scan_func (obj, queue);
1186                         }
1187                 } while (max_objs < 0);
1188                 return FALSE;
1189         }
1190 }
1191
1192 /*
1193  * Addresses from start to end are already sorted. This function finds
1194  * the object header for each address and pins the object. The
1195  * addresses must be inside the passed section.  The (start of the)
1196  * address array is overwritten with the addresses of the actually
1197  * pinned objects.  Return the number of pinned objects.
1198  */
1199 static int
1200 pin_objects_from_addresses (GCMemSection *section, void **start, void **end, void *start_nursery, void *end_nursery, ScanCopyContext ctx)
1201 {
1202         void *last = NULL;
1203         int count = 0;
1204         void *search_start;
1205         void *last_obj = NULL;
1206         size_t last_obj_size = 0;
1207         void *addr;
1208         int idx;
1209         void **definitely_pinned = start;
1210         ScanObjectFunc scan_func = ctx.scan_func;
1211         SgenGrayQueue *queue = ctx.queue;
1212
1213         sgen_nursery_allocator_prepare_for_pinning ();
1214
1215         while (start < end) {
1216                 addr = *start;
1217                 /* the range check should be reduntant */
1218                 if (addr != last && addr >= start_nursery && addr < end_nursery) {
1219                         SGEN_LOG (5, "Considering pinning addr %p", addr);
1220                         /* multiple pointers to the same object */
1221                         if (addr >= last_obj && (char*)addr < (char*)last_obj + last_obj_size) {
1222                                 start++;
1223                                 continue;
1224                         }
1225                         idx = ((char*)addr - (char*)section->data) / SCAN_START_SIZE;
1226                         g_assert (idx < section->num_scan_start);
1227                         search_start = (void*)section->scan_starts [idx];
1228                         if (!search_start || search_start > addr) {
1229                                 while (idx) {
1230                                         --idx;
1231                                         search_start = section->scan_starts [idx];
1232                                         if (search_start && search_start <= addr)
1233                                                 break;
1234                                 }
1235                                 if (!search_start || search_start > addr)
1236                                         search_start = start_nursery;
1237                         }
1238                         if (search_start < last_obj)
1239                                 search_start = (char*)last_obj + last_obj_size;
1240                         /* now addr should be in an object a short distance from search_start
1241                          * Note that search_start must point to zeroed mem or point to an object.
1242                          */
1243
1244                         do {
1245                                 if (!*(void**)search_start) {
1246                                         /* Consistency check */
1247                                         /*
1248                                         for (frag = nursery_fragments; frag; frag = frag->next) {
1249                                                 if (search_start >= frag->fragment_start && search_start < frag->fragment_end)
1250                                                         g_assert_not_reached ();
1251                                         }
1252                                         */
1253
1254                                         search_start = (void*)ALIGN_UP ((mword)search_start + sizeof (gpointer));
1255                                         continue;
1256                                 }
1257                                 last_obj = search_start;
1258                                 last_obj_size = ALIGN_UP (safe_object_get_size ((MonoObject*)search_start));
1259
1260                                 if (((MonoObject*)last_obj)->synchronisation == GINT_TO_POINTER (-1)) {
1261                                         /* Marks the beginning of a nursery fragment, skip */
1262                                 } else {
1263                                         SGEN_LOG (8, "Pinned try match %p (%s), size %zd", last_obj, safe_name (last_obj), last_obj_size);
1264                                         if (addr >= search_start && (char*)addr < (char*)last_obj + last_obj_size) {
1265                                                 SGEN_LOG (4, "Pinned object %p, vtable %p (%s), count %d\n", search_start, *(void**)search_start, safe_name (search_start), count);
1266                                                 binary_protocol_pin (search_start, (gpointer)LOAD_VTABLE (search_start), safe_object_get_size (search_start));
1267                                                 if (G_UNLIKELY (MONO_GC_OBJ_PINNED_ENABLED ())) {
1268                                                         int gen = sgen_ptr_in_nursery (search_start) ? GENERATION_NURSERY : GENERATION_OLD;
1269                                                         MonoVTable *vt = (MonoVTable*)LOAD_VTABLE (search_start);
1270                                                         MONO_GC_OBJ_PINNED ((mword)search_start, sgen_safe_object_get_size (search_start), vt->klass->name_space, vt->klass->name, gen);
1271                                                 }
1272                                                 if (scan_func) {
1273                                                         scan_func (search_start, queue);
1274                                                 } else {
1275                                                         pin_object (search_start);
1276                                                         GRAY_OBJECT_ENQUEUE (queue, search_start);
1277                                                         if (G_UNLIKELY (do_pin_stats))
1278                                                                 sgen_pin_stats_register_object (search_start, last_obj_size);
1279                                                         definitely_pinned [count] = search_start;
1280                                                         count++;
1281                                                 }
1282                                                 break;
1283                                         }
1284                                 }
1285                                 /* skip to the next object */
1286                                 search_start = (void*)((char*)search_start + last_obj_size);
1287                         } while (search_start <= addr);
1288                         /* we either pinned the correct object or we ignored the addr because
1289                          * it points to unused zeroed memory.
1290                          */
1291                         last = addr;
1292                 }
1293                 start++;
1294         }
1295         //printf ("effective pinned: %d (at the end: %d)\n", count, (char*)end_nursery - (char*)last);
1296         if (mono_profiler_get_events () & MONO_PROFILE_GC_ROOTS) {
1297                 GCRootReport report;
1298                 report.count = 0;
1299                 for (idx = 0; idx < count; ++idx)
1300                         add_profile_gc_root (&report, definitely_pinned [idx], MONO_PROFILE_GC_ROOT_PINNING | MONO_PROFILE_GC_ROOT_MISC, 0);
1301                 notify_gc_roots (&report);
1302         }
1303         stat_pinned_objects += count;
1304         return count;
1305 }
1306
1307 void
1308 sgen_pin_objects_in_section (GCMemSection *section, ScanCopyContext ctx)
1309 {
1310         int num_entries = section->pin_queue_num_entries;
1311         if (num_entries) {
1312                 void **start = section->pin_queue_start;
1313                 int reduced_to;
1314                 reduced_to = pin_objects_from_addresses (section, start, start + num_entries,
1315                                 section->data, section->next_data, ctx);
1316                 section->pin_queue_num_entries = reduced_to;
1317                 if (!reduced_to)
1318                         section->pin_queue_start = NULL;
1319         }
1320 }
1321
1322
1323 void
1324 sgen_pin_object (void *object, GrayQueue *queue)
1325 {
1326         g_assert (!concurrent_collection_in_progress);
1327
1328         if (sgen_collection_is_parallel ()) {
1329                 LOCK_PIN_QUEUE;
1330                 /*object arrives pinned*/
1331                 sgen_pin_stage_ptr (object);
1332                 ++objects_pinned ;
1333                 UNLOCK_PIN_QUEUE;
1334         } else {
1335                 SGEN_PIN_OBJECT (object);
1336                 sgen_pin_stage_ptr (object);
1337                 ++objects_pinned;
1338                 if (G_UNLIKELY (do_pin_stats))
1339                         sgen_pin_stats_register_object (object, safe_object_get_size (object));
1340         }
1341         GRAY_OBJECT_ENQUEUE (queue, object);
1342         binary_protocol_pin (object, (gpointer)LOAD_VTABLE (object), safe_object_get_size (object));
1343         if (G_UNLIKELY (MONO_GC_OBJ_PINNED_ENABLED ())) {
1344                 int gen = sgen_ptr_in_nursery (object) ? GENERATION_NURSERY : GENERATION_OLD;
1345                 MonoVTable *vt = (MonoVTable*)LOAD_VTABLE (object);
1346                 MONO_GC_OBJ_PINNED ((mword)object, sgen_safe_object_get_size (object), vt->klass->name_space, vt->klass->name, gen);
1347         }
1348 }
1349
1350 void
1351 sgen_parallel_pin_or_update (void **ptr, void *obj, MonoVTable *vt, SgenGrayQueue *queue)
1352 {
1353         for (;;) {
1354                 mword vtable_word;
1355                 gboolean major_pinned = FALSE;
1356
1357                 if (sgen_ptr_in_nursery (obj)) {
1358                         if (SGEN_CAS_PTR (obj, (void*)((mword)vt | SGEN_PINNED_BIT), vt) == vt) {
1359                                 sgen_pin_object (obj, queue);
1360                                 break;
1361                         }
1362                 } else {
1363                         major_collector.pin_major_object (obj, queue);
1364                         major_pinned = TRUE;
1365                 }
1366
1367                 vtable_word = *(mword*)obj;
1368                 /*someone else forwarded it, update the pointer and bail out*/
1369                 if (vtable_word & SGEN_FORWARDED_BIT) {
1370                         *ptr = (void*)(vtable_word & ~SGEN_VTABLE_BITS_MASK);
1371                         break;
1372                 }
1373
1374                 /*someone pinned it, nothing to do.*/
1375                 if (vtable_word & SGEN_PINNED_BIT || major_pinned)
1376                         break;
1377         }
1378 }
1379
1380 /* Sort the addresses in array in increasing order.
1381  * Done using a by-the book heap sort. Which has decent and stable performance, is pretty cache efficient.
1382  */
1383 void
1384 sgen_sort_addresses (void **array, int size)
1385 {
1386         int i;
1387         void *tmp;
1388
1389         for (i = 1; i < size; ++i) {
1390                 int child = i;
1391                 while (child > 0) {
1392                         int parent = (child - 1) / 2;
1393
1394                         if (array [parent] >= array [child])
1395                                 break;
1396
1397                         tmp = array [parent];
1398                         array [parent] = array [child];
1399                         array [child] = tmp;
1400
1401                         child = parent;
1402                 }
1403         }
1404
1405         for (i = size - 1; i > 0; --i) {
1406                 int end, root;
1407                 tmp = array [i];
1408                 array [i] = array [0];
1409                 array [0] = tmp;
1410
1411                 end = i - 1;
1412                 root = 0;
1413
1414                 while (root * 2 + 1 <= end) {
1415                         int child = root * 2 + 1;
1416
1417                         if (child < end && array [child] < array [child + 1])
1418                                 ++child;
1419                         if (array [root] >= array [child])
1420                                 break;
1421
1422                         tmp = array [root];
1423                         array [root] = array [child];
1424                         array [child] = tmp;
1425
1426                         root = child;
1427                 }
1428         }
1429 }
1430
1431 /* 
1432  * Scan the memory between start and end and queue values which could be pointers
1433  * to the area between start_nursery and end_nursery for later consideration.
1434  * Typically used for thread stacks.
1435  */
1436 static void
1437 conservatively_pin_objects_from (void **start, void **end, void *start_nursery, void *end_nursery, int pin_type)
1438 {
1439         int count = 0;
1440
1441 #ifdef VALGRIND_MAKE_MEM_DEFINED_IF_ADDRESSABLE
1442         VALGRIND_MAKE_MEM_DEFINED_IF_ADDRESSABLE (start, (char*)end - (char*)start);
1443 #endif
1444
1445         while (start < end) {
1446                 if (*start >= start_nursery && *start < end_nursery) {
1447                         /*
1448                          * *start can point to the middle of an object
1449                          * note: should we handle pointing at the end of an object?
1450                          * pinning in C# code disallows pointing at the end of an object
1451                          * but there is some small chance that an optimizing C compiler
1452                          * may keep the only reference to an object by pointing
1453                          * at the end of it. We ignore this small chance for now.
1454                          * Pointers to the end of an object are indistinguishable
1455                          * from pointers to the start of the next object in memory
1456                          * so if we allow that we'd need to pin two objects...
1457                          * We queue the pointer in an array, the
1458                          * array will then be sorted and uniqued. This way
1459                          * we can coalesce several pinning pointers and it should
1460                          * be faster since we'd do a memory scan with increasing
1461                          * addresses. Note: we can align the address to the allocation
1462                          * alignment, so the unique process is more effective.
1463                          */
1464                         mword addr = (mword)*start;
1465                         addr &= ~(ALLOC_ALIGN - 1);
1466                         if (addr >= (mword)start_nursery && addr < (mword)end_nursery) {
1467                                 SGEN_LOG (6, "Pinning address %p from %p", (void*)addr, start);
1468                                 sgen_pin_stage_ptr ((void*)addr);
1469                                 count++;
1470                         }
1471                         if (G_UNLIKELY (do_pin_stats)) { 
1472                                 if (ptr_in_nursery ((void*)addr))
1473                                         sgen_pin_stats_register_address ((char*)addr, pin_type);
1474                         }
1475                 }
1476                 start++;
1477         }
1478         if (count)
1479                 SGEN_LOG (7, "found %d potential pinned heap pointers", count);
1480 }
1481
1482 /*
1483  * The first thing we do in a collection is to identify pinned objects.
1484  * This function considers all the areas of memory that need to be
1485  * conservatively scanned.
1486  */
1487 static void
1488 pin_from_roots (void *start_nursery, void *end_nursery, GrayQueue *queue)
1489 {
1490         void **start_root;
1491         RootRecord *root;
1492         SGEN_LOG (2, "Scanning pinned roots (%d bytes, %d/%d entries)", (int)roots_size, roots_hash [ROOT_TYPE_NORMAL].num_entries, roots_hash [ROOT_TYPE_PINNED].num_entries);
1493         /* objects pinned from the API are inside these roots */
1494         SGEN_HASH_TABLE_FOREACH (&roots_hash [ROOT_TYPE_PINNED], start_root, root) {
1495                 SGEN_LOG (6, "Pinned roots %p-%p", start_root, root->end_root);
1496                 conservatively_pin_objects_from (start_root, (void**)root->end_root, start_nursery, end_nursery, PIN_TYPE_OTHER);
1497         } SGEN_HASH_TABLE_FOREACH_END;
1498         /* now deal with the thread stacks
1499          * in the future we should be able to conservatively scan only:
1500          * *) the cpu registers
1501          * *) the unmanaged stack frames
1502          * *) the _last_ managed stack frame
1503          * *) pointers slots in managed frames
1504          */
1505         scan_thread_data (start_nursery, end_nursery, FALSE, queue);
1506 }
1507
1508 static void
1509 unpin_objects_from_queue (SgenGrayQueue *queue)
1510 {
1511         for (;;) {
1512                 char *addr;
1513                 GRAY_OBJECT_DEQUEUE (queue, addr);
1514                 if (!addr)
1515                         break;
1516                 SGEN_UNPIN_OBJECT (addr);
1517         }
1518 }
1519
1520 typedef struct {
1521         CopyOrMarkObjectFunc func;
1522         GrayQueue *queue;
1523 } UserCopyOrMarkData;
1524
1525 static MonoNativeTlsKey user_copy_or_mark_key;
1526
1527 static void
1528 init_user_copy_or_mark_key (void)
1529 {
1530         mono_native_tls_alloc (&user_copy_or_mark_key, NULL);
1531 }
1532
1533 static void
1534 set_user_copy_or_mark_data (UserCopyOrMarkData *data)
1535 {
1536         mono_native_tls_set_value (user_copy_or_mark_key, data);
1537 }
1538
1539 static void
1540 single_arg_user_copy_or_mark (void **obj)
1541 {
1542         UserCopyOrMarkData *data = mono_native_tls_get_value (user_copy_or_mark_key);
1543
1544         data->func (obj, data->queue);
1545 }
1546
1547 /*
1548  * The memory area from start_root to end_root contains pointers to objects.
1549  * Their position is precisely described by @desc (this means that the pointer
1550  * can be either NULL or the pointer to the start of an object).
1551  * This functions copies them to to_space updates them.
1552  *
1553  * This function is not thread-safe!
1554  */
1555 static void
1556 precisely_scan_objects_from (void** start_root, void** end_root, char* n_start, char *n_end, mword desc, ScanCopyContext ctx)
1557 {
1558         CopyOrMarkObjectFunc copy_func = ctx.copy_func;
1559         SgenGrayQueue *queue = ctx.queue;
1560
1561         switch (desc & ROOT_DESC_TYPE_MASK) {
1562         case ROOT_DESC_BITMAP:
1563                 desc >>= ROOT_DESC_TYPE_SHIFT;
1564                 while (desc) {
1565                         if ((desc & 1) && *start_root) {
1566                                 copy_func (start_root, queue);
1567                                 SGEN_LOG (9, "Overwrote root at %p with %p", start_root, *start_root);
1568                                 sgen_drain_gray_stack (-1, ctx);
1569                         }
1570                         desc >>= 1;
1571                         start_root++;
1572                 }
1573                 return;
1574         case ROOT_DESC_COMPLEX: {
1575                 gsize *bitmap_data = sgen_get_complex_descriptor_bitmap (desc);
1576                 int bwords = (*bitmap_data) - 1;
1577                 void **start_run = start_root;
1578                 bitmap_data++;
1579                 while (bwords-- > 0) {
1580                         gsize bmap = *bitmap_data++;
1581                         void **objptr = start_run;
1582                         while (bmap) {
1583                                 if ((bmap & 1) && *objptr) {
1584                                         copy_func (objptr, queue);
1585                                         SGEN_LOG (9, "Overwrote root at %p with %p", objptr, *objptr);
1586                                         sgen_drain_gray_stack (-1, ctx);
1587                                 }
1588                                 bmap >>= 1;
1589                                 ++objptr;
1590                         }
1591                         start_run += GC_BITS_PER_WORD;
1592                 }
1593                 break;
1594         }
1595         case ROOT_DESC_USER: {
1596                 UserCopyOrMarkData data = { copy_func, queue };
1597                 MonoGCRootMarkFunc marker = sgen_get_user_descriptor_func (desc);
1598                 set_user_copy_or_mark_data (&data);
1599                 marker (start_root, single_arg_user_copy_or_mark);
1600                 set_user_copy_or_mark_data (NULL);
1601                 break;
1602         }
1603         case ROOT_DESC_RUN_LEN:
1604                 g_assert_not_reached ();
1605         default:
1606                 g_assert_not_reached ();
1607         }
1608 }
1609
1610 static void
1611 reset_heap_boundaries (void)
1612 {
1613         lowest_heap_address = ~(mword)0;
1614         highest_heap_address = 0;
1615 }
1616
1617 void
1618 sgen_update_heap_boundaries (mword low, mword high)
1619 {
1620         mword old;
1621
1622         do {
1623                 old = lowest_heap_address;
1624                 if (low >= old)
1625                         break;
1626         } while (SGEN_CAS_PTR ((gpointer*)&lowest_heap_address, (gpointer)low, (gpointer)old) != (gpointer)old);
1627
1628         do {
1629                 old = highest_heap_address;
1630                 if (high <= old)
1631                         break;
1632         } while (SGEN_CAS_PTR ((gpointer*)&highest_heap_address, (gpointer)high, (gpointer)old) != (gpointer)old);
1633 }
1634
1635 /*
1636  * Allocate and setup the data structures needed to be able to allocate objects
1637  * in the nursery. The nursery is stored in nursery_section.
1638  */
1639 static void
1640 alloc_nursery (void)
1641 {
1642         GCMemSection *section;
1643         char *data;
1644         int scan_starts;
1645         int alloc_size;
1646
1647         if (nursery_section)
1648                 return;
1649         SGEN_LOG (2, "Allocating nursery size: %lu", (unsigned long)sgen_nursery_size);
1650         /* later we will alloc a larger area for the nursery but only activate
1651          * what we need. The rest will be used as expansion if we have too many pinned
1652          * objects in the existing nursery.
1653          */
1654         /* FIXME: handle OOM */
1655         section = sgen_alloc_internal (INTERNAL_MEM_SECTION);
1656
1657         alloc_size = sgen_nursery_size;
1658
1659         /* If there isn't enough space even for the nursery we should simply abort. */
1660         g_assert (sgen_memgov_try_alloc_space (alloc_size, SPACE_NURSERY));
1661
1662 #ifdef SGEN_ALIGN_NURSERY
1663         data = major_collector.alloc_heap (alloc_size, alloc_size, DEFAULT_NURSERY_BITS);
1664 #else
1665         data = major_collector.alloc_heap (alloc_size, 0, DEFAULT_NURSERY_BITS);
1666 #endif
1667         sgen_update_heap_boundaries ((mword)data, (mword)(data + sgen_nursery_size));
1668         SGEN_LOG (4, "Expanding nursery size (%p-%p): %lu, total: %lu", data, data + alloc_size, (unsigned long)sgen_nursery_size, (unsigned long)mono_gc_get_heap_size ());
1669         section->data = section->next_data = data;
1670         section->size = alloc_size;
1671         section->end_data = data + sgen_nursery_size;
1672         scan_starts = (alloc_size + SCAN_START_SIZE - 1) / SCAN_START_SIZE;
1673         section->scan_starts = sgen_alloc_internal_dynamic (sizeof (char*) * scan_starts, INTERNAL_MEM_SCAN_STARTS, TRUE);
1674         section->num_scan_start = scan_starts;
1675         section->block.role = MEMORY_ROLE_GEN0;
1676         section->block.next = NULL;
1677
1678         nursery_section = section;
1679
1680         sgen_nursery_allocator_set_nursery_bounds (data, data + sgen_nursery_size);
1681 }
1682
1683 void*
1684 mono_gc_get_nursery (int *shift_bits, size_t *size)
1685 {
1686         *size = sgen_nursery_size;
1687 #ifdef SGEN_ALIGN_NURSERY
1688         *shift_bits = DEFAULT_NURSERY_BITS;
1689 #else
1690         *shift_bits = -1;
1691 #endif
1692         return sgen_get_nursery_start ();
1693 }
1694
1695 void
1696 mono_gc_set_current_thread_appdomain (MonoDomain *domain)
1697 {
1698         SgenThreadInfo *info = mono_thread_info_current ();
1699
1700         /* Could be called from sgen_thread_unregister () with a NULL info */
1701         if (domain) {
1702                 g_assert (info);
1703                 info->stopped_domain = domain;
1704         }
1705 }
1706
1707 gboolean
1708 mono_gc_precise_stack_mark_enabled (void)
1709 {
1710         return !conservative_stack_mark;
1711 }
1712
1713 FILE *
1714 mono_gc_get_logfile (void)
1715 {
1716         return gc_debug_file;
1717 }
1718
1719 static void
1720 report_finalizer_roots_list (FinalizeReadyEntry *list)
1721 {
1722         GCRootReport report;
1723         FinalizeReadyEntry *fin;
1724
1725         report.count = 0;
1726         for (fin = list; fin; fin = fin->next) {
1727                 if (!fin->object)
1728                         continue;
1729                 add_profile_gc_root (&report, fin->object, MONO_PROFILE_GC_ROOT_FINALIZER, 0);
1730         }
1731         notify_gc_roots (&report);
1732 }
1733
1734 static void
1735 report_finalizer_roots (void)
1736 {
1737         report_finalizer_roots_list (fin_ready_list);
1738         report_finalizer_roots_list (critical_fin_list);
1739 }
1740
1741 static GCRootReport *root_report;
1742
1743 static void
1744 single_arg_report_root (void **obj)
1745 {
1746         if (*obj)
1747                 add_profile_gc_root (root_report, *obj, MONO_PROFILE_GC_ROOT_OTHER, 0);
1748 }
1749
1750 static void
1751 precisely_report_roots_from (GCRootReport *report, void** start_root, void** end_root, mword desc)
1752 {
1753         switch (desc & ROOT_DESC_TYPE_MASK) {
1754         case ROOT_DESC_BITMAP:
1755                 desc >>= ROOT_DESC_TYPE_SHIFT;
1756                 while (desc) {
1757                         if ((desc & 1) && *start_root) {
1758                                 add_profile_gc_root (report, *start_root, MONO_PROFILE_GC_ROOT_OTHER, 0);
1759                         }
1760                         desc >>= 1;
1761                         start_root++;
1762                 }
1763                 return;
1764         case ROOT_DESC_COMPLEX: {
1765                 gsize *bitmap_data = sgen_get_complex_descriptor_bitmap (desc);
1766                 int bwords = (*bitmap_data) - 1;
1767                 void **start_run = start_root;
1768                 bitmap_data++;
1769                 while (bwords-- > 0) {
1770                         gsize bmap = *bitmap_data++;
1771                         void **objptr = start_run;
1772                         while (bmap) {
1773                                 if ((bmap & 1) && *objptr) {
1774                                         add_profile_gc_root (report, *objptr, MONO_PROFILE_GC_ROOT_OTHER, 0);
1775                                 }
1776                                 bmap >>= 1;
1777                                 ++objptr;
1778                         }
1779                         start_run += GC_BITS_PER_WORD;
1780                 }
1781                 break;
1782         }
1783         case ROOT_DESC_USER: {
1784                 MonoGCRootMarkFunc marker = sgen_get_user_descriptor_func (desc);
1785                 root_report = report;
1786                 marker (start_root, single_arg_report_root);
1787                 break;
1788         }
1789         case ROOT_DESC_RUN_LEN:
1790                 g_assert_not_reached ();
1791         default:
1792                 g_assert_not_reached ();
1793         }
1794 }
1795
1796 static void
1797 report_registered_roots_by_type (int root_type)
1798 {
1799         GCRootReport report;
1800         void **start_root;
1801         RootRecord *root;
1802         report.count = 0;
1803         SGEN_HASH_TABLE_FOREACH (&roots_hash [root_type], start_root, root) {
1804                 SGEN_LOG (6, "Precise root scan %p-%p (desc: %p)", start_root, root->end_root, (void*)root->root_desc);
1805                 precisely_report_roots_from (&report, start_root, (void**)root->end_root, root->root_desc);
1806         } SGEN_HASH_TABLE_FOREACH_END;
1807         notify_gc_roots (&report);
1808 }
1809
1810 static void
1811 report_registered_roots (void)
1812 {
1813         report_registered_roots_by_type (ROOT_TYPE_NORMAL);
1814         report_registered_roots_by_type (ROOT_TYPE_WBARRIER);
1815 }
1816
1817 static void
1818 scan_finalizer_entries (FinalizeReadyEntry *list, ScanCopyContext ctx)
1819 {
1820         CopyOrMarkObjectFunc copy_func = ctx.copy_func;
1821         SgenGrayQueue *queue = ctx.queue;
1822         FinalizeReadyEntry *fin;
1823
1824         for (fin = list; fin; fin = fin->next) {
1825                 if (!fin->object)
1826                         continue;
1827                 SGEN_LOG (5, "Scan of fin ready object: %p (%s)\n", fin->object, safe_name (fin->object));
1828                 copy_func (&fin->object, queue);
1829         }
1830 }
1831
1832 static const char*
1833 generation_name (int generation)
1834 {
1835         switch (generation) {
1836         case GENERATION_NURSERY: return "nursery";
1837         case GENERATION_OLD: return "old";
1838         default: g_assert_not_reached ();
1839         }
1840 }
1841
1842 const char*
1843 sgen_generation_name (int generation)
1844 {
1845         return generation_name (generation);
1846 }
1847
1848 SgenObjectOperations *
1849 sgen_get_current_object_ops (void){
1850         return &current_object_ops;
1851 }
1852
1853
1854 static void
1855 finish_gray_stack (char *start_addr, char *end_addr, int generation, GrayQueue *queue)
1856 {
1857         TV_DECLARE (atv);
1858         TV_DECLARE (btv);
1859         int done_with_ephemerons, ephemeron_rounds = 0;
1860         CopyOrMarkObjectFunc copy_func = current_object_ops.copy_or_mark_object;
1861         ScanObjectFunc scan_func = current_object_ops.scan_object;
1862         ScanCopyContext ctx = { scan_func, copy_func, queue };
1863
1864         /*
1865          * We copied all the reachable objects. Now it's the time to copy
1866          * the objects that were not referenced by the roots, but by the copied objects.
1867          * we built a stack of objects pointed to by gray_start: they are
1868          * additional roots and we may add more items as we go.
1869          * We loop until gray_start == gray_objects which means no more objects have
1870          * been added. Note this is iterative: no recursion is involved.
1871          * We need to walk the LO list as well in search of marked big objects
1872          * (use a flag since this is needed only on major collections). We need to loop
1873          * here as well, so keep a counter of marked LO (increasing it in copy_object).
1874          *   To achieve better cache locality and cache usage, we drain the gray stack 
1875          * frequently, after each object is copied, and just finish the work here.
1876          */
1877         sgen_drain_gray_stack (-1, ctx);
1878         TV_GETTIME (atv);
1879         SGEN_LOG (2, "%s generation done", generation_name (generation));
1880
1881         /*
1882         Reset bridge data, we might have lingering data from a previous collection if this is a major
1883         collection trigged by minor overflow.
1884
1885         We must reset the gathered bridges since their original block might be evacuated due to major
1886         fragmentation in the meanwhile and the bridge code should not have to deal with that.
1887         */
1888         sgen_bridge_reset_data ();
1889
1890         /*
1891          * Walk the ephemeron tables marking all values with reachable keys. This must be completely done
1892          * before processing finalizable objects or non-tracking weak hamdle to avoid finalizing/clearing
1893          * objects that are in fact reachable.
1894          */
1895         done_with_ephemerons = 0;
1896         do {
1897                 done_with_ephemerons = mark_ephemerons_in_range (start_addr, end_addr, ctx);
1898                 sgen_drain_gray_stack (-1, ctx);
1899                 ++ephemeron_rounds;
1900         } while (!done_with_ephemerons);
1901
1902         sgen_scan_togglerefs (start_addr, end_addr, ctx);
1903         if (generation == GENERATION_OLD)
1904                 sgen_scan_togglerefs (sgen_get_nursery_start (), sgen_get_nursery_end (), ctx);
1905
1906         if (sgen_need_bridge_processing ()) {
1907                 sgen_collect_bridge_objects (start_addr, end_addr, generation, ctx);
1908                 if (generation == GENERATION_OLD)
1909                         sgen_collect_bridge_objects (sgen_get_nursery_start (), sgen_get_nursery_end (), GENERATION_NURSERY, ctx);
1910         }
1911
1912         /*
1913         Make sure we drain the gray stack before processing disappearing links and finalizers.
1914         If we don't make sure it is empty we might wrongly see a live object as dead.
1915         */
1916         sgen_drain_gray_stack (-1, ctx);
1917
1918         /*
1919         We must clear weak links that don't track resurrection before processing object ready for
1920         finalization so they can be cleared before that.
1921         */
1922         sgen_null_link_in_range (start_addr, end_addr, generation, TRUE, ctx);
1923         if (generation == GENERATION_OLD)
1924                 sgen_null_link_in_range (start_addr, end_addr, GENERATION_NURSERY, TRUE, ctx);
1925
1926
1927         /* walk the finalization queue and move also the objects that need to be
1928          * finalized: use the finalized objects as new roots so the objects they depend
1929          * on are also not reclaimed. As with the roots above, only objects in the nursery
1930          * are marked/copied.
1931          */
1932         sgen_finalize_in_range (start_addr, end_addr, generation, ctx);
1933         if (generation == GENERATION_OLD)
1934                 sgen_finalize_in_range (sgen_get_nursery_start (), sgen_get_nursery_end (), GENERATION_NURSERY, ctx);
1935         /* drain the new stack that might have been created */
1936         SGEN_LOG (6, "Precise scan of gray area post fin");
1937         sgen_drain_gray_stack (-1, ctx);
1938
1939         /*
1940          * This must be done again after processing finalizable objects since CWL slots are cleared only after the key is finalized.
1941          */
1942         done_with_ephemerons = 0;
1943         do {
1944                 done_with_ephemerons = mark_ephemerons_in_range (start_addr, end_addr, ctx);
1945                 sgen_drain_gray_stack (-1, ctx);
1946                 ++ephemeron_rounds;
1947         } while (!done_with_ephemerons);
1948
1949         /*
1950          * Clear ephemeron pairs with unreachable keys.
1951          * We pass the copy func so we can figure out if an array was promoted or not.
1952          */
1953         clear_unreachable_ephemerons (start_addr, end_addr, ctx);
1954
1955         TV_GETTIME (btv);
1956         SGEN_LOG (2, "Finalize queue handling scan for %s generation: %d usecs %d ephemeron rounds", generation_name (generation), TV_ELAPSED (atv, btv), ephemeron_rounds);
1957
1958         /*
1959          * handle disappearing links
1960          * Note we do this after checking the finalization queue because if an object
1961          * survives (at least long enough to be finalized) we don't clear the link.
1962          * This also deals with a possible issue with the monitor reclamation: with the Boehm
1963          * GC a finalized object my lose the monitor because it is cleared before the finalizer is
1964          * called.
1965          */
1966         g_assert (sgen_gray_object_queue_is_empty (queue));
1967         for (;;) {
1968                 sgen_null_link_in_range (start_addr, end_addr, generation, FALSE, ctx);
1969                 if (generation == GENERATION_OLD)
1970                         sgen_null_link_in_range (start_addr, end_addr, GENERATION_NURSERY, FALSE, ctx);
1971                 if (sgen_gray_object_queue_is_empty (queue))
1972                         break;
1973                 sgen_drain_gray_stack (-1, ctx);
1974         }
1975
1976         g_assert (sgen_gray_object_queue_is_empty (queue));
1977 }
1978
1979 void
1980 sgen_check_section_scan_starts (GCMemSection *section)
1981 {
1982         int i;
1983         for (i = 0; i < section->num_scan_start; ++i) {
1984                 if (section->scan_starts [i]) {
1985                         guint size = safe_object_get_size ((MonoObject*) section->scan_starts [i]);
1986                         g_assert (size >= sizeof (MonoObject) && size <= MAX_SMALL_OBJ_SIZE);
1987                 }
1988         }
1989 }
1990
1991 static void
1992 check_scan_starts (void)
1993 {
1994         if (!do_scan_starts_check)
1995                 return;
1996         sgen_check_section_scan_starts (nursery_section);
1997         major_collector.check_scan_starts ();
1998 }
1999
2000 static void
2001 scan_from_registered_roots (char *addr_start, char *addr_end, int root_type, ScanCopyContext ctx)
2002 {
2003         void **start_root;
2004         RootRecord *root;
2005         SGEN_HASH_TABLE_FOREACH (&roots_hash [root_type], start_root, root) {
2006                 SGEN_LOG (6, "Precise root scan %p-%p (desc: %p)", start_root, root->end_root, (void*)root->root_desc);
2007                 precisely_scan_objects_from (start_root, (void**)root->end_root, addr_start, addr_end, root->root_desc, ctx);
2008         } SGEN_HASH_TABLE_FOREACH_END;
2009 }
2010
2011 void
2012 sgen_dump_occupied (char *start, char *end, char *section_start)
2013 {
2014         fprintf (heap_dump_file, "<occupied offset=\"%td\" size=\"%td\"/>\n", start - section_start, end - start);
2015 }
2016
2017 void
2018 sgen_dump_section (GCMemSection *section, const char *type)
2019 {
2020         char *start = section->data;
2021         char *end = section->data + section->size;
2022         char *occ_start = NULL;
2023         GCVTable *vt;
2024         char *old_start = NULL; /* just for debugging */
2025
2026         fprintf (heap_dump_file, "<section type=\"%s\" size=\"%lu\">\n", type, (unsigned long)section->size);
2027
2028         while (start < end) {
2029                 guint size;
2030                 MonoClass *class;
2031
2032                 if (!*(void**)start) {
2033                         if (occ_start) {
2034                                 sgen_dump_occupied (occ_start, start, section->data);
2035                                 occ_start = NULL;
2036                         }
2037                         start += sizeof (void*); /* should be ALLOC_ALIGN, really */
2038                         continue;
2039                 }
2040                 g_assert (start < section->next_data);
2041
2042                 if (!occ_start)
2043                         occ_start = start;
2044
2045                 vt = (GCVTable*)LOAD_VTABLE (start);
2046                 class = vt->klass;
2047
2048                 size = ALIGN_UP (safe_object_get_size ((MonoObject*) start));
2049
2050                 /*
2051                 fprintf (heap_dump_file, "<object offset=\"%d\" class=\"%s.%s\" size=\"%d\"/>\n",
2052                                 start - section->data,
2053                                 vt->klass->name_space, vt->klass->name,
2054                                 size);
2055                 */
2056
2057                 old_start = start;
2058                 start += size;
2059         }
2060         if (occ_start)
2061                 sgen_dump_occupied (occ_start, start, section->data);
2062
2063         fprintf (heap_dump_file, "</section>\n");
2064 }
2065
2066 static void
2067 dump_object (MonoObject *obj, gboolean dump_location)
2068 {
2069         static char class_name [1024];
2070
2071         MonoClass *class = mono_object_class (obj);
2072         int i, j;
2073
2074         /*
2075          * Python's XML parser is too stupid to parse angle brackets
2076          * in strings, so we just ignore them;
2077          */
2078         i = j = 0;
2079         while (class->name [i] && j < sizeof (class_name) - 1) {
2080                 if (!strchr ("<>\"", class->name [i]))
2081                         class_name [j++] = class->name [i];
2082                 ++i;
2083         }
2084         g_assert (j < sizeof (class_name));
2085         class_name [j] = 0;
2086
2087         fprintf (heap_dump_file, "<object class=\"%s.%s\" size=\"%d\"",
2088                         class->name_space, class_name,
2089                         safe_object_get_size (obj));
2090         if (dump_location) {
2091                 const char *location;
2092                 if (ptr_in_nursery (obj))
2093                         location = "nursery";
2094                 else if (safe_object_get_size (obj) <= MAX_SMALL_OBJ_SIZE)
2095                         location = "major";
2096                 else
2097                         location = "LOS";
2098                 fprintf (heap_dump_file, " location=\"%s\"", location);
2099         }
2100         fprintf (heap_dump_file, "/>\n");
2101 }
2102
2103 static void
2104 dump_heap (const char *type, int num, const char *reason)
2105 {
2106         ObjectList *list;
2107         LOSObject *bigobj;
2108
2109         fprintf (heap_dump_file, "<collection type=\"%s\" num=\"%d\"", type, num);
2110         if (reason)
2111                 fprintf (heap_dump_file, " reason=\"%s\"", reason);
2112         fprintf (heap_dump_file, ">\n");
2113         fprintf (heap_dump_file, "<other-mem-usage type=\"mempools\" size=\"%ld\"/>\n", mono_mempool_get_bytes_allocated ());
2114         sgen_dump_internal_mem_usage (heap_dump_file);
2115         fprintf (heap_dump_file, "<pinned type=\"stack\" bytes=\"%zu\"/>\n", sgen_pin_stats_get_pinned_byte_count (PIN_TYPE_STACK));
2116         /* fprintf (heap_dump_file, "<pinned type=\"static-data\" bytes=\"%d\"/>\n", pinned_byte_counts [PIN_TYPE_STATIC_DATA]); */
2117         fprintf (heap_dump_file, "<pinned type=\"other\" bytes=\"%zu\"/>\n", sgen_pin_stats_get_pinned_byte_count (PIN_TYPE_OTHER));
2118
2119         fprintf (heap_dump_file, "<pinned-objects>\n");
2120         for (list = sgen_pin_stats_get_object_list (); list; list = list->next)
2121                 dump_object (list->obj, TRUE);
2122         fprintf (heap_dump_file, "</pinned-objects>\n");
2123
2124         sgen_dump_section (nursery_section, "nursery");
2125
2126         major_collector.dump_heap (heap_dump_file);
2127
2128         fprintf (heap_dump_file, "<los>\n");
2129         for (bigobj = los_object_list; bigobj; bigobj = bigobj->next)
2130                 dump_object ((MonoObject*)bigobj->data, FALSE);
2131         fprintf (heap_dump_file, "</los>\n");
2132
2133         fprintf (heap_dump_file, "</collection>\n");
2134 }
2135
2136 void
2137 sgen_register_moved_object (void *obj, void *destination)
2138 {
2139         g_assert (mono_profiler_events & MONO_PROFILE_GC_MOVES);
2140
2141         /* FIXME: handle this for parallel collector */
2142         g_assert (!sgen_collection_is_parallel ());
2143
2144         if (moved_objects_idx == MOVED_OBJECTS_NUM) {
2145                 mono_profiler_gc_moves (moved_objects, moved_objects_idx);
2146                 moved_objects_idx = 0;
2147         }
2148         moved_objects [moved_objects_idx++] = obj;
2149         moved_objects [moved_objects_idx++] = destination;
2150 }
2151
2152 static void
2153 init_stats (void)
2154 {
2155         static gboolean inited = FALSE;
2156
2157         if (inited)
2158                 return;
2159
2160         mono_counters_register ("Minor fragment clear", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_minor_pre_collection_fragment_clear);
2161         mono_counters_register ("Minor pinning", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_minor_pinning);
2162         mono_counters_register ("Minor scan remembered set", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_minor_scan_remsets);
2163         mono_counters_register ("Minor scan pinned", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_minor_scan_pinned);
2164         mono_counters_register ("Minor scan registered roots", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_minor_scan_registered_roots);
2165         mono_counters_register ("Minor scan thread data", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_minor_scan_thread_data);
2166         mono_counters_register ("Minor finish gray stack", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_minor_finish_gray_stack);
2167         mono_counters_register ("Minor fragment creation", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_minor_fragment_creation);
2168
2169         mono_counters_register ("Major fragment clear", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_major_pre_collection_fragment_clear);
2170         mono_counters_register ("Major pinning", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_major_pinning);
2171         mono_counters_register ("Major scan pinned", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_major_scan_pinned);
2172         mono_counters_register ("Major scan registered roots", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_major_scan_registered_roots);
2173         mono_counters_register ("Major scan thread data", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_major_scan_thread_data);
2174         mono_counters_register ("Major scan alloc_pinned", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_major_scan_alloc_pinned);
2175         mono_counters_register ("Major scan finalized", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_major_scan_finalized);
2176         mono_counters_register ("Major scan big objects", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_major_scan_big_objects);
2177         mono_counters_register ("Major finish gray stack", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_major_finish_gray_stack);
2178         mono_counters_register ("Major free big objects", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_major_free_bigobjs);
2179         mono_counters_register ("Major LOS sweep", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_major_los_sweep);
2180         mono_counters_register ("Major sweep", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_major_sweep);
2181         mono_counters_register ("Major fragment creation", MONO_COUNTER_GC | MONO_COUNTER_TIME_INTERVAL, &time_major_fragment_creation);
2182
2183         mono_counters_register ("Number of pinned objects", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_pinned_objects);
2184
2185 #ifdef HEAVY_STATISTICS
2186         mono_counters_register ("WBarrier set field", MONO_COUNTER_GC | MONO_COUNTER_INT, &stat_wbarrier_set_field);
2187         mono_counters_register ("WBarrier set arrayref", MONO_COUNTER_GC | MONO_COUNTER_INT, &stat_wbarrier_set_arrayref);
2188         mono_counters_register ("WBarrier arrayref copy", MONO_COUNTER_GC | MONO_COUNTER_INT, &stat_wbarrier_arrayref_copy);
2189         mono_counters_register ("WBarrier generic store called", MONO_COUNTER_GC | MONO_COUNTER_INT, &stat_wbarrier_generic_store);
2190         mono_counters_register ("WBarrier set root", MONO_COUNTER_GC | MONO_COUNTER_INT, &stat_wbarrier_set_root);
2191         mono_counters_register ("WBarrier value copy", MONO_COUNTER_GC | MONO_COUNTER_INT, &stat_wbarrier_value_copy);
2192         mono_counters_register ("WBarrier object copy", MONO_COUNTER_GC | MONO_COUNTER_INT, &stat_wbarrier_object_copy);
2193
2194         mono_counters_register ("# objects allocated degraded", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_objects_alloced_degraded);
2195         mono_counters_register ("bytes allocated degraded", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_bytes_alloced_degraded);
2196
2197         mono_counters_register ("# copy_object() called (nursery)", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_copy_object_called_nursery);
2198         mono_counters_register ("# objects copied (nursery)", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_objects_copied_nursery);
2199         mono_counters_register ("# copy_object() called (major)", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_copy_object_called_major);
2200         mono_counters_register ("# objects copied (major)", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_objects_copied_major);
2201
2202         mono_counters_register ("# scan_object() called (nursery)", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_scan_object_called_nursery);
2203         mono_counters_register ("# scan_object() called (major)", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_scan_object_called_major);
2204
2205         mono_counters_register ("Slots allocated in vain", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_slots_allocated_in_vain);
2206
2207         mono_counters_register ("# nursery copy_object() failed from space", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_nursery_copy_object_failed_from_space);
2208         mono_counters_register ("# nursery copy_object() failed forwarded", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_nursery_copy_object_failed_forwarded);
2209         mono_counters_register ("# nursery copy_object() failed pinned", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_nursery_copy_object_failed_pinned);
2210         mono_counters_register ("# nursery copy_object() failed to space", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_nursery_copy_object_failed_to_space);
2211
2212         sgen_nursery_allocator_init_heavy_stats ();
2213         sgen_alloc_init_heavy_stats ();
2214 #endif
2215
2216         inited = TRUE;
2217 }
2218
2219
2220 static void
2221 reset_pinned_from_failed_allocation (void)
2222 {
2223         bytes_pinned_from_failed_allocation = 0;
2224 }
2225
2226 void
2227 sgen_set_pinned_from_failed_allocation (mword objsize)
2228 {
2229         bytes_pinned_from_failed_allocation += objsize;
2230 }
2231
2232 gboolean
2233 sgen_collection_is_parallel (void)
2234 {
2235         switch (current_collection_generation) {
2236         case GENERATION_NURSERY:
2237                 return nursery_collection_is_parallel;
2238         case GENERATION_OLD:
2239                 return major_collector.is_parallel;
2240         default:
2241                 g_error ("Invalid current generation %d", current_collection_generation);
2242         }
2243 }
2244
2245 gboolean
2246 sgen_collection_is_concurrent (void)
2247 {
2248         switch (current_collection_generation) {
2249         case GENERATION_NURSERY:
2250                 return FALSE;
2251         case GENERATION_OLD:
2252                 return major_collector.is_concurrent;
2253         default:
2254                 g_error ("Invalid current generation %d", current_collection_generation);
2255         }
2256 }
2257
2258 gboolean
2259 sgen_concurrent_collection_in_progress (void)
2260 {
2261         return concurrent_collection_in_progress;
2262 }
2263
2264 typedef struct
2265 {
2266         char *heap_start;
2267         char *heap_end;
2268 } FinishRememberedSetScanJobData;
2269
2270 static void
2271 job_finish_remembered_set_scan (WorkerData *worker_data, void *job_data_untyped)
2272 {
2273         FinishRememberedSetScanJobData *job_data = job_data_untyped;
2274
2275         remset.finish_scan_remsets (job_data->heap_start, job_data->heap_end, sgen_workers_get_job_gray_queue (worker_data));
2276         sgen_free_internal_dynamic (job_data, sizeof (FinishRememberedSetScanJobData), INTERNAL_MEM_WORKER_JOB_DATA);
2277 }
2278
2279 typedef struct
2280 {
2281         CopyOrMarkObjectFunc copy_or_mark_func;
2282         ScanObjectFunc scan_func;
2283         char *heap_start;
2284         char *heap_end;
2285         int root_type;
2286 } ScanFromRegisteredRootsJobData;
2287
2288 static void
2289 job_scan_from_registered_roots (WorkerData *worker_data, void *job_data_untyped)
2290 {
2291         ScanFromRegisteredRootsJobData *job_data = job_data_untyped;
2292         ScanCopyContext ctx = { job_data->scan_func, job_data->copy_or_mark_func,
2293                 sgen_workers_get_job_gray_queue (worker_data) };
2294
2295         scan_from_registered_roots (job_data->heap_start, job_data->heap_end, job_data->root_type, ctx);
2296         sgen_free_internal_dynamic (job_data, sizeof (ScanFromRegisteredRootsJobData), INTERNAL_MEM_WORKER_JOB_DATA);
2297 }
2298
2299 typedef struct
2300 {
2301         char *heap_start;
2302         char *heap_end;
2303 } ScanThreadDataJobData;
2304
2305 static void
2306 job_scan_thread_data (WorkerData *worker_data, void *job_data_untyped)
2307 {
2308         ScanThreadDataJobData *job_data = job_data_untyped;
2309
2310         scan_thread_data (job_data->heap_start, job_data->heap_end, TRUE,
2311                         sgen_workers_get_job_gray_queue (worker_data));
2312         sgen_free_internal_dynamic (job_data, sizeof (ScanThreadDataJobData), INTERNAL_MEM_WORKER_JOB_DATA);
2313 }
2314
2315 typedef struct
2316 {
2317         FinalizeReadyEntry *list;
2318 } ScanFinalizerEntriesJobData;
2319
2320 static void
2321 job_scan_finalizer_entries (WorkerData *worker_data, void *job_data_untyped)
2322 {
2323         ScanFinalizerEntriesJobData *job_data = job_data_untyped;
2324         ScanCopyContext ctx = { NULL, current_object_ops.copy_or_mark_object, sgen_workers_get_job_gray_queue (worker_data) };
2325
2326         scan_finalizer_entries (job_data->list, ctx);
2327         sgen_free_internal_dynamic (job_data, sizeof (ScanFinalizerEntriesJobData), INTERNAL_MEM_WORKER_JOB_DATA);
2328 }
2329
2330 static void
2331 job_scan_major_mod_union_cardtable (WorkerData *worker_data, void *job_data_untyped)
2332 {
2333         g_assert (concurrent_collection_in_progress);
2334         major_collector.scan_card_table (TRUE, sgen_workers_get_job_gray_queue (worker_data));
2335 }
2336
2337 static void
2338 job_scan_los_mod_union_cardtable (WorkerData *worker_data, void *job_data_untyped)
2339 {
2340         g_assert (concurrent_collection_in_progress);
2341         sgen_los_scan_card_table (TRUE, sgen_workers_get_job_gray_queue (worker_data));
2342 }
2343
2344 static void
2345 verify_scan_starts (char *start, char *end)
2346 {
2347         int i;
2348
2349         for (i = 0; i < nursery_section->num_scan_start; ++i) {
2350                 char *addr = nursery_section->scan_starts [i];
2351                 if (addr > start && addr < end)
2352                         SGEN_LOG (1, "NFC-BAD SCAN START [%d] %p for obj [%p %p]", i, addr, start, end);
2353         }
2354 }
2355
2356 static void
2357 verify_nursery (void)
2358 {
2359         char *start, *end, *cur, *hole_start;
2360
2361         if (!do_verify_nursery)
2362                 return;
2363
2364         /*This cleans up unused fragments */
2365         sgen_nursery_allocator_prepare_for_pinning ();
2366
2367         hole_start = start = cur = sgen_get_nursery_start ();
2368         end = sgen_get_nursery_end ();
2369
2370         while (cur < end) {
2371                 size_t ss, size;
2372
2373                 if (!*(void**)cur) {
2374                         cur += sizeof (void*);
2375                         continue;
2376                 }
2377
2378                 if (object_is_forwarded (cur))
2379                         SGEN_LOG (1, "FORWARDED OBJ %p", cur);
2380                 else if (object_is_pinned (cur))
2381                         SGEN_LOG (1, "PINNED OBJ %p", cur);
2382
2383                 ss = safe_object_get_size ((MonoObject*)cur);
2384                 size = ALIGN_UP (safe_object_get_size ((MonoObject*)cur));
2385                 verify_scan_starts (cur, cur + size);
2386                 if (do_dump_nursery_content) {
2387                         if (cur > hole_start)
2388                                 SGEN_LOG (1, "HOLE [%p %p %d]", hole_start, cur, (int)(cur - hole_start));
2389                         SGEN_LOG (1, "OBJ  [%p %p %d %d %s %d]", cur, cur + size, (int)size, (int)ss, sgen_safe_name ((MonoObject*)cur), (gpointer)LOAD_VTABLE (cur) == sgen_get_array_fill_vtable ());
2390                 }
2391                 cur += size;
2392                 hole_start = cur;
2393         }
2394 }
2395
2396 /*
2397  * Checks that no objects in the nursery are fowarded or pinned.  This
2398  * is a precondition to restarting the mutator while doing a
2399  * concurrent collection.  Note that we don't clear fragments because
2400  * we depend on that having happened earlier.
2401  */
2402 static void
2403 check_nursery_is_clean (void)
2404 {
2405         char *start, *end, *cur;
2406
2407         start = cur = sgen_get_nursery_start ();
2408         end = sgen_get_nursery_end ();
2409
2410         while (cur < end) {
2411                 size_t ss, size;
2412
2413                 if (!*(void**)cur) {
2414                         cur += sizeof (void*);
2415                         continue;
2416                 }
2417
2418                 g_assert (!object_is_forwarded (cur));
2419                 g_assert (!object_is_pinned (cur));
2420
2421                 ss = safe_object_get_size ((MonoObject*)cur);
2422                 size = ALIGN_UP (safe_object_get_size ((MonoObject*)cur));
2423                 verify_scan_starts (cur, cur + size);
2424
2425                 cur += size;
2426         }
2427 }
2428
2429 static void
2430 init_gray_queue (void)
2431 {
2432         if (sgen_collection_is_parallel () || sgen_collection_is_concurrent ()) {
2433                 sgen_workers_init_distribute_gray_queue ();
2434                 sgen_gray_object_queue_init_with_alloc_prepare (&gray_queue, NULL,
2435                                 gray_queue_redirect, sgen_workers_get_distribute_section_gray_queue ());
2436         } else {
2437                 sgen_gray_object_queue_init (&gray_queue, NULL);
2438         }
2439
2440         if (major_collector.is_concurrent) {
2441                 sgen_gray_object_queue_init_with_alloc_prepare (&remember_major_objects_gray_queue, NULL,
2442                                 gray_queue_redirect, sgen_workers_get_distribute_section_gray_queue ());
2443         } else {
2444                 sgen_gray_object_queue_init_invalid (&remember_major_objects_gray_queue);
2445         }
2446 }
2447
2448 /*
2449  * Collect objects in the nursery.  Returns whether to trigger a major
2450  * collection.
2451  */
2452 static gboolean
2453 collect_nursery (SgenGrayQueue *unpin_queue)
2454 {
2455         gboolean needs_major;
2456         size_t max_garbage_amount;
2457         char *nursery_next;
2458         FinishRememberedSetScanJobData *frssjd;
2459         ScanFromRegisteredRootsJobData *scrrjd_normal, *scrrjd_wbarrier;
2460         ScanFinalizerEntriesJobData *sfejd_fin_ready, *sfejd_critical_fin;
2461         ScanThreadDataJobData *stdjd;
2462         mword fragment_total;
2463         ScanCopyContext ctx;
2464         TV_DECLARE (all_atv);
2465         TV_DECLARE (all_btv);
2466         TV_DECLARE (atv);
2467         TV_DECLARE (btv);
2468
2469         if (disable_minor_collections)
2470                 return TRUE;
2471
2472         MONO_GC_BEGIN (GENERATION_NURSERY);
2473         binary_protocol_collection_begin (stat_minor_gcs, GENERATION_NURSERY);
2474
2475         verify_nursery ();
2476
2477 #ifndef DISABLE_PERFCOUNTERS
2478         mono_perfcounters->gc_collections0++;
2479 #endif
2480
2481         current_collection_generation = GENERATION_NURSERY;
2482         if (sgen_collection_is_parallel ())
2483                 current_object_ops = sgen_minor_collector.parallel_ops;
2484         else
2485                 current_object_ops = sgen_minor_collector.serial_ops;
2486         
2487         reset_pinned_from_failed_allocation ();
2488
2489         check_scan_starts ();
2490
2491         sgen_nursery_alloc_prepare_for_minor ();
2492
2493         degraded_mode = 0;
2494         objects_pinned = 0;
2495         nursery_next = sgen_nursery_alloc_get_upper_alloc_bound ();
2496         /* FIXME: optimize later to use the higher address where an object can be present */
2497         nursery_next = MAX (nursery_next, sgen_get_nursery_end ());
2498
2499         SGEN_LOG (1, "Start nursery collection %d %p-%p, size: %d", stat_minor_gcs, sgen_get_nursery_start (), nursery_next, (int)(nursery_next - sgen_get_nursery_start ()));
2500         max_garbage_amount = nursery_next - sgen_get_nursery_start ();
2501         g_assert (nursery_section->size >= max_garbage_amount);
2502
2503         /* world must be stopped already */
2504         TV_GETTIME (all_atv);
2505         atv = all_atv;
2506
2507         TV_GETTIME (btv);
2508         time_minor_pre_collection_fragment_clear += TV_ELAPSED (atv, btv);
2509
2510         if (xdomain_checks) {
2511                 sgen_clear_nursery_fragments ();
2512                 check_for_xdomain_refs ();
2513         }
2514
2515         nursery_section->next_data = nursery_next;
2516
2517         major_collector.start_nursery_collection ();
2518
2519         sgen_memgov_minor_collection_start ();
2520
2521         init_gray_queue ();
2522
2523         stat_minor_gcs++;
2524         gc_stats.minor_gc_count ++;
2525
2526         if (remset.prepare_for_minor_collection)
2527                 remset.prepare_for_minor_collection ();
2528
2529         MONO_GC_CHECKPOINT_1 (GENERATION_NURSERY);
2530
2531         sgen_process_fin_stage_entries ();
2532         sgen_process_dislink_stage_entries ();
2533
2534         MONO_GC_CHECKPOINT_2 (GENERATION_NURSERY);
2535
2536         /* pin from pinned handles */
2537         sgen_init_pinning ();
2538         mono_profiler_gc_event (MONO_GC_EVENT_MARK_START, 0);
2539         pin_from_roots (sgen_get_nursery_start (), nursery_next, WORKERS_DISTRIBUTE_GRAY_QUEUE);
2540         /* identify pinned objects */
2541         sgen_optimize_pin_queue (0);
2542         sgen_pinning_setup_section (nursery_section);
2543         ctx.scan_func = NULL;
2544         ctx.copy_func = NULL;
2545         ctx.queue = WORKERS_DISTRIBUTE_GRAY_QUEUE;
2546         sgen_pin_objects_in_section (nursery_section, ctx);
2547         sgen_pinning_trim_queue_to_section (nursery_section);
2548
2549         TV_GETTIME (atv);
2550         time_minor_pinning += TV_ELAPSED (btv, atv);
2551         SGEN_LOG (2, "Finding pinned pointers: %d in %d usecs", sgen_get_pinned_count (), TV_ELAPSED (btv, atv));
2552         SGEN_LOG (4, "Start scan with %d pinned objects", sgen_get_pinned_count ());
2553
2554         MONO_GC_CHECKPOINT_3 (GENERATION_NURSERY);
2555
2556         if (whole_heap_check_before_collection) {
2557                 sgen_clear_nursery_fragments ();
2558                 sgen_check_whole_heap ();
2559         }
2560         if (consistency_check_at_minor_collection)
2561                 sgen_check_consistency ();
2562
2563         sgen_workers_start_all_workers ();
2564
2565         /*
2566          * Perform the sequential part of remembered set scanning.
2567          * This usually involves scanning global information that might later be produced by evacuation.
2568          */
2569         if (remset.begin_scan_remsets)
2570                 remset.begin_scan_remsets (sgen_get_nursery_start (), nursery_next, WORKERS_DISTRIBUTE_GRAY_QUEUE);
2571
2572         sgen_workers_start_marking ();
2573
2574         frssjd = sgen_alloc_internal_dynamic (sizeof (FinishRememberedSetScanJobData), INTERNAL_MEM_WORKER_JOB_DATA, TRUE);
2575         frssjd->heap_start = sgen_get_nursery_start ();
2576         frssjd->heap_end = nursery_next;
2577         sgen_workers_enqueue_job (job_finish_remembered_set_scan, frssjd);
2578
2579         /* we don't have complete write barrier yet, so we scan all the old generation sections */
2580         TV_GETTIME (btv);
2581         time_minor_scan_remsets += TV_ELAPSED (atv, btv);
2582         SGEN_LOG (2, "Old generation scan: %d usecs", TV_ELAPSED (atv, btv));
2583
2584         MONO_GC_CHECKPOINT_4 (GENERATION_NURSERY);
2585
2586         if (!sgen_collection_is_parallel ()) {
2587                 ctx.scan_func = current_object_ops.scan_object;
2588                 ctx.copy_func = NULL;
2589                 ctx.queue = &gray_queue;
2590                 sgen_drain_gray_stack (-1, ctx);
2591         }
2592
2593         if (mono_profiler_get_events () & MONO_PROFILE_GC_ROOTS)
2594                 report_registered_roots ();
2595         if (mono_profiler_get_events () & MONO_PROFILE_GC_ROOTS)
2596                 report_finalizer_roots ();
2597         TV_GETTIME (atv);
2598         time_minor_scan_pinned += TV_ELAPSED (btv, atv);
2599
2600         MONO_GC_CHECKPOINT_5 (GENERATION_NURSERY);
2601
2602         /* registered roots, this includes static fields */
2603         scrrjd_normal = sgen_alloc_internal_dynamic (sizeof (ScanFromRegisteredRootsJobData), INTERNAL_MEM_WORKER_JOB_DATA, TRUE);
2604         scrrjd_normal->copy_or_mark_func = current_object_ops.copy_or_mark_object;
2605         scrrjd_normal->scan_func = current_object_ops.scan_object;
2606         scrrjd_normal->heap_start = sgen_get_nursery_start ();
2607         scrrjd_normal->heap_end = nursery_next;
2608         scrrjd_normal->root_type = ROOT_TYPE_NORMAL;
2609         sgen_workers_enqueue_job (job_scan_from_registered_roots, scrrjd_normal);
2610
2611         scrrjd_wbarrier = sgen_alloc_internal_dynamic (sizeof (ScanFromRegisteredRootsJobData), INTERNAL_MEM_WORKER_JOB_DATA, TRUE);
2612         scrrjd_wbarrier->copy_or_mark_func = current_object_ops.copy_or_mark_object;
2613         scrrjd_wbarrier->scan_func = current_object_ops.scan_object;
2614         scrrjd_wbarrier->heap_start = sgen_get_nursery_start ();
2615         scrrjd_wbarrier->heap_end = nursery_next;
2616         scrrjd_wbarrier->root_type = ROOT_TYPE_WBARRIER;
2617         sgen_workers_enqueue_job (job_scan_from_registered_roots, scrrjd_wbarrier);
2618
2619         TV_GETTIME (btv);
2620         time_minor_scan_registered_roots += TV_ELAPSED (atv, btv);
2621
2622         MONO_GC_CHECKPOINT_6 (GENERATION_NURSERY);
2623
2624         /* thread data */
2625         stdjd = sgen_alloc_internal_dynamic (sizeof (ScanThreadDataJobData), INTERNAL_MEM_WORKER_JOB_DATA, TRUE);
2626         stdjd->heap_start = sgen_get_nursery_start ();
2627         stdjd->heap_end = nursery_next;
2628         sgen_workers_enqueue_job (job_scan_thread_data, stdjd);
2629
2630         TV_GETTIME (atv);
2631         time_minor_scan_thread_data += TV_ELAPSED (btv, atv);
2632         btv = atv;
2633
2634         MONO_GC_CHECKPOINT_7 (GENERATION_NURSERY);
2635
2636         g_assert (!sgen_collection_is_parallel () && !sgen_collection_is_concurrent ());
2637
2638         if (sgen_collection_is_parallel () || sgen_collection_is_concurrent ())
2639                 g_assert (sgen_gray_object_queue_is_empty (&gray_queue));
2640
2641         /* Scan the list of objects ready for finalization. If */
2642         sfejd_fin_ready = sgen_alloc_internal_dynamic (sizeof (ScanFinalizerEntriesJobData), INTERNAL_MEM_WORKER_JOB_DATA, TRUE);
2643         sfejd_fin_ready->list = fin_ready_list;
2644         sgen_workers_enqueue_job (job_scan_finalizer_entries, sfejd_fin_ready);
2645
2646         sfejd_critical_fin = sgen_alloc_internal_dynamic (sizeof (ScanFinalizerEntriesJobData), INTERNAL_MEM_WORKER_JOB_DATA, TRUE);
2647         sfejd_critical_fin->list = critical_fin_list;
2648         sgen_workers_enqueue_job (job_scan_finalizer_entries, sfejd_critical_fin);
2649
2650         MONO_GC_CHECKPOINT_8 (GENERATION_NURSERY);
2651
2652         finish_gray_stack (sgen_get_nursery_start (), nursery_next, GENERATION_NURSERY, &gray_queue);
2653         TV_GETTIME (atv);
2654         time_minor_finish_gray_stack += TV_ELAPSED (btv, atv);
2655         mono_profiler_gc_event (MONO_GC_EVENT_MARK_END, 0);
2656
2657         MONO_GC_CHECKPOINT_9 (GENERATION_NURSERY);
2658
2659         /*
2660          * The (single-threaded) finalization code might have done
2661          * some copying/marking so we can only reset the GC thread's
2662          * worker data here instead of earlier when we joined the
2663          * workers.
2664          */
2665         sgen_workers_reset_data ();
2666
2667         if (objects_pinned) {
2668                 sgen_optimize_pin_queue (0);
2669                 sgen_pinning_setup_section (nursery_section);
2670         }
2671
2672         /* walk the pin_queue, build up the fragment list of free memory, unmark
2673          * pinned objects as we go, memzero() the empty fragments so they are ready for the
2674          * next allocations.
2675          */
2676         mono_profiler_gc_event (MONO_GC_EVENT_RECLAIM_START, 0);
2677         fragment_total = sgen_build_nursery_fragments (nursery_section,
2678                         nursery_section->pin_queue_start, nursery_section->pin_queue_num_entries,
2679                         unpin_queue);
2680         if (!fragment_total)
2681                 degraded_mode = 1;
2682
2683         /* Clear TLABs for all threads */
2684         sgen_clear_tlabs ();
2685
2686         mono_profiler_gc_event (MONO_GC_EVENT_RECLAIM_END, 0);
2687         TV_GETTIME (btv);
2688         time_minor_fragment_creation += TV_ELAPSED (atv, btv);
2689         SGEN_LOG (2, "Fragment creation: %d usecs, %lu bytes available", TV_ELAPSED (atv, btv), (unsigned long)fragment_total);
2690
2691         if (consistency_check_at_minor_collection)
2692                 sgen_check_major_refs ();
2693
2694         major_collector.finish_nursery_collection ();
2695
2696         TV_GETTIME (all_btv);
2697         gc_stats.minor_gc_time_usecs += TV_ELAPSED (all_atv, all_btv);
2698
2699         if (heap_dump_file)
2700                 dump_heap ("minor", stat_minor_gcs - 1, NULL);
2701
2702         /* prepare the pin queue for the next collection */
2703         sgen_finish_pinning ();
2704         if (fin_ready_list || critical_fin_list) {
2705                 SGEN_LOG (4, "Finalizer-thread wakeup: ready %d", num_ready_finalizers);
2706                 mono_gc_finalize_notify ();
2707         }
2708         sgen_pin_stats_reset ();
2709
2710         g_assert (sgen_gray_object_queue_is_empty (&gray_queue));
2711
2712         if (remset.finish_minor_collection)
2713                 remset.finish_minor_collection ();
2714
2715         check_scan_starts ();
2716
2717         binary_protocol_flush_buffers (FALSE);
2718
2719         sgen_memgov_minor_collection_end ();
2720
2721         /*objects are late pinned because of lack of memory, so a major is a good call*/
2722         needs_major = objects_pinned > 0;
2723         current_collection_generation = -1;
2724         objects_pinned = 0;
2725
2726         MONO_GC_END (GENERATION_NURSERY);
2727         binary_protocol_collection_end (stat_minor_gcs - 1, GENERATION_NURSERY);
2728
2729         return needs_major;
2730 }
2731
2732 static void
2733 major_copy_or_mark_from_roots (int *old_next_pin_slot, gboolean finish_up_concurrent_mark, gboolean scan_mod_union)
2734 {
2735         LOSObject *bigobj;
2736         TV_DECLARE (atv);
2737         TV_DECLARE (btv);
2738         /* FIXME: only use these values for the precise scan
2739          * note that to_space pointers should be excluded anyway...
2740          */
2741         char *heap_start = NULL;
2742         char *heap_end = (char*)-1;
2743         gboolean profile_roots = mono_profiler_get_events () & MONO_PROFILE_GC_ROOTS;
2744         GCRootReport root_report = { 0 };
2745         ScanFromRegisteredRootsJobData *scrrjd_normal, *scrrjd_wbarrier;
2746         ScanThreadDataJobData *stdjd;
2747         ScanFinalizerEntriesJobData *sfejd_fin_ready, *sfejd_critical_fin;
2748         ScanCopyContext ctx;
2749
2750         if (major_collector.is_concurrent) {
2751                 /*This cleans up unused fragments */
2752                 sgen_nursery_allocator_prepare_for_pinning ();
2753
2754                 if (do_concurrent_checks)
2755                         check_nursery_is_clean ();
2756         } else {
2757                 /* The concurrent collector doesn't touch the nursery. */
2758                 sgen_nursery_alloc_prepare_for_major ();
2759         }
2760
2761         init_gray_queue ();
2762
2763         TV_GETTIME (atv);
2764
2765         /* Pinning depends on this */
2766         sgen_clear_nursery_fragments ();
2767
2768         if (whole_heap_check_before_collection)
2769                 sgen_check_whole_heap ();
2770
2771         TV_GETTIME (btv);
2772         time_major_pre_collection_fragment_clear += TV_ELAPSED (atv, btv);
2773
2774         if (!sgen_collection_is_concurrent ())
2775                 nursery_section->next_data = sgen_get_nursery_end ();
2776         /* we should also coalesce scanning from sections close to each other
2777          * and deal with pointers outside of the sections later.
2778          */
2779
2780         objects_pinned = 0;
2781         *major_collector.have_swept = FALSE;
2782
2783         if (xdomain_checks) {
2784                 sgen_clear_nursery_fragments ();
2785                 check_for_xdomain_refs ();
2786         }
2787
2788         if (!major_collector.is_concurrent) {
2789                 /* Remsets are not useful for a major collection */
2790                 remset.prepare_for_major_collection ();
2791         }
2792
2793         sgen_process_fin_stage_entries ();
2794         sgen_process_dislink_stage_entries ();
2795
2796         TV_GETTIME (atv);
2797         sgen_init_pinning ();
2798         SGEN_LOG (6, "Collecting pinned addresses");
2799         pin_from_roots ((void*)lowest_heap_address, (void*)highest_heap_address, WORKERS_DISTRIBUTE_GRAY_QUEUE);
2800         sgen_optimize_pin_queue (0);
2801
2802         /*
2803          * The concurrent collector doesn't move objects, neither on
2804          * the major heap nor in the nursery, so we can mark even
2805          * before pinning has finished.  For the non-concurrent
2806          * collector we start the workers after pinning.
2807          */
2808         if (major_collector.is_concurrent) {
2809                 sgen_workers_start_all_workers ();
2810                 sgen_workers_start_marking ();
2811         }
2812
2813         /*
2814          * pin_queue now contains all candidate pointers, sorted and
2815          * uniqued.  We must do two passes now to figure out which
2816          * objects are pinned.
2817          *
2818          * The first is to find within the pin_queue the area for each
2819          * section.  This requires that the pin_queue be sorted.  We
2820          * also process the LOS objects and pinned chunks here.
2821          *
2822          * The second, destructive, pass is to reduce the section
2823          * areas to pointers to the actually pinned objects.
2824          */
2825         SGEN_LOG (6, "Pinning from sections");
2826         /* first pass for the sections */
2827         sgen_find_section_pin_queue_start_end (nursery_section);
2828         major_collector.find_pin_queue_start_ends (WORKERS_DISTRIBUTE_GRAY_QUEUE);
2829         /* identify possible pointers to the insize of large objects */
2830         SGEN_LOG (6, "Pinning from large objects");
2831         for (bigobj = los_object_list; bigobj; bigobj = bigobj->next) {
2832                 int dummy;
2833                 if (sgen_find_optimized_pin_queue_area (bigobj->data, (char*)bigobj->data + sgen_los_object_size (bigobj), &dummy)) {
2834                         binary_protocol_pin (bigobj->data, (gpointer)LOAD_VTABLE (bigobj->data), safe_object_get_size (((MonoObject*)(bigobj->data))));
2835                         if (G_UNLIKELY (MONO_GC_OBJ_PINNED_ENABLED ())) {
2836                                 MonoVTable *vt = (MonoVTable*)LOAD_VTABLE (bigobj->data);
2837                                 MONO_GC_OBJ_PINNED ((mword)bigobj->data, sgen_safe_object_get_size ((MonoObject*)bigobj->data), vt->klass->name_space, vt->klass->name, GENERATION_OLD);
2838                         }
2839                         if (sgen_los_object_is_pinned (bigobj->data)) {
2840                                 g_assert (finish_up_concurrent_mark);
2841                                 continue;
2842                         }
2843                         sgen_los_pin_object (bigobj->data);
2844                         /* FIXME: only enqueue if object has references */
2845                         GRAY_OBJECT_ENQUEUE (WORKERS_DISTRIBUTE_GRAY_QUEUE, bigobj->data);
2846                         if (G_UNLIKELY (do_pin_stats))
2847                                 sgen_pin_stats_register_object ((char*) bigobj->data, safe_object_get_size ((MonoObject*) bigobj->data));
2848                         SGEN_LOG (6, "Marked large object %p (%s) size: %lu from roots", bigobj->data, safe_name (bigobj->data), (unsigned long)sgen_los_object_size (bigobj));
2849
2850                         if (profile_roots)
2851                                 add_profile_gc_root (&root_report, bigobj->data, MONO_PROFILE_GC_ROOT_PINNING | MONO_PROFILE_GC_ROOT_MISC, 0);
2852                 }
2853         }
2854         if (profile_roots)
2855                 notify_gc_roots (&root_report);
2856         /* second pass for the sections */
2857         ctx.scan_func = concurrent_collection_in_progress ? current_object_ops.scan_object : NULL;
2858         ctx.copy_func = NULL;
2859         ctx.queue = WORKERS_DISTRIBUTE_GRAY_QUEUE;
2860         sgen_pin_objects_in_section (nursery_section, ctx);
2861         major_collector.pin_objects (WORKERS_DISTRIBUTE_GRAY_QUEUE);
2862         if (old_next_pin_slot)
2863                 *old_next_pin_slot = sgen_get_pinned_count ();
2864
2865         TV_GETTIME (btv);
2866         time_major_pinning += TV_ELAPSED (atv, btv);
2867         SGEN_LOG (2, "Finding pinned pointers: %d in %d usecs", sgen_get_pinned_count (), TV_ELAPSED (atv, btv));
2868         SGEN_LOG (4, "Start scan with %d pinned objects", sgen_get_pinned_count ());
2869
2870         major_collector.init_to_space ();
2871
2872 #ifdef SGEN_DEBUG_INTERNAL_ALLOC
2873         main_gc_thread = mono_native_thread_self ();
2874 #endif
2875
2876         if (!major_collector.is_concurrent) {
2877                 sgen_workers_start_all_workers ();
2878                 sgen_workers_start_marking ();
2879         }
2880
2881         if (mono_profiler_get_events () & MONO_PROFILE_GC_ROOTS)
2882                 report_registered_roots ();
2883         TV_GETTIME (atv);
2884         time_major_scan_pinned += TV_ELAPSED (btv, atv);
2885
2886         /* registered roots, this includes static fields */
2887         scrrjd_normal = sgen_alloc_internal_dynamic (sizeof (ScanFromRegisteredRootsJobData), INTERNAL_MEM_WORKER_JOB_DATA, TRUE);
2888         scrrjd_normal->copy_or_mark_func = current_object_ops.copy_or_mark_object;
2889         scrrjd_normal->scan_func = current_object_ops.scan_object;
2890         scrrjd_normal->heap_start = heap_start;
2891         scrrjd_normal->heap_end = heap_end;
2892         scrrjd_normal->root_type = ROOT_TYPE_NORMAL;
2893         sgen_workers_enqueue_job (job_scan_from_registered_roots, scrrjd_normal);
2894
2895         scrrjd_wbarrier = sgen_alloc_internal_dynamic (sizeof (ScanFromRegisteredRootsJobData), INTERNAL_MEM_WORKER_JOB_DATA, TRUE);
2896         scrrjd_wbarrier->copy_or_mark_func = current_object_ops.copy_or_mark_object;
2897         scrrjd_wbarrier->scan_func = current_object_ops.scan_object;
2898         scrrjd_wbarrier->heap_start = heap_start;
2899         scrrjd_wbarrier->heap_end = heap_end;
2900         scrrjd_wbarrier->root_type = ROOT_TYPE_WBARRIER;
2901         sgen_workers_enqueue_job (job_scan_from_registered_roots, scrrjd_wbarrier);
2902
2903         TV_GETTIME (btv);
2904         time_major_scan_registered_roots += TV_ELAPSED (atv, btv);
2905
2906         /* Threads */
2907         stdjd = sgen_alloc_internal_dynamic (sizeof (ScanThreadDataJobData), INTERNAL_MEM_WORKER_JOB_DATA, TRUE);
2908         stdjd->heap_start = heap_start;
2909         stdjd->heap_end = heap_end;
2910         sgen_workers_enqueue_job (job_scan_thread_data, stdjd);
2911
2912         TV_GETTIME (atv);
2913         time_major_scan_thread_data += TV_ELAPSED (btv, atv);
2914
2915         TV_GETTIME (btv);
2916         time_major_scan_alloc_pinned += TV_ELAPSED (atv, btv);
2917
2918         if (mono_profiler_get_events () & MONO_PROFILE_GC_ROOTS)
2919                 report_finalizer_roots ();
2920
2921         /* scan the list of objects ready for finalization */
2922         sfejd_fin_ready = sgen_alloc_internal_dynamic (sizeof (ScanFinalizerEntriesJobData), INTERNAL_MEM_WORKER_JOB_DATA, TRUE);
2923         sfejd_fin_ready->list = fin_ready_list;
2924         sgen_workers_enqueue_job (job_scan_finalizer_entries, sfejd_fin_ready);
2925
2926         sfejd_critical_fin = sgen_alloc_internal_dynamic (sizeof (ScanFinalizerEntriesJobData), INTERNAL_MEM_WORKER_JOB_DATA, TRUE);
2927         sfejd_critical_fin->list = critical_fin_list;
2928         sgen_workers_enqueue_job (job_scan_finalizer_entries, sfejd_critical_fin);
2929
2930         if (scan_mod_union) {
2931                 g_assert (finish_up_concurrent_mark);
2932
2933                 /* Mod union card table */
2934                 sgen_workers_enqueue_job (job_scan_major_mod_union_cardtable, NULL);
2935                 sgen_workers_enqueue_job (job_scan_los_mod_union_cardtable, NULL);
2936         }
2937
2938         TV_GETTIME (atv);
2939         time_major_scan_finalized += TV_ELAPSED (btv, atv);
2940         SGEN_LOG (2, "Root scan: %d usecs", TV_ELAPSED (btv, atv));
2941
2942         TV_GETTIME (btv);
2943         time_major_scan_big_objects += TV_ELAPSED (atv, btv);
2944
2945         if (major_collector.is_concurrent) {
2946                 /* prepare the pin queue for the next collection */
2947                 sgen_finish_pinning ();
2948
2949                 sgen_pin_stats_reset ();
2950
2951                 if (do_concurrent_checks)
2952                         check_nursery_is_clean ();
2953         }
2954 }
2955
2956 static void
2957 major_start_collection (int *old_next_pin_slot)
2958 {
2959         MONO_GC_BEGIN (GENERATION_OLD);
2960         binary_protocol_collection_begin (stat_major_gcs, GENERATION_OLD);
2961
2962         current_collection_generation = GENERATION_OLD;
2963 #ifndef DISABLE_PERFCOUNTERS
2964         mono_perfcounters->gc_collections1++;
2965 #endif
2966
2967         g_assert (sgen_section_gray_queue_is_empty (sgen_workers_get_distribute_section_gray_queue ()));
2968
2969         if (major_collector.is_concurrent)
2970                 concurrent_collection_in_progress = TRUE;
2971
2972         current_object_ops = major_collector.major_ops;
2973
2974         reset_pinned_from_failed_allocation ();
2975
2976         sgen_memgov_major_collection_start ();
2977
2978         //count_ref_nonref_objs ();
2979         //consistency_check ();
2980
2981         check_scan_starts ();
2982
2983         degraded_mode = 0;
2984         SGEN_LOG (1, "Start major collection %d", stat_major_gcs);
2985         stat_major_gcs++;
2986         gc_stats.major_gc_count ++;
2987
2988         if (major_collector.start_major_collection)
2989                 major_collector.start_major_collection ();
2990
2991         major_copy_or_mark_from_roots (old_next_pin_slot, FALSE, FALSE);
2992 }
2993
2994 static void
2995 wait_for_workers_to_finish (void)
2996 {
2997         g_assert (sgen_gray_object_queue_is_empty (&remember_major_objects_gray_queue));
2998
2999         if (major_collector.is_parallel || major_collector.is_concurrent) {
3000                 gray_queue_redirect (&gray_queue);
3001                 sgen_workers_join ();
3002         }
3003
3004         g_assert (sgen_gray_object_queue_is_empty (&gray_queue));
3005
3006 #ifdef SGEN_DEBUG_INTERNAL_ALLOC
3007         main_gc_thread = NULL;
3008 #endif
3009 }
3010
3011 static void
3012 major_finish_collection (const char *reason, int old_next_pin_slot, gboolean scan_mod_union)
3013 {
3014         LOSObject *bigobj, *prevbo;
3015         TV_DECLARE (atv);
3016         TV_DECLARE (btv);
3017         char *heap_start = NULL;
3018         char *heap_end = (char*)-1;
3019
3020         TV_GETTIME (btv);
3021
3022         if (major_collector.is_concurrent || major_collector.is_parallel)
3023                 wait_for_workers_to_finish ();
3024
3025         current_object_ops = major_collector.major_ops;
3026
3027         if (major_collector.is_concurrent) {
3028                 major_copy_or_mark_from_roots (NULL, TRUE, scan_mod_union);
3029                 wait_for_workers_to_finish ();
3030
3031                 g_assert (sgen_gray_object_queue_is_empty (&gray_queue));
3032
3033                 if (do_concurrent_checks)
3034                         check_nursery_is_clean ();
3035         }
3036
3037         /*
3038          * The workers have stopped so we need to finish gray queue
3039          * work that might result from finalization in the main GC
3040          * thread.  Redirection must therefore be turned off.
3041          */
3042         sgen_gray_object_queue_disable_alloc_prepare (&gray_queue);
3043         g_assert (sgen_section_gray_queue_is_empty (sgen_workers_get_distribute_section_gray_queue ()));
3044
3045         /* all the objects in the heap */
3046         finish_gray_stack (heap_start, heap_end, GENERATION_OLD, &gray_queue);
3047         TV_GETTIME (atv);
3048         time_major_finish_gray_stack += TV_ELAPSED (btv, atv);
3049
3050         /*
3051          * The (single-threaded) finalization code might have done
3052          * some copying/marking so we can only reset the GC thread's
3053          * worker data here instead of earlier when we joined the
3054          * workers.
3055          */
3056         sgen_workers_reset_data ();
3057
3058         if (objects_pinned) {
3059                 g_assert (!major_collector.is_concurrent);
3060
3061                 /*This is slow, but we just OOM'd*/
3062                 sgen_pin_queue_clear_discarded_entries (nursery_section, old_next_pin_slot);
3063                 sgen_optimize_pin_queue (0);
3064                 sgen_find_section_pin_queue_start_end (nursery_section);
3065                 objects_pinned = 0;
3066         }
3067
3068         reset_heap_boundaries ();
3069         sgen_update_heap_boundaries ((mword)sgen_get_nursery_start (), (mword)sgen_get_nursery_end ());
3070
3071         if (check_mark_bits_after_major_collection)
3072                 sgen_check_major_heap_marked ();
3073
3074         MONO_GC_SWEEP_BEGIN (GENERATION_OLD, !major_collector.sweeps_lazily);
3075
3076         /* sweep the big objects list */
3077         prevbo = NULL;
3078         for (bigobj = los_object_list; bigobj;) {
3079                 g_assert (!object_is_pinned (bigobj->data));
3080                 if (sgen_los_object_is_pinned (bigobj->data)) {
3081                         sgen_los_unpin_object (bigobj->data);
3082                         sgen_update_heap_boundaries ((mword)bigobj->data, (mword)bigobj->data + sgen_los_object_size (bigobj));
3083                 } else {
3084                         LOSObject *to_free;
3085                         /* not referenced anywhere, so we can free it */
3086                         if (prevbo)
3087                                 prevbo->next = bigobj->next;
3088                         else
3089                                 los_object_list = bigobj->next;
3090                         to_free = bigobj;
3091                         bigobj = bigobj->next;
3092                         sgen_los_free_object (to_free);
3093                         continue;
3094                 }
3095                 prevbo = bigobj;
3096                 bigobj = bigobj->next;
3097         }
3098
3099         TV_GETTIME (btv);
3100         time_major_free_bigobjs += TV_ELAPSED (atv, btv);
3101
3102         sgen_los_sweep ();
3103
3104         TV_GETTIME (atv);
3105         time_major_los_sweep += TV_ELAPSED (btv, atv);
3106
3107         major_collector.sweep ();
3108
3109         MONO_GC_SWEEP_END (GENERATION_OLD, !major_collector.sweeps_lazily);
3110
3111         TV_GETTIME (btv);
3112         time_major_sweep += TV_ELAPSED (atv, btv);
3113
3114         if (!major_collector.is_concurrent) {
3115                 /* walk the pin_queue, build up the fragment list of free memory, unmark
3116                  * pinned objects as we go, memzero() the empty fragments so they are ready for the
3117                  * next allocations.
3118                  */
3119                 if (!sgen_build_nursery_fragments (nursery_section, nursery_section->pin_queue_start, nursery_section->pin_queue_num_entries, NULL))
3120                         degraded_mode = 1;
3121
3122                 /* prepare the pin queue for the next collection */
3123                 sgen_finish_pinning ();
3124
3125                 /* Clear TLABs for all threads */
3126                 sgen_clear_tlabs ();
3127
3128                 sgen_pin_stats_reset ();
3129         }
3130
3131         TV_GETTIME (atv);
3132         time_major_fragment_creation += TV_ELAPSED (btv, atv);
3133
3134         if (heap_dump_file)
3135                 dump_heap ("major", stat_major_gcs - 1, reason);
3136
3137         if (fin_ready_list || critical_fin_list) {
3138                 SGEN_LOG (4, "Finalizer-thread wakeup: ready %d", num_ready_finalizers);
3139                 mono_gc_finalize_notify ();
3140         }
3141
3142         g_assert (sgen_gray_object_queue_is_empty (&gray_queue));
3143
3144         sgen_memgov_major_collection_end ();
3145         current_collection_generation = -1;
3146
3147         major_collector.finish_major_collection ();
3148
3149         g_assert (sgen_section_gray_queue_is_empty (sgen_workers_get_distribute_section_gray_queue ()));
3150
3151         if (major_collector.is_concurrent)
3152                 concurrent_collection_in_progress = FALSE;
3153
3154         check_scan_starts ();
3155
3156         binary_protocol_flush_buffers (FALSE);
3157
3158         //consistency_check ();
3159
3160         MONO_GC_END (GENERATION_OLD);
3161         binary_protocol_collection_end (stat_major_gcs - 1, GENERATION_OLD);
3162 }
3163
3164 static gboolean
3165 major_do_collection (const char *reason)
3166 {
3167         TV_DECLARE (all_atv);
3168         TV_DECLARE (all_btv);
3169         int old_next_pin_slot;
3170
3171         if (major_collector.get_and_reset_num_major_objects_marked) {
3172                 long long num_marked = major_collector.get_and_reset_num_major_objects_marked ();
3173                 g_assert (!num_marked);
3174         }
3175
3176         /* world must be stopped already */
3177         TV_GETTIME (all_atv);
3178
3179         major_start_collection (&old_next_pin_slot);
3180         major_finish_collection (reason, old_next_pin_slot, FALSE);
3181
3182         TV_GETTIME (all_btv);
3183         gc_stats.major_gc_time_usecs += TV_ELAPSED (all_atv, all_btv);
3184
3185         /* FIXME: also report this to the user, preferably in gc-end. */
3186         if (major_collector.get_and_reset_num_major_objects_marked)
3187                 major_collector.get_and_reset_num_major_objects_marked ();
3188
3189         return bytes_pinned_from_failed_allocation > 0;
3190 }
3191
3192 static gboolean major_do_collection (const char *reason);
3193
3194 static void
3195 major_start_concurrent_collection (const char *reason)
3196 {
3197         long long num_objects_marked = major_collector.get_and_reset_num_major_objects_marked ();
3198
3199         g_assert (num_objects_marked == 0);
3200
3201         MONO_GC_CONCURRENT_START_BEGIN (GENERATION_OLD);
3202
3203         // FIXME: store reason and pass it when finishing
3204         major_start_collection (NULL);
3205
3206         gray_queue_redirect (&gray_queue);
3207         sgen_workers_wait_for_jobs ();
3208
3209         num_objects_marked = major_collector.get_and_reset_num_major_objects_marked ();
3210         MONO_GC_CONCURRENT_START_END (GENERATION_OLD, num_objects_marked);
3211
3212         current_collection_generation = -1;
3213 }
3214
3215 static gboolean
3216 major_update_or_finish_concurrent_collection (gboolean force_finish)
3217 {
3218         SgenGrayQueue unpin_queue;
3219         memset (&unpin_queue, 0, sizeof (unpin_queue));
3220
3221         MONO_GC_CONCURRENT_UPDATE_FINISH_BEGIN (GENERATION_OLD, major_collector.get_and_reset_num_major_objects_marked ());
3222
3223         g_assert (sgen_gray_object_queue_is_empty (&gray_queue));
3224         if (!have_non_collection_major_object_remembers)
3225                 g_assert (sgen_gray_object_queue_is_empty (&remember_major_objects_gray_queue));
3226
3227         major_collector.update_cardtable_mod_union ();
3228         sgen_los_update_cardtable_mod_union ();
3229
3230         if (!force_finish && !sgen_workers_all_done ()) {
3231                 MONO_GC_CONCURRENT_UPDATE_END (GENERATION_OLD, major_collector.get_and_reset_num_major_objects_marked ());
3232                 return FALSE;
3233         }
3234
3235         collect_nursery (&unpin_queue);
3236         redirect_major_object_remembers ();
3237
3238         current_collection_generation = GENERATION_OLD;
3239         major_finish_collection ("finishing", -1, TRUE);
3240
3241         unpin_objects_from_queue (&unpin_queue);
3242         sgen_gray_object_queue_deinit (&unpin_queue);
3243
3244         MONO_GC_CONCURRENT_FINISH_END (GENERATION_OLD, major_collector.get_and_reset_num_major_objects_marked ());
3245
3246         current_collection_generation = -1;
3247
3248         if (whole_heap_check_before_collection)
3249                 sgen_check_whole_heap ();
3250
3251         return TRUE;
3252 }
3253
3254 /*
3255  * Ensure an allocation request for @size will succeed by freeing enough memory.
3256  *
3257  * LOCKING: The GC lock MUST be held.
3258  */
3259 void
3260 sgen_ensure_free_space (size_t size)
3261 {
3262         int generation_to_collect = -1;
3263         const char *reason = NULL;
3264
3265
3266         if (size > SGEN_MAX_SMALL_OBJ_SIZE) {
3267                 if (sgen_need_major_collection (size)) {
3268                         reason = "LOS overflow";
3269                         generation_to_collect = GENERATION_OLD;
3270                 }
3271         } else {
3272                 if (degraded_mode) {
3273                         if (sgen_need_major_collection (size)) {
3274                                 reason = "Degraded mode overflow";
3275                                 generation_to_collect = GENERATION_OLD;
3276                         }
3277                 } else if (sgen_need_major_collection (size)) {
3278                         reason = "Minor allowance";
3279                         generation_to_collect = GENERATION_OLD;
3280                 } else {
3281                         generation_to_collect = GENERATION_NURSERY;
3282                         reason = "Nursery full";                        
3283                 }
3284         }
3285
3286         if (generation_to_collect == -1)
3287                 return;
3288         sgen_perform_collection (size, generation_to_collect, reason, FALSE);
3289 }
3290
3291 void
3292 sgen_perform_collection (size_t requested_size, int generation_to_collect, const char *reason, gboolean wait_to_finish)
3293 {
3294         TV_DECLARE (gc_end);
3295         GGTimingInfo infos [2];
3296         int overflow_generation_to_collect = -1;
3297         int oldest_generation_collected = generation_to_collect;
3298         const char *overflow_reason = NULL;
3299
3300         g_assert (generation_to_collect == GENERATION_NURSERY || generation_to_collect == GENERATION_OLD);
3301
3302         if (have_non_collection_major_object_remembers) {
3303                 g_assert (concurrent_collection_in_progress);
3304                 redirect_major_object_remembers ();
3305         }
3306
3307         memset (infos, 0, sizeof (infos));
3308         mono_profiler_gc_event (MONO_GC_EVENT_START, generation_to_collect);
3309
3310         infos [0].generation = generation_to_collect;
3311         infos [0].reason = reason;
3312         infos [0].is_overflow = FALSE;
3313         TV_GETTIME (infos [0].total_time);
3314         infos [1].generation = -1;
3315
3316         sgen_stop_world (generation_to_collect);
3317
3318         if (concurrent_collection_in_progress) {
3319                 if (major_update_or_finish_concurrent_collection (wait_to_finish && generation_to_collect == GENERATION_OLD)) {
3320                         oldest_generation_collected = GENERATION_OLD;
3321                         goto done;
3322                 }
3323                 if (generation_to_collect == GENERATION_OLD)
3324                         goto done;
3325         }
3326
3327         //FIXME extract overflow reason
3328         if (generation_to_collect == GENERATION_NURSERY) {
3329                 if (collect_nursery (NULL)) {
3330                         overflow_generation_to_collect = GENERATION_OLD;
3331                         overflow_reason = "Minor overflow";
3332                 }
3333                 if (concurrent_collection_in_progress) {
3334                         redirect_major_object_remembers ();
3335                         sgen_workers_wake_up_all ();
3336                 }
3337         } else {
3338                 SgenGrayQueue unpin_queue;
3339                 SgenGrayQueue *unpin_queue_ptr;
3340                 memset (&unpin_queue, 0, sizeof (unpin_queue));
3341
3342                 if (major_collector.is_concurrent && wait_to_finish)
3343                         unpin_queue_ptr = &unpin_queue;
3344                 else
3345                         unpin_queue_ptr = NULL;
3346
3347                 if (major_collector.is_concurrent) {
3348                         g_assert (!concurrent_collection_in_progress);
3349                         collect_nursery (unpin_queue_ptr);
3350                 }
3351
3352                 if (major_collector.is_concurrent && !wait_to_finish) {
3353                         major_start_concurrent_collection (reason);
3354                         // FIXME: set infos[0] properly
3355                         goto done;
3356                 } else {
3357                         if (major_do_collection (reason)) {
3358                                 overflow_generation_to_collect = GENERATION_NURSERY;
3359                                 overflow_reason = "Excessive pinning";
3360                         }
3361                 }
3362
3363                 if (unpin_queue_ptr) {
3364                         unpin_objects_from_queue (unpin_queue_ptr);
3365                         sgen_gray_object_queue_deinit (unpin_queue_ptr);
3366                 }
3367         }
3368
3369         TV_GETTIME (gc_end);
3370         infos [0].total_time = SGEN_TV_ELAPSED (infos [0].total_time, gc_end);
3371
3372
3373         if (!major_collector.is_concurrent && overflow_generation_to_collect != -1) {
3374                 mono_profiler_gc_event (MONO_GC_EVENT_START, overflow_generation_to_collect);
3375                 infos [1].generation = overflow_generation_to_collect;
3376                 infos [1].reason = overflow_reason;
3377                 infos [1].is_overflow = TRUE;
3378                 infos [1].total_time = gc_end;
3379
3380                 if (overflow_generation_to_collect == GENERATION_NURSERY)
3381                         collect_nursery (NULL);
3382                 else
3383                         major_do_collection (overflow_reason);
3384
3385                 TV_GETTIME (gc_end);
3386                 infos [1].total_time = SGEN_TV_ELAPSED (infos [1].total_time, gc_end);
3387
3388                 /* keep events symmetric */
3389                 mono_profiler_gc_event (MONO_GC_EVENT_END, overflow_generation_to_collect);
3390
3391                 oldest_generation_collected = MAX (oldest_generation_collected, overflow_generation_to_collect);
3392         }
3393
3394         SGEN_LOG (2, "Heap size: %lu, LOS size: %lu", (unsigned long)mono_gc_get_heap_size (), (unsigned long)los_memory_usage);
3395
3396         /* this also sets the proper pointers for the next allocation */
3397         if (generation_to_collect == GENERATION_NURSERY && !sgen_can_alloc_size (requested_size)) {
3398                 /* TypeBuilder and MonoMethod are killing mcs with fragmentation */
3399                 SGEN_LOG (1, "nursery collection didn't find enough room for %zd alloc (%d pinned)", requested_size, sgen_get_pinned_count ());
3400                 sgen_dump_pin_queue ();
3401                 degraded_mode = 1;
3402         }
3403
3404  done:
3405         g_assert (sgen_gray_object_queue_is_empty (&gray_queue));
3406         g_assert (sgen_gray_object_queue_is_empty (&remember_major_objects_gray_queue));
3407
3408         sgen_restart_world (oldest_generation_collected, infos);
3409
3410         mono_profiler_gc_event (MONO_GC_EVENT_END, generation_to_collect);
3411 }
3412
3413 /*
3414  * ######################################################################
3415  * ########  Memory allocation from the OS
3416  * ######################################################################
3417  * This section of code deals with getting memory from the OS and
3418  * allocating memory for GC-internal data structures.
3419  * Internal memory can be handled with a freelist for small objects.
3420  */
3421
3422 /*
3423  * Debug reporting.
3424  */
3425 G_GNUC_UNUSED static void
3426 report_internal_mem_usage (void)
3427 {
3428         printf ("Internal memory usage:\n");
3429         sgen_report_internal_mem_usage ();
3430         printf ("Pinned memory usage:\n");
3431         major_collector.report_pinned_memory_usage ();
3432 }
3433
3434 /*
3435  * ######################################################################
3436  * ########  Finalization support
3437  * ######################################################################
3438  */
3439
3440 /*
3441  * If the object has been forwarded it means it's still referenced from a root. 
3442  * If it is pinned it's still alive as well.
3443  * A LOS object is only alive if we have pinned it.
3444  * Return TRUE if @obj is ready to be finalized.
3445  */
3446 static inline gboolean
3447 sgen_is_object_alive (void *object)
3448 {
3449         mword objsize;
3450
3451         if (ptr_in_nursery (object))
3452                 return sgen_nursery_is_object_alive (object);
3453         /* Oldgen objects can be pinned and forwarded too */
3454         if (SGEN_OBJECT_IS_PINNED (object) || SGEN_OBJECT_IS_FORWARDED (object))
3455                 return TRUE;
3456
3457         /*
3458          * FIXME: major_collector.is_object_live() also calculates the
3459          * size.  Avoid the double calculation.
3460          */
3461         objsize = SGEN_ALIGN_UP (sgen_safe_object_get_size ((MonoObject*)object));
3462         if (objsize > SGEN_MAX_SMALL_OBJ_SIZE)
3463                 return sgen_los_object_is_pinned (object);
3464
3465         return major_collector.is_object_live (object);
3466 }
3467
3468 gboolean
3469 sgen_gc_is_object_ready_for_finalization (void *object)
3470 {
3471         return !sgen_is_object_alive (object);
3472 }
3473
3474 static gboolean
3475 has_critical_finalizer (MonoObject *obj)
3476 {
3477         MonoClass *class;
3478
3479         if (!mono_defaults.critical_finalizer_object)
3480                 return FALSE;
3481
3482         class = ((MonoVTable*)LOAD_VTABLE (obj))->klass;
3483
3484         return mono_class_has_parent_fast (class, mono_defaults.critical_finalizer_object);
3485 }
3486
3487 void
3488 sgen_queue_finalization_entry (MonoObject *obj)
3489 {
3490         FinalizeReadyEntry *entry = sgen_alloc_internal (INTERNAL_MEM_FINALIZE_READY_ENTRY);
3491         gboolean critical = has_critical_finalizer (obj);
3492         entry->object = obj;
3493         if (critical) {
3494                 entry->next = critical_fin_list;
3495                 critical_fin_list = entry;
3496         } else {
3497                 entry->next = fin_ready_list;
3498                 fin_ready_list = entry;
3499         }
3500
3501         if (G_UNLIKELY (MONO_GC_FINALIZE_ENQUEUE_ENABLED ())) {
3502                 int gen = sgen_ptr_in_nursery (obj) ? GENERATION_NURSERY : GENERATION_OLD;
3503                 MonoVTable *vt = (MonoVTable*)LOAD_VTABLE (obj);
3504                 MONO_GC_FINALIZE_ENQUEUE ((mword)obj, sgen_safe_object_get_size (obj),
3505                                 vt->klass->name_space, vt->klass->name, gen, critical);
3506         }
3507 }
3508
3509 static inline int
3510 object_is_reachable (char *object, char *start, char *end)
3511 {
3512         /*This happens for non nursery objects during minor collections. We just treat all objects as alive.*/
3513         if (object < start || object >= end)
3514                 return TRUE;
3515
3516         return sgen_is_object_alive (object);
3517 }
3518
3519 gboolean
3520 sgen_object_is_live (void *obj)
3521 {
3522         if (ptr_in_nursery (obj))
3523                 return object_is_pinned (obj);
3524         /* FIXME This is semantically wrong! All tenured object are considered alive during a nursery collection. */
3525         if (current_collection_generation == GENERATION_NURSERY)
3526                 return FALSE;
3527         return major_collector.is_object_live (obj);
3528 }
3529
3530 /* LOCKING: requires that the GC lock is held */
3531 static void
3532 null_ephemerons_for_domain (MonoDomain *domain)
3533 {
3534         EphemeronLinkNode *current = ephemeron_list, *prev = NULL;
3535
3536         while (current) {
3537                 MonoObject *object = (MonoObject*)current->array;
3538
3539                 if (object && !object->vtable) {
3540                         EphemeronLinkNode *tmp = current;
3541
3542                         if (prev)
3543                                 prev->next = current->next;
3544                         else
3545                                 ephemeron_list = current->next;
3546
3547                         current = current->next;
3548                         sgen_free_internal (tmp, INTERNAL_MEM_EPHEMERON_LINK);
3549                 } else {
3550                         prev = current;
3551                         current = current->next;
3552                 }
3553         }
3554 }
3555
3556 /* LOCKING: requires that the GC lock is held */
3557 static void
3558 clear_unreachable_ephemerons (char *start, char *end, ScanCopyContext ctx)
3559 {
3560         CopyOrMarkObjectFunc copy_func = ctx.copy_func;
3561         GrayQueue *queue = ctx.queue;
3562         int was_in_nursery, was_promoted;
3563         EphemeronLinkNode *current = ephemeron_list, *prev = NULL;
3564         MonoArray *array;
3565         Ephemeron *cur, *array_end;
3566         char *tombstone;
3567
3568         while (current) {
3569                 char *object = current->array;
3570
3571                 if (!object_is_reachable (object, start, end)) {
3572                         EphemeronLinkNode *tmp = current;
3573
3574                         SGEN_LOG (5, "Dead Ephemeron array at %p", object);
3575
3576                         if (prev)
3577                                 prev->next = current->next;
3578                         else
3579                                 ephemeron_list = current->next;
3580
3581                         current = current->next;
3582                         sgen_free_internal (tmp, INTERNAL_MEM_EPHEMERON_LINK);
3583
3584                         continue;
3585                 }
3586
3587                 was_in_nursery = ptr_in_nursery (object);
3588                 copy_func ((void**)&object, queue);
3589                 current->array = object;
3590
3591                 /*The array was promoted, add global remsets for key/values left behind in nursery.*/
3592                 was_promoted = was_in_nursery && !ptr_in_nursery (object);
3593
3594                 SGEN_LOG (5, "Clearing unreachable entries for ephemeron array at %p", object);
3595
3596                 array = (MonoArray*)object;
3597                 cur = mono_array_addr (array, Ephemeron, 0);
3598                 array_end = cur + mono_array_length_fast (array);
3599                 tombstone = (char*)((MonoVTable*)LOAD_VTABLE (object))->domain->ephemeron_tombstone;
3600
3601                 for (; cur < array_end; ++cur) {
3602                         char *key = (char*)cur->key;
3603
3604                         if (!key || key == tombstone)
3605                                 continue;
3606
3607                         SGEN_LOG (5, "[%td] key %p (%s) value %p (%s)", cur - mono_array_addr (array, Ephemeron, 0),
3608                                 key, object_is_reachable (key, start, end) ? "reachable" : "unreachable",
3609                                 cur->value, cur->value && object_is_reachable (cur->value, start, end) ? "reachable" : "unreachable");
3610
3611                         if (!object_is_reachable (key, start, end)) {
3612                                 cur->key = tombstone;
3613                                 cur->value = NULL;
3614                                 continue;
3615                         }
3616
3617                         if (was_promoted) {
3618                                 gpointer value = cur->value;
3619                                 if (ptr_in_nursery (key)) {/*key was not promoted*/
3620                                         SGEN_LOG (5, "\tAdded remset to key %p", key);
3621                                         sgen_add_to_global_remset (&cur->key, key);
3622                                 }
3623                                 if (ptr_in_nursery (value)) {/*value was not promoted*/
3624                                         SGEN_LOG (5, "\tAdded remset to value %p", cur->value);
3625                                         sgen_add_to_global_remset (&cur->value, value);
3626                                 }
3627                         }
3628                 }
3629                 prev = current;
3630                 current = current->next;
3631         }
3632 }
3633
3634 /* LOCKING: requires that the GC lock is held */
3635 static int
3636 mark_ephemerons_in_range (char *start, char *end, ScanCopyContext ctx)
3637 {
3638         CopyOrMarkObjectFunc copy_func = ctx.copy_func;
3639         GrayQueue *queue = ctx.queue;
3640         int nothing_marked = 1;
3641         EphemeronLinkNode *current = ephemeron_list;
3642         MonoArray *array;
3643         Ephemeron *cur, *array_end;
3644         char *tombstone;
3645
3646         for (current = ephemeron_list; current; current = current->next) {
3647                 char *object = current->array;
3648                 SGEN_LOG (5, "Ephemeron array at %p", object);
3649
3650                 /*
3651                 For now we process all ephemerons during all collections.
3652                 Ideally we should use remset information to partially scan those
3653                 arrays.
3654                 We already emit write barriers for Ephemeron fields, it's
3655                 just that we don't process them.
3656                 */
3657                 /*if (object < start || object >= end)
3658                         continue;*/
3659
3660                 /*It has to be alive*/
3661                 if (!object_is_reachable (object, start, end)) {
3662                         SGEN_LOG (5, "\tnot reachable");
3663                         continue;
3664                 }
3665
3666                 copy_func ((void**)&object, queue);
3667
3668                 array = (MonoArray*)object;
3669                 cur = mono_array_addr (array, Ephemeron, 0);
3670                 array_end = cur + mono_array_length_fast (array);
3671                 tombstone = (char*)((MonoVTable*)LOAD_VTABLE (object))->domain->ephemeron_tombstone;
3672
3673                 for (; cur < array_end; ++cur) {
3674                         char *key = cur->key;
3675
3676                         if (!key || key == tombstone)
3677                                 continue;
3678
3679                         SGEN_LOG (5, "[%td] key %p (%s) value %p (%s)", cur - mono_array_addr (array, Ephemeron, 0),
3680                                 key, object_is_reachable (key, start, end) ? "reachable" : "unreachable",
3681                                 cur->value, cur->value && object_is_reachable (cur->value, start, end) ? "reachable" : "unreachable");
3682
3683                         if (object_is_reachable (key, start, end)) {
3684                                 char *value = cur->value;
3685
3686                                 copy_func ((void**)&cur->key, queue);
3687                                 if (value) {
3688                                         if (!object_is_reachable (value, start, end))
3689                                                 nothing_marked = 0;
3690                                         copy_func ((void**)&cur->value, queue);
3691                                 }
3692                         }
3693                 }
3694         }
3695
3696         SGEN_LOG (5, "Ephemeron run finished. Is it done %d", nothing_marked);
3697         return nothing_marked;
3698 }
3699
3700 int
3701 mono_gc_invoke_finalizers (void)
3702 {
3703         FinalizeReadyEntry *entry = NULL;
3704         gboolean entry_is_critical = FALSE;
3705         int count = 0;
3706         void *obj;
3707         /* FIXME: batch to reduce lock contention */
3708         while (fin_ready_list || critical_fin_list) {
3709                 LOCK_GC;
3710
3711                 if (entry) {
3712                         FinalizeReadyEntry **list = entry_is_critical ? &critical_fin_list : &fin_ready_list;
3713
3714                         /* We have finalized entry in the last
3715                            interation, now we need to remove it from
3716                            the list. */
3717                         if (*list == entry)
3718                                 *list = entry->next;
3719                         else {
3720                                 FinalizeReadyEntry *e = *list;
3721                                 while (e->next != entry)
3722                                         e = e->next;
3723                                 e->next = entry->next;
3724                         }
3725                         sgen_free_internal (entry, INTERNAL_MEM_FINALIZE_READY_ENTRY);
3726                         entry = NULL;
3727                 }
3728
3729                 /* Now look for the first non-null entry. */
3730                 for (entry = fin_ready_list; entry && !entry->object; entry = entry->next)
3731                         ;
3732                 if (entry) {
3733                         entry_is_critical = FALSE;
3734                 } else {
3735                         entry_is_critical = TRUE;
3736                         for (entry = critical_fin_list; entry && !entry->object; entry = entry->next)
3737                                 ;
3738                 }
3739
3740                 if (entry) {
3741                         g_assert (entry->object);
3742                         num_ready_finalizers--;
3743                         obj = entry->object;
3744                         entry->object = NULL;
3745                         SGEN_LOG (7, "Finalizing object %p (%s)", obj, safe_name (obj));
3746                 }
3747
3748                 UNLOCK_GC;
3749
3750                 if (!entry)
3751                         break;
3752
3753                 g_assert (entry->object == NULL);
3754                 count++;
3755                 /* the object is on the stack so it is pinned */
3756                 /*g_print ("Calling finalizer for object: %p (%s)\n", entry->object, safe_name (entry->object));*/
3757                 mono_gc_run_finalize (obj, NULL);
3758         }
3759         g_assert (!entry);
3760         return count;
3761 }
3762
3763 gboolean
3764 mono_gc_pending_finalizers (void)
3765 {
3766         return fin_ready_list || critical_fin_list;
3767 }
3768
3769 /*
3770  * ######################################################################
3771  * ########  registered roots support
3772  * ######################################################################
3773  */
3774
3775 /*
3776  * We do not coalesce roots.
3777  */
3778 static int
3779 mono_gc_register_root_inner (char *start, size_t size, void *descr, int root_type)
3780 {
3781         RootRecord new_root;
3782         int i;
3783         LOCK_GC;
3784         for (i = 0; i < ROOT_TYPE_NUM; ++i) {
3785                 RootRecord *root = sgen_hash_table_lookup (&roots_hash [i], start);
3786                 /* we allow changing the size and the descriptor (for thread statics etc) */
3787                 if (root) {
3788                         size_t old_size = root->end_root - start;
3789                         root->end_root = start + size;
3790                         g_assert (((root->root_desc != 0) && (descr != NULL)) ||
3791                                           ((root->root_desc == 0) && (descr == NULL)));
3792                         root->root_desc = (mword)descr;
3793                         roots_size += size;
3794                         roots_size -= old_size;
3795                         UNLOCK_GC;
3796                         return TRUE;
3797                 }
3798         }
3799
3800         new_root.end_root = start + size;
3801         new_root.root_desc = (mword)descr;
3802
3803         sgen_hash_table_replace (&roots_hash [root_type], start, &new_root, NULL);
3804         roots_size += size;
3805
3806         SGEN_LOG (3, "Added root for range: %p-%p, descr: %p  (%d/%d bytes)", start, new_root.end_root, descr, (int)size, (int)roots_size);
3807
3808         UNLOCK_GC;
3809         return TRUE;
3810 }
3811
3812 int
3813 mono_gc_register_root (char *start, size_t size, void *descr)
3814 {
3815         return mono_gc_register_root_inner (start, size, descr, descr ? ROOT_TYPE_NORMAL : ROOT_TYPE_PINNED);
3816 }
3817
3818 int
3819 mono_gc_register_root_wbarrier (char *start, size_t size, void *descr)
3820 {
3821         return mono_gc_register_root_inner (start, size, descr, ROOT_TYPE_WBARRIER);
3822 }
3823
3824 void
3825 mono_gc_deregister_root (char* addr)
3826 {
3827         int root_type;
3828         RootRecord root;
3829
3830         LOCK_GC;
3831         for (root_type = 0; root_type < ROOT_TYPE_NUM; ++root_type) {
3832                 if (sgen_hash_table_remove (&roots_hash [root_type], addr, &root))
3833                         roots_size -= (root.end_root - addr);
3834         }
3835         UNLOCK_GC;
3836 }
3837
3838 /*
3839  * ######################################################################
3840  * ########  Thread handling (stop/start code)
3841  * ######################################################################
3842  */
3843
3844 unsigned int sgen_global_stop_count = 0;
3845
3846 void
3847 sgen_fill_thread_info_for_suspend (SgenThreadInfo *info)
3848 {
3849         if (remset.fill_thread_info_for_suspend)
3850                 remset.fill_thread_info_for_suspend (info);
3851 }
3852
3853 int
3854 sgen_get_current_collection_generation (void)
3855 {
3856         return current_collection_generation;
3857 }
3858
3859 void
3860 mono_gc_set_gc_callbacks (MonoGCCallbacks *callbacks)
3861 {
3862         gc_callbacks = *callbacks;
3863 }
3864
3865 MonoGCCallbacks *
3866 mono_gc_get_gc_callbacks ()
3867 {
3868         return &gc_callbacks;
3869 }
3870
3871 /* Variables holding start/end nursery so it won't have to be passed at every call */
3872 static void *scan_area_arg_start, *scan_area_arg_end;
3873
3874 void
3875 mono_gc_conservatively_scan_area (void *start, void *end)
3876 {
3877         conservatively_pin_objects_from (start, end, scan_area_arg_start, scan_area_arg_end, PIN_TYPE_STACK);
3878 }
3879
3880 void*
3881 mono_gc_scan_object (void *obj)
3882 {
3883         UserCopyOrMarkData *data = mono_native_tls_get_value (user_copy_or_mark_key);
3884         current_object_ops.copy_or_mark_object (&obj, data->queue);
3885         return obj;
3886 }
3887
3888 /*
3889  * Mark from thread stacks and registers.
3890  */
3891 static void
3892 scan_thread_data (void *start_nursery, void *end_nursery, gboolean precise, GrayQueue *queue)
3893 {
3894         SgenThreadInfo *info;
3895
3896         scan_area_arg_start = start_nursery;
3897         scan_area_arg_end = end_nursery;
3898
3899         FOREACH_THREAD (info) {
3900                 if (info->skip) {
3901                         SGEN_LOG (3, "Skipping dead thread %p, range: %p-%p, size: %td", info, info->stack_start, info->stack_end, (char*)info->stack_end - (char*)info->stack_start);
3902                         continue;
3903                 }
3904                 if (info->gc_disabled) {
3905                         SGEN_LOG (3, "GC disabled for thread %p, range: %p-%p, size: %td", info, info->stack_start, info->stack_end, (char*)info->stack_end - (char*)info->stack_start);
3906                         continue;
3907                 }
3908
3909                 if (!info->joined_stw) {
3910                         SGEN_LOG (3, "Skipping thread not seen in STW %p, range: %p-%p, size: %td", info, info->stack_start, info->stack_end, (char*)info->stack_end - (char*)info->stack_start);
3911                         continue;
3912                 }
3913                 
3914                 SGEN_LOG (3, "Scanning thread %p, range: %p-%p, size: %td, pinned=%d", info, info->stack_start, info->stack_end, (char*)info->stack_end - (char*)info->stack_start, sgen_get_pinned_count ());
3915                 if (!info->thread_is_dying) {
3916                         if (gc_callbacks.thread_mark_func && !conservative_stack_mark) {
3917                                 UserCopyOrMarkData data = { NULL, queue };
3918                                 set_user_copy_or_mark_data (&data);
3919                                 gc_callbacks.thread_mark_func (info->runtime_data, info->stack_start, info->stack_end, precise);
3920                                 set_user_copy_or_mark_data (NULL);
3921                         } else if (!precise) {
3922                                 if (!conservative_stack_mark) {
3923                                         fprintf (stderr, "Precise stack mark not supported - disabling.\n");
3924                                         conservative_stack_mark = TRUE;
3925                                 }
3926                                 conservatively_pin_objects_from (info->stack_start, info->stack_end, start_nursery, end_nursery, PIN_TYPE_STACK);
3927                         }
3928                 }
3929
3930                 if (!info->thread_is_dying && !precise) {
3931 #ifdef USE_MONO_CTX
3932                         conservatively_pin_objects_from ((void**)&info->ctx, (void**)&info->ctx + ARCH_NUM_REGS,
3933                                 start_nursery, end_nursery, PIN_TYPE_STACK);
3934 #else
3935                         conservatively_pin_objects_from (&info->regs, &info->regs + ARCH_NUM_REGS,
3936                                         start_nursery, end_nursery, PIN_TYPE_STACK);
3937 #endif
3938                 }
3939         } END_FOREACH_THREAD
3940 }
3941
3942 static gboolean
3943 ptr_on_stack (void *ptr)
3944 {
3945         gpointer stack_start = &stack_start;
3946         SgenThreadInfo *info = mono_thread_info_current ();
3947
3948         if (ptr >= stack_start && ptr < (gpointer)info->stack_end)
3949                 return TRUE;
3950         return FALSE;
3951 }
3952
3953 static void*
3954 sgen_thread_register (SgenThreadInfo* info, void *addr)
3955 {
3956 #ifndef HAVE_KW_THREAD
3957         SgenThreadInfo *__thread_info__ = info;
3958 #endif
3959
3960         LOCK_GC;
3961 #ifndef HAVE_KW_THREAD
3962         info->tlab_start = info->tlab_next = info->tlab_temp_end = info->tlab_real_end = NULL;
3963
3964         g_assert (!mono_native_tls_get_value (thread_info_key));
3965         mono_native_tls_set_value (thread_info_key, info);
3966 #else
3967         sgen_thread_info = info;
3968 #endif
3969
3970 #if !defined(__MACH__)
3971         info->stop_count = -1;
3972         info->signal = 0;
3973 #endif
3974         info->skip = 0;
3975         info->joined_stw = FALSE;
3976         info->doing_handshake = FALSE;
3977         info->thread_is_dying = FALSE;
3978         info->stack_start = NULL;
3979         info->store_remset_buffer_addr = &STORE_REMSET_BUFFER;
3980         info->store_remset_buffer_index_addr = &STORE_REMSET_BUFFER_INDEX;
3981         info->stopped_ip = NULL;
3982         info->stopped_domain = NULL;
3983 #ifdef USE_MONO_CTX
3984         memset (&info->ctx, 0, sizeof (MonoContext));
3985 #else
3986         memset (&info->regs, 0, sizeof (info->regs));
3987 #endif
3988
3989         sgen_init_tlab_info (info);
3990
3991         binary_protocol_thread_register ((gpointer)mono_thread_info_get_tid (info));
3992
3993 #ifdef HAVE_KW_THREAD
3994         store_remset_buffer_index_addr = &store_remset_buffer_index;
3995 #endif
3996
3997         /* try to get it with attributes first */
3998 #if defined(HAVE_PTHREAD_GETATTR_NP) && defined(HAVE_PTHREAD_ATTR_GETSTACK)
3999         {
4000                 size_t size;
4001                 void *sstart;
4002                 pthread_attr_t attr;
4003                 pthread_getattr_np (pthread_self (), &attr);
4004                 pthread_attr_getstack (&attr, &sstart, &size);
4005                 info->stack_start_limit = sstart;
4006                 info->stack_end = (char*)sstart + size;
4007                 pthread_attr_destroy (&attr);
4008         }
4009 #elif defined(HAVE_PTHREAD_GET_STACKSIZE_NP) && defined(HAVE_PTHREAD_GET_STACKADDR_NP)
4010                  info->stack_end = (char*)pthread_get_stackaddr_np (pthread_self ());
4011                  info->stack_start_limit = (char*)info->stack_end - pthread_get_stacksize_np (pthread_self ());
4012 #else
4013         {
4014                 /* FIXME: we assume the stack grows down */
4015                 gsize stack_bottom = (gsize)addr;
4016                 stack_bottom += 4095;
4017                 stack_bottom &= ~4095;
4018                 info->stack_end = (char*)stack_bottom;
4019         }
4020 #endif
4021
4022 #ifdef HAVE_KW_THREAD
4023         stack_end = info->stack_end;
4024 #endif
4025
4026         if (remset.register_thread)
4027                 remset.register_thread (info);
4028
4029         SGEN_LOG (3, "registered thread %p (%p) stack end %p", info, (gpointer)mono_thread_info_get_tid (info), info->stack_end);
4030
4031         if (gc_callbacks.thread_attach_func)
4032                 info->runtime_data = gc_callbacks.thread_attach_func ();
4033
4034         UNLOCK_GC;
4035         return info;
4036 }
4037
4038 static void
4039 sgen_wbarrier_cleanup_thread (SgenThreadInfo *p)
4040 {
4041         if (remset.cleanup_thread)
4042                 remset.cleanup_thread (p);
4043 }
4044
4045 static void
4046 sgen_thread_unregister (SgenThreadInfo *p)
4047 {
4048         /* If a delegate is passed to native code and invoked on a thread we dont
4049          * know about, the jit will register it with mono_jit_thread_attach, but
4050          * we have no way of knowing when that thread goes away.  SGen has a TSD
4051          * so we assume that if the domain is still registered, we can detach
4052          * the thread
4053          */
4054         if (mono_domain_get ())
4055                 mono_thread_detach (mono_thread_current ());
4056
4057         p->thread_is_dying = TRUE;
4058
4059         /*
4060         There is a race condition between a thread finishing executing and been removed
4061         from the GC thread set.
4062         This happens on posix systems when TLS data is been cleaned-up, libpthread will
4063         set the thread_info slot to NULL before calling the cleanup function. This
4064         opens a window in which the thread is registered but has a NULL TLS.
4065
4066         The suspend signal handler needs TLS data to know where to store thread state
4067         data or otherwise it will simply ignore the thread.
4068
4069         This solution works because the thread doing STW will wait until all threads been
4070         suspended handshake back, so there is no race between the doing_hankshake test
4071         and the suspend_thread call.
4072
4073         This is not required on systems that do synchronous STW as those can deal with
4074         the above race at suspend time.
4075
4076         FIXME: I believe we could avoid this by using mono_thread_info_lookup when
4077         mono_thread_info_current returns NULL. Or fix mono_thread_info_lookup to do so.
4078         */
4079 #if (defined(__MACH__) && MONO_MACH_ARCH_SUPPORTED) || !defined(HAVE_PTHREAD_KILL)
4080         LOCK_GC;
4081 #else
4082         while (!TRYLOCK_GC) {
4083                 if (!sgen_park_current_thread_if_doing_handshake (p))
4084                         g_usleep (50);
4085         }
4086         MONO_GC_LOCKED ();
4087 #endif
4088
4089         binary_protocol_thread_unregister ((gpointer)mono_thread_info_get_tid (p));
4090         SGEN_LOG (3, "unregister thread %p (%p)", p, (gpointer)mono_thread_info_get_tid (p));
4091
4092         if (gc_callbacks.thread_detach_func) {
4093                 gc_callbacks.thread_detach_func (p->runtime_data);
4094                 p->runtime_data = NULL;
4095         }
4096         sgen_wbarrier_cleanup_thread (p);
4097
4098         mono_threads_unregister_current_thread (p);
4099         UNLOCK_GC;
4100 }
4101
4102
4103 static void
4104 sgen_thread_attach (SgenThreadInfo *info)
4105 {
4106         LOCK_GC;
4107         /*this is odd, can we get attached before the gc is inited?*/
4108         init_stats ();
4109         UNLOCK_GC;
4110         
4111         if (gc_callbacks.thread_attach_func && !info->runtime_data)
4112                 info->runtime_data = gc_callbacks.thread_attach_func ();
4113 }
4114 gboolean
4115 mono_gc_register_thread (void *baseptr)
4116 {
4117         return mono_thread_info_attach (baseptr) != NULL;
4118 }
4119
4120 /*
4121  * mono_gc_set_stack_end:
4122  *
4123  *   Set the end of the current threads stack to STACK_END. The stack space between 
4124  * STACK_END and the real end of the threads stack will not be scanned during collections.
4125  */
4126 void
4127 mono_gc_set_stack_end (void *stack_end)
4128 {
4129         SgenThreadInfo *info;
4130
4131         LOCK_GC;
4132         info = mono_thread_info_current ();
4133         if (info) {
4134                 g_assert (stack_end < info->stack_end);
4135                 info->stack_end = stack_end;
4136         }
4137         UNLOCK_GC;
4138 }
4139
4140 #if USE_PTHREAD_INTERCEPT
4141
4142
4143 int
4144 mono_gc_pthread_create (pthread_t *new_thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)
4145 {
4146         return pthread_create (new_thread, attr, start_routine, arg);
4147 }
4148
4149 int
4150 mono_gc_pthread_join (pthread_t thread, void **retval)
4151 {
4152         return pthread_join (thread, retval);
4153 }
4154
4155 int
4156 mono_gc_pthread_detach (pthread_t thread)
4157 {
4158         return pthread_detach (thread);
4159 }
4160
4161 void
4162 mono_gc_pthread_exit (void *retval)
4163 {
4164         pthread_exit (retval);
4165 }
4166
4167 #endif /* USE_PTHREAD_INTERCEPT */
4168
4169 /*
4170  * ######################################################################
4171  * ########  Write barriers
4172  * ######################################################################
4173  */
4174
4175 /*
4176  * Note: the write barriers first do the needed GC work and then do the actual store:
4177  * this way the value is visible to the conservative GC scan after the write barrier
4178  * itself. If a GC interrupts the barrier in the middle, value will be kept alive by
4179  * the conservative scan, otherwise by the remembered set scan.
4180  */
4181 void
4182 mono_gc_wbarrier_set_field (MonoObject *obj, gpointer field_ptr, MonoObject* value)
4183 {
4184         HEAVY_STAT (++stat_wbarrier_set_field);
4185         if (ptr_in_nursery (field_ptr)) {
4186                 *(void**)field_ptr = value;
4187                 return;
4188         }
4189         SGEN_LOG (8, "Adding remset at %p", field_ptr);
4190         if (value)
4191                 binary_protocol_wbarrier (field_ptr, value, value->vtable);
4192
4193         remset.wbarrier_set_field (obj, field_ptr, value);
4194 }
4195
4196 void
4197 mono_gc_wbarrier_set_arrayref (MonoArray *arr, gpointer slot_ptr, MonoObject* value)
4198 {
4199         HEAVY_STAT (++stat_wbarrier_set_arrayref);
4200         if (ptr_in_nursery (slot_ptr)) {
4201                 *(void**)slot_ptr = value;
4202                 return;
4203         }
4204         SGEN_LOG (8, "Adding remset at %p", slot_ptr);
4205         if (value)
4206                 binary_protocol_wbarrier (slot_ptr, value, value->vtable);
4207
4208         remset.wbarrier_set_arrayref (arr, slot_ptr, value);
4209 }
4210
4211 void
4212 mono_gc_wbarrier_arrayref_copy (gpointer dest_ptr, gpointer src_ptr, int count)
4213 {
4214         HEAVY_STAT (++stat_wbarrier_arrayref_copy);
4215         /*This check can be done without taking a lock since dest_ptr array is pinned*/
4216         if (ptr_in_nursery (dest_ptr) || count <= 0) {
4217                 mono_gc_memmove (dest_ptr, src_ptr, count * sizeof (gpointer));
4218                 return;
4219         }
4220
4221 #ifdef SGEN_BINARY_PROTOCOL
4222         {
4223                 int i;
4224                 for (i = 0; i < count; ++i) {
4225                         gpointer dest = (gpointer*)dest_ptr + i;
4226                         gpointer obj = *((gpointer*)src_ptr + i);
4227                         if (obj)
4228                                 binary_protocol_wbarrier (dest, obj, (gpointer)LOAD_VTABLE (obj));
4229                 }
4230         }
4231 #endif
4232
4233         remset.wbarrier_arrayref_copy (dest_ptr, src_ptr, count);
4234 }
4235
4236 static char *found_obj;
4237
4238 static void
4239 find_object_for_ptr_callback (char *obj, size_t size, void *user_data)
4240 {
4241         char *ptr = user_data;
4242
4243         if (ptr >= obj && ptr < obj + size) {
4244                 g_assert (!found_obj);
4245                 found_obj = obj;
4246         }
4247 }
4248
4249 /* for use in the debugger */
4250 char* find_object_for_ptr (char *ptr);
4251 char*
4252 find_object_for_ptr (char *ptr)
4253 {
4254         if (ptr >= nursery_section->data && ptr < nursery_section->end_data) {
4255                 found_obj = NULL;
4256                 sgen_scan_area_with_callback (nursery_section->data, nursery_section->end_data,
4257                                 find_object_for_ptr_callback, ptr, TRUE);
4258                 if (found_obj)
4259                         return found_obj;
4260         }
4261
4262         found_obj = NULL;
4263         sgen_los_iterate_objects (find_object_for_ptr_callback, ptr);
4264         if (found_obj)
4265                 return found_obj;
4266
4267         /*
4268          * Very inefficient, but this is debugging code, supposed to
4269          * be called from gdb, so we don't care.
4270          */
4271         found_obj = NULL;
4272         major_collector.iterate_objects (TRUE, TRUE, find_object_for_ptr_callback, ptr);
4273         return found_obj;
4274 }
4275
4276 void
4277 mono_gc_wbarrier_generic_nostore (gpointer ptr)
4278 {
4279         gpointer obj;
4280
4281         HEAVY_STAT (++stat_wbarrier_generic_store);
4282
4283 #ifdef XDOMAIN_CHECKS_IN_WBARRIER
4284         /* FIXME: ptr_in_heap must be called with the GC lock held */
4285         if (xdomain_checks && *(MonoObject**)ptr && ptr_in_heap (ptr)) {
4286                 char *start = find_object_for_ptr (ptr);
4287                 MonoObject *value = *(MonoObject**)ptr;
4288                 LOCK_GC;
4289                 g_assert (start);
4290                 if (start) {
4291                         MonoObject *obj = (MonoObject*)start;
4292                         if (obj->vtable->domain != value->vtable->domain)
4293                                 g_assert (is_xdomain_ref_allowed (ptr, start, obj->vtable->domain));
4294                 }
4295                 UNLOCK_GC;
4296         }
4297 #endif
4298
4299         obj = *(gpointer*)ptr;
4300         if (obj)
4301                 binary_protocol_wbarrier (ptr, obj, (gpointer)LOAD_VTABLE (obj));
4302
4303         if (ptr_in_nursery (ptr) || ptr_on_stack (ptr)) {
4304                 SGEN_LOG (8, "Skipping remset at %p", ptr);
4305                 return;
4306         }
4307
4308         /*
4309          * We need to record old->old pointer locations for the
4310          * concurrent collector.
4311          */
4312         if (!ptr_in_nursery (obj) && !concurrent_collection_in_progress) {
4313                 SGEN_LOG (8, "Skipping remset at %p", ptr);
4314                 return;
4315         }
4316
4317         SGEN_LOG (8, "Adding remset at %p", ptr);
4318
4319         remset.wbarrier_generic_nostore (ptr);
4320 }
4321
4322 void
4323 mono_gc_wbarrier_generic_store (gpointer ptr, MonoObject* value)
4324 {
4325         SGEN_LOG (8, "Wbarrier store at %p to %p (%s)", ptr, value, value ? safe_name (value) : "null");
4326         *(void**)ptr = value;
4327         if (ptr_in_nursery (value))
4328                 mono_gc_wbarrier_generic_nostore (ptr);
4329         sgen_dummy_use (value);
4330 }
4331
4332 void mono_gc_wbarrier_value_copy_bitmap (gpointer _dest, gpointer _src, int size, unsigned bitmap)
4333 {
4334         mword *dest = _dest;
4335         mword *src = _src;
4336
4337         while (size) {
4338                 if (bitmap & 0x1)
4339                         mono_gc_wbarrier_generic_store (dest, (MonoObject*)*src);
4340                 else
4341                         *dest = *src;
4342                 ++src;
4343                 ++dest;
4344                 size -= SIZEOF_VOID_P;
4345                 bitmap >>= 1;
4346         }
4347 }
4348
4349 #ifdef SGEN_BINARY_PROTOCOL
4350 #undef HANDLE_PTR
4351 #define HANDLE_PTR(ptr,obj) do {                                        \
4352                 gpointer o = *(gpointer*)(ptr);                         \
4353                 if ((o)) {                                              \
4354                         gpointer d = ((char*)dest) + ((char*)(ptr) - (char*)(obj)); \
4355                         binary_protocol_wbarrier (d, o, (gpointer) LOAD_VTABLE (o)); \
4356                 }                                                       \
4357         } while (0)
4358
4359 static void
4360 scan_object_for_binary_protocol_copy_wbarrier (gpointer dest, char *start, mword desc)
4361 {
4362 #define SCAN_OBJECT_NOVTABLE
4363 #include "sgen-scan-object.h"
4364 }
4365 #endif
4366
4367 void
4368 mono_gc_wbarrier_value_copy (gpointer dest, gpointer src, int count, MonoClass *klass)
4369 {
4370         HEAVY_STAT (++stat_wbarrier_value_copy);
4371         g_assert (klass->valuetype);
4372
4373         SGEN_LOG (8, "Adding value remset at %p, count %d, descr %p for class %s (%p)", dest, count, klass->gc_descr, klass->name, klass);
4374
4375         if (ptr_in_nursery (dest) || ptr_on_stack (dest) || !SGEN_CLASS_HAS_REFERENCES (klass)) {
4376                 size_t element_size = mono_class_value_size (klass, NULL);
4377                 size_t size = count * element_size;
4378                 mono_gc_memmove (dest, src, size);              
4379                 return;
4380         }
4381
4382 #ifdef SGEN_BINARY_PROTOCOL
4383         {
4384                 size_t element_size = mono_class_value_size (klass, NULL);
4385                 int i;
4386                 for (i = 0; i < count; ++i) {
4387                         scan_object_for_binary_protocol_copy_wbarrier ((char*)dest + i * element_size,
4388                                         (char*)src + i * element_size - sizeof (MonoObject),
4389                                         (mword) klass->gc_descr);
4390                 }
4391         }
4392 #endif
4393
4394         remset.wbarrier_value_copy (dest, src, count, klass);
4395 }
4396
4397 /**
4398  * mono_gc_wbarrier_object_copy:
4399  *
4400  * Write barrier to call when obj is the result of a clone or copy of an object.
4401  */
4402 void
4403 mono_gc_wbarrier_object_copy (MonoObject* obj, MonoObject *src)
4404 {
4405         int size;
4406
4407         HEAVY_STAT (++stat_wbarrier_object_copy);
4408
4409         if (ptr_in_nursery (obj) || ptr_on_stack (obj)) {
4410                 size = mono_object_class (obj)->instance_size;
4411                 mono_gc_memmove ((char*)obj + sizeof (MonoObject), (char*)src + sizeof (MonoObject),
4412                                 size - sizeof (MonoObject));
4413                 return; 
4414         }
4415
4416 #ifdef SGEN_BINARY_PROTOCOL
4417         scan_object_for_binary_protocol_copy_wbarrier (obj, (char*)src, (mword) src->vtable->gc_descr);
4418 #endif
4419
4420         remset.wbarrier_object_copy (obj, src);
4421 }
4422
4423
4424 /*
4425  * ######################################################################
4426  * ########  Other mono public interface functions.
4427  * ######################################################################
4428  */
4429
4430 #define REFS_SIZE 128
4431 typedef struct {
4432         void *data;
4433         MonoGCReferences callback;
4434         int flags;
4435         int count;
4436         int called;
4437         MonoObject *refs [REFS_SIZE];
4438         uintptr_t offsets [REFS_SIZE];
4439 } HeapWalkInfo;
4440
4441 #undef HANDLE_PTR
4442 #define HANDLE_PTR(ptr,obj)     do {    \
4443                 if (*(ptr)) {   \
4444                         if (hwi->count == REFS_SIZE) {  \
4445                                 hwi->callback ((MonoObject*)start, mono_object_class (start), hwi->called? 0: size, hwi->count, hwi->refs, hwi->offsets, hwi->data);    \
4446                                 hwi->count = 0; \
4447                                 hwi->called = 1;        \
4448                         }       \
4449                         hwi->offsets [hwi->count] = (char*)(ptr)-(char*)start;  \
4450                         hwi->refs [hwi->count++] = *(ptr);      \
4451                 }       \
4452         } while (0)
4453
4454 static void
4455 collect_references (HeapWalkInfo *hwi, char *start, size_t size)
4456 {
4457 #include "sgen-scan-object.h"
4458 }
4459
4460 static void
4461 walk_references (char *start, size_t size, void *data)
4462 {
4463         HeapWalkInfo *hwi = data;
4464         hwi->called = 0;
4465         hwi->count = 0;
4466         collect_references (hwi, start, size);
4467         if (hwi->count || !hwi->called)
4468                 hwi->callback ((MonoObject*)start, mono_object_class (start), hwi->called? 0: size, hwi->count, hwi->refs, hwi->offsets, hwi->data);
4469 }
4470
4471 /**
4472  * mono_gc_walk_heap:
4473  * @flags: flags for future use
4474  * @callback: a function pointer called for each object in the heap
4475  * @data: a user data pointer that is passed to callback
4476  *
4477  * This function can be used to iterate over all the live objects in the heap:
4478  * for each object, @callback is invoked, providing info about the object's
4479  * location in memory, its class, its size and the objects it references.
4480  * For each referenced object it's offset from the object address is
4481  * reported in the offsets array.
4482  * The object references may be buffered, so the callback may be invoked
4483  * multiple times for the same object: in all but the first call, the size
4484  * argument will be zero.
4485  * Note that this function can be only called in the #MONO_GC_EVENT_PRE_START_WORLD
4486  * profiler event handler.
4487  *
4488  * Returns: a non-zero value if the GC doesn't support heap walking
4489  */
4490 int
4491 mono_gc_walk_heap (int flags, MonoGCReferences callback, void *data)
4492 {
4493         HeapWalkInfo hwi;
4494
4495         hwi.flags = flags;
4496         hwi.callback = callback;
4497         hwi.data = data;
4498
4499         sgen_clear_nursery_fragments ();
4500         sgen_scan_area_with_callback (nursery_section->data, nursery_section->end_data, walk_references, &hwi, FALSE);
4501
4502         major_collector.iterate_objects (TRUE, TRUE, walk_references, &hwi);
4503         sgen_los_iterate_objects (walk_references, &hwi);
4504
4505         return 0;
4506 }
4507
4508 void
4509 mono_gc_collect (int generation)
4510 {
4511         LOCK_GC;
4512         if (generation > 1)
4513                 generation = 1;
4514         sgen_perform_collection (0, generation, "user request", TRUE);
4515         UNLOCK_GC;
4516 }
4517
4518 int
4519 mono_gc_max_generation (void)
4520 {
4521         return 1;
4522 }
4523
4524 int
4525 mono_gc_collection_count (int generation)
4526 {
4527         if (generation == 0)
4528                 return stat_minor_gcs;
4529         return stat_major_gcs;
4530 }
4531
4532 int64_t
4533 mono_gc_get_used_size (void)
4534 {
4535         gint64 tot = 0;
4536         LOCK_GC;
4537         tot = los_memory_usage;
4538         tot += nursery_section->next_data - nursery_section->data;
4539         tot += major_collector.get_used_size ();
4540         /* FIXME: account for pinned objects */
4541         UNLOCK_GC;
4542         return tot;
4543 }
4544
4545 void
4546 mono_gc_disable (void)
4547 {
4548         LOCK_GC;
4549         gc_disabled++;
4550         UNLOCK_GC;
4551 }
4552
4553 void
4554 mono_gc_enable (void)
4555 {
4556         LOCK_GC;
4557         gc_disabled--;
4558         UNLOCK_GC;
4559 }
4560
4561 int
4562 mono_gc_get_los_limit (void)
4563 {
4564         return MAX_SMALL_OBJ_SIZE;
4565 }
4566
4567 gboolean
4568 mono_gc_user_markers_supported (void)
4569 {
4570         return TRUE;
4571 }
4572
4573 gboolean
4574 mono_object_is_alive (MonoObject* o)
4575 {
4576         return TRUE;
4577 }
4578
4579 int
4580 mono_gc_get_generation (MonoObject *obj)
4581 {
4582         if (ptr_in_nursery (obj))
4583                 return 0;
4584         return 1;
4585 }
4586
4587 void
4588 mono_gc_enable_events (void)
4589 {
4590 }
4591
4592 void
4593 mono_gc_weak_link_add (void **link_addr, MonoObject *obj, gboolean track)
4594 {
4595         sgen_register_disappearing_link (obj, link_addr, track, FALSE);
4596 }
4597
4598 void
4599 mono_gc_weak_link_remove (void **link_addr)
4600 {
4601         sgen_register_disappearing_link (NULL, link_addr, FALSE, FALSE);
4602 }
4603
4604 MonoObject*
4605 mono_gc_weak_link_get (void **link_addr)
4606 {
4607         /*
4608          * We must only load *link_addr once because it might change
4609          * under our feet, and REVEAL_POINTER (NULL) results in an
4610          * invalid reference.
4611          */
4612         void *ptr = *link_addr;
4613         if (!ptr)
4614                 return NULL;
4615
4616         /*
4617          * During the second bridge processing step the world is
4618          * running again.  That step processes all weak links once
4619          * more to null those that refer to dead objects.  Before that
4620          * is completed, those links must not be followed, so we
4621          * conservatively wait for bridge processing when any weak
4622          * link is dereferenced.
4623          */
4624         if (G_UNLIKELY (bridge_processing_in_progress))
4625                 mono_gc_wait_for_bridge_processing ();
4626
4627         return (MonoObject*) REVEAL_POINTER (ptr);
4628 }
4629
4630 gboolean
4631 mono_gc_ephemeron_array_add (MonoObject *obj)
4632 {
4633         EphemeronLinkNode *node;
4634
4635         LOCK_GC;
4636
4637         node = sgen_alloc_internal (INTERNAL_MEM_EPHEMERON_LINK);
4638         if (!node) {
4639                 UNLOCK_GC;
4640                 return FALSE;
4641         }
4642         node->array = (char*)obj;
4643         node->next = ephemeron_list;
4644         ephemeron_list = node;
4645
4646         SGEN_LOG (5, "Registered ephemeron array %p", obj);
4647
4648         UNLOCK_GC;
4649         return TRUE;
4650 }
4651
4652 void*
4653 mono_gc_invoke_with_gc_lock (MonoGCLockedCallbackFunc func, void *data)
4654 {
4655         void *result;
4656         LOCK_INTERRUPTION;
4657         result = func (data);
4658         UNLOCK_INTERRUPTION;
4659         return result;
4660 }
4661
4662 gboolean
4663 mono_gc_is_gc_thread (void)
4664 {
4665         gboolean result;
4666         LOCK_GC;
4667         result = mono_thread_info_current () != NULL;
4668         UNLOCK_GC;
4669         return result;
4670 }
4671
4672 static gboolean
4673 is_critical_method (MonoMethod *method)
4674 {
4675         return mono_runtime_is_critical_method (method) || sgen_is_critical_method (method);
4676 }
4677         
4678 void
4679 mono_gc_base_init (void)
4680 {
4681         MonoThreadInfoCallbacks cb;
4682         char *env;
4683         char **opts, **ptr;
4684         char *major_collector_opt = NULL;
4685         char *minor_collector_opt = NULL;
4686         glong max_heap = 0;
4687         glong soft_limit = 0;
4688         int num_workers;
4689         int result;
4690         int dummy;
4691         gboolean debug_print_allowance = FALSE;
4692         double allowance_ratio = 0, save_target = 0;
4693         gboolean have_split_nursery = FALSE;
4694
4695         do {
4696                 result = InterlockedCompareExchange (&gc_initialized, -1, 0);
4697                 switch (result) {
4698                 case 1:
4699                         /* already inited */
4700                         return;
4701                 case -1:
4702                         /* being inited by another thread */
4703                         g_usleep (1000);
4704                         break;
4705                 case 0:
4706                         /* we will init it */
4707                         break;
4708                 default:
4709                         g_assert_not_reached ();
4710                 }
4711         } while (result != 0);
4712
4713         LOCK_INIT (gc_mutex);
4714
4715         pagesize = mono_pagesize ();
4716         gc_debug_file = stderr;
4717
4718         cb.thread_register = sgen_thread_register;
4719         cb.thread_unregister = sgen_thread_unregister;
4720         cb.thread_attach = sgen_thread_attach;
4721         cb.mono_method_is_critical = (gpointer)is_critical_method;
4722 #ifndef HOST_WIN32
4723         cb.mono_gc_pthread_create = (gpointer)mono_gc_pthread_create;
4724 #endif
4725
4726         mono_threads_init (&cb, sizeof (SgenThreadInfo));
4727
4728         LOCK_INIT (sgen_interruption_mutex);
4729         LOCK_INIT (pin_queue_mutex);
4730
4731         init_user_copy_or_mark_key ();
4732
4733         if ((env = getenv ("MONO_GC_PARAMS"))) {
4734                 opts = g_strsplit (env, ",", -1);
4735                 for (ptr = opts; *ptr; ++ptr) {
4736                         char *opt = *ptr;
4737                         if (g_str_has_prefix (opt, "major=")) {
4738                                 opt = strchr (opt, '=') + 1;
4739                                 major_collector_opt = g_strdup (opt);
4740                         } else if (g_str_has_prefix (opt, "minor=")) {
4741                                 opt = strchr (opt, '=') + 1;
4742                                 minor_collector_opt = g_strdup (opt);
4743                         }
4744                 }
4745         } else {
4746                 opts = NULL;
4747         }
4748
4749         init_stats ();
4750         sgen_init_internal_allocator ();
4751         sgen_init_nursery_allocator ();
4752
4753         sgen_register_fixed_internal_mem_type (INTERNAL_MEM_SECTION, SGEN_SIZEOF_GC_MEM_SECTION);
4754         sgen_register_fixed_internal_mem_type (INTERNAL_MEM_FINALIZE_READY_ENTRY, sizeof (FinalizeReadyEntry));
4755         sgen_register_fixed_internal_mem_type (INTERNAL_MEM_GRAY_QUEUE, sizeof (GrayQueueSection));
4756         g_assert (sizeof (GenericStoreRememberedSet) == sizeof (gpointer) * STORE_REMSET_BUFFER_SIZE);
4757         sgen_register_fixed_internal_mem_type (INTERNAL_MEM_STORE_REMSET, sizeof (GenericStoreRememberedSet));
4758         sgen_register_fixed_internal_mem_type (INTERNAL_MEM_EPHEMERON_LINK, sizeof (EphemeronLinkNode));
4759
4760 #ifndef HAVE_KW_THREAD
4761         mono_native_tls_alloc (&thread_info_key, NULL);
4762 #endif
4763
4764         /*
4765          * This needs to happen before any internal allocations because
4766          * it inits the small id which is required for hazard pointer
4767          * operations.
4768          */
4769         sgen_os_init ();
4770
4771         mono_thread_info_attach (&dummy);
4772
4773         if (!minor_collector_opt) {
4774                 sgen_simple_nursery_init (&sgen_minor_collector);
4775         } else {
4776                 if (!strcmp (minor_collector_opt, "simple")) {
4777                         sgen_simple_nursery_init (&sgen_minor_collector);
4778                 } else if (!strcmp (minor_collector_opt, "split")) {
4779                         sgen_split_nursery_init (&sgen_minor_collector);
4780                         have_split_nursery = TRUE;
4781                 } else {
4782                         fprintf (stderr, "Unknown minor collector `%s'.\n", minor_collector_opt);
4783                         exit (1);
4784                 }
4785         }
4786
4787         if (!major_collector_opt || !strcmp (major_collector_opt, "marksweep")) {
4788                 sgen_marksweep_init (&major_collector);
4789         } else if (!major_collector_opt || !strcmp (major_collector_opt, "marksweep-fixed")) {
4790                 sgen_marksweep_fixed_init (&major_collector);
4791         } else if (!major_collector_opt || !strcmp (major_collector_opt, "marksweep-par")) {
4792                 sgen_marksweep_par_init (&major_collector);
4793         } else if (!major_collector_opt || !strcmp (major_collector_opt, "marksweep-fixed-par")) {
4794                 sgen_marksweep_fixed_par_init (&major_collector);
4795         } else if (!major_collector_opt || !strcmp (major_collector_opt, "marksweep-conc")) {
4796                 if (have_split_nursery) {
4797                         fprintf (stderr, "Concurrent Mark&Sweep does not yet support the split nursery.\n");
4798                         exit (1);
4799                 }
4800                 sgen_marksweep_conc_init (&major_collector);
4801         } else if (!strcmp (major_collector_opt, "copying")) {
4802                 sgen_copying_init (&major_collector);
4803         } else {
4804                 fprintf (stderr, "Unknown major collector `%s'.\n", major_collector_opt);
4805                 exit (1);
4806         }
4807
4808 #ifdef SGEN_HAVE_CARDTABLE
4809         use_cardtable = major_collector.supports_cardtable;
4810 #else
4811         use_cardtable = FALSE;
4812 #endif
4813
4814         num_workers = mono_cpu_count ();
4815         g_assert (num_workers > 0);
4816         if (num_workers > 16)
4817                 num_workers = 16;
4818
4819         ///* Keep this the default for now */
4820         /* Precise marking is broken on all supported targets. Disable until fixed. */
4821         conservative_stack_mark = TRUE;
4822
4823         sgen_nursery_size = DEFAULT_NURSERY_SIZE;
4824
4825         if (opts) {
4826                 for (ptr = opts; *ptr; ++ptr) {
4827                         char *opt = *ptr;
4828                         if (g_str_has_prefix (opt, "major="))
4829                                 continue;
4830                         if (g_str_has_prefix (opt, "minor="))
4831                                 continue;
4832                         if (g_str_has_prefix (opt, "wbarrier=")) {
4833                                 opt = strchr (opt, '=') + 1;
4834                                 if (strcmp (opt, "remset") == 0) {
4835                                         if (major_collector.is_concurrent) {
4836                                                 fprintf (stderr, "The concurrent collector does not support the SSB write barrier.\n");
4837                                                 exit (1);
4838                                         }
4839                                         use_cardtable = FALSE;
4840                                 } else if (strcmp (opt, "cardtable") == 0) {
4841                                         if (!use_cardtable) {
4842                                                 if (major_collector.supports_cardtable)
4843                                                         fprintf (stderr, "The cardtable write barrier is not supported on this platform.\n");
4844                                                 else
4845                                                         fprintf (stderr, "The major collector does not support the cardtable write barrier.\n");
4846                                                 exit (1);
4847                                         }
4848                                 } else {
4849                                         fprintf (stderr, "wbarrier must either be `remset' or `cardtable'.");
4850                                         exit (1);
4851                                 }
4852                                 continue;
4853                         }
4854                         if (g_str_has_prefix (opt, "max-heap-size=")) {
4855                                 opt = strchr (opt, '=') + 1;
4856                                 if (*opt && mono_gc_parse_environment_string_extract_number (opt, &max_heap)) {
4857                                         if ((max_heap & (mono_pagesize () - 1))) {
4858                                                 fprintf (stderr, "max-heap-size size must be a multiple of %d.\n", mono_pagesize ());
4859                                                 exit (1);
4860                                         }
4861                                 } else {
4862                                         fprintf (stderr, "max-heap-size must be an integer.\n");
4863                                         exit (1);
4864                                 }
4865                                 continue;
4866                         }
4867                         if (g_str_has_prefix (opt, "soft-heap-limit=")) {
4868                                 opt = strchr (opt, '=') + 1;
4869                                 if (*opt && mono_gc_parse_environment_string_extract_number (opt, &soft_limit)) {
4870                                         if (soft_limit <= 0) {
4871                                                 fprintf (stderr, "soft-heap-limit must be positive.\n");
4872                                                 exit (1);
4873                                         }
4874                                 } else {
4875                                         fprintf (stderr, "soft-heap-limit must be an integer.\n");
4876                                         exit (1);
4877                                 }
4878                                 continue;
4879                         }
4880                         if (g_str_has_prefix (opt, "workers=")) {
4881                                 long val;
4882                                 char *endptr;
4883                                 if (!major_collector.is_parallel) {
4884                                         fprintf (stderr, "The workers= option can only be used for parallel collectors.");
4885                                         exit (1);
4886                                 }
4887                                 opt = strchr (opt, '=') + 1;
4888                                 val = strtol (opt, &endptr, 10);
4889                                 if (!*opt || *endptr) {
4890                                         fprintf (stderr, "Cannot parse the workers= option value.");
4891                                         exit (1);
4892                                 }
4893                                 if (val <= 0 || val > 16) {
4894                                         fprintf (stderr, "The number of workers must be in the range 1 to 16.");
4895                                         exit (1);
4896                                 }
4897                                 num_workers = (int)val;
4898                                 continue;
4899                         }
4900                         if (g_str_has_prefix (opt, "stack-mark=")) {
4901                                 opt = strchr (opt, '=') + 1;
4902                                 if (!strcmp (opt, "precise")) {
4903                                         conservative_stack_mark = FALSE;
4904                                 } else if (!strcmp (opt, "conservative")) {
4905                                         conservative_stack_mark = TRUE;
4906                                 } else {
4907                                         fprintf (stderr, "Invalid value '%s' for stack-mark= option, possible values are: 'precise', 'conservative'.\n", opt);
4908                                         exit (1);
4909                                 }
4910                                 continue;
4911                         }
4912                         if (g_str_has_prefix (opt, "bridge=")) {
4913                                 opt = strchr (opt, '=') + 1;
4914                                 sgen_register_test_bridge_callbacks (g_strdup (opt));
4915                                 continue;
4916                         }
4917 #ifdef USER_CONFIG
4918                         if (g_str_has_prefix (opt, "nursery-size=")) {
4919                                 long val;
4920                                 opt = strchr (opt, '=') + 1;
4921                                 if (*opt && mono_gc_parse_environment_string_extract_number (opt, &val)) {
4922                                         sgen_nursery_size = val;
4923 #ifdef SGEN_ALIGN_NURSERY
4924                                         if ((val & (val - 1))) {
4925                                                 fprintf (stderr, "The nursery size must be a power of two.\n");
4926                                                 exit (1);
4927                                         }
4928
4929                                         if (val < SGEN_MAX_NURSERY_WASTE) {
4930                                                 fprintf (stderr, "The nursery size must be at least %d bytes.\n", SGEN_MAX_NURSERY_WASTE);
4931                                                 exit (1);
4932                                         }
4933
4934                                         sgen_nursery_bits = 0;
4935                                         while (1 << (++ sgen_nursery_bits) != sgen_nursery_size)
4936                                                 ;
4937 #endif
4938                                 } else {
4939                                         fprintf (stderr, "nursery-size must be an integer.\n");
4940                                         exit (1);
4941                                 }
4942                                 continue;
4943                         }
4944 #endif
4945                         if (g_str_has_prefix (opt, "save-target-ratio=")) {
4946                                 char *endptr;
4947                                 opt = strchr (opt, '=') + 1;
4948                                 save_target = strtod (opt, &endptr);
4949                                 if (endptr == opt) {
4950                                         fprintf (stderr, "save-target-ratio must be a number.");
4951                                         exit (1);
4952                                 }
4953                                 if (save_target < SGEN_MIN_SAVE_TARGET_RATIO || save_target > SGEN_MAX_SAVE_TARGET_RATIO) {
4954                                         fprintf (stderr, "save-target-ratio must be between %.2f - %.2f.", SGEN_MIN_SAVE_TARGET_RATIO, SGEN_MAX_SAVE_TARGET_RATIO);
4955                                         exit (1);
4956                                 }
4957                                 continue;
4958                         }
4959                         if (g_str_has_prefix (opt, "default-allowance-ratio=")) {
4960                                 char *endptr;
4961                                 opt = strchr (opt, '=') + 1;
4962
4963                                 allowance_ratio = strtod (opt, &endptr);
4964                                 if (endptr == opt) {
4965                                         fprintf (stderr, "save-target-ratio must be a number.");
4966                                         exit (1);
4967                                 }
4968                                 if (allowance_ratio < SGEN_MIN_ALLOWANCE_NURSERY_SIZE_RATIO || allowance_ratio > SGEN_MIN_ALLOWANCE_NURSERY_SIZE_RATIO) {
4969                                         fprintf (stderr, "default-allowance-ratio must be between %.2f - %.2f.", SGEN_MIN_ALLOWANCE_NURSERY_SIZE_RATIO, SGEN_MIN_ALLOWANCE_NURSERY_SIZE_RATIO);
4970                                         exit (1);
4971                                 }
4972                                 continue;
4973                         }
4974
4975                         if (major_collector.handle_gc_param && major_collector.handle_gc_param (opt))
4976                                 continue;
4977
4978                         if (sgen_minor_collector.handle_gc_param && sgen_minor_collector.handle_gc_param (opt))
4979                                 continue;
4980
4981                         fprintf (stderr, "MONO_GC_PARAMS must be a comma-delimited list of one or more of the following:\n");
4982                         fprintf (stderr, "  max-heap-size=N (where N is an integer, possibly with a k, m or a g suffix)\n");
4983                         fprintf (stderr, "  soft-heap-limit=n (where N is an integer, possibly with a k, m or a g suffix)\n");
4984                         fprintf (stderr, "  nursery-size=N (where N is an integer, possibly with a k, m or a g suffix)\n");
4985                         fprintf (stderr, "  major=COLLECTOR (where COLLECTOR is `marksweep', `marksweep-par', 'marksweep-fixed', 'marksweep-fixed-par' or `copying')\n");
4986                         fprintf (stderr, "  minor=COLLECTOR (where COLLECTOR is `simple' or `split')\n");
4987                         fprintf (stderr, "  wbarrier=WBARRIER (where WBARRIER is `remset' or `cardtable')\n");
4988                         fprintf (stderr, "  stack-mark=MARK-METHOD (where MARK-METHOD is 'precise' or 'conservative')\n");
4989                         if (major_collector.print_gc_param_usage)
4990                                 major_collector.print_gc_param_usage ();
4991                         if (sgen_minor_collector.print_gc_param_usage)
4992                                 sgen_minor_collector.print_gc_param_usage ();
4993                         fprintf (stderr, " Experimental options:\n");
4994                         fprintf (stderr, "  save-target-ratio=R (where R must be between %.2f - %.2f).\n", SGEN_MIN_SAVE_TARGET_RATIO, SGEN_MAX_SAVE_TARGET_RATIO);
4995                         fprintf (stderr, "  default-allowance-ratio=R (where R must be between %.2f - %.2f).\n", SGEN_MIN_ALLOWANCE_NURSERY_SIZE_RATIO, SGEN_MAX_ALLOWANCE_NURSERY_SIZE_RATIO);
4996                         exit (1);
4997                 }
4998                 g_strfreev (opts);
4999         }
5000
5001         if (major_collector.is_parallel)
5002                 sgen_workers_init (num_workers);
5003         else if (major_collector.is_concurrent)
5004                 sgen_workers_init (1);
5005
5006         if (major_collector_opt)
5007                 g_free (major_collector_opt);
5008
5009         if (minor_collector_opt)
5010                 g_free (minor_collector_opt);
5011
5012         alloc_nursery ();
5013
5014         if ((env = getenv ("MONO_GC_DEBUG"))) {
5015                 opts = g_strsplit (env, ",", -1);
5016                 for (ptr = opts; ptr && *ptr; ptr ++) {
5017                         char *opt = *ptr;
5018                         if (opt [0] >= '0' && opt [0] <= '9') {
5019                                 gc_debug_level = atoi (opt);
5020                                 opt++;
5021                                 if (opt [0] == ':')
5022                                         opt++;
5023                                 if (opt [0]) {
5024 #ifdef HOST_WIN32
5025                                         char *rf = g_strdup_printf ("%s.%d", opt, GetCurrentProcessId ());
5026 #else
5027                                         char *rf = g_strdup_printf ("%s.%d", opt, getpid ());
5028 #endif
5029                                         gc_debug_file = fopen (rf, "wb");
5030                                         if (!gc_debug_file)
5031                                                 gc_debug_file = stderr;
5032                                         g_free (rf);
5033                                 }
5034                         } else if (!strcmp (opt, "print-allowance")) {
5035                                 debug_print_allowance = TRUE;
5036                         } else if (!strcmp (opt, "print-pinning")) {
5037                                 do_pin_stats = TRUE;
5038                         } else if (!strcmp (opt, "verify-before-allocs")) {
5039                                 verify_before_allocs = 1;
5040                                 has_per_allocation_action = TRUE;
5041                         } else if (g_str_has_prefix (opt, "verify-before-allocs=")) {
5042                                 char *arg = strchr (opt, '=') + 1;
5043                                 verify_before_allocs = atoi (arg);
5044                                 has_per_allocation_action = TRUE;
5045                         } else if (!strcmp (opt, "collect-before-allocs")) {
5046                                 collect_before_allocs = 1;
5047                                 has_per_allocation_action = TRUE;
5048                         } else if (g_str_has_prefix (opt, "collect-before-allocs=")) {
5049                                 char *arg = strchr (opt, '=') + 1;
5050                                 has_per_allocation_action = TRUE;
5051                                 collect_before_allocs = atoi (arg);
5052                         } else if (!strcmp (opt, "verify-before-collections")) {
5053                                 whole_heap_check_before_collection = TRUE;
5054                         } else if (!strcmp (opt, "check-at-minor-collections")) {
5055                                 consistency_check_at_minor_collection = TRUE;
5056                                 nursery_clear_policy = CLEAR_AT_GC;
5057                         } else if (!strcmp (opt, "check-mark-bits")) {
5058                                 check_mark_bits_after_major_collection = TRUE;
5059                         } else if (!strcmp (opt, "xdomain-checks")) {
5060                                 xdomain_checks = TRUE;
5061                         } else if (!strcmp (opt, "clear-at-gc")) {
5062                                 nursery_clear_policy = CLEAR_AT_GC;
5063                         } else if (!strcmp (opt, "clear-nursery-at-gc")) {
5064                                 nursery_clear_policy = CLEAR_AT_GC;
5065                         } else if (!strcmp (opt, "check-scan-starts")) {
5066                                 do_scan_starts_check = TRUE;
5067                         } else if (!strcmp (opt, "verify-nursery-at-minor-gc")) {
5068                                 do_verify_nursery = TRUE;
5069                         } else if (!strcmp (opt, "check-concurrent")) {
5070                                 if (!major_collector.is_concurrent) {
5071                                         fprintf (stderr, "Error: check-concurrent only world with concurrent major collectors.\n");
5072                                         exit (1);
5073                                 }
5074                                 do_concurrent_checks = TRUE;
5075                         } else if (!strcmp (opt, "dump-nursery-at-minor-gc")) {
5076                                 do_dump_nursery_content = TRUE;
5077                         } else if (!strcmp (opt, "no-managed-allocator")) {
5078                                 sgen_set_use_managed_allocator (FALSE);
5079                         } else if (!strcmp (opt, "disable-minor")) {
5080                                 disable_minor_collections = TRUE;
5081                         } else if (!strcmp (opt, "disable-major")) {
5082                                 disable_major_collections = TRUE;
5083                         } else if (g_str_has_prefix (opt, "heap-dump=")) {
5084                                 char *filename = strchr (opt, '=') + 1;
5085                                 nursery_clear_policy = CLEAR_AT_GC;
5086                                 heap_dump_file = fopen (filename, "w");
5087                                 if (heap_dump_file) {
5088                                         fprintf (heap_dump_file, "<sgen-dump>\n");
5089                                         do_pin_stats = TRUE;
5090                                 }
5091 #ifdef SGEN_BINARY_PROTOCOL
5092                         } else if (g_str_has_prefix (opt, "binary-protocol=")) {
5093                                 char *filename = strchr (opt, '=') + 1;
5094                                 binary_protocol_init (filename);
5095                                 if (use_cardtable)
5096                                         fprintf (stderr, "Warning: Cardtable write barriers will not be binary-protocolled.\n");
5097 #endif
5098                         } else {
5099                                 fprintf (stderr, "Invalid format for the MONO_GC_DEBUG env variable: '%s'\n", env);
5100                                 fprintf (stderr, "The format is: MONO_GC_DEBUG=[l[:filename]|<option>]+ where l is a debug level 0-9.\n");
5101                                 fprintf (stderr, "Valid options are:\n");
5102                                 fprintf (stderr, "  collect-before-allocs[=<n>]\n");
5103                                 fprintf (stderr, "  verify-before-allocs[=<n>]\n");
5104                                 fprintf (stderr, "  check-at-minor-collections\n");
5105                                 fprintf (stderr, "  check-mark-bits\n");
5106                                 fprintf (stderr, "  verify-before-collections\n");
5107                                 fprintf (stderr, "  verify-nursery-at-minor-gc\n");
5108                                 fprintf (stderr, "  dump-nursery-at-minor-gc\n");
5109                                 fprintf (stderr, "  disable-minor\n");
5110                                 fprintf (stderr, "  disable-major\n");
5111                                 fprintf (stderr, "  xdomain-checks\n");
5112                                 fprintf (stderr, "  check-concurrent\n");
5113                                 fprintf (stderr, "  clear-at-gc\n");
5114                                 fprintf (stderr, "  clear-nursery-at-gc\n");
5115                                 fprintf (stderr, "  check-scan-starts\n");
5116                                 fprintf (stderr, "  no-managed-allocator\n");
5117                                 fprintf (stderr, "  print-allowance\n");
5118                                 fprintf (stderr, "  print-pinning\n");
5119                                 fprintf (stderr, "  heap-dump=<filename>\n");
5120 #ifdef SGEN_BINARY_PROTOCOL
5121                                 fprintf (stderr, "  binary-protocol=<filename>\n");
5122 #endif
5123                                 exit (1);
5124                         }
5125                 }
5126                 g_strfreev (opts);
5127         }
5128
5129         if (major_collector.is_parallel) {
5130                 if (heap_dump_file) {
5131                         fprintf (stderr, "Error: Cannot do heap dump with the parallel collector.\n");
5132                         exit (1);
5133                 }
5134                 if (do_pin_stats) {
5135                         fprintf (stderr, "Error: Cannot gather pinning statistics with the parallel collector.\n");
5136                         exit (1);
5137                 }
5138         }
5139
5140         if (major_collector.post_param_init)
5141                 major_collector.post_param_init (&major_collector);
5142
5143         sgen_memgov_init (max_heap, soft_limit, debug_print_allowance, allowance_ratio, save_target);
5144
5145         memset (&remset, 0, sizeof (remset));
5146
5147 #ifdef SGEN_HAVE_CARDTABLE
5148         if (use_cardtable)
5149                 sgen_card_table_init (&remset);
5150         else
5151 #endif
5152                 sgen_ssb_init (&remset);
5153
5154         if (remset.register_thread)
5155                 remset.register_thread (mono_thread_info_current ());
5156
5157         gc_initialized = 1;
5158 }
5159
5160 const char *
5161 mono_gc_get_gc_name (void)
5162 {
5163         return "sgen";
5164 }
5165
5166 static MonoMethod *write_barrier_method;
5167
5168 gboolean
5169 sgen_is_critical_method (MonoMethod *method)
5170 {
5171         return (method == write_barrier_method || sgen_is_managed_allocator (method));
5172 }
5173
5174 gboolean
5175 sgen_has_critical_method (void)
5176 {
5177         return write_barrier_method || sgen_has_managed_allocator ();
5178 }
5179
5180 static void
5181 emit_nursery_check (MonoMethodBuilder *mb, int *nursery_check_return_labels)
5182 {
5183         memset (nursery_check_return_labels, 0, sizeof (int) * 3);
5184 #ifdef SGEN_ALIGN_NURSERY
5185         // if (ptr_in_nursery (ptr)) return;
5186         /*
5187          * Masking out the bits might be faster, but we would have to use 64 bit
5188          * immediates, which might be slower.
5189          */
5190         mono_mb_emit_ldarg (mb, 0);
5191         mono_mb_emit_icon (mb, DEFAULT_NURSERY_BITS);
5192         mono_mb_emit_byte (mb, CEE_SHR_UN);
5193         mono_mb_emit_icon (mb, (mword)sgen_get_nursery_start () >> DEFAULT_NURSERY_BITS);
5194         nursery_check_return_labels [0] = mono_mb_emit_branch (mb, CEE_BEQ);
5195
5196         if (!major_collector.is_concurrent) {
5197                 // if (!ptr_in_nursery (*ptr)) return;
5198                 mono_mb_emit_ldarg (mb, 0);
5199                 mono_mb_emit_byte (mb, CEE_LDIND_I);
5200                 mono_mb_emit_icon (mb, DEFAULT_NURSERY_BITS);
5201                 mono_mb_emit_byte (mb, CEE_SHR_UN);
5202                 mono_mb_emit_icon (mb, (mword)sgen_get_nursery_start () >> DEFAULT_NURSERY_BITS);
5203                 nursery_check_return_labels [1] = mono_mb_emit_branch (mb, CEE_BNE_UN);
5204         }
5205 #else
5206         int label_continue1, label_continue2;
5207         int dereferenced_var;
5208
5209         // if (ptr < (sgen_get_nursery_start ())) goto continue;
5210         mono_mb_emit_ldarg (mb, 0);
5211         mono_mb_emit_ptr (mb, (gpointer) sgen_get_nursery_start ());
5212         label_continue_1 = mono_mb_emit_branch (mb, CEE_BLT);
5213
5214         // if (ptr >= sgen_get_nursery_end ())) goto continue;
5215         mono_mb_emit_ldarg (mb, 0);
5216         mono_mb_emit_ptr (mb, (gpointer) sgen_get_nursery_end ());
5217         label_continue_2 = mono_mb_emit_branch (mb, CEE_BGE);
5218
5219         // Otherwise return
5220         nursery_check_return_labels [0] = mono_mb_emit_branch (mb, CEE_BR);
5221
5222         // continue:
5223         mono_mb_patch_branch (mb, label_continue_1);
5224         mono_mb_patch_branch (mb, label_continue_2);
5225
5226         // Dereference and store in local var
5227         dereferenced_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
5228         mono_mb_emit_ldarg (mb, 0);
5229         mono_mb_emit_byte (mb, CEE_LDIND_I);
5230         mono_mb_emit_stloc (mb, dereferenced_var);
5231
5232         if (!major_collector.is_concurrent) {
5233                 // if (*ptr < sgen_get_nursery_start ()) return;
5234                 mono_mb_emit_ldloc (mb, dereferenced_var);
5235                 mono_mb_emit_ptr (mb, (gpointer) sgen_get_nursery_start ());
5236                 nursery_check_return_labels [1] = mono_mb_emit_branch (mb, CEE_BLT);
5237
5238                 // if (*ptr >= sgen_get_nursery_end ()) return;
5239                 mono_mb_emit_ldloc (mb, dereferenced_var);
5240                 mono_mb_emit_ptr (mb, (gpointer) sgen_get_nursery_end ());
5241                 nursery_check_return_labels [2] = mono_mb_emit_branch (mb, CEE_BGE);
5242         }
5243 #endif  
5244 }
5245
5246 MonoMethod*
5247 mono_gc_get_write_barrier (void)
5248 {
5249         MonoMethod *res;
5250         MonoMethodBuilder *mb;
5251         MonoMethodSignature *sig;
5252 #ifdef MANAGED_WBARRIER
5253         int i, nursery_check_labels [3];
5254         int label_no_wb_3, label_no_wb_4, label_need_wb, label_slow_path;
5255         int buffer_var, buffer_index_var, dummy_var;
5256
5257 #ifdef HAVE_KW_THREAD
5258         int stack_end_offset = -1, store_remset_buffer_offset = -1;
5259         int store_remset_buffer_index_offset = -1, store_remset_buffer_index_addr_offset = -1;
5260
5261         MONO_THREAD_VAR_OFFSET (stack_end, stack_end_offset);
5262         g_assert (stack_end_offset != -1);
5263         MONO_THREAD_VAR_OFFSET (store_remset_buffer, store_remset_buffer_offset);
5264         g_assert (store_remset_buffer_offset != -1);
5265         MONO_THREAD_VAR_OFFSET (store_remset_buffer_index, store_remset_buffer_index_offset);
5266         g_assert (store_remset_buffer_index_offset != -1);
5267         MONO_THREAD_VAR_OFFSET (store_remset_buffer_index_addr, store_remset_buffer_index_addr_offset);
5268         g_assert (store_remset_buffer_index_addr_offset != -1);
5269 #endif
5270 #endif
5271
5272         // FIXME: Maybe create a separate version for ctors (the branch would be
5273         // correctly predicted more times)
5274         if (write_barrier_method)
5275                 return write_barrier_method;
5276
5277         /* Create the IL version of mono_gc_barrier_generic_store () */
5278         sig = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
5279         sig->ret = &mono_defaults.void_class->byval_arg;
5280         sig->params [0] = &mono_defaults.int_class->byval_arg;
5281
5282         mb = mono_mb_new (mono_defaults.object_class, "wbarrier", MONO_WRAPPER_WRITE_BARRIER);
5283
5284 #ifdef MANAGED_WBARRIER
5285         if (use_cardtable) {
5286                 emit_nursery_check (mb, nursery_check_labels);
5287                 /*
5288                 addr = sgen_cardtable + ((address >> CARD_BITS) & CARD_MASK)
5289                 *addr = 1;
5290
5291                 sgen_cardtable: 
5292                         LDC_PTR sgen_cardtable
5293
5294                 address >> CARD_BITS
5295                         LDARG_0
5296                         LDC_I4 CARD_BITS
5297                         SHR_UN
5298                 if (SGEN_HAVE_OVERLAPPING_CARDS) {
5299                         LDC_PTR card_table_mask
5300                         AND
5301                 }
5302                 AND
5303                 ldc_i4_1
5304                 stind_i1
5305                 */
5306                 mono_mb_emit_ptr (mb, sgen_cardtable);
5307                 mono_mb_emit_ldarg (mb, 0);
5308                 mono_mb_emit_icon (mb, CARD_BITS);
5309                 mono_mb_emit_byte (mb, CEE_SHR_UN);
5310 #ifdef SGEN_HAVE_OVERLAPPING_CARDS
5311                 mono_mb_emit_ptr (mb, (gpointer)CARD_MASK);
5312                 mono_mb_emit_byte (mb, CEE_AND);
5313 #endif
5314                 mono_mb_emit_byte (mb, CEE_ADD);
5315                 mono_mb_emit_icon (mb, 1);
5316                 mono_mb_emit_byte (mb, CEE_STIND_I1);
5317
5318                 // return;
5319                 for (i = 0; i < 3; ++i) {
5320                         if (nursery_check_labels [i])
5321                                 mono_mb_patch_branch (mb, nursery_check_labels [i]);
5322                 }               
5323                 mono_mb_emit_byte (mb, CEE_RET);
5324         } else if (mono_runtime_has_tls_get ()) {
5325                 emit_nursery_check (mb, nursery_check_labels);
5326
5327                 // if (ptr >= stack_end) goto need_wb;
5328                 mono_mb_emit_ldarg (mb, 0);
5329                 EMIT_TLS_ACCESS (mb, stack_end, stack_end_offset);
5330                 label_need_wb = mono_mb_emit_branch (mb, CEE_BGE_UN);
5331
5332                 // if (ptr >= stack_start) return;
5333                 dummy_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
5334                 mono_mb_emit_ldarg (mb, 0);
5335                 mono_mb_emit_ldloc_addr (mb, dummy_var);
5336                 label_no_wb_3 = mono_mb_emit_branch (mb, CEE_BGE_UN);
5337
5338                 // need_wb:
5339                 mono_mb_patch_branch (mb, label_need_wb);
5340
5341                 // buffer = STORE_REMSET_BUFFER;
5342                 buffer_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
5343                 EMIT_TLS_ACCESS (mb, store_remset_buffer, store_remset_buffer_offset);
5344                 mono_mb_emit_stloc (mb, buffer_var);
5345
5346                 // buffer_index = STORE_REMSET_BUFFER_INDEX;
5347                 buffer_index_var = mono_mb_add_local (mb, &mono_defaults.int_class->byval_arg);
5348                 EMIT_TLS_ACCESS (mb, store_remset_buffer_index, store_remset_buffer_index_offset);
5349                 mono_mb_emit_stloc (mb, buffer_index_var);
5350
5351                 // if (buffer [buffer_index] == ptr) return;
5352                 mono_mb_emit_ldloc (mb, buffer_var);
5353                 mono_mb_emit_ldloc (mb, buffer_index_var);
5354                 g_assert (sizeof (gpointer) == 4 || sizeof (gpointer) == 8);
5355                 mono_mb_emit_icon (mb, sizeof (gpointer) == 4 ? 2 : 3);
5356                 mono_mb_emit_byte (mb, CEE_SHL);
5357                 mono_mb_emit_byte (mb, CEE_ADD);
5358                 mono_mb_emit_byte (mb, CEE_LDIND_I);
5359                 mono_mb_emit_ldarg (mb, 0);
5360                 label_no_wb_4 = mono_mb_emit_branch (mb, CEE_BEQ);
5361
5362                 // ++buffer_index;
5363                 mono_mb_emit_ldloc (mb, buffer_index_var);
5364                 mono_mb_emit_icon (mb, 1);
5365                 mono_mb_emit_byte (mb, CEE_ADD);
5366                 mono_mb_emit_stloc (mb, buffer_index_var);
5367
5368                 // if (buffer_index >= STORE_REMSET_BUFFER_SIZE) goto slow_path;
5369                 mono_mb_emit_ldloc (mb, buffer_index_var);
5370                 mono_mb_emit_icon (mb, STORE_REMSET_BUFFER_SIZE);
5371                 label_slow_path = mono_mb_emit_branch (mb, CEE_BGE);
5372
5373                 // buffer [buffer_index] = ptr;
5374                 mono_mb_emit_ldloc (mb, buffer_var);
5375                 mono_mb_emit_ldloc (mb, buffer_index_var);
5376                 g_assert (sizeof (gpointer) == 4 || sizeof (gpointer) == 8);
5377                 mono_mb_emit_icon (mb, sizeof (gpointer) == 4 ? 2 : 3);
5378                 mono_mb_emit_byte (mb, CEE_SHL);
5379                 mono_mb_emit_byte (mb, CEE_ADD);
5380                 mono_mb_emit_ldarg (mb, 0);
5381                 mono_mb_emit_byte (mb, CEE_STIND_I);
5382
5383                 // STORE_REMSET_BUFFER_INDEX = buffer_index;
5384                 EMIT_TLS_ACCESS (mb, store_remset_buffer_index_addr, store_remset_buffer_index_addr_offset);
5385                 mono_mb_emit_ldloc (mb, buffer_index_var);
5386                 mono_mb_emit_byte (mb, CEE_STIND_I);
5387
5388                 // return;
5389                 for (i = 0; i < 3; ++i) {
5390                         if (nursery_check_labels [i])
5391                                 mono_mb_patch_branch (mb, nursery_check_labels [i]);
5392                 }
5393                 mono_mb_patch_branch (mb, label_no_wb_3);
5394                 mono_mb_patch_branch (mb, label_no_wb_4);
5395                 mono_mb_emit_byte (mb, CEE_RET);
5396
5397                 // slow path
5398                 mono_mb_patch_branch (mb, label_slow_path);
5399
5400                 mono_mb_emit_ldarg (mb, 0);
5401                 mono_mb_emit_icall (mb, mono_gc_wbarrier_generic_nostore);
5402                 mono_mb_emit_byte (mb, CEE_RET);
5403         } else
5404 #endif
5405         {
5406                 mono_mb_emit_ldarg (mb, 0);
5407                 mono_mb_emit_icall (mb, mono_gc_wbarrier_generic_nostore);
5408                 mono_mb_emit_byte (mb, CEE_RET);
5409         }
5410
5411         res = mono_mb_create_method (mb, sig, 16);
5412         mono_mb_free (mb);
5413
5414         mono_loader_lock ();
5415         if (write_barrier_method) {
5416                 /* Already created */
5417                 mono_free_method (res);
5418         } else {
5419                 /* double-checked locking */
5420                 mono_memory_barrier ();
5421                 write_barrier_method = res;
5422         }
5423         mono_loader_unlock ();
5424
5425         return write_barrier_method;
5426 }
5427
5428 char*
5429 mono_gc_get_description (void)
5430 {
5431         return g_strdup ("sgen");
5432 }
5433
5434 void
5435 mono_gc_set_desktop_mode (void)
5436 {
5437 }
5438
5439 gboolean
5440 mono_gc_is_moving (void)
5441 {
5442         return TRUE;
5443 }
5444
5445 gboolean
5446 mono_gc_is_disabled (void)
5447 {
5448         return FALSE;
5449 }
5450
5451 #ifdef HOST_WIN32
5452 BOOL APIENTRY mono_gc_dllmain (HMODULE module_handle, DWORD reason, LPVOID reserved)
5453 {
5454         return TRUE;
5455 }
5456 #endif
5457
5458 NurseryClearPolicy
5459 sgen_get_nursery_clear_policy (void)
5460 {
5461         return nursery_clear_policy;
5462 }
5463
5464 MonoVTable*
5465 sgen_get_array_fill_vtable (void)
5466 {
5467         if (!array_fill_vtable) {
5468                 static MonoClass klass;
5469                 static MonoVTable vtable;
5470                 gsize bmap;
5471
5472                 MonoDomain *domain = mono_get_root_domain ();
5473                 g_assert (domain);
5474
5475                 klass.element_class = mono_defaults.byte_class;
5476                 klass.rank = 1;
5477                 klass.instance_size = sizeof (MonoArray);
5478                 klass.sizes.element_size = 1;
5479                 klass.name = "array_filler_type";
5480
5481                 vtable.klass = &klass;
5482                 bmap = 0;
5483                 vtable.gc_descr = mono_gc_make_descr_for_array (TRUE, &bmap, 0, 1);
5484                 vtable.rank = 1;
5485
5486                 array_fill_vtable = &vtable;
5487         }
5488         return array_fill_vtable;
5489 }
5490
5491 void
5492 sgen_gc_lock (void)
5493 {
5494         LOCK_GC;
5495 }
5496
5497 void
5498 sgen_gc_unlock (void)
5499 {
5500         UNLOCK_GC;
5501 }
5502
5503 void
5504 sgen_major_collector_iterate_live_block_ranges (sgen_cardtable_block_callback callback)
5505 {
5506         major_collector.iterate_live_block_ranges (callback);
5507 }
5508
5509 void
5510 sgen_major_collector_scan_card_table (SgenGrayQueue *queue)
5511 {
5512         major_collector.scan_card_table (FALSE, queue);
5513 }
5514
5515 SgenMajorCollector*
5516 sgen_get_major_collector (void)
5517 {
5518         return &major_collector;
5519 }
5520
5521 void mono_gc_set_skip_thread (gboolean skip)
5522 {
5523         SgenThreadInfo *info = mono_thread_info_current ();
5524
5525         LOCK_GC;
5526         info->gc_disabled = skip;
5527         UNLOCK_GC;
5528 }
5529
5530 SgenRemeberedSet*
5531 sgen_get_remset (void)
5532 {
5533         return &remset;
5534 }
5535
5536 guint
5537 mono_gc_get_vtable_bits (MonoClass *class)
5538 {
5539         if (sgen_need_bridge_processing () && sgen_is_bridge_class (class))
5540                 return SGEN_GC_BIT_BRIDGE_OBJECT;
5541         return 0;
5542 }
5543
5544 void
5545 mono_gc_register_altstack (gpointer stack, gint32 stack_size, gpointer altstack, gint32 altstack_size)
5546 {
5547         // FIXME:
5548 }
5549
5550
5551 void
5552 sgen_check_whole_heap_stw (void)
5553 {
5554         sgen_stop_world (0);
5555         sgen_clear_nursery_fragments ();
5556         sgen_check_whole_heap ();
5557         sgen_restart_world (0, NULL);
5558 }
5559
5560 void
5561 sgen_gc_event_moves (void)
5562 {
5563         if (moved_objects_idx) {
5564                 mono_profiler_gc_moves (moved_objects, moved_objects_idx);
5565                 moved_objects_idx = 0;
5566         }
5567 }
5568
5569 #endif /* HAVE_SGEN_GC */