• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
3  * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
4  * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
5  * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
6  * Copyright (c) 2000 PocketPenguins Inc.  Linux for Hitachi SuperH
7  *                    port by Greg Banks <gbanks@pocketpenguins.com>
8  * Copyright (c) 1999-2018 The strace developers.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include "defs.h"
35 #include <linux/mman.h>
36 #include <sys/mman.h>
37 
38 unsigned long
get_pagesize(void)39 get_pagesize(void)
40 {
41 	static unsigned long pagesize;
42 
43 	if (!pagesize)
44 		pagesize = sysconf(_SC_PAGESIZE);
45 	return pagesize;
46 }
47 
SYS_FUNC(brk)48 SYS_FUNC(brk)
49 {
50 	printaddr(tcp->u_arg[0]);
51 
52 	return RVAL_DECODED | RVAL_HEX;
53 }
54 
55 #include "xlat/mmap_prot.h"
56 #include "xlat/mmap_flags.h"
57 
58 #ifndef MAP_HUGE_SHIFT
59 # define MAP_HUGE_SHIFT 26
60 #endif
61 
62 #ifndef MAP_HUGE_MASK
63 # define MAP_HUGE_MASK 0x3f
64 #endif
65 
66 static void
print_mmap_flags(kernel_ulong_t flags)67 print_mmap_flags(kernel_ulong_t flags)
68 {
69 	printxval64(mmap_flags, flags & MAP_TYPE, "MAP_???");
70 	flags &= ~MAP_TYPE;
71 
72 	const unsigned int mask = MAP_HUGE_MASK << MAP_HUGE_SHIFT;
73 	const unsigned int hugetlb_value = flags & mask;
74 
75 	flags &= ~mask;
76 	addflags(mmap_flags, flags);
77 
78 	if (hugetlb_value)
79 		tprintf("|%u<<MAP_HUGE_SHIFT",
80 			hugetlb_value >> MAP_HUGE_SHIFT);
81 }
82 
83 static void
print_mmap(struct tcb * tcp,kernel_ulong_t * u_arg,unsigned long long offset)84 print_mmap(struct tcb *tcp, kernel_ulong_t *u_arg, unsigned long long offset)
85 {
86 	const kernel_ulong_t addr = u_arg[0];
87 	const kernel_ulong_t len = u_arg[1];
88 	const kernel_ulong_t prot = u_arg[2];
89 	const kernel_ulong_t flags = u_arg[3];
90 	const int fd = u_arg[4];
91 
92 	printaddr(addr);
93 	tprintf(", %" PRI_klu ", ", len);
94 	printflags64(mmap_prot, prot, "PROT_???");
95 	tprints(", ");
96 	print_mmap_flags(flags);
97 	tprints(", ");
98 	printfd(tcp, fd);
99 	tprintf(", %#llx", offset);
100 }
101 
102 /* Syscall name<->function correspondence is messed up on many arches.
103  * For example:
104  * i386 has __NR_mmap == 90, and it is "old mmap", and
105  * also it has __NR_mmap2 == 192, which is a "new mmap with page offsets".
106  * But x86_64 has just one __NR_mmap == 9, a "new mmap with byte offsets".
107  * Confused? Me too!
108  */
109 
110 #if HAVE_ARCH_OLD_MMAP
111 /* Params are pointed to by u_arg[0], offset is in bytes */
SYS_FUNC(old_mmap)112 SYS_FUNC(old_mmap)
113 {
114 	kernel_ulong_t *args =
115 		fetch_indirect_syscall_args(tcp, tcp->u_arg[0], 6);
116 
117 	if (args)
118 		print_mmap(tcp, args, args[5]);
119 	else
120 		printaddr(tcp->u_arg[0]);
121 
122 	return RVAL_DECODED | RVAL_HEX;
123 }
124 
125 # if HAVE_ARCH_OLD_MMAP_PGOFF
126 /* Params are pointed to by u_arg[0], offset is in pages */
SYS_FUNC(old_mmap_pgoff)127 SYS_FUNC(old_mmap_pgoff)
128 {
129 	kernel_ulong_t *args =
130 		fetch_indirect_syscall_args(tcp, tcp->u_arg[0], 6);
131 
132 	if (args) {
133 		unsigned long long offset;
134 
135 		offset = args[5];
136 		offset *= get_pagesize();
137 
138 		print_mmap(tcp, args, offset);
139 	} else {
140 		printaddr(tcp->u_arg[0]);
141 	}
142 
143 	return RVAL_DECODED | RVAL_HEX;
144 }
145 # endif /* HAVE_ARCH_OLD_MMAP_PGOFF */
146 #endif /* HAVE_ARCH_OLD_MMAP */
147 
148 /* Params are passed directly, offset is in bytes */
SYS_FUNC(mmap)149 SYS_FUNC(mmap)
150 {
151 	/* Example of kernel-side handling of this variety of mmap:
152 	 * arch/x86/kernel/sys_x86_64.c::SYSCALL_DEFINE6(mmap, ...) calls
153 	 * sys_mmap_pgoff(..., off >> PAGE_SHIFT); i.e. off is in bytes,
154 	 * since the above code converts off to pages.
155 	 */
156 	print_mmap(tcp, tcp->u_arg, tcp->u_arg[5]);
157 
158 	return RVAL_DECODED | RVAL_HEX;
159 }
160 
161 /* Params are passed directly, offset is in pages */
SYS_FUNC(mmap_pgoff)162 SYS_FUNC(mmap_pgoff)
163 {
164 	/* Try test/mmap_offset_decode.c */
165 	unsigned long long offset;
166 	offset = tcp->u_arg[5];
167 	offset *= get_pagesize();
168 	print_mmap(tcp, tcp->u_arg, offset);
169 
170 	return RVAL_DECODED | RVAL_HEX;
171 }
172 
173 /* Params are passed directly, offset is in 4k units */
SYS_FUNC(mmap_4koff)174 SYS_FUNC(mmap_4koff)
175 {
176 	unsigned long long offset;
177 	offset = tcp->u_arg[5];
178 	offset <<= 12;
179 	print_mmap(tcp, tcp->u_arg, offset);
180 
181 	return RVAL_DECODED | RVAL_HEX;
182 }
183 
SYS_FUNC(munmap)184 SYS_FUNC(munmap)
185 {
186 	printaddr(tcp->u_arg[0]);
187 	tprintf(", %" PRI_klu, tcp->u_arg[1]);
188 
189 	return RVAL_DECODED;
190 }
191 
192 static int
do_mprotect(struct tcb * tcp,bool has_pkey)193 do_mprotect(struct tcb *tcp, bool has_pkey)
194 {
195 	printaddr(tcp->u_arg[0]);
196 	tprintf(", %" PRI_klu ", ", tcp->u_arg[1]);
197 	printflags64(mmap_prot, tcp->u_arg[2], "PROT_???");
198 
199 	if (has_pkey)
200 		tprintf(", %d", (int) tcp->u_arg[3]);
201 
202 	return RVAL_DECODED;
203 }
204 
SYS_FUNC(mprotect)205 SYS_FUNC(mprotect)
206 {
207 	return do_mprotect(tcp, false);
208 }
209 
SYS_FUNC(pkey_mprotect)210 SYS_FUNC(pkey_mprotect)
211 {
212 	return do_mprotect(tcp, true);
213 }
214 
215 #include "xlat/mremap_flags.h"
216 
SYS_FUNC(mremap)217 SYS_FUNC(mremap)
218 {
219 	printaddr(tcp->u_arg[0]);
220 	tprintf(", %" PRI_klu ", %" PRI_klu ", ", tcp->u_arg[1], tcp->u_arg[2]);
221 	printflags64(mremap_flags, tcp->u_arg[3], "MREMAP_???");
222 #ifdef MREMAP_FIXED
223 	if ((tcp->u_arg[3] & (MREMAP_MAYMOVE | MREMAP_FIXED)) ==
224 	    (MREMAP_MAYMOVE | MREMAP_FIXED)) {
225 		tprints(", ");
226 		printaddr(tcp->u_arg[4]);
227 	}
228 #endif
229 	return RVAL_DECODED | RVAL_HEX;
230 }
231 
232 #include "xlat/madvise_cmds.h"
233 
SYS_FUNC(madvise)234 SYS_FUNC(madvise)
235 {
236 	printaddr(tcp->u_arg[0]);
237 	tprintf(", %" PRI_klu ", ", tcp->u_arg[1]);
238 	printxval(madvise_cmds, tcp->u_arg[2], "MADV_???");
239 
240 	return RVAL_DECODED;
241 }
242 
243 #include "xlat/mlockall_flags.h"
244 
SYS_FUNC(mlockall)245 SYS_FUNC(mlockall)
246 {
247 	printflags(mlockall_flags, tcp->u_arg[0], "MCL_???");
248 
249 	return RVAL_DECODED;
250 }
251 
252 #include "xlat/mctl_sync.h"
253 
SYS_FUNC(msync)254 SYS_FUNC(msync)
255 {
256 	/* addr */
257 	printaddr(tcp->u_arg[0]);
258 	/* len */
259 	tprintf(", %" PRI_klu ", ", tcp->u_arg[1]);
260 	/* flags */
261 	printflags(mctl_sync, tcp->u_arg[2], "MS_???");
262 
263 	return RVAL_DECODED;
264 }
265 
266 #include "xlat/mlock_flags.h"
267 
SYS_FUNC(mlock2)268 SYS_FUNC(mlock2)
269 {
270 	printaddr(tcp->u_arg[0]);
271 	tprintf(", %" PRI_klu ", ", tcp->u_arg[1]);
272 	printflags(mlock_flags, tcp->u_arg[2], "MLOCK_???");
273 
274 	return RVAL_DECODED;
275 }
276 
SYS_FUNC(mincore)277 SYS_FUNC(mincore)
278 {
279 	if (entering(tcp)) {
280 		printaddr(tcp->u_arg[0]);
281 		tprintf(", %" PRI_klu ", ", tcp->u_arg[1]);
282 	} else {
283 		const unsigned long page_size = get_pagesize();
284 		const unsigned long page_mask = page_size - 1;
285 		unsigned long len = tcp->u_arg[1];
286 		unsigned char *vec = NULL;
287 
288 		len = len / page_size + (len & page_mask ? 1 : 0);
289 		if (syserror(tcp) || !verbose(tcp) ||
290 		    !tcp->u_arg[2] || !(vec = malloc(len)) ||
291 		    umoven(tcp, tcp->u_arg[2], len, vec) < 0)
292 			printaddr(tcp->u_arg[2]);
293 		else {
294 			unsigned long i;
295 			tprints("[");
296 			for (i = 0; i < len; i++) {
297 				if (i)
298 					tprints(", ");
299 				if (abbrev(tcp) && i >= max_strlen) {
300 					tprints("...");
301 					break;
302 				}
303 				tprints((vec[i] & 1) ? "1" : "0");
304 			}
305 			tprints("]");
306 		}
307 		free(vec);
308 	}
309 	return 0;
310 }
311 
SYS_FUNC(remap_file_pages)312 SYS_FUNC(remap_file_pages)
313 {
314 	const kernel_ulong_t addr = tcp->u_arg[0];
315 	const kernel_ulong_t size = tcp->u_arg[1];
316 	const kernel_ulong_t prot = tcp->u_arg[2];
317 	const kernel_ulong_t pgoff = tcp->u_arg[3];
318 	const kernel_ulong_t flags = tcp->u_arg[4];
319 
320 	printaddr(addr);
321 	tprintf(", %" PRI_klu ", ", size);
322 	printflags64(mmap_prot, prot, "PROT_???");
323 	tprintf(", %" PRI_klu ", ", pgoff);
324 	print_mmap_flags(flags);
325 
326 	return RVAL_DECODED;
327 }
328 
329 #if defined(POWERPC)
330 static bool
print_protmap_entry(struct tcb * tcp,void * elem_buf,size_t elem_size,void * data)331 print_protmap_entry(struct tcb *tcp, void *elem_buf, size_t elem_size, void *data)
332 {
333 	tprintf("%#08x", *(unsigned int *) elem_buf);
334 
335 	return true;
336 }
337 
SYS_FUNC(subpage_prot)338 SYS_FUNC(subpage_prot)
339 {
340 	kernel_ulong_t addr = tcp->u_arg[0];
341 	kernel_ulong_t len = tcp->u_arg[1];
342 	kernel_ulong_t nmemb = len >> 16;
343 	kernel_ulong_t map = tcp->u_arg[2];
344 
345 	printaddr(addr);
346 	tprintf(", %" PRI_klu ", ", len);
347 
348 	unsigned int entry;
349 	print_array(tcp, map, nmemb, &entry, sizeof(entry),
350 		    umoven_or_printaddr, print_protmap_entry, 0);
351 
352 	return RVAL_DECODED;
353 }
354 #endif
355