syscalls_64.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright (C) 2003 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  3. * Copyright 2003 PathScale, Inc.
  4. *
  5. * Licensed under the GPL
  6. */
  7. #include <linux/sched.h>
  8. #include <linux/sched/mm.h>
  9. #include <linux/syscalls.h>
  10. #include <linux/uaccess.h>
  11. #include <asm/prctl.h> /* XXX This should get the constants from libc */
  12. #include <registers.h>
  13. #include <os.h>
  14. long arch_prctl(struct task_struct *task, int option,
  15. unsigned long __user *arg2)
  16. {
  17. unsigned long *ptr = arg2, tmp;
  18. long ret;
  19. int pid = task->mm->context.id.u.pid;
  20. /*
  21. * With ARCH_SET_FS (and ARCH_SET_GS is treated similarly to
  22. * be safe), we need to call arch_prctl on the host because
  23. * setting %fs may result in something else happening (like a
  24. * GDT or thread.fs being set instead). So, we let the host
  25. * fiddle the registers and thread struct and restore the
  26. * registers afterwards.
  27. *
  28. * So, the saved registers are stored to the process (this
  29. * needed because a stub may have been the last thing to run),
  30. * arch_prctl is run on the host, then the registers are read
  31. * back.
  32. */
  33. switch (option) {
  34. case ARCH_SET_FS:
  35. case ARCH_SET_GS:
  36. ret = restore_pid_registers(pid, &current->thread.regs.regs);
  37. if (ret)
  38. return ret;
  39. break;
  40. case ARCH_GET_FS:
  41. case ARCH_GET_GS:
  42. /*
  43. * With these two, we read to a local pointer and
  44. * put_user it to the userspace pointer that we were
  45. * given. If addr isn't valid (because it hasn't been
  46. * faulted in or is just bogus), we want put_user to
  47. * fault it in (or return -EFAULT) instead of having
  48. * the host return -EFAULT.
  49. */
  50. ptr = &tmp;
  51. }
  52. ret = os_arch_prctl(pid, option, ptr);
  53. if (ret)
  54. return ret;
  55. switch (option) {
  56. case ARCH_SET_FS:
  57. current->thread.arch.fs = (unsigned long) ptr;
  58. ret = save_registers(pid, &current->thread.regs.regs);
  59. break;
  60. case ARCH_SET_GS:
  61. ret = save_registers(pid, &current->thread.regs.regs);
  62. break;
  63. case ARCH_GET_FS:
  64. ret = put_user(tmp, arg2);
  65. break;
  66. case ARCH_GET_GS:
  67. ret = put_user(tmp, arg2);
  68. break;
  69. }
  70. return ret;
  71. }
  72. SYSCALL_DEFINE2(arch_prctl, int, option, unsigned long, arg2)
  73. {
  74. return arch_prctl(current, option, (unsigned long __user *) arg2);
  75. }
  76. void arch_switch_to(struct task_struct *to)
  77. {
  78. if ((to->thread.arch.fs == 0) || (to->mm == NULL))
  79. return;
  80. arch_prctl(to, ARCH_SET_FS, (void __user *) to->thread.arch.fs);
  81. }
  82. SYSCALL_DEFINE6(mmap, unsigned long, addr, unsigned long, len,
  83. unsigned long, prot, unsigned long, flags,
  84. unsigned long, fd, unsigned long, off)
  85. {
  86. if (off & ~PAGE_MASK)
  87. return -EINVAL;
  88. return ksys_mmap_pgoff(addr, len, prot, flags, fd, off >> PAGE_SHIFT);
  89. }