1 /*
2  * Copyright (c) 2014-2015 Dmitry V. Levin <ldv@altlinux.org>
3  * Copyright (c) 2014-2018 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 
31 #include "xlat/kexec_load_flags.h"
32 #include "xlat/kexec_arch_values.h"
33 
34 #ifndef KEXEC_ARCH_MASK
35 # define KEXEC_ARCH_MASK 0xffff0000
36 #endif
37 #ifndef KEXEC_SEGMENT_MAX
38 # define KEXEC_SEGMENT_MAX 16
39 #endif
40 
41 static bool
print_seg(struct tcb * tcp,void * elem_buf,size_t elem_size,void * data)42 print_seg(struct tcb *tcp, void *elem_buf, size_t elem_size, void *data)
43 {
44 	const kernel_ulong_t *seg;
45 	kernel_ulong_t seg_buf[4];
46 
47 	if (elem_size < sizeof(seg_buf)) {
48 		unsigned int i;
49 
50 		for (i = 0; i < ARRAY_SIZE(seg_buf); ++i)
51 			seg_buf[i] = ((unsigned int *) elem_buf)[i];
52 		seg = seg_buf;
53 	} else {
54 		seg = elem_buf;
55 	}
56 
57 	tprints("{buf=");
58 	printaddr(seg[0]);
59 	tprintf(", bufsz=%" PRI_klu ", mem=", seg[1]);
60 	printaddr(seg[2]);
61 	tprintf(", memsz=%" PRI_klu "}", seg[3]);
62 
63 	return true;
64 }
65 
66 static void
print_kexec_segments(struct tcb * const tcp,const kernel_ulong_t addr,const kernel_ulong_t len)67 print_kexec_segments(struct tcb *const tcp, const kernel_ulong_t addr,
68 		     const kernel_ulong_t len)
69 {
70 	if (len > KEXEC_SEGMENT_MAX) {
71 		printaddr(addr);
72 		return;
73 	}
74 
75 	kernel_ulong_t seg[4];
76 	const size_t sizeof_seg = ARRAY_SIZE(seg) * current_wordsize;
77 
78 	print_array(tcp, addr, len, seg, sizeof_seg,
79 		    tfetch_mem, print_seg, 0);
80 }
81 
SYS_FUNC(kexec_load)82 SYS_FUNC(kexec_load)
83 {
84 	/* entry, nr_segments */
85 	printaddr(tcp->u_arg[0]);
86 	tprintf(", %" PRI_klu ", ", tcp->u_arg[1]);
87 
88 	/* segments */
89 	print_kexec_segments(tcp, tcp->u_arg[2], tcp->u_arg[1]);
90 	tprints(", ");
91 
92 	/* flags */
93 	kernel_ulong_t n = tcp->u_arg[3];
94 	printxval64(kexec_arch_values, n & KEXEC_ARCH_MASK, "KEXEC_ARCH_???");
95 	n &= ~(kernel_ulong_t) KEXEC_ARCH_MASK;
96 	if (n) {
97 		tprints("|");
98 		printflags64(kexec_load_flags, n, "KEXEC_???");
99 	}
100 
101 	return RVAL_DECODED;
102 }
103 
104 #include "xlat/kexec_file_load_flags.h"
105 
SYS_FUNC(kexec_file_load)106 SYS_FUNC(kexec_file_load)
107 {
108 	/* kernel_fd */
109 	printfd(tcp, tcp->u_arg[0]);
110 	tprints(", ");
111 	/* initrd_fd */
112 	printfd(tcp, tcp->u_arg[1]);
113 	tprints(", ");
114 	/* cmdline_len */
115 	tprintf("%" PRI_klu ", ", tcp->u_arg[2]);
116 	/* cmdline */
117 	printstrn(tcp, tcp->u_arg[3], tcp->u_arg[2]);
118 	tprints(", ");
119 	/* flags */
120 	printflags64(kexec_file_load_flags, tcp->u_arg[4], "KEXEC_FILE_???");
121 
122 	return RVAL_DECODED;
123 }
124