test_xcall.c 3.9 KB

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