From: Warner Losh <imp@bsdimp.com>
To: qemu-devel@nongnu.org
Cc: "Kyle Evans" <kevans@freebsd.org>,
richard.henderson@linaro.org,
"Alex Bennée" <alex.bennee@linaro.org>,
f4bug@amsat.org, "Warner Losh" <imp@bsdimp.com>,
"Paolo Bonzini" <pbonzini@redhat.com>,
"Thomas Huth" <thuth@redhat.com>,
"Juergen Lock" <nox@jelal.kn-bremen.de>,
"Stacey Son" <sson@FreeBSD.org>
Subject: [PATCH v3 09/11] bsd-user: Start translation of arch-specific sysctls
Date: Thu, 16 Feb 2023 16:33:51 -0700 [thread overview]
Message-ID: <20230216233353.13944-10-imp@bsdimp.com> (raw)
In-Reply-To: <20230216233353.13944-1-imp@bsdimp.com>
From: Juergen Lock <nox@jelal.kn-bremen.de>
Intercept some syscalls that we need to translate (like the archiecture
we're running on) and translate them. These are only the simplest ones
so far.
Signed-off-by: Juergen Lock <nox@jelal.kn-bremen.de>
Co-Authored-by: Stacey Son <sson@FreeBSD.org>
Signed-off-by: Stacey Son <sson@FreeBSD.org>
Co-Authored-by: Warner Losh <imp@bsdimp.com>
Signed-off-by: Warner Losh <imp@bsdimp.com>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
---
bsd-user/freebsd/os-sys.c | 145 +++++++++++++++++++++++++++++++++++++-
1 file changed, 143 insertions(+), 2 deletions(-)
diff --git a/bsd-user/freebsd/os-sys.c b/bsd-user/freebsd/os-sys.c
index 42f0cc82279..1464e64428f 100644
--- a/bsd-user/freebsd/os-sys.c
+++ b/bsd-user/freebsd/os-sys.c
@@ -67,13 +67,13 @@ static const int host_ctl_size[CTLTYPE+1] = {
*/
static const abi_ulong guest_max_mem = UINT32_MAX - 0x100c000 + 1;
-static abi_ulong G_GNUC_UNUSED cap_memory(uint64_t mem)
+static abi_ulong cap_memory(uint64_t mem)
{
return MIN(guest_max_mem, mem);
}
#endif
-static abi_ulong G_GNUC_UNUSED scale_to_guest_pages(uint64_t pages)
+static abi_ulong scale_to_guest_pages(uint64_t pages)
{
/* Scale pages from host to guest */
pages = muldiv64(pages, qemu_real_host_page_size(), TARGET_PAGE_SIZE);
@@ -264,6 +264,146 @@ static abi_long G_GNUC_UNUSED do_freebsd_sysctl_oid(CPUArchState *env, int32_t *
oidfmt(snamep, namelen, NULL, &kind);
/* Handle some arch/emulator dependent sysctl()'s here. */
+ switch (snamep[0]) {
+ case CTL_KERN:
+ switch (snamep[1]) {
+ case KERN_USRSTACK:
+ if (oldlen) {
+ (*(abi_ulong *)holdp) = tswapal(TARGET_USRSTACK);
+ }
+ holdlen = sizeof(abi_ulong);
+ ret = 0;
+ goto out;
+
+ case KERN_PS_STRINGS:
+ if (oldlen) {
+ (*(abi_ulong *)holdp) = tswapal(TARGET_PS_STRINGS);
+ }
+ holdlen = sizeof(abi_ulong);
+ ret = 0;
+ goto out;
+
+ default:
+ break;
+ }
+ break;
+
+ case CTL_HW:
+ switch (snamep[1]) {
+ case HW_MACHINE:
+ holdlen = sizeof(TARGET_HW_MACHINE);
+ if (holdp) {
+ strlcpy(holdp, TARGET_HW_MACHINE, oldlen);
+ }
+ ret = 0;
+ goto out;
+
+ case HW_MACHINE_ARCH:
+ {
+ holdlen = sizeof(TARGET_HW_MACHINE_ARCH);
+ if (holdp) {
+ strlcpy(holdp, TARGET_HW_MACHINE_ARCH, oldlen);
+ }
+ ret = 0;
+ goto out;
+ }
+ case HW_NCPU:
+ if (oldlen) {
+ (*(abi_int *)holdp) = tswap32(bsd_get_ncpu());
+ }
+ holdlen = sizeof(int32_t);
+ ret = 0;
+ goto out;
+#if defined(TARGET_ARM)
+ case HW_FLOATINGPT:
+ if (oldlen) {
+ ARMCPU *cpu = env_archcpu(env);
+ *(abi_int *)holdp = cpu_isar_feature(aa32_vfp, cpu);
+ }
+ holdlen = sizeof(abi_int);
+ ret = 0;
+ goto out;
+#endif
+
+
+#ifdef TARGET_ABI32
+ case HW_PHYSMEM:
+ case HW_USERMEM:
+ case HW_REALMEM:
+ holdlen = sizeof(abi_ulong);
+ ret = 0;
+
+ if (oldlen) {
+ int mib[2] = {snamep[0], snamep[1]};
+ unsigned long lvalue;
+ size_t len = sizeof(lvalue);
+
+ if (sysctl(mib, 2, &lvalue, &len, NULL, 0) == -1) {
+ ret = -1;
+ } else {
+ lvalue = cap_memory(lvalue);
+ (*(abi_ulong *)holdp) = tswapal((abi_ulong)lvalue);
+ }
+ }
+ goto out;
+#endif
+
+ default:
+ {
+ static int oid_hw_availpages;
+ static int oid_hw_pagesizes;
+
+ if (!oid_hw_availpages) {
+ int real_oid[CTL_MAXNAME + 2];
+ size_t len = sizeof(real_oid) / sizeof(int);
+
+ if (sysctlnametomib("hw.availpages", real_oid, &len) >= 0) {
+ oid_hw_availpages = real_oid[1];
+ }
+ }
+ if (!oid_hw_pagesizes) {
+ int real_oid[CTL_MAXNAME + 2];
+ size_t len = sizeof(real_oid) / sizeof(int);
+
+ if (sysctlnametomib("hw.pagesizes", real_oid, &len) >= 0) {
+ oid_hw_pagesizes = real_oid[1];
+ }
+ }
+
+ if (oid_hw_availpages && snamep[1] == oid_hw_availpages) {
+ long lvalue;
+ size_t len = sizeof(lvalue);
+
+ if (sysctlbyname("hw.availpages", &lvalue, &len, NULL, 0) == -1) {
+ ret = -1;
+ } else {
+ if (oldlen) {
+ lvalue = scale_to_guest_pages(lvalue);
+ (*(abi_ulong *)holdp) = tswapal((abi_ulong)lvalue);
+ }
+ holdlen = sizeof(abi_ulong);
+ ret = 0;
+ }
+ goto out;
+ }
+
+ if (oid_hw_pagesizes && snamep[1] == oid_hw_pagesizes) {
+ if (oldlen) {
+ (*(abi_ulong *)holdp) = tswapal((abi_ulong)getpagesize());
+ ((abi_ulong *)holdp)[1] = 0;
+ }
+ holdlen = sizeof(abi_ulong) * 2;
+ ret = 0;
+ goto out;
+ }
+ break;
+ }
+ }
+ break;
+
+ default:
+ break;
+ }
#ifdef TARGET_ABI32
/*
@@ -327,6 +467,7 @@ static abi_long G_GNUC_UNUSED do_freebsd_sysctl_oid(CPUArchState *env, int32_t *
}
}
+out:
*holdlenp = holdlen;
return ret;
}
--
2.39.1
next prev parent reply other threads:[~2023-02-16 23:35 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-02-16 23:33 [PATCH v3 00/11] 2023 Q1 bsd-user upstreaming: bugfixes and sysctl Warner Losh
2023-02-16 23:33 ` [PATCH v3 01/11] bsd-user: Don't truncate the return value from freebsd_syscall Warner Losh
2023-02-16 23:33 ` [PATCH v3 02/11] build: Don't specify -no-pie for --static user-mode programs Warner Losh
2023-02-17 0:38 ` Alex Bennée
2023-02-16 23:33 ` [PATCH v3 03/11] bsd-user: Add sysarch syscall Warner Losh
2023-02-16 23:33 ` [PATCH v3 04/11] bsd-user: various helper routines for sysctl Warner Losh
2023-02-17 17:38 ` Richard Henderson
2023-02-16 23:33 ` [PATCH v3 05/11] bsd-user: Helper routines oidfmt Warner Losh
2023-02-16 23:33 ` [PATCH v3 06/11] bsd-user: Helper routines h2g_old_sysctl Warner Losh
2023-02-17 17:41 ` Richard Henderson
2023-02-16 23:33 ` [PATCH v3 07/11] bsd-user: sysctl helper funtions: sysctl_name2oid and sysctl_oidfmt Warner Losh
2023-02-16 23:33 ` [PATCH v3 08/11] bsd-user: common routine do_freebsd_sysctl_oid for all sysctl variants Warner Losh
2023-02-16 23:33 ` Warner Losh [this message]
2023-02-16 23:33 ` [PATCH v3 10/11] bsd-user: do_freebsd_sysctl helper for sysctl(2) Warner Losh
2023-02-16 23:33 ` [PATCH v3 11/11] bsd-user: implement sysctlbyname(2) Warner Losh
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230216233353.13944-10-imp@bsdimp.com \
--to=imp@bsdimp.com \
--cc=alex.bennee@linaro.org \
--cc=f4bug@amsat.org \
--cc=kevans@freebsd.org \
--cc=nox@jelal.kn-bremen.de \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=richard.henderson@linaro.org \
--cc=sson@FreeBSD.org \
--cc=thuth@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.