2023-01-09 10:08:55 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2006-2022, RT-Thread Development Team
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2022-11-14 WangXiaoyao the first version
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Virtual Address Space
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <rtdef.h>
|
|
|
|
#include <rtthread.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2023-01-16 08:24:03 +08:00
|
|
|
#define DBG_TAG "mm.aspace"
|
|
|
|
#define DBG_LVL DBG_INFO
|
|
|
|
#include <rtdbg.h>
|
|
|
|
|
2023-01-09 10:08:55 +08:00
|
|
|
#include "avl_adpt.h"
|
|
|
|
#include "mm_aspace.h"
|
|
|
|
#include "mm_fault.h"
|
|
|
|
#include "mm_flag.h"
|
|
|
|
#include "mm_page.h"
|
|
|
|
#include "mm_private.h"
|
|
|
|
|
|
|
|
#include <mmu.h>
|
|
|
|
#include <tlb.h>
|
|
|
|
|
|
|
|
static void *_find_free(rt_aspace_t aspace, void *prefer, rt_size_t req_size,
|
|
|
|
void *limit_start, rt_size_t limit_size,
|
|
|
|
mm_flag_t flags);
|
2023-03-30 08:25:15 +08:00
|
|
|
static void _varea_uninstall(rt_varea_t varea);
|
2023-01-09 10:08:55 +08:00
|
|
|
|
|
|
|
struct rt_aspace rt_kernel_space;
|
|
|
|
|
2023-03-31 20:58:01 +08:00
|
|
|
static int _init_lock(rt_aspace_t aspace)
|
2023-01-09 10:08:55 +08:00
|
|
|
{
|
2023-03-30 08:25:15 +08:00
|
|
|
int err;
|
|
|
|
MM_PGTBL_LOCK_INIT(aspace);
|
|
|
|
err = rt_mutex_init(&aspace->bst_lock, "aspace", RT_IPC_FLAG_FIFO);
|
2023-01-09 10:08:55 +08:00
|
|
|
|
2023-03-30 08:25:15 +08:00
|
|
|
return err;
|
2023-01-09 10:08:55 +08:00
|
|
|
}
|
|
|
|
|
2023-03-30 08:25:15 +08:00
|
|
|
rt_err_t rt_aspace_init(rt_aspace_t aspace, void *start, rt_size_t length, void *pgtbl)
|
2023-03-03 11:51:21 +08:00
|
|
|
{
|
2023-03-30 08:25:15 +08:00
|
|
|
int err = RT_EOK;
|
2023-03-19 14:41:50 +08:00
|
|
|
|
2023-03-30 08:25:15 +08:00
|
|
|
if (pgtbl)
|
|
|
|
{
|
|
|
|
aspace->page_table = pgtbl;
|
|
|
|
aspace->start = start;
|
|
|
|
aspace->size = length;
|
2023-03-03 11:51:21 +08:00
|
|
|
|
2023-03-30 08:25:15 +08:00
|
|
|
err = _aspace_bst_init(aspace);
|
|
|
|
if (err == RT_EOK)
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* It has the side effect that lock will be added to object
|
|
|
|
* system management. So it must be paired with a detach once
|
|
|
|
* the initialization return successfully.
|
|
|
|
*/
|
|
|
|
err = _init_lock(aspace);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
err = -RT_EINVAL;
|
|
|
|
}
|
2023-01-09 10:08:55 +08:00
|
|
|
|
2023-03-30 08:25:15 +08:00
|
|
|
return err;
|
2023-01-09 10:08:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
rt_aspace_t rt_aspace_create(void *start, rt_size_t length, void *pgtbl)
|
|
|
|
{
|
|
|
|
rt_aspace_t aspace = NULL;
|
2023-03-30 08:25:15 +08:00
|
|
|
int err;
|
2023-01-09 10:08:55 +08:00
|
|
|
|
2023-03-30 08:25:15 +08:00
|
|
|
RT_ASSERT(length <= 0 - (rt_size_t)start);
|
|
|
|
aspace = (rt_aspace_t)rt_malloc(sizeof(*aspace));
|
|
|
|
if (aspace)
|
2023-01-09 10:08:55 +08:00
|
|
|
{
|
2023-03-30 08:25:15 +08:00
|
|
|
rt_memset(aspace, 0, sizeof(*aspace));
|
|
|
|
|
|
|
|
err = rt_aspace_init(aspace, start, length, pgtbl);
|
|
|
|
|
|
|
|
if (err != RT_EOK)
|
2023-01-09 10:08:55 +08:00
|
|
|
{
|
2023-03-30 08:25:15 +08:00
|
|
|
LOG_W("%s(%p, %lx, %p): failed with code %d\n", __func__,
|
|
|
|
start, length, pgtbl, err);
|
|
|
|
rt_free(aspace);
|
|
|
|
aspace = RT_NULL;
|
2023-01-09 10:08:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return aspace;
|
|
|
|
}
|
|
|
|
|
2023-03-30 08:25:15 +08:00
|
|
|
void rt_aspace_detach(rt_aspace_t aspace)
|
2023-01-09 10:08:55 +08:00
|
|
|
{
|
2023-03-30 08:25:15 +08:00
|
|
|
WR_LOCK(aspace);
|
|
|
|
rt_varea_t varea = ASPACE_VAREA_FIRST(aspace);
|
|
|
|
while (varea)
|
2023-01-09 10:08:55 +08:00
|
|
|
{
|
2023-03-30 08:25:15 +08:00
|
|
|
rt_varea_t prev = varea;
|
|
|
|
_varea_uninstall(varea);
|
|
|
|
|
|
|
|
varea = ASPACE_VAREA_NEXT(varea);
|
|
|
|
if (!(prev->flag & MMF_STATIC_ALLOC))
|
2023-01-09 10:08:55 +08:00
|
|
|
{
|
2023-03-30 08:25:15 +08:00
|
|
|
rt_free(prev);
|
2023-01-09 10:08:55 +08:00
|
|
|
}
|
|
|
|
}
|
2023-03-30 08:25:15 +08:00
|
|
|
WR_UNLOCK(aspace);
|
2023-01-09 10:08:55 +08:00
|
|
|
|
|
|
|
rt_mutex_detach(&aspace->bst_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void rt_aspace_delete(rt_aspace_t aspace)
|
|
|
|
{
|
2023-03-30 08:25:15 +08:00
|
|
|
RT_ASSERT(aspace);
|
|
|
|
rt_aspace_detach(aspace);
|
|
|
|
rt_free(aspace);
|
2023-01-09 10:08:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int _do_named_map(rt_aspace_t aspace, void *vaddr, rt_size_t length,
|
|
|
|
rt_size_t offset, rt_size_t attr)
|
|
|
|
{
|
2023-01-16 08:24:03 +08:00
|
|
|
LOG_D("%s: va %p length %p", __func__, vaddr, length);
|
2023-01-09 10:08:55 +08:00
|
|
|
int err = RT_EOK;
|
|
|
|
|
|
|
|
/* it's ensured by caller that (void*)end will not overflow */
|
|
|
|
void *phyaddr = (void *)(offset << MM_PAGE_SHIFT);
|
2023-04-22 23:59:11 +08:00
|
|
|
|
|
|
|
void *ret = rt_hw_mmu_map(aspace, vaddr, phyaddr, length, attr);
|
|
|
|
if (ret == RT_NULL)
|
2023-01-09 10:08:55 +08:00
|
|
|
{
|
2023-04-22 23:59:11 +08:00
|
|
|
err = -RT_ERROR;
|
2023-01-09 10:08:55 +08:00
|
|
|
}
|
|
|
|
|
2023-03-03 11:51:21 +08:00
|
|
|
if (err == RT_EOK)
|
2023-04-22 23:59:11 +08:00
|
|
|
rt_hw_tlb_invalidate_range(aspace, vaddr, length, ARCH_PAGE_SIZE);
|
2023-01-09 10:08:55 +08:00
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2023-03-30 08:25:15 +08:00
|
|
|
rt_inline void _do_page_fault(struct rt_aspace_fault_msg *msg, rt_size_t off,
|
2023-01-09 10:08:55 +08:00
|
|
|
void *vaddr, rt_mem_obj_t mem_obj,
|
|
|
|
rt_varea_t varea)
|
|
|
|
{
|
|
|
|
msg->off = off;
|
2023-03-30 08:25:15 +08:00
|
|
|
msg->fault_vaddr = vaddr;
|
2023-01-09 10:08:55 +08:00
|
|
|
msg->fault_op = MM_FAULT_OP_READ;
|
|
|
|
msg->fault_type = MM_FAULT_TYPE_PAGE_FAULT;
|
2023-04-22 23:59:11 +08:00
|
|
|
msg->response.status = MM_FAULT_STATUS_UNRECOVERABLE;
|
2023-03-30 08:25:15 +08:00
|
|
|
msg->response.vaddr = 0;
|
|
|
|
msg->response.size = 0;
|
2023-01-09 10:08:55 +08:00
|
|
|
|
|
|
|
mem_obj->on_page_fault(varea, msg);
|
|
|
|
}
|
|
|
|
|
2023-03-30 08:25:15 +08:00
|
|
|
int _varea_map_with_msg(rt_varea_t varea, struct rt_aspace_fault_msg *msg)
|
|
|
|
{
|
|
|
|
int err = -RT_ERROR;
|
|
|
|
if (msg->response.status == MM_FAULT_STATUS_OK)
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* the page returned by handler is not checked
|
|
|
|
* cause no much assumption can make on it
|
|
|
|
*/
|
2023-04-22 23:59:11 +08:00
|
|
|
char *store = msg->response.vaddr;
|
2023-03-30 08:25:15 +08:00
|
|
|
rt_size_t store_sz = msg->response.size;
|
2023-04-22 23:59:11 +08:00
|
|
|
if ((char *)msg->fault_vaddr + store_sz > (char *)varea->start + varea->size)
|
2023-03-30 08:25:15 +08:00
|
|
|
{
|
|
|
|
LOG_W("%s: too much (0x%lx) of buffer on vaddr %p is provided",
|
|
|
|
__func__, store_sz, msg->fault_vaddr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
void *map;
|
|
|
|
void *v_addr = msg->fault_vaddr;
|
|
|
|
void *p_addr = store + PV_OFFSET;
|
|
|
|
map = rt_hw_mmu_map(varea->aspace, v_addr, p_addr, store_sz, varea->attr);
|
|
|
|
|
|
|
|
if (!map)
|
|
|
|
{
|
|
|
|
LOG_W("%s: MMU mapping failed for va %p to %p of %lx", __func__,
|
|
|
|
msg->fault_vaddr, store + PV_OFFSET, store_sz);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rt_hw_tlb_invalidate_range(varea->aspace, v_addr, store_sz, ARCH_PAGE_SIZE);
|
|
|
|
err = RT_EOK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (msg->response.status == MM_FAULT_STATUS_OK_MAPPED)
|
|
|
|
{
|
|
|
|
if (rt_hw_mmu_v2p(varea->aspace, msg->fault_vaddr) == ARCH_MAP_FAILED)
|
|
|
|
{
|
|
|
|
LOG_W("%s: no page is mapped on %p", __func__, msg->fault_vaddr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
err = RT_EOK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LOG_W("%s: failed on va %p inside varea %p(%s)", __func__, msg->fault_vaddr, varea,
|
|
|
|
varea->mem_obj->get_name ? varea->mem_obj->get_name(varea) : "unknow");
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2023-01-09 10:08:55 +08:00
|
|
|
/* allocate memory page for mapping range */
|
|
|
|
static int _do_prefetch(rt_aspace_t aspace, rt_varea_t varea, void *start,
|
|
|
|
rt_size_t size)
|
|
|
|
{
|
|
|
|
int err = RT_EOK;
|
|
|
|
|
|
|
|
/* it's ensured by caller that start & size ara page-aligned */
|
2023-04-22 23:59:11 +08:00
|
|
|
char *end = (char *)start + size;
|
|
|
|
char *vaddr = start;
|
|
|
|
rt_size_t off = varea->offset + ((vaddr - (char *)varea->start) >> ARCH_PAGE_SHIFT);
|
2023-01-09 10:08:55 +08:00
|
|
|
|
|
|
|
while (vaddr != end)
|
|
|
|
{
|
|
|
|
/* TODO try to map with huge TLB, when flag & HUGEPAGE */
|
2023-03-30 08:25:15 +08:00
|
|
|
struct rt_aspace_fault_msg msg;
|
2023-01-09 10:08:55 +08:00
|
|
|
_do_page_fault(&msg, off, vaddr, varea->mem_obj, varea);
|
|
|
|
|
2023-03-30 08:25:15 +08:00
|
|
|
if (_varea_map_with_msg(varea, &msg))
|
2023-04-22 23:59:11 +08:00
|
|
|
{
|
|
|
|
err = -RT_ENOMEM;
|
2023-03-30 08:25:15 +08:00
|
|
|
break;
|
2023-04-22 23:59:11 +08:00
|
|
|
}
|
2023-03-30 08:25:15 +08:00
|
|
|
/**
|
|
|
|
* It's hard to identify the mapping pattern on a customized handler
|
|
|
|
* So we terminate the prefetch process on that case
|
|
|
|
*/
|
|
|
|
if (msg.response.status == MM_FAULT_STATUS_OK_MAPPED)
|
2023-01-09 10:08:55 +08:00
|
|
|
break;
|
2023-03-30 08:25:15 +08:00
|
|
|
|
|
|
|
vaddr += msg.response.size;
|
|
|
|
off += msg.response.size >> ARCH_PAGE_SHIFT;
|
2023-01-09 10:08:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2023-03-30 08:25:15 +08:00
|
|
|
/* caller must hold the aspace lock */
|
|
|
|
static int _varea_install(rt_aspace_t aspace, rt_varea_t varea, rt_mm_va_hint_t hint)
|
2023-01-09 10:08:55 +08:00
|
|
|
{
|
|
|
|
void *alloc_va;
|
|
|
|
int err = RT_EOK;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* find a suitable va range.
|
|
|
|
* even though this is sleepable, it's still ok for startup routine
|
|
|
|
*/
|
|
|
|
alloc_va =
|
|
|
|
_find_free(aspace, hint->prefer, hint->map_size, hint->limit_start,
|
|
|
|
hint->limit_range_size, hint->flags);
|
|
|
|
|
|
|
|
/* TODO try merge surrounding regions to optimize memory footprint */
|
|
|
|
|
2023-02-20 13:44:20 +08:00
|
|
|
if (alloc_va != RT_NULL)
|
2023-01-09 10:08:55 +08:00
|
|
|
{
|
|
|
|
varea->start = alloc_va;
|
|
|
|
_aspace_bst_insert(aspace, varea);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
err = -RT_ENOSPC;
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2023-03-30 08:25:15 +08:00
|
|
|
static inline void _varea_post_install(rt_varea_t varea, rt_aspace_t aspace,
|
|
|
|
rt_size_t attr, rt_size_t flags,
|
|
|
|
rt_mem_obj_t mem_obj, rt_size_t offset)
|
|
|
|
{
|
|
|
|
varea->aspace = aspace;
|
|
|
|
varea->attr = attr;
|
|
|
|
varea->mem_obj = mem_obj;
|
|
|
|
varea->flag = flags;
|
|
|
|
varea->offset = offset;
|
|
|
|
varea->frames = NULL;
|
|
|
|
|
|
|
|
if (varea->mem_obj && varea->mem_obj->on_varea_open)
|
|
|
|
varea->mem_obj->on_varea_open(varea);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* restore context modified by varea install
|
|
|
|
* caller must NOT hold the aspace lock
|
|
|
|
*/
|
|
|
|
static void _varea_uninstall(rt_varea_t varea)
|
|
|
|
{
|
|
|
|
rt_aspace_t aspace = varea->aspace;
|
|
|
|
|
|
|
|
if (varea->mem_obj && varea->mem_obj->on_varea_close)
|
|
|
|
varea->mem_obj->on_varea_close(varea);
|
|
|
|
|
|
|
|
rt_hw_mmu_unmap(aspace, varea->start, varea->size);
|
|
|
|
rt_hw_tlb_invalidate_range(aspace, varea->start, varea->size, ARCH_PAGE_SIZE);
|
|
|
|
|
|
|
|
rt_varea_pgmgr_pop_all(varea);
|
|
|
|
|
|
|
|
WR_LOCK(aspace);
|
|
|
|
_aspace_bst_remove(aspace, varea);
|
|
|
|
WR_UNLOCK(aspace);
|
|
|
|
}
|
|
|
|
|
2023-01-09 10:08:55 +08:00
|
|
|
static int _mm_aspace_map(rt_aspace_t aspace, rt_varea_t varea, rt_size_t attr,
|
|
|
|
mm_flag_t flags, rt_mem_obj_t mem_obj,
|
|
|
|
rt_size_t offset)
|
|
|
|
{
|
|
|
|
int err = RT_EOK;
|
|
|
|
|
|
|
|
WR_LOCK(aspace);
|
2023-03-30 08:25:15 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief .prefer & .map_size are scratched from varea which setup by caller
|
|
|
|
* .limit_start & .limit_range_size have default to be in range of aspace
|
|
|
|
* .flags is from parameter, and will be fill in varea if install successfully
|
|
|
|
*/
|
2023-01-09 10:08:55 +08:00
|
|
|
struct rt_mm_va_hint hint = {.prefer = varea->start,
|
|
|
|
.map_size = varea->size,
|
|
|
|
.limit_start = aspace->start,
|
|
|
|
.limit_range_size = aspace->size,
|
|
|
|
.flags = flags};
|
|
|
|
|
|
|
|
if (mem_obj->hint_free)
|
|
|
|
{
|
2023-03-03 11:51:21 +08:00
|
|
|
/* mem object can control mapping range and so by modifing hint */
|
2023-01-09 10:08:55 +08:00
|
|
|
mem_obj->hint_free(&hint);
|
|
|
|
}
|
|
|
|
|
2023-03-30 08:25:15 +08:00
|
|
|
/* try to allocate a virtual address region for varea */
|
2023-01-09 10:08:55 +08:00
|
|
|
err = _varea_install(aspace, varea, &hint);
|
|
|
|
WR_UNLOCK(aspace);
|
|
|
|
|
|
|
|
if (err == RT_EOK)
|
|
|
|
{
|
2023-03-03 11:51:21 +08:00
|
|
|
/* fill in varea data */
|
2023-01-09 10:08:55 +08:00
|
|
|
_varea_post_install(varea, aspace, attr, flags, mem_obj, offset);
|
|
|
|
|
|
|
|
if (MMF_TEST_CNTL(flags, MMF_PREFETCH))
|
|
|
|
{
|
2023-03-03 11:51:21 +08:00
|
|
|
/* do the MMU & TLB business */
|
2023-01-09 10:08:55 +08:00
|
|
|
err = _do_prefetch(aspace, varea, varea->start, varea->size);
|
2023-03-03 11:51:21 +08:00
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
/* restore data structure and MMU */
|
|
|
|
_varea_uninstall(varea);
|
|
|
|
}
|
2023-01-09 10:08:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2023-03-30 08:25:15 +08:00
|
|
|
rt_varea_t _varea_create(void *start, rt_size_t size)
|
|
|
|
{
|
|
|
|
rt_varea_t varea;
|
|
|
|
varea = (rt_varea_t)rt_malloc(sizeof(struct rt_varea));
|
|
|
|
if (varea)
|
|
|
|
{
|
|
|
|
varea->start = start;
|
|
|
|
varea->size = size;
|
|
|
|
}
|
|
|
|
return varea;
|
|
|
|
}
|
|
|
|
|
2023-03-03 11:51:21 +08:00
|
|
|
#define _IS_OVERFLOW(start, length) ((length) > (0ul - (uintptr_t)(start)))
|
2023-04-22 23:59:11 +08:00
|
|
|
#define _IS_OVERSIZE(start, length, limit_s, limit_sz) (((length) + (rt_size_t)((char *)(start) - (char *)(limit_start))) > (limit_size))
|
2023-03-03 11:51:21 +08:00
|
|
|
|
2023-01-09 10:08:55 +08:00
|
|
|
static inline int _not_in_range(void *start, rt_size_t length,
|
|
|
|
void *limit_start, rt_size_t limit_size)
|
|
|
|
{
|
2023-02-20 13:44:20 +08:00
|
|
|
if (start != RT_NULL)
|
|
|
|
LOG_D("%s: [%p : %p] [%p : %p]", __func__, start, length, limit_start, limit_size);
|
2023-01-09 10:08:55 +08:00
|
|
|
/* assuming (base + length) will not overflow except (0) */
|
2023-02-20 13:44:20 +08:00
|
|
|
return start != RT_NULL
|
2023-03-03 11:51:21 +08:00
|
|
|
? (_IS_OVERFLOW(start, length) || start < limit_start ||
|
|
|
|
_IS_OVERSIZE(start, length, limit_start, limit_size))
|
2023-01-09 10:08:55 +08:00
|
|
|
: length > limit_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int _not_align(void *start, rt_size_t length, rt_size_t mask)
|
|
|
|
{
|
2023-02-20 13:44:20 +08:00
|
|
|
return (start != RT_NULL) &&
|
2023-01-09 10:08:55 +08:00
|
|
|
(((uintptr_t)start & mask) || (length & mask));
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int _not_support(rt_size_t flags)
|
|
|
|
{
|
2023-03-30 08:25:15 +08:00
|
|
|
rt_size_t support_ops = (MMF_PREFETCH | MMF_MAP_FIXED | MMF_TEXT |
|
|
|
|
MMF_STATIC_ALLOC | MMF_REQUEST_ALIGN);
|
2023-01-09 10:08:55 +08:00
|
|
|
return flags & ~(support_ops | _MMF_ALIGN_MASK);
|
|
|
|
}
|
|
|
|
|
|
|
|
int rt_aspace_map(rt_aspace_t aspace, void **addr, rt_size_t length,
|
|
|
|
rt_size_t attr, mm_flag_t flags, rt_mem_obj_t mem_obj,
|
|
|
|
rt_size_t offset)
|
|
|
|
{
|
|
|
|
/* TODO check not in atomic context: irq, spinlock, local intr disable... */
|
|
|
|
int err;
|
|
|
|
rt_varea_t varea;
|
|
|
|
|
2023-03-30 08:25:15 +08:00
|
|
|
if (!aspace || !addr || !mem_obj || length == 0)
|
2023-01-09 10:08:55 +08:00
|
|
|
{
|
|
|
|
err = -RT_EINVAL;
|
2023-03-30 08:25:15 +08:00
|
|
|
LOG_I("%s(%p, %p, %lx, %lx, %lx, %p, %lx): Invalid input",
|
|
|
|
__func__, aspace, addr, length, attr, flags, mem_obj, offset);
|
|
|
|
}
|
|
|
|
else if (_not_in_range(*addr, length, aspace->start, aspace->size))
|
|
|
|
{
|
|
|
|
err = -RT_EINVAL;
|
|
|
|
LOG_I("%s(addr:%p, len:%lx): out of range", __func__, *addr, length);
|
2023-01-09 10:08:55 +08:00
|
|
|
}
|
|
|
|
else if (_not_support(flags))
|
|
|
|
{
|
2023-03-30 08:25:15 +08:00
|
|
|
LOG_I("%s: no support flags 0x%lx", __func__, flags);
|
2023-01-09 10:08:55 +08:00
|
|
|
err = -RT_ENOSYS;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-03-30 08:25:15 +08:00
|
|
|
/* allocate the varea and fill in start and size */
|
2023-01-09 10:08:55 +08:00
|
|
|
varea = _varea_create(*addr, length);
|
|
|
|
|
|
|
|
if (varea)
|
|
|
|
{
|
|
|
|
err = _mm_aspace_map(aspace, varea, attr, flags, mem_obj, offset);
|
2023-03-03 11:51:21 +08:00
|
|
|
if (err != RT_EOK)
|
|
|
|
{
|
|
|
|
rt_free(varea);
|
|
|
|
}
|
2023-04-22 23:59:11 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
*addr = varea->start;
|
|
|
|
}
|
2023-01-09 10:08:55 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-03-30 08:25:15 +08:00
|
|
|
LOG_W("%s: memory allocation failed", __func__);
|
2023-01-09 10:08:55 +08:00
|
|
|
err = -RT_ENOMEM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err != RT_EOK)
|
|
|
|
{
|
|
|
|
*addr = NULL;
|
|
|
|
}
|
2023-04-22 23:59:11 +08:00
|
|
|
|
2023-01-09 10:08:55 +08:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rt_aspace_map_static(rt_aspace_t aspace, rt_varea_t varea, void **addr,
|
|
|
|
rt_size_t length, rt_size_t attr, mm_flag_t flags,
|
|
|
|
rt_mem_obj_t mem_obj, rt_size_t offset)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (!aspace || !varea || !addr || !mem_obj || length == 0 ||
|
|
|
|
_not_in_range(*addr, length, aspace->start, aspace->size))
|
|
|
|
{
|
|
|
|
err = -RT_EINVAL;
|
|
|
|
LOG_W("%s: Invalid input", __func__);
|
|
|
|
}
|
|
|
|
else if (_not_support(flags))
|
|
|
|
{
|
|
|
|
LOG_W("%s: no support flags", __func__);
|
|
|
|
err = -RT_ENOSYS;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
varea->size = length;
|
|
|
|
varea->start = *addr;
|
2023-03-03 11:51:21 +08:00
|
|
|
flags |= MMF_STATIC_ALLOC;
|
2023-01-09 10:08:55 +08:00
|
|
|
err = _mm_aspace_map(aspace, varea, attr, flags, mem_obj, offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err != RT_EOK)
|
|
|
|
{
|
|
|
|
*addr = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*addr = varea->start;
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
int _mm_aspace_map_phy(rt_aspace_t aspace, rt_varea_t varea,
|
|
|
|
rt_mm_va_hint_t hint, rt_size_t attr, rt_size_t pa_off,
|
|
|
|
void **ret_va)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
void *vaddr;
|
|
|
|
|
2023-01-16 08:24:03 +08:00
|
|
|
if (!aspace || !hint || !hint->limit_range_size || !hint->map_size)
|
|
|
|
{
|
|
|
|
LOG_W("%s: Invalid input", __func__);
|
|
|
|
err = -RT_EINVAL;
|
|
|
|
}
|
|
|
|
else if (_not_align(hint->prefer, hint->map_size, ARCH_PAGE_MASK))
|
|
|
|
{
|
|
|
|
LOG_W("%s: not aligned", __func__);
|
|
|
|
err = -RT_EINVAL;
|
|
|
|
}
|
|
|
|
else if (_not_in_range(hint->limit_start, hint->limit_range_size, aspace->start,
|
2023-01-09 10:08:55 +08:00
|
|
|
aspace->size) ||
|
|
|
|
_not_in_range(hint->prefer, hint->map_size, aspace->start,
|
|
|
|
aspace->size))
|
|
|
|
{
|
2023-01-16 08:24:03 +08:00
|
|
|
LOG_W("%s: not in range", __func__);
|
2023-01-09 10:08:55 +08:00
|
|
|
err = -RT_EINVAL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
WR_LOCK(aspace);
|
|
|
|
err = _varea_install(aspace, varea, hint);
|
|
|
|
WR_UNLOCK(aspace);
|
|
|
|
|
|
|
|
if (err == RT_EOK)
|
|
|
|
{
|
2023-03-30 08:25:15 +08:00
|
|
|
_varea_post_install(varea, aspace, attr, hint->flags, NULL, pa_off);
|
2023-01-09 10:08:55 +08:00
|
|
|
|
|
|
|
vaddr = varea->start;
|
|
|
|
|
|
|
|
err = _do_named_map(aspace, varea->start, varea->size,
|
|
|
|
(rt_size_t)pa_off, attr);
|
|
|
|
|
|
|
|
if (err != RT_EOK)
|
|
|
|
{
|
2023-03-03 11:51:21 +08:00
|
|
|
_varea_uninstall(varea);
|
2023-01-09 10:08:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret_va)
|
|
|
|
{
|
|
|
|
if (err == RT_EOK)
|
|
|
|
*ret_va = vaddr;
|
|
|
|
else
|
2023-02-20 13:44:20 +08:00
|
|
|
*ret_va = RT_NULL;
|
2023-01-09 10:08:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rt_aspace_map_phy(rt_aspace_t aspace, rt_mm_va_hint_t hint, rt_size_t attr,
|
|
|
|
rt_size_t pa_off, void **ret_va)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (hint)
|
|
|
|
{
|
|
|
|
rt_varea_t varea = _varea_create(hint->prefer, hint->map_size);
|
|
|
|
if (varea)
|
|
|
|
{
|
|
|
|
err = _mm_aspace_map_phy(aspace, varea, hint, attr, pa_off, ret_va);
|
|
|
|
if (err != RT_EOK)
|
|
|
|
{
|
|
|
|
rt_free(varea);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
err = -RT_ENOMEM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
err = -RT_EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rt_aspace_map_phy_static(rt_aspace_t aspace, rt_varea_t varea,
|
|
|
|
rt_mm_va_hint_t hint, rt_size_t attr,
|
|
|
|
rt_size_t pa_off, void **ret_va)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (varea && hint)
|
|
|
|
{
|
|
|
|
varea->start = hint->prefer;
|
|
|
|
varea->size = hint->map_size;
|
2023-03-03 11:51:21 +08:00
|
|
|
hint->flags |= (MMF_MAP_FIXED | MMF_STATIC_ALLOC);
|
2023-02-14 23:08:32 +08:00
|
|
|
LOG_D("%s: start %p size %p phy at %p", __func__, varea->start, varea->size, pa_off << MM_PAGE_SHIFT);
|
2023-01-09 10:08:55 +08:00
|
|
|
err = _mm_aspace_map_phy(aspace, varea, hint, attr, pa_off, ret_va);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
err = -RT_EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2023-03-30 08:25:15 +08:00
|
|
|
void _aspace_unmap(rt_aspace_t aspace, void *addr)
|
2023-01-09 10:08:55 +08:00
|
|
|
{
|
2023-03-30 08:25:15 +08:00
|
|
|
WR_LOCK(aspace);
|
|
|
|
rt_varea_t varea = _aspace_bst_search(aspace, addr);
|
|
|
|
WR_UNLOCK(aspace);
|
2023-01-09 10:08:55 +08:00
|
|
|
|
2023-03-03 11:51:21 +08:00
|
|
|
if (varea == RT_NULL)
|
|
|
|
{
|
2023-03-30 08:25:15 +08:00
|
|
|
LOG_I("%s: No such entry found at %p\n", __func__, addr);
|
2023-03-03 11:51:21 +08:00
|
|
|
}
|
2023-01-09 10:08:55 +08:00
|
|
|
|
2023-03-30 08:25:15 +08:00
|
|
|
_varea_uninstall(varea);
|
|
|
|
if (!(varea->flag & MMF_STATIC_ALLOC))
|
2023-03-03 11:51:21 +08:00
|
|
|
{
|
2023-03-30 08:25:15 +08:00
|
|
|
rt_free(varea);
|
2023-01-09 10:08:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-30 08:25:15 +08:00
|
|
|
int rt_aspace_unmap(rt_aspace_t aspace, void *addr)
|
2023-01-09 10:08:55 +08:00
|
|
|
{
|
|
|
|
if (!aspace)
|
|
|
|
{
|
|
|
|
LOG_I("%s: Invalid input", __func__);
|
|
|
|
return -RT_EINVAL;
|
|
|
|
}
|
|
|
|
|
2023-03-30 08:25:15 +08:00
|
|
|
if (_not_in_range(addr, 1, aspace->start, aspace->size))
|
2023-01-09 10:08:55 +08:00
|
|
|
{
|
|
|
|
LOG_I("%s: %lx not in range of aspace[%lx:%lx]", __func__, addr,
|
2023-04-22 23:59:11 +08:00
|
|
|
aspace->start, (char *)aspace->start + aspace->size);
|
2023-01-09 10:08:55 +08:00
|
|
|
return -RT_EINVAL;
|
|
|
|
}
|
|
|
|
|
2023-03-30 08:25:15 +08:00
|
|
|
_aspace_unmap(aspace, addr);
|
2023-01-09 10:08:55 +08:00
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void *_lower(void *a, void *b)
|
|
|
|
{
|
|
|
|
return a < b ? a : b;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void *_align(void *va, rt_ubase_t align_mask)
|
|
|
|
{
|
2023-04-22 23:59:11 +08:00
|
|
|
return (void *)((rt_ubase_t)((char *)va + ~align_mask) & align_mask);
|
2023-01-09 10:08:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void *_ascending_search(rt_varea_t varea, rt_size_t req_size,
|
|
|
|
rt_ubase_t align_mask, struct _mm_range limit)
|
|
|
|
{
|
2023-02-20 13:44:20 +08:00
|
|
|
void *ret = RT_NULL;
|
2023-01-09 10:08:55 +08:00
|
|
|
while (varea && varea->start < limit.end)
|
|
|
|
{
|
2023-04-22 23:59:11 +08:00
|
|
|
char *candidate = (char *)varea->start + varea->size;
|
2023-01-09 10:08:55 +08:00
|
|
|
candidate = _align(candidate, align_mask);
|
|
|
|
|
2023-04-22 23:59:11 +08:00
|
|
|
if (candidate > (char *)limit.end || (char *)limit.end - candidate + 1 < req_size)
|
2023-01-09 10:08:55 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
rt_varea_t nx_va = ASPACE_VAREA_NEXT(varea);
|
|
|
|
if (nx_va)
|
|
|
|
{
|
|
|
|
rt_size_t gap_size =
|
2023-04-22 23:59:11 +08:00
|
|
|
(char *)_lower(limit.end, (char *)nx_va->start - 1) - candidate + 1;
|
2023-01-09 10:08:55 +08:00
|
|
|
if (gap_size >= req_size)
|
|
|
|
{
|
|
|
|
ret = candidate;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = candidate;
|
|
|
|
}
|
|
|
|
varea = nx_va;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** find suitable place in [limit_start, limit_end] */
|
|
|
|
static void *_find_head_and_asc_search(rt_aspace_t aspace, rt_size_t req_size,
|
|
|
|
rt_ubase_t align_mask,
|
|
|
|
struct _mm_range limit)
|
|
|
|
{
|
2023-02-20 13:44:20 +08:00
|
|
|
void *va = RT_NULL;
|
2023-01-09 10:08:55 +08:00
|
|
|
|
|
|
|
rt_varea_t varea = _aspace_bst_search_exceed(aspace, limit.start);
|
|
|
|
if (varea)
|
|
|
|
{
|
2023-04-22 23:59:11 +08:00
|
|
|
char *candidate = _align(limit.start, align_mask);
|
|
|
|
rt_size_t gap_size = (char *)varea->start - candidate;
|
2023-01-09 10:08:55 +08:00
|
|
|
if (gap_size >= req_size)
|
|
|
|
{
|
|
|
|
rt_varea_t former = _aspace_bst_search(aspace, limit.start);
|
|
|
|
if (former)
|
|
|
|
{
|
2023-04-22 23:59:11 +08:00
|
|
|
candidate = _align((char *)former->start + former->size, align_mask);
|
|
|
|
gap_size = (char *)varea->start - candidate;
|
2023-01-09 10:08:55 +08:00
|
|
|
|
|
|
|
if (gap_size >= req_size)
|
|
|
|
va = candidate;
|
|
|
|
else
|
|
|
|
va = _ascending_search(varea, req_size, align_mask, limit);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
va = candidate;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
va = _ascending_search(varea, req_size, align_mask, limit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-04-22 23:59:11 +08:00
|
|
|
char *candidate;
|
2023-01-09 10:08:55 +08:00
|
|
|
rt_size_t gap_size;
|
|
|
|
|
2023-03-30 08:25:15 +08:00
|
|
|
candidate = limit.start;
|
2023-01-09 10:08:55 +08:00
|
|
|
candidate = _align(candidate, align_mask);
|
2023-04-22 23:59:11 +08:00
|
|
|
gap_size = (char *)limit.end - candidate + 1;
|
2023-01-09 10:08:55 +08:00
|
|
|
|
|
|
|
if (gap_size >= req_size)
|
|
|
|
va = candidate;
|
|
|
|
}
|
|
|
|
|
|
|
|
return va;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *_find_free(rt_aspace_t aspace, void *prefer, rt_size_t req_size,
|
|
|
|
void *limit_start, rt_size_t limit_size,
|
|
|
|
mm_flag_t flags)
|
|
|
|
{
|
|
|
|
rt_varea_t varea = NULL;
|
2023-02-20 13:44:20 +08:00
|
|
|
void *va = RT_NULL;
|
2023-04-22 23:59:11 +08:00
|
|
|
struct _mm_range limit = {limit_start, (char *)limit_start + limit_size - 1};
|
2023-01-09 10:08:55 +08:00
|
|
|
|
|
|
|
rt_ubase_t align_mask = ~0ul;
|
|
|
|
if (flags & MMF_REQUEST_ALIGN)
|
|
|
|
{
|
|
|
|
align_mask = ~((1 << MMF_GET_ALIGN(flags)) - 1);
|
|
|
|
}
|
|
|
|
|
2023-02-20 13:44:20 +08:00
|
|
|
if (prefer != RT_NULL)
|
2023-01-09 10:08:55 +08:00
|
|
|
{
|
2023-03-30 08:25:15 +08:00
|
|
|
/* if prefer and free, just return the prefer region */
|
2023-01-09 10:08:55 +08:00
|
|
|
prefer = _align(prefer, align_mask);
|
2023-04-22 23:59:11 +08:00
|
|
|
struct _mm_range range = {prefer, (char *)prefer + req_size - 1};
|
2023-01-09 10:08:55 +08:00
|
|
|
varea = _aspace_bst_search_overlap(aspace, range);
|
|
|
|
|
|
|
|
if (!varea)
|
|
|
|
{
|
|
|
|
va = prefer;
|
|
|
|
}
|
|
|
|
else if (flags & MMF_MAP_FIXED)
|
|
|
|
{
|
2023-02-20 13:44:20 +08:00
|
|
|
/* OVERLAP */
|
2023-01-09 10:08:55 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-03-30 08:25:15 +08:00
|
|
|
/* search from `varea` in ascending order */
|
2023-01-09 10:08:55 +08:00
|
|
|
va = _ascending_search(varea, req_size, align_mask, limit);
|
2023-02-20 13:44:20 +08:00
|
|
|
if (va == RT_NULL)
|
2023-01-09 10:08:55 +08:00
|
|
|
{
|
2023-03-30 08:25:15 +08:00
|
|
|
/* rewind to first range */
|
2023-04-22 23:59:11 +08:00
|
|
|
limit.end = (char *)varea->start - 1;
|
2023-01-09 10:08:55 +08:00
|
|
|
va = _find_head_and_asc_search(aspace, req_size, align_mask,
|
|
|
|
limit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
va = _find_head_and_asc_search(aspace, req_size, align_mask, limit);
|
|
|
|
}
|
|
|
|
|
|
|
|
return va;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rt_aspace_load_page(rt_aspace_t aspace, void *addr, rt_size_t npage)
|
|
|
|
{
|
|
|
|
int err = RT_EOK;
|
2023-03-30 08:25:15 +08:00
|
|
|
rt_varea_t varea;
|
2023-04-22 23:59:11 +08:00
|
|
|
char *end = (char *)addr + (npage << ARCH_PAGE_SHIFT);
|
2023-01-09 10:08:55 +08:00
|
|
|
|
2023-03-30 08:25:15 +08:00
|
|
|
WR_LOCK(aspace);
|
|
|
|
varea = _aspace_bst_search(aspace, addr);
|
|
|
|
WR_UNLOCK(aspace);
|
|
|
|
|
2023-01-09 10:08:55 +08:00
|
|
|
if (!varea)
|
|
|
|
{
|
|
|
|
LOG_W("%s: varea not exist", __func__);
|
|
|
|
err = -RT_ENOENT;
|
|
|
|
}
|
2023-04-22 23:59:11 +08:00
|
|
|
else if ((char *)addr >= end || (rt_size_t)addr & ARCH_PAGE_MASK ||
|
2023-01-09 10:08:55 +08:00
|
|
|
_not_in_range(addr, npage << ARCH_PAGE_SHIFT, varea->start,
|
|
|
|
varea->size))
|
|
|
|
{
|
|
|
|
LOG_W("%s: Unaligned parameter or out of range", __func__);
|
|
|
|
err = -RT_EINVAL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
err = _do_prefetch(aspace, varea, addr, npage << ARCH_PAGE_SHIFT);
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2023-03-30 08:25:15 +08:00
|
|
|
int rt_varea_map_page(rt_varea_t varea, void *vaddr, void *page)
|
|
|
|
{
|
|
|
|
int err = RT_EOK;
|
|
|
|
void *page_pa = rt_kmem_v2p(page);
|
|
|
|
|
|
|
|
if (!varea || !vaddr || !page)
|
|
|
|
{
|
|
|
|
LOG_W("%s(%p,%p,%p): invalid input", __func__, varea, vaddr, page);
|
|
|
|
err = -RT_EINVAL;
|
|
|
|
}
|
|
|
|
else if (page_pa == ARCH_MAP_FAILED)
|
|
|
|
{
|
|
|
|
LOG_W("%s: page is not in kernel space", __func__);
|
|
|
|
err = -RT_ERROR;
|
|
|
|
}
|
|
|
|
else if (_not_in_range(vaddr, ARCH_PAGE_SIZE, varea->start, varea->size))
|
|
|
|
{
|
|
|
|
LOG_W("%s(%p,%lx): not in range of varea(%p,%lx)", __func__,
|
|
|
|
vaddr, ARCH_PAGE_SIZE, varea->start, varea->size);
|
|
|
|
err = -RT_EINVAL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
err = _do_named_map(
|
|
|
|
varea->aspace,
|
|
|
|
vaddr,
|
|
|
|
ARCH_PAGE_SIZE,
|
|
|
|
MM_PA_TO_OFF(page_pa),
|
|
|
|
varea->attr
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define ALIGNED(addr) (!((rt_size_t)(addr) & ARCH_PAGE_MASK))
|
|
|
|
|
|
|
|
int rt_varea_map_range(rt_varea_t varea, void *vaddr, void *paddr, rt_size_t length)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
if (!varea || !vaddr || !paddr || !length ||
|
|
|
|
!ALIGNED(vaddr) || !ALIGNED(paddr) || !(ALIGNED(length)))
|
|
|
|
{
|
|
|
|
LOG_W("%s(%p,%p,%p,%lx): invalid input", __func__, varea, vaddr, paddr, length);
|
|
|
|
err = -RT_EINVAL;
|
|
|
|
}
|
|
|
|
else if (_not_in_range(vaddr, length, varea->start, varea->size))
|
|
|
|
{
|
|
|
|
LOG_W("%s(%p,%lx): not in range of varea(%p,%lx)", __func__,
|
|
|
|
vaddr, length, varea->start, varea->size);
|
|
|
|
err = -RT_EINVAL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
err = _do_named_map(
|
|
|
|
varea->aspace,
|
|
|
|
vaddr,
|
|
|
|
length,
|
|
|
|
MM_PA_TO_OFF(paddr),
|
|
|
|
varea->attr
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2023-01-09 10:08:55 +08:00
|
|
|
int rt_aspace_offload_page(rt_aspace_t aspace, void *addr, rt_size_t npage)
|
|
|
|
{
|
|
|
|
return -RT_ENOSYS;
|
|
|
|
}
|
|
|
|
|
2023-03-30 08:25:15 +08:00
|
|
|
int rt_aspace_control(rt_aspace_t aspace, void *addr, enum rt_mmu_cntl cmd)
|
2023-01-09 10:08:55 +08:00
|
|
|
{
|
|
|
|
int err;
|
2023-03-30 08:25:15 +08:00
|
|
|
rt_varea_t varea;
|
|
|
|
|
|
|
|
WR_LOCK(aspace);
|
|
|
|
varea = _aspace_bst_search(aspace, addr);
|
|
|
|
WR_UNLOCK(aspace);
|
|
|
|
|
2023-01-09 10:08:55 +08:00
|
|
|
if (varea)
|
|
|
|
{
|
|
|
|
err = rt_hw_mmu_control(aspace, varea->start, varea->size, cmd);
|
2023-03-30 08:25:15 +08:00
|
|
|
if (err == RT_EOK)
|
|
|
|
{
|
|
|
|
rt_hw_tlb_invalidate_range(aspace, varea->start, varea->size, ARCH_PAGE_SIZE);
|
|
|
|
}
|
2023-01-09 10:08:55 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
err = -RT_ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rt_aspace_traversal(rt_aspace_t aspace,
|
|
|
|
int (*fn)(rt_varea_t varea, void *arg), void *arg)
|
|
|
|
{
|
2023-03-30 08:25:15 +08:00
|
|
|
rt_varea_t varea;
|
|
|
|
WR_LOCK(aspace);
|
|
|
|
varea = ASPACE_VAREA_FIRST(aspace);
|
2023-01-09 10:08:55 +08:00
|
|
|
while (varea)
|
|
|
|
{
|
|
|
|
fn(varea, arg);
|
|
|
|
varea = ASPACE_VAREA_NEXT(varea);
|
|
|
|
}
|
2023-03-30 08:25:15 +08:00
|
|
|
WR_UNLOCK(aspace);
|
2023-01-09 10:08:55 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int _dump(rt_varea_t varea, void *arg)
|
|
|
|
{
|
2023-02-14 23:08:32 +08:00
|
|
|
if (varea->mem_obj && varea->mem_obj->get_name)
|
|
|
|
{
|
2023-04-22 23:59:11 +08:00
|
|
|
rt_kprintf("[%p - %p] %s\n", varea->start, (char *)varea->start + varea->size,
|
2023-02-14 23:08:32 +08:00
|
|
|
varea->mem_obj->get_name(varea));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-04-22 23:59:11 +08:00
|
|
|
rt_kprintf("[%p - %p] phy-map\n", varea->start, (char *)varea->start + varea->size);
|
2023-02-14 23:08:32 +08:00
|
|
|
rt_kprintf("\t\\_ paddr = %p\n", varea->offset << MM_PAGE_SHIFT);
|
|
|
|
}
|
2023-01-09 10:08:55 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void rt_aspace_print_all(rt_aspace_t aspace)
|
|
|
|
{
|
|
|
|
rt_aspace_traversal(aspace, _dump, NULL);
|
|
|
|
}
|