From patchwork Tue Dec 19 11:50:41 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: YunQiang Su X-Patchwork-Id: 10123493 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 C800C602CB for ; Tue, 19 Dec 2017 15:42:59 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B9D8929430 for ; Tue, 19 Dec 2017 15:42:59 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id AE5F429447; Tue, 19 Dec 2017 15:42:59 +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_SIGNED, 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 5136629430 for ; Tue, 19 Dec 2017 15:42:59 +0000 (UTC) Received: from localhost ([::1]:50541 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eRK2k-0006zp-Ji for patchwork-qemu-devel@patchwork.kernel.org; Tue, 19 Dec 2017 10:42:58 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:39397) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eRGQB-0007h8-Rx for qemu-devel@nongnu.org; Tue, 19 Dec 2017 06:50:56 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eRGQ8-0005NS-NU for qemu-devel@nongnu.org; Tue, 19 Dec 2017 06:50:55 -0500 Received: from mail-pg0-x230.google.com ([2607:f8b0:400e:c05::230]:38562) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1eRGQ8-0005Mc-1W for qemu-devel@nongnu.org; Tue, 19 Dec 2017 06:50:52 -0500 Received: by mail-pg0-x230.google.com with SMTP id f12so10433289pgo.5 for ; Tue, 19 Dec 2017 03:50:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id; bh=hcZFkjDR3mfOu13sfRhb+lWhiFhHi67zFGf4n8R8zi8=; b=bkbWYYJnDEzNISzj3NziT9jalGH6RbK9mQ9anZu5M0+H1YoqLViASbVEx1N3JJYh+F 4K2R+5tVYv8f2Lc0YmXADZYbMryISEREGZyJMw2BIk0W6rPdmUCwwazInA7f1Shbx0mU bcxA7J6M+YS19g4pZltDsV+x4msCrI3BLfGQ/Y2fnYQIXNiuoQAZUpWL4TySinDSp1AN WgneGGsOEH/WbfGr72DVXTH5pAxOSPJLwQZSw2GOlRdI+KhSTa24b9fc+pqQz1L3RkKR XFuUNOA2lunxvp6tTMgYayhw491LdHregLbRuBfrDWH8v7ls6jy/pUl3RmUhHDtq5uxH o/ag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id; bh=hcZFkjDR3mfOu13sfRhb+lWhiFhHi67zFGf4n8R8zi8=; b=d7m6raaM86SW6jYLOUJQOj+5bCC78SBefaUsZrdc+aow+abriD+biZ9FQnv/7ll3lp SyasWZeNCOndyra/wP/S9xrM/UAU27b87ligam7XAfBUCX08Y1dv2zqSq/kTHGcHisA7 JHyni78zuFzJFTrYxiQLwsLwi1E/HNH93+pRIJHt/eVR0nk6bK1LfQ2MmxWakz6C5Qi9 iOS74AcKrnvwmvpPSMyYm1La2Hs3ngW4xgJ+0CN+YCOnUTbiXSab7jitdd5331NILMm9 bMds1SAg3II/Vb+huHyzqCMuJP6cPfAbIB5fV2Wh3xrsoafkpJ0gR+bkE44WnziLkjQW qjWg== X-Gm-Message-State: AKGB3mL7/BXJ1cBI3h2UKK3kC0zrDOYrfUQ98wAJ/1XY7vCSBA+mxH2u 8E0MbBdam/Tinho9PGYReg/4gWMf X-Google-Smtp-Source: ACJfBotTrKVbNNlzlPkRZXs5ggqiY5E+If7yvrOyCfk1xCp/SGktk+H4oDMYeDQL3ASaKDO/KMdBiw== X-Received: by 10.99.97.210 with SMTP id v201mr2661658pgb.84.1513684250860; Tue, 19 Dec 2017 03:50:50 -0800 (PST) Received: from loongson.vpn2.bfsu.edu.cn ([103.88.45.229]) by smtp.gmail.com with ESMTPSA id g5sm23559914pgq.71.2017.12.19.03.50.48 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 19 Dec 2017 03:50:50 -0800 (PST) From: YunQiang Su To: qemu-devel@nongnu.org Date: Tue, 19 Dec 2017 19:50:41 +0800 Message-Id: <20171219115041.6611-1-syq@debian.org> X-Mailer: git-send-email 2.15.1 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c05::230 X-Mailman-Approved-At: Tue, 19 Dec 2017 10:38:10 -0500 Subject: [Qemu-devel] [PATCH] linux-user: MIPS set cpu to r6 CPU if binary is R6 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: YunQiang Su Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP MIPS r6 is not just simple super set for pre-R6, it also drops some instruction and even changes encoding for some. But r6 binary has the same header for binfmt_misc. So here we need to detect the version of binaries and set cpu_model for it. Signed-off-by: YunQiang Su --- include/elf.h | 4 ++++ linux-user/elfload.c | 36 ++++++++++++++++++++++++++++++++++++ linux-user/main.c | 15 +++++++++++++++ linux-user/qemu.h | 1 + 4 files changed, 56 insertions(+) diff --git a/include/elf.h b/include/elf.h index e8a515ce3d..f2104809b1 100644 --- a/include/elf.h +++ b/include/elf.h @@ -40,6 +40,10 @@ typedef int64_t Elf64_Sxword; #define EF_MIPS_ARCH_5 0x40000000 /* -mips5 code. */ #define EF_MIPS_ARCH_32 0x50000000 /* MIPS32 code. */ #define EF_MIPS_ARCH_64 0x60000000 /* MIPS64 code. */ +#define EF_MIPS_ARCH_32R2 0x70000000 /* MIPS32r2 code. */ +#define EF_MIPS_ARCH_64R2 0x80000000 /* MIPS64r2 code. */ +#define EF_MIPS_ARCH_32R6 0x90000000 /* MIPS32r6 code. */ +#define EF_MIPS_ARCH_64R6 0xa0000000 /* MIPS64r6 code. */ /* The ABI of a file. */ #define EF_MIPS_ABI_O32 0x00001000 /* O32 ABI. */ diff --git a/linux-user/elfload.c b/linux-user/elfload.c index 20f3d8c2c3..f9b8e028ca 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -2224,6 +2224,42 @@ static void load_elf_interp(const char *filename, struct image_info *info, exit(-1); } +uint32_t get_elf_eflags(const char *filename) +{ + int fd, retval; + char bprm_buf[BPRM_BUF_SIZE]; + + fd = open(path(filename), O_RDONLY); + if (fd < 0) { + return 0; + } + retval = read(fd, bprm_buf, BPRM_BUF_SIZE); + close(fd); + if (retval < 0) { + return 0; + } + if (retval < BPRM_BUF_SIZE) { + memset(bprm_buf + retval, 0, BPRM_BUF_SIZE - retval); + } + + if (bprm_buf[0] != 0x7f + || bprm_buf[1] != 'E' + || bprm_buf[2] != 'L' + || bprm_buf[3] != 'F') { + return 0; + } + + struct elfhdr *ehdr = (struct elfhdr *)bprm_buf; + if (!elf_check_ident(ehdr)) { + return 0; + } + bswap_ehdr(ehdr); + if (!elf_check_ehdr(ehdr)) { + return 0; + } + return ehdr->e_flags; +} + static int symfind(const void *s0, const void *s1) { target_ulong addr = *(target_ulong *)s0; diff --git a/linux-user/main.c b/linux-user/main.c index 7c0bffeff6..b4626e5aa0 100644 --- a/linux-user/main.c +++ b/linux-user/main.c @@ -4287,6 +4287,21 @@ int main(int argc, char **argv, char **envp) } trace_init_file(trace_file); +#if defined(TARGET_MIPS) + if (cpu_model == NULL) { + uint32_t eflags = get_elf_eflags(filename); +#if defined(TARGET_ABI_MIPSN32) || defined(TARGET_ABI_MIPSN64) + if ((eflags & EF_MIPS_ARCH_64R6) != 0) { + cpu_model = "I6400"; + } +#else + if ((eflags & EF_MIPS_ARCH_32R6) != 0) { + cpu_model = "mips32r6-generic"; + } +#endif + } +#endif + /* Zero out regs */ memset(regs, 0, sizeof(struct target_pt_regs)); diff --git a/linux-user/qemu.h b/linux-user/qemu.h index 4edd7d0c08..cf09110bf9 100644 --- a/linux-user/qemu.h +++ b/linux-user/qemu.h @@ -190,6 +190,7 @@ int loader_exec(int fdexec, const char *filename, char **argv, char **envp, int load_elf_binary(struct linux_binprm *bprm, struct image_info *info); int load_flt_binary(struct linux_binprm *bprm, struct image_info *info); +uint32_t get_elf_eflags(const char *filename); abi_long memcpy_to_target(abi_ulong dest, const void *src, unsigned long len);