aboutsummaryrefslogtreecommitdiffstats
path: root/docs/gitbook/images/favicon.ico
blob: 2b4e04abafb33adc0bdf4c8da56de2dd73cce097 (plain)
ofshex dumpascii
0000 00 00 01 00 01 00 10 10 00 00 01 00 20 00 68 04 00 00 16 00 00 00 28 00 00 00 10 00 00 00 20 00 ..............h.......(.........
0020 00 00 01 00 20 00 00 00 00 00 00 04 00 00 13 0b 00 00 13 0b 00 00 00 00 00 00 00 00 00 00 00 00 ................................
0040 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................................
0060 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................................
0080 00 00 00 00 00 00 00 00 00 00 8c 8c 8c ff 00 00 00 00 8c 8c 8c ff 00 00 00 00 8c 8c 8c ff 00 00 ................................
00a0 00 00 8c 8c 8c ff 00 00 00 00 8c 8c 8c ff 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................................
00c0 00 00 33 33 33 42 33 33 33 d0 33 33 33 ff 33 33 33 ff 33 33 33 ff 33 33 33 ff 33 33 33 ff 33 33 ..333B333.333.333.333.333.333.33
00e0 33 ff 33 33 33 ff 33 33 33 ff 33 33 33 ff 33 33 33 d0 33 33 33 42 00 00 00 00 00 00 00 00 00 00 3.333.333.333.333.333B..........
0100 00 00 33 33 33 d0 33 33 33 ff 33 33 33 ff 33 33 33 ff 33 33 33 ff 33 33 33 ff 33 33 33 ff 33 33 ..333.333.333.333.333.333.333.33
0120 33 ff 33 33 33 ff 33 33 33 ff 33 33 33 ff 33 33 33 ff 33 33 33 d0 00 00 00 00 00 00 00 00 8c 8c 3.333.333.333.333.333...........
0140 8c ff 33 33 33 ff 33 33 33 ff 33 33 33 ff 33 33 33 ff 33 33 33 ff 3b 3b 3b ff d3 d3 d3 ff 3b 3b ..333.333.333.333.333.;;;.....;;
0160 3b ff 33 33 33 ff 33 33 33 ff 33 33 33 ff 33 33 33 ff 33 33 33 ff 8c 8c 8c ff 00 00 00 00 00 00 ;.333.333.333.333.333...........
0180 00 00 33 33 33 ff 33 33 33 ff 33 33 33 ff 33 33 33 ff 33 33 33 ff 47 47 47 ff ff ff ff ff 47 47 ..333.333.333.333.333.GGG.....GG
01a0 47 ff 33 33 33 ff 33 33 33 ff 33 33 33 ff 33 33 33 ff 33 33 33 ff 00 00 00 00 00 00 00 00 8c 8c G.333.333.333.333.333...........
01c0 8c ff 33 33 33 ff 33 33 33 ff 33 33 33 ff 63 63 63 ff e1 e1 e1 ff f9 f9 f9 ff ff ff ff ff f8 f8 ..333.333.333.ccc...............
01e0 f8 ff dc dc dc ff 56 56 56 ff 33 33 33 ff 33 33 33 ff 33 33 33 ff 8c 8c 8c ff 00 00 00 00 00 00 ......VVV.333.333.333...........
0200 00 00 33 33 33 ff 33 33 33 ff 40 40 40 ff fa fa fa ff df df df ff 7c 7c 7c ff ff ff ff ff 7c 7c ..333.333.@@@.........|||.....||
0220 7c ff e0 e0 e0 ff f7 f7 f7 ff 3b 3b 3b ff 33 33 33 ff 33 33 33 ff 00 00 00 00 00 00 00 00 8c 8c |.........;;;.333.333...........
0240 8c ff 33 33 33 ff 33 33 33 ff 90 90 90 ff fd fd fd ff 39 39 39 ff 47 47 47 ff ff ff ff ff 47 47 ..333.333.........999.GGG.....GG
0260 47 ff 39 39 39 ff fd fd fd ff 87 87 87 ff 33 33 33 ff 33 33 33 ff 8c 8c 8c ff 00 00 00 00 00 00 G.999.........333.333...........
0280 00 00 33 33 33 ff 33 33 33 ff b0 b0 b0 ff f6 f6 f6 ff 33 33 33 ff 47 47 47 ff ff ff ff ff 47 47 ..333.333.........333.GGG.....GG
02a0 47 ff 33 33 33 ff f6 f6 f6 ff af af af ff 33 33 33 ff 33 33 33 ff 00 00 00 00 00 00 00 00 8c 8c G.333.........333.333...........
02c0 8c ff 33 33 33 ff 33 33 33 ff b0 b0 b0 ff f6 f6 f6 ff 33 33 33 ff 47 47 47 ff ff ff ff ff 47 47 ..333.333.........333.GGG.....GG
02e0 47 ff 33 33 33 ff f5 f5 f5 ff b0 b0 b0 ff 33 33 33 ff 33 33 33 ff 8c 8c 8c ff 00 00 00 00 00 00 G.333.........333.333...........
0300 00 00 33 33 33 ff 33 33 33 ff b0 b0 b0 ff f6 f6 f6 ff 33 33 33 ff 47 47 47 ff ff ff ff ff 47 47 ..333.333.........333.GGG.....GG
0320 47 ff 33 33 33 ff f5 f5 f5 ff b0 b0 b0 ff 33 33 33 ff 33 33 33 ff 00 00 00 00 00 00 00 00 8c 8c G.333.........333.333...........
0340 8c ff 33 33 33 ff 33 33 33 ff 71 71 71 ff ae ae ae ff 33 33 33 ff 3b 3b 3b ff c5 c5 c5 ff 3b 3b ..333.333.qqq.....333.;;;.....;;
0360 3b ff 33 33 33 ff ae ae ae ff 71 71 71 ff 33 33 33 ff 33 33 33 ff 8c 8c 8c ff 00 00 00 00 00 00 ;.333.....qqq.333.333...........
0380 00 00 33 33 33 d0 33 33 33 ff 33 33 33 ff 33 33 33 ff 33 33 33 ff 33 33 33 ff 33 33 33 ff 33 33 ..333.333.333.333.333.333.333.33
03a0 33 ff 33 33 33 ff 33 33 33 ff 33 33 33 ff 33 33 33 ff 33 33 33 d0 00 00 00 00 00 00 00 00 00 00 3.333.333.333.333.333...........
03c0 00 00 33 33 33 43 33 33 33 d0 33 33 33 ff 33 33 33 ff 33 33 33 ff 33 33 33 ff 33 33 33 ff 33 33 ..333C333.333.333.333.333.333.33
03e0 33 ff 33 33 33 ff 33 33 33 ff 33 33 33 ff 33 33 33 d0 33 33 33 42 00 00 00 00 00 00 00 00 00 00 3.333.333.333.333.333B..........
0400 00 00 00 00 00 00 00 00 00 00 8c 8c 8c ff 00 00 00 00 8c 8c 8c ff 00 00 00 00 8c 8c 8c ff 00 00 ................................
0420 00 00 8c 8c 8c ff 00 00 00 00 8c 8c 8c ff 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ff ff ................................
0440 00 00 ea af 00 00 80 03 00 00 80 03 00 00 00 01 00 00 80 03 00 00 00 01 00 00 80 03 00 00 00 01 ................................
0460 00 00 80 03 00 00 00 01 00 00 80 03 00 00 00 01 00 00 80 03 00 00 80 03 00 00 ea af 00 00 ..............................
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
/******************************************************************************
 * common/grant_table.c
 * 
 * Mechanism for granting foreign access to page frames, and receiving
 * page-ownership transfers.
 * 
 * Copyright (c) 2005 Christopher Clark
 * Copyright (c) 2004 K A Fraser
 * Copyright (c) 2005 Andrew Warfield
 * Modifications by Geoffrey Lefebvre are (c) Intel Research Cambridge
 * 
 * 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/lib.h>
#include <xen/sched.h>
#include <xen/shadow.h>
#include <xen/mm.h>
#include <xen/trace.h>
#include <xen/guest_access.h>
#include <acm/acm_hooks.h>

#define PIN_FAIL(_lbl, _rc, _f, _a...)          \
    do {                                        \
        DPRINTK( _f, ## _a );                   \
        rc = (_rc);                             \
        goto _lbl;                              \
    } while ( 0 )

static inline int
get_maptrack_handle(
    struct grant_table *t)
{
    unsigned int h;
    if ( unlikely((h = t->maptrack_head) == (t->maptrack_limit - 1)) )
        return -1;
    t->maptrack_head = t->maptrack[h].ref;
    t->map_count++;
    return h;
}

static inline void
put_maptrack_handle(
    struct grant_table *t, int handle)
{
    t->maptrack[handle].ref = t->maptrack_head;
    t->maptrack_head = handle;
    t->map_count--;
}

/*
 * Returns 0 if TLB flush / invalidate required by caller.
 * va will indicate the address to be invalidated.
 * 
 * addr is _either_ a host virtual address, or the address of the pte to
 * update, as indicated by the GNTMAP_contains_pte flag.
 */
static void
__gnttab_map_grant_ref(
    struct gnttab_map_grant_ref *op)
{
    struct domain *ld, *rd;
    struct vcpu   *led;
    int            handle;
    unsigned long  frame = 0;
    int            rc = GNTST_okay;
    struct active_grant_entry *act;

    /* Entry details from @rd's shared grant table. */
    grant_entry_t *sha;
    domid_t        sdom;
    u16            sflags;

    /*
     * We bound the number of times we retry CMPXCHG on memory locations that
     * we share with a guest OS. The reason is that the guest can modify that
     * location at a higher rate than we can read-modify-CMPXCHG, so the guest
     * could cause us to livelock. There are a few cases where it is valid for
     * the guest to race our updates (e.g., to change the GTF_readonly flag),
     * so we allow a few retries before failing.
     */
    int retries = 0;

    led = current;
    ld = led->domain;

    if ( unlikely(op->ref >= NR_GRANT_ENTRIES) ||
         unlikely((op->flags & (GNTMAP_device_map|GNTMAP_host_map)) == 0) )
    {
        DPRINTK("Bad ref (%d) or flags (%x).\n", op->ref, op->flags);
        op->status = GNTST_bad_gntref;
        return;
    }

    if ( acm_pre_grant_map_ref(op->dom) )
    {
        op->status = GNTST_permission_denied;
        return;
    }

    if ( unlikely((rd = find_domain_by_id(op->dom)) == NULL) )
    {
        if ( rd != NULL )
            put_domain(rd);
        DPRINTK("Could not find domain %d\n", op->dom);
        op->status = GNTST_bad_domain;
        return;
    }

    /* Get a maptrack handle. */
    if ( unlikely((handle = get_maptrack_handle(ld->grant_table)) == -1) )
    {
        int                   i;
        struct grant_mapping *new_mt;
        struct grant_table   *lgt = ld->grant_table;

        if ( (lgt->maptrack_limit << 1) > MAPTRACK_MAX_ENTRIES )
        {
            put_domain(rd);
            DPRINTK("Maptrack table is at maximum size.\n");
            op->status = GNTST_no_device_space;
            return;
        }

        /* Grow the maptrack table. */
        new_mt = alloc_xenheap_pages(lgt->maptrack_order + 1);
        if ( new_mt == NULL )
        {
            put_domain(rd);
            DPRINTK("No more map handles available.\n");
            op->status = GNTST_no_device_space;
            return;
        }

        memcpy(new_mt, lgt->maptrack, PAGE_SIZE << lgt->maptrack_order);
        for ( i = lgt->maptrack_limit; i < (lgt->maptrack_limit << 1); i++ )
            new_mt[i].ref = i+1;

        free_xenheap_pages(lgt->maptrack, lgt->maptrack_order);
        lgt->maptrack          = new_mt;
        lgt->maptrack_order   += 1;
        lgt->maptrack_limit  <<= 1;

        DPRINTK("Doubled maptrack size\n");
        handle = get_maptrack_handle(ld->grant_table);
    }

    act = &rd->grant_table->active[op->ref];
    sha = &rd->grant_table->shared[op->ref];

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

    if ( !act->pin ||
         (!(op->flags & GNTMAP_readonly) &&
          !(act->pin & (GNTPIN_hstw_mask|GNTPIN_devw_mask))) )
    {
        sflags = sha->flags;
        sdom   = sha->domid;

        /*
         * This loop attempts to set the access (reading/writing) flags
         * in the grant table entry.  It tries a cmpxchg on the field
         * up to five times, and then fails under the assumption that 
         * the guest is misbehaving.
         */
        for ( ; ; )
        {
            u32 scombo, prev_scombo, new_scombo;

            if ( unlikely((sflags & GTF_type_mask) != GTF_permit_access) ||
                 unlikely(sdom != led->domain->domain_id) )
                PIN_FAIL(unlock_out, GNTST_general_error,
                         "Bad flags (%x) or dom (%d). (NB. expected dom %d)\n",
                         sflags, sdom, led->domain->domain_id);

            /* Merge two 16-bit values into a 32-bit combined update. */
            /* NB. Endianness! */
            scombo = ((u32)sdom << 16) | (u32)sflags;

            new_scombo = scombo | GTF_reading;
            if ( !(op->flags & GNTMAP_readonly) )
            {
                new_scombo |= GTF_writing;
                if ( unlikely(sflags & GTF_readonly) )
                    PIN_FAIL(unlock_out, GNTST_general_error,
                             "Attempt to write-pin a r/o grant entry.\n");
            }

            prev_scombo = cmpxchg((u32 *)&sha->flags, scombo, new_scombo);

            /* Did the combined update work (did we see what we expected?). */
            if ( likely(prev_scombo == scombo) )
                break;

            if ( retries++ == 4 )
                PIN_FAIL(unlock_out, GNTST_general_error,
                         "Shared grant entry is unstable.\n");

            /* Didn't see what we expected. Split out the seen flags & dom. */
            /* NB. Endianness! */
            sflags = (u16)prev_scombo;
            sdom   = (u16)(prev_scombo >> 16);
        }

        if ( !act->pin )
        {
            act->domid = sdom;
            act->frame = gmfn_to_mfn(rd, sha->frame);
        }
    }
    else if ( (act->pin & 0x80808080U) != 0 )
        PIN_FAIL(unlock_out, ENOSPC,
                 "Risk of counter overflow %08x\n", act->pin);

    if ( op->flags & GNTMAP_device_map )