From patchwork Sun Jul 3 08:59:03 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 586857 Delivered-To: patch@linaro.org Received: by 2002:a05:7000:5036:0:0:0:0 with SMTP id e22csp2942791mab; Sun, 3 Jul 2022 02:35:04 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vRfPbYvGQYVMBLxRrVZmWgRPccrozCxnQTrap+7X2zm9vkxXYFHFKiWcfo5IjSkyDSQ8AG X-Received: by 2002:a81:1315:0:b0:318:1841:8060 with SMTP id 21-20020a811315000000b0031818418060mr26963694ywt.452.1656840904253; Sun, 03 Jul 2022 02:35:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656840904; cv=none; d=google.com; s=arc-20160816; b=p1g/U+WTP9ojqCsn3npGIT33cv6nBaiOC8NjcOcXXPrcEdDTfG4p2lpPitl6x+qgul hSwLE02p/dgPGaoZXpajU7bwiOIstNfUq+YURkMbjBZQxrWSZhmlwy7W2SAv67XjQEv6 XZZTltORqs1zLuDPN35Y9LBXmmJVpuz4O3XshgAWHKEqHYGDdf7Csqd4IKUOi4JTilhe UM1tL0uv6NsSdCLJ+fZMx1d7tzvfHWG466e7fz9yD8nHqP/0UJXyEZPgcVELwXo9dF+G 5E1xI1LCPfkbjQiyo65y1Jh6R3tJx42orCdym8o3UMeJ6pnyQ7Ha2dAdSXqub5xI58wt 5Rlw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to: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:cc:to :from:dkim-signature; bh=wdR2AdXYQcU9MB9eRdNwupFKlHHJUO4XdfGfsX7z9ZA=; b=EenmbmZ/1hZfEJ/m57NVM+4X/Jegl1qCyB02IKw1PaF+cHFdicIfLR4L9xKlMz/sbD 6hSuwHGlm56cWfxBjiDQWDdQ6yvxXOkSIR/kUgX5rda9MlpvX8/xbcpeEq8ShZlAUIB6 Xj/F/ZoOWgUlg/YpPdZ+BPHSNaZ5+CUaZi5Jmzol2N7+2aY8DMxEBywdCKFOHXKLVecn vgXh3CsfLQoVRfip8QyrI1XsWuyqZP0EF5lH7C6fXmfksQmX3PK2rGNdPcsFIDEDtruY Cs+y5cYpHOxTK9Wqe9EqeVFQ4FuWNJ9yIq+zTaf52DcjIuJqNJCdiZffYgQtBHQCmcfq 0yxQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="r5s3lkQ/"; 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=pass (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 f83-20020a256a56000000b0066e248430fbsi4813591ybc.393.2022.07.03.02.35.04 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Sun, 03 Jul 2022 02:35:04 -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=pass header.i=@linaro.org header.s=google header.b="r5s3lkQ/"; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:56748 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1o7w0N-0004gi-Nm for patch@linaro.org; Sun, 03 Jul 2022 05:35:03 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:58622) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o7vRz-0004DD-NA for qemu-devel@nongnu.org; Sun, 03 Jul 2022 04:59:31 -0400 Received: from mail-pj1-x1030.google.com ([2607:f8b0:4864:20::1030]:53213) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1o7vRy-00049V-2e for qemu-devel@nongnu.org; Sun, 03 Jul 2022 04:59:31 -0400 Received: by mail-pj1-x1030.google.com with SMTP id fz10so475223pjb.2 for ; Sun, 03 Jul 2022 01:59:29 -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=wdR2AdXYQcU9MB9eRdNwupFKlHHJUO4XdfGfsX7z9ZA=; b=r5s3lkQ/KH/owNvwkx8GX3ARK+tLsS7L8Y3iDkvhKZOpNdAPHnpVGHD8XVqjad6oaD 91dlITPT86XOWg4XNwFBoRprFn+mkHqjf61iLU7ssdVxT7e7HqkbYlY5xT8wp7zSRxfe 7kGohEzRh6XqnTCYeROhh9kc7du36btbJhupgc+2hYtpN3i62lXgddAb/mxbRMV0By6F JaEyOnnfsvYYvuGJK1qfOawM+qnVyjVq8LdgMBOJSA4qcgHMiXiAfRbpnE6/+GMyJmvZ r/uvdgVJ0hzn1LxZtOZjeEfOZ7sc74D7/B+I1P6M8WRyWPeVZCTjv+TkcgWxL3xKjw46 hnZw== 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=wdR2AdXYQcU9MB9eRdNwupFKlHHJUO4XdfGfsX7z9ZA=; b=ayg9OOOQODTiaIlufDRZaPsWaWUgAZnksPibz3MrkJKAubRXbm9rbU5eyQzCdZB4CZ pbzaGLGMm/2/vQpGWfysPCDnSrvG/JrudT64XqjUqpeAdv5u6twhIO7P+5qXV0Ty3582 ZERoebOhK+9ZXtUazdTpCtwMsxG+kToej71feeUfzHKZulaopV4evIoK8nwuC1GKnrpf m+j15Uoc6DngI8ATo9hKVH1K/+X9zLan/uy83YbbGYXNRvqTTttrG8SkAHTbbFhIVp0B /77NNP6tUnFrcUZWeQRHPcHKXnI55u/Xh4k1vfvkuXTfru1p/6K7Eo1QxQkp30xKXF/j jJVw== X-Gm-Message-State: AJIora+68pseEi0+at84dEaVBKfmrbulVxHbDGT4j2FWlGsS4QVWrDAc q/a5CbIul3cdg/8AzRabdWP/w+8p7th9O3Gw X-Received: by 2002:a17:90b:388c:b0:1ec:d129:704 with SMTP id mu12-20020a17090b388c00b001ecd1290704mr29205904pjb.98.1656838768784; Sun, 03 Jul 2022 01:59:28 -0700 (PDT) Received: from stoup.. ([122.255.60.245]) by smtp.gmail.com with ESMTPSA id cq16-20020a056a00331000b00518764d09cdsm18777593pfb.164.2022.07.03.01.59.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 03 Jul 2022 01:59:28 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: laurent@vivier.eu, Song Gao , Xiaojuan Yang , =?utf-8?q?Philippe_Mathieu-Daud?= =?utf-8?q?=C3=A9?= Subject: [PATCH v21 03/13] linux-user: Add LoongArch elf support Date: Sun, 3 Jul 2022 14:29:03 +0530 Message-Id: <20220703085913.772936-4-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220703085913.772936-1-richard.henderson@linaro.org> References: <20220703085913.772936-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::1030; envelope-from=richard.henderson@linaro.org; helo=mail-pj1-x1030.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, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" From: Song Gao Signed-off-by: Song Gao Signed-off-by: Xiaojuan Yang Reviewed-by: Richard Henderson Reviewed-by: Philippe Mathieu-Daudé Message-Id: <20220624031049.1716097-4-gaosong@loongson.cn> Signed-off-by: Richard Henderson --- linux-user/loongarch64/target_elf.h | 12 ++++ linux-user/elfload.c | 91 +++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 linux-user/loongarch64/target_elf.h diff --git a/linux-user/loongarch64/target_elf.h b/linux-user/loongarch64/target_elf.h new file mode 100644 index 0000000000..95c3f05a46 --- /dev/null +++ b/linux-user/loongarch64/target_elf.h @@ -0,0 +1,12 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (c) 2021 Loongson Technology Corporation Limited + */ + +#ifndef LOONGARCH_TARGET_ELF_H +#define LOONGARCH_TARGET_ELF_H +static inline const char *cpu_get_model(uint32_t eflags) +{ + return "la464"; +} +#endif diff --git a/linux-user/elfload.c b/linux-user/elfload.c index 163fc8a1ee..1de77c7959 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -922,6 +922,97 @@ static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUPPCState *en #endif +#ifdef TARGET_LOONGARCH64 + +#define ELF_START_MMAP 0x80000000 + +#define ELF_CLASS ELFCLASS64 +#define ELF_ARCH EM_LOONGARCH + +#define elf_check_arch(x) ((x) == EM_LOONGARCH) + +static inline void init_thread(struct target_pt_regs *regs, + struct image_info *infop) +{ + /*Set crmd PG,DA = 1,0 */ + regs->csr.crmd = 2 << 3; + regs->csr.era = infop->entry; + regs->regs[3] = infop->start_stack; +} + +/* See linux kernel: arch/loongarch/include/asm/elf.h */ +#define ELF_NREG 45 +typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG]; + +enum { + TARGET_EF_R0 = 0, + TARGET_EF_CSR_ERA = TARGET_EF_R0 + 33, + TARGET_EF_CSR_BADV = TARGET_EF_R0 + 34, +}; + +static void elf_core_copy_regs(target_elf_gregset_t *regs, + const CPULoongArchState *env) +{ + int i; + + (*regs)[TARGET_EF_R0] = 0; + + for (i = 1; i < ARRAY_SIZE(env->gpr); i++) { + (*regs)[TARGET_EF_R0 + i] = tswapreg(env->gpr[i]); + } + + (*regs)[TARGET_EF_CSR_ERA] = tswapreg(env->pc); + (*regs)[TARGET_EF_CSR_BADV] = tswapreg(env->CSR_BADV); +} + +#define USE_ELF_CORE_DUMP +#define ELF_EXEC_PAGESIZE 4096 + +#define ELF_HWCAP get_elf_hwcap() + +/* See arch/loongarch/include/uapi/asm/hwcap.h */ +enum { + HWCAP_LOONGARCH_CPUCFG = (1 << 0), + HWCAP_LOONGARCH_LAM = (1 << 1), + HWCAP_LOONGARCH_UAL = (1 << 2), + HWCAP_LOONGARCH_FPU = (1 << 3), + HWCAP_LOONGARCH_LSX = (1 << 4), + HWCAP_LOONGARCH_LASX = (1 << 5), + HWCAP_LOONGARCH_CRC32 = (1 << 6), + HWCAP_LOONGARCH_COMPLEX = (1 << 7), + HWCAP_LOONGARCH_CRYPTO = (1 << 8), + HWCAP_LOONGARCH_LVZ = (1 << 9), + HWCAP_LOONGARCH_LBT_X86 = (1 << 10), + HWCAP_LOONGARCH_LBT_ARM = (1 << 11), + HWCAP_LOONGARCH_LBT_MIPS = (1 << 12), +}; + +static uint32_t get_elf_hwcap(void) +{ + LoongArchCPU *cpu = LOONGARCH_CPU(thread_cpu); + uint32_t hwcaps = 0; + + hwcaps |= HWCAP_LOONGARCH_CRC32; + + if (FIELD_EX32(cpu->env.cpucfg[1], CPUCFG1, UAL)) { + hwcaps |= HWCAP_LOONGARCH_UAL; + } + + if (FIELD_EX32(cpu->env.cpucfg[2], CPUCFG2, FP)) { + hwcaps |= HWCAP_LOONGARCH_FPU; + } + + if (FIELD_EX32(cpu->env.cpucfg[2], CPUCFG2, LAM)) { + hwcaps |= HWCAP_LOONGARCH_LAM; + } + + return hwcaps; +} + +#define ELF_PLATFORM "loongarch" + +#endif /* TARGET_LOONGARCH64 */ + #ifdef TARGET_MIPS #define ELF_START_MMAP 0x80000000