From nobody Thu May 2 12:48:56 2024 Delivered-To: importer@patchew.org Received-SPF: temperror (zoho.com: Error in retrieving data from DNS) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=temperror (zoho.com: Error in retrieving data from DNS) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1513698220721377.1832176722131; Tue, 19 Dec 2017 07:43:40 -0800 (PST) Received: from localhost ([::1]:50541 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eRK3B-0006zp-Vx for importer@patchew.org; Tue, 19 Dec 2017 10:43:26 -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) 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) 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) 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+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_6 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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. --- 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. */ =20 /* 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, st= ruct image_info *info, exit(-1); } =20 +uint32_t get_elf_eflags(const char *filename) +{ + int fd, retval; + char bprm_buf[BPRM_BUF_SIZE]; + + fd =3D open(path(filename), O_RDONLY); + if (fd < 0) { + return 0; + } + retval =3D 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] !=3D 0x7f + || bprm_buf[1] !=3D 'E' + || bprm_buf[2] !=3D 'L' + || bprm_buf[3] !=3D 'F') { + return 0; + } + + struct elfhdr *ehdr =3D (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 =3D *(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); =20 +#if defined(TARGET_MIPS) + if (cpu_model =3D=3D NULL) { + uint32_t eflags =3D get_elf_eflags(filename); +#if defined(TARGET_ABI_MIPSN32) || defined(TARGET_ABI_MIPSN64) + if ((eflags & EF_MIPS_ARCH_64R6) !=3D 0) { + cpu_model =3D "I6400"; + } +#else + if ((eflags & EF_MIPS_ARCH_32R6) !=3D 0) { + cpu_model =3D "mips32r6-generic"; + } +#endif + } +#endif + /* Zero out regs */ memset(regs, 0, sizeof(struct target_pt_regs)); =20 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, =20 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); =20 abi_long memcpy_to_target(abi_ulong dest, const void *src, unsigned long len); --=20 2.15.1