• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2015 Dmitry V. Levin <ldv@altlinux.org>
3  * Copyright (c) 2015-2017 The strace developers.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include "defs.h"
30 #include <fcntl.h>
31 
32 #include "xlat/uffd_flags.h"
33 
SYS_FUNC(userfaultfd)34 SYS_FUNC(userfaultfd)
35 {
36 	printflags(uffd_flags, tcp->u_arg[0], "UFFD_???");
37 
38 	return RVAL_DECODED | RVAL_FD;
39 }
40 
41 #ifdef HAVE_LINUX_USERFAULTFD_H
42 # include <linux/ioctl.h>
43 # include <linux/userfaultfd.h>
44 
45 # include "xlat/uffd_api_flags.h"
46 # include "xlat/uffd_copy_flags.h"
47 # include "xlat/uffd_register_ioctl_flags.h"
48 # include "xlat/uffd_register_mode_flags.h"
49 # include "xlat/uffd_zeropage_flags.h"
50 
51 static void
tprintf_uffdio_range(const struct uffdio_range * range)52 tprintf_uffdio_range(const struct uffdio_range *range)
53 {
54 	tprintf("{start=%#" PRI__x64 ", len=%#" PRI__x64 "}",
55 		range->start, range->len);
56 }
57 
58 int
uffdio_ioctl(struct tcb * const tcp,const unsigned int code,const kernel_ulong_t arg)59 uffdio_ioctl(struct tcb *const tcp, const unsigned int code,
60 	     const kernel_ulong_t arg)
61 {
62 	switch (code) {
63 	case UFFDIO_API: {
64 		struct uffdio_api ua;
65 		if (entering(tcp)) {
66 			tprints(", ");
67 			if (umove_or_printaddr(tcp, arg, &ua))
68 				return RVAL_DECODED | 1;
69 			/* Features is intended to contain some flags, but
70 			 * there aren't any defined yet.
71 			 */
72 			tprintf("{api=%#" PRI__x64
73 				", features=%#" PRI__x64,
74 				ua.api, ua.features);
75 		} else {
76 			if (!syserror(tcp) && !umove(tcp, arg, &ua)) {
77 				tprintf(", features.out=%#" PRI__x64
78 					", ioctls=", ua.features);
79 				printflags64(uffd_api_flags, ua.ioctls,
80 					     "_UFFDIO_???");
81 			}
82 			tprints("}");
83 		}
84 		return 1;
85 	}
86 
87 	case UFFDIO_COPY: {
88 		struct uffdio_copy uc;
89 		if (entering(tcp)) {
90 			tprints(", ");
91 			if (umove_or_printaddr(tcp, arg, &uc))
92 				return RVAL_DECODED | 1;
93 			tprintf("{dst=%#" PRI__x64 ", src=%#" PRI__x64
94 				", len=%#" PRI__x64 ", mode=",
95 				uc.dst, uc.src, uc.len);
96 			printflags64(uffd_copy_flags, uc.mode,
97 				     "UFFDIO_COPY_???");
98 		} else {
99 			if (!syserror(tcp) && !umove(tcp, arg, &uc))
100 				tprintf(", copy=%#" PRI__x64, uc.copy);
101 			tprints("}");
102 		}
103 		return 1;
104 	}
105 
106 	case UFFDIO_REGISTER: {
107 		struct uffdio_register ur;
108 		if (entering(tcp)) {
109 			tprints(", ");
110 			if (umove_or_printaddr(tcp, arg, &ur))
111 				return RVAL_DECODED | 1;
112 			tprints("{range=");
113 			tprintf_uffdio_range(&ur.range);
114 			tprints(", mode=");
115 			printflags64(uffd_register_mode_flags, ur.mode,
116 				     "UFFDIO_REGISTER_MODE_???");
117 		} else {
118 			if (!syserror(tcp) && !umove(tcp, arg, &ur)) {
119 				tprints(", ioctls=");
120 				printflags64(uffd_register_ioctl_flags,
121 					     ur.ioctls, "UFFDIO_???");
122 			}
123 			tprints("}");
124 		}
125 		return 1;
126 	}
127 
128 	case UFFDIO_UNREGISTER:
129 	case UFFDIO_WAKE: {
130 		struct uffdio_range ura;
131 		tprints(", ");
132 		if (!umove_or_printaddr(tcp, arg, &ura))
133 			tprintf_uffdio_range(&ura);
134 		return RVAL_DECODED | 1;
135 	}
136 
137 	case UFFDIO_ZEROPAGE: {
138 		struct uffdio_zeropage uz;
139 		if (entering(tcp)) {
140 			tprints(", ");
141 			if (umove_or_printaddr(tcp, arg, &uz))
142 				return RVAL_DECODED | 1;
143 			tprints("{range=");
144 			tprintf_uffdio_range(&uz.range);
145 			tprints(", mode=");
146 			printflags64(uffd_zeropage_flags, uz.mode,
147 				     "UFFDIO_ZEROPAGE_???");
148 		} else {
149 			if (!syserror(tcp) && !umove(tcp, arg, &uz))
150 				tprintf(", zeropage=%#" PRI__x64, uz.zeropage);
151 			tprints("}");
152 		}
153 		return 1;
154 	}
155 
156 	default:
157 		return RVAL_DECODED;
158 	}
159 }
160 #endif /* HAVE_LINUX_USERFAULTFD_H */
161