1 /* go-trampoline.c -- allocate a trampoline for a nested function.
3 Copyright 2009 The Go Authors. All rights reserved.
4 Use of this source code is governed by a BSD-style
5 license that can be found in the LICENSE file. */
13 #ifdef HAVE_SYS_MMAN_H
20 #include "go-assert.h"
22 /* Trampolines need to run in memory that is both writable and
23 executable. In order to implement them, we grab a page of memory
24 and mprotect it. We fill in the page with trampolines as they are
25 required. When we run out of space, we drop the pointer to the
26 page and allocate a new one. The page will be freed by the garbage
27 collector when there are no more variables of type func pointing to
30 /* A lock to control access to the page of closures. */
32 static Lock trampoline_lock;
34 /* The page of closures. */
36 static unsigned char *trampoline_page;
38 /* The size of trampoline_page. */
40 static uintptr_t trampoline_page_size;
42 /* The number of bytes we have used on trampoline_page. */
44 static uintptr_t trampoline_page_used;
46 /* Allocate a trampoline of SIZE bytes that will use the closure in
50 __go_allocate_trampoline (uintptr_t size, void *closure)
56 /* Because the garbage collector only looks at aligned addresses, we
57 need to store the closure at an aligned address to ensure that it
59 ptr_size = sizeof (void *);
60 full_size = (((size + ptr_size - 1) / ptr_size) * ptr_size);
61 full_size += ptr_size;
63 runtime_lock (&trampoline_lock);
65 if (full_size < trampoline_page_size - trampoline_page_used)
66 trampoline_page = NULL;
68 if (trampoline_page == NULL)
73 page_size = getpagesize ();
74 __go_assert (page_size >= full_size);
75 page = (unsigned char *) runtime_mallocgc (2 * page_size - 1, 0, 0, 0);
76 page = (unsigned char *) (((uintptr_t) page + page_size - 1)
79 #ifdef HAVE_SYS_MMAN_H
83 i = mprotect (page, page_size, PROT_READ | PROT_WRITE | PROT_EXEC);
88 trampoline_page = page;
89 trampoline_page_size = page_size;
90 trampoline_page_used = 0;
93 ret = trampoline_page + trampoline_page_used;
94 trampoline_page_used += full_size;
96 runtime_unlock (&trampoline_lock);
98 __builtin_memcpy (ret + full_size - ptr_size, &closure, ptr_size);
103 /* Scan the trampoline page when running the garbage collector. This
104 just makes sure that the garbage collector sees the pointer in
105 trampoline_page, so that the page itself is not freed if there are
106 no other references to it. */
109 runtime_trampoline_scan (void (*addroot) (Obj))
111 if (trampoline_page != NULL)
112 addroot ((Obj){(byte *) &trampoline_page, sizeof trampoline_page, 0});