From patchwork Thu Jul 28 10:50:07 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anthony PERARD X-Patchwork-Id: 9251085 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 712C060869 for ; Thu, 28 Jul 2016 10:53:46 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 620DE26785 for ; Thu, 28 Jul 2016 10:53:46 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 56A3D26B4A; Thu, 28 Jul 2016 10:53:46 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id CB29F26785 for ; Thu, 28 Jul 2016 10:53:45 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bSitd-0007Dh-HK; Thu, 28 Jul 2016 10:50:33 +0000 Received: from mail6.bemta3.messagelabs.com ([195.245.230.39]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bSitc-0007B1-C2 for xen-devel@lists.xen.org; Thu, 28 Jul 2016 10:50:32 +0000 Received: from [85.158.137.68] by server-6.bemta-3.messagelabs.com id 9F/67-28813-773E9975; Thu, 28 Jul 2016 10:50:31 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFprAIsWRWlGSWpSXmKPExsXitHRDpG7545n hBpdf81os+biYxYHR4+ju30wBjFGsmXlJ+RUJrBkbW3cyFXxSrJj5/BVTA2ODZBcjJ4eEgL9E W+MhJhCbTcBAYsX0q4wgtoiArMTqrjnsXYxcHMwCRxglbt38CZYQBmqYeegLkM3BwSKgKnHmO zeIyStgL3GrkxtipJxEw/n7zCA2J1B45eH77CC2kICdxKGNC5khbDWJGwuXsYDYvAKCEidnPg GzmQUkJA6+eMEMMYdb4vbpqcwTGPlmISmbhaRsASPTKkaN4tSistQiXUMLvaSizPSMktzEzBx dQwNjvdzU4uLE9NScxKRiveT83E2MwJCqZ2Bg3MH4+7TnIUZJDiYlUd6w0JnhQnxJ+SmVGYnF GfFFpTmpxYcYZTg4lCR4Mx8B5QSLUtNTK9Iyc4DBDZOW4OBREuHdBpLmLS5IzC3OTIdInWJUl BLnNQFJCIAkMkrz4NpgEXWJUVZKmJeRgYFBiKcgtSg3swRV/hWjOAejkjDvTJApPJl5JXDTXw EtZgJaXBw7A2RxSSJCSqqBUSFQb67eyxtVhX27d3o4X3TurBUyLp3U98AoPFZsVb/aCYO6mqP lz84FfEt8vbj/8Ny7Vls+e0fte1R4PupxjK72BYbY3B+xae9zr2abLDE7Zc+z6tbZeTMyfbwO cdX8kw02kX/CPvPPfvttgnlLl/9Z1X4kd02G/9P4jT6KS9ikldP2Tro0Q4mlOCPRUIu5qDgRA GMV30+jAgAA X-Env-Sender: prvs=0100a76d6=anthony.perard@citrix.com X-Msg-Ref: server-4.tower-31.messagelabs.com!1469703028!2637216!2 X-Originating-IP: [66.165.176.89] X-SpamReason: No, hits=0.0 required=7.0 tests=sa_preprocessor: VHJ1c3RlZCBJUDogNjYuMTY1LjE3Ni44OSA9PiAyMDMwMDc=\n, received_headers: No Received headers X-StarScan-Received: X-StarScan-Version: 8.77; banners=-,-,- X-VirusChecked: Checked Received: (qmail 3655 invoked from network); 28 Jul 2016 10:50:30 -0000 Received: from smtp.citrix.com (HELO SMTP.CITRIX.COM) (66.165.176.89) by server-4.tower-31.messagelabs.com with RC4-SHA encrypted SMTP; 28 Jul 2016 10:50:30 -0000 X-IronPort-AV: E=Sophos;i="5.28,433,1464652800"; d="scan'208";a="368929254" From: Anthony PERARD To: Date: Thu, 28 Jul 2016 11:50:07 +0100 Message-ID: <20160728105013.22310-10-anthony.perard@citrix.com> X-Mailer: git-send-email 2.9.0 In-Reply-To: <20160728105013.22310-1-anthony.perard@citrix.com> References: <20160728105013.22310-1-anthony.perard@citrix.com> MIME-Version: 1.0 X-DLP: MIA2 Cc: Anthony PERARD , Andrew Cooper , Ian Jackson , Wei Liu , Jan Beulich Subject: [Xen-devel] [PATCH v7 09/15] hvmloader: Check modules whereabouts in perform_tests X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP As perform_tests() is going to clear memory past 4MB, we check that the memory can be use or we skip the tests. Signed-off-by: Anthony PERARD --- Changes in V6: - define and use TEST_START and PT_END. - cast addresses to uintptr_t instead of uint32_t. - use UINTPTR_MAX for upper limit checks, instead of UINT_MAX. - fix typos - include xen/arch-x86/hvm/start_info.h - better check for the cmdlines, now check if a string would cross the 4GB boundary. Changes in V5: - also account for the pages table - fix coding style - also check modules cmdline and main cmdline and modlist_paddr - make use of check_overlap. Changes in v4: - move the check into the perform_test() function. - skip tests instead of using BUG. New in V3 --- tools/firmware/hvmloader/tests.c | 76 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 75 insertions(+), 1 deletion(-) diff --git a/tools/firmware/hvmloader/tests.c b/tools/firmware/hvmloader/tests.c index fea3ad3..4b5cb93 100644 --- a/tools/firmware/hvmloader/tests.c +++ b/tools/firmware/hvmloader/tests.c @@ -20,6 +20,8 @@ */ #include "util.h" +#include "config.h" +#include #define TEST_FAIL 0 #define TEST_PASS 1 @@ -32,8 +34,10 @@ * 4 page table pages reside at 8MB+4kB to 8MB+20kB. * Pagetables identity-map 0-16MB, except 4kB at va 6MB maps to pa 5MB. */ +#define TEST_START (4ul << 20) #define PD_START (8ul << 20) #define PT_START (PD_START + 4096) +#define PT_END (PT_START + 4 * PAGE_SIZE) static void setup_paging(void) { @@ -189,6 +193,37 @@ static int shadow_gs_test(void) return (ebx == 2) ? TEST_PASS : TEST_FAIL; } +static bool check_test_overlap(uint64_t start, uint64_t size) +{ + if ( start ) + return check_overlap(start, size, + TEST_START, + PT_END - TEST_START); + return false; +} + +/* Only return true if the string overlap with the TEST_START,PT_END */ +static bool check_string_overlap_with_test(uint64_t paddr) +{ + unsigned len = 0; + const char *s; + + if ( !paddr || paddr > UINTPTR_MAX ) + return false; + + s = (char *)(uintptr_t)paddr; + while ( *s && (uintptr_t)s < UINTPTR_MAX ) + s++; + + /* Not valid string, ignore it. */ + if ( (uintptr_t)s == UINTPTR_MAX && *s ) + return false; + + len = (uintptr_t)s - paddr; + + return check_test_overlap(paddr, len); +} + void perform_tests(void) { int i, passed, skipped; @@ -210,11 +245,50 @@ void perform_tests(void) return; } + /* Check that tests does not use memory where modules are stored */ + if ( check_test_overlap((uintptr_t)hvm_start_info, + sizeof(*hvm_start_info)) ) + { + printf("Skipping tests due to memory used by hvm_start_info\n"); + return; + } + if ( check_test_overlap(hvm_start_info->modlist_paddr, + hvm_start_info->nr_modules * + sizeof(struct hvm_modlist_entry)) ) + { + printf("Skipping tests due to memory used by" + " hvm_start_info->modlist\n"); + return; + } + for ( i = 0; i < hvm_start_info->nr_modules; i++ ) + { + const struct hvm_modlist_entry *modlist = + (void *)(uintptr_t)hvm_start_info->modlist_paddr; + + if ( check_test_overlap(modlist[i].paddr, modlist[i].size) ) + { + printf("Skipping tests due to memory used by module[%d]\n", i); + return; + } + if ( check_string_overlap_with_test(modlist[i].cmdline_paddr) ) + { + printf("Skipping tests due to memory used by" + " module[%d]'s cmdline\n", i); + return; + } + } + if ( check_string_overlap_with_test(hvm_start_info->cmdline_paddr) ) + { + printf("Skipping tests due to memory used by the" + " hvm_start_info->cmdline\n"); + return; + } + passed = skipped = 0; for ( i = 0; tests[i].test; i++ ) { printf(" - %s ... ", tests[i].description); - memset((char *)(4ul << 20), 0, 4ul << 20); + memset((char *)TEST_START, 0, 4ul << 20); setup_paging(); switch ( (*tests[i].test)() ) {