From patchwork Tue Jul 26 03:50:11 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Rolnik X-Patchwork-Id: 9247583 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 80A5A60757 for ; Tue, 26 Jul 2016 03:58:16 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6A5E1205B0 for ; Tue, 26 Jul 2016 03:58:16 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5C927223A4; Tue, 26 Jul 2016 03:58:16 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 5648F205B0 for ; Tue, 26 Jul 2016 03:58:15 +0000 (UTC) Received: from localhost ([::1]:37285 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bRtVW-0001pL-5m for patchwork-qemu-devel@patchwork.kernel.org; Mon, 25 Jul 2016 23:58:14 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50437) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bRtOP-0004ca-Na for qemu-devel@nongnu.org; Mon, 25 Jul 2016 23:50:56 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bRtON-0001Rh-Sr for qemu-devel@nongnu.org; Mon, 25 Jul 2016 23:50:53 -0400 Received: from mail-wm0-x243.google.com ([2a00:1450:400c:c09::243]:36419) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bRtON-0001Rc-I9 for qemu-devel@nongnu.org; Mon, 25 Jul 2016 23:50:51 -0400 Received: by mail-wm0-x243.google.com with SMTP id x83so19371800wma.3 for ; Mon, 25 Jul 2016 20:50:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=7+mL0qnZnBUZ+SGt3sRSKDSfjRYnEPSsizgC4MoXJmo=; b=jABxXzyXjI3/2m2ot93IbscXDOH9rD2K7WeLZy0GuUrf9LHk0c0O+siJvt1XVtgbM+ YHYIM52LYAb3evN13dkjzroXZF1zzyq+j81eQRjvYG1GsO66sqfa+DoviUXkHZDG2CHg KC0zf+eCyKZNym84wRJUCNe6wq1I08kXZhiPen9PQ6gvXYcY5rXO4siCligbKRFov3tA sbMzivUKr3kusxHBS3GdtJtK8DXx2jCS7dAISHSxuu36QLSUZzyAHwFDw5PAeUMuiGGG NRxecrnvGbl5zzJZjt/RjvF6fZ/874K6AkZh5mlBFFnURfPcngxZnp96RdVMLCz5a/R3 orug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=7+mL0qnZnBUZ+SGt3sRSKDSfjRYnEPSsizgC4MoXJmo=; b=bAXQg++Xr1Kw8KDouQrlgdoDButuyh2yCRgB981MY19UX6Cau728aLicHfjvdyOMfO n1J3eTZtVBYL8/K4dCXlxqgR8UVAWduorHtQ4GAguBEZ9ng8DRpHbqWUcOG0XTAta+ar S3IEAY0e2w7mwXT/33pgtqfvZl25JQZrCPQsox+N12ZZvNjt69N2eTBHiTpi2JKhGzt8 97sT8guROILe4RILshe+Wvym+nNtO6fE88QPlbhrT3opuOSBhE88JAc81sSj/c0w6Wb8 XkS4ci7Nj2f4Eb1+iHGXJuZIZC62jde2RTJdfE4iVZ62x/EWYFhD/HhIo8EPX58DN/eQ Lncg== X-Gm-Message-State: AEkoousjb8Zat1lwI5P8DFDsAXp21Wl9q0s3QqhR0yzuJIVRbBQusO6FBbYzSlhn5aGDBg== X-Received: by 10.28.171.214 with SMTP id u205mr5264609wme.97.1469505050774; Mon, 25 Jul 2016 20:50:50 -0700 (PDT) Received: from a0999b0126e1.ant.amazon.com ([31.210.187.232]) by smtp.gmail.com with ESMTPSA id g8sm18860757wjj.48.2016.07.25.20.50.49 (version=TLS1 cipher=AES128-SHA bits=128/128); Mon, 25 Jul 2016 20:50:50 -0700 (PDT) From: Michael Rolnik To: qemu-devel@nongnu.org Date: Tue, 26 Jul 2016 06:50:11 +0300 Message-Id: <1469505014-62480-7-git-send-email-mrolnik@gmail.com> X-Mailer: git-send-email 2.4.9 (Apple Git-60) In-Reply-To: <1469505014-62480-1-git-send-email-mrolnik@gmail.com> References: <1469505014-62480-1-git-send-email-mrolnik@gmail.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:400c:c09::243 Subject: [Qemu-devel] [PATCH v13 6/9] target-avr: adding helpers for IN, OUT, SLEEP, WBR & unsupported instructions X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: peter.maydell@linaro.org, Michael Rolnik , rth@twiddle.net Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP Signed-off-by: Michael Rolnik --- target-avr/cpu.h | 13 ++- target-avr/helper.c | 288 ++++++++++++++++++++++++++++++++++++++++++++++--- target-avr/helper.h | 8 +- target-avr/translate.c | 9 ++ 4 files changed, 300 insertions(+), 18 deletions(-) diff --git a/target-avr/cpu.h b/target-avr/cpu.h index 9ef8c41..85c48f1 100644 --- a/target-avr/cpu.h +++ b/target-avr/cpu.h @@ -146,6 +146,7 @@ struct CPUAVRState { uint32_t sp; /* 16 bits */ uint64_t intsrc; /* interrupt sources */ + bool fullacc;/* CPU/MEM if true MEM only otherwise */ uint32_t features; @@ -188,12 +189,22 @@ int avr_cpu_handle_mmu_fault(CPUState *cpu, vaddr address, int rw, int avr_cpu_memory_rw_debug(CPUState *cs, vaddr address, uint8_t *buf, int len, bool is_write); +enum { + TB_FLAGS_FULL_ACCESS = 1, +}; static inline void cpu_get_tb_cpu_state(CPUAVRState *env, target_ulong *pc, target_ulong *cs_base, uint32_t *pflags) { + uint32_t flags = 0; + *pc = env->pc_w * 2; *cs_base = 0; - *pflags = 0; + + if (env->fullacc) { + flags |= TB_FLAGS_FULL_ACCESS; + } + + *pflags = flags; } static inline int cpu_interrupts_enabled(CPUAVRState *env) diff --git a/target-avr/helper.c b/target-avr/helper.c index 1e5d97d..9635d38 100644 --- a/target-avr/helper.c +++ b/target-avr/helper.c @@ -28,6 +28,7 @@ #include "exec/cpu_ldst.h" #include "qemu/host-utils.h" #include "exec/helper-proto.h" +#include "exec/ioport.h" bool avr_cpu_exec_interrupt(CPUState *cs, int interrupt_request) { @@ -42,14 +43,14 @@ bool avr_cpu_exec_interrupt(CPUState *cs, int interrupt_request) cs->exception_index = EXCP_RESET; cc->do_interrupt(cs); - cs->interrupt_request &= ~CPU_INTERRUPT_RESET; + cs->interrupt_request &= ~CPU_INTERRUPT_RESET; ret = true; } } if (interrupt_request & CPU_INTERRUPT_HARD) { if (cpu_interrupts_enabled(env) && env->intsrc != 0) { - int index = ctz32(env->intsrc); + int index = ctz32(env->intsrc); cs->exception_index = EXCP_INT(index); cc->do_interrupt(cs); @@ -64,13 +65,13 @@ bool avr_cpu_exec_interrupt(CPUState *cs, int interrupt_request) void avr_cpu_do_interrupt(CPUState *cs) { - AVRCPU *cpu = AVR_CPU(cs); - CPUAVRState *env = &cpu->env; + AVRCPU *cpu = AVR_CPU(cs); + CPUAVRState *env = &cpu->env; uint32_t ret = env->pc_w; int vector = 0; int size = avr_feature(env, AVR_FEATURE_JMP_CALL) ? 2 : 1; - int base = 0; /* TODO: where to get it */ + int base = 0; /* TODO: where to get it */ if (cs->exception_index == EXCP_RESET) { vector = 0; @@ -79,18 +80,18 @@ void avr_cpu_do_interrupt(CPUState *cs) } if (avr_feature(env, AVR_FEATURE_3_BYTE_PC)) { - stb_phys(cs->as, env->sp--, (ret & 0x0000ff)); - stb_phys(cs->as, env->sp--, (ret & 0x00ff00) >> 8); - stb_phys(cs->as, env->sp--, (ret & 0xff0000) >> 16); + cpu_stb_data(env, env->sp--, (ret & 0x0000ff)); + cpu_stb_data(env, env->sp--, (ret & 0x00ff00) >> 8); + cpu_stb_data(env, env->sp--, (ret & 0xff0000) >> 16); } else if (avr_feature(env, AVR_FEATURE_2_BYTE_PC)) { - stb_phys(cs->as, env->sp--, (ret & 0x0000ff)); - stb_phys(cs->as, env->sp--, (ret & 0x00ff00) >> 8); + cpu_stb_data(env, env->sp--, (ret & 0x0000ff)); + cpu_stb_data(env, env->sp--, (ret & 0x00ff00) >> 8); } else { - stb_phys(cs->as, env->sp--, (ret & 0x0000ff)); + cpu_stb_data(env, env->sp--, (ret & 0x0000ff)); } env->pc_w = base + vector * size; - env->sregI = 0; /* clear Global Interrupt Flag */ + env->sregI = 0; /* clear Global Interrupt Flag */ cs->exception_index = -1; } @@ -108,7 +109,7 @@ hwaddr avr_cpu_get_phys_page_debug(CPUState *cs, vaddr addr) int avr_cpu_handle_mmu_fault(CPUState *cs, vaddr address, int rw, int mmu_idx) { - /* currently it's assumed that this will never happen */ + /* currently it's assumed that this will never happen */ cs->exception_index = EXCP_DEBUG; cpu_dump_state(cs, stderr, fprintf, 0); return 1; @@ -125,15 +126,55 @@ void tlb_fill(CPUState *cs, target_ulong vaddr, MMUAccessType access_type, vaddr &= TARGET_PAGE_MASK; if (mmu_idx == MMU_CODE_IDX) { - paddr = PHYS_BASE_CODE + vaddr; + paddr = PHYS_BASE_CODE + vaddr - VIRT_BASE_CODE; prot = PAGE_READ | PAGE_EXEC; } else { - paddr = PHYS_BASE_DATA + vaddr; - prot = PAGE_READ | PAGE_WRITE; +#if VIRT_BASE_REGS == 0 + if (vaddr < VIRT_BASE_REGS + AVR_REGS) { +#else + if (VIRT_BASE_REGS <= vaddr && vaddr < VIRT_BASE_REGS + SIZE_REGS) { +#endif + /* + * this is a write into CPU registers, exit and rebuilt this TB + * to use full write + */ + AVRCPU *cpu = AVR_CPU(cs); + CPUAVRState *env = &cpu->env; + env->fullacc = 1; + cpu_loop_exit_restore(cs, retaddr); + } else { + /* + * this is a write into memory. nothing special + */ + paddr = PHYS_BASE_DATA + vaddr - VIRT_BASE_DATA; + prot = PAGE_READ | PAGE_WRITE; + } } tlb_set_page_with_attrs(cs, vaddr, paddr, attrs, prot, mmu_idx, page_size); } +void helper_sleep(CPUAVRState *env) +{ + CPUState *cs = CPU(avr_env_get_cpu(env)); + + cs->exception_index = EXCP_HLT; + cpu_loop_exit(cs); +} +void helper_unsupported(CPUAVRState *env) +{ + CPUState *cs = CPU(avr_env_get_cpu(env)); + + /* + * I count not find what happens on the real platform, so + * it's EXCP_DEBUG for meanwhile + */ + cs->exception_index = EXCP_DEBUG; + if (qemu_loglevel_mask(LOG_UNIMP)) { + qemu_log("UNSUPPORTED\n"); + cpu_dump_state(cs, qemu_logfile, fprintf, 0); + } + cpu_loop_exit(cs); +} void helper_debug(CPUAVRState *env) { @@ -143,3 +184,218 @@ void helper_debug(CPUAVRState *env) cpu_loop_exit(cs); } +void helper_wdr(CPUAVRState *env) +{ + CPUState *cs = CPU(avr_env_get_cpu(env)); + + /* WD is not implemented yet, placeholder */ + cs->exception_index = EXCP_DEBUG; + cpu_loop_exit(cs); +} + +/* + * This function implements IN instruction + * + * It does the following + * a. if an IO register belongs to CPU, its value is read and returned + * b. otherwise io address is translated to mem address and physical memory + * is read. + * c. it caches the value for sake of SBI, SBIC, SBIS & CBI implementation + * + */ +target_ulong helper_inb(CPUAVRState *env, uint32_t port) +{ + target_ulong data = 0; + + switch (port) { + case 0x38: { + data = 0xff & (env->rampD >> 16); /* RAMPD */ + break; + } + case 0x39: { + data = 0xff & (env->rampX >> 16); /* RAMPX */ + break; + } + case 0x3a: { + data = 0xff & (env->rampY >> 16); /* RAMPY */ + break; + } + case 0x3b: { + data = 0xff & (env->rampZ >> 16); /* RAMPZ */ + break; + } + case 0x3c: { + data = 0xff & (env->eind >> 16); /* EIND */ + break; + } + case 0x3d: { /* SPL */ + data = env->sp & 0x00ff; + break; + } + case 0x3e: { /* SPH */ + data = env->sp >> 8; + break; + } + case 0x3f: { /* SREG */ + data = cpu_get_sreg(env); + break; + } + default: { + /* + * CPU does not know how to read this register, pass it to the + * device/board + */ + + cpu_physical_memory_read(PHYS_BASE_REGS + port + + AVR_CPU_IO_REGS_BASE, &data, 1); + } + } + + /* make a copy */ + if (port < AVR_CPU_IO_REGS) { + env->io[port] = data; + } + + return data; +} + +/* + * This function implements OUT instruction + * + * It does the following + * a. if an IO register belongs to CPU, its value is written into the register + * b. otherwise io address is translated to mem address and physical memory + * is written. + * c. it caches the value for sake of SBI, SBIC, SBIS & CBI implementation + * + */ +void helper_outb(CPUAVRState *env, uint32_t port, uint32_t data) +{ + data &= 0x000000ff; + + switch (port) { + case 0x04: { + qemu_irq irq; + CPUState *cpu = CPU(avr_env_get_cpu(env)); + irq = qdev_get_gpio_in(DEVICE(cpu), 3); + qemu_set_irq(irq, 1); + break; + } + case 0x38: { + if (avr_feature(env, AVR_FEATURE_RAMPD)) { + env->rampD = (data & 0xff) << 16; /* RAMPD */ + } + break; + } + case 0x39: { + if (avr_feature(env, AVR_FEATURE_RAMPX)) { + env->rampX = (data & 0xff) << 16; /* RAMPX */ + } + break; + } + case 0x3a: { + if (avr_feature(env, AVR_FEATURE_RAMPY)) { + env->rampY = (data & 0xff) << 16; /* RAMPY */ + } + break; + } + case 0x3b: { + if (avr_feature(env, AVR_FEATURE_RAMPZ)) { + env->rampZ = (data & 0xff) << 16; /* RAMPZ */ + } + break; + } + case 0x3c: { + env->eind = (data & 0xff) << 16; /* EIDN */ + break; + } + case 0x3d: { /* SPL */ + env->sp = (env->sp & 0xff00) | (data); + break; + } + case 0x3e: { /* SPH */ + if (avr_feature(env, AVR_FEATURE_2_BYTE_SP)) { + env->sp = (env->sp & 0x00ff) | (data << 8); + } + break; + } + case 0x3f: { /* SREG */ + cpu_set_sreg(env, data); + break; + } + default: { + /* + * CPU does not know how to write this register, pass it to the + * device/board + */ + cpu_physical_memory_write(PHYS_BASE_REGS + port + + AVR_CPU_IO_REGS_BASE, &data, 1); + } + } + + /* make a copy */ + if (port < AVR_CPU_IO_REGS) { + env->io[port] = data; + } +} + +/* + * this function implements LD instruction when there is a posibility to read + * from a CPU register + */ +target_ulong helper_fullrd(CPUAVRState *env, uint32_t addr) +{ + uint8_t data; + switch (addr) { + /* CPU registers */ + case AVR_CPU_REGS_BASE ... AVR_CPU_REGS_LAST: { + data = env->r[addr - AVR_CPU_REGS_BASE]; + break; + } + /* CPU IO registers & EXT IO registers */ + case AVR_CPU_IO_REGS_BASE ... AVR_EXT_IO_REGS_LAST: { + data = helper_inb(env, addr); + break; + } + + /* memory */ + default: { + cpu_physical_memory_read(PHYS_BASE_DATA + addr - VIRT_BASE_DATA, + &data, 1); + } + } + + env->fullacc = false; + + return data; +} + +/* + * this function implements LD instruction when there is a posibility to write + * into a CPU register + */ +void helper_fullwr(CPUAVRState *env, uint32_t data, uint32_t addr) +{ + switch (addr) { + /* CPU registers */ + case AVR_CPU_REGS_BASE ... AVR_CPU_REGS_LAST: { + env->r[addr - AVR_CPU_REGS_BASE] = data; + break; + } + + /* CPU IO registers & EXT IO registers */ + case AVR_CPU_IO_REGS_BASE ... AVR_EXT_IO_REGS_LAST: { + helper_outb(env, data, addr); + break; + } + + /* memory */ + default: { + cpu_physical_memory_write(PHYS_BASE_DATA + addr - VIRT_BASE_DATA, + &data, 1); + } + } + + env->fullacc = false; +} + diff --git a/target-avr/helper.h b/target-avr/helper.h index c60ac3e..6036315 100644 --- a/target-avr/helper.h +++ b/target-avr/helper.h @@ -18,5 +18,11 @@ * */ +DEF_HELPER_1(wdr, void, env) DEF_HELPER_1(debug, void, env) - +DEF_HELPER_1(sleep, void, env) +DEF_HELPER_1(unsupported, void, env) +DEF_HELPER_3(outb, void, env, i32, i32) +DEF_HELPER_2(inb, tl, env, i32) +DEF_HELPER_3(fullwr, void, env, i32, i32) +DEF_HELPER_2(fullrd, tl, env, i32) diff --git a/target-avr/translate.c b/target-avr/translate.c index 1a2b192..cc3c091 100644 --- a/target-avr/translate.c +++ b/target-avr/translate.c @@ -151,6 +151,14 @@ void gen_intermediate_code(CPUAVRState *env, struct TranslationBlock *tb) if (max_insns > TCG_MAX_INSNS) { max_insns = TCG_MAX_INSNS; } + if (tb->flags & TB_FLAGS_FULL_ACCESS) { + /* + this flag is set by ST/LD instruction + we will regenerate ONLY it with mem/cpu memory access + insttead of mem access + */ + max_insns = 1; + } gen_tb_start(tb); @@ -221,6 +229,7 @@ void gen_intermediate_code(CPUAVRState *env, struct TranslationBlock *tb) } done_generating: + env->fullacc = false; gen_tb_end(tb, num_insns); tb->size = (npc - pc_start) * 2;