tnum.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* tnum: tracked (or tristate) numbers
  2. *
  3. * A tnum tracks knowledge about the bits of a value. Each bit can be either
  4. * known (0 or 1), or unknown (x). Arithmetic operations on tnums will
  5. * propagate the unknown bits such that the tnum result represents all the
  6. * possible results for possible values of the operands.
  7. */
  8. #ifndef _LINUX_TNUM_H
  9. #define _LINUX_TNUM_H
  10. #include <linux/types.h>
  11. struct tnum {
  12. u64 value;
  13. u64 mask;
  14. };
  15. /* Constructors */
  16. /* Represent a known constant as a tnum. */
  17. struct tnum tnum_const(u64 value);
  18. /* A completely unknown value */
  19. extern const struct tnum tnum_unknown;
  20. /* An unknown value that is a superset of @min <= value <= @max.
  21. *
  22. * Could include values outside the range of [@min, @max].
  23. * For example tnum_range(0, 2) is represented by {0, 1, 2, *3*},
  24. * rather than the intended set of {0, 1, 2}.
  25. */
  26. struct tnum tnum_range(u64 min, u64 max);
  27. /* Arithmetic and logical ops */
  28. /* Shift a tnum left (by a fixed shift) */
  29. struct tnum tnum_lshift(struct tnum a, u8 shift);
  30. /* Shift (rsh) a tnum right (by a fixed shift) */
  31. struct tnum tnum_rshift(struct tnum a, u8 shift);
  32. /* Shift (arsh) a tnum right (by a fixed min_shift) */
  33. struct tnum tnum_arshift(struct tnum a, u8 min_shift, u8 insn_bitness);
  34. /* Add two tnums, return @a + @b */
  35. struct tnum tnum_add(struct tnum a, struct tnum b);
  36. /* Subtract two tnums, return @a - @b */
  37. struct tnum tnum_sub(struct tnum a, struct tnum b);
  38. /* Bitwise-AND, return @a & @b */
  39. struct tnum tnum_and(struct tnum a, struct tnum b);
  40. /* Bitwise-OR, return @a | @b */
  41. struct tnum tnum_or(struct tnum a, struct tnum b);
  42. /* Bitwise-XOR, return @a ^ @b */
  43. struct tnum tnum_xor(struct tnum a, struct tnum b);
  44. /* Multiply two tnums, return @a * @b */
  45. struct tnum tnum_mul(struct tnum a, struct tnum b);
  46. /* Return a tnum representing numbers satisfying both @a and @b */
  47. struct tnum tnum_intersect(struct tnum a, struct tnum b);
  48. /* Return @a with all but the lowest @size bytes cleared */
  49. struct tnum tnum_cast(struct tnum a, u8 size);
  50. /* Returns true if @a is a known constant */
  51. static inline bool tnum_is_const(struct tnum a)
  52. {
  53. return !a.mask;
  54. }
  55. /* Returns true if @a == tnum_const(@b) */
  56. static inline bool tnum_equals_const(struct tnum a, u64 b)
  57. {
  58. return tnum_is_const(a) && a.value == b;
  59. }
  60. /* Returns true if @a is completely unknown */
  61. static inline bool tnum_is_unknown(struct tnum a)
  62. {
  63. return !~a.mask;
  64. }
  65. /* Returns true if @a is known to be a multiple of @size.
  66. * @size must be a power of two.
  67. */
  68. bool tnum_is_aligned(struct tnum a, u64 size);
  69. /* Returns true if @b represents a subset of @a.
  70. *
  71. * Note that using tnum_range() as @a requires extra cautions as tnum_in() may
  72. * return true unexpectedly due to tnum limited ability to represent tight
  73. * range, e.g.
  74. *
  75. * tnum_in(tnum_range(0, 2), tnum_const(3)) == true
  76. *
  77. * As a rule of thumb, if @a is explicitly coded rather than coming from
  78. * reg->var_off, it should be in form of tnum_const(), tnum_range(0, 2**n - 1),
  79. * or tnum_range(2**n, 2**(n+1) - 1).
  80. */
  81. bool tnum_in(struct tnum a, struct tnum b);
  82. /* Formatting functions. These have snprintf-like semantics: they will write
  83. * up to @size bytes (including the terminating NUL byte), and return the number
  84. * of bytes (excluding the terminating NUL) which would have been written had
  85. * sufficient space been available. (Thus tnum_sbin always returns 64.)
  86. */
  87. /* Format a tnum as a pair of hex numbers (value; mask) */
  88. int tnum_strn(char *str, size_t size, struct tnum a);
  89. /* Format a tnum as tristate binary expansion */
  90. int tnum_sbin(char *str, size_t size, struct tnum a);
  91. /* Returns the 32-bit subreg */
  92. struct tnum tnum_subreg(struct tnum a);
  93. /* Returns the tnum with the lower 32-bit subreg cleared */
  94. struct tnum tnum_clear_subreg(struct tnum a);
  95. /* Returns the tnum with the lower 32-bit subreg set to value */
  96. struct tnum tnum_const_subreg(struct tnum a, u32 value);
  97. /* Returns true if 32-bit subreg @a is a known constant*/
  98. static inline bool tnum_subreg_is_const(struct tnum a)
  99. {
  100. return !(tnum_subreg(a)).mask;
  101. }
  102. #endif /* _LINUX_TNUM_H */