Merge remote-tracking branch 'remotes/kraxel/tags/ui-20210304-pull-request' into...
[qemu.git] / exec-vary.c
bloba603b1b4336cb004f3c68fc98994d90c1e7811f8
1 /*
2 * Variable page size handling
4 * Copyright (c) 2003 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 #include "qemu/osdep.h"
21 #include "qemu-common.h"
23 #define IN_EXEC_VARY 1
25 #include "exec/exec-all.h"
27 #ifdef TARGET_PAGE_BITS_VARY
28 # ifdef CONFIG_ATTRIBUTE_ALIAS
30 * We want to declare the "target_page" variable as const, which tells
31 * the compiler that it can cache any value that it reads across calls.
32 * This avoids multiple assertions and multiple reads within any one user.
34 * This works because we finish initializing the data before we ever read
35 * from the "target_page" symbol.
37 * This also requires that we have a non-constant symbol by which we can
38 * perform the actual initialization, and which forces the data to be
39 * allocated within writable memory. Thus "init_target_page", and we use
40 * that symbol exclusively in the two functions that initialize this value.
42 * The "target_page" symbol is created as an alias of "init_target_page".
44 static TargetPageBits init_target_page;
47 * Note that this is *not* a redundant decl, this is the definition of
48 * the "target_page" symbol. The syntax for this definition requires
49 * the use of the extern keyword. This seems to be a GCC bug in
50 * either the syntax for the alias attribute or in -Wredundant-decls.
52 * See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91765
54 # pragma GCC diagnostic push
55 # pragma GCC diagnostic ignored "-Wredundant-decls"
57 extern const TargetPageBits target_page
58 __attribute__((alias("init_target_page")));
60 # pragma GCC diagnostic pop
61 # else
63 * When aliases are not supported then we force two different declarations,
64 * by way of suppressing the header declaration with IN_EXEC_VARY.
65 * We assume that on such an old compiler, LTO cannot be used, and so the
66 * compiler cannot not detect the mismatched declarations, and all is well.
68 TargetPageBits target_page;
69 # define init_target_page target_page
70 # endif
71 #endif
73 bool set_preferred_target_page_bits(int bits)
76 * The target page size is the lowest common denominator for all
77 * the CPUs in the system, so we can only make it smaller, never
78 * larger. And we can't make it smaller once we've committed to
79 * a particular size.
81 #ifdef TARGET_PAGE_BITS_VARY
82 assert(bits >= TARGET_PAGE_BITS_MIN);
83 if (init_target_page.bits == 0 || init_target_page.bits > bits) {
84 if (init_target_page.decided) {
85 return false;
87 init_target_page.bits = bits;
89 #endif
90 return true;
93 void finalize_target_page_bits(void)
95 #ifdef TARGET_PAGE_BITS_VARY
96 if (init_target_page.bits == 0) {
97 init_target_page.bits = TARGET_PAGE_BITS_MIN;
99 init_target_page.mask = (target_long)-1 << init_target_page.bits;
100 init_target_page.decided = true;
103 * For the benefit of an -flto build, prevent the compiler from
104 * hoisting a read from target_page before we finish initializing.
106 barrier();
107 #endif