Implement MMU for mini-rv32ima to boot xv6 or Linux
Background
Paging Concept
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
-
Method:
- Dividing physical memory into fixed-sized blocks is referred to as "frames".
- Dividing the logical address space into equally sized blocks is termed as "pages".
- To execute a program with n pages, it is necessary to locate n free frames and load the program into them.
- The operating system continuously tracks free frames using a page table to establish the mapping between logical and physical addresses.
-
Benefit:
- Allows the physical address space of a process to be non-contiguous.
- Mitigates external fragmentation.
- Facilitates shared memory/pages.
dts & dtb file
dts
dts comes with the following format:
The general structure of dts used in the linux kernel is
dtb
dts compiles into dtb via dtc, compiled drvice tree are stored into the storage with kernel, when kernel is booted, it will read the file, an get hardware information dynamically.
running mini-rv32ima
This section assumes the working directory is in mini-rv32ima, which can be achieved by using
Build mini-rv32ima the first time
In order to let mini-rv32ima run on specialized linux system, we should compile both mini-rv32ima (into executable) and entire linux kernel (into Image file).
This command automates everything.
The original file system is located in buildroot/output/target/
, when using make everything
, files in the folder will also be included into the image.
Execute mini-rv32ima
To run linux in mini-rv32ima, run
How original mini-rv32ima works
Original booting generally consists of the following steps:
- load bios/uefi
- power on self test
- load os bootloader (such as GRUB)
- load system configuration
- user login (finish booting)
mini-rv32ima is an minimalist emulator, there are no bios, and the whole operating system is included in the image.
- load os image
- load system configuration (dtb file)
- user login (finish booting)
Configurations
Spec for Page-Based 32-bit Virtual-Memory Systems (Sv32)
- Sv32 address translation process
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
- page size:
- virtual memory size:
- page table level:
- virtual entries for each layer:
- physical address structure ()
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
- virtual address structure ()
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
- CSR satp(Supervisor Address Translation and Protection Register) structure (number 0x180)
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
PPN field represents start of page table 1
- Sv32 page table entry :
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
- X, W, R represent executable, readable and writable, respectively.
When {X, W, R} = {0, 0, 0}
, it is pointing to next level of page table.
The combination {W, R} = {1, 0}
is reversed for future use.
- The U(User) indicates whether the page is accessible to user mode. U-mode software can only access the page when U=1,supervisor mode software can only access pages with U=0 most of the time.
- The V(Valid) bit indicates whether the PTE is valid,0 means absence in memory.
- The A(Access) bit indicates the virtual page has been read, written, or fetched from since the last time the A bit was cleared.
- The D(Dirty) bit indicates the virtual page has been written since the last time the D bit was cleared.
configuration for 64 mb system
virtual address translation process
- find the start of layer 1 page table from , .
- find layer 1 page table data from virtual address (whose address is ).
- If (address it points to doesn't exist), or (no such combination), raise a page-fault exception corresponding to the original access type.
- If (not pointing to layer 0 page table), check if , if true, go to step 6, otherwise (misaligned page table), raise a page-fault exception corresponding to the original access type.
- find layer 0 page table data from virtual address (whose address is ).
- A leaf PTE has been found. If (address it points to doesn't exist),or memory access isn't allowed for the corresponding ,or privilege mode isn't allowed for the corresponding , raise a page-fault exception corresponding to the original access type.
- If ,or if the memory access is a store and , raise a page-fault exception corresponding to the original access type.
- The translation is successful. The translated physical address has the following attributes:
- .
- If step 5 is skipped, it is a superpage translation, .
- Layer 1 , layer 0 .
mmu implementation
modification
mini-rv32ima.h
satp register is essential for mmu to process, so we need to add it, and let operating system access it.
In order to use virtual address, we need to rewrite the load/store part, the original load/store will only trap when illegal instruction, adding mmu introduces page fault on top of that.
setup:
modification
Instruction fetch:
Load:
Store:
Atomic:
After that, we include above code, and create a header for it.
dts file
We cannot figure out how to modify dts file yet.
Reference
- https://blog.csdn.net/acs713/article/details/70036359
- https://hackmd.io/@Chang-Chia-Chi/rkPuUJVaY
- https://hackmd.io/@jacky5610/riscv_plic
- https://drive.google.com/file/d/1EMip5dZlnypTk7pt4WWUKmtjUKTOkBqh/view
(riscv privileged spec, 4.3, p79)
- Sv39 implementation
- https://dingfen.github.io/risc-v/2020/08/05/riscv-privileged.html
(risv csr)
- The RISC-V Instruction Set Manual (privileged) drive version, github version
For sv32, go to page 79.