xref: /dragonfly/sys/sys/upmap.h (revision adb6cc9d)
1 /*
2  * Copyright (c) 2014 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
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  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #ifndef	_SYS_UPMAP_H_
36 #define	_SYS_UPMAP_H_
37 
38 #ifndef _SYS_TYPES_H_
39 #include <sys/types.h>
40 #endif
41 #ifndef _SYS_TIME_H_
42 #include <sys/time.h>
43 #endif
44 
45 #define UPMAP_MAXPROCTITLE	1024
46 #define LPMAP_MAXTHREADTITLE	1024
47 #define LPMAP_MAPSIZE		65536
48 #define UPMAP_MAPSIZE		65536
49 #define KPMAP_MAPSIZE		65536
50 
51 #define LPMAP_VERSION		1
52 #define UPMAP_VERSION		1
53 #define KPMAP_VERSION		1
54 
55 typedef uint64_t	forkid_t;
56 
57 typedef struct ukpheader {
58 	uint16_t	type;		/* element type */
59 	uint16_t	offset;		/* offset from map base, max 65535 */
60 } ukpheader_t;
61 
62 #define UKPLEN_MASK		0x0F00
63 #define UKPLEN_1		0x0000
64 #define UKPLEN_2		0x0100
65 #define UKPLEN_4		0x0200
66 #define UKPLEN_8		0x0300
67 #define UKPLEN_16		0x0400
68 #define UKPLEN_32		0x0500
69 #define UKPLEN_64		0x0600
70 #define UKPLEN_128		0x0700
71 #define UKPLEN_256		0x0800
72 #define UKPLEN_512		0x0900
73 #define UKPLEN_1024		0x0A00
74 
75 #define UKPLEN_TS		((sizeof(struct timespec) == 8) ? \
76 					UKPLEN_16 : UKPLEN_32)
77 #define UKPLEN_DECODE(type)	(1 << ((type >> 8) & 0x0F))
78 
79 /*
80  * Global types - may exist in all three mapping types
81  */
82 #define UKPTYPE_VERSION		(0x0001 | UKPLEN_4)	/* always first */
83 
84 /*
85  * /dev/lpmap - per-thread
86  */
87 #define LPTYPE_RESERVEDINT0	(0x4010 | UKPLEN_4)
88 #define LPTYPE_RESERVEDINT1	(0x4011 | UKPLEN_4)
89 #define LPTYPE_BLOCKALLSIGS	(0x4012 | UKPLEN_4)
90 #define LPTYPE_THREAD_TITLE	(0x4013 | UKPLEN_1024)
91 #define LPTYPE_THREAD_TID	(0x4014 | UKPLEN_4)
92 
93 /*
94  * /dev/upmap - per-process
95  */
96 #define UPTYPE_RUNTICKS		(0x0010 | UKPLEN_4)
97 #define UPTYPE_FORKID		(0x0011 | UKPLEN_8)
98 #define UPTYPE_PID		(0x0012 | UKPLEN_4)
99 #define UPTYPE_PROC_TITLE	(0x0013 | UKPLEN_1024)
100 #define UPTYPE_INVFORK		(0x0014 | UKPLEN_4)
101 
102 /*
103  * /dev/kpmap - kernel-wide
104  */
105 #define KPTYPE_UPTICKS		(0x8000 | UKPLEN_4)
106 #define KPTYPE_TS_UPTIME	(0x8001 | UKPLEN_TS)
107 #define KPTYPE_TS_REALTIME	(0x8002 | UKPLEN_TS)
108 #define KPTYPE_TSC_FREQ		(0x8003 | UKPLEN_8)
109 #define KPTYPE_TICK_FREQ	(0x8004 | UKPLEN_8)
110 #define KPTYPE_FAST_GTOD	(0x8005 | UKPLEN_4)
111 
112 #if defined(_KERNEL) || defined(_KERNEL_STRUCTURES)
113 
114 /*
115  * (writable) user per-thread map via /dev/lpmap.
116  *
117  * ABSOLUTE LOCATIONS CAN CHANGE, ITERATE HEADERS FOR THE TYPE YOU DESIRE
118  * UNTIL YOU HIT TYPE 0, THEN CACHE THE RESULTING POINTER.
119  *
120  * If you insist, at least check that the version matches LPMAP_VERSION.
121  *
122  * --
123  *
124  * The current thread can block all blockable signals by (atomically)
125  * incrementing blockallsigs.  If the kernel receives a signal while
126  * the low 31 bits of blockallsigs are non-zero, the received signal
127  * will be made pending but not acted upon and bit 31 of blockallsigs
128  * will be set.  The signal mask is not affected.
129  *
130  * Upon decrementing blockallsigs to 0 (low 31 bits to 0), again atomically,
131  * userland should then check to see if bit 31 is set, clear it, and then
132  * issue any real system call to force the kernel to re-check pending signals
133  * and act upon them.
134  */
135 struct sys_lpmap {
136 	ukpheader_t	header[64];
137 	uint32_t	version;
138 	uint32_t	tid;
139 	uint32_t	reserved02;
140 	uint32_t	blockallsigs;
141 	char		thread_title[LPMAP_MAXTHREADTITLE];
142 };
143 
144 /*
145  * (writable) user per-process map via /dev/upmap.
146  *
147  * ABSOLUTE LOCATIONS CAN CHANGE, ITERATE HEADERS FOR THE TYPE YOU DESIRE
148  * UNTIL YOU HIT TYPE 0, THEN CACHE THE RESULTING POINTER.
149  *
150  * If you insist, at least check that the version matches UPMAP_VERSION.
151  */
152 struct sys_upmap {
153 	ukpheader_t	header[64];
154 	uint32_t	version;
155 	uint32_t	runticks;	/* running scheduler ticks */
156 	forkid_t	forkid;		/* unique 2^64 (fork detect) NOT MONO */
157 	uint32_t	invfork;	/* vfork active */
158 	pid_t		pid;		/* process id */
159 	uint32_t	reserved[16];
160 	char		proc_title[UPMAP_MAXPROCTITLE];
161 };
162 
163 /*
164  * (read-only) kernel per-cpu map via /dev/kpmap.
165  *
166  * ABSOLUTE LOCATIONS CAN CHANGE, ITERATE HEADERS FOR THE TYPE YOU DESIRE
167  * UNTIL YOU HIT TYPE 0, THEN CACHE THE RESULTING POINTER.
168  *
169  * If you insist, at least check that the version matches KPMAP_VERSION.
170  *
171  * Procedure for reading stable values from ts_uptime/ts_realtime.  This
172  * avoids looping in nearly all cases, including during a kernel update.
173  * The only case where this might loop is if the kernel deschedules
174  * the user thread for more than 1 tick.
175  *
176  *	do {
177  *		w = upticks;
178  *		cpu_lfence();
179  *		load ts_uptime[w & 1] or ts_realtime[w & 1]
180  *		cpu_lfence();
181  *		w = upticks - w;
182  *	} while (w > 1);
183  */
184 struct sys_kpmap {
185 	ukpheader_t	header[64];
186 	int32_t		version;
187 	int32_t		upticks;	/* userland reads ts_*[upticks & 1] */
188 	struct timespec	ts_uptime[2];	/* mono uptime @ticks (uncompensated) */
189 	struct timespec ts_realtime[2];	/* realtime @ticks resolution */
190 	int64_t		tsc_freq;	/* (if supported by cpu) */
191 	int32_t		tick_freq;	/* scheduler tick frequency */
192 	int32_t		fast_gtod;	/* fast gettimeofday() */
193 };
194 
195 #endif
196 
197 #ifdef _KERNEL
198 extern struct sys_kpmap *kpmap;
199 #endif
200 
201 #endif
202