diff options
| author | Rusty Russell <rusty@rustcorp.com.au> | 2015-02-13 01:43:44 -0500 |
|---|---|---|
| committer | Rusty Russell <rusty@rustcorp.com.au> | 2015-02-13 01:45:53 -0500 |
| commit | 1e1c17a7a2e5c585926eefffbea8a61d7a03a247 (patch) | |
| tree | 1b47fdc5f3a65e89e11d179930a3cdbbecc14e01 | |
| parent | 17c56d6de8e809ac57bf4c93d504f5336eb03dd1 (diff) | |
tools/lguest: use common error macros in the example launcher.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
| -rw-r--r-- | tools/lguest/lguest.c | 206 |
1 files changed, 105 insertions, 101 deletions
diff --git a/tools/lguest/lguest.c b/tools/lguest/lguest.c index 70ee62a0eb9a..eebe94b84e8c 100644 --- a/tools/lguest/lguest.c +++ b/tools/lguest/lguest.c | |||
| @@ -252,6 +252,16 @@ static struct termios orig_term; | |||
| 252 | #define le32_to_cpu(v32) (v32) | 252 | #define le32_to_cpu(v32) (v32) |
| 253 | #define le64_to_cpu(v64) (v64) | 253 | #define le64_to_cpu(v64) (v64) |
| 254 | 254 | ||
| 255 | /* | ||
| 256 | * A real device would ignore weird/non-compliant driver behaviour. We | ||
| 257 | * stop and flag it, to help debugging Linux problems. | ||
| 258 | */ | ||
| 259 | #define bad_driver(d, fmt, ...) \ | ||
| 260 | errx(1, "%s: bad driver: " fmt, (d)->name, ## __VA_ARGS__) | ||
| 261 | #define bad_driver_vq(vq, fmt, ...) \ | ||
| 262 | errx(1, "%s vq %s: bad driver: " fmt, (vq)->dev->name, \ | ||
| 263 | vq->name, ## __VA_ARGS__) | ||
| 264 | |||
| 255 | /* Is this iovec empty? */ | 265 | /* Is this iovec empty? */ |
| 256 | static bool iov_empty(const struct iovec iov[], unsigned int num_iov) | 266 | static bool iov_empty(const struct iovec iov[], unsigned int num_iov) |
| 257 | { | 267 | { |
| @@ -264,7 +274,8 @@ static bool iov_empty(const struct iovec iov[], unsigned int num_iov) | |||
| 264 | } | 274 | } |
| 265 | 275 | ||
| 266 | /* Take len bytes from the front of this iovec. */ | 276 | /* Take len bytes from the front of this iovec. */ |
| 267 | static void iov_consume(struct iovec iov[], unsigned num_iov, | 277 | static void iov_consume(struct device *d, |
| 278 | struct iovec iov[], unsigned num_iov, | ||
| 268 | void *dest, unsigned len) | 279 | void *dest, unsigned len) |
| 269 | { | 280 | { |
| 270 | unsigned int i; | 281 | unsigned int i; |
| @@ -282,7 +293,7 @@ static void iov_consume(struct iovec iov[], unsigned num_iov, | |||
| 282 | len -= used; | 293 | len -= used; |
| 283 | } | 294 | } |
| 284 | if (len != 0) | 295 | if (len != 0) |
| 285 | errx(1, "iovec too short!"); | 296 | bad_driver(d, "iovec too short!"); |
| 286 | } | 297 | } |
| 287 | 298 | ||
| 288 | /*L:100 | 299 | /*L:100 |
| @@ -618,7 +629,8 @@ static void tell_kernel(unsigned long start) | |||
| 618 | * we have a convenient routine which checks it and exits with an error message | 629 | * we have a convenient routine which checks it and exits with an error message |
| 619 | * if something funny is going on: | 630 | * if something funny is going on: |
| 620 | */ | 631 | */ |
| 621 | static void *_check_pointer(unsigned long addr, unsigned int size, | 632 | static void *_check_pointer(struct device *d, |
| 633 | unsigned long addr, unsigned int size, | ||
| 622 | unsigned int line) | 634 | unsigned int line) |
| 623 | { | 635 | { |
| 624 | /* | 636 | /* |
| @@ -626,7 +638,8 @@ static void *_check_pointer(unsigned long addr, unsigned int size, | |||
| 626 | * or addr + size wraps around. | 638 | * or addr + size wraps around. |
| 627 | */ | 639 | */ |
| 628 | if ((addr + size) > guest_limit || (addr + size) < addr) | 640 | if ((addr + size) > guest_limit || (addr + size) < addr) |
| 629 | errx(1, "%s:%i: Invalid address %#lx", __FILE__, line, addr); | 641 | bad_driver(d, "%s:%i: Invalid address %#lx", |
| 642 | __FILE__, line, addr); | ||
| 630 | /* | 643 | /* |
| 631 | * We return a pointer for the caller's convenience, now we know it's | 644 | * We return a pointer for the caller's convenience, now we know it's |
| 632 | * safe to use. | 645 | * safe to use. |
| @@ -634,14 +647,14 @@ static void *_check_pointer(unsigned long addr, unsigned int size, | |||
| 634 | return from_guest_phys(addr); | 647 | return from_guest_phys(addr); |
| 635 | } | 648 | } |
| 636 | /* A macro which transparently hands the line number to the real function. */ | 649 | /* A macro which transparently hands the line number to the real function. */ |
| 637 | #define check_pointer(addr,size) _check_pointer(addr, size, __LINE__) | 650 | #define check_pointer(d,addr,size) _check_pointer(d, addr, size, __LINE__) |
| 638 | 651 | ||
| 639 | /* | 652 | /* |
| 640 | * Each buffer in the virtqueues is actually a chain of descriptors. This | 653 | * Each buffer in the virtqueues is actually a chain of descriptors. This |
| 641 | * function returns the next descriptor in the chain, or vq->vring.num if we're | 654 | * function returns the next descriptor in the chain, or vq->vring.num if we're |
| 642 | * at the end. | 655 | * at the end. |
| 643 | */ | 656 | */ |
| 644 | static unsigned next_desc(struct vring_desc *desc, | 657 | static unsigned next_desc(struct device *d, struct vring_desc *desc, |
| 645 | unsigned int i, unsigned int max) | 658 | unsigned int i, unsigned int max) |
| 646 | { | 659 | { |
| 647 | unsigned int next; | 660 | unsigned int next; |
| @@ -656,7 +669,7 @@ static unsigned next_desc(struct vring_desc *desc, | |||
| 656 | wmb(); | 669 | wmb(); |
| 657 | 670 | ||
| 658 | if (next >= max) | 671 | if (next >= max) |
| 659 | errx(1, "Desc next is %u", next); | 672 | bad_driver(d, "Desc next is %u", next); |
| 660 | 673 | ||
| 661 | return next; | 674 | return next; |
| 662 | } | 675 | } |
| @@ -681,8 +694,7 @@ static void trigger_irq(struct virtqueue *vq) | |||
| 681 | * The driver MUST set flags to 0 or 1. | 694 | * The driver MUST set flags to 0 or 1. |
| 682 | */ | 695 | */ |
| 683 | if (vq->vring.avail->flags > 1) | 696 | if (vq->vring.avail->flags > 1) |
| 684 | errx(1, "%s: avail->flags = %u\n", | 697 | bad_driver_vq(vq, "avail->flags = %u\n", vq->vring.avail->flags); |
| 685 | vq->dev->name, vq->vring.avail->flags); | ||
| 686 | 698 | ||
| 687 | /* | 699 | /* |
| 688 | * 2.4.7.2: | 700 | * 2.4.7.2: |
| @@ -769,8 +781,8 @@ static unsigned wait_for_vq_desc(struct virtqueue *vq, | |||
| 769 | 781 | ||
| 770 | /* Check it isn't doing very strange things with descriptor numbers. */ | 782 | /* Check it isn't doing very strange things with descriptor numbers. */ |
| 771 | if ((u16)(vq->vring.avail->idx - last_avail) > vq->vring.num) | 783 | if ((u16)(vq->vring.avail->idx - last_avail) > vq->vring.num) |
| 772 | errx(1, "Guest moved used index from %u to %u", | 784 | bad_driver_vq(vq, "Guest moved used index from %u to %u", |
| 773 | last_avail, vq->vring.avail->idx); | 785 | last_avail, vq->vring.avail->idx); |
| 774 | 786 | ||
| 775 | /* | 787 | /* |
| 776 | * Make sure we read the descriptor number *after* we read the ring | 788 | * Make sure we read the descriptor number *after* we read the ring |
| @@ -787,7 +799,7 @@ static unsigned wait_for_vq_desc(struct virtqueue *vq, | |||
| 787 | 799 | ||
| 788 | /* If their number is silly, that's a fatal mistake. */ | 800 | /* If their number is silly, that's a fatal mistake. */ |
| 789 | if (head >= vq->vring.num) | 801 | if (head >= vq->vring.num) |
| 790 | errx(1, "Guest says index %u is available", head); | 802 | bad_driver_vq(vq, "Guest says index %u is available", head); |
| 791 | 803 | ||
| 792 | /* When we start there are none of either input nor output. */ | 804 | /* When we start there are none of either input nor output. */ |
| 793 | *out_num = *in_num = 0; | 805 | *out_num = *in_num = 0; |
| @@ -817,8 +829,7 @@ static unsigned wait_for_vq_desc(struct virtqueue *vq, | |||
| 817 | */ | 829 | */ |
| 818 | if (!(vq->dev->features_accepted & | 830 | if (!(vq->dev->features_accepted & |
| 819 | (1<<VIRTIO_RING_F_INDIRECT_DESC))) | 831 | (1<<VIRTIO_RING_F_INDIRECT_DESC))) |
| 820 | errx(1, "%s: vq indirect not negotiated", | 832 | bad_driver_vq(vq, "vq indirect not negotiated"); |
| 821 | vq->dev->name); | ||
| 822 | 833 | ||
| 823 | /* | 834 | /* |
| 824 | * 2.4.5.3.1: | 835 | * 2.4.5.3.1: |
| @@ -828,8 +839,7 @@ static unsigned wait_for_vq_desc(struct virtqueue *vq, | |||
| 828 | * table per descriptor). | 839 | * table per descriptor). |
| 829 | */ | 840 | */ |
| 830 | if (desc != vq->vring.desc) | 841 | if (desc != vq->vring.desc) |
| 831 | errx(1, "%s: Indirect within indirect", | 842 | bad_driver_vq(vq, "Indirect within indirect"); |
| 832 | vq->dev->name); | ||
| 833 | 843 | ||
| 834 | /* | 844 | /* |
| 835 | * Proposed update VIRTIO-134 spells this out: | 845 | * Proposed update VIRTIO-134 spells this out: |
| @@ -838,11 +848,11 @@ static unsigned wait_for_vq_desc(struct virtqueue *vq, | |||
| 838 | * and VIRTQ_DESC_F_NEXT in flags. | 848 | * and VIRTQ_DESC_F_NEXT in flags. |
| 839 | */ | 849 | */ |
| 840 | if (desc[i].flags & VRING_DESC_F_NEXT) | 850 | if (desc[i].flags & VRING_DESC_F_NEXT) |
| 841 | errx(1, "%s: indirect and next together", | 851 | bad_driver_vq(vq, "indirect and next together"); |
| 842 | vq->dev->name); | ||
| 843 | 852 | ||
| 844 | if (desc[i].len % sizeof(struct vring_desc)) | 853 | if (desc[i].len % sizeof(struct vring_desc)) |
| 845 | errx(1, "Invalid size for indirect buffer table"); | 854 | bad_driver_vq(vq, |
| 855 | "Invalid size for indirect table"); | ||
| 846 | /* | 856 | /* |
| 847 | * 2.4.5.3.2: | 857 | * 2.4.5.3.2: |
| 848 | * | 858 | * |
| @@ -854,7 +864,7 @@ static unsigned wait_for_vq_desc(struct virtqueue *vq, | |||
| 854 | */ | 864 | */ |
| 855 | 865 | ||
| 856 | max = desc[i].len / sizeof(struct vring_desc); | 866 | max = desc[i].len / sizeof(struct vring_desc); |
| 857 | desc = check_pointer(desc[i].addr, desc[i].len); | 867 | desc = check_pointer(vq->dev, desc[i].addr, desc[i].len); |
| 858 | i = 0; | 868 | i = 0; |
| 859 | 869 | ||
| 860 | /* 2.4.5.3.1: | 870 | /* 2.4.5.3.1: |
| @@ -863,14 +873,14 @@ static unsigned wait_for_vq_desc(struct virtqueue *vq, | |||
| 863 | |||
