gfp-translate 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #!/bin/bash
  2. # SPDX-License-Identifier: GPL-2.0-only
  3. # Translate the bits making up a GFP mask
  4. # (c) 2009, Mel Gorman <mel@csn.ul.ie>
  5. SOURCE=
  6. GFPMASK=none
  7. # Helper function to report failures and exit
  8. die() {
  9. echo ERROR: $@
  10. if [ "$TMPFILE" != "" ]; then
  11. rm -f $TMPFILE
  12. fi
  13. exit -1
  14. }
  15. usage() {
  16. echo "usage: gfp-translate [-h] [ --source DIRECTORY ] gfpmask"
  17. exit 0
  18. }
  19. # Parse command-line arguments
  20. while [ $# -gt 0 ]; do
  21. case $1 in
  22. --source)
  23. SOURCE=$2
  24. shift 2
  25. ;;
  26. -h)
  27. usage
  28. ;;
  29. --help)
  30. usage
  31. ;;
  32. *)
  33. GFPMASK=$1
  34. shift
  35. ;;
  36. esac
  37. done
  38. # Guess the kernel source directory if it's not set. Preference is in order of
  39. # o current directory
  40. # o /usr/src/linux
  41. if [ "$SOURCE" = "" ]; then
  42. if [ -r "/usr/src/linux/Makefile" ]; then
  43. SOURCE=/usr/src/linux
  44. fi
  45. if [ -r "`pwd`/Makefile" ]; then
  46. SOURCE=`pwd`
  47. fi
  48. fi
  49. # Confirm that a source directory exists
  50. if [ ! -r "$SOURCE/Makefile" ]; then
  51. die "Could not locate kernel source directory or it is invalid"
  52. fi
  53. # Confirm that a GFP mask has been specified
  54. if [ "$GFPMASK" = "none" ]; then
  55. usage
  56. fi
  57. # Extract GFP flags from the kernel source
  58. TMPFILE=`mktemp -t gfptranslate-XXXXXX.c` || exit 1
  59. echo Source: $SOURCE
  60. echo Parsing: $GFPMASK
  61. (
  62. cat <<EOF
  63. #include <stdint.h>
  64. #include <stdio.h>
  65. // Try to fool compiler.h into not including extra stuff
  66. #define __ASSEMBLY__ 1
  67. #include <generated/autoconf.h>
  68. #include <linux/gfp_types.h>
  69. static const char *masks[] = {
  70. EOF
  71. sed -nEe 's/^[[:space:]]+(___GFP_.*)_BIT,.*$/\1/p' $SOURCE/include/linux/gfp_types.h |
  72. while read b; do
  73. cat <<EOF
  74. #if defined($b) && ($b > 0)
  75. [${b}_BIT] = "$b",
  76. #endif
  77. EOF
  78. done
  79. cat <<EOF
  80. };
  81. int main(int argc, char *argv[])
  82. {
  83. unsigned long long mask = $GFPMASK;
  84. for (int i = 0; i < sizeof(mask) * 8; i++) {
  85. unsigned long long bit = 1ULL << i;
  86. if (mask & bit)
  87. printf("\t%-25s0x%llx\n",
  88. (i < ___GFP_LAST_BIT && masks[i]) ?
  89. masks[i] : "*** INVALID ***",
  90. bit);
  91. }
  92. return 0;
  93. }
  94. EOF
  95. ) > $TMPFILE
  96. ${CC:-gcc} -Wall -o ${TMPFILE}.bin -I $SOURCE/include $TMPFILE && ${TMPFILE}.bin
  97. rm -f $TMPFILE ${TMPFILE}.bin
  98. exit 0