bsd-user: do_freebsd_sysctl helper for sysctl(2)

Implement the wrapper function for sysctl(2). This puts the oid
arguments into a standard form and calls the common
do_freebsd_sysctl_oid.

Signed-off-by: Kyle Evans <kevans@FreeBSD.org>
Co-Authored-by: Juergen Lock <nox@jelal.kn-bremen.de>
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>
Reviewed-by: Warner Losh <imp@bsdimp.com>
Signed-off-by: Warner Losh <imp@bsdimp.com>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
This commit is contained in:
Kyle Evans 2023-02-10 10:02:45 -07:00 committed by Warner Losh
parent 248a485bf6
commit 7adda6de6d
3 changed files with 61 additions and 1 deletions

View File

@ -248,7 +248,7 @@ static inline void sysctl_oidfmt(uint32_t *holdp)
holdp[0] = tswap32(holdp[0]);
}
static abi_long G_GNUC_UNUSED do_freebsd_sysctl_oid(CPUArchState *env, int32_t *snamep,
static abi_long do_freebsd_sysctl_oid(CPUArchState *env, int32_t *snamep,
int32_t namelen, void *holdp, size_t *holdlenp, void *hnewp,
size_t newlen)
{
@ -471,6 +471,60 @@ out:
return ret;
}
abi_long do_freebsd_sysctl(CPUArchState *env, abi_ulong namep, int32_t namelen,
abi_ulong oldp, abi_ulong oldlenp, abi_ulong newp, abi_ulong newlen)
{
abi_long ret = -TARGET_EFAULT;
void *hnamep, *holdp = NULL, *hnewp = NULL;
size_t holdlen;
abi_ulong oldlen = 0;
int32_t *snamep = g_malloc(sizeof(int32_t) * namelen), *p, *q, i;
/* oldlenp is read/write, pre-check here for write */
if (oldlenp) {
if (!access_ok(VERIFY_WRITE, oldlenp, sizeof(abi_ulong)) ||
get_user_ual(oldlen, oldlenp)) {
goto out;
}
}
hnamep = lock_user(VERIFY_READ, namep, namelen, 1);
if (hnamep == NULL) {
goto out;
}
if (newp) {
hnewp = lock_user(VERIFY_READ, newp, newlen, 1);
if (hnewp == NULL) {
goto out;
}
}
if (oldp) {
holdp = lock_user(VERIFY_WRITE, oldp, oldlen, 0);
if (holdp == NULL) {
goto out;
}
}
holdlen = oldlen;
for (p = hnamep, q = snamep, i = 0; i < namelen; p++, i++, q++) {
*q = tswap32(*p);
}
ret = do_freebsd_sysctl_oid(env, snamep, namelen, holdp, &holdlen, hnewp,
newlen);
/*
* writeability pre-checked above. __sysctl(2) returns ENOMEM and updates
* oldlenp for the proper size to use.
*/
if (oldlenp && (ret == 0 || ret == -TARGET_ENOMEM)) {
put_user_ual(holdlen, oldlenp);
}
unlock_user(hnamep, namep, 0);
unlock_user(holdp, oldp, ret == 0 ? holdlen : 0);
out:
g_free(snamep);
return ret;
}
/* sysarch() is architecture dependent. */
abi_long do_freebsd_sysarch(void *cpu_env, abi_long arg1, abi_long arg2)
{

View File

@ -494,6 +494,10 @@ static abi_long freebsd_syscall(void *cpu_env, int num, abi_long arg1,
/*
* sys{ctl, arch, call}
*/
case TARGET_FREEBSD_NR___sysctl: /* sysctl(3) */
ret = do_freebsd_sysctl(cpu_env, arg1, arg2, arg3, arg4, arg5, arg6);
break;
case TARGET_FREEBSD_NR_sysarch: /* sysarch(2) */
ret = do_freebsd_sysarch(cpu_env, arg1, arg2);
break;

View File

@ -253,6 +253,8 @@ bool is_error(abi_long ret);
int host_to_target_errno(int err);
/* os-sys.c */
abi_long do_freebsd_sysctl(CPUArchState *env, abi_ulong namep, int32_t namelen,
abi_ulong oldp, abi_ulong oldlenp, abi_ulong newp, abi_ulong newlen);
abi_long do_freebsd_sysarch(void *cpu_env, abi_long arg1, abi_long arg2);
/* user access */