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