sys_riscv.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Copyright (C) 2012 Regents of the University of California
  3. * Copyright (C) 2014 Darius Rad <darius@bluespec.com>
  4. * Copyright (C) 2017 SiFive
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation, version 2.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/syscalls.h>
  16. #include <asm/unistd.h>
  17. #include <asm/cacheflush.h>
  18. #include <asm-generic/mman-common.h>
  19. static long riscv_sys_mmap(unsigned long addr, unsigned long len,
  20. unsigned long prot, unsigned long flags,
  21. unsigned long fd, off_t offset,
  22. unsigned long page_shift_offset)
  23. {
  24. if (unlikely(offset & (~PAGE_MASK >> page_shift_offset)))
  25. return -EINVAL;
  26. if ((prot & PROT_WRITE) && (prot & PROT_EXEC))
  27. if (unlikely(!(prot & PROT_READ)))
  28. return -EINVAL;
  29. return ksys_mmap_pgoff(addr, len, prot, flags, fd,
  30. offset >> (PAGE_SHIFT - page_shift_offset));
  31. }
  32. #ifdef CONFIG_64BIT
  33. SYSCALL_DEFINE6(mmap, unsigned long, addr, unsigned long, len,
  34. unsigned long, prot, unsigned long, flags,
  35. unsigned long, fd, off_t, offset)
  36. {
  37. return riscv_sys_mmap(addr, len, prot, flags, fd, offset, 0);
  38. }
  39. #else
  40. SYSCALL_DEFINE6(mmap2, unsigned long, addr, unsigned long, len,
  41. unsigned long, prot, unsigned long, flags,
  42. unsigned long, fd, off_t, offset)
  43. {
  44. /*
  45. * Note that the shift for mmap2 is constant (12),
  46. * regardless of PAGE_SIZE
  47. */
  48. return riscv_sys_mmap(addr, len, prot, flags, fd, offset, 12);
  49. }
  50. #endif /* !CONFIG_64BIT */
  51. /*
  52. * Allows the instruction cache to be flushed from userspace. Despite RISC-V
  53. * having a direct 'fence.i' instruction available to userspace (which we
  54. * can't trap!), that's not actually viable when running on Linux because the
  55. * kernel might schedule a process on another hart. There is no way for
  56. * userspace to handle this without invoking the kernel (as it doesn't know the
  57. * thread->hart mappings), so we've defined a RISC-V specific system call to
  58. * flush the instruction cache.
  59. *
  60. * sys_riscv_flush_icache() is defined to flush the instruction cache over an
  61. * address range, with the flush applying to either all threads or just the
  62. * caller. We don't currently do anything with the address range, that's just
  63. * in there for forwards compatibility.
  64. */
  65. SYSCALL_DEFINE3(riscv_flush_icache, uintptr_t, start, uintptr_t, end,
  66. uintptr_t, flags)
  67. {
  68. /* Check the reserved flags. */
  69. if (unlikely(flags & ~SYS_RISCV_FLUSH_ICACHE_ALL))
  70. return -EINVAL;
  71. flush_icache_mm(current->mm, flags & SYS_RISCV_FLUSH_ICACHE_LOCAL);
  72. return 0;
  73. }