test_xcall.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright (c) 2014-2018 Richard Braun.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. *
  18. * This test creates a thread that tests cross-calls for all combinations
  19. * of processors. This thread sequentially creates other threads that are
  20. * bound to a single processor, and perform cross-calls to all processors,
  21. * including the local one.
  22. */
  23. #include <assert.h>
  24. #include <stdbool.h>
  25. #include <stddef.h>
  26. #include <stdio.h>
  27. #include <kern/cpumap.h>
  28. #include <kern/error.h>
  29. #include <kern/init.h>
  30. #include <kern/log.h>
  31. #include <kern/panic.h>
  32. #include <kern/thread.h>
  33. #include <kern/xcall.h>
  34. #include <test/test.h>
  35. struct test_data {
  36. unsigned int cpu;
  37. bool done;
  38. };
  39. static void
  40. test_fn(void *arg)
  41. {
  42. struct test_data *data;
  43. assert(thread_interrupted());
  44. data = arg;
  45. if (data->cpu != cpu_id()) {
  46. panic("test: invalid cpu");
  47. }
  48. log_info("function called, running on cpu%u\n", cpu_id());
  49. data->done = true;
  50. }
  51. static void
  52. test_once(unsigned int cpu)
  53. {
  54. struct test_data data;
  55. data.cpu = cpu;
  56. data.done = false;
  57. log_info("cross-call: cpu%u -> cpu%u:\n", cpu_id(), cpu);
  58. xcall_call(test_fn, &data, cpu);
  59. if (!data.done) {
  60. panic("test: xcall failed");
  61. }
  62. }
  63. static void
  64. test_run_cpu(void *arg)
  65. {
  66. (void)arg;
  67. for (unsigned int i = (cpu_count() - 1); i < cpu_count(); i--) {
  68. test_once(i);
  69. }
  70. }
  71. static void
  72. test_run(void *arg)
  73. {
  74. char name[THREAD_NAME_SIZE];
  75. struct thread_attr attr;
  76. struct thread *thread;
  77. struct cpumap *cpumap;
  78. unsigned int cpu;
  79. int error;
  80. (void)arg;
  81. error = cpumap_create(&cpumap);
  82. error_check(error, "cpumap_create");
  83. for (unsigned int i = 0; i < cpu_count(); i++) {
  84. /*
  85. * Send IPIs from CPU 1 first, in order to better trigger any
  86. * initialization race that may prevent correct IPI transmission.
  87. * This assumes CPUs are initialized sequentially, and that CPU 1
  88. * may have finished initialization much earlier than the last CPU.
  89. * CPU 0 isn't used since it's the one normally initializing remote
  90. * CPUs.
  91. */
  92. cpu = (1 + i) % cpu_count();
  93. cpumap_zero(cpumap);
  94. cpumap_set(cpumap, cpu);
  95. snprintf(name, sizeof(name), THREAD_KERNEL_PREFIX "test_run/%u", cpu);
  96. thread_attr_init(&attr, name);
  97. thread_attr_set_cpumap(&attr, cpumap);
  98. error = thread_create(&thread, &attr, test_run_cpu, NULL);
  99. error_check(error, "thread_create");
  100. thread_join(thread);
  101. }
  102. cpumap_destroy(cpumap);
  103. log_info("done\n");
  104. }
  105. void __init
  106. test_setup(void)
  107. {
  108. struct thread_attr attr;
  109. struct thread *thread;
  110. int error;
  111. thread_attr_init(&attr, THREAD_KERNEL_PREFIX "test_run");
  112. thread_attr_set_detached(&attr);
  113. error = thread_create(&thread, &attr, test_run, NULL);
  114. error_check(error, "thread_create");
  115. }