xref: /linux/arch/um/os-Linux/process.c (revision 831c1926ee728c3e747255f7c0f434762e8e863d)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2015 Thomas Meyer (thomas@m3y3r.de)
4  * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
5  */
6 
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <errno.h>
11 #include <signal.h>
12 #include <fcntl.h>
13 #include <sys/mman.h>
14 #include <sys/ptrace.h>
15 #include <sys/prctl.h>
16 #include <sys/wait.h>
17 #include <asm/unistd.h>
18 #include <init.h>
19 #include <longjmp.h>
20 #include <os.h>
21 
os_alarm_process(int pid)22 void os_alarm_process(int pid)
23 {
24 	kill(pid, SIGALRM);
25 }
26 
os_kill_process(int pid,int reap_child)27 void os_kill_process(int pid, int reap_child)
28 {
29 	kill(pid, SIGKILL);
30 	if (reap_child)
31 		CATCH_EINTR(waitpid(pid, NULL, __WALL));
32 }
33 
34 /* Kill off a ptraced child by all means available.  kill it normally first,
35  * then PTRACE_KILL it, then PTRACE_CONT it in case it's in a run state from
36  * which it can't exit directly.
37  */
38 
os_kill_ptraced_process(int pid,int reap_child)39 void os_kill_ptraced_process(int pid, int reap_child)
40 {
41 	kill(pid, SIGKILL);
42 	ptrace(PTRACE_KILL, pid);
43 	ptrace(PTRACE_CONT, pid);
44 	if (reap_child)
45 		CATCH_EINTR(waitpid(pid, NULL, __WALL));
46 }
47 
48 /* Don't use the glibc version, which caches the result in TLS. It misses some
49  * syscalls, and also breaks with clone(), which does not unshare the TLS.
50  */
51 
os_getpid(void)52 int os_getpid(void)
53 {
54 	return syscall(__NR_getpid);
55 }
56 
os_map_memory(void * virt,int fd,unsigned long long off,unsigned long len,int r,int w,int x)57 int os_map_memory(void *virt, int fd, unsigned long long off, unsigned long len,
58 		  int r, int w, int x)
59 {
60 	void *loc;
61 	int prot;
62 
63 	prot = (r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
64 		(x ? PROT_EXEC : 0);
65 
66 	loc = mmap64((void *) virt, len, prot, MAP_SHARED | MAP_FIXED,
67 		     fd, off);
68 	if (loc == MAP_FAILED)
69 		return -errno;
70 	return 0;
71 }
72 
os_protect_memory(void * addr,unsigned long len,int r,int w,int x)73 int os_protect_memory(void *addr, unsigned long len, int r, int w, int x)
74 {
75 	int prot = ((r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
76 		    (x ? PROT_EXEC : 0));
77 
78 	if (mprotect(addr, len, prot) < 0)
79 		return -errno;
80 
81 	return 0;
82 }
83 
os_unmap_memory(void * addr,int len)84 int os_unmap_memory(void *addr, int len)
85 {
86 	int err;
87 
88 	err = munmap(addr, len);
89 	if (err < 0)
90 		return -errno;
91 	return 0;
92 }
93 
94 #ifndef MADV_REMOVE
95 #define MADV_REMOVE KERNEL_MADV_REMOVE
96 #endif
97 
os_drop_memory(void * addr,int length)98 int os_drop_memory(void *addr, int length)
99 {
100 	int err;
101 
102 	err = madvise(addr, length, MADV_REMOVE);
103 	if (err < 0)
104 		err = -errno;
105 	return err;
106 }
107 
can_drop_memory(void)108 int __init can_drop_memory(void)
109 {
110 	void *addr;
111 	int fd, ok = 0;
112 
113 	printk(UM_KERN_INFO "Checking host MADV_REMOVE support...");
114 	fd = create_mem_file(UM_KERN_PAGE_SIZE);
115 	if (fd < 0) {
116 		printk(UM_KERN_ERR "Creating test memory file failed, "
117 		       "err = %d\n", -fd);
118 		goto out;
119 	}
120 
121 	addr = mmap64(NULL, UM_KERN_PAGE_SIZE, PROT_READ | PROT_WRITE,
122 		      MAP_SHARED, fd, 0);
123 	if (addr == MAP_FAILED) {
124 		printk(UM_KERN_ERR "Mapping test memory file failed, "
125 		       "err = %d\n", -errno);
126 		goto out_close;
127 	}
128 
129 	if (madvise(addr, UM_KERN_PAGE_SIZE, MADV_REMOVE) != 0) {
130 		printk(UM_KERN_ERR "MADV_REMOVE failed, err = %d\n", -errno);
131 		goto out_unmap;
132 	}
133 
134 	printk(UM_KERN_CONT "OK\n");
135 	ok = 1;
136 
137 out_unmap:
138 	munmap(addr, UM_KERN_PAGE_SIZE);
139 out_close:
140 	close(fd);
141 out:
142 	return ok;
143 }
144 
os_page_mincore(void * addr)145 static int os_page_mincore(void *addr)
146 {
147 	char vec[2];
148 	int ret;
149 
150 	ret = mincore(addr, UM_KERN_PAGE_SIZE, vec);
151 	if (ret < 0) {
152 		if (errno == ENOMEM || errno == EINVAL)
153 			return 0;
154 		else
155 			return -errno;
156 	}
157 
158 	return vec[0] & 1;
159 }
160 
os_mincore(void * addr,unsigned long len)161 int os_mincore(void *addr, unsigned long len)
162 {
163 	char *vec;
164 	int ret, i;
165 
166 	if (len <= UM_KERN_PAGE_SIZE)
167 		return os_page_mincore(addr);
168 
169 	vec = calloc(1, (len + UM_KERN_PAGE_SIZE - 1) / UM_KERN_PAGE_SIZE);
170 	if (!vec)
171 		return -ENOMEM;
172 
173 	ret = mincore(addr, UM_KERN_PAGE_SIZE, vec);
174 	if (ret < 0) {
175 		if (errno == ENOMEM || errno == EINVAL)
176 			ret = 0;
177 		else
178 			ret = -errno;
179 
180 		goto out;
181 	}
182 
183 	for (i = 0; i < ((len + UM_KERN_PAGE_SIZE - 1) / UM_KERN_PAGE_SIZE); i++) {
184 		if (!(vec[i] & 1)) {
185 			ret = 0;
186 			goto out;
187 		}
188 	}
189 
190 	ret = 1;
191 out:
192 	free(vec);
193 	return ret;
194 }
195 
init_new_thread_signals(void)196 void init_new_thread_signals(void)
197 {
198 	set_handler(SIGSEGV);
199 	set_handler(SIGTRAP);
200 	set_handler(SIGFPE);
201 	set_handler(SIGILL);
202 	set_handler(SIGBUS);
203 	signal(SIGHUP, SIG_IGN);
204 	set_handler(SIGIO);
205 	signal(SIGWINCH, SIG_IGN);
206 }
207 
os_set_pdeathsig(void)208 void os_set_pdeathsig(void)
209 {
210 	prctl(PR_SET_PDEATHSIG, SIGKILL);
211 }
212