/arch/x86_64/kernel/sys_x86_64.c

https://bitbucket.org/evzijst/gittest · C · 173 lines · 133 code · 18 blank · 22 comment · 25 complexity · 8acdd8115a49202c808681a7f22af8dc MD5 · raw file

  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/smp_lock.h>
  10. #include <linux/sem.h>
  11. #include <linux/msg.h>
  12. #include <linux/shm.h>
  13. #include <linux/stat.h>
  14. #include <linux/mman.h>
  15. #include <linux/file.h>
  16. #include <linux/utsname.h>
  17. #include <linux/personality.h>
  18. #include <asm/uaccess.h>
  19. #include <asm/ia32.h>
  20. /*
  21. * sys_pipe() is the normal C calling standard for creating
  22. * a pipe. It's not the way Unix traditionally does this, though.
  23. */
  24. asmlinkage long sys_pipe(int __user *fildes)
  25. {
  26. int fd[2];
  27. int error;
  28. error = do_pipe(fd);
  29. if (!error) {
  30. if (copy_to_user(fildes, fd, 2*sizeof(int)))
  31. error = -EFAULT;
  32. }
  33. return error;
  34. }
  35. asmlinkage long sys_mmap(unsigned long addr, unsigned long len, unsigned long prot, unsigned long flags,
  36. unsigned long fd, unsigned long off)
  37. {
  38. long error;
  39. struct file * file;
  40. error = -EINVAL;
  41. if (off & ~PAGE_MASK)
  42. goto out;
  43. error = -EBADF;
  44. file = NULL;
  45. flags &= ~(MAP_EXECUTABLE | MAP_DENYWRITE);
  46. if (!(flags & MAP_ANONYMOUS)) {
  47. file = fget(fd);
  48. if (!file)
  49. goto out;
  50. }
  51. down_write(&current->mm->mmap_sem);
  52. error = do_mmap_pgoff(file, addr, len, prot, flags, off >> PAGE_SHIFT);
  53. up_write(&current->mm->mmap_sem);
  54. if (file)
  55. fput(file);
  56. out:
  57. return error;
  58. }
  59. static void find_start_end(unsigned long flags, unsigned long *begin,
  60. unsigned long *end)
  61. {
  62. #ifdef CONFIG_IA32_EMULATION
  63. if (test_thread_flag(TIF_IA32)) {
  64. *begin = TASK_UNMAPPED_32;
  65. *end = IA32_PAGE_OFFSET;
  66. } else
  67. #endif
  68. if (flags & MAP_32BIT) {
  69. /* This is usually used needed to map code in small
  70. model, so it needs to be in the first 31bit. Limit
  71. it to that. This means we need to move the
  72. unmapped base down for this case. This can give
  73. conflicts with the heap, but we assume that glibc
  74. malloc knows how to fall back to mmap. Give it 1GB
  75. of playground for now. -AK */
  76. *begin = 0x40000000;
  77. *end = 0x80000000;
  78. } else {
  79. *begin = TASK_UNMAPPED_64;
  80. *end = TASK_SIZE;
  81. }
  82. }
  83. unsigned long
  84. arch_get_unmapped_area(struct file *filp, unsigned long addr,
  85. unsigned long len, unsigned long pgoff, unsigned long flags)
  86. {
  87. struct mm_struct *mm = current->mm;
  88. struct vm_area_struct *vma;
  89. unsigned long start_addr;
  90. unsigned long begin, end;
  91. find_start_end(flags, &begin, &end);
  92. if (len > end)
  93. return -ENOMEM;
  94. if (addr) {
  95. addr = PAGE_ALIGN(addr);
  96. vma = find_vma(mm, addr);
  97. if (end - len >= addr &&
  98. (!vma || addr + len <= vma->vm_start))
  99. return addr;
  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. goto full_search;
  116. }
  117. return -ENOMEM;
  118. }
  119. if (!vma || addr + len <= vma->vm_start) {
  120. /*
  121. * Remember the place where we stopped the search:
  122. */
  123. mm->free_area_cache = addr + len;
  124. return addr;
  125. }
  126. addr = vma->vm_end;
  127. }
  128. }
  129. asmlinkage long sys_uname(struct new_utsname __user * name)
  130. {
  131. int err;
  132. down_read(&uts_sem);
  133. err = copy_to_user(name, &system_utsname, sizeof (*name));
  134. up_read(&uts_sem);
  135. if (personality(current->personality) == PER_LINUX32)
  136. err |= copy_to_user(&name->machine, "i686", 5);
  137. return err ? -EFAULT : 0;
  138. }
  139. asmlinkage long wrap_sys_shmat(int shmid, char __user *shmaddr, int shmflg)
  140. {
  141. unsigned long raddr;
  142. return do_shmat(shmid,shmaddr,shmflg,&raddr) ?: (long)raddr;
  143. }
  144. asmlinkage long sys_time64(long __user * tloc)
  145. {
  146. struct timeval now;
  147. int i;
  148. do_gettimeofday(&now);
  149. i = now.tv_sec;
  150. if (tloc) {
  151. if (put_user(i,tloc))
  152. i = -EFAULT;
  153. }
  154. return i;
  155. }