mem.c 6.8 KB
Newer Older
1
/*
L
Linus Torvalds 已提交
2 3 4 5
 * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
 * Licensed under the GPL
 */

6
#include <signal.h>
L
Linus Torvalds 已提交
7
#include <errno.h>
8
#include <string.h>
9
#include <unistd.h>
L
Linus Torvalds 已提交
10
#include <sys/mman.h>
11 12
#include <sys/wait.h>
#include <asm/unistd.h>
L
Linus Torvalds 已提交
13 14
#include "mem_user.h"
#include "mem.h"
15
#include "skas.h"
L
Linus Torvalds 已提交
16 17 18
#include "user.h"
#include "os.h"
#include "proc_mm.h"
19 20 21 22 23 24 25
#include "ptrace_user.h"
#include "kern_util.h"
#include "task.h"
#include "registers.h"
#include "uml-config.h"
#include "sysdep/ptrace.h"
#include "sysdep/stub.h"
J
Jeff Dike 已提交
26
#include "init.h"
J
Jeff Dike 已提交
27
#include "kern_constants.h"
28

29
extern unsigned long batch_syscall_stub, __syscall_stub_start;
30

J
Jeff Dike 已提交
31
extern void wait_stub_done(int pid);
32

33 34 35
static inline unsigned long *check_init_stack(struct mm_id * mm_idp,
					      unsigned long *stack)
{
36
	if(stack == NULL) {
37 38 39 40 41 42
		stack = (unsigned long *) mm_idp->stack + 2;
		*stack = 0;
	}
	return stack;
}

J
Jeff Dike 已提交
43 44 45 46
static unsigned long syscall_regs[MAX_REG_NR];

static int __init init_syscall_regs(void)
{
J
Jeff Dike 已提交
47
	get_safe_registers(syscall_regs);
J
Jeff Dike 已提交
48 49 50 51 52 53 54 55
	syscall_regs[REGS_IP_INDEX] = UML_CONFIG_STUB_CODE +
		((unsigned long) &batch_syscall_stub -
		 (unsigned long) &__syscall_stub_start);
	return 0;
}

__initcall(init_syscall_regs);

56 57
extern int proc_mm;

J
Jeff Dike 已提交
58
int single_count = 0;
59 60
int multi_count = 0;
int multi_op_count = 0;
J
Jeff Dike 已提交
61

62
static inline long do_syscall_stub(struct mm_id * mm_idp, void **addr)
L
Linus Torvalds 已提交
63
{
64
	int n, i;
65
	long ret, offset;
66 67
	unsigned long * data;
	unsigned long * syscall;
J
Jeff Dike 已提交
68
	int err, pid = mm_idp->u.pid;
69 70

	if(proc_mm)
71
		/* FIXME: Need to look up userspace_pid by cpu */
72 73 74
		pid = userspace_pid[0];

	multi_count++;
75

J
Jeff Dike 已提交
76
	n = ptrace_setregs(pid, syscall_regs);
77 78 79
	if(n < 0){
		printk("Registers - \n");
		for(i = 0; i < MAX_REG_NR; i++)
J
Jeff Dike 已提交
80
			printk("\t%d\t0x%lx\n", i, syscall_regs[i]);
81
		panic("do_syscall_stub : PTRACE_SETREGS failed, errno = %d\n",
82 83
		      -n);
	}
84

J
Jeff Dike 已提交
85 86 87 88 89 90
	err = ptrace(PTRACE_CONT, pid, 0, 0);
	if(err)
		panic("Failed to continue stub, pid = %d, errno = %d\n", pid,
		      errno);

	wait_stub_done(pid);
91 92 93 94 95 96 97 98 99 100 101

	/* When the stub stops, we find the following values on the
	 * 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) {
		data = (unsigned long *)(mm_idp->stack +
					 offset - UML_CONFIG_STUB_DATA);
102 103
		printk("do_syscall_stub : ret = %ld, offset = %ld, "
		       "data = %p\n", ret, offset, data);
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
		syscall = (unsigned long *)((unsigned long)data + data[0]);
		printk("do_syscall_stub: syscall %ld failed, return value = "
		       "0x%lx, expected return value = 0x%lx\n",
		       syscall[0], ret, syscall[7]);
		printk("    syscall parameters: "
		       "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]);
		for(n = 1; n < data[0]/sizeof(long); n++) {
			if(n == 1)
				printk("    additional syscall data:");
			if(n % 4 == 1)
				printk("\n      ");
			printk("  0x%lx", data[n]);
		}
		if(n > 1)
			printk("\n");
J
Jeff Dike 已提交
121
	}
122
	else ret = 0;
J
Jeff Dike 已提交
123

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

126
	return ret;
J
Jeff Dike 已提交
127 128
}

129 130
long run_syscall_stub(struct mm_id * mm_idp, int syscall,
		      unsigned long *args, long expected, void **addr,
131
		      int done)
J
Jeff Dike 已提交
132
{
133
	unsigned long *stack = check_init_stack(mm_idp, *addr);
134 135 136 137

	if(done && *addr == NULL)
		single_count++;

138
	*stack += sizeof(long);
139
	stack += *stack / sizeof(long);
J
Jeff Dike 已提交
140

141 142 143 144 145 146 147
	*stack++ = syscall;
	*stack++ = args[0];
	*stack++ = args[1];
	*stack++ = args[2];
	*stack++ = args[3];
	*stack++ = args[4];
	*stack++ = args[5];
148
	*stack++ = expected;
149 150
	*stack = 0;
	multi_op_count++;
J
Jeff Dike 已提交
151

J
Jeff Dike 已提交
152 153
	if(!done && ((((unsigned long) stack) & ~UM_KERN_PAGE_MASK) <
		     UM_KERN_PAGE_SIZE - 10 * sizeof(long))){
154
		*addr = stack;
155 156
		return 0;
	}
J
Jeff Dike 已提交
157

158 159
	return do_syscall_stub(mm_idp, addr);
}
J
Jeff Dike 已提交
160

161 162 163 164 165 166 167 168 169 170
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;

	/* If *addr still is uninitialized, it *must* contain NULL.
	 * Thus in this case do_syscall_stub correctly won't be called.
	 */
J
Jeff Dike 已提交
171 172
	if((((unsigned long) *addr) & ~UM_KERN_PAGE_MASK) >=
	   UM_KERN_PAGE_SIZE - (10 + data_count) * sizeof(long)) {
173
		ret = do_syscall_stub(mm_idp, addr);
174
		/* in case of error, don't overwrite data on stack */
175 176 177
		if(ret)
			return ret;
	}
178

179 180
	stack = check_init_stack(mm_idp, *addr);
	*addr = stack;
181

182
	*stack = data_count * sizeof(long);
L
Linus Torvalds 已提交
183

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

J
Jeff Dike 已提交
186 187
	*stub_addr = (void *)(((unsigned long)(stack + 1) &
			       ~UM_KERN_PAGE_MASK) + UML_CONFIG_STUB_DATA);
J
Jeff Dike 已提交
188

189
	return 0;
J
Jeff Dike 已提交
190 191
}

J
Jeff Dike 已提交
192 193
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 已提交
194
{
J
Jeff Dike 已提交
195
	int ret;
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211

	if(proc_mm){
		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
					 } } } );
212 213 214
		CATCH_EINTR(ret = write(fd, &map, sizeof(map)));
		if(ret != sizeof(map)){
			ret = -errno;
215
			printk("map : /proc/mm map failed, err = %d\n", -ret);
216
		}
217
		else ret = 0;
218 219 220 221 222
	}
	else {
		unsigned long args[] = { virt, len, prot,
					 MAP_SHARED | MAP_FIXED, phys_fd,
					 MMAP_OFFSET(offset) };
223

224 225
		ret = run_syscall_stub(mm_idp, STUB_MMAP_NR, args, virt,
				       data, done);
226
	}
227

228
	return ret;
L
Linus Torvalds 已提交
229 230
}

J
Jeff Dike 已提交
231 232
int unmap(struct mm_id * mm_idp, unsigned long addr, unsigned long len,
	  int done, void **data)
L
Linus Torvalds 已提交
233
{
234 235 236 237 238 239 240 241 242 243 244 245
	int ret;

	if(proc_mm){
		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 } } } );
246 247 248
		CATCH_EINTR(ret = write(fd, &unmap, sizeof(unmap)));
		if(ret != sizeof(unmap)){
			ret = -errno;
249
			printk("unmap - proc_mm write returned %d\n", ret);
250
		}
251
		else ret = 0;
252 253 254 255
	}
	else {
		unsigned long args[] = { (unsigned long) addr, len, 0, 0, 0,
					 0 };
256

257 258
		ret = run_syscall_stub(mm_idp, __NR_munmap, args, 0,
				       data, done);
259
	}
260

261
	return ret;
262
}
L
Linus Torvalds 已提交
263

264
int protect(struct mm_id * mm_idp, unsigned long addr, unsigned long len,
J
Jeff Dike 已提交
265
	    unsigned int prot, int done, void **data)
266
{
267
	struct proc_mm_op protect;
J
Jeff Dike 已提交
268
	int ret;
269 270 271 272 273 274 275 276 277 278 279 280

	if(proc_mm){
		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 } } } );

281 282 283
		CATCH_EINTR(ret = write(fd, &protect, sizeof(protect)));
		if(ret != sizeof(protect)){
			ret = -errno;
284
			printk("protect failed, err = %d", -ret);
285
		}
286 287 288 289 290 291 292 293 294 295
		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 已提交
296 297 298 299 300
}

void before_mem_skas(unsigned long unused)
{
}