]> rtime.felk.cvut.cz Git - lisovros/qemu_apohw.git/blob - qemu-file.c
apohw: port A0B36APO labs matrix keyboard hardware emulation to QEMU 2.0.
[lisovros/qemu_apohw.git] / qemu-file.c
1 #include "qemu-common.h"
2 #include "qemu/iov.h"
3 #include "qemu/sockets.h"
4 #include "block/coroutine.h"
5 #include "migration/migration.h"
6 #include "migration/qemu-file.h"
7 #include "trace.h"
8
9 #define IO_BUF_SIZE 32768
10 #define MAX_IOV_SIZE MIN(IOV_MAX, 64)
11
12 struct QEMUFile {
13     const QEMUFileOps *ops;
14     void *opaque;
15
16     int64_t bytes_xfer;
17     int64_t xfer_limit;
18
19     int64_t pos; /* start of buffer when writing, end of buffer
20                     when reading */
21     int buf_index;
22     int buf_size; /* 0 when writing */
23     uint8_t buf[IO_BUF_SIZE];
24
25     struct iovec iov[MAX_IOV_SIZE];
26     unsigned int iovcnt;
27
28     int last_error;
29 };
30
31 typedef struct QEMUFileStdio {
32     FILE *stdio_file;
33     QEMUFile *file;
34 } QEMUFileStdio;
35
36 typedef struct QEMUFileSocket {
37     int fd;
38     QEMUFile *file;
39 } QEMUFileSocket;
40
41 static ssize_t socket_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,
42                                     int64_t pos)
43 {
44     QEMUFileSocket *s = opaque;
45     ssize_t len;
46     ssize_t size = iov_size(iov, iovcnt);
47
48     len = iov_send(s->fd, iov, iovcnt, 0, size);
49     if (len < size) {
50         len = -socket_error();
51     }
52     return len;
53 }
54
55 static int socket_get_fd(void *opaque)
56 {
57     QEMUFileSocket *s = opaque;
58
59     return s->fd;
60 }
61
62 static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
63 {
64     QEMUFileSocket *s = opaque;
65     ssize_t len;
66
67     for (;;) {
68         len = qemu_recv(s->fd, buf, size, 0);
69         if (len != -1) {
70             break;
71         }
72         if (socket_error() == EAGAIN) {
73             yield_until_fd_readable(s->fd);
74         } else if (socket_error() != EINTR) {
75             break;
76         }
77     }
78
79     if (len == -1) {
80         len = -socket_error();
81     }
82     return len;
83 }
84
85 static int socket_close(void *opaque)
86 {
87     QEMUFileSocket *s = opaque;
88     closesocket(s->fd);
89     g_free(s);
90     return 0;
91 }
92
93 static int stdio_get_fd(void *opaque)
94 {
95     QEMUFileStdio *s = opaque;
96
97     return fileno(s->stdio_file);
98 }
99
100 static int stdio_put_buffer(void *opaque, const uint8_t *buf, int64_t pos,
101                             int size)
102 {
103     QEMUFileStdio *s = opaque;
104     int res;
105
106     res = fwrite(buf, 1, size, s->stdio_file);
107
108     if (res != size) {
109         return -errno;
110     }
111     return res;
112 }
113
114 static int stdio_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
115 {
116     QEMUFileStdio *s = opaque;
117     FILE *fp = s->stdio_file;
118     int bytes;
119
120     for (;;) {
121         clearerr(fp);
122         bytes = fread(buf, 1, size, fp);
123         if (bytes != 0 || !ferror(fp)) {
124             break;
125         }
126         if (errno == EAGAIN) {
127             yield_until_fd_readable(fileno(fp));
128         } else if (errno != EINTR) {
129             break;
130         }
131     }
132     return bytes;
133 }
134
135 static int stdio_pclose(void *opaque)
136 {
137     QEMUFileStdio *s = opaque;
138     int ret;
139     ret = pclose(s->stdio_file);
140     if (ret == -1) {
141         ret = -errno;
142     } else if (!WIFEXITED(ret) || WEXITSTATUS(ret) != 0) {
143         /* close succeeded, but non-zero exit code: */
144         ret = -EIO; /* fake errno value */
145     }
146     g_free(s);
147     return ret;
148 }
149
150 static int stdio_fclose(void *opaque)
151 {
152     QEMUFileStdio *s = opaque;
153     int ret = 0;
154
155     if (s->file->ops->put_buffer || s->file->ops->writev_buffer) {
156         int fd = fileno(s->stdio_file);
157         struct stat st;
158
159         ret = fstat(fd, &st);
160         if (ret == 0 && S_ISREG(st.st_mode)) {
161             /*
162              * If the file handle is a regular file make sure the
163              * data is flushed to disk before signaling success.
164              */
165             ret = fsync(fd);
166             if (ret != 0) {
167                 ret = -errno;
168                 return ret;
169             }
170         }
171     }
172     if (fclose(s->stdio_file) == EOF) {
173         ret = -errno;
174     }
175     g_free(s);
176     return ret;
177 }
178
179 static const QEMUFileOps stdio_pipe_read_ops = {
180     .get_fd =     stdio_get_fd,
181     .get_buffer = stdio_get_buffer,
182     .close =      stdio_pclose
183 };
184
185 static const QEMUFileOps stdio_pipe_write_ops = {
186     .get_fd =     stdio_get_fd,
187     .put_buffer = stdio_put_buffer,
188     .close =      stdio_pclose
189 };
190
191 QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
192 {
193     FILE *stdio_file;
194     QEMUFileStdio *s;
195
196     if (mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
197         fprintf(stderr, "qemu_popen: Argument validity check failed\n");
198         return NULL;
199     }
200
201     stdio_file = popen(command, mode);
202     if (stdio_file == NULL) {
203         return NULL;
204     }
205
206     s = g_malloc0(sizeof(QEMUFileStdio));
207
208     s->stdio_file = stdio_file;
209
210     if (mode[0] == 'r') {
211         s->file = qemu_fopen_ops(s, &stdio_pipe_read_ops);
212     } else {
213         s->file = qemu_fopen_ops(s, &stdio_pipe_write_ops);
214     }
215     return s->file;
216 }
217
218 static const QEMUFileOps stdio_file_read_ops = {
219     .get_fd =     stdio_get_fd,
220     .get_buffer = stdio_get_buffer,
221     .close =      stdio_fclose
222 };
223
224 static const QEMUFileOps stdio_file_write_ops = {
225     .get_fd =     stdio_get_fd,
226     .put_buffer = stdio_put_buffer,
227     .close =      stdio_fclose
228 };
229
230 static ssize_t unix_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,
231                                   int64_t pos)
232 {
233     QEMUFileSocket *s = opaque;
234     ssize_t len, offset;
235     ssize_t size = iov_size(iov, iovcnt);
236     ssize_t total = 0;
237
238     assert(iovcnt > 0);
239     offset = 0;
240     while (size > 0) {
241         /* Find the next start position; skip all full-sized vector elements  */
242         while (offset >= iov[0].iov_len) {
243             offset -= iov[0].iov_len;
244             iov++, iovcnt--;
245         }
246
247         /* skip `offset' bytes from the (now) first element, undo it on exit */
248         assert(iovcnt > 0);
249         iov[0].iov_base += offset;
250         iov[0].iov_len -= offset;
251
252         do {
253             len = writev(s->fd, iov, iovcnt);
254         } while (len == -1 && errno == EINTR);
255         if (len == -1) {
256             return -errno;
257         }
258
259         /* Undo the changes above */
260         iov[0].iov_base -= offset;
261         iov[0].iov_len += offset;
262
263         /* Prepare for the next iteration */
264         offset += len;
265         total += len;
266         size -= len;
267     }
268
269     return total;
270 }
271
272 static int unix_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
273 {
274     QEMUFileSocket *s = opaque;
275     ssize_t len;
276
277     for (;;) {
278         len = read(s->fd, buf, size);
279         if (len != -1) {
280             break;
281         }
282         if (errno == EAGAIN) {
283             yield_until_fd_readable(s->fd);
284         } else if (errno != EINTR) {
285             break;
286         }
287     }
288
289     if (len == -1) {
290         len = -errno;
291     }
292     return len;
293 }
294
295 static int unix_close(void *opaque)
296 {
297     QEMUFileSocket *s = opaque;
298     close(s->fd);
299     g_free(s);
300     return 0;
301 }
302
303 static const QEMUFileOps unix_read_ops = {
304     .get_fd =     socket_get_fd,
305     .get_buffer = unix_get_buffer,
306     .close =      unix_close
307 };
308
309 static const QEMUFileOps unix_write_ops = {
310     .get_fd =     socket_get_fd,
311     .writev_buffer = unix_writev_buffer,
312     .close =      unix_close
313 };
314
315 QEMUFile *qemu_fdopen(int fd, const char *mode)
316 {
317     QEMUFileSocket *s;
318
319     if (mode == NULL ||
320         (mode[0] != 'r' && mode[0] != 'w') ||
321         mode[1] != 'b' || mode[2] != 0) {
322         fprintf(stderr, "qemu_fdopen: Argument validity check failed\n");
323         return NULL;
324     }
325
326     s = g_malloc0(sizeof(QEMUFileSocket));
327     s->fd = fd;
328
329     if (mode[0] == 'r') {
330         s->file = qemu_fopen_ops(s, &unix_read_ops);
331     } else {
332         s->file = qemu_fopen_ops(s, &unix_write_ops);
333     }
334     return s->file;
335 }
336
337 static const QEMUFileOps socket_read_ops = {
338     .get_fd =     socket_get_fd,
339     .get_buffer = socket_get_buffer,
340     .close =      socket_close
341 };
342
343 static const QEMUFileOps socket_write_ops = {
344     .get_fd =     socket_get_fd,
345     .writev_buffer = socket_writev_buffer,
346     .close =      socket_close
347 };
348
349 bool qemu_file_mode_is_not_valid(const char *mode)
350 {
351     if (mode == NULL ||
352         (mode[0] != 'r' && mode[0] != 'w') ||
353         mode[1] != 'b' || mode[2] != 0) {
354         fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
355         return true;
356     }
357
358     return false;
359 }
360
361 QEMUFile *qemu_fopen_socket(int fd, const char *mode)
362 {
363     QEMUFileSocket *s;
364
365     if (qemu_file_mode_is_not_valid(mode)) {
366         return NULL;
367     }
368
369     s = g_malloc0(sizeof(QEMUFileSocket));
370     s->fd = fd;
371     if (mode[0] == 'w') {
372         qemu_set_block(s->fd);
373         s->file = qemu_fopen_ops(s, &socket_write_ops);
374     } else {
375         s->file = qemu_fopen_ops(s, &socket_read_ops);
376     }
377     return s->file;
378 }
379
380 QEMUFile *qemu_fopen(const char *filename, const char *mode)
381 {
382     QEMUFileStdio *s;
383
384     if (qemu_file_mode_is_not_valid(mode)) {
385         return NULL;
386     }
387
388     s = g_malloc0(sizeof(QEMUFileStdio));
389
390     s->stdio_file = fopen(filename, mode);
391     if (!s->stdio_file) {
392         goto fail;
393     }
394
395     if (mode[0] == 'w') {
396         s->file = qemu_fopen_ops(s, &stdio_file_write_ops);
397     } else {
398         s->file = qemu_fopen_ops(s, &stdio_file_read_ops);
399     }
400     return s->file;
401 fail:
402     g_free(s);
403     return NULL;
404 }
405
406 QEMUFile *qemu_fopen_ops(void *opaque, const QEMUFileOps *ops)
407 {
408     QEMUFile *f;
409
410     f = g_malloc0(sizeof(QEMUFile));
411
412     f->opaque = opaque;
413     f->ops = ops;
414     return f;
415 }
416
417 /*
418  * Get last error for stream f
419  *
420  * Return negative error value if there has been an error on previous
421  * operations, return 0 if no error happened.
422  *
423  */
424 int qemu_file_get_error(QEMUFile *f)
425 {
426     return f->last_error;
427 }
428
429 void qemu_file_set_error(QEMUFile *f, int ret)
430 {
431     if (f->last_error == 0) {
432         f->last_error = ret;
433     }
434 }
435
436 static inline bool qemu_file_is_writable(QEMUFile *f)
437 {
438     return f->ops->writev_buffer || f->ops->put_buffer;
439 }
440
441 /**
442  * Flushes QEMUFile buffer
443  *
444  * If there is writev_buffer QEMUFileOps it uses it otherwise uses
445  * put_buffer ops.
446  */
447 void qemu_fflush(QEMUFile *f)
448 {
449     ssize_t ret = 0;
450
451     if (!qemu_file_is_writable(f)) {
452         return;
453     }
454
455     if (f->ops->writev_buffer) {
456         if (f->iovcnt > 0) {
457             ret = f->ops->writev_buffer(f->opaque, f->iov, f->iovcnt, f->pos);
458         }
459     } else {
460         if (f->buf_index > 0) {
461             ret = f->ops->put_buffer(f->opaque, f->buf, f->pos, f->buf_index);
462         }
463     }
464     if (ret >= 0) {
465         f->pos += ret;
466     }
467     f->buf_index = 0;
468     f->iovcnt = 0;
469     if (ret < 0) {
470         qemu_file_set_error(f, ret);
471     }
472 }
473
474 void ram_control_before_iterate(QEMUFile *f, uint64_t flags)
475 {
476     int ret = 0;
477
478     if (f->ops->before_ram_iterate) {
479         ret = f->ops->before_ram_iterate(f, f->opaque, flags);
480         if (ret < 0) {
481             qemu_file_set_error(f, ret);
482         }
483     }
484 }
485
486 void ram_control_after_iterate(QEMUFile *f, uint64_t flags)
487 {
488     int ret = 0;
489
490     if (f->ops->after_ram_iterate) {
491         ret = f->ops->after_ram_iterate(f, f->opaque, flags);
492         if (ret < 0) {
493             qemu_file_set_error(f, ret);
494         }
495     }
496 }
497
498 void ram_control_load_hook(QEMUFile *f, uint64_t flags)
499 {
500     int ret = -EINVAL;
501
502     if (f->ops->hook_ram_load) {
503         ret = f->ops->hook_ram_load(f, f->opaque, flags);
504         if (ret < 0) {
505             qemu_file_set_error(f, ret);
506         }
507     } else {
508         qemu_file_set_error(f, ret);
509     }
510 }
511
512 size_t ram_control_save_page(QEMUFile *f, ram_addr_t block_offset,
513                          ram_addr_t offset, size_t size, int *bytes_sent)
514 {
515     if (f->ops->save_page) {
516         int ret = f->ops->save_page(f, f->opaque, block_offset,
517                                     offset, size, bytes_sent);
518
519         if (ret != RAM_SAVE_CONTROL_DELAYED) {
520             if (bytes_sent && *bytes_sent > 0) {
521                 qemu_update_position(f, *bytes_sent);
522             } else if (ret < 0) {
523                 qemu_file_set_error(f, ret);
524             }
525         }
526
527         return ret;
528     }
529
530     return RAM_SAVE_CONTROL_NOT_SUPP;
531 }
532
533 static void qemu_fill_buffer(QEMUFile *f)
534 {
535     int len;
536     int pending;
537
538     assert(!qemu_file_is_writable(f));
539
540     pending = f->buf_size - f->buf_index;
541     if (pending > 0) {
542         memmove(f->buf, f->buf + f->buf_index, pending);
543     }
544     f->buf_index = 0;
545     f->buf_size = pending;
546
547     len = f->ops->get_buffer(f->opaque, f->buf + pending, f->pos,
548                         IO_BUF_SIZE - pending);
549     if (len > 0) {
550         f->buf_size += len;
551         f->pos += len;
552     } else if (len == 0) {
553         qemu_file_set_error(f, -EIO);
554     } else if (len != -EAGAIN) {
555         qemu_file_set_error(f, len);
556     }
557 }
558
559 int qemu_get_fd(QEMUFile *f)
560 {
561     if (f->ops->get_fd) {
562         return f->ops->get_fd(f->opaque);
563     }
564     return -1;
565 }
566
567 void qemu_update_position(QEMUFile *f, size_t size)
568 {
569     f->pos += size;
570 }
571
572 /** Closes the file
573  *
574  * Returns negative error value if any error happened on previous operations or
575  * while closing the file. Returns 0 or positive number on success.
576  *
577  * The meaning of return value on success depends on the specific backend
578  * being used.
579  */
580 int qemu_fclose(QEMUFile *f)
581 {
582     int ret;
583     qemu_fflush(f);
584     ret = qemu_file_get_error(f);
585
586     if (f->ops->close) {
587         int ret2 = f->ops->close(f->opaque);
588         if (ret >= 0) {
589             ret = ret2;
590         }
591     }
592     /* If any error was spotted before closing, we should report it
593      * instead of the close() return value.
594      */
595     if (f->last_error) {
596         ret = f->last_error;
597     }
598     g_free(f);
599     trace_qemu_file_fclose();
600     return ret;
601 }
602
603 static void add_to_iovec(QEMUFile *f, const uint8_t *buf, int size)
604 {
605     /* check for adjacent buffer and coalesce them */
606     if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base +
607         f->iov[f->iovcnt - 1].iov_len) {
608         f->iov[f->iovcnt - 1].iov_len += size;
609     } else {
610         f->iov[f->iovcnt].iov_base = (uint8_t *)buf;
611         f->iov[f->iovcnt++].iov_len = size;
612     }
613
614     if (f->iovcnt >= MAX_IOV_SIZE) {
615         qemu_fflush(f);
616     }
617 }
618
619 void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, int size)
620 {
621     if (!f->ops->writev_buffer) {
622         qemu_put_buffer(f, buf, size);
623         return;
624     }
625
626     if (f->last_error) {
627         return;
628     }
629
630     f->bytes_xfer += size;
631     add_to_iovec(f, buf, size);
632 }
633
634 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
635 {
636     int l;
637
638     if (f->last_error) {
639         return;
640     }
641
642     while (size > 0) {
643         l = IO_BUF_SIZE - f->buf_index;
644         if (l > size) {
645             l = size;
646         }
647         memcpy(f->buf + f->buf_index, buf, l);
648         f->bytes_xfer += l;
649         if (f->ops->writev_buffer) {
650             add_to_iovec(f, f->buf + f->buf_index, l);
651         }
652         f->buf_index += l;
653         if (f->buf_index == IO_BUF_SIZE) {
654             qemu_fflush(f);
655         }
656         if (qemu_file_get_error(f)) {
657             break;
658         }
659         buf += l;
660         size -= l;
661     }
662 }
663
664 void qemu_put_byte(QEMUFile *f, int v)
665 {
666     if (f->last_error) {
667         return;
668     }
669
670     f->buf[f->buf_index] = v;
671     f->bytes_xfer++;
672     if (f->ops->writev_buffer) {
673         add_to_iovec(f, f->buf + f->buf_index, 1);
674     }
675     f->buf_index++;
676     if (f->buf_index == IO_BUF_SIZE) {
677         qemu_fflush(f);
678     }
679 }
680
681 void qemu_file_skip(QEMUFile *f, int size)
682 {
683     if (f->buf_index + size <= f->buf_size) {
684         f->buf_index += size;
685     }
686 }
687
688 int qemu_peek_buffer(QEMUFile *f, uint8_t *buf, int size, size_t offset)
689 {
690     int pending;
691     int index;
692
693     assert(!qemu_file_is_writable(f));
694
695     index = f->buf_index + offset;
696     pending = f->buf_size - index;
697     if (pending < size) {
698         qemu_fill_buffer(f);
699         index = f->buf_index + offset;
700         pending = f->buf_size - index;
701     }
702
703     if (pending <= 0) {
704         return 0;
705     }
706     if (size > pending) {
707         size = pending;
708     }
709
710     memcpy(buf, f->buf + index, size);
711     return size;
712 }
713
714 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
715 {
716     int pending = size;
717     int done = 0;
718
719     while (pending > 0) {
720         int res;
721
722         res = qemu_peek_buffer(f, buf, pending, 0);
723         if (res == 0) {
724             return done;
725         }
726         qemu_file_skip(f, res);
727         buf += res;
728         pending -= res;
729         done += res;
730     }
731     return done;
732 }
733
734 int qemu_peek_byte(QEMUFile *f, int offset)
735 {
736     int index = f->buf_index + offset;
737
738     assert(!qemu_file_is_writable(f));
739
740     if (index >= f->buf_size) {
741         qemu_fill_buffer(f);
742         index = f->buf_index + offset;
743         if (index >= f->buf_size) {
744             return 0;
745         }
746     }
747     return f->buf[index];
748 }
749
750 int qemu_get_byte(QEMUFile *f)
751 {
752     int result;
753
754     result = qemu_peek_byte(f, 0);
755     qemu_file_skip(f, 1);
756     return result;
757 }
758
759 int64_t qemu_ftell(QEMUFile *f)
760 {
761     qemu_fflush(f);
762     return f->pos;
763 }
764
765 int qemu_file_rate_limit(QEMUFile *f)
766 {
767     if (qemu_file_get_error(f)) {
768         return 1;
769     }
770     if (f->xfer_limit > 0 && f->bytes_xfer > f->xfer_limit) {
771         return 1;
772     }
773     return 0;
774 }
775
776 int64_t qemu_file_get_rate_limit(QEMUFile *f)
777 {
778     return f->xfer_limit;
779 }
780
781 void qemu_file_set_rate_limit(QEMUFile *f, int64_t limit)
782 {
783     f->xfer_limit = limit;
784 }
785
786 void qemu_file_reset_rate_limit(QEMUFile *f)
787 {
788     f->bytes_xfer = 0;
789 }
790
791 void qemu_put_be16(QEMUFile *f, unsigned int v)
792 {
793     qemu_put_byte(f, v >> 8);
794     qemu_put_byte(f, v);
795 }
796
797 void qemu_put_be32(QEMUFile *f, unsigned int v)
798 {
799     qemu_put_byte(f, v >> 24);
800     qemu_put_byte(f, v >> 16);
801     qemu_put_byte(f, v >> 8);
802     qemu_put_byte(f, v);
803 }
804
805 void qemu_put_be64(QEMUFile *f, uint64_t v)
806 {
807     qemu_put_be32(f, v >> 32);
808     qemu_put_be32(f, v);
809 }
810
811 unsigned int qemu_get_be16(QEMUFile *f)
812 {
813     unsigned int v;
814     v = qemu_get_byte(f) << 8;
815     v |= qemu_get_byte(f);
816     return v;
817 }
818
819 unsigned int qemu_get_be32(QEMUFile *f)
820 {
821     unsigned int v;
822     v = qemu_get_byte(f) << 24;
823     v |= qemu_get_byte(f) << 16;
824     v |= qemu_get_byte(f) << 8;
825     v |= qemu_get_byte(f);
826     return v;
827 }
828
829 uint64_t qemu_get_be64(QEMUFile *f)
830 {
831     uint64_t v;
832     v = (uint64_t)qemu_get_be32(f) << 32;
833     v |= qemu_get_be32(f);
834     return v;
835 }