git: a647d4a4d15d - main - kboot: Implement stat(2) and fstat(2) system calls

From: Warner Losh <imp_at_FreeBSD.org>
Date: Fri, 15 Jul 2022 18:03:57 UTC
The branch main has been updated by imp:

URL: https://cgit.FreeBSD.org/src/commit/?id=a647d4a4d15df856d590c39c19d3973ec2e18825

commit a647d4a4d15df856d590c39c19d3973ec2e18825
Author:     Warner Losh <imp@FreeBSD.org>
AuthorDate: 2022-07-15 05:19:18 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2022-07-15 18:00:50 +0000

    kboot: Implement stat(2) and fstat(2) system calls
    
    Implement stat(2) and fstat(2) in terms of newfstatat and newfstat
    system calls respectively (assume we have a compat #define when
    there's no newfstat and just a regular fstat and do so for ppc).
    
    Snag struct kstat (the Linux kernel stat(2), et al interface) from musl
    and attribute properly.
    
    Sponsored by:           Netflix
---
 stand/kboot/arch/amd64/stat_arch.h      | 31 +++++++++++++++++++++++++++++++
 stand/kboot/arch/amd64/syscall_nr.h     |  2 ++
 stand/kboot/arch/powerpc64/stat_arch.h  | 27 +++++++++++++++++++++++++++
 stand/kboot/arch/powerpc64/syscall_nr.h |  3 +++
 stand/kboot/host_syscall.h              | 17 +++++++++++++++++
 stand/kboot/host_syscalls.c             | 12 ++++++++++++
 6 files changed, 92 insertions(+)

diff --git a/stand/kboot/arch/amd64/stat_arch.h b/stand/kboot/arch/amd64/stat_arch.h
new file mode 100644
index 000000000000..ae180322fab4
--- /dev/null
+++ b/stand/kboot/arch/amd64/stat_arch.h
@@ -0,0 +1,31 @@
+/*
+ * Copyright (c) 2005-2020 Rich Felker, et al.
+ *
+ * SPDX-Licnse-Identifier: MIT
+ *
+ * Note: From the musl project
+ */
+
+struct host_kstat {
+	host_dev_t st_dev;
+	host_ino_t st_ino;
+	host_nlink_t st_nlink;
+
+	host_mode_t st_mode;
+	host_uid_t st_uid;
+	host_gid_t st_gid;
+	unsigned int    __pad0;
+	host_dev_t st_rdev;
+	host_off_t st_size;
+	host_blksize_t st_blksize;
+	host_blkcnt_t st_blocks;
+
+	long st_atime_sec;
+	long st_atime_nsec;
+	long st_mtime_sec;
+	long st_mtime_nsec;
+	long st_ctime_sec;
+	long st_ctime_nsec;
+	long __pad_for_future[3];
+};
+
diff --git a/stand/kboot/arch/amd64/syscall_nr.h b/stand/kboot/arch/amd64/syscall_nr.h
index 506f85b3e731..c22c80ea7cb9 100644
--- a/stand/kboot/arch/amd64/syscall_nr.h
+++ b/stand/kboot/arch/amd64/syscall_nr.h
@@ -4,6 +4,8 @@
 #define SYS_kexec_load		246
 #define SYS_lseek		  8
 #define SYS_mmap		  9
+#define SYS_newfstat		  5
+#define SYS_newfstatat		262
 #define SYS_openat		257
 #define SYS_pselect6		270
 #define SYS_read		  0
diff --git a/stand/kboot/arch/powerpc64/stat_arch.h b/stand/kboot/arch/powerpc64/stat_arch.h
new file mode 100644
index 000000000000..f22787b45feb
--- /dev/null
+++ b/stand/kboot/arch/powerpc64/stat_arch.h
@@ -0,0 +1,27 @@
+/*
+ * Copyright (c) 2005-2020 Rich Felker, et al.
+ *
+ * SPDX-Licnse-Identifier: MIT
+ *
+ * Note: From the musl project
+ */
+
+struct host_kstat {
+	host_dev_t st_dev;
+	host_ino_t st_ino;
+	host_nlink_t st_nlink;
+	host_mode_t st_mode;
+	host_uid_t st_uid;
+	host_gid_t st_gid;
+	host_dev_t st_rdev;
+	host_off_t st_size;
+	host_blksize_t st_blksize;
+	host_blkcnt_t st_blocks;
+	long st_atime_sec;
+	long st_atime_nsec;
+	long st_mtime_sec;
+	long st_mtime_nsec;
+	long st_ctime_sec;
+	long st_ctime_nsec;
+	long __pad_for_future[3];
+};
diff --git a/stand/kboot/arch/powerpc64/syscall_nr.h b/stand/kboot/arch/powerpc64/syscall_nr.h
index 592f3d6a7631..aa94e6a82bb7 100644
--- a/stand/kboot/arch/powerpc64/syscall_nr.h
+++ b/stand/kboot/arch/powerpc64/syscall_nr.h
@@ -1,9 +1,12 @@
 #define SYS_close		  6
+#define SYS_fstat		108
 #define SYS_getdents		141
 #define SYS_gettimeofday	 78
 #define SYS_kexec_load		268
 #define SYS_llseek		140
 #define SYS_mmap		 90
+#define SYS_newfstat		SYS_fstat
+#define SYS_newfstatat		291
 #define SYS_openat		286
 #define SYS_pselect6		280
 #define SYS_read		  3
diff --git a/stand/kboot/host_syscall.h b/stand/kboot/host_syscall.h
index 24b966b34ed8..6ff20f7ba393 100644
--- a/stand/kboot/host_syscall.h
+++ b/stand/kboot/host_syscall.h
@@ -32,6 +32,21 @@
 
 long host_syscall(int number, ...);
 
+/*
+ * Sizes taken from musl's include/alltypes.h.in and expanded for LP64 hosts
+ */
+typedef uint64_t host_dev_t;
+typedef uint64_t host_ino_t;
+typedef int64_t host_nlink_t;
+typedef unsigned int host_mode_t;
+typedef unsigned int host_uid_t;
+typedef unsigned int host_gid_t;
+typedef int64_t host_off_t;
+typedef long host_blksize_t;
+typedef int64_t host_blkcnt_t;
+
+#include "stat_arch.h"
+
 /*
  * Constants for open, fcntl, etc
  *
@@ -75,6 +90,7 @@ struct host_timeval {
  * System Calls
  */
 int host_close(int fd);
+int host_fstat(int fd, struct host_kstat *sb);
 int host_getdents(int fd, void *dirp, int count);
 int host_gettimeofday(struct host_timeval *a, void *b);
 int host_kexec_load(uint32_t start, int nsegs, uint32_t segs, uint32_t flags);
@@ -85,6 +101,7 @@ ssize_t host_read(int fd, void *buf, size_t nbyte);
 int host_reboot(int, int, int, uintptr_t);
 int host_select(int nfds, long *readfds, long *writefds, long *exceptfds,
     struct host_timeval *timeout);
+int host_stat(const char *path, struct host_kstat *sb);
 int host_uname(struct old_utsname *);
 ssize_t host_write(int fd, const void *buf, size_t nbyte);
 
diff --git a/stand/kboot/host_syscalls.c b/stand/kboot/host_syscalls.c
index 3db066acb781..9cc6c6cd873e 100644
--- a/stand/kboot/host_syscalls.c
+++ b/stand/kboot/host_syscalls.c
@@ -13,6 +13,12 @@ host_close(int fd)
 	return host_syscall(SYS_close, fd);
 }
 
+int
+host_fstat(int fd, struct host_kstat *sb)
+{
+	return host_syscall(SYS_newfstat, fd, (uintptr_t)sb);
+}
+
 int
 host_getdents(int fd, void *dirp, int count)
 {
@@ -86,6 +92,12 @@ host_select(int nfds, long *readfds, long *writefds, long *exceptfds,
 	    (uintptr_t)exceptfds, (uintptr_t)&ts, (uintptr_t)NULL);
 }
 
+int
+host_stat(const char *path, struct host_kstat *sb)
+{
+	return host_syscall(SYS_newfstatat, HOST_AT_FDCWD, (uintptr_t)path, (uintptr_t)sb, 0);
+}
+
 int
 host_uname(struct old_utsname *uts)
 {