mmap.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ARC700 mmap
  4. *
  5. * (started from arm version - for VIPT alias handling)
  6. *
  7. * Copyright (C) 2013 Synopsys, Inc. (www.synopsys.com)
  8. */
  9. #include <linux/fs.h>
  10. #include <linux/mm.h>
  11. #include <linux/mman.h>
  12. #include <linux/sched/mm.h>
  13. #include <asm/cacheflush.h>
  14. #define COLOUR_ALIGN(addr, pgoff) \
  15. ((((addr) + SHMLBA - 1) & ~(SHMLBA - 1)) + \
  16. (((pgoff) << PAGE_SHIFT) & (SHMLBA - 1)))
  17. /*
  18. * Ensure that shared mappings are correctly aligned to
  19. * avoid aliasing issues with VIPT caches.
  20. * We need to ensure that
  21. * a specific page of an object is always mapped at a multiple of
  22. * SHMLBA bytes.
  23. */
  24. unsigned long
  25. arch_get_unmapped_area(struct file *filp, unsigned long addr,
  26. unsigned long len, unsigned long pgoff, unsigned long flags)
  27. {
  28. struct mm_struct *mm = current->mm;
  29. struct vm_area_struct *vma;
  30. int do_align = 0;
  31. int aliasing = cache_is_vipt_aliasing();
  32. struct vm_unmapped_area_info info;
  33. /*
  34. * We only need to do colour alignment if D cache aliases.
  35. */
  36. if (aliasing)
  37. do_align = filp || (flags & MAP_SHARED);
  38. /*
  39. * We enforce the MAP_FIXED case.
  40. */
  41. if (flags & MAP_FIXED) {
  42. if (aliasing && flags & MAP_SHARED &&
  43. (addr - (pgoff << PAGE_SHIFT)) & (SHMLBA - 1))
  44. return -EINVAL;
  45. return addr;
  46. }
  47. if (len > TASK_SIZE)
  48. return -ENOMEM;
  49. if (addr) {
  50. if (do_align)
  51. addr = COLOUR_ALIGN(addr, pgoff);
  52. else
  53. addr = PAGE_ALIGN(addr);
  54. vma = find_vma(mm, addr);
  55. if (TASK_SIZE - len >= addr &&
  56. (!vma || addr + len <= vm_start_gap(vma)))
  57. return addr;
  58. }
  59. info.flags = 0;
  60. info.length = len;
  61. info.low_limit = mm->mmap_base;
  62. info.high_limit = TASK_SIZE;
  63. info.align_mask = do_align ? (PAGE_MASK & (SHMLBA - 1)) : 0;
  64. info.align_offset = pgoff << PAGE_SHIFT;
  65. return vm_unmapped_area(&info);
  66. }
  67. static const pgprot_t protection_map[16] = {
  68. [VM_NONE] = PAGE_U_NONE,
  69. [VM_READ] = PAGE_U_R,
  70. [VM_WRITE] = PAGE_U_R,
  71. [VM_WRITE | VM_READ] = PAGE_U_R,
  72. [VM_EXEC] = PAGE_U_X_R,
  73. [VM_EXEC | VM_READ] = PAGE_U_X_R,
  74. [VM_EXEC | VM_WRITE] = PAGE_U_X_R,
  75. [VM_EXEC | VM_WRITE | VM_READ] = PAGE_U_X_R,
  76. [VM_SHARED] = PAGE_U_NONE,
  77. [VM_SHARED | VM_READ] = PAGE_U_R,
  78. [VM_SHARED | VM_WRITE] = PAGE_U_W_R,
  79. [VM_SHARED | VM_WRITE | VM_READ] = PAGE_U_W_R,
  80. [VM_SHARED | VM_EXEC] = PAGE_U_X_R,
  81. [VM_SHARED | VM_EXEC | VM_READ] = PAGE_U_X_R,
  82. [VM_SHARED | VM_EXEC | VM_WRITE] = PAGE_U_X_W_R,
  83. [VM_SHARED | VM_EXEC | VM_WRITE | VM_READ] = PAGE_U_X_W_R
  84. };
  85. DECLARE_VM_GET_PAGE_PROT