2022-12-03 12:07:44 +08:00
|
|
|
/*
|
2024-09-02 17:58:56 +08:00
|
|
|
* Copyright (c) 2006-2024, RT-Thread Development Team
|
2022-12-03 12:07:44 +08:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2021-01-30 lizhirui first version
|
|
|
|
* 2021-05-03 lizhirui porting to c906
|
2023-10-17 13:07:59 +08:00
|
|
|
* 2023-10-12 Shell Add permission control API
|
2022-12-03 12:07:44 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __RISCV_MMU_H__
|
|
|
|
#define __RISCV_MMU_H__
|
|
|
|
|
|
|
|
#include <rtthread.h>
|
2023-01-09 10:08:55 +08:00
|
|
|
#include <rthw.h>
|
2022-12-03 12:07:44 +08:00
|
|
|
#include "riscv.h"
|
|
|
|
|
|
|
|
#undef PAGE_SIZE
|
|
|
|
|
|
|
|
/* C-SKY extend */
|
2024-09-02 17:58:56 +08:00
|
|
|
#define PTE_SEC (1UL << 59) /* Security */
|
|
|
|
#define PTE_SHARE (1UL << 60) /* Shareable */
|
|
|
|
#define PTE_BUF (1UL << 61) /* Bufferable */
|
|
|
|
#define PTE_CACHE (1UL << 62) /* Cacheable */
|
|
|
|
#define PTE_SO (1UL << 63) /* Strong Order */
|
2022-12-03 12:07:44 +08:00
|
|
|
|
|
|
|
#define PAGE_OFFSET_SHIFT 0
|
2024-09-02 17:58:56 +08:00
|
|
|
#define PAGE_OFFSET_BIT 12
|
|
|
|
#define PAGE_SIZE __SIZE(PAGE_OFFSET_BIT)
|
|
|
|
#define PAGE_OFFSET_MASK __MASK(PAGE_OFFSET_BIT)
|
|
|
|
#define VPN0_SHIFT (PAGE_OFFSET_SHIFT + PAGE_OFFSET_BIT)
|
|
|
|
#define VPN0_BIT 9
|
|
|
|
#define VPN1_SHIFT (VPN0_SHIFT + VPN0_BIT)
|
|
|
|
#define VPN1_BIT 9
|
|
|
|
#define VPN2_SHIFT (VPN1_SHIFT + VPN1_BIT)
|
|
|
|
#define VPN2_BIT 9
|
2022-12-03 12:07:44 +08:00
|
|
|
|
|
|
|
#define PPN0_SHIFT (PAGE_OFFSET_SHIFT + PAGE_OFFSET_BIT)
|
2024-09-02 17:58:56 +08:00
|
|
|
#define PPN0_BIT 9
|
2022-12-03 12:07:44 +08:00
|
|
|
#define PPN1_SHIFT (PPN0_SHIFT + PPN0_BIT)
|
2024-09-02 17:58:56 +08:00
|
|
|
#define PPN1_BIT 9
|
2022-12-03 12:07:44 +08:00
|
|
|
#define PPN2_SHIFT (PPN1_SHIFT + PPN1_BIT)
|
2024-09-02 17:58:56 +08:00
|
|
|
#define PPN2_BIT 26
|
|
|
|
#define PPN_BITS (PPN0_BIT + PPN1_BIT + PPN2_BIT)
|
2022-12-03 12:07:44 +08:00
|
|
|
|
|
|
|
#define L1_PAGE_SIZE __SIZE(PAGE_OFFSET_BIT + VPN0_BIT + VPN1_BIT)
|
|
|
|
#define L2_PAGE_SIZE __SIZE(PAGE_OFFSET_BIT + VPN0_BIT)
|
|
|
|
#define L3_PAGE_SIZE __SIZE(PAGE_OFFSET_BIT)
|
|
|
|
|
|
|
|
#define ARCH_ADDRESS_WIDTH_BITS 64
|
|
|
|
|
|
|
|
#define PHYSICAL_ADDRESS_WIDTH_BITS 56
|
|
|
|
|
2024-09-02 17:58:56 +08:00
|
|
|
#define PAGE_ATTR_NEXT_LEVEL (0)
|
|
|
|
#define PAGE_ATTR_RWX (PTE_X | PTE_W | PTE_R)
|
|
|
|
#define PAGE_ATTR_READONLY (PTE_R)
|
|
|
|
#define PAGE_ATTR_XN (PTE_W | PTE_R)
|
|
|
|
#define PAGE_ATTR_READEXECUTE (PTE_X | PTE_R)
|
2022-12-03 12:07:44 +08:00
|
|
|
|
2024-09-02 17:58:56 +08:00
|
|
|
#define PAGE_ATTR_USER (PTE_U)
|
2022-12-03 12:07:44 +08:00
|
|
|
#define PAGE_ATTR_SYSTEM (0)
|
|
|
|
|
2024-09-02 17:58:56 +08:00
|
|
|
#define PAGE_ATTR_CB (PTE_BUF | PTE_CACHE)
|
|
|
|
#define PAGE_ATTR_DEV (PTE_SO)
|
2023-01-16 08:24:03 +08:00
|
|
|
|
2024-09-02 17:58:56 +08:00
|
|
|
#define PAGE_DEFAULT_ATTR_LEAF \
|
|
|
|
(PTE_SHARE | PTE_BUF | PTE_CACHE | PTE_A | PTE_D | PTE_G | PTE_U | \
|
|
|
|
PAGE_ATTR_RWX | PTE_V)
|
|
|
|
#define PAGE_DEFAULT_ATTR_NEXT \
|
|
|
|
(PTE_SHARE | PTE_BUF | PTE_CACHE | PTE_A | PTE_D | PTE_G | PTE_V)
|
2022-12-03 12:07:44 +08:00
|
|
|
|
2023-01-09 10:08:55 +08:00
|
|
|
#define PAGE_IS_LEAF(pte) __MASKVALUE(pte, PAGE_ATTR_RWX)
|
2022-12-03 12:07:44 +08:00
|
|
|
|
2024-09-02 17:58:56 +08:00
|
|
|
#define PTE_USED(pte) __MASKVALUE(pte, PTE_V)
|
2023-01-16 08:24:03 +08:00
|
|
|
#define PTE_WRAP(attr) (attr | PTE_A | PTE_D)
|
2022-12-03 12:07:44 +08:00
|
|
|
|
2023-01-09 10:08:55 +08:00
|
|
|
/**
|
|
|
|
* encoding of SATP (Supervisor Address Translation and Protection register)
|
|
|
|
*/
|
2024-09-02 17:58:56 +08:00
|
|
|
#define SATP_MODE_OFFSET 60
|
|
|
|
#define SATP_MODE_BARE 0
|
|
|
|
#define SATP_MODE_SV39 8
|
|
|
|
#define SATP_MODE_SV48 9
|
|
|
|
#define SATP_MODE_SV57 10
|
|
|
|
#define SATP_MODE_SV64 11
|
|
|
|
|
|
|
|
#define ARCH_VADDR_WIDTH 39
|
|
|
|
#define SATP_MODE SATP_MODE_SV39
|
|
|
|
|
|
|
|
#define MMU_MAP_K_DEVICE PTE_WRAP(PAGE_ATTR_DEV | PTE_G | PAGE_ATTR_XN | PTE_V)
|
|
|
|
#define MMU_MAP_K_RWCB PTE_WRAP(PAGE_ATTR_CB | PTE_G | PAGE_ATTR_RWX | PTE_V)
|
|
|
|
#define MMU_MAP_K_RW PTE_WRAP(PTE_G | PAGE_ATTR_RWX | PTE_V)
|
|
|
|
#define MMU_MAP_U_RWCB PTE_WRAP(PAGE_ATTR_CB | PTE_U | PAGE_ATTR_RWX | PTE_V)
|
|
|
|
#define MMU_MAP_U_ROCB \
|
|
|
|
PTE_WRAP(PAGE_ATTR_CB | PTE_U | PAGE_ATTR_READONLY | PTE_V)
|
|
|
|
#define MMU_MAP_U_RWCB_XN PTE_WRAP(PAGE_ATTR_CB | PTE_U | PAGE_ATTR_XN | PTE_V)
|
|
|
|
#define MMU_MAP_U_RW PTE_WRAP(PTE_U | PAGE_ATTR_RWX | PTE_V)
|
|
|
|
#define MMU_MAP_EARLY \
|
|
|
|
PTE_WRAP(PAGE_ATTR_RWX | PTE_G | PTE_V | PTE_CACHE | PTE_SHARE | PTE_BUF)
|
|
|
|
#define MMU_MAP_TRACE(attr) (attr)
|
|
|
|
|
|
|
|
#define PTE_XWR_MASK 0xe
|
|
|
|
|
|
|
|
#define ARCH_PAGE_SIZE PAGE_SIZE
|
|
|
|
#define ARCH_PAGE_MASK (ARCH_PAGE_SIZE - 1)
|
|
|
|
#define ARCH_PAGE_SHIFT PAGE_OFFSET_BIT
|
|
|
|
#define ARCH_INDEX_WIDTH 9
|
|
|
|
#define ARCH_INDEX_SIZE (1ul << ARCH_INDEX_WIDTH)
|
|
|
|
#define ARCH_INDEX_MASK (ARCH_INDEX_SIZE - 1)
|
|
|
|
|
|
|
|
#define ARCH_MAP_FAILED ((void *)0x8000000000000000)
|
2022-12-03 12:07:44 +08:00
|
|
|
|
|
|
|
void mmu_set_pagetable(rt_ubase_t addr);
|
2024-02-21 11:45:04 +08:00
|
|
|
void mmu_enable_user_page_access(void);
|
|
|
|
void mmu_disable_user_page_access(void);
|
2022-12-03 12:07:44 +08:00
|
|
|
|
2024-09-02 17:58:56 +08:00
|
|
|
#define RT_HW_MMU_PROT_READ 1
|
|
|
|
#define RT_HW_MMU_PROT_WRITE 2
|
2023-10-20 13:28:20 +08:00
|
|
|
#define RT_HW_MMU_PROT_EXECUTE 4
|
2024-09-02 17:58:56 +08:00
|
|
|
#define RT_HW_MMU_PROT_KERNEL 8
|
|
|
|
#define RT_HW_MMU_PROT_USER 16
|
|
|
|
#define RT_HW_MMU_PROT_CACHE 32
|
|
|
|
|
|
|
|
void rt_hw_asid_init(void);
|
|
|
|
struct rt_aspace;
|
|
|
|
void rt_hw_asid_switch_pgtbl(struct rt_aspace *aspace, rt_ubase_t pgtbl);
|
2023-10-17 13:07:59 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Remove permission from attribution
|
|
|
|
*
|
|
|
|
* @param attr architecture specified mmu attribution
|
|
|
|
* @param prot protect that will be removed
|
|
|
|
* @return size_t returned attribution
|
|
|
|
*/
|
2023-10-20 13:28:20 +08:00
|
|
|
rt_inline size_t rt_hw_mmu_attr_rm_perm(size_t attr, rt_base_t prot)
|
2023-10-17 13:07:59 +08:00
|
|
|
{
|
|
|
|
switch (prot)
|
|
|
|
{
|
|
|
|
/* remove write permission for user */
|
|
|
|
case RT_HW_MMU_PROT_WRITE | RT_HW_MMU_PROT_USER:
|
2024-06-26 12:16:37 +08:00
|
|
|
attr &= ~PTE_W;
|
|
|
|
break;
|
|
|
|
/* remove write permission for kernel */
|
|
|
|
case RT_HW_MMU_PROT_WRITE | RT_HW_MMU_PROT_KERNEL:
|
|
|
|
attr &= ~PTE_W;
|
|
|
|
break;
|
2023-10-17 13:07:59 +08:00
|
|
|
default:
|
|
|
|
RT_ASSERT(0);
|
|
|
|
}
|
|
|
|
return attr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Add permission from attribution
|
|
|
|
*
|
|
|
|
* @param attr architecture specified mmu attribution
|
|
|
|
* @param prot protect that will be added
|
|
|
|
* @return size_t returned attribution
|
|
|
|
*/
|
2023-10-20 13:28:20 +08:00
|
|
|
rt_inline size_t rt_hw_mmu_attr_add_perm(size_t attr, rt_base_t prot)
|
2023-10-17 13:07:59 +08:00
|
|
|
{
|
|
|
|
switch (prot)
|
|
|
|
{
|
|
|
|
/* add write permission for user */
|
|
|
|
case RT_HW_MMU_PROT_WRITE | RT_HW_MMU_PROT_USER:
|
2024-06-26 12:16:37 +08:00
|
|
|
attr |= (PTE_R | PTE_W | PTE_U);
|
|
|
|
break;
|
2023-10-17 13:07:59 +08:00
|
|
|
default:
|
|
|
|
RT_ASSERT(0);
|
|
|
|
}
|
|
|
|
return attr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Test permission from attribution
|
|
|
|
*
|
|
|
|
* @param attr architecture specified mmu attribution
|
|
|
|
* @param prot protect that will be test
|
|
|
|
* @return rt_bool_t RT_TRUE if the prot is allowed, otherwise RT_FALSE
|
|
|
|
*/
|
2023-10-20 13:28:20 +08:00
|
|
|
rt_inline rt_bool_t rt_hw_mmu_attr_test_perm(size_t attr, rt_base_t prot)
|
2023-10-17 13:07:59 +08:00
|
|
|
{
|
|
|
|
rt_bool_t rc = 0;
|
2024-06-26 12:16:37 +08:00
|
|
|
switch (prot & ~RT_HW_MMU_PROT_USER)
|
2023-10-17 13:07:59 +08:00
|
|
|
{
|
|
|
|
/* test write permission for user */
|
2024-06-26 12:16:37 +08:00
|
|
|
case RT_HW_MMU_PROT_WRITE:
|
|
|
|
rc = ((attr & PTE_W) && (attr & PTE_R));
|
|
|
|
break;
|
|
|
|
case RT_HW_MMU_PROT_READ:
|
|
|
|
rc = !!(attr & PTE_R);
|
|
|
|
break;
|
|
|
|
case RT_HW_MMU_PROT_EXECUTE:
|
|
|
|
rc = !!(attr & PTE_X);
|
|
|
|
break;
|
2023-10-17 13:07:59 +08:00
|
|
|
default:
|
|
|
|
RT_ASSERT(0);
|
|
|
|
}
|
2024-06-26 12:16:37 +08:00
|
|
|
|
|
|
|
if (rc && (prot & RT_HW_MMU_PROT_USER))
|
|
|
|
{
|
|
|
|
rc = !!(attr & PTE_U);
|
|
|
|
}
|
2023-10-17 13:07:59 +08:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
#endif
|