From patchwork Thu Mar 25 18:07:27 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcelo Tosatti X-Patchwork-Id: 88300 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter.kernel.org (8.14.3/8.14.3) with ESMTP id o2PI8cOL000519 for ; Thu, 25 Mar 2010 18:08:38 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753768Ab0CYSIg (ORCPT ); Thu, 25 Mar 2010 14:08:36 -0400 Received: from mx1.redhat.com ([209.132.183.28]:2609 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753554Ab0CYSIf (ORCPT ); Thu, 25 Mar 2010 14:08:35 -0400 Received: from int-mx03.intmail.prod.int.phx2.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.16]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id o2PI8Ydr016392 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Thu, 25 Mar 2010 14:08:35 -0400 Received: from ns3.rdu.redhat.com (ns3.rdu.redhat.com [10.11.255.199]) by int-mx03.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id o2PI8YSC000305; Thu, 25 Mar 2010 14:08:34 -0400 Received: from amt.cnet (vpn-11-249.rdu.redhat.com [10.11.11.249]) by ns3.rdu.redhat.com (8.13.8/8.13.8) with ESMTP id o2PI8Wq2000429; Thu, 25 Mar 2010 14:08:33 -0400 Received: from amt.cnet (amt.cnet [127.0.0.1]) by amt.cnet (Postfix) with ESMTP id C28F568AC6B; Thu, 25 Mar 2010 15:07:28 -0300 (BRT) Received: (from marcelo@localhost) by amt.cnet (8.14.3/8.14.3/Submit) id o2PI7RkC006022; Thu, 25 Mar 2010 15:07:27 -0300 Date: Thu, 25 Mar 2010 15:07:27 -0300 From: Marcelo Tosatti To: Avi Kivity Cc: kvm@vger.kernel.org Subject: Re: [patch 1/8] test: allow functions to execute on non-irq context remotely Message-ID: <20100325180727.GA23070@amt.cnet> References: <20100324212408.790319364@amt.cnet> <20100324212725.523800252@amt.cnet> <4BAB8E94.5090902@redhat.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <4BAB8E94.5090902@redhat.com> User-Agent: Mutt/1.5.20 (2009-08-17) X-Scanned-By: MIMEDefang 2.67 on 10.5.11.16 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.3 (demeter.kernel.org [140.211.167.41]); Thu, 25 Mar 2010 18:08:38 +0000 (UTC) Index: qemu-kvm/kvm/user/test/lib/x86/smp.c =================================================================== --- qemu-kvm.orig/kvm/user/test/lib/x86/smp.c +++ qemu-kvm/kvm/user/test/lib/x86/smp.c @@ -114,7 +114,7 @@ void on_cpu_async(int cpu, void (*functi } -void smp_init(void) +void smp_init_ids(void) { int i; void ipi_entry(void); @@ -125,4 +125,70 @@ void smp_init(void) for (i = 1; i < cpu_count(); ++i) on_cpu(i, setup_smp_id, 0); + printf("detected %d cpus\n", cpu_count()); +} + +static void *smp_function(void) +{ + void *fn; + + asm ("mov %%gs:8, %0" : "=r"(fn)); + return fn; +} + +static void setup_smp_function(void *data) +{ + asm ("mov %0, %%gs:8" : : "r"(data) : "memory"); +} + +static void *smp_data(void) +{ + void *fn; + + asm ("mov %%gs:16, %0" : "=r"(fn)); + return fn; +} + +static void setup_smp_data(void *data) +{ + asm ("mov %0, %%gs:16" : : "r"(data) : "memory"); +} + +void on_cpu_noipi(int cpu, void (*function)(void *data), void *data) +{ + if (cpu == smp_id()) + function(data); + else { + on_cpu(cpu, setup_smp_data, data); + on_cpu(cpu, setup_smp_function, function); + } +} + +static void irq_disable(void) +{ + asm volatile("cli"); +} + +static void irq_enable(void) +{ + asm volatile("sti"); +} + +void smp_loop(void) +{ + void (*fn)(void *data); + + irq_disable(); + fn = smp_function(); + if (fn) { + setup_smp_function(0); + irq_enable(); + fn(smp_data()); + irq_disable(); + } + + irq_enable(); + asm volatile ("hlt"); + irq_disable(); + } Index: qemu-kvm/kvm/user/test/lib/x86/smp.h =================================================================== --- qemu-kvm.orig/kvm/user/test/lib/x86/smp.h +++ qemu-kvm/kvm/user/test/lib/x86/smp.h @@ -5,12 +5,11 @@ struct spinlock { int v; }; -void smp_init(void); - int cpu_count(void); int smp_id(void); void on_cpu(int cpu, void (*function)(void *data), void *data); void on_cpu_async(int cpu, void (*function)(void *data), void *data); +void on_cpu_noipi(int cpu, void (*function)(void *data), void *data); void spin_lock(struct spinlock *lock); void spin_unlock(struct spinlock *lock); Index: qemu-kvm/kvm/user/test/x86/cstart64.S =================================================================== --- qemu-kvm.orig/kvm/user/test/x86/cstart64.S +++ qemu-kvm/kvm/user/test/x86/cstart64.S @@ -165,7 +165,7 @@ ap_start64: nop lock incw cpu_online_count -1: hlt +1: call smp_loop jmp 1b start64: @@ -174,6 +174,7 @@ start64: call enable_apic call smp_init call enable_x2apic + call smp_init_ids call main mov %eax, %edi call exit Index: qemu-kvm/kvm/user/test/x86/smptest.c =================================================================== --- qemu-kvm.orig/kvm/user/test/x86/smptest.c +++ qemu-kvm/kvm/user/test/x86/smptest.c @@ -15,8 +15,6 @@ int main() int ncpus; int i; - smp_init(); - ncpus = cpu_count(); printf("found %d cpus\n", ncpus); for (i = 0; i < ncpus; ++i) Index: qemu-kvm/kvm/user/test/x86/vmexit.c =================================================================== --- qemu-kvm.orig/kvm/user/test/x86/vmexit.c +++ qemu-kvm/kvm/user/test/x86/vmexit.c @@ -155,8 +155,6 @@ int main(void) { int i; - smp_init(); - for (i = 0; i < ARRAY_SIZE(tests); ++i) do_test(&tests[i]);