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