memory-managementpagingriscvforthriscv32

Executing dynamically generated code on RISC-V


I am writing a Forth-like language for RISC-V and naturally that means I need to have a way to allow the users to define new words and execute them.

But I am struggling to find a way that will allow users to execute dynamically generated code without using privileged instructions to change a page's status. Is there any way to do this - for example, can I define a large empty section in .text (as opposed to .bss which is what I am currently doing)?

Or do I have to write an explicit trap handler?

Update: I am using the pk proxy kernel with Spike. I am executing an mprotect system call against either a buffer created in the .bss section or an allocation in the .text section.

In either case the mprotect call fails - returning -EACCES. (So using the .bss version fails on any attempt to execute the new code and using the .text version fails when I attempt to write the new code.)

It seems I cannot mark a page as R/W/X - and this might be fundamental. Is there a way round this (there surely must be!)


Solution

  • The answer to this problem was to use mmap to map in a range of addresses with the appropriate READ, WRITE and EXECUTE permissions.

    mv a0, zero
    li a1, BIGSPACE
    li a2, 0x07
    li a3, 0x22
    li a4, -1
    li a5, 0x0
    li a7, 222
    ecall