OSDN Git Service

tcg: Return the map protection from alloc_code_gen_buffer
[qmiga/qemu.git] / tcg / region.c
1 /*
2  * Memory region management for Tiny Code Generator for QEMU
3  *
4  * Copyright (c) 2008 Fabrice Bellard
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24
25 #include "qemu/osdep.h"
26 #include "qemu/units.h"
27 #include "qapi/error.h"
28 #include "exec/exec-all.h"
29 #include "tcg/tcg.h"
30 #include "tcg-internal.h"
31
32
33 struct tcg_region_tree {
34     QemuMutex lock;
35     GTree *tree;
36     /* padding to avoid false sharing is computed at run-time */
37 };
38
39 /*
40  * We divide code_gen_buffer into equally-sized "regions" that TCG threads
41  * dynamically allocate from as demand dictates. Given appropriate region
42  * sizing, this minimizes flushes even when some TCG threads generate a lot
43  * more code than others.
44  */
45 struct tcg_region_state {
46     QemuMutex lock;
47
48     /* fields set at init time */
49     void *start_aligned;
50     void *after_prologue;
51     size_t n;
52     size_t size; /* size of one region */
53     size_t stride; /* .size + guard size */
54     size_t total_size; /* size of entire buffer, >= n * stride */
55
56     /* fields protected by the lock */
57     size_t current; /* current region index */
58     size_t agg_size_full; /* aggregate size of full regions */
59 };
60
61 static struct tcg_region_state region;
62
63 /*
64  * This is an array of struct tcg_region_tree's, with padding.
65  * We use void * to simplify the computation of region_trees[i]; each
66  * struct is found every tree_size bytes.
67  */
68 static void *region_trees;
69 static size_t tree_size;
70
71 bool in_code_gen_buffer(const void *p)
72 {
73     /*
74      * Much like it is valid to have a pointer to the byte past the
75      * end of an array (so long as you don't dereference it), allow
76      * a pointer to the byte past the end of the code gen buffer.
77      */
78     return (size_t)(p - region.start_aligned) <= region.total_size;
79 }
80
81 #ifdef CONFIG_DEBUG_TCG
82 const void *tcg_splitwx_to_rx(void *rw)
83 {
84     /* Pass NULL pointers unchanged. */
85     if (rw) {
86         g_assert(in_code_gen_buffer(rw));
87         rw += tcg_splitwx_diff;
88     }
89     return rw;
90 }
91
92 void *tcg_splitwx_to_rw(const void *rx)
93 {
94     /* Pass NULL pointers unchanged. */
95     if (rx) {
96         rx -= tcg_splitwx_diff;
97         /* Assert that we end with a pointer in the rw region. */
98         g_assert(in_code_gen_buffer(rx));
99     }
100     return (void *)rx;
101 }
102 #endif /* CONFIG_DEBUG_TCG */
103
104 /* compare a pointer @ptr and a tb_tc @s */
105 static int ptr_cmp_tb_tc(const void *ptr, const struct tb_tc *s)
106 {
107     if (ptr >= s->ptr + s->size) {
108         return 1;
109     } else if (ptr < s->ptr) {
110         return -1;
111     }
112     return 0;
113 }
114
115 static gint tb_tc_cmp(gconstpointer ap, gconstpointer bp)
116 {
117     const struct tb_tc *a = ap;
118     const struct tb_tc *b = bp;
119
120     /*
121      * When both sizes are set, we know this isn't a lookup.
122      * This is the most likely case: every TB must be inserted; lookups
123      * are a lot less frequent.
124      */
125     if (likely(a->size && b->size)) {
126         if (a->ptr > b->ptr) {
127             return 1;
128         } else if (a->ptr < b->ptr) {
129             return -1;
130         }
131         /* a->ptr == b->ptr should happen only on deletions */
132         g_assert(a->size == b->size);
133         return 0;
134     }
135     /*
136      * All lookups have either .size field set to 0.
137      * From the glib sources we see that @ap is always the lookup key. However
138      * the docs provide no guarantee, so we just mark this case as likely.
139      */
140     if (likely(a->size == 0)) {
141         return ptr_cmp_tb_tc(a->ptr, b);
142     }
143     return ptr_cmp_tb_tc(b->ptr, a);
144 }
145
146 static void tcg_region_trees_init(void)
147 {
148     size_t i;
149
150     tree_size = ROUND_UP(sizeof(struct tcg_region_tree), qemu_dcache_linesize);
151     region_trees = qemu_memalign(qemu_dcache_linesize, region.n * tree_size);
152     for (i = 0; i < region.n; i++) {
153         struct tcg_region_tree *rt = region_trees + i * tree_size;
154
155         qemu_mutex_init(&rt->lock);
156         rt->tree = g_tree_new(tb_tc_cmp);
157     }
158 }
159
160 static struct tcg_region_tree *tc_ptr_to_region_tree(const void *p)
161 {
162     size_t region_idx;
163
164     /*
165      * Like tcg_splitwx_to_rw, with no assert.  The pc may come from
166      * a signal handler over which the caller has no control.
167      */
168     if (!in_code_gen_buffer(p)) {
169         p -= tcg_splitwx_diff;
170         if (!in_code_gen_buffer(p)) {
171             return NULL;
172         }
173     }
174
175     if (p < region.start_aligned) {
176         region_idx = 0;
177     } else {
178         ptrdiff_t offset = p - region.start_aligned;
179
180         if (offset > region.stride * (region.n - 1)) {
181             region_idx = region.n - 1;
182         } else {
183             region_idx = offset / region.stride;
184         }
185     }
186     return region_trees + region_idx * tree_size;
187 }
188
189 void tcg_tb_insert(TranslationBlock *tb)
190 {
191     struct tcg_region_tree *rt = tc_ptr_to_region_tree(tb->tc.ptr);
192
193     g_assert(rt != NULL);
194     qemu_mutex_lock(&rt->lock);
195     g_tree_insert(rt->tree, &tb->tc, tb);
196     qemu_mutex_unlock(&rt->lock);
197 }
198
199 void tcg_tb_remove(TranslationBlock *tb)
200 {
201     struct tcg_region_tree *rt = tc_ptr_to_region_tree(tb->tc.ptr);
202
203     g_assert(rt != NULL);
204     qemu_mutex_lock(&rt->lock);
205     g_tree_remove(rt->tree, &tb->tc);
206     qemu_mutex_unlock(&rt->lock);
207 }
208
209 /*
210  * Find the TB 'tb' such that
211  * tb->tc.ptr <= tc_ptr < tb->tc.ptr + tb->tc.size
212  * Return NULL if not found.
213  */
214 TranslationBlock *tcg_tb_lookup(uintptr_t tc_ptr)
215 {
216     struct tcg_region_tree *rt = tc_ptr_to_region_tree((void *)tc_ptr);
217     TranslationBlock *tb;
218     struct tb_tc s = { .ptr = (void *)tc_ptr };
219
220     if (rt == NULL) {
221         return NULL;
222     }
223
224     qemu_mutex_lock(&rt->lock);
225     tb = g_tree_lookup(rt->tree, &s);
226     qemu_mutex_unlock(&rt->lock);
227     return tb;
228 }
229
230 static void tcg_region_tree_lock_all(void)
231 {
232     size_t i;
233
234     for (i = 0; i < region.n; i++) {
235         struct tcg_region_tree *rt = region_trees + i * tree_size;
236
237         qemu_mutex_lock(&rt->lock);
238     }
239 }
240
241 static void tcg_region_tree_unlock_all(void)
242 {
243     size_t i;
244
245     for (i = 0; i < region.n; i++) {
246         struct tcg_region_tree *rt = region_trees + i * tree_size;
247
248         qemu_mutex_unlock(&rt->lock);
249     }
250 }
251
252 void tcg_tb_foreach(GTraverseFunc func, gpointer user_data)
253 {
254     size_t i;
255
256     tcg_region_tree_lock_all();
257     for (i = 0; i < region.n; i++) {
258         struct tcg_region_tree *rt = region_trees + i * tree_size;
259
260         g_tree_foreach(rt->tree, func, user_data);
261     }
262     tcg_region_tree_unlock_all();
263 }
264
265 size_t tcg_nb_tbs(void)
266 {
267     size_t nb_tbs = 0;
268     size_t i;
269
270     tcg_region_tree_lock_all();
271     for (i = 0; i < region.n; i++) {
272         struct tcg_region_tree *rt = region_trees + i * tree_size;
273
274         nb_tbs += g_tree_nnodes(rt->tree);
275     }
276     tcg_region_tree_unlock_all();
277     return nb_tbs;
278 }
279
280 static gboolean tcg_region_tree_traverse(gpointer k, gpointer v, gpointer data)
281 {
282     TranslationBlock *tb = v;
283
284     tb_destroy(tb);
285     return FALSE;
286 }
287
288 static void tcg_region_tree_reset_all(void)
289 {
290     size_t i;
291
292     tcg_region_tree_lock_all();
293     for (i = 0; i < region.n; i++) {
294         struct tcg_region_tree *rt = region_trees + i * tree_size;
295
296         g_tree_foreach(rt->tree, tcg_region_tree_traverse, NULL);
297         /* Increment the refcount first so that destroy acts as a reset */
298         g_tree_ref(rt->tree);
299         g_tree_destroy(rt->tree);
300     }
301     tcg_region_tree_unlock_all();
302 }
303
304 static void tcg_region_bounds(size_t curr_region, void **pstart, void **pend)
305 {
306     void *start, *end;
307
308     start = region.start_aligned + curr_region * region.stride;
309     end = start + region.size;
310
311     if (curr_region == 0) {
312         start = region.after_prologue;
313     }
314     /* The final region may have a few extra pages due to earlier rounding. */
315     if (curr_region == region.n - 1) {
316         end = region.start_aligned + region.total_size;
317     }
318
319     *pstart = start;
320     *pend = end;
321 }
322
323 static void tcg_region_assign(TCGContext *s, size_t curr_region)
324 {
325     void *start, *end;
326
327     tcg_region_bounds(curr_region, &start, &end);
328
329     s->code_gen_buffer = start;
330     s->code_gen_ptr = start;
331     s->code_gen_buffer_size = end - start;
332     s->code_gen_highwater = end - TCG_HIGHWATER;
333 }
334
335 static bool tcg_region_alloc__locked(TCGContext *s)
336 {
337     if (region.current == region.n) {
338         return true;
339     }
340     tcg_region_assign(s, region.current);
341     region.current++;
342     return false;
343 }
344
345 /*
346  * Request a new region once the one in use has filled up.
347  * Returns true on error.
348  */
349 bool tcg_region_alloc(TCGContext *s)
350 {
351     bool err;
352     /* read the region size now; alloc__locked will overwrite it on success */
353     size_t size_full = s->code_gen_buffer_size;
354
355     qemu_mutex_lock(&region.lock);
356     err = tcg_region_alloc__locked(s);
357     if (!err) {
358         region.agg_size_full += size_full - TCG_HIGHWATER;
359     }
360     qemu_mutex_unlock(&region.lock);
361     return err;
362 }
363
364 /*
365  * Perform a context's first region allocation.
366  * This function does _not_ increment region.agg_size_full.
367  */
368 static void tcg_region_initial_alloc__locked(TCGContext *s)
369 {
370     bool err = tcg_region_alloc__locked(s);
371     g_assert(!err);
372 }
373
374 void tcg_region_initial_alloc(TCGContext *s)
375 {
376     qemu_mutex_lock(&region.lock);
377     tcg_region_initial_alloc__locked(s);
378     qemu_mutex_unlock(&region.lock);
379 }
380
381 /* Call from a safe-work context */
382 void tcg_region_reset_all(void)
383 {
384     unsigned int n_ctxs = qatomic_read(&tcg_cur_ctxs);
385     unsigned int i;
386
387     qemu_mutex_lock(&region.lock);
388     region.current = 0;
389     region.agg_size_full = 0;
390
391     for (i = 0; i < n_ctxs; i++) {
392         TCGContext *s = qatomic_read(&tcg_ctxs[i]);
393         tcg_region_initial_alloc__locked(s);
394     }
395     qemu_mutex_unlock(&region.lock);
396
397     tcg_region_tree_reset_all();
398 }
399
400 static size_t tcg_n_regions(size_t tb_size, unsigned max_cpus)
401 {
402 #ifdef CONFIG_USER_ONLY
403     return 1;
404 #else
405     size_t n_regions;
406
407     /*
408      * It is likely that some vCPUs will translate more code than others,
409      * so we first try to set more regions than max_cpus, with those regions
410      * being of reasonable size. If that's not possible we make do by evenly
411      * dividing the code_gen_buffer among the vCPUs.
412      */
413     /* Use a single region if all we have is one vCPU thread */
414     if (max_cpus == 1 || !qemu_tcg_mttcg_enabled()) {
415         return 1;
416     }
417
418     /*
419      * Try to have more regions than max_cpus, with each region being >= 2 MB.
420      * If we can't, then just allocate one region per vCPU thread.
421      */
422     n_regions = tb_size / (2 * MiB);
423     if (n_regions <= max_cpus) {
424         return max_cpus;
425     }
426     return MIN(n_regions, max_cpus * 8);
427 #endif
428 }
429
430 /*
431  * Minimum size of the code gen buffer.  This number is randomly chosen,
432  * but not so small that we can't have a fair number of TB's live.
433  *
434  * Maximum size, MAX_CODE_GEN_BUFFER_SIZE, is defined in tcg-target.h.
435  * Unless otherwise indicated, this is constrained by the range of
436  * direct branches on the host cpu, as used by the TCG implementation
437  * of goto_tb.
438  */
439 #define MIN_CODE_GEN_BUFFER_SIZE     (1 * MiB)
440
441 #if TCG_TARGET_REG_BITS == 32
442 #define DEFAULT_CODE_GEN_BUFFER_SIZE_1 (32 * MiB)
443 #ifdef CONFIG_USER_ONLY
444 /*
445  * For user mode on smaller 32 bit systems we may run into trouble
446  * allocating big chunks of data in the right place. On these systems
447  * we utilise a static code generation buffer directly in the binary.
448  */
449 #define USE_STATIC_CODE_GEN_BUFFER
450 #endif
451 #else /* TCG_TARGET_REG_BITS == 64 */
452 #ifdef CONFIG_USER_ONLY
453 /*
454  * As user-mode emulation typically means running multiple instances
455  * of the translator don't go too nuts with our default code gen
456  * buffer lest we make things too hard for the OS.
457  */
458 #define DEFAULT_CODE_GEN_BUFFER_SIZE_1 (128 * MiB)
459 #else
460 /*
461  * We expect most system emulation to run one or two guests per host.
462  * Users running large scale system emulation may want to tweak their
463  * runtime setup via the tb-size control on the command line.
464  */
465 #define DEFAULT_CODE_GEN_BUFFER_SIZE_1 (1 * GiB)
466 #endif
467 #endif
468
469 #define DEFAULT_CODE_GEN_BUFFER_SIZE \
470   (DEFAULT_CODE_GEN_BUFFER_SIZE_1 < MAX_CODE_GEN_BUFFER_SIZE \
471    ? DEFAULT_CODE_GEN_BUFFER_SIZE_1 : MAX_CODE_GEN_BUFFER_SIZE)
472
473 static size_t size_code_gen_buffer(size_t tb_size)
474 {
475     /* Size the buffer.  */
476     if (tb_size == 0) {
477         size_t phys_mem = qemu_get_host_physmem();
478         if (phys_mem == 0) {
479             tb_size = DEFAULT_CODE_GEN_BUFFER_SIZE;
480         } else {
481             tb_size = MIN(DEFAULT_CODE_GEN_BUFFER_SIZE, phys_mem / 8);
482         }
483     }
484     if (tb_size < MIN_CODE_GEN_BUFFER_SIZE) {
485         tb_size = MIN_CODE_GEN_BUFFER_SIZE;
486     }
487     if (tb_size > MAX_CODE_GEN_BUFFER_SIZE) {
488         tb_size = MAX_CODE_GEN_BUFFER_SIZE;
489     }
490     return tb_size;
491 }
492
493 #ifdef __mips__
494 /*
495  * In order to use J and JAL within the code_gen_buffer, we require
496  * that the buffer not cross a 256MB boundary.
497  */
498 static inline bool cross_256mb(void *addr, size_t size)
499 {
500     return ((uintptr_t)addr ^ ((uintptr_t)addr + size)) & ~0x0ffffffful;
501 }
502
503 /*
504  * We weren't able to allocate a buffer without crossing that boundary,
505  * so make do with the larger portion of the buffer that doesn't cross.
506  * Returns the new base and size of the buffer in *obuf and *osize.
507  */
508 static inline void split_cross_256mb(void **obuf, size_t *osize,
509                                      void *buf1, size_t size1)
510 {
511     void *buf2 = (void *)(((uintptr_t)buf1 + size1) & ~0x0ffffffful);
512     size_t size2 = buf1 + size1 - buf2;
513
514     size1 = buf2 - buf1;
515     if (size1 < size2) {
516         size1 = size2;
517         buf1 = buf2;
518     }
519
520     *obuf = buf1;
521     *osize = size1;
522 }
523 #endif
524
525 #ifdef USE_STATIC_CODE_GEN_BUFFER
526 static uint8_t static_code_gen_buffer[DEFAULT_CODE_GEN_BUFFER_SIZE]
527     __attribute__((aligned(CODE_GEN_ALIGN)));
528
529 static int alloc_code_gen_buffer(size_t tb_size, int splitwx, Error **errp)
530 {
531     void *buf, *end;
532     size_t size;
533
534     if (splitwx > 0) {
535         error_setg(errp, "jit split-wx not supported");
536         return -1;
537     }
538
539     /* page-align the beginning and end of the buffer */
540     buf = static_code_gen_buffer;
541     end = static_code_gen_buffer + sizeof(static_code_gen_buffer);
542     buf = QEMU_ALIGN_PTR_UP(buf, qemu_real_host_page_size);
543     end = QEMU_ALIGN_PTR_DOWN(end, qemu_real_host_page_size);
544
545     size = end - buf;
546
547     /* Honor a command-line option limiting the size of the buffer.  */
548     if (size > tb_size) {
549         size = QEMU_ALIGN_DOWN(tb_size, qemu_real_host_page_size);
550     }
551
552 #ifdef __mips__
553     if (cross_256mb(buf, size)) {
554         split_cross_256mb(&buf, &size, buf, size);
555     }
556 #endif
557
558     if (qemu_mprotect_rwx(buf, size)) {
559         error_setg_errno(errp, errno, "mprotect of jit buffer");
560         return false;
561     }
562     qemu_madvise(buf, size, QEMU_MADV_HUGEPAGE);
563
564     region.start_aligned = buf;
565     region.total_size = size;
566
567     return PROT_READ | PROT_WRITE;
568 }
569 #elif defined(_WIN32)
570 static int alloc_code_gen_buffer(size_t size, int splitwx, Error **errp)
571 {
572     void *buf;
573
574     if (splitwx > 0) {
575         error_setg(errp, "jit split-wx not supported");
576         return -1;
577     }
578
579     buf = VirtualAlloc(NULL, size, MEM_RESERVE | MEM_COMMIT,
580                              PAGE_EXECUTE_READWRITE);
581     if (buf == NULL) {
582         error_setg_win32(errp, GetLastError(),
583                          "allocate %zu bytes for jit buffer", size);
584         return false;
585     }
586
587     region.start_aligned = buf;
588     region.total_size = size;
589
590     return PAGE_READ | PAGE_WRITE | PAGE_EXEC;
591 }
592 #else
593 static int alloc_code_gen_buffer_anon(size_t size, int prot,
594                                       int flags, Error **errp)
595 {
596     void *buf;
597
598     buf = mmap(NULL, size, prot, flags, -1, 0);
599     if (buf == MAP_FAILED) {
600         error_setg_errno(errp, errno,
601                          "allocate %zu bytes for jit buffer", size);
602         return -1;
603     }
604
605 #ifdef __mips__
606     if (cross_256mb(buf, size)) {
607         /*
608          * Try again, with the original still mapped, to avoid re-acquiring
609          * the same 256mb crossing.
610          */
611         size_t size2;
612         void *buf2 = mmap(NULL, size, prot, flags, -1, 0);
613         switch ((int)(buf2 != MAP_FAILED)) {
614         case 1:
615             if (!cross_256mb(buf2, size)) {
616                 /* Success!  Use the new buffer.  */
617                 munmap(buf, size);
618                 break;
619             }
620             /* Failure.  Work with what we had.  */
621             munmap(buf2, size);
622             /* fallthru */
623         default:
624             /* Split the original buffer.  Free the smaller half.  */
625             split_cross_256mb(&buf2, &size2, buf, size);
626             if (buf == buf2) {
627                 munmap(buf + size2, size - size2);
628             } else {
629                 munmap(buf, size - size2);
630             }
631             size = size2;
632             break;
633         }
634         buf = buf2;
635     }
636 #endif
637
638     /* Request large pages for the buffer.  */
639     qemu_madvise(buf, size, QEMU_MADV_HUGEPAGE);
640
641     region.start_aligned = buf;
642     region.total_size = size;
643     return prot;
644 }
645
646 #ifndef CONFIG_TCG_INTERPRETER
647 #ifdef CONFIG_POSIX
648 #include "qemu/memfd.h"
649
650 static bool alloc_code_gen_buffer_splitwx_memfd(size_t size, Error **errp)
651 {
652     void *buf_rw = NULL, *buf_rx = MAP_FAILED;
653     int fd = -1;
654
655 #ifdef __mips__
656     /* Find space for the RX mapping, vs the 256MiB regions. */
657     if (alloc_code_gen_buffer_anon(size, PROT_NONE,
658                                    MAP_PRIVATE | MAP_ANONYMOUS |
659                                    MAP_NORESERVE, errp) < 0) {
660         return false;
661     }
662     /* The size of the mapping may have been adjusted. */
663     buf_rx = region.start_aligned;
664     size = region.total_size;
665 #endif
666
667     buf_rw = qemu_memfd_alloc("tcg-jit", size, 0, &fd, errp);
668     if (buf_rw == NULL) {
669         goto fail;
670     }
671
672 #ifdef __mips__
673     void *tmp = mmap(buf_rx, size, PROT_READ | PROT_EXEC,
674                      MAP_SHARED | MAP_FIXED, fd, 0);
675     if (tmp != buf_rx) {
676         goto fail_rx;
677     }
678 #else
679     buf_rx = mmap(NULL, size, PROT_READ | PROT_EXEC, MAP_SHARED, fd, 0);
680     if (buf_rx == MAP_FAILED) {
681         goto fail_rx;
682     }
683 #endif
684
685     close(fd);
686     region.start_aligned = buf_rw;
687     region.total_size = size;
688     tcg_splitwx_diff = buf_rx - buf_rw;
689
690     /* Request large pages for the buffer and the splitwx.  */
691     qemu_madvise(buf_rw, size, QEMU_MADV_HUGEPAGE);
692     qemu_madvise(buf_rx, size, QEMU_MADV_HUGEPAGE);
693     return PROT_READ | PROT_WRITE;
694
695  fail_rx:
696     error_setg_errno(errp, errno, "failed to map shared memory for execute");
697  fail:
698     if (buf_rx != MAP_FAILED) {
699         munmap(buf_rx, size);
700     }
701     if (buf_rw) {
702         munmap(buf_rw, size);
703     }
704     if (fd >= 0) {
705         close(fd);
706     }
707     return -1;
708 }
709 #endif /* CONFIG_POSIX */
710
711 #ifdef CONFIG_DARWIN
712 #include <mach/mach.h>
713
714 extern kern_return_t mach_vm_remap(vm_map_t target_task,
715                                    mach_vm_address_t *target_address,
716                                    mach_vm_size_t size,
717                                    mach_vm_offset_t mask,
718                                    int flags,
719                                    vm_map_t src_task,
720                                    mach_vm_address_t src_address,
721                                    boolean_t copy,
722                                    vm_prot_t *cur_protection,
723                                    vm_prot_t *max_protection,
724                                    vm_inherit_t inheritance);
725
726 static int alloc_code_gen_buffer_splitwx_vmremap(size_t size, Error **errp)
727 {
728     kern_return_t ret;
729     mach_vm_address_t buf_rw, buf_rx;
730     vm_prot_t cur_prot, max_prot;
731
732     /* Map the read-write portion via normal anon memory. */
733     if (!alloc_code_gen_buffer_anon(size, PROT_READ | PROT_WRITE,
734                                     MAP_PRIVATE | MAP_ANONYMOUS, errp)) {
735         return -1;
736     }
737
738     buf_rw = (mach_vm_address_t)region.start_aligned;
739     buf_rx = 0;
740     ret = mach_vm_remap(mach_task_self(),
741                         &buf_rx,
742                         size,
743                         0,
744                         VM_FLAGS_ANYWHERE,
745                         mach_task_self(),
746                         buf_rw,
747                         false,
748                         &cur_prot,
749                         &max_prot,
750                         VM_INHERIT_NONE);
751     if (ret != KERN_SUCCESS) {
752         /* TODO: Convert "ret" to a human readable error message. */
753         error_setg(errp, "vm_remap for jit splitwx failed");
754         munmap((void *)buf_rw, size);
755         return -1;
756     }
757
758     if (mprotect((void *)buf_rx, size, PROT_READ | PROT_EXEC) != 0) {
759         error_setg_errno(errp, errno, "mprotect for jit splitwx");
760         munmap((void *)buf_rx, size);
761         munmap((void *)buf_rw, size);
762         return -1;
763     }
764
765     tcg_splitwx_diff = buf_rx - buf_rw;
766     return PROT_READ | PROT_WRITE;
767 }
768 #endif /* CONFIG_DARWIN */
769 #endif /* CONFIG_TCG_INTERPRETER */
770
771 static int alloc_code_gen_buffer_splitwx(size_t size, Error **errp)
772 {
773 #ifndef CONFIG_TCG_INTERPRETER
774 # ifdef CONFIG_DARWIN
775     return alloc_code_gen_buffer_splitwx_vmremap(size, errp);
776 # endif
777 # ifdef CONFIG_POSIX
778     return alloc_code_gen_buffer_splitwx_memfd(size, errp);
779 # endif
780 #endif
781     error_setg(errp, "jit split-wx not supported");
782     return -1;
783 }
784
785 static int alloc_code_gen_buffer(size_t size, int splitwx, Error **errp)
786 {
787     ERRP_GUARD();
788     int prot, flags;
789
790     if (splitwx) {
791         prot = alloc_code_gen_buffer_splitwx(size, errp);
792         if (prot >= 0) {
793             return prot;
794         }
795         /*
796          * If splitwx force-on (1), fail;
797          * if splitwx default-on (-1), fall through to splitwx off.
798          */
799         if (splitwx > 0) {
800             return -1;
801         }
802         error_free_or_abort(errp);
803     }
804
805     prot = PROT_READ | PROT_WRITE | PROT_EXEC;
806     flags = MAP_PRIVATE | MAP_ANONYMOUS;
807 #ifdef CONFIG_TCG_INTERPRETER
808     /* The tcg interpreter does not need execute permission. */
809     prot = PROT_READ | PROT_WRITE;
810 #elif defined(CONFIG_DARWIN)
811     /* Applicable to both iOS and macOS (Apple Silicon). */
812     if (!splitwx) {
813         flags |= MAP_JIT;
814     }
815 #endif
816
817     return alloc_code_gen_buffer_anon(size, prot, flags, errp);
818 }
819 #endif /* USE_STATIC_CODE_GEN_BUFFER, WIN32, POSIX */
820
821 /*
822  * Initializes region partitioning.
823  *
824  * Called at init time from the parent thread (i.e. the one calling
825  * tcg_context_init), after the target's TCG globals have been set.
826  *
827  * Region partitioning works by splitting code_gen_buffer into separate regions,
828  * and then assigning regions to TCG threads so that the threads can translate
829  * code in parallel without synchronization.
830  *
831  * In softmmu the number of TCG threads is bounded by max_cpus, so we use at
832  * least max_cpus regions in MTTCG. In !MTTCG we use a single region.
833  * Note that the TCG options from the command-line (i.e. -accel accel=tcg,[...])
834  * must have been parsed before calling this function, since it calls
835  * qemu_tcg_mttcg_enabled().
836  *
837  * In user-mode we use a single region.  Having multiple regions in user-mode
838  * is not supported, because the number of vCPU threads (recall that each thread
839  * spawned by the guest corresponds to a vCPU thread) is only bounded by the
840  * OS, and usually this number is huge (tens of thousands is not uncommon).
841  * Thus, given this large bound on the number of vCPU threads and the fact
842  * that code_gen_buffer is allocated at compile-time, we cannot guarantee
843  * that the availability of at least one region per vCPU thread.
844  *
845  * However, this user-mode limitation is unlikely to be a significant problem
846  * in practice. Multi-threaded guests share most if not all of their translated
847  * code, which makes parallel code generation less appealing than in softmmu.
848  */
849 void tcg_region_init(size_t tb_size, int splitwx, unsigned max_cpus)
850 {
851     size_t page_size;
852     size_t region_size;
853     size_t i;
854     int have_prot;
855
856     have_prot = alloc_code_gen_buffer(size_code_gen_buffer(tb_size),
857                                       splitwx, &error_fatal);
858     assert(have_prot >= 0);
859
860     /*
861      * Make region_size a multiple of page_size, using aligned as the start.
862      * As a result of this we might end up with a few extra pages at the end of
863      * the buffer; we will assign those to the last region.
864      */
865     region.n = tcg_n_regions(region.total_size, max_cpus);
866     page_size = qemu_real_host_page_size;
867     region_size = region.total_size / region.n;
868     region_size = QEMU_ALIGN_DOWN(region_size, page_size);
869
870     /* A region must have at least 2 pages; one code, one guard */
871     g_assert(region_size >= 2 * page_size);
872     region.stride = region_size;
873
874     /* Reserve space for guard pages. */
875     region.size = region_size - page_size;
876     region.total_size -= page_size;
877
878     /*
879      * The first region will be smaller than the others, via the prologue,
880      * which has yet to be allocated.  For now, the first region begins at
881      * the page boundary.
882      */
883     region.after_prologue = region.start_aligned;
884
885     /* init the region struct */
886     qemu_mutex_init(&region.lock);
887
888     /*
889      * Set guard pages in the rw buffer, as that's the one into which
890      * buffer overruns could occur.  Do not set guard pages in the rx
891      * buffer -- let that one use hugepages throughout.
892      */
893     for (i = 0; i < region.n; i++) {
894         void *start, *end;
895
896         tcg_region_bounds(i, &start, &end);
897
898         /*
899          * macOS 11.2 has a bug (Apple Feedback FB8994773) in which mprotect
900          * rejects a permission change from RWX -> NONE.  Guard pages are
901          * nice for bug detection but are not essential; ignore any failure.
902          */
903         (void)qemu_mprotect_none(end, page_size);
904     }
905
906     tcg_region_trees_init();
907
908     /*
909      * Leave the initial context initialized to the first region.
910      * This will be the context into which we generate the prologue.
911      * It is also the only context for CONFIG_USER_ONLY.
912      */
913     tcg_region_initial_alloc__locked(&tcg_init_ctx);
914 }
915
916 void tcg_region_prologue_set(TCGContext *s)
917 {
918     /* Deduct the prologue from the first region.  */
919     g_assert(region.start_aligned == s->code_gen_buffer);
920     region.after_prologue = s->code_ptr;
921
922     /* Recompute boundaries of the first region. */
923     tcg_region_assign(s, 0);
924
925     /* Register the balance of the buffer with gdb. */
926     tcg_register_jit(tcg_splitwx_to_rx(region.after_prologue),
927                      region.start_aligned + region.total_size -
928                      region.after_prologue);
929 }
930
931 /*
932  * Returns the size (in bytes) of all translated code (i.e. from all regions)
933  * currently in the cache.
934  * See also: tcg_code_capacity()
935  * Do not confuse with tcg_current_code_size(); that one applies to a single
936  * TCG context.
937  */
938 size_t tcg_code_size(void)
939 {
940     unsigned int n_ctxs = qatomic_read(&tcg_cur_ctxs);
941     unsigned int i;
942     size_t total;
943
944     qemu_mutex_lock(&region.lock);
945     total = region.agg_size_full;
946     for (i = 0; i < n_ctxs; i++) {
947         const TCGContext *s = qatomic_read(&tcg_ctxs[i]);
948         size_t size;
949
950         size = qatomic_read(&s->code_gen_ptr) - s->code_gen_buffer;
951         g_assert(size <= s->code_gen_buffer_size);
952         total += size;
953     }
954     qemu_mutex_unlock(&region.lock);
955     return total;
956 }
957
958 /*
959  * Returns the code capacity (in bytes) of the entire cache, i.e. including all
960  * regions.
961  * See also: tcg_code_size()
962  */
963 size_t tcg_code_capacity(void)
964 {
965     size_t guard_size, capacity;
966
967     /* no need for synchronization; these variables are set at init time */
968     guard_size = region.stride - region.size;
969     capacity = region.total_size;
970     capacity -= (region.n - 1) * guard_size;
971     capacity -= region.n * TCG_HIGHWATER;
972
973     return capacity;
974 }
975
976 size_t tcg_tb_phys_invalidate_count(void)
977 {
978     unsigned int n_ctxs = qatomic_read(&tcg_cur_ctxs);
979     unsigned int i;
980     size_t total = 0;
981
982     for (i = 0; i < n_ctxs; i++) {
983         const TCGContext *s = qatomic_read(&tcg_ctxs[i]);
984
985         total += qatomic_read(&s->tb_phys_invalidate_count);
986     }
987     return total;
988 }