exec-vary.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Variable page size handling
  3. *
  4. * Copyright (c) 2003 Fabrice Bellard
  5. *
  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 of the License, or (at your option) any later version.
  10. *
  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.
  15. *
  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/>.
  18. */
  19. #include "qemu/osdep.h"
  20. #include "qemu-common.h"
  21. #define IN_EXEC_VARY 1
  22. #include "exec/exec-all.h"
  23. #ifdef TARGET_PAGE_BITS_VARY
  24. # ifdef CONFIG_ATTRIBUTE_ALIAS
  25. /*
  26. * We want to declare the "target_page" variable as const, which tells
  27. * the compiler that it can cache any value that it reads across calls.
  28. * This avoids multiple assertions and multiple reads within any one user.
  29. *
  30. * This works because we finish initializing the data before we ever read
  31. * from the "target_page" symbol.
  32. *
  33. * This also requires that we have a non-constant symbol by which we can
  34. * perform the actual initialization, and which forces the data to be
  35. * allocated within writable memory. Thus "init_target_page", and we use
  36. * that symbol exclusively in the two functions that initialize this value.
  37. *
  38. * The "target_page" symbol is created as an alias of "init_target_page".
  39. */
  40. static TargetPageBits init_target_page;
  41. /*
  42. * Note that this is *not* a redundant decl, this is the definition of
  43. * the "target_page" symbol. The syntax for this definition requires
  44. * the use of the extern keyword. This seems to be a GCC bug in
  45. * either the syntax for the alias attribute or in -Wredundant-decls.
  46. *
  47. * See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91765
  48. */
  49. # pragma GCC diagnostic push
  50. # pragma GCC diagnostic ignored "-Wredundant-decls"
  51. extern const TargetPageBits target_page
  52. __attribute__((alias("init_target_page")));
  53. # pragma GCC diagnostic pop
  54. # else
  55. /*
  56. * When aliases are not supported then we force two different declarations,
  57. * by way of suppressing the header declaration with IN_EXEC_VARY.
  58. * We assume that on such an old compiler, LTO cannot be used, and so the
  59. * compiler cannot not detect the mismatched declarations, and all is well.
  60. */
  61. TargetPageBits target_page;
  62. # define init_target_page target_page
  63. # endif
  64. #endif
  65. bool set_preferred_target_page_bits(int bits)
  66. {
  67. /*
  68. * The target page size is the lowest common denominator for all
  69. * the CPUs in the system, so we can only make it smaller, never
  70. * larger. And we can't make it smaller once we've committed to
  71. * a particular size.
  72. */
  73. #ifdef TARGET_PAGE_BITS_VARY
  74. assert(bits >= TARGET_PAGE_BITS_MIN);
  75. if (init_target_page.bits == 0 || init_target_page.bits > bits) {
  76. if (init_target_page.decided) {
  77. return false;
  78. }
  79. init_target_page.bits = bits;
  80. }
  81. #endif
  82. return true;
  83. }
  84. void finalize_target_page_bits(void)
  85. {
  86. #ifdef TARGET_PAGE_BITS_VARY
  87. if (init_target_page.bits == 0) {
  88. init_target_page.bits = TARGET_PAGE_BITS_MIN;
  89. }
  90. init_target_page.mask = (target_long)-1 << init_target_page.bits;
  91. init_target_page.decided = true;
  92. /*
  93. * For the benefit of an -flto build, prevent the compiler from
  94. * hoisting a read from target_page before we finish initializing.
  95. */
  96. barrier();
  97. #endif
  98. }