mem.c 6.6 KB
Newer Older
1
/*
J
Jeff Dike 已提交
2
 * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
L
Linus Torvalds 已提交
3 4 5
 * Licensed under the GPL
 */

J
Jeff Dike 已提交
6 7
#include <stddef.h>
#include <unistd.h>
L
Linus Torvalds 已提交
8
#include <errno.h>
9
#include <string.h>
L
Linus Torvalds 已提交
10
#include <sys/mman.h>
11 12 13 14 15 16 17 18 19 20
#include <init.h>
#include <as-layout.h>
#include <mm_id.h>
#include <os.h>
#include <proc_mm.h>
#include <ptrace_user.h>
#include <registers.h>
#include <skas.h>
#include <sysdep/ptrace.h>
#include <sysdep/stub.h>
21

22
extern unsigned long batch_syscall_stub, __syscall_stub_start;
23

J
Jeff Dike 已提交
24
extern void wait_stub_done(int pid);
25

26 27 28
static inline unsigned long *check_init_stack(struct mm_id * mm_idp,
					      unsigned long *stack)
{
J
Jeff Dike 已提交
29
	if (stack == NULL) {
30 31 32 33 34 35
		stack = (unsigned long *) mm_idp->stack + 2;
		*stack = 0;
	}
	return stack;
}

J
Jeff Dike 已提交
36 37 38 39
static unsigned long syscall_regs[MAX_REG_NR];

static int __init init_syscall_regs(void)
{
40
	get_safe_registers(syscall_regs, NULL);
J
Jeff Dike 已提交
41
	syscall_regs[REGS_IP_INDEX] = STUB_CODE +
J
Jeff Dike 已提交
42 43 44 45 46 47 48
		((unsigned long) &batch_syscall_stub -
		 (unsigned long) &__syscall_stub_start);
	return 0;
}

__initcall(init_syscall_regs);

49 50
extern int proc_mm;

51
static inline long do_syscall_stub(struct mm_id * mm_idp, void **addr)
L
Linus Torvalds 已提交
52
{
53
	int n, i;
54
	long ret, offset;
55 56
	unsigned long * data;
	unsigned long * syscall;
J
Jeff Dike 已提交
57
	int err, pid = mm_idp->u.pid;
58

J
Jeff Dike 已提交
59
	if (proc_mm)
60
		/* FIXME: Need to look up userspace_pid by cpu */
61 62
		pid = userspace_pid[0];

J
Jeff Dike 已提交
63
	n = ptrace_setregs(pid, syscall_regs);
J
Jeff Dike 已提交
64 65 66 67
	if (n < 0) {
		printk(UM_KERN_ERR "Registers - \n");
		for (i = 0; i < MAX_REG_NR; i++)
			printk(UM_KERN_ERR "\t%d\t0x%lx\n", i, syscall_regs[i]);
68
		panic("do_syscall_stub : PTRACE_SETREGS failed, errno = %d\n",
69 70
		      -n);
	}
71

J
Jeff Dike 已提交
72
	err = ptrace(PTRACE_CONT, pid, 0, 0);
J
Jeff Dike 已提交
73
	if (err)
J
Jeff Dike 已提交
74 75 76 77
		panic("Failed to continue stub, pid = %d, errno = %d\n", pid,
		      errno);

	wait_stub_done(pid);
78

J
Jeff Dike 已提交
79 80
	/*
	 * When the stub stops, we find the following values on the
81 82 83 84 85 86 87
	 * beginning of the stack:
	 * (long )return_value
	 * (long )offset to failed sycall-data (0, if no error)
	 */
	ret = *((unsigned long *) mm_idp->stack);
	offset = *((unsigned long *) mm_idp->stack + 1);
	if (offset) {
J
Jeff Dike 已提交
88
		data = (unsigned long *)(mm_idp->stack + offset - STUB_DATA);
J
Jeff Dike 已提交
89
		printk(UM_KERN_ERR "do_syscall_stub : ret = %ld, offset = %ld, "
90
		       "data = %p\n", ret, offset, data);
91
		syscall = (unsigned long *)((unsigned long)data + data[0]);
J
Jeff Dike 已提交
92 93
		printk(UM_KERN_ERR "do_syscall_stub: syscall %ld failed, "
		       "return value = 0x%lx, expected return value = 0x%lx\n",
94
		       syscall[0], ret, syscall[7]);
J
Jeff Dike 已提交
95
		printk(UM_KERN_ERR "    syscall parameters: "
96 97 98
		       "0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
		       syscall[1], syscall[2], syscall[3],
		       syscall[4], syscall[5], syscall[6]);
J
Jeff Dike 已提交
99 100 101 102 103 104
		for (n = 1; n < data[0]/sizeof(long); n++) {
			if (n == 1)
				printk(UM_KERN_ERR "    additional syscall "
				       "data:");
			if (n % 4 == 1)
				printk("\n" UM_KERN_ERR "      ");
105 106
			printk("  0x%lx", data[n]);
		}
J
Jeff Dike 已提交
107
		if (n > 1)
108
			printk("\n");
J
Jeff Dike 已提交
109
	}
110
	else ret = 0;
J
Jeff Dike 已提交
111

112
	*addr = check_init_stack(mm_idp, NULL);
J
Jeff Dike 已提交
113

114
	return ret;
J
Jeff Dike 已提交
115 116
}

117 118
long run_syscall_stub(struct mm_id * mm_idp, int syscall,
		      unsigned long *args, long expected, void **addr,
119
		      int done)
J
Jeff Dike 已提交
120
{
121
	unsigned long *stack = check_init_stack(mm_idp, *addr);
122

123
	*stack += sizeof(long);
124
	stack += *stack / sizeof(long);
J
Jeff Dike 已提交
125

126 127 128 129 130 131 132
	*stack++ = syscall;
	*stack++ = args[0];
	*stack++ = args[1];
	*stack++ = args[2];
	*stack++ = args[3];
	*stack++ = args[4];
	*stack++ = args[5];
133
	*stack++ = expected;
134
	*stack = 0;
J
Jeff Dike 已提交
135

J
Jeff Dike 已提交
136 137
	if (!done && ((((unsigned long) stack) & ~UM_KERN_PAGE_MASK) <
		     UM_KERN_PAGE_SIZE - 10 * sizeof(long))) {
138
		*addr = stack;
139 140
		return 0;
	}
J
Jeff Dike 已提交
141

142 143
	return do_syscall_stub(mm_idp, addr);
}
J
Jeff Dike 已提交
144

145 146 147 148 149 150 151
long syscall_stub_data(struct mm_id * mm_idp,
		       unsigned long *data, int data_count,
		       void **addr, void **stub_addr)
{
	unsigned long *stack;
	int ret = 0;

J
Jeff Dike 已提交
152 153
	/*
	 * If *addr still is uninitialized, it *must* contain NULL.
154 155
	 * Thus in this case do_syscall_stub correctly won't be called.
	 */
J
Jeff Dike 已提交
156
	if ((((unsigned long) *addr) & ~UM_KERN_PAGE_MASK) >=
J
Jeff Dike 已提交
157
	   UM_KERN_PAGE_SIZE - (10 + data_count) * sizeof(long)) {
158
		ret = do_syscall_stub(mm_idp, addr);
159
		/* in case of error, don't overwrite data on stack */
J
Jeff Dike 已提交
160
		if (ret)
161 162
			return ret;
	}
163

164 165
	stack = check_init_stack(mm_idp, *addr);
	*addr = stack;
166

167
	*stack = data_count * sizeof(long);
L
Linus Torvalds 已提交
168

169
	memcpy(stack + 1, data, data_count * sizeof(long));
J
Jeff Dike 已提交
170

J
Jeff Dike 已提交
171
	*stub_addr = (void *)(((unsigned long)(stack + 1) &
J
Jeff Dike 已提交
172
			       ~UM_KERN_PAGE_MASK) + STUB_DATA);
J
Jeff Dike 已提交
173

174
	return 0;
J
Jeff Dike 已提交
175 176
}

J
Jeff Dike 已提交
177 178
int map(struct mm_id * mm_idp, unsigned long virt, unsigned long len, int prot,
	int phys_fd, unsigned long long offset, int done, void **data)
L
Linus Torvalds 已提交
179
{
J
Jeff Dike 已提交
180
	int ret;
181

J
Jeff Dike 已提交
182
	if (proc_mm) {
183 184 185 186 187 188 189 190 191 192 193 194 195 196
		struct proc_mm_op map;
		int fd = mm_idp->u.mm_fd;

		map = ((struct proc_mm_op) { .op	= MM_MMAP,
				       .u		=
				       { .mmap	=
					 { .addr	= virt,
					   .len	= len,
					   .prot	= prot,
					   .flags	= MAP_SHARED |
					   MAP_FIXED,
					   .fd	= phys_fd,
					   .offset= offset
					 } } } );
197
		CATCH_EINTR(ret = write(fd, &map, sizeof(map)));
J
Jeff Dike 已提交
198
		if (ret != sizeof(map)) {
199
			ret = -errno;
J
Jeff Dike 已提交
200 201
			printk(UM_KERN_ERR "map : /proc/mm map failed, "
			       "err = %d\n", -ret);
202
		}
203
		else ret = 0;
204 205 206 207 208
	}
	else {
		unsigned long args[] = { virt, len, prot,
					 MAP_SHARED | MAP_FIXED, phys_fd,
					 MMAP_OFFSET(offset) };
209

210 211
		ret = run_syscall_stub(mm_idp, STUB_MMAP_NR, args, virt,
				       data, done);
212
	}
213

214
	return ret;
L
Linus Torvalds 已提交
215 216
}

J
Jeff Dike 已提交
217 218
int unmap(struct mm_id * mm_idp, unsigned long addr, unsigned long len,
	  int done, void **data)
L
Linus Torvalds 已提交
219
{
220 221
	int ret;

J
Jeff Dike 已提交
222
	if (proc_mm) {
223 224 225 226 227 228 229 230 231
		struct proc_mm_op unmap;
		int fd = mm_idp->u.mm_fd;

		unmap = ((struct proc_mm_op) { .op	= MM_MUNMAP,
					 .u	=
					 { .munmap	=
					   { .addr	=
					     (unsigned long) addr,
					     .len		= len } } } );
232
		CATCH_EINTR(ret = write(fd, &unmap, sizeof(unmap)));
J
Jeff Dike 已提交
233
		if (ret != sizeof(unmap)) {
234
			ret = -errno;
J
Jeff Dike 已提交
235 236
			printk(UM_KERN_ERR "unmap - proc_mm write returned "
			       "%d\n", ret);
237
		}
238
		else ret = 0;
239 240 241 242
	}
	else {
		unsigned long args[] = { (unsigned long) addr, len, 0, 0, 0,
					 0 };
243

244 245
		ret = run_syscall_stub(mm_idp, __NR_munmap, args, 0,
				       data, done);
246
	}
247

248
	return ret;
249
}
L
Linus Torvalds 已提交
250

251
int protect(struct mm_id * mm_idp, unsigned long addr, unsigned long len,
J
Jeff Dike 已提交
252
	    unsigned int prot, int done, void **data)
253
{
254
	struct proc_mm_op protect;
J
Jeff Dike 已提交
255
	int ret;
256

J
Jeff Dike 已提交
257
	if (proc_mm) {
258 259 260 261 262 263 264 265 266 267
		int fd = mm_idp->u.mm_fd;

		protect = ((struct proc_mm_op) { .op	= MM_MPROTECT,
					   .u	=
					   { .mprotect	=
					     { .addr	=
					       (unsigned long) addr,
					       .len	= len,
					       .prot	= prot } } } );

268
		CATCH_EINTR(ret = write(fd, &protect, sizeof(protect)));
J
Jeff Dike 已提交
269
		if (ret != sizeof(protect)) {
270
			ret = -errno;
J
Jeff Dike 已提交
271
			printk(UM_KERN_ERR "protect failed, err = %d", -ret);
272
		}
273 274 275 276 277 278 279 280 281 282
		else ret = 0;
	}
	else {
		unsigned long args[] = { addr, len, prot, 0, 0, 0 };

		ret = run_syscall_stub(mm_idp, __NR_mprotect, args, 0,
				       data, done);
	}

	return ret;
L
Linus Torvalds 已提交
283
}