summaryrefslogtreecommitdiff
path: root/sys/compat/netbsd32/netbsd32_compat_12.c
blob: 976018f3d1b15ca2a675b51e4b030976b6a86597 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*	$NetBSD: netbsd32_compat_12.c,v 1.36 2021/09/07 11:43:05 riastradh Exp $	*/

/*
 * Copyright (c) 1998, 2001 Matthew R. Green
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: netbsd32_compat_12.c,v 1.36 2021/09/07 11:43:05 riastradh Exp $");

#if defined(_KERNEL_OPT)
#include "opt_compat_netbsd.h"
#endif

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/module.h>
#include <sys/filedesc.h>
#include <sys/mount.h>
#include <sys/mman.h>
#include <sys/namei.h>
#include <sys/proc.h>
#include <sys/stat.h>
#include <sys/swap.h>
#include <sys/vfs_syscalls.h>

#include <sys/syscallargs.h>
#include <sys/syscallvar.h>

#include <compat/sys/stat.h>

#include <compat/netbsd32/netbsd32.h>
#include <compat/netbsd32/netbsd32_syscall.h>
#include <compat/netbsd32/netbsd32_syscallargs.h>

static void netbsd32_stat12_to_netbsd32(struct stat12 *,
		struct netbsd32_stat12 *);

/* for use with {,fl}stat() */
static void
netbsd32_stat12_to_netbsd32(struct stat12 *sp12, struct netbsd32_stat12 *sp32)
{

	memset(sp32, 0, sizeof(*sp32));
	sp32->st_dev = sp12->st_dev;
	sp32->st_ino = sp12->st_ino;
	sp32->st_mode = sp12->st_mode;
	sp32->st_nlink = sp12->st_nlink;
	sp32->st_uid = sp12->st_uid;
	sp32->st_gid = sp12->st_gid;
	sp32->st_rdev = sp12->st_rdev;
	if (sp12->st_size < (quad_t)1 << 32)
		sp32->st_size = sp12->st_size;
	else
		sp32->st_size = -2;
	sp32->st_atimespec.tv_sec = sp12->st_atimespec.tv_sec;
	sp32->st_atimespec.tv_nsec = sp12->st_atimespec.tv_nsec;
	sp32->st_mtimespec.tv_sec = sp12->st_mtimespec.tv_sec;
	sp32->st_mtimespec.tv_nsec = sp12->st_mtimespec.tv_nsec;
	sp32->st_ctimespec.tv_sec = sp12->st_ctimespec.tv_sec;
	sp32->st_ctimespec.tv_nsec = sp12->st_ctimespec.tv_nsec;
	sp32->st_blocks = sp12->st_blocks;
	sp32->st_blksize = sp12->st_blksize;
	sp32->st_flags = sp12->st_flags;
	sp32->st_gen = sp12->st_gen;
}

int
compat_12_netbsd32_reboot(struct lwp *l, const struct compat_12_netbsd32_reboot_args *uap, register_t *retval)
{
	/* {
		syscallarg(int) opt;
	} */
	struct compat_12_sys_reboot_args ua;

	NETBSD32TO64_UAP(opt);
	return compat_12_sys_reboot(l, &ua, retval);
}

int
compat_12_netbsd32_msync(struct lwp *l, const struct compat_12_netbsd32_msync_args *uap, register_t *retval)
{
	/* {
		syscallarg(netbsd32_voidp) addr;
		syscallarg(netbsd32_size_t) len;
	} */
	struct sys___msync13_args ua;

	NETBSD32TOP_UAP(addr, void *);
	NETBSD32TOX_UAP(len, size_t);
	SCARG(&ua, flags) = MS_SYNC | MS_INVALIDATE;
	return sys___msync13(l, &ua, retval);
}

int
compat_12_netbsd32_oswapon(struct lwp *l, const struct compat_12_netbsd32_oswapon_args *uap, register_t *retval)
{
	/* {
		syscallarg(const netbsd32_charp) name;
	} */
	struct sys_swapctl_args ua;

	SCARG(&ua, cmd) = SWAP_ON;
	SCARG(&ua, arg) = SCARG_P32(uap, name);
	SCARG(&ua, misc) = 0;	/* priority */
	return sys_swapctl(l, &ua, retval);
}

int
compat_12_netbsd32_stat12(struct lwp *l, const struct compat_12_netbsd32_stat12_args *uap, register_t *retval)
{
	/* {
		syscallarg(const netbsd32_charp) path;
		syscallarg(netbsd32_stat12p_t) ub;
	} */
	struct netbsd32_stat12 sb32;
	struct stat12 sb12;
	struct stat sb;
	int error;

	error = do_sys_stat(SCARG_P32(uap, path), FOLLOW, &sb);
	if (error)
		return error;

	compat_12_stat_conv(&sb, &sb12);
	netbsd32_stat12_to_netbsd32(&sb12, &sb32);

	return copyout(&sb32, SCARG_P32(uap, ub), sizeof sb32);
}

int
compat_12_netbsd32_fstat12(struct lwp *l, const struct compat_12_netbsd32_fstat12_args *uap, register_t *retval)
{
	/* {
		syscallarg(int) fd;
		syscallarg(netbsd32_stat12p_t) sb;
	} */
	struct netbsd32_stat12 sb32;
	struct stat12 sb12;
	struct stat sb;
	int error;

	error = do_sys_fstat(SCARG(uap, fd), &sb);
	if (error)
		return error;

	compat_12_stat_conv(&sb, &sb12);
	netbsd32_stat12_to_netbsd32(&sb12, &sb32);

	return copyout(&sb32, SCARG_P32(uap, sb), sizeof sb32);
}

int
compat_12_netbsd32_lstat12(struct lwp *l, const struct compat_12_netbsd32_lstat12_args *uap, register_t *retval)
{
	/* {
		syscallarg(const netbsd32_charp) path;
		syscallarg(netbsd32_stat12p_t) ub;
	} */
	struct netbsd32_stat12 sb32;
	struct stat12 sb12;
	struct stat sb;
	int error;

	error = do_sys_stat(SCARG_P32(uap, path), NOFOLLOW, &sb);
	if (error)
		return error;

	compat_12_stat_conv(&sb, &sb12);
	netbsd32_stat12_to_netbsd32(&sb12, &sb32);

	return copyout(&sb32, SCARG_P32(uap, ub), sizeof sb32);
}

int
compat_12_netbsd32_getdirentries(struct lwp *l, const struct compat_12_netbsd32_getdirentries_args *uap, register_t *retval)
{
	/* {
		syscallarg(int) fd;
		syscallarg(netbsd32_charp) buf;
		syscallarg(u_int) count;
		syscallarg(netbsd32_longp) basep;
	} */
	struct compat_12_sys_getdirentries_args ua;

	NETBSD32TO64_UAP(fd);
	NETBSD32TOP_UAP(buf, char);
	NETBSD32TO64_UAP(count);
	NETBSD32TOP_UAP(basep, long);

	return compat_12_sys_getdirentries(l, &ua, retval);
}

static struct syscall_package compat_netbsd32_12_syscalls[] = {
	{ NETBSD32_SYS_compat_12_netbsd32_reboot, 0,
	    (sy_call_t *)compat_12_netbsd32_reboot },
	{ NETBSD32_SYS_compat_12_netbsd32_msync, 0,
	    (sy_call_t *)compat_12_netbsd32_msync },
	{ NETBSD32_SYS_compat_12_netbsd32_oswapon, 0,
	    (sy_call_t *)compat_12_netbsd32_oswapon },
	{ NETBSD32_SYS_compat_12_netbsd32_stat12, 0,
	    (sy_call_t *)compat_12_netbsd32_stat12 },
	{ NETBSD32_SYS_compat_12_netbsd32_fstat12, 0,
	    (sy_call_t *)compat_12_netbsd32_fstat12 },
	{ NETBSD32_SYS_compat_12_netbsd32_lstat12, 0,
	    (sy_call_t *)compat_12_netbsd32_lstat12 },
	{ NETBSD32_SYS_compat_12_netbsd32_getdirentries, 0,
	    (sy_call_t *)compat_12_netbsd32_getdirentries },
	{ 0, 0, NULL }
};

MODULE(MODULE_CLASS_EXEC, compat_netbsd32_12, "compat_netbsd32_13,compat_12");

static int
compat_netbsd32_12_modcmd(modcmd_t cmd, void *arg)
{

	switch (cmd) {
	case MODULE_CMD_INIT:
		return syscall_establish(&emul_netbsd32,
		    compat_netbsd32_12_syscalls);

	case MODULE_CMD_FINI:
		return syscall_disestablish(&emul_netbsd32,
		    compat_netbsd32_12_syscalls);

	default:
		return ENOTTY;
	}
}