mmap.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright 2010 Tilera Corporation. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation, version 2.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  11. * NON INFRINGEMENT. See the GNU General Public License for
  12. * more details.
  13. *
  14. * Taken from the i386 architecture and simplified.
  15. */
  16. #include <linux/mm.h>
  17. #include <linux/random.h>
  18. #include <linux/limits.h>
  19. #include <linux/sched.h>
  20. #include <linux/mman.h>
  21. #include <linux/compat.h>
  22. /*
  23. * Top of mmap area (just below the process stack).
  24. *
  25. * Leave an at least ~128 MB hole.
  26. */
  27. #define MIN_GAP (128*1024*1024)
  28. #define MAX_GAP (TASK_SIZE/6*5)
  29. static inline unsigned long mmap_base(struct mm_struct *mm)
  30. {
  31. unsigned long gap = rlimit(RLIMIT_STACK);
  32. unsigned long random_factor = 0;
  33. if (current->flags & PF_RANDOMIZE)
  34. random_factor = get_random_int() % (1024*1024);
  35. if (gap < MIN_GAP)
  36. gap = MIN_GAP;
  37. else if (gap > MAX_GAP)
  38. gap = MAX_GAP;
  39. return PAGE_ALIGN(TASK_SIZE - gap - random_factor);
  40. }
  41. /*
  42. * This function, called very early during the creation of a new
  43. * process VM image, sets up which VM layout function to use:
  44. */
  45. void arch_pick_mmap_layout(struct mm_struct *mm)
  46. {
  47. #if !defined(__tilegx__)
  48. int is_32bit = 1;
  49. #elif defined(CONFIG_COMPAT)
  50. int is_32bit = is_compat_task();
  51. #else
  52. int is_32bit = 0;
  53. #endif
  54. unsigned long random_factor = 0UL;
  55. /*
  56. * 8 bits of randomness in 32bit mmaps, 24 address space bits
  57. * 12 bits of randomness in 64bit mmaps, 28 address space bits
  58. */
  59. if (current->flags & PF_RANDOMIZE) {
  60. if (is_32bit)
  61. random_factor = get_random_int() % (1<<8);
  62. else
  63. random_factor = get_random_int() % (1<<12);
  64. random_factor <<= PAGE_SHIFT;
  65. }
  66. /*
  67. * Use standard layout if the expected stack growth is unlimited
  68. * or we are running native 64 bits.
  69. */
  70. if (rlimit(RLIMIT_STACK) == RLIM_INFINITY) {
  71. mm->mmap_base = TASK_UNMAPPED_BASE + random_factor;
  72. mm->get_unmapped_area = arch_get_unmapped_area;
  73. } else {
  74. mm->mmap_base = mmap_base(mm);
  75. mm->get_unmapped_area = arch_get_unmapped_area_topdown;
  76. }
  77. }
  78. unsigned long arch_randomize_brk(struct mm_struct *mm)
  79. {
  80. unsigned long range_end = mm->brk + 0x02000000;
  81. return randomize_range(mm->brk, range_end, 0) ? : mm->brk;
  82. }