sys_x86_64.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. /*
  33. * To avoid aliasing in the I$ on AMD F15h, the bits defined by the
  34. * va_align.bits, [12:upper_bit), are set to a random value instead of
  35. * zeroing them. This random value is computed once per boot. This form
  36. * of ASLR is known as "per-boot ASLR".
  37. *
  38. * To achieve this, the random value is added to the info.align_offset
  39. * value before calling vm_unmapped_area() or ORed directly to the
  40. * address.
  41. */
  42. static unsigned long get_align_bits(void)
  43. {
  44. return va_align.bits & get_align_mask();
  45. }
  46. unsigned long align_vdso_addr(unsigned long addr)
  47. {
  48. unsigned long align_mask = get_align_mask();
  49. addr = (addr + align_mask) & ~align_mask;
  50. return addr | get_align_bits();
  51. }
  52. static int __init control_va_addr_alignment(char *str)
  53. {
  54. /* guard against enabling this on other CPU families */
  55. if (va_align.flags < 0)
  56. return 1;
  57. if (*str == 0)
  58. return 1;
  59. if (*str == '=')
  60. str++;
  61. if (!strcmp(str, "32"))
  62. va_align.flags = ALIGN_VA_32;
  63. else if (!strcmp(str, "64"))
  64. va_align.flags = ALIGN_VA_64;
  65. else if (!strcmp(str, "off"))
  66. va_align.flags = 0;
  67. else if (!strcmp(str, "on"))
  68. va_align.flags = ALIGN_VA_32 | ALIGN_VA_64;
  69. else
  70. return 0;
  71. return 1;
  72. }
  73. __setup("align_va_addr", control_va_addr_alignment);
  74. SYSCALL_DEFINE6(mmap, unsigned long, addr, unsigned long, len,
  75. unsigned long, prot, unsigned long, flags,
  76. unsigned long, fd, unsigned long, off)
  77. {
  78. long error;
  79. error = -EINVAL;
  80. if (off & ~PAGE_MASK)
  81. goto out;
  82. error = sys_mmap_pgoff(addr, len, prot, flags, fd, off >> PAGE_SHIFT);
  83. out:
  84. return error;
  85. }
  86. static void find_start_end(unsigned long flags, unsigned long *begin,
  87. unsigned long *end)
  88. {
  89. if (!test_thread_flag(TIF_ADDR32) && (flags & MAP_32BIT)) {
  90. unsigned long new_begin;
  91. /* This is usually used needed to map code in small
  92. model, so it needs to be in the first 31bit. Limit
  93. it to that. This means we need to move the
  94. unmapped base down for this case. This can give
  95. conflicts with the heap, but we assume that glibc
  96. malloc knows how to fall back to mmap. Give it 1GB
  97. of playground for now. -AK */
  98. *begin = 0x40000000;
  99. *end = 0x80000000;
  100. if (current->flags & PF_RANDOMIZE) {
  101. new_begin = randomize_range(*begin, *begin + 0x02000000, 0);
  102. if (new_begin)
  103. *begin = new_begin;
  104. }
  105. } else {
  106. *begin = current->mm->mmap_legacy_base;
  107. *end = TASK_SIZE;
  108. }
  109. }
  110. unsigned long
  111. arch_get_unmapped_area(struct file *filp, unsigned long addr,
  112. unsigned long len, unsigned long pgoff, unsigned long flags)
  113. {
  114. struct mm_struct *mm = current->mm;
  115. struct vm_area_struct *vma;
  116. struct vm_unmapped_area_info info;
  117. unsigned long begin, end;
  118. if (flags & MAP_FIXED)
  119. return addr;
  120. find_start_end(flags, &begin, &end);
  121. if (len > end)
  122. return -ENOMEM;
  123. if (addr) {
  124. addr = PAGE_ALIGN(addr);
  125. vma = find_vma(mm, addr);
  126. if (end - len >= addr &&
  127. (!vma || addr + len <= vm_start_gap(vma)))
  128. return addr;
  129. }
  130. info.flags = 0;
  131. info.length = len;
  132. info.low_limit = begin;
  133. info.high_limit = end;
  134. info.align_mask = 0;
  135. info.align_offset = pgoff << PAGE_SHIFT;
  136. if (filp) {
  137. info.align_mask = get_align_mask();
  138. info.align_offset += get_align_bits();
  139. }
  140. return vm_unmapped_area(&info);
  141. }
  142. unsigned long
  143. arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
  144. const unsigned long len, const unsigned long pgoff,
  145. const unsigned long flags)
  146. {
  147. struct vm_area_struct *vma;
  148. struct mm_struct *mm = current->mm;
  149. unsigned long addr = addr0;
  150. struct vm_unmapped_area_info info;
  151. /* requested length too big for entire address space */
  152. if (len > TASK_SIZE)
  153. return -ENOMEM;
  154. if (flags & MAP_FIXED)
  155. return addr;
  156. /* for MAP_32BIT mappings we force the legacy mmap base */
  157. if (!test_thread_flag(TIF_ADDR32) && (flags & MAP_32BIT))
  158. goto bottomup;
  159. /* requesting a specific address */
  160. if (addr) {
  161. addr = PAGE_ALIGN(addr);
  162. vma = find_vma(mm, addr);
  163. if (TASK_SIZE - len >= addr &&
  164. (!vma || addr + len <= vm_start_gap(vma)))
  165. return addr;
  166. }
  167. info.flags = VM_UNMAPPED_AREA_TOPDOWN;
  168. info.length = len;
  169. info.low_limit = PAGE_SIZE;
  170. info.high_limit = mm->mmap_base;
  171. info.align_mask = 0;
  172. info.align_offset = pgoff << PAGE_SHIFT;
  173. if (filp) {
  174. info.align_mask = get_align_mask();
  175. info.align_offset += get_align_bits();
  176. }
  177. addr = vm_unmapped_area(&info);
  178. if (!(addr & ~PAGE_MASK))
  179. return addr;
  180. VM_BUG_ON(addr != -ENOMEM);
  181. bottomup:
  182. /*
  183. * A failed mmap() very likely causes application failure,
  184. * so fall back to the bottom-up function here. This scenario
  185. * can happen with large stack limits and large mmap()
  186. * allocations.
  187. */
  188. return arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
  189. }