test_vm_page_fill.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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/kmem.h>
  36. #include <vm/map.h>
  37. #include <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. _Auto kernel_map = vm_map_get_kernel_map ();
  45. _Auto kernel_pmap = pmap_get_kernel_pmap ();
  46. while (1)
  47. {
  48. struct vm_page *page = vm_page_alloc (0, VM_PAGE_SEL_HIGHMEM,
  49. VM_PAGE_KERNEL, 0);
  50. if (! page)
  51. break;
  52. uintptr_t va = 0;
  53. int flags = VM_MAP_FLAGS (VM_PROT_RDWR, VM_PROT_RDWR, VM_INHERIT_NONE,
  54. VM_ADV_DEFAULT, 0);
  55. int error = vm_map_enter (kernel_map, &va, PAGE_SIZE, flags, NULL, 0);
  56. error_check (error, __func__);
  57. error = pmap_enter (kernel_pmap, va, vm_page_to_pa (page),
  58. VM_PROT_RDWR, 0);
  59. error_check (error, __func__);
  60. error = pmap_update (kernel_pmap);
  61. error_check (error, __func__);
  62. memset ((void *) va, test_pattern, PAGE_SIZE);
  63. error = pmap_remove (kernel_pmap, va, 0);
  64. error_check (error, __func__);
  65. error = pmap_update (kernel_pmap);
  66. error_check (error, __func__);
  67. vm_map_remove (kernel_map, va, va + PAGE_SIZE);
  68. list_insert_tail (&test_pages, &page->node);
  69. }
  70. }
  71. static void
  72. test_reset_pages (void)
  73. {
  74. _Auto kernel_map = vm_map_get_kernel_map ();
  75. _Auto kernel_pmap = pmap_get_kernel_pmap ();
  76. while (!list_empty (&test_pages))
  77. {
  78. _Auto page = list_first_entry (&test_pages, struct vm_page, node);
  79. list_remove (&page->node);
  80. uintptr_t va = 0;
  81. int flags = VM_MAP_FLAGS (VM_PROT_RDWR, VM_PROT_RDWR, VM_INHERIT_NONE,
  82. VM_ADV_DEFAULT, 0);
  83. int error = vm_map_enter (kernel_map, &va, PAGE_SIZE, flags, NULL, 0);
  84. error_check (error, __func__);
  85. error = pmap_enter (kernel_pmap, va, vm_page_to_pa (page),
  86. VM_PROT_RDWR, 0);
  87. error_check (error, __func__);
  88. error = pmap_update (kernel_pmap);
  89. error_check (error, __func__);
  90. memset ((void *) va, 0, PAGE_SIZE);
  91. error = pmap_remove (kernel_pmap, va, 0);
  92. error_check (error, __func__);
  93. error = pmap_update (kernel_pmap);
  94. error_check (error, __func__);
  95. vm_map_remove (kernel_map, va, va + PAGE_SIZE);
  96. vm_page_free (page, 0, 0);
  97. }
  98. }
  99. static void
  100. test_run (void *arg __unused)
  101. {
  102. for (uint32_t i = 0 ; ; i++)
  103. {
  104. printf ("test: pass:%u pattern:%hhx\n", i, test_pattern);
  105. test_write_pages ();
  106. test_reset_pages ();
  107. ++test_pattern;
  108. }
  109. }
  110. TEST_INLINE (vm_page_fill)
  111. {
  112. list_init (&test_pages);
  113. cpumap_zero (&test_cpumap);
  114. cpumap_set (&test_cpumap, cpu_id ());
  115. struct thread_attr attr;
  116. thread_attr_init (&attr, THREAD_KERNEL_PREFIX "test_run");
  117. thread_attr_set_detached (&attr);
  118. thread_attr_set_cpumap (&attr, &test_cpumap);
  119. int error = thread_create (NULL, &attr, test_run, NULL);
  120. error_check (error, "thread_create");
  121. return (TEST_RUNNING);
  122. }