lwp_mmap_expand.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-08-22 Shell test case for aspace_map with varea_expand
  9. */
  10. #include "common.h"
  11. #include "lwp_user_mm.h"
  12. #include <mm_aspace.h>
  13. #include <rtthread.h>
  14. static long fd = -1;
  15. static long pgoffset = 0;
  16. static size_t flags = MAP_FIXED | MAP_ANONYMOUS;
  17. static size_t prot1 = PROT_READ | PROT_WRITE;
  18. static char *ex_vaddr = (void *)0x100000000;
  19. static size_t map_size = 0x3000;
  20. static size_t former_vsz;
  21. static size_t former_vcount;
  22. static struct rt_lwp *lwp;
  23. static int _count_vsz(rt_varea_t varea, void *arg)
  24. {
  25. rt_base_t *pvsz = arg;
  26. *pvsz += 1;
  27. return 0;
  28. }
  29. static rt_base_t count_vcount(rt_aspace_t aspace)
  30. {
  31. rt_base_t vcount = 0;
  32. rt_aspace_traversal(aspace, _count_vsz, &vcount);
  33. return vcount;
  34. }
  35. static void test_mmap_expand(void)
  36. {
  37. char *next_va;
  38. /* map new pages at ex_vaddr to anonymous */
  39. next_va = ex_vaddr;
  40. former_vsz = rt_aspace_count_vsz(lwp->aspace);
  41. former_vcount = count_vcount(lwp->aspace);
  42. next_va = lwp_mmap2(lwp, next_va, map_size, prot1, flags, fd, pgoffset);
  43. uassert_true(next_va == ex_vaddr);
  44. utest_int_equal(former_vsz + map_size, rt_aspace_count_vsz(lwp->aspace));
  45. utest_int_equal(former_vcount + 1, count_vcount(lwp->aspace));
  46. former_vsz += map_size;
  47. former_vcount += 1;
  48. /* test the RIGHT side expansion of varea by lwp_mmap2 */
  49. next_va = ex_vaddr + map_size;
  50. uassert_true(
  51. lwp_mmap2(lwp, next_va, map_size, prot1, flags, fd, pgoffset)
  52. == next_va
  53. );
  54. utest_int_equal(former_vsz + map_size, rt_aspace_count_vsz(lwp->aspace));
  55. utest_int_equal(former_vcount, count_vcount(lwp->aspace));
  56. former_vsz += map_size;
  57. /* test the LEFT side expansion of varea by rt_aspace_map */
  58. next_va = ex_vaddr - map_size;
  59. uassert_true(
  60. lwp_mmap2(lwp, next_va, map_size, prot1, flags, fd, pgoffset)
  61. == next_va
  62. );
  63. utest_int_equal(former_vsz + map_size, rt_aspace_count_vsz(lwp->aspace));
  64. utest_int_equal(former_vcount, count_vcount(lwp->aspace));
  65. former_vsz += map_size;
  66. /* test other prot/offset/flags */
  67. /* clear mapping */
  68. utest_int_equal(RT_EOK, rt_aspace_unmap_range(lwp->aspace, next_va, 3 * map_size));
  69. }
  70. static void aspace_map_tc(void)
  71. {
  72. CONSIST_HEAP(test_mmap_expand());
  73. }
  74. static rt_err_t utest_tc_init(void)
  75. {
  76. lwp = lwp_create(0);
  77. if (lwp)
  78. lwp_user_space_init(lwp, 1);
  79. else
  80. return -RT_ENOMEM;
  81. return RT_EOK;
  82. }
  83. static rt_err_t utest_tc_cleanup(void)
  84. {
  85. lwp_ref_dec(lwp);
  86. return RT_EOK;
  87. }
  88. static void testcase(void)
  89. {
  90. UTEST_UNIT_RUN(aspace_map_tc);
  91. }
  92. UTEST_TC_EXPORT(testcase, "testcases.lwp.mman.mmap_anon.expand", utest_tc_init, utest_tc_cleanup, 10);