sys_riscv.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2012 Regents of the University of California
  4. * Copyright (C) 2014 Darius Rad <[email protected]>
  5. * Copyright (C) 2017 SiFive
  6. */
  7. #include <linux/syscalls.h>
  8. #include <asm/unistd.h>
  9. #include <asm/cacheflush.h>
  10. #include <asm-generic/mman-common.h>
  11. static long riscv_sys_mmap(unsigned long addr, unsigned long len,
  12. unsigned long prot, unsigned long flags,
  13. unsigned long fd, off_t offset,
  14. unsigned long page_shift_offset)
  15. {
  16. if (unlikely(offset & (~PAGE_MASK >> page_shift_offset)))
  17. return -EINVAL;
  18. return ksys_mmap_pgoff(addr, len, prot, flags, fd,
  19. offset >> (PAGE_SHIFT - page_shift_offset));
  20. }
  21. #ifdef CONFIG_64BIT
  22. SYSCALL_DEFINE6(mmap, unsigned long, addr, unsigned long, len,
  23. unsigned long, prot, unsigned long, flags,
  24. unsigned long, fd, off_t, offset)
  25. {
  26. return riscv_sys_mmap(addr, len, prot, flags, fd, offset, 0);
  27. }
  28. #endif
  29. #if defined(CONFIG_32BIT) || defined(CONFIG_COMPAT)
  30. SYSCALL_DEFINE6(mmap2, unsigned long, addr, unsigned long, len,
  31. unsigned long, prot, unsigned long, flags,
  32. unsigned long, fd, off_t, offset)
  33. {
  34. /*
  35. * Note that the shift for mmap2 is constant (12),
  36. * regardless of PAGE_SIZE
  37. */
  38. return riscv_sys_mmap(addr, len, prot, flags, fd, offset, 12);
  39. }
  40. #endif
  41. /*
  42. * Allows the instruction cache to be flushed from userspace. Despite RISC-V
  43. * having a direct 'fence.i' instruction available to userspace (which we
  44. * can't trap!), that's not actually viable when running on Linux because the
  45. * kernel might schedule a process on another hart. There is no way for
  46. * userspace to handle this without invoking the kernel (as it doesn't know the
  47. * thread->hart mappings), so we've defined a RISC-V specific system call to
  48. * flush the instruction cache.
  49. *
  50. * sys_riscv_flush_icache() is defined to flush the instruction cache over an
  51. * address range, with the flush applying to either all threads or just the
  52. * caller. We don't currently do anything with the address range, that's just
  53. * in there for forwards compatibility.
  54. */
  55. SYSCALL_DEFINE3(riscv_flush_icache, uintptr_t, start, uintptr_t, end,
  56. uintptr_t, flags)
  57. {
  58. /* Check the reserved flags. */
  59. if (unlikely(flags & ~SYS_RISCV_FLUSH_ICACHE_ALL))
  60. return -EINVAL;
  61. flush_icache_mm(current->mm, flags & SYS_RISCV_FLUSH_ICACHE_LOCAL);
  62. return 0;
  63. }