From patchwork Mon Nov 12 11:57:29 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Will Deacon X-Patchwork-Id: 1727991 Return-Path: X-Original-To: patchwork-kvm@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork2.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork2.kernel.org (Postfix) with ESMTP id 16EFDDFE80 for ; Mon, 12 Nov 2012 11:58:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753014Ab2KLL6F (ORCPT ); Mon, 12 Nov 2012 06:58:05 -0500 Received: from cam-admin0.cambridge.arm.com ([217.140.96.50]:55173 "EHLO cam-admin0.cambridge.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752864Ab2KLL5k (ORCPT ); Mon, 12 Nov 2012 06:57:40 -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 qACBvaE7025868; Mon, 12 Nov 2012 11:57:36 GMT Received: by mudshark.cambridge.arm.com (Postfix, from userid 1000) id 69C81C2A98; Mon, 12 Nov 2012 11:57:34 +0000 (GMT) From: Will Deacon To: kvm@vger.kernel.org Cc: penberg@kernel.org, marc.zyngier@arm.com, c.dall@virtualopensystems.com, matt.evans@arm.com, peter.maydell@linaro.org, Will Deacon Subject: [RFC PATCH 15/16] kvm tools: provide a mechanism for translating host to guest addresses Date: Mon, 12 Nov 2012 11:57:29 +0000 Message-Id: <1352721450-11340-16-git-send-email-will.deacon@arm.com> X-Mailer: git-send-email 1.7.4.1 In-Reply-To: <1352721450-11340-1-git-send-email-will.deacon@arm.com> References: <1352721450-11340-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(+), 0 deletions(-) diff --git a/tools/kvm/include/kvm/kvm.h b/tools/kvm/include/kvm/kvm.h index ce09142..3ebd229 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, unsigned long offset); +unsigned long 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 1702533..b876906 100644 --- a/tools/kvm/kvm.c +++ b/tools/kvm/kvm.c @@ -200,6 +200,22 @@ void *guest_flat_to_host(struct kvm *kvm, unsigned long offset) return NULL; } +unsigned long 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;