cmodx.rst 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. .. SPDX-License-Identifier: GPL-2.0
  2. ==============================================================================
  3. Concurrent Modification and Execution of Instructions (CMODX) for RISC-V Linux
  4. ==============================================================================
  5. CMODX is a programming technique where a program executes instructions that were
  6. modified by the program itself. Instruction storage and the instruction cache
  7. (icache) are not guaranteed to be synchronized on RISC-V hardware. Therefore, the
  8. program must enforce its own synchronization with the unprivileged fence.i
  9. instruction.
  10. However, the default Linux ABI prohibits the use of fence.i in userspace
  11. applications. At any point the scheduler may migrate a task onto a new hart. If
  12. migration occurs after the userspace synchronized the icache and instruction
  13. storage with fence.i, the icache on the new hart will no longer be clean. This
  14. is due to the behavior of fence.i only affecting the hart that it is called on.
  15. Thus, the hart that the task has been migrated to may not have synchronized
  16. instruction storage and icache.
  17. There are two ways to solve this problem: use the riscv_flush_icache() syscall,
  18. or use the ``PR_RISCV_SET_ICACHE_FLUSH_CTX`` prctl() and emit fence.i in
  19. userspace. The syscall performs a one-off icache flushing operation. The prctl
  20. changes the Linux ABI to allow userspace to emit icache flushing operations.
  21. As an aside, "deferred" icache flushes can sometimes be triggered in the kernel.
  22. At the time of writing, this only occurs during the riscv_flush_icache() syscall
  23. and when the kernel uses copy_to_user_page(). These deferred flushes happen only
  24. when the memory map being used by a hart changes. If the prctl() context caused
  25. an icache flush, this deferred icache flush will be skipped as it is redundant.
  26. Therefore, there will be no additional flush when using the riscv_flush_icache()
  27. syscall inside of the prctl() context.
  28. prctl() Interface
  29. ---------------------
  30. Call prctl() with ``PR_RISCV_SET_ICACHE_FLUSH_CTX`` as the first argument. The
  31. remaining arguments will be delegated to the riscv_set_icache_flush_ctx
  32. function detailed below.
  33. .. kernel-doc:: arch/riscv/mm/cacheflush.c
  34. :identifiers: riscv_set_icache_flush_ctx
  35. Example usage:
  36. The following files are meant to be compiled and linked with each other. The
  37. modify_instruction() function replaces an add with 0 with an add with one,
  38. causing the instruction sequence in get_value() to change from returning a zero
  39. to returning a one.
  40. cmodx.c::
  41. #include <stdio.h>
  42. #include <sys/prctl.h>
  43. extern int get_value();
  44. extern void modify_instruction();
  45. int main()
  46. {
  47. int value = get_value();
  48. printf("Value before cmodx: %d\n", value);
  49. // Call prctl before first fence.i is called inside modify_instruction
  50. prctl(PR_RISCV_SET_ICACHE_FLUSH_CTX, PR_RISCV_CTX_SW_FENCEI_ON, PR_RISCV_SCOPE_PER_PROCESS);
  51. modify_instruction();
  52. // Call prctl after final fence.i is called in process
  53. prctl(PR_RISCV_SET_ICACHE_FLUSH_CTX, PR_RISCV_CTX_SW_FENCEI_OFF, PR_RISCV_SCOPE_PER_PROCESS);
  54. value = get_value();
  55. printf("Value after cmodx: %d\n", value);
  56. return 0;
  57. }
  58. cmodx.S::
  59. .option norvc
  60. .text
  61. .global modify_instruction
  62. modify_instruction:
  63. lw a0, new_insn
  64. lui a5,%hi(old_insn)
  65. sw a0,%lo(old_insn)(a5)
  66. fence.i
  67. ret
  68. .section modifiable, "awx"
  69. .global get_value
  70. get_value:
  71. li a0, 0
  72. old_insn:
  73. addi a0, a0, 0
  74. ret
  75. .data
  76. new_insn:
  77. addi a0, a0, 1