Home | History | Annotate | Line # | Download | only in netbsd32
      1 /*	$NetBSD: netbsd32_compat_12.c,v 1.37 2026/09/20 13:45:00 riastradh Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1998, 2001 Matthew R. Green
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: netbsd32_compat_12.c,v 1.37 2026/09/20 13:45:00 riastradh Exp $");
     31 
     32 #if defined(_KERNEL_OPT)
     33 #include "opt_compat_netbsd.h"
     34 #endif
     35 
     36 #include <sys/param.h>
     37 #include <sys/systm.h>
     38 #include <sys/module.h>
     39 #include <sys/filedesc.h>
     40 #include <sys/mount.h>
     41 #include <sys/mman.h>
     42 #include <sys/namei.h>
     43 #include <sys/proc.h>
     44 #include <sys/stat.h>
     45 #include <sys/swap.h>
     46 #include <sys/vfs_syscalls.h>
     47 
     48 #include <sys/syscallargs.h>
     49 #include <sys/syscallvar.h>
     50 
     51 #include <compat/sys/stat.h>
     52 
     53 #include <compat/netbsd32/netbsd32.h>
     54 #include <compat/netbsd32/netbsd32_syscall.h>
     55 #include <compat/netbsd32/netbsd32_syscallargs.h>
     56 
     57 static void netbsd32_stat12_to_netbsd32(struct stat12 *,
     58 		struct netbsd32_stat12 *);
     59 
     60 /* for use with {,fl}stat() */
     61 static void
     62 netbsd32_stat12_to_netbsd32(struct stat12 *sp12, struct netbsd32_stat12 *sp32)
     63 {
     64 
     65 	memset(sp32, 0, sizeof(*sp32));
     66 	sp32->st_dev = sp12->st_dev;
     67 	sp32->st_ino = sp12->st_ino;
     68 	sp32->st_mode = sp12->st_mode;
     69 	sp32->st_nlink = sp12->st_nlink;
     70 	sp32->st_uid = sp12->st_uid;
     71 	sp32->st_gid = sp12->st_gid;
     72 	sp32->st_rdev = sp12->st_rdev;
     73 	if (sp12->st_size < (quad_t)1 << 32)
     74 		sp32->st_size = sp12->st_size;
     75 	else
     76 		sp32->st_size = -2;
     77 	sp32->st_atimespec.tv_sec = sp12->st_atimespec.tv_sec;
     78 	sp32->st_atimespec.tv_nsec = sp12->st_atimespec.tv_nsec;
     79 	sp32->st_mtimespec.tv_sec = sp12->st_mtimespec.tv_sec;
     80 	sp32->st_mtimespec.tv_nsec = sp12->st_mtimespec.tv_nsec;
     81 	sp32->st_ctimespec.tv_sec = sp12->st_ctimespec.tv_sec;
     82 	sp32->st_ctimespec.tv_nsec = sp12->st_ctimespec.tv_nsec;
     83 	sp32->st_blocks = sp12->st_blocks;
     84 	sp32->st_blksize = sp12->st_blksize;
     85 	sp32->st_flags = sp12->st_flags;
     86 	sp32->st_gen = sp12->st_gen;
     87 }
     88 
     89 int
     90 compat_12_netbsd32_reboot(struct lwp *l, const struct compat_12_netbsd32_reboot_args *uap, register_t *retval)
     91 {
     92 	/* {
     93 		syscallarg(int) opt;
     94 	} */
     95 	struct compat_12_sys_reboot_args ua;
     96 
     97 	memset(&ua, 0, sizeof(ua));
     98 	NETBSD32TO64_UAP(opt);
     99 	return compat_12_sys_reboot(l, &ua, retval);
    100 }
    101 
    102 int
    103 compat_12_netbsd32_msync(struct lwp *l, const struct compat_12_netbsd32_msync_args *uap, register_t *retval)
    104 {
    105 	/* {
    106 		syscallarg(netbsd32_voidp) addr;
    107 		syscallarg(netbsd32_size_t) len;
    108 	} */
    109 	struct sys___msync13_args ua;
    110 
    111 	memset(&ua, 0, sizeof(ua));
    112 	NETBSD32TOP_UAP(addr, void *);
    113 	NETBSD32TOX_UAP(len, size_t);
    114 	SCARG(&ua, flags) = MS_SYNC | MS_INVALIDATE;
    115 	return sys___msync13(l, &ua, retval);
    116 }
    117 
    118 int
    119 compat_12_netbsd32_oswapon(struct lwp *l, const struct compat_12_netbsd32_oswapon_args *uap, register_t *retval)
    120 {
    121 	/* {
    122 		syscallarg(const netbsd32_charp) name;
    123 	} */
    124 	struct sys_swapctl_args ua;
    125 
    126 	memset(&ua, 0, sizeof(ua));
    127 	SCARG(&ua, cmd) = SWAP_ON;
    128 	SCARG(&ua, arg) = SCARG_P32(uap, name);
    129 	SCARG(&ua, misc) = 0;	/* priority */
    130 	return sys_swapctl(l, &ua, retval);
    131 }
    132 
    133 int
    134 compat_12_netbsd32_stat12(struct lwp *l, const struct compat_12_netbsd32_stat12_args *uap, register_t *retval)
    135 {
    136 	/* {
    137 		syscallarg(const netbsd32_charp) path;
    138 		syscallarg(netbsd32_stat12p_t) ub;
    139 	} */
    140 	struct netbsd32_stat12 sb32;
    141 	struct stat12 sb12;
    142 	struct stat sb;
    143 	int error;
    144 
    145 	error = do_sys_stat(SCARG_P32(uap, path), FOLLOW, &sb);
    146 	if (error)
    147 		return error;
    148 
    149 	compat_12_stat_conv(&sb, &sb12);
    150 	netbsd32_stat12_to_netbsd32(&sb12, &sb32);
    151 
    152 	return copyout(&sb32, SCARG_P32(uap, ub), sizeof sb32);
    153 }
    154 
    155 int
    156 compat_12_netbsd32_fstat12(struct lwp *l, const struct compat_12_netbsd32_fstat12_args *uap, register_t *retval)
    157 {
    158 	/* {
    159 		syscallarg(int) fd;
    160 		syscallarg(netbsd32_stat12p_t) sb;
    161 	} */
    162 	struct netbsd32_stat12 sb32;
    163 	struct stat12 sb12;
    164 	struct stat sb;
    165 	int error;
    166 
    167 	error = do_sys_fstat(SCARG(uap, fd), &sb);
    168 	if (error)
    169 		return error;
    170 
    171 	compat_12_stat_conv(&sb, &sb12);
    172 	netbsd32_stat12_to_netbsd32(&sb12, &sb32);
    173 
    174 	return copyout(&sb32, SCARG_P32(uap, sb), sizeof sb32);
    175 }
    176 
    177 int
    178 compat_12_netbsd32_lstat12(struct lwp *l, const struct compat_12_netbsd32_lstat12_args *uap, register_t *retval)
    179 {
    180 	/* {
    181 		syscallarg(const netbsd32_charp) path;
    182 		syscallarg(netbsd32_stat12p_t) ub;
    183 	} */
    184 	struct netbsd32_stat12 sb32;
    185 	struct stat12 sb12;
    186 	struct stat sb;
    187 	int error;
    188 
    189 	error = do_sys_stat(SCARG_P32(uap, path), NOFOLLOW, &sb);
    190 	if (error)
    191 		return error;
    192 
    193 	compat_12_stat_conv(&sb, &sb12);
    194 	netbsd32_stat12_to_netbsd32(&sb12, &sb32);
    195 
    196 	return copyout(&sb32, SCARG_P32(uap, ub), sizeof sb32);
    197 }
    198 
    199 int
    200 compat_12_netbsd32_getdirentries(struct lwp *l, const struct compat_12_netbsd32_getdirentries_args *uap, register_t *retval)
    201 {
    202 	/* {
    203 		syscallarg(int) fd;
    204 		syscallarg(netbsd32_charp) buf;
    205 		syscallarg(u_int) count;
    206 		syscallarg(netbsd32_longp) basep;
    207 	} */
    208 	struct compat_12_sys_getdirentries_args ua;
    209 
    210 	memset(&ua, 0, sizeof(ua));
    211 	NETBSD32TO64_UAP(fd);
    212 	NETBSD32TOP_UAP(buf, char);
    213 	NETBSD32TO64_UAP(count);
    214 	NETBSD32TOP_UAP(basep, long);
    215 
    216 	return compat_12_sys_getdirentries(l, &ua, retval);
    217 }
    218 
    219 static struct syscall_package compat_netbsd32_12_syscalls[] = {
    220 	{ NETBSD32_SYS_compat_12_netbsd32_reboot, 0,
    221 	    (sy_call_t *)compat_12_netbsd32_reboot },
    222 	{ NETBSD32_SYS_compat_12_netbsd32_msync, 0,
    223 	    (sy_call_t *)compat_12_netbsd32_msync },
    224 	{ NETBSD32_SYS_compat_12_netbsd32_oswapon, 0,
    225 	    (sy_call_t *)compat_12_netbsd32_oswapon },
    226 	{ NETBSD32_SYS_compat_12_netbsd32_stat12, 0,
    227 	    (sy_call_t *)compat_12_netbsd32_stat12 },
    228 	{ NETBSD32_SYS_compat_12_netbsd32_fstat12, 0,
    229 	    (sy_call_t *)compat_12_netbsd32_fstat12 },
    230 	{ NETBSD32_SYS_compat_12_netbsd32_lstat12, 0,
    231 	    (sy_call_t *)compat_12_netbsd32_lstat12 },
    232 	{ NETBSD32_SYS_compat_12_netbsd32_getdirentries, 0,
    233 	    (sy_call_t *)compat_12_netbsd32_getdirentries },
    234 	{ 0, 0, NULL }
    235 };
    236 
    237 MODULE(MODULE_CLASS_EXEC, compat_netbsd32_12, "compat_netbsd32_13,compat_12");
    238 
    239 static int
    240 compat_netbsd32_12_modcmd(modcmd_t cmd, void *arg)
    241 {
    242 
    243 	switch (cmd) {
    244 	case MODULE_CMD_INIT:
    245 		return syscall_establish(&emul_netbsd32,
    246 		    compat_netbsd32_12_syscalls);
    247 
    248 	case MODULE_CMD_FINI:
    249 		return syscall_disestablish(&emul_netbsd32,
    250 		    compat_netbsd32_12_syscalls);
    251 
    252 	default:
    253 		return ENOTTY;
    254 	}
    255 }
    256