Skip to contentSkip to conversation

Create os-time.c and add t2h_freebsd_timeval

PATCH New Ajeets6 1 participant 7 messages Apr 22, 2023 Arm/Amlogic Meson SoC support

lore
Share & tools
lore

Ajeets6

author
#0 lore raw

PATCH 1/7 Create os-time.c and add t2h_freebsd_timeval

From: Stacey Son <[email protected]>

os-time.c contains various functions to convert FreeBSD-specific time structure between host and guest formats

---
 bsd-user/freebsd/os-time.c | 41 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)
 create mode 100644 bsd-user/freebsd/os-time.c
bsd-user/freebsd/os-time.c new file +41 -0
new file mode 100644
index 0000000000..ec9f59ded7
@@ -0,0 +1,41 @@
+/*
+ *  FreeBSD time related system call helpers
+ *
+ *  Copyright (c) 2013-15 Stacey D. Son
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+/*
+ * FreeBSD time conversion functions
+ */
+#include <errno.h>
+#include <time.h>
+#include <sys/types.h>
+#include "qemu.h"
+
+
+abi_long t2h_freebsd_timeval(struct timeval *tv, abi_ulong target_tv_addr)
+{
+    struct target_freebsd_timeval *target_tv;
+
+    if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 0)) {
+        return -TARGET_EFAULT;
+    }
+    __get_user(tv->tv_sec, &target_tv->tv_sec);
+    __get_user(tv->tv_usec, &target_tv->tv_usec);
+    unlock_user_struct(target_tv, target_tv_addr, 1);
+
+    return 0;
+}
\ No newline at end of file
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH 2/7 Create os-time.h and modify os-time.c

From: Stacey Son <[email protected]>

+add nanosleep(2) in os-time.h
+add t2h_freebsd_timeval and h2t_freebsd_timeval time conversion functions
-remove t2h_freebsd_timeval in os-time.c

---
 bsd-user/freebsd/os-time.c | 29 ++++++++++++++++++-------
 bsd-user/freebsd/os-time.h | 44 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 65 insertions(+), 8 deletions(-)
 create mode 100644 bsd-user/freebsd/os-time.h
bsd-user/freebsd/os-time.c +21 -8
index ec9f59ded7..e71eed6519 100644
@@ -20,22 +20,35 @@
 /*
  * FreeBSD time conversion functions
  */
-#include <errno.h>
+#include "qemu/osdep.h"
 #include <time.h>
-#include <sys/types.h>
 #include "qemu.h"
 
 
-abi_long t2h_freebsd_timeval(struct timeval *tv, abi_ulong target_tv_addr)
+abi_long t2h_freebsd_timespec(struct timespec *ts, abi_ulong target_ts_addr)
 {
-    struct target_freebsd_timeval *target_tv;
+    struct target_freebsd_timespec *target_ts;
 
-    if (!lock_user_struct(VERIFY_READ, target_tv, target_tv_addr, 0)) {
+    if (!lock_user_struct(VERIFY_READ, target_ts, target_ts_addr, 0)) {
         return -TARGET_EFAULT;
     }
-    __get_user(tv->tv_sec, &target_tv->tv_sec);
-    __get_user(tv->tv_usec, &target_tv->tv_usec);
-    unlock_user_struct(target_tv, target_tv_addr, 1);
+    __get_user(ts->tv_sec, &target_ts->tv_sec);
+    __get_user(ts->tv_nsec, &target_ts->tv_nsec);
+    unlock_user_struct(target_ts, target_ts_addr, 1);
+
+    return 0;
+}
+
+abi_long h2t_freebsd_timespec(abi_ulong target_ts_addr, struct timespec *ts)
+{
+    struct target_freebsd_timespec *target_ts;
+
+    if (!lock_user_struct(VERIFY_WRITE, target_ts, target_ts_addr, 0)) {
+        return -TARGET_EFAULT;
+    }
+    __put_user(ts->tv_sec, &target_ts->tv_sec);
+    __put_user(ts->tv_nsec, &target_ts->tv_nsec);
+    unlock_user_struct(target_ts, target_ts_addr, 1);
 
     return 0;
 }
\ No newline at end of file
bsd-user/freebsd/os-time.h new file +44 -0

44 lines not on this page · raw message

Signature · 1 line
2.34.1
author
#2 lore raw

PATCH 3/7 Add clock_nanosleep

In reply to Ajeets6

From: Kyle Evans <[email protected]>

+Add clock_nanosleep(2)
Provide sleep interval in nanoseconds and allows to choose which clock to measure it against.

---
 bsd-user/freebsd/os-time.h | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
bsd-user/freebsd/os-time.h +21 -0
index 18c9e1dd12..29d2c8d02a 100644
@@ -42,3 +42,24 @@ static inline abi_long do_freebsd_nanosleep(abi_long arg1, abi_long arg2)
 
     return ret;
 }
+/* clock_nanosleep(2) */
+static inline abi_long do_freebsd_clock_nanosleep(abi_long arg1, abi_long arg2,
+    abi_long arg3, abi_long arg4)
+{
+    struct timespec req, rem;
+    abi_long ret;
+    int clkid, flags;
+
+    clkid = arg1;
+    /* XXX Translate? */
+    flags = arg2;
+    ret = t2h_freebsd_timespec(&req, arg3);
+    if (!is_error(ret)) {
+        ret = get_errno(safe_clock_nanosleep(clkid, flags, &req, &rem));
+        if (ret == -TARGET_EINTR && arg4) {
+            h2t_freebsd_timespec(arg4, &rem);
+        }
+    }
+
+    return ret;
+}
Signature · 1 line
2.34.1
author
#3 lore raw

PATCH 4/7 Added clock_gettime(2) and clock_getres(2)

In reply to Ajeets6

From: Stacey Son <[email protected]>

+Added clock_gettime(2) which gets the time
+Added clock_getres(2) which finds the resoultion of the specidfied clock

---
 bsd-user/freebsd/os-time.h | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
bsd-user/freebsd/os-time.h +32 -0
index 29d2c8d02a..f76744e808 100644
@@ -63,3 +63,35 @@ static inline abi_long do_freebsd_clock_nanosleep(abi_long arg1, abi_long arg2,
 
     return ret;
 }
+
+/* clock_gettime(2) */
+static inline abi_long do_freebsd_clock_gettime(abi_long arg1, abi_long arg2)
+{
+    abi_long ret;
+    struct timespec ts;
+
+    ret = get_errno(clock_gettime(arg1, &ts));
+    if (!is_error(ret)) {
+        if (h2t_freebsd_timespec(arg2, &ts)) {
+            return -TARGET_EFAULT;
+        }
+    }
+
+    return ret;
+}
+
+/* clock_getres(2) */
+static inline abi_long do_freebsd_clock_getres(abi_long arg1, abi_long arg2)
+{
+    abi_long ret;
+    struct timespec ts;
+
+    ret = get_errno(clock_getres(arg1, &ts));
+    if (!is_error(ret)) {
+        if (h2t_freebsd_timespec(arg2, &ts)) {
+            return -TARGET_EFAULT;
+        }
+    }
+
+    return ret;
+}
\ No newline at end of file
Signature · 1 line
2.34.1
author
#4 lore raw

PATCH 5/7 Created qemu-os.h for function prototype

In reply to Ajeets6

From: Stacey Son <[email protected]>

+Added t2h_freebsd_timespec and h2t_freebsd_timespec function protype in qemu-is.h
+included qemu-os.h in os-time.c and os-time.h

---
 bsd-user/freebsd/os-time.c |  2 ++
 bsd-user/freebsd/os-time.h |  5 ++++-
 bsd-user/freebsd/qemu-os.h | 30 ++++++++++++++++++++++++++++++
 3 files changed, 36 insertions(+), 1 deletion(-)
 create mode 100644 bsd-user/freebsd/qemu-os.h
bsd-user/freebsd/os-time.c +2 -0
index e71eed6519..5c88e1f13d 100644
@@ -23,6 +23,8 @@
 #include "qemu/osdep.h"
 #include <time.h>
 #include "qemu.h"
+#include "qemu-os.h"
+
 
 
 abi_long t2h_freebsd_timespec(struct timespec *ts, abi_ulong target_ts_addr)
bsd-user/freebsd/os-time.h +4 -1
index f76744e808..bd995c8a7b 100644
@@ -22,6 +22,7 @@
 
 
 #include "qemu.h"
+#include "qemu-os.h"
 
 
 
@@ -94,4 +95,6 @@ static inline abi_long do_freebsd_clock_getres(abi_long arg1, abi_long arg2)
     }
 
     return ret;
-}
\ No newline at end of file
+}
+
+#endif /* FREEBSD_OS_TIME_H */
bsd-user/freebsd/qemu-os.h new file +30 -0
new file mode 100644
index 0000000000..0c502ff0e5
@@ -0,0 +1,30 @@
+/*
+ *  FreeBSD conversion extern declarations
+ *
+ *  Copyright (c) 2013 Stacey D. Son
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef QEMU_OS_H
+#define QEMU_OS_H
+
+#include <sys/timex.h>
+
+/* os-time.c */
+abi_long t2h_freebsd_timespec(struct timespec *ts, abi_ulong target_ts_addr);
+abi_long h2t_freebsd_timespec(abi_ulong target_ts_addr, struct timespec *ts);
+
+
+#endif /* QEMU_OS_H */
\ No newline at end of file
Signature · 1 line
2.34.1
author
#5 lore raw

PATCH 6/7 Added case staements for time functions

In reply to Ajeets6

From: Warner Losh <[email protected]>

+Added cases for nanosleep(2),clock_nanosleep(2),clock_gettime(2) and clock_getres(2)
+Updated meson.build

---
 bsd-user/freebsd/meson.build  |  1 +
 bsd-user/freebsd/os-syscall.c | 20 ++++++++++++++++++++
 2 files changed, 21 insertions(+)
bsd-user/freebsd/meson.build +1 -0
index f87c788e84..2eb0ed4d96 100644
@@ -1,4 +1,5 @@
 bsd_user_ss.add(files(
   'os-sys.c',
   'os-syscall.c',
+  'os-time.c',
 ))
bsd-user/freebsd/os-syscall.c +20 -0
index c8f998ecec..8fd6eb05cb 100644
@@ -46,6 +46,8 @@
 
 #include "bsd-file.h"
 #include "bsd-proc.h"
+/* *BSD dependent syscall shims */
+#include "os-time.h"
 
 /* I/O */
 safe_syscall3(int, open, const char *, path, int, flags, mode_t, mode);
@@ -507,6 +509,24 @@ static abi_long freebsd_syscall(void *cpu_env, int num, abi_long arg1,
     case TARGET_FREEBSD_NR_sysarch: /* sysarch(2) */
         ret = do_freebsd_sysarch(cpu_env, arg1, arg2);
         break;
+     /*
+         * time related system calls.
+         */
+    case TARGET_FREEBSD_NR_nanosleep: /* nanosleep(2) */
+        ret = do_freebsd_nanosleep(arg1, arg2);
+        break;
+
+    case TARGET_FREEBSD_NR_clock_nanosleep: /* clock_nanosleep(2) */
+        ret = do_freebsd_clock_nanosleep(arg1, arg2, arg3, arg4);
+        break;
+
+    case TARGET_FREEBSD_NR_clock_gettime: /* clock_gettime(2) */
+        ret = do_freebsd_clock_gettime(arg1, arg2);
+        break;
+
+    case TARGET_FREEBSD_NR_clock_getres: /* clock_getres(2) */
+        ret = do_freebsd_clock_getres(arg1, arg2);
+        break;
 
     default:
         qemu_log_mask(LOG_UNIMP, "Unsupported syscall: %d\n", num);
Signature · 1 line
2.34.1
author
#6 lore raw

PATCH 7/7 Added safe_syscalls for time functions.

In reply to Ajeets6

From: Warner Losh <[email protected]>

+Added safe_syscalls

---
 bsd-user/freebsd/os-syscall.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
bsd-user/freebsd/os-syscall.c +10 -0
index 8fd6eb05cb..3d56aff0fd 100644
@@ -49,6 +49,16 @@
 /* *BSD dependent syscall shims */
 #include "os-time.h"
 
+/* used in os-time */
+safe_syscall2(int, nanosleep, const struct timespec *, rqtp, struct timespec *,
+    rmtp);
+safe_syscall4(int, clock_nanosleep, clockid_t, clock_id, int, flags,
+    const struct timespec *, rqtp, struct timespec *, rmtp);
+
+safe_syscall6(int, kevent, int, kq, const struct kevent *, changelist,
+    int, nchanges, struct kevent *, eventlist, int, nevents,
+    const struct timespec *, timeout);
+    
 /* I/O */
 safe_syscall3(int, open, const char *, path, int, flags, mode_t, mode);
 safe_syscall4(int, openat, int, fd, const char *, path, int, flags, mode_t,
Signature · 1 line
2.34.1