]> granicus.if.org Git - strace/blob - mem.c
poll: change address argument type from long to kernel_ureg_t
[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
37 unsigned long
38 get_pagesize(void)
39 {
40         static unsigned long pagesize;
41
42         if (!pagesize)
43                 pagesize = sysconf(_SC_PAGESIZE);
44         return pagesize;
45 }
46
47 SYS_FUNC(brk)
48 {
49         printaddr(tcp->u_arg[0]);
50
51         return RVAL_DECODED | RVAL_HEX;
52 }
53
54 #include "xlat/mmap_prot.h"
55 #include "xlat/mmap_flags.h"
56
57 static void
58 print_mmap(struct tcb *tcp, kernel_ureg_t *u_arg, unsigned long long offset)
59 {
60         const kernel_ureg_t addr = u_arg[0];
61         const unsigned long len = u_arg[1];
62         const unsigned long prot = u_arg[2];
63         const unsigned long flags = u_arg[3];
64         const int fd = u_arg[4];
65
66         printaddr(addr);
67         tprintf(", %lu, ", len);
68         printflags_long(mmap_prot, prot, "PROT_???");
69         tprints(", ");
70 #ifdef MAP_TYPE
71         printxval_long(mmap_flags, flags & MAP_TYPE, "MAP_???");
72         addflags(mmap_flags, flags & ~MAP_TYPE);
73 #else
74         printflags_long(mmap_flags, flags, "MAP_???");
75 #endif
76         tprints(", ");
77         printfd(tcp, fd);
78         tprintf(", %#llx", offset);
79 }
80
81 /* Syscall name<->function correspondence is messed up on many arches.
82  * For example:
83  * i386 has __NR_mmap == 90, and it is "old mmap", and
84  * also it has __NR_mmap2 == 192, which is a "new mmap with page offsets".
85  * But x86_64 has just one __NR_mmap == 9, a "new mmap with byte offsets".
86  * Confused? Me too!
87  */
88
89 #if defined AARCH64 || defined ARM \
90  || defined I386 || defined X86_64 || defined X32 \
91  || defined M68K \
92  || defined S390 || defined S390X
93 /* Params are pointed to by u_arg[0], offset is in bytes */
94 SYS_FUNC(old_mmap)
95 {
96         kernel_ureg_t u_arg[6];
97 # if defined AARCH64 || defined X86_64
98         /* We are here only in a 32-bit personality. */
99         unsigned int narrow_arg[6];
100         if (umove_or_printaddr(tcp, tcp->u_arg[0], &narrow_arg))
101                 return RVAL_DECODED | RVAL_HEX;
102         unsigned int i;
103         for (i = 0; i < 6; i++)
104                 u_arg[i] = narrow_arg[i];
105 # else
106         if (umove_or_printaddr(tcp, tcp->u_arg[0], &u_arg))
107                 return RVAL_DECODED | RVAL_HEX;
108 # endif
109         print_mmap(tcp, u_arg, u_arg[5]);
110
111         return RVAL_DECODED | RVAL_HEX;
112 }
113 #endif /* old_mmap architectures */
114
115 #ifdef S390
116 /* Params are pointed to by u_arg[0], offset is in pages */
117 SYS_FUNC(old_mmap_pgoff)
118 {
119         kernel_ureg_t u_arg[5];
120         int i;
121         unsigned int narrow_arg[6];
122         unsigned long long offset;
123         if (umove_or_printaddr(tcp, tcp->u_arg[0], &narrow_arg))
124                 return RVAL_DECODED | RVAL_HEX;
125         for (i = 0; i < 5; i++)
126                 u_arg[i] = narrow_arg[i];
127         offset = narrow_arg[5];
128         offset *= get_pagesize();
129         print_mmap(tcp, u_arg, offset);
130
131         return RVAL_DECODED | RVAL_HEX;
132 }
133 #endif /* S390 */
134
135 /* Params are passed directly, offset is in bytes */
136 SYS_FUNC(mmap)
137 {
138         unsigned long long offset =
139 #if HAVE_STRUCT_TCB_EXT_ARG
140                 tcp->ext_arg[5];        /* try test/x32_mmap.c */
141 #else
142                 tcp->u_arg[5];
143 #endif
144         /* Example of kernel-side handling of this variety of mmap:
145          * arch/x86/kernel/sys_x86_64.c::SYSCALL_DEFINE6(mmap, ...) calls
146          * sys_mmap_pgoff(..., off >> PAGE_SHIFT); i.e. off is in bytes,
147          * since the above code converts off to pages.
148          */
149         print_mmap(tcp, tcp->u_arg, offset);
150
151         return RVAL_DECODED | RVAL_HEX;
152 }
153
154 /* Params are passed directly, offset is in pages */
155 SYS_FUNC(mmap_pgoff)
156 {
157         /* Try test/mmap_offset_decode.c */
158         unsigned long long offset;
159         offset = tcp->u_arg[5];
160         offset *= get_pagesize();
161         print_mmap(tcp, tcp->u_arg, offset);
162
163         return RVAL_DECODED | RVAL_HEX;
164 }
165
166 /* Params are passed directly, offset is in 4k units */
167 SYS_FUNC(mmap_4koff)
168 {
169         unsigned long long offset;
170         offset = tcp->u_arg[5];
171         offset <<= 12;
172         print_mmap(tcp, tcp->u_arg, offset);
173
174         return RVAL_DECODED | RVAL_HEX;
175 }
176
177 SYS_FUNC(munmap)
178 {
179         printaddr(tcp->u_arg[0]);
180         tprintf(", %lu", tcp->u_arg[1]);
181
182         return RVAL_DECODED;
183 }
184
185 static int
186 do_mprotect(struct tcb *tcp, bool has_pkey)
187 {
188         printaddr_klu(getarg_klu(tcp, 0));
189         tprintf(", %" PRI_klu ", ", getarg_klu(tcp, 1));
190         printflags64(mmap_prot, getarg_klu(tcp, 2), "PROT_???");
191
192         if (has_pkey)
193                 tprintf(", %d", (int) tcp->u_arg[3]);
194
195         return RVAL_DECODED;
196 }
197
198 SYS_FUNC(mprotect)
199 {
200         return do_mprotect(tcp, false);
201 }
202
203 SYS_FUNC(pkey_mprotect)
204 {
205         return do_mprotect(tcp, true);
206 }
207
208 #include "xlat/mremap_flags.h"
209
210 SYS_FUNC(mremap)
211 {
212         printaddr(tcp->u_arg[0]);
213         tprintf(", %lu, %lu, ", tcp->u_arg[1], tcp->u_arg[2]);
214         printflags_long(mremap_flags, tcp->u_arg[3], "MREMAP_???");
215 #ifdef MREMAP_FIXED
216         if ((tcp->u_arg[3] & (MREMAP_MAYMOVE | MREMAP_FIXED)) ==
217             (MREMAP_MAYMOVE | MREMAP_FIXED)) {
218                 tprints(", ");
219                 printaddr(tcp->u_arg[4]);
220         }
221 #endif
222         return RVAL_DECODED | RVAL_HEX;
223 }
224
225 #include "xlat/madvise_cmds.h"
226
227 SYS_FUNC(madvise)
228 {
229         printaddr(tcp->u_arg[0]);
230         tprintf(", %lu, ", tcp->u_arg[1]);
231         printxval(madvise_cmds, tcp->u_arg[2], "MADV_???");
232
233         return RVAL_DECODED;
234 }
235
236 #include "xlat/mlockall_flags.h"
237
238 SYS_FUNC(mlockall)
239 {
240         printflags(mlockall_flags, tcp->u_arg[0], "MCL_???");
241
242         return RVAL_DECODED;
243 }
244
245 #include "xlat/mctl_sync.h"
246
247 SYS_FUNC(msync)
248 {
249         /* addr */
250         printaddr(tcp->u_arg[0]);
251         /* len */
252         tprintf(", %lu, ", tcp->u_arg[1]);
253         /* flags */
254         printflags(mctl_sync, tcp->u_arg[2], "MS_???");
255
256         return RVAL_DECODED;
257 }
258
259 #include "xlat/mlock_flags.h"
260
261 SYS_FUNC(mlock2)
262 {
263         printaddr(tcp->u_arg[0]);
264         tprintf(", %lu, ", tcp->u_arg[1]);
265         printflags(mlock_flags, tcp->u_arg[2], "MLOCK_???");
266
267         return RVAL_DECODED;
268 }
269
270 SYS_FUNC(mincore)
271 {
272         if (entering(tcp)) {
273                 printaddr(tcp->u_arg[0]);
274                 tprintf(", %lu, ", tcp->u_arg[1]);
275         } else {
276                 const unsigned long page_size = get_pagesize();
277                 const unsigned long page_mask = page_size - 1;
278                 unsigned long len = tcp->u_arg[1];
279                 unsigned char *vec = NULL;
280
281                 len = len / page_size + (len & page_mask ? 1 : 0);
282                 if (syserror(tcp) || !verbose(tcp) ||
283                     !tcp->u_arg[2] || !(vec = malloc(len)) ||
284                     umoven(tcp, tcp->u_arg[2], len, vec) < 0)
285                         printaddr(tcp->u_arg[2]);
286                 else {
287                         unsigned long i;
288                         tprints("[");
289                         for (i = 0; i < len; i++) {
290                                 if (i)
291                                         tprints(", ");
292                                 if (abbrev(tcp) && i >= max_strlen) {
293                                         tprints("...");
294                                         break;
295                                 }
296                                 tprints((vec[i] & 1) ? "1" : "0");
297                         }
298                         tprints("]");
299                 }
300                 free(vec);
301         }
302         return 0;
303 }
304
305 #if defined ALPHA || defined IA64 || defined M68K \
306  || defined SPARC || defined SPARC64
307 SYS_FUNC(getpagesize)
308 {
309         return RVAL_DECODED | RVAL_HEX;
310 }
311 #endif
312
313 SYS_FUNC(remap_file_pages)
314 {
315         const kernel_ureg_t addr = tcp->u_arg[0];
316         const unsigned long size = tcp->u_arg[1];
317         const unsigned long prot = tcp->u_arg[2];
318         const unsigned long pgoff = tcp->u_arg[3];
319         const unsigned long flags = tcp->u_arg[4];
320
321         printaddr(addr);
322         tprintf(", %lu, ", size);
323         printflags_long(mmap_prot, prot, "PROT_???");
324         tprintf(", %lu, ", pgoff);
325 #ifdef MAP_TYPE
326         printxval_long(mmap_flags, flags & MAP_TYPE, "MAP_???");
327         addflags(mmap_flags, flags & ~MAP_TYPE);
328 #else
329         printflags_long(mmap_flags, flags, "MAP_???");
330 #endif
331
332         return RVAL_DECODED;
333 }
334
335 #if defined(POWERPC)
336 static bool
337 print_protmap_entry(struct tcb *tcp, void *elem_buf, size_t elem_size, void *data)
338 {
339         tprintf("%#08x", * (unsigned int *) elem_buf);
340
341         return true;
342 }
343
344 SYS_FUNC(subpage_prot)
345 {
346         kernel_ureg_t addr = tcp->u_arg[0];
347         unsigned long len = tcp->u_arg[1];
348         unsigned long nmemb = len >> 16;
349         kernel_ureg_t map = tcp->u_arg[2];
350
351         printaddr(addr);
352         tprintf(", %lu, ", len);
353
354         unsigned int entry;
355         print_array(tcp, map, nmemb, &entry, sizeof(entry),
356                     umoven_or_printaddr, print_protmap_entry, 0);
357
358         return RVAL_DECODED;
359 }
360 #endif