xref: /openbmc/linux/arch/um/os-Linux/util.c (revision de2fe5e0)
1 /*
2  * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
3  * Licensed under the GPL
4  */
5 
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <limits.h>
10 #include <setjmp.h>
11 #include <sys/mman.h>
12 #include <sys/stat.h>
13 #include <sys/utsname.h>
14 #include <sys/param.h>
15 #include <sys/time.h>
16 #include "asm/types.h"
17 #include <ctype.h>
18 #include <signal.h>
19 #include <wait.h>
20 #include <errno.h>
21 #include <stdarg.h>
22 #include <sched.h>
23 #include <termios.h>
24 #include <string.h>
25 #include "user_util.h"
26 #include "kern_util.h"
27 #include "user.h"
28 #include "mem_user.h"
29 #include "init.h"
30 #include "ptrace_user.h"
31 #include "uml-config.h"
32 #include "os.h"
33 #include "longjmp.h"
34 
35 void stack_protections(unsigned long address)
36 {
37 	int prot = PROT_READ | PROT_WRITE | PROT_EXEC;
38 
39 	if(mprotect((void *) address, page_size(), prot) < 0)
40 		panic("protecting stack failed, errno = %d", errno);
41 }
42 
43 void task_protections(unsigned long address)
44 {
45 	unsigned long guard = address + page_size();
46 	unsigned long stack = guard + page_size();
47 	int prot = 0, pages;
48 
49 #ifdef notdef
50 	if(mprotect((void *) stack, page_size(), prot) < 0)
51 		panic("protecting guard page failed, errno = %d", errno);
52 #endif
53 	pages = (1 << UML_CONFIG_KERNEL_STACK_ORDER) - 2;
54 	prot = PROT_READ | PROT_WRITE | PROT_EXEC;
55 	if(mprotect((void *) stack, pages * page_size(), prot) < 0)
56 		panic("protecting stack failed, errno = %d", errno);
57 }
58 
59 int raw(int fd)
60 {
61 	struct termios tt;
62 	int err;
63 
64 	CATCH_EINTR(err = tcgetattr(fd, &tt));
65 	if(err < 0)
66 		return -errno;
67 
68 	cfmakeraw(&tt);
69 
70 	CATCH_EINTR(err = tcsetattr(fd, TCSADRAIN, &tt));
71 	if(err < 0)
72 		return -errno;
73 
74 	/* XXX tcsetattr could have applied only some changes
75 	 * (and cfmakeraw() is a set of changes) */
76 	return(0);
77 }
78 
79 void setup_machinename(char *machine_out)
80 {
81 	struct utsname host;
82 
83 	uname(&host);
84 #if defined(UML_CONFIG_UML_X86) && !defined(UML_CONFIG_64BIT)
85 	if (!strcmp(host.machine, "x86_64")) {
86 		strcpy(machine_out, "i686");
87 		return;
88 	}
89 #endif
90 	strcpy(machine_out, host.machine);
91 }
92 
93 char host_info[(_UTSNAME_LENGTH + 1) * 4 + _UTSNAME_NODENAME_LENGTH + 1];
94 
95 void setup_hostinfo(void)
96 {
97 	struct utsname host;
98 
99 	uname(&host);
100 	sprintf(host_info, "%s %s %s %s %s", host.sysname, host.nodename,
101 		host.release, host.version, host.machine);
102 }
103 
104 int setjmp_wrapper(void (*proc)(void *, void *), ...)
105 {
106 	va_list args;
107 	sigjmp_buf buf;
108 	int n;
109 
110 	n = sigsetjmp(buf, 1);
111 	if(n == 0){
112 		va_start(args, proc);
113 		(*proc)(&buf, &args);
114 	}
115 	va_end(args);
116 	return(n);
117 }
118