syscalls_64.c 2.2 KB

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