mem.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
  3. * Licensed under the GPL
  4. */
  5. #include <signal.h>
  6. #include <errno.h>
  7. #include <string.h>
  8. #include <sys/mman.h>
  9. #include <sys/wait.h>
  10. #include <asm/page.h>
  11. #include <asm/unistd.h>
  12. #include "mem_user.h"
  13. #include "mem.h"
  14. #include "skas.h"
  15. #include "user.h"
  16. #include "os.h"
  17. #include "proc_mm.h"
  18. #include "ptrace_user.h"
  19. #include "user_util.h"
  20. #include "kern_util.h"
  21. #include "task.h"
  22. #include "registers.h"
  23. #include "uml-config.h"
  24. #include "sysdep/ptrace.h"
  25. #include "sysdep/stub.h"
  26. extern unsigned long batch_syscall_stub, __syscall_stub_start;
  27. extern void wait_stub_done(int pid, int sig, char * fname);
  28. static inline unsigned long *check_init_stack(struct mm_id * mm_idp,
  29. unsigned long *stack)
  30. {
  31. if(stack == NULL) {
  32. stack = (unsigned long *) mm_idp->stack + 2;
  33. *stack = 0;
  34. }
  35. return stack;
  36. }
  37. extern int proc_mm;
  38. int single_count = 0;
  39. int multi_count = 0;
  40. int multi_op_count = 0;
  41. static inline long do_syscall_stub(struct mm_id * mm_idp, void **addr)
  42. {
  43. unsigned long regs[MAX_REG_NR];
  44. int n;
  45. long ret, offset;
  46. unsigned long * data;
  47. unsigned long * syscall;
  48. int pid = mm_idp->u.pid;
  49. if(proc_mm)
  50. #warning Need to look up userspace_pid by cpu
  51. pid = userspace_pid[0];
  52. multi_count++;
  53. get_safe_registers(regs, NULL);
  54. regs[REGS_IP_INDEX] = UML_CONFIG_STUB_CODE +
  55. ((unsigned long) &batch_syscall_stub -
  56. (unsigned long) &__syscall_stub_start);
  57. n = ptrace_setregs(pid, regs);
  58. if(n < 0)
  59. panic("do_syscall_stub : PTRACE_SETREGS failed, errno = %d\n",
  60. n);
  61. wait_stub_done(pid, 0, "do_syscall_stub");
  62. /* When the stub stops, we find the following values on the
  63. * beginning of the stack:
  64. * (long )return_value
  65. * (long )offset to failed sycall-data (0, if no error)
  66. */
  67. ret = *((unsigned long *) mm_idp->stack);
  68. offset = *((unsigned long *) mm_idp->stack + 1);
  69. if (offset) {
  70. data = (unsigned long *)(mm_idp->stack +
  71. offset - UML_CONFIG_STUB_DATA);
  72. printk("do_syscall_stub : ret = %ld, offset = %ld, "
  73. "data = %p\n", ret, offset, data);
  74. syscall = (unsigned long *)((unsigned long)data + data[0]);
  75. printk("do_syscall_stub: syscall %ld failed, return value = "
  76. "0x%lx, expected return value = 0x%lx\n",
  77. syscall[0], ret, syscall[7]);
  78. printk(" syscall parameters: "
  79. "0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
  80. syscall[1], syscall[2], syscall[3],
  81. syscall[4], syscall[5], syscall[6]);
  82. for(n = 1; n < data[0]/sizeof(long); n++) {
  83. if(n == 1)
  84. printk(" additional syscall data:");
  85. if(n % 4 == 1)
  86. printk("\n ");
  87. printk(" 0x%lx", data[n]);
  88. }
  89. if(n > 1)
  90. printk("\n");
  91. }
  92. else ret = 0;
  93. *addr = check_init_stack(mm_idp, NULL);
  94. return ret;
  95. }
  96. long run_syscall_stub(struct mm_id * mm_idp, int syscall,
  97. unsigned long *args, long expected, void **addr,
  98. int done)
  99. {
  100. unsigned long *stack = check_init_stack(mm_idp, *addr);
  101. if(done && *addr == NULL)
  102. single_count++;
  103. *stack += sizeof(long);
  104. stack += *stack / sizeof(long);
  105. *stack++ = syscall;
  106. *stack++ = args[0];
  107. *stack++ = args[1];
  108. *stack++ = args[2];
  109. *stack++ = args[3];
  110. *stack++ = args[4];
  111. *stack++ = args[5];
  112. *stack++ = expected;
  113. *stack = 0;
  114. multi_op_count++;
  115. if(!done && ((((unsigned long) stack) & ~PAGE_MASK) <
  116. PAGE_SIZE - 10 * sizeof(long))){
  117. *addr = stack;
  118. return 0;
  119. }
  120. return do_syscall_stub(mm_idp, addr);
  121. }
  122. long syscall_stub_data(struct mm_id * mm_idp,
  123. unsigned long *data, int data_count,
  124. void **addr, void **stub_addr)
  125. {
  126. unsigned long *stack;
  127. int ret = 0;
  128. /* If *addr still is uninitialized, it *must* contain NULL.
  129. * Thus in this case do_syscall_stub correctly won't be called.
  130. */
  131. if((((unsigned long) *addr) & ~PAGE_MASK) >=
  132. PAGE_SIZE - (10 + data_count) * sizeof(long)) {
  133. ret = do_syscall_stub(mm_idp, addr);
  134. /* in case of error, don't overwrite data on stack */
  135. if(ret)
  136. return ret;
  137. }
  138. stack = check_init_stack(mm_idp, *addr);
  139. *addr = stack;
  140. *stack = data_count * sizeof(long);
  141. memcpy(stack + 1, data, data_count * sizeof(long));
  142. *stub_addr = (void *)(((unsigned long)(stack + 1) & ~PAGE_MASK) +
  143. UML_CONFIG_STUB_DATA);
  144. return 0;
  145. }
  146. int map(struct mm_id * mm_idp, unsigned long virt, unsigned long len,
  147. int r, int w, int x, int phys_fd, unsigned long long offset,
  148. int done, void **data)
  149. {
  150. int prot, ret;
  151. prot = (r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
  152. (x ? PROT_EXEC : 0);
  153. if(proc_mm){
  154. struct proc_mm_op map;
  155. int fd = mm_idp->u.mm_fd;
  156. map = ((struct proc_mm_op) { .op = MM_MMAP,
  157. .u =
  158. { .mmap =
  159. { .addr = virt,
  160. .len = len,
  161. .prot = prot,
  162. .flags = MAP_SHARED |
  163. MAP_FIXED,
  164. .fd = phys_fd,
  165. .offset= offset
  166. } } } );
  167. ret = os_write_file(fd, &map, sizeof(map));
  168. if(ret != sizeof(map))
  169. printk("map : /proc/mm map failed, err = %d\n", -ret);
  170. else ret = 0;
  171. }
  172. else {
  173. unsigned long args[] = { virt, len, prot,
  174. MAP_SHARED | MAP_FIXED, phys_fd,
  175. MMAP_OFFSET(offset) };
  176. ret = run_syscall_stub(mm_idp, STUB_MMAP_NR, args, virt,
  177. data, done);
  178. }
  179. return ret;
  180. }
  181. int unmap(struct mm_id * mm_idp, void *addr, unsigned long len, int done,
  182. void **data)
  183. {
  184. int ret;
  185. if(proc_mm){
  186. struct proc_mm_op unmap;
  187. int fd = mm_idp->u.mm_fd;
  188. unmap = ((struct proc_mm_op) { .op = MM_MUNMAP,
  189. .u =
  190. { .munmap =
  191. { .addr =
  192. (unsigned long) addr,
  193. .len = len } } } );
  194. ret = os_write_file(fd, &unmap, sizeof(unmap));
  195. if(ret != sizeof(unmap))
  196. printk("unmap - proc_mm write returned %d\n", ret);
  197. else ret = 0;
  198. }
  199. else {
  200. unsigned long args[] = { (unsigned long) addr, len, 0, 0, 0,
  201. 0 };
  202. ret = run_syscall_stub(mm_idp, __NR_munmap, args, 0,
  203. data, done);
  204. }
  205. return ret;
  206. }
  207. int protect(struct mm_id * mm_idp, unsigned long addr, unsigned long len,
  208. int r, int w, int x, int done, void **data)
  209. {
  210. struct proc_mm_op protect;
  211. int prot, ret;
  212. prot = (r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
  213. (x ? PROT_EXEC : 0);
  214. if(proc_mm){
  215. int fd = mm_idp->u.mm_fd;
  216. protect = ((struct proc_mm_op) { .op = MM_MPROTECT,
  217. .u =
  218. { .mprotect =
  219. { .addr =
  220. (unsigned long) addr,
  221. .len = len,
  222. .prot = prot } } } );
  223. ret = os_write_file(fd, &protect, sizeof(protect));
  224. if(ret != sizeof(protect))
  225. printk("protect failed, err = %d", -ret);
  226. else ret = 0;
  227. }
  228. else {
  229. unsigned long args[] = { addr, len, prot, 0, 0, 0 };
  230. ret = run_syscall_stub(mm_idp, __NR_mprotect, args, 0,
  231. data, done);
  232. }
  233. return ret;
  234. }
  235. void before_mem_skas(unsigned long unused)
  236. {
  237. }