From patchwork Thu Nov 22 15:58:16 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Will Deacon X-Patchwork-Id: 1792791 Return-Path: X-Original-To: patchwork-kvm@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork1.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork1.kernel.org (Postfix) with ESMTP id EA7453FD1A for ; Thu, 22 Nov 2012 23:01:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752989Ab2KVXBq (ORCPT ); Thu, 22 Nov 2012 18:01:46 -0500 Received: from cam-admin0.cambridge.arm.com ([217.140.96.50]:52264 "EHLO cam-admin0.cambridge.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752760Ab2KVSaj (ORCPT ); Thu, 22 Nov 2012 13:30:39 -0500 Received: from mudshark.cambridge.arm.com (mudshark.cambridge.arm.com [10.1.79.58]) by cam-admin0.cambridge.arm.com (8.12.6/8.12.6) with ESMTP id qAMFwK53000655; Thu, 22 Nov 2012 15:58:20 GMT Received: by mudshark.cambridge.arm.com (Postfix, from userid 1000) id 5B26BC2B16; Thu, 22 Nov 2012 15:58:19 +0000 (GMT) From: Will Deacon To: kvm@vger.kernel.org Cc: penberg@kernel.org, marc.zyngier@arm.com, c.dall@virtualopensystems.com, matt@ozlabs.org, peter.maydell@linaro.org, michael@ellerman.id.au, levinsasha928@gmail.com, kvmarm@lists.cs.columbia.edu, Will Deacon Subject: [PATCH v2 7/8] kvm tools: provide a mechanism for translating host to guest addresses Date: Thu, 22 Nov 2012 15:58:16 +0000 Message-Id: <1353599897-15656-8-git-send-email-will.deacon@arm.com> X-Mailer: git-send-email 1.8.0 In-Reply-To: <1353599897-15656-1-git-send-email-will.deacon@arm.com> References: <1353599897-15656-1-git-send-email-will.deacon@arm.com> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org When generating a device tree for a guest, it is useful to have a helper for converting host addresses to guest addresses in order to populate the device nodes correctly. This patch adds such a helper, following a similar implementation to the reverse translation function that already exists. Signed-off-by: Will Deacon --- tools/kvm/include/kvm/kvm.h | 1 + tools/kvm/kvm.c | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/tools/kvm/include/kvm/kvm.h b/tools/kvm/include/kvm/kvm.h index 5fb2fb2..b54ac03 100644 --- a/tools/kvm/include/kvm/kvm.h +++ b/tools/kvm/include/kvm/kvm.h @@ -106,6 +106,7 @@ bool kvm__arch_cpu_supports_vm(void); void kvm__arch_periodic_poll(struct kvm *kvm); void *guest_flat_to_host(struct kvm *kvm, u64 offset); +u64 host_to_guest_flat(struct kvm *kvm, void *ptr); int load_flat_binary(struct kvm *kvm, int fd_kernel, int fd_initrd, const char *kernel_cmdline); bool load_bzimage(struct kvm *kvm, int fd_kernel, int fd_initrd, const char *kernel_cmdline, u16 vidmode); diff --git a/tools/kvm/kvm.c b/tools/kvm/kvm.c index a7e2628..af19e37 100644 --- a/tools/kvm/kvm.c +++ b/tools/kvm/kvm.c @@ -201,6 +201,22 @@ void *guest_flat_to_host(struct kvm *kvm, u64 offset) return NULL; } +u64 host_to_guest_flat(struct kvm *kvm, void *ptr) +{ + struct kvm_mem_bank *bank; + + list_for_each_entry(bank, &kvm->mem_banks, list) { + void *bank_start = bank->host_addr; + void *bank_end = bank_start + bank->size; + + if (ptr >= bank_start && ptr < bank_end) + return bank->guest_phys_addr + (ptr - bank_start); + } + + pr_warning("unable to translate host address %p to guest", ptr); + return 0; +} + int kvm__recommended_cpus(struct kvm *kvm) { int ret;