Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp6174387rwd; Wed, 24 May 2023 11:48:39 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7EyfeqRVTHFvDeeAuLmZN/SiFCClYanvKRNJVbwXLzEY4oU5YrDXKCiuE2TWmc643xm/Qc X-Received: by 2002:a05:6a00:b84:b0:644:ad29:fd5a with SMTP id g4-20020a056a000b8400b00644ad29fd5amr5169388pfj.21.1684954118856; Wed, 24 May 2023 11:48:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684954118; cv=none; d=google.com; s=arc-20160816; b=vtZ7KO9/zjPXtfBJeWT7nhKtQ/3nCLSm4omXjBKzaor7G6zj0SWTos6I0eD5XIQN9j +/IDP++cY9MW7m1fSMhMmGpSeplQSpiIO9fCD/wG5knRHiWUddEsDqTcxQJ6ftq81Nwy 0g04XEZ26wZKOnDzCzhDsCym+6Z3P8ro6cgMd8yUNs0g6CFIGolUzVwu/rDhBiTwlprU VjX61oAylp6tjQA18wwjDbNczEuW683b8ITkYep+DrTzoOEFU99gPnxU09r0SUR4/ExL HVnY1zFJvQBuJ+wbguM2jSJL4uwRvx6OkuGHvfQQsS/y7NSTJoro37j6OJQ3MzHEP2Fu V/mQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:feedback-id:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from; bh=VIq9v8HQZor/uvOv+jSiWtwx3Ioz5UP4LsoUZuAffVk=; b=KQT2F1S00qA6DBcjCUKONn/J9HbvQvKAnUk4DsD3hDk4t+MFY2YrBNlzCSzL99avbt nawJtuv2btnaOOQL1fg3w8pDcb7IU/dnh4O5LwCXrQdHqaT//vrkuc0tX9ucfPMydB43 l9f8Tlfngy30iQxUYBpbICzr7740JwntI028FvG1L5UPRTseSVigmfchW6Avm6afvF0u 0OARczH3N/HQTFZSc1fFePG5rnZ91/W+J/sSMgIUYU+Vhu76MaM3fjxZF9VKI2aQi9JG Cd0+Zjh0bqKXVv5Yi/ip9RxTV7uAQPbFsVUrmDHe0MTGZmFaQc5uStcap1l+2IBPeA2x CcCw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o17-20020a639211000000b0053f1387ecfcsi424904pgd.356.2023.05.24.11.48.26; Wed, 24 May 2023 11:48:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235341AbjEXSDp (ORCPT + 99 others); Wed, 24 May 2023 14:03:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40064 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229604AbjEXSDo (ORCPT ); Wed, 24 May 2023 14:03:44 -0400 Received: from bg4.exmail.qq.com (bg4.exmail.qq.com [43.155.67.158]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1C082B6; Wed, 24 May 2023 11:03:41 -0700 (PDT) X-QQ-mid: bizesmtp78t1684951416tb3pz334 Received: from linux-lab-host.localdomain ( [116.30.125.36]) by bizesmtp.qq.com (ESMTP) with id ; Thu, 25 May 2023 02:03:35 +0800 (CST) X-QQ-SSF: 01200000000000D0V000000A0000000 X-QQ-FEAT: znfcQSa1hKahN1m9MXihhsfaKEggHvIyA5/pTK2SaWBMBZDnB/qZVYB9hhaeW CtThDB38b2eIGVPxEtHTZ/aQx/m+LDrbyyqaUCEQjb3UrqG8e6vmoo+YN9ez5R3jAa+ctud hvf6+/GoNail8IsTWRntTm/VesMGwdeFQrjRCv9wgfeqGxpgfAXNzkEbm39kA+CQ13SCWiE iowUfkUtPUrUPEHv1pYfm9xxUrsV9UR/X3MRXjJ4GFhpHfjlERqt1laG9cY/MuPEBPIPVse G6RO3Z7tTbo4v1LfUw+MGPlMNWQaDMO/oUPriwjPaOHCEtWdRGHW+OdwyxBTwFSMSP/o1qi MlHUfX46tdBlGm7LLQfYMG6eXe10fkgOsXkruJqOw4zHZuAjYDpIHkDjpGh47G+lEvpB/RZ X-QQ-GoodBg: 0 X-BIZMAIL-ID: 13367086140274863961 From: Zhangjin Wu To: w@1wt.eu Cc: falcon@tinylab.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-riscv@lists.infradead.org, palmer@dabbelt.com, paul.walmsley@sifive.com, thomas@t-8ch.de Subject: [PATCH 13/13] tools/nolibc: sys_gettimeofday: riscv: use __NR_clock_gettime64 for rv32 Date: Thu, 25 May 2023 02:03:32 +0800 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-QQ-SENDSIZE: 520 Feedback-ID: bizesmtp:tinylab.org:qybglogicsvrsz:qybglogicsvrsz3a-3 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham 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 rv32 uses the generic include/uapi/asm-generic/unistd.h and it has no __NR_gettimeofday and __NR_clock_gettime after kernel commit d4c08b9776b3 ("riscv: Use latest system call ABI"), use __NR_clock_gettime64 instead. This code is based on src/time/gettimeofday.c of musl and sysdeps/unix/sysv/linux/clock_gettime.c of glibc. Both __NR_clock_gettime and __NR_clock_gettime64 are added for sys_gettimeofday() for they share most of the code. Notes: * Both tv and tz are not directly passed to kernel clock_gettime* syscalls, so, it isn't able to check the pointer automatically with the get_user/put_user helpers just like kernel gettimeofday syscall does. instead, we emulate (but not completely) such checks in our new __NR_clock_gettime* branch of nolibc. * kernel clock_gettime* syscalls can not get tz info, just like musl and glibc do, we set tz to zero to avoid a random number. Signed-off-by: Zhangjin Wu --- tools/include/nolibc/sys.h | 46 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h index 2642b380c6aa..ad38cc3856be 100644 --- a/tools/include/nolibc/sys.h +++ b/tools/include/nolibc/sys.h @@ -26,6 +26,7 @@ #include "arch.h" #include "errno.h" +#include "string.h" #include "types.h" @@ -51,6 +52,11 @@ * should not be placed here. */ +/* + * This is the first address past the end of the text segment (the program code). + */ + +extern char etext; /* * int brk(void *addr); @@ -554,7 +560,47 @@ long getpagesize(void) static __attribute__((unused)) int sys_gettimeofday(struct timeval *tv, struct timezone *tz) { +#ifdef __NR_gettimeofday return my_syscall2(__NR_gettimeofday, tv, tz); +#elif defined(__NR_clock_gettime) || defined(__NR_clock_gettime64) +#ifdef __NR_clock_gettime + struct timespec ts; +#else + struct timespec64 ts; +#define __NR_clock_gettime __NR_clock_gettime64 +#endif + int ret; + + /* make sure tv pointer is at least after code segment */ + if (tv != NULL && (char *)tv <= &etext) + return -EFAULT; + + /* set tz to zero to avoid random number */ + if (tz != NULL) { + if ((char *)tz > &etext) + memset(tz, 0, sizeof(struct timezone)); + else + return -EFAULT; + } + + if (tv == NULL) + return 0; + + ret = my_syscall2(__NR_clock_gettime, CLOCK_REALTIME, &ts); + if (ret) + return ret; + + tv->tv_sec = (time_t) ts.tv_sec; +#ifdef __NR_clock_gettime64 + if (tv->tv_sec != ts.tv_sec) + return -EOVERFLOW; +#endif + + tv->tv_usec = ts.tv_nsec / 1000; + return 0; +#else +#error None of __NR_gettimeofday, __NR_clock_gettime and __NR_clock_gettime64 defined, cannot implement sys_gettimeofday() +#endif } static __attribute__((unused)) -- 2.25.1