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  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include "defs.h"
34 #include <asm/mman.h>
35 #include <sys/mman.h>
36 
37 unsigned long
get_pagesize(void)38 get_pagesize(void)
39 {
40 	static unsigned long pagesize;
41 
42 	if (!pagesize)
43 		pagesize = sysconf(_SC_PAGESIZE);
44 	return pagesize;
45 }
46 
SYS_FUNC(brk)47 SYS_FUNC(brk)
48 {
49 	if (entering(tcp)) {
50 		tprintf("%#lx", tcp->u_arg[0]);
51 	}
52 	return RVAL_HEX;
53 }
54 
55 #include "xlat/mmap_prot.h"
56 #include "xlat/mmap_flags.h"
57 
58 static int
print_mmap(struct tcb * tcp,long * u_arg,unsigned long long offset)59 print_mmap(struct tcb *tcp, long *u_arg, unsigned long long offset)
60 {
61 	if (entering(tcp)) {
62 		/* addr */
63 		if (!u_arg[0])
64 			tprints("NULL, ");
65 		else
66 			tprintf("%#lx, ", u_arg[0]);
67 		/* len */
68 		tprintf("%lu, ", u_arg[1]);
69 		/* prot */
70 		printflags(mmap_prot, u_arg[2], "PROT_???");
71 		tprints(", ");
72 		/* flags */
73 #ifdef MAP_TYPE
74 		printxval(mmap_flags, u_arg[3] & MAP_TYPE, "MAP_???");
75 		addflags(mmap_flags, u_arg[3] & ~MAP_TYPE);
76 #else
77 		printflags(mmap_flags, u_arg[3], "MAP_???");
78 #endif
79 		tprints(", ");
80 		/* fd */
81 		printfd(tcp, u_arg[4]);
82 		/* offset */
83 		tprintf(", %#llx", offset);
84 	}
85 	return RVAL_HEX;
86 }
87 
88 /* Syscall name<->function correspondence is messed up on many arches.
89  * For example:
90  * i386 has __NR_mmap == 90, and it is "old mmap", and
91  * also it has __NR_mmap2 == 192, which is a "new mmap with page offsets".
92  * But x86_64 has just one __NR_mmap == 9, a "new mmap with byte offsets".
93  * Confused? Me too!
94  */
95 
96 /* Params are pointed to by u_arg[0], offset is in bytes */
SYS_FUNC(old_mmap)97 SYS_FUNC(old_mmap)
98 {
99 	long u_arg[6];
100 #if defined(IA64)
101 	/*
102 	 * IA64 processes never call this routine, they only use the
103 	 * new 'sys_mmap' interface. Only IA32 processes come here.
104 	 */
105 	int i;
106 	unsigned narrow_arg[6];
107 	if (umoven(tcp, tcp->u_arg[0], sizeof(narrow_arg), narrow_arg) == -1)
108 		return 0;
109 	for (i = 0; i < 6; i++)
110 		u_arg[i] = (unsigned long) narrow_arg[i];
111 #elif defined(X86_64)
112 	/* We are here only in personality 1 (i386) */
113 	int i;
114 	unsigned narrow_arg[6];
115 	if (umoven(tcp, tcp->u_arg[0], sizeof(narrow_arg), narrow_arg) == -1)
116 		return 0;
117 	for (i = 0; i < 6; ++i)
118 		u_arg[i] = (unsigned long) narrow_arg[i];
119 #else
120 	if (umoven(tcp, tcp->u_arg[0], sizeof(u_arg), u_arg) == -1)
121 		return 0;
122 #endif
123 	return print_mmap(tcp, u_arg, (unsigned long) u_arg[5]);
124 }
125 
126 #if defined(S390)
127 /* Params are pointed to by u_arg[0], offset is in pages */
SYS_FUNC(old_mmap_pgoff)128 SYS_FUNC(old_mmap_pgoff)
129 {
130 	long u_arg[5];
131 	int i;
132 	unsigned narrow_arg[6];
133 	unsigned long long offset;
134 	if (umoven(tcp, tcp->u_arg[0], sizeof(narrow_arg), narrow_arg) == -1)
135 		return 0;
136 	for (i = 0; i < 5; i++)
137 		u_arg[i] = (unsigned long) narrow_arg[i];
138 	offset = narrow_arg[5];
139 	offset *= get_pagesize();
140 	return print_mmap(tcp, u_arg, offset);
141 }
142 #endif
143 
144 /* Params are passed directly, offset is in bytes */
SYS_FUNC(mmap)145 SYS_FUNC(mmap)
146 {
147 	unsigned long long offset = (unsigned long) tcp->u_arg[5];
148 #if defined(LINUX_MIPSN32) || defined(X32)
149 	/* Try test/x32_mmap.c */
150 	offset = tcp->ext_arg[5];
151 #endif
152 	/* Example of kernel-side handling of this variety of mmap:
153 	 * arch/x86/kernel/sys_x86_64.c::SYSCALL_DEFINE6(mmap, ...) calls
154 	 * sys_mmap_pgoff(..., off >> PAGE_SHIFT); i.e. off is in bytes,
155 	 * since the above code converts off to pages.
156 	 */
157 	return print_mmap(tcp, tcp->u_arg, offset);
158 }
159 
160 /* Params are passed directly, offset is in pages */
SYS_FUNC(mmap_pgoff)161 SYS_FUNC(mmap_pgoff)
162 {
163 	/* Try test/mmap_offset_decode.c */
164 	unsigned long long offset;
165 	offset = (unsigned long) tcp->u_arg[5];
166 	offset *= get_pagesize();
167 	return print_mmap(tcp, tcp->u_arg, offset);
168 }
169 
170 /* Params are passed directly, offset is in 4k units */
SYS_FUNC(mmap_4koff)171 SYS_FUNC(mmap_4koff)
172 {
173 	unsigned long long offset;
174 	offset = (unsigned long) tcp->u_arg[5];
175 	offset <<= 12;
176 	return print_mmap(tcp, tcp->u_arg, offset);
177 }
178 
SYS_FUNC(munmap)179 SYS_FUNC(munmap)
180 {
181 	if (entering(tcp)) {
182 		tprintf("%#lx, %lu",
183 			tcp->u_arg[0], tcp->u_arg[1]);
184 	}
185 	return 0;
186 }
187 
SYS_FUNC(mprotect)188 SYS_FUNC(mprotect)
189 {
190 	if (entering(tcp)) {
191 		tprintf("%#lx, %lu, ",
192 			tcp->u_arg[0], tcp->u_arg[1]);
193 		printflags(mmap_prot, tcp->u_arg[2], "PROT_???");
194 	}
195 	return 0;
196 }
197 
198 #include "xlat/mremap_flags.h"
199 
SYS_FUNC(mremap)200 SYS_FUNC(mremap)
201 {
202 	if (entering(tcp)) {
203 		tprintf("%#lx, %lu, %lu, ", tcp->u_arg[0], tcp->u_arg[1],
204 			tcp->u_arg[2]);
205 		printflags(mremap_flags, tcp->u_arg[3], "MREMAP_???");
206 #ifdef MREMAP_FIXED
207 		if ((tcp->u_arg[3] & (MREMAP_MAYMOVE | MREMAP_FIXED)) ==
208 		    (MREMAP_MAYMOVE | MREMAP_FIXED))
209 			tprintf(", %#lx", tcp->u_arg[4]);
210 #endif
211 	}
212 	return RVAL_HEX;
213 }
214 
215 #include "xlat/madvise_cmds.h"
216 
SYS_FUNC(madvise)217 SYS_FUNC(madvise)
218 {
219 	if (entering(tcp)) {
220 		tprintf("%#lx, %lu, ", tcp->u_arg[0], tcp->u_arg[1]);
221 		printxval(madvise_cmds, tcp->u_arg[2], "MADV_???");
222 	}
223 	return 0;
224 }
225 
226 #include "xlat/mlockall_flags.h"
227 
SYS_FUNC(mlockall)228 SYS_FUNC(mlockall)
229 {
230 	if (entering(tcp)) {
231 		printflags(mlockall_flags, tcp->u_arg[0], "MCL_???");
232 	}
233 	return 0;
234 }
235 
236 #include "xlat/mctl_sync.h"
237 
SYS_FUNC(msync)238 SYS_FUNC(msync)
239 {
240 	if (entering(tcp)) {
241 		/* addr */
242 		tprintf("%#lx", tcp->u_arg[0]);
243 		/* len */
244 		tprintf(", %lu, ", tcp->u_arg[1]);
245 		/* flags */
246 		printflags(mctl_sync, tcp->u_arg[2], "MS_???");
247 	}
248 	return 0;
249 }
250 
SYS_FUNC(mincore)251 SYS_FUNC(mincore)
252 {
253 	if (entering(tcp)) {
254 		tprintf("%#lx, %lu, ", tcp->u_arg[0], tcp->u_arg[1]);
255 	} else {
256 		unsigned long i, len;
257 		char *vec = NULL;
258 
259 		len = tcp->u_arg[1];
260 		if (syserror(tcp) || tcp->u_arg[2] == 0 ||
261 			(vec = malloc(len)) == NULL ||
262 			umoven(tcp, tcp->u_arg[2], len, vec) < 0)
263 			tprintf("%#lx", tcp->u_arg[2]);
264 		else {
265 			tprints("[");
266 			for (i = 0; i < len; i++) {
267 				if (abbrev(tcp) && i >= max_strlen) {
268 					tprints("...");
269 					break;
270 				}
271 				tprints((vec[i] & 1) ? "1" : "0");
272 			}
273 			tprints("]");
274 		}
275 		free(vec);
276 	}
277 	return 0;
278 }
279 
280 #if defined(ALPHA) || defined(IA64) || defined(SPARC) || defined(SPARC64)
SYS_FUNC(getpagesize)281 SYS_FUNC(getpagesize)
282 {
283 	if (exiting(tcp))
284 		return RVAL_HEX;
285 	return 0;
286 }
287 #endif
288 
SYS_FUNC(remap_file_pages)289 SYS_FUNC(remap_file_pages)
290 {
291 	if (entering(tcp)) {
292 		tprintf("%#lx, %lu, ", tcp->u_arg[0], tcp->u_arg[1]);
293 		printflags(mmap_prot, tcp->u_arg[2], "PROT_???");
294 		tprintf(", %lu, ", tcp->u_arg[3]);
295 #ifdef MAP_TYPE
296 		printxval(mmap_flags, tcp->u_arg[4] & MAP_TYPE, "MAP_???");
297 		addflags(mmap_flags, tcp->u_arg[4] & ~MAP_TYPE);
298 #else
299 		printflags(mmap_flags, tcp->u_arg[4], "MAP_???");
300 #endif
301 	}
302 	return 0;
303 }
304 
305 #define MPOL_DEFAULT    0
306 #define MPOL_PREFERRED  1
307 #define MPOL_BIND       2
308 #define MPOL_INTERLEAVE 3
309 
310 #define MPOL_F_NODE     (1<<0)
311 #define MPOL_F_ADDR     (1<<1)
312 
313 #define MPOL_MF_STRICT  (1<<0)
314 #define MPOL_MF_MOVE	(1<<1)
315 #define MPOL_MF_MOVE_ALL (1<<2)
316 
317 #include "xlat/policies.h"
318 #include "xlat/mbindflags.h"
319 #include "xlat/mempolicyflags.h"
320 #include "xlat/move_pages_flags.h"
321 
322 static void
get_nodes(struct tcb * tcp,unsigned long ptr,unsigned long maxnodes,int err)323 get_nodes(struct tcb *tcp, unsigned long ptr, unsigned long maxnodes, int err)
324 {
325 	unsigned long nlongs, size, end;
326 
327 	nlongs = (maxnodes + 8 * sizeof(long) - 1) / (8 * sizeof(long));
328 	size = nlongs * sizeof(long);
329 	end = ptr + size;
330 	if (nlongs == 0 || ((err || verbose(tcp)) && (size * 8 == maxnodes)
331 			    && (end > ptr))) {
332 		unsigned long n, cur, abbrev_end;
333 		int failed = 0;
334 
335 		if (abbrev(tcp)) {
336 			abbrev_end = ptr + max_strlen * sizeof(long);
337 			if (abbrev_end < ptr)
338 				abbrev_end = end;
339 		} else {
340 			abbrev_end = end;
341 		}
342 		tprints(", {");
343 		for (cur = ptr; cur < end; cur += sizeof(long)) {
344 			if (cur > ptr)
345 				tprints(", ");
346 			if (cur >= abbrev_end) {
347 				tprints("...");
348 				break;
349 			}
350 			if (umoven(tcp, cur, sizeof(n), &n) < 0) {
351 				tprints("?");
352 				failed = 1;
353 				break;
354 			}
355 			tprintf("%#0*lx", (int) sizeof(long) * 2 + 2, n);
356 		}
357 		tprints("}");
358 		if (failed)
359 			tprintf(" %#lx", ptr);
360 	} else
361 		tprintf(", %#lx", ptr);
362 	tprintf(", %lu", maxnodes);
363 }
364 
SYS_FUNC(mbind)365 SYS_FUNC(mbind)
366 {
367 	if (entering(tcp)) {
368 		tprintf("%#lx, %lu, ", tcp->u_arg[0], tcp->u_arg[1]);
369 		printxval(policies, tcp->u_arg[2], "MPOL_???");
370 		get_nodes(tcp, tcp->u_arg[3], tcp->u_arg[4], 0);
371 		tprints(", ");
372 		printflags(mbindflags, tcp->u_arg[5], "MPOL_???");
373 	}
374 	return 0;
375 }
376 
SYS_FUNC(set_mempolicy)377 SYS_FUNC(set_mempolicy)
378 {
379 	if (entering(tcp)) {
380 		printxval(policies, tcp->u_arg[0], "MPOL_???");
381 		get_nodes(tcp, tcp->u_arg[1], tcp->u_arg[2], 0);
382 	}
383 	return 0;
384 }
385 
SYS_FUNC(get_mempolicy)386 SYS_FUNC(get_mempolicy)
387 {
388 	if (exiting(tcp)) {
389 		int pol;
390 		if (tcp->u_arg[0] == 0)
391 			tprints("NULL");
392 		else if (syserror(tcp) || umove(tcp, tcp->u_arg[0], &pol) < 0)
393 			tprintf("%#lx", tcp->u_arg[0]);
394 		else
395 			printxval(policies, pol, "MPOL_???");
396 		get_nodes(tcp, tcp->u_arg[1], tcp->u_arg[2], syserror(tcp));
397 		tprintf(", %#lx, ", tcp->u_arg[3]);
398 		printflags(mempolicyflags, tcp->u_arg[4], "MPOL_???");
399 	}
400 	return 0;
401 }
402 
SYS_FUNC(migrate_pages)403 SYS_FUNC(migrate_pages)
404 {
405 	if (entering(tcp)) {
406 		tprintf("%ld, ", (long) (pid_t) tcp->u_arg[0]);
407 		get_nodes(tcp, tcp->u_arg[2], tcp->u_arg[1], 0);
408 		tprints(", ");
409 		get_nodes(tcp, tcp->u_arg[3], tcp->u_arg[1], 0);
410 	}
411 	return 0;
412 }
413 
SYS_FUNC(move_pages)414 SYS_FUNC(move_pages)
415 {
416 	if (entering(tcp)) {
417 		unsigned long npages = tcp->u_arg[1];
418 		tprintf("%ld, %lu, ", tcp->u_arg[0], npages);
419 		if (tcp->u_arg[2] == 0)
420 			tprints("NULL, ");
421 		else {
422 			unsigned int i;
423 			long puser = tcp->u_arg[2];
424 			tprints("{");
425 			for (i = 0; i < npages; ++i) {
426 				void *p;
427 				if (i > 0)
428 					tprints(", ");
429 				if (umove(tcp, puser, &p) < 0) {
430 					tprints("???");
431 					break;
432 				}
433 				tprintf("%p", p);
434 				puser += sizeof(void *);
435 			}
436 			tprints("}, ");
437 		}
438 		if (tcp->u_arg[3] == 0)
439 			tprints("NULL, ");
440 		else {
441 			unsigned int i;
442 			long nodeuser = tcp->u_arg[3];
443 			tprints("{");
444 			for (i = 0; i < npages; ++i) {
445 				int node;
446 				if (i > 0)
447 					tprints(", ");
448 				if (umove(tcp, nodeuser, &node) < 0) {
449 					tprints("???");
450 					break;
451 				}
452 				tprintf("%#x", node);
453 				nodeuser += sizeof(int);
454 			}
455 			tprints("}, ");
456 		}
457 	}
458 	if (exiting(tcp)) {
459 		unsigned long npages = tcp->u_arg[1];
460 		if (tcp->u_arg[4] == 0)
461 			tprints("NULL, ");
462 		else {
463 			unsigned int i;
464 			long statususer = tcp->u_arg[4];
465 			tprints("{");
466 			for (i = 0; i < npages; ++i) {
467 				int status;
468 				if (i > 0)
469 					tprints(", ");
470 				if (umove(tcp, statususer, &status) < 0) {
471 					tprints("???");
472 					break;
473 				}
474 				tprintf("%#x", status);
475 				statususer += sizeof(int);
476 			}
477 			tprints("}, ");
478 		}
479 		printflags(move_pages_flags, tcp->u_arg[5], "MPOL_???");
480 	}
481 	return 0;
482 }
483 
484 #if defined(POWERPC)
SYS_FUNC(subpage_prot)485 SYS_FUNC(subpage_prot)
486 {
487 	if (entering(tcp)) {
488 		unsigned long cur, end, abbrev_end, entries;
489 		unsigned int entry;
490 
491 		tprintf("%#lx, %#lx, ", tcp->u_arg[0], tcp->u_arg[1]);
492 		entries = tcp->u_arg[1] >> 16;
493 		if (!entries || !tcp->u_arg[2]) {
494 			tprints("{}");
495 			return 0;
496 		}
497 		cur = tcp->u_arg[2];
498 		end = cur + (sizeof(int) * entries);
499 		if (!verbose(tcp) || end < (unsigned long) tcp->u_arg[2]) {
500 			tprintf("%#lx", tcp->u_arg[2]);
501 			return 0;
502 		}
503 		if (abbrev(tcp)) {
504 			abbrev_end = cur + (sizeof(int) * max_strlen);
505 			if (abbrev_end > end)
506 				abbrev_end = end;
507 		}
508 		else
509 			abbrev_end = end;
510 		tprints("{");
511 		for (; cur < end; cur += sizeof(int)) {
512 			if (cur > (unsigned long) tcp->u_arg[2])
513 				tprints(", ");
514 			if (cur >= abbrev_end) {
515 				tprints("...");
516 				break;
517 			}
518 			if (umove(tcp, cur, &entry) < 0) {
519 				tprintf("??? [%#lx]", cur);
520 				break;
521 			}
522 			else
523 				tprintf("%#08x", entry);
524 		}
525 		tprints("}");
526 	}
527 
528 	return 0;
529 }
530 #endif
531