aboutsummaryrefslogtreecommitdiffstats
path: root/arch/um/kernel/skas/mem_user.c
diff options
context:
space:
mode:
Diffstat (limited to 'arch/um/kernel/skas/mem_user.c')
-rw-r--r--arch/um/kernel/skas/mem_user.c234
1 files changed, 138 insertions, 96 deletions
diff --git a/arch/um/kernel/skas/mem_user.c b/arch/um/kernel/skas/mem_user.c
index c976320ebe84..1d89640bd502 100644
--- a/arch/um/kernel/skas/mem_user.c
+++ b/arch/um/kernel/skas/mem_user.c
@@ -5,13 +5,14 @@
5 5
6#include <signal.h> 6#include <signal.h>
7#include <errno.h> 7#include <errno.h>
8#include <string.h>
8#include <sys/mman.h> 9#include <sys/mman.h>
9#include <sys/wait.h> 10#include <sys/wait.h>
10#include <asm/page.h> 11#include <asm/page.h>
11#include <asm/unistd.h> 12#include <asm/unistd.h>
12#include "mem_user.h" 13#include "mem_user.h"
13#include "mem.h" 14#include "mem.h"
14#include "mm_id.h" 15#include "skas.h"
15#include "user.h" 16#include "user.h"
16#include "os.h" 17#include "os.h"
17#include "proc_mm.h" 18#include "proc_mm.h"
@@ -23,56 +24,99 @@
23#include "uml-config.h" 24#include "uml-config.h"
24#include "sysdep/ptrace.h" 25#include "sysdep/ptrace.h"
25#include "sysdep/stub.h" 26#include "sysdep/stub.h"
26#include "skas.h"
27 27
28extern unsigned long syscall_stub, batch_syscall_stub, __syscall_stub_start; 28extern unsigned long batch_syscall_stub, __syscall_stub_start;
29 29
30extern void wait_stub_done(int pid, int sig, char * fname); 30extern void wait_stub_done(int pid, int sig, char * fname);
31 31
32static inline unsigned long *check_init_stack(struct mm_id * mm_idp,
33 unsigned long *stack)
34{
35 if(stack == NULL){
36 stack = (unsigned long *) mm_idp->stack + 2;
37 *stack = 0;
38 }
39 return stack;
40}
41
42extern int proc_mm;
43
32int single_count = 0; 44int single_count = 0;
45int multi_count = 0;
46int multi_op_count = 0;
33 47
34static long one_syscall_stub(struct mm_id * mm_idp, int syscall, 48static long do_syscall_stub(struct mm_id *mm_idp, void **addr)
35 unsigned long *args)
36{ 49{
50 unsigned long regs[MAX_REG_NR];
51 unsigned long *data;
52 unsigned long *syscall;
53 long ret, offset;
37 int n, pid = mm_idp->u.pid; 54 int n, pid = mm_idp->u.pid;
38 unsigned long regs[MAX_REG_NR]; 55
56 if(proc_mm)
57#warning Need to look up userspace_pid by cpu
58 pid = userspace_pid[0];
59
60 multi_count++;
39 61
40 get_safe_registers(regs); 62 get_safe_registers(regs);
41 regs[REGS_IP_INDEX] = UML_CONFIG_STUB_CODE + 63 regs[REGS_IP_INDEX] = UML_CONFIG_STUB_CODE +
42 ((unsigned long) &syscall_stub - 64 ((unsigned long) &batch_syscall_stub -
43 (unsigned long) &__syscall_stub_start); 65 (unsigned long) &__syscall_stub_start);
44 /* XXX Don't have a define for starting a syscall */ 66 n = ptrace_setregs(pid, regs);
45 regs[REGS_SYSCALL_NR] = syscall; 67 if(n < 0)
46 regs[REGS_SYSCALL_ARG1] = args[0]; 68 panic("do_syscall_stub : PTRACE_SETREGS failed, errno = %d\n",
47 regs[REGS_SYSCALL_ARG2] = args[1]; 69 n);
48 regs[REGS_SYSCALL_ARG3] = args[2]; 70
49 regs[REGS_SYSCALL_ARG4] = args[3]; 71 wait_stub_done(pid, 0, "do_syscall_stub");
50 regs[REGS_SYSCALL_ARG5] = args[4]; 72
51 regs[REGS_SYSCALL_ARG6] = args[5]; 73 /* When the stub stops, we find the following values on the
52 n = ptrace_setregs(pid, regs); 74 * beginning of the stack:
53 if(n < 0){ 75 * (long )return_value
54 printk("one_syscall_stub : PTRACE_SETREGS failed, " 76 * (long )offset to failed sycall-data (0, if no error)
55 "errno = %d\n", n); 77 */
56 return(n); 78 ret = *((unsigned long *) mm_idp->stack);
79 offset = *((unsigned long *) mm_idp->stack + 1);
80 if (offset) {
81 data = (unsigned long *)(mm_idp->stack +
82 offset - UML_CONFIG_STUB_DATA);
83 syscall = (unsigned long *)((unsigned long)data + data[0]);
84 printk("do_syscall_stub: syscall %ld failed, return value = "
85 "0x%lx, expected return value = 0x%lx\n",
86 syscall[0], ret, syscall[7]);
87 printk(" syscall parameters: "
88 "0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
89 syscall[1], syscall[2], syscall[3],
90 syscall[4], syscall[5], syscall[6]);
91 for(n = 1; n < data[0]/sizeof(long); n++) {
92 if(n == 1)
93 printk(" additional syscall data:");
94 if(n % 4 == 1)
95 printk("\n ");
96 printk(" 0x%lx", data[n]);
97 }
98 if(n > 1)
99 printk("\n");
57 } 100 }
101 else ret = 0;
58 102
59 wait_stub_done(pid, 0, "one_syscall_stub"); 103 *addr = check_init_stack(mm_idp, NULL);
60 104
61 return(*((unsigned long *) mm_idp->stack)); 105 return ret;
62} 106}
63 107
64int multi_count = 0; 108long run_syscall_stub(struct mm_id * mm_idp, int syscall,
65int multi_op_count = 0; 109 unsigned long *args, long expected, void **addr,
66 110 int done)
67static long many_syscall_stub(struct mm_id * mm_idp, int syscall,
68 unsigned long *args, int done, void **addr_out)
69{ 111{
70 unsigned long regs[MAX_REG_NR], *stack; 112 unsigned long *stack = check_init_stack(mm_idp, *addr);
71 int n, pid = mm_idp->u.pid; 113
114 if(done && *addr == NULL)
115 single_count++;
116
117 *stack += sizeof(long);
118 stack += *stack / sizeof(long);
72 119
73 stack = *addr_out;
74 if(stack == NULL)
75 stack = (unsigned long *) current_stub_stack();
76 *stack++ = syscall; 120 *stack++ = syscall;
77 *stack++ = args[0]; 121 *stack++ = args[0];
78 *stack++ = args[1]; 122 *stack++ = args[1];
@@ -80,53 +124,55 @@ static long many_syscall_stub(struct mm_id * mm_idp, int syscall,
80 *stack++ = args[3]; 124 *stack++ = args[3];
81 *stack++ = args[4]; 125 *stack++ = args[4];
82 *stack++ = args[5]; 126 *stack++ = args[5];
127 *stack++ = expected;
83 *stack = 0; 128 *stack = 0;
84 multi_op_count++; 129 multi_op_count++;
85 130
86 if(!done && ((((unsigned long) stack) & ~PAGE_MASK) < 131 if(!done && ((((unsigned long) stack) & ~PAGE_MASK) <
87 PAGE_SIZE - 8 * sizeof(long))){ 132 PAGE_SIZE - 10 * sizeof(long))){
88 *addr_out = stack; 133 *addr = stack;
89 return 0; 134 return 0;
90 } 135 }
91 136
92 multi_count++; 137 return do_syscall_stub(mm_idp, addr);
93 get_safe_registers(regs); 138}
94 regs[REGS_IP_INDEX] = UML_CONFIG_STUB_CODE +
95 ((unsigned long) &batch_syscall_stub -
96 (unsigned long) &__syscall_stub_start);
97 regs[REGS_SP_INDEX] = UML_CONFIG_STUB_DATA;
98 139
99 n = ptrace_setregs(pid, regs); 140long syscall_stub_data(struct mm_id * mm_idp,
100 if(n < 0){ 141 unsigned long *data, int data_count,
101 printk("many_syscall_stub : PTRACE_SETREGS failed, " 142 void **addr, void **stub_addr)
102 "errno = %d\n", n); 143{
103 return(n); 144 unsigned long *stack;
104 } 145 int ret = 0;
146
147 /* If *addr still is uninitialized, it *must* contain NULL.
148 * Thus in this case do_syscall_stub correctly won't be called.
149 */
150 if((((unsigned long) *addr) & ~PAGE_MASK) >=
151 PAGE_SIZE - (10 + data_count) * sizeof(long)) {
152 ret = do_syscall_stub(mm_idp, addr);
153 /* in case of error, don't overwrite data on stack */
154 if(ret)
155 return ret;
156 }
105 157
106 wait_stub_done(pid, 0, "many_syscall_stub"); 158 stack = check_init_stack(mm_idp, *addr);
107 stack = (unsigned long *) mm_idp->stack; 159 *addr = stack;
108 160
109 *addr_out = stack; 161 *stack = data_count * sizeof(long);
110 return(*stack);
111}
112 162
113static long run_syscall_stub(struct mm_id * mm_idp, int syscall, 163 memcpy(stack + 1, data, data_count * sizeof(long));
114 unsigned long *args, void **addr, int done)
115{
116 long res;
117 164
118 if((*addr == NULL) && done) 165 *stub_addr = (void *)(((unsigned long)(stack + 1) & ~PAGE_MASK) +
119 res = one_syscall_stub(mm_idp, syscall, args); 166 UML_CONFIG_STUB_DATA);
120 else res = many_syscall_stub(mm_idp, syscall, args, done, addr);
121 167
122 return res; 168 return 0;
123} 169}
124 170
125void *map(struct mm_id * mm_idp, unsigned long virt, unsigned long len, 171int map(struct mm_id * mm_idp, unsigned long virt, unsigned long len,
126 int r, int w, int x, int phys_fd, unsigned long long offset, 172 int r, int w, int x, int phys_fd, unsigned long long offset,
127 int done, void *data) 173 int done, void **data)
128{ 174{
129 int prot, n; 175 int prot, ret;
130 176
131 prot = (r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) | 177 prot = (r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
132 (x ? PROT_EXEC : 0); 178 (x ? PROT_EXEC : 0);
@@ -146,29 +192,27 @@ void *map(struct mm_id * mm_idp, unsigned long virt, unsigned long len,
146 .fd = phys_fd, 192 .fd = phys_fd,
147 .offset= offset 193 .offset= offset
148 } } } ); 194 } } } );
149 n = os_write_file(fd, &map, sizeof(map)); 195 ret = os_write_file(fd, &map, sizeof(map));
150 if(n != sizeof(map)) 196 if(ret != sizeof(map))
151 printk("map : /proc/mm map failed, err = %d\n", -n); 197 printk("map : /proc/mm map failed, err = %d\n", -ret);
198 else ret = 0;
152 } 199 }
153 else { 200 else {
154 long res;
155 unsigned long args[] = { virt, len, prot, 201 unsigned long args[] = { virt, len, prot,
156 MAP_SHARED | MAP_FIXED, phys_fd, 202 MAP_SHARED | MAP_FIXED, phys_fd,
157 MMAP_OFFSET(offset) }; 203 MMAP_OFFSET(offset) };
158 204
159 res = run_syscall_stub(mm_idp, STUB_MMAP_NR, args, 205 ret = run_syscall_stub(mm_idp, STUB_MMAP_NR, args, virt,
160 &data, done); 206 data, done);
161 if((void *) res == MAP_FAILED)
162 printk("mmap stub failed, errno = %d\n", res);
163 } 207 }
164 208
165 return data; 209 return ret;
166} 210}
167 211
168void *unmap(struct mm_id * mm_idp, void *addr, unsigned long len, int done, 212int unmap(struct mm_id * mm_idp, void *addr, unsigned long len, int done,
169 void *data) 213 void **data)
170{ 214{
171 int n; 215 int ret;
172 216
173 if(proc_mm){ 217 if(proc_mm){
174 struct proc_mm_op unmap; 218 struct proc_mm_op unmap;
@@ -180,29 +224,29 @@ void *unmap(struct mm_id * mm_idp, void *addr, unsigned long len, int done,
180 { .addr = 224 { .addr =
181 (unsigned long) addr, 225 (unsigned long) addr,
182 .len = len } } } ); 226 .len = len } } } );
183 n = os_write_file(fd, &unmap, sizeof(unmap)); 227 ret = os_write_file(fd, &unmap, sizeof(unmap));
184 if(n != sizeof(unmap)) 228 if(ret != sizeof(unmap))
185 printk("unmap - proc_mm write returned %d\n", n); 229 printk("unmap - proc_mm write returned %d\n", ret);
230 else ret = 0;
186 } 231 }
187 else { 232 else {
188 int res;
189 unsigned long args[] = { (unsigned long) addr, len, 0, 0, 0, 233 unsigned long args[] = { (unsigned long) addr, len, 0, 0, 0,
190 0 }; 234 0 };
191 235
192 res = run_syscall_stub(mm_idp, __NR_munmap, args, 236 ret = run_syscall_stub(mm_idp, __NR_munmap, args, 0,
193 &data, done); 237 data, done);
194 if(res < 0) 238 if(ret < 0)
195 printk("munmap stub failed, errno = %d\n", res); 239 printk("munmap stub failed, errno = %d\n", ret);
196 } 240 }
197 241
198 return data; 242 return ret;
199} 243}
200 244
201void *protect(struct mm_id * mm_idp, unsigned long addr, unsigned long len, 245int protect(struct mm_id * mm_idp, unsigned long addr, unsigned long len,
202 int r, int w, int x, int done, void *data) 246 int r, int w, int x, int done, void **data)
203{ 247{
204 struct proc_mm_op protect; 248 struct proc_mm_op protect;
205 int prot, n; 249 int prot, ret;
206 250
207 prot = (r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) | 251 prot = (r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
208 (x ? PROT_EXEC : 0); 252 (x ? PROT_EXEC : 0);
@@ -217,21 +261,19 @@ void *protect(struct mm_id * mm_idp, unsigned long addr, unsigned long len,
217 .len = len, 261 .len = len,
218 .prot = prot } } } ); 262 .prot = prot } } } );
219 263
220 n = os_write_file(fd, &protect, sizeof(protect)); 264 ret = os_write_file(fd, &protect, sizeof(protect));
221 if(n != sizeof(protect)) 265 if(ret != sizeof(protect))
222 panic("protect failed, err = %d", -n); 266 printk("protect failed, err = %d", -ret);
267 else ret = 0;
223 } 268 }
224 else { 269 else {
225 int res;
226 unsigned long args[] = { addr, len, prot, 0, 0, 0 }; 270 unsigned long args[] = { addr, len, prot, 0, 0, 0 };
227 271
228 res = run_syscall_stub(mm_idp, __NR_mprotect, args, 272 ret = run_syscall_stub(mm_idp, __NR_mprotect, args, 0,
229 &data, done); 273 data, done);
230 if(res < 0)
231 panic("mprotect stub failed, errno = %d\n", res);
232 } 274 }
233 275
234 return data; 276 return ret;
235} 277}
236 278
237void before_mem_skas(unsigned long unused) 279void before_mem_skas(unsigned long unused)