sys_x86_64.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #include <linux/errno.h>
  2. #include <linux/sched.h>
  3. #include <linux/syscalls.h>
  4. #include <linux/mm.h>
  5. #include <linux/fs.h>
  6. #include <linux/smp.h>
  7. #include <linux/sem.h>
  8. #include <linux/msg.h>
  9. #include <linux/shm.h>
  10. #include <linux/stat.h>
  11. #include <linux/mman.h>
  12. #include <linux/file.h>
  13. #include <linux/utsname.h>
  14. #include <linux/personality.h>
  15. #include <linux/random.h>
  16. #include <linux/uaccess.h>
  17. #include <linux/elf.h>
  18. #include <asm/ia32.h>
  19. #include <asm/syscalls.h>
  20. /*
  21. * Align a virtual address to avoid aliasing in the I$ on AMD F15h.
  22. */
  23. static unsigned long get_align_mask(void)
  24. {
  25. /* handle 32- and 64-bit case with a single conditional */
  26. if (va_align.flags < 0 || !(va_align.flags & (2 - mmap_is_ia32())))
  27. return 0;
  28. if (!(current->flags & PF_RANDOMIZE))
  29. return 0;
  30. return va_align.mask;
  31. }
  32. unsigned long align_vdso_addr(unsigned long addr)
  33. {
  34. unsigned long align_mask = get_align_mask();
  35. return (addr + align_mask) & ~align_mask;
  36. }
  37. static int __init control_va_addr_alignment(char *str)
  38. {
  39. /* guard against enabling this on other CPU families */
  40. if (va_align.flags < 0)
  41. return 1;
  42. if (*str == 0)
  43. return 1;
  44. if (*str == '=')
  45. str++;
  46. if (!strcmp(str, "32"))
  47. va_align.flags = ALIGN_VA_32;
  48. else if (!strcmp(str, "64"))
  49. va_align.flags = ALIGN_VA_64;
  50. else if (!strcmp(str, "off"))
  51. va_align.flags = 0;
  52. else if (!strcmp(str, "on"))
  53. va_align.flags = ALIGN_VA_32 | ALIGN_VA_64;
  54. else
  55. return 0;
  56. return 1;
  57. }
  58. __setup("align_va_addr", control_va_addr_alignment);
  59. SYSCALL_DEFINE6(mmap, unsigned long, addr, unsigned long, len,
  60. unsigned long, prot, unsigned long, flags,
  61. unsigned long, fd, unsigned long, off)
  62. {
  63. long error;
  64. error = -EINVAL;
  65. if (off & ~PAGE_MASK)
  66. goto out;
  67. error = sys_mmap_pgoff(addr, len, prot, flags, fd, off >> PAGE_SHIFT);
  68. out:
  69. return error;
  70. }
  71. static void find_start_end(unsigned long flags, unsigned long *begin,
  72. unsigned long *end)
  73. {
  74. if (!test_thread_flag(TIF_ADDR32) && (flags & MAP_32BIT)) {
  75. unsigned long new_begin;
  76. /* This is usually used needed to map code in small
  77. model, so it needs to be in the first 31bit. Limit
  78. it to that. This means we need to move the
  79. unmapped base down for this case. This can give
  80. conflicts with the heap, but we assume that glibc
  81. malloc knows how to fall back to mmap. Give it 1GB
  82. of playground for now. -AK */
  83. *begin = 0x40000000;
  84. *end = 0x80000000;
  85. if (current->flags & PF_RANDOMIZE) {
  86. new_begin = randomize_range(*begin, *begin + 0x02000000, 0);
  87. if (new_begin)
  88. *begin = new_begin;
  89. }
  90. } else {
  91. *begin = TASK_UNMAPPED_BASE;
  92. *end = TASK_SIZE;
  93. }
  94. }
  95. unsigned long
  96. arch_get_unmapped_area(struct file *filp, unsigned long addr,
  97. unsigned long len, unsigned long pgoff, unsigned long flags)
  98. {
  99. struct mm_struct *mm = current->mm;
  100. struct vm_area_struct *vma;
  101. struct vm_unmapped_area_info info;
  102. unsigned long begin, end;
  103. if (flags & MAP_FIXED)
  104. return addr;
  105. find_start_end(flags, &begin, &end);
  106. if (len > end)
  107. return -ENOMEM;
  108. if (addr) {
  109. addr = PAGE_ALIGN(addr);
  110. vma = find_vma(mm, addr);
  111. if (end - len >= addr &&
  112. (!vma || addr + len <= vma->vm_start))
  113. return addr;
  114. }
  115. info.flags = 0;
  116. info.length = len;
  117. info.low_limit = begin;
  118. info.high_limit = end;
  119. info.align_mask = filp ? get_align_mask() : 0;
  120. info.align_offset = pgoff << PAGE_SHIFT;
  121. return vm_unmapped_area(&info);
  122. }
  123. unsigned long
  124. arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
  125. const unsigned long len, const unsigned long pgoff,
  126. const unsigned long flags)
  127. {
  128. struct vm_area_struct *vma;
  129. struct mm_struct *mm = current->mm;
  130. unsigned long addr = addr0;
  131. struct vm_unmapped_area_info info;
  132. /* requested length too big for entire address space */
  133. if (len > TASK_SIZE)
  134. return -ENOMEM;
  135. if (flags & MAP_FIXED)
  136. return addr;
  137. /* for MAP_32BIT mappings we force the legacy mmap base */
  138. if (!test_thread_flag(TIF_ADDR32) && (flags & MAP_32BIT))
  139. goto bottomup;
  140. /* requesting a specific address */
  141. if (addr) {
  142. addr = PAGE_ALIGN(addr);
  143. vma = find_vma(mm, addr);
  144. if (TASK_SIZE - len >= addr &&
  145. (!vma || addr + len <= vma->vm_start))
  146. return addr;
  147. }
  148. info.flags = VM_UNMAPPED_AREA_TOPDOWN;
  149. info.length = len;
  150. info.low_limit = PAGE_SIZE;
  151. info.high_limit = mm->mmap_base;
  152. info.align_mask = filp ? get_align_mask() : 0;
  153. info.align_offset = pgoff << PAGE_SHIFT;
  154. addr = vm_unmapped_area(&info);
  155. if (!(addr & ~PAGE_MASK))
  156. return addr;
  157. VM_BUG_ON(addr != -ENOMEM);
  158. bottomup:
  159. /*
  160. * A failed mmap() very likely causes application failure,
  161. * so fall back to the bottom-up function here. This scenario
  162. * can happen with large stack limits and large mmap()
  163. * allocations.
  164. */
  165. return arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
  166. }