mmu.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  1. /*
  2. * Copyright (c) 2006-2025 RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-01-30 lizhirui first version
  9. * 2022-12-13 WangXiaoyao Port to new mm
  10. * 2023-10-12 Shell Add permission control API
  11. */
  12. #include <rtthread.h>
  13. #include <stddef.h>
  14. #include <stdint.h>
  15. #define DBG_TAG "hw.mmu"
  16. #define DBG_LVL DBG_INFO
  17. #include <rtdbg.h>
  18. #include <board.h>
  19. #include <cache.h>
  20. #include <mm_aspace.h>
  21. #include <mm_page.h>
  22. #include <mmu.h>
  23. #include <riscv_mmu.h>
  24. #include <tlb.h>
  25. #ifdef RT_USING_SMART
  26. #include <board.h>
  27. #include <ioremap.h>
  28. #include <lwp_user_mm.h>
  29. #endif
  30. #ifndef RT_USING_SMART
  31. #define USER_VADDR_START 0
  32. #endif
  33. static size_t _unmap_area(struct rt_aspace *aspace, void *v_addr);
  34. /* Define the structure of early page table */
  35. struct page_table
  36. {
  37. unsigned long page[ARCH_PAGE_SIZE / sizeof(unsigned long)];
  38. };
  39. static struct page_table *__init_page_array;
  40. #ifndef RT_USING_SMP
  41. static void *current_mmu_table = RT_NULL;
  42. #else
  43. static void *current_mmu_table[RT_CPUS_NR] = { RT_NULL };
  44. #endif /* RT_USING_SMP */
  45. volatile __attribute__((aligned(4 * 1024)))
  46. rt_ubase_t MMUTable[__SIZE(VPN2_BIT) * RT_CPUS_NR];
  47. /**
  48. * @brief Switch the current address space to the specified one.
  49. *
  50. * This function is responsible for switching the address space by updating the page table
  51. * and related hardware state. The behavior depends on whether the architecture supports
  52. * Address Space Identifiers (ASIDs), devided by macro definition of ARCH_USING_ASID.
  53. *
  54. * @param aspace Pointer to the address space structure containing the new page table.
  55. *
  56. * @note If ASID is supported (`ARCH_USING_ASID` is defined), the function will call
  57. * `rt_hw_asid_switch_pgtbl` to switch the page table and update the ASID.
  58. * Otherwise, it will directly write the `satp` CSR to switch the page table
  59. * and invalidate the TLB.
  60. */
  61. #ifdef ARCH_USING_ASID
  62. void rt_hw_aspace_switch(rt_aspace_t aspace)
  63. {
  64. uintptr_t page_table = (uintptr_t)rt_kmem_v2p(aspace->page_table);
  65. current_mmu_table = aspace->page_table;
  66. rt_hw_asid_switch_pgtbl(aspace, page_table);
  67. }
  68. #else /* !ARCH_USING_ASID */
  69. void rt_hw_aspace_switch(rt_aspace_t aspace)
  70. {
  71. // It is necessary to find the MMU page table specific to each core.
  72. uint32_t hartid = rt_cpu_get_id();
  73. uintptr_t ptr = (uintptr_t)aspace->page_table + (uintptr_t)(hartid * ARCH_PAGE_SIZE);
  74. uintptr_t page_table = (uintptr_t)rt_kmem_v2p((void *)ptr);
  75. #ifndef RT_USING_SMP
  76. current_mmu_table = aspace->page_table;
  77. #else
  78. current_mmu_table[rt_hw_cpu_id()] = (void *)ptr;
  79. #endif
  80. write_csr(satp, (((size_t)SATP_MODE) << SATP_MODE_OFFSET) |
  81. ((rt_ubase_t)page_table >> PAGE_OFFSET_BIT));
  82. rt_hw_tlb_invalidate_all_local();
  83. }
  84. void rt_hw_asid_init(void)
  85. {
  86. }
  87. #endif /* ARCH_USING_ASID */
  88. /* get current page table. */
  89. void *rt_hw_mmu_tbl_get()
  90. {
  91. #ifndef RT_USING_SMP
  92. return current_mmu_table;
  93. #else
  94. return current_mmu_table[rt_hw_cpu_id()];
  95. #endif /* RT_USING_SMP */
  96. }
  97. /* Map a single virtual address page to a physical address page in the page table. */
  98. static int _map_one_page(struct rt_aspace *aspace, void *va, void *pa,
  99. size_t attr)
  100. {
  101. rt_ubase_t l1_off, l2_off, l3_off;
  102. rt_ubase_t *mmu_l1, *mmu_l2, *mmu_l3;
  103. l1_off = GET_L1((size_t)va);
  104. l2_off = GET_L2((size_t)va);
  105. l3_off = GET_L3((size_t)va);
  106. /* Create a separate page table for each hart to facilitate access to the .percpu section. */
  107. for (int hartid = 0; hartid < RT_CPUS_NR; hartid++)
  108. {
  109. mmu_l1 = (rt_ubase_t *)((rt_ubase_t)aspace->page_table + (rt_ubase_t)(hartid * ARCH_PAGE_SIZE)) + l1_off;
  110. if (PTE_USED(*mmu_l1))
  111. {
  112. mmu_l2 = (rt_ubase_t *)PPN_TO_VPN(GET_PADDR(*mmu_l1), PV_OFFSET);
  113. }
  114. else
  115. {
  116. mmu_l2 = (rt_ubase_t *)rt_pages_alloc(0);
  117. if (mmu_l2)
  118. {
  119. rt_memset(mmu_l2, 0, PAGE_SIZE);
  120. rt_hw_cpu_dcache_clean(mmu_l2, PAGE_SIZE);
  121. *mmu_l1 = COMBINEPTE((rt_ubase_t)VPN_TO_PPN(mmu_l2, PV_OFFSET),
  122. PAGE_DEFAULT_ATTR_NEXT);
  123. rt_hw_cpu_dcache_clean(mmu_l1, sizeof(*mmu_l1));
  124. }
  125. else
  126. {
  127. return -1;
  128. }
  129. }
  130. if (PTE_USED(*(mmu_l2 + l2_off)))
  131. {
  132. RT_ASSERT(!PAGE_IS_LEAF(*(mmu_l2 + l2_off)));
  133. mmu_l3 =
  134. (rt_ubase_t *)PPN_TO_VPN(GET_PADDR(*(mmu_l2 + l2_off)), PV_OFFSET);
  135. }
  136. else
  137. {
  138. mmu_l3 = (rt_ubase_t *)rt_pages_alloc(0);
  139. if (mmu_l3)
  140. {
  141. rt_memset(mmu_l3, 0, PAGE_SIZE);
  142. rt_hw_cpu_dcache_clean(mmu_l3, PAGE_SIZE);
  143. *(mmu_l2 + l2_off) =
  144. COMBINEPTE((rt_ubase_t)VPN_TO_PPN(mmu_l3, PV_OFFSET),
  145. PAGE_DEFAULT_ATTR_NEXT);
  146. rt_hw_cpu_dcache_clean(mmu_l2, sizeof(*mmu_l2));
  147. /* declares a reference to parent page table */
  148. rt_page_ref_inc((void *)mmu_l2, 0);
  149. }
  150. else
  151. {
  152. return -1;
  153. }
  154. }
  155. RT_ASSERT(!PTE_USED(*(mmu_l3 + l3_off)));
  156. /* declares a reference to parent page table */
  157. rt_page_ref_inc((void *)mmu_l3, 0);
  158. *(mmu_l3 + l3_off) = COMBINEPTE((rt_ubase_t)pa, attr);
  159. rt_hw_cpu_dcache_clean(mmu_l3 + l3_off, sizeof(*(mmu_l3 + l3_off)));
  160. }
  161. return 0;
  162. }
  163. #if defined(RT_USING_SMP) && defined(ARCH_MM_MMU)
  164. static int _map_percpu_area(rt_ubase_t *table, void *va, void *pa, int cpu_id)
  165. {
  166. unsigned long page;
  167. rt_ubase_t off, level_shift;
  168. level_shift = PPN2_SHIFT;
  169. // map pages - 4KB
  170. for (int level = 0; level < 2; ++level)
  171. {
  172. off = ((rt_ubase_t)va >> level_shift) & VPN_MASK;
  173. if (table[off] & PTE_V)
  174. {
  175. /* Step into the next level page table */
  176. table = (unsigned long *)((table[off] >> PTE_BITS) << ARCH_PAGE_SHIFT);
  177. level_shift -= VPN_BITS;
  178. continue;
  179. }
  180. if (!(page = get_free_page()))
  181. {
  182. return MMU_MAP_ERROR_NOPAGE;
  183. }
  184. rt_memset((void *)page, 0, ARCH_PAGE_SIZE);
  185. table[off] = ((page >> ARCH_PAGE_SHIFT) << PTE_BITS) | PTE_V;
  186. rt_hw_cpu_dcache_ops(RT_HW_CACHE_FLUSH, table + off, sizeof(void *));
  187. /* Step into the next level page table */
  188. table = (unsigned long *)((table[off] >> PTE_BITS) << ARCH_PAGE_SHIFT);
  189. level_shift -= VPN_BITS;
  190. }
  191. off = ((rt_ubase_t)va >> level_shift) & VPN_MASK;
  192. table[off] = (((rt_ubase_t)pa >> ARCH_PAGE_SHIFT) << PTE_BITS) | MMU_MAP_K_RWCB;
  193. rt_hw_cpu_dcache_ops(RT_HW_CACHE_FLUSH, table + off, sizeof(void *));
  194. return ARCH_PAGE_SIZE;
  195. }
  196. // Ensure that the .percpu section is mapped in the specific address for each core.
  197. static void rt_hw_percpu_mmu_init_check(void)
  198. {
  199. size_t mapped, size;
  200. void *page_table, *vaddr, *paddr;
  201. static rt_bool_t inited = RT_FALSE;
  202. if (inited)
  203. {
  204. return;
  205. }
  206. inited = RT_TRUE;
  207. page_table = rt_kernel_space.page_table;
  208. for (int hartid = 0; hartid < RT_CPUS_NR; ++hartid)
  209. {
  210. vaddr = &__percpu_start;
  211. paddr = vaddr + rt_kmem_pvoff();
  212. size = (size_t)((rt_ubase_t)&__percpu_end - (rt_ubase_t)&__percpu_start);
  213. /* Offset to per-CPU partition for current CPU */
  214. paddr += size * hartid;
  215. while (size > 0)
  216. {
  217. MM_PGTBL_LOCK(&rt_kernel_space);
  218. mapped = _map_percpu_area(page_table, vaddr, paddr, hartid);
  219. MM_PGTBL_UNLOCK(&rt_kernel_space);
  220. RT_ASSERT(mapped > 0);
  221. size -= mapped;
  222. vaddr += mapped;
  223. paddr += mapped;
  224. }
  225. page_table += ARCH_PAGE_SIZE;
  226. }
  227. }
  228. #endif /* RT_USING_SMP && RT_USING_SMART */
  229. /**
  230. * @brief Maps a virtual address space to a physical address space.
  231. *
  232. * This function maps a specified range of virtual addresses to a range of physical addresses
  233. * and sets the attributes of the page table entries (PTEs). If an error occurs during the
  234. * mapping process, the function will automatically roll back any partially completed mappings.
  235. *
  236. * @param aspace Pointer to the address space structure containing the page table information.
  237. * @param v_addr The starting virtual address to be mapped.
  238. * @param p_addr The starting physical address to be mapped.
  239. * @param size The size of the memory to be mapped (in bytes).
  240. * @param attr The attributes of the page table entries (e.g., read/write permissions, cache policies).
  241. *
  242. * @return On success, returns the starting virtual address `v_addr`;
  243. * On failure, returns `NULL`.
  244. *
  245. * @note This function will not override existing page table entries.
  246. * @warning The caller must ensure that `v_addr` and `p_addr` are page-aligned,
  247. * and `size` is a multiple of the page size.
  248. *
  249. */
  250. void *rt_hw_mmu_map(struct rt_aspace *aspace, void *v_addr, void *p_addr,
  251. size_t size, size_t attr)
  252. {
  253. int ret = -1;
  254. void *unmap_va = v_addr;
  255. size_t npages = size >> ARCH_PAGE_SHIFT;
  256. #if defined(RT_USING_SMP) && defined(ARCH_MM_MMU)
  257. // Map the memory of the .percpu section separately for each core.
  258. rt_hw_percpu_mmu_init_check();
  259. #endif
  260. /* TODO trying with HUGEPAGE here */
  261. while (npages--)
  262. {
  263. #if defined(RT_USING_SMP) && defined(ARCH_MM_MMU)
  264. // skip mapping .percpu section pages
  265. if (v_addr < (void *)&__percpu_start ||
  266. v_addr >= (void *)&__percpu_end)
  267. #endif
  268. {
  269. MM_PGTBL_LOCK(aspace);
  270. ret = _map_one_page(aspace, v_addr, p_addr, attr);
  271. MM_PGTBL_UNLOCK(aspace);
  272. if (ret != 0)
  273. {
  274. /* error, undo map */
  275. while (unmap_va != v_addr)
  276. {
  277. MM_PGTBL_LOCK(aspace);
  278. _unmap_area(aspace, unmap_va);
  279. MM_PGTBL_UNLOCK(aspace);
  280. unmap_va += ARCH_PAGE_SIZE;
  281. }
  282. break;
  283. }
  284. }
  285. v_addr += ARCH_PAGE_SIZE;
  286. p_addr += ARCH_PAGE_SIZE;
  287. }
  288. if (ret == 0)
  289. {
  290. return unmap_va;
  291. }
  292. return NULL;
  293. }
  294. #ifdef ARCH_MM_MMU
  295. void set_free_page(void *page_array)
  296. {
  297. __init_page_array = page_array;
  298. }
  299. // Early-stage page allocator
  300. unsigned long get_free_page(void)
  301. {
  302. static rt_atomic_t page_off = 0;
  303. rt_atomic_t old_off = rt_hw_atomic_add(&page_off, 1);
  304. if (old_off < ARCH_PAGE_SIZE / sizeof(unsigned long))
  305. {
  306. return (unsigned long)(__init_page_array[old_off].page);
  307. }
  308. return 0;
  309. }
  310. #ifdef RT_USING_SMP
  311. // Perform early mapping for the .percpu section
  312. static int rt_hw_mmu_map_percpu_early(rt_ubase_t *tbl, rt_ubase_t va, rt_ubase_t pa)
  313. {
  314. unsigned long page;
  315. rt_ubase_t off, level_shift;
  316. level_shift = PPN2_SHIFT;
  317. // page size 2MB
  318. off = (va >> level_shift) & VPN_MASK;
  319. // Step into the next level page table
  320. tbl = (rt_ubase_t *)((tbl[off] >> PTE_BITS) << ARCH_PAGE_SHIFT);
  321. level_shift -= VPN_BITS;
  322. off = (va >> level_shift) & VPN_MASK;
  323. tbl[off] = ((pa >> ARCH_PAGE_SHIFT) << PTE_BITS) | MMU_MAP_K_RWCB;
  324. asm volatile("sfence.vma x0, x0");
  325. return 0;
  326. }
  327. #endif /* RT_USING_SMP */
  328. static int rt_hw_mmu_map_early(rt_ubase_t *tbl, rt_ubase_t va, rt_ubase_t pa,
  329. rt_ubase_t attr)
  330. {
  331. unsigned long page, *table;
  332. rt_ubase_t off, level_shift;
  333. if ((va & (L2_PAGE_SIZE - 1)) || (pa & (L2_PAGE_SIZE - 1)))
  334. {
  335. return MMU_MAP_ERROR_VANOTALIGN;
  336. }
  337. table = tbl;
  338. level_shift = PPN2_SHIFT;
  339. // page size 2MB
  340. for (int level = 0; level < 1; ++level)
  341. {
  342. off = (va >> level_shift) & VPN_MASK;
  343. if (!(table[off] & PTE_V))
  344. {
  345. if (!(page = get_free_page()))
  346. {
  347. return MMU_MAP_ERROR_NOPAGE;
  348. }
  349. rt_memset((void *)page, 0, ARCH_PAGE_SIZE);
  350. table[off] = ((page >> ARCH_PAGE_SHIFT) << PTE_PPN_SHIFT) | PTE_V;
  351. }
  352. if ((table[off] & PTE_ATTR_RWX) != 0)
  353. {
  354. /* No a page! */
  355. return MMU_MAP_ERROR_CONFLICT;
  356. }
  357. /* Step into the next level page table */
  358. page = (table[off] >> PTE_BITS) << ARCH_PAGE_SHIFT;
  359. table = (unsigned long *)page;
  360. level_shift -= VPN_BITS;
  361. }
  362. off = (va >> level_shift) & VPN_MASK;
  363. table[off] = ((pa >> ARCH_PAGE_SHIFT) << PTE_BITS) | attr;
  364. return 0;
  365. }
  366. #endif
  367. /* unmap page table entry */
  368. static void _unmap_pte(rt_ubase_t *pentry, rt_ubase_t *lvl_entry[], int level)
  369. {
  370. int loop_flag = 1;
  371. while (loop_flag)
  372. {
  373. loop_flag = 0;
  374. *pentry = 0;
  375. rt_hw_cpu_dcache_clean(pentry, sizeof(*pentry));
  376. /* we don't handle level 0, which is maintained by caller */
  377. if (level > 0)
  378. {
  379. void *page = (void *)((rt_ubase_t)pentry & ~ARCH_PAGE_MASK);
  380. /* decrease reference from child page to parent */
  381. rt_pages_free(page, 0);
  382. int free = rt_page_ref_get(page, 0);
  383. if (free == 1)
  384. {
  385. rt_pages_free(page, 0);
  386. pentry = lvl_entry[--level];
  387. loop_flag = 1;
  388. }
  389. }
  390. }
  391. }
  392. /* Unmaps a virtual address range (1GB/2MB/4KB according to actual page level) from the page table. */
  393. static size_t _unmap_area(struct rt_aspace *aspace, void *v_addr)
  394. {
  395. rt_ubase_t loop_va = __UMASKVALUE((rt_ubase_t)v_addr, PAGE_OFFSET_MASK);
  396. size_t unmapped = 0;
  397. int i = 0;
  398. rt_ubase_t lvl_off[3];
  399. rt_ubase_t *lvl_entry[3];
  400. lvl_off[0] = (rt_ubase_t)GET_L1(loop_va);
  401. lvl_off[1] = (rt_ubase_t)GET_L2(loop_va);
  402. lvl_off[2] = (rt_ubase_t)GET_L3(loop_va);
  403. unmapped = 1 << (ARCH_PAGE_SHIFT + ARCH_INDEX_WIDTH * 2ul);
  404. rt_ubase_t *pentry;
  405. lvl_entry[i] = ((rt_ubase_t *)aspace->page_table + lvl_off[i]);
  406. pentry = lvl_entry[i];
  407. /* check if lvl_entry[0] is valid. if no, return 0 directly. */
  408. if (!PTE_USED(*pentry))
  409. {
  410. return 0;
  411. }
  412. /* find leaf page table entry */
  413. while (PTE_USED(*pentry) && !PAGE_IS_LEAF(*pentry))
  414. {
  415. i += 1;
  416. if (i >= 3)
  417. {
  418. unmapped = 0;
  419. break;
  420. }
  421. lvl_entry[i] = ((rt_ubase_t *)PPN_TO_VPN(GET_PADDR(*pentry), PV_OFFSET) +
  422. lvl_off[i]);
  423. pentry = lvl_entry[i];
  424. unmapped >>= ARCH_INDEX_WIDTH;
  425. }
  426. /* clear PTE & setup its */
  427. if (PTE_USED(*pentry))
  428. {
  429. _unmap_pte(pentry, lvl_entry, i);
  430. }
  431. else
  432. {
  433. unmapped = 0; /* invalid pte, return 0. */
  434. }
  435. return unmapped;
  436. }
  437. /**
  438. * @brief Unmaps a range of virtual memory addresses from the specified address space.
  439. *
  440. * This function is responsible for unmapping a contiguous region of virtual memory
  441. * from the given address space. It handles multiple pages and ensures thread safety
  442. * by locking the page table during the unmapping operation.
  443. *
  444. * @param aspace Pointer to the address space structure from which the memory will be unmapped.
  445. * @param v_addr Starting virtual address to unmap. Must be page-aligned.
  446. * @param size Size of the memory region to unmap. Must be page-aligned.
  447. *
  448. * @note The caller must ensure that both `v_addr` and `size` are page-aligned.
  449. *
  450. * @details The function operates in a loop, unmapping memory in chunks. It uses the
  451. * `_unmap_area` function to perform the actual unmapping, which is called within a
  452. * locked section to ensure thread safety. The loop continues until the entire region
  453. * is unmapped.
  454. *
  455. * @see _unmap_area
  456. * @note unmap is different from map that it can handle multiple pages
  457. */
  458. void rt_hw_mmu_unmap(struct rt_aspace *aspace, void *v_addr, size_t size)
  459. {
  460. /* caller guarantee that v_addr & size are page aligned */
  461. if (!aspace->page_table)
  462. {
  463. return;
  464. }
  465. size_t unmapped = 0;
  466. while (size > 0)
  467. {
  468. MM_PGTBL_LOCK(aspace);
  469. unmapped = _unmap_area(aspace, v_addr);
  470. MM_PGTBL_UNLOCK(aspace);
  471. /* when unmapped == 0, region not exist in pgtbl */
  472. if (!unmapped || unmapped > size) break;
  473. size -= unmapped;
  474. v_addr += unmapped;
  475. }
  476. }
  477. #ifdef RT_USING_SMART
  478. static inline void _init_region(void *vaddr, size_t size)
  479. {
  480. rt_ioremap_start = vaddr;
  481. rt_ioremap_size = size;
  482. rt_mpr_start = rt_ioremap_start - rt_mpr_size;
  483. LOG_D("rt_ioremap_start: %p, rt_mpr_start: %p", rt_ioremap_start,
  484. rt_mpr_start);
  485. }
  486. #else
  487. static inline void _init_region(void *vaddr, size_t size)
  488. {
  489. rt_mpr_start = vaddr - rt_mpr_size;
  490. }
  491. #endif
  492. #if defined(RT_USING_SMART) && defined(ARCH_REMAP_KERNEL)
  493. #define KERN_SPACE_START ((void *)KERNEL_VADDR_START)
  494. #define KERN_SPACE_SIZE (0xfffffffffffff000UL - KERNEL_VADDR_START + 0x1000)
  495. #else
  496. #define KERN_SPACE_START ((void *)0x1000)
  497. #define KERN_SPACE_SIZE ((size_t)USER_VADDR_START - 0x1000)
  498. #endif
  499. /**
  500. * @brief Initialize the MMU (Memory Management Unit) mapping.
  501. *
  502. * This function initializes the MMU mapping, incluing these steps as follows:
  503. * 1. Check the validity of the input parameters,
  504. * 2. Calculate the start and end virtual addresses based on the input virtual address and size.
  505. * 3. Convert the virtual addresses to PPN2 indices.
  506. * 4. Check the initialization of the page table. If any entry in the page table within
  507. * the specified range is non-zero, it returns -1.
  508. * 5. It initializes the kernel address space using rt_aspace_init() and initializes the specified region
  509. * using _init_region.
  510. *
  511. * @param aspace Pointer to the address space. Must not be NULL.
  512. * @param v_address The starting virtual address.
  513. * @param size The size of the virtual address space.
  514. * @param vtable Pointer to the page table. Must not be NULL.
  515. * @param pv_off The page table offset.
  516. *
  517. * @return Returns 0 if the initialization is successful. Returns -1 if any input parameter is invalid
  518. * or the page table initialization check fails.
  519. */
  520. int rt_hw_mmu_map_init(rt_aspace_t aspace, void *v_address, rt_ubase_t size,
  521. rt_ubase_t *vtable, rt_ubase_t pv_off)
  522. {
  523. size_t l1_off, va_s, va_e;
  524. if ((!aspace) || (!vtable))
  525. {
  526. return -1;
  527. }
  528. va_s = (rt_ubase_t)v_address;
  529. va_e = ((rt_ubase_t)v_address) + size - 1;
  530. if (va_e < va_s)
  531. {
  532. return -1;
  533. }
  534. /* convert address to PPN2 index */
  535. va_s = GET_L1(va_s);
  536. va_e = GET_L1(va_e);
  537. if (va_s == 0)
  538. {
  539. return -1;
  540. }
  541. /* vtable initialization check */
  542. for (l1_off = va_s; l1_off <= va_e; l1_off++)
  543. {
  544. size_t v = vtable[l1_off];
  545. if (v)
  546. {
  547. return -1;
  548. }
  549. }
  550. rt_aspace_init(&rt_kernel_space, KERN_SPACE_START, KERN_SPACE_SIZE, vtable);
  551. _init_region(v_address, size);
  552. return 0;
  553. }
  554. const static int max_level =
  555. (ARCH_VADDR_WIDTH - ARCH_PAGE_SHIFT) / ARCH_INDEX_WIDTH;
  556. static inline uintptr_t _get_level_size(int level)
  557. {
  558. return 1ul << (ARCH_PAGE_SHIFT + (max_level - level) * ARCH_INDEX_WIDTH);
  559. }
  560. static rt_ubase_t *_query(struct rt_aspace *aspace, void *vaddr, int *level)
  561. {
  562. rt_ubase_t l1_off, l2_off, l3_off;
  563. rt_ubase_t *mmu_l1, *mmu_l2, *mmu_l3;
  564. rt_ubase_t pa;
  565. l1_off = GET_L1((rt_uintptr_t)vaddr);
  566. l2_off = GET_L2((rt_uintptr_t)vaddr);
  567. l3_off = GET_L3((rt_uintptr_t)vaddr);
  568. if (!aspace)
  569. {
  570. LOG_W("%s: no aspace", __func__);
  571. return RT_NULL;
  572. }
  573. mmu_l1 = ((rt_ubase_t *)((rt_ubase_t)aspace->page_table + (rt_ubase_t)(rt_hw_cpu_id() * ARCH_PAGE_SIZE))) + l1_off;
  574. if (PTE_USED(*mmu_l1))
  575. {
  576. if (*mmu_l1 & PTE_XWR_MASK)
  577. {
  578. *level = 1;
  579. return mmu_l1;
  580. }
  581. mmu_l2 = (rt_ubase_t *)PPN_TO_VPN(GET_PADDR(*mmu_l1), PV_OFFSET);
  582. if (PTE_USED(*(mmu_l2 + l2_off)))
  583. {
  584. if (*(mmu_l2 + l2_off) & PTE_XWR_MASK)
  585. {
  586. *level = 2;
  587. return mmu_l2 + l2_off;
  588. }
  589. mmu_l3 = (rt_ubase_t *)PPN_TO_VPN(GET_PADDR(*(mmu_l2 + l2_off)),
  590. PV_OFFSET);
  591. if (PTE_USED(*(mmu_l3 + l3_off)))
  592. {
  593. *level = 3;
  594. return mmu_l3 + l3_off;
  595. }
  596. }
  597. }
  598. return RT_NULL;
  599. }
  600. /**
  601. * @brief Translate a virtual address to a physical address.
  602. *
  603. * This function translates a given virtual address (`vaddr`) to its corresponding
  604. * physical address (`paddr`) using the page table in the specified address space (`aspace`).
  605. *
  606. * @param aspace Pointer to the address space structure containing the page table.
  607. * @param vaddr The virtual address to be translated.
  608. *
  609. * @return The translated physical address. If the translation fails, `ARCH_MAP_FAILED` is returned.
  610. *
  611. * @note The function queries the page table entry (PTE) for the virtual address using `_query`.
  612. * If a valid PTE is found, the physical address is extracted and combined with the offset
  613. * from the virtual address. If no valid PTE is found, a debug log is recorded, and
  614. * `ARCH_MAP_FAILED` is returned.
  615. */
  616. void *rt_hw_mmu_v2p(struct rt_aspace *aspace, void *vaddr)
  617. {
  618. int level;
  619. rt_ubase_t *pte = _query(aspace, vaddr, &level);
  620. uintptr_t paddr;
  621. if (pte)
  622. {
  623. paddr = GET_PADDR(*pte);
  624. paddr |= ((intptr_t)vaddr & (_get_level_size(level) - 1));
  625. }
  626. else
  627. {
  628. LOG_D("%s: failed at %p", __func__, vaddr);
  629. paddr = (uintptr_t)ARCH_MAP_FAILED;
  630. }
  631. return (void *)paddr;
  632. }
  633. static int _noncache(rt_ubase_t *pte)
  634. {
  635. return 0;
  636. }
  637. static int _cache(rt_ubase_t *pte)
  638. {
  639. return 0;
  640. }
  641. static int (*control_handler[MMU_CNTL_DUMMY_END])(rt_ubase_t *pte)=
  642. {
  643. [MMU_CNTL_CACHE] = _cache,
  644. [MMU_CNTL_NONCACHE] = _noncache,
  645. };
  646. /**
  647. * @brief Control the page table entries (PTEs) for a specified virtual address range.
  648. *
  649. * This function applies a control command (e.g., cache control) to the page table entries
  650. * (PTEs) corresponding to the specified virtual address range (`vaddr` to `vaddr + size`).
  651. *
  652. * @param aspace Pointer to the address space structure containing the page table.
  653. * @param vaddr The starting virtual address of the range.
  654. * @param size The size of the virtual address range.
  655. * @param cmd The control command to apply (e.g., `MMU_CNTL_CACHE`, `MMU_CNTL_NONCACHE`.etc.).
  656. *
  657. * @return `RT_EOK` on success, or an error code (`-RT_EINVAL` or `-RT_ENOSYS`) on failure.
  658. *
  659. * @note The function uses the `control_handler` array to map the command to a handler function.
  660. * It iterates over the virtual address range, queries the PTEs, and applies the handler
  661. * to each valid PTE. If the command is invalid, `-RT_ENOSYS` is returned.
  662. */
  663. int rt_hw_mmu_control(struct rt_aspace *aspace, void *vaddr, size_t size,
  664. enum rt_mmu_cntl cmd)
  665. {
  666. int level;
  667. int err = -RT_EINVAL;
  668. void *vend = vaddr + size;
  669. int (*handler)(rt_ubase_t *pte);
  670. if (cmd >= 0 && cmd < MMU_CNTL_DUMMY_END)
  671. {
  672. handler = control_handler[cmd];
  673. while (vaddr < vend)
  674. {
  675. rt_ubase_t *pte = _query(aspace, vaddr, &level);
  676. void *range_end = vaddr + _get_level_size(level);
  677. RT_ASSERT(range_end <= vend);
  678. if (pte)
  679. {
  680. err = handler(pte);
  681. RT_ASSERT(err == RT_EOK);
  682. }
  683. vaddr = range_end;
  684. }
  685. }
  686. else
  687. {
  688. err = -RT_ENOSYS;
  689. }
  690. return err;
  691. }
  692. /**
  693. * @brief setup Page Table for kernel space. It's a fixed map
  694. * and all mappings cannot be changed after initialization.
  695. *
  696. * Memory region in struct mem_desc must be page aligned,
  697. * otherwise is a failure and no report will be
  698. * returned.
  699. *
  700. * @param aspace Pointer to the address space structure.
  701. * @param mdesc Pointer to the array of memory descriptors.
  702. * @param desc_nr Number of memory descriptors in the array.
  703. */
  704. void rt_hw_mmu_setup(rt_aspace_t aspace, struct mem_desc *mdesc, int desc_nr)
  705. {
  706. void *err;
  707. for (size_t i = 0; i < desc_nr; i++)
  708. {
  709. size_t attr;
  710. switch (mdesc->attr)
  711. {
  712. case NORMAL_MEM:
  713. attr = MMU_MAP_K_RWCB;
  714. break;
  715. case NORMAL_NOCACHE_MEM:
  716. attr = MMU_MAP_K_RWCB;
  717. break;
  718. case DEVICE_MEM:
  719. attr = MMU_MAP_K_DEVICE;
  720. break;
  721. default:
  722. attr = MMU_MAP_K_DEVICE;
  723. }
  724. struct rt_mm_va_hint hint = {
  725. .flags = MMF_MAP_FIXED,
  726. .limit_start = aspace->start,
  727. .limit_range_size = aspace->size,
  728. .map_size = mdesc->vaddr_end - mdesc->vaddr_start + 1,
  729. .prefer = (void *)mdesc->vaddr_start};
  730. if (mdesc->paddr_start == (rt_uintptr_t)ARCH_MAP_FAILED)
  731. mdesc->paddr_start = mdesc->vaddr_start + PV_OFFSET;
  732. rt_aspace_map_phy_static(aspace, &mdesc->varea, &hint, attr,
  733. mdesc->paddr_start >> MM_PAGE_SHIFT, &err);
  734. mdesc++;
  735. }
  736. rt_hw_asid_init();
  737. rt_hw_aspace_switch(&rt_kernel_space);
  738. rt_page_cleanup();
  739. }
  740. #define SATP_BASE ((rt_ubase_t)SATP_MODE << SATP_MODE_OFFSET)
  741. /**
  742. * @brief Early memory setup function for hardware initialization.
  743. *
  744. * This function performs early memory setup tasks, including:
  745. * - Calculating the physical-to-virtual (PV) offset.
  746. * - Setting up initial page tables for identity mapping and text region relocation.
  747. * - Applying new memory mappings by updating the SATP register.
  748. *
  749. * @note This function is typically called during the early stages of system initialization (startup_gcc.S),
  750. * before the memory management system is fully operational.
  751. * Here the identity mapping is implemented by a 1-stage page table, whose page size is 1GB.
  752. */
  753. void rt_hw_mem_setup_early(void *pgtbl, rt_uint64_t hartid)
  754. {
  755. rt_ubase_t pv_off, size;
  756. rt_ubase_t ps = 0x0;
  757. rt_ubase_t vs = 0x0;
  758. rt_ubase_t *early_pgtbl = (rt_ubase_t *)(pgtbl + hartid * ARCH_PAGE_SIZE);
  759. /* calculate pv_offset */
  760. void *symb_pc;
  761. void *symb_linker;
  762. __asm__ volatile("la %0, _start\n" : "=r"(symb_pc));
  763. __asm__ volatile("la %0, _start_link_addr\n" : "=r"(symb_linker));
  764. symb_linker = *(void **)symb_linker;
  765. pv_off = symb_pc - symb_linker;
  766. rt_kmem_pvoff_set(pv_off);
  767. if (pv_off)
  768. {
  769. if (pv_off & ((1ul << (ARCH_INDEX_WIDTH * 2 + ARCH_PAGE_SHIFT)) - 1))
  770. {
  771. LOG_E("%s: not aligned virtual address. pv_offset %p", __func__,
  772. pv_off);
  773. RT_ASSERT(0);
  774. }
  775. /**
  776. * identical mapping,
  777. * PC are still at lower region before relocating to high memory
  778. */
  779. rt_ubase_t pg_idx ;
  780. /* Round down symb_pc to L1_PAGE_SIZE boundary to ensure proper page alignment.
  781. * This is necessary because MMU operations work with page-aligned addresses, and
  782. * make sure all the text region is mapped.*/
  783. ps = (rt_ubase_t)symb_pc & (~(L1_PAGE_SIZE - 1));
  784. pg_idx = GET_L1(ps);
  785. early_pgtbl[pg_idx] = COMBINEPTE(ps, MMU_MAP_EARLY);
  786. /* relocate text region */
  787. __asm__ volatile("la %0, _start\n" : "=r"(ps));
  788. ps &= ~(L1_PAGE_SIZE - 1);
  789. vs = ps - pv_off;
  790. /* relocate region */
  791. rt_ubase_t ve = vs + 0x80000000;
  792. #if defined(RT_USING_SMP) && defined(ARCH_MM_MMU)
  793. while (vs < ve)
  794. {
  795. rt_hw_mmu_map_early(early_pgtbl, vs, ps, MMU_MAP_EARLY);
  796. vs += L2_PAGE_SIZE;
  797. ps += L2_PAGE_SIZE;
  798. }
  799. #else
  800. for (int i = GET_L1(vs); i < GET_L1(ve); i++)
  801. {
  802. early_pgtbl[i] = COMBINEPTE(ps, MMU_MAP_EARLY);
  803. ps += L1_PAGE_SIZE;
  804. }
  805. #endif
  806. #if defined(RT_USING_SMP) && defined(ARCH_MM_MMU)
  807. // map .percpu section
  808. ps = (rt_ubase_t)&__percpu_start;
  809. vs = ps - rt_kmem_pvoff();
  810. size = (rt_size_t)((rt_ubase_t)&__percpu_end - (rt_ubase_t)&__percpu_start);
  811. /* Offset to per-CPU partition for current CPU */
  812. ps += hartid * size;
  813. ve = vs + size;
  814. while (vs < ve)
  815. {
  816. /* Map physical address per-CPU partition */
  817. rt_hw_mmu_map_percpu_early(early_pgtbl, vs, ps);
  818. ps += L2_PAGE_SIZE;
  819. vs += L2_PAGE_SIZE;
  820. }
  821. #endif
  822. /* apply new mapping */
  823. asm volatile("sfence.vma x0, x0");
  824. write_csr(satp, SATP_BASE | ((size_t)early_pgtbl >> PAGE_OFFSET_BIT));
  825. asm volatile("sfence.vma x0, x0");
  826. }
  827. /* return to lower text section */
  828. }
  829. /**
  830. * @brief Creates and initializes a new MMU page table.
  831. *
  832. * This function allocates a new MMU page table, copies the kernel space
  833. * page table into it, and flushes the data cache to ensure consistency.
  834. *
  835. * @return
  836. * - A pointer to the newly allocated MMU page table on success.
  837. * - RT_NULL if the allocation fails.
  838. */
  839. void *rt_hw_mmu_pgtbl_create(void)
  840. {
  841. rt_ubase_t *mmu_table;
  842. mmu_table = (rt_ubase_t *)rt_pages_alloc_ext(0, PAGE_ANY_AVAILABLE);
  843. if (!mmu_table)
  844. {
  845. return RT_NULL;
  846. }
  847. rt_memcpy(mmu_table, rt_kernel_space.page_table, ARCH_PAGE_SIZE);
  848. rt_hw_cpu_dcache_ops(RT_HW_CACHE_FLUSH, mmu_table, ARCH_PAGE_SIZE);
  849. return mmu_table;
  850. }
  851. /**
  852. * @brief Deletes an MMU page table.
  853. *
  854. * This function frees the memory allocated for the given MMU page table.
  855. *
  856. * @param pgtbl Pointer to the MMU page table to be deleted.
  857. */
  858. void rt_hw_mmu_pgtbl_delete(void *pgtbl)
  859. {
  860. rt_pages_free(pgtbl, 0);
  861. }