sys_x86_64.c 3.6 KB

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