From patchwork Thu Dec 20 15:21:26 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Julien Grall X-Patchwork-Id: 10739065 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id C825913BF for ; Thu, 20 Dec 2018 15:21:54 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B917328AC2 for ; Thu, 20 Dec 2018 15:21:54 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B788F28CE5; Thu, 20 Dec 2018 15:21:54 +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=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1D8F028D07 for ; Thu, 20 Dec 2018 15:21:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387843AbeLTPVw (ORCPT ); Thu, 20 Dec 2018 10:21:52 -0500 Received: from usa-sjc-mx-foss1.foss.arm.com ([217.140.101.70]:57876 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387823AbeLTPVv (ORCPT ); Thu, 20 Dec 2018 10:21:51 -0500 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id AC20E15AD; Thu, 20 Dec 2018 07:21:50 -0800 (PST) Received: from e108454-lin.cambridge.arm.com (e108454-lin.cambridge.arm.com [10.1.196.50]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id BCA053F5C0; Thu, 20 Dec 2018 07:21:49 -0800 (PST) From: Julien Grall To: kvm@vger.kernel.org Cc: suzuki.poulose@arm.com, marc.zyngier@arm.com, will.deacon@arm.com Subject: [PATCH kvmtool v3 9/9] kvmtool: Allow standard size specifiers for memory bank Date: Thu, 20 Dec 2018 15:21:26 +0000 Message-Id: <20181220152126.18741-10-julien.grall@arm.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20181220152126.18741-1-julien.grall@arm.com> References: <20181220152126.18741-1-julien.grall@arm.com> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Suzuki K Poulose Allow standard suffixes, K, M, G, T & P suffixes for sizes and addresses for memory bank parameters. By default, the size is specified in MB. Signed-off-by: Suzuki K Poulose --- Changes in v2: - Patch added --- builtin-run.c | 50 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 42 insertions(+), 8 deletions(-) diff --git a/builtin-run.c b/builtin-run.c index 568af5c..c907295 100644 --- a/builtin-run.c +++ b/builtin-run.c @@ -49,9 +49,11 @@ #include #include -#define MB_SHIFT (20) #define KB_SHIFT (10) +#define MB_SHIFT (20) #define GB_SHIFT (30) +#define TB_SHIFT (40) +#define PB_SHIFT (50) __thread struct kvm_cpu *current_kvm_cpu; @@ -87,6 +89,37 @@ void kvm_run_set_wrapper_sandbox(void) kvm_run_wrapper = KVM_RUN_SANDBOX; } +static int __parse_size_spec(char **next) +{ + int shift = 0; + + switch(**next) { + case 'K': shift = KB_SHIFT; break; + case 'M': shift = MB_SHIFT; break; + case 'G': shift = GB_SHIFT; break; + case 'T': shift = TB_SHIFT; break; + case 'P': shift = PB_SHIFT; break; + } + if (shift) + (*next)++; + return shift; +} + +static u64 parse_mem_size_spec(char **next) +{ + int shift = __parse_size_spec(next); + + /* By default the size is in MB, if none is specified */ + if (!shift) + shift = 20; + return ((u64)1) << shift; +} + +static u64 parse_addr_spec(char **next) +{ + return ((u64)1) << __parse_size_spec(next); +} + static int mem_parser(const struct option *opt, const char *arg, int unset) { struct kvm_config *cfg = opt->value; @@ -101,6 +134,8 @@ static int mem_parser(const struct option *opt, const char *arg, int unset) if (next == p) die("mem: no size specified.\n"); + size *= parse_mem_size_spec(&next); + if (*next != '@' && *next != '\0') die("mem: unexpected chars after size.\n"); @@ -122,6 +157,8 @@ static int mem_parser(const struct option *opt, const char *arg, int unset) if (cfg->nr_ram > 0) die("mem: must be specified\n"); addr = INVALID_RAM_ADDR; + } else { + addr *= parse_addr_spec(&next); } if ( cfg->nr_ram == MAX_RAM_BANKS ) @@ -322,7 +359,7 @@ static u64 host_ram_size(void) return 0; } - return (nr_pages * page_size) >> MB_SHIFT; + return (nr_pages * page_size); } /* @@ -336,11 +373,11 @@ static u64 get_ram_size(int nr_cpus) u64 available; u64 ram_size; - ram_size = 64 * (nr_cpus + 3); + ram_size = (64 * (nr_cpus + 3)) << MB_SHIFT; available = host_ram_size() * RAM_SIZE_RATIO; if (!available) - available = MIN_RAM_SIZE_MB; + available = MIN_RAM_SIZE_MB << MB_SHIFT; if (ram_size > available) ram_size = available; @@ -594,11 +631,8 @@ static struct kvm *kvm_cmd_run_init(int argc, const char **argv) kvm->cfg.nr_ram = 1; } - for (i = 0; i < kvm->cfg.nr_ram; i++) { + for (i = 0; i < kvm->cfg.nr_ram; i++) total_ram += kvm->cfg.ram[i].size; - /* The user input is in MB, but the internal deals with bytes */ - kvm->cfg.ram[i].size <<= MB_SHIFT; - } if (total_ram > host_ram_size()) pr_warning("Guest memory size %lluMB exceeds host physical RAM size %lluMB",