From patchwork Mon Jan 14 16:35:09 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoffer Dall X-Patchwork-Id: 1972771 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork2.kernel.org Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) by patchwork2.kernel.org (Postfix) with ESMTP id EF6FBDF2E1 for ; Mon, 14 Jan 2013 16:38:42 +0000 (UTC) Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.76 #1 (Red Hat Linux)) id 1Tun0F-0006Yx-MI; Mon, 14 Jan 2013 16:35:15 +0000 Received: from mail-vb0-f54.google.com ([209.85.212.54]) by merlin.infradead.org with esmtps (Exim 4.76 #1 (Red Hat Linux)) id 1Tun0A-0006YE-NC for linux-arm-kernel@lists.infradead.org; Mon, 14 Jan 2013 16:35:11 +0000 Received: by mail-vb0-f54.google.com with SMTP id l1so3735689vba.27 for ; Mon, 14 Jan 2013 08:35:09 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=mime-version:x-originating-ip:in-reply-to:references:date :message-id:subject:from:to:cc:content-type:x-gm-message-state; bh=i9xfBP8phNE/Er0ykcASu6JPt2Op5MOmIsq/bdEamWw=; b=fqYOw0rs/WzSt3zmmhi4AQwIX8cvkuQn0lVw2t8GEklK1tueqDp2LC75FllpzUjGkV EecDmHwurlo/fZSQpj52218OeTzixHArTg+RFogKKl73sQrwYeefCWaWDtCNkstvqO8q j1Fm6k3g7mvs58mzVijraD5j7lgN4x8XTifHxsB9XTM4K05VSVTA3TohkdoHqmx+AatU SxV+Ak8DBhr7Bo5CtOeudLAbwnl/wwwcq7/tWYYZ9N8Y34S/zvZ7L98pTSOLl0FZ/mmd t6Nm+bHMKRWzdzb6MkvzFEnU8VSu1/X7kOEMKWfoR+DYutoENN5DTCvzhICi6WLwi9ig m7og== MIME-Version: 1.0 Received: by 10.58.229.197 with SMTP id ss5mr18355104vec.14.1358181309498; Mon, 14 Jan 2013 08:35:09 -0800 (PST) Received: by 10.221.7.71 with HTTP; Mon, 14 Jan 2013 08:35:09 -0800 (PST) X-Originating-IP: [72.80.83.148] In-Reply-To: <20130114151106.GB18935@mudshark.cambridge.arm.com> References: <20130108183811.46302.58543.stgit@ubuntu> <20130108183903.46302.97512.stgit@ubuntu> <20130114151106.GB18935@mudshark.cambridge.arm.com> Date: Mon, 14 Jan 2013 11:35:09 -0500 Message-ID: Subject: Re: [PATCH v5 04/14] KVM: ARM: Hypervisor initialization From: Christoffer Dall To: Will Deacon X-Gm-Message-State: ALoCoQkr2Ey8J6DQcGZGSEdB2D13LUcteVxo78v6FdH+uKGdACQbFUpSKcaHfQKwT2Tzjyqpu0iE X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20130114_113510_891345_D7DDC698 X-CRM114-Status: GOOD ( 19.75 ) X-Spam-Score: -2.6 (--) X-Spam-Report: SpamAssassin version 3.3.2 on merlin.infradead.org summary: Content analysis details: (-2.6 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at http://www.dnswl.org/, low trust [209.85.212.54 listed in list.dnswl.org] -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] Cc: Marc Zyngier , Marcelo Tosatti , "linux-arm-kernel@lists.infradead.org" , "kvm@vger.kernel.org" , "kvmarm@lists.cs.columbia.edu" X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: linux-arm-kernel-bounces@lists.infradead.org Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org On Mon, Jan 14, 2013 at 10:11 AM, Will Deacon wrote: > On Tue, Jan 08, 2013 at 06:39:03PM +0000, Christoffer Dall wrote: >> Sets up KVM code to handle all exceptions taken to Hyp mode. >> >> When the kernel is booted in Hyp mode, calling an hvc instruction with r0 >> pointing to the new vectors, the HVBAR is changed to the the vector pointers. >> This allows subsystems (like KVM here) to execute code in Hyp-mode with the >> MMU disabled. >> >> We initialize other Hyp-mode registers and enables the MMU for Hyp-mode from >> the id-mapped hyp initialization code. Afterwards, the HVBAR is changed to >> point to KVM Hyp vectors used to catch guest faults and to switch to Hyp mode >> to perform a world-switch into a KVM guest. >> >> Also provides memory mapping code to map required code pages, data structures, >> and I/O regions accessed in Hyp mode at the same virtual address as the host >> kernel virtual addresses, but which conforms to the architectural requirements >> for translations in Hyp mode. This interface is added in arch/arm/kvm/arm_mmu.c >> and comprises: >> - create_hyp_mappings(from, to); >> - create_hyp_io_mappings(from, to, phys_addr); >> - free_hyp_pmds(); > > [...] > >> diff --git a/arch/arm/kvm/arm.c b/arch/arm/kvm/arm.c >> index 82cb338..2dddc58 100644 >> --- a/arch/arm/kvm/arm.c >> +++ b/arch/arm/kvm/arm.c >> @@ -34,11 +34,21 @@ >> #include >> #include >> #include >> +#include >> +#include >> +#include >> +#include >> +#include >> >> #ifdef REQUIRES_VIRT >> __asm__(".arch_extension virt"); >> #endif >> >> +static DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_page); >> +static struct vfp_hard_struct __percpu *kvm_host_vfp_state; >> +static unsigned long hyp_default_vectors; >> + >> + >> int kvm_arch_hardware_enable(void *garbage) >> { >> return 0; >> @@ -336,9 +346,176 @@ long kvm_arch_vm_ioctl(struct file *filp, >> return -EINVAL; >> } >> >> +static void cpu_init_hyp_mode(void *vector) >> +{ >> + unsigned long long pgd_ptr; >> + unsigned long hyp_stack_ptr; >> + unsigned long stack_page; >> + unsigned long vector_ptr; >> + >> + /* Switch from the HYP stub to our own HYP init vector */ >> + __hyp_set_vectors((unsigned long)vector); >> + >> + pgd_ptr = (unsigned long long)kvm_mmu_get_httbr(); >> + stack_page = __get_cpu_var(kvm_arm_hyp_stack_page); >> + hyp_stack_ptr = stack_page + PAGE_SIZE; >> + vector_ptr = (unsigned long)__kvm_hyp_vector; >> + >> + /* >> + * Call initialization code, and switch to the full blown >> + * HYP code. The init code corrupts r12, so set the clobber >> + * list accordingly. >> + */ >> + asm volatile ( >> + "mov r0, %[pgd_ptr_low]\n\t" >> + "mov r1, %[pgd_ptr_high]\n\t" >> + "mov r2, %[hyp_stack_ptr]\n\t" >> + "mov r3, %[vector_ptr]\n\t" >> + "hvc #0\n\t" : : >> + [pgd_ptr_low] "r" ((unsigned long)(pgd_ptr & 0xffffffff)), >> + [pgd_ptr_high] "r" ((unsigned long)(pgd_ptr >> 32ULL)), >> + [hyp_stack_ptr] "r" (hyp_stack_ptr), >> + [vector_ptr] "r" (vector_ptr) : >> + "r0", "r1", "r2", "r3", "r12"); >> +} > > Use kvm_call_hyp here instead. > good idea: commit 00e22196205800ce9caa561e7c806023f4915138 Author: Christoffer Dall Date: Mon Jan 14 11:32:36 2013 -0500 KVM: ARM: Reuse kvm_call_hyp in vcpu_init_hyp_mode Instead of directly and manually callin the hypercall into the KVM init code, use the kvm_call_hyp function, which only requires a small abuse of the prototype in exchange for much nicer C code. Cc: Will Deacon Signed-off-by: Christoffer Dall --- Thanks, -Christoffer diff --git a/arch/arm/kvm/arm.c b/arch/arm/kvm/arm.c index 6997326..b5c6ab1 100644 --- a/arch/arm/kvm/arm.c +++ b/arch/arm/kvm/arm.c @@ -971,6 +971,7 @@ long kvm_arch_vm_ioctl(struct file *filp, static void cpu_init_hyp_mode(void *vector) { unsigned long long pgd_ptr; + unsigned long pgd_low, pgd_high; unsigned long hyp_stack_ptr; unsigned long stack_page; unsigned long vector_ptr; @@ -979,26 +980,20 @@ static void cpu_init_hyp_mode(void *vector) __hyp_set_vectors((unsigned long)vector); pgd_ptr = (unsigned long long)kvm_mmu_get_httbr(); + pgd_low = (pgd_ptr & ((1ULL << 32) - 1)); + pgd_high = (pgd_ptr >> 32ULL); stack_page = __get_cpu_var(kvm_arm_hyp_stack_page); hyp_stack_ptr = stack_page + PAGE_SIZE; vector_ptr = (unsigned long)__kvm_hyp_vector; /* * Call initialization code, and switch to the full blown - * HYP code. The init code corrupts r12, so set the clobber - * list accordingly. + * HYP code. The init code doesn't need to preserve these registers as + * r1-r3 and r12 are already callee save according to the AAPCS. + * Note that we slightly misuse the prototype by casing the pgd_low to + * a void *. */ - asm volatile ( - "mov r0, %[pgd_ptr_low]\n\t" - "mov r1, %[pgd_ptr_high]\n\t" - "mov r2, %[hyp_stack_ptr]\n\t" - "mov r3, %[vector_ptr]\n\t" - "hvc #0\n\t" : : - [pgd_ptr_low] "r" ((unsigned long)(pgd_ptr & 0xffffffff)), - [pgd_ptr_high] "r" ((unsigned long)(pgd_ptr >> 32ULL)), - [hyp_stack_ptr] "r" (hyp_stack_ptr), - [vector_ptr] "r" (vector_ptr) : - "r0", "r1", "r2", "r3", "r12"); + kvm_call_hyp((void *)pgd_low, pgd_high, hyp_stack_ptr, vector_ptr); } /**