xref: /xnu-12377.41.6/bsd/net/net_thread_marks.c (revision bbb1b6f9e71b8cdde6e5cd6f4841f207dee3d828)
1 /*
2  * Copyright (c) 2024 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 
29 #include "kpi_interface.h"
30 #include <stddef.h>
31 
32 #include <net/dlil.h>
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/mbuf.h>
39 #include <sys/socket.h>
40 #include <sys/domain.h>
41 #include <sys/user.h>
42 
43 #include <kern/assert.h>
44 #include <kern/task.h>
45 #include <kern/thread.h>
46 #include <kern/sched_prim.h>
47 #include <kern/locks.h>
48 #include <kern/zalloc.h>
49 
50 struct net_thread_marks { };
51 static const struct net_thread_marks net_thread_marks_base = { };
52 
53 __private_extern__ const net_thread_marks_t net_thread_marks_none =
54     &net_thread_marks_base;
55 
56 __private_extern__ net_thread_marks_t
net_thread_marks_push(u_int32_t push)57 net_thread_marks_push(u_int32_t push)
58 {
59 	static const char *__unsafe_indexable const base = (const void*__single)&net_thread_marks_base;
60 	u_int32_t pop = 0;
61 
62 	if (push != 0) {
63 		struct uthread *uth = current_uthread();
64 
65 		pop = push & ~uth->uu_network_marks;
66 		if (pop != 0) {
67 			uth->uu_network_marks |= pop;
68 		}
69 	}
70 
71 	return __unsafe_forge_single(net_thread_marks_t, (base + pop));
72 }
73 
74 __private_extern__ net_thread_marks_t
net_thread_unmarks_push(u_int32_t unpush)75 net_thread_unmarks_push(u_int32_t unpush)
76 {
77 	static const char *__unsafe_indexable const base = (const void*__single)&net_thread_marks_base;
78 	u_int32_t unpop = 0;
79 
80 	if (unpush != 0) {
81 		struct uthread *uth = current_uthread();
82 
83 		unpop = unpush & uth->uu_network_marks;
84 		if (unpop != 0) {
85 			uth->uu_network_marks &= ~unpop;
86 		}
87 	}
88 
89 	return __unsafe_forge_single(net_thread_marks_t, (base + unpop));
90 }
91 
92 __private_extern__ void
net_thread_marks_pop(net_thread_marks_t popx)93 net_thread_marks_pop(net_thread_marks_t popx)
94 {
95 	static const char *__unsafe_indexable const base = (const void*__single)&net_thread_marks_base;
96 	const ptrdiff_t pop = (const char *)popx - (const char *)base;
97 	if (pop != 0) {
98 		static const ptrdiff_t ones = (ptrdiff_t)(u_int32_t)~0U;
99 		struct uthread *uth = current_uthread();
100 
101 		VERIFY((pop & ones) == pop);
102 		VERIFY((ptrdiff_t)(uth->uu_network_marks & pop) == pop);
103 		uth->uu_network_marks &= ~pop;
104 	}
105 }
106 
107 __private_extern__ void
net_thread_unmarks_pop(net_thread_marks_t unpopx)108 net_thread_unmarks_pop(net_thread_marks_t unpopx)
109 {
110 	static const char *__unsafe_indexable const base = (const void*__single)&net_thread_marks_base;
111 	ptrdiff_t unpop = (const char *)unpopx - (const char *)base;
112 
113 	if (unpop != 0) {
114 		static const ptrdiff_t ones = (ptrdiff_t)(u_int32_t)~0U;
115 		struct uthread *uth = current_uthread();
116 
117 		VERIFY((unpop & ones) == unpop);
118 		VERIFY((ptrdiff_t)(uth->uu_network_marks & unpop) == 0);
119 		uth->uu_network_marks |= (u_int32_t)unpop;
120 	}
121 }
122 
123 
124 __private_extern__ u_int32_t
net_thread_is_marked(u_int32_t check)125 net_thread_is_marked(u_int32_t check)
126 {
127 	if (check != 0) {
128 		struct uthread *uth = current_uthread();
129 		return uth->uu_network_marks & check;
130 	} else {
131 		return 0;
132 	}
133 }
134 
135 __private_extern__ u_int32_t
net_thread_is_unmarked(u_int32_t check)136 net_thread_is_unmarked(u_int32_t check)
137 {
138 	if (check != 0) {
139 		struct uthread *uth = current_uthread();
140 		return ~uth->uu_network_marks & check;
141 	} else {
142 		return 0;
143 	}
144 }
145