syscalls_64.c 2.2 KB

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