Merge branches 'stable/irq', 'stable/p2m.bugfixes', 'stable/e820.bugfixes' and 'stabl...
authorLinus Torvalds <torvalds@linux-foundation.org>
Thu, 19 May 2011 23:14:58 +0000 (16:14 -0700)
committerLinus Torvalds <torvalds@linux-foundation.org>
Thu, 19 May 2011 23:14:58 +0000 (16:14 -0700)
* 'stable/irq' of git://git.kernel.org/pub/scm/linux/kernel/git/konrad/xen:
  xen: do not clear and mask evtchns in __xen_evtchn_do_upcall

* 'stable/p2m.bugfixes' of git://git.kernel.org/pub/scm/linux/kernel/git/konrad/xen:
  xen/p2m: Create entries in the P2M_MFN trees's to track 1-1 mappings

* 'stable/e820.bugfixes' of git://git.kernel.org/pub/scm/linux/kernel/git/konrad/xen:
  xen/setup: Fix for incorrect xen_extra_mem_start initialization under 32-bit
  xen/setup: Ignore E820_UNUSABLE when setting 1-1 mappings.

* 'stable/mmu.bugfixes' of git://git.kernel.org/pub/scm/linux/kernel/git/konrad/xen:
  xen mmu: fix a race window causing leave_mm BUG()

1  2  3  4  5 
arch/x86/xen/mmu.c
arch/x86/xen/p2m.c
arch/x86/xen/setup.c
drivers/xen/events.c

diff --combined arch/x86/xen/mmu.c
     #include <linux/module.h>
     #include <linux/gfp.h>
     #include <linux/memblock.h>
    +#include <linux/seq_file.h>
     
     #include <asm/pgtable.h>
     #include <asm/tlbflush.h>
     
     /*
      * Protects atomic reservation decrease/increase against concurrent increases.
    - * Also protects non-atomic updates of current_pages and driver_pages, and
    - * balloon lists.
    + * Also protects non-atomic updates of current_pages and balloon lists.
      */
     DEFINE_SPINLOCK(xen_reservation_lock);
     
@@@@@@ -416,12 -416,12 -416,12 -416,12 -416,8 +416,12 @@@@@@ static pteval_t pte_pfn_to_mfn(pteval_
        if (val & _PAGE_PRESENT) {
                unsigned long pfn = (val & PTE_PFN_MASK) >> PAGE_SHIFT;
                pteval_t flags = val & PTE_FLAGS_MASK;
    -           unsigned long mfn = pfn_to_mfn(pfn);
    +           unsigned long mfn;
     
    +           if (!xen_feature(XENFEAT_auto_translated_physmap))
    +                   mfn = get_phys_to_machine(pfn);
    +           else
    +                   mfn = pfn;
                /*
                 * If there's no mfn for the pfn, then just create an
                 * empty non-present pte.  Unfortunately this loses
                if (unlikely(mfn == INVALID_P2M_ENTRY)) {
                        mfn = 0;
                        flags = 0;
    +           } else {
    +                   /*
    +                    * Paramount to do this test _after_ the
    +                    * INVALID_P2M_ENTRY as INVALID_P2M_ENTRY &
    +                    * IDENTITY_FRAME_BIT resolves to true.
    +                    */
    +                   mfn &= ~FOREIGN_FRAME_BIT;
    +                   if (mfn & IDENTITY_FRAME_BIT) {
    +                           mfn &= ~IDENTITY_FRAME_BIT;
    +                           flags |= _PAGE_IOMAP;
    +                   }
                }
    -
                val = ((pteval_t)mfn << PAGE_SHIFT) | flags;
        }
     
@@@@@@ -546,41 -546,41 -546,41 -546,41 -532,6 +546,41 @@@@@@ pte_t xen_make_pte(pteval_t pte
     }
     PV_CALLEE_SAVE_REGS_THUNK(xen_make_pte);
     
  -             WARN(addr != other_addr,
    +#ifdef CONFIG_XEN_DEBUG
    +pte_t xen_make_pte_debug(pteval_t pte)
    +{
    +   phys_addr_t addr = (pte & PTE_PFN_MASK);
    +   phys_addr_t other_addr;
    +   bool io_page = false;
    +   pte_t _pte;
    +
    +   if (pte & _PAGE_IOMAP)
    +           io_page = true;
    +
    +   _pte = xen_make_pte(pte);
    +
    +   if (!addr)
    +           return _pte;
    +
    +   if (io_page &&
    +       (xen_initial_domain() || addr >= ISA_END_ADDRESS)) {
    +           other_addr = pfn_to_mfn(addr >> PAGE_SHIFT) << PAGE_SHIFT;
  -             WARN((addr == other_addr) && (!io_page) && (!iomap_set),
  + +           WARN_ONCE(addr != other_addr,
    +                   "0x%lx is using VM_IO, but it is 0x%lx!\n",
    +                   (unsigned long)addr, (unsigned long)other_addr);
    +   } else {
    +           pteval_t iomap_set = (_pte.pte & PTE_FLAGS_MASK) & _PAGE_IOMAP;
    +           other_addr = (_pte.pte & PTE_PFN_MASK);
  + +           WARN_ONCE((addr == other_addr) && (!io_page) && (!iomap_set),
    +                   "0x%lx is missing VM_IO (and wasn't fixed)!\n",
    +                   (unsigned long)addr);
    +   }
    +
    +   return _pte;
    +}
    +PV_CALLEE_SAVE_REGS_THUNK(xen_make_pte_debug);
    +#endif
    +
     pgd_t xen_make_pgd(pgdval_t pgd)
     {
        pgd = pte_pfn_to_mfn(pgd);
@@@@@@ -1035,9 -1035,9 -1035,9 -1035,9 -986,10 +1035,9 @@@@@@ static void xen_pgd_pin(struct mm_struc
      */
     void xen_mm_pin_all(void)
     {
    -   unsigned long flags;
        struct page *page;
     
    -   spin_lock_irqsave(&pgd_lock, flags);
    +   spin_lock(&pgd_lock);
     
        list_for_each_entry(page, &pgd_list, lru) {
                if (!PagePinned(page)) {
                }
        }
     
    -   spin_unlock_irqrestore(&pgd_lock, flags);
    +   spin_unlock(&pgd_lock);
     }
     
     /*
      * that's before we have page structures to store the bits.  So do all
      * the book-keeping now.
      */
 ----static __init int xen_mark_pinned(struct mm_struct *mm, struct page *page,
 ++++static int __init xen_mark_pinned(struct mm_struct *mm, struct page *page,
                                  enum pt_level level)
     {
        SetPagePinned(page);
@@@@@@ -1147,9 -1147,9 -1147,9 -1147,9 -1099,10 +1147,9 @@@@@@ static void xen_pgd_unpin(struct mm_str
      */
     void xen_mm_unpin_all(void)
     {
    -   unsigned long flags;
        struct page *page;
     
    -   spin_lock_irqsave(&pgd_lock, flags);
    +   spin_lock(&pgd_lock);
     
        list_for_each_entry(page, &pgd_list, lru) {
                if (PageSavePinned(page)) {
                }
        }
     
    -   spin_unlock_irqrestore(&pgd_lock, flags);
    +   spin_unlock(&pgd_lock);
     }
     
     void xen_activate_mm(struct mm_struct *prev, struct mm_struct *next)
@@@@@@ -1187,7 -1187,7 -1187,7 -1187,7 -1140,7 +1187,7 @@@@@@ static void drop_other_mm_ref(void *inf
     
        active_mm = percpu_read(cpu_tlbstate.active_mm);
     
----    if (active_mm == mm)
++++    if (active_mm == mm && percpu_read(cpu_tlbstate.state) != TLBSTATE_OK)
                leave_mm(smp_processor_id());
     
        /* If this cpu still has a stale cr3 reference, then make sure
@@@@@@ -1271,27 -1271,13 -1271,13 -1271,13 -1224,13 +1271,27 @@@@@@ void xen_exit_mmap(struct mm_struct *mm
        spin_unlock(&mm->page_table_lock);
     }
     
 ----static __init void xen_pagetable_setup_start(pgd_t *base)
 ++++static void __init xen_pagetable_setup_start(pgd_t *base)
     {
     }
     
 ++++static __init void xen_mapping_pagetable_reserve(u64 start, u64 end)
 ++++{
 ++++   /* reserve the range used */
 ++++   native_pagetable_reserve(start, end);
 ++++
 ++++   /* set as RW the rest */
 ++++   printk(KERN_DEBUG "xen: setting RW the range %llx - %llx\n", end,
 ++++                   PFN_PHYS(pgt_buf_top));
 ++++   while (end < PFN_PHYS(pgt_buf_top)) {
 ++++           make_lowmem_page_readwrite(__va(end));
 ++++           end += PAGE_SIZE;
 ++++   }
 ++++}
 ++++
     static void xen_post_allocator_init(void);
     
 ----static __init void xen_pagetable_setup_done(pgd_t *base)
 ++++static void __init xen_pagetable_setup_done(pgd_t *base)
     {
        xen_setup_shared_info();
        xen_post_allocator_init();
@@@@@@ -1477,6 -1463,119 -1463,6 -1463,119 -1416,6 +1477,6 @@@@@@ static int xen_pgd_alloc(struct mm_stru
        return ret;
     }
     
 - - #ifdef CONFIG_X86_64
 - - static __initdata u64 __last_pgt_set_rw = 0;
 - - static __initdata u64 __pgt_buf_start = 0;
 - - static __initdata u64 __pgt_buf_end = 0;
 - - static __initdata u64 __pgt_buf_top = 0;
 - - /*
 - -  * As a consequence of the commit:
 - -  * 
 - -  * commit 4b239f458c229de044d6905c2b0f9fe16ed9e01e
 - -  * Author: Yinghai Lu <yinghai@kernel.org>
 - -  * Date:   Fri Dec 17 16:58:28 2010 -0800
 - -  * 
 - -  *     x86-64, mm: Put early page table high
 - -  * 
 - -  * at some point init_memory_mapping is going to reach the pagetable pages
 - -  * area and map those pages too (mapping them as normal memory that falls
 - -  * in the range of addresses passed to init_memory_mapping as argument).
 - -  * Some of those pages are already pagetable pages (they are in the range
 - -  * pgt_buf_start-pgt_buf_end) therefore they are going to be mapped RO and
 - -  * everything is fine.
 - -  * Some of these pages are not pagetable pages yet (they fall in the range
 - -  * pgt_buf_end-pgt_buf_top; for example the page at pgt_buf_end) so they
 - -  * are going to be mapped RW.  When these pages become pagetable pages and
 - -  * are hooked into the pagetable, xen will find that the guest has already
 - -  * a RW mapping of them somewhere and fail the operation.
 - -  * The reason Xen requires pagetables to be RO is that the hypervisor needs
 - -  * to verify that the pagetables are valid before using them. The validation
 - -  * operations are called "pinning".
 - -  * 
 - -  * In order to fix the issue we mark all the pages in the entire range
 - -  * pgt_buf_start-pgt_buf_top as RO, however when the pagetable allocation
 - -  * is completed only the range pgt_buf_start-pgt_buf_end is reserved by
 - -  * init_memory_mapping. Hence the kernel is going to crash as soon as one
 - -  * of the pages in the range pgt_buf_end-pgt_buf_top is reused (b/c those
 - -  * ranges are RO).
 - -  * 
 - -  * For this reason, 'mark_rw_past_pgt' is introduced which is called _after_
 - -  * the init_memory_mapping has completed (in a perfect world we would
 - -  * call this function from init_memory_mapping, but lets ignore that).
 - -  * 
 - -  * Because we are called _after_ init_memory_mapping the pgt_buf_[start,
 - -  * end,top] have all changed to new values (b/c init_memory_mapping
 - -  * is called and setting up another new page-table). Hence, the first time
 - -  * we enter this function, we save away the pgt_buf_start value and update
 - -  * the pgt_buf_[end,top].
 - -  * 
 - -  * When we detect that the "old" pgt_buf_start through pgt_buf_end
 - -  * PFNs have been reserved (so memblock_x86_reserve_range has been called),
 - -  * we immediately set out to RW the "old" pgt_buf_end through pgt_buf_top.
 - -  * 
 - -  * And then we update those "old" pgt_buf_[end|top] with the new ones
 - -  * so that we can redo this on the next pagetable.
 - -  */
 - - static __init void mark_rw_past_pgt(void) {
 - - 
 - -    if (pgt_buf_end > pgt_buf_start) {
 - -            u64 addr, size;
 - - 
 - -            /* Save it away. */
 - -            if (!__pgt_buf_start) {
 - -                    __pgt_buf_start = pgt_buf_start;
 - -                    __pgt_buf_end = pgt_buf_end;
 - -                    __pgt_buf_top = pgt_buf_top;
 - -                    return;
 - -            }
 - -            /* If we get the range that starts at __pgt_buf_end that means
 - -             * the range is reserved, and that in 'init_memory_mapping'
 - -             * the 'memblock_x86_reserve_range' has been called with the
 - -             * outdated __pgt_buf_start, __pgt_buf_end (the "new"
 - -             * pgt_buf_[start|end|top] refer now to a new pagetable.
 - -             * Note: we are called _after_ the pgt_buf_[..] have been
 - -             * updated.*/
 - - 
 - -            addr = memblock_x86_find_in_range_size(PFN_PHYS(__pgt_buf_start),
 - -                                                   &size, PAGE_SIZE);
 - - 
 - -            /* Still not reserved, meaning 'memblock_x86_reserve_range'
 - -             * hasn't been called yet. Update the _end and _top.*/
 - -            if (addr == PFN_PHYS(__pgt_buf_start)) {
 - -                    __pgt_buf_end = pgt_buf_end;
 - -                    __pgt_buf_top = pgt_buf_top;
 - -                    return;
 - -            }
 - - 
 - -            /* OK, the area is reserved, meaning it is time for us to
 - -             * set RW for the old end->top PFNs. */
 - - 
 - -            /* ..unless we had already done this. */
 - -            if (__pgt_buf_end == __last_pgt_set_rw)
 - -                    return;
 - - 
 - -            addr = PFN_PHYS(__pgt_buf_end);
 - -            
 - -            /* set as RW the rest */
 - -            printk(KERN_DEBUG "xen: setting RW the range %llx - %llx\n",
 - -                    PFN_PHYS(__pgt_buf_end), PFN_PHYS(__pgt_buf_top));
 - -            
 - -            while (addr < PFN_PHYS(__pgt_buf_top)) {
 - -                    make_lowmem_page_readwrite(__va(addr));
 - -                    addr += PAGE_SIZE;
 - -            }
 - -            /* And update everything so that we are ready for the next
 - -             * pagetable (the one created for regions past 4GB) */
 - -            __last_pgt_set_rw = __pgt_buf_end;
 - -            __pgt_buf_start = pgt_buf_start;
 - -            __pgt_buf_end = pgt_buf_end;
 - -            __pgt_buf_top = pgt_buf_top;
 - -    }
 - -    return;
 - - }
 - - #else
 - - static __init void mark_rw_past_pgt(void) { }
 - - #endif
     static void xen_pgd_free(struct mm_struct *mm, pgd_t *pgd)
     {
     #ifdef CONFIG_X86_64
     #endif
     }
     
  - -static __init pte_t mask_rw_pte(pte_t *ptep, pte_t pte)
  - -{
  - -   unsigned long pfn = pte_pfn(pte);
  - -
     #ifdef CONFIG_X86_32
 - - static __init pte_t mask_rw_pte(pte_t *ptep, pte_t pte)
 ++++static pte_t __init mask_rw_pte(pte_t *ptep, pte_t pte)
  + +{
        /* If there's an existing pte, then don't allow _PAGE_RW to be set */
        if (pte_val_ma(*ptep) & _PAGE_PRESENT)
                pte = __pte_ma(((pte_val_ma(*ptep) & _PAGE_RW) | ~_PAGE_RW) &
                               pte_val_ma(pte));
  - -#endif
  + +
  + +   return pte;
  + +}
  + +#else /* CONFIG_X86_64 */
 - - static __init pte_t mask_rw_pte(pte_t *ptep, pte_t pte)
 ++++static pte_t __init mask_rw_pte(pte_t *ptep, pte_t pte)
  + +{
  + +   unsigned long pfn = pte_pfn(pte);
     
 - -    /*
 - -     * A bit of optimization. We do not need to call the workaround
 - -     * when xen_set_pte_init is called with a PTE with 0 as PFN.
 - -     * That is b/c the pagetable at that point are just being populated
 - -     * with empty values and we can save some cycles by not calling
 - -     * the 'memblock' code.*/
 - -    if (pfn)
 - -            mark_rw_past_pgt();
        /*
         * If the new pfn is within the range of the newly allocated
         * kernel pagetable, and it isn't being mapped into an
    -    * early_ioremap fixmap slot, make sure it is RO.
    +    * early_ioremap fixmap slot as a freshly allocated page, make sure
    +    * it is RO.
         */
    -   if (!is_early_ioremap_ptep(ptep) &&
    -       pfn >= e820_table_start && pfn < e820_table_end)
    +   if (((!is_early_ioremap_ptep(ptep) &&
  -                     pfn >= pgt_buf_start && pfn < pgt_buf_end)) ||
  + +                   pfn >= pgt_buf_start && pfn < pgt_buf_top)) ||
    +                   (is_early_ioremap_ptep(ptep) && pfn != (pgt_buf_end - 1)))
                pte = pte_wrprotect(pte);
     
        return pte;
     }
  + +#endif /* CONFIG_X86_64 */
     
     /* Init-time set_pte while constructing initial pagetables, which
        doesn't allow RO pagetable pages to be remapped RW */
 ----static __init void xen_set_pte_init(pte_t *ptep, pte_t pte)
 ++++static void __init xen_set_pte_init(pte_t *ptep, pte_t pte)
     {
        pte = mask_rw_pte(ptep, pte);
     
@@@@@@ -1537,7 -1644,7 -1518,7 -1644,7 -1469,7 +1537,7 @@@@@@ static void pin_pagetable_pfn(unsigned 
     
     /* Early in boot, while setting up the initial pagetable, assume
        everything is pinned. */
 ----static __init void xen_alloc_pte_init(struct mm_struct *mm, unsigned long pfn)
 ++++static void __init xen_alloc_pte_init(struct mm_struct *mm, unsigned long pfn)
     {
     #ifdef CONFIG_FLATMEM
        BUG_ON(mem_map);        /* should only be used early */
     }
     
     /* Used for pmd and pud */
 ----static __init void xen_alloc_pmd_init(struct mm_struct *mm, unsigned long pfn)
 ++++static void __init xen_alloc_pmd_init(struct mm_struct *mm, unsigned long pfn)
     {
     #ifdef CONFIG_FLATMEM
        BUG_ON(mem_map);        /* should only be used early */
     
     /* Early release_pte assumes that all pts are pinned, since there's
        only init_mm and anything attached to that is pinned. */
 ----static __init void xen_release_pte_init(unsigned long pfn)
 ++++static void __init xen_release_pte_init(unsigned long pfn)
     {
        pin_pagetable_pfn(MMUEXT_UNPIN_TABLE, pfn);
        make_lowmem_page_readwrite(__va(PFN_PHYS(pfn)));
     }
     
 ----static __init void xen_release_pmd_init(unsigned long pfn)
 ++++static void __init xen_release_pmd_init(unsigned long pfn)
     {
        make_lowmem_page_readwrite(__va(PFN_PHYS(pfn)));
     }
@@@@@@ -1689,7 -1796,7 -1670,7 -1796,7 -1621,7 +1689,7 @@@@@@ static void set_page_prot(void *addr, p
                BUG();
     }
     
 ----static __init void xen_map_identity_early(pmd_t *pmd, unsigned long max_pfn)
 ++++static void __init xen_map_identity_early(pmd_t *pmd, unsigned long max_pfn)
     {
        unsigned pmdidx, pteidx;
        unsigned ident_pte;
                for (pteidx = 0; pteidx < PTRS_PER_PTE; pteidx++, pfn++) {
                        pte_t pte;
     
    -                   if (pfn > max_pfn_mapped)
    -                           max_pfn_mapped = pfn;
    -
                        if (!pte_none(pte_page[pteidx]))
                                continue;
     
@@@@@@ -1762,7 -1869,7 -1743,7 -1869,7 -1697,7 +1762,7 @@@@@@ static void convert_pfn_mfn(void *v
     }
     
     /*
    - * Set up the inital kernel pagetable.
    + * Set up the initial kernel pagetable.
      *
      * We can construct this by grafting the Xen provided pagetable into
      * head_64.S's preconstructed pagetables.  We copy the Xen L2's into
      * of the physical mapping once some sort of allocator has been set
      * up.
      */
 ----__init pgd_t *xen_setup_kernel_pagetable(pgd_t *pgd,
 ++++pgd_t * __init xen_setup_kernel_pagetable(pgd_t *pgd,
                                         unsigned long max_pfn)
     {
        pud_t *l3;
        pmd_t *l2;
     
    +   /* max_pfn_mapped is the last pfn mapped in the initial memory
    +    * mappings. Considering that on Xen after the kernel mappings we
    +    * have the mappings of some pages that don't exist in pfn space, we
    +    * set max_pfn_mapped to the last real pfn mapped. */
    +   max_pfn_mapped = PFN_DOWN(__pa(xen_start_info->mfn_list));
    +
        /* Zap identity mapping */
        init_level4_pgt[0] = __pgd(0);
     
     static RESERVE_BRK_ARRAY(pmd_t, initial_kernel_pmd, PTRS_PER_PMD);
     static RESERVE_BRK_ARRAY(pmd_t, swapper_kernel_pmd, PTRS_PER_PMD);
     
 ----static __init void xen_write_cr3_init(unsigned long cr3)
 ++++static void __init xen_write_cr3_init(unsigned long cr3)
     {
        unsigned long pfn = PFN_DOWN(__pa(swapper_pg_dir));
     
        pv_mmu_ops.write_cr3 = &xen_write_cr3;
     }
     
 ----__init pgd_t *xen_setup_kernel_pagetable(pgd_t *pgd,
 ++++pgd_t * __init xen_setup_kernel_pagetable(pgd_t *pgd,
                                         unsigned long max_pfn)
     {
        pmd_t *kernel_pmd;
        initial_kernel_pmd =
                extend_brk(sizeof(pmd_t) * PTRS_PER_PMD, PAGE_SIZE);
     
    -   max_pfn_mapped = PFN_DOWN(__pa(xen_start_info->pt_base) +
    -                             xen_start_info->nr_pt_frames * PAGE_SIZE +
    -                             512*1024);
    +   max_pfn_mapped = PFN_DOWN(__pa(xen_start_info->mfn_list));
     
        kernel_pmd = m2v(pgd[KERNEL_PGD_BOUNDARY].pgd);
        memcpy(initial_kernel_pmd, kernel_pmd, sizeof(pmd_t) * PTRS_PER_PMD);
@@@@@@ -1986,7 -2093,7 -1967,7 -2093,7 -1917,7 +1986,7 @@@@@@ static void xen_set_fixmap(unsigned idx
     #endif
     }
     
 ----__init void xen_ident_map_ISA(void)
 ++++void __init xen_ident_map_ISA(void)
     {
        unsigned long pa;
     
        xen_flush_tlb();
     }
     
 ----static __init void xen_post_allocator_init(void)
 ++++static void __init xen_post_allocator_init(void)
     {
 - -    mark_rw_past_pgt();
 - - 
    +#ifdef CONFIG_XEN_DEBUG
    +   pv_mmu_ops.make_pte = PV_CALLEE_SAVE(xen_make_pte_debug);
    +#endif
        pv_mmu_ops.set_pte = xen_set_pte;
        pv_mmu_ops.set_pmd = xen_set_pmd;
        pv_mmu_ops.set_pud = xen_set_pud;
@@@@@@ -2046,7 -2155,7 -2027,7 -2155,7 -1974,7 +2046,7 @@@@@@ static void xen_leave_lazy_mmu(void
        preempt_enable();
     }
     
 ----static const struct pv_mmu_ops xen_mmu_ops __initdata = {
 ++++static const struct pv_mmu_ops xen_mmu_ops __initconst = {
        .read_cr2 = xen_read_cr2,
        .write_cr2 = xen_write_cr2,
     
     
     void __init xen_init_mmu_ops(void)
     {
 ++++   x86_init.mapping.pagetable_reserve = xen_mapping_pagetable_reserve;
        x86_init.paging.pagetable_setup_start = xen_pagetable_setup_start;
        x86_init.paging.pagetable_setup_done = xen_pagetable_setup_done;
        pv_mmu_ops = xen_mmu_ops;
@@@@@@ -2147,7 -2255,7 -2127,7 -2255,7 -2074,7 +2147,7 @@@@@@ static void xen_zap_pfn_range(unsigned 
                        in_frames[i] = virt_to_mfn(vaddr);
     
                MULTI_update_va_mapping(mcs.mc, vaddr, VOID_PTE, 0);
    -           set_phys_to_machine(virt_to_pfn(vaddr), INVALID_P2M_ENTRY);
    +           __set_phys_to_machine(virt_to_pfn(vaddr), INVALID_P2M_ENTRY);
     
                if (out_frames)
                        out_frames[i] = virt_to_pfn(vaddr);
@@@@@@ -2426,18 -2534,18 -2406,18 -2534,18 -2353,6 +2426,18 @@@@@@ EXPORT_SYMBOL_GPL(xen_remap_domain_mfn_
     
     #ifdef CONFIG_XEN_DEBUG_FS
     
    +static int p2m_dump_open(struct inode *inode, struct file *filp)
    +{
    +   return single_open(filp, p2m_dump_show, NULL);
    +}
    +
    +static const struct file_operations p2m_dump_fops = {
    +   .open           = p2m_dump_open,
    +   .read           = seq_read,
    +   .llseek         = seq_lseek,
    +   .release        = single_release,
    +};
    +
     static struct dentry *d_mmu_debug;
     
     static int __init xen_mmu_debugfs(void)
        debugfs_create_u32("prot_commit_batched", 0444, d_mmu_debug,
                           &mmu_stats.prot_commit_batched);
     
    +   debugfs_create_file("p2m", 0600, d_mmu_debug, NULL, &p2m_dump_fops);
        return 0;
     }
     fs_initcall(xen_mmu_debugfs);
diff --combined arch/x86/xen/p2m.c
      * P2M_PER_PAGE depends on the architecture, as a mfn is always
      * unsigned long (8 bytes on 64-bit, 4 bytes on 32), leading to
      * 512 and 1024 entries respectively. 
    + *
    + * In short, these structures contain the Machine Frame Number (MFN) of the PFN.
    + *
    + * However not all entries are filled with MFNs. Specifically for all other
    + * leaf entries, or for the top  root, or middle one, for which there is a void
    + * entry, we assume it is  "missing". So (for example)
    + *  pfn_to_mfn(0x90909090)=INVALID_P2M_ENTRY.
    + *
    + * We also have the possibility of setting 1-1 mappings on certain regions, so
    + * that:
    + *  pfn_to_mfn(0xc0000)=0xc0000
    + *
    + * The benefit of this is, that we can assume for non-RAM regions (think
    + * PCI BARs, or ACPI spaces), we can create mappings easily b/c we
    + * get the PFN value to match the MFN.
    + *
    + * For this to work efficiently we have one new page p2m_identity and
    + * allocate (via reserved_brk) any other pages we need to cover the sides
    + * (1GB or 4MB boundary violations). All entries in p2m_identity are set to
    + * INVALID_P2M_ENTRY type (Xen toolstack only recognizes that and MFNs,
    + * no other fancy value).
    + *
    + * On lookup we spot that the entry points to p2m_identity and return the
    + * identity value instead of dereferencing and returning INVALID_P2M_ENTRY.
    + * If the entry points to an allocated page, we just proceed as before and
    + * return the PFN.  If the PFN has IDENTITY_FRAME_BIT set we unmask that in
    + * appropriate functions (pfn_to_mfn).
    + *
    + * The reason for having the IDENTITY_FRAME_BIT instead of just returning the
    + * PFN is that we could find ourselves where pfn_to_mfn(pfn)==pfn for a
    + * non-identity pfn. To protect ourselves against we elect to set (and get) the
    + * IDENTITY_FRAME_BIT on all identity mapped PFNs.
    + *
    + * This simplistic diagram is used to explain the more subtle piece of code.
    + * There is also a digram of the P2M at the end that can help.
    + * Imagine your E820 looking as so:
    + *
    + *                    1GB                                           2GB
    + * /-------------------+---------\/----\         /----------\    /---+-----\
    + * | System RAM        | Sys RAM ||ACPI|         | reserved |    | Sys RAM |
    + * \-------------------+---------/\----/         \----------/    \---+-----/
    + *                               ^- 1029MB                       ^- 2001MB
    + *
    + * [1029MB = 263424 (0x40500), 2001MB = 512256 (0x7D100),
    + *  2048MB = 524288 (0x80000)]
    + *
    + * And dom0_mem=max:3GB,1GB is passed in to the guest, meaning memory past 1GB
    + * is actually not present (would have to kick the balloon driver to put it in).
    + *
    + * When we are told to set the PFNs for identity mapping (see patch: "xen/setup:
    + * Set identity mapping for non-RAM E820 and E820 gaps.") we pass in the start
    + * of the PFN and the end PFN (263424 and 512256 respectively). The first step
    + * is to reserve_brk a top leaf page if the p2m[1] is missing. The top leaf page
    + * covers 512^2 of page estate (1GB) and in case the start or end PFN is not
    + * aligned on 512^2*PAGE_SIZE (1GB) we loop on aligned 1GB PFNs from start pfn
    + * to end pfn.  We reserve_brk top leaf pages if they are missing (means they
    + * point to p2m_mid_missing).
    + *
    + * With the E820 example above, 263424 is not 1GB aligned so we allocate a
    + * reserve_brk page which will cover the PFNs estate from 0x40000 to 0x80000.
    + * Each entry in the allocate page is "missing" (points to p2m_missing).
    + *
    + * Next stage is to determine if we need to do a more granular boundary check
    + * on the 4MB (or 2MB depending on architecture) off the start and end pfn's.
    + * We check if the start pfn and end pfn violate that boundary check, and if
    + * so reserve_brk a middle (p2m[x][y]) leaf page. This way we have a much finer
    + * granularity of setting which PFNs are missing and which ones are identity.
    + * In our example 263424 and 512256 both fail the check so we reserve_brk two
    + * pages. Populate them with INVALID_P2M_ENTRY (so they both have "missing"
    + * values) and assign them to p2m[1][2] and p2m[1][488] respectively.
    + *
    + * At this point we would at minimum reserve_brk one page, but could be up to
    + * three. Each call to set_phys_range_identity has at maximum a three page
    + * cost. If we were to query the P2M at this stage, all those entries from
    + * start PFN through end PFN (so 1029MB -> 2001MB) would return
    + * INVALID_P2M_ENTRY ("missing").
    + *
    + * The next step is to walk from the start pfn to the end pfn setting
    + * the IDENTITY_FRAME_BIT on each PFN. This is done in set_phys_range_identity.
    + * If we find that the middle leaf is pointing to p2m_missing we can swap it
    + * over to p2m_identity - this way covering 4MB (or 2MB) PFN space.  At this
    + * point we do not need to worry about boundary aligment (so no need to
    + * reserve_brk a middle page, figure out which PFNs are "missing" and which
    + * ones are identity), as that has been done earlier.  If we find that the
    + * middle leaf is not occupied by p2m_identity or p2m_missing, we dereference
    + * that page (which covers 512 PFNs) and set the appropriate PFN with
    + * IDENTITY_FRAME_BIT. In our example 263424 and 512256 end up there, and we
    + * set from p2m[1][2][256->511] and p2m[1][488][0->256] with
    + * IDENTITY_FRAME_BIT set.
    + *
    + * All other regions that are void (or not filled) either point to p2m_missing
    + * (considered missing) or have the default value of INVALID_P2M_ENTRY (also
    + * considered missing). In our case, p2m[1][2][0->255] and p2m[1][488][257->511]
    + * contain the INVALID_P2M_ENTRY value and are considered "missing."
    + *
    + * This is what the p2m ends up looking (for the E820 above) with this
    + * fabulous drawing:
    + *
    + *    p2m         /--------------\
    + *  /-----\       | &mfn_list[0],|                           /-----------------\
    + *  |  0  |------>| &mfn_list[1],|    /---------------\      | ~0, ~0, ..      |
    + *  |-----|       |  ..., ~0, ~0 |    | ~0, ~0, [x]---+----->| IDENTITY [@256] |
    + *  |  1  |---\   \--------------/    | [p2m_identity]+\     | IDENTITY [@257] |
    + *  |-----|    \                      | [p2m_identity]+\\    | ....            |
    + *  |  2  |--\  \-------------------->|  ...          | \\   \----------------/
    + *  |-----|   \                       \---------------/  \\
    + *  |  3  |\   \                                          \\  p2m_identity
    + *  |-----| \   \-------------------->/---------------\   /-----------------\
    + *  | ..  +->+                        | [p2m_identity]+-->| ~0, ~0, ~0, ... |
    + *  \-----/ /                         | [p2m_identity]+-->| ..., ~0         |
    + *         / /---------------\        | ....          |   \-----------------/
    + *        /  | IDENTITY[@0]  |      /-+-[x], ~0, ~0.. |
    + *       /   | IDENTITY[@256]|<----/  \---------------/
    + *      /    | ~0, ~0, ....  |
    + *     |     \---------------/
    + *     |
    + *     p2m_missing             p2m_missing
    + * /------------------\     /------------\
    + * | [p2m_mid_missing]+---->| ~0, ~0, ~0 |
    + * | [p2m_mid_missing]+---->| ..., ~0    |
    + * \------------------/     \------------/
    + *
    + * where ~0 is INVALID_P2M_ENTRY. IDENTITY is (PFN | IDENTITY_BIT)
      */
     
     #include <linux/init.h>
     #include <linux/list.h>
     #include <linux/hash.h>
     #include <linux/sched.h>
    +#include <linux/seq_file.h>
     
     #include <asm/cache.h>
     #include <asm/setup.h>
@@@@@@ -183,15 -183,15 -183,15 -183,15 -59,9 +183,15 @@@@@@ static RESERVE_BRK_ARRAY(unsigned long 
     static RESERVE_BRK_ARRAY(unsigned long, p2m_top_mfn, P2M_TOP_PER_PAGE);
     static RESERVE_BRK_ARRAY(unsigned long *, p2m_top_mfn_p, P2M_TOP_PER_PAGE);
     
    +static RESERVE_BRK_ARRAY(unsigned long, p2m_identity, P2M_PER_PAGE);
    +
     RESERVE_BRK(p2m_mid, PAGE_SIZE * (MAX_DOMAIN_PAGES / (P2M_PER_PAGE * P2M_MID_PER_PAGE)));
     RESERVE_BRK(p2m_mid_mfn, PAGE_SIZE * (MAX_DOMAIN_PAGES / (P2M_PER_PAGE * P2M_MID_PER_PAGE)));
     
    +/* We might hit two boundary violations at the start and end, at max each
    + * boundary violation will require three middle nodes. */
    +RESERVE_BRK(p2m_mid_identity, PAGE_SIZE * 2 * 3);
    +
     static inline unsigned p2m_top_index(unsigned long pfn)
     {
        BUG_ON(pfn >= MAX_P2M_PFN);
@@@@@@ -266,7 -266,7 -266,7 -266,7 -136,7 +266,7 @@@@@@ static void p2m_init(unsigned long *p2m
      * - After resume we're called from within stop_machine, but the mfn
      *   tree should alreay be completely allocated.
      */
    -void xen_build_mfn_list_list(void)
    +void __ref xen_build_mfn_list_list(void)
     {
        unsigned long pfn;
     
@@@@@@ -351,9 -351,9 -351,9 -351,9 -221,6 +351,9 @@@@@@ void __init xen_build_dynamic_phys_to_m
        p2m_top = extend_brk(PAGE_SIZE, PAGE_SIZE);
        p2m_top_init(p2m_top);
     
    +   p2m_identity = extend_brk(PAGE_SIZE, PAGE_SIZE);
    +   p2m_init(p2m_identity);
    +
        /*
         * The domain builder gives us a pre-constructed p2m array in
         * mfn_list for all the pages initially given to us, so we just
@@@@@@ -399,14 -399,14 -399,14 -399,14 -266,6 +399,14 @@@@@@ unsigned long get_phys_to_machine(unsig
        mididx = p2m_mid_index(pfn);
        idx = p2m_index(pfn);
     
    +   /*
    +    * The INVALID_P2M_ENTRY is filled in both p2m_*identity
    +    * and in p2m_*missing, so returning the INVALID_P2M_ENTRY
    +    * would be wrong.
    +    */
    +   if (p2m_top[topidx][mididx] == p2m_identity)
    +           return IDENTITY_FRAME(pfn);
    +
        return p2m_top[topidx][mididx][idx];
     }
     EXPORT_SYMBOL_GPL(get_phys_to_machine);
@@@@@@ -476,11 -476,11 -476,11 -476,11 -335,9 +476,11 @@@@@@ static bool alloc_p2m(unsigned long pfn
                        p2m_top_mfn_p[topidx] = mid_mfn;
        }
     
    -   if (p2m_top[topidx][mididx] == p2m_missing) {
    +   if (p2m_top[topidx][mididx] == p2m_identity ||
    +       p2m_top[topidx][mididx] == p2m_missing) {
                /* p2m leaf page is missing */
                unsigned long *p2m;
    +           unsigned long *p2m_orig = p2m_top[topidx][mididx];
     
                p2m = alloc_p2m_page();
                if (!p2m)
     
                p2m_init(p2m);
     
    -           if (cmpxchg(&mid[mididx], p2m_missing, p2m) != p2m_missing)
    +           if (cmpxchg(&mid[mididx], p2m_orig, p2m) != p2m_orig)
                        free_p2m_page(p2m);
                else
                        mid_mfn[mididx] = virt_to_mfn(p2m);
        return true;
     }
     
-- -            if (p2m_top[topidx] == p2m_mid_missing) {
-- -                    unsigned long **mid = extend_brk(PAGE_SIZE, PAGE_SIZE);
    +static bool __init __early_alloc_p2m(unsigned long pfn)
    +{
    +   unsigned topidx, mididx, idx;
    +
    +   topidx = p2m_top_index(pfn);
    +   mididx = p2m_mid_index(pfn);
    +   idx = p2m_index(pfn);
    +
    +   /* Pfff.. No boundary cross-over, lets get out. */
    +   if (!idx)
    +           return false;
    +
    +   WARN(p2m_top[topidx][mididx] == p2m_identity,
    +           "P2M[%d][%d] == IDENTITY, should be MISSING (or alloced)!\n",
    +           topidx, mididx);
    +
    +   /*
    +    * Could be done by xen_build_dynamic_phys_to_machine..
    +    */
    +   if (p2m_top[topidx][mididx] != p2m_missing)
    +           return false;
    +
    +   /* Boundary cross-over for the edges: */
    +   if (idx) {
    +           unsigned long *p2m = extend_brk(PAGE_SIZE, PAGE_SIZE);
++ ++           unsigned long *mid_mfn_p;
    +
    +           p2m_init(p2m);
    +
    +           p2m_top[topidx][mididx] = p2m;
    +
++ ++           /* For save/restore we need to MFN of the P2M saved */
++ ++           
++ ++           mid_mfn_p = p2m_top_mfn_p[topidx];
++ ++           WARN(mid_mfn_p[mididx] != virt_to_mfn(p2m_missing),
++ ++                   "P2M_TOP_P[%d][%d] != MFN of p2m_missing!\n",
++ ++                   topidx, mididx);
++ ++           mid_mfn_p[mididx] = virt_to_mfn(p2m);
++ ++
    +   }
    +   return idx != 0;
    +}
    +unsigned long __init set_phys_range_identity(unsigned long pfn_s,
    +                                 unsigned long pfn_e)
    +{
    +   unsigned long pfn;
    +
    +   if (unlikely(pfn_s >= MAX_P2M_PFN || pfn_e >= MAX_P2M_PFN))
    +           return 0;
    +
    +   if (unlikely(xen_feature(XENFEAT_auto_translated_physmap)))
    +           return pfn_e - pfn_s;
    +
    +   if (pfn_s > pfn_e)
    +           return 0;
    +
    +   for (pfn = (pfn_s & ~(P2M_MID_PER_PAGE * P2M_PER_PAGE - 1));
    +           pfn < ALIGN(pfn_e, (P2M_MID_PER_PAGE * P2M_PER_PAGE));
    +           pfn += P2M_MID_PER_PAGE * P2M_PER_PAGE)
    +   {
    +           unsigned topidx = p2m_top_index(pfn);
++ ++           unsigned long *mid_mfn_p;
++ ++           unsigned long **mid;
++ ++
++ ++           mid = p2m_top[topidx];
++ ++           mid_mfn_p = p2m_top_mfn_p[topidx];
++ ++           if (mid == p2m_mid_missing) {
++ ++                   mid = extend_brk(PAGE_SIZE, PAGE_SIZE);
    +
    +                   p2m_mid_init(mid);
    +
    +                   p2m_top[topidx] = mid;
++ ++
++ ++                   BUG_ON(mid_mfn_p != p2m_mid_missing_mfn);
++ ++           }
++ ++           /* And the save/restore P2M tables.. */
++ ++           if (mid_mfn_p == p2m_mid_missing_mfn) {
++ ++                   mid_mfn_p = extend_brk(PAGE_SIZE, PAGE_SIZE);
++ ++                   p2m_mid_mfn_init(mid_mfn_p);
++ ++
++ ++                   p2m_top_mfn_p[topidx] = mid_mfn_p;
++ ++                   p2m_top_mfn[topidx] = virt_to_mfn(mid_mfn_p);
++ ++                   /* Note: we don't set mid_mfn_p[midix] here,
++ ++                    * look in __early_alloc_p2m */
    +           }
    +   }
    +
    +   __early_alloc_p2m(pfn_s);
    +   __early_alloc_p2m(pfn_e);
    +
    +   for (pfn = pfn_s; pfn < pfn_e; pfn++)
    +           if (!__set_phys_to_machine(pfn, IDENTITY_FRAME(pfn)))
    +                   break;
    +
    +   if (!WARN((pfn - pfn_s) != (pfn_e - pfn_s),
    +           "Identity mapping failed. We are %ld short of 1-1 mappings!\n",
    +           (pfn_e - pfn_s) - (pfn - pfn_s)))
    +           printk(KERN_DEBUG "1-1 mapping on %lx->%lx\n", pfn_s, pfn);
    +
    +   return pfn - pfn_s;
    +}
    +
     /* Try to install p2m mapping; fail if intermediate bits missing */
     bool __set_phys_to_machine(unsigned long pfn, unsigned long mfn)
     {
        unsigned topidx, mididx, idx;
     
    +   if (unlikely(xen_feature(XENFEAT_auto_translated_physmap))) {
    +           BUG_ON(pfn != mfn && mfn != INVALID_P2M_ENTRY);
    +           return true;
    +   }
        if (unlikely(pfn >= MAX_P2M_PFN)) {
                BUG_ON(mfn != INVALID_P2M_ENTRY);
                return true;
        mididx = p2m_mid_index(pfn);
        idx = p2m_index(pfn);
     
    +   /* For sparse holes were the p2m leaf has real PFN along with
    +    * PCI holes, stick in the PFN as the MFN value.
    +    */
    +   if (mfn != INVALID_P2M_ENTRY && (mfn & IDENTITY_FRAME_BIT)) {
    +           if (p2m_top[topidx][mididx] == p2m_identity)
    +                   return true;
    +
    +           /* Swap over from MISSING to IDENTITY if needed. */
    +           if (p2m_top[topidx][mididx] == p2m_missing) {
    +                   WARN_ON(cmpxchg(&p2m_top[topidx][mididx], p2m_missing,
    +                           p2m_identity) != p2m_missing);
    +                   return true;
    +           }
    +   }
    +
        if (p2m_top[topidx][mididx] == p2m_missing)
                return mfn == INVALID_P2M_ENTRY;
     
     
     bool set_phys_to_machine(unsigned long pfn, unsigned long mfn)
     {
    -   if (unlikely(xen_feature(XENFEAT_auto_translated_physmap))) {
    -           BUG_ON(pfn != mfn && mfn != INVALID_P2M_ENTRY);
    -           return true;
    -   }
    -
        if (unlikely(!__set_phys_to_machine(pfn, mfn)))  {
                if (!alloc_p2m(pfn))
                        return false;
@@@@@@ -650,11 -650,11 -676,11 -650,11 -417,11 +676,11 @@@@@@ static unsigned long mfn_hash(unsigned 
     }
     
     /* Add an MFN override for a particular page */
 ----int m2p_add_override(unsigned long mfn, struct page *page)
 ++++int m2p_add_override(unsigned long mfn, struct page *page, bool clear_pte)
     {
        unsigned long flags;
        unsigned long pfn;
    -   unsigned long address;
    +   unsigned long uninitialized_var(address);
        unsigned level;
        pte_t *ptep = NULL;
     
        if (!PageHighMem(page)) {
                address = (unsigned long)__va(pfn << PAGE_SHIFT);
                ptep = lookup_address(address, &level);
 ----
                if (WARN(ptep == NULL || level != PG_LEVEL_4K,
                                        "m2p_add_override: pfn %lx not mapped", pfn))
                        return -EINVAL;
        page->private = mfn;
        page->index = pfn_to_mfn(pfn);
     
    -   __set_phys_to_machine(pfn, FOREIGN_FRAME(mfn));
    -   if (!PageHighMem(page))
    +   if (unlikely(!set_phys_to_machine(pfn, FOREIGN_FRAME(mfn))))
    +           return -ENOMEM;
    +
 ---    if (!PageHighMem(page))
 ++++   if (clear_pte && !PageHighMem(page))
                /* Just zap old mapping for now */
                pte_clear(&init_mm, address, ptep);
 ----
        spin_lock_irqsave(&m2p_override_lock, flags);
        list_add(&page->lru,  &m2p_overrides[mfn_hash(mfn)]);
        spin_unlock_irqrestore(&m2p_override_lock, flags);
     
        return 0;
     }
 ----
 ----int m2p_remove_override(struct page *page)
 ++++EXPORT_SYMBOL_GPL(m2p_add_override);
 ++++int m2p_remove_override(struct page *page, bool clear_pte)
     {
        unsigned long flags;
        unsigned long mfn;
        unsigned long pfn;
    -   unsigned long address;
    +   unsigned long uninitialized_var(address);
        unsigned level;
        pte_t *ptep = NULL;
     
        spin_lock_irqsave(&m2p_override_lock, flags);
        list_del(&page->lru);
        spin_unlock_irqrestore(&m2p_override_lock, flags);
    -   __set_phys_to_machine(pfn, page->index);
    +   set_phys_to_machine(pfn, page->index);
     
 ----   if (!PageHighMem(page))
 ++++   if (clear_pte && !PageHighMem(page))
                set_pte_at(&init_mm, address, ptep,
                                pfn_pte(pfn, PAGE_KERNEL));
                /* No tlb flush necessary because the caller already
     
        return 0;
     }
 ++++EXPORT_SYMBOL_GPL(m2p_remove_override);
     
     struct page *m2p_find_override(unsigned long mfn)
     {
@@@@@@ -754,80 -755,80 -781,80 -755,80 -520,3 +780,80 @@@@@@ unsigned long m2p_find_override_pfn(uns
        return ret;
     }
     EXPORT_SYMBOL_GPL(m2p_find_override_pfn);
    +
    +#ifdef CONFIG_XEN_DEBUG_FS
    +
    +int p2m_dump_show(struct seq_file *m, void *v)
    +{
    +   static const char * const level_name[] = { "top", "middle",
    +                                           "entry", "abnormal" };
    +   static const char * const type_name[] = { "identity", "missing",
    +                                           "pfn", "abnormal"};
    +#define TYPE_IDENTITY 0
    +#define TYPE_MISSING 1
    +#define TYPE_PFN 2
    +#define TYPE_UNKNOWN 3
    +   unsigned long pfn, prev_pfn_type = 0, prev_pfn_level = 0;
    +   unsigned int uninitialized_var(prev_level);
    +   unsigned int uninitialized_var(prev_type);
    +
    +   if (!p2m_top)
    +           return 0;
    +
    +   for (pfn = 0; pfn < MAX_DOMAIN_PAGES; pfn++) {
    +           unsigned topidx = p2m_top_index(pfn);
    +           unsigned mididx = p2m_mid_index(pfn);
    +           unsigned idx = p2m_index(pfn);
    +           unsigned lvl, type;
    +
    +           lvl = 4;
    +           type = TYPE_UNKNOWN;
    +           if (p2m_top[topidx] == p2m_mid_missing) {
    +                   lvl = 0; type = TYPE_MISSING;
    +           } else if (p2m_top[topidx] == NULL) {
    +                   lvl = 0; type = TYPE_UNKNOWN;
    +           } else if (p2m_top[topidx][mididx] == NULL) {
    +                   lvl = 1; type = TYPE_UNKNOWN;
    +           } else if (p2m_top[topidx][mididx] == p2m_identity) {
    +                   lvl = 1; type = TYPE_IDENTITY;
    +           } else if (p2m_top[topidx][mididx] == p2m_missing) {
    +                   lvl = 1; type = TYPE_MISSING;
    +           } else if (p2m_top[topidx][mididx][idx] == 0) {
    +                   lvl = 2; type = TYPE_UNKNOWN;
    +           } else if (p2m_top[topidx][mididx][idx] == IDENTITY_FRAME(pfn)) {
    +                   lvl = 2; type = TYPE_IDENTITY;
    +           } else if (p2m_top[topidx][mididx][idx] == INVALID_P2M_ENTRY) {
    +                   lvl = 2; type = TYPE_MISSING;
    +           } else if (p2m_top[topidx][mididx][idx] == pfn) {
    +                   lvl = 2; type = TYPE_PFN;
    +           } else if (p2m_top[topidx][mididx][idx] != pfn) {
    +                   lvl = 2; type = TYPE_PFN;
    +           }
    +           if (pfn == 0) {
    +                   prev_level = lvl;
    +                   prev_type = type;
    +           }
    +           if (pfn == MAX_DOMAIN_PAGES-1) {
    +                   lvl = 3;
    +                   type = TYPE_UNKNOWN;
    +           }
    +           if (prev_type != type) {
    +                   seq_printf(m, " [0x%lx->0x%lx] %s\n",
    +                           prev_pfn_type, pfn, type_name[prev_type]);
    +                   prev_pfn_type = pfn;
    +                   prev_type = type;
    +           }
    +           if (prev_level != lvl) {
    +                   seq_printf(m, " [0x%lx->0x%lx] level %s\n",
    +                           prev_pfn_level, pfn, level_name[prev_level]);
    +                   prev_pfn_level = pfn;
    +                   prev_level = lvl;
    +           }
    +   }
    +   return 0;
    +#undef TYPE_IDENTITY
    +#undef TYPE_MISSING
    +#undef TYPE_PFN
    +#undef TYPE_UNKNOWN
    +}
    +#endif
diff --combined arch/x86/xen/setup.c
@@@@@@ -50,10 -50,10 -50,10 -50,10 -50,8 +50,10 @@@@@@ phys_addr_t xen_extra_mem_start, xen_ex
      */
     #define EXTRA_MEM_RATIO            (10)
     
 ----static __init void xen_add_extra_mem(unsigned long pages)
 ++++static void __init xen_add_extra_mem(unsigned long pages)
     {
    +   unsigned long pfn;
    +
        u64 size = (u64)pages * PAGE_SIZE;
        u64 extra_start = xen_extra_mem_start + xen_extra_mem_size;
     
        xen_extra_mem_size += size;
     
        xen_max_p2m_pfn = PFN_DOWN(extra_start + size);
    +
    +   for (pfn = PFN_DOWN(extra_start); pfn <= xen_max_p2m_pfn; pfn++)
    +           __set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
     }
     
     static unsigned long __init xen_release_chunk(phys_addr_t start_addr,
                WARN(ret != 1, "Failed to release memory %lx-%lx err=%d\n",
                     start, end, ret);
                if (ret == 1) {
    -                   set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
    +                   __set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
                        len++;
                }
        }
@@@@@@ -143,55 -143,55 -143,55 -143,55 -138,12 +143,55 @@@@@@ static unsigned long __init xen_return_
        return released;
     }
     
---             if (entry->type == E820_RAM) {
    +static unsigned long __init xen_set_identity(const struct e820entry *list,
    +                                        ssize_t map_size)
    +{
    +   phys_addr_t last = xen_initial_domain() ? 0 : ISA_END_ADDRESS;
    +   phys_addr_t start_pci = last;
    +   const struct e820entry *entry;
    +   unsigned long identity = 0;
    +   int i;
    +
    +   for (i = 0, entry = list; i < map_size; i++, entry++) {
    +           phys_addr_t start = entry->addr;
    +           phys_addr_t end = start + entry->size;
    +
    +           if (start < last)
    +                   start = last;
    +
    +           if (end <= start)
    +                   continue;
    +
    +           /* Skip over the 1MB region. */
    +           if (last > end)
    +                   continue;
    +
+++ +           if ((entry->type == E820_RAM) || (entry->type == E820_UNUSABLE)) {
    +                   if (start > start_pci)
    +                           identity += set_phys_range_identity(
    +                                           PFN_UP(start_pci), PFN_DOWN(start));
    +
    +                   /* Without saving 'last' we would gooble RAM too
    +                    * at the end of the loop. */
    +                   last = end;
    +                   start_pci = end;
    +                   continue;
    +           }
    +           start_pci = min(start, start_pci);
    +           last = end;
    +   }
    +   if (last > start_pci)
    +           identity += set_phys_range_identity(
    +                                   PFN_UP(start_pci), PFN_DOWN(last));
    +   return identity;
    +}
     /**
      * machine_specific_memory_setup - Hook for machine specific memory setup.
      **/
     char * __init xen_memory_setup(void)
     {
        static struct e820entry map[E820MAX] __initdata;
    +   static struct e820entry map_raw[E820MAX] __initdata;
     
        unsigned long max_pfn = xen_start_info->nr_pages;
        unsigned long long mem_end;
        struct xen_memory_map memmap;
        unsigned long extra_pages = 0;
        unsigned long extra_limit;
    +   unsigned long identity_pages = 0;
        int i;
        int op;
     
        }
        BUG_ON(rc);
     
    +   memcpy(map_raw, map, sizeof(map));
        e820.nr_map = 0;
+++ +#ifdef CONFIG_X86_32
++      xen_extra_mem_start = mem_end;
+++ +#else
  + +   xen_extra_mem_start = max((1ULL << 32), mem_end);
+++ +#endif
        for (i = 0; i < memmap.nr_entries; i++) {
                unsigned long long end;
     
                        end -= delta;
     
                        extra_pages += PFN_DOWN(delta);
    +                   /*
    +                    * Set RAM below 4GB that is not for us to be unusable.
    +                    * This prevents "System RAM" address space from being
    +                    * used as potential resource for I/O address (happens
    +                    * when 'allocate_resource' is called).
    +                    */
    +                   if (delta &&
    +                           (xen_initial_domain() && end < 0x100000000ULL))
    +                           e820_add_region(end, delta, E820_UNUSABLE);
                }
     
                if (map[i].size > 0 && end > xen_extra_mem_start)
     
        xen_add_extra_mem(extra_pages);
     
    +   /*
    +    * Set P2M for all non-RAM pages and E820 gaps to be identity
    +    * type PFNs. We supply it with the non-sanitized version
    +    * of the E820.
    +    */
    +   identity_pages = xen_set_identity(map_raw, memmap.nr_entries);
    +   printk(KERN_INFO "Set %ld page(s) to 1-1 mapping.\n", identity_pages);
        return "Xen";
     }
     
@@@@@@ -336,7 -336,7 -336,7 -340,7 -270,7 +340,7 @@@@@@ static void __init fiddle_vdso(void
     #endif
     }
     
 ----static __cpuinit int register_callback(unsigned type, const void *func)
 ++++static int __cpuinit register_callback(unsigned type, const void *func)
     {
        struct callback_register callback = {
                .type = type,
diff --combined drivers/xen/events.c
@@@@@@ -5,7 -5,7 -5,7 -5,7 -5,7 +5,7 @@@@@@
      * domain gets 1024 event channels, but NR_IRQ is not that large, we
      * must dynamically map irqs<->event channels.  The event channels
      * interface with the rest of the kernel by defining a xen interrupt
  - - * chip.  When an event is recieved, it is mapped to an irq and sent
  + + * chip.  When an event is received, it is mapped to an irq and sent
      * through the normal interrupt processing path.
      *
      * There are four kinds of events which can be mapped to an event
      */
     static DEFINE_SPINLOCK(irq_mapping_update_lock);
     
    +static LIST_HEAD(xen_irq_list_head);
    +
     /* IRQ <-> VIRQ mapping. */
     static DEFINE_PER_CPU(int [NR_VIRQS], virq_to_irq) = {[0 ... NR_VIRQS-1] = -1};
     
@@@@@@ -87,9 -87,9 -87,9 -87,9 -85,7 +87,9 @@@@@@ enum xen_irq_type 
      */
     struct irq_info
     {
    +   struct list_head list;
        enum xen_irq_type type; /* type */
    +   unsigned irq;
        unsigned short evtchn;  /* event channel */
        unsigned short cpu;     /* cpu bound */
     
                        unsigned short gsi;
                        unsigned char vector;
                        unsigned char flags;
 ++++                   uint16_t domid;
                } pirq;
        } u;
     };
     #define PIRQ_NEEDS_EOI     (1 << 0)
     #define PIRQ_SHAREABLE     (1 << 1)
     
    -static struct irq_info *irq_info;
    -static int *pirq_to_irq;
    -
     static int *evtchn_to_irq;
    -struct cpu_evtchn_s {
    -   unsigned long bits[NR_EVENT_CHANNELS/BITS_PER_LONG];
    -};
     
    -static __initdata struct cpu_evtchn_s init_evtchn_mask = {
    -   .bits[0 ... (NR_EVENT_CHANNELS/BITS_PER_LONG)-1] = ~0ul,
    -};
    -static struct cpu_evtchn_s *cpu_evtchn_mask_p = &init_evtchn_mask;
    -
    -static inline unsigned long *cpu_evtchn_mask(int cpu)
    -{
    -   return cpu_evtchn_mask_p[cpu].bits;
    -}
    +static DEFINE_PER_CPU(unsigned long [NR_EVENT_CHANNELS/BITS_PER_LONG],
    +                 cpu_evtchn_mask);
     
     /* Xen will never allocate port zero for any purpose. */
     #define VALID_EVTCHN(chn)  ((chn) != 0)
     static struct irq_chip xen_dynamic_chip;
     static struct irq_chip xen_percpu_chip;
     static struct irq_chip xen_pirq_chip;
+ +++static void enable_dynirq(struct irq_data *data);
+ +++static void disable_dynirq(struct irq_data *data);
    +
    +/* Get info for IRQ */
    +static struct irq_info *info_for_irq(unsigned irq)
    +{
    +   return irq_get_handler_data(irq);
    +}
     
    -/* Constructor for packed IRQ information. */
    -static struct irq_info mk_unbound_info(void)
    +/* Constructors for packed IRQ information. */
    +static void xen_irq_info_common_init(struct irq_info *info,
    +                                unsigned irq,
    +                                enum xen_irq_type type,
    +                                unsigned short evtchn,
    +                                unsigned short cpu)
     {
    -   return (struct irq_info) { .type = IRQT_UNBOUND };
    +
    +   BUG_ON(info->type != IRQT_UNBOUND && info->type != type);
    +
    +   info->type = type;
    +   info->irq = irq;
    +   info->evtchn = evtchn;
    +   info->cpu = cpu;
    +
    +   evtchn_to_irq[evtchn] = irq;
     }
     
    -static struct irq_info mk_evtchn_info(unsigned short evtchn)
    +static void xen_irq_info_evtchn_init(unsigned irq,
    +                                unsigned short evtchn)
     {
    -   return (struct irq_info) { .type = IRQT_EVTCHN, .evtchn = evtchn,
    -                   .cpu = 0 };
    +   struct irq_info *info = info_for_irq(irq);
    +
    +   xen_irq_info_common_init(info, irq, IRQT_EVTCHN, evtchn, 0);
     }
     
    -static struct irq_info mk_ipi_info(unsigned short evtchn, enum ipi_vector ipi)
    +static void xen_irq_info_ipi_init(unsigned cpu,
    +                             unsigned irq,
    +                             unsigned short evtchn,
    +                             enum ipi_vector ipi)
     {
    -   return (struct irq_info) { .type = IRQT_IPI, .evtchn = evtchn,
    -                   .cpu = 0, .u.ipi = ipi };
    +   struct irq_info *info = info_for_irq(irq);
    +
    +   xen_irq_info_common_init(info, irq, IRQT_IPI, evtchn, 0);
    +
    +   info->u.ipi = ipi;
    +
    +   per_cpu(ipi_to_irq, cpu)[ipi] = irq;
     }
     
    -static struct irq_info mk_virq_info(unsigned short evtchn, unsigned short virq)
    +static void xen_irq_info_virq_init(unsigned cpu,
    +                              unsigned irq,
    +                              unsigned short evtchn,
    +                              unsigned short virq)
     {
    -   return (struct irq_info) { .type = IRQT_VIRQ, .evtchn = evtchn,
    -                   .cpu = 0, .u.virq = virq };
    +   struct irq_info *info = info_for_irq(irq);
    +
    +   xen_irq_info_common_init(info, irq, IRQT_VIRQ, evtchn, 0);
    +
    +   info->u.virq = virq;
    +
    +   per_cpu(virq_to_irq, cpu)[virq] = irq;
     }
     
    -static struct irq_info mk_pirq_info(unsigned short evtchn, unsigned short pirq,
    -                               unsigned short gsi, unsigned short vector)
    +static void xen_irq_info_pirq_init(unsigned irq,
    +                              unsigned short evtchn,
    +                              unsigned short pirq,
    +                              unsigned short gsi,
    +                              unsigned short vector,
 ++++                              uint16_t domid,
    +                              unsigned char flags)
     {
    -   return (struct irq_info) { .type = IRQT_PIRQ, .evtchn = evtchn,
    -                   .cpu = 0,
    -                   .u.pirq = { .pirq = pirq, .gsi = gsi, .vector = vector } };
    +   struct irq_info *info = info_for_irq(irq);
    +
    +   xen_irq_info_common_init(info, irq, IRQT_PIRQ, evtchn, 0);
    +
    +   info->u.pirq.pirq = pirq;
    +   info->u.pirq.gsi = gsi;
    +   info->u.pirq.vector = vector;
 ++++   info->u.pirq.domid = domid;
    +   info->u.pirq.flags = flags;
     }
     
     /*
      * Accessors for packed IRQ information.
      */
    -static struct irq_info *info_for_irq(unsigned irq)
    -{
    -   return &irq_info[irq];
    -}
    -
     static unsigned int evtchn_from_irq(unsigned irq)
     {
        if (unlikely(WARN(irq < 0 || irq >= nr_irqs, "Invalid irq %d!\n", irq)))
@@@@@@ -246,6 -245,6 -243,6 -243,6 -212,26 +248,6 @@@@@@ static unsigned pirq_from_irq(unsigned 
        return info->u.pirq.pirq;
     }
     
    -static unsigned gsi_from_irq(unsigned irq)
    -{
    -   struct irq_info *info = info_for_irq(irq);
    -
    -   BUG_ON(info == NULL);
    -   BUG_ON(info->type != IRQT_PIRQ);
    -
    -   return info->u.pirq.gsi;
    -}
    -
    -static unsigned vector_from_irq(unsigned irq)
    -{
    -   struct irq_info *info = info_for_irq(irq);
    -
    -   BUG_ON(info == NULL);
    -   BUG_ON(info->type != IRQT_PIRQ);
    -
    -   return info->u.pirq.vector;
    -}
    -
     static enum xen_irq_type type_from_irq(unsigned irq)
     {
        return info_for_irq(irq)->type;
@@@@@@ -281,7 -280,7 -278,7 -278,7 -267,7 +283,7 @@@@@@ static inline unsigned long active_evtc
                                           unsigned int idx)
     {
        return (sh->evtchn_pending[idx] &
    -           cpu_evtchn_mask(cpu)[idx] &
    +           per_cpu(cpu_evtchn_mask, cpu)[idx] &
                ~sh->evtchn_mask[idx]);
     }
     
@@@@@@ -291,31 -290,31 -288,31 -288,31 -277,31 +293,31 @@@@@@ static void bind_evtchn_to_cpu(unsigne
     
        BUG_ON(irq == -1);
     #ifdef CONFIG_SMP
    -   cpumask_copy(irq_to_desc(irq)->affinity, cpumask_of(cpu));
    +   cpumask_copy(irq_to_desc(irq)->irq_data.affinity, cpumask_of(cpu));
     #endif
     
    -   clear_bit(chn, cpu_evtchn_mask(cpu_from_irq(irq)));
    -   set_bit(chn, cpu_evtchn_mask(cpu));
    +   clear_bit(chn, per_cpu(cpu_evtchn_mask, cpu_from_irq(irq)));
    +   set_bit(chn, per_cpu(cpu_evtchn_mask, cpu));
     
    -   irq_info[irq].cpu = cpu;
    +   info_for_irq(irq)->cpu = cpu;
     }
     
     static void init_evtchn_cpu_bindings(void)
     {
        int i;
     #ifdef CONFIG_SMP
    -   struct irq_desc *desc;
    +   struct irq_info *info;
     
        /* By default all event channels notify CPU#0. */
    -   for_each_irq_desc(i, desc) {
    -           cpumask_copy(desc->affinity, cpumask_of(0));
    +   list_for_each_entry(info, &xen_irq_list_head, list) {
    +           struct irq_desc *desc = irq_to_desc(info->irq);
    +           cpumask_copy(desc->irq_data.affinity, cpumask_of(0));
        }
     #endif
     
        for_each_possible_cpu(i)
    -           memset(cpu_evtchn_mask(i),
    -                  (i == 0) ? ~0 : 0, sizeof(struct cpu_evtchn_s));
    -
    +           memset(per_cpu(cpu_evtchn_mask, i),
    +                  (i == 0) ? ~0 : 0, sizeof(*per_cpu(cpu_evtchn_mask, i)));
     }
     
     static inline void clear_evtchn(int port)
@@@@@@ -390,102 -389,92 -387,102 -387,102 -376,93 +392,92 @@@@@@ static void unmask_evtchn(int port
        put_cpu();
     }
     
    -static int get_nr_hw_irqs(void)
    +static void xen_irq_init(unsigned irq)
     {
    -   int ret = 1;
    +   struct irq_info *info;
    +   struct irq_desc *desc = irq_to_desc(irq);
     
    -#ifdef CONFIG_X86_IO_APIC
    -   ret = get_nr_irqs_gsi();
    +#ifdef CONFIG_SMP
    +   /* By default all event channels notify CPU#0. */
    +   cpumask_copy(desc->irq_data.affinity, cpumask_of(0));
     #endif
     
    -   return ret;
    -}
    +   info = kzalloc(sizeof(*info), GFP_KERNEL);
    +   if (info == NULL)
    +           panic("Unable to allocate metadata for IRQ%d\n", irq);
     
    -static int find_unbound_pirq(int type)
    -{
    -   int rc, i;
    -   struct physdev_get_free_pirq op_get_free_pirq;
    -   op_get_free_pirq.type = type;
    +   info->type = IRQT_UNBOUND;
     
    -   rc = HYPERVISOR_physdev_op(PHYSDEVOP_get_free_pirq, &op_get_free_pirq);
    -   if (!rc)
    -           return op_get_free_pirq.pirq;
    +   irq_set_handler_data(irq, info);
     
    -   for (i = 0; i < nr_irqs; i++) {
    -           if (pirq_to_irq[i] < 0)
    -                   return i;
    -   }
    -   return -1;
    +   list_add_tail(&info->list, &xen_irq_list_head);
     }
     
    -static int find_unbound_irq(void)
    +static int __must_check xen_allocate_irq_dynamic(void)
     {
    -   struct irq_data *data;
    -   int irq, res;
    -   int bottom = get_nr_hw_irqs();
    -   int top = nr_irqs-1;
    -
    -   if (bottom == nr_irqs)
    -           goto no_irqs;
    +   int first = 0;
    +   int irq;
     
    -   /* This loop starts from the top of IRQ space and goes down.
    -    * We need this b/c if we have a PCI device in a Xen PV guest
    -    * we do not have an IO-APIC (though the backend might have them)
    -    * mapped in. To not have a collision of physical IRQs with the Xen
    -    * event channels start at the top of the IRQ space for virtual IRQs.
    +#ifdef CONFIG_X86_IO_APIC
    +   /*
    +    * For an HVM guest or domain 0 which see "real" (emulated or
  -      * actual repectively) GSIs we allocate dynamic IRQs
  + +    * actual respectively) GSIs we allocate dynamic IRQs
    +    * e.g. those corresponding to event channels or MSIs
    +    * etc. from the range above those "real" GSIs to avoid
    +    * collisions.
         */
    -   for (irq = top; irq > bottom; irq--) {
    -           data = irq_get_irq_data(irq);
    -           /* only 15->0 have init'd desc; handle irq > 16 */
    -           if (!data)
    -                   break;
    -           if (data->chip == &no_irq_chip)
    -                   break;
    -           if (data->chip != &xen_dynamic_chip)
    -                   continue;
    -           if (irq_info[irq].type == IRQT_UNBOUND)
    -                   return irq;
    -   }
    -
    -   if (irq == bottom)
    -           goto no_irqs;
    +   if (xen_initial_domain() || xen_hvm_domain())
    +           first = get_nr_irqs_gsi();
    +#endif
     
    -   res = irq_alloc_desc_at(irq, -1);
    +   irq = irq_alloc_desc_from(first, -1);
     
    -   if (WARN_ON(res != irq))
    -           return -1;
    +   xen_irq_init(irq);
     
        return irq;
    -
    -no_irqs:
    -   panic("No available IRQ to bind to: increase nr_irqs!\n");
     }
     
    -static bool identity_mapped_irq(unsigned irq)
    +static int __must_check xen_allocate_irq_gsi(unsigned gsi)
     {
    -   /* identity map all the hardware irqs */
    -   return irq < get_nr_hw_irqs();
    +   int irq;
    +
    +   /*
    +    * A PV guest has no concept of a GSI (since it has no ACPI
    +    * nor access to/knowledge of the physical APICs). Therefore
    +    * all IRQs are dynamically allocated from the entire IRQ
    +    * space.
    +    */
    +   if (xen_pv_domain() && !xen_initial_domain())
    +           return xen_allocate_irq_dynamic();
    +
    +   /* Legacy IRQ descriptors are already allocated by the arch. */
    +   if (gsi < NR_IRQS_LEGACY)
    +           irq = gsi;
    +   else
    +           irq = irq_alloc_desc_at(gsi, -1);
    +
    +   xen_irq_init(irq);
    +
    +   return irq;
     }
     
    -static void pirq_unmask_notify(int irq)
    +static void xen_free_irq(unsigned irq)
     {
    -   struct physdev_eoi eoi = { .irq = pirq_from_irq(irq) };
    +   struct irq_info *info = irq_get_handler_data(irq);
     
    -   if (unlikely(pirq_needs_eoi(irq))) {
    -           int rc = HYPERVISOR_physdev_op(PHYSDEVOP_eoi, &eoi);
    -           WARN_ON(rc);
    -   }
    +   list_del(&info->list);
    +
    +   irq_set_handler_data(irq, NULL);
    +
    +   kfree(info);
    +
    +   /* Legacy IRQ descriptors are managed by the arch. */
    +   if (irq < NR_IRQS_LEGACY)
    +           return;
    +
    +   irq_free_desc(irq);
     }
     
- -- static void pirq_unmask_notify(int irq)
- -- {
- --    struct physdev_eoi eoi = { .irq = pirq_from_irq(irq) };
- -- 
- --    if (unlikely(pirq_needs_eoi(irq))) {
- --            int rc = HYPERVISOR_physdev_op(PHYSDEVOP_eoi, &eoi);
- --            WARN_ON(rc);
- --    }
- -- }
- -- 
     static void pirq_query_unmask(int irq)
     {
        struct physdev_irq_status_query irq_status;
@@@@@@ -509,7 -498,30 -506,7 -506,7 -486,7 +501,30 @@@@@@ static bool probing_irq(int irq
        return desc && desc->action == NULL;
     }
     
    -static unsigned int startup_pirq(unsigned int irq)
+ +++static void eoi_pirq(struct irq_data *data)
+ +++{
+ +++   int evtchn = evtchn_from_irq(data->irq);
+ +++   struct physdev_eoi eoi = { .irq = pirq_from_irq(data->irq) };
+ +++   int rc = 0;
+ +++
+ +++   irq_move_irq(data);
+ +++
+ +++   if (VALID_EVTCHN(evtchn))
+ +++           clear_evtchn(evtchn);
+ +++
+ +++   if (pirq_needs_eoi(data->irq)) {
+ +++           rc = HYPERVISOR_physdev_op(PHYSDEVOP_eoi, &eoi);
+ +++           WARN_ON(rc);
+ +++   }
+ +++}
+ +++
+ +++static void mask_ack_pirq(struct irq_data *data)
+ +++{
+ +++   disable_dynirq(data);
+ +++   eoi_pirq(data);
+ +++}
+ +++
    +static unsigned int __startup_pirq(unsigned int irq)
     {
        struct evtchn_bind_pirq bind_pirq;
        struct irq_info *info = info_for_irq(irq);
     
     out:
        unmask_evtchn(evtchn);
- ---   pirq_unmask_notify(irq);
+ +++   eoi_pirq(irq_get_irq_data(irq));
     
        return 0;
     }
     
    -static void shutdown_pirq(unsigned int irq)
    +static unsigned int startup_pirq(struct irq_data *data)
    +{
    +   return __startup_pirq(data->irq);
    +}
    +
    +static void shutdown_pirq(struct irq_data *data)
     {
        struct evtchn_close close;
    +   unsigned int irq = data->irq;
        struct irq_info *info = info_for_irq(irq);
        int evtchn = evtchn_from_irq(irq);
     
        info->evtchn = 0;
     }
     
    -static void enable_pirq(unsigned int irq)
    +static void enable_pirq(struct irq_data *data)
     {
    -   startup_pirq(irq);
    +   startup_pirq(data);
     }
     
    -static void disable_pirq(unsigned int irq)
    +static void disable_pirq(struct irq_data *data)
     {
- ---}
- ---
- -- static void ack_pirq(struct irq_data *data)
    -static void ack_pirq(unsigned int irq)
- ---{
- --    int evtchn = evtchn_from_irq(data->irq);
    -   int evtchn = evtchn_from_irq(irq);
- ---
- --    irq_move_irq(data);
    -   move_native_irq(irq);
- ---
- ---   if (VALID_EVTCHN(evtchn)) {
- ---           mask_evtchn(evtchn);
- ---           clear_evtchn(evtchn);
    -   }
    -}
    -
    -static void end_pirq(unsigned int irq)
    -{
    -   int evtchn = evtchn_from_irq(irq);
    -   struct irq_desc *desc = irq_to_desc(irq);
    -
    -   if (WARN_ON(!desc))
    -           return;
    -
    -   if ((desc->status & (IRQ_DISABLED|IRQ_PENDING)) ==
    -       (IRQ_DISABLED|IRQ_PENDING)) {
    -           shutdown_pirq(irq);
    -   } else if (VALID_EVTCHN(evtchn)) {
    -           unmask_evtchn(evtchn);
    -           pirq_unmask_notify(irq);
- ---   }
+ +++   disable_dynirq(data);
     }
     
     static int find_irq_by_gsi(unsigned gsi)
     {
    -   int irq;
    -
    -   for (irq = 0; irq < nr_irqs; irq++) {
    -           struct irq_info *info = info_for_irq(irq);
    +   struct irq_info *info;
     
    -           if (info == NULL || info->type != IRQT_PIRQ)
    +   list_for_each_entry(info, &xen_irq_list_head, list) {
    +           if (info->type != IRQT_PIRQ)
                        continue;
     
    -           if (gsi_from_irq(irq) == gsi)
    -                   return irq;
    +           if (info->u.pirq.gsi == gsi)
    +                   return info->irq;
        }
     
        return -1;
     }
     
    -int xen_allocate_pirq(unsigned gsi, int shareable, char *name)
    +int xen_allocate_pirq_gsi(unsigned gsi)
     {
    -   return xen_map_pirq_gsi(gsi, gsi, shareable, name);
    +   return gsi;
     }
     
    -/* xen_map_pirq_gsi might allocate irqs from the top down, as a
    - * consequence don't assume that the irq number returned has a low value
    - * or can be used as a pirq number unless you know otherwise.
    - *
    - * One notable exception is when xen_map_pirq_gsi is called passing an
    - * hardware gsi as argument, in that case the irq number returned
    - * matches the gsi number passed as second argument.
    +/*
    + * Do not make any assumptions regarding the relationship between the
    + * IRQ number returned here and the Xen pirq argument.
      *
      * Note: We don't assign an event channel until the irq actually started
      * up.  Return an existing irq if we've already got one for the gsi.
      */
    -int xen_map_pirq_gsi(unsigned pirq, unsigned gsi, int shareable, char *name)
    +int xen_bind_pirq_gsi_to_irq(unsigned gsi,
    +                        unsigned pirq, int shareable, char *name)
     {
    -   int irq = 0;
    +   int irq = -1;
        struct physdev_irq irq_op;
     
        spin_lock(&irq_mapping_update_lock);
     
    -   if ((pirq > nr_irqs) || (gsi > nr_irqs)) {
    -           printk(KERN_WARNING "xen_map_pirq_gsi: %s %s is incorrect!\n",
    -                   pirq > nr_irqs ? "pirq" :"",
    -                   gsi > nr_irqs ? "gsi" : "");
    -           goto out;
    -   }
    -
        irq = find_irq_by_gsi(gsi);
        if (irq != -1) {
                printk(KERN_INFO "xen_map_pirq_gsi: returning irq %d for gsi %u\n",
                goto out;       /* XXX need refcount? */
        }
     
    -   /* If we are a PV guest, we don't have GSIs (no ACPI passed). Therefore
    -    * we are using the !xen_initial_domain() to drop in the function.*/
    -   if (identity_mapped_irq(gsi) || (!xen_initial_domain() &&
    -                           xen_pv_domain())) {
    -           irq = gsi;
    -           irq_alloc_desc_at(irq, -1);
    -   } else
    -           irq = find_unbound_irq();
    -
    -   set_irq_chip_and_handler_name(irq, &xen_pirq_chip,
    -                                 handle_level_irq, name);
    +   irq = xen_allocate_irq_gsi(gsi);
    +   if (irq < 0)
    +           goto out;
     
- --    irq_set_chip_and_handler_name(irq, &xen_pirq_chip, handle_level_irq,
- --                                  name);
- -- 
        irq_op.irq = irq;
        irq_op.vector = 0;
     
         * this in the priv domain. */
        if (xen_initial_domain() &&
            HYPERVISOR_physdev_op(PHYSDEVOP_alloc_irq_vector, &irq_op)) {
    -           irq_free_desc(irq);
    +           xen_free_irq(irq);
                irq = -ENOSPC;
                goto out;
        }
     
 ---    xen_irq_info_pirq_init(irq, 0, pirq, gsi, irq_op.vector,
    -   irq_info[irq] = mk_pirq_info(0, pirq, gsi, irq_op.vector);
    -   irq_info[irq].u.pirq.flags |= shareable ? PIRQ_SHAREABLE : 0;
    -   pirq_to_irq[pirq] = irq;
 ++++   xen_irq_info_pirq_init(irq, 0, pirq, gsi, irq_op.vector, DOMID_SELF,
    +                          shareable ? PIRQ_SHAREABLE : 0);
    +
+ +++   pirq_query_unmask(irq);
+ +++   /* We try to use the handler with the appropriate semantic for the
+ +++    * type of interrupt: if the interrupt doesn't need an eoi
+ +++    * (pirq_needs_eoi returns false), we treat it like an edge
+ +++    * triggered interrupt so we use handle_edge_irq.
+ +++    * As a matter of fact this only happens when the corresponding
+ +++    * physical interrupt is edge triggered or an msi.
+ +++    *
+ +++    * On the other hand if the interrupt needs an eoi (pirq_needs_eoi
+ +++    * returns true) we treat it like a level triggered interrupt so we
+ +++    * use handle_fasteoi_irq like the native code does for this kind of
+ +++    * interrupts.
+ +++    * Depending on the Xen version, pirq_needs_eoi might return true
+ +++    * not only for level triggered interrupts but for edge triggered
+ +++    * interrupts too. In any case Xen always honors the eoi mechanism,
+ +++    * not injecting any more pirqs of the same kind if the first one
+ +++    * hasn't received an eoi yet. Therefore using the fasteoi handler
+ +++    * is the right choice either way.
+ +++    */
+ +++   if (pirq_needs_eoi(irq))
+ +++           irq_set_chip_and_handler_name(irq, &xen_pirq_chip,
+ +++                           handle_fasteoi_irq, name);
+ +++   else
+ +++           irq_set_chip_and_handler_name(irq, &xen_pirq_chip,
+ +++                           handle_edge_irq, name);
+ ++ 
     out:
        spin_unlock(&irq_mapping_update_lock);
     
     }
     
     #ifdef CONFIG_PCI_MSI
    -#include <linux/msi.h>
    -#include "../pci/msi.h"
    -
    -void xen_allocate_pirq_msi(char *name, int *irq, int *pirq, int alloc)
    +int xen_allocate_pirq_msi(struct pci_dev *dev, struct msi_desc *msidesc)
     {
    -   spin_lock(&irq_mapping_update_lock);
    -
    -   if (alloc & XEN_ALLOC_IRQ) {
    -           *irq = find_unbound_irq();
    -           if (*irq == -1)
    -                   goto out;
    -   }
    -
    -   if (alloc & XEN_ALLOC_PIRQ) {
    -           *pirq = find_unbound_pirq(MAP_PIRQ_TYPE_MSI);
    -           if (*pirq == -1)
    -                   goto out;
    -   }
    +   int rc;
    +   struct physdev_get_free_pirq op_get_free_pirq;
     
    -   set_irq_chip_and_handler_name(*irq, &xen_pirq_chip,
    -                                 handle_level_irq, name);
    +   op_get_free_pirq.type = MAP_PIRQ_TYPE_MSI;
    +   rc = HYPERVISOR_physdev_op(PHYSDEVOP_get_free_pirq, &op_get_free_pirq);
     
    -   irq_info[*irq] = mk_pirq_info(0, *pirq, 0, 0);
    -   pirq_to_irq[*pirq] = *irq;
    +   WARN_ONCE(rc == -ENOSYS,
    +             "hypervisor does not support the PHYSDEVOP_get_free_pirq interface\n");
     
    -out:
    -   spin_unlock(&irq_mapping_update_lock);
    +   return rc ? -1 : op_get_free_pirq.pirq;
     }
     
    -int xen_create_msi_irq(struct pci_dev *dev, struct msi_desc *msidesc, int type)
    +int xen_bind_pirq_msi_to_irq(struct pci_dev *dev, struct msi_desc *msidesc,
 ---                         int pirq, int vector, const char *name)
 ++++                        int pirq, int vector, const char *name,
 ++++                        domid_t domid)
     {
    -   int irq = -1;
    -   struct physdev_map_pirq map_irq;
    -   int rc;
    -   int pos;
    -   u32 table_offset, bir;
    -
    -   memset(&map_irq, 0, sizeof(map_irq));
    -   map_irq.domid = DOMID_SELF;
    -   map_irq.type = MAP_PIRQ_TYPE_MSI;
    -   map_irq.index = -1;
    -   map_irq.pirq = -1;
    -   map_irq.bus = dev->bus->number;
    -   map_irq.devfn = dev->devfn;
    -
    -   if (type == PCI_CAP_ID_MSIX) {
    -           pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
    -
    -           pci_read_config_dword(dev, msix_table_offset_reg(pos),
    -                                   &table_offset);
    -           bir = (u8)(table_offset & PCI_MSIX_FLAGS_BIRMASK);
    -
    -           map_irq.table_base = pci_resource_start(dev, bir);
    -           map_irq.entry_nr = msidesc->msi_attrib.entry_nr;
    -   }
    +   int irq, ret;
     
        spin_lock(&irq_mapping_update_lock);
     
    -   irq = find_unbound_irq();
    -
    +   irq = xen_allocate_irq_dynamic();
        if (irq == -1)
                goto out;
     
- --    irq_set_chip_and_handler_name(irq, &xen_pirq_chip, handle_level_irq,
- --                                  name);
    -   rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq);
    -   if (rc) {
    -           printk(KERN_WARNING "xen map irq failed %d\n", rc);
    -
    -           irq_free_desc(irq);
    -
    -           irq = -1;
    -           goto out;
    -   }
    -   irq_info[irq] = mk_pirq_info(0, map_irq.pirq, 0, map_irq.index);
    -
    -   set_irq_chip_and_handler_name(irq, &xen_pirq_chip,
    -                   handle_level_irq,
    -                   (type == PCI_CAP_ID_MSIX) ? "msi-x":"msi");
+ +++   irq_set_chip_and_handler_name(irq, &xen_pirq_chip, handle_edge_irq,
+ +++                   name);
     
 ---    xen_irq_info_pirq_init(irq, 0, pirq, 0, vector, 0);
 ++++   xen_irq_info_pirq_init(irq, 0, pirq, 0, vector, domid, 0);
    +   ret = irq_set_msi_desc(irq, msidesc);
    +   if (ret < 0)
    +           goto error_irq;
     out:
        spin_unlock(&irq_mapping_update_lock);
        return irq;
    +error_irq:
    +   spin_unlock(&irq_mapping_update_lock);
    +   xen_free_irq(irq);
    +   return -1;
     }
     #endif
     
@@@@@@ -726,56 -749,43 -722,43 -722,43 -773,38 +753,56 @@@@@@ int xen_destroy_irq(int irq
     
        if (xen_initial_domain()) {
                unmap_irq.pirq = info->u.pirq.pirq;
 ----           unmap_irq.domid = DOMID_SELF;
 ++++           unmap_irq.domid = info->u.pirq.domid;
                rc = HYPERVISOR_physdev_op(PHYSDEVOP_unmap_pirq, &unmap_irq);
 ----           if (rc) {
 ++++           /* If another domain quits without making the pci_disable_msix
 ++++            * call, the Xen hypervisor takes care of freeing the PIRQs
 ++++            * (free_domain_pirqs).
 ++++            */
 ++++           if ((rc == -ESRCH && info->u.pirq.domid != DOMID_SELF))
 ++++                   printk(KERN_INFO "domain %d does not have %d anymore\n",
 ++++                           info->u.pirq.domid, info->u.pirq.pirq);
 ++++           else if (rc) {
                        printk(KERN_WARNING "unmap irq failed %d\n", rc);
                        goto out;
                }
    -           pirq_to_irq[info->u.pirq.pirq] = -1;
        }
    -   irq_info[irq] = mk_unbound_info();
     
    -   irq_free_desc(irq);
    +   xen_free_irq(irq);
     
     out:
        spin_unlock(&irq_mapping_update_lock);
        return rc;
     }
     
    -int xen_vector_from_irq(unsigned irq)
    +int xen_irq_from_pirq(unsigned pirq)
     {
    -   return vector_from_irq(irq);
    -}
    +   int irq;
     
    -int xen_gsi_from_irq(unsigned irq)
    -{
    -   return gsi_from_irq(irq);
    +   struct irq_info *info;
    +
    +   spin_lock(&irq_mapping_update_lock);
    +
    +   list_for_each_entry(info, &xen_irq_list_head, list) {
    +           if (info == NULL || info->type != IRQT_PIRQ)
    +                   continue;
    +           irq = info->irq;
    +           if (info->u.pirq.pirq == pirq)
    +                   goto out;
    +   }
    +   irq = -1;
    +out:
    +   spin_unlock(&irq_mapping_update_lock);
    +
    +   return irq;
     }
     
    -int xen_irq_from_pirq(unsigned pirq)
 ++++
 ++++int xen_pirq_from_irq(unsigned irq)
 +++ {
    -   return pirq_to_irq[pirq];
 ++++   return pirq_from_irq(irq);
 +++ }
    -
 ++++EXPORT_SYMBOL_GPL(xen_pirq_from_irq);
     int bind_evtchn_to_irq(unsigned int evtchn)
     {
        int irq;
        irq = evtchn_to_irq[evtchn];
     
        if (irq == -1) {
    -           irq = find_unbound_irq();
    +           irq = xen_allocate_irq_dynamic();
    +           if (irq == -1)
    +                   goto out;
     
    -           set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
    -                                         handle_fasteoi_irq, "event");
    +           irq_set_chip_and_handler_name(irq, &xen_dynamic_chip,
- --                                          handle_fasteoi_irq, "event");
+ +++                                         handle_edge_irq, "event");
     
    -           evtchn_to_irq[evtchn] = irq;
    -           irq_info[irq] = mk_evtchn_info(evtchn);
    +           xen_irq_info_evtchn_init(irq, evtchn);
        }
     
    +out:
        spin_unlock(&irq_mapping_update_lock);
     
        return irq;
@@@@@@ -812,11 -822,11 -795,11 -795,11 -839,11 +839,11 @@@@@@ static int bind_ipi_to_irq(unsigned in
        irq = per_cpu(ipi_to_irq, cpu)[ipi];
     
        if (irq == -1) {
    -           irq = find_unbound_irq();
    +           irq = xen_allocate_irq_dynamic();
                if (irq < 0)
                        goto out;
     
    -           set_irq_chip_and_handler_name(irq, &xen_percpu_chip,
    +           irq_set_chip_and_handler_name(irq, &xen_percpu_chip,
                                              handle_percpu_irq, "ipi");
     
                bind_ipi.vcpu = cpu;
                        BUG();
                evtchn = bind_ipi.port;
     
    -           evtchn_to_irq[evtchn] = irq;
    -           irq_info[irq] = mk_ipi_info(evtchn, ipi);
    -           per_cpu(ipi_to_irq, cpu)[ipi] = irq;
    +           xen_irq_info_ipi_init(cpu, irq, evtchn, ipi);
     
                bind_evtchn_to_cpu(evtchn, cpu);
        }
        return irq;
     }
     
    +static int bind_interdomain_evtchn_to_irq(unsigned int remote_domain,
    +                                     unsigned int remote_port)
    +{
    +   struct evtchn_bind_interdomain bind_interdomain;
    +   int err;
    +
    +   bind_interdomain.remote_dom  = remote_domain;
    +   bind_interdomain.remote_port = remote_port;
    +
    +   err = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain,
    +                                     &bind_interdomain);
    +
    +   return err ? : bind_evtchn_to_irq(bind_interdomain.local_port);
    +}
    +
     
     int bind_virq_to_irq(unsigned int virq, unsigned int cpu)
     {
        irq = per_cpu(virq_to_irq, cpu)[virq];
     
        if (irq == -1) {
    -           irq = find_unbound_irq();
    +           irq = xen_allocate_irq_dynamic();
    +           if (irq == -1)
    +                   goto out;
     
    -           set_irq_chip_and_handler_name(irq, &xen_percpu_chip,
    +           irq_set_chip_and_handler_name(irq, &xen_percpu_chip,
                                              handle_percpu_irq, "virq");
     
                bind_virq.virq = virq;
                        BUG();
                evtchn = bind_virq.port;
     
    -           evtchn_to_irq[evtchn] = irq;
    -           irq_info[irq] = mk_virq_info(evtchn, virq);
    -
    -           per_cpu(virq_to_irq, cpu)[virq] = irq;
    +           xen_irq_info_virq_init(cpu, irq, evtchn, virq);
     
                bind_evtchn_to_cpu(evtchn, cpu);
        }
     
    +out:
        spin_unlock(&irq_mapping_update_lock);
     
        return irq;
@@@@@@ -917,9 -927,9 -900,9 -900,9 -931,11 +944,9 @@@@@@ static void unbind_from_irq(unsigned in
                evtchn_to_irq[evtchn] = -1;
        }
     
    -   if (irq_info[irq].type != IRQT_UNBOUND) {
    -           irq_info[irq] = mk_unbound_info();
    +   BUG_ON(info_for_irq(irq)->type == IRQT_UNBOUND);
     
    -           irq_free_desc(irq);
    -   }
    +   xen_free_irq(irq);
     
        spin_unlock(&irq_mapping_update_lock);
     }
@@@@@@ -929,11 -939,11 -912,12 -912,11 -945,10 +956,11 @@@@@@ int bind_evtchn_to_irqhandler(unsigned 
                              unsigned long irqflags,
                              const char *devname, void *dev_id)
     {
  - -   unsigned int irq;
  - -   int retval;
  + +   int irq, retval;
     
        irq = bind_evtchn_to_irq(evtchn);
    +   if (irq < 0)
    +           return irq;
        retval = request_irq(irq, handler, irqflags, devname, dev_id);
        if (retval != 0) {
                unbind_from_irq(irq);
     }
     EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler);
     
    +int bind_interdomain_evtchn_to_irqhandler(unsigned int remote_domain,
    +                                     unsigned int remote_port,
    +                                     irq_handler_t handler,
    +                                     unsigned long irqflags,
    +                                     const char *devname,
    +                                     void *dev_id)
    +{
    +   int irq, retval;
    +
    +   irq = bind_interdomain_evtchn_to_irq(remote_domain, remote_port);
    +   if (irq < 0)
    +           return irq;
    +
    +   retval = request_irq(irq, handler, irqflags, devname, dev_id);
    +   if (retval != 0) {
    +           unbind_from_irq(irq);
    +           return retval;
    +   }
    +
    +   return irq;
    +}
    +EXPORT_SYMBOL_GPL(bind_interdomain_evtchn_to_irqhandler);
    +
     int bind_virq_to_irqhandler(unsigned int virq, unsigned int cpu,
                            irq_handler_t handler,
                            unsigned long irqflags, const char *devname, void *dev_id)
     {
  - -   unsigned int irq;
  - -   int retval;
  + +   int irq, retval;
     
        irq = bind_virq_to_irq(virq, cpu);
    +   if (irq < 0)
    +           return irq;
        retval = request_irq(irq, handler, irqflags, devname, dev_id);
        if (retval != 0) {
                unbind_from_irq(irq);
@@@@@@ -999,7 -1009,7 -984,7 -982,7 -990,7 +1026,7 @@@@@@ int bind_ipi_to_irqhandler(enum ipi_vec
        if (irq < 0)
                return irq;
     
    -   irqflags |= IRQF_NO_SUSPEND;
    +   irqflags |= IRQF_NO_SUSPEND | IRQF_FORCE_RESUME;
        retval = request_irq(irq, handler, irqflags, devname, dev_id);
        if (retval != 0) {
                unbind_from_irq(irq);
@@@@@@ -1027,7 -1037,7 -1012,7 -1010,7 -1018,7 +1054,7 @@@@@@ irqreturn_t xen_debug_interrupt(int irq
     {
        struct shared_info *sh = HYPERVISOR_shared_info;
        int cpu = smp_processor_id();
    -   unsigned long *cpu_evtchn = cpu_evtchn_mask(cpu);
    +   unsigned long *cpu_evtchn = per_cpu(cpu_evtchn_mask, cpu);
        int i;
        unsigned long flags;
        static DEFINE_SPINLOCK(debug_lock);
     }
     
     static DEFINE_PER_CPU(unsigned, xed_nesting_count);
    +static DEFINE_PER_CPU(unsigned int, current_word_idx);
    +static DEFINE_PER_CPU(unsigned int, current_bit_idx);
    +
    +/*
    + * Mask out the i least significant bits of w
    + */
    +#define MASK_LSBS(w, i) (w & ((~0UL) << i))
     
     /*
      * Search the CPUs pending events bitmasks.  For each one found, map
      */
     static void __xen_evtchn_do_upcall(void)
     {
    +   int start_word_idx, start_bit_idx;
    +   int word_idx, bit_idx;
    +   int i;
        int cpu = get_cpu();
        struct shared_info *s = HYPERVISOR_shared_info;
        struct vcpu_info *vcpu_info = __this_cpu_read(xen_vcpu);
                wmb();
     #endif
                pending_words = xchg(&vcpu_info->evtchn_pending_sel, 0);
    -           while (pending_words != 0) {
    +
    +           start_word_idx = __this_cpu_read(current_word_idx);
    +           start_bit_idx = __this_cpu_read(current_bit_idx);
    +
    +           word_idx = start_word_idx;
    +
    +           for (i = 0; pending_words != 0; i++) {
                        unsigned long pending_bits;
    -                   int word_idx = __ffs(pending_words);
    -                   pending_words &= ~(1UL << word_idx);
    +                   unsigned long words;
    +
    +                   words = MASK_LSBS(pending_words, word_idx);
    +
    +                   /*
    +                    * If we masked out all events, wrap to beginning.
    +                    */
    +                   if (words == 0) {
    +                           word_idx = 0;
    +                           bit_idx = 0;
    +                           continue;
    +                   }
    +                   word_idx = __ffs(words);
    +
    +                   pending_bits = active_evtchns(cpu, s, word_idx);
    +                   bit_idx = 0; /* usually scan entire word from start */
    +                   if (word_idx == start_word_idx) {
    +                           /* We scan the starting word in two parts */
    +                           if (i == 0)
    +                                   /* 1st time: start in the middle */
    +                                   bit_idx = start_bit_idx;
    +                           else
    +                                   /* 2nd time: mask bits done already */
    +                                   bit_idx &= (1UL << start_bit_idx) - 1;
    +                   }
     
    -                   while ((pending_bits = active_evtchns(cpu, s, word_idx)) != 0) {
    -                           int bit_idx = __ffs(pending_bits);
    -                           int port = (word_idx * BITS_PER_LONG) + bit_idx;
    -                           int irq = evtchn_to_irq[port];
    +                   do {
    +                           unsigned long bits;
    +                           int port, irq;
                                struct irq_desc *desc;
     
    -                           mask_evtchn(port);
    -                           clear_evtchn(port);
    +                           bits = MASK_LSBS(pending_bits, bit_idx);
    +
    +                           /* If we masked out all events, move on. */
    +                           if (bits == 0)
    +                                   break;
    +
    +                           bit_idx = __ffs(bits);
    +
    +                           /* Process port. */
    +                           port = (word_idx * BITS_PER_LONG) + bit_idx;
    +                           irq = evtchn_to_irq[port];
     
- --                            mask_evtchn(port);
- --                            clear_evtchn(port);
- -- 
                                if (irq != -1) {
                                        desc = irq_to_desc(irq);
                                        if (desc)
                                                generic_handle_irq_desc(irq, desc);
                                }
    -                   }
    +
    +                           bit_idx = (bit_idx + 1) % BITS_PER_LONG;
    +
    +                           /* Next caller starts at last processed + 1 */
    +                           __this_cpu_write(current_word_idx,
    +                                            bit_idx ? word_idx :
    +                                            (word_idx+1) % BITS_PER_LONG);
    +                           __this_cpu_write(current_bit_idx, bit_idx);
    +                   } while (bit_idx != 0);
    +
    +                   /* Scan start_l1i twice; all others once. */
    +                   if ((word_idx != start_word_idx) || (i != 0))
    +                           pending_words &= ~(1UL << word_idx);
    +
    +                   word_idx = (word_idx + 1) % BITS_PER_LONG;
                }
     
                BUG_ON(!irqs_disabled());
@@@@@@ -1268,7 -1275,7 -1253,7 -1251,7 -1195,8 +1292,7 @@@@@@ void rebind_evtchn_irq(int evtchn, int 
           so there should be a proper type */
        BUG_ON(info->type == IRQT_UNBOUND);
     
    -   evtchn_to_irq[evtchn] = irq;
    -   irq_info[irq] = mk_evtchn_info(evtchn);
    +   xen_irq_info_evtchn_init(irq, evtchn);
     
        spin_unlock(&irq_mapping_update_lock);
     
@@@@@@ -1285,14 -1292,14 -1270,14 -1268,14 -1213,10 +1309,14 @@@@@@ static int rebind_irq_to_cpu(unsigned i
        struct evtchn_bind_vcpu bind_vcpu;
        int evtchn = evtchn_from_irq(irq);
     
    -   /* events delivered via platform PCI interrupts are always
    -    * routed to vcpu 0 */
    -   if (!VALID_EVTCHN(evtchn) ||
    -           (xen_hvm_domain() && !xen_have_vector_callback))
    +   if (!VALID_EVTCHN(evtchn))
    +           return -1;
    +
    +   /*
    +    * Events delivered via platform PCI interrupts are always
    +    * routed to vcpu 0 and hence cannot be rebound.
    +    */
    +   if (xen_hvm_domain() && !xen_have_vector_callback)
                return -1;
     
        /* Send future instances of this interrupt to other vcpu. */
        return 0;
     }
     
    -static int set_affinity_irq(unsigned irq, const struct cpumask *dest)
    +static int set_affinity_irq(struct irq_data *data, const struct cpumask *dest,
    +                       bool force)
     {
        unsigned tcpu = cpumask_first(dest);
     
    -   return rebind_irq_to_cpu(irq, tcpu);
    +   return rebind_irq_to_cpu(data->irq, tcpu);
     }
     
     int resend_irq_on_evtchn(unsigned int irq)
        return 1;
     }
     
    -static void enable_dynirq(unsigned int irq)
    +static void enable_dynirq(struct irq_data *data)
     {
    -   int evtchn = evtchn_from_irq(irq);
    +   int evtchn = evtchn_from_irq(data->irq);
     
        if (VALID_EVTCHN(evtchn))
                unmask_evtchn(evtchn);
     }
     
    -static void disable_dynirq(unsigned int irq)
    +static void disable_dynirq(struct irq_data *data)
     {
    -   int evtchn = evtchn_from_irq(irq);
    +   int evtchn = evtchn_from_irq(data->irq);
     
        if (VALID_EVTCHN(evtchn))
                mask_evtchn(evtchn);
     }
     
    -static void ack_dynirq(unsigned int irq)
    +static void ack_dynirq(struct irq_data *data)
     {
    -   int evtchn = evtchn_from_irq(irq);
    +   int evtchn = evtchn_from_irq(data->irq);
     
- --    irq_move_masked_irq(data);
    -   move_masked_irq(irq);
+ +++   irq_move_irq(data);
     
        if (VALID_EVTCHN(evtchn))
- ---           unmask_evtchn(evtchn);
+ +++           clear_evtchn(evtchn);
+ ++ }
+ ++ 
    -static int retrigger_dynirq(unsigned int irq)
+ +++static void mask_ack_dynirq(struct irq_data *data)
+ ++ {
    -   int evtchn = evtchn_from_irq(irq);
+ +++   disable_dynirq(data);
+ +++   ack_dynirq(data);
    +}
    +
    +static int retrigger_dynirq(struct irq_data *data)
    +{
    +   int evtchn = evtchn_from_irq(data->irq);
        struct shared_info *sh = HYPERVISOR_shared_info;
        int ret = 0;
     
        return ret;
     }
     
    -static void restore_cpu_pirqs(void)
    +static void restore_pirqs(void)
     {
        int pirq, rc, irq, gsi;
        struct physdev_map_pirq map_irq;
    +   struct irq_info *info;
     
    -   for (pirq = 0; pirq < nr_irqs; pirq++) {
    -           irq = pirq_to_irq[pirq];
    -           if (irq == -1)
    +   list_for_each_entry(info, &xen_irq_list_head, list) {
    +           if (info->type != IRQT_PIRQ)
                        continue;
     
    +           pirq = info->u.pirq.pirq;
    +           gsi = info->u.pirq.gsi;
    +           irq = info->irq;
    +
                /* save/restore of PT devices doesn't work, so at this point the
                 * only devices present are GSI based emulated devices */
    -           gsi = gsi_from_irq(irq);
                if (!gsi)
                        continue;
     
                if (rc) {
                        printk(KERN_WARNING "xen map irq failed gsi=%d irq=%d pirq=%d rc=%d\n",
                                        gsi, irq, pirq, rc);
    -                   irq_info[irq] = mk_unbound_info();
    -                   pirq_to_irq[pirq] = -1;
    +                   xen_free_irq(irq);
                        continue;
                }
     
                printk(KERN_DEBUG "xen: --> irq=%d, pirq=%d\n", irq, map_irq.pirq);
     
    -           startup_pirq(irq);
    +           __startup_pirq(irq);
        }
     }
     
@@@@@@ -1437,7 -1450,7 -1422,7 -1420,7 -1358,8 +1467,7 @@@@@@ static void restore_cpu_virqs(unsigned 
                evtchn = bind_virq.port;
     
                /* Record the new mapping. */
    -           evtchn_to_irq[evtchn] = irq;
    -           irq_info[irq] = mk_virq_info(evtchn, virq);
    +           xen_irq_info_virq_init(cpu, irq, evtchn, virq);
                bind_evtchn_to_cpu(evtchn, cpu);
        }
     }
@@@@@@ -1461,7 -1474,7 -1446,7 -1444,7 -1383,8 +1491,7 @@@@@@ static void restore_cpu_ipis(unsigned i
                evtchn = bind_ipi.port;
     
                /* Record the new mapping. */
    -           evtchn_to_irq[evtchn] = irq;
    -           irq_info[irq] = mk_ipi_info(evtchn, ipi);
    +           xen_irq_info_ipi_init(cpu, irq, evtchn, ipi);
                bind_evtchn_to_cpu(evtchn, cpu);
        }
     }
@@@@@@ -1519,22 -1532,10 -1504,10 -1502,10 -1442,10 +1549,22 @@@@@@ void xen_poll_irq(int irq
        xen_poll_irq_timeout(irq, 0 /* no timeout */);
     }
     
 ++++/* Check whether the IRQ line is shared with other guests. */
 ++++int xen_test_irq_shared(int irq)
 ++++{
 ++++   struct irq_info *info = info_for_irq(irq);
 ++++   struct physdev_irq_status_query irq_status = { .irq = info->u.pirq.pirq };
 ++++
 ++++   if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
 ++++           return 0;
 ++++   return !(irq_status.flags & XENIRQSTAT_shared);
 ++++}
 ++++EXPORT_SYMBOL_GPL(xen_test_irq_shared);
 ++++
     void xen_irq_resume(void)
     {
    -   unsigned int cpu, irq, evtchn;
    -   struct irq_desc *desc;
    +   unsigned int cpu, evtchn;
    +   struct irq_info *info;
     
        init_evtchn_cpu_bindings();
     
                mask_evtchn(evtchn);
     
        /* No IRQ <-> event-channel mappings. */
    -   for (irq = 0; irq < nr_irqs; irq++)
    -           irq_info[irq].evtchn = 0; /* zap event-channel binding */
    +   list_for_each_entry(info, &xen_irq_list_head, list)
    +           info->evtchn = 0; /* zap event-channel binding */
     
        for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
                evtchn_to_irq[evtchn] = -1;
                restore_cpu_ipis(cpu);
        }
     
    -   /*
    -    * Unmask any IRQF_NO_SUSPEND IRQs which are enabled. These
    -    * are not handled by the IRQ core.
    -    */
    -   for_each_irq_desc(irq, desc) {
    -           if (!desc->action || !(desc->action->flags & IRQF_NO_SUSPEND))
    -                   continue;
    -           if (desc->status & IRQ_DISABLED)
    -                   continue;
    -
    -           evtchn = evtchn_from_irq(irq);
    -           if (evtchn == -1)
    -                   continue;
    -
    -           unmask_evtchn(evtchn);
    -   }
    -
    -   restore_cpu_pirqs();
    +   restore_pirqs();
     }
     
     static struct irq_chip xen_dynamic_chip __read_mostly = {
    -   .name           = "xen-dyn",
    +   .name                   = "xen-dyn",
     
    -   .disable        = disable_dynirq,
    -   .mask           = disable_dynirq,
    -   .unmask         = enable_dynirq,
    +   .irq_disable            = disable_dynirq,
    +   .irq_mask               = disable_dynirq,
    +   .irq_unmask             = enable_dynirq,
     
- --    .irq_eoi                = ack_dynirq,
    -   .eoi            = ack_dynirq,
    -   .set_affinity   = set_affinity_irq,
    -   .retrigger      = retrigger_dynirq,
+ +++   .irq_ack                = ack_dynirq,
+ +++   .irq_mask_ack           = mask_ack_dynirq,
+ +++
    +   .irq_set_affinity       = set_affinity_irq,
    +   .irq_retrigger          = retrigger_dynirq,
     };
     
     static struct irq_chip xen_pirq_chip __read_mostly = {
    -   .name           = "xen-pirq",
    -
    -   .startup        = startup_pirq,
    -   .shutdown       = shutdown_pirq,
    +   .name                   = "xen-pirq",
     
    -   .enable         = enable_pirq,
    -   .unmask         = enable_pirq,
    +   .irq_startup            = startup_pirq,
    +   .irq_shutdown           = shutdown_pirq,
- -- 
    +   .irq_enable             = enable_pirq,
- --    .irq_unmask             = enable_pirq,
- -- 
    +   .irq_disable            = disable_pirq,
- --    .irq_mask               = disable_pirq,
     
- --    .irq_ack                = ack_pirq,
    -   .disable        = disable_pirq,
    -   .mask           = disable_pirq,
+ +++   .irq_mask               = disable_dynirq,
+ +++   .irq_unmask             = enable_dynirq,
+ ++ 
    -   .ack            = ack_pirq,
    -   .end            = end_pirq,
+ +++   .irq_ack                = eoi_pirq,
+ +++   .irq_eoi                = eoi_pirq,
+ +++   .irq_mask_ack           = mask_ack_pirq,
     
    -   .set_affinity   = set_affinity_irq,
    +   .irq_set_affinity       = set_affinity_irq,
     
    -   .retrigger      = retrigger_dynirq,
    +   .irq_retrigger          = retrigger_dynirq,
     };
     
     static struct irq_chip xen_percpu_chip __read_mostly = {
    -   .name           = "xen-percpu",
    +   .name                   = "xen-percpu",
     
    -   .disable        = disable_dynirq,
    -   .mask           = disable_dynirq,
    -   .unmask         = enable_dynirq,
    +   .irq_disable            = disable_dynirq,
    +   .irq_mask               = disable_dynirq,
    +   .irq_unmask             = enable_dynirq,
     
    -   .ack            = ack_dynirq,
    +   .irq_ack                = ack_dynirq,
     };
     
     int xen_set_callback_via(uint64_t via)
@@@@@@ -1640,6 -1644,6 -1613,6 -1611,6 -1569,17 +1673,6 @@@@@@ void __init xen_init_IRQ(void
     {
        int i;
     
    -   cpu_evtchn_mask_p = kcalloc(nr_cpu_ids, sizeof(struct cpu_evtchn_s),
    -                               GFP_KERNEL);
    -   irq_info = kcalloc(nr_irqs, sizeof(*irq_info), GFP_KERNEL);
    -
    -   /* We are using nr_irqs as the maximum number of pirq available but
    -    * that number is actually chosen by Xen and we don't know exactly
    -    * what it is. Be careful choosing high pirq numbers. */
    -   pirq_to_irq = kcalloc(nr_irqs, sizeof(*pirq_to_irq), GFP_KERNEL);
    -   for (i = 0; i < nr_irqs; i++)
    -           pirq_to_irq[i] = -1;
    -
        evtchn_to_irq = kcalloc(NR_EVENT_CHANNELS, sizeof(*evtchn_to_irq),
                                    GFP_KERNEL);
        for (i = 0; i < NR_EVENT_CHANNELS; i++)