syscalls_64.c 2.2 KB

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