test_xcall.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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/init.h>
  29. #include <kern/log.h>
  30. #include <kern/panic.h>
  31. #include <kern/thread.h>
  32. #include <kern/xcall.h>
  33. #include <test/test.h>
  34. struct test_data
  35. {
  36. uint32_t cpu;
  37. bool done;
  38. };
  39. static void
  40. test_fn (void *arg)
  41. {
  42. assert (thread_interrupted ());
  43. struct test_data *data = arg;
  44. if (data->cpu != cpu_id ())
  45. panic ("test: invalid cpu");
  46. log_info ("function called, running on cpu%u\n", cpu_id ());
  47. data->done = true;
  48. }
  49. static void
  50. test_once (uint32_t cpu)
  51. {
  52. struct test_data data = { .cpu = cpu, .done = false };
  53. log_info ("cross-call: cpu%u -> cpu%u:\n", cpu_id (), cpu);
  54. xcall_call (test_fn, &data, cpu);
  55. if (!data.done)
  56. panic ("test: xcall failed");
  57. }
  58. static void
  59. test_run_cpu (void *arg __unused)
  60. {
  61. for (uint32_t i = cpu_count () - 1; i < cpu_count (); --i)
  62. test_once (i);
  63. }
  64. static void
  65. async_xcall_test_fn (void *arg)
  66. {
  67. log_info ("async xcall: %d\n", *(int *)arg);
  68. *(int *)arg = -1;
  69. }
  70. static void
  71. test_async_xcall_run (void *arg __unused)
  72. {
  73. struct xcall_async async;
  74. int value = 42;
  75. xcall_async_init (&async, async_xcall_test_fn, &value, 0);
  76. xcall_async_call (&async);
  77. xcall_async_wait (&async);
  78. assert (value == -1);
  79. log_info ("test (async-xcall): done");
  80. }
  81. static void
  82. test_async_xcall (void)
  83. {
  84. struct cpumap *cpumap;
  85. if (cpumap_create (&cpumap) != 0)
  86. {
  87. log_err ("failed to allocate cpumap");
  88. return;
  89. }
  90. struct thread_attr attr;
  91. thread_attr_init (&attr, THREAD_KERNEL_PREFIX "test_asyncx");
  92. cpumap_zero (cpumap);
  93. cpumap_set (cpumap, 1);
  94. thread_attr_set_cpumap (&attr, cpumap);
  95. thread_attr_set_detached (&attr);
  96. if (thread_create (NULL, &attr, test_async_xcall_run, NULL) != 0)
  97. log_err ("failed to create thread for async xcall");
  98. }
  99. TEST_DEFERRED (xcall)
  100. {
  101. struct cpumap *cpumap;
  102. int error = cpumap_create (&cpumap);
  103. test_assert_zero (error);
  104. for (uint32_t i = 0; i < cpu_count (); i++)
  105. {
  106. /*
  107. * Send IPIs from CPU 1 first, in order to better trigger any
  108. * initialization race that may prevent correct IPI transmission.
  109. * This assumes CPUs are initialized sequentially, and that CPU 1
  110. * may have finished initialization much earlier than the last CPU.
  111. * CPU 0 isn't used since it's the one normally initializing remote
  112. * CPUs.
  113. */
  114. uint32_t cpu = (1 + i) % cpu_count();
  115. cpumap_zero (cpumap);
  116. cpumap_set (cpumap, cpu);
  117. char name[THREAD_NAME_SIZE];
  118. snprintf (name, sizeof (name), THREAD_KERNEL_PREFIX "test_xcall/%u", cpu);
  119. struct thread_attr attr;
  120. thread_attr_init (&attr, name);
  121. thread_attr_set_cpumap (&attr, cpumap);
  122. struct thread *thread;
  123. error = thread_create (&thread, &attr, test_run_cpu, NULL);
  124. test_assert_zero (error);
  125. thread_join (thread);
  126. }
  127. cpumap_destroy (cpumap);
  128. if (cpu_count () > 1)
  129. test_async_xcall ();
  130. return (TEST_OK);
  131. }