aboutsummaryrefslogtreecommitdiffstats
path: root/lib/lufa/Demos/Device/LowLevel/DualVirtualSerial/DualVirtualSerial.h
blob: 56a510b7a0d0577cc6cca25ebd12252e95c1f934 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
             LUFA Library
     Copyright (C) Dean Camera, 2017.

  dean [at] fourwalledcubicle [dot] com
           www.lufa-lib.org
*/

/*
  Copyright 2017  Dean Camera (dean [at] fourwalledcubicle [dot] com)

  Permission to use, copy, modify, distribute, and sell this
  software and its documentation for any purpose is hereby granted
  without fee, provided that the above copyright notice appear in
  all copies and that both that the copyright notice and this
  permission notice and warranty disclaimer appear in supporting
  documentation, and that the name of the author not be used in
  advertising or publicity pertaining to distribution of the
  software without specific, written prior permission.

  The author disclaims all warranties with regard to this
  software, including all implied warranties of merchantability
  and fitness.  In no event shall the author be liable for any
  special, indirect or consequential damages or any damages
  whatsoever resulting from loss of use, data or profits, whether
  in an action of contract, negligence or other tortious action,
  arising out of or in connection with the use or performance of
  this software.
*/

/** \file
 *
 *  Header file for DualVirtualSerial.c.
 */

#ifndef _DUAL_VIRTUALSERIAL_H_
#define _DUAL_VIRTUALSERIAL_H_

	/* Includes: */
		#include <avr/io.h>
		#include <avr/wdt.h>
		#include <avr/power.h>
		#include <avr/interrupt.h>
		#include <string.h>

		#include "Descriptors.h"

		#include <LUFA/Drivers/USB/USB.h>
		#include <LUFA/Drivers/Board/Joystick.h>
		#include <LUFA/Drivers/Board/LEDs.h>
		#include <LUFA/Platform/Platform.h>

	/* Macros: */
		/** LED mask for the library LED driver, to indicate that the USB interface is not ready. */
		#define LEDMASK_USB_NOTREADY      LEDS_LED1

		/** LED mask for the library LED driver, to indicate that the USB interface is enumerating. */
		#define LEDMASK_USB_ENUMERATING  (LEDS_LED2 | LEDS_LED3)

		/** LED mask for the library LED driver, to indicate that the USB interface is ready. */
		#define LEDMASK_USB_READY        (LEDS_LED2 | LEDS_LED4)

		/** LED mask for the library LED driver, to indicate that an error has occurred in the USB interface. */
		#define LEDMASK_USB_ERROR        (LEDS_LED1 | LEDS_LED3)

	/* Function Prototypes: */
		void CDC1_Task(void);
		void CDC2_Task(void);
		void SetupHardware(void);

		void EVENT_USB_Device_Connect(void);
		void EVENT_USB_Device_Disconnect(void);
		void EVENT_USB_Device_ConfigurationChanged(void);
		void EVENT_USB_Device_ControlRequest(void);

#endif
9' href='#n409'>409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956
/*
 * vmx_io.c: handling I/O, interrupts related VMX entry/exit
 * Copyright (c) 2004, Intel Corporation.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place - Suite 330, Boston, MA 02111-1307 USA.
 *
 */

#include <xen/config.h>
#include <xen/init.h>
#include <xen/mm.h>
#include <xen/lib.h>
#include <xen/errno.h>
#include <xen/trace.h>
#include <xen/event.h>

#include <asm/current.h>
#include <asm/cpufeature.h>
#include <asm/processor.h>
#include <asm/msr.h>
#include <asm/vmx.h>
#include <asm/vmx_vmcs.h>
#include <asm/vmx_platform.h>
#include <asm/vmx_virpit.h>
#include <asm/apic.h>
#include <asm/shadow.h>

#include <asm/vmx_vlapic.h>
#include <public/io/ioreq.h>
#include <public/io/vmx_vpic.h>

#ifdef CONFIG_VMX
#if defined (__i386__)
void load_cpu_user_regs(struct cpu_user_regs *regs)
{
    /*
     * Write the guest register value into VMCS
     */
    __vmwrite(GUEST_SS_SELECTOR, regs->ss);
    __vmwrite(GUEST_RSP, regs->esp);

    __vmwrite(GUEST_RFLAGS, regs->eflags);
    if (regs->eflags & EF_TF)
        __vm_set_bit(EXCEPTION_BITMAP, EXCEPTION_BITMAP_DB);
    else
        __vm_clear_bit(EXCEPTION_BITMAP, EXCEPTION_BITMAP_DB);

    __vmwrite(GUEST_CS_SELECTOR, regs->cs);
    __vmwrite(GUEST_RIP, regs->eip);
}

static void set_reg_value (int size, int index, int seg, struct cpu_user_regs *regs, long value)
{
    switch (size) {
    case BYTE:
        switch (index) {
        case 0:
            regs->eax &= 0xFFFFFF00;
            regs->eax |= (value & 0xFF);
            break;
        case 1:
            regs->ecx &= 0xFFFFFF00;
            regs->ecx |= (value & 0xFF);
            break;
        case 2:
            regs->edx &= 0xFFFFFF00;
            regs->edx |= (value & 0xFF);
            break;
        case 3:
            regs->ebx &= 0xFFFFFF00;
            regs->ebx |= (value & 0xFF);
            break;
        case 4:
            regs->eax &= 0xFFFF00FF;
            regs->eax |= ((value & 0xFF) << 8);
            break;
        case 5:
            regs->ecx &= 0xFFFF00FF;
            regs->ecx |= ((value & 0xFF) << 8);
            break;
        case 6:
            regs->edx &= 0xFFFF00FF;
            regs->edx |= ((value & 0xFF) << 8);
            break;
        case 7:
            regs->ebx &= 0xFFFF00FF;
            regs->ebx |= ((value & 0xFF) << 8);
            break;
        default:
            printk("Error: size:%x, index:%x are invalid!\n", size, index);
            domain_crash_synchronous();
            break;
        }
        break;
    case WORD:
        switch (index) {
        case 0:
            regs->eax &= 0xFFFF0000;
            regs->eax |= (value & 0xFFFF);
            break;
        case 1:
            regs->ecx &= 0xFFFF0000;
            regs->ecx |= (value & 0xFFFF);
            break;
        case 2:
            regs->edx &= 0xFFFF0000;
            regs->edx |= (value & 0xFFFF);
            break;
        case 3:
            regs->ebx &= 0xFFFF0000;
            regs->ebx |= (value & 0xFFFF);
            break;
        case 4:
            regs->esp &= 0xFFFF0000;
            regs->esp |= (value & 0xFFFF);
            break;
        case 5:
            regs->ebp &= 0xFFFF0000;
            regs->ebp |= (value & 0xFFFF);
            break;
        case 6:
            regs->esi &= 0xFFFF0000;
            regs->esi |= (value & 0xFFFF);
            break;
        case 7:
            regs->edi &= 0xFFFF0000;
            regs->edi |= (value & 0xFFFF);
            break;
        default:
            printk("Error: size:%x, index:%x are invalid!\n", size, index);
            domain_crash_synchronous();
            break;
        }
        break;
    case LONG:
        switch (index) {
        case 0:
            regs->eax = value;
            break;
        case 1:
            regs->ecx = value;
            break;
        case 2:
            regs->edx = value;
            break;
        case 3:
            regs->ebx = value;
            break;
        case 4:
            regs->esp = value;
            break;
        case 5:
            regs->ebp = value;
            break;
        case 6:
            regs->esi = value;
            break;
        case 7:
            regs->edi = value;
            break;
        default:
            printk("Error: size:%x, index:%x are invalid!\n", size, index);
            domain_crash_synchronous();
            break;
        }
        break;
    default:
        printk("Error: size:%x, index:%x are invalid!\n", size, index);
        domain_crash_synchronous();
        break;
    }
}
#else
void load_cpu_user_regs(struct cpu_user_regs *regs)
{
    __vmwrite(GUEST_SS_SELECTOR, regs->ss);
    __vmwrite(GUEST_RSP, regs->rsp);

    __vmwrite(GUEST_RFLAGS, regs->rflags);
    if (regs->rflags & EF_TF)
        __vm_set_bit(EXCEPTION_BITMAP, EXCEPTION_BITMAP_DB);
    else
        __vm_clear_bit(EXCEPTION_BITMAP, EXCEPTION_BITMAP_DB);

    __vmwrite(GUEST_CS_SELECTOR, regs->cs);
    __vmwrite(GUEST_RIP, regs->rip);
}

static inline void __set_reg_value(unsigned long *reg, int size, long value)
{
    switch (size) {
    case BYTE_64:
        *reg &= ~0xFF;
        *reg |= (value & 0xFF);
        break;
    case WORD:
        *reg &= ~0xFFFF;
        *reg |= (value & 0xFFFF);
        break;
    case LONG:
        *reg &= ~0xFFFFFFFF;
        *reg |= (value & 0xFFFFFFFF);
        break;
    case QUAD:
        *reg = value;
        break;
    default:
        printk("Error: <__set_reg_value>: size:%x is invalid\n", size);
        domain_crash_synchronous();
    }
}

static void set_reg_value (int size, int index, int seg, struct cpu_user_regs *regs, long value)
{
    if (size == BYTE) {
        switch (index) {
        case 0:
            regs->rax &= ~0xFF;
            regs->rax |= (value & 0xFF);
            break;
        case 1:
            regs->rcx &= ~0xFF;
            regs->rcx |= (value & 0xFF);
            break;
        case 2:
            regs->rdx &= ~0xFF;
            regs->rdx |= (value & 0xFF);
            break;
        case 3:
            regs->rbx &= ~0xFF;
            regs->rbx |= (value & 0xFF);
            break;
        case 4:
            regs->rax &= 0xFFFFFFFFFFFF00FF;
            regs->rax |= ((value & 0xFF) << 8);
            break;
        case 5:
            regs->rcx &= 0xFFFFFFFFFFFF00FF;
            regs->rcx |= ((value & 0xFF) << 8);
            break;
        case 6:
            regs->rdx &= 0xFFFFFFFFFFFF00FF;
            regs->rdx |= ((value & 0xFF) << 8);
            break;
        case 7:
            regs->rbx &= 0xFFFFFFFFFFFF00FF;
            regs->rbx |= ((value & 0xFF) << 8);
            break;
        default:
            printk("Error: size:%x, index:%x are invalid!\n", size, index);
            domain_crash_synchronous();
            break;
        }
        return;
    }

    switch (index) {
    case 0:
        __set_reg_value(&regs->rax, size, value);
        break;
    case 1:
        __set_reg_value(&regs->rcx, size, value);
        break;
    case 2:
        __set_reg_value(&regs->rdx, size, value);
        break;
    case 3:
        __set_reg_value(&regs->rbx, size, value);
        break;
    case 4:
        __set_reg_value(&regs->rsp, size, value);
        break;
    case 5:
        __set_reg_value(&regs->rbp, size, value);
        break;
    case 6:
        __set_reg_value(&regs->rsi, size, value);
        break;
    case 7:
        __set_reg_value(&regs->rdi, size, value);
        break;
    case 8:
        __set_reg_value(&regs->r8, size, value);
        break;
    case 9:
        __set_reg_value(&regs->r9, size, value);
        break;
    case 10:
        __set_reg_value(&regs->r10, size, value);
        break;
    case 11:
        __set_reg_value(&regs->r11, size, value);
        break;
    case 12:
        __set_reg_value(&regs->r12, size, value);
        break;
    case 13:
        __set_reg_value(&regs->r13, size, value);
        break;
    case 14:
        __set_reg_value(&regs->r14, size, value);
        break;
    case 15:
        __set_reg_value(&regs->r15, size, value);
        break;
    default:
        printk("Error: <set_reg_value> Invalid index\n");
        domain_crash_synchronous();
    }
    return;
}
#endif

extern long get_reg_value(int size, int index, int seg, struct cpu_user_regs *regs);

static inline void set_eflags_CF(int size, unsigned long v1,
                                 unsigned long v2, struct cpu_user_regs *regs)
{
    unsigned long mask = (1 << (8 * size)) - 1;

    if ((v1 & mask) > (v2 & mask))
        regs->eflags |= X86_EFLAGS_CF;
    else
        regs->eflags &= ~X86_EFLAGS_CF;
}

static inline void set_eflags_OF(int size, unsigned long v1,
                                 unsigned long v2, unsigned long v3, struct cpu_user_regs *regs)
{
    if ((v3 ^ v2) & (v3 ^ v1) & (1 << ((8 * size) - 1)))
        regs->eflags |= X86_EFLAGS_OF;
}

static inline void set_eflags_AF(int size, unsigned long v1,
                                 unsigned long v2, unsigned long v3, struct cpu_user_regs *regs)
{
    if ((v1 ^ v2 ^ v3) & 0x10)
        regs->eflags |= X86_EFLAGS_AF;
}

static inline void set_eflags_ZF(int size, unsigned long v1,
                                 struct cpu_user_regs *regs)
{
    unsigned long mask = (1 << (8 * size)) - 1;

    if ((v1 & mask) == 0)
        regs->eflags |= X86_EFLAGS_ZF;
}

static inline void set_eflags_SF(int size, unsigned long v1,
                                 struct cpu_user_regs *regs)
{
    if (v1 & (1 << ((8 * size) - 1)))
        regs->eflags |= X86_EFLAGS_SF;
}

static char parity_table[256] = {
    1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
    0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
    0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
    1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
    0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
    1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
    1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
    0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
    0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
    1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
    1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
    0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
    1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
    0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
    0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
    1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1
};

static inline void set_eflags_PF(int size, unsigned long v1,
                                 struct cpu_user_regs *regs)
{
    if (parity_table[v1 & 0xFF])
        regs->eflags |= X86_EFLAGS_PF;
}

static void vmx_pio_assist(struct cpu_user_regs *regs, ioreq_t *p,
                           struct mmio_op *mmio_opp)
{
    unsigned long old_eax;
    int sign = p->df ? -1 : 1;

    if (p->dir == IOREQ_WRITE) {
        if (p->pdata_valid) {
            regs->esi += sign * p->count * p->size;
            if (mmio_opp->flags & REPZ)
                regs->ecx -= p->count;
        }
    } else {
        if (mmio_opp->flags & OVERLAP) {
            unsigned long addr;

            regs->edi += sign * p->count * p->size;
            if (mmio_opp->flags & REPZ)
                regs->ecx -= p->count;

            addr = regs->edi;
            if (sign > 0)
                addr -= p->size;
            vmx_copy(&p->u.data, addr, p->size, VMX_COPY_OUT);
        } else if (p->pdata_valid) {
            regs->edi += sign * p->count * p->size;
            if (mmio_opp->flags & REPZ)
                regs->ecx -= p->count;
        } else {
            old_eax = regs->eax;
            switch (p->size) {
            case 1:
                regs->eax = (old_eax & 0xffffff00) | (p->u.data & 0xff);
                break;
            case 2:
                regs->eax = (old_eax & 0xffff0000) | (p->u.data & 0xffff);
                break;
            case 4:
                regs->eax = (p->u.data & 0xffffffff);
                break;
            default:
                printk("Error: %s unknown port size\n", __FUNCTION__);
                domain_crash_synchronous();
            }
        }
    }
}

static void vmx_mmio_assist(struct cpu_user_regs *regs, ioreq_t *p,
                            struct mmio_op *mmio_opp)
{
    int sign = p->df ? -1 : 1;
    int size = -1, index = -1;
    unsigned long value = 0, diff = 0;
    unsigned long src, dst;

    src = mmio_opp->operand[0];
    dst = mmio_opp->operand[1];
    size = operand_size(src);

    switch (mmio_opp->instr) {
    case INSTR_MOV:
        if (dst & REGISTER) {
            index = operand_index(dst);
            set_reg_value(size, index, 0, regs, p->u.data);
        }
        break;

    case INSTR_MOVZ:
        if (dst & REGISTER) {
            index = operand_index(dst);
            switch (size) {
            case BYTE: p->u.data = p->u.data & 0xFFULL; break;
            case WORD: p->u.data = p->u.data & 0xFFFFULL; break;
            case LONG: p->u.data = p->u.data & 0xFFFFFFFFULL; break;
            }
            set_reg_value(operand_size(dst), index, 0, regs, p->u.data);
        }
        break;

    case INSTR_MOVS:
        sign = p->df ? -1 : 1;
        regs->esi += sign * p->count * p->size;
        regs->edi += sign * p->count * p->size;

        if ((mmio_opp->flags & OVERLAP) && p->dir == IOREQ_READ) {
            unsigned long addr = regs->edi;

            if (sign > 0)
                addr -= p->size;
            vmx_copy(&p->u.data, addr, p->size, VMX_COPY_OUT);
        }

        if (mmio_opp->flags & REPZ)
            regs->ecx -= p->count;
        break;

    case INSTR_STOS:
        sign = p->df ? -1 : 1;
        regs->edi += sign * p->count * p->size;
        if (mmio_opp->flags & REPZ)
            regs->ecx -= p->count;
        break;

    case INSTR_AND:
        if (src & REGISTER) {
            index = operand_index(src);
            value = get_reg_value(size, index, 0, regs);
            diff = (unsigned long) p->u.data & value;
        } else if (src & IMMEDIATE) {
            value = mmio_opp->immediate;
            diff = (unsigned long) p->u.data & value;
        } else if (src & MEMORY) {
            index = operand_index(dst);
            value = get_reg_value(size, index, 0, regs);
            diff = (unsigned long) p->u.data & value;
            set_reg_value(size, index, 0, regs, diff);
        }

        /*
         * The OF and CF flags are cleared; the SF, ZF, and PF
         * flags are set according to the result. The state of
         * the AF flag is undefined.
         */
        regs->eflags &= ~(X86_EFLAGS_CF|X86_EFLAGS_PF|
                          X86_EFLAGS_ZF|X86_EFLAGS_SF|X86_EFLAGS_OF);
        set_eflags_ZF(size, diff, regs);
        set_eflags_SF(size, diff, regs);
        set_eflags_PF(size, diff, regs);
        break;

    case INSTR_OR:
        if (src & REGISTER) {
            index = operand_index(src);
            value = get_reg_value(size, index, 0, regs);
            diff = (unsigned long) p->u.data | value;
        } else if (src & IMMEDIATE) {
            value = mmio_opp->immediate;
            diff = (unsigned long) p->u.data | value;
        } else if (src & MEMORY) {
            index = operand_index(dst);
            value = get_reg_value(size, index, 0, regs);
            diff = (unsigned long) p->u.data | value;
            set_reg_value(size, index, 0, regs, diff);
        }

        /*
         * The OF and CF flags are cleared; the SF, ZF, and PF
         * flags are set according to the result. The state of
         * the AF flag is undefined.
         */
        regs->eflags &= ~(X86_EFLAGS_CF|X86_EFLAGS_PF|
                          X86_EFLAGS_ZF|X86_EFLAGS_SF|X86_EFLAGS_OF);
        set_eflags_ZF(size, diff, regs);
        set_eflags_SF(size, diff, regs);
        set_eflags_PF(size, diff, regs);
        break;

    case INSTR_XOR:
        if (src & REGISTER) {
            index = operand_index(src);
            value = get_reg_value(size, index, 0, regs);
            diff = (unsigned long) p->u.data ^ value;
        } else if (src & IMMEDIATE) {
            value = mmio_opp->immediate;
            diff = (unsigned long) p->u.data ^ value;
        } else if (src & MEMORY) {
            index = operand_index(dst);
            value = get_reg_value(size, index, 0, regs);
            diff = (unsigned long) p->u.data ^ value;
            set_reg_value(size, index, 0, regs, diff);
        }

        /*
         * The OF and CF flags are cleared; the SF, ZF, and PF
         * flags are set according to the result. The state of
         * the AF flag is undefined.
         */
        regs->eflags &= ~(X86_EFLAGS_CF|X86_EFLAGS_PF|
                          X86_EFLAGS_ZF|X86_EFLAGS_SF|X86_EFLAGS_OF);
        set_eflags_ZF(size, diff, regs);
        set_eflags_SF(size, diff, regs);
        set_eflags_PF(size, diff, regs);
        break;

    case INSTR_CMP:
        if (src & REGISTER) {
            index = operand_index(src);
            value = get_reg_value(size, index, 0, regs);
            diff = (unsigned long) p->u.data - value;
        } else if (src & IMMEDIATE) {
            value = mmio_opp->immediate;
            diff = (unsigned long) p->u.data - value;
        } else if (src & MEMORY) {
            index = operand_index(dst);
            value = get_reg_value(size, index, 0, regs);
            diff = value - (unsigned long) p->u.data;
        }

        /*
         * The CF, OF, SF, ZF, AF, and PF flags are set according
         * to the result
         */
        regs->eflags &= ~(X86_EFLAGS_CF|X86_EFLAGS_PF|X86_EFLAGS_AF|
                          X86_EFLAGS_ZF|X86_EFLAGS_SF|X86_EFLAGS_OF);
        set_eflags_CF(size, value, (unsigned long) p->u.data, regs);
        set_eflags_OF(size, diff, value, (unsigned long) p->u.data, regs);
        set_eflags_AF(size, diff, value, (unsigned long) p->u.data, regs);
        set_eflags_ZF(size, diff, regs);
        set_eflags_SF(size, diff, regs);
        set_eflags_PF(size, diff, regs);
        break;

    case INSTR_TEST:
        if (src & REGISTER) {
            index = operand_index(src);
            value = get_reg_value(size, index, 0, regs);
        } else if (src & IMMEDIATE) {
            value = mmio_opp->immediate;
        } else if (src & MEMORY) {
            index = operand_index(dst);
            value = get_reg_value(size, index, 0, regs);
        }
        diff = (unsigned long) p->u.data & value;

        /*
         * Sets the SF, ZF, and PF status flags. CF and OF are set to 0
         */
        regs->eflags &= ~(X86_EFLAGS_CF|X86_EFLAGS_PF|
                          X86_EFLAGS_ZF|X86_EFLAGS_SF|X86_EFLAGS_OF);
        set_eflags_ZF(size, diff, regs);
        set_eflags_SF(size, diff, regs);
        set_eflags_PF(size, diff, regs);
        break;

    case INSTR_BT:
        index = operand_index(src);
        value = get_reg_value(size, index, 0, regs);

        if (p->u.data & (1 << (value & ((1 << 5) - 1))))
            regs->eflags |= X86_EFLAGS_CF;
        else
            regs->eflags &= ~X86_EFLAGS_CF;

        break;
    }

    load_cpu_user_regs(regs);
}

void vmx_io_assist(struct vcpu *v)
{
    vcpu_iodata_t *vio;
    ioreq_t *p;
    struct cpu_user_regs *regs = guest_cpu_user_regs();
    struct mmio_op *mmio_opp;
    struct cpu_user_regs *inst_decoder_regs;

    mmio_opp = &v->arch.arch_vmx.mmio_op;
    inst_decoder_regs = mmio_opp->inst_decoder_regs;

    vio = get_vio(v->domain, v->vcpu_id);

    if (vio == 0) {
        VMX_DBG_LOG(DBG_LEVEL_1,
                    "bad shared page: %lx", (unsigned long) vio);
        printf("bad shared page: %lx\n", (unsigned long) vio);
        domain_crash_synchronous();
    }

    p = &vio->vp_ioreq;
    if (p->state == STATE_IORESP_HOOK)
        vmx_hooks_assist(v);

    /* clear IO wait VMX flag */
    if (test_bit(ARCH_VMX_IO_WAIT, &v->arch.arch_vmx.flags)) {
        if (p->state == STATE_IORESP_READY) {
            p->state = STATE_INVALID;
            clear_bit(ARCH_VMX_IO_WAIT, &v->arch.arch_vmx.flags);

            if (p->type == IOREQ_TYPE_PIO)
                vmx_pio_assist(regs, p, mmio_opp);
            else
                vmx_mmio_assist(regs, p, mmio_opp);
        }
        /* else an interrupt send event raced us */
    }
}

int vmx_clear_pending_io_event(struct vcpu *v)
{
    struct domain *d = v->domain;
    int port = iopacket_port(d);

    /* evtchn_pending_sel bit is shared by other event channels. */
    if (!d->shared_info->evtchn_pending[port/BITS_PER_LONG])
        clear_bit(port/BITS_PER_LONG, &v->vcpu_info->evtchn_pending_sel);

    /* Note: VMX domains may need upcalls as well. */
    if (!v->vcpu_info->evtchn_pending_sel)
        clear_bit(0, &v->vcpu_info->evtchn_upcall_pending);

    /* Clear the pending bit for port. */
    return test_and_clear_bit(port, &d->shared_info->evtchn_pending[0]);
}

/* Because we've cleared the pending events first, we need to guarantee that
 * all events to be handled by xen for VMX domains are taken care of here.
 *
 * interrupts are guaranteed to be checked before resuming guest.
 * VMX upcalls have been already arranged for if necessary.
 */
void vmx_check_events(struct vcpu *v)
{
    /* clear the event *before* checking for work. This should avoid
       the set-and-check races */
    if (vmx_clear_pending_io_event(current))
        vmx_io_assist(v);
}

/* On exit from vmx_wait_io, we're guaranteed to have a I/O response from
   the device model */
void vmx_wait_io()
{
    extern void do_block();
    int port = iopacket_port(current->domain);

    do {
        if (!test_bit(port, &current->domain->shared_info->evtchn_pending[0]))
            do_block();

        vmx_check_events(current);
        if (!test_bit(ARCH_VMX_IO_WAIT, &current->arch.arch_vmx.flags))
            break;
        /* Events other than IOPACKET_PORT might have woken us up. In that
           case, safely go back to sleep. */
        clear_bit(port/BITS_PER_LONG, &current->vcpu_info->evtchn_pending_sel);
        clear_bit(0, &current->vcpu_info->evtchn_upcall_pending);
    } while(1);
}

/* Simple minded Local APIC priority implementation. Fix later */
static __inline__ int find_highest_irq(u32 *pintr)
{
    if (pintr[7])
        return __fls(pintr[7]) + (256-32*1);
    if (pintr[6])
        return __fls(pintr[6]) + (256-32*2);
    if (pintr[5])
        return __fls(pintr[5]) + (256-32*3);
    if (pintr[4])
        return __fls(pintr[4]) + (256-32*4);
    if (pintr[3])
        return __fls(pintr[3]) + (256-32*5);
    if (pintr[2])
        return __fls(pintr[2]) + (256-32*6);
    if (pintr[1])
        return __fls(pintr[1]) + (256-32*7);
    return __fls(pintr[0]);
}

#define BSP_CPU(v)    (!(v->vcpu_id))
static inline void
interrupt_post_injection(struct vcpu * v, int vector, int type)
{
    struct vmx_virpit *vpit = &(v->domain->arch.vmx_platform.vmx_pit);
    u64    drift;

    if ( is_pit_irq(v, vector, type) ) {
        if ( !vpit->first_injected ) {
            vpit->first_injected = 1;
            vpit->pending_intr_nr = 0;
        } else {
            vpit->pending_intr_nr--;
        }
        vpit->inject_point = NOW();
        drift = vpit->period_cycles * vpit->pending_intr_nr;
        drift = v->arch.arch_vmx.tsc_offset - drift;
        __vmwrite(TSC_OFFSET, drift);

#if defined (__i386__)
        __vmwrite(TSC_OFFSET_HIGH, (drift >> 32));
#endif

    }

    switch(type)
    {
    case VLAPIC_DELIV_MODE_EXT:
        break;

    default:
        vlapic_post_injection(v, vector, type);
        break;
    }
}

static inline void
enable_irq_window(unsigned long cpu_exec_control)
{
    if (!(cpu_exec_control & CPU_BASED_VIRTUAL_INTR_PENDING)) {
        cpu_exec_control |= CPU_BASED_VIRTUAL_INTR_PENDING;
        __vmwrite(CPU_BASED_VM_EXEC_CONTROL, cpu_exec_control);
    }
}

static inline void
disable_irq_window(unsigned long cpu_exec_control)
{
    if ( cpu_exec_control & CPU_BASED_VIRTUAL_INTR_PENDING ) {
        cpu_exec_control &= ~CPU_BASED_VIRTUAL_INTR_PENDING;
        __vmwrite(CPU_BASED_VM_EXEC_CONTROL, cpu_exec_control);
    }
}

static inline int irq_masked(unsigned long eflags)
{
    return ((eflags & X86_EFLAGS_IF) == 0);
}

void pic_irq_request(int *interrupt_request, int level)
{
    if (level)
        *interrupt_request = 1;
    else
        *interrupt_request = 0;
}

void vmx_pic_assist(struct vcpu *v)
{
    global_iodata_t *spg;
    u16   *virq_line, irqs;
    struct vmx_virpic *pic = &v->domain->arch.vmx_platform.vmx_pic;
    
    spg = &get_sp(v->domain)->sp_global;
    virq_line  = &spg->pic_clear_irr;
    if ( *virq_line ) {
        do {
            irqs = *(volatile u16*)virq_line;
        } while ( (u16)cmpxchg(virq_line,irqs, 0) != irqs );
        do_pic_irqs_clear(pic, irqs);
    }
    virq_line  = &spg->pic_irr;
    if ( *virq_line ) {
        do {
            irqs = *(volatile u16*)virq_line;
        } while ( (u16)cmpxchg(virq_line,irqs, 0) != irqs );
        do_pic_irqs(pic, irqs);
    }

}

int cpu_get_interrupt(struct vcpu *v, int *type)
{
    int intno;
    struct vmx_virpic *s = &v->domain->arch.vmx_platform.vmx_pic;

    if ( (intno = cpu_get_apic_interrupt(v, type)) != -1 ) {
        /* set irq request if a PIC irq is still pending */
        /* XXX: improve that */
        pic_update_irq(s);
        return intno;
    }
    /* read the irq from the PIC */
    if ( (intno = cpu_get_pic_interrupt(v, type)) != -1 )
        return intno;

    return -1;
}

asmlinkage void vmx_intr_assist(void)
{
    int intr_type = 0;
    int highest_vector;
    unsigned long intr_fields, eflags, interruptibility, cpu_exec_control;
    struct vcpu *v = current;
    struct vmx_platform *plat=&v->domain->arch.vmx_platform;
    struct vmx_virpit *vpit = &plat->vmx_pit;
    struct vmx_virpic *pic= &plat->vmx_pic;

    vmx_pic_assist(v);
    __vmread_vcpu(v, CPU_BASED_VM_EXEC_CONTROL, &cpu_exec_control);
    if ( vpit->pending_intr_nr ) {
        pic_set_irq(pic, 0, 0);
        pic_set_irq(pic, 0, 1);
    }

    __vmread(VM_ENTRY_INTR_INFO_FIELD, &intr_fields);

    if (intr_fields & INTR_INFO_VALID_MASK) {
        VMX_DBG_LOG(DBG_LEVEL_1, "vmx_intr_assist: intr_fields: %lx",
                    intr_fields);
        return;
    }

    __vmread(GUEST_INTERRUPTIBILITY_INFO, &interruptibility);

    if (interruptibility) {
        enable_irq_window(cpu_exec_control);
        VMX_DBG_LOG(DBG_LEVEL_1, "interruptibility: %lx",interruptibility);
        return;
    }

    __vmread(GUEST_RFLAGS, &eflags);
    if (irq_masked(eflags)) {
        enable_irq_window(cpu_exec_control);
        return;
    }

    highest_vector = cpu_get_interrupt(v, &intr_type); 

    if (highest_vector == -1) {
        disable_irq_window(cpu_exec_control);
        return;
    }

    switch (intr_type) {
    case VLAPIC_DELIV_MODE_EXT:
    case VLAPIC_DELIV_MODE_FIXED:
    case VLAPIC_DELIV_MODE_LPRI:
        vmx_inject_extint(v, highest_vector, VMX_INVALID_ERROR_CODE);
        TRACE_3D(TRC_VMX_INT, v->domain->domain_id, highest_vector, 0);
        break;
    case VLAPIC_DELIV_MODE_SMI:
    case VLAPIC_DELIV_MODE_NMI:
    case VLAPIC_DELIV_MODE_INIT:
    case VLAPIC_DELIV_MODE_STARTUP:
    default:
        printk("Unsupported interrupt type\n");
        BUG();
        break;
    }

    interrupt_post_injection(v, highest_vector, intr_type);
    return;
}

void vmx_do_resume(struct vcpu *v)
{
    vmx_stts();

    if (event_pending(v)) {
        vmx_check_events(v);

        if (test_bit(ARCH_VMX_IO_WAIT, &v->arch.arch_vmx.flags))
            vmx_wait_io();
    }

    /* We can't resume the guest if we're waiting on I/O */
    ASSERT(!test_bit(ARCH_VMX_IO_WAIT, &v->arch.arch_vmx.flags));
}

#endif /* CONFIG_VMX */

/*
 * Local variables:
 * mode: C
 * c-set-style: "BSD"
 * c-basic-offset: 4
 * tab-width: 4
 * indent-tabs-mode: nil
 * End:
 */