/****************************************************************************** * memory.c * * Code to handle memory-related requests. * * Copyright (c) 2003-2004, B Dragovic * Copyright (c) 2003-2005, K A Fraser */ #include #include #include #include #include #include #include #include #include #include #include #include #include /* * To allow safe resume of do_memory_op() after preemption, we need to know * at what point in the page list to resume. For this purpose I steal the * high-order bits of the @cmd parameter, which are otherwise unused and zero. */ #define START_EXTENT_SHIFT 4 /* cmd[:4] == start_extent */ static long increase_reservation( struct domain *d, XEN_GUEST_HANDLE(xen_pfn_t) extent_list, unsigned int nr_extents, unsigned int extent_order, unsigned int memflags, int *preempted) { struct page_info *page; unsigned long i; xen_pfn_t mfn; if ( !guest_handle_is_null(extent_list) && !guest_handle_okay(extent_list, nr_extents) ) return 0; if ( (extent_order != 0) && !multipage_allocation_permitted(current->domain) ) return 0; for ( i = 0; i < nr_extents; i++ ) { if ( hypercall_preempt_check() ) { *preempted = 1; return i; } if ( unlikely((page = alloc_domheap_pages( d, extent_order, memflags)) == NULL) ) { DPRINTK("Could not allocate order=%d extent: " "id=%d memflags=%x (%ld of %d)\n", extent_order, d->domain_id, memflags, i, nr_extents); return i; } /* Inform the domain of the new page's machine address. */ if ( !guest_handle_is_null(extent_list) ) { mfn = page_to_mfn(page); if ( unlikely(__copy_to_guest_offset(extent_list, i, &mfn, 1)) ) return i; } } return nr_extents; } static long populate_physmap( struct domain *d, XEN_GUEST_HANDLE(xen_pfn_t) extent_list, unsigned int nr_extents, unsigned int extent_order, unsigned int memflags, int *preempted) { struct page_info *page; unsigned long i, j; xen_pfn_t gpfn; xen_pfn_t mfn; if ( !guest_handle_okay(extent_list, nr_extents) ) return 0; if ( (extent_order != 0) && !multipage_allocation_permitted(current->domain) ) return 0; for ( i = 0; i < nr_extents; i++ ) { if ( hypercall_preempt_check() ) { *preempted = 1; goto out; } if ( unlikely(__copy_from_guest_offset(&gpfn, extent_list, i, 1)) ) goto out; if ( unlikely((page = alloc_domheap_pages( d, extent_order, memflags)) == NULL) ) { DPRINTK("Could not allocate order=%d extent: " "id=%d memflags=%x (%ld of %d)\n", extent_order, d->domain_id, memflags, i, nr_extents); goto out; } mfn = page_to_mfn(page); if ( unlikely(shadow_mode_translate(d)) ) { for ( j = 0; j < (1 << extent_order); j++ ) guest_physmap_add_page(d, gpfn + j, mfn + j); } else { for ( j = 0; j < (1 << extent_order); j++ ) set_gpfn_from_mfn(mfn + j, gpfn + j); /* Inform the domain of the new page's machine address. */ if ( unlikely(__copy_to_guest_offset(extent_list, i, &mfn, 1)) ) goto out; } } out: return i; } int guest_remove_page( struct domain *d, unsigned long gmfn) { struct page_info *page; unsigned long mfn; mfn = gmfn_to_mfn(d, gmfn); if ( unlikely(!mfn_valid(mfn)) ) { DPRINTK("Domain %u page number %lx invalid\n", d->domain_id, mfn); return 0; } page = mfn_to_page(mfn); if ( unlikely(!get_page(page, d)) ) { DPRINTK("Bad page free for domain %u\n", d->domain_id); return 0; } if ( test_and_clear_bit(_PGT_pinned, &page->u.inuse.type_info) ) put_page_and_type(page); if ( test_and_clear_bit(_PGC_allocated, &page->count_info) ) put_page(page); if ( unlikely((page->count_info & PGC_count_mask) != 1) ) { /* We'll make this a guest-visible error in future, so take heed! */ DPRINTK("Dom%d freeing in-use page %lx (pseudophys %lx):" " count=%x type=%lx\n", d->domain_id, mfn, get_gpfn_from_mfn(mfn), page->count_info, page->u.inuse.type_info); } guest_physmap_remove_page(d, gmfn, mfn); put_page(page); return 1; } static long decrease_reservation( struct domain *d, XEN_GUEST_HANDLE(xen_pfn_t) extent_list, unsigned int nr_extents, unsigned int extent_order, int *preempted) { unsigned long i, j; xen_pfn_t gmfn; if ( !guest_handle_okay(extent_list, nr_extents) ) return 0; for ( i = 0; i < nr_extents; i++ ) { if ( hypercall_preempt_check() ) { *preempted = 1; return i; } if ( unlikely(__copy_from_guest_offset(&gmfn, extent_list, i, 1)) ) return i; for ( j = 0; j < (1 << extent_order); j++ ) { if ( !guest_remove_page(d, gmfn + j) ) return i; } } return nr_extents; } static long translate_gpfn_list( XEN_GUEST_HANDLE(xen_translate_gpfn_list_t) uop, unsigned long *progress) { struct xen_translate_gpfn_list op; unsigned long i; xen_pfn_t gpfn; xen_pfn_t mfn; struct domain *d; if ( copy_from_guest(&op, uop, 1) ) return -EFAULT; /* Is size too large for us to encode a continuation? */ if ( op.nr_gpfns > (ULONG_MAX >> START_EXTENT_SHIFT) ) return -EINVAL; if ( !guest_handle_okay(op.gpfn_list, op.nr_gpfns) || !guest_handle_okay(op.mfn_list, op.nr_gpfns) ) return -EFAULT; if ( op.domid == DOMID_SELF ) op.domid = current->domain->domain_id; else if ( !IS_PRIV(current->domain) ) return -EPERM; if ( (d = find_domain_by_id(op.domid)) == NULL ) return -ESRCH; if ( !shadow_mode_translate(d) ) { put_domain(d); return -EINVAL; } for ( i = *progress; i < op.nr_gpfns; i++ ) { if ( hypercall_preempt_check() ) { put_domain(d); *progress = i; return -EAGAIN; } if ( unlikely(__copy_from_guest_offset(&gpfn, op.gpfn_list, i, 1)) ) { put_domain(d); return -EFAULT; } mfn = gmfn_to_mfn(d, gpfn); if ( unlikely(__copy_to_guest_offset(op.mfn_list, i, &mfn, 1)) ) { put_domain(d); return -EFAULT; } } put_domain(d); return 0; } static long memory_exchange(XEN_GUEST_HANDLE(xen_memory_exchange_t) arg) { struct xen_memory_exchange exch; LIST_HEAD(in_chunk_list); LIST_HEAD(out_chunk_list); unsigned long in_chunk_order, out_chunk_order; xen_pfn_t gpfn, gmfn, mfn; unsigned long i, j, k; unsigned int memflags = 0; long rc = 0; struct domain *d; struct page_info *page; if ( copy_from_guest(&exch, arg, 1) ) return -EFAULT; /* Various sanity checks. */ if ( (exch.nr_exchanged > exch.in.nr_extents) || /* Input and output domain identifiers match? */ (exch.in.domid != exch.out.domid) || /* Sizes of input and output lists do not overflow a long? */ ((~0UL >> exch.in.extent_order) < exch.in.nr_extents) || ((~0UL >> exch.out.extent_order) < exch.out.nr_extents) || /* Sizes of input and output lists match? */ ((exch.in.nr_extents << exch.in.extent_order) != (exch.out.nr_extents << exch.out.extent_order)) ) {
// Copyright 2009 Google Inc. All Rights Reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//     * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Author: vladl@google.com (Vlad Losev)

// This sample shows how to use Google Test listener API to implement
// an alternative console output and how to use the UnitTest reflection API
// to enumerate test cases and tests and to inspect their results.

#include <stdio.h>

#include "gtest/gtest.h"

using ::testing::EmptyTestEventListener;
using ::testing::InitGoogleTest;
using ::testing::Test;
using ::testing::TestCase;
using ::testing::TestEventListeners;
using ::testing::TestInfo;
using ::testing::TestPartResult;
using ::testing::UnitTest;

namespace {

// Provides alternative output mode which produces minimal amount of
// information about tests.
class TersePrinter : public EmptyTestEventListener {
 private:
  // Called before any test activity starts.
  virtual void OnTestProgramStart(const UnitTest& /* unit_test */) {}

  // Called after all test activities have ended.
  virtual void OnTestProgramEnd(const UnitTest& unit_test) {
    fprintf(stdout, "TEST %s\n", unit_test.Passed() ? "PASSED" : "FAILED");
    fflush(stdout);
  }

  // Called before a test starts.
  virtual void OnTestStart(const TestInfo& test_info) {
    fprintf(stdout,
            "*** Test %s.%s starting.\n",
            test_info.test_case_name(),
            test_info.name());
    fflush(stdout);
  }

  // Called after a failed assertion or a SUCCEED() invocation.
  virtual void OnTestPartResult(const TestPartResult& test_part_result) {
    fprintf(stdout,
            "%s in %s:%d\n%s\n",
            test_part_result.failed() ? "*** Failure" : "Success",
            test_part_result.file_name(),
            test_part_result.line_number(),
            test_part_result.summary());
    fflush(stdout);
  }

  // Called after a test ends.
  virtual void OnTestEnd(const TestInfo& test_info) {
    fprintf(stdout,
            "*** Test %s.%s ending.\n",
            test_info.test_case_name(),
            test_info.name());
    fflush(stdout);
  }
};  // class TersePrinter

TEST(CustomOutputTest, PrintsMessage) {
  printf("Printing something from the test body...\n");
}

TEST(CustomOutputTest, Succeeds) {
  SUCCEED() << "SUCCEED() has been invoked from here";
}

TEST(CustomOutputTest, Fails) {
  EXPECT_EQ(1, 2)
      << "This test fails in order to demonstrate alternative failure messages";
}

}  // namespace

int main(int argc, char **argv) {
  InitGoogleTest(&argc, argv);

  bool terse_output = false;
  if (argc > 1 && strcmp(argv[1], "--terse_output") == 0 )
    terse_output = true;
  else
    printf("%s\n", "Run this program with --terse_output to change the way "
           "it prints its output.");

  UnitTest& unit_test = *UnitTest::GetInstance();

  // If we are given the --terse_output command line flag, suppresses the
  // standard output and attaches own result printer.
  if (terse_output) {
    TestEventListeners& listeners = unit_test.listeners();

    // Removes the default console output listener from the list so it will
    // not receive events from Google Test and won't print any output. Since
    // this operation transfers ownership of the listener to the caller we
    // have to delete it as well.
    delete listeners.Release(listeners.default_result_printer());

    // Adds the custom output listener to the list. It will now receive
    // events from Google Test and print the alternative output. We don't
    // have to worry about deleting it since Google Test assumes ownership
    // over it after adding it to the list.
    listeners.Append(new TersePrinter);
  }
  int ret_val = RUN_ALL_TESTS();

  // This is an example of using the UnitTest reflection API to inspect test
  // results. Here we discount failures from the tests we expected to fail.
  int unexpectedly_failed_tests = 0;
  for (int i = 0; i < unit_test.total_test_case_count(); ++i) {
    const TestCase& test_case = *unit_test.GetTestCase(i);
    for (int j = 0; j < test_case.total_test_count(); ++j) {
      const TestInfo& test_info = *test_case.GetTestInfo(j);
      // Counts failed tests that were not meant to fail (those without
      // 'Fails' in the name).
      if (test_info.result()->Failed() &&
          strcmp(test_info.name(), "Fails") != 0) {
        unexpectedly_failed_tests++;
      }
    }
  }

  // Test that were meant to fail should not affect the test program outcome.
  if (unexpectedly_failed_tests == 0)
    ret_val = 0;

  return ret_val;
}