mem.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  3. * Licensed under the GPL
  4. */
  5. #include <stddef.h>
  6. #include <unistd.h>
  7. #include <errno.h>
  8. #include <string.h>
  9. #include <sys/mman.h>
  10. #include <init.h>
  11. #include <as-layout.h>
  12. #include <mm_id.h>
  13. #include <os.h>
  14. #include <ptrace_user.h>
  15. #include <registers.h>
  16. #include <skas.h>
  17. #include <sysdep/ptrace.h>
  18. #include <sysdep/stub.h>
  19. extern char batch_syscall_stub[], __syscall_stub_start[];
  20. extern void wait_stub_done(int pid);
  21. static inline unsigned long *check_init_stack(struct mm_id * mm_idp,
  22. unsigned long *stack)
  23. {
  24. if (stack == NULL) {
  25. stack = (unsigned long *) mm_idp->stack + 2;
  26. *stack = 0;
  27. }
  28. return stack;
  29. }
  30. static unsigned long syscall_regs[MAX_REG_NR];
  31. static int __init init_syscall_regs(void)
  32. {
  33. get_safe_registers(syscall_regs, NULL);
  34. syscall_regs[REGS_IP_INDEX] = STUB_CODE +
  35. ((unsigned long) batch_syscall_stub -
  36. (unsigned long) __syscall_stub_start);
  37. return 0;
  38. }
  39. __initcall(init_syscall_regs);
  40. static inline long do_syscall_stub(struct mm_id * mm_idp, void **addr)
  41. {
  42. int n, i;
  43. long ret, offset;
  44. unsigned long * data;
  45. unsigned long * syscall;
  46. int err, pid = mm_idp->u.pid;
  47. n = ptrace_setregs(pid, syscall_regs);
  48. if (n < 0) {
  49. printk(UM_KERN_ERR "Registers - \n");
  50. for (i = 0; i < MAX_REG_NR; i++)
  51. printk(UM_KERN_ERR "\t%d\t0x%lx\n", i, syscall_regs[i]);
  52. panic("do_syscall_stub : PTRACE_SETREGS failed, errno = %d\n",
  53. -n);
  54. }
  55. err = ptrace(PTRACE_CONT, pid, 0, 0);
  56. if (err)
  57. panic("Failed to continue stub, pid = %d, errno = %d\n", pid,
  58. errno);
  59. wait_stub_done(pid);
  60. /*
  61. * When the stub stops, we find the following values on the
  62. * beginning of the stack:
  63. * (long )return_value
  64. * (long )offset to failed sycall-data (0, if no error)
  65. */
  66. ret = *((unsigned long *) mm_idp->stack);
  67. offset = *((unsigned long *) mm_idp->stack + 1);
  68. if (offset) {
  69. data = (unsigned long *)(mm_idp->stack + offset - STUB_DATA);
  70. printk(UM_KERN_ERR "do_syscall_stub : ret = %ld, offset = %ld, "
  71. "data = %p\n", ret, offset, data);
  72. syscall = (unsigned long *)((unsigned long)data + data[0]);
  73. printk(UM_KERN_ERR "do_syscall_stub: syscall %ld failed, "
  74. "return value = 0x%lx, expected return value = 0x%lx\n",
  75. syscall[0], ret, syscall[7]);
  76. printk(UM_KERN_ERR " syscall parameters: "
  77. "0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
  78. syscall[1], syscall[2], syscall[3],
  79. syscall[4], syscall[5], syscall[6]);
  80. for (n = 1; n < data[0]/sizeof(long); n++) {
  81. if (n == 1)
  82. printk(UM_KERN_ERR " additional syscall "
  83. "data:");
  84. if (n % 4 == 1)
  85. printk("\n" UM_KERN_ERR " ");
  86. printk(" 0x%lx", data[n]);
  87. }
  88. if (n > 1)
  89. printk("\n");
  90. }
  91. else ret = 0;
  92. *addr = check_init_stack(mm_idp, NULL);
  93. return ret;
  94. }
  95. long run_syscall_stub(struct mm_id * mm_idp, int syscall,
  96. unsigned long *args, long expected, void **addr,
  97. int done)
  98. {
  99. unsigned long *stack = check_init_stack(mm_idp, *addr);
  100. *stack += sizeof(long);
  101. stack += *stack / sizeof(long);
  102. *stack++ = syscall;
  103. *stack++ = args[0];
  104. *stack++ = args[1];
  105. *stack++ = args[2];
  106. *stack++ = args[3];
  107. *stack++ = args[4];
  108. *stack++ = args[5];
  109. *stack++ = expected;
  110. *stack = 0;
  111. if (!done && ((((unsigned long) stack) & ~UM_KERN_PAGE_MASK) <
  112. UM_KERN_PAGE_SIZE - 10 * sizeof(long))) {
  113. *addr = stack;
  114. return 0;
  115. }
  116. return do_syscall_stub(mm_idp, addr);
  117. }
  118. long syscall_stub_data(struct mm_id * mm_idp,
  119. unsigned long *data, int data_count,
  120. void **addr, void **stub_addr)
  121. {
  122. unsigned long *stack;
  123. int ret = 0;
  124. /*
  125. * If *addr still is uninitialized, it *must* contain NULL.
  126. * Thus in this case do_syscall_stub correctly won't be called.
  127. */
  128. if ((((unsigned long) *addr) & ~UM_KERN_PAGE_MASK) >=
  129. UM_KERN_PAGE_SIZE - (10 + data_count) * sizeof(long)) {
  130. ret = do_syscall_stub(mm_idp, addr);
  131. /* in case of error, don't overwrite data on stack */
  132. if (ret)
  133. return ret;
  134. }
  135. stack = check_init_stack(mm_idp, *addr);
  136. *addr = stack;
  137. *stack = data_count * sizeof(long);
  138. memcpy(stack + 1, data, data_count * sizeof(long));
  139. *stub_addr = (void *)(((unsigned long)(stack + 1) &
  140. ~UM_KERN_PAGE_MASK) + STUB_DATA);
  141. return 0;
  142. }
  143. int map(struct mm_id * mm_idp, unsigned long virt, unsigned long len, int prot,
  144. int phys_fd, unsigned long long offset, int done, void **data)
  145. {
  146. int ret;
  147. unsigned long args[] = { virt, len, prot,
  148. MAP_SHARED | MAP_FIXED, phys_fd,
  149. MMAP_OFFSET(offset) };
  150. ret = run_syscall_stub(mm_idp, STUB_MMAP_NR, args, virt,
  151. data, done);
  152. return ret;
  153. }
  154. int unmap(struct mm_id * mm_idp, unsigned long addr, unsigned long len,
  155. int done, void **data)
  156. {
  157. int ret;
  158. unsigned long args[] = { (unsigned long) addr, len, 0, 0, 0,
  159. 0 };
  160. ret = run_syscall_stub(mm_idp, __NR_munmap, args, 0,
  161. data, done);
  162. return ret;
  163. }
  164. int protect(struct mm_id * mm_idp, unsigned long addr, unsigned long len,
  165. unsigned int prot, int done, void **data)
  166. {
  167. int ret;
  168. unsigned long args[] = { addr, len, prot, 0, 0, 0 };
  169. ret = run_syscall_stub(mm_idp, __NR_mprotect, args, 0,
  170. data, done);
  171. return ret;
  172. }