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