Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TLB & PTW - Rocketchip] Research the ptw and tlb microarchitecture and output the ppt. #2

Open
Jerryy959 opened this issue Aug 14, 2024 · 13 comments
Assignees
Labels
documentation Improvements or additions to documentation PTW rocketchip TLB

Comments

@Jerryy959
Copy link
Member

No description provided.

@Jerryy959 Jerryy959 added the bug Something isn't working label Aug 14, 2024
@Jerryy959 Jerryy959 self-assigned this Aug 14, 2024
@Jerryy959 Jerryy959 added rocketchip TLB PTW documentation Improvements or additions to documentation and removed bug Something isn't working labels Aug 14, 2024
@Jerryy959
Copy link
Member Author

Jerryy959 commented Aug 14, 2024

https://carrv.github.io/2020/papers/CARRV2020_paper_11_Papadopoulos.pdf
This paper describes the TLB microarchitecture, valuable to understand.
Especially this figure,
Image

You should combine this figure with rocketchip’s PTW code for understanding and reading.
Link this:
https://github.com/chipsalliance/rocket-chip/blob/master/src/main/scala/rocket/PTW.scala

Image

Page Walker
Page Walker 是一个状态机,根据虚拟地址访问页表,访问规则与 RV 手册中相似,直到:

访问到 2MB 的节点,返回给 Last Level Page Walker,由 Last Level Page Walker 进行最后一级页表的访问
访问叶子节点,为大页(SuperPage),返回给一级 TLB
访问到非法节点,页表项的 v 位为 false 等
Page Walker 访存的结果会存到 Page Cache 中。 Page Walker 同时只能够处理一个请求,并且最多只访问前两级页表,访存能力较弱,原因在 Last Level Page Walker 节说明。

@Jerryy959
Copy link
Member Author

Jerryy959 commented Aug 14, 2024

Check out my keynote:
This will be reported at the next group meeting.

https://github.com/BOSC-Hvisor/Doc/blob/9a14dd1aa66bfa72a46d94fae880f441e2a9f508/Presentation/TLB_and_PTW.key

@Jerryy959
Copy link
Member Author

TLB的工作原理:
https://pages.cs.wisc.edu/~remzi/OSTEP/Chinese/19.pdf

@Jerryy959
Copy link
Member Author

虚拟存储器的讲解:
https://www.cnblogs.com/lyc-seu/p/16995924.html

@Jerryy959
Copy link
Member Author

Jerryy959 commented Aug 20, 2024

@Jerryy959
Copy link
Member Author

@Jerryy959
Copy link
Member Author

当前的配置是这样的
new WithNBigCores(4) ++
new WithNExtTopInterrupts(6)
...

我需要弄懂每一个配置的作用,
然后测试删除某个配置,
然后启动原本的OS,
然后再尝试加上H扩展。

@Jerryy959
Copy link
Member Author

@Jerryy959
Copy link
Member Author

页表只能放入内存中,因为页表占用的空间太大了。
TLB是页表缓存。
TLB访问和Cache访问同时进行,TLB可视为页表的Cache。

@Jerryy959
Copy link
Member Author

嵌入式系统中的用户模式和进程隔离

1. 特权模式切换

Riscv中,M模式可以直接访问所有的硬件资源,为了更好的保护系统代码安全,必须禁止不可信代码执行特权指令(如mret)和访问特权CSR(如mstatus)。

U模式为此功能服务。当CPU处于U模式,将拒绝执行M模式指令或访问M模式CSR。

M切换U的方法:将mstatus.MPP设为U,然后执行mret。当U模式出现异常,则控制权交会M模式。

2. 限制不可信代码只能访问各自的内存 —— PMP (Physical Memory Protection, PMP)

该功能允许M模式指定哪些内存地址可让U模式访问。

方法是:

PMP包含若干地址寄存器(8-16个)和相应的配置寄存器,后者用于配置读写执行的权限。

U模式的CPU尝试取指访存时,该地址X会与PMP寄存器比较。

  1. PMP_i < X < PMP_(i+1) => PMP_(i+1) 决策是否允许访问,若否,则抛出异常。
  2. PMP粒度为4字节。

@Jerryy959
Copy link
Member Author

Jerryy959 commented Aug 21, 2024

Rocketchip中的PTW:

PTW(Page Table Walker)是一个处理页表查询的状态机,用于高级TLB(Translation Lookaside Buffer)的页面表查询。这个状态机在TLB缓存未命中时,通过查询内存中的页表,找到所需的页表条目(PTE),并将其缓存到L2TLB或PTE缓存中。以下是对PTW状态机的详细分析:

PTW 的状态机概述:

1.	s_ready:
         准备好接收来自TLB的请求。
         在这个状态下,如果TLB发出请求,并且没有L2TLB的回填操作,PTW将进入下一个状态。
2.	s_req:
         PTW接收到来自TLB的请求,判断是否命中PTE缓存。
         如果命中,处理该PTE,并继续处理;否则,发出内存请求以查询页表。
3.	s_wait1:
         处理L2TLB错误,或者等待内存响应。
         如果在L2TLB中找到错误或者需要等待内存响应,PTW进入这个状态。
4.	s_wait2:
         最后一次检查是否命中PTE缓存。
         在这个状态下,PTW进行最后的PTE缓存命中判断,如果命中,则可以直接使用该PTE。
5.	s_wait3:
         接收内存响应,处理查询结果。
         如果PTW从内存中得到了所需的PTE,会进入这个状态来处理响应数据。
6.	s_fragment_superpage:
         处理超级页面(superpage)的PTE片段。
         当PTE指向一个超级页面(通常是更大的页面大小),需要特殊处理以确保地址映射的正确性。

状态机中的关键事件:

•	PTE Cache Hit (pte_cache_hit):
•	在s_req或s_wait1状态时,如果命中PTE缓存(不论是一级还是二级),PTW可以快速找到所需的PTE,而不需要继续查询内存。
•	L2TLB Hit (l2_hit):
•	L2TLB命中发生在s_req状态或s_wait1状态,这意味着PTW可以直接使用L2TLB中的条目,而不需要继续查询内存。
•	Memory Access (mem access):
•	如果PTE缓存未命中,PTW将在s_req状态时发出内存请求以查询页表,并在s_wait3状态时处理内存响应。
•	Two-Stage Translation:
•	如果启用了两级地址转换(如Hypervisor模式),PTW在s_fragment_superpage状态时需要处理多层次的PTE,以确保正确的物理地址映射。

PTW中的状态转换:

1.	从s_ready状态开始,接收到TLB的请求后进入s_req状态。
2.	在s_req状态判断是否命中PTE缓存,如果命中则跳到s_wait2状态,否则发出内存请求,进入s_wait1或s_wait3状态。
3.	在s_wait1状态处理错误或者等待响应,然后进入s_wait2或s_wait3状态。
4.	在s_wait3状态接收并处理内存响应,最终返回到s_ready状态以准备处理下一个请求。

总结

PTW状态机在处理TLB缓存未命中的情况下,通过一系列状态转换完成页表查询,并缓存结果以加速后续访问。其主要目标是通过L2TLB和PTE缓存的命中率,减少对内存的访问,提升虚拟内存系统的性能。

@Jerryy959 Jerryy959 changed the title [RocketChip] Research the ptw and tlb microarchitecture and output the ppt. [TLB & PTW - Rocketchip] Research the ptw and tlb microarchitecture and output the ppt. Aug 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation PTW rocketchip TLB
Projects
Status: In progress
Development

No branches or pull requests

1 participant