1 /*
2  * Copyright (c) 2003-2007 Ulrich Drepper <drepper@redhat.com>
3  * Copyright (c) 2005-2016 Dmitry V. Levin <ldv@altlinux.org>
4  * Copyright (c) 2016-2018 The strace developers.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include "defs.h"
31 
32 static bool
print_node(struct tcb * tcp,void * elem_buf,size_t elem_size,void * data)33 print_node(struct tcb *tcp, void *elem_buf, size_t elem_size, void *data)
34 {
35 	if (elem_size < sizeof(kernel_ulong_t)) {
36 		tprintf("%#0*x", (int) elem_size * 2 + 2,
37 			*(unsigned int *) elem_buf);
38 	} else {
39 		tprintf("%#0*" PRI_klx, (int) elem_size * 2 + 2,
40 			*(kernel_ulong_t *) elem_buf);
41 	}
42 
43 	return true;
44 }
45 
46 static void
print_nodemask(struct tcb * const tcp,const kernel_ulong_t addr,const kernel_ulong_t maxnodes)47 print_nodemask(struct tcb *const tcp, const kernel_ulong_t addr,
48 	       const kernel_ulong_t maxnodes)
49 {
50 	const unsigned int bits_per_long = 8 * current_wordsize;
51 	const kernel_ulong_t nmemb =
52 		(maxnodes + bits_per_long - 2) / bits_per_long;
53 
54 	if (nmemb < maxnodes / bits_per_long ||
55 	    (maxnodes && !nmemb)) {
56 		printaddr(addr);
57 		return;
58 	}
59 
60 	kernel_ulong_t buf;
61 	print_array(tcp, addr, nmemb, &buf, current_wordsize,
62 		    tfetch_mem, print_node, 0);
63 }
64 
SYS_FUNC(migrate_pages)65 SYS_FUNC(migrate_pages)
66 {
67 	tprintf("%d, %" PRI_klu ", ", (int) tcp->u_arg[0], tcp->u_arg[1]);
68 	print_nodemask(tcp, tcp->u_arg[2], tcp->u_arg[1]);
69 	tprints(", ");
70 	print_nodemask(tcp, tcp->u_arg[3], tcp->u_arg[1]);
71 
72 	return RVAL_DECODED;
73 }
74 
75 #include "xlat/policies.h"
76 #include "xlat/mbindflags.h"
77 
SYS_FUNC(mbind)78 SYS_FUNC(mbind)
79 {
80 	printaddr(tcp->u_arg[0]);
81 	tprintf(", %" PRI_klu ", ", tcp->u_arg[1]);
82 	printxval64(policies, tcp->u_arg[2], "MPOL_???");
83 	tprints(", ");
84 	print_nodemask(tcp, tcp->u_arg[3], tcp->u_arg[4]);
85 	tprintf(", %" PRI_klu ", ", tcp->u_arg[4]);
86 	printflags(mbindflags, tcp->u_arg[5], "MPOL_???");
87 
88 	return RVAL_DECODED;
89 }
90 
SYS_FUNC(set_mempolicy)91 SYS_FUNC(set_mempolicy)
92 {
93 	printxval(policies, tcp->u_arg[0], "MPOL_???");
94 	tprints(", ");
95 	print_nodemask(tcp, tcp->u_arg[1], tcp->u_arg[2]);
96 	tprintf(", %" PRI_klu, tcp->u_arg[2]);
97 
98 	return RVAL_DECODED;
99 }
100 
101 #include "xlat/mempolicyflags.h"
102 
SYS_FUNC(get_mempolicy)103 SYS_FUNC(get_mempolicy)
104 {
105 	if (exiting(tcp)) {
106 		int pol;
107 		if (!umove_or_printaddr(tcp, tcp->u_arg[0], &pol)) {
108 			tprints("[");
109 			printxval(policies, pol, "MPOL_???");
110 			tprints("]");
111 		}
112 		tprints(", ");
113 		print_nodemask(tcp, tcp->u_arg[1], tcp->u_arg[2]);
114 		tprintf(", %" PRI_klu ", ", tcp->u_arg[2]);
115 		printaddr(tcp->u_arg[3]);
116 		tprints(", ");
117 		printflags64(mempolicyflags, tcp->u_arg[4], "MPOL_???");
118 	}
119 	return 0;
120 }
121 
122 #include "xlat/move_pages_flags.h"
123 
124 static bool
print_addr(struct tcb * tcp,void * elem_buf,size_t elem_size,void * data)125 print_addr(struct tcb *tcp, void *elem_buf, size_t elem_size, void *data)
126 {
127 	kernel_ulong_t addr;
128 
129 	if (elem_size < sizeof(addr)) {
130 		addr = *(unsigned int *) elem_buf;
131 	} else {
132 		addr = *(kernel_ulong_t *) elem_buf;
133 	}
134 
135 	printaddr(addr);
136 
137 	return true;
138 }
139 
140 static bool
print_status(struct tcb * tcp,void * elem_buf,size_t elem_size,void * data)141 print_status(struct tcb *tcp, void *elem_buf, size_t elem_size, void *data)
142 {
143 	const int status = *(int *) elem_buf;
144 
145 	if (status < 0 && (unsigned) -status < nerrnos)
146 		tprintf("-%s", errnoent[-status]);
147 	else
148 		tprintf("%d", status);
149 
150 	return true;
151 }
152 
153 static bool
print_int(struct tcb * tcp,void * elem_buf,size_t elem_size,void * data)154 print_int(struct tcb *tcp, void *elem_buf, size_t elem_size, void *data)
155 {
156 	tprintf("%d", *(int *) elem_buf);
157 
158 	return true;
159 }
160 
SYS_FUNC(move_pages)161 SYS_FUNC(move_pages)
162 {
163 	const kernel_ulong_t npages = tcp->u_arg[1];
164 	kernel_ulong_t buf;
165 
166 	if (entering(tcp)) {
167 		tprintf("%d, %" PRI_klu ", ", (int) tcp->u_arg[0], npages);
168 		print_array(tcp, tcp->u_arg[2], npages, &buf, current_wordsize,
169 			    tfetch_mem, print_addr, 0);
170 		tprints(", ");
171 		print_array(tcp, tcp->u_arg[3], npages, &buf, sizeof(int),
172 			    tfetch_mem, print_int, 0);
173 		tprints(", ");
174 	} else {
175 		print_array(tcp, tcp->u_arg[4], npages, &buf, sizeof(int),
176 			    tfetch_mem, print_status, 0);
177 		tprints(", ");
178 		printflags(move_pages_flags, tcp->u_arg[5], "MPOL_???");
179 	}
180 	return 0;
181 }
182