From fe6ef662d1a9cacbfe215474cbed33248673f299 Mon Sep 17 00:00:00 2001 From: Tiberiu Bucur Date: Tue, 20 Aug 2024 13:58:20 +0100 Subject: [PATCH 1/5] configs: Add --param to starter_se This commit adds the --param option to the starter_se configuration script for the Arm ISA. This is in order to support attaching remote debugger sessions. Change-Id: I2d8cc9f677f731948872003cca6066d1072ad570 Reviewed-by: Giacomo Travaglini --- configs/example/arm/starter_se.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/configs/example/arm/starter_se.py b/configs/example/arm/starter_se.py index 6d5b06b9ae..fbbb5ea94e 100644 --- a/configs/example/arm/starter_se.py +++ b/configs/example/arm/starter_se.py @@ -1,4 +1,4 @@ -# Copyright (c) 2016-2017, 2022-2023 Arm Limited +# Copyright (c) 2016-2017, 2022-2024 Arm Limited # All rights reserved. # # The license below extends only to copyright in the software and shall @@ -203,6 +203,19 @@ def main(): default="stdoutput", help="Destination for the Tarmac trace output. [Default: stdoutput]", ) + parser.add_argument( + "-P", + "--param", + action="append", + default=[], + help="Set a SimObject parameter relative to the root node. " + "An extended Python multi range slicing syntax can be used " + "for arrays. For example: " + "'system.cpu[0,1,3:8:2].max_insts_all_threads = 42' " + "sets max_insts_all_threads for cpus 0, 1, 3, 5 and 7 " + "Direct parameters of the root object are not accessible, " + "only parameters of its children.", + ) args = parser.parse_args() @@ -215,6 +228,7 @@ def main(): # Populate the root node with a system. A system corresponds to a # single node with shared memory. root.system = create(args) + root.apply_config(args.param) # Instantiate the C++ object hierarchy. After this point, # SimObjects can't be instantiated anymore. From 9b9b9ffbff72462155d95816d06be5505f0a37f6 Mon Sep 17 00:00:00 2001 From: Tiberiu Bucur Date: Tue, 20 Aug 2024 14:30:09 +0100 Subject: [PATCH 2/5] arch-arm: Ignore/implement several syscalls This commit either adds the implementation or the ignoreFunc to the corresponding entry in the syscall table for some Arm syscalls that were required in order to test the fix for the incorrect parameter size bug in se mode. Change-Id: Ifc6d87e2decf1bf96ecd81de6690f92927377bf8 Reviewed-by: Giacomo Travaglini --- src/arch/arm/linux/se_workload.cc | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/arch/arm/linux/se_workload.cc b/src/arch/arm/linux/se_workload.cc index c919d85edf..83d6788c91 100644 --- a/src/arch/arm/linux/se_workload.cc +++ b/src/arch/arm/linux/se_workload.cc @@ -1,5 +1,5 @@ /* - * Copyright 2010-2013, 2015, 2020 ARM Limited + * Copyright (c) 2010-2013, 2015, 2020, 2024 ARM Limited * * The license below extends only to copyright in the software and shall * not be construed as granting a license to any other intellectual @@ -185,7 +185,7 @@ class SyscallTable32 : public SyscallDescTable { base + 42, "pipe", pipePseudoFunc }, { base + 43, "times", timesFunc }, { base + 45, "brk", brkFunc }, - { base + 46, "setgid" }, + { base + 46, "setgid", ignoreFunc }, { base + 47, "getgid", getgidFunc }, { base + 49, "geteuid", geteuidFunc }, { base + 50, "getegid", getegidFunc }, @@ -293,7 +293,7 @@ class SyscallTable32 : public SyscallDescTable { base + 163, "mremap", mremapFunc }, // ARM-specific { base + 164, "setresuid" }, { base + 165, "getresuid" }, - { base + 168, "poll" }, + { base + 168, "poll", pollFunc }, { base + 169, "nfsservctl" }, { base + 170, "setresgid" }, { base + 171, "getresgid" }, @@ -316,7 +316,7 @@ class SyscallTable32 : public SyscallDescTable { base + 190, "vfork" }, { base + 191, "getrlimit", getrlimitFunc }, { base + 192, "mmap2", mmapFunc }, - { base + 193, "truncate64" }, + { base + 193, "truncate64", truncate64Func }, { base + 194, "ftruncate64", ftruncate64Func }, { base + 195, "stat64", stat64Func }, { base + 196, "lstat64", lstat64Func }, @@ -336,8 +336,8 @@ class SyscallTable32 : public SyscallDescTable { base + 210, "setresgid" }, { base + 211, "getresgid" }, { base + 212, "chown" }, - { base + 213, "setuid" }, - { base + 214, "setgid" }, + { base + 213, "setuid", ignoreFunc }, + { base + 214, "setgid", ignoreFunc }, { base + 215, "setfsuid" }, { base + 216, "setfsgid" }, #if defined(SYS_getdents64) @@ -488,7 +488,9 @@ class SyscallTable32 : public SyscallDescTable { base + 363, "sys_rt_tgsigqueueinfo" }, { base + 364, "sys_perf_event_open" }, { base + 365, "sys_recvmmsg" }, - { base + 384, "getrandom", getrandomFunc } + { base + 384, "getrandom", getrandomFunc }, + { base + 397, "sys_statx", ignoreFunc }, + { base + 398, "sys_rseq", ignoreFunc } }) {} }; @@ -544,7 +546,7 @@ class SyscallTable64 : public SyscallDescTable { base + 42, "nfsservctl" }, { base + 43, "statfs64" }, { base + 44, "fstatfs64" }, - { base + 45, "truncate64" }, + { base + 45, "truncate64", truncate64Func }, { base + 46, "ftruncate64", ftruncate64Func }, { base + 47, "fallocate", fallocateFunc }, { base + 48, "faccessat", faccessatFunc }, @@ -830,7 +832,7 @@ class SyscallTable64 : public SyscallDescTable #endif { base + 1066, "futimesat", futimesatFunc }, { base + 1067, "select" }, - { base + 1068, "poll" }, + { base + 1068, "poll", pollFunc }, { base + 1069, "epoll_wait" }, { base + 1070, "ustat" }, { base + 1071, "vfork" }, From f74260c55232ffeeb0e4318894b0c2a2f00993a7 Mon Sep 17 00:00:00 2001 From: Tiberiu Bucur Date: Tue, 20 Aug 2024 14:56:22 +0100 Subject: [PATCH 3/5] arch, sim-se: Fix off_t size mismatch bug Some system calls were using incorrect sizing for offset parametres, which was causing the ABI to pass wrong values due to size mismatches. One such syscall is lseek, which in the Arm syscall table was incorrectly marked as llseek, which does not exist in aarch64 Linux. In addition, the off_t alias for general Linux was changed from an unsigned to a signed type, to accurately reflect the behaviour in the real-life Linux operating system. Change-Id: Iada4b66a8933466c162ba9ec901dbdae73c73a18 Reviewed-by: Giacomo Travaglini --- src/arch/arm/linux/se_workload.cc | 6 +++--- src/arch/mips/linux/linux.hh | 12 +++++++++++ src/arch/mips/linux/se_workload.cc | 13 +++++++++++- src/arch/power/linux/se_workload.cc | 13 +++++++++++- src/arch/riscv/linux/se_workload.cc | 19 +++++++++++++---- src/arch/sparc/linux/linux.hh | 15 ++++++++++++++ src/arch/sparc/linux/syscalls.cc | 15 ++++++++++++-- src/arch/x86/linux/linux.hh | 5 +++-- src/arch/x86/linux/syscall_tbl32.cc | 13 +++++++++++- src/arch/x86/linux/syscall_tbl64.cc | 13 +++++++++++- src/kern/linux/linux.hh | 4 ++-- src/sim/syscall_emul.cc | 32 +++++++++++++---------------- src/sim/syscall_emul.hh | 32 +++++++++++++++++++++-------- 13 files changed, 149 insertions(+), 43 deletions(-) diff --git a/src/arch/arm/linux/se_workload.cc b/src/arch/arm/linux/se_workload.cc index 83d6788c91..ef9c9136dc 100644 --- a/src/arch/arm/linux/se_workload.cc +++ b/src/arch/arm/linux/se_workload.cc @@ -163,7 +163,7 @@ class SyscallTable32 : public SyscallDescTable { base + 14, "mknod", mknodFunc }, { base + 15, "chmod", chmodFunc }, { base + 16, "lchown", chownFunc }, - { base + 19, "lseek", lseekFunc }, + { base + 19, "lseek", lseekFunc }, { base + 20, "getpid", getpidFunc }, { base + 21, "mount" }, { base + 22, "umount" }, @@ -567,7 +567,7 @@ class SyscallTable64 : public SyscallDescTable #else { base + 61, "getdents64" }, #endif - { base + 62, "llseek", lseekFunc }, + { base + 62, "lseek", lseekFunc }, { base + 63, "read", readFunc }, { base + 64, "write", writeFunc }, { base + 65, "readv" }, @@ -817,7 +817,7 @@ class SyscallTable64 : public SyscallDescTable { base + 1054, "newfstatat" }, { base + 1055, "fstatfs" }, { base + 1056, "statfs" }, - { base + 1057, "lseek", lseekFunc }, + { base + 1057, "lseek", lseekFunc }, { base + 1058, "mmap", mmapFunc }, { base + 1059, "alarm" }, { base + 1060, "getpgrp" }, diff --git a/src/arch/mips/linux/linux.hh b/src/arch/mips/linux/linux.hh index 2dab558750..393cceaedd 100644 --- a/src/arch/mips/linux/linux.hh +++ b/src/arch/mips/linux/linux.hh @@ -1,4 +1,15 @@ /* + * Copyright (c) 2024 Arm Limited + * + * The license below extends only to copyright in the software and shall + * not be construed as granting a license to any other intellectual + * property including but not limited to intellectual property relating + * to a hardware implementation of the functionality of the software + * licensed hereunder. You may use the software subject to the license + * terms below provided that you ensure that this notice is replicated + * unmodified and in its entirety in all distributions of the software, + * modified or unmodified, in source code or in binary form. + * * Copyright (c) 2006 The Regents of The University of Michigan * All rights reserved. * @@ -192,6 +203,7 @@ class MipsLinux : public Linux, public OpenFlagTable uint32_t mem_unit; /* Memory unit size in bytes */ }; + typedef int32_t off_t; }; } // namespace gem5 diff --git a/src/arch/mips/linux/se_workload.cc b/src/arch/mips/linux/se_workload.cc index 0f5cd788e2..0cb59d2f26 100644 --- a/src/arch/mips/linux/se_workload.cc +++ b/src/arch/mips/linux/se_workload.cc @@ -1,4 +1,15 @@ /* + * Copyright (c) 2024 Arm Limited + * + * The license below extends only to copyright in the software and shall + * not be construed as granting a license to any other intellectual + * property including but not limited to intellectual property relating + * to a hardware implementation of the functionality of the software + * licensed hereunder. You may use the software subject to the license + * terms below provided that you ensure that this notice is replicated + * unmodified and in its entirety in all distributions of the software, + * modified or unmodified, in source code or in binary form. + * * Copyright 2005 The Regents of The University of Michigan * Copyright 2007 MIPS Technologies, Inc. * Copyright 2020 Google Inc. @@ -177,7 +188,7 @@ SyscallDescTable EmuLinux::syscallDescs = { { 4016, "lchown", chownFunc }, { 4017, "break", brkFunc }, { 4018, "unused#18" }, - { 4019, "lseek", lseekFunc }, + { 4019, "lseek", lseekFunc }, { 4020, "getpid", getpidFunc }, { 4021, "mount" }, { 4022, "umount" }, diff --git a/src/arch/power/linux/se_workload.cc b/src/arch/power/linux/se_workload.cc index c376b5f723..5304de8a15 100644 --- a/src/arch/power/linux/se_workload.cc +++ b/src/arch/power/linux/se_workload.cc @@ -1,4 +1,15 @@ /* + * Copyright (c) 2024 Arm Limited + * + * The license below extends only to copyright in the software and shall + * not be construed as granting a license to any other intellectual + * property including but not limited to intellectual property relating + * to a hardware implementation of the functionality of the software + * licensed hereunder. You may use the software subject to the license + * terms below provided that you ensure that this notice is replicated + * unmodified and in its entirety in all distributions of the software, + * modified or unmodified, in source code or in binary form. + * * Copyright 2003-2005 The Regents of The University of Michigan * Copyright 2007-2008 The Florida State University * Copyright 2009 The University of Edinburgh @@ -126,7 +137,7 @@ SyscallDescTable EmuLinux::syscallDescs = { { 16, "lchown", chownFunc }, { 17, "break", brkFunc }, //??? { 18, "unused#18" }, //??? - { 19, "lseek", lseekFunc }, + { 19, "lseek", lseekFunc }, { 20, "getpid", getpidFunc }, { 21, "mount" }, { 22, "umount" }, diff --git a/src/arch/riscv/linux/se_workload.cc b/src/arch/riscv/linux/se_workload.cc index 952fe7140f..acd7d7c094 100644 --- a/src/arch/riscv/linux/se_workload.cc +++ b/src/arch/riscv/linux/se_workload.cc @@ -1,4 +1,15 @@ /* + * Copyright (c) 2024 Arm Limited + * + * The license below extends only to copyright in the software and shall + * not be construed as granting a license to any other intellectual + * property including but not limited to intellectual property relating + * to a hardware implementation of the functionality of the software + * licensed hereunder. You may use the software subject to the license + * terms below provided that you ensure that this notice is replicated + * unmodified and in its entirety in all distributions of the software, + * modified or unmodified, in source code or in binary form. + * * Copyright 2005 The Regents of The University of Michigan * Copyright 2007 MIPS Technologies, Inc. * Copyright 2016 The University of Virginia @@ -190,7 +201,7 @@ SyscallDescTable EmuLinux::syscallDescs64 = { #else { 61, "getdents64" }, #endif - { 62, "lseek", lseekFunc }, + { 62, "lseek", lseekFunc }, { 63, "read", readFunc }, { 64, "write", writeFunc }, { 66, "writev", writevFunc }, @@ -432,7 +443,7 @@ SyscallDescTable EmuLinux::syscallDescs64 = { { 1054, "newfstatat", newfstatatFunc }, { 1055, "fstatfs", fstatfsFunc }, { 1056, "statfs", statfsFunc }, - { 1057, "lseek", lseekFunc }, + { 1057, "lseek", lseekFunc }, { 1058, "mmap", mmapFunc }, { 1059, "alarm" }, { 1060, "getpgrp", getpgrpFunc }, @@ -529,7 +540,7 @@ SyscallDescTable EmuLinux::syscallDescs32 = { #else { 61, "getdents64" }, #endif - { 62, "lseek", lseekFunc }, + { 62, "lseek", lseekFunc }, { 63, "read", readFunc }, { 64, "write", writeFunc }, { 66, "writev", writevFunc }, @@ -771,7 +782,7 @@ SyscallDescTable EmuLinux::syscallDescs32 = { { 1054, "newfstatat", newfstatatFunc }, { 1055, "fstatfs", fstatfsFunc }, { 1056, "statfs", statfsFunc }, - { 1057, "lseek", lseekFunc }, + { 1057, "lseek", lseekFunc }, { 1058, "mmap", mmapFunc }, { 1059, "alarm" }, { 1060, "getpgrp", getpgrpFunc }, diff --git a/src/arch/sparc/linux/linux.hh b/src/arch/sparc/linux/linux.hh index 5913ddb737..eae4651e99 100644 --- a/src/arch/sparc/linux/linux.hh +++ b/src/arch/sparc/linux/linux.hh @@ -1,4 +1,15 @@ /* + * Copyright (c) 2024 Arm Limited + * + * The license below extends only to copyright in the software and shall + * not be construed as granting a license to any other intellectual + * property including but not limited to intellectual property relating + * to a hardware implementation of the functionality of the software + * licensed hereunder. You may use the software subject to the license + * terms below provided that you ensure that this notice is replicated + * unmodified and in its entirety in all distributions of the software, + * modified or unmodified, in source code or in binary form. + * * Copyright (c) 2003-2005 The Regents of The University of Michigan * All rights reserved. * @@ -242,6 +253,8 @@ class SparcLinux : public Linux, public OpenFlagTable ptc->setReg(SparcISA::SyscallPseudoReturnReg, (RegVal)0); ctc->setReg(SparcISA::SyscallPseudoReturnReg, 1); } + + typedef int64_t off_t; }; class Sparc32Linux : public SparcLinux @@ -288,6 +301,8 @@ class Sparc32Linux : public SparcLinux uint32_t mem_unit; /* Memory unit size in bytes */ }; + typedef int32_t off_t; + /// Resource constants for getrlimit() (overide some generics). static const unsigned TGT_RLIMIT_NPROC = 7; static const unsigned TGT_RLIMIT_NOFILE = 6; diff --git a/src/arch/sparc/linux/syscalls.cc b/src/arch/sparc/linux/syscalls.cc index 093fc9ccd3..1b5be17fca 100644 --- a/src/arch/sparc/linux/syscalls.cc +++ b/src/arch/sparc/linux/syscalls.cc @@ -1,4 +1,15 @@ /* + * Copyright (c) 2024 Arm Limited + * + * The license below extends only to copyright in the software and shall + * not be construed as granting a license to any other intellectual + * property including but not limited to intellectual property relating + * to a hardware implementation of the functionality of the software + * licensed hereunder. You may use the software subject to the license + * terms below provided that you ensure that this notice is replicated + * unmodified and in its entirety in all distributions of the software, + * modified or unmodified, in source code or in binary form. + * * Copyright (c) 2003-2005 The Regents of The University of Michigan * All rights reserved. * @@ -104,7 +115,7 @@ SyscallDescTable EmuLinux::syscall32Descs = { { 16, "lchown" }, // 32 bit { 17, "brk", brkFunc }, { 18, "perfctr" }, // 32 bit - { 19, "lseek", lseekFunc }, // 32 bit + { 19, "lseek", lseekFunc }, // 32 bit { 20, "getpid", getpidFunc }, { 21, "capget" }, { 22, "capset" }, @@ -407,7 +418,7 @@ SyscallDescTable EmuLinux::syscallDescs = { { 16, "lchown" }, { 17, "brk", brkFunc }, { 18, "perfctr" }, - { 19, "lseek", lseekFunc }, + { 19, "lseek", lseekFunc }, { 20, "getpid", getpidFunc }, { 21, "capget" }, { 22, "capset" }, diff --git a/src/arch/x86/linux/linux.hh b/src/arch/x86/linux/linux.hh index 2b2031cc3c..caebbe8247 100644 --- a/src/arch/x86/linux/linux.hh +++ b/src/arch/x86/linux/linux.hh @@ -1,4 +1,5 @@ /* + * Copyright (c) 2024 Arm Limited * Copyright (c) 2007 The Hewlett-Packard Development Company * All rights reserved. * @@ -240,7 +241,7 @@ class X86Linux64 : public X86Linux, public OpenFlagTable //@{ /// Basic X86_64 Linux types typedef uint64_t size_t; - typedef uint64_t off_t; + typedef int64_t off_t; typedef int64_t time_t; typedef int64_t clock_t; //@} @@ -387,7 +388,7 @@ class X86Linux32 : public X86Linux, public OpenFlagTable //@{ /// Basic X86 Linux types typedef uint32_t size_t; - typedef uint32_t off_t; + typedef int32_t off_t; typedef int32_t time_t; typedef int32_t clock_t; //@} diff --git a/src/arch/x86/linux/syscall_tbl32.cc b/src/arch/x86/linux/syscall_tbl32.cc index 2de334cee3..02f12f1014 100644 --- a/src/arch/x86/linux/syscall_tbl32.cc +++ b/src/arch/x86/linux/syscall_tbl32.cc @@ -1,4 +1,15 @@ /* + * Copyright (c) 2024 Arm Limited + * + * The license below extends only to copyright in the software and shall + * not be construed as granting a license to any other intellectual + * property including but not limited to intellectual property relating + * to a hardware implementation of the functionality of the software + * licensed hereunder. You may use the software subject to the license + * terms below provided that you ensure that this notice is replicated + * unmodified and in its entirety in all distributions of the software, + * modified or unmodified, in source code or in binary form. + * * Copyright 2020 Google Inc. * * Redistribution and use in source and binary forms, with or without @@ -58,7 +69,7 @@ SyscallDescTable EmuLinux::syscallDescs32 = { { 16, "lchown" }, { 17, "break" }, { 18, "oldstat" }, - { 19, "lseek" }, + { 19, "lseek", lseekFunc }, { 20, "getpid", getpidFunc }, { 21, "mount" }, { 22, "umount" }, diff --git a/src/arch/x86/linux/syscall_tbl64.cc b/src/arch/x86/linux/syscall_tbl64.cc index 1b3dfaf020..c8bd28ced1 100644 --- a/src/arch/x86/linux/syscall_tbl64.cc +++ b/src/arch/x86/linux/syscall_tbl64.cc @@ -1,4 +1,15 @@ /* + * Copyright (c) 2024 Arm Limited + * + * The license below extends only to copyright in the software and shall + * not be construed as granting a license to any other intellectual + * property including but not limited to intellectual property relating + * to a hardware implementation of the functionality of the software + * licensed hereunder. You may use the software subject to the license + * terms below provided that you ensure that this notice is replicated + * unmodified and in its entirety in all distributions of the software, + * modified or unmodified, in source code or in binary form. + * * Copyright 2020 Google Inc. * * Redistribution and use in source and binary forms, with or without @@ -47,7 +58,7 @@ SyscallDescTable EmuLinux::syscallDescs64 = { { 5, "fstat", fstat64Func }, { 6, "lstat", lstat64Func }, { 7, "poll", pollFunc }, - { 8, "lseek", lseekFunc }, + { 8, "lseek", lseekFunc }, { 9, "mmap", mmapFunc }, { 10, "mprotect", ignoreFunc }, { 11, "munmap", munmapFunc }, diff --git a/src/kern/linux/linux.hh b/src/kern/linux/linux.hh index 5b7a20ff4a..9d1d5392de 100644 --- a/src/kern/linux/linux.hh +++ b/src/kern/linux/linux.hh @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Arm Limited + * Copyright (c) 2021, 2024 Arm Limited * All rights reserved * * The license below extends only to copyright in the software and shall @@ -66,7 +66,7 @@ class Linux : public OperatingSystem //@{ /// Basic Linux types. typedef uint64_t size_t; - typedef uint64_t off_t; + typedef int64_t off_t; typedef int64_t time_t; typedef int64_t clock_t; typedef uint32_t uid_t; diff --git a/src/sim/syscall_emul.cc b/src/sim/syscall_emul.cc index 9794a4835e..28fe4798ae 100644 --- a/src/sim/syscall_emul.cc +++ b/src/sim/syscall_emul.cc @@ -1,4 +1,16 @@ /* + * Copyright (c) 2024 Arm Limited + * + * The license below extends only to copyright in the software and + * shall not be construed as granting a license to any other + * intellectual property including but not limited to intellectual + * property relating to a hardware implementation of the + * functionality of the software licensed hereunder. You may use the + * software subject to the license terms below provided that you + * ensure that this notice is replicated unmodified and in its + * entirety in all distributions of the software, modified or + * unmodified, in source code or in binary form. + * * Copyright (c) 2003-2005 The Regents of The University of Michigan * All rights reserved. * @@ -292,26 +304,10 @@ closeFunc(SyscallDesc *desc, ThreadContext *tc, int tgt_fd) return p->fds->closeFDEntry(tgt_fd); } -SyscallReturn -lseekFunc(SyscallDesc *desc, ThreadContext *tc, - int tgt_fd, uint64_t offs, int whence) -{ - auto p = tc->getProcessPtr(); - - auto ffdp = std::dynamic_pointer_cast((*p->fds)[tgt_fd]); - if (!ffdp) - return -EBADF; - int sim_fd = ffdp->getSimFD(); - - off_t result = lseek(sim_fd, offs, whence); - - return (result == (off_t)-1) ? -errno : result; -} - SyscallReturn _llseekFunc(SyscallDesc *desc, ThreadContext *tc, - int tgt_fd, uint64_t offset_high, uint32_t offset_low, + int tgt_fd, uint32_t offset_high, uint32_t offset_low, VPtr<> result_ptr, int whence) { auto p = tc->getProcessPtr(); @@ -321,7 +317,7 @@ _llseekFunc(SyscallDesc *desc, ThreadContext *tc, return -EBADF; int sim_fd = ffdp->getSimFD(); - uint64_t offset = (offset_high << 32) | offset_low; + uint64_t offset = ((uint64_t) offset_high << 32) | offset_low; uint64_t result = lseek(sim_fd, offset, whence); result = htog(result, tc->getSystemPtr()->getGuestByteOrder()); diff --git a/src/sim/syscall_emul.hh b/src/sim/syscall_emul.hh index 4c822d14a1..37d4a598eb 100644 --- a/src/sim/syscall_emul.hh +++ b/src/sim/syscall_emul.hh @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012-2013, 2015, 2019-2021, 2023 Arm Limited + * Copyright (c) 2012-2013, 2015, 2019-2021, 2023-2024 Arm Limited * Copyright (c) 2015 Advanced Micro Devices, Inc. * All rights reserved * @@ -160,13 +160,9 @@ SyscallReturn brkFunc(SyscallDesc *desc, ThreadContext *tc, VPtr<> new_brk); /// Target close() handler. SyscallReturn closeFunc(SyscallDesc *desc, ThreadContext *tc, int tgt_fd); -/// Target lseek() handler. -SyscallReturn lseekFunc(SyscallDesc *desc, ThreadContext *tc, - int tgt_fd, uint64_t offs, int whence); - /// Target _llseek() handler. SyscallReturn _llseekFunc(SyscallDesc *desc, ThreadContext *tc, - int tgt_fd, uint64_t offset_high, + int tgt_fd, uint32_t offset_high, uint32_t offset_low, VPtr<> result_ptr, int whence); /// Target shutdown() handler. @@ -976,6 +972,24 @@ openFunc(SyscallDesc *desc, ThreadContext *tc, desc, tc, OS::TGT_AT_FDCWD, pathname, tgt_flags, mode); } +/// Target lseek() handler +template +SyscallReturn +lseekFunc(SyscallDesc *desc, ThreadContext *tc, + int tgt_fd, typename OS::off_t offs, int whence) +{ + auto p = tc->getProcessPtr(); + + auto ffdp = std::dynamic_pointer_cast((*p->fds)[tgt_fd]); + if (!ffdp) + return -EBADF; + int sim_fd = ffdp->getSimFD(); + + off_t result = lseek(sim_fd, offs, whence); + + return (result == (off_t)-1) ? -errno : result; +} + /// Target unlinkat() handler. template SyscallReturn @@ -2092,7 +2106,8 @@ mmapFunc(SyscallDesc *desc, ThreadContext *tc, template SyscallReturn pread64Func(SyscallDesc *desc, ThreadContext *tc, - int tgt_fd, VPtr<> bufPtr, int nbytes, int offset) + int tgt_fd, VPtr<> bufPtr, int nbytes, + typename OS::off_t offset) { auto p = tc->getProcessPtr(); @@ -2113,7 +2128,8 @@ pread64Func(SyscallDesc *desc, ThreadContext *tc, template SyscallReturn pwrite64Func(SyscallDesc *desc, ThreadContext *tc, - int tgt_fd, VPtr<> bufPtr, int nbytes, int offset) + int tgt_fd, VPtr<> bufPtr, int nbytes, + typename OS::off_t offset) { auto p = tc->getProcessPtr(); From 107e8f3d17de6729c2115cdf2ed6406bad783c04 Mon Sep 17 00:00:00 2001 From: Tiberiu Bucur Date: Tue, 20 Aug 2024 15:17:51 +0100 Subject: [PATCH 4/5] arch, sim-se: Fix size_t size mismatch bug Same as with the off_t, some syscalls were using incorrect size parametres in place of a guest-defined size_t. This commit changes the signature of said syscalls and adds the size_t typedef to the arch-dependent Linux OSs. Change-Id: Iece43814971a8e6275d25f6789e41528d241d1f4 Reviewed-by: Giacomo Travaglini --- src/arch/arm/linux/se_workload.cc | 12 ++++---- src/arch/mips/linux/linux.hh | 1 + src/arch/mips/linux/se_workload.cc | 2 +- src/arch/riscv/linux/se_workload.cc | 4 +-- src/arch/sparc/linux/linux.hh | 2 ++ src/arch/sparc/linux/syscalls.cc | 2 +- src/arch/x86/linux/syscall_tbl32.cc | 2 +- src/arch/x86/linux/syscall_tbl64.cc | 2 +- src/sim/syscall_emul.cc | 30 ------------------ src/sim/syscall_emul.hh | 47 +++++++++++++++++++++++------ 10 files changed, 53 insertions(+), 51 deletions(-) diff --git a/src/arch/arm/linux/se_workload.cc b/src/arch/arm/linux/se_workload.cc index ef9c9136dc..e2df5374a6 100644 --- a/src/arch/arm/linux/se_workload.cc +++ b/src/arch/arm/linux/se_workload.cc @@ -305,10 +305,10 @@ class SyscallTable32 : public SyscallDescTable { base + 177, "rt_sigtimedwait" }, { base + 178, "rt_sigqueueinfo", ignoreFunc }, { base + 179, "rt_sigsuspend" }, - { base + 180, "pread64" }, - { base + 181, "pwrite64" }, + { base + 180, "pread64", pread64Func }, + { base + 181, "pwrite64", pwrite64Func }, { base + 182, "chown" }, - { base + 183, "getcwd", getcwdFunc }, + { base + 183, "getcwd", getcwdFunc }, { base + 184, "capget" }, { base + 185, "capset" }, { base + 186, "sigaltstack" }, @@ -518,7 +518,7 @@ class SyscallTable64 : public SyscallDescTable { base + 14, "removexattr" }, { base + 15, "lremovexattr" }, { base + 16, "fremovexattr" }, - { base + 17, "getcwd", getcwdFunc }, + { base + 17, "getcwd", getcwdFunc }, { base + 18, "lookup_dcookie" }, { base + 19, "eventfd2" }, { base + 20, "epoll_create1" }, @@ -572,8 +572,8 @@ class SyscallTable64 : public SyscallDescTable { base + 64, "write", writeFunc }, { base + 65, "readv" }, { base + 66, "writev", writevFunc }, - { base + 67, "pread64" }, - { base + 68, "pwrite64" }, + { base + 67, "pread64", pread64Func }, + { base + 68, "pwrite64", pwrite64Func }, { base + 69, "preadv" }, { base + 70, "pwritev" }, { base + 71, "sendfile64" }, diff --git a/src/arch/mips/linux/linux.hh b/src/arch/mips/linux/linux.hh index 393cceaedd..543cdb05b0 100644 --- a/src/arch/mips/linux/linux.hh +++ b/src/arch/mips/linux/linux.hh @@ -203,6 +203,7 @@ class MipsLinux : public Linux, public OpenFlagTable uint32_t mem_unit; /* Memory unit size in bytes */ }; + typedef uint32_t size_t; typedef int32_t off_t; }; diff --git a/src/arch/mips/linux/se_workload.cc b/src/arch/mips/linux/se_workload.cc index 0cb59d2f26..a7aedbc0e6 100644 --- a/src/arch/mips/linux/se_workload.cc +++ b/src/arch/mips/linux/se_workload.cc @@ -372,7 +372,7 @@ SyscallDescTable EmuLinux::syscallDescs = { { 4200, "pread64" }, { 4201, "pwrite64" }, { 4202, "chown" }, - { 4203, "getcwd", getcwdFunc }, + { 4203, "getcwd", getcwdFunc }, { 4204, "capget" }, { 4205, "capset" }, { 4206, "sigalstack" }, diff --git a/src/arch/riscv/linux/se_workload.cc b/src/arch/riscv/linux/se_workload.cc index acd7d7c094..c1af16fb3b 100644 --- a/src/arch/riscv/linux/se_workload.cc +++ b/src/arch/riscv/linux/se_workload.cc @@ -152,7 +152,7 @@ SyscallDescTable EmuLinux::syscallDescs64 = { { 14, "removexattr" }, { 15, "lremovexattr" }, { 16, "fremovexattr" }, - { 17, "getcwd", getcwdFunc }, + { 17, "getcwd", getcwdFunc }, { 18, "lookup_dcookie" }, { 19, "eventfd2" }, { 20, "epoll_create1" }, @@ -491,7 +491,7 @@ SyscallDescTable EmuLinux::syscallDescs32 = { { 14, "removexattr" }, { 15, "lremovexattr" }, { 16, "fremovexattr" }, - { 17, "getcwd", getcwdFunc }, + { 17, "getcwd", getcwdFunc }, { 18, "lookup_dcookie" }, { 19, "eventfd2" }, { 20, "epoll_create1" }, diff --git a/src/arch/sparc/linux/linux.hh b/src/arch/sparc/linux/linux.hh index eae4651e99..e609031e8b 100644 --- a/src/arch/sparc/linux/linux.hh +++ b/src/arch/sparc/linux/linux.hh @@ -254,6 +254,7 @@ class SparcLinux : public Linux, public OpenFlagTable ctc->setReg(SparcISA::SyscallPseudoReturnReg, 1); } + typedef uint64_t size_t; typedef int64_t off_t; }; @@ -301,6 +302,7 @@ class Sparc32Linux : public SparcLinux uint32_t mem_unit; /* Memory unit size in bytes */ }; + typedef uint32_t size_t; typedef int32_t off_t; /// Resource constants for getrlimit() (overide some generics). diff --git a/src/arch/sparc/linux/syscalls.cc b/src/arch/sparc/linux/syscalls.cc index 1b5be17fca..2ff0fa1a1e 100644 --- a/src/arch/sparc/linux/syscalls.cc +++ b/src/arch/sparc/linux/syscalls.cc @@ -215,7 +215,7 @@ SyscallDescTable EmuLinux::syscall32Descs = { { 116, "gettimeofday", gettimeofdayFunc }, // 32 bit { 117, "getrusage" }, // 32 bit { 118, "getsockopt" }, - { 119, "getcwd", getcwdFunc }, + { 119, "getcwd", getcwdFunc }, { 120, "readv" }, { 121, "writev" }, { 122, "settimeofday" }, // 32 bit diff --git a/src/arch/x86/linux/syscall_tbl32.cc b/src/arch/x86/linux/syscall_tbl32.cc index 02f12f1014..bf5d03e7b5 100644 --- a/src/arch/x86/linux/syscall_tbl32.cc +++ b/src/arch/x86/linux/syscall_tbl32.cc @@ -237,7 +237,7 @@ SyscallDescTable EmuLinux::syscallDescs32 = { { 180, "pread64", pread64Func }, { 181, "pwrite64", pwrite64Func }, { 182, "chown" }, - { 183, "getcwd", getcwdFunc }, + { 183, "getcwd", getcwdFunc }, { 184, "capget" }, { 185, "capset" }, { 186, "sigaltstack" }, diff --git a/src/arch/x86/linux/syscall_tbl64.cc b/src/arch/x86/linux/syscall_tbl64.cc index c8bd28ced1..1c3eebd63d 100644 --- a/src/arch/x86/linux/syscall_tbl64.cc +++ b/src/arch/x86/linux/syscall_tbl64.cc @@ -133,7 +133,7 @@ SyscallDescTable EmuLinux::syscallDescs64 = { #else { 78, "getdents" }, #endif - { 79, "getcwd", getcwdFunc }, + { 79, "getcwd", getcwdFunc }, { 80, "chdir", chdirFunc }, { 81, "fchdir" }, { 82, "rename", renameFunc }, diff --git a/src/sim/syscall_emul.cc b/src/sim/syscall_emul.cc index 28fe4798ae..13f7cf09fb 100644 --- a/src/sim/syscall_emul.cc +++ b/src/sim/syscall_emul.cc @@ -344,36 +344,6 @@ gethostnameFunc(SyscallDesc *desc, ThreadContext *tc, return 0; } -SyscallReturn -getcwdFunc(SyscallDesc *desc, ThreadContext *tc, - VPtr<> buf_ptr, unsigned long size) -{ - int result = 0; - auto p = tc->getProcessPtr(); - BufferArg buf(buf_ptr, size); - - // Is current working directory defined? - std::string cwd = p->tgtCwd; - if (!cwd.empty()) { - if (cwd.length() >= size) { - // Buffer too small - return -ERANGE; - } - strncpy((char *)buf.bufferPtr(), cwd.c_str(), size); - result = cwd.length(); - } else { - if (getcwd((char *)buf.bufferPtr(), size)) { - result = strlen((char *)buf.bufferPtr()); - } else { - result = -1; - } - } - - buf.copyOut(SETranslatingPortProxy(tc)); - - return (result == -1) ? -errno : result; -} - SyscallReturn unlinkFunc(SyscallDesc *desc, ThreadContext *tc, VPtr<> pathname) { diff --git a/src/sim/syscall_emul.hh b/src/sim/syscall_emul.hh index 37d4a598eb..02d032bcd7 100644 --- a/src/sim/syscall_emul.hh +++ b/src/sim/syscall_emul.hh @@ -173,10 +173,6 @@ SyscallReturn shutdownFunc(SyscallDesc *desc, ThreadContext *tc, SyscallReturn gethostnameFunc(SyscallDesc *desc, ThreadContext *tc, VPtr<> buf_ptr, int name_len); -/// Target getcwd() handler. -SyscallReturn getcwdFunc(SyscallDesc *desc, ThreadContext *tc, - VPtr<> buf_ptr, unsigned long size); - /// Target unlink() handler. SyscallReturn unlinkFunc(SyscallDesc *desc, ThreadContext *tc, VPtr<> pathname); @@ -972,6 +968,38 @@ openFunc(SyscallDesc *desc, ThreadContext *tc, desc, tc, OS::TGT_AT_FDCWD, pathname, tgt_flags, mode); } +/// Target getcwd() handler +template +SyscallReturn +getcwdFunc(SyscallDesc *desc, ThreadContext *tc, + VPtr<> buf_ptr, typename OS::size_t size) +{ + int result = 0; + auto p = tc->getProcessPtr(); + BufferArg buf(buf_ptr, size); + + // Is current working directory defined? + std::string cwd = p->tgtCwd; + if (!cwd.empty()) { + if (cwd.length() >= size) { + // Buffer too small + return -ERANGE; + } + strncpy((char *)buf.bufferPtr(), cwd.c_str(), size); + result = cwd.length(); + } else { + if (getcwd((char *)buf.bufferPtr(), size)) { + result = strlen((char *)buf.bufferPtr()); + } else { + result = -1; + } + } + + buf.copyOut(SETranslatingPortProxy(tc)); + + return (result == -1) ? -errno : result; +} + /// Target lseek() handler template SyscallReturn @@ -1341,7 +1369,8 @@ fchmodFunc(SyscallDesc *desc, ThreadContext *tc, int tgt_fd, uint32_t mode) template SyscallReturn mremapFunc(SyscallDesc *desc, ThreadContext *tc, - VPtr<> start, uint64_t old_length, uint64_t new_length, uint64_t flags, + VPtr<> start, typename OS::size_t old_length, + typename OS::size_t new_length, int flags, guest_abi::VarArgs varargs) { auto p = tc->getProcessPtr(); @@ -2106,7 +2135,7 @@ mmapFunc(SyscallDesc *desc, ThreadContext *tc, template SyscallReturn pread64Func(SyscallDesc *desc, ThreadContext *tc, - int tgt_fd, VPtr<> bufPtr, int nbytes, + int tgt_fd, VPtr<> bufPtr, typename OS::size_t nbytes, typename OS::off_t offset) { auto p = tc->getProcessPtr(); @@ -2128,7 +2157,7 @@ pread64Func(SyscallDesc *desc, ThreadContext *tc, template SyscallReturn pwrite64Func(SyscallDesc *desc, ThreadContext *tc, - int tgt_fd, VPtr<> bufPtr, int nbytes, + int tgt_fd, VPtr<> bufPtr, typename OS::size_t nbytes, typename OS::off_t offset) { auto p = tc->getProcessPtr(); @@ -2762,7 +2791,7 @@ selectFunc(SyscallDesc *desc, ThreadContext *tc, int nfds, template SyscallReturn readFunc(SyscallDesc *desc, ThreadContext *tc, - int tgt_fd, VPtr<> buf_ptr, int nbytes) + int tgt_fd, VPtr<> buf_ptr, typename OS::size_t nbytes) { auto p = tc->getProcessPtr(); @@ -2790,7 +2819,7 @@ readFunc(SyscallDesc *desc, ThreadContext *tc, template SyscallReturn writeFunc(SyscallDesc *desc, ThreadContext *tc, - int tgt_fd, VPtr<> buf_ptr, int nbytes) + int tgt_fd, VPtr<> buf_ptr, typename OS::size_t nbytes) { auto p = tc->getProcessPtr(); From 88de81f167ed28980de1d5bdbff1aa52b7515ae1 Mon Sep 17 00:00:00 2001 From: Tiberiu Bucur Date: Tue, 20 Aug 2024 15:27:58 +0100 Subject: [PATCH 5/5] arch-arm, sim-se: Fix VPtr bug Some syscalls were incorrectly using 64 bit integers instead of VPtr's guest pointers, causing parameter value corruption. This commit addresses this issue. Change-Id: If9e27a7c776b802dda18979d1a83a76c23557359 Reviewed-by: Giacomo Travaglini --- src/arch/arm/linux/se_workload.cc | 4 ++-- src/sim/syscall_emul.cc | 2 +- src/sim/syscall_emul.hh | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/arch/arm/linux/se_workload.cc b/src/arch/arm/linux/se_workload.cc index e2df5374a6..04d2382661 100644 --- a/src/arch/arm/linux/se_workload.cc +++ b/src/arch/arm/linux/se_workload.cc @@ -272,7 +272,7 @@ class SyscallTable32 : public SyscallDescTable { base + 142, "newselect" }, { base + 143, "flock" }, { base + 144, "msync" }, - { base + 145, "readv" }, + { base + 145, "readv", readvFunc }, { base + 146, "writev", writevFunc }, { base + 147, "getsid" }, { base + 148, "fdatasync" }, @@ -570,7 +570,7 @@ class SyscallTable64 : public SyscallDescTable { base + 62, "lseek", lseekFunc }, { base + 63, "read", readFunc }, { base + 64, "write", writeFunc }, - { base + 65, "readv" }, + { base + 65, "readv", readvFunc }, { base + 66, "writev", writevFunc }, { base + 67, "pread64", pread64Func }, { base + 68, "pwrite64", pwrite64Func }, diff --git a/src/sim/syscall_emul.cc b/src/sim/syscall_emul.cc index 13f7cf09fb..909b566b96 100644 --- a/src/sim/syscall_emul.cc +++ b/src/sim/syscall_emul.cc @@ -289,7 +289,7 @@ brkFunc(SyscallDesc *desc, ThreadContext *tc, VPtr<> new_brk) } SyscallReturn -setTidAddressFunc(SyscallDesc *desc, ThreadContext *tc, uint64_t tidPtr) +setTidAddressFunc(SyscallDesc *desc, ThreadContext *tc, VPtr<> tidPtr) { auto process = tc->getProcessPtr(); diff --git a/src/sim/syscall_emul.hh b/src/sim/syscall_emul.hh index 02d032bcd7..b72d12025b 100644 --- a/src/sim/syscall_emul.hh +++ b/src/sim/syscall_emul.hh @@ -149,7 +149,7 @@ SyscallReturn exitGroupFunc(SyscallDesc *desc, ThreadContext *tc, int status); /// Target set_tid_address() handler. SyscallReturn setTidAddressFunc(SyscallDesc *desc, ThreadContext *tc, - uint64_t tidPtr); + VPtr<> tidPtr); /// Target getpagesize() handler. SyscallReturn getpagesizeFunc(SyscallDesc *desc, ThreadContext *tc); @@ -1920,7 +1920,7 @@ fstatfsFunc(SyscallDesc *desc, ThreadContext *tc, template SyscallReturn readvFunc(SyscallDesc *desc, ThreadContext *tc, - int tgt_fd, uint64_t tiov_base, + int tgt_fd, VPtr<> tiov_base, typename OS::size_t count) { auto p = tc->getProcessPtr(); @@ -1958,7 +1958,7 @@ readvFunc(SyscallDesc *desc, ThreadContext *tc, template SyscallReturn writevFunc(SyscallDesc *desc, ThreadContext *tc, - int tgt_fd, uint64_t tiov_base, + int tgt_fd, VPtr<> tiov_base, typename OS::size_t count) { auto p = tc->getProcessPtr();