test_vm_fault.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright (c) 2022 Agustina Arzille.
  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. * This test module tests page faults and the retrieval of page data
  18. * from VM objects.
  19. */
  20. #include <stddef.h>
  21. #include <stdint.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <kern/cpumap.h>
  25. #include <kern/error.h>
  26. #include <kern/init.h>
  27. #include <kern/list.h>
  28. #include <kern/task.h>
  29. #include <kern/thread.h>
  30. #include <machine/page.h>
  31. #include <machine/pmap.h>
  32. #include <test/test.h>
  33. #include <vm/kmem.h>
  34. #include <vm/map.h>
  35. #include <vm/object.h>
  36. #include <vm/page.h>
  37. #define TEST_OFFSET PAGE_SIZE
  38. static int test_pager_mapped;
  39. static int
  40. test_pager_get (struct vm_object *obj __unused, uint64_t off,
  41. size_t bytes, void *dst)
  42. {
  43. assert (! test_pager_mapped);
  44. assert (off == TEST_OFFSET);
  45. memset (dst, 'x', bytes);
  46. test_pager_mapped = 1;
  47. return (0);
  48. }
  49. static const struct vm_object_pager test_obj_pager =
  50. {
  51. .get = test_pager_get
  52. };
  53. static void
  54. test_vm_fault_thread (void *arg __unused)
  55. {
  56. struct vm_object *test_obj;
  57. int error = vm_object_create (&test_obj, PAGE_SIZE * 4, &test_obj_pager);
  58. assert (! error);
  59. uintptr_t va = PMAP_END_ADDRESS - PAGE_SIZE * 10;
  60. int flags = VM_MAP_FLAGS (VM_PROT_READ, VM_PROT_READ, VM_INHERIT_NONE,
  61. VM_ADV_DEFAULT, 0);
  62. struct vm_map *map = thread_self()->task->map;
  63. error = vm_map_enter (map, &va, PAGE_SIZE, 0, flags,
  64. test_obj, TEST_OFFSET);
  65. assert (! error);
  66. // First fault.
  67. assert (memcmp ((void *)va, "xxx", 3) == 0);
  68. // This mustn't fault.
  69. assert (memcmp ((void *)(va + PAGE_SIZE / 2), "xxxx", 4) == 0);
  70. // Test that writing to read-only mappings fails with EACCES.
  71. error = vm_copy ((void *)va, "???", 3);
  72. assert (error == EACCES);
  73. struct vm_map_entry entry;
  74. error = vm_map_lookup (map, va, &entry);
  75. assert (! error);
  76. assert (entry.object == test_obj);
  77. assert (VM_MAP_PROT (entry.flags) == VM_PROT_READ);
  78. vm_map_entry_put (&entry);
  79. vm_map_remove (map, map->start, map->end);
  80. vm_object_unref (test_obj);
  81. }
  82. TEST_DEFERRED (vm_fault)
  83. {
  84. struct task *task;
  85. int error = task_create (&task, "vm_fault");
  86. assert (! error);
  87. struct thread_attr attr;
  88. thread_attr_init (&attr, "vm_fault/0");
  89. thread_attr_set_task (&attr, task);
  90. struct thread *thread;
  91. thread_create (&thread, &attr, test_vm_fault_thread, 0);
  92. thread_join (thread);
  93. int val;
  94. error = vm_copy (&val, (void *)0x1, sizeof (val));
  95. assert (error == EFAULT);
  96. assert (!thread_self()->fixup);
  97. return (TEST_OK);
  98. }