xref: /dragonfly/sys/sys/upmap.h (revision 279dd846)
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 UPMAP_MAPSIZE		65536
47 #define KPMAP_MAPSIZE		65536
48 
49 #define UPMAP_VERSION		1
50 #define KPMAP_VERSION		1
51 
52 typedef uint64_t	forkid_t;
53 
54 typedef struct ukpheader {
55 	uint16_t	type;		/* element type */
56 	uint16_t	offset;		/* offset from map base, max 65535 */
57 } ukpheader_t;
58 
59 #define UKPLEN_MASK		0x0F00
60 #define UKPLEN_1		0x0000
61 #define UKPLEN_2		0x0100
62 #define UKPLEN_4		0x0200
63 #define UKPLEN_8		0x0300
64 #define UKPLEN_16		0x0400
65 #define UKPLEN_32		0x0500
66 #define UKPLEN_64		0x0600
67 #define UKPLEN_128		0x0700
68 #define UKPLEN_256		0x0800
69 #define UKPLEN_512		0x0900
70 #define UKPLEN_1024		0x0A00
71 
72 #define UKPLEN_TS		((sizeof(struct timespec) == 8) ? \
73 					UKPLEN_16 : UKPLEN_32)
74 #define UKPLEN_DECODE(type)	(1 << ((type >> 8) & 0x0F))
75 
76 #define UKPTYPE_VERSION		(0x0001 | UKPLEN_4)	/* always first */
77 
78 #define UPTYPE_RUNTICKS		(0x0010 | UKPLEN_4)
79 #define UPTYPE_FORKID		(0x0011 | UKPLEN_8)
80 #define UPTYPE_PID		(0x0012 | UKPLEN_4)
81 #define UPTYPE_PROC_TITLE	(0x0013 | UKPLEN_1024)
82 #define UPTYPE_INVFORK		(0x0014 | UKPLEN_4)
83 
84 #define KPTYPE_UPTICKS		(0x8000 | UKPLEN_4)
85 #define KPTYPE_TS_UPTIME	(0x8001 | UKPLEN_TS)
86 #define KPTYPE_TS_REALTIME	(0x8002 | UKPLEN_TS)
87 #define KPTYPE_TSC_FREQ		(0x8003 | UKPLEN_8)
88 #define KPTYPE_TICK_FREQ	(0x8004 | UKPLEN_8)
89 
90 #if defined(_KERNEL) || defined(_KERNEL_STRUCTURES)
91 
92 /*
93  * (writable) user per-process map via /dev/upmap.
94  *
95  * ABSOLUTE LOCATIONS CAN CHANGE, ITERATE HEADERS FOR THE TYPE YOU DESIRE
96  * UNTIL YOU HIT TYPE 0, THEN CACHE THE RESULTING POINTER.
97  *
98  * If you insist, at least check that the version matches UPMAP_VERSION.
99  */
100 struct sys_upmap {
101 	ukpheader_t	header[64];
102 	uint32_t	version;
103 	uint32_t	runticks;	/* running scheduler ticks */
104 	forkid_t	forkid;		/* unique 2^64 (fork detect) NOT MONO */
105 	uint32_t	invfork;	/* vfork active */
106 	pid_t		pid;		/* process id */
107 	uint32_t	reserved[16];
108 	char		proc_title[UPMAP_MAXPROCTITLE];
109 };
110 
111 /*
112  * (read-only) kernel per-cpu map via /dev/kpmap.
113  *
114  * ABSOLUTE LOCATIONS CAN CHANGE, ITERATE HEADERS FOR THE TYPE YOU DESIRE
115  * UNTIL YOU HIT TYPE 0, THEN CACHE THE RESULTING POINTER.
116  *
117  * If you insist, at least check that the version matches KPMAP_VERSION.
118  *
119  * Procedure for reading stable values from ts_uptime/ts_realtime.  This
120  * avoids looping in nearly all cases, including during a kernel update.
121  * The only case where this might loop is if the kernel deschedules
122  * the user thread for more than 1 tick.
123  *
124  *	do {
125  *		w = upticks;
126  *		cpu_lfence();
127  *		load ts_uptime[w & 1] or ts_realtime[w & 1]
128  *		cpu_lfence();
129  *		w = upticks - w;
130  *	} while (w > 1);
131  */
132 struct sys_kpmap {
133 	ukpheader_t	header[64];
134 	int32_t		version;
135 	int32_t		upticks;	/* userland reads ts_*[upticks & 1] */
136 	struct timespec	ts_uptime[2];	/* mono uptime @ticks (uncompensated) */
137 	struct timespec ts_realtime[2];	/* realtime @ticks resolution */
138 	int64_t		tsc_freq;	/* (if supported by cpu) */
139 	int32_t		tick_freq;	/* scheduler tick frequency */
140 };
141 
142 #endif
143 
144 #ifdef _KERNEL
145 extern struct sys_kpmap *kpmap;
146 #endif
147 
148 #endif
149