1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
4 */
5
6 #include <stdarg.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <errno.h>
11 #include <signal.h>
12 #include <string.h>
13 #include <termios.h>
14 #include <sys/wait.h>
15 #include <sys/mman.h>
16 #include <sys/utsname.h>
17 #include <sys/random.h>
18 #include <init.h>
19 #include <os.h>
20
stack_protections(unsigned long address)21 void stack_protections(unsigned long address)
22 {
23 if (mprotect((void *) address, UM_THREAD_SIZE, PROT_READ | PROT_WRITE) < 0)
24 panic("protecting stack failed, errno = %d", errno);
25 }
26
raw(int fd)27 int raw(int fd)
28 {
29 struct termios tt;
30 int err;
31
32 CATCH_EINTR(err = tcgetattr(fd, &tt));
33 if (err < 0)
34 return -errno;
35
36 cfmakeraw(&tt);
37
38 CATCH_EINTR(err = tcsetattr(fd, TCSADRAIN, &tt));
39 if (err < 0)
40 return -errno;
41
42 /*
43 * XXX tcsetattr could have applied only some changes
44 * (and cfmakeraw() is a set of changes)
45 */
46 return 0;
47 }
48
setup_machinename(char * machine_out)49 void setup_machinename(char *machine_out)
50 {
51 struct utsname host;
52
53 uname(&host);
54 #if IS_ENABLED(CONFIG_UML_X86)
55 # if !IS_ENABLED(CONFIG_64BIT)
56 if (!strcmp(host.machine, "x86_64")) {
57 strcpy(machine_out, "i686");
58 return;
59 }
60 # else
61 if (!strcmp(host.machine, "i686")) {
62 strcpy(machine_out, "x86_64");
63 return;
64 }
65 # endif
66 #endif
67 strcpy(machine_out, host.machine);
68 }
69
setup_hostinfo(char * buf,int len)70 void setup_hostinfo(char *buf, int len)
71 {
72 struct utsname host;
73
74 uname(&host);
75 snprintf(buf, len, "%s %s %s %s %s", host.sysname, host.nodename,
76 host.release, host.version, host.machine);
77 }
78
79 /*
80 * We cannot use glibc's abort(). It makes use of tgkill() which
81 * has no effect within UML's kernel threads.
82 * After that glibc would execute an invalid instruction to kill
83 * the calling process and UML crashes with SIGSEGV.
84 */
uml_abort(void)85 static inline void __attribute__ ((noreturn)) uml_abort(void)
86 {
87 sigset_t sig;
88
89 fflush(NULL);
90
91 if (!sigemptyset(&sig) && !sigaddset(&sig, SIGABRT))
92 sigprocmask(SIG_UNBLOCK, &sig, 0);
93
94 for (;;)
95 if (kill(getpid(), SIGABRT) < 0)
96 exit(127);
97 }
98
os_getrandom(void * buf,size_t len,unsigned int flags)99 ssize_t os_getrandom(void *buf, size_t len, unsigned int flags)
100 {
101 return getrandom(buf, len, flags);
102 }
103
104 /*
105 * UML helper threads must not handle SIGWINCH/INT/TERM
106 */
os_fix_helper_signals(void)107 void os_fix_helper_signals(void)
108 {
109 signal(SIGWINCH, SIG_IGN);
110 signal(SIGINT, SIG_DFL);
111 signal(SIGTERM, SIG_DFL);
112 }
113
os_dump_core(void)114 void os_dump_core(void)
115 {
116 int pid;
117
118 signal(SIGSEGV, SIG_DFL);
119
120 /*
121 * We are about to SIGTERM this entire process group to ensure that
122 * nothing is around to run after the kernel exits. The
123 * kernel wants to abort, not die through SIGTERM, so we
124 * ignore it here.
125 */
126
127 signal(SIGTERM, SIG_IGN);
128 kill(0, SIGTERM);
129 /*
130 * Most of the other processes associated with this UML are
131 * likely sTopped, so give them a SIGCONT so they see the
132 * SIGTERM.
133 */
134 kill(0, SIGCONT);
135
136 /*
137 * Now, having sent signals to everyone but us, make sure they
138 * die by ptrace. Processes can survive what's been done to
139 * them so far - the mechanism I understand is receiving a
140 * SIGSEGV and segfaulting immediately upon return. There is
141 * always a SIGSEGV pending, and (I'm guessing) signals are
142 * processed in numeric order so the SIGTERM (signal 15 vs
143 * SIGSEGV being signal 11) is never handled.
144 *
145 * Run a waitpid loop until we get some kind of error.
146 * Hopefully, it's ECHILD, but there's not a lot we can do if
147 * it's something else. Tell os_kill_ptraced_process not to
148 * wait for the child to report its death because there's
149 * nothing reasonable to do if that fails.
150 */
151
152 while ((pid = waitpid(-1, NULL, WNOHANG | __WALL)) > 0)
153 os_kill_ptraced_process(pid, 0);
154
155 uml_abort();
156 }
157
um_early_printk(const char * s,unsigned int n)158 void um_early_printk(const char *s, unsigned int n)
159 {
160 printf("%.*s", n, s);
161 }
162
163 static int quiet_info;
164
quiet_cmd_param(char * str,int * add)165 static int __init quiet_cmd_param(char *str, int *add)
166 {
167 quiet_info = 1;
168 return 0;
169 }
170
171 __uml_setup("quiet", quiet_cmd_param,
172 "quiet\n"
173 " Turns off information messages during boot.\n\n");
174
175 /*
176 * The os_info/os_warn functions will be called by helper threads. These
177 * have a very limited stack size and using the libc formatting functions
178 * may overflow the stack.
179 * So pull in the kernel vscnprintf and use that instead with a fixed
180 * on-stack buffer.
181 */
182 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args);
183
os_info(const char * fmt,...)184 void os_info(const char *fmt, ...)
185 {
186 char buf[256];
187 va_list list;
188 int len;
189
190 if (quiet_info)
191 return;
192
193 va_start(list, fmt);
194 len = vscnprintf(buf, sizeof(buf), fmt, list);
195 fwrite(buf, len, 1, stderr);
196 va_end(list);
197 }
198
os_warn(const char * fmt,...)199 void os_warn(const char *fmt, ...)
200 {
201 char buf[256];
202 va_list list;
203 int len;
204
205 va_start(list, fmt);
206 len = vscnprintf(buf, sizeof(buf), fmt, list);
207 fwrite(buf, len, 1, stderr);
208 va_end(list);
209 }
210