• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Check decoding of s390_pci_mmio_read and s390_pci_mmio_write syscalls.
3  *
4  * Copyright (c) 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 "tests.h"
31 #include <asm/unistd.h>
32 
33 #if defined __NR_s390_pci_mmio_read && defined __NR_s390_pci_mmio_write
34 
35 # include <errno.h>
36 # include <stdbool.h>
37 # include <stdint.h>
38 # include <stdio.h>
39 # include <unistd.h>
40 
41 static void
do_call(bool wr,kernel_ulong_t mmio_addr,kernel_ulong_t buf,kernel_ulong_t len,bool buf_valid,const char * buf_str)42 do_call(bool wr, kernel_ulong_t mmio_addr, kernel_ulong_t buf,
43 	kernel_ulong_t len, bool buf_valid, const char *buf_str)
44 {
45 	long saved_errno = 0;
46 	long rc = 0;
47 
48 	printf("s390_pci_mmio_%s(%#llx, ", wr ? "write" : "read",
49 	       (unsigned long long) mmio_addr);
50 
51 	if (!wr) {
52 		rc = syscall(__NR_s390_pci_mmio_read, mmio_addr, buf, len);
53 		saved_errno = errno;
54 	}
55 
56 	if (buf_valid && !rc) {
57 		char *buf_ptr = (char *) (uintptr_t) buf;
58 
59 		print_quoted_hex(buf_ptr,
60 				 len > DEFAULT_STRLEN ? DEFAULT_STRLEN : len);
61 
62 		if (len > DEFAULT_STRLEN)
63 			printf("...");
64 	} else {
65 		if (buf_str)
66 			printf("%s", buf_str);
67 		else
68 			printf("%#llx", (unsigned long long) buf);
69 	}
70 
71 	printf(", %llu) = ", (unsigned long long) len);
72 
73 	if (wr)
74 		rc = syscall(__NR_s390_pci_mmio_write, mmio_addr, buf, len);
75 	else
76 		errno = saved_errno;
77 
78 	puts(sprintrc(rc));
79 }
80 
81 int
main(void)82 main(void)
83 {
84 	static const size_t buf_size = DEFAULT_STRLEN + 10;
85 
86 	char *buf = tail_alloc(buf_size);
87 
88 	bool bools[] = { true, false };
89 
90 	kernel_ulong_t addrs[] = {
91 		0,
92 		(kernel_ulong_t) 0xdeafbeefdeadc0deULL,
93 	};
94 
95 	struct {
96 		kernel_ulong_t buf;
97 		const char *str;
98 		size_t size;
99 	} bufs[] = {
100 		{ (kernel_ulong_t) ARG_STR(NULL),            0 },
101 		{ (kernel_ulong_t) (buf + buf_size), NULL,   0 },
102 		{ (kernel_ulong_t) (buf),            NULL,   buf_size },
103 		{ (kernel_ulong_t) (buf + 9),        NULL,   buf_size - 9 },
104 		{ (kernel_ulong_t) (buf + 10),       NULL,   buf_size - 10 },
105 		{ (kernel_ulong_t) (buf + 16),       NULL,   buf_size - 16 },
106 		{ (kernel_ulong_t) (buf + 26),       NULL,   buf_size - 26 },
107 		{ (kernel_ulong_t) (buf + 28),       NULL,   buf_size - 28 },
108 	};
109 
110 	kernel_ulong_t sizes[] = {
111 		0,
112 		DEFAULT_STRLEN / 2,
113 		DEFAULT_STRLEN - 10,
114 		DEFAULT_STRLEN,
115 		DEFAULT_STRLEN + 1,
116 		buf_size,
117 		buf_size + 10,
118 		(kernel_ulong_t) 0xfacefeedac0ffeedULL,
119 	};
120 
121 	unsigned int i, j, k, l;
122 	unsigned int ctr = 0;
123 
124 	for (i = 0; i < ARRAY_SIZE(addrs); i++) {
125 		for (j = 0; j < ARRAY_SIZE(bufs); j++) {
126 			for (k = 0; k < ARRAY_SIZE(sizes); k++) {
127 				for (l = 0; l < ARRAY_SIZE(bools); l++) {
128 					bool valid = bufs[j].buf &&
129 						bufs[j].size >=
130 						MIN(sizes[k],
131 						    DEFAULT_STRLEN + 1);
132 
133 					if (bufs[j].size && bools[l])
134 						fill_memory_ex((char *) buf,
135 							       bufs[j].size,
136 							       0xC0 + ctr, 255);
137 
138 					do_call(bools[l], addrs[i], bufs[j].buf,
139 						sizes[k], valid, bufs[j].str);
140 
141 					ctr++;
142 				}
143 			}
144 		}
145 	}
146 
147 	puts("+++ exited with 0 +++");
148 	return 0;
149 }
150 
151 #else
152 
153 SKIP_MAIN_UNDEFINED("__NR_s390_pci_mmio_read && __NR_s390_pci_mmio_write");
154 
155 #endif
156