From patchwork Wed Sep 15 17:49:50 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 512225 Delivered-To: patch@linaro.org Received: by 2002:a02:c816:0:0:0:0:0 with SMTP id p22csp422742jao; Wed, 15 Sep 2021 10:51:41 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyZ1SV8wcgs7biKEaY6D77pNXgRUJGixkjyUlfWDMTOnO593yMnM485ptjPWgzzaVgWtQVX X-Received: by 2002:a0c:b2c5:: with SMTP id d5mr911574qvf.65.1631728300976; Wed, 15 Sep 2021 10:51:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631728300; cv=none; d=google.com; s=arc-20160816; b=d3C/ATiXXKIzIaogozou2v9FKo/aXyvBz/0mAmPJOVTKTTEddQK/EA4A467xcLCPVz DWpmMB2nyJ26khQ91SmnIqOG9IEjBHtfHkjlhOlv+SaeHZcBS1MOO8fkMV1NZaSi6OAn DQHKvBswA/fU/OztAbaNdOBH3WoYL5EYsDXGj7DSbH3PIbNh2KXyVsZhYLr3521eWXCX d1tKYdT73DhvWmDPOnS9iO/BcALzxUvGUGuwlgpPSoBEBmnRuCZBRPw4xxJtADal8lv/ Sqiky8VYn8m/iqLwNX01Q2CWJtX1+MRXXf88nJ9YeQ55uRo18mHIzr5tkN60w0/v9kT7 wdYQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dkim-signature; bh=75RqB79QvpeJPK1TnV3ytIKNiMyyMPujvshHYxsAxvE=; b=kxM4igp2P8dFZIkrgwLff1SKWt3Id+eWnvUDLoeGlbceLW2o6zSJrGuGQhBxg6s2+F JOi0iiiqEuskHoOak03iK+rE/Ifavm4FtXEPdk3CRx+4xpYgJ/CwMRbpWDllDAFpkqq2 zPFhufPduOz9sZcNPwg50p+5kcry87zZa7wLXgL9wXLitTk1rAoSP/0A1jvgIUnRKFIK E59XhQIEz6Z1DHYswIYw4FL9IkZAH5uuTcxiOngYonYkVhI2ilQRYAxyg3jL1ATOa6xw cHLiuTlbk0lk/+MCwpuETu6zV2LVACH4lq0dzIJ2FjX06VO29g1s95KlxhyrhHrpLzM9 sQ/w== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=mnSP83B5; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id p18si279426qtn.233.2021.09.15.10.51.40 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Wed, 15 Sep 2021 10:51:40 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=mnSP83B5; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:38314 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mQZ4O-00024W-GZ for patch@linaro.org; Wed, 15 Sep 2021 13:51:40 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:50448) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mQZ2o-00012M-Ai for qemu-devel@nongnu.org; Wed, 15 Sep 2021 13:50:02 -0400 Received: from mail-pg1-x52f.google.com ([2607:f8b0:4864:20::52f]:42793) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mQZ2h-00037l-Fl for qemu-devel@nongnu.org; Wed, 15 Sep 2021 13:50:02 -0400 Received: by mail-pg1-x52f.google.com with SMTP id q68so3459942pga.9 for ; Wed, 15 Sep 2021 10:49:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=75RqB79QvpeJPK1TnV3ytIKNiMyyMPujvshHYxsAxvE=; b=mnSP83B5TSJ+mLg5oy7b+32S1DAKt08gQFhJyYmPPxHSAYUUlUzZ58K6zgQjiOACD7 DKJhAn2Ia9Jn/mr6z7A+C1EHo+Wn75m4rroX4feX8bjpZ+zkj6v39wr95uqGkVneOot8 BNEWVExd2cl/++bqCqSa2W8jFAXHTjzNSE87Txcd61QF8/4uq3iXkM6Dz0/ijANdN9Cz 4bqAQDuafLCYyTe/RnNbe2KD3UHaufi4qFlR2iondoLJZdGicOSxf19lzAC7tsxTBTA9 aHnAW+tWjU/hkYnis6DcZWdRHhxmyJ229+KX8EP3tvegr8btBj6MxPtd6JX5FNR3UM/8 0XeQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=75RqB79QvpeJPK1TnV3ytIKNiMyyMPujvshHYxsAxvE=; b=PRK4CC59yiYyytWWELqmq8Ir0jUKfDYCbtiJAUPRptL4GxsdJUr/Urpdg6wnGBtLCN /6VO8eDRlYaoHJbIPdY7D74uBSmBlHV9n+aFsFv0Ze/iYrOGLTokGjDxAjW5vdQr1zP+ NtmtDxhi4IT38BAeH9YOWarY47wIXfNXdIvRuwxgMUp/H8r66Wmyw/zJ8U/8nWPZ+mRU HjQR80W6xVME7Pe6M0YBKQMwpkQOEcsxw7kIjtFp2OwN9EvtEHtOlqjeFUVlD8e/sh9O KYKuLvv9NMa1mTfAP/vbYt7x/ehFQvQTZrcEvtfvh1pb/AfjQLcw4cvljppec9RiyCbm ykbw== X-Gm-Message-State: AOAM531DeswzxVQd2eYd3ngcduWnSIuVL63Wnz+euT2XVwP2L2YennQx FpbCpvXOxUJd7AQFCqgDaqeZf9098i/wLw== X-Received: by 2002:a65:404d:: with SMTP id h13mr943458pgp.130.1631728193850; Wed, 15 Sep 2021 10:49:53 -0700 (PDT) Received: from localhost.localdomain ([71.212.134.125]) by smtp.gmail.com with ESMTPSA id g4sm311667pjt.56.2021.09.15.10.49.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Sep 2021 10:49:53 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Subject: [PATCH 1/2] linux-user/nios2: Properly emulate EXCP_TRAP Date: Wed, 15 Sep 2021 10:49:50 -0700 Message-Id: <20210915174951.1852266-2-richard.henderson@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210915174951.1852266-1-richard.henderson@linaro.org> References: <20210915174951.1852266-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::52f; envelope-from=richard.henderson@linaro.org; helo=mail-pg1-x52f.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: marex@denx.de, crwulff@gmail.com, laurent@vivier.eu Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" The real kernel has to load the instruction and extract the imm5 field; for qemu, modify the translator to do this. The use of R_AT for this in cpu_loop was a bug. Handle the other trap numbers as per the kernel's trap_table. Signed-off-by: Richard Henderson --- target/nios2/cpu.h | 5 +++-- linux-user/nios2/cpu_loop.c | 35 ++++++++++++++++++----------------- target/nios2/translate.c | 17 ++++++++++++++++- 3 files changed, 37 insertions(+), 20 deletions(-) -- 2.25.1 diff --git a/target/nios2/cpu.h b/target/nios2/cpu.h index 2ab82fdc71..395e4d3281 100644 --- a/target/nios2/cpu.h +++ b/target/nios2/cpu.h @@ -158,9 +158,10 @@ struct Nios2CPUClass { struct CPUNios2State { uint32_t regs[NUM_CORE_REGS]; -#if !defined(CONFIG_USER_ONLY) +#ifdef CONFIG_USER_ONLY + int trap_code; +#else Nios2MMU mmu; - uint32_t irq_pending; #endif }; diff --git a/linux-user/nios2/cpu_loop.c b/linux-user/nios2/cpu_loop.c index 9869083fa1..d76bdb2891 100644 --- a/linux-user/nios2/cpu_loop.c +++ b/linux-user/nios2/cpu_loop.c @@ -37,9 +37,10 @@ void cpu_loop(CPUNios2State *env) case EXCP_INTERRUPT: /* just indicate that signals should be handled asap */ break; + case EXCP_TRAP: - if (env->regs[R_AT] == 0) { - abi_long ret; + switch (env->trap_code) { + case 0: qemu_log_mask(CPU_LOG_INT, "\nSyscall\n"); ret = do_syscall(env, env->regs[2], @@ -53,26 +54,26 @@ void cpu_loop(CPUNios2State *env) env->regs[2] = abs(ret); /* Return value is 0..4096 */ - env->regs[7] = (ret > 0xfffffffffffff000ULL); - env->regs[CR_ESTATUS] = env->regs[CR_STATUS]; - env->regs[CR_STATUS] &= ~0x3; - env->regs[R_EA] = env->regs[R_PC] + 4; + env->regs[7] = ret > 0xfffff000u; env->regs[R_PC] += 4; break; - } else { - qemu_log_mask(CPU_LOG_INT, "\nTrap\n"); - env->regs[CR_ESTATUS] = env->regs[CR_STATUS]; - env->regs[CR_STATUS] &= ~0x3; - env->regs[R_EA] = env->regs[R_PC] + 4; - env->regs[R_PC] = cpu->exception_addr; - - info.si_signo = TARGET_SIGTRAP; - info.si_errno = 0; - info.si_code = TARGET_TRAP_BRKPT; - queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info); + case 1: + qemu_log_mask(CPU_LOG_INT, "\nTrap 1\n"); + force_sig_fault(TARGET_SIGUSR1, 0, env->regs[R_PC]); + break; + case 2: + qemu_log_mask(CPU_LOG_INT, "\nTrap 2\n"); + force_sig_fault(TARGET_SIGUSR2, 0, env->regs[R_PC]); + break; + default: + qemu_log_mask(CPU_LOG_INT, "\nTrap %d\n", env->trap_code); + force_sig_fault(TARGET_SIGILL, TARGET_ILL_ILLTRP, + env->regs[R_PC]); break; } + break; + case EXCP_DEBUG: info.si_signo = TARGET_SIGTRAP; info.si_errno = 0; diff --git a/target/nios2/translate.c b/target/nios2/translate.c index 08d7ac5398..485b487665 100644 --- a/target/nios2/translate.c +++ b/target/nios2/translate.c @@ -636,6 +636,21 @@ static void divu(DisasContext *dc, uint32_t code, uint32_t flags) tcg_temp_free(t0); } +static void trap(DisasContext *dc, uint32_t code, uint32_t flags) +{ +#ifdef CONFIG_USER_ONLY + /* + * The imm5 field is not stored anywhere on real hw; the kernel + * has to load the insn and extract the field. But we can make + * things easier for cpu_loop if we pop this into env->trap_code. + */ + R_TYPE(instr, code); + tcg_gen_st_i32(tcg_constant_i32(instr.imm5), cpu_env, + offsetof(CPUNios2State, trap_code)); +#endif + t_gen_helper_raise_exception(dc, EXCP_TRAP); +} + static const Nios2Instruction r_type_instructions[] = { INSTRUCTION_ILLEGAL(), INSTRUCTION(eret), /* eret */ @@ -682,7 +697,7 @@ static const Nios2Instruction r_type_instructions[] = { INSTRUCTION_ILLEGAL(), INSTRUCTION_ILLEGAL(), INSTRUCTION_ILLEGAL(), - INSTRUCTION_FLG(gen_excp, EXCP_TRAP), /* trap */ + INSTRUCTION(trap), /* trap */ INSTRUCTION(wrctl), /* wrctl */ INSTRUCTION_ILLEGAL(), INSTRUCTION_FLG(gen_cmpxx, TCG_COND_LTU), /* cmpltu */ From patchwork Wed Sep 15 17:49:51 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 512226 Delivered-To: patch@linaro.org Received: by 2002:a02:c816:0:0:0:0:0 with SMTP id p22csp423903jao; Wed, 15 Sep 2021 10:53:19 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzdOVeMAt7kC5Z9LgbXnhOdjSW/gSgsioR7tVPqspoX0F9qEcLi8r+gv17QUOO4z84AmVj9 X-Received: by 2002:a0c:9046:: with SMTP id o64mr1122141qvo.47.1631728399614; Wed, 15 Sep 2021 10:53:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631728399; cv=none; d=google.com; s=arc-20160816; b=vu81h/NvYT5QeiF3ovFuTwJbJ3Cm+G7RTFWxbkAMAyjlF9+Ldsnbykp0E1fc///1eL eLbxk2KQMHvG0uuAaOj+1z6wEOxjuoYb8aTy//xf8RMovySNSpA+UY47QprhYe9yXC+R CRi+THaxlpqCm+cA8kVrKNCpMW0JaL7cw+p+VcsJwjjY6/nM90nCLj6d1/cfPDXGGKv7 NKGc3p4kl782qohZGyoyulc4m0w1Aa0f6rl3M8CSX4fUASI6J1J4ZnnJYPcz7FNjUWqZ v0NqUF7ThBVK4G1tLOjvIOQ1ZHhqr07eqqUTsOUJhfvJMyOe6UaFDXM5Dnv0P7x1jJGm nMkQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dkim-signature; bh=mV2eu/Zav+aVRqycI4SiuColM6i5MZEh5iI5/KRQ2x0=; b=iEq6OK0w8DanqGrJJlTV6w7/ARwDl8CkRGwVm2ZAJo5hyja9JvyrOhtj5kBaqPsqzt Nwfo0xmwWxZXDcng1zaPAj9sqxJB9XZzDPld85WEXqlAnt0PtAv9l74yvOi13k34N0VW 3dV+4yX67f8KFYv/HdKBYyHc+d08auBZzwB3NM5LbgpMj5w9LfdKk80tidsGyVqF7Npn +DCtIM2qkR23ewQopEDofZRLKQnVOVbzr1ptEZf3Q8gcVhB3wW4BJL12/AHxX40ew7hY DNd+WZITIdSQiNCOuyaNPYtk262iOQgymAcw/7/pMyXHW4gBDYt2lJkQ3qFpyTQj1wSG RLeg== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=IT6WbsAI; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id t26si330310qkj.131.2021.09.15.10.53.19 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Wed, 15 Sep 2021 10:53:19 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=IT6WbsAI; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:41230 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mQZ5z-00044h-4R for patch@linaro.org; Wed, 15 Sep 2021 13:53:19 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:50420) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mQZ2j-0000uG-Fb for qemu-devel@nongnu.org; Wed, 15 Sep 2021 13:49:57 -0400 Received: from mail-pg1-x531.google.com ([2607:f8b0:4864:20::531]:42795) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mQZ2h-00038f-Pd for qemu-devel@nongnu.org; Wed, 15 Sep 2021 13:49:57 -0400 Received: by mail-pg1-x531.google.com with SMTP id q68so3459966pga.9 for ; Wed, 15 Sep 2021 10:49:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=mV2eu/Zav+aVRqycI4SiuColM6i5MZEh5iI5/KRQ2x0=; b=IT6WbsAItzILiqzNwuskc3kHAUDmVsx1ahoVDCIzZzeqqDaO9cbgCR5ZdE8M5rKNvO 6d0mYGef7bWjzwD9WTHj4frx9hhRUgMOQ1tYUFhH8aLVohecFsCKi+aeXe+cEz+fvuY/ AAIDRrP0PIKRdN4SXaDrW99WnU0D4FjjrdIeTp4pLuoHWUNCIw6ZYsAYulS/bXlPSQKM HCiXkLfFt3rmY9HfFyd5tBIDryhgfQ5A5K83Be3YpG5JL49tmG6H6+cnXs6jCVrPlMcS 7yI0GoLXS7WE/itE9onVCwGrA7XbXemwVBCICYkMjO7pC12L7r36kYrFp3n7f6mLtZoY A7CQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=mV2eu/Zav+aVRqycI4SiuColM6i5MZEh5iI5/KRQ2x0=; b=N9hG5dWuTsrrdARiMw7Gq5AFpU2yk7ASIK647Dr3EyCIC8mwXe4xfwKYdwHbNXugHJ nOfVfVklBxyWKRJjJWaavXDvvWPD4S+SFL0ibF3gM+2ElSzn1jWA6NejUMLIE2yvLOVb /3bcegWqxGqhaa2gPJArTSFRYKulUJ9C2GKCxHDmOyhMRGHLscu9MrvsSnKnU0dyWJ/X QHSutY+D5v+sD7nN09bfnyQjdO2tU50opqrCRQuwufOpOhcrYbUIxwpVcB+zcCjJg3h7 HT1HtagaBGLsgYBWRfg/LH+JKWoq6/sAvJOmy6ejoB+6pexKA6ebi5oJUJUVne7r8jQy sxqg== X-Gm-Message-State: AOAM533KQq4GM6WVuGT94JNblRubMJhNhw1gdaR1RfasXScbctg3Og3b UpzuAqBJUsqfphQOSDnD2J+Srpg2eG7n3w== X-Received: by 2002:aa7:8426:0:b0:438:3550:f190 with SMTP id q6-20020aa78426000000b004383550f190mr1095691pfn.19.1631728194493; Wed, 15 Sep 2021 10:49:54 -0700 (PDT) Received: from localhost.localdomain ([71.212.134.125]) by smtp.gmail.com with ESMTPSA id g4sm311667pjt.56.2021.09.15.10.49.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Sep 2021 10:49:54 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Subject: [PATCH 2/2] linux-user/nios2: Map a real kuser page Date: Wed, 15 Sep 2021 10:49:51 -0700 Message-Id: <20210915174951.1852266-3-richard.henderson@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210915174951.1852266-1-richard.henderson@linaro.org> References: <20210915174951.1852266-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::531; envelope-from=richard.henderson@linaro.org; helo=mail-pg1-x531.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: marex@denx.de, crwulff@gmail.com, laurent@vivier.eu Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" The first word of page1 is data, so the whole thing can't be implemented with emulation of addresses. Hijack trap number 31 to implement cmpxchg. Signed-off-by: Richard Henderson --- linux-user/elfload.c | 35 +++++++++++++++++++++++++ linux-user/nios2/cpu_loop.c | 52 ++++++++++++++++++++----------------- target/nios2/translate.c | 9 ------- 3 files changed, 63 insertions(+), 33 deletions(-) -- 2.25.1 diff --git a/linux-user/elfload.c b/linux-user/elfload.c index 01e9a833fb..e78c0af183 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -1091,6 +1091,41 @@ static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUMBState *env static void init_thread(struct target_pt_regs *regs, struct image_info *infop) { + static const uint8_t kuser_page[128] = { + /* __kuser_helper_version */ + [0x00] = 0x02, 0x00, 0x00, 0x00, + + /* __kuser_cmpxchg */ + [0x04] = 0xfa, 0x6f, 0x3b, 0x00, /* trap 31 */ + 0x3a, 0x28, 0x00, 0xf8, /* ret */ + + /* __kuser_sigtramp */ + [0x40] = 0xc4, 0x22, 0x80, 0x00, /* movi r2, __NR_rt_sigreturn */ + 0x3a, 0x68, 0x3b, 0x00, /* trap 0 */ + }; + + abi_ulong pg; + uint8_t *ph; + + pg = target_mmap(TARGET_PAGE_SIZE, TARGET_PAGE_SIZE, + PROT_READ | PROT_WRITE, + MAP_ANON | MAP_PRIVATE | MAP_FIXED, -1, 0); + + /* + * If the mmap doesn't give us exactly page 1, there's nothing + * we can do, and it's unlikely that the program will be able + * to continue. FIXME: Error out now? + */ + if (pg == TARGET_PAGE_SIZE) { + ph = lock_user(VERIFY_WRITE, pg, sizeof(kuser_page), 0); + memcpy(ph, kuser_page, sizeof(kuser_page)); + unlock_user(ph, pg, sizeof(kuser_page)); + target_mprotect(TARGET_PAGE_SIZE, TARGET_PAGE_SIZE, + PROT_READ | PROT_EXEC); + } else { + target_munmap(pg, TARGET_PAGE_SIZE); + } + regs->ea = infop->entry; regs->sp = infop->start_stack; regs->estatus = 0x3; diff --git a/linux-user/nios2/cpu_loop.c b/linux-user/nios2/cpu_loop.c index d76bdb2891..fd3f853ac2 100644 --- a/linux-user/nios2/cpu_loop.c +++ b/linux-user/nios2/cpu_loop.c @@ -20,11 +20,11 @@ #include "qemu/osdep.h" #include "qemu.h" #include "cpu_loop-common.h" +#include "signal-common.h" void cpu_loop(CPUNios2State *env) { CPUState *cs = env_cpu(env); - Nios2CPU *cpu = NIOS2_CPU(cs); target_siginfo_t info; int trapnr, ret; @@ -71,6 +71,32 @@ void cpu_loop(CPUNios2State *env) force_sig_fault(TARGET_SIGILL, TARGET_ILL_ILLTRP, env->regs[R_PC]); break; + + case 31: /* QEMU specific, for __kuser_cmpxchg */ + { + abi_ptr g = env->regs[4]; + uint32_t *h, n, o; + + if (g & 0x3) { + force_sig_fault(TARGET_SIGBUS, TARGET_BUS_ADRALN, g); + break; + } + ret = page_get_flags(g); + if (!(ret & PAGE_VALID)) { + force_sig_fault(TARGET_SIGSEGV, TARGET_SEGV_MAPERR, g); + break; + } + if (!(ret & PAGE_READ) || !(ret & PAGE_WRITE)) { + force_sig_fault(TARGET_SIGSEGV, TARGET_SEGV_ACCERR, g); + break; + } + h = g2h(cs, g); + o = env->regs[5]; + n = env->regs[6]; + env->regs[2] = qatomic_cmpxchg(h, o, n) - o; + env->regs[R_PC] += 4; + } + break; } break; @@ -81,29 +107,7 @@ void cpu_loop(CPUNios2State *env) queue_signal(env, info.si_signo, QEMU_SI_FAULT, &info); break; case 0xaa: - switch (env->regs[R_PC]) { - /*case 0x1000:*/ /* TODO:__kuser_helper_version */ - case 0x1004: /* __kuser_cmpxchg */ - start_exclusive(); - if (env->regs[4] & 0x3) { - goto kuser_fail; - } - ret = get_user_u32(env->regs[2], env->regs[4]); - if (ret) { - end_exclusive(); - goto kuser_fail; - } - env->regs[2] -= env->regs[5]; - if (env->regs[2] == 0) { - put_user_u32(env->regs[6], env->regs[4]); - } - end_exclusive(); - env->regs[R_PC] = env->regs[R_RA]; - break; - /*case 0x1040:*/ /* TODO:__kuser_sigtramp */ - default: - ; -kuser_fail: + { info.si_signo = TARGET_SIGSEGV; info.si_errno = 0; /* TODO: check env->error_code */ diff --git a/target/nios2/translate.c b/target/nios2/translate.c index 485b487665..77706dd805 100644 --- a/target/nios2/translate.c +++ b/target/nios2/translate.c @@ -795,15 +795,6 @@ static void nios2_tr_translate_insn(DisasContextBase *dcbase, CPUState *cs) dc->base.pc_next = pc + 4; /* Decode an instruction */ - -#if defined(CONFIG_USER_ONLY) - /* FIXME: Is this needed ? */ - if (pc >= 0x1000 && pc < 0x2000) { - t_gen_helper_raise_exception(dc, 0xaa); - return; - } -#endif - code = cpu_ldl_code(env, pc); op = get_opcode(code);