1/*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1990, 1993
5 *	The Regents of the University of California.  All rights reserved.
6 * Copyright (c) 2000
7 *	Poul-Henning Kamp.  All rights reserved.
8 * (c) UNIX System Laboratories, Inc.
9 * All or some portions of this file are derived from material licensed
10 * to the University of California by American Telephone and Telegraph
11 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
12 * the permission of UNIX System Laboratories, Inc.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 *    notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 *    notice, this list of conditions and the following disclaimer in the
21 *    documentation and/or other materials provided with the distribution.
22 * 3. Neither the name of the University nor the names of its contributors
23 *    may be used to endorse or promote products derived from this software
24 *    without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39#ifndef _SYS_CONF_H_
40#define	_SYS_CONF_H_
41
42#ifdef _KERNEL
43#include <sys/_eventhandler.h>
44#else
45#include <sys/queue.h>
46#endif
47
48struct snapdata;
49struct devfs_dirent;
50struct cdevsw;
51struct file;
52
53struct cdev {
54	void		*si_spare0;
55	u_int		si_flags;
56#define	SI_ETERNAL	0x0001	/* never destroyed */
57#define	SI_ALIAS	0x0002	/* carrier of alias name */
58#define	SI_NAMED	0x0004	/* make_dev{_alias} has been called */
59#define	SI_UNUSED1	0x0008	/* unused */
60#define	SI_CHILD	0x0010	/* child of another struct cdev **/
61#define	SI_DUMPDEV	0x0080	/* is kernel dumpdev */
62#define	SI_CLONELIST	0x0200	/* on a clone list */
63#define	SI_UNMAPPED	0x0400	/* can handle unmapped I/O */
64#define	SI_NOSPLIT	0x0800	/* I/O should not be split up */
65	struct timespec	si_atime;
66	struct timespec	si_ctime;
67	struct timespec	si_mtime;
68	uid_t		si_uid;
69	gid_t		si_gid;
70	mode_t		si_mode;
71	struct ucred	*si_cred;	/* cached clone-time credential */
72	int		si_drv0;
73	int		si_refcount;
74	LIST_ENTRY(cdev)	si_list;
75	LIST_ENTRY(cdev)	si_clone;
76	LIST_HEAD(, cdev)	si_children;
77	LIST_ENTRY(cdev)	si_siblings;
78	struct cdev *si_parent;
79	struct mount	*si_mountpt;
80	void		*si_drv1, *si_drv2;
81	struct cdevsw	*si_devsw;
82	int		si_iosize_max;	/* maximum I/O size (for physio &al) */
83	u_long		si_usecount;
84	u_long		si_threadcount;
85	union {
86		struct snapdata *__sid_snapdata;
87	} __si_u;
88	char		si_name[SPECNAMELEN + 1];
89};
90
91#define	si_snapdata	__si_u.__sid_snapdata
92
93#ifdef _KERNEL
94
95/*
96 * Definitions of device driver entry switches
97 */
98
99struct bio;
100struct buf;
101struct dumperinfo;
102struct kerneldumpheader;
103struct thread;
104struct uio;
105struct knote;
106struct clonedevs;
107struct vm_object;
108struct vnode;
109
110typedef int d_open_t(struct cdev *dev, int oflags, int devtype, struct thread *td);
111typedef int d_fdopen_t(struct cdev *dev, int oflags, struct thread *td, struct file *fp);
112typedef int d_close_t(struct cdev *dev, int fflag, int devtype, struct thread *td);
113typedef void d_strategy_t(struct bio *bp);
114typedef int d_ioctl_t(struct cdev *dev, u_long cmd, caddr_t data,
115		      int fflag, struct thread *td);
116
117typedef int d_read_t(struct cdev *dev, struct uio *uio, int ioflag);
118typedef int d_write_t(struct cdev *dev, struct uio *uio, int ioflag);
119typedef int d_poll_t(struct cdev *dev, int events, struct thread *td);
120typedef int d_kqfilter_t(struct cdev *dev, struct knote *kn);
121typedef int d_mmap_t(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr,
122		     int nprot, vm_memattr_t *memattr);
123typedef int d_mmap_single_t(struct cdev *cdev, vm_ooffset_t *offset,
124    vm_size_t size, struct vm_object **object, int nprot);
125typedef void d_purge_t(struct cdev *dev);
126
127typedef int dumper_t(
128	void *_priv,		/* Private to the driver. */
129	void *_virtual,		/* Virtual (mapped) address. */
130	off_t _offset,		/* Byte-offset to write at. */
131	size_t _length);	/* Number of bytes to dump. */
132typedef int dumper_start_t(struct dumperinfo *di, void *key, uint32_t keysize);
133typedef int dumper_hdr_t(struct dumperinfo *di, struct kerneldumpheader *kdh);
134
135#endif /* _KERNEL */
136
137/*
138 * Types for d_flags.
139 */
140#define	D_TAPE	0x0001
141#define	D_DISK	0x0002
142#define	D_TTY	0x0004
143#define	D_MEM	0x0008	/* /dev/(k)mem */
144
145/* Defined uid and gid values. */
146#define		UID_ROOT	0
147#define		UID_BIN		3
148#define		UID_UUCP	66
149#define		UID_NOBODY	65534
150
151#define		GID_WHEEL	0
152#define		GID_KMEM	2
153#define		GID_TTY		4
154#define		GID_OPERATOR	5
155#define		GID_BIN		7
156#define		GID_GAMES	13
157#define		GID_VIDEO	44
158#define		GID_RT_PRIO	47
159#define		GID_ID_PRIO	48
160#define		GID_DIALER	68
161#define		GID_NOGROUP	65533
162#define		GID_NOBODY	65534
163
164#ifdef _KERNEL
165
166#define	D_TYPEMASK	0xffff
167
168/*
169 * Flags for d_flags which the drivers can set.
170 */
171#define	D_TRACKCLOSE	0x00080000	/* track all closes */
172#define	D_MMAP_ANON	0x00100000	/* special treatment in vm_mmap.c */
173#define	D_GIANTOK	0x00200000	/* suppress warning about using Giant */
174#define	D_NEEDGIANT	0x00400000	/* driver want Giant */
175#define	D_NEEDMINOR	0x00800000	/* driver uses clone_create() */
176
177/*
178 * Version numbers.
179 */
180#define	D_VERSION_00	0x20011966
181#define	D_VERSION_01	0x17032005	/* Add d_uid,gid,mode & kind */
182#define	D_VERSION_02	0x28042009	/* Add d_mmap_single */
183#define	D_VERSION_03	0x17122009	/* d_mmap takes memattr,vm_ooffset_t */
184#define	D_VERSION_04	0x5c48c353	/* SPECNAMELEN bumped to MAXNAMLEN */
185#define	D_VERSION	D_VERSION_04
186
187/*
188 * Flags used for internal housekeeping
189 */
190#define	D_INIT		0x80000000	/* cdevsw initialized */
191
192/*
193 * Character device switch table
194 */
195struct cdevsw {
196	int			d_version;
197	u_int			d_flags;
198	const char		*d_name;
199	d_open_t		*d_open;
200	d_fdopen_t		*d_fdopen;
201	d_close_t		*d_close;
202	d_read_t		*d_read;
203	d_write_t		*d_write;
204	d_ioctl_t		*d_ioctl;
205	d_poll_t		*d_poll;
206	d_mmap_t		*d_mmap;
207	d_strategy_t		*d_strategy;
208	void			*d_spare0;
209	d_kqfilter_t		*d_kqfilter;
210	d_purge_t		*d_purge;
211	d_mmap_single_t		*d_mmap_single;
212
213	int32_t			d_spare1[3];
214	void			*d_spare2[3];
215
216	/* These fields should not be messed with by drivers */
217	LIST_HEAD(, cdev)	d_devs;
218	int			d_spare3;
219	union {
220		struct cdevsw		*gianttrick;
221		SLIST_ENTRY(cdevsw)	postfree_list;
222	} __d_giant;
223};
224#define	d_gianttrick		__d_giant.gianttrick
225#define	d_postfree_list		__d_giant.postfree_list
226
227struct module;
228
229struct devsw_module_data {
230	int	(*chainevh)(struct module *, int, void *); /* next handler */
231	void	*chainarg;	/* arg for next event handler */
232	/* Do not initialize fields hereafter */
233};
234
235#define	DEV_MODULE_ORDERED(name, evh, arg, ord)				\
236static moduledata_t name##_mod = {					\
237    #name,								\
238    evh,								\
239    arg									\
240};									\
241DECLARE_MODULE(name, name##_mod, SI_SUB_DRIVERS, ord)
242
243#define	DEV_MODULE(name, evh, arg)					\
244    DEV_MODULE_ORDERED(name, evh, arg, SI_ORDER_MIDDLE)
245
246void clone_setup(struct clonedevs **cdp);
247void clone_cleanup(struct clonedevs **);
248#define	CLONE_UNITMASK	0xfffff
249#define	CLONE_FLAG0	(CLONE_UNITMASK + 1)
250int clone_create(struct clonedevs **, struct cdevsw *, int *unit, struct cdev **dev, int extra);
251
252#define	MAKEDEV_REF		0x01
253#define	MAKEDEV_WHTOUT		0x02
254#define	MAKEDEV_NOWAIT		0x04
255#define	MAKEDEV_WAITOK		0x08
256#define	MAKEDEV_ETERNAL		0x10
257#define	MAKEDEV_CHECKNAME	0x20
258struct make_dev_args {
259	size_t		 mda_size;
260	int		 mda_flags;
261	struct cdevsw	*mda_devsw;
262	struct ucred	*mda_cr;
263	uid_t		 mda_uid;
264	gid_t		 mda_gid;
265	int		 mda_mode;
266	int		 mda_unit;
267	void		*mda_si_drv1;
268	void		*mda_si_drv2;
269};
270void make_dev_args_init_impl(struct make_dev_args *_args, size_t _sz);
271#define	make_dev_args_init(a) \
272    make_dev_args_init_impl((a), sizeof(struct make_dev_args))
273
274void	delist_dev(struct cdev *_dev);
275void	destroy_dev(struct cdev *_dev);
276int	destroy_dev_sched(struct cdev *dev);
277int	destroy_dev_sched_cb(struct cdev *dev, void (*cb)(void *), void *arg);
278void	destroy_dev_drain(struct cdevsw *csw);
279struct cdevsw *dev_refthread(struct cdev *_dev, int *_ref);
280struct cdevsw *devvn_refthread(struct vnode *vp, struct cdev **devp, int *_ref);
281void	dev_relthread(struct cdev *_dev, int _ref);
282void	dev_depends(struct cdev *_pdev, struct cdev *_cdev);
283void	dev_ref(struct cdev *dev);
284void	dev_refl(struct cdev *dev);
285void	dev_rel(struct cdev *dev);
286struct cdev *make_dev(struct cdevsw *_devsw, int _unit, uid_t _uid, gid_t _gid,
287		int _perms, const char *_fmt, ...) __printflike(6, 7);
288struct cdev *make_dev_cred(struct cdevsw *_devsw, int _unit,
289		struct ucred *_cr, uid_t _uid, gid_t _gid, int _perms,
290		const char *_fmt, ...) __printflike(7, 8);
291struct cdev *make_dev_credf(int _flags,
292		struct cdevsw *_devsw, int _unit,
293		struct ucred *_cr, uid_t _uid, gid_t _gid, int _mode,
294		const char *_fmt, ...) __printflike(8, 9);
295int	make_dev_p(int _flags, struct cdev **_cdev, struct cdevsw *_devsw,
296		struct ucred *_cr, uid_t _uid, gid_t _gid, int _mode,
297		const char *_fmt, ...) __printflike(8, 9);
298int	make_dev_s(struct make_dev_args *_args, struct cdev **_cdev,
299		const char *_fmt, ...) __printflike(3, 4);
300struct cdev *make_dev_alias(struct cdev *_pdev, const char *_fmt, ...)
301		__printflike(2, 3);
302int	make_dev_alias_p(int _flags, struct cdev **_cdev, struct cdev *_pdev,
303		const char *_fmt, ...) __printflike(4, 5);
304int	make_dev_physpath_alias(int _flags, struct cdev **_cdev,
305		struct cdev *_pdev, struct cdev *_old_alias,
306		const char *_physpath);
307void	dev_lock(void);
308void	dev_unlock(void);
309
310#ifdef KLD_MODULE
311#define	MAKEDEV_ETERNAL_KLD	0
312#else
313#define	MAKEDEV_ETERNAL_KLD	MAKEDEV_ETERNAL
314#endif
315
316#define	dev2unit(d)	((d)->si_drv0)
317
318typedef void d_priv_dtor_t(void *data);
319int	devfs_get_cdevpriv(void **datap);
320int	devfs_set_cdevpriv(void *priv, d_priv_dtor_t *dtr);
321void	devfs_clear_cdevpriv(void);
322int	devfs_foreach_cdevpriv(struct cdev *dev,
323	    int (*cb)(void *data, void *arg), void *arg);
324
325ino_t	devfs_alloc_cdp_inode(void);
326void	devfs_free_cdp_inode(ino_t ino);
327
328typedef void (*dev_clone_fn)(void *arg, struct ucred *cred, char *name,
329	    int namelen, struct cdev **result);
330
331int dev_stdclone(char *_name, char **_namep, const char *_stem, int *_unit);
332EVENTHANDLER_DECLARE(dev_clone, dev_clone_fn);
333
334/* Stuff relating to kernel-dump */
335struct kerneldumpcrypto;
336struct kerneldumpheader;
337
338struct dumperinfo {
339	dumper_t *dumper;	/* Dumping function. */
340	dumper_start_t *dumper_start; /* Dumper callback for dump_start(). */
341	dumper_hdr_t *dumper_hdr; /* Dumper callback for writing headers. */
342	void	*priv;		/* Private parts. */
343	u_int	blocksize;	/* Size of block in bytes. */
344	u_int	maxiosize;	/* Max size allowed for an individual I/O */
345	off_t	mediaoffset;	/* Initial offset in bytes. */
346	off_t	mediasize;	/* Space available in bytes. */
347
348	/* MI kernel dump state. */
349	void	*blockbuf;	/* Buffer for padding shorter dump blocks */
350	off_t	dumpoff;	/* Offset of ongoing kernel dump. */
351	off_t	origdumpoff;	/* Starting dump offset. */
352	struct kerneldumpcrypto	*kdcrypto; /* Kernel dump crypto. */
353	struct kerneldumpcomp *kdcomp; /* Kernel dump compression. */
354
355	TAILQ_ENTRY(dumperinfo)	di_next;
356
357	char			di_devname[];
358};
359
360extern int dumping;		/* system is dumping */
361extern bool dumped_core;	/* system successfully dumped kernel core */
362
363/*
364 * Save registers for later extraction from a kernel dump.
365 *
366 * This must be inlined into the caller, which in turn must be the function that
367 * calls (mini)dumpsys().  Otherwise, the saved frame pointer will reference a
368 * stack frame that may be clobbered by subsequent function calls.
369 */
370#define	dump_savectx() do {		\
371	extern struct pcb dumppcb;	\
372	extern lwpid_t dumptid;		\
373					\
374	savectx(&dumppcb);		\
375	dumptid = curthread->td_tid;	\
376} while (0)
377
378int doadump(boolean_t);
379struct diocskerneldump_arg;
380int dumper_create(const struct dumperinfo *di_template, const char *devname,
381    const struct diocskerneldump_arg *kda, struct dumperinfo **dip);
382void dumper_destroy(struct dumperinfo *di);
383int dumper_insert(const struct dumperinfo *di_template, const char *devname,
384    const struct diocskerneldump_arg *kda);
385int dumper_remove(const char *devname, const struct diocskerneldump_arg *kda);
386
387/* For ddb(4)-time use only. */
388void dumper_ddb_insert(struct dumperinfo *);
389void dumper_ddb_remove(struct dumperinfo *);
390
391int dump_start(struct dumperinfo *di, struct kerneldumpheader *kdh);
392int dump_append(struct dumperinfo *, void *, size_t);
393int dump_write(struct dumperinfo *, void *, off_t, size_t);
394int dump_finish(struct dumperinfo *di, struct kerneldumpheader *kdh);
395void dump_init_header(const struct dumperinfo *di, struct kerneldumpheader *kdh,
396    const char *magic, uint32_t archver, uint64_t dumplen);
397
398#endif /* _KERNEL */
399
400#endif /* !_SYS_CONF_H_ */
401