syscalls.c 2.6 KB

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