From patchwork Tue Jul 12 14:42:46 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anthony PERARD X-Patchwork-Id: 9225541 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 127C06086B for ; Tue, 12 Jul 2016 14:49:04 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 02DEF27D29 for ; Tue, 12 Jul 2016 14:49:04 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id EB8B327E78; Tue, 12 Jul 2016 14:49:03 +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 8998227D29 for ; Tue, 12 Jul 2016 14:49:03 +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 1bMyxG-00040N-JE; Tue, 12 Jul 2016 14:46:34 +0000 Received: from mail6.bemta3.messagelabs.com ([195.245.230.39]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bMyxF-0003yq-BE for xen-devel@lists.xen.org; Tue, 12 Jul 2016 14:46:33 +0000 Received: from [85.158.137.68] by server-4.bemta-3.messagelabs.com id 04/8C-18304-8C205875; Tue, 12 Jul 2016 14:46:32 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFprEIsWRWlGSWpSXmKPExsXitHSDve5RptZ wg+k39CyWfFzM4sDocXT3b6YAxijWzLyk/IoE1oyNrTuZCj4pVsx8/oqpgbFBsouRk0NCwF/i 3JcljCA2m4CBxIrpV8FsEQFZidVdc9i7GLk4mAWOMErcuvkTLCEM1PB1zhxmEJtFQFVi8botT CA2r4CtxNKuJawQQ+UkGs7fB6vhBIq3tT4Fs4UEbCTWLp3FCmGrSdxYuIwFoldQ4uTMJ2A2s4 CExMEXL5gh5nBL3D49lXkCI98sJGWzkJQtYGRaxahRnFpUllqka2Sil1SUmZ5RkpuYmaNraGC sl5taXJyYnpqTmFSsl5yfu4kRGFb1DAyMOxhfHfc7xCjJwaQkytvM3BIuxJeUn1KZkVicEV9U mpNafIhRhoNDSYLXj7E1XEiwKDU9tSItMwcY4DBpCQ4eJRFee5A0b3FBYm5xZjpE6hSjopQ4r yZIQgAkkVGaB9cGi6pLjLJSwryMDAwMQjwFqUW5mSWo8q8YxTkYlYR5p4NM4cnMK4Gb/gpoMR PQ4lqHZpDFJYkIKakGRtfE+2pm0SZ/V4nI39my0pHvr9+h4+L/1gZnJ6g5irbHRy2T/+que+q SNMey4IIbcX5zeN8Y6V/1Obze30iNp+DvzCURbAcXXO18/bNzg2Mhp7r6O8k5/htTytI07swr XNnF+1burf7bTMGFMcWR614fK7n1/Z5l7lfza4tsgwzFa+YfUv6Vp8RSnJFoqMVcVJwIADWqD jClAgAA X-Env-Sender: prvs=994f31c19=anthony.perard@citrix.com X-Msg-Ref: server-16.tower-31.messagelabs.com!1468334787!42528891!2 X-Originating-IP: [66.165.176.63] X-SpamReason: No, hits=0.0 required=7.0 tests=sa_preprocessor: VHJ1c3RlZCBJUDogNjYuMTY1LjE3Ni42MyA9PiAzMDYwNDg=\n, received_headers: No Received headers X-StarScan-Received: X-StarScan-Version: 8.46; banners=-,-,- X-VirusChecked: Checked Received: (qmail 7353 invoked from network); 12 Jul 2016 14:46:29 -0000 Received: from smtp02.citrix.com (HELO SMTP02.CITRIX.COM) (66.165.176.63) by server-16.tower-31.messagelabs.com with RC4-SHA encrypted SMTP; 12 Jul 2016 14:46:29 -0000 X-IronPort-AV: E=Sophos;i="5.28,352,1464652800"; d="scan'208";a="372679191" From: Anthony PERARD To: Date: Tue, 12 Jul 2016 15:42:46 +0100 Message-ID: <20160712144251.558-10-anthony.perard@citrix.com> X-Mailer: git-send-email 2.9.0 In-Reply-To: <20160712144251.558-1-anthony.perard@citrix.com> References: <20160712144251.558-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 v6 09/14] 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)() ) {