sys_riscv.c 2.4 KB

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