]> granicus.if.org Git - strace/blob - mem.c
Fixes in "new" mmap
[strace] / mem.c
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 #if defined(I386)
37 # include <asm/ldt.h>
38 # ifdef HAVE_STRUCT_USER_DESC
39 #  define modify_ldt_ldt_s user_desc
40 # endif
41 #endif
42 #if defined(SH64)
43 # include <asm/page.h>      /* for PAGE_SHIFT */
44 #endif
45
46 int
47 sys_brk(struct tcb *tcp)
48 {
49         if (entering(tcp)) {
50                 tprintf("%#lx", tcp->u_arg[0]);
51         }
52         return RVAL_HEX;
53 }
54
55 static const struct xlat mmap_prot[] = {
56         { PROT_NONE,    "PROT_NONE",    },
57         { PROT_READ,    "PROT_READ"     },
58         { PROT_WRITE,   "PROT_WRITE"    },
59         { PROT_EXEC,    "PROT_EXEC"     },
60 #ifdef PROT_SEM
61         { PROT_SEM,     "PROT_SEM"      },
62 #endif
63 #ifdef PROT_GROWSDOWN
64         { PROT_GROWSDOWN,"PROT_GROWSDOWN"},
65 #endif
66 #ifdef PROT_GROWSUP
67         { PROT_GROWSUP, "PROT_GROWSUP"  },
68 #endif
69 #ifdef PROT_SAO
70         { PROT_SAO,     "PROT_SAO"      },
71 #endif
72         { 0,            NULL            },
73 };
74
75 static const struct xlat mmap_flags[] = {
76         { MAP_SHARED,   "MAP_SHARED"    },
77         { MAP_PRIVATE,  "MAP_PRIVATE"   },
78         { MAP_FIXED,    "MAP_FIXED"     },
79 #ifdef MAP_ANONYMOUS
80         { MAP_ANONYMOUS,"MAP_ANONYMOUS" },
81 #endif
82 #ifdef MAP_32BIT
83         { MAP_32BIT,    "MAP_32BIT"     },
84 #endif
85 #ifdef MAP_RENAME
86         { MAP_RENAME,   "MAP_RENAME"    },
87 #endif
88 #ifdef MAP_NORESERVE
89         { MAP_NORESERVE,"MAP_NORESERVE" },
90 #endif
91 #ifdef MAP_POPULATE
92         { MAP_POPULATE, "MAP_POPULATE" },
93 #endif
94 #ifdef MAP_NONBLOCK
95         { MAP_NONBLOCK, "MAP_NONBLOCK" },
96 #endif
97         /*
98          * XXX - this was introduced in SunOS 4.x to distinguish between
99          * the old pre-4.x "mmap()", which:
100          *
101          *      only let you map devices with an "mmap" routine (e.g.,
102          *      frame buffers) in;
103          *
104          *      required you to specify the mapping address;
105          *
106          *      returned 0 on success and -1 on failure;
107          *
108          * memory and which, and the 4.x "mmap()" which:
109          *
110          *      can map plain files;
111          *
112          *      can be asked to pick where to map the file;
113          *
114          *      returns the address where it mapped the file on success
115          *      and -1 on failure.
116          *
117          * It's not actually used in source code that calls "mmap()"; the
118          * "mmap()" routine adds it for you.
119          *
120          * It'd be nice to come up with some way of eliminating it from
121          * the flags, e.g. reporting calls *without* it as "old_mmap()"
122          * and calls with it as "mmap()".
123          */
124 #ifdef _MAP_NEW
125         { _MAP_NEW,     "_MAP_NEW"      },
126 #endif
127 #ifdef MAP_GROWSDOWN
128         { MAP_GROWSDOWN,"MAP_GROWSDOWN" },
129 #endif
130 #ifdef MAP_DENYWRITE
131         { MAP_DENYWRITE,"MAP_DENYWRITE" },
132 #endif
133 #ifdef MAP_EXECUTABLE
134         { MAP_EXECUTABLE,"MAP_EXECUTABLE"},
135 #endif
136 #ifdef MAP_INHERIT
137         { MAP_INHERIT,  "MAP_INHERIT"   },
138 #endif
139 #ifdef MAP_FILE
140         { MAP_FILE,     "MAP_FILE"      },
141 #endif
142 #ifdef MAP_LOCKED
143         { MAP_LOCKED,   "MAP_LOCKED"    },
144 #endif
145         /* FreeBSD ones */
146 #if defined(MAP_ANON) && (!defined(MAP_ANONYMOUS) || MAP_ANON != MAP_ANONYMOUS)
147         { MAP_ANON,     "MAP_ANON"      },
148 #endif
149 #ifdef MAP_HASSEMAPHORE
150         { MAP_HASSEMAPHORE,"MAP_HASSEMAPHORE"},
151 #endif
152 #ifdef MAP_STACK
153         { MAP_STACK,    "MAP_STACK"     },
154 #endif
155 #if defined MAP_UNINITIALIZED && MAP_UNINITIALIZED > 0
156         { MAP_UNINITIALIZED,"MAP_UNINITIALIZED"},
157 #endif
158 #ifdef MAP_NOSYNC
159         { MAP_NOSYNC,   "MAP_NOSYNC"    },
160 #endif
161 #ifdef MAP_NOCORE
162         { MAP_NOCORE,   "MAP_NOCORE"    },
163 #endif
164 #ifdef TILE
165         { MAP_CACHE_NO_LOCAL, "MAP_CACHE_NO_LOCAL" },
166         { MAP_CACHE_NO_L2, "MAP_CACHE_NO_L2" },
167         { MAP_CACHE_NO_L1, "MAP_CACHE_NO_L1" },
168 #endif
169         { 0,            NULL            },
170 };
171
172 #ifdef TILE
173 static int
174 addtileflags(long flags)
175 {
176         long home = flags & _MAP_CACHE_MKHOME(_MAP_CACHE_HOME_MASK);
177         flags &= ~_MAP_CACHE_MKHOME(_MAP_CACHE_HOME_MASK);
178
179         if (flags & _MAP_CACHE_INCOHERENT) {
180                 flags &= ~_MAP_CACHE_INCOHERENT;
181                 if (home == MAP_CACHE_HOME_NONE) {
182                         tprints("|MAP_CACHE_INCOHERENT");
183                         return flags;
184                 }
185                 tprints("|_MAP_CACHE_INCOHERENT");
186         }
187
188         switch (home) {
189         case 0: break;
190         case MAP_CACHE_HOME_HERE: tprints("|MAP_CACHE_HOME_HERE"); break;
191         case MAP_CACHE_HOME_NONE: tprints("|MAP_CACHE_HOME_NONE"); break;
192         case MAP_CACHE_HOME_SINGLE: tprints("|MAP_CACHE_HOME_SINGLE"); break;
193         case MAP_CACHE_HOME_TASK: tprints("|MAP_CACHE_HOME_TASK"); break;
194         case MAP_CACHE_HOME_HASH: tprints("|MAP_CACHE_HOME_HASH"); break;
195         default:
196                 tprintf("|MAP_CACHE_HOME(%ld)",
197                         (home >> _MAP_CACHE_HOME_SHIFT) );
198                 break;
199         }
200
201         return flags;
202 }
203 #endif
204
205 #if !HAVE_LONG_LONG_OFF_T
206 static int
207 print_mmap(struct tcb *tcp, long *u_arg, unsigned long long offset)
208 {
209         if (entering(tcp)) {
210                 /* addr */
211                 if (!u_arg[0])
212                         tprints("NULL, ");
213                 else
214                         tprintf("%#lx, ", u_arg[0]);
215                 /* len */
216                 tprintf("%lu, ", u_arg[1]);
217                 /* prot */
218                 printflags(mmap_prot, u_arg[2], "PROT_???");
219                 tprints(", ");
220                 /* flags */
221 #ifdef MAP_TYPE
222                 printxval(mmap_flags, u_arg[3] & MAP_TYPE, "MAP_???");
223 # ifdef TILE
224                 addflags(mmap_flags, addtileflags(u_arg[3] & ~MAP_TYPE));
225 # else
226                 addflags(mmap_flags, u_arg[3] & ~MAP_TYPE);
227 # endif
228 #else
229                 printflags(mmap_flags, u_arg[3], "MAP_???");
230 #endif
231                 tprints(", ");
232                 /* fd */
233                 printfd(tcp, u_arg[4]);
234                 /* offset */
235                 tprintf(", %#llx", offset);
236         }
237         return RVAL_HEX;
238 }
239
240 int sys_old_mmap(struct tcb *tcp)
241 {
242 #if defined(IA64)
243         /*
244          * IA64 processes never call this routine, they only use the
245          * new `sys_mmap' interface.
246          * For IA32 processes, this code converts the integer arguments
247          * that they pushed onto the stack, into longs.
248          */
249         int i;
250         long u_arg[6];
251         unsigned narrow_arg[6];
252         if (umoven(tcp, tcp->u_arg[0], sizeof(narrow_arg), (char *) narrow_arg) == -1)
253                 return 0;
254         for (i = 0; i < 6; i++)
255                 u_arg[i] = narrow_arg[i];
256 #elif defined(SH) || defined(SH64)
257         /* SH has always passed the args in registers */
258         long *u_arg = tcp->u_arg;
259 #elif defined(X86_64)
260         long u_arg[6];
261         if (current_personality == 1) {
262                 int i;
263                 unsigned narrow_arg[6];
264                 if (umoven(tcp, tcp->u_arg[0], sizeof(narrow_arg), (char *) narrow_arg) == -1)
265                         return 0;
266                 for (i = 0; i < 6; ++i)
267                         u_arg[i] = narrow_arg[i];
268         } else {
269                 if (umoven(tcp, tcp->u_arg[0], sizeof(u_arg), (char *) u_arg) == -1)
270                         return 0;
271         }
272 #else
273         long u_arg[6];
274         if (umoven(tcp, tcp->u_arg[0], sizeof(u_arg), (char *) u_arg) == -1)
275                 return 0;
276 #endif
277         return print_mmap(tcp, u_arg, (unsigned long)u_arg[5]);
278 }
279
280 int
281 sys_mmap(struct tcb *tcp)
282 {
283         unsigned long long offset = (unsigned long) tcp->u_arg[5];
284
285 #if defined(SH64)
286         /*
287          * Old mmap differs from new mmap in specifying the
288          * offset in units of bytes rather than pages.  We
289          * pretend it's in byte units so the user only ever
290          * sees bytes in the printout.
291          */
292         offset <<= PAGE_SHIFT;
293 #elif defined(I386)
294         /* Try test/mmap_offset_decode.c */
295         offset <<= 12; /* 4096 byte pages */
296 #elif defined(LINUX_MIPSN32) || defined(X32)
297         /* Try test/x32_mmap.c */
298         /* At least for X32 it definitely should not be page-shifted! */
299         offset = tcp->ext_arg[5];
300 #endif
301         return print_mmap(tcp, tcp->u_arg, offset);
302 }
303 #endif /* !HAVE_LONG_LONG_OFF_T */
304
305 #if _LFS64_LARGEFILE || HAVE_LONG_LONG_OFF_T
306 /* TODO: comment which arches use this routine.
307  * For one, does ALPHA on Linux use this??
308  * From code it seems that it might use 7 or 8 registers,
309  * which is strange - Linux syscalls can pass maximum of 6 parameters!
310  */
311 # ifdef HAVE_LONG_LONG_OFF_T
312 /* For systems that have a long long off_t,
313  * sys_mmap in syscall tables is handled by sys_mmap64:
314  */
315 #  define sys_mmap64 sys_mmap
316 # endif
317 int
318 sys_mmap64(struct tcb *tcp)
319 {
320         if (entering(tcp)) {
321 #if defined(ALPHA) || defined(X32)
322                 long *u_arg = tcp->u_arg;
323 #else
324                 long u_arg[7];
325                 if (umoven(tcp, tcp->u_arg[0], sizeof u_arg,
326                                 (char *) u_arg) == -1)
327                         return 0;
328 #endif
329                 /* addr */
330                 if (!u_arg[0])
331                         tprints("NULL, ");
332                 else
333                         tprintf("%#lx, ", u_arg[0]);
334                 /* len */
335                 tprintf("%lu, ", u_arg[1]);
336                 /* prot */
337                 printflags(mmap_prot, u_arg[2], "PROT_???");
338                 tprints(", ");
339                 /* flags */
340 #ifdef MAP_TYPE
341                 printxval(mmap_flags, u_arg[3] & MAP_TYPE, "MAP_???");
342                 addflags(mmap_flags, u_arg[3] & ~MAP_TYPE);
343 #else
344                 printflags(mmap_flags, u_arg[3], "MAP_???");
345 #endif
346                 /* fd */
347                 tprints(", ");
348                 printfd(tcp, u_arg[4]);
349                 /* offset */
350 #if defined(ALPHA) || defined(X32)
351                 printllval(tcp, ", %#llx", 5);
352 #else
353                 /* NOTE: not verified that [5] and [6] should be used.
354                  * It's possible that long long is 64-bit aligned in memory
355                  * and we need to use [6] and [7] here instead:
356                  */
357                 tprintf(", %#llx", LONG_LONG(u_arg[5], u_arg[6]));
358 #endif
359         }
360         return RVAL_HEX;
361 }
362 #endif /* _LFS64_LARGEFILE || HAVE_LONG_LONG_OFF_T */
363
364 int
365 sys_munmap(struct tcb *tcp)
366 {
367         if (entering(tcp)) {
368                 tprintf("%#lx, %lu",
369                         tcp->u_arg[0], tcp->u_arg[1]);
370         }
371         return 0;
372 }
373
374 int
375 sys_mprotect(struct tcb *tcp)
376 {
377         if (entering(tcp)) {
378                 tprintf("%#lx, %lu, ",
379                         tcp->u_arg[0], tcp->u_arg[1]);
380                 printflags(mmap_prot, tcp->u_arg[2], "PROT_???");
381         }
382         return 0;
383 }
384
385 static const struct xlat mremap_flags[] = {
386         { MREMAP_MAYMOVE,       "MREMAP_MAYMOVE"        },
387 #ifdef MREMAP_FIXED
388         { MREMAP_FIXED,         "MREMAP_FIXED"          },
389 #endif
390         { 0,                    NULL                    }
391 };
392
393 int
394 sys_mremap(struct tcb *tcp)
395 {
396         if (entering(tcp)) {
397                 tprintf("%#lx, %lu, %lu, ", tcp->u_arg[0], tcp->u_arg[1],
398                         tcp->u_arg[2]);
399                 printflags(mremap_flags, tcp->u_arg[3], "MREMAP_???");
400 #ifdef MREMAP_FIXED
401                 if ((tcp->u_arg[3] & (MREMAP_MAYMOVE | MREMAP_FIXED)) ==
402                     (MREMAP_MAYMOVE | MREMAP_FIXED))
403                         tprintf(", %#lx", tcp->u_arg[4]);
404 #endif
405         }
406         return RVAL_HEX;
407 }
408
409 static const struct xlat madvise_cmds[] = {
410 #ifdef MADV_NORMAL
411         { MADV_NORMAL,          "MADV_NORMAL" },
412 #endif
413 #ifdef MADV_RANDOM
414         { MADV_RANDOM,          "MADV_RANDOM" },
415 #endif
416 #ifdef MADV_SEQUENTIAL
417         { MADV_SEQUENTIAL,      "MADV_SEQUENTIAL" },
418 #endif
419 #ifdef MADV_WILLNEED
420         { MADV_WILLNEED,        "MADV_WILLNEED" },
421 #endif
422 #ifdef MADV_DONTNEED
423         { MADV_DONTNEED,        "MADV_DONTNEED" },
424 #endif
425         { 0,                    NULL },
426 };
427
428 int
429 sys_madvise(struct tcb *tcp)
430 {
431         if (entering(tcp)) {
432                 tprintf("%#lx, %lu, ", tcp->u_arg[0], tcp->u_arg[1]);
433                 printxval(madvise_cmds, tcp->u_arg[2], "MADV_???");
434         }
435         return 0;
436 }
437
438 static const struct xlat mlockall_flags[] = {
439 #ifdef MCL_CURRENT
440         { MCL_CURRENT,  "MCL_CURRENT" },
441 #endif
442 #ifdef MCL_FUTURE
443         { MCL_FUTURE,   "MCL_FUTURE" },
444 #endif
445         { 0,            NULL}
446 };
447
448 int
449 sys_mlockall(struct tcb *tcp)
450 {
451         if (entering(tcp)) {
452                 printflags(mlockall_flags, tcp->u_arg[0], "MCL_???");
453         }
454         return 0;
455 }
456
457 #ifdef MS_ASYNC
458
459 static const struct xlat mctl_sync[] = {
460 #ifdef MS_SYNC
461         { MS_SYNC,      "MS_SYNC"       },
462 #endif
463         { MS_ASYNC,     "MS_ASYNC"      },
464         { MS_INVALIDATE,"MS_INVALIDATE" },
465         { 0,            NULL            },
466 };
467
468 int
469 sys_msync(struct tcb *tcp)
470 {
471         if (entering(tcp)) {
472                 /* addr */
473                 tprintf("%#lx", tcp->u_arg[0]);
474                 /* len */
475                 tprintf(", %lu, ", tcp->u_arg[1]);
476                 /* flags */
477                 printflags(mctl_sync, tcp->u_arg[2], "MS_???");
478         }
479         return 0;
480 }
481
482 #endif /* MS_ASYNC */
483
484 #ifdef MC_SYNC
485
486 static const struct xlat mctl_funcs[] = {
487         { MC_LOCK,      "MC_LOCK"       },
488         { MC_LOCKAS,    "MC_LOCKAS"     },
489         { MC_SYNC,      "MC_SYNC"       },
490         { MC_UNLOCK,    "MC_UNLOCK"     },
491         { MC_UNLOCKAS,  "MC_UNLOCKAS"   },
492         { 0,            NULL            },
493 };
494
495 static const struct xlat mctl_lockas[] = {
496         { MCL_CURRENT,  "MCL_CURRENT"   },
497         { MCL_FUTURE,   "MCL_FUTURE"    },
498         { 0,            NULL            },
499 };
500
501 int
502 sys_mctl(struct tcb *tcp)
503 {
504         int arg, function;
505
506         if (entering(tcp)) {
507                 /* addr */
508                 tprintf("%#lx", tcp->u_arg[0]);
509                 /* len */
510                 tprintf(", %lu, ", tcp->u_arg[1]);
511                 /* function */
512                 function = tcp->u_arg[2];
513                 printflags(mctl_funcs, function, "MC_???");
514                 /* arg */
515                 arg = tcp->u_arg[3];
516                 tprints(", ");
517                 switch (function) {
518                 case MC_SYNC:
519                         printflags(mctl_sync, arg, "MS_???");
520                         break;
521                 case MC_LOCKAS:
522                         printflags(mctl_lockas, arg, "MCL_???");
523                         break;
524                 default:
525                         tprintf("%#x", arg);
526                         break;
527                 }
528         }
529         return 0;
530 }
531
532 #endif /* MC_SYNC */
533
534 int
535 sys_mincore(struct tcb *tcp)
536 {
537         if (entering(tcp)) {
538                 tprintf("%#lx, %lu, ", tcp->u_arg[0], tcp->u_arg[1]);
539         } else {
540                 unsigned long i, len;
541                 char *vec = NULL;
542
543                 len = tcp->u_arg[1];
544                 if (syserror(tcp) || tcp->u_arg[2] == 0 ||
545                         (vec = malloc(len)) == NULL ||
546                         umoven(tcp, tcp->u_arg[2], len, vec) < 0)
547                         tprintf("%#lx", tcp->u_arg[2]);
548                 else {
549                         tprints("[");
550                         for (i = 0; i < len; i++) {
551                                 if (abbrev(tcp) && i >= max_strlen) {
552                                         tprints("...");
553                                         break;
554                                 }
555                                 tprints((vec[i] & 1) ? "1" : "0");
556                         }
557                         tprints("]");
558                 }
559                 free(vec);
560         }
561         return 0;
562 }
563
564 #if defined(ALPHA) || defined(IA64) || defined(SPARC) || defined(SPARC64)
565 int
566 sys_getpagesize(struct tcb *tcp)
567 {
568         if (exiting(tcp))
569                 return RVAL_HEX;
570         return 0;
571 }
572 #endif
573
574 #if defined(I386)
575 void
576 print_ldt_entry(struct modify_ldt_ldt_s *ldt_entry)
577 {
578         tprintf("base_addr:%#08lx, "
579                 "limit:%d, "
580                 "seg_32bit:%d, "
581                 "contents:%d, "
582                 "read_exec_only:%d, "
583                 "limit_in_pages:%d, "
584                 "seg_not_present:%d, "
585                 "useable:%d}",
586                 (long) ldt_entry->base_addr,
587                 ldt_entry->limit,
588                 ldt_entry->seg_32bit,
589                 ldt_entry->contents,
590                 ldt_entry->read_exec_only,
591                 ldt_entry->limit_in_pages,
592                 ldt_entry->seg_not_present,
593                 ldt_entry->useable);
594 }
595
596 int
597 sys_modify_ldt(struct tcb *tcp)
598 {
599         if (entering(tcp)) {
600                 struct modify_ldt_ldt_s copy;
601                 tprintf("%ld", tcp->u_arg[0]);
602                 if (tcp->u_arg[1] == 0
603                                 || tcp->u_arg[2] != sizeof(struct modify_ldt_ldt_s)
604                                 || umove(tcp, tcp->u_arg[1], &copy) == -1)
605                         tprintf(", %lx", tcp->u_arg[1]);
606                 else {
607                         tprintf(", {entry_number:%d, ", copy.entry_number);
608                         if (!verbose(tcp))
609                                 tprints("...}");
610                         else {
611                                 print_ldt_entry(&copy);
612                         }
613                 }
614                 tprintf(", %lu", tcp->u_arg[2]);
615         }
616         return 0;
617 }
618
619 int
620 sys_set_thread_area(struct tcb *tcp)
621 {
622         struct modify_ldt_ldt_s copy;
623         if (entering(tcp)) {
624                 if (umove(tcp, tcp->u_arg[0], &copy) != -1) {
625                         if (copy.entry_number == -1)
626                                 tprintf("{entry_number:%d -> ",
627                                         copy.entry_number);
628                         else
629                                 tprints("{entry_number:");
630                 }
631         } else {
632                 if (umove(tcp, tcp->u_arg[0], &copy) != -1) {
633                         tprintf("%d, ", copy.entry_number);
634                         if (!verbose(tcp))
635                                 tprints("...}");
636                         else {
637                                 print_ldt_entry(&copy);
638                         }
639                 } else {
640                         tprintf("%lx", tcp->u_arg[0]);
641                 }
642         }
643         return 0;
644
645 }
646
647 int
648 sys_get_thread_area(struct tcb *tcp)
649 {
650         struct modify_ldt_ldt_s copy;
651         if (exiting(tcp)) {
652                 if (umove(tcp, tcp->u_arg[0], &copy) != -1) {
653                         tprintf("{entry_number:%d, ", copy.entry_number);
654                         if (!verbose(tcp))
655                                 tprints("...}");
656                         else {
657                                 print_ldt_entry(&copy);
658                         }
659                 } else {
660                         tprintf("%lx", tcp->u_arg[0]);
661                 }
662         }
663         return 0;
664
665 }
666 #endif /* I386 */
667
668 #if defined(M68K)
669 int
670 sys_set_thread_area(struct tcb *tcp)
671 {
672         if (entering(tcp))
673                 tprintf("%#lx", tcp->u_arg[0]);
674         return 0;
675
676 }
677
678 int
679 sys_get_thread_area(struct tcb *tcp)
680 {
681         return RVAL_HEX;
682 }
683 #endif
684
685 int
686 sys_remap_file_pages(struct tcb *tcp)
687 {
688         if (entering(tcp)) {
689                 tprintf("%#lx, %lu, ", tcp->u_arg[0], tcp->u_arg[1]);
690                 printflags(mmap_prot, tcp->u_arg[2], "PROT_???");
691                 tprintf(", %lu, ", tcp->u_arg[3]);
692 #ifdef MAP_TYPE
693                 printxval(mmap_flags, tcp->u_arg[4] & MAP_TYPE, "MAP_???");
694                 addflags(mmap_flags, tcp->u_arg[4] & ~MAP_TYPE);
695 #else
696                 printflags(mmap_flags, tcp->u_arg[4], "MAP_???");
697 #endif
698         }
699         return 0;
700 }
701
702 #define MPOL_DEFAULT    0
703 #define MPOL_PREFERRED  1
704 #define MPOL_BIND       2
705 #define MPOL_INTERLEAVE 3
706
707 #define MPOL_F_NODE     (1<<0)
708 #define MPOL_F_ADDR     (1<<1)
709
710 #define MPOL_MF_STRICT  (1<<0)
711 #define MPOL_MF_MOVE    (1<<1)
712 #define MPOL_MF_MOVE_ALL (1<<2)
713
714 static const struct xlat policies[] = {
715         { MPOL_DEFAULT,         "MPOL_DEFAULT"          },
716         { MPOL_PREFERRED,       "MPOL_PREFERRED"        },
717         { MPOL_BIND,            "MPOL_BIND"             },
718         { MPOL_INTERLEAVE,      "MPOL_INTERLEAVE"       },
719         { 0,                    NULL                    }
720 };
721
722 static const struct xlat mbindflags[] = {
723         { MPOL_MF_STRICT,       "MPOL_MF_STRICT"        },
724         { MPOL_MF_MOVE,         "MPOL_MF_MOVE"          },
725         { MPOL_MF_MOVE_ALL,     "MPOL_MF_MOVE_ALL"      },
726         { 0,                    NULL                    }
727 };
728
729 static const struct xlat mempolicyflags[] = {
730         { MPOL_F_NODE,          "MPOL_F_NODE"           },
731         { MPOL_F_ADDR,          "MPOL_F_ADDR"           },
732         { 0,                    NULL                    }
733 };
734
735 static const struct xlat move_pages_flags[] = {
736         { MPOL_MF_MOVE,         "MPOL_MF_MOVE"          },
737         { MPOL_MF_MOVE_ALL,     "MPOL_MF_MOVE_ALL"      },
738         { 0,                    NULL                    }
739 };
740
741 static void
742 get_nodes(struct tcb *tcp, unsigned long ptr, unsigned long maxnodes, int err)
743 {
744         unsigned long nlongs, size, end;
745
746         nlongs = (maxnodes + 8 * sizeof(long) - 1) / (8 * sizeof(long));
747         size = nlongs * sizeof(long);
748         end = ptr + size;
749         if (nlongs == 0 || ((err || verbose(tcp)) && (size * 8 == maxnodes)
750                             && (end > ptr))) {
751                 unsigned long n, cur, abbrev_end;
752                 int failed = 0;
753
754                 if (abbrev(tcp)) {
755                         abbrev_end = ptr + max_strlen * sizeof(long);
756                         if (abbrev_end < ptr)
757                                 abbrev_end = end;
758                 } else {
759                         abbrev_end = end;
760                 }
761                 tprints(", {");
762                 for (cur = ptr; cur < end; cur += sizeof(long)) {
763                         if (cur > ptr)
764                                 tprints(", ");
765                         if (cur >= abbrev_end) {
766                                 tprints("...");
767                                 break;
768                         }
769                         if (umoven(tcp, cur, sizeof(n), (char *) &n) < 0) {
770                                 tprints("?");
771                                 failed = 1;
772                                 break;
773                         }
774                         tprintf("%#0*lx", (int) sizeof(long) * 2 + 2, n);
775                 }
776                 tprints("}");
777                 if (failed)
778                         tprintf(" %#lx", ptr);
779         } else
780                 tprintf(", %#lx", ptr);
781         tprintf(", %lu", maxnodes);
782 }
783
784 int
785 sys_mbind(struct tcb *tcp)
786 {
787         if (entering(tcp)) {
788                 tprintf("%#lx, %lu, ", tcp->u_arg[0], tcp->u_arg[1]);
789                 printxval(policies, tcp->u_arg[2], "MPOL_???");
790                 get_nodes(tcp, tcp->u_arg[3], tcp->u_arg[4], 0);
791                 tprints(", ");
792                 printflags(mbindflags, tcp->u_arg[5], "MPOL_???");
793         }
794         return 0;
795 }
796
797 int
798 sys_set_mempolicy(struct tcb *tcp)
799 {
800         if (entering(tcp)) {
801                 printxval(policies, tcp->u_arg[0], "MPOL_???");
802                 get_nodes(tcp, tcp->u_arg[1], tcp->u_arg[2], 0);
803         }
804         return 0;
805 }
806
807 int
808 sys_get_mempolicy(struct tcb *tcp)
809 {
810         if (exiting(tcp)) {
811                 int pol;
812                 if (tcp->u_arg[0] == 0)
813                         tprints("NULL");
814                 else if (syserror(tcp) || umove(tcp, tcp->u_arg[0], &pol) < 0)
815                         tprintf("%#lx", tcp->u_arg[0]);
816                 else
817                         printxval(policies, pol, "MPOL_???");
818                 get_nodes(tcp, tcp->u_arg[1], tcp->u_arg[2], syserror(tcp));
819                 tprintf(", %#lx, ", tcp->u_arg[3]);
820                 printflags(mempolicyflags, tcp->u_arg[4], "MPOL_???");
821         }
822         return 0;
823 }
824
825 int
826 sys_migrate_pages(struct tcb *tcp)
827 {
828         if (entering(tcp)) {
829                 tprintf("%ld, ", (long) (pid_t) tcp->u_arg[0]);
830                 get_nodes(tcp, tcp->u_arg[2], tcp->u_arg[1], 0);
831                 tprints(", ");
832                 get_nodes(tcp, tcp->u_arg[3], tcp->u_arg[1], 0);
833         }
834         return 0;
835 }
836
837 int
838 sys_move_pages(struct tcb *tcp)
839 {
840         if (entering(tcp)) {
841                 unsigned long npages = tcp->u_arg[1];
842                 tprintf("%ld, %lu, ", tcp->u_arg[0], npages);
843                 if (tcp->u_arg[2] == 0)
844                         tprints("NULL, ");
845                 else {
846                         int i;
847                         long puser = tcp->u_arg[2];
848                         tprints("{");
849                         for (i = 0; i < npages; ++i) {
850                                 void *p;
851                                 if (i > 0)
852                                         tprints(", ");
853                                 if (umove(tcp, puser, &p) < 0) {
854                                         tprints("???");
855                                         break;
856                                 }
857                                 tprintf("%p", p);
858                                 puser += sizeof(void *);
859                         }
860                         tprints("}, ");
861                 }
862                 if (tcp->u_arg[3] == 0)
863                         tprints("NULL, ");
864                 else {
865                         int i;
866                         long nodeuser = tcp->u_arg[3];
867                         tprints("{");
868                         for (i = 0; i < npages; ++i) {
869                                 int node;
870                                 if (i > 0)
871                                         tprints(", ");
872                                 if (umove(tcp, nodeuser, &node) < 0) {
873                                         tprints("???");
874                                         break;
875                                 }
876                                 tprintf("%#x", node);
877                                 nodeuser += sizeof(int);
878                         }
879                         tprints("}, ");
880                 }
881         }
882         if (exiting(tcp)) {
883                 unsigned long npages = tcp->u_arg[1];
884                 if (tcp->u_arg[4] == 0)
885                         tprints("NULL, ");
886                 else {
887                         int i;
888                         long statususer = tcp->u_arg[4];
889                         tprints("{");
890                         for (i = 0; i < npages; ++i) {
891                                 int status;
892                                 if (i > 0)
893                                         tprints(", ");
894                                 if (umove(tcp, statususer, &status) < 0) {
895                                         tprints("???");
896                                         break;
897                                 }
898                                 tprintf("%#x", status);
899                                 statususer += sizeof(int);
900                         }
901                         tprints("}, ");
902                 }
903                 printflags(move_pages_flags, tcp->u_arg[5], "MPOL_???");
904         }
905         return 0;
906 }
907
908 #if defined(POWERPC)
909 int
910 sys_subpage_prot(struct tcb *tcp)
911 {
912         if (entering(tcp)) {
913                 unsigned long cur, end, abbrev_end, entries;
914                 unsigned int entry;
915
916                 tprintf("%#lx, %#lx, ", tcp->u_arg[0], tcp->u_arg[1]);
917                 entries = tcp->u_arg[1] >> 16;
918                 if (!entries || !tcp->u_arg[2]) {
919                         tprints("{}");
920                         return 0;
921                 }
922                 cur = tcp->u_arg[2];
923                 end = cur + (sizeof(int) * entries);
924                 if (!verbose(tcp) || end < tcp->u_arg[2]) {
925                         tprintf("%#lx", tcp->u_arg[2]);
926                         return 0;
927                 }
928                 if (abbrev(tcp)) {
929                         abbrev_end = cur + (sizeof(int) * max_strlen);
930                         if (abbrev_end > end)
931                                 abbrev_end = end;
932                 }
933                 else
934                         abbrev_end = end;
935                 tprints("{");
936                 for (; cur < end; cur += sizeof(int)) {
937                         if (cur > tcp->u_arg[2])
938                                 tprints(", ");
939                         if (cur >= abbrev_end) {
940                                 tprints("...");
941                                 break;
942                         }
943                         if (umove(tcp, cur, &entry) < 0) {
944                                 tprintf("??? [%#lx]", cur);
945                                 break;
946                         }
947                         else
948                                 tprintf("%#08x", entry);
949                 }
950                 tprints("}");
951         }
952
953         return 0;
954 }
955 #endif