translation-block.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* SPDX-License-Identifier: LGPL-2.1-or-later */
  2. /*
  3. * Definition of TranslationBlock.
  4. * Copyright (c) 2003 Fabrice Bellard
  5. */
  6. #ifndef EXEC_TRANSLATION_BLOCK_H
  7. #define EXEC_TRANSLATION_BLOCK_H
  8. #include "qemu/atomic.h"
  9. #include "qemu/thread.h"
  10. #include "exec/cpu-common.h"
  11. #include "exec/vaddr.h"
  12. #ifdef CONFIG_USER_ONLY
  13. #include "qemu/interval-tree.h"
  14. #endif
  15. /*
  16. * Page tracking code uses ram addresses in system mode, and virtual
  17. * addresses in userspace mode. Define tb_page_addr_t to be an
  18. * appropriate type.
  19. */
  20. #if defined(CONFIG_USER_ONLY)
  21. typedef vaddr tb_page_addr_t;
  22. #define TB_PAGE_ADDR_FMT "%" VADDR_PRIx
  23. #else
  24. typedef ram_addr_t tb_page_addr_t;
  25. #define TB_PAGE_ADDR_FMT RAM_ADDR_FMT
  26. #endif
  27. /*
  28. * Translation Cache-related fields of a TB.
  29. * This struct exists just for convenience; we keep track of TB's in a binary
  30. * search tree, and the only fields needed to compare TB's in the tree are
  31. * @ptr and @size.
  32. * Note: the address of search data can be obtained by adding @size to @ptr.
  33. */
  34. struct tb_tc {
  35. const void *ptr; /* pointer to the translated code */
  36. size_t size;
  37. };
  38. struct TranslationBlock {
  39. /*
  40. * Guest PC corresponding to this block. This must be the true
  41. * virtual address. Therefore e.g. x86 stores EIP + CS_BASE, and
  42. * targets like Arm, MIPS, HP-PA, which reuse low bits for ISA or
  43. * privilege, must store those bits elsewhere.
  44. *
  45. * If CF_PCREL, the opcodes for the TranslationBlock are written
  46. * such that the TB is associated only with the physical page and
  47. * may be run in any virtual address context. In this case, PC
  48. * must always be taken from ENV in a target-specific manner.
  49. * Unwind information is taken as offsets from the page, to be
  50. * deposited into the "current" PC.
  51. */
  52. vaddr pc;
  53. /*
  54. * Target-specific data associated with the TranslationBlock, e.g.:
  55. * x86: the original user, the Code Segment virtual base,
  56. * arm: an extension of tb->flags,
  57. * s390x: instruction data for EXECUTE,
  58. * sparc: the next pc of the instruction queue (for delay slots).
  59. */
  60. uint64_t cs_base;
  61. uint32_t flags; /* flags defining in which context the code was generated */
  62. uint32_t cflags; /* compile flags */
  63. /* Note that TCG_MAX_INSNS is 512; we validate this match elsewhere. */
  64. #define CF_COUNT_MASK 0x000001ff
  65. #define CF_NO_GOTO_TB 0x00000200 /* Do not chain with goto_tb */
  66. #define CF_NO_GOTO_PTR 0x00000400 /* Do not chain with goto_ptr */
  67. #define CF_SINGLE_STEP 0x00000800 /* gdbstub single-step in effect */
  68. #define CF_MEMI_ONLY 0x00001000 /* Only instrument memory ops */
  69. #define CF_USE_ICOUNT 0x00002000
  70. #define CF_INVALID 0x00004000 /* TB is stale. Set with @jmp_lock held */
  71. #define CF_PARALLEL 0x00008000 /* Generate code for a parallel context */
  72. #define CF_NOIRQ 0x00010000 /* Generate an uninterruptible TB */
  73. #define CF_PCREL 0x00020000 /* Opcodes in TB are PC-relative */
  74. #define CF_BP_PAGE 0x00040000 /* Breakpoint present in code page */
  75. #define CF_CLUSTER_MASK 0xff000000 /* Top 8 bits are cluster ID */
  76. #define CF_CLUSTER_SHIFT 24
  77. /*
  78. * Above fields used for comparing
  79. */
  80. /* size of target code for this block (1 <= size <= TARGET_PAGE_SIZE) */
  81. uint16_t size;
  82. uint16_t icount;
  83. struct tb_tc tc;
  84. /*
  85. * Track tb_page_addr_t intervals that intersect this TB.
  86. * For user-only, the virtual addresses are always contiguous,
  87. * and we use a unified interval tree. For system, we use a
  88. * linked list headed in each PageDesc. Within the list, the lsb
  89. * of the previous pointer tells the index of page_next[], and the
  90. * list is protected by the PageDesc lock(s).
  91. */
  92. #ifdef CONFIG_USER_ONLY
  93. IntervalTreeNode itree;
  94. #else
  95. uintptr_t page_next[2];
  96. tb_page_addr_t page_addr[2];
  97. #endif
  98. /* jmp_lock placed here to fill a 4-byte hole. Its documentation is below */
  99. QemuSpin jmp_lock;
  100. /* The following data are used to directly call another TB from
  101. * the code of this one. This can be done either by emitting direct or
  102. * indirect native jump instructions. These jumps are reset so that the TB
  103. * just continues its execution. The TB can be linked to another one by
  104. * setting one of the jump targets (or patching the jump instruction). Only
  105. * two of such jumps are supported.
  106. */
  107. #define TB_JMP_OFFSET_INVALID 0xffff /* indicates no jump generated */
  108. uint16_t jmp_reset_offset[2]; /* offset of original jump target */
  109. uint16_t jmp_insn_offset[2]; /* offset of direct jump insn */
  110. uintptr_t jmp_target_addr[2]; /* target address */
  111. /*
  112. * Each TB has a NULL-terminated list (jmp_list_head) of incoming jumps.
  113. * Each TB can have two outgoing jumps, and therefore can participate
  114. * in two lists. The list entries are kept in jmp_list_next[2]. The least
  115. * significant bit (LSB) of the pointers in these lists is used to encode
  116. * which of the two list entries is to be used in the pointed TB.
  117. *
  118. * List traversals are protected by jmp_lock. The destination TB of each
  119. * outgoing jump is kept in jmp_dest[] so that the appropriate jmp_lock
  120. * can be acquired from any origin TB.
  121. *
  122. * jmp_dest[] are tagged pointers as well. The LSB is set when the TB is
  123. * being invalidated, so that no further outgoing jumps from it can be set.
  124. *
  125. * jmp_lock also protects the CF_INVALID cflag; a jump must not be chained
  126. * to a destination TB that has CF_INVALID set.
  127. */
  128. uintptr_t jmp_list_head;
  129. uintptr_t jmp_list_next[2];
  130. uintptr_t jmp_dest[2];
  131. };
  132. /* The alignment given to TranslationBlock during allocation. */
  133. #define CODE_GEN_ALIGN 16
  134. /* Hide the qatomic_read to make code a little easier on the eyes */
  135. static inline uint32_t tb_cflags(const TranslationBlock *tb)
  136. {
  137. return qatomic_read(&tb->cflags);
  138. }
  139. bool tcg_cflags_has(CPUState *cpu, uint32_t flags);
  140. void tcg_cflags_set(CPUState *cpu, uint32_t flags);
  141. #endif /* EXEC_TRANSLATION_BLOCK_H */