Skip to content

Commit 470454d

Browse files
authored
[components/mm] support for scalable memory management (#7277)
* [mm/page] multi-list page manager [mm/page] page debugger [libcpu/aarch64] hugepage support * [quality] remove void-arith * [format] remove kasan codes
1 parent 90c69a0 commit 470454d

File tree

20 files changed

+603
-230
lines changed

20 files changed

+603
-230
lines changed

components/drivers/virtio/virtio_net.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <rthw.h>
1212
#include <rtthread.h>
1313
#include <cpuport.h>
14+
#include <mm_aspace.h>
1415

1516
#ifdef RT_USING_VIRTIO_NET
1617

@@ -106,7 +107,7 @@ static struct pbuf *virtio_net_rx(rt_device_t dev)
106107
#ifdef RT_USING_SMP
107108
level = rt_spin_lock_irqsave(&virtio_dev->spinlock);
108109
#endif
109-
rt_memcpy(p->payload, (void *)VIRTIO_PA2VA(queue_rx->desc[id].addr), len);
110+
rt_memcpy(p->payload, (void *)queue_rx->desc[id].addr - PV_OFFSET, len);
110111

111112
queue_rx->used_idx++;
112113

components/lwp/arch/aarch64/cortex-a/lwp_arch.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ int arch_user_space_init(struct rt_lwp *lwp)
2626
{
2727
size_t *mmu_table;
2828

29-
mmu_table = (size_t *)rt_pages_alloc(0);
29+
mmu_table = (size_t *)rt_pages_alloc_ext(0, PAGE_ANY_AVAILABLE);
3030
if (!mmu_table)
3131
{
3232
return -RT_ENOMEM;

components/lwp/arch/risc-v/rv64/lwp_arch.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ int arch_user_space_init(struct rt_lwp *lwp)
9191
{
9292
rt_ubase_t *mmu_table;
9393

94-
mmu_table = (rt_ubase_t *)rt_pages_alloc(0);
94+
mmu_table = (rt_ubase_t *)rt_pages_alloc_ext(0, PAGE_ANY_AVAILABLE);
9595
if (!mmu_table)
9696
{
9797
return -RT_ENOMEM;

components/lwp/arch/x86/i386/lwp_arch.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ int arch_user_space_init(struct rt_lwp *lwp)
8282
{
8383
rt_size_t *mmu_table;
8484

85-
mmu_table = (rt_size_t *)rt_pages_alloc(0);
85+
mmu_table = (rt_size_t *)rt_pages_alloc_ext(0, PAGE_ANY_AVAILABLE);
8686
if (!mmu_table)
8787
{
8888
return -1;

components/lwp/lwp.c

+14-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* 2018-11-02 heyuanjie fix complie error in iar
1010
* 2021-02-03 lizhirui add 64-bit arch support and riscv64 arch support
1111
* 2021-08-26 linzhenxing add lwp_setcwd\lwp_getcwd
12+
* 2023-02-20 wangxiaoyao inv icache before new app startup
1213
*/
1314

1415
#include <rthw.h>
@@ -1097,10 +1098,22 @@ static void _lwp_thread_entry(void *parameter)
10971098
icache_invalid_all();
10981099
}
10991100

1101+
/**
1102+
* without ASID support, it will be a special case when trying to run application
1103+
* and exit multiple times and a same page frame allocated to it bound to
1104+
* different text segment. Then we are in a situation where icache contains
1105+
* out-of-dated data and must be handle by the running core itself.
1106+
* with ASID support, this should be a rare case that ASID & page frame both
1107+
* identical to previous running application.
1108+
*
1109+
* For a new application loaded into memory, icache are seen as empty. And there
1110+
* should be nothing in the icache entry to match. So this icache invalidation
1111+
* operation should have barely influence.
1112+
*/
11001113
rt_hw_icache_invalidate_all();
11011114

11021115
#ifdef ARCH_MM_MMU
1103-
arch_start_umode(lwp->args, lwp->text_entry, (void *)USER_STACK_VEND, tid->stack_addr + tid->stack_size);
1116+
arch_start_umode(lwp->args, lwp->text_entry, (void *)USER_STACK_VEND, (char *)tid->stack_addr + tid->stack_size);
11041117
#else
11051118
arch_start_umode(lwp->args, lwp->text_entry, lwp->data_entry, (void *)((uint32_t)lwp->data_entry + lwp->data_size));
11061119
#endif /* ARCH_MM_MMU */

components/lwp/lwp_shm.c

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* Change Logs:
77
* Date Author Notes
88
* 2019-10-12 Jesven first version
9+
* 2023-02-20 wangxiaoyao adapt to mm
910
*/
1011
#include <rthw.h>
1112
#include <rtthread.h>
@@ -17,8 +18,6 @@
1718

1819
#include <lwp_user_mm.h>
1920
#include <mmu.h>
20-
#include <mm_aspace.h>
21-
#include <mm_flag.h>
2221

2322
/* the kernel structure to represent a share-memory */
2423
struct lwp_shm_struct
@@ -64,7 +63,7 @@ static void on_shm_page_fault(struct rt_varea *varea, struct rt_aspace_fault_msg
6463

6564
/* map all share page frames to user space in a time */
6665
void *page = (void *)shm->addr;
67-
void *pg_paddr = page + PV_OFFSET;
66+
void *pg_paddr = (char *)page + PV_OFFSET;
6867
err = rt_varea_map_range(varea, varea->start, pg_paddr, shm->size);
6968

7069
if (err == RT_EOK)
@@ -140,7 +139,7 @@ static int _lwp_shmget(size_t key, size_t size, int create)
140139

141140
/* allocate pages up to 2's exponent to cover the required size */
142141
bit = rt_page_bits(size);
143-
page_addr = rt_pages_alloc(bit); /* virtual address */
142+
page_addr = rt_pages_alloc_ext(bit, PAGE_ANY_AVAILABLE); /* virtual address */
144143
if (!page_addr)
145144
{
146145
goto err;

components/lwp/lwp_syscall.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ static void _crt_thread_entry(void *parameter)
306306
user_stack &= ~7; //align 8
307307

308308
#ifdef ARCH_MM_MMU
309-
arch_crt_start_umode(parameter, tid->user_entry, (void *)user_stack, tid->stack_addr + tid->stack_size);
309+
arch_crt_start_umode(parameter, tid->user_entry, (void *)user_stack, (char *)tid->stack_addr + tid->stack_size);
310310
#else
311311
set_user_context((void*)user_stack);
312312
arch_start_umode(parameter, tid->user_entry, ((struct rt_lwp *)tid->lwp)->data_entry, (void*)user_stack);
@@ -1861,7 +1861,7 @@ static char *_insert_args(int new_argc, char *new_argv[], struct lwp_args_info *
18611861
{
18621862
goto quit;
18631863
}
1864-
page = rt_pages_alloc(0); /* 1 page */
1864+
page = rt_pages_alloc_ext(0, PAGE_ANY_AVAILABLE); /* 1 page */
18651865
if (!page)
18661866
{
18671867
goto quit;
@@ -2065,7 +2065,7 @@ int load_ldso(struct rt_lwp *lwp, char *exec_name, char *const argv[], char *con
20652065
}
20662066
}
20672067

2068-
page = rt_pages_alloc(0); /* 1 page */
2068+
page = rt_pages_alloc_ext(0, PAGE_ANY_AVAILABLE); /* 1 page */
20692069
if (!page)
20702070
{
20712071
SET_ERRNO(ENOMEM);
@@ -2252,7 +2252,7 @@ sysret_t sys_execve(const char *path, char *const argv[], char *const envp[])
22522252
SET_ERRNO(EINVAL);
22532253
goto quit;
22542254
}
2255-
page = rt_pages_alloc(0); /* 1 page */
2255+
page = rt_pages_alloc_ext(0, PAGE_ANY_AVAILABLE); /* 1 page */
22562256
if (!page)
22572257
{
22582258
SET_ERRNO(ENOMEM);
@@ -2396,7 +2396,7 @@ sysret_t sys_execve(const char *path, char *const argv[], char *const envp[])
23962396
arch_start_umode(lwp->args,
23972397
lwp->text_entry,
23982398
(void*)USER_STACK_VEND,
2399-
thread->stack_addr + thread->stack_size);
2399+
(char *)thread->stack_addr + thread->stack_size);
24002400
/* never reach here */
24012401
}
24022402
return -EINVAL;

components/lwp/lwp_user_mm.c

+8-7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* 2021-02-12 lizhirui add 64-bit support for lwp_brk
1111
* 2021-02-19 lizhirui add riscv64 support for lwp_user_accessable and lwp_get_from_user
1212
* 2021-06-07 lizhirui modify user space bound check
13+
* 2022-12-25 wangxiaoyao adapt to new mm
1314
*/
1415

1516
#include <rtthread.h>
@@ -122,15 +123,15 @@ static void _user_do_page_fault(struct rt_varea *varea,
122123

123124
if (lwp_objs->source)
124125
{
125-
void *paddr = rt_hw_mmu_v2p(lwp_objs->source, msg->fault_vaddr);
126+
char *paddr = rt_hw_mmu_v2p(lwp_objs->source, msg->fault_vaddr);
126127
if (paddr != ARCH_MAP_FAILED)
127128
{
128129
void *vaddr;
129130
vaddr = paddr - PV_OFFSET;
130131

131132
if (!(varea->flag & MMF_TEXT))
132133
{
133-
void *cp = rt_pages_alloc(0);
134+
void *cp = rt_pages_alloc_ext(0, PAGE_ANY_AVAILABLE);
134135
if (cp)
135136
{
136137
memcpy(cp, vaddr, ARCH_PAGE_SIZE);
@@ -220,9 +221,9 @@ int lwp_unmap_user(struct rt_lwp *lwp, void *va)
220221
static void _dup_varea(rt_varea_t varea, struct rt_lwp *src_lwp,
221222
rt_aspace_t dst)
222223
{
223-
void *vaddr = varea->start;
224-
void *vend = vaddr + varea->size;
225-
if (vaddr < (void *)USER_STACK_VSTART || vaddr >= (void *)USER_STACK_VEND)
224+
char *vaddr = varea->start;
225+
char *vend = vaddr + varea->size;
226+
if (vaddr < (char *)USER_STACK_VSTART || vaddr >= (char *)USER_STACK_VEND)
226227
{
227228
while (vaddr != vend)
228229
{
@@ -430,7 +431,7 @@ void *lwp_map_user_phy(struct rt_lwp *lwp, void *map_va, void *map_pa,
430431
size_t map_size, int cached)
431432
{
432433
int err;
433-
void *va;
434+
char *va;
434435
size_t offset = 0;
435436

436437
if (!map_size)
@@ -458,7 +459,7 @@ void *lwp_map_user_phy(struct rt_lwp *lwp, void *map_va, void *map_pa,
458459
rt_size_t attr = cached ? MMU_MAP_U_RWCB : MMU_MAP_U_RW;
459460

460461
err =
461-
rt_aspace_map_phy(lwp->aspace, &hint, attr, MM_PA_TO_OFF(map_pa), &va);
462+
rt_aspace_map_phy(lwp->aspace, &hint, attr, MM_PA_TO_OFF(map_pa), (void **)&va);
462463
if (err != RT_EOK)
463464
{
464465
va = RT_NULL;

components/mm/avl_adpt.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ static struct rt_varea *search(struct util_avl_root *root,
7878
{
7979
rt_varea_t varea = VAREA_ENTRY(node);
8080
int cmp = compare(range.start, range.end, varea->start,
81-
varea->start + varea->size - 1);
81+
(char *)varea->start + varea->size - 1);
8282

8383
if (cmp < 0)
8484
{
@@ -118,7 +118,7 @@ rt_varea_t _aspace_bst_search_exceed(struct rt_aspace *aspace, void *start)
118118
if (cmp < 0)
119119
{
120120
/* varae exceed start */
121-
ptrdiff_t off = va_s - start;
121+
ptrdiff_t off = (char *)va_s - (char *)start;
122122
if (off < min_off)
123123
{
124124
min_off = off;

components/mm/ioremap.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ enum ioremap_type
3333

3434
static void *_ioremap_type(void *paddr, size_t size, enum ioremap_type type)
3535
{
36-
void *v_addr = NULL;
36+
char *v_addr = NULL;
3737
size_t attr;
3838
size_t lo_off;
3939
int err;
4040

41-
lo_off = (uintptr_t)paddr & ARCH_PAGE_MASK;
41+
lo_off = (rt_ubase_t)paddr & ARCH_PAGE_MASK;
4242

4343
struct rt_mm_va_hint hint = {
4444
.prefer = RT_NULL,
@@ -62,7 +62,7 @@ static void *_ioremap_type(void *paddr, size_t size, enum ioremap_type type)
6262
default:
6363
return v_addr;
6464
}
65-
err = rt_aspace_map_phy(&rt_kernel_space, &hint, attr, MM_PA_TO_OFF(paddr), &v_addr);
65+
err = rt_aspace_map_phy(&rt_kernel_space, &hint, attr, MM_PA_TO_OFF(paddr), (void **)&v_addr);
6666

6767
if (err)
6868
{

0 commit comments

Comments
 (0)