mmap.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (C) 2018 Hangzhou C-SKY Microsystems co.,ltd.
  3. #include <linux/fs.h>
  4. #include <linux/mm.h>
  5. #include <linux/mman.h>
  6. #include <linux/shm.h>
  7. #include <linux/sched.h>
  8. #include <linux/random.h>
  9. #include <linux/io.h>
  10. #define COLOUR_ALIGN(addr,pgoff) \
  11. ((((addr)+SHMLBA-1)&~(SHMLBA-1)) + \
  12. (((pgoff)<<PAGE_SHIFT) & (SHMLBA-1)))
  13. /*
  14. * We need to ensure that shared mappings are correctly aligned to
  15. * avoid aliasing issues with VIPT caches. We need to ensure that
  16. * a specific page of an object is always mapped at a multiple of
  17. * SHMLBA bytes.
  18. *
  19. * We unconditionally provide this function for all cases.
  20. */
  21. unsigned long
  22. arch_get_unmapped_area(struct file *filp, unsigned long addr,
  23. unsigned long len, unsigned long pgoff,
  24. unsigned long flags, vm_flags_t vm_flags)
  25. {
  26. struct mm_struct *mm = current->mm;
  27. struct vm_area_struct *vma;
  28. int do_align = 0;
  29. struct vm_unmapped_area_info info = {
  30. .length = len,
  31. .low_limit = mm->mmap_base,
  32. .high_limit = TASK_SIZE,
  33. .align_offset = pgoff << PAGE_SHIFT
  34. };
  35. /*
  36. * We only need to do colour alignment if either the I or D
  37. * caches alias.
  38. */
  39. do_align = filp || (flags & MAP_SHARED);
  40. /*
  41. * We enforce the MAP_FIXED case.
  42. */
  43. if (flags & MAP_FIXED) {
  44. if (flags & MAP_SHARED &&
  45. (addr - (pgoff << PAGE_SHIFT)) & (SHMLBA - 1))
  46. return -EINVAL;
  47. return addr;
  48. }
  49. if (len > TASK_SIZE)
  50. return -ENOMEM;
  51. if (addr) {
  52. if (do_align)
  53. addr = COLOUR_ALIGN(addr, pgoff);
  54. else
  55. addr = PAGE_ALIGN(addr);
  56. vma = find_vma(mm, addr);
  57. if (TASK_SIZE - len >= addr &&
  58. (!vma || addr + len <= vm_start_gap(vma)))
  59. return addr;
  60. }
  61. info.align_mask = do_align ? (PAGE_MASK & (SHMLBA - 1)) : 0;
  62. return vm_unmapped_area(&info);
  63. }