sys_x86_64.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * linux/arch/x86_64/kernel/sys_x86_64.c
  3. */
  4. #include <linux/errno.h>
  5. #include <linux/sched.h>
  6. #include <linux/syscalls.h>
  7. #include <linux/mm.h>
  8. #include <linux/smp.h>
  9. #include <linux/sem.h>
  10. #include <linux/msg.h>
  11. #include <linux/shm.h>
  12. #include <linux/stat.h>
  13. #include <linux/mman.h>
  14. #include <linux/file.h>
  15. #include <linux/utsname.h>
  16. #include <linux/personality.h>
  17. #include <asm/uaccess.h>
  18. #include <asm/ia32.h>
  19. /*
  20. * sys_pipe() is the normal C calling standard for creating
  21. * a pipe. It's not the way Unix traditionally does this, though.
  22. */
  23. asmlinkage long sys_pipe(int __user *fildes)
  24. {
  25. int fd[2];
  26. int error;
  27. error = do_pipe(fd);
  28. if (!error) {
  29. if (copy_to_user(fildes, fd, 2*sizeof(int)))
  30. error = -EFAULT;
  31. }
  32. return error;
  33. }
  34. asmlinkage long sys_mmap(unsigned long addr, unsigned long len, unsigned long prot, unsigned long flags,
  35. unsigned long fd, unsigned long off)
  36. {
  37. long error;
  38. struct file * file;
  39. error = -EINVAL;
  40. if (off & ~PAGE_MASK)
  41. goto out;
  42. error = -EBADF;
  43. file = NULL;
  44. flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
  45. if (!(flags & MAP_ANONYMOUS)) {
  46. file = fget(fd);
  47. if (!file)
  48. goto out;
  49. }
  50. down_write(&current->mm->mmap_sem);
  51. error = do_mmap_pgoff(file, addr, len, prot, flags, off >> PAGE_SHIFT);
  52. up_write(&current->mm->mmap_sem);
  53. if (file)
  54. fput(file);
  55. out:
  56. return error;
  57. }
  58. static void find_start_end(unsigned long flags, unsigned long *begin,
  59. unsigned long *end)
  60. {
  61. if (!test_thread_flag(TIF_IA32) && (flags & MAP_32BIT)) {
  62. /* This is usually used needed to map code in small
  63. model, so it needs to be in the first 31bit. Limit
  64. it to that. This means we need to move the
  65. unmapped base down for this case. This can give
  66. conflicts with the heap, but we assume that glibc
  67. malloc knows how to fall back to mmap. Give it 1GB
  68. of playground for now. -AK */
  69. *begin = 0x40000000;
  70. *end = 0x80000000;
  71. } else {
  72. *begin = TASK_UNMAPPED_BASE;
  73. *end = TASK_SIZE;
  74. }
  75. }
  76. unsigned long
  77. arch_get_unmapped_area(struct file *filp, unsigned long addr,
  78. unsigned long len, unsigned long pgoff, unsigned long flags)
  79. {
  80. struct mm_struct *mm = current->mm;
  81. struct vm_area_struct *vma;
  82. unsigned long start_addr;
  83. unsigned long begin, end;
  84. if (flags & MAP_FIXED)
  85. return addr;
  86. find_start_end(flags, &begin, &end);
  87. if (len > end)
  88. return -ENOMEM;
  89. if (addr) {
  90. addr = PAGE_ALIGN(addr);
  91. vma = find_vma(mm, addr);
  92. if (end - len >= addr &&
  93. (!vma || addr + len <= vma->vm_start))
  94. return addr;
  95. }
  96. if (((flags & MAP_32BIT) || test_thread_flag(TIF_IA32))
  97. && len <= mm->cached_hole_size) {
  98. mm->cached_hole_size = 0;
  99. mm->free_area_cache = begin;
  100. }
  101. addr = mm->free_area_cache;
  102. if (addr < begin)
  103. addr = begin;
  104. start_addr = addr;
  105. full_search:
  106. for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
  107. /* At this point: (!vma || addr < vma->vm_end). */
  108. if (end - len < addr) {
  109. /*
  110. * Start a new search - just in case we missed
  111. * some holes.
  112. */
  113. if (start_addr != begin) {
  114. start_addr = addr = begin;
  115. mm->cached_hole_size = 0;
  116. goto full_search;
  117. }
  118. return -ENOMEM;
  119. }
  120. if (!vma || addr + len <= vma->vm_start) {
  121. /*
  122. * Remember the place where we stopped the search:
  123. */
  124. mm->free_area_cache = addr + len;
  125. return addr;
  126. }
  127. if (addr + mm->cached_hole_size < vma->vm_start)
  128. mm->cached_hole_size = vma->vm_start - addr;
  129. addr = vma->vm_end;
  130. }
  131. }
  132. asmlinkage long sys_uname(struct new_utsname __user * name)
  133. {
  134. int err;
  135. down_read(&uts_sem);
  136. err = copy_to_user(name, utsname(), sizeof (*name));
  137. up_read(&uts_sem);
  138. if (personality(current->personality) == PER_LINUX32)
  139. err |= copy_to_user(&name->machine, "i686", 5);
  140. return err ? -EFAULT : 0;
  141. }