One of the first things that must be done when porting the nanokernel to a new architecture is the am_inkernel() macro (or inline function). When called, it should return 1 if the current execution mode is "kernel" (e.g. a supervisor), and 0 if the mode is "user". On different architectures it is implemented differently: on ARM, for example, it's just examining some bit in CPSR, on x86 examining a current code segment, and on others it may boil down to checking the current stack pointer against two variables ker_stack_top and ker_stack_bottom.
I found an interesting discussion in a SiFive forum about such problem and RISC-V. Quote:
RISC-V deliberately doesn’t make it easy for code to discover what mode it is running it because this is a virtualisation hole. As a general principle, code should be designed for and implicitly know what mode it will run in. Applications code should assume it is in U mode. The operating system should assume it is in S mode (it might in fact be virtualised and running in U mode, with things U mode can’t do trapped and emulated by the hypervisor).
Since I'm porting for RV64, I'm thinking about just checking the top bits of the instruction pointer against e.g. 0xFFFF. Let's see how it works.
No comments:
Post a Comment