aboutsummaryrefslogtreecommitdiffstats
path: root/toolchain/info.mk
Commit message (Collapse)AuthorAgeFilesLines
* uClibc cleanup: - use full version string (0.9.30.1), instead of base ↵Nicolas Thill2009-04-241-1/+1
| | | | | | (0.9.30) + extra (.1) - remove support for 0.9.28 and snapshots (building from SVN to be added later) SVN-Revision: 15368
* add support for alternative C libraries (currently only glibc/eglibc) other ↵Nicolas Thill2009-01-081-2/+4
| | | | | | (related) changes: - kernel headers are now installed using "make headers_install" on 2.6 - target names now contain an openwrt "vendor" tag (e.g. mips-openwrt-linux-gnu) - build directory names now contain gcc/libc name/version - default cpu for x86 is now i486 (required to build glibc/eglibc) SVN-Revision: 13931
* build system cleanup/restructuring as described in ↵Felix Fietkau2007-08-071-0/+4
http://lists.openwrt.org/pipermail/openwrt-devel/2007-August/001159.html SVN-Revision: 8362
id='n102' href='#n102'>102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975
/******************************************************************************
 * arch/x86/audit.c
 * 
 * Copyright (c) 2002-2005 K A Fraser
 * Copyright (c) 2004 Christian Limpach
 * Copyright (c) 2005 Michael A Fetterman
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <xen/config.h>
#include <xen/init.h>
#include <xen/kernel.h>
#include <xen/lib.h>
#include <xen/mm.h>
#include <xen/perfc.h>
#include <asm/shadow.h>
#include <asm/page.h>
#include <asm/flushtlb.h>

/* XXX SMP bug -- these should not be statics... */
static int ttot=0, ctot=0, io_mappings=0, lowmem_mappings=0;
static int l1, l2, oos_count, page_count;

#define FILE_AND_LINE 0

#if FILE_AND_LINE
#define adjust(_p, _a) _adjust((_p), (_a), __FILE__, __LINE__)
#define ADJUST_EXTRA_ARGS ,const char *file, int line
#define APRINTK(_f, _a...) printk(_f " %s:%d\n", ## _a, file, line)
#else
#define adjust _adjust
#define ADJUST_EXTRA_ARGS
#define APRINTK(_f, _a...) printk(_f "\n", ##_a)
#endif

int audit_adjust_pgtables(struct domain *d, int dir, int noisy)
{
    int errors = 0;
    int shadow_refcounts = !!shadow_mode_refcounts(d);
    int shadow_enabled = !!shadow_mode_enabled(d);
    int l2limit;

    void _adjust(struct pfn_info *page, int adjtype ADJUST_EXTRA_ARGS)
    {
        if ( adjtype )
        {
            // adjust the type count
            //
            int tcount = page->u.inuse.type_info & PGT_count_mask;
            tcount += dir;
            ttot++;

            if ( page_get_owner(page) == NULL )
            {
                APRINTK("adjust(mfn=%lx, dir=%d, adjtype=%d) owner=NULL",
                        page_to_pfn(page), dir, adjtype);
                errors++;
            }

            if ( tcount < 0 )
            {
                APRINTK("Audit %d: type count went below zero "
                        "mfn=%lx t=%" PRtype_info " ot=%x",
                        d->domain_id, page_to_pfn(page),
                        page->u.inuse.type_info,
                        page->tlbflush_timestamp);
                errors++;
            }
            else if ( (tcount & ~PGT_count_mask) != 0 )
            {
                APRINTK("Audit %d: type count overflowed "
                        "mfn=%lx t=%" PRtype_info " ot=%x",
                        d->domain_id, page_to_pfn(page),
                        page->u.inuse.type_info,
                        page->tlbflush_timestamp);
                errors++;
            }
            else
                page->u.inuse.type_info += dir;
        }

        // adjust the general count
        //
        int count = page->count_info & PGC_count_mask;
        count += dir;
        ctot++;

        if ( count < 0 )
        {
            APRINTK("Audit %d: general count went below zero "
                    "mfn=%lx t=%" PRtype_info " ot=%x",
                    d->domain_id, page_to_pfn(page),
                    page->u.inuse.type_info,
                    page->tlbflush_timestamp);
            errors++;
        }
        else if ( (count & ~PGT_count_mask) != 0 )
        {
            APRINTK("Audit %d: general count overflowed "
                    "mfn=%lx t=%" PRtype_info " ot=%x",
                    d->domain_id, page_to_pfn(page),
                    page->u.inuse.type_info,
                    page->tlbflush_timestamp);
            errors++;
        }
        else
            page->count_info += dir;
    }

    void adjust_l2_page(unsigned long mfn, int shadow)
    {
        unsigned long *pt = map_domain_page(mfn);
        int i;

        for ( i = 0; i < l2limit; i++ )
        {
            if ( pt[i] & _PAGE_PRESENT )
            {
                unsigned long l1mfn = pt[i] >> PAGE_SHIFT;
                struct pfn_info *l1page = pfn_to_page(l1mfn);

                if ( noisy )
                {
                    if ( shadow )
                    {
                        if ( page_get_owner(l1page) != NULL )
                        {
                            printk("L2: Bizarre shadow L1 page mfn=%lx "
                                   "belonging to a domain %p (id=%d)\n",
                                   l1mfn,
                                   page_get_owner(l1page),
                                   page_get_owner(l1page)->domain_id);
                            errors++;
                            continue;
                        }

                        u32 page_type = l1page->u.inuse.type_info & PGT_type_mask;

                        if ( page_type != PGT_l1_shadow )
                        {
                            printk("Audit %d: [Shadow L2 mfn=%lx i=%x] "
                                   "Expected Shadow L1 t=%" PRtype_info 
				   " mfn=%lx\n",
                                   d->domain_id, mfn, i,
                                   l1page->u.inuse.type_info, l1mfn);
                            errors++;
                        }
                    }
                    else
                    {
                        if ( page_get_owner(l1page) != d )
                        {
                            printk("L2: Skip bizarre L1 page mfn=%lx "
                                   "belonging to other dom %p (id=%d)\n",
                                   l1mfn,
                                   page_get_owner(l1page),
                                   (page_get_owner(l1page)
                                    ? page_get_owner(l1page)->domain_id
                                    : -1));
                            errors++;
                            continue;
                        }

                        u32 page_type = l1page->u.inuse.type_info & PGT_type_mask;

                        if ( page_type == PGT_l2_page_table )
                        {
                            printk("Audit %d: [%x] Found %s Linear PT "
                                   "t=%" PRtype_info " mfn=%lx\n",
                                   d->domain_id, i, (l1mfn==mfn) ? "Self" : "Other",
                                   l1page->u.inuse.type_info, l1mfn);
                        }
                        else if ( page_type != PGT_l1_page_table )
                        {
                            printk("Audit %d: [L2 mfn=%lx i=%x] "
                                   "Expected L1 t=%" PRtype_info " mfn=%lx\n",
                                   d->domain_id, mfn, i,
                                   l1page->u.inuse.type_info, l1mfn);
                            errors++;
                        }
                    }
                }

                adjust(l1page, !shadow);
            }
        }

        if ( shadow_mode_translate(d) && !shadow_mode_external(d) )
        {
            unsigned long hl2mfn =
                pt[l2_table_offset(LINEAR_PT_VIRT_START)] >> PAGE_SHIFT;
            struct pfn_info *hl2page = pfn_to_page(hl2mfn);
            adjust(hl2page, 0);
        }

        unmap_domain_page(pt);
    }

    void adjust_hl2_page(unsigned long hl2mfn)
    {
        unsigned long *pt = map_domain_page(hl2mfn);
        int i;

        for ( i = 0; i < l2limit; i++ )
        {
            if ( pt[i] & _PAGE_PRESENT )
            {
                unsigned long gmfn = pt[i] >> PAGE_SHIFT;
                struct pfn_info *gpage = pfn_to_page(gmfn);

                if ( gmfn < 0x100 )
                {
                    lowmem_mappings++;
                    continue;
                }

                if ( gmfn > max_page )
                {
                    io_mappings++;
                    continue;
                }

                if ( noisy )
                {
                    if ( page_get_owner(gpage) != d )
                    {
                        printk("Audit %d: [hl2mfn=%lx,i=%x] Skip foreign page "
                               "dom=%p (id=%d) mfn=%lx c=%08x t=%"
			       PRtype_info "\n",
                               d->domain_id, hl2mfn, i,
                               page_get_owner(gpage),
                               page_get_owner(gpage)->domain_id,
                               gmfn,
                               gpage->count_info,
                               gpage->u.inuse.type_info);
                        continue;
                    }
                }
                adjust(gpage, 0);
            }
        }

        unmap_domain_page(pt);
    }

    void adjust_l1_page(unsigned long l1mfn)
    {
        unsigned long *pt = map_domain_page(l1mfn);
        int i;

        for ( i = 0; i < L1_PAGETABLE_ENTRIES; i++ )
        {
            if ( pt[i] & _PAGE_PRESENT )
            {
                unsigned long gmfn = pt[i] >> PAGE_SHIFT;
                struct pfn_info *gpage = pfn_to_page(gmfn);

                if ( gmfn < 0x100 )
                {
                    lowmem_mappings++;
                    continue;
                }

                if ( gmfn > max_page )
                {
                    io_mappings++;
                    continue;
                }

                if ( noisy )
                {
                    if ( pt[i] & _PAGE_RW )
                    {
                        // If it's not a writable page, complain.
                        //
                        if ( !((gpage->u.inuse.type_info & PGT_type_mask) ==
                               PGT_writable_page) )
                        {
                            printk("Audit %d: [l1mfn=%lx, i=%x] Illegal RW "
                                   "t=%" PRtype_info " mfn=%lx\n",
                                   d->domain_id, l1mfn, i,
                                   gpage->u.inuse.type_info, gmfn);
                            errors++;
                        }

                        if ( shadow_refcounts &&
                             page_is_page_table(gpage) &&
                             ! page_out_of_sync(gpage) )
                        {
                            printk("Audit %d: [l1mfn=%lx, i=%x] Illegal RW of "
                                   "page table gmfn=%lx\n",
                                   d->domain_id, l1mfn, i, gmfn);
                            errors++;
                        }
                    }		   

                    if ( page_get_owner(gpage) != d )
                    {
                        printk("Audit %d: [l1mfn=%lx,i=%x] Skip foreign page "
                               "dom=%p (id=%d) mfn=%lx c=%08x t=%" 
			       PRtype_info "\n",
                               d->domain_id, l1mfn, i,
                               page_get_owner(gpage),
                               page_get_owner(gpage)->domain_id,
                               gmfn,
                               gpage->count_info,
                               gpage->u.inuse.type_info);
                        continue;
                    }
                }

                adjust(gpage, (pt[i] & _PAGE_RW) ? 1 : 0);
            }
        }

        unmap_domain_page(pt);
    }

    void adjust_shadow_tables()
    {
        struct shadow_status *a;
        unsigned long smfn, gmfn;
        struct pfn_info *page;
        int i;

        for ( i = 0; i < shadow_ht_buckets; i++ )
        {
            a = &d->arch.shadow_ht[i];
            while ( a && a->gpfn_and_flags )
            {
                gmfn = __gpfn_to_mfn(d, a->gpfn_and_flags & PGT_mfn_mask);
                smfn = a->smfn;
                page = &frame_table[smfn];

                switch ( a->gpfn_and_flags & PGT_type_mask ) {
                case PGT_writable_pred:
                    break;
                case PGT_snapshot:
                    adjust(pfn_to_page(gmfn), 0);
                    break;
                case PGT_l1_shadow:
                    adjust(pfn_to_page(gmfn), 0);
                    if ( shadow_refcounts )
                        adjust_l1_page(smfn);
                    if ( page->u.inuse.type_info & PGT_pinned )
                        adjust(page, 0);
                    break;
                case PGT_hl2_shadow:
                    adjust(pfn_to_page(gmfn), 0);
                    if ( shadow_refcounts )
                        adjust_hl2_page(smfn);
                    if ( page->u.inuse.type_info & PGT_pinned )
                        adjust(page, 0);
                    break;
                case PGT_l2_shadow:
                    adjust(pfn_to_page(gmfn), 0);
                    adjust_l2_page(smfn, 1);
                    if ( page->u.inuse.type_info & PGT_pinned )
                        adjust(page, 0);
                    break;
                default:
                    BUG();
                    break;
                }

                a = a->next;
            }
        }
    }

    void adjust_oos_list()
    {
        struct out_of_sync_entry *oos;

        if ( (oos = d->arch.out_of_sync) )
            ASSERT(shadow_enabled);

        while ( oos )
        {
            adjust(pfn_to_page(oos->gmfn), 0);

            // Only use entries that have low bits clear...
            //
            if ( !(oos->writable_pl1e & (sizeof(l1_pgentry_t)-1)) )
                adjust(pfn_to_page(oos->writable_pl1e >> PAGE_SHIFT), 0);

            if ( oos->snapshot_mfn != SHADOW_SNAPSHOT_ELSEWHERE )
                adjust(pfn_to_page(oos->snapshot_mfn), 0);

            oos = oos->next;
            oos_count++;
        }
    }

    void adjust_for_pgtbase()
    {
        struct vcpu *v;

        for_each_vcpu(d, v)
        {
            if ( pagetable_get_paddr(v->arch.guest_table) )
                adjust(&frame_table[pagetable_get_pfn(v->arch.guest_table)], 1);
            if ( pagetable_get_paddr(v->arch.shadow_table) )
                adjust(&frame_table[pagetable_get_pfn(v->arch.shadow_table)], 0);
            if ( v->arch.monitor_shadow_ref )
                adjust(&frame_table[v->arch.monitor_shadow_ref], 0);
        }
    }

    void adjust_guest_pages()
    {
        struct list_head *list_ent = d->page_list.next;
        struct pfn_info *page;
        unsigned long mfn, snapshot_mfn;

        while ( list_ent != &d->page_list )
        {
            u32 page_type;

            page = list_entry(list_ent, struct pfn_info, list);
            snapshot_mfn = mfn = page_to_pfn(page);
            page_type = page->u.inuse.type_info & PGT_type_mask;

            BUG_ON(page_get_owner(page) != d);

            page_count++;

            if ( shadow_enabled && !shadow_refcounts &&
                 page_out_of_sync(page) )
            {
                unsigned long gpfn = __mfn_to_gpfn(d, mfn);
                ASSERT( VALID_M2P(gpfn) );
                snapshot_mfn = __shadow_status(d, gpfn, PGT_snapshot);
                ASSERT( snapshot_mfn );
            }

            switch ( page_type )
            {
            case PGT_l2_page_table:
                l2++;

                if ( noisy )
                {
                    if ( shadow_refcounts )
                    {
                        printk("Audit %d: found an L2 guest page "
                               "mfn=%lx t=%" PRtype_info " c=%08x while in shadow mode\n",
                               d->domain_id, mfn, page->u.inuse.type_info,
                               page->count_info);
                        errors++;
                    }

                    if ( (page->u.inuse.type_info & PGT_count_mask) != 0 )
                    {
                        if ( (page->u.inuse.type_info & PGT_validated) !=
                             PGT_validated )
                        {
                            printk("Audit %d: L2 mfn=%lx not validated %"
				   PRtype_info "\n",
                                   d->domain_id, mfn, page->u.inuse.type_info);
                            errors++;
                        }

                        if ( (page->u.inuse.type_info & PGT_pinned) != PGT_pinned )
                        {
                            printk("Audit %d: L2 mfn=%lx not pinned t=%"
				   PRtype_info "\n",
                                   d->domain_id, mfn, page->u.inuse.type_info);
                            errors++;
                        }
                    }
                }

                if ( page->u.inuse.type_info & PGT_pinned )
                    adjust(page, 1);

                if ( page->u.inuse.type_info & PGT_validated )
                    adjust_l2_page(snapshot_mfn, 0);

                break;

            case PGT_l1_page_table:
                l1++;

                if ( noisy )
                {
                    if ( shadow_refcounts )
                    {
                        printk("found an L1 guest page mfn=%lx t=%" 
			       PRtype_info " c=%08x "
                               "while in shadow mode\n",
                               mfn, page->u.inuse.type_info, page->count_info);
                        errors++;
                    }

                    if ( (page->u.inuse.type_info & PGT_count_mask) != 0 )
                    {
                        if ( (page->u.inuse.type_info & PGT_validated) !=
                             PGT_validated )
                        {
                            printk("Audit %d: L1 not validated mfn=%lx t=%"
				   PRtype_info "\n",
                                   d->domain_id, mfn, page->u.inuse.type_info);
                            errors++;
                        }
                    }
                }
                
                if ( page->u.inuse.type_info & PGT_pinned )
                    adjust(page, 1);

                if ( page->u.inuse.type_info & PGT_validated )
                    adjust_l1_page(snapshot_mfn);

                break;

            case PGT_gdt_page:
                ASSERT( !page_out_of_sync(page) );
                adjust(page, 1);
                break;

            case PGT_ldt_page:
                ASSERT( !page_out_of_sync(page) );
                adjust(page, 1);
                break;

            case PGT_writable_page:
                if ( shadow_refcounts )
                {
                    // In shadow mode, writable pages can get pinned by
                    // paravirtualized guests that think they are pinning
                    // their L1s and/or L2s.
                    //
                    if ( page->u.inuse.type_info & PGT_pinned )
                        adjust(page, 1);
                }
            }

            list_ent = page->list.next;
        }
    }

#ifdef __i386__
    if ( shadow_mode_external(d) )
        l2limit = L2_PAGETABLE_ENTRIES;
    else
        l2limit = DOMAIN_ENTRIES_PER_L2_PAGETABLE;
#else
    l2limit = 0; /* XXX x86/64 XXX */
#endif

    adjust_for_pgtbase();

    adjust_guest_pages();

    if ( shadow_enabled )
    {
        adjust_oos_list();
        adjust_shadow_tables();
    }

    adjust(virt_to_page(d->shared_info), 1);

    return errors;
}


#ifndef NDEBUG

void audit_pagelist(struct domain *d)
{
    struct list_head *list_ent;
    int xenpages, totpages;

    list_ent = d->xenpage_list.next;
    for ( xenpages = 0; (list_ent != &d->xenpage_list); xenpages++ )
    {
        list_ent = list_ent->next;
    }
    list_ent = d->page_list.next;
    for ( totpages = 0; (list_ent != &d->page_list); totpages++ )
    {
        list_ent = list_ent->next;
    }

    if ( xenpages != d->xenheap_pages ||
         totpages != d->tot_pages )
    {
        printk("ARGH! dom %d: xen=%d %d, pages=%d %d\n", d->domain_id,
               xenpages, d->xenheap_pages, 
               totpages, d->tot_pages );
    }
}

void _audit_domain(struct domain *d, int flags)
{
    int shadow_refcounts = !!shadow_mode_refcounts(d);

    void scan_for_pfn_in_mfn(struct domain *d, unsigned long xmfn,
                             unsigned long mfn)
    {
        struct pfn_info *page = &frame_table[mfn];
        unsigned long *pt = map_domain_page(mfn);
        int i;

        for ( i = 0; i < L1_PAGETABLE_ENTRIES; i++ )
        {
            if ( (pt[i] & _PAGE_PRESENT) && ((pt[i] >> PAGE_SHIFT) == xmfn) )
                printk("     found dom=%d mfn=%lx t=%" PRtype_info " c=%08x "
                       "pt[i=%x]=%lx\n",
                       d->domain_id, mfn, page->u.inuse.type_info,
                       page->count_info, i, pt[i]);
        }

        unmap_domain_page(pt);           
    }

    void scan_for_pfn_in_grant_table(struct domain *d, unsigned xmfn)
    {
        int i;
        active_grant_entry_t *act = d->grant_table->active;

        spin_lock(&d->grant_table->lock);

        for ( i = 0; i < NR_GRANT_ENTRIES; i++ )
        {
            if ( act[i].pin && (act[i].frame == xmfn) )
            {
                printk("     found active grant table entry i=%d dom=%d pin=%d\n",
                       i, act[i].domid, act[i].pin);
            }
        }

        spin_unlock(&d->grant_table->lock);
    }

    void scan_for_pfn(struct domain *d, unsigned long xmfn)
    {
        scan_for_pfn_in_grant_table(d, xmfn);

        if ( !shadow_mode_enabled(d) )
        {
            struct list_head *list_ent = d->page_list.next;
            struct pfn_info *page;

            while ( list_ent != &d->page_list )
            {
                page = list_entry(list_ent, struct pfn_info, list);

                switch ( page->u.inuse.type_info & PGT_type_mask )
                {
                case PGT_l1_page_table:
                case PGT_l2_page_table:
                    scan_for_pfn_in_mfn(d, xmfn, page_to_pfn(page));
                    break;
                default:
                    break;
                }

                list_ent = page->list.next;
            }
        }
        else
        {
            struct shadow_status *a;
            int i;
            
            for ( i = 0; i < shadow_ht_buckets; i++ )
            {
                a = &d->arch.shadow_ht[i];
                while ( a && a->gpfn_and_flags )
                {
                    switch ( a->gpfn_and_flags & PGT_type_mask )
                    {
                    case PGT_l1_shadow:
                    case PGT_l2_shadow:
                    case PGT_hl2_shadow:
                        scan_for_pfn_in_mfn(d, xmfn, a->smfn);
                        break;
                    case PGT_snapshot:
                    case PGT_writable_pred:
                        break;
                    default:
                        BUG();
                        break;
                    }
                    a = a->next;
                }
            }
        }
    }

    void scan_for_pfn_remote(unsigned long xmfn)
    {
        struct domain *e;
        for_each_domain ( e )
            scan_for_pfn( e, xmfn );
    } 

    unsigned long mfn;
    struct list_head *list_ent;
    struct pfn_info *page;
    int errors = 0;

    if ( (d != current->domain) && shadow_mode_translate(d) )
    {
        printk("skipping audit domain of translated domain %d "
               "from other context\n",
               d->domain_id);
        return;
    }

    if ( d != current->domain )
        domain_pause(d);

    // Maybe we should just be using BIGLOCK?
    //
    if ( !(flags & AUDIT_SHADOW_ALREADY_LOCKED) )
        shadow_lock(d);

    spin_lock(&d->page_alloc_lock);

    audit_pagelist(d);

    /* PHASE 0 */

    list_ent = d->page_list.next;
    while ( list_ent != &d->page_list )
    {
        u32 page_type;

        page = list_entry(list_ent, struct pfn_info, list);
        mfn = page_to_pfn(page);
        page_type = page->u.inuse.type_info & PGT_type_mask;

        BUG_ON(page_get_owner(page) != d);

        if ( (page->u.inuse.type_info & PGT_count_mask) >
             (page->count_info & PGC_count_mask) )
        {
            printk("taf(%" PRtype_info ") > caf(%08x) mfn=%lx\n",
                   page->u.inuse.type_info, page->count_info, mfn);
            errors++;
        }

        if ( shadow_mode_refcounts(d) &&
             (page_type == PGT_writable_page) &&
             !(page->u.inuse.type_info & PGT_validated) )
        {
            printk("shadow mode writable page not validated mfn=%lx " 
		   "t=%" PRtype_info  " c=%08x\n",
                   mfn, page->u.inuse.type_info, page->count_info);
            errors++;
        }
 
#if 0   /* SYSV shared memory pages plus writeable files. */
        if ( page_type == PGT_writable_page && 
             (page->u.inuse.type_info & PGT_count_mask) > 1 )
        {
            printk("writeable page with type count >1: "
                   "mfn=%lx t=%" PRtype_info " c=%08x\n",
                  mfn,
                  page->u.inuse.type_info,
                  page->count_info );
            errors++;
            scan_for_pfn_remote(mfn);
        }
#endif

        if ( page_type == PGT_none && 
             (page->u.inuse.type_info & PGT_count_mask) > 0 )
        {
            printk("normal page with type count >0: mfn=%lx t=%" PRtype_info " c=%08x\n",
                  mfn,
                  page->u.inuse.type_info,
                  page->count_info );
            errors++;
        }

        if ( page_out_of_sync(page) )
        {
            if ( !page_is_page_table(page) )
            {
                printk("out of sync page mfn=%lx is not a page table\n", mfn);
                errors++;
            }
            unsigned long pfn = __mfn_to_gpfn(d, mfn);
            if ( !__shadow_status(d, pfn, PGT_snapshot) )
            {
                printk("out of sync page mfn=%lx doesn't have a snapshot\n",
                       mfn);
                errors++;
            }
            if ( shadow_refcounts
                 ? (page_type != PGT_writable_page)
                 : !(page_type && (page_type <= PGT_l4_page_table)) )
            {
                printk("out of sync page mfn=%lx has strange type "
                       "t=%" PRtype_info  " c=%08x\n",
                       mfn, page->u.inuse.type_info, page->count_info);
                errors++;
            }
        }

        /* Use tlbflush_timestamp to store original type_info. */
        page->tlbflush_timestamp = page->u.inuse.type_info;

        list_ent = page->list.next;
    }

    /* PHASE 1 */
    io_mappings = lowmem_mappings = 0;

    errors += audit_adjust_pgtables(d, -1, 1);

    if ( !(flags & AUDIT_QUIET) &&
         ((io_mappings > 0) || (lowmem_mappings > 0)) )
        printk("Audit %d: Found %d lowmem mappings and %d io mappings\n",
               d->domain_id, lowmem_mappings, io_mappings);

    /* PHASE 2 */

    list_ent = d->page_list.next;
    while ( list_ent != &d->page_list )
    {
        page = list_entry(list_ent, struct pfn_info, list);
        mfn = page_to_pfn(page);

        switch ( page->u.inuse.type_info & PGT_type_mask)
        {
        case PGT_l1_page_table:
        case PGT_l2_page_table:
        case PGT_l3_page_table:
        case PGT_l4_page_table:
            if ( (page->u.inuse.type_info & PGT_count_mask) != 0 )
            {
                printk("Audit %d: type count!=0 t=%" PRtype_info " ot=%x c=%x mfn=%lx\n",
                       d->domain_id, page->u.inuse.type_info, 
                       page->tlbflush_timestamp,
                       page->count_info, mfn);
                errors++;
                scan_for_pfn_remote(mfn);
            }
            break;
        case PGT_none:
        case PGT_writable_page:
        case PGT_gdt_page:
        case PGT_ldt_page:
            if ( (page->u.inuse.type_info & PGT_count_mask) != 0 )
            {
                printk("Audit %d: type count!=0 t=%" PRtype_info " ot=%x c=%x mfn=%lx\n",
                       d->domain_id, page->u.inuse.type_info, 
                       page->tlbflush_timestamp,
                       page->count_info, mfn);
                //errors++;
            }
            break;
        default:
            BUG(); // XXX fix me...
        }
        
        if ( (page->count_info & PGC_count_mask) != 1 )
        {
            printk("Audit %d: gen count!=1 (c=%x) t=%" PRtype_info " ot=%x mfn=%lx\n",
                   d->domain_id,
                   page->count_info,
                   page->u.inuse.type_info, 
                   page->tlbflush_timestamp, mfn );
            //errors++;
            scan_for_pfn_remote(mfn);
        }

        list_ent = page->list.next;
    }

    if ( shadow_mode_enabled(d) )
    {
        struct shadow_status *a;
        struct pfn_info *page;
        u32 page_type;
        int i;

        for ( i = 0; i < shadow_ht_buckets; i++ )
        {
            a = &d->arch.shadow_ht[i];
            while ( a && a->gpfn_and_flags )
            {
                page = pfn_to_page(a->smfn);
                page_type = a->gpfn_and_flags & PGT_type_mask;

                switch ( page_type ) {
                case PGT_l1_shadow:
                case PGT_l2_shadow:
                case PGT_hl2_shadow:
                case PGT_snapshot:
                    if ( ((page->u.inuse.type_info & PGT_type_mask) != page_type ) ||
                         (page->count_info != 0) )
                    {
                        printk("Audit %d: shadow page counts wrong "
                               "mfn=%lx t=%" PRtype_info " c=%08x\n",
                               d->domain_id, page_to_pfn(page),
                               page->u.inuse.type_info,
                               page->count_info);
                        printk("a->gpfn_and_flags=%p\n",
                               (void *)a->gpfn_and_flags);
                        errors++;
                    }
                    break;
                case PGT_writable_pred:
                    // XXX - nothing to check?
                    break;

                default:
                    BUG();
                    break;
                }

                a = a->next;
            }
        }
    }

    /* PHASE 3 */
    ctot = ttot = page_count = l1 = l2 = oos_count = 0;

    audit_adjust_pgtables(d, 1, 0);

#if 0
    // This covers our sins of trashing the tlbflush_timestamps...
    //
    local_flush_tlb();
#endif

    spin_unlock(&d->page_alloc_lock);

    if ( !(flags & AUDIT_QUIET) )
        printk("Audit dom%d Done. "
               "pages=%d oos=%d l1=%d l2=%d ctot=%d ttot=%d\n",
               d->domain_id, page_count, oos_count, l1, l2, ctot, ttot);

    if ( !(flags & AUDIT_SHADOW_ALREADY_LOCKED) )
        shadow_unlock(d);

    if ( d != current->domain )
        domain_unpause(d);

    if ( errors && !(flags & AUDIT_ERRORS_OK) )
        BUG();
}

void audit_domains(void)
{
    struct domain *d;
    for_each_domain ( d )
        audit_domain(d);
}

void audit_domains_key(unsigned char key)
{
    audit_domains();
}
#endif