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

#include <stdio.h>
7
#include <stdlib.h>
J
Jeff Dike 已提交
8
#include <stdarg.h>
L
Linus Torvalds 已提交
9 10
#include <unistd.h>
#include <errno.h>
J
Jeff Dike 已提交
11 12 13 14
#include <fcntl.h>
#include <sched.h>
#include <signal.h>
#include <string.h>
L
Linus Torvalds 已提交
15
#include <sys/mman.h>
J
Jeff Dike 已提交
16 17 18
#include <sys/ptrace.h>
#include <sys/stat.h>
#include <sys/wait.h>
L
Linus Torvalds 已提交
19 20
#include <asm/unistd.h>
#include "init.h"
J
Jeff Dike 已提交
21 22 23
#include "os.h"
#include "mem_user.h"
#include "ptrace_user.h"
L
Linus Torvalds 已提交
24
#include "registers.h"
25
#include "skas.h"
J
Jeff Dike 已提交
26
#include "skas_ptrace.h"
L
Linus Torvalds 已提交
27

28
static void ptrace_child(void)
L
Linus Torvalds 已提交
29 30
{
	int ret;
31
	/* Calling os_getpid because some libcs cached getpid incorrectly */
L
Linus Torvalds 已提交
32 33 34
	int pid = os_getpid(), ppid = getppid();
	int sc_result;

35 36
	if (change_sig(SIGWINCH, 0) < 0 ||
	    ptrace(PTRACE_TRACEME, 0, 0, 0) < 0) {
L
Linus Torvalds 已提交
37
		perror("ptrace");
38
		kill(pid, SIGKILL);
L
Linus Torvalds 已提交
39
	}
40
	kill(pid, SIGSTOP);
L
Linus Torvalds 已提交
41

J
Jeff Dike 已提交
42 43 44 45
	/*
	 * This syscall will be intercepted by the parent. Don't call more than
	 * once, please.
	 */
L
Linus Torvalds 已提交
46 47 48
	sc_result = os_getpid();

	if (sc_result == pid)
J
Jeff Dike 已提交
49 50
		/* Nothing modified by the parent, we are running normally. */
		ret = 1;
L
Linus Torvalds 已提交
51
	else if (sc_result == ppid)
J
Jeff Dike 已提交
52 53 54 55 56
		/*
		 * Expected in check_ptrace and check_sysemu when they succeed
		 * in modifying the stack frame
		 */
		ret = 0;
L
Linus Torvalds 已提交
57
	else
J
Jeff Dike 已提交
58 59 60 61 62
		/* Serious trouble! This could be caused by a bug in host 2.6
		 * SKAS3/2.6 patch before release -V6, together with a bug in
		 * the UML code itself.
		 */
		ret = 2;
J
Jeff Dike 已提交
63 64

	exit(ret);
L
Linus Torvalds 已提交
65 66
}

67
static void fatal_perror(const char *str)
68 69 70 71 72 73 74 75 76 77
{
	perror(str);
	exit(1);
}

static void fatal(char *fmt, ...)
{
	va_list list;

	va_start(list, fmt);
78
	vfprintf(stderr, fmt, list);
79 80 81 82 83 84 85 86 87 88
	va_end(list);

	exit(1);
}

static void non_fatal(char *fmt, ...)
{
	va_list list;

	va_start(list, fmt);
89
	vfprintf(stderr, fmt, list);
90 91 92
	va_end(list);
}

J
Jeff Dike 已提交
93
static int start_ptraced_child(void)
L
Linus Torvalds 已提交
94 95
{
	int pid, n, status;
96

J
Jeff Dike 已提交
97 98 99 100 101
	pid = fork();
	if (pid == 0)
		ptrace_child();
	else if (pid < 0)
		fatal_perror("start_ptraced_child : fork failed");
J
Jeff Dike 已提交
102

L
Linus Torvalds 已提交
103
	CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
J
Jeff Dike 已提交
104
	if (n < 0)
J
Jeff Dike 已提交
105
		fatal_perror("check_ptrace : waitpid failed");
J
Jeff Dike 已提交
106
	if (!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGSTOP))
107
		fatal("check_ptrace : expected SIGSTOP, got status = %d",
L
Linus Torvalds 已提交
108 109
		      status);

110
	return pid;
L
Linus Torvalds 已提交
111 112
}

113 114 115
/* When testing for SYSEMU support, if it is one of the broken versions, we
 * must just avoid using sysemu, not panic, but only if SYSEMU features are
 * broken.
L
Linus Torvalds 已提交
116
 * So only for SYSEMU features we test mustpanic, while normal host features
117 118
 * must work anyway!
 */
J
Jeff Dike 已提交
119
static int stop_ptraced_child(int pid, int exitcode, int mustexit)
L
Linus Torvalds 已提交
120 121 122
{
	int status, n, ret = 0;

123 124 125 126
	if (ptrace(PTRACE_CONT, pid, 0, 0) < 0) {
		perror("stop_ptraced_child : ptrace failed");
		return -1;
	}
L
Linus Torvalds 已提交
127
	CATCH_EINTR(n = waitpid(pid, &status, 0));
J
Jeff Dike 已提交
128
	if (!WIFEXITED(status) || (WEXITSTATUS(status) != exitcode)) {
L
Linus Torvalds 已提交
129 130
		int exit_with = WEXITSTATUS(status);
		if (exit_with == 2)
131
			non_fatal("check_ptrace : child exited with status 2. "
J
Jeff Dike 已提交
132
				  "\nDisabling SYSEMU support.\n");
133 134 135 136 137
		non_fatal("check_ptrace : child exited with exitcode %d, while "
			  "expecting %d; status 0x%x\n", exit_with,
			  exitcode, status);
		if (mustexit)
			exit(1);
L
Linus Torvalds 已提交
138 139 140 141 142 143
		ret = -1;
	}

	return ret;
}

144
/* Changed only during early boot */
145 146 147 148 149 150 151 152 153 154 155 156 157
int ptrace_faultinfo;
static int disable_ptrace_faultinfo;

int ptrace_ldt;
static int disable_ptrace_ldt;

int proc_mm;
static int disable_proc_mm;

int have_switch_mm;
static int disable_switch_mm;

int skas_needs_stub;
158 159 160

static int __init skas0_cmd_param(char *str, int* add)
{
161 162 163 164 165
	disable_ptrace_faultinfo = 1;
	disable_ptrace_ldt = 1;
	disable_proc_mm = 1;
	disable_switch_mm = 1;

166 167 168
	return 0;
}

169 170 171 172 173
/* The two __uml_setup would conflict, without this stupid alias. */

static int __init mode_skas0_cmd_param(char *str, int* add)
	__attribute__((alias("skas0_cmd_param")));

174
__uml_setup("skas0", skas0_cmd_param,
175 176
"skas0\n"
"    Disables SKAS3 and SKAS4 usage, so that SKAS0 is used\n\n");
177

178
__uml_setup("mode=skas0", mode_skas0_cmd_param,
179 180
"mode=skas0\n"
"    Disables SKAS3 and SKAS4 usage, so that SKAS0 is used.\n\n");
181

182
/* Changed only during early boot */
183 184
static int force_sysemu_disabled = 0;

L
Linus Torvalds 已提交
185 186 187 188 189 190 191
static int __init nosysemu_cmd_param(char *str, int* add)
{
	force_sysemu_disabled = 1;
	return 0;
}

__uml_setup("nosysemu", nosysemu_cmd_param,
192 193 194 195 196 197 198
"nosysemu\n"
"    Turns off syscall emulation patch for ptrace (SYSEMU) on.\n"
"    SYSEMU is a performance-patch introduced by Laurent Vivier. It changes\n"
"    behaviour of ptrace() and helps reducing host context switch rate.\n"
"    To make it working, you need a kernel patch for your host, too.\n"
"    See http://perso.wanadoo.fr/laurent.vivier/UML/ for further \n"
"    information.\n\n");
L
Linus Torvalds 已提交
199 200 201

static void __init check_sysemu(void)
{
J
Jeff Dike 已提交
202
	unsigned long regs[MAX_REG_NR];
203
	int pid, n, status, count=0;
L
Linus Torvalds 已提交
204

205
	non_fatal("Checking syscall emulation patch for ptrace...");
L
Linus Torvalds 已提交
206
	sysemu_supported = 0;
J
Jeff Dike 已提交
207
	pid = start_ptraced_child();
L
Linus Torvalds 已提交
208

J
Jeff Dike 已提交
209
	if (ptrace(PTRACE_SYSEMU, pid, 0, 0) < 0)
L
Linus Torvalds 已提交
210 211 212 213
		goto fail;

	CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
	if (n < 0)
214
		fatal_perror("check_sysemu : wait failed");
J
Jeff Dike 已提交
215
	if (!WIFSTOPPED(status) || (WSTOPSIG(status) != SIGTRAP))
216
		fatal("check_sysemu : expected SIGTRAP, got status = %d\n",
217
		      status);
L
Linus Torvalds 已提交
218

J
Jeff Dike 已提交
219
	if (ptrace(PTRACE_GETREGS, pid, 0, regs) < 0)
J
Jeff Dike 已提交
220
		fatal_perror("check_sysemu : PTRACE_GETREGS failed");
J
Jeff Dike 已提交
221
	if (PT_SYSCALL_NR(regs) != __NR_getpid) {
J
Jeff Dike 已提交
222 223 224 225 226 227
		non_fatal("check_sysemu got system call number %d, "
			  "expected %d...", PT_SYSCALL_NR(regs), __NR_getpid);
		goto fail;
	}

	n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET, os_getpid());
J
Jeff Dike 已提交
228
	if (n < 0) {
J
Jeff Dike 已提交
229 230 231 232
		non_fatal("check_sysemu : failed to modify system call "
			  "return");
		goto fail;
	}
L
Linus Torvalds 已提交
233

J
Jeff Dike 已提交
234
	if (stop_ptraced_child(pid, 0, 0) < 0)
L
Linus Torvalds 已提交
235 236 237
		goto fail_stopped;

	sysemu_supported = 1;
238
	non_fatal("OK\n");
L
Linus Torvalds 已提交
239 240
	set_using_sysemu(!force_sysemu_disabled);

241
	non_fatal("Checking advanced syscall emulation patch for ptrace...");
J
Jeff Dike 已提交
242
	pid = start_ptraced_child();
243

J
Jeff Dike 已提交
244
	if ((ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
245
		   (void *) PTRACE_O_TRACESYSGOOD) < 0))
W
WANG Cong 已提交
246
		fatal_perror("check_sysemu: PTRACE_OLDSETOPTIONS failed");
247

J
Jeff Dike 已提交
248
	while (1) {
L
Linus Torvalds 已提交
249
		count++;
J
Jeff Dike 已提交
250
		if (ptrace(PTRACE_SYSEMU_SINGLESTEP, pid, 0, 0) < 0)
L
Linus Torvalds 已提交
251 252
			goto fail;
		CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
J
Jeff Dike 已提交
253
		if (n < 0)
W
WANG Cong 已提交
254
			fatal_perror("check_sysemu: wait failed");
255

J
Jeff Dike 已提交
256 257
		if (WIFSTOPPED(status) &&
		    (WSTOPSIG(status) == (SIGTRAP|0x80))) {
258
			if (!count) {
W
WANG Cong 已提交
259
				non_fatal("check_sysemu: SYSEMU_SINGLESTEP "
260 261 262
					  "doesn't singlestep");
				goto fail;
			}
L
Linus Torvalds 已提交
263 264
			n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_RET_OFFSET,
				   os_getpid());
J
Jeff Dike 已提交
265
			if (n < 0)
266 267
				fatal_perror("check_sysemu : failed to modify "
					     "system call return");
L
Linus Torvalds 已提交
268 269
			break;
		}
J
Jeff Dike 已提交
270
		else if (WIFSTOPPED(status) && (WSTOPSIG(status) == SIGTRAP))
271
			count++;
272
		else {
W
WANG Cong 已提交
273
			non_fatal("check_sysemu: expected SIGTRAP or "
274 275 276 277
				  "(SIGTRAP | 0x80), got status = %d\n",
				  status);
			goto fail;
		}
L
Linus Torvalds 已提交
278
	}
J
Jeff Dike 已提交
279
	if (stop_ptraced_child(pid, 0, 0) < 0)
L
Linus Torvalds 已提交
280 281 282
		goto fail_stopped;

	sysemu_supported = 2;
283
	non_fatal("OK\n");
L
Linus Torvalds 已提交
284

J
Jeff Dike 已提交
285
	if (!force_sysemu_disabled)
L
Linus Torvalds 已提交
286 287 288 289
		set_using_sysemu(sysemu_supported);
	return;

fail:
J
Jeff Dike 已提交
290
	stop_ptraced_child(pid, 1, 0);
L
Linus Torvalds 已提交
291
fail_stopped:
292
	non_fatal("missing\n");
L
Linus Torvalds 已提交
293 294
}

295
static void __init check_ptrace(void)
L
Linus Torvalds 已提交
296 297 298
{
	int pid, syscall, n, status;

299
	non_fatal("Checking that ptrace can change system call numbers...");
J
Jeff Dike 已提交
300
	pid = start_ptraced_child();
L
Linus Torvalds 已提交
301

J
Jeff Dike 已提交
302
	if ((ptrace(PTRACE_OLDSETOPTIONS, pid, 0,
303 304
		   (void *) PTRACE_O_TRACESYSGOOD) < 0))
		fatal_perror("check_ptrace: PTRACE_OLDSETOPTIONS failed");
L
Linus Torvalds 已提交
305

J
Jeff Dike 已提交
306 307
	while (1) {
		if (ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0)
308 309
			fatal_perror("check_ptrace : ptrace failed");

L
Linus Torvalds 已提交
310
		CATCH_EINTR(n = waitpid(pid, &status, WUNTRACED));
J
Jeff Dike 已提交
311
		if (n < 0)
312 313
			fatal_perror("check_ptrace : wait failed");

J
Jeff Dike 已提交
314
		if (!WIFSTOPPED(status) ||
315 316 317
		   (WSTOPSIG(status) != (SIGTRAP | 0x80)))
			fatal("check_ptrace : expected (SIGTRAP|0x80), "
			       "got status = %d", status);
318

L
Linus Torvalds 已提交
319 320
		syscall = ptrace(PTRACE_PEEKUSR, pid, PT_SYSCALL_NR_OFFSET,
				 0);
J
Jeff Dike 已提交
321
		if (syscall == __NR_getpid) {
L
Linus Torvalds 已提交
322 323
			n = ptrace(PTRACE_POKEUSR, pid, PT_SYSCALL_NR_OFFSET,
				   __NR_getppid);
J
Jeff Dike 已提交
324
			if (n < 0)
325 326
				fatal_perror("check_ptrace : failed to modify "
					     "system call");
L
Linus Torvalds 已提交
327 328 329
			break;
		}
	}
J
Jeff Dike 已提交
330
	stop_ptraced_child(pid, 0, 1);
331
	non_fatal("OK\n");
L
Linus Torvalds 已提交
332 333 334
	check_sysemu();
}

335
extern void check_tmpexec(void);
336

J
Jeff Dike 已提交
337
static void __init check_coredump_limit(void)
J
Jeff Dike 已提交
338 339 340 341
{
	struct rlimit lim;
	int err = getrlimit(RLIMIT_CORE, &lim);

J
Jeff Dike 已提交
342
	if (err) {
J
Jeff Dike 已提交
343 344 345 346 347
		perror("Getting core dump limit");
		return;
	}

	printf("Core dump limits :\n\tsoft - ");
J
Jeff Dike 已提交
348
	if (lim.rlim_cur == RLIM_INFINITY)
J
Jeff Dike 已提交
349 350 351 352
		printf("NONE\n");
	else printf("%lu\n", lim.rlim_cur);

	printf("\thard - ");
J
Jeff Dike 已提交
353
	if (lim.rlim_max == RLIM_INFINITY)
J
Jeff Dike 已提交
354 355 356 357
		printf("NONE\n");
	else printf("%lu\n", lim.rlim_max);
}

J
Jeff Dike 已提交
358
void __init os_early_checks(void)
L
Linus Torvalds 已提交
359
{
J
Jeff Dike 已提交
360 361
	int pid;

J
Jeff Dike 已提交
362 363 364
	/* Print out the core dump limits early */
	check_coredump_limit();

365
	check_ptrace();
366 367 368 369 370

	/* Need to check this early because mmapping happens before the
	 * kernel is running.
	 */
	check_tmpexec();
J
Jeff Dike 已提交
371 372 373 374 375

	pid = start_ptraced_child();
	if (init_registers(pid))
		fatal("Failed to initialize default registers");
	stop_ptraced_child(pid, 1, 1);
L
Linus Torvalds 已提交
376 377
}

378 379
static int __init noprocmm_cmd_param(char *str, int* add)
{
380
	disable_proc_mm = 1;
381 382 383 384 385 386 387 388 389 390 391
	return 0;
}

__uml_setup("noprocmm", noprocmm_cmd_param,
"noprocmm\n"
"    Turns off usage of /proc/mm, even if host supports it.\n"
"    To support /proc/mm, the host needs to be patched using\n"
"    the current skas3 patch.\n\n");

static int __init noptracefaultinfo_cmd_param(char *str, int* add)
{
392
	disable_ptrace_faultinfo = 1;
393 394 395 396 397 398 399 400 401
	return 0;
}

__uml_setup("noptracefaultinfo", noptracefaultinfo_cmd_param,
"noptracefaultinfo\n"
"    Turns off usage of PTRACE_FAULTINFO, even if host supports\n"
"    it. To support PTRACE_FAULTINFO, the host needs to be patched\n"
"    using the current skas3 patch.\n\n");

402 403
static int __init noptraceldt_cmd_param(char *str, int* add)
{
404
	disable_ptrace_ldt = 1;
405 406 407 408 409 410 411 412 413 414
	return 0;
}

__uml_setup("noptraceldt", noptraceldt_cmd_param,
"noptraceldt\n"
"    Turns off usage of PTRACE_LDT, even if host supports it.\n"
"    To support PTRACE_LDT, the host needs to be patched using\n"
"    the current skas3 patch.\n\n");

static inline void check_skas3_ptrace_faultinfo(void)
L
Linus Torvalds 已提交
415 416
{
	struct ptrace_faultinfo fi;
417
	int pid, n;
L
Linus Torvalds 已提交
418

419
	non_fatal("  - PTRACE_FAULTINFO...");
J
Jeff Dike 已提交
420
	pid = start_ptraced_child();
L
Linus Torvalds 已提交
421 422 423

	n = ptrace(PTRACE_FAULTINFO, pid, 0, &fi);
	if (n < 0) {
J
Jeff Dike 已提交
424
		if (errno == EIO)
425
			non_fatal("not found\n");
426
		else
L
Linus Torvalds 已提交
427
			perror("not found");
428 429
	} else if (disable_ptrace_faultinfo)
		non_fatal("found but disabled on command line\n");
430
	else {
431 432
		ptrace_faultinfo = 1;
		non_fatal("found\n");
L
Linus Torvalds 已提交
433 434
	}

J
Jeff Dike 已提交
435
	stop_ptraced_child(pid, 1, 1);
L
Linus Torvalds 已提交
436 437
}

438 439 440 441 442 443 444 445 446 447
static inline void check_skas3_ptrace_ldt(void)
{
#ifdef PTRACE_LDT
	int pid, n;
	unsigned char ldtbuf[40];
	struct ptrace_ldt ldt_op = (struct ptrace_ldt) {
		.func = 2, /* read default ldt */
		.ptr = ldtbuf,
		.bytecount = sizeof(ldtbuf)};

448
	non_fatal("  - PTRACE_LDT...");
J
Jeff Dike 已提交
449
	pid = start_ptraced_child();
450 451 452

	n = ptrace(PTRACE_LDT, pid, 0, (unsigned long) &ldt_op);
	if (n < 0) {
J
Jeff Dike 已提交
453
		if (errno == EIO)
454
			non_fatal("not found\n");
455
		else
456
			perror("not found");
457 458
	} else if (disable_ptrace_ldt)
		non_fatal("found, but use is disabled\n");
459
	else {
460 461
		ptrace_ldt = 1;
		non_fatal("found\n");
462 463
	}

J
Jeff Dike 已提交
464
	stop_ptraced_child(pid, 1, 1);
465 466 467 468
#endif
}

static inline void check_skas3_proc_mm(void)
L
Linus Torvalds 已提交
469
{
470
	non_fatal("  - /proc/mm...");
471
	if (access("/proc/mm", W_OK) < 0)
472
		perror("not found");
473
	else if (disable_proc_mm)
J
Jeff Dike 已提交
474
		non_fatal("found but disabled on command line\n");
475 476 477 478
	else {
		proc_mm = 1;
		non_fatal("found\n");
	}
479 480
}

481
void can_do_skas(void)
482
{
483
	non_fatal("Checking for the skas3 patch in the host:\n");
484 485 486 487 488

	check_skas3_proc_mm();
	check_skas3_ptrace_faultinfo();
	check_skas3_ptrace_ldt();

J
Jeff Dike 已提交
489
	if (!proc_mm || !ptrace_faultinfo || !ptrace_ldt)
490
		skas_needs_stub = 1;
L
Linus Torvalds 已提交
491
}
492 493 494 495

int __init parse_iomem(char *str, int *add)
{
	struct iomem_region *new;
496
	struct stat64 buf;
497
	char *file, *driver;
498
	int fd, size;
499 500 501

	driver = str;
	file = strchr(str,',');
J
Jeff Dike 已提交
502
	if (file == NULL) {
503
		fprintf(stderr, "parse_iomem : failed to parse iomem\n");
504 505 506 507
		goto out;
	}
	*file = '\0';
	file++;
508
	fd = open(file, O_RDWR, 0);
J
Jeff Dike 已提交
509
	if (fd < 0) {
510
		perror("parse_iomem - Couldn't open io file");
511 512 513
		goto out;
	}

J
Jeff Dike 已提交
514
	if (fstat64(fd, &buf) < 0) {
515
		perror("parse_iomem - cannot stat_fd file");
516 517 518 519
		goto out_close;
	}

	new = malloc(sizeof(*new));
J
Jeff Dike 已提交
520
	if (new == NULL) {
521 522 523 524
		perror("Couldn't allocate iomem_region struct");
		goto out_close;
	}

525
	size = (buf.st_size + UM_KERN_PAGE_SIZE) & ~(UM_KERN_PAGE_SIZE - 1);
526 527 528 529 530 531 532 533 534 535

	*new = ((struct iomem_region) { .next		= iomem_regions,
					.driver		= driver,
					.fd		= fd,
					.size		= size,
					.phys		= 0,
					.virt		= 0 });
	iomem_regions = new;
	iomem_size += new->size + UM_KERN_PAGE_SIZE;

536
	return 0;
537
 out_close:
538
	close(fd);
539
 out:
540
	return 1;
541
}