test_vm_page_fill.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (c) 2016-2017 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 show whether allocating and
  19. * modifying all available physical pages leads to unexpected problems.
  20. * In particular, if the early allocation code misbehaves, important
  21. * data structures such as kernel page tables may be considered free,
  22. * in which case this test will catch the error.
  23. */
  24. #include <stddef.h>
  25. #include <stdint.h>
  26. #include <stdio.h>
  27. #include <kern/cpumap.h>
  28. #include <kern/error.h>
  29. #include <kern/init.h>
  30. #include <kern/list.h>
  31. #include <kern/thread.h>
  32. #include <machine/page.h>
  33. #include <machine/pmap.h>
  34. #include <test/test.h>
  35. #include <vm/vm_kmem.h>
  36. #include <vm/vm_map.h>
  37. #include <vm/vm_page.h>
  38. static struct list test_pages;
  39. static struct cpumap test_cpumap;
  40. static unsigned char test_pattern = 1;
  41. static void
  42. test_write_pages(void)
  43. {
  44. struct vm_map *kernel_map;
  45. struct pmap *kernel_pmap;
  46. struct vm_page *page;
  47. int error, flags;
  48. uintptr_t va;
  49. kernel_map = vm_map_get_kernel_map();
  50. kernel_pmap = pmap_get_kernel_pmap();
  51. for (;;) {
  52. page = vm_page_alloc(0, VM_PAGE_SEL_HIGHMEM, VM_PAGE_KERNEL);
  53. if (page == NULL) {
  54. break;
  55. }
  56. va = 0;
  57. flags = VM_MAP_FLAGS(VM_PROT_ALL, VM_PROT_ALL, VM_INHERIT_NONE,
  58. VM_ADV_DEFAULT, 0);
  59. error = vm_map_enter(kernel_map, &va, PAGE_SIZE, 0, flags, NULL, 0);
  60. error_check(error, __func__);
  61. error = pmap_enter(kernel_pmap, va, vm_page_to_pa(page),
  62. VM_PROT_READ | VM_PROT_WRITE, 0);
  63. error_check(error, __func__);
  64. error = pmap_update(kernel_pmap);
  65. error_check(error, __func__);
  66. memset((void *)va, test_pattern, PAGE_SIZE);
  67. error = pmap_remove(kernel_pmap, va, &test_cpumap);
  68. error_check(error, __func__);
  69. error = pmap_update(kernel_pmap);
  70. error_check(error, __func__);
  71. vm_map_remove(kernel_map, va, va + PAGE_SIZE);
  72. list_insert_tail(&test_pages, &page->node);
  73. }
  74. }
  75. static void
  76. test_reset_pages(void)
  77. {
  78. struct vm_map *kernel_map;
  79. struct pmap *kernel_pmap;
  80. struct vm_page *page;
  81. int error, flags;
  82. uintptr_t va;
  83. kernel_map = vm_map_get_kernel_map();
  84. kernel_pmap = pmap_get_kernel_pmap();
  85. while (!list_empty(&test_pages)) {
  86. page = list_first_entry(&test_pages, struct vm_page, node);
  87. list_remove(&page->node);
  88. va = 0;
  89. flags = VM_MAP_FLAGS(VM_PROT_ALL, VM_PROT_ALL, VM_INHERIT_NONE,
  90. VM_ADV_DEFAULT, 0);
  91. error = vm_map_enter(kernel_map, &va, PAGE_SIZE, 0, flags, NULL, 0);
  92. error_check(error, __func__);
  93. error = pmap_enter(kernel_pmap, va, vm_page_to_pa(page),
  94. VM_PROT_READ | VM_PROT_WRITE, 0);
  95. error_check(error, __func__);
  96. error = pmap_update(kernel_pmap);
  97. error_check(error, __func__);
  98. memset((void *)va, 0, PAGE_SIZE);
  99. error = pmap_remove(kernel_pmap, va, &test_cpumap);
  100. error_check(error, __func__);
  101. error = pmap_update(kernel_pmap);
  102. error_check(error, __func__);
  103. vm_map_remove(kernel_map, va, va + PAGE_SIZE);
  104. vm_page_free(page, 0);
  105. }
  106. }
  107. static void
  108. test_run(void *arg)
  109. {
  110. unsigned int i;
  111. (void)arg;
  112. for (i = 0; /* no condition */; i++) {
  113. printf("test: pass:%u pattern:%hhx\n", i, test_pattern);
  114. test_write_pages();
  115. test_reset_pages();
  116. test_pattern++;
  117. }
  118. }
  119. void __init
  120. test_setup(void)
  121. {
  122. struct thread_attr attr;
  123. struct thread *thread;
  124. int error;
  125. list_init(&test_pages);
  126. cpumap_zero(&test_cpumap);
  127. cpumap_set(&test_cpumap, cpu_id());
  128. thread_attr_init(&attr, THREAD_KERNEL_PREFIX "test_run");
  129. thread_attr_set_detached(&attr);
  130. thread_attr_set_cpumap(&attr, &test_cpumap);
  131. error = thread_create(&thread, &attr, test_run, NULL);
  132. error_check(error, "thread_create");
  133. }