syscalls.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. ret = restore_registers(pid, &current->thread.regs.regs);
  45. if (ret)
  46. return ret;
  47. break;
  48. case ARCH_GET_FS:
  49. case ARCH_GET_GS:
  50. /*
  51. * With these two, we read to a local pointer and
  52. * put_user it to the userspace pointer that we were
  53. * given. If addr isn't valid (because it hasn't been
  54. * faulted in or is just bogus), we want put_user to
  55. * fault it in (or return -EFAULT) instead of having
  56. * the host return -EFAULT.
  57. */
  58. ptr = &tmp;
  59. }
  60. ret = os_arch_prctl(pid, code, ptr);
  61. if (ret)
  62. return ret;
  63. switch (code) {
  64. case ARCH_SET_FS:
  65. current->thread.arch.fs = (unsigned long) ptr;
  66. ret = save_registers(pid, &current->thread.regs.regs);
  67. break;
  68. case ARCH_SET_GS:
  69. ret = save_registers(pid, &current->thread.regs.regs);
  70. break;
  71. case ARCH_GET_FS:
  72. ret = put_user(tmp, addr);
  73. break;
  74. case ARCH_GET_GS:
  75. ret = put_user(tmp, addr);
  76. break;
  77. }
  78. return ret;
  79. }
  80. long sys_arch_prctl(int code, unsigned long addr)
  81. {
  82. return arch_prctl(current, code, (unsigned long __user *) addr);
  83. }
  84. long sys_clone(unsigned long clone_flags, unsigned long newsp,
  85. void __user *parent_tid, void __user *child_tid)
  86. {
  87. long ret;
  88. if (!newsp)
  89. newsp = UPT_SP(&current->thread.regs.regs);
  90. current->thread.forking = 1;
  91. ret = do_fork(clone_flags, newsp, &current->thread.regs, 0, parent_tid,
  92. child_tid);
  93. current->thread.forking = 0;
  94. return ret;
  95. }
  96. void arch_switch_to(struct task_struct *to)
  97. {
  98. if ((to->thread.arch.fs == 0) || (to->mm == NULL))
  99. return;
  100. arch_prctl(to, ARCH_SET_FS, (void __user *) to->thread.arch.fs);
  101. }