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