syscalls_64.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 <os.h>
  13. long arch_prctl(struct task_struct *task, int option,
  14. unsigned long __user *arg2)
  15. {
  16. unsigned long *ptr = arg2, tmp;
  17. long ret;
  18. int pid = task->mm->context.id.u.pid;
  19. /*
  20. * With ARCH_SET_FS (and ARCH_SET_GS is treated similarly to
  21. * be safe), we need to call arch_prctl on the host because
  22. * setting %fs may result in something else happening (like a
  23. * GDT or thread.fs being set instead). So, we let the host
  24. * fiddle the registers and thread struct and restore the
  25. * registers afterwards.
  26. *
  27. * So, the saved registers are stored to the process (this
  28. * needed because a stub may have been the last thing to run),
  29. * arch_prctl is run on the host, then the registers are read
  30. * back.
  31. */
  32. switch (option) {
  33. case ARCH_SET_FS:
  34. case ARCH_SET_GS:
  35. ret = restore_registers(pid, &current->thread.regs.regs);
  36. if (ret)
  37. return ret;
  38. break;
  39. case ARCH_GET_FS:
  40. case ARCH_GET_GS:
  41. /*
  42. * With these two, we read to a local pointer and
  43. * put_user it to the userspace pointer that we were
  44. * given. If addr isn't valid (because it hasn't been
  45. * faulted in or is just bogus), we want put_user to
  46. * fault it in (or return -EFAULT) instead of having
  47. * the host return -EFAULT.
  48. */
  49. ptr = &tmp;
  50. }
  51. ret = os_arch_prctl(pid, option, ptr);
  52. if (ret)
  53. return ret;
  54. switch (option) {
  55. case ARCH_SET_FS:
  56. current->thread.arch.fs = (unsigned long) ptr;
  57. ret = save_registers(pid, &current->thread.regs.regs);
  58. break;
  59. case ARCH_SET_GS:
  60. ret = save_registers(pid, &current->thread.regs.regs);
  61. break;
  62. case ARCH_GET_FS:
  63. ret = put_user(tmp, arg2);
  64. break;
  65. case ARCH_GET_GS:
  66. ret = put_user(tmp, arg2);
  67. break;
  68. }
  69. return ret;
  70. }
  71. SYSCALL_DEFINE2(arch_prctl, int, option, unsigned long, arg2)
  72. {
  73. return arch_prctl(current, option, (unsigned long __user *) arg2);
  74. }
  75. void arch_switch_to(struct task_struct *to)
  76. {
  77. if ((to->thread.arch.fs == 0) || (to->mm == NULL))
  78. return;
  79. arch_prctl(to, ARCH_SET_FS, (void __user *) to->thread.arch.fs);
  80. }