From patchwork Sun Oct 28 23:48:46 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: pingfank@linux.vnet.ibm.com X-Patchwork-Id: 194767 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 38ED12C008F for ; Mon, 29 Oct 2012 11:38:37 +1100 (EST) Received: from localhost ([::1]:37834 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TScc7-0007Qg-5e for incoming@patchwork.ozlabs.org; Sun, 28 Oct 2012 19:49:55 -0400 Received: from eggs.gnu.org ([208.118.235.92]:52712) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TScbN-0005hk-Qh for qemu-devel@nongnu.org; Sun, 28 Oct 2012 19:49:10 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TScbM-0003tE-FS for qemu-devel@nongnu.org; Sun, 28 Oct 2012 19:49:09 -0400 Received: from e28smtp03.in.ibm.com ([122.248.162.3]:52430) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TScbL-0003qu-GB for qemu-devel@nongnu.org; Sun, 28 Oct 2012 19:49:08 -0400 Received: from /spool/local by e28smtp03.in.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 29 Oct 2012 05:19:05 +0530 Received: from d28relay02.in.ibm.com (9.184.220.59) by e28smtp03.in.ibm.com (192.168.1.133) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Mon, 29 Oct 2012 05:19:04 +0530 Received: from d28av01.in.ibm.com (d28av01.in.ibm.com [9.184.220.63]) by d28relay02.in.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id q9SNn3ns31981786 for ; Mon, 29 Oct 2012 05:19:03 +0530 Received: from d28av01.in.ibm.com (loopback [127.0.0.1]) by d28av01.in.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id q9T5ItAf008728 for ; Mon, 29 Oct 2012 05:18:56 GMT Received: from oc8440477808.ibm.com ([9.125.25.249]) by d28av01.in.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id q9T5IebO008036; Mon, 29 Oct 2012 05:18:53 GMT From: Liu Ping Fan To: qemu-devel@nongnu.org Date: Mon, 29 Oct 2012 07:48:46 +0800 Message-Id: <1351468127-15025-8-git-send-email-pingfank@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.4.4 In-Reply-To: <1351468127-15025-1-git-send-email-pingfank@linux.vnet.ibm.com> References: <1351468127-15025-1-git-send-email-pingfank@linux.vnet.ibm.com> x-cbid: 12102823-3864-0000-0000-00000544F42F X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 122.248.162.3 Cc: Peter Maydell , Stefan Hajnoczi , Marcelo Tosatti , Avi Kivity , Anthony Liguori , Jan Kiszka , Paolo Bonzini Subject: [Qemu-devel] [patch v5 7/8] memory: introduce tls context to record nested dma X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Signed-off-by: Liu Ping Fan --- cpus.c | 3 ++ exec.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ qemu-thread.h | 8 +++++++ vl.c | 1 + 4 files changed, 70 insertions(+), 0 deletions(-) diff --git a/cpus.c b/cpus.c index 191cbf5..e67d80f 100644 --- a/cpus.c +++ b/cpus.c @@ -733,6 +733,7 @@ static void *qemu_kvm_cpu_thread_fn(void *arg) qemu_mutex_lock(&qemu_global_mutex); qemu_thread_get_self(cpu->thread); + qemu_thread_init_context(); env->thread_id = qemu_get_thread_id(); cpu_single_env = env; @@ -774,6 +775,7 @@ static void *qemu_dummy_cpu_thread_fn(void *arg) qemu_mutex_lock_iothread(); qemu_thread_get_self(cpu->thread); + qemu_thread_init_context(); env->thread_id = qemu_get_thread_id(); sigemptyset(&waitset); @@ -813,6 +815,7 @@ static void *qemu_tcg_cpu_thread_fn(void *arg) qemu_tcg_init_cpu_signals(); qemu_thread_get_self(cpu->thread); + qemu_thread_init_context(); /* signal CPU creation */ qemu_mutex_lock(&qemu_global_mutex); diff --git a/exec.c b/exec.c index 46da08c..ea672c6 100644 --- a/exec.c +++ b/exec.c @@ -3449,6 +3449,49 @@ static bool address_space_section_lookup_ref(AddressSpace *as, return safe_ref; } +typedef struct ThreadContext { + DispatchType dispatch_type; + unsigned int mmio_req_pending; +} ThreadContext; + +static __thread ThreadContext *thread_context; + +void qemu_thread_init_context(void) +{ + thread_context = g_new(ThreadContext, 1); + thread_context->dispatch_type = DISPATCH_INIT; + thread_context->mmio_req_pending = 0; +} + +void qemu_thread_set_dispatch_type(DispatchType type) +{ + thread_context->dispatch_type = type; +} + +void qemu_thread_reset_dispatch_type(void) +{ + thread_context->dispatch_type = DISPATCH_INIT; +} + +static bool address_space_inc_req_pending(void) +{ + bool nested = false; + + /* currently, only mmio out of big lock, and need this to avoid dead lock */ + if (thread_context->dispatch_type == DISPATCH_MMIO) { + nested = ++thread_context->mmio_req_pending > 1 ? true : false; + } + + return nested; +} + +static void address_space_dec_req_pending(void) +{ + if (thread_context->dispatch_type == DISPATCH_MMIO) { + thread_context->mmio_req_pending--; + } +} + void address_space_rw(AddressSpace *as, target_phys_addr_t addr, uint8_t *buf, int len, bool is_write) { @@ -3459,6 +3502,7 @@ void address_space_rw(AddressSpace *as, target_phys_addr_t addr, uint8_t *buf, target_phys_addr_t page; bool safe_ref = false; MemoryRegionSection *section, obj_mrs; + bool nested_dma = false; while (len > 0) { page = addr & TARGET_PAGE_MASK; @@ -3485,10 +3529,17 @@ void address_space_rw(AddressSpace *as, target_phys_addr_t addr, uint8_t *buf, memory_region_section_lookup_ref(d, page, &obj_mrs); } section = &obj_mrs; + nested_dma = address_space_inc_req_pending(); if (is_write) { if (!memory_region_is_ram(section->mr)) { target_phys_addr_t addr1; + + /* To fix, will filter iommu case */ + if (nested_dma) { + fprintf(stderr, "can not support nested DMA"); + abort(); + } addr1 = memory_region_section_addr(section, addr); /* XXX: could force cpu_single_env to NULL to avoid potential bugs */ @@ -3522,6 +3573,12 @@ void address_space_rw(AddressSpace *as, target_phys_addr_t addr, uint8_t *buf, if (!(memory_region_is_ram(section->mr) || memory_region_is_romd(section->mr))) { target_phys_addr_t addr1; + + /* To fix, will filter iommu case */ + if (nested_dma) { + fprintf(stderr, "can not support nested DMA"); + abort(); + } /* I/O case */ addr1 = memory_region_section_addr(section, addr); if (l >= 4 && ((addr1 & 3) == 0)) { @@ -3549,6 +3606,7 @@ void address_space_rw(AddressSpace *as, target_phys_addr_t addr, uint8_t *buf, qemu_put_ram_ptr(ptr); } } + address_space_dec_req_pending(); memory_region_section_unref(&obj_mrs); len -= l; buf += l; diff --git a/qemu-thread.h b/qemu-thread.h index 05fdaaf..bb9535e 100644 --- a/qemu-thread.h +++ b/qemu-thread.h @@ -7,6 +7,11 @@ typedef struct QemuMutex QemuMutex; typedef struct QemuCond QemuCond; typedef struct QemuThread QemuThread; +typedef enum { + DISPATCH_INIT = 0, + DISPATCH_MMIO, + DISPATCH_IO, +} DispatchType; #ifdef _WIN32 #include "qemu-thread-win32.h" @@ -46,4 +51,7 @@ void qemu_thread_get_self(QemuThread *thread); bool qemu_thread_is_self(QemuThread *thread); void qemu_thread_exit(void *retval); +void qemu_thread_init_context(void); +void qemu_thread_set_dispatch_type(DispatchType type); +void qemu_thread_reset_dispatch_type(void); #endif diff --git a/vl.c b/vl.c index ee3c43a..be8d825 100644 --- a/vl.c +++ b/vl.c @@ -3439,6 +3439,7 @@ int main(int argc, char **argv, char **envp) add_device_config(DEV_VIRTCON, "vc:80Cx24C"); } + qemu_thread_init_context(); socket_init(); if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)