]> git.saurik.com Git - apple/xnu.git/blame - libkern/firehose/chunk_private.h
xnu-6153.141.1.tar.gz
[apple/xnu.git] / libkern / firehose / chunk_private.h
CommitLineData
813fb2f6
A
1/*
2 * Copyright (c) 2016 Apple Inc. All rights reserved.
3 *
4 * @APPLE_APACHE_LICENSE_HEADER_START@
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * @APPLE_APACHE_LICENSE_HEADER_END@
19 */
20
21#ifndef __FIREHOSE_CHUNK_PRIVATE__
22#define __FIREHOSE_CHUNK_PRIVATE__
23
24#if KERNEL
25#include <machine/atomic.h>
26#endif
27#include <stdatomic.h>
28#include <sys/param.h>
29#include <os/base.h>
30#include "firehose_types_private.h"
31#include "tracepoint_private.h"
32
33__BEGIN_DECLS
34
35#define FIREHOSE_CHUNK_SIZE 4096ul
36
37#define FIREHOSE_CHUNK_POS_ENTRY_OFFS_INC (1ULL << 0)
38#define FIREHOSE_CHUNK_POS_PRIVATE_OFFS_INC (1ULL << 16)
39#define FIREHOSE_CHUNK_POS_REFCNT_INC (1ULL << 32)
40#define FIREHOSE_CHUNK_POS_FULL_BIT (1ULL << 56)
41#define FIREHOSE_CHUNK_POS_USABLE_FOR_STREAM(pos, stream) \
0a7de745 42 ((((pos).fcp_pos >> 48) & 0x1ff) == (uint16_t)stream)
813fb2f6
A
43
44typedef union {
45 _Atomic(uint64_t) fcp_atomic_pos;
46 uint64_t fcp_pos;
47 struct {
48 uint16_t fcp_next_entry_offs;
49 uint16_t fcp_private_offs;
50 uint8_t fcp_refcnt;
51 uint8_t fcp_qos;
52 uint8_t fcp_stream;
53 uint8_t fcp_flag_full : 1;
54 uint8_t fcp_flag_io : 1;
5ba3f43e
A
55 uint8_t fcp_quarantined : 1;
56 uint8_t _fcp_flag_unused : 5;
813fb2f6
A
57 };
58} firehose_chunk_pos_u;
59
60typedef struct firehose_chunk_s {
61 uint8_t fc_start[0];
62 firehose_chunk_pos_u fc_pos;
63 uint64_t fc_timestamp;
64 uint8_t fc_data[FIREHOSE_CHUNK_SIZE - 8 - 8];
65} *firehose_chunk_t;
66
67typedef struct firehose_chunk_range_s {
68 uint16_t fcr_offset; // offset from the start of the chunk
69 uint16_t fcr_length;
70} *firehose_chunk_range_t;
71
72#if defined(KERNEL) || defined(OS_FIREHOSE_SPI)
73
74OS_ALWAYS_INLINE
75static inline bool
76firehose_chunk_pos_fits(firehose_chunk_pos_u pos, uint16_t size)
77{
78 return pos.fcp_next_entry_offs + size <= pos.fcp_private_offs;
79}
80
81#define FIREHOSE_CHUNK_TRY_RESERVE_FAIL_ENQUEUE (-1)
82#define FIREHOSE_CHUNK_TRY_RESERVE_FAIL ( 0)
83
84OS_ALWAYS_INLINE
85static inline long
86firehose_chunk_tracepoint_try_reserve(firehose_chunk_t fc, uint64_t stamp,
0a7de745
A
87 firehose_stream_t stream, uint8_t qos, uint16_t pubsize,
88 uint16_t privsize, uint8_t **privptr)
813fb2f6
A
89{
90 const uint16_t ft_size = offsetof(struct firehose_tracepoint_s, ft_data);
91 firehose_chunk_pos_u orig, pos;
92 bool reservation_failed, stamp_delta_fits;
93
94 stamp_delta_fits = ((stamp - fc->fc_timestamp) >> 48) == 0;
95
96 // no acquire barrier because the returned space is written to only
97 os_atomic_rmw_loop(&fc->fc_pos.fcp_atomic_pos,
0a7de745 98 orig.fcp_pos, pos.fcp_pos, relaxed, {
813fb2f6 99 if (orig.fcp_pos == 0) {
0a7de745
A
100 // we acquired a really really old reference, and we probably
101 // just faulted in a new page
102 os_atomic_rmw_loop_give_up(return FIREHOSE_CHUNK_TRY_RESERVE_FAIL);
813fb2f6
A
103 }
104 if (!FIREHOSE_CHUNK_POS_USABLE_FOR_STREAM(orig, stream)) {
0a7de745
A
105 // nothing to do if the chunk is full, or the stream doesn't match,
106 // in which case the thread probably:
107 // - loaded the chunk ref
108 // - been suspended a long while
109 // - read the chunk to find a very old thing
110 os_atomic_rmw_loop_give_up(return FIREHOSE_CHUNK_TRY_RESERVE_FAIL);
813fb2f6
A
111 }
112 pos = orig;
113 if (!firehose_chunk_pos_fits(orig,
0a7de745
A
114 ft_size + pubsize + privsize) || !stamp_delta_fits) {
115 pos.fcp_flag_full = true;
116 reservation_failed = true;
813fb2f6 117 } else {
0a7de745
A
118 if (qos > pos.fcp_qos) {
119 pos.fcp_qos = qos;
813fb2f6 120 }
0a7de745
A
121 // using these *_INC macros is so that the compiler generates better
122 // assembly: using the struct individual fields forces the compiler
123 // to handle carry propagations, and we know it won't happen
124 pos.fcp_pos += roundup(ft_size + pubsize, 8) *
125 FIREHOSE_CHUNK_POS_ENTRY_OFFS_INC;
126 pos.fcp_pos -= privsize * FIREHOSE_CHUNK_POS_PRIVATE_OFFS_INC;
127 pos.fcp_pos += FIREHOSE_CHUNK_POS_REFCNT_INC;
128 const uint16_t minimum_payload_size = 16;
129 if (!firehose_chunk_pos_fits(pos,
130 roundup(ft_size + minimum_payload_size, 8))) {
131 // if we can't even have minimum_payload_size bytes of payload
132 // for the next tracepoint, just flush right away
133 pos.fcp_flag_full = true;
134 }
135 reservation_failed = false;
813fb2f6
A
136 }
137 });
138
139 if (reservation_failed) {
140 if (pos.fcp_refcnt) {
141 // nothing to do, there is a thread writing that will pick up
142 // the "FULL" flag on flush and push as a consequence
143 return FIREHOSE_CHUNK_TRY_RESERVE_FAIL;
144 }
145 // caller must enqueue chunk
146 return FIREHOSE_CHUNK_TRY_RESERVE_FAIL_ENQUEUE;
147 }
148 if (privptr) {
cb323159 149 *privptr = (uint8_t *)((uintptr_t)fc->fc_start + pos.fcp_private_offs);
813fb2f6
A
150 }
151 return orig.fcp_next_entry_offs;
152}
153
154OS_ALWAYS_INLINE
155static inline firehose_tracepoint_t
156firehose_chunk_tracepoint_begin(firehose_chunk_t fc, uint64_t stamp,
0a7de745 157 uint16_t pubsize, uint64_t thread_id, long offset)
813fb2f6
A
158{
159 firehose_tracepoint_t ft = (firehose_tracepoint_t)
cb323159 160 __builtin_assume_aligned((void *)((uintptr_t)fc->fc_start + (uintptr_t)offset), 8);
813fb2f6
A
161 stamp -= fc->fc_timestamp;
162 stamp |= (uint64_t)pubsize << 48;
163 // The compiler barrier is needed for userland process death handling, see
164 // (tracepoint-begin) in libdispatch's firehose_buffer_stream_chunk_install.
165 atomic_store_explicit(&ft->ft_atomic_stamp_and_length, stamp,
0a7de745
A
166 memory_order_relaxed);
167 __asm__ __volatile__ ("" ::: "memory");
813fb2f6
A
168 ft->ft_thread = thread_id;
169 return ft;
170}
171
172OS_ALWAYS_INLINE
173static inline bool
174firehose_chunk_tracepoint_end(firehose_chunk_t fc,
0a7de745 175 firehose_tracepoint_t ft, firehose_tracepoint_id_u ftid)
813fb2f6
A
176{
177 firehose_chunk_pos_u pos;
178
179 atomic_store_explicit(&ft->ft_id.ftid_atomic_value,
0a7de745 180 ftid.ftid_value, memory_order_release);
813fb2f6 181 pos.fcp_pos = atomic_fetch_sub_explicit(&fc->fc_pos.fcp_atomic_pos,
0a7de745 182 FIREHOSE_CHUNK_POS_REFCNT_INC, memory_order_relaxed);
813fb2f6
A
183 return pos.fcp_refcnt == 1 && pos.fcp_flag_full;
184}
185
186#endif // defined(KERNEL) || defined(OS_FIREHOSE_SPI)
187
188__END_DECLS
189
190#endif // __FIREHOSE_CHUNK_PRIVATE__