oscourse-tsinghua / rcore_plus

Rust version of THU uCore OS. Linux compatible.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Port to K210 again

wangrunji0408 opened this issue · comments

I was surprised to learn that K210 has S-Mode and Sv39 MMU!
So it's possible to run rCore on it!

But K210 is based on priv v1.9.1 spec, which has some differences compared to current v1.10.
For example: sptbr in v1.9.1, satp in v1.10.

Proof: MAIX MMU example: run 32MB GBA on FATFS!

I started porting today. Log the problems here.

  1. In priv v1.9.1, mstatus.VM controls the paging mode, but it can't be accessed in S-Mode.
    So we have to setup a page table and enable paging before entering S-Mode.
    Thus OpenSBI needs to be modified.
  2. In priv v1.9.1, it uses sfence.vm instead of sfence.vma, and their coding are different.
    So we can't use sfence.vma in asm, and have to put the raw code of sfence.vm.
    Thus riscv crate needs to be modified.
    Another solution: emulate instructions in OpenSBI.
  3. In priv v1.9.1, we only have mbadaddr instead of mtval, which means we can't get the instruction code when handling illegal instruction exception.
    So the current instruction emulation in OpenSBI is broken, then rdtime will cause an exception.
    We have to modify OpenSBI: load instruction code from memory according to mepc.
  4. Fixed a memory issue: avoid allocating kernel stacks for init threads.
  5. In priv v1.9.1, sstatus.SUM is still PUM which has opposite meaning!
    So we will failed to access user memory.
    Don't set SUM bit!
  6. The default payload location is a page after the firmware (0x8000b000). But I noticed that some data at 0x8000bdxx was overwritten before entering S-Mode. I didn't figure out why. So I moved the payload to 0x80010000 and everything went fine.

Now rCore is working on K210! @ 2019.04.21
But the RAM is too small (6M) to run Busybox (~1M).

TODO in the future:

  • enable hart1
  • enable serial interrupt
  • SPI flash and SD card driver

In a new development enviroment, many errors will be encountered when compiling the old version of rCore for K210.
So fixed the necessary dependencies , then compiled and passed.
See: rcore-os#65
But it does not run on k210 board normally.

commented

@wangrunji0408 did you ever document your findings somewhere? as the K210 made it into the linux kernel (with NOMMU to my knowledge) i am wondering if an actual MMU Linux Kernel for K210 would be possible. Would love to hear your thoughts.

@z3cko All my findings are recorded here. As far as I know, the MMU feature in the K210 is not official documented. We can only assume that K210 has an MMU complies with the RISC-V spec v1.9.1, and it seems to be. So I think an MMU Linux kernel for K210 is possible in technical, but I'm not sure if the Linux community would like to accept that for an undocumented hardware.