mmap.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. /*
  55. * Use standard layout if the expected stack growth is unlimited
  56. * or we are running native 64 bits.
  57. */
  58. if (!is_32bit || rlimit(RLIMIT_STACK) == RLIM_INFINITY) {
  59. mm->mmap_base = TASK_UNMAPPED_BASE;
  60. mm->get_unmapped_area = arch_get_unmapped_area;
  61. mm->unmap_area = arch_unmap_area;
  62. } else {
  63. mm->mmap_base = mmap_base(mm);
  64. mm->get_unmapped_area = arch_get_unmapped_area_topdown;
  65. mm->unmap_area = arch_unmap_area_topdown;
  66. }
  67. }