aspace_map_expand.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 size_t flags = MMF_PREFETCH | MMF_MAP_FIXED;
  15. static size_t attr = MMU_MAP_K_RWCB;
  16. static rt_mem_obj_t mem_obj = &rt_mm_dummy_mapper;
  17. static char *ex_vaddr = (void *)0x100000000;
  18. static size_t ex_offset = 1024;
  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_map_varea_expand(void)
  36. {
  37. char *next_va;
  38. size_t next_offset;
  39. /* create an existed mapping */
  40. next_va = ex_vaddr;
  41. former_vsz = rt_aspace_count_vsz(lwp->aspace);
  42. former_vcount = count_vcount(lwp->aspace);
  43. utest_int_equal(
  44. RT_EOK,
  45. rt_aspace_map(lwp->aspace, (void *)&ex_vaddr, map_size, attr, flags, mem_obj, ex_offset)
  46. );
  47. uassert_true(next_va == ex_vaddr);
  48. utest_int_equal(former_vsz + map_size, rt_aspace_count_vsz(lwp->aspace));
  49. utest_int_equal(former_vcount + 1, count_vcount(lwp->aspace));
  50. former_vsz += map_size;
  51. former_vcount += 1;
  52. /* test the RIGHT side expansion of varea by rt_aspace_map */
  53. next_va = ex_vaddr + map_size;
  54. next_offset = ex_offset + (map_size >> MM_PAGE_SHIFT);
  55. utest_int_equal(
  56. RT_EOK,
  57. rt_aspace_map(lwp->aspace, (void *)&next_va, map_size, attr, flags, mem_obj, next_offset)
  58. );
  59. uassert_true(next_va == (char *)ex_vaddr + map_size);
  60. utest_int_equal(former_vsz + map_size, rt_aspace_count_vsz(lwp->aspace));
  61. utest_int_equal(former_vcount, count_vcount(lwp->aspace));
  62. former_vsz += map_size;
  63. /* test the LEFT side expansion of varea by rt_aspace_map */
  64. next_va = ex_vaddr - map_size;
  65. next_offset = ex_offset - (map_size >> MM_PAGE_SHIFT);
  66. utest_int_equal(
  67. RT_EOK,
  68. rt_aspace_map(lwp->aspace, (void *)&next_va, map_size, attr, flags, mem_obj, next_offset)
  69. );
  70. uassert_true(next_va == ex_vaddr - map_size);
  71. utest_int_equal(former_vsz + map_size, rt_aspace_count_vsz(lwp->aspace));
  72. utest_int_equal(former_vcount, count_vcount(lwp->aspace));
  73. former_vsz += map_size;
  74. /* test the expand varea routine from rt_aspace_map_static */
  75. utest_int_equal(RT_EOK, rt_aspace_unmap_range(lwp->aspace, next_va, 3 * map_size));
  76. /* test the expand varea routine from rt_aspace_map_phy */
  77. /* test the expand varea routine from rt_aspace_map_phy_static */
  78. /* these 2 from another file */
  79. }
  80. static void aspace_map_tc(void)
  81. {
  82. CONSIST_HEAP(test_map_varea_expand());
  83. }
  84. static rt_err_t utest_tc_init(void)
  85. {
  86. lwp = lwp_create(0);
  87. if (lwp)
  88. lwp_user_space_init(lwp, 1);
  89. else
  90. return -RT_ENOMEM;
  91. return RT_EOK;
  92. }
  93. static rt_err_t utest_tc_cleanup(void)
  94. {
  95. lwp_ref_dec(lwp);
  96. return RT_EOK;
  97. }
  98. static void testcase(void)
  99. {
  100. UTEST_UNIT_RUN(aspace_map_tc);
  101. }
  102. UTEST_TC_EXPORT(testcase, "testcases.mm.aspace_map.varea_expand", utest_tc_init, utest_tc_cleanup, 10);