Received: by 2002:a19:771d:0:0:0:0:0 with SMTP id s29csp1238998lfc; Wed, 1 Jun 2022 12:44:10 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxS8FlUBskozjHWWpByVNqZ3o1kEbsaoI3b1Cyf5wh3GNnz8l4/i41O/4wqKsnm80VIwLX7 X-Received: by 2002:a17:902:ef45:b0:156:1858:71fc with SMTP id e5-20020a170902ef4500b00156185871fcmr1020505plx.23.1654112650354; Wed, 01 Jun 2022 12:44:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654112650; cv=none; d=google.com; s=arc-20160816; b=mZcQ72E+DsGrDrjuUbi1LE2Fx/wMADB8A0c7KR+kLaDCEG9GFuyP0Ynwwof7XCsx8t vBiNjKDRqPbMuHX9yM0qCovg0CDBbMDMJ8BKI7C6yVPZXweAPz7rTnVKthuA+XBegaY5 eiuc0+/KtYSjOqdH7ox6QF/+X8ik2r4gnoEeNK0Ik16LrqI+K76CvB7mtrhJ60GZ4dUo DYiTUi3RHweh3RLDqqlb00BkUGXg19Mc/MlXrfICPG5hbU1Zk+0mNuK897lFX0eU2XSF g9oOxjFycn7aR0ulhxKedYlnqxIXBqFbyu9iAttg2h6Hq06W+RkeQ8AljsxwGtrjGQpp +5VQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:subject:cc:to:from :date:references:in-reply-to:message-id:mime-version:user-agent :feedback-id:dkim-signature:dkim-signature; bh=WsYR66AVfJUwEkAWrjHJbNRFD8IGXppyLsIWSRTR72U=; b=1I9f/eTI7GWRCbYp5FyHUWRl1vN7ajzB/Tzm4E/R+lsgtgPu46cgnhcIkTwb+iTZAX lWm70MOLThEpWnoYyCctuxIUQ9FrCXQj8h5JcNY3JnCzdV2sAF+0rU866iSA0DlPlh1O KxgfBF1VTaItik0Cwrx1h5bhGZKdopytkUOW9X8PySKSryX2Jmwku7SRHMXrEQ1XC2fU 0Sc+xAw7BziopEan5dw34rEYaWnMgkQCNSoQRYzQZ049ANXV03TT16eowdUUyGFu7C6M k9KmFpps4vT+YMRwPcUdXk9h5Kprf+520NIAdyYKlmMdGXD9XNutK6tX/FgGQTW0wTH7 1a0w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@flygoat.com header.s=fm2 header.b="It6O9db/"; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=eXoJr6dh; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=flygoat.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id t28-20020a62d15c000000b0051964d16f32si2863205pfl.180.2022.06.01.12.44.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Jun 2022 12:44:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@flygoat.com header.s=fm2 header.b="It6O9db/"; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=eXoJr6dh; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=flygoat.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 8BCE21D6868; Wed, 1 Jun 2022 12:09:36 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1352864AbiFAMbO (ORCPT + 99 others); Wed, 1 Jun 2022 08:31:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42630 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1352853AbiFAMbI (ORCPT ); Wed, 1 Jun 2022 08:31:08 -0400 Received: from out3-smtp.messagingengine.com (out3-smtp.messagingengine.com [66.111.4.27]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0C83D8D68B; Wed, 1 Jun 2022 05:30:46 -0700 (PDT) Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id 714C15C02F0; Wed, 1 Jun 2022 08:30:45 -0400 (EDT) Received: from imap44 ([10.202.2.94]) by compute4.internal (MEProxy); Wed, 01 Jun 2022 08:30:45 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=flygoat.com; h= cc:cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to; s=fm2; t=1654086645; x= 1654173045; bh=WsYR66AVfJUwEkAWrjHJbNRFD8IGXppyLsIWSRTR72U=; b=I t6O9db/Ms0Ye7foiXYhg5XCPlz6CYPO3G4eNyOfK0C9BeppEewtUhj21EYtnki+S jXEyoHar6E/LhMA5RNhUQTOfPh92mIUttmn36h7h/JSfDikkVnVMMGdb2itEQiTx 173s1kXC5OGyKJvC/WK7GY0BuqRyvGFjs5ZMPBrxwlOuNtpDXO3IhrZfuVZ+i6yy vqQzH4Yd+qUZ4MRctpieJ4QJH06SfGJ/Cg7WsTyjBji6Z/Jli4cTTRpnD2hPIta2 D1DkPITKyL0gnAGqp0hGzJjEqLGyRGoxuT1np0QlHCNMCtb/MX/o4+YSW9cFVtid nyhMe6tvcBji3XumTbqKQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t=1654086645; x= 1654173045; bh=WsYR66AVfJUwEkAWrjHJbNRFD8IGXppyLsIWSRTR72U=; b=e XoJr6dhwild5y/iHBz+kHUqbv9dJsrfphcPemU31/EEDa4LfEKq9ycUZYZ2ynkc1 F+qr1VHKcQxcQERzUZQSVSk49N88/PsPyssE3xO+YZmMikDuTl9FHmfJsp6E++5S pAJzg11pYRksELuGIpUbQdi/zvLt1OVCY2SBTU3pxDzO+9s5O/lpgeGig1aFhC8X Gfc+YKeCMcyPo2g2p4aQizIug5LCqkid98k6vv4roR06wF4BLGA8TSWDtAXa/gU3 gJjeidLMqDSzG18dKwkvA1FZCZb6w+xPvXH2gBmc62HaxtKm0ynMZC8xWCgCet+c f428S1W0LkeL8QGPeZYLg== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvfedrledtgdehudcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefofgggkfgjfhffhffvvefutgfgsehtqhertderreejnecuhfhrohhmpedflfhi rgiguhhnucgjrghnghdfuceojhhirgiguhhnrdihrghnghesfhhlhihgohgrthdrtghomh eqnecuggftrfgrthhtvghrnhepffevueejieethfdtkeehlefhvdevieejudetteffkeff ledtueefiedtteekudevnecuffhomhgrihhnpehgohhoghhlvghsohhurhgtvgdrtghomh dpvghnthhrhidrshgsnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghi lhhfrhhomhepjhhirgiguhhnrdihrghnghesfhhlhihgohgrthdrtghomh X-ME-Proxy: Feedback-ID: ifd894703:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id 3C25D36A006D; Wed, 1 Jun 2022 08:30:45 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.7.0-alpha0-591-gfe6c3a2700-fm-20220427.001-gfe6c3a27 Mime-Version: 1.0 Message-Id: <5578ad2c-1cae-40fe-b745-7fd326538fbc@www.fastmail.com> In-Reply-To: <20220601100005.2989022-16-chenhuacai@loongson.cn> References: <20220601100005.2989022-1-chenhuacai@loongson.cn> <20220601100005.2989022-16-chenhuacai@loongson.cn> Date: Wed, 01 Jun 2022 13:30:24 +0100 From: "Jiaxun Yang" To: "Huacai Chen" , "Arnd Bergmann" , "Andy Lutomirski" , "Thomas Gleixner" , "Peter Zijlstra" , "Andrew Morton" , "David Airlie" , "Jonathan Corbet" , "Linus Torvalds" Cc: linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, "Xuefeng Li" , "Yanteng Si" , "Huacai Chen" , "Guo Ren" , "Xuerui Wang" , "Stephen Rothwell" , "WANG Xuerui" Subject: Re: [PATCH V12 15/24] LoongArch: Add system call support Content-Type: text/plain;charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org =E5=9C=A82022=E5=B9=B46=E6=9C=881=E6=97=A5=E5=85=AD=E6=9C=88 =E4=B8=8A=E5= =8D=8810:59=EF=BC=8CHuacai Chen=E5=86=99=E9=81=93=EF=BC=9A > Add system call support and related uaccess.h for LoongArch. > > Q: Why keep _ARCH_WANT_SYS_CLONE definition while there is clone3: > A: The latest glibc release has some basic support for clone3 but it is > not complete. E.g., pthread_create() and spawni() have converted to > use clone3 but fork() will still use clone. Moreover, some seccomp > related applications can still not work perfectly with clone3. E.g., > Chromium sandbox cannot work at all and there is no solution for it, > which is more terrible than the fork() story [1]. > > [1] https://chromium-review.googlesource.com/c/chromium/src/+/2936184 > > Reviewed-by: WANG Xuerui > Signed-off-by: Huacai Chen Reviewed-by: Jiaxun Yang > --- > arch/loongarch/include/asm/seccomp.h | 20 ++ > arch/loongarch/include/asm/syscall.h | 74 +++++++ > arch/loongarch/include/asm/uaccess.h | 269 +++++++++++++++++++++++ > arch/loongarch/include/asm/unistd.h | 11 + > arch/loongarch/include/uapi/asm/unistd.h | 5 + > arch/loongarch/kernel/entry.S | 89 ++++++++ > arch/loongarch/kernel/syscall.c | 63 ++++++ > 7 files changed, 531 insertions(+) > create mode 100644 arch/loongarch/include/asm/seccomp.h > create mode 100644 arch/loongarch/include/asm/syscall.h > create mode 100644 arch/loongarch/include/asm/uaccess.h > create mode 100644 arch/loongarch/include/asm/unistd.h > create mode 100644 arch/loongarch/include/uapi/asm/unistd.h > create mode 100644 arch/loongarch/kernel/entry.S > create mode 100644 arch/loongarch/kernel/syscall.c > > diff --git a/arch/loongarch/include/asm/seccomp.h=20 > b/arch/loongarch/include/asm/seccomp.h > new file mode 100644 > index 000000000000..31d6ab42e43e > --- /dev/null > +++ b/arch/loongarch/include/asm/seccomp.h > @@ -0,0 +1,20 @@ > +/* SPDX-License-Identifier: GPL-2.0 */ > + > +#ifndef _ASM_SECCOMP_H > +#define _ASM_SECCOMP_H > + > +#include > + > +#include > + > +#ifdef CONFIG_32BIT > +# define SECCOMP_ARCH_NATIVE AUDIT_ARCH_LOONGARCH32 > +# define SECCOMP_ARCH_NATIVE_NR NR_syscalls > +# define SECCOMP_ARCH_NATIVE_NAME "loongarch32" > +#else > +# define SECCOMP_ARCH_NATIVE AUDIT_ARCH_LOONGARCH64 > +# define SECCOMP_ARCH_NATIVE_NR NR_syscalls > +# define SECCOMP_ARCH_NATIVE_NAME "loongarch64" > +#endif > + > +#endif /* _ASM_SECCOMP_H */ > diff --git a/arch/loongarch/include/asm/syscall.h=20 > b/arch/loongarch/include/asm/syscall.h > new file mode 100644 > index 000000000000..e286dc58476e > --- /dev/null > +++ b/arch/loongarch/include/asm/syscall.h > @@ -0,0 +1,74 @@ > +/* SPDX-License-Identifier: GPL-2.0+ */ > +/* > + * Author: Hanlu Li > + * Huacai Chen > + * > + * Copyright (C) 2020-2022 Loongson Technology Corporation Limited > + */ > + > +#ifndef __ASM_LOONGARCH_SYSCALL_H > +#define __ASM_LOONGARCH_SYSCALL_H > + > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > + > +extern void *sys_call_table[]; > + > +static inline long syscall_get_nr(struct task_struct *task, > + struct pt_regs *regs) > +{ > + return regs->regs[11]; > +} > + > +static inline void syscall_rollback(struct task_struct *task, > + struct pt_regs *regs) > +{ > + regs->regs[4] =3D regs->orig_a0; > +} > + > +static inline long syscall_get_error(struct task_struct *task, > + struct pt_regs *regs) > +{ > + unsigned long error =3D regs->regs[4]; > + > + return IS_ERR_VALUE(error) ? error : 0; > +} > + > +static inline long syscall_get_return_value(struct task_struct *task, > + struct pt_regs *regs) > +{ > + return regs->regs[4]; > +} > + > +static inline void syscall_set_return_value(struct task_struct *task, > + struct pt_regs *regs, > + int error, long val) > +{ > + regs->regs[4] =3D (long) error ? error : val; > +} > + > +static inline void syscall_get_arguments(struct task_struct *task, > + struct pt_regs *regs, > + unsigned long *args) > +{ > + args[0] =3D regs->orig_a0; > + memcpy(&args[1], ®s->regs[5], 5 * sizeof(long)); > +} > + > +static inline int syscall_get_arch(struct task_struct *task) > +{ > + return AUDIT_ARCH_LOONGARCH64; > +} > + > +static inline bool arch_syscall_is_vdso_sigreturn(struct pt_regs *reg= s) > +{ > + return false; > +} > + > +#endif /* __ASM_LOONGARCH_SYSCALL_H */ > diff --git a/arch/loongarch/include/asm/uaccess.h=20 > b/arch/loongarch/include/asm/uaccess.h > new file mode 100644 > index 000000000000..217c6a3727b1 > --- /dev/null > +++ b/arch/loongarch/include/asm/uaccess.h > @@ -0,0 +1,269 @@ > +/* SPDX-License-Identifier: GPL-2.0 */ > +/* > + * Copyright (C) 2020-2022 Loongson Technology Corporation Limited > + * > + * Derived from MIPS: > + * Copyright (C) 1996, 1997, 1998, 1999, 2000, 03, 04 by Ralf Baechle > + * Copyright (C) 1999, 2000 Silicon Graphics, Inc. > + * Copyright (C) 2007 Maciej W. Rozycki > + * Copyright (C) 2014, Imagination Technologies Ltd. > + */ > +#ifndef _ASM_UACCESS_H > +#define _ASM_UACCESS_H > + > +#include > +#include > +#include > +#include > +#include > +#include > + > +extern u64 __ua_limit; > + > +#define __UA_ADDR ".dword" > +#define __UA_LA "la.abs" > +#define __UA_LIMIT __ua_limit > + > +/* > + * get_user: - Get a simple variable from user space. > + * @x: Variable to store result. > + * @ptr: Source address, in user space. > + * > + * Context: User context only. This function may sleep if pagefaults=20 > are > + * enabled. > + * > + * This macro copies a single simple variable from user space to kern= el > + * space. It supports simple types like char and int, but not larger > + * data types like structures or arrays. > + * > + * @ptr must have pointer-to-simple-variable type, and the result of > + * dereferencing @ptr must be assignable to @x without a cast. > + * > + * Returns zero on success, or -EFAULT on error. > + * On error, the variable @x is set to zero. > + */ > +#define get_user(x, ptr) \ > +({ \ > + const __typeof__(*(ptr)) __user *__p =3D (ptr); \ > + \ > + might_fault(); \ > + access_ok(__p, sizeof(*__p)) ? __get_user((x), __p) : \ > + ((x) =3D 0, -EFAULT); \ > +}) > + > +/* > + * put_user: - Write a simple value into user space. > + * @x: Value to copy to user space. > + * @ptr: Destination address, in user space. > + * > + * Context: User context only. This function may sleep if pagefaults=20 > are > + * enabled. > + * > + * This macro copies a single simple value from kernel space to user > + * space. It supports simple types like char and int, but not larger > + * data types like structures or arrays. > + * > + * @ptr must have pointer-to-simple-variable type, and @x must be=20 > assignable > + * to the result of dereferencing @ptr. > + * > + * Returns zero on success, or -EFAULT on error. > + */ > +#define put_user(x, ptr) \ > +({ \ > + __typeof__(*(ptr)) __user *__p =3D (ptr); \ > + \ > + might_fault(); \ > + access_ok(__p, sizeof(*__p)) ? __put_user((x), __p) : -EFAULT; \ > +}) > + > +/* > + * __get_user: - Get a simple variable from user space, with less=20 > checking. > + * @x: Variable to store result. > + * @ptr: Source address, in user space. > + * > + * Context: User context only. This function may sleep if pagefaults=20 > are > + * enabled. > + * > + * This macro copies a single simple variable from user space to kern= el > + * space. It supports simple types like char and int, but not larger > + * data types like structures or arrays. > + * > + * @ptr must have pointer-to-simple-variable type, and the result of > + * dereferencing @ptr must be assignable to @x without a cast. > + * > + * Caller must check the pointer with access_ok() before calling this > + * function. > + * > + * Returns zero on success, or -EFAULT on error. > + * On error, the variable @x is set to zero. > + */ > +#define __get_user(x, ptr) \ > +({ \ > + int __gu_err =3D 0; \ > + \ > + __chk_user_ptr(ptr); \ > + __get_user_common((x), sizeof(*(ptr)), ptr); \ > + __gu_err; \ > +}) > + > +/* > + * __put_user: - Write a simple value into user space, with less=20 > checking. > + * @x: Value to copy to user space. > + * @ptr: Destination address, in user space. > + * > + * Context: User context only. This function may sleep if pagefaults=20 > are > + * enabled. > + * > + * This macro copies a single simple value from kernel space to user > + * space. It supports simple types like char and int, but not larger > + * data types like structures or arrays. > + * > + * @ptr must have pointer-to-simple-variable type, and @x must be=20 > assignable > + * to the result of dereferencing @ptr. > + * > + * Caller must check the pointer with access_ok() before calling this > + * function. > + * > + * Returns zero on success, or -EFAULT on error. > + */ > +#define __put_user(x, ptr) \ > +({ \ > + int __pu_err =3D 0; \ > + __typeof__(*(ptr)) __pu_val; \ > + \ > + __pu_val =3D (x); \ > + __chk_user_ptr(ptr); \ > + __put_user_common(ptr, sizeof(*(ptr))); \ > + __pu_err; \ > +}) > + > +struct __large_struct { unsigned long buf[100]; }; > +#define __m(x) (*(struct __large_struct __user *)(x)) > + > +#define __get_user_common(val, size, ptr) \ > +do { \ > + switch (size) { \ > + case 1: __get_data_asm(val, "ld.b", ptr); break; \ > + case 2: __get_data_asm(val, "ld.h", ptr); break; \ > + case 4: __get_data_asm(val, "ld.w", ptr); break; \ > + case 8: __get_data_asm(val, "ld.d", ptr); break; \ > + default: BUILD_BUG(); break; \ > + } \ > +} while (0) > + > +#define __get_kernel_common(val, size, ptr) __get_user_common(val,=20 > size, ptr) > + > +#define __get_data_asm(val, insn, ptr) \ > +{ \ > + long __gu_tmp; \ > + \ > + __asm__ __volatile__( \ > + "1: " insn " %1, %2 \n" \ > + "2: \n" \ > + " .section .fixup,\"ax\" \n" \ > + "3: li.w %0, %3 \n" \ > + " or %1, $r0, $r0 \n" \ > + " b 2b \n" \ > + " .previous \n" \ > + " .section __ex_table,\"a\" \n" \ > + " "__UA_ADDR "\t1b, 3b \n" \ > + " .previous \n" \ > + : "+r" (__gu_err), "=3Dr" (__gu_tmp) \ > + : "m" (__m(ptr)), "i" (-EFAULT)); \ > + \ > + (val) =3D (__typeof__(*(ptr))) __gu_tmp; \ > +} > + > +#define __put_user_common(ptr, size) \ > +do { \ > + switch (size) { \ > + case 1: __put_data_asm("st.b", ptr); break; \ > + case 2: __put_data_asm("st.h", ptr); break; \ > + case 4: __put_data_asm("st.w", ptr); break; \ > + case 8: __put_data_asm("st.d", ptr); break; \ > + default: BUILD_BUG(); break; \ > + } \ > +} while (0) > + > +#define __put_kernel_common(ptr, size) __put_user_common(ptr, size) > + > +#define __put_data_asm(insn, ptr) \ > +{ \ > + __asm__ __volatile__( \ > + "1: " insn " %z2, %1 # __put_user_asm\n" \ > + "2: \n" \ > + " .section .fixup,\"ax\" \n" \ > + "3: li.w %0, %3 \n" \ > + " b 2b \n" \ > + " .previous \n" \ > + " .section __ex_table,\"a\" \n" \ > + " " __UA_ADDR " 1b, 3b \n" \ > + " .previous \n" \ > + : "+r" (__pu_err), "=3Dm" (__m(ptr)) \ > + : "Jr" (__pu_val), "i" (-EFAULT)); \ > +} > + > +#define __get_kernel_nofault(dst, src, type, err_label) \ > +do { \ > + int __gu_err =3D 0; \ > + \ > + __get_kernel_common(*((type *)(dst)), sizeof(type), \ > + (__force type *)(src)); \ > + if (unlikely(__gu_err)) \ > + goto err_label; \ > +} while (0) > + > +#define __put_kernel_nofault(dst, src, type, err_label) \ > +do { \ > + type __pu_val; \ > + int __pu_err =3D 0; \ > + \ > + __pu_val =3D *(__force type *)(src); \ > + __put_kernel_common(((type *)(dst)), sizeof(type)); \ > + if (unlikely(__pu_err)) \ > + goto err_label; \ > +} while (0) > + > +extern unsigned long __copy_user(void *to, const void *from,=20 > __kernel_size_t n); > + > +static inline unsigned long __must_check > +raw_copy_from_user(void *to, const void __user *from, unsigned long n) > +{ > + return __copy_user(to, from, n); > +} > + > +static inline unsigned long __must_check > +raw_copy_to_user(void __user *to, const void *from, unsigned long n) > +{ > + return __copy_user(to, from, n); > +} > + > +#define INLINE_COPY_FROM_USER > +#define INLINE_COPY_TO_USER > + > +/* > + * __clear_user: - Zero a block of memory in user space, with less=20 > checking. > + * @addr: Destination address, in user space. > + * @size: Number of bytes to zero. > + * > + * Zero a block of memory in user space. Caller must check > + * the specified block with access_ok() before calling this function. > + * > + * Returns number of bytes that could not be cleared. > + * On success, this will be zero. > + */ > +extern unsigned long __clear_user(void __user *addr, __kernel_size_t=20 > size); > + > +#define clear_user(addr, n) \ > +({ \ > + void __user *__cl_addr =3D (addr); \ > + unsigned long __cl_size =3D (n); \ > + if (__cl_size && access_ok(__cl_addr, __cl_size)) \ > + __cl_size =3D __clear_user(__cl_addr, __cl_size); \ > + __cl_size; \ > +}) > + > +extern long strncpy_from_user(char *to, const char __user *from, long=20 > n); > +extern long strnlen_user(const char __user *str, long n); > + > +#endif /* _ASM_UACCESS_H */ > diff --git a/arch/loongarch/include/asm/unistd.h=20 > b/arch/loongarch/include/asm/unistd.h > new file mode 100644 > index 000000000000..cfddb0116a8c > --- /dev/null > +++ b/arch/loongarch/include/asm/unistd.h > @@ -0,0 +1,11 @@ > +/* SPDX-License-Identifier: GPL-2.0+ */ > +/* > + * Author: Hanlu Li > + * Huacai Chen > + * > + * Copyright (C) 2020-2022 Loongson Technology Corporation Limited > + */ > + > +#include > + > +#define NR_syscalls (__NR_syscalls) > diff --git a/arch/loongarch/include/uapi/asm/unistd.h=20 > b/arch/loongarch/include/uapi/asm/unistd.h > new file mode 100644 > index 000000000000..fcb668984f03 > --- /dev/null > +++ b/arch/loongarch/include/uapi/asm/unistd.h > @@ -0,0 +1,5 @@ > +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ > +#define __ARCH_WANT_SYS_CLONE > +#define __ARCH_WANT_SYS_CLONE3 > + > +#include > diff --git a/arch/loongarch/kernel/entry.S=20 > b/arch/loongarch/kernel/entry.S > new file mode 100644 > index 000000000000..d5b3dbcf5425 > --- /dev/null > +++ b/arch/loongarch/kernel/entry.S > @@ -0,0 +1,89 @@ > +/* SPDX-License-Identifier: GPL-2.0 */ > +/* > + * Copyright (C) 2020-2022 Loongson Technology Corporation Limited > + * > + * Derived from MIPS: > + * Copyright (C) 1994 - 2000, 2001, 2003 Ralf Baechle > + * Copyright (C) 1999, 2000 Silicon Graphics, Inc. > + * Copyright (C) 2001 MIPS Technologies, Inc. > + */ > + > +#include > +#include > +#include > +#include > +#include > +#include > + > + .text > + .cfi_sections .debug_frame > + .align 5 > +SYM_FUNC_START(handle_syscall) > + csrrd t0, PERCPU_BASE_KS > + la.abs t1, kernelsp > + add.d t1, t1, t0 > + move t2, sp > + ld.d sp, t1, 0 > + > + addi.d sp, sp, -PT_SIZE > + cfi_st t2, PT_R3 > + cfi_rel_offset sp, PT_R3 > + st.d zero, sp, PT_R0 > + csrrd t2, LOONGARCH_CSR_PRMD > + st.d t2, sp, PT_PRMD > + csrrd t2, LOONGARCH_CSR_CRMD > + st.d t2, sp, PT_CRMD > + csrrd t2, LOONGARCH_CSR_EUEN > + st.d t2, sp, PT_EUEN > + csrrd t2, LOONGARCH_CSR_ECFG > + st.d t2, sp, PT_ECFG > + csrrd t2, LOONGARCH_CSR_ESTAT > + st.d t2, sp, PT_ESTAT > + cfi_st ra, PT_R1 > + cfi_st a0, PT_R4 > + cfi_st a1, PT_R5 > + cfi_st a2, PT_R6 > + cfi_st a3, PT_R7 > + cfi_st a4, PT_R8 > + cfi_st a5, PT_R9 > + cfi_st a6, PT_R10 > + cfi_st a7, PT_R11 > + csrrd ra, LOONGARCH_CSR_ERA > + st.d ra, sp, PT_ERA > + cfi_rel_offset ra, PT_ERA > + > + cfi_st tp, PT_R2 > + cfi_st u0, PT_R21 > + cfi_st fp, PT_R22 > + > + SAVE_STATIC > + > + move u0, t0 > + li.d tp, ~_THREAD_MASK > + and tp, tp, sp > + > + move a0, sp > + bl do_syscall > + > + RESTORE_ALL_AND_RET > +SYM_FUNC_END(handle_syscall) > + > +SYM_CODE_START(ret_from_fork) > + bl schedule_tail # a0 =3D struct task_struct *prev > + move a0, sp > + bl syscall_exit_to_user_mode > + RESTORE_STATIC > + RESTORE_SOME > + RESTORE_SP_AND_RET > +SYM_CODE_END(ret_from_fork) > + > +SYM_CODE_START(ret_from_kernel_thread) > + bl schedule_tail # a0 =3D struct task_struct *prev > + move a0, s1 > + jirl ra, s0, 0 > + move a0, sp > + bl syscall_exit_to_user_mode > + RESTORE_STATIC > + RESTORE_SOME > + RESTORE_SP_AND_RET > +SYM_CODE_END(ret_from_kernel_thread) > diff --git a/arch/loongarch/kernel/syscall.c=20 > b/arch/loongarch/kernel/syscall.c > new file mode 100644 > index 000000000000..3fc4211db989 > --- /dev/null > +++ b/arch/loongarch/kernel/syscall.c > @@ -0,0 +1,63 @@ > +// SPDX-License-Identifier: GPL-2.0+ > +/* > + * Author: Hanlu Li > + * Huacai Chen > + * > + * Copyright (C) 2020-2022 Loongson Technology Corporation Limited > + */ > +#include > +#include > +#include > +#include > +#include > +#include > + > +#include > +#include > +#include > +#include > + > +#undef __SYSCALL > +#define __SYSCALL(nr, call) [nr] =3D (call), > + > +SYSCALL_DEFINE6(mmap, unsigned long, addr, unsigned long, len,=20 > unsigned long, > + prot, unsigned long, flags, unsigned long, fd, off_t, offset) > +{ > + if (offset & ~PAGE_MASK) > + return -EINVAL; > + > + return ksys_mmap_pgoff(addr, len, prot, flags, fd, offset >>=20 > PAGE_SHIFT); > +} > + > +void *sys_call_table[__NR_syscalls] =3D { > + [0 ... __NR_syscalls - 1] =3D sys_ni_syscall, > +#include > +}; > + > +typedef long (*sys_call_fn)(unsigned long, unsigned long, > + unsigned long, unsigned long, unsigned long, unsigned long); > + > +void noinstr do_syscall(struct pt_regs *regs) > +{ > + unsigned long nr; > + sys_call_fn syscall_fn; > + > + nr =3D regs->regs[11]; > + /* Set for syscall restarting */ > + if (nr < NR_syscalls) > + regs->regs[0] =3D nr + 1; > + > + regs->csr_era +=3D 4; > + regs->orig_a0 =3D regs->regs[4]; > + regs->regs[4] =3D -ENOSYS; > + > + nr =3D syscall_enter_from_user_mode(regs, nr); > + > + if (nr < NR_syscalls) { > + syscall_fn =3D sys_call_table[nr]; > + regs->regs[4] =3D syscall_fn(regs->orig_a0, regs->regs[5],=20 > regs->regs[6], > + regs->regs[7], regs->regs[8], regs->regs[9]); > + } > + > + syscall_exit_to_user_mode(regs); > +} > --=20 > 2.27.0 --=20 - Jiaxun