test_string.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #include <linux/module.h>
  2. #include <linux/printk.h>
  3. #include <linux/slab.h>
  4. #include <linux/string.h>
  5. static __init int memset16_selftest(void)
  6. {
  7. unsigned i, j, k;
  8. u16 v, *p;
  9. p = kmalloc(256 * 2 * 2, GFP_KERNEL);
  10. if (!p)
  11. return -1;
  12. for (i = 0; i < 256; i++) {
  13. for (j = 0; j < 256; j++) {
  14. memset(p, 0xa1, 256 * 2 * sizeof(v));
  15. memset16(p + i, 0xb1b2, j);
  16. for (k = 0; k < 512; k++) {
  17. v = p[k];
  18. if (k < i) {
  19. if (v != 0xa1a1)
  20. goto fail;
  21. } else if (k < i + j) {
  22. if (v != 0xb1b2)
  23. goto fail;
  24. } else {
  25. if (v != 0xa1a1)
  26. goto fail;
  27. }
  28. }
  29. }
  30. }
  31. fail:
  32. kfree(p);
  33. if (i < 256)
  34. return (i << 24) | (j << 16) | k | 0x8000;
  35. return 0;
  36. }
  37. static __init int memset32_selftest(void)
  38. {
  39. unsigned i, j, k;
  40. u32 v, *p;
  41. p = kmalloc(256 * 2 * 4, GFP_KERNEL);
  42. if (!p)
  43. return -1;
  44. for (i = 0; i < 256; i++) {
  45. for (j = 0; j < 256; j++) {
  46. memset(p, 0xa1, 256 * 2 * sizeof(v));
  47. memset32(p + i, 0xb1b2b3b4, j);
  48. for (k = 0; k < 512; k++) {
  49. v = p[k];
  50. if (k < i) {
  51. if (v != 0xa1a1a1a1)
  52. goto fail;
  53. } else if (k < i + j) {
  54. if (v != 0xb1b2b3b4)
  55. goto fail;
  56. } else {
  57. if (v != 0xa1a1a1a1)
  58. goto fail;
  59. }
  60. }
  61. }
  62. }
  63. fail:
  64. kfree(p);
  65. if (i < 256)
  66. return (i << 24) | (j << 16) | k | 0x8000;
  67. return 0;
  68. }
  69. static __init int memset64_selftest(void)
  70. {
  71. unsigned i, j, k;
  72. u64 v, *p;
  73. p = kmalloc(256 * 2 * 8, GFP_KERNEL);
  74. if (!p)
  75. return -1;
  76. for (i = 0; i < 256; i++) {
  77. for (j = 0; j < 256; j++) {
  78. memset(p, 0xa1, 256 * 2 * sizeof(v));
  79. memset64(p + i, 0xb1b2b3b4b5b6b7b8ULL, j);
  80. for (k = 0; k < 512; k++) {
  81. v = p[k];
  82. if (k < i) {
  83. if (v != 0xa1a1a1a1a1a1a1a1ULL)
  84. goto fail;
  85. } else if (k < i + j) {
  86. if (v != 0xb1b2b3b4b5b6b7b8ULL)
  87. goto fail;
  88. } else {
  89. if (v != 0xa1a1a1a1a1a1a1a1ULL)
  90. goto fail;
  91. }
  92. }
  93. }
  94. }
  95. fail:
  96. kfree(p);
  97. if (i < 256)
  98. return (i << 24) | (j << 16) | k | 0x8000;
  99. return 0;
  100. }
  101. static __init int string_selftest_init(void)
  102. {
  103. int test, subtest;
  104. test = 1;
  105. subtest = memset16_selftest();
  106. if (subtest)
  107. goto fail;
  108. test = 2;
  109. subtest = memset32_selftest();
  110. if (subtest)
  111. goto fail;
  112. test = 3;
  113. subtest = memset64_selftest();
  114. if (subtest)
  115. goto fail;
  116. pr_info("String selftests succeeded\n");
  117. return 0;
  118. fail:
  119. pr_crit("String selftest failure %d.%08x\n", test, subtest);
  120. return 0;
  121. }
  122. module_init(string_selftest_init);
  123. MODULE_LICENSE("GPL v2");