test_pmap_update_mp.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright (c) 2014 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. * The purpose of this test module is to check that the pmap module properly
  19. * synchronizes page tables across processors. Two threads are created and
  20. * bound to processors 0 and 1 respectively. The first thread allocates a
  21. * page and writes it, making sure physical mappings are up-to-date locally.
  22. * It then transfers the page address to the second thread which validates
  23. * the content of the page, an operation that can only be done if the page
  24. * tables of the current processor have been updated.
  25. */
  26. #include <stddef.h>
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <kern/condition.h>
  30. #include <kern/cpumap.h>
  31. #include <kern/error.h>
  32. #include <kern/init.h>
  33. #include <kern/mutex.h>
  34. #include <kern/panic.h>
  35. #include <kern/thread.h>
  36. #include <machine/page.h>
  37. #include <test/test.h>
  38. #include <vm/vm_kmem.h>
  39. static struct condition test_condition;
  40. static struct mutex test_lock;
  41. static void *test_va;
  42. static void
  43. test_run1(void *arg)
  44. {
  45. void *ptr;
  46. (void)arg;
  47. printf("allocating page\n");
  48. ptr = vm_kmem_alloc(PAGE_SIZE);
  49. printf("writing page\n");
  50. memset(ptr, 'a', PAGE_SIZE);
  51. printf("passing page to second thread (%p)\n", ptr);
  52. mutex_lock(&test_lock);
  53. test_va = ptr;
  54. condition_signal(&test_condition);
  55. mutex_unlock(&test_lock);
  56. }
  57. static void
  58. test_run2(void *arg)
  59. {
  60. char *ptr;
  61. unsigned int i;
  62. (void)arg;
  63. printf("waiting for page\n");
  64. mutex_lock(&test_lock);
  65. while (test_va == NULL) {
  66. condition_wait(&test_condition, &test_lock);
  67. }
  68. ptr = test_va;
  69. mutex_unlock(&test_lock);
  70. printf("page received (%p), checking page\n", ptr);
  71. for (i = 0; i < PAGE_SIZE; i++) {
  72. if (ptr[i] != 'a') {
  73. panic("invalid content");
  74. }
  75. }
  76. vm_kmem_free(ptr, PAGE_SIZE);
  77. printf("done\n");
  78. }
  79. void __init
  80. test_setup(void)
  81. {
  82. struct thread_attr attr;
  83. struct thread *thread;
  84. struct cpumap *cpumap;
  85. int error;
  86. condition_init(&test_condition);
  87. mutex_init(&test_lock);
  88. test_va = NULL;
  89. error = cpumap_create(&cpumap);
  90. error_check(error, "cpumap_create");
  91. cpumap_zero(cpumap);
  92. cpumap_set(cpumap, 0);
  93. thread_attr_init(&attr, THREAD_KERNEL_PREFIX "test_run1");
  94. thread_attr_set_detached(&attr);
  95. thread_attr_set_cpumap(&attr, cpumap);
  96. error = thread_create(&thread, &attr, test_run1, NULL);
  97. error_check(error, "thread_create");
  98. cpumap_zero(cpumap);
  99. cpumap_set(cpumap, 1);
  100. thread_attr_init(&attr, THREAD_KERNEL_PREFIX "test_run2");
  101. thread_attr_set_detached(&attr);
  102. thread_attr_set_cpumap(&attr, cpumap);
  103. error = thread_create(&thread, &attr, test_run2, NULL);
  104. error_check(error, "thread_create");
  105. cpumap_destroy(cpumap);
  106. }