syscalls.c 2.9 KB

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