2 * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
3 * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
4 * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
5 * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
6 * Copyright (c) 2000 PocketPenguins Inc. Linux for Hitachi SuperH
7 * port by Greg Banks <gbanks@pocketpenguins.com>
8 * Copyright (c) 1999-2017 The strace developers.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <linux/mman.h>
41 static unsigned long pagesize;
44 pagesize = sysconf(_SC_PAGESIZE);
50 printaddr(tcp->u_arg[0]);
52 return RVAL_DECODED | RVAL_HEX;
55 #include "xlat/mmap_prot.h"
56 #include "xlat/mmap_flags.h"
58 #ifndef MAP_HUGE_SHIFT
59 # define MAP_HUGE_SHIFT 26
63 # define MAP_HUGE_MASK 0x3f
67 print_mmap_flags(kernel_ulong_t flags)
69 printxval64(mmap_flags, flags & MAP_TYPE, "MAP_???");
72 const unsigned int mask = MAP_HUGE_MASK << MAP_HUGE_SHIFT;
73 const unsigned int hugetlb_value = flags & mask;
76 addflags(mmap_flags, flags);
79 tprintf("|%u<<MAP_HUGE_SHIFT",
80 hugetlb_value >> MAP_HUGE_SHIFT);
84 print_mmap(struct tcb *tcp, kernel_ulong_t *u_arg, unsigned long long offset)
86 const kernel_ulong_t addr = u_arg[0];
87 const kernel_ulong_t len = u_arg[1];
88 const kernel_ulong_t prot = u_arg[2];
89 const kernel_ulong_t flags = u_arg[3];
90 const int fd = u_arg[4];
93 tprintf(", %" PRI_klu ", ", len);
94 printflags64(mmap_prot, prot, "PROT_???");
96 print_mmap_flags(flags);
99 tprintf(", %#llx", offset);
102 /* Syscall name<->function correspondence is messed up on many arches.
104 * i386 has __NR_mmap == 90, and it is "old mmap", and
105 * also it has __NR_mmap2 == 192, which is a "new mmap with page offsets".
106 * But x86_64 has just one __NR_mmap == 9, a "new mmap with byte offsets".
110 #ifdef HAVE_ARCH_OLD_MMAP
112 * Fetch old_mmap/old_mmap_pgoff arguments that are provided as a 6-element
113 * array. Return pointer to a static array or NULL in case of fetch failure.
116 fetch_old_mmap_args(struct tcb *tcp)
118 static kernel_ulong_t u_arg[6];
120 if (current_wordsize == 4) {
121 unsigned int narrow_arg[6];
122 if (umove(tcp, tcp->u_arg[0], &narrow_arg))
124 for (unsigned int i = 0; i < 6; i++)
125 u_arg[i] = narrow_arg[i];
127 if (umove(tcp, tcp->u_arg[0], &u_arg))
134 /* Params are pointed to by u_arg[0], offset is in bytes */
137 kernel_ulong_t *args = fetch_old_mmap_args(tcp);
140 print_mmap(tcp, args, args[5]);
142 printaddr(tcp->u_arg[0]);
144 return RVAL_DECODED | RVAL_HEX;
147 # ifdef HAVE_ARCH_OLD_MMAP_PGOFF
148 /* Params are pointed to by u_arg[0], offset is in pages */
149 SYS_FUNC(old_mmap_pgoff)
151 kernel_ulong_t *args = fetch_old_mmap_args(tcp);
154 unsigned long long offset;
157 offset *= get_pagesize();
159 print_mmap(tcp, args, offset);
161 printaddr(tcp->u_arg[0]);
164 return RVAL_DECODED | RVAL_HEX;
166 # endif /* HAVE_ARCH_OLD_MMAP_PGOFF */
167 #endif /* HAVE_ARCH_OLD_MMAP */
169 /* Params are passed directly, offset is in bytes */
172 /* Example of kernel-side handling of this variety of mmap:
173 * arch/x86/kernel/sys_x86_64.c::SYSCALL_DEFINE6(mmap, ...) calls
174 * sys_mmap_pgoff(..., off >> PAGE_SHIFT); i.e. off is in bytes,
175 * since the above code converts off to pages.
177 print_mmap(tcp, tcp->u_arg, tcp->u_arg[5]);
179 return RVAL_DECODED | RVAL_HEX;
182 /* Params are passed directly, offset is in pages */
185 /* Try test/mmap_offset_decode.c */
186 unsigned long long offset;
187 offset = tcp->u_arg[5];
188 offset *= get_pagesize();
189 print_mmap(tcp, tcp->u_arg, offset);
191 return RVAL_DECODED | RVAL_HEX;
194 /* Params are passed directly, offset is in 4k units */
197 unsigned long long offset;
198 offset = tcp->u_arg[5];
200 print_mmap(tcp, tcp->u_arg, offset);
202 return RVAL_DECODED | RVAL_HEX;
207 printaddr(tcp->u_arg[0]);
208 tprintf(", %" PRI_klu, tcp->u_arg[1]);
214 do_mprotect(struct tcb *tcp, bool has_pkey)
216 printaddr(tcp->u_arg[0]);
217 tprintf(", %" PRI_klu ", ", tcp->u_arg[1]);
218 printflags64(mmap_prot, tcp->u_arg[2], "PROT_???");
221 tprintf(", %d", (int) tcp->u_arg[3]);
228 return do_mprotect(tcp, false);
231 SYS_FUNC(pkey_mprotect)
233 return do_mprotect(tcp, true);
236 #include "xlat/mremap_flags.h"
240 printaddr(tcp->u_arg[0]);
241 tprintf(", %" PRI_klu ", %" PRI_klu ", ", tcp->u_arg[1], tcp->u_arg[2]);
242 printflags64(mremap_flags, tcp->u_arg[3], "MREMAP_???");
244 if ((tcp->u_arg[3] & (MREMAP_MAYMOVE | MREMAP_FIXED)) ==
245 (MREMAP_MAYMOVE | MREMAP_FIXED)) {
247 printaddr(tcp->u_arg[4]);
250 return RVAL_DECODED | RVAL_HEX;
253 #include "xlat/madvise_cmds.h"
257 printaddr(tcp->u_arg[0]);
258 tprintf(", %" PRI_klu ", ", tcp->u_arg[1]);
259 printxval(madvise_cmds, tcp->u_arg[2], "MADV_???");
264 #include "xlat/mlockall_flags.h"
268 printflags(mlockall_flags, tcp->u_arg[0], "MCL_???");
273 #include "xlat/mctl_sync.h"
278 printaddr(tcp->u_arg[0]);
280 tprintf(", %" PRI_klu ", ", tcp->u_arg[1]);
282 printflags(mctl_sync, tcp->u_arg[2], "MS_???");
287 #include "xlat/mlock_flags.h"
291 printaddr(tcp->u_arg[0]);
292 tprintf(", %" PRI_klu ", ", tcp->u_arg[1]);
293 printflags(mlock_flags, tcp->u_arg[2], "MLOCK_???");
301 printaddr(tcp->u_arg[0]);
302 tprintf(", %" PRI_klu ", ", tcp->u_arg[1]);
304 const unsigned long page_size = get_pagesize();
305 const unsigned long page_mask = page_size - 1;
306 unsigned long len = tcp->u_arg[1];
307 unsigned char *vec = NULL;
309 len = len / page_size + (len & page_mask ? 1 : 0);
310 if (syserror(tcp) || !verbose(tcp) ||
311 !tcp->u_arg[2] || !(vec = malloc(len)) ||
312 umoven(tcp, tcp->u_arg[2], len, vec) < 0)
313 printaddr(tcp->u_arg[2]);
317 for (i = 0; i < len; i++) {
320 if (abbrev(tcp) && i >= max_strlen) {
324 tprints((vec[i] & 1) ? "1" : "0");
333 #if defined ALPHA || defined IA64 || defined M68K \
334 || defined SPARC || defined SPARC64
335 SYS_FUNC(getpagesize)
337 return RVAL_DECODED | RVAL_HEX;
341 SYS_FUNC(remap_file_pages)
343 const kernel_ulong_t addr = tcp->u_arg[0];
344 const kernel_ulong_t size = tcp->u_arg[1];
345 const kernel_ulong_t prot = tcp->u_arg[2];
346 const kernel_ulong_t pgoff = tcp->u_arg[3];
347 const kernel_ulong_t flags = tcp->u_arg[4];
350 tprintf(", %" PRI_klu ", ", size);
351 printflags64(mmap_prot, prot, "PROT_???");
352 tprintf(", %" PRI_klu ", ", pgoff);
353 print_mmap_flags(flags);
360 print_protmap_entry(struct tcb *tcp, void *elem_buf, size_t elem_size, void *data)
362 tprintf("%#08x", *(unsigned int *) elem_buf);
367 SYS_FUNC(subpage_prot)
369 kernel_ulong_t addr = tcp->u_arg[0];
370 kernel_ulong_t len = tcp->u_arg[1];
371 kernel_ulong_t nmemb = len >> 16;
372 kernel_ulong_t map = tcp->u_arg[2];
375 tprintf(", %" PRI_klu ", ", len);
378 print_array(tcp, map, nmemb, &entry, sizeof(entry),
379 umoven_or_printaddr, print_protmap_entry, 0);