target/loongarch: Add constant timer support
[qemu.git] / tests / qtest / fdc-test.c
blob52ade90a7d0cc0ec2189329a3b1852639eb1ce3e
1 /*
2 * Floppy test cases.
4 * Copyright (c) 2012 Kevin Wolf <kwolf@redhat.com>
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
25 #include "qemu/osdep.h"
28 #include "libqtest-single.h"
29 #include "qapi/qmp/qdict.h"
31 /* TODO actually test the results and get rid of this */
32 #define qmp_discard_response(...) qobject_unref(qmp(__VA_ARGS__))
34 #define DRIVE_FLOPPY_BLANK \
35 "-drive if=floppy,file=null-co://,file.read-zeroes=on,format=raw,size=1440k"
37 #define TEST_IMAGE_SIZE 1440 * 1024
39 #define FLOPPY_BASE 0x3f0
40 #define FLOPPY_IRQ 6
42 enum {
43 reg_sra = 0x0,
44 reg_srb = 0x1,
45 reg_dor = 0x2,
46 reg_msr = 0x4,
47 reg_dsr = 0x4,
48 reg_fifo = 0x5,
49 reg_dir = 0x7,
52 enum {
53 CMD_SENSE_INT = 0x08,
54 CMD_READ_ID = 0x0a,
55 CMD_SEEK = 0x0f,
56 CMD_VERIFY = 0x16,
57 CMD_READ = 0xe6,
58 CMD_RELATIVE_SEEK_OUT = 0x8f,
59 CMD_RELATIVE_SEEK_IN = 0xcf,
62 enum {
63 BUSY = 0x10,
64 NONDMA = 0x20,
65 RQM = 0x80,
66 DIO = 0x40,
68 DSKCHG = 0x80,
71 static char test_image[] = "/tmp/qtest.XXXXXX";
73 #define assert_bit_set(data, mask) g_assert_cmphex((data) & (mask), ==, (mask))
74 #define assert_bit_clear(data, mask) g_assert_cmphex((data) & (mask), ==, 0)
76 static uint8_t base = 0x70;
78 enum {
79 CMOS_FLOPPY = 0x10,
82 static void floppy_send(uint8_t byte)
84 uint8_t msr;
86 msr = inb(FLOPPY_BASE + reg_msr);
87 assert_bit_set(msr, RQM);
88 assert_bit_clear(msr, DIO);
90 outb(FLOPPY_BASE + reg_fifo, byte);
93 static uint8_t floppy_recv(void)
95 uint8_t msr;
97 msr = inb(FLOPPY_BASE + reg_msr);
98 assert_bit_set(msr, RQM | DIO);
100 return inb(FLOPPY_BASE + reg_fifo);
103 /* pcn: Present Cylinder Number */
104 static void ack_irq(uint8_t *pcn)
106 uint8_t ret;
108 g_assert(get_irq(FLOPPY_IRQ));
109 floppy_send(CMD_SENSE_INT);
110 floppy_recv();
112 ret = floppy_recv();
113 if (pcn != NULL) {
114 *pcn = ret;
117 g_assert(!get_irq(FLOPPY_IRQ));
120 static uint8_t send_read_command(uint8_t cmd)
122 uint8_t drive = 0;
123 uint8_t head = 0;
124 uint8_t cyl = 0;
125 uint8_t sect_addr = 1;
126 uint8_t sect_size = 2;
127 uint8_t eot = 1;
128 uint8_t gap = 0x1b;
129 uint8_t gpl = 0xff;
131 uint8_t msr = 0;
132 uint8_t st0;
134 uint8_t ret = 0;
136 floppy_send(cmd);
137 floppy_send(head << 2 | drive);
138 g_assert(!get_irq(FLOPPY_IRQ));
139 floppy_send(cyl);
140 floppy_send(head);
141 floppy_send(sect_addr);
142 floppy_send(sect_size);
143 floppy_send(eot);
144 floppy_send(gap);
145 floppy_send(gpl);
147 uint8_t i = 0;
148 uint8_t n = 2;
149 for (; i < n; i++) {
150 msr = inb(FLOPPY_BASE + reg_msr);
151 if (msr == 0xd0) {
152 break;
154 sleep(1);
157 if (i >= n) {
158 return 1;
161 st0 = floppy_recv();
162 if (st0 != 0x40) {
163 ret = 1;
166 floppy_recv();
167 floppy_recv();
168 floppy_recv();
169 floppy_recv();
170 floppy_recv();
171 floppy_recv();
173 return ret;
176 static uint8_t send_read_no_dma_command(int nb_sect, uint8_t expected_st0)
178 uint8_t drive = 0;
179 uint8_t head = 0;
180 uint8_t cyl = 0;
181 uint8_t sect_addr = 1;
182 uint8_t sect_size = 2;
183 uint8_t eot = nb_sect;
184 uint8_t gap = 0x1b;
185 uint8_t gpl = 0xff;
187 uint8_t msr = 0;
188 uint8_t st0;
190 uint8_t ret = 0;
192 floppy_send(CMD_READ);
193 floppy_send(head << 2 | drive);
194 g_assert(!get_irq(FLOPPY_IRQ));
195 floppy_send(cyl);
196 floppy_send(head);
197 floppy_send(sect_addr);
198 floppy_send(sect_size);
199 floppy_send(eot);
200 floppy_send(gap);
201 floppy_send(gpl);
203 uint16_t i = 0;
204 uint8_t n = 2;
205 for (; i < n; i++) {
206 msr = inb(FLOPPY_BASE + reg_msr);
207 if (msr == (BUSY | NONDMA | DIO | RQM)) {
208 break;
210 sleep(1);
213 if (i >= n) {
214 return 1;
217 /* Non-DMA mode */
218 for (i = 0; i < 512 * 2 * nb_sect; i++) {
219 msr = inb(FLOPPY_BASE + reg_msr);
220 assert_bit_set(msr, BUSY | RQM | DIO);
221 inb(FLOPPY_BASE + reg_fifo);
224 msr = inb(FLOPPY_BASE + reg_msr);
225 assert_bit_set(msr, BUSY | RQM | DIO);
226 g_assert(get_irq(FLOPPY_IRQ));
228 st0 = floppy_recv();
229 if (st0 != expected_st0) {
230 ret = 1;
233 floppy_recv();
234 floppy_recv();
235 floppy_recv();
236 floppy_recv();
237 floppy_recv();
238 g_assert(get_irq(FLOPPY_IRQ));
239 floppy_recv();
241 /* Check that we're back in command phase */
242 msr = inb(FLOPPY_BASE + reg_msr);
243 assert_bit_clear(msr, BUSY | DIO);
244 assert_bit_set(msr, RQM);
245 g_assert(!get_irq(FLOPPY_IRQ));
247 return ret;
250 static void send_seek(int cyl)
252 int drive = 0;
253 int head = 0;
255 floppy_send(CMD_SEEK);
256 floppy_send(head << 2 | drive);
257 g_assert(!get_irq(FLOPPY_IRQ));
258 floppy_send(cyl);
259 ack_irq(NULL);
262 static uint8_t cmos_read(uint8_t reg)
264 outb(base + 0, reg);
265 return inb(base + 1);
268 static void test_cmos(void)
270 uint8_t cmos;
272 cmos = cmos_read(CMOS_FLOPPY);
273 g_assert(cmos == 0x40 || cmos == 0x50);
276 static void test_no_media_on_start(void)
278 uint8_t dir;
280 /* Media changed bit must be set all time after start if there is
281 * no media in drive. */
282 dir = inb(FLOPPY_BASE + reg_dir);
283 assert_bit_set(dir, DSKCHG);
284 dir = inb(FLOPPY_BASE + reg_dir);
285 assert_bit_set(dir, DSKCHG);
286 send_seek(1);
287 dir = inb(FLOPPY_BASE + reg_dir);
288 assert_bit_set(dir, DSKCHG);
289 dir = inb(FLOPPY_BASE + reg_dir);
290 assert_bit_set(dir, DSKCHG);
293 static void test_read_without_media(void)
295 uint8_t ret;
297 ret = send_read_command(CMD_READ);
298 g_assert(ret == 0);
301 static void test_media_insert(void)
303 uint8_t dir;
305 /* Insert media in drive. DSKCHK should not be reset until a step pulse
306 * is sent. */
307 qmp_discard_response("{'execute':'blockdev-change-medium', 'arguments':{"
308 " 'id':'floppy0', 'filename': %s, 'format': 'raw' }}",
309 test_image);
311 dir = inb(FLOPPY_BASE + reg_dir);
312 assert_bit_set(dir, DSKCHG);
313 dir = inb(FLOPPY_BASE + reg_dir);
314 assert_bit_set(dir, DSKCHG);
316 send_seek(0);
317 dir = inb(FLOPPY_BASE + reg_dir);
318 assert_bit_set(dir, DSKCHG);
319 dir = inb(FLOPPY_BASE + reg_dir);
320 assert_bit_set(dir, DSKCHG);
322 /* Step to next track should clear DSKCHG bit. */
323 send_seek(1);
324 dir = inb(FLOPPY_BASE + reg_dir);
325 assert_bit_clear(dir, DSKCHG);
326 dir = inb(FLOPPY_BASE + reg_dir);
327 assert_bit_clear(dir, DSKCHG);
330 static void test_media_change(void)
332 uint8_t dir;
334 test_media_insert();
336 /* Eject the floppy and check that DSKCHG is set. Reading it out doesn't
337 * reset the bit. */
338 qmp_discard_response("{'execute':'eject', 'arguments':{"
339 " 'id':'floppy0' }}");
341 dir = inb(FLOPPY_BASE + reg_dir);
342 assert_bit_set(dir, DSKCHG);
343 dir = inb(FLOPPY_BASE + reg_dir);
344 assert_bit_set(dir, DSKCHG);
346 send_seek(0);
347 dir = inb(FLOPPY_BASE + reg_dir);
348 assert_bit_set(dir, DSKCHG);
349 dir = inb(FLOPPY_BASE + reg_dir);
350 assert_bit_set(dir, DSKCHG);
352 send_seek(1);
353 dir = inb(FLOPPY_BASE + reg_dir);
354 assert_bit_set(dir, DSKCHG);
355 dir = inb(FLOPPY_BASE + reg_dir);
356 assert_bit_set(dir, DSKCHG);
359 static void test_sense_interrupt(void)
361 int drive = 0;
362 int head = 0;
363 int cyl = 0;
364 int ret = 0;
366 floppy_send(CMD_SENSE_INT);
367 ret = floppy_recv();
368 g_assert(ret == 0x80);
370 floppy_send(CMD_SEEK);
371 floppy_send(head << 2 | drive);
372 g_assert(!get_irq(FLOPPY_IRQ));
373 floppy_send(cyl);
375 floppy_send(CMD_SENSE_INT);
376 ret = floppy_recv();
377 g_assert(ret == 0x20);
378 floppy_recv();
381 static void test_relative_seek(void)
383 uint8_t drive = 0;
384 uint8_t head = 0;
385 uint8_t cyl = 1;
386 uint8_t pcn;
388 /* Send seek to track 0 */
389 send_seek(0);
391 /* Send relative seek to increase track by 1 */
392 floppy_send(CMD_RELATIVE_SEEK_IN);
393 floppy_send(head << 2 | drive);
394 g_assert(!get_irq(FLOPPY_IRQ));
395 floppy_send(cyl);
397 ack_irq(&pcn);
398 g_assert(pcn == 1);
400 /* Send relative seek to decrease track by 1 */
401 floppy_send(CMD_RELATIVE_SEEK_OUT);
402 floppy_send(head << 2 | drive);
403 g_assert(!get_irq(FLOPPY_IRQ));
404 floppy_send(cyl);
406 ack_irq(&pcn);
407 g_assert(pcn == 0);
410 static void test_read_id(void)
412 uint8_t drive = 0;
413 uint8_t head = 0;
414 uint8_t cyl;
415 uint8_t st0;
416 uint8_t msr;
418 /* Seek to track 0 and check with READ ID */
419 send_seek(0);
421 floppy_send(CMD_READ_ID);
422 g_assert(!get_irq(FLOPPY_IRQ));
423 floppy_send(head << 2 | drive);
425 msr = inb(FLOPPY_BASE + reg_msr);
426 if (!get_irq(FLOPPY_IRQ)) {
427 assert_bit_set(msr, BUSY);
428 assert_bit_clear(msr, RQM);
431 while (!get_irq(FLOPPY_IRQ)) {
432 /* qemu involves a timer with READ ID... */
433 clock_step(1000000000LL / 50);
436 msr = inb(FLOPPY_BASE + reg_msr);
437 assert_bit_set(msr, BUSY | RQM | DIO);
439 st0 = floppy_recv();
440 floppy_recv();
441 floppy_recv();
442 cyl = floppy_recv();
443 head = floppy_recv();
444 floppy_recv();
445 g_assert(get_irq(FLOPPY_IRQ));
446 floppy_recv();
447 g_assert(!get_irq(FLOPPY_IRQ));
449 g_assert_cmpint(cyl, ==, 0);
450 g_assert_cmpint(head, ==, 0);
451 g_assert_cmpint(st0, ==, head << 2);
453 /* Seek to track 8 on head 1 and check with READ ID */
454 head = 1;
455 cyl = 8;
457 floppy_send(CMD_SEEK);
458 floppy_send(head << 2 | drive);
459 g_assert(!get_irq(FLOPPY_IRQ));
460 floppy_send(cyl);
461 g_assert(get_irq(FLOPPY_IRQ));
462 ack_irq(NULL);
464 floppy_send(CMD_READ_ID);
465 g_assert(!get_irq(FLOPPY_IRQ));
466 floppy_send(head << 2 | drive);
468 msr = inb(FLOPPY_BASE + reg_msr);
469 if (!get_irq(FLOPPY_IRQ)) {
470 assert_bit_set(msr, BUSY);
471 assert_bit_clear(msr, RQM);
474 while (!get_irq(FLOPPY_IRQ)) {
475 /* qemu involves a timer with READ ID... */
476 clock_step(1000000000LL / 50);
479 msr = inb(FLOPPY_BASE + reg_msr);
480 assert_bit_set(msr, BUSY | RQM | DIO);
482 st0 = floppy_recv();
483 floppy_recv();
484 floppy_recv();
485 cyl = floppy_recv();
486 head = floppy_recv();
487 floppy_recv();
488 g_assert(get_irq(FLOPPY_IRQ));
489 floppy_recv();
490 g_assert(!get_irq(FLOPPY_IRQ));
492 g_assert_cmpint(cyl, ==, 8);
493 g_assert_cmpint(head, ==, 1);
494 g_assert_cmpint(st0, ==, head << 2);
497 static void test_read_no_dma_1(void)
499 uint8_t ret;
501 outb(FLOPPY_BASE + reg_dor, inb(FLOPPY_BASE + reg_dor) & ~0x08);
502 send_seek(0);
503 ret = send_read_no_dma_command(1, 0x04);
504 g_assert(ret == 0);
507 static void test_read_no_dma_18(void)
509 uint8_t ret;
511 outb(FLOPPY_BASE + reg_dor, inb(FLOPPY_BASE + reg_dor) & ~0x08);
512 send_seek(0);
513 ret = send_read_no_dma_command(18, 0x04);
514 g_assert(ret == 0);
517 static void test_read_no_dma_19(void)
519 uint8_t ret;
521 outb(FLOPPY_BASE + reg_dor, inb(FLOPPY_BASE + reg_dor) & ~0x08);
522 send_seek(0);
523 ret = send_read_no_dma_command(19, 0x20);
524 g_assert(ret == 0);
527 static void test_verify(void)
529 uint8_t ret;
531 ret = send_read_command(CMD_VERIFY);
532 g_assert(ret == 0);
535 /* success if no crash or abort */
536 static void fuzz_registers(void)
538 unsigned int i;
540 for (i = 0; i < 1000; i++) {
541 uint8_t reg, val;
543 reg = (uint8_t)g_test_rand_int_range(0, 8);
544 val = (uint8_t)g_test_rand_int_range(0, 256);
546 outb(FLOPPY_BASE + reg, val);
547 inb(FLOPPY_BASE + reg);
551 static bool qtest_check_clang_sanitizer(void)
553 #ifdef QEMU_SANITIZE_ADDRESS
554 return true;
555 #else
556 g_test_skip("QEMU not configured using --enable-sanitizers");
557 return false;
558 #endif
560 static void test_cve_2021_20196(void)
562 QTestState *s;
564 if (!qtest_check_clang_sanitizer()) {
565 return;
568 s = qtest_initf("-nographic -m 32M -nodefaults " DRIVE_FLOPPY_BLANK);
570 qtest_outw(s, 0x3f4, 0x0500);
571 qtest_outb(s, 0x3f5, 0x00);
572 qtest_outb(s, 0x3f5, 0x00);
573 qtest_outw(s, 0x3f4, 0x0000);
574 qtest_outb(s, 0x3f5, 0x00);
575 qtest_outw(s, 0x3f1, 0x0400);
576 qtest_outw(s, 0x3f4, 0x0000);
577 qtest_outw(s, 0x3f4, 0x0000);
578 qtest_outb(s, 0x3f5, 0x00);
579 qtest_outb(s, 0x3f5, 0x01);
580 qtest_outw(s, 0x3f1, 0x0500);
581 qtest_outb(s, 0x3f5, 0x00);
582 qtest_quit(s);
585 static void test_cve_2021_3507(void)
587 QTestState *s;
589 s = qtest_initf("-nographic -m 32M -nodefaults "
590 "-drive file=%s,format=raw,if=floppy,snapshot=on",
591 test_image);
592 qtest_outl(s, 0x9, 0x0a0206);
593 qtest_outw(s, 0x3f4, 0x1600);
594 qtest_outw(s, 0x3f4, 0x0000);
595 qtest_outw(s, 0x3f4, 0x0000);
596 qtest_outw(s, 0x3f4, 0x0000);
597 qtest_outw(s, 0x3f4, 0x0200);
598 qtest_outw(s, 0x3f4, 0x0200);
599 qtest_outw(s, 0x3f4, 0x0000);
600 qtest_outw(s, 0x3f4, 0x0000);
601 qtest_outw(s, 0x3f4, 0x0000);
602 qtest_quit(s);
605 int main(int argc, char **argv)
607 int fd;
608 int ret;
610 /* Create a temporary raw image */
611 fd = mkstemp(test_image);
612 g_assert(fd >= 0);
613 ret = ftruncate(fd, TEST_IMAGE_SIZE);
614 g_assert(ret == 0);
615 close(fd);
617 /* Run the tests */
618 g_test_init(&argc, &argv, NULL);
620 qtest_start("-machine pc -device floppy,id=floppy0");
621 qtest_irq_intercept_in(global_qtest, "ioapic");
622 qtest_add_func("/fdc/cmos", test_cmos);
623 qtest_add_func("/fdc/no_media_on_start", test_no_media_on_start);
624 qtest_add_func("/fdc/read_without_media", test_read_without_media);
625 qtest_add_func("/fdc/media_change", test_media_change);
626 qtest_add_func("/fdc/sense_interrupt", test_sense_interrupt);
627 qtest_add_func("/fdc/relative_seek", test_relative_seek);
628 qtest_add_func("/fdc/read_id", test_read_id);
629 qtest_add_func("/fdc/verify", test_verify);
630 qtest_add_func("/fdc/media_insert", test_media_insert);
631 qtest_add_func("/fdc/read_no_dma_1", test_read_no_dma_1);
632 qtest_add_func("/fdc/read_no_dma_18", test_read_no_dma_18);
633 qtest_add_func("/fdc/read_no_dma_19", test_read_no_dma_19);
634 qtest_add_func("/fdc/fuzz-registers", fuzz_registers);
635 qtest_add_func("/fdc/fuzz/cve_2021_20196", test_cve_2021_20196);
636 qtest_add_func("/fdc/fuzz/cve_2021_3507", test_cve_2021_3507);
638 ret = g_test_run();
640 /* Cleanup */
641 qtest_end();
642 unlink(test_image);
644 return ret;