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
14 #include <rtthread.h>
15
16 static long fd = -1;
17 static long pgoffset = 0;
18 static size_t flags = MAP_FIXED | MAP_ANONYMOUS;
19 static size_t prot1 = PROT_READ | PROT_WRITE;
20
21 static char *ex_vaddr = (void *)0x100000000;
22 static size_t map_size = 0x3000;
23
24 static size_t former_vsz;
25 static size_t former_vcount;
26
27 static struct rt_lwp *lwp;
28
_count_vsz(rt_varea_t varea,void * arg)29 static int _count_vsz(rt_varea_t varea, void *arg)
30 {
31 rt_base_t *pvsz = arg;
32 *pvsz += 1;
33 return 0;
34 }
35
count_vcount(rt_aspace_t aspace)36 static rt_base_t count_vcount(rt_aspace_t aspace)
37 {
38 rt_base_t vcount = 0;
39 rt_aspace_traversal(aspace, _count_vsz, &vcount);
40 return vcount;
41 }
42
test_mmap_expand(void)43 static void test_mmap_expand(void)
44 {
45 char *next_va;
46
47 /* map new pages at ex_vaddr to anonymous */
48 next_va = ex_vaddr;
49 former_vsz = rt_aspace_count_vsz(lwp->aspace);
50 former_vcount = count_vcount(lwp->aspace);
51 next_va = lwp_mmap2(lwp, next_va, map_size, prot1, flags, fd, pgoffset);
52 uassert_true(next_va == ex_vaddr);
53 utest_int_equal(former_vsz + map_size, rt_aspace_count_vsz(lwp->aspace));
54 utest_int_equal(former_vcount + 1, count_vcount(lwp->aspace));
55 former_vsz += map_size;
56 former_vcount += 1;
57
58 /* test the RIGHT side expansion of varea by lwp_mmap2 */
59 next_va = ex_vaddr + map_size;
60 uassert_true(
61 lwp_mmap2(lwp, next_va, map_size, prot1, flags, fd, pgoffset)
62 == next_va
63 );
64 utest_int_equal(former_vsz + map_size, rt_aspace_count_vsz(lwp->aspace));
65 utest_int_equal(former_vcount, count_vcount(lwp->aspace));
66 former_vsz += map_size;
67
68 /* test the LEFT side expansion of varea by rt_aspace_map */
69 next_va = ex_vaddr - map_size;
70 uassert_true(
71 lwp_mmap2(lwp, next_va, map_size, prot1, flags, fd, pgoffset)
72 == next_va
73 );
74 utest_int_equal(former_vsz + map_size, rt_aspace_count_vsz(lwp->aspace));
75 utest_int_equal(former_vcount, count_vcount(lwp->aspace));
76 former_vsz += map_size;
77
78 /* test other prot/offset/flags */
79
80 /* clear mapping */
81 utest_int_equal(RT_EOK, rt_aspace_unmap_range(lwp->aspace, next_va, 3 * map_size));
82 }
83
aspace_map_tc(void)84 static void aspace_map_tc(void)
85 {
86 CONSIST_HEAP(test_mmap_expand());
87 }
88
utest_tc_init(void)89 static rt_err_t utest_tc_init(void)
90 {
91 lwp = lwp_create(0);
92 if (lwp)
93 lwp_user_space_init(lwp, 1);
94 else
95 return -RT_ENOMEM;
96 return RT_EOK;
97 }
98
utest_tc_cleanup(void)99 static rt_err_t utest_tc_cleanup(void)
100 {
101 lwp_ref_dec(lwp);
102 return RT_EOK;
103 }
104
testcase(void)105 static void testcase(void)
106 {
107 UTEST_UNIT_RUN(aspace_map_tc);
108 }
109 UTEST_TC_EXPORT(testcase, "testcases.lwp.mman.mmap_anon.expand", utest_tc_init, utest_tc_cleanup, 10);
110