mm_api_tc.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. * 2022-12-14 WangXiaoyao the first version
  9. * 2023-03-20 WangXiaoyao Format & add more testcases for API under mm_aspace.h
  10. * 2025-12-14 ChuanN-sudo Add standardized utest documentation block
  11. */
  12. /**
  13. * Test Case Name: MM API Test
  14. *
  15. * Test Objectives:
  16. * - Verify I/O remapping functionality with cached memory access.
  17. * - Test memory mapping flag creation and extraction mechanisms.
  18. * - Validate address space management operations.
  19. * - Test core APIs: rt_pages_alloc(), rt_ioremap_cached(), rt_iounmap(), rt_pages_free(),
  20. * rt_pages_free(), MMF_CREATE(), MMF_GET_CNTL(), MMF_GET_ALIGN().
  21. *
  22. * Test Scenarios:
  23. * - Address Space Test (aspace_tc): Tests memory address space management APIs.
  24. * - I/O Remap Test (ioremap_tc): Allocates physical pages, maps to virtual address with cache, verifies data consistency, and cleans up resources.
  25. * - Flag Test (flag_tc): Creates memory mapping flags with different parameters and validates control bits and alignment values extraction.
  26. *
  27. * Verification Metrics:
  28. * - I/O remapping should correctly map physical to virtual addresses.
  29. * - Cached memory access should maintain data consistency between physical and virtual addresses.
  30. * - Memory mapping flags should correctly encode and decode control bits and alignment values.
  31. * - Flag creation with alignment should set MMF_REQUEST_ALIGN bit and store alignment value.
  32. * - Flag creation without alignment should not set alignment-related bits.
  33. *
  34. Dependencies:
  35. * - Hardware requirements: QEMU emulator or any hardware platform that supports RT-Thread with MMU.
  36. * - Software configuration:
  37. * - RT_UTEST_MM_API must be enabled (enable via: RT-Thread Utestcases -> RT-Thread Utestcases -> Memory Management Subsystem Testcase -> Enable Utest for MM API).
  38. * - RT_USING_SMART must be enabled (enable via: Enable RT-Thread Kernel -> RT-Thread Smart (microkernel on kernel/userland)).
  39. * - Environmental Assumptions: MMU support must be available on the target platform.
  40. * Expected Results:
  41. * - Final output: "[ PASSED ] [ result ] testcase (testcases.mm.api_tc)"
  42. * - No assertion failures during test execution.
  43. */
  44. #include "common.h"
  45. /**
  46. * @brief Testing all APIs under components/mm
  47. */
  48. void ioremap_tc(void);
  49. void flag_tc(void);
  50. #ifdef STANDALONE_TC
  51. #define TC_ASSERT(expr) \
  52. ((expr) \
  53. ? 0 \
  54. : rt_kprintf("AssertFault(%d): %s\n", __LINE__, RT_STRINGIFY(expr)))
  55. #else
  56. #define TC_ASSERT(expr) uassert_true(expr)
  57. #endif
  58. static rt_err_t utest_tc_init(void)
  59. {
  60. return RT_EOK;
  61. }
  62. static rt_err_t utest_tc_cleanup(void)
  63. {
  64. return RT_EOK;
  65. }
  66. #include "test_aspace_api.h"
  67. static void testcase(void)
  68. {
  69. aspace_tc();
  70. ioremap_tc();
  71. flag_tc();
  72. }
  73. UTEST_TC_EXPORT(testcase, "testcases.mm.api_tc", utest_tc_init, utest_tc_cleanup, 20);
  74. void ioremap_tc(void)
  75. {
  76. const size_t bufsz = 0x1000;
  77. void *paddr = (void *)rt_pages_alloc(rt_page_bits(bufsz)) + PV_OFFSET;
  78. int *vaddr;
  79. vaddr = rt_ioremap_cached(paddr, bufsz);
  80. if (vaddr)
  81. {
  82. TC_ASSERT(*vaddr == *(int *)(paddr - PV_OFFSET));
  83. rt_iounmap(vaddr);
  84. rt_pages_free(paddr - PV_OFFSET, 0);
  85. }
  86. }
  87. void flag_tc(void)
  88. {
  89. size_t flags;
  90. flags = MMF_CREATE(MMF_MAP_FIXED, 0x4000);
  91. TC_ASSERT(MMF_GET_CNTL(flags) == (MMF_MAP_FIXED | MMF_REQUEST_ALIGN));
  92. TC_ASSERT((1 << MMF_GET_ALIGN(flags)) == 0x4000);
  93. flags = MMF_CREATE(MMF_MAP_FIXED, 0);
  94. TC_ASSERT(MMF_GET_CNTL(flags) == MMF_MAP_FIXED);
  95. TC_ASSERT(MMF_GET_ALIGN(flags) == 0);
  96. }
  97. #if 0
  98. #define BUF_SIZE (4ul << 20)
  99. static char ALIGN(BUF_SIZE) buf[BUF_SIZE];
  100. void buddy_tc(void)
  101. {
  102. size_t total, free;
  103. rt_page_get_info(&total, &free);
  104. rt_region_t region = {
  105. .start = (size_t)buf,
  106. .end = (size_t)buf + BUF_SIZE,
  107. };
  108. size_t new_total, new_free;
  109. rt_page_install(region);
  110. rt_page_get_info(&new_total, &new_free);
  111. TC_ASSERT(new_total - total == (BUF_SIZE >> ARCH_PAGE_SHIFT));
  112. TC_ASSERT(new_free > free);
  113. }
  114. void mmu_page_tc()
  115. {
  116. mm_aspace_t aspace = ASPACE_NEW();
  117. size_t total, free;
  118. rt_page_get_info(&total, &free);
  119. rt_hw_mmu_map(aspace, (void *)0x3fffffffff, 0, ARCH_PAGE_SIZE,
  120. MMU_MAP_K_RWCB);
  121. rt_hw_mmu_unmap(aspace, (void *)0x3fffffffff, ARCH_PAGE_SIZE);
  122. size_t new_total, new_free;
  123. rt_page_get_info(&new_total, &new_free);
  124. TC_ASSERT(new_free == free);
  125. mm_aspace_delete(aspace);
  126. }
  127. #endif