xref: /openbmc/qemu/tests/qtest/fuzz/qtest_wrappers.c (revision 7fd51e68c34fcefdb4d6fd646ed3346f780f89f4)
1d92e1b6dSAlexander Bulekov /*
2d92e1b6dSAlexander Bulekov  * qtest function wrappers
3d92e1b6dSAlexander Bulekov  *
4d92e1b6dSAlexander Bulekov  * Copyright Red Hat Inc., 2019
5d92e1b6dSAlexander Bulekov  *
6d92e1b6dSAlexander Bulekov  * Authors:
7d92e1b6dSAlexander Bulekov  *  Alexander Bulekov   <alxndr@bu.edu>
8d92e1b6dSAlexander Bulekov  *
9d92e1b6dSAlexander Bulekov  * This work is licensed under the terms of the GNU GPL, version 2 or later.
10d92e1b6dSAlexander Bulekov  * See the COPYING file in the top-level directory.
11d92e1b6dSAlexander Bulekov  *
12d92e1b6dSAlexander Bulekov  */
13d92e1b6dSAlexander Bulekov 
14d92e1b6dSAlexander Bulekov #include "qemu/osdep.h"
15*64ed6f92SPaolo Bonzini #include "hw/core/cpu.h"
16d92e1b6dSAlexander Bulekov #include "exec/ioport.h"
17d92e1b6dSAlexander Bulekov 
18d92e1b6dSAlexander Bulekov #include "fuzz.h"
19d92e1b6dSAlexander Bulekov 
20d92e1b6dSAlexander Bulekov static bool serialize = true;
21d92e1b6dSAlexander Bulekov 
22d92e1b6dSAlexander Bulekov #define WRAP(RET_TYPE, NAME_AND_ARGS)\
23d92e1b6dSAlexander Bulekov     RET_TYPE __wrap_##NAME_AND_ARGS;\
24d92e1b6dSAlexander Bulekov     RET_TYPE __real_##NAME_AND_ARGS;
25d92e1b6dSAlexander Bulekov 
WRAP(uint8_t,qtest_inb (QTestState * s,uint16_t addr))26d92e1b6dSAlexander Bulekov WRAP(uint8_t  , qtest_inb(QTestState *s, uint16_t addr))
27d92e1b6dSAlexander Bulekov WRAP(uint16_t , qtest_inw(QTestState *s, uint16_t addr))
28d92e1b6dSAlexander Bulekov WRAP(uint32_t , qtest_inl(QTestState *s, uint16_t addr))
29d92e1b6dSAlexander Bulekov WRAP(void     , qtest_outb(QTestState *s, uint16_t addr, uint8_t value))
30d92e1b6dSAlexander Bulekov WRAP(void     , qtest_outw(QTestState *s, uint16_t addr, uint16_t value))
31d92e1b6dSAlexander Bulekov WRAP(void     , qtest_outl(QTestState *s, uint16_t addr, uint32_t value))
32d92e1b6dSAlexander Bulekov WRAP(uint8_t  , qtest_readb(QTestState *s, uint64_t addr))
33d92e1b6dSAlexander Bulekov WRAP(uint16_t , qtest_readw(QTestState *s, uint64_t addr))
34d92e1b6dSAlexander Bulekov WRAP(uint32_t , qtest_readl(QTestState *s, uint64_t addr))
35d92e1b6dSAlexander Bulekov WRAP(uint64_t , qtest_readq(QTestState *s, uint64_t addr))
36d92e1b6dSAlexander Bulekov WRAP(void     , qtest_writeb(QTestState *s, uint64_t addr, uint8_t value))
37d92e1b6dSAlexander Bulekov WRAP(void     , qtest_writew(QTestState *s, uint64_t addr, uint16_t value))
38d92e1b6dSAlexander Bulekov WRAP(void     , qtest_writel(QTestState *s, uint64_t addr, uint32_t value))
39d92e1b6dSAlexander Bulekov WRAP(void     , qtest_writeq(QTestState *s, uint64_t addr, uint64_t value))
40d92e1b6dSAlexander Bulekov WRAP(void     , qtest_memread(QTestState *s, uint64_t addr,
41d92e1b6dSAlexander Bulekov                               void *data, size_t size))
42d92e1b6dSAlexander Bulekov WRAP(void     , qtest_bufread(QTestState *s, uint64_t addr, void *data,
43d92e1b6dSAlexander Bulekov                               size_t size))
44d92e1b6dSAlexander Bulekov WRAP(void     , qtest_memwrite(QTestState *s, uint64_t addr, const void *data,
45d92e1b6dSAlexander Bulekov                                size_t size))
46d92e1b6dSAlexander Bulekov WRAP(void,      qtest_bufwrite(QTestState *s, uint64_t addr,
47d92e1b6dSAlexander Bulekov                                const void *data, size_t size))
48d92e1b6dSAlexander Bulekov WRAP(void,      qtest_memset(QTestState *s, uint64_t addr,
49d92e1b6dSAlexander Bulekov                              uint8_t patt, size_t size))
50d92e1b6dSAlexander Bulekov 
51d92e1b6dSAlexander Bulekov 
52d92e1b6dSAlexander Bulekov uint8_t __wrap_qtest_inb(QTestState *s, uint16_t addr)
53d92e1b6dSAlexander Bulekov {
54d92e1b6dSAlexander Bulekov     if (!serialize) {
55d92e1b6dSAlexander Bulekov         return cpu_inb(addr);
56d92e1b6dSAlexander Bulekov     } else {
57d92e1b6dSAlexander Bulekov         return __real_qtest_inb(s, addr);
58d92e1b6dSAlexander Bulekov     }
59d92e1b6dSAlexander Bulekov }
60d92e1b6dSAlexander Bulekov 
__wrap_qtest_inw(QTestState * s,uint16_t addr)61d92e1b6dSAlexander Bulekov uint16_t __wrap_qtest_inw(QTestState *s, uint16_t addr)
62d92e1b6dSAlexander Bulekov {
63d92e1b6dSAlexander Bulekov     if (!serialize) {
64d92e1b6dSAlexander Bulekov         return cpu_inw(addr);
65d92e1b6dSAlexander Bulekov     } else {
66d92e1b6dSAlexander Bulekov         return __real_qtest_inw(s, addr);
67d92e1b6dSAlexander Bulekov     }
68d92e1b6dSAlexander Bulekov }
69d92e1b6dSAlexander Bulekov 
__wrap_qtest_inl(QTestState * s,uint16_t addr)70d92e1b6dSAlexander Bulekov uint32_t __wrap_qtest_inl(QTestState *s, uint16_t addr)
71d92e1b6dSAlexander Bulekov {
72d92e1b6dSAlexander Bulekov     if (!serialize) {
73d92e1b6dSAlexander Bulekov         return cpu_inl(addr);
74d92e1b6dSAlexander Bulekov     } else {
75d92e1b6dSAlexander Bulekov         return __real_qtest_inl(s, addr);
76d92e1b6dSAlexander Bulekov     }
77d92e1b6dSAlexander Bulekov }
78d92e1b6dSAlexander Bulekov 
__wrap_qtest_outb(QTestState * s,uint16_t addr,uint8_t value)79d92e1b6dSAlexander Bulekov void __wrap_qtest_outb(QTestState *s, uint16_t addr, uint8_t value)
80d92e1b6dSAlexander Bulekov {
81d92e1b6dSAlexander Bulekov     if (!serialize) {
82d92e1b6dSAlexander Bulekov         cpu_outb(addr, value);
83d92e1b6dSAlexander Bulekov     } else {
84d92e1b6dSAlexander Bulekov         __real_qtest_outb(s, addr, value);
85d92e1b6dSAlexander Bulekov     }
86d92e1b6dSAlexander Bulekov }
87d92e1b6dSAlexander Bulekov 
__wrap_qtest_outw(QTestState * s,uint16_t addr,uint16_t value)88d92e1b6dSAlexander Bulekov void __wrap_qtest_outw(QTestState *s, uint16_t addr, uint16_t value)
89d92e1b6dSAlexander Bulekov {
90d92e1b6dSAlexander Bulekov     if (!serialize) {
91d92e1b6dSAlexander Bulekov         cpu_outw(addr, value);
92d92e1b6dSAlexander Bulekov     } else {
93d92e1b6dSAlexander Bulekov         __real_qtest_outw(s, addr, value);
94d92e1b6dSAlexander Bulekov     }
95d92e1b6dSAlexander Bulekov }
96d92e1b6dSAlexander Bulekov 
__wrap_qtest_outl(QTestState * s,uint16_t addr,uint32_t value)97d92e1b6dSAlexander Bulekov void __wrap_qtest_outl(QTestState *s, uint16_t addr, uint32_t value)
98d92e1b6dSAlexander Bulekov {
99d92e1b6dSAlexander Bulekov     if (!serialize) {
100d92e1b6dSAlexander Bulekov         cpu_outl(addr, value);
101d92e1b6dSAlexander Bulekov     } else {
102d92e1b6dSAlexander Bulekov         __real_qtest_outl(s, addr, value);
103d92e1b6dSAlexander Bulekov     }
104d92e1b6dSAlexander Bulekov }
105d92e1b6dSAlexander Bulekov 
__wrap_qtest_readb(QTestState * s,uint64_t addr)106d92e1b6dSAlexander Bulekov uint8_t __wrap_qtest_readb(QTestState *s, uint64_t addr)
107d92e1b6dSAlexander Bulekov {
108d92e1b6dSAlexander Bulekov     uint8_t value;
109d92e1b6dSAlexander Bulekov     if (!serialize) {
110d92e1b6dSAlexander Bulekov         address_space_read(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
111d92e1b6dSAlexander Bulekov                             &value, 1);
112d92e1b6dSAlexander Bulekov         return value;
113d92e1b6dSAlexander Bulekov     } else {
114d92e1b6dSAlexander Bulekov         return __real_qtest_readb(s, addr);
115d92e1b6dSAlexander Bulekov     }
116d92e1b6dSAlexander Bulekov }
117d92e1b6dSAlexander Bulekov 
__wrap_qtest_readw(QTestState * s,uint64_t addr)118d92e1b6dSAlexander Bulekov uint16_t __wrap_qtest_readw(QTestState *s, uint64_t addr)
119d92e1b6dSAlexander Bulekov {
120d92e1b6dSAlexander Bulekov     uint16_t value;
121d92e1b6dSAlexander Bulekov     if (!serialize) {
122d92e1b6dSAlexander Bulekov         address_space_read(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
123d92e1b6dSAlexander Bulekov                             &value, 2);
124d92e1b6dSAlexander Bulekov         return value;
125d92e1b6dSAlexander Bulekov     } else {
126d92e1b6dSAlexander Bulekov         return __real_qtest_readw(s, addr);
127d92e1b6dSAlexander Bulekov     }
128d92e1b6dSAlexander Bulekov }
129d92e1b6dSAlexander Bulekov 
__wrap_qtest_readl(QTestState * s,uint64_t addr)130d92e1b6dSAlexander Bulekov uint32_t __wrap_qtest_readl(QTestState *s, uint64_t addr)
131d92e1b6dSAlexander Bulekov {
132d92e1b6dSAlexander Bulekov     uint32_t value;
133d92e1b6dSAlexander Bulekov     if (!serialize) {
134d92e1b6dSAlexander Bulekov         address_space_read(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
135d92e1b6dSAlexander Bulekov                             &value, 4);
136d92e1b6dSAlexander Bulekov         return value;
137d92e1b6dSAlexander Bulekov     } else {
138d92e1b6dSAlexander Bulekov         return __real_qtest_readl(s, addr);
139d92e1b6dSAlexander Bulekov     }
140d92e1b6dSAlexander Bulekov }
141d92e1b6dSAlexander Bulekov 
__wrap_qtest_readq(QTestState * s,uint64_t addr)142d92e1b6dSAlexander Bulekov uint64_t __wrap_qtest_readq(QTestState *s, uint64_t addr)
143d92e1b6dSAlexander Bulekov {
144d92e1b6dSAlexander Bulekov     uint64_t value;
145d92e1b6dSAlexander Bulekov     if (!serialize) {
146d92e1b6dSAlexander Bulekov         address_space_read(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
147d92e1b6dSAlexander Bulekov                             &value, 8);
148d92e1b6dSAlexander Bulekov         return value;
149d92e1b6dSAlexander Bulekov     } else {
150d92e1b6dSAlexander Bulekov         return __real_qtest_readq(s, addr);
151d92e1b6dSAlexander Bulekov     }
152d92e1b6dSAlexander Bulekov }
153d92e1b6dSAlexander Bulekov 
__wrap_qtest_writeb(QTestState * s,uint64_t addr,uint8_t value)154d92e1b6dSAlexander Bulekov void __wrap_qtest_writeb(QTestState *s, uint64_t addr, uint8_t value)
155d92e1b6dSAlexander Bulekov {
156d92e1b6dSAlexander Bulekov     if (!serialize) {
157d92e1b6dSAlexander Bulekov         address_space_write(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
158d92e1b6dSAlexander Bulekov                             &value, 1);
159d92e1b6dSAlexander Bulekov     } else {
160d92e1b6dSAlexander Bulekov         __real_qtest_writeb(s, addr, value);
161d92e1b6dSAlexander Bulekov     }
162d92e1b6dSAlexander Bulekov }
163d92e1b6dSAlexander Bulekov 
__wrap_qtest_writew(QTestState * s,uint64_t addr,uint16_t value)164d92e1b6dSAlexander Bulekov void __wrap_qtest_writew(QTestState *s, uint64_t addr, uint16_t value)
165d92e1b6dSAlexander Bulekov {
166d92e1b6dSAlexander Bulekov     if (!serialize) {
167d92e1b6dSAlexander Bulekov         address_space_write(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
168d92e1b6dSAlexander Bulekov                             &value, 2);
169d92e1b6dSAlexander Bulekov     } else {
170d92e1b6dSAlexander Bulekov         __real_qtest_writew(s, addr, value);
171d92e1b6dSAlexander Bulekov     }
172d92e1b6dSAlexander Bulekov }
173d92e1b6dSAlexander Bulekov 
__wrap_qtest_writel(QTestState * s,uint64_t addr,uint32_t value)174d92e1b6dSAlexander Bulekov void __wrap_qtest_writel(QTestState *s, uint64_t addr, uint32_t value)
175d92e1b6dSAlexander Bulekov {
176d92e1b6dSAlexander Bulekov     if (!serialize) {
177d92e1b6dSAlexander Bulekov         address_space_write(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
178d92e1b6dSAlexander Bulekov                             &value, 4);
179d92e1b6dSAlexander Bulekov     } else {
180d92e1b6dSAlexander Bulekov         __real_qtest_writel(s, addr, value);
181d92e1b6dSAlexander Bulekov     }
182d92e1b6dSAlexander Bulekov }
183d92e1b6dSAlexander Bulekov 
__wrap_qtest_writeq(QTestState * s,uint64_t addr,uint64_t value)184d92e1b6dSAlexander Bulekov void __wrap_qtest_writeq(QTestState *s, uint64_t addr, uint64_t value)
185d92e1b6dSAlexander Bulekov {
186d92e1b6dSAlexander Bulekov     if (!serialize) {
187d92e1b6dSAlexander Bulekov         address_space_write(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
188d92e1b6dSAlexander Bulekov                             &value, 8);
189d92e1b6dSAlexander Bulekov     } else {
190d92e1b6dSAlexander Bulekov         __real_qtest_writeq(s, addr, value);
191d92e1b6dSAlexander Bulekov     }
192d92e1b6dSAlexander Bulekov }
193d92e1b6dSAlexander Bulekov 
__wrap_qtest_memread(QTestState * s,uint64_t addr,void * data,size_t size)194d92e1b6dSAlexander Bulekov void __wrap_qtest_memread(QTestState *s, uint64_t addr, void *data, size_t size)
195d92e1b6dSAlexander Bulekov {
196d92e1b6dSAlexander Bulekov     if (!serialize) {
197d92e1b6dSAlexander Bulekov         address_space_read(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED, data,
198d92e1b6dSAlexander Bulekov                            size);
199d92e1b6dSAlexander Bulekov     } else {
200d92e1b6dSAlexander Bulekov         __real_qtest_memread(s, addr, data, size);
201d92e1b6dSAlexander Bulekov     }
202d92e1b6dSAlexander Bulekov }
203d92e1b6dSAlexander Bulekov 
__wrap_qtest_bufread(QTestState * s,uint64_t addr,void * data,size_t size)204d92e1b6dSAlexander Bulekov void __wrap_qtest_bufread(QTestState *s, uint64_t addr, void *data, size_t size)
205d92e1b6dSAlexander Bulekov {
206d92e1b6dSAlexander Bulekov     if (!serialize) {
207d92e1b6dSAlexander Bulekov         address_space_read(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED, data,
208d92e1b6dSAlexander Bulekov                            size);
209d92e1b6dSAlexander Bulekov     } else {
210d92e1b6dSAlexander Bulekov         __real_qtest_bufread(s, addr, data, size);
211d92e1b6dSAlexander Bulekov     }
212d92e1b6dSAlexander Bulekov }
213d92e1b6dSAlexander Bulekov 
__wrap_qtest_memwrite(QTestState * s,uint64_t addr,const void * data,size_t size)214d92e1b6dSAlexander Bulekov void __wrap_qtest_memwrite(QTestState *s, uint64_t addr, const void *data,
215d92e1b6dSAlexander Bulekov                            size_t size)
216d92e1b6dSAlexander Bulekov {
217d92e1b6dSAlexander Bulekov     if (!serialize) {
218d92e1b6dSAlexander Bulekov         address_space_write(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
219d92e1b6dSAlexander Bulekov                             data, size);
220d92e1b6dSAlexander Bulekov     } else {
221d92e1b6dSAlexander Bulekov         __real_qtest_memwrite(s, addr, data, size);
222d92e1b6dSAlexander Bulekov     }
223d92e1b6dSAlexander Bulekov }
224d92e1b6dSAlexander Bulekov 
__wrap_qtest_bufwrite(QTestState * s,uint64_t addr,const void * data,size_t size)225d92e1b6dSAlexander Bulekov void __wrap_qtest_bufwrite(QTestState *s, uint64_t addr,
226d92e1b6dSAlexander Bulekov                     const void *data, size_t size)
227d92e1b6dSAlexander Bulekov {
228d92e1b6dSAlexander Bulekov     if (!serialize) {
229d92e1b6dSAlexander Bulekov         address_space_write(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
230d92e1b6dSAlexander Bulekov                             data, size);
231d92e1b6dSAlexander Bulekov     } else {
232d92e1b6dSAlexander Bulekov         __real_qtest_bufwrite(s, addr, data, size);
233d92e1b6dSAlexander Bulekov     }
234d92e1b6dSAlexander Bulekov }
__wrap_qtest_memset(QTestState * s,uint64_t addr,uint8_t patt,size_t size)235d92e1b6dSAlexander Bulekov void __wrap_qtest_memset(QTestState *s, uint64_t addr,
236d92e1b6dSAlexander Bulekov                          uint8_t patt, size_t size)
237d92e1b6dSAlexander Bulekov {
238d92e1b6dSAlexander Bulekov     void *data;
239d92e1b6dSAlexander Bulekov     if (!serialize) {
240d92e1b6dSAlexander Bulekov         data = malloc(size);
241d92e1b6dSAlexander Bulekov         memset(data, patt, size);
242d92e1b6dSAlexander Bulekov         address_space_write(first_cpu->as, addr, MEMTXATTRS_UNSPECIFIED,
243d92e1b6dSAlexander Bulekov                             data, size);
244d92e1b6dSAlexander Bulekov     } else {
245d92e1b6dSAlexander Bulekov         __real_qtest_memset(s, addr, patt, size);
246d92e1b6dSAlexander Bulekov     }
247d92e1b6dSAlexander Bulekov }
248d92e1b6dSAlexander Bulekov 
fuzz_qtest_set_serialize(bool option)249d92e1b6dSAlexander Bulekov void fuzz_qtest_set_serialize(bool option)
250d92e1b6dSAlexander Bulekov {
251d92e1b6dSAlexander Bulekov     serialize = option;
252d92e1b6dSAlexander Bulekov }
253