1*1031c584SApple OSS Distributions /*
2*1031c584SApple OSS Distributions * Copyright (c) 2015-2020 Apple Inc. All rights reserved.
3*1031c584SApple OSS Distributions *
4*1031c584SApple OSS Distributions * @APPLE_LICENSE_HEADER_START@
5*1031c584SApple OSS Distributions *
6*1031c584SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*1031c584SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*1031c584SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*1031c584SApple OSS Distributions * compliance with the License. Please obtain a copy of the License at
10*1031c584SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this
11*1031c584SApple OSS Distributions * file.
12*1031c584SApple OSS Distributions *
13*1031c584SApple OSS Distributions * The Original Code and all software distributed under the License are
14*1031c584SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15*1031c584SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16*1031c584SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17*1031c584SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18*1031c584SApple OSS Distributions * Please see the License for the specific language governing rights and
19*1031c584SApple OSS Distributions * limitations under the License.
20*1031c584SApple OSS Distributions *
21*1031c584SApple OSS Distributions * @APPLE_LICENSE_HEADER_END@
22*1031c584SApple OSS Distributions */
23*1031c584SApple OSS Distributions
24*1031c584SApple OSS Distributions #include <stdbool.h>
25*1031c584SApple OSS Distributions #include <firehose/tracepoint_private.h>
26*1031c584SApple OSS Distributions #include <kern/assert.h>
27*1031c584SApple OSS Distributions #include <kern/counter.h>
28*1031c584SApple OSS Distributions #include <kern/locks.h>
29*1031c584SApple OSS Distributions #include <pexpert/pexpert.h>
30*1031c584SApple OSS Distributions #include <sys/param.h>
31*1031c584SApple OSS Distributions
32*1031c584SApple OSS Distributions #if __has_feature(ptrauth_calls)
33*1031c584SApple OSS Distributions #include <mach/vm_param.h>
34*1031c584SApple OSS Distributions #include <ptrauth.h>
35*1031c584SApple OSS Distributions #endif /* __has_feature(ptrauth_calls) */
36*1031c584SApple OSS Distributions
37*1031c584SApple OSS Distributions #include "log_encode.h"
38*1031c584SApple OSS Distributions #include "log_mem.h"
39*1031c584SApple OSS Distributions
40*1031c584SApple OSS Distributions #define LOG_FMT_MAX_PRECISION (1024)
41*1031c584SApple OSS Distributions #define log_context_cursor(ctx) &(ctx)->ctx_hdr->hdr_data[(ctx)->ctx_content_off]
42*1031c584SApple OSS Distributions
43*1031c584SApple OSS Distributions extern boolean_t doprnt_hide_pointers;
44*1031c584SApple OSS Distributions
45*1031c584SApple OSS Distributions SCALABLE_COUNTER_DEFINE(oslog_p_fmt_invalid_msgcount);
46*1031c584SApple OSS Distributions SCALABLE_COUNTER_DEFINE(oslog_p_fmt_max_args_msgcount);
47*1031c584SApple OSS Distributions SCALABLE_COUNTER_DEFINE(oslog_p_truncated_msgcount);
48*1031c584SApple OSS Distributions
49*1031c584SApple OSS Distributions static bool
is_digit(char ch)50*1031c584SApple OSS Distributions is_digit(char ch)
51*1031c584SApple OSS Distributions {
52*1031c584SApple OSS Distributions return (ch >= '0') && (ch <= '9');
53*1031c584SApple OSS Distributions }
54*1031c584SApple OSS Distributions
55*1031c584SApple OSS Distributions static bool
is_kernel_pointer(void * arg,size_t arg_len)56*1031c584SApple OSS Distributions is_kernel_pointer(void *arg, size_t arg_len)
57*1031c584SApple OSS Distributions {
58*1031c584SApple OSS Distributions if (arg_len < sizeof(void *)) {
59*1031c584SApple OSS Distributions return false;
60*1031c584SApple OSS Distributions }
61*1031c584SApple OSS Distributions
62*1031c584SApple OSS Distributions unsigned long long value = 0;
63*1031c584SApple OSS Distributions assert(arg_len <= sizeof(value));
64*1031c584SApple OSS Distributions (void) memcpy(&value, arg, arg_len);
65*1031c584SApple OSS Distributions
66*1031c584SApple OSS Distributions #if __has_feature(ptrauth_calls)
67*1031c584SApple OSS Distributions /**
68*1031c584SApple OSS Distributions * Strip out the pointer authentication code before
69*1031c584SApple OSS Distributions * checking whether the pointer is a kernel address.
70*1031c584SApple OSS Distributions */
71*1031c584SApple OSS Distributions value = (unsigned long long)VM_KERNEL_STRIP_PTR(value);
72*1031c584SApple OSS Distributions #endif /* __has_feature(ptrauth_calls) */
73*1031c584SApple OSS Distributions
74*1031c584SApple OSS Distributions return value >= VM_MIN_KERNEL_AND_KEXT_ADDRESS && value <= VM_MAX_KERNEL_ADDRESS;
75*1031c584SApple OSS Distributions }
76*1031c584SApple OSS Distributions
77*1031c584SApple OSS Distributions static void
log_context_cursor_advance(os_log_context_t ctx,size_t amount)78*1031c584SApple OSS Distributions log_context_cursor_advance(os_log_context_t ctx, size_t amount)
79*1031c584SApple OSS Distributions {
80*1031c584SApple OSS Distributions ctx->ctx_content_off += amount;
81*1031c584SApple OSS Distributions assert(log_context_cursor(ctx) <= (ctx->ctx_buffer + ctx->ctx_buffer_sz));
82*1031c584SApple OSS Distributions }
83*1031c584SApple OSS Distributions
84*1031c584SApple OSS Distributions static bool
log_fits(os_log_context_t ctx,size_t data_size)85*1031c584SApple OSS Distributions log_fits(os_log_context_t ctx, size_t data_size)
86*1031c584SApple OSS Distributions {
87*1031c584SApple OSS Distributions return (ctx->ctx_content_off + data_size) <= ctx->ctx_content_sz;
88*1031c584SApple OSS Distributions }
89*1031c584SApple OSS Distributions
90*1031c584SApple OSS Distributions static bool
log_fits_cmd(os_log_context_t ctx,size_t data_size)91*1031c584SApple OSS Distributions log_fits_cmd(os_log_context_t ctx, size_t data_size)
92*1031c584SApple OSS Distributions {
93*1031c584SApple OSS Distributions return log_fits(ctx, sizeof(*ctx->ctx_hdr) + data_size);
94*1031c584SApple OSS Distributions }
95*1031c584SApple OSS Distributions
96*1031c584SApple OSS Distributions static void
log_range_update(os_log_fmt_range_t range,uint16_t offset,uint16_t length)97*1031c584SApple OSS Distributions log_range_update(os_log_fmt_range_t range, uint16_t offset, uint16_t length)
98*1031c584SApple OSS Distributions {
99*1031c584SApple OSS Distributions range->offset = offset;
100*1031c584SApple OSS Distributions /*
101*1031c584SApple OSS Distributions * Truncated flag may have already been set earlier, hence do not
102*1031c584SApple OSS Distributions * overwrite it blindly.
103*1031c584SApple OSS Distributions */
104*1031c584SApple OSS Distributions if (length < range->length) {
105*1031c584SApple OSS Distributions range->truncated = true;
106*1031c584SApple OSS Distributions }
107*1031c584SApple OSS Distributions range->length = length;
108*1031c584SApple OSS Distributions }
109*1031c584SApple OSS Distributions
110*1031c584SApple OSS Distributions /*
111*1031c584SApple OSS Distributions * Stores a command in the main section. The value itself is wrapped in
112*1031c584SApple OSS Distributions * the os_log_fmt_cmd_t struct.
113*1031c584SApple OSS Distributions */
114*1031c584SApple OSS Distributions static void
log_add_cmd(os_log_context_t ctx,os_log_fmt_cmd_type_t type,uint8_t flags,void * arg,size_t arg_size)115*1031c584SApple OSS Distributions log_add_cmd(os_log_context_t ctx, os_log_fmt_cmd_type_t type, uint8_t flags,
116*1031c584SApple OSS Distributions void *arg, size_t arg_size)
117*1031c584SApple OSS Distributions {
118*1031c584SApple OSS Distributions os_log_fmt_cmd_t cmd;
119*1031c584SApple OSS Distributions const size_t cmd_sz = sizeof(*cmd) + arg_size;
120*1031c584SApple OSS Distributions
121*1031c584SApple OSS Distributions assert(log_fits_cmd(ctx, cmd_sz));
122*1031c584SApple OSS Distributions assert(arg_size <= UINT8_MAX);
123*1031c584SApple OSS Distributions
124*1031c584SApple OSS Distributions cmd = (os_log_fmt_cmd_t)log_context_cursor(ctx);
125*1031c584SApple OSS Distributions cmd->cmd_type = type;
126*1031c584SApple OSS Distributions cmd->cmd_flags = flags;
127*1031c584SApple OSS Distributions cmd->cmd_size = (uint8_t)arg_size;
128*1031c584SApple OSS Distributions (void) memcpy(cmd->cmd_data, arg, cmd->cmd_size);
129*1031c584SApple OSS Distributions
130*1031c584SApple OSS Distributions assert(cmd_sz == sizeof(*cmd) + cmd->cmd_size);
131*1031c584SApple OSS Distributions log_context_cursor_advance(ctx, cmd_sz);
132*1031c584SApple OSS Distributions }
133*1031c584SApple OSS Distributions
134*1031c584SApple OSS Distributions /*
135*1031c584SApple OSS Distributions * Collect details about argument which needs to be stored in the pubdata
136*1031c584SApple OSS Distributions * section.
137*1031c584SApple OSS Distributions */
138*1031c584SApple OSS Distributions static void
log_collect_public_range_data(os_log_context_t ctx,os_log_fmt_range_t range,void * arg)139*1031c584SApple OSS Distributions log_collect_public_range_data(os_log_context_t ctx, os_log_fmt_range_t range, void *arg)
140*1031c584SApple OSS Distributions {
141*1031c584SApple OSS Distributions ctx->ctx_pubdata[ctx->ctx_pubdata_cnt++] = (char *)arg;
142*1031c584SApple OSS Distributions ctx->ctx_pubdata_sz += range->length;
143*1031c584SApple OSS Distributions }
144*1031c584SApple OSS Distributions
145*1031c584SApple OSS Distributions static void
log_add_range_data(os_log_context_t ctx,os_log_fmt_range_t range,void * arg)146*1031c584SApple OSS Distributions log_add_range_data(os_log_context_t ctx, os_log_fmt_range_t range, void *arg)
147*1031c584SApple OSS Distributions {
148*1031c584SApple OSS Distributions assert(log_fits(ctx, range->length));
149*1031c584SApple OSS Distributions (void) memcpy(log_context_cursor(ctx), arg, range->length);
150*1031c584SApple OSS Distributions log_context_cursor_advance(ctx, range->length);
151*1031c584SApple OSS Distributions }
152*1031c584SApple OSS Distributions
153*1031c584SApple OSS Distributions static struct os_log_fmt_range_s
log_create_range(os_log_context_t ctx,size_t arg_len)154*1031c584SApple OSS Distributions log_create_range(os_log_context_t ctx, size_t arg_len)
155*1031c584SApple OSS Distributions {
156*1031c584SApple OSS Distributions const size_t final_arg_len = MIN(arg_len, UINT16_MAX);
157*1031c584SApple OSS Distributions
158*1031c584SApple OSS Distributions return (struct os_log_fmt_range_s) {
159*1031c584SApple OSS Distributions .offset = ctx->ctx_pubdata_sz,
160*1031c584SApple OSS Distributions .length = (uint16_t)final_arg_len,
161*1031c584SApple OSS Distributions .truncated = (final_arg_len < arg_len)
162*1031c584SApple OSS Distributions };
163*1031c584SApple OSS Distributions }
164*1031c584SApple OSS Distributions
165*1031c584SApple OSS Distributions static int
log_add_range_arg(os_log_context_t ctx,os_log_fmt_cmd_type_t type,os_log_fmt_cmd_flags_t flags,void * arg,size_t arg_len)166*1031c584SApple OSS Distributions log_add_range_arg(os_log_context_t ctx, os_log_fmt_cmd_type_t type, os_log_fmt_cmd_flags_t flags,
167*1031c584SApple OSS Distributions void *arg, size_t arg_len)
168*1031c584SApple OSS Distributions {
169*1031c584SApple OSS Distributions struct os_log_fmt_range_s range;
170*1031c584SApple OSS Distributions
171*1031c584SApple OSS Distributions if (!log_fits_cmd(ctx, sizeof(range))) {
172*1031c584SApple OSS Distributions return ENOMEM;
173*1031c584SApple OSS Distributions }
174*1031c584SApple OSS Distributions
175*1031c584SApple OSS Distributions range = log_create_range(ctx, arg_len);
176*1031c584SApple OSS Distributions
177*1031c584SApple OSS Distributions if (flags == OSLF_CMD_FLAG_PUBLIC) {
178*1031c584SApple OSS Distributions if (ctx->ctx_pubdata_cnt == OS_LOG_MAX_PUB_ARGS) {
179*1031c584SApple OSS Distributions return ENOMEM;
180*1031c584SApple OSS Distributions }
181*1031c584SApple OSS Distributions assert(ctx->ctx_pubdata_cnt < OS_LOG_MAX_PUB_ARGS);
182*1031c584SApple OSS Distributions log_collect_public_range_data(ctx, &range, arg);
183*1031c584SApple OSS Distributions }
184*1031c584SApple OSS Distributions log_add_cmd(ctx, type, flags, &range, sizeof(range));
185*1031c584SApple OSS Distributions ctx->ctx_hdr->hdr_cmd_cnt++;
186*1031c584SApple OSS Distributions
187*1031c584SApple OSS Distributions return 0;
188*1031c584SApple OSS Distributions }
189*1031c584SApple OSS Distributions
190*1031c584SApple OSS Distributions /*
191*1031c584SApple OSS Distributions * Adds a scalar argument value to the main section.
192*1031c584SApple OSS Distributions */
193*1031c584SApple OSS Distributions static int
log_add_arg(os_log_context_t ctx,os_log_fmt_cmd_type_t type,void * arg,size_t arg_len)194*1031c584SApple OSS Distributions log_add_arg(os_log_context_t ctx, os_log_fmt_cmd_type_t type, void *arg, size_t arg_len)
195*1031c584SApple OSS Distributions {
196*1031c584SApple OSS Distributions assert(type == OSLF_CMD_TYPE_COUNT || type == OSLF_CMD_TYPE_SCALAR);
197*1031c584SApple OSS Distributions assert(arg_len < UINT16_MAX);
198*1031c584SApple OSS Distributions
199*1031c584SApple OSS Distributions if (log_fits_cmd(ctx, arg_len)) {
200*1031c584SApple OSS Distributions log_add_cmd(ctx, type, OSLF_CMD_FLAG_PUBLIC, arg, arg_len);
201*1031c584SApple OSS Distributions ctx->ctx_hdr->hdr_cmd_cnt++;
202*1031c584SApple OSS Distributions return 0;
203*1031c584SApple OSS Distributions }
204*1031c584SApple OSS Distributions
205*1031c584SApple OSS Distributions return ENOMEM;
206*1031c584SApple OSS Distributions }
207*1031c584SApple OSS Distributions
208*1031c584SApple OSS Distributions static void
log_encode_public_data(os_log_context_t ctx)209*1031c584SApple OSS Distributions log_encode_public_data(os_log_context_t ctx)
210*1031c584SApple OSS Distributions {
211*1031c584SApple OSS Distributions const uint16_t orig_content_off = ctx->ctx_content_off;
212*1031c584SApple OSS Distributions os_log_fmt_hdr_t const hdr = ctx->ctx_hdr;
213*1031c584SApple OSS Distributions os_log_fmt_cmd_t cmd = (os_log_fmt_cmd_t)hdr->hdr_data;
214*1031c584SApple OSS Distributions
215*1031c584SApple OSS Distributions assert(ctx->ctx_pubdata_cnt <= hdr->hdr_cmd_cnt);
216*1031c584SApple OSS Distributions
217*1031c584SApple OSS Distributions for (int i = 0, pub_i = 0; i < hdr->hdr_cmd_cnt; i++, cmd = (os_log_fmt_cmd_t)(cmd->cmd_data + cmd->cmd_size)) {
218*1031c584SApple OSS Distributions if (cmd->cmd_type != OSLF_CMD_TYPE_STRING) {
219*1031c584SApple OSS Distributions continue;
220*1031c584SApple OSS Distributions }
221*1031c584SApple OSS Distributions
222*1031c584SApple OSS Distributions os_log_fmt_range_t const range __attribute__((aligned(8))) = (os_log_fmt_range_t)&cmd->cmd_data;
223*1031c584SApple OSS Distributions
224*1031c584SApple OSS Distributions // Fix offset and length of the argument data in the hdr.
225*1031c584SApple OSS Distributions log_range_update(range, ctx->ctx_content_off - orig_content_off,
226*1031c584SApple OSS Distributions MIN(range->length, ctx->ctx_content_sz - ctx->ctx_content_off));
227*1031c584SApple OSS Distributions
228*1031c584SApple OSS Distributions if (range->truncated) {
229*1031c584SApple OSS Distributions ctx->ctx_truncated = true;
230*1031c584SApple OSS Distributions }
231*1031c584SApple OSS Distributions
232*1031c584SApple OSS Distributions assert(pub_i < ctx->ctx_pubdata_cnt);
233*1031c584SApple OSS Distributions log_add_range_data(ctx, range, ctx->ctx_pubdata[pub_i++]);
234*1031c584SApple OSS Distributions }
235*1031c584SApple OSS Distributions }
236*1031c584SApple OSS Distributions
237*1031c584SApple OSS Distributions static bool
log_expand(os_log_context_t ctx,size_t new_size)238*1031c584SApple OSS Distributions log_expand(os_log_context_t ctx, size_t new_size)
239*1031c584SApple OSS Distributions {
240*1031c584SApple OSS Distributions assert(new_size > ctx->ctx_buffer_sz);
241*1031c584SApple OSS Distributions
242*1031c584SApple OSS Distributions if (!oslog_is_safe()) {
243*1031c584SApple OSS Distributions return false;
244*1031c584SApple OSS Distributions }
245*1031c584SApple OSS Distributions
246*1031c584SApple OSS Distributions size_t final_size = new_size;
247*1031c584SApple OSS Distributions
248*1031c584SApple OSS Distributions void *buf = logmem_alloc_locked(ctx->ctx_logmem, &final_size);
249*1031c584SApple OSS Distributions if (!buf) {
250*1031c584SApple OSS Distributions return false;
251*1031c584SApple OSS Distributions }
252*1031c584SApple OSS Distributions assert(final_size >= new_size);
253*1031c584SApple OSS Distributions
254*1031c584SApple OSS Distributions // address length header + already stored data
255*1031c584SApple OSS Distributions const size_t hdr_size = (uint8_t *)ctx->ctx_hdr - ctx->ctx_buffer;
256*1031c584SApple OSS Distributions const size_t copy_size = hdr_size + sizeof(*ctx->ctx_hdr) + ctx->ctx_content_sz;
257*1031c584SApple OSS Distributions assert(copy_size <= new_size);
258*1031c584SApple OSS Distributions (void) memcpy(buf, ctx->ctx_buffer, copy_size);
259*1031c584SApple OSS Distributions
260*1031c584SApple OSS Distributions if (ctx->ctx_allocated) {
261*1031c584SApple OSS Distributions logmem_free_locked(ctx->ctx_logmem, ctx->ctx_buffer, ctx->ctx_buffer_sz);
262*1031c584SApple OSS Distributions }
263*1031c584SApple OSS Distributions
264*1031c584SApple OSS Distributions ctx->ctx_buffer = buf;
265*1031c584SApple OSS Distributions ctx->ctx_buffer_sz = final_size;
266*1031c584SApple OSS Distributions ctx->ctx_content_sz = (uint16_t)(ctx->ctx_buffer_sz - hdr_size - sizeof(*ctx->ctx_hdr));
267*1031c584SApple OSS Distributions ctx->ctx_hdr = (os_log_fmt_hdr_t)&ctx->ctx_buffer[hdr_size];
268*1031c584SApple OSS Distributions ctx->ctx_allocated = true;
269*1031c584SApple OSS Distributions
270*1031c584SApple OSS Distributions return true;
271*1031c584SApple OSS Distributions }
272*1031c584SApple OSS Distributions
273*1031c584SApple OSS Distributions static int
log_encode_fmt_arg(void * arg,size_t arg_len,os_log_fmt_cmd_type_t type,os_log_context_t ctx)274*1031c584SApple OSS Distributions log_encode_fmt_arg(void *arg, size_t arg_len, os_log_fmt_cmd_type_t type, os_log_context_t ctx)
275*1031c584SApple OSS Distributions {
276*1031c584SApple OSS Distributions int rc = 0;
277*1031c584SApple OSS Distributions
278*1031c584SApple OSS Distributions switch (type) {
279*1031c584SApple OSS Distributions case OSLF_CMD_TYPE_COUNT:
280*1031c584SApple OSS Distributions case OSLF_CMD_TYPE_SCALAR:
281*1031c584SApple OSS Distributions // Scrub kernel pointers.
282*1031c584SApple OSS Distributions if (doprnt_hide_pointers && is_kernel_pointer(arg, arg_len)) {
283*1031c584SApple OSS Distributions rc = log_add_range_arg(ctx, type, OSLF_CMD_FLAG_PRIVATE, NULL, 0);
284*1031c584SApple OSS Distributions ctx->ctx_hdr->hdr_flags |= OSLF_HDR_FLAG_HAS_PRIVATE;
285*1031c584SApple OSS Distributions } else {
286*1031c584SApple OSS Distributions rc = log_add_arg(ctx, type, arg, arg_len);
287*1031c584SApple OSS Distributions }
288*1031c584SApple OSS Distributions break;
289*1031c584SApple OSS Distributions case OSLF_CMD_TYPE_STRING:
290*1031c584SApple OSS Distributions rc = log_add_range_arg(ctx, type, OSLF_CMD_FLAG_PUBLIC, arg, arg_len);
291*1031c584SApple OSS Distributions ctx->ctx_hdr->hdr_flags |= OSLF_HDR_FLAG_HAS_NON_SCALAR;
292*1031c584SApple OSS Distributions break;
293*1031c584SApple OSS Distributions default:
294*1031c584SApple OSS Distributions panic("Unsupported log value type");
295*1031c584SApple OSS Distributions }
296*1031c584SApple OSS Distributions
297*1031c584SApple OSS Distributions return rc;
298*1031c584SApple OSS Distributions }
299*1031c584SApple OSS Distributions
300*1031c584SApple OSS Distributions static int
log_encode_fmt(os_log_context_t ctx,const char * format,va_list args)301*1031c584SApple OSS Distributions log_encode_fmt(os_log_context_t ctx, const char *format, va_list args)
302*1031c584SApple OSS Distributions {
303*1031c584SApple OSS Distributions const char *position = format;
304*1031c584SApple OSS Distributions
305*1031c584SApple OSS Distributions while ((position = strchr(position, '%'))) {
306*1031c584SApple OSS Distributions position++; // Look at character(s) after %.
307*1031c584SApple OSS Distributions
308*1031c584SApple OSS Distributions int type = OST_INT;
309*1031c584SApple OSS Distributions boolean_t has_precision = false;
310*1031c584SApple OSS Distributions int precision = 0;
311*1031c584SApple OSS Distributions
312*1031c584SApple OSS Distributions for (bool done = false; !done; position++) {
313*1031c584SApple OSS Distributions union os_log_fmt_types_u value;
314*1031c584SApple OSS Distributions size_t str_length;
315*1031c584SApple OSS Distributions int err = 0;
316*1031c584SApple OSS Distributions
317*1031c584SApple OSS Distributions switch (position[0]) {
318*1031c584SApple OSS Distributions case '%':
319*1031c584SApple OSS Distributions // %% prints % character
320*1031c584SApple OSS Distributions done = true;
321*1031c584SApple OSS Distributions break;
322*1031c584SApple OSS Distributions
323*1031c584SApple OSS Distributions /* type of types or other */
324*1031c584SApple OSS Distributions case 'l': // longer
325*1031c584SApple OSS Distributions type++;
326*1031c584SApple OSS Distributions break;
327*1031c584SApple OSS Distributions
328*1031c584SApple OSS Distributions case 'h': // shorter
329*1031c584SApple OSS Distributions type--;
330*1031c584SApple OSS Distributions break;
331*1031c584SApple OSS Distributions
332*1031c584SApple OSS Distributions case 'z':
333*1031c584SApple OSS Distributions type = OST_SIZE;
334*1031c584SApple OSS Distributions break;
335*1031c584SApple OSS Distributions
336*1031c584SApple OSS Distributions case 'j':
337*1031c584SApple OSS Distributions type = OST_INTMAX;
338*1031c584SApple OSS Distributions break;
339*1031c584SApple OSS Distributions
340*1031c584SApple OSS Distributions case 't':
341*1031c584SApple OSS Distributions type = OST_PTRDIFF;
342*1031c584SApple OSS Distributions break;
343*1031c584SApple OSS Distributions
344*1031c584SApple OSS Distributions case 'q':
345*1031c584SApple OSS Distributions type = OST_LONGLONG;
346*1031c584SApple OSS Distributions break;
347*1031c584SApple OSS Distributions
348*1031c584SApple OSS Distributions case '.': // precision
349*1031c584SApple OSS Distributions if (position[1] == '*') {
350*1031c584SApple OSS Distributions // Dynamic precision, argument holds actual value.
351*1031c584SApple OSS Distributions precision = va_arg(args, int);
352*1031c584SApple OSS Distributions position++;
353*1031c584SApple OSS Distributions } else {
354*1031c584SApple OSS Distributions // Static precision, the value follows in the fmt.
355*1031c584SApple OSS Distributions precision = 0;
356*1031c584SApple OSS Distributions while (is_digit(position[1])) {
357*1031c584SApple OSS Distributions if (precision < LOG_FMT_MAX_PRECISION) {
358*1031c584SApple OSS Distributions precision = 10 * precision + (position[1] - '0');
359*1031c584SApple OSS Distributions }
360*1031c584SApple OSS Distributions position++;
361*1031c584SApple OSS Distributions }
362*1031c584SApple OSS Distributions precision = MIN(precision, LOG_FMT_MAX_PRECISION);
363*1031c584SApple OSS Distributions }
364*1031c584SApple OSS Distributions err = log_encode_fmt_arg(&precision, sizeof(precision), OSLF_CMD_TYPE_COUNT, ctx);
365*1031c584SApple OSS Distributions // A negative precision is treated as though it were missing.
366*1031c584SApple OSS Distributions if (precision >= 0) {
367*1031c584SApple OSS Distributions has_precision = true;
368*1031c584SApple OSS Distributions }
369*1031c584SApple OSS Distributions break;
370*1031c584SApple OSS Distributions
371*1031c584SApple OSS Distributions case '-': // left-align
372*1031c584SApple OSS Distributions case '+': // force sign
373*1031c584SApple OSS Distributions case ' ': // prefix non-negative with space
374*1031c584SApple OSS Distributions case '#': // alternate
375*1031c584SApple OSS Distributions case '\'': // group by thousands
376*1031c584SApple OSS Distributions break;
377*1031c584SApple OSS Distributions
378*1031c584SApple OSS Distributions /* fixed types */
379*1031c584SApple OSS Distributions case 'd': // integer
380*1031c584SApple OSS Distributions case 'i': // integer
381*1031c584SApple OSS Distributions case 'o': // octal
382*1031c584SApple OSS Distributions case 'u': // unsigned
383*1031c584SApple OSS Distributions case 'x': // hex
384*1031c584SApple OSS Distributions case 'X': // upper-hex
385*1031c584SApple OSS Distributions switch (type) {
386*1031c584SApple OSS Distributions case OST_CHAR:
387*1031c584SApple OSS Distributions value.ch = (char) va_arg(args, int);
388*1031c584SApple OSS Distributions err = log_encode_fmt_arg(&value.ch, sizeof(value.ch), OSLF_CMD_TYPE_SCALAR, ctx);
389*1031c584SApple OSS Distributions break;
390*1031c584SApple OSS Distributions
391*1031c584SApple OSS Distributions case OST_SHORT:
392*1031c584SApple OSS Distributions value.s = (short) va_arg(args, int);
393*1031c584SApple OSS Distributions err = log_encode_fmt_arg(&value.s, sizeof(value.s), OSLF_CMD_TYPE_SCALAR, ctx);
394*1031c584SApple OSS Distributions break;
395*1031c584SApple OSS Distributions
396*1031c584SApple OSS Distributions case OST_INT:
397*1031c584SApple OSS Distributions value.i = va_arg(args, int);
398*1031c584SApple OSS Distributions err = log_encode_fmt_arg(&value.i, sizeof(value.i), OSLF_CMD_TYPE_SCALAR, ctx);
399*1031c584SApple OSS Distributions break;
400*1031c584SApple OSS Distributions
401*1031c584SApple OSS Distributions case OST_LONG:
402*1031c584SApple OSS Distributions value.l = va_arg(args, long);
403*1031c584SApple OSS Distributions err = log_encode_fmt_arg(&value.l, sizeof(value.l), OSLF_CMD_TYPE_SCALAR, ctx);
404*1031c584SApple OSS Distributions break;
405*1031c584SApple OSS Distributions
406*1031c584SApple OSS Distributions case OST_LONGLONG:
407*1031c584SApple OSS Distributions value.ll = va_arg(args, long long);
408*1031c584SApple OSS Distributions err = log_encode_fmt_arg(&value.ll, sizeof(value.ll), OSLF_CMD_TYPE_SCALAR, ctx);
409*1031c584SApple OSS Distributions break;
410*1031c584SApple OSS Distributions
411*1031c584SApple OSS Distributions case OST_SIZE:
412*1031c584SApple OSS Distributions value.z = va_arg(args, size_t);
413*1031c584SApple OSS Distributions err = log_encode_fmt_arg(&value.z, sizeof(value.z), OSLF_CMD_TYPE_SCALAR, ctx);
414*1031c584SApple OSS Distributions break;
415*1031c584SApple OSS Distributions
416*1031c584SApple OSS Distributions case OST_INTMAX:
417*1031c584SApple OSS Distributions value.im = va_arg(args, intmax_t);
418*1031c584SApple OSS Distributions err = log_encode_fmt_arg(&value.im, sizeof(value.im), OSLF_CMD_TYPE_SCALAR, ctx);
419*1031c584SApple OSS Distributions break;
420*1031c584SApple OSS Distributions
421*1031c584SApple OSS Distributions case OST_PTRDIFF:
422*1031c584SApple OSS Distributions value.pd = va_arg(args, ptrdiff_t);
423*1031c584SApple OSS Distributions err = log_encode_fmt_arg(&value.pd, sizeof(value.pd), OSLF_CMD_TYPE_SCALAR, ctx);
424*1031c584SApple OSS Distributions break;
425*1031c584SApple OSS Distributions
426*1031c584SApple OSS Distributions default:
427*1031c584SApple OSS Distributions return EINVAL;
428*1031c584SApple OSS Distributions }
429*1031c584SApple OSS Distributions done = true;
430*1031c584SApple OSS Distributions break;
431*1031c584SApple OSS Distributions
432*1031c584SApple OSS Distributions case 'p': // pointer
433*1031c584SApple OSS Distributions value.p = va_arg(args, void *);
434*1031c584SApple OSS Distributions err = log_encode_fmt_arg(&value.p, sizeof(value.p), OSLF_CMD_TYPE_SCALAR, ctx);
435*1031c584SApple OSS Distributions done = true;
436*1031c584SApple OSS Distributions break;
437*1031c584SApple OSS Distributions
438*1031c584SApple OSS Distributions case 'c': // char
439*1031c584SApple OSS Distributions value.ch = (char) va_arg(args, int);
440*1031c584SApple OSS Distributions err = log_encode_fmt_arg(&value.ch, sizeof(value.ch), OSLF_CMD_TYPE_SCALAR, ctx);
441*1031c584SApple OSS Distributions done = true;
442*1031c584SApple OSS Distributions break;
443*1031c584SApple OSS Distributions
444*1031c584SApple OSS Distributions case 's': // string
445*1031c584SApple OSS Distributions value.pch = va_arg(args, char *);
446*1031c584SApple OSS Distributions if (!value.pch) {
447*1031c584SApple OSS Distributions str_length = 0;
448*1031c584SApple OSS Distributions } else if (has_precision) {
449*1031c584SApple OSS Distributions assert(precision >= 0);
450*1031c584SApple OSS Distributions str_length = strnlen(value.pch, precision);
451*1031c584SApple OSS Distributions } else {
452*1031c584SApple OSS Distributions str_length = strlen(value.pch) + 1;
453*1031c584SApple OSS Distributions }
454*1031c584SApple OSS Distributions err = log_encode_fmt_arg(value.pch, str_length, OSLF_CMD_TYPE_STRING, ctx);
455*1031c584SApple OSS Distributions done = true;
456*1031c584SApple OSS Distributions break;
457*1031c584SApple OSS Distributions
458*1031c584SApple OSS Distributions case 'm':
459*1031c584SApple OSS Distributions value.i = 0; // Does %m make sense in the kernel?
460*1031c584SApple OSS Distributions err = log_encode_fmt_arg(&value.i, sizeof(value.i), OSLF_CMD_TYPE_SCALAR, ctx);
461*1031c584SApple OSS Distributions done = true;
462*1031c584SApple OSS Distributions break;
463*1031c584SApple OSS Distributions
464*1031c584SApple OSS Distributions case '0' ... '9':
465*1031c584SApple OSS Distributions // Skipping field width, libtrace takes care of it.
466*1031c584SApple OSS Distributions break;
467*1031c584SApple OSS Distributions
468*1031c584SApple OSS Distributions default:
469*1031c584SApple OSS Distributions return EINVAL;
470*1031c584SApple OSS Distributions }
471*1031c584SApple OSS Distributions
472*1031c584SApple OSS Distributions if (slowpath(err)) {
473*1031c584SApple OSS Distributions return err;
474*1031c584SApple OSS Distributions }
475*1031c584SApple OSS Distributions }
476*1031c584SApple OSS Distributions }
477*1031c584SApple OSS Distributions
478*1031c584SApple OSS Distributions return 0;
479*1031c584SApple OSS Distributions }
480*1031c584SApple OSS Distributions
481*1031c584SApple OSS Distributions static inline size_t
write_address_location(uint8_t buf[static sizeof (uint64_t)],uintptr_t loc,size_t loc_size)482*1031c584SApple OSS Distributions write_address_location(uint8_t buf[static sizeof(uint64_t)], uintptr_t loc, size_t loc_size)
483*1031c584SApple OSS Distributions {
484*1031c584SApple OSS Distributions if (loc_size == sizeof(uintptr_t)) {
485*1031c584SApple OSS Distributions #if __LP64__
486*1031c584SApple OSS Distributions loc_size = 6; // 48 bits are enough
487*1031c584SApple OSS Distributions #endif
488*1031c584SApple OSS Distributions memcpy(buf, (uintptr_t[]){ loc }, loc_size);
489*1031c584SApple OSS Distributions } else {
490*1031c584SApple OSS Distributions assert(loc_size == sizeof(uint32_t));
491*1031c584SApple OSS Distributions memcpy(buf, (uint32_t[]){ (uint32_t)loc }, loc_size);
492*1031c584SApple OSS Distributions }
493*1031c584SApple OSS Distributions return loc_size;
494*1031c584SApple OSS Distributions }
495*1031c584SApple OSS Distributions
496*1031c584SApple OSS Distributions static void
os_log_encode_location(os_log_context_t ctx,uintptr_t loc,size_t loc_size)497*1031c584SApple OSS Distributions os_log_encode_location(os_log_context_t ctx, uintptr_t loc, size_t loc_size)
498*1031c584SApple OSS Distributions {
499*1031c584SApple OSS Distributions const size_t hdr_size = write_address_location(ctx->ctx_buffer, loc, loc_size);
500*1031c584SApple OSS Distributions ctx->ctx_hdr = (os_log_fmt_hdr_t)&ctx->ctx_buffer[hdr_size];
501*1031c584SApple OSS Distributions bzero(ctx->ctx_hdr, sizeof(*ctx->ctx_hdr));
502*1031c584SApple OSS Distributions ctx->ctx_content_sz = (uint16_t)(ctx->ctx_buffer_sz - hdr_size - sizeof(*ctx->ctx_hdr));
503*1031c584SApple OSS Distributions }
504*1031c584SApple OSS Distributions
505*1031c584SApple OSS Distributions /*
506*1031c584SApple OSS Distributions * Encodes argument (meta)data into a format consumed by libtrace. Stores
507*1031c584SApple OSS Distributions * metadada for all arguments first. Metadata also include scalar argument
508*1031c584SApple OSS Distributions * values. Second step saves data which are encoded separately from respective
509*1031c584SApple OSS Distributions * metadata (like strings).
510*1031c584SApple OSS Distributions */
511*1031c584SApple OSS Distributions bool
os_log_context_encode(os_log_context_t ctx,const char * fmt,va_list args,uintptr_t loc,size_t loc_size)512*1031c584SApple OSS Distributions os_log_context_encode(os_log_context_t ctx, const char *fmt, va_list args,
513*1031c584SApple OSS Distributions uintptr_t loc, size_t loc_size)
514*1031c584SApple OSS Distributions {
515*1031c584SApple OSS Distributions os_log_encode_location(ctx, loc, loc_size);
516*1031c584SApple OSS Distributions
517*1031c584SApple OSS Distributions va_list args_copy;
518*1031c584SApple OSS Distributions va_copy(args_copy, args);
519*1031c584SApple OSS Distributions
520*1031c584SApple OSS Distributions int rc = log_encode_fmt(ctx, fmt, args);
521*1031c584SApple OSS Distributions
522*1031c584SApple OSS Distributions va_end(args_copy);
523*1031c584SApple OSS Distributions
524*1031c584SApple OSS Distributions switch (rc) {
525*1031c584SApple OSS Distributions case EINVAL:
526*1031c584SApple OSS Distributions // Bogus/Unsupported fmt string
527*1031c584SApple OSS Distributions counter_inc(&oslog_p_fmt_invalid_msgcount);
528*1031c584SApple OSS Distributions return false;
529*1031c584SApple OSS Distributions case ENOMEM:
530*1031c584SApple OSS Distributions /*
531*1031c584SApple OSS Distributions * The fmt contains unreasonable number of arguments (> 32) and
532*1031c584SApple OSS Distributions * we ran out of space. We could call log_expand()
533*1031c584SApple OSS Distributions * here and retry. However, using such formatting strings rather
534*1031c584SApple OSS Distributions * seem like a misuse of the logging system, hence error.
535*1031c584SApple OSS Distributions */
536*1031c584SApple OSS Distributions counter_inc(&oslog_p_fmt_max_args_msgcount);
537*1031c584SApple OSS Distributions return false;
538*1031c584SApple OSS Distributions case 0:
539*1031c584SApple OSS Distributions break;
540*1031c584SApple OSS Distributions default:
541*1031c584SApple OSS Distributions panic("unhandled return value");
542*1031c584SApple OSS Distributions }
543*1031c584SApple OSS Distributions
544*1031c584SApple OSS Distributions if (ctx->ctx_pubdata_sz == 0) {
545*1031c584SApple OSS Distributions goto finish;
546*1031c584SApple OSS Distributions }
547*1031c584SApple OSS Distributions
548*1031c584SApple OSS Distributions /*
549*1031c584SApple OSS Distributions * Logmem may not have been set up yet when logging very early during
550*1031c584SApple OSS Distributions * the boot. Be sure to check its state.
551*1031c584SApple OSS Distributions */
552*1031c584SApple OSS Distributions if (!log_fits(ctx, ctx->ctx_pubdata_sz) && logmem_ready(ctx->ctx_logmem)) {
553*1031c584SApple OSS Distributions size_t space_needed = log_context_cursor(ctx) + ctx->ctx_pubdata_sz - ctx->ctx_buffer;
554*1031c584SApple OSS Distributions space_needed = MIN(space_needed, logmem_max_size(ctx->ctx_logmem));
555*1031c584SApple OSS Distributions (void) log_expand(ctx, space_needed);
556*1031c584SApple OSS Distributions }
557*1031c584SApple OSS Distributions
558*1031c584SApple OSS Distributions log_encode_public_data(ctx);
559*1031c584SApple OSS Distributions
560*1031c584SApple OSS Distributions if (ctx->ctx_truncated) {
561*1031c584SApple OSS Distributions counter_inc(&oslog_p_truncated_msgcount);
562*1031c584SApple OSS Distributions }
563*1031c584SApple OSS Distributions finish:
564*1031c584SApple OSS Distributions ctx->ctx_content_sz = (uint16_t)(log_context_cursor(ctx) - ctx->ctx_buffer);
565*1031c584SApple OSS Distributions ctx->ctx_content_off = 0;
566*1031c584SApple OSS Distributions return true;
567*1031c584SApple OSS Distributions }
568*1031c584SApple OSS Distributions
569*1031c584SApple OSS Distributions void
os_log_context_init(os_log_context_t ctx,logmem_t * logmem,uint8_t * buffer,size_t buffer_sz)570*1031c584SApple OSS Distributions os_log_context_init(os_log_context_t ctx, logmem_t *logmem, uint8_t *buffer, size_t buffer_sz)
571*1031c584SApple OSS Distributions {
572*1031c584SApple OSS Distributions assert(logmem);
573*1031c584SApple OSS Distributions assert(buffer);
574*1031c584SApple OSS Distributions assert(buffer_sz > 0);
575*1031c584SApple OSS Distributions
576*1031c584SApple OSS Distributions bzero(ctx, sizeof(*ctx));
577*1031c584SApple OSS Distributions ctx->ctx_logmem = logmem;
578*1031c584SApple OSS Distributions ctx->ctx_buffer = buffer;
579*1031c584SApple OSS Distributions ctx->ctx_buffer_sz = buffer_sz;
580*1031c584SApple OSS Distributions }
581*1031c584SApple OSS Distributions
582*1031c584SApple OSS Distributions void
os_log_context_free(os_log_context_t ctx)583*1031c584SApple OSS Distributions os_log_context_free(os_log_context_t ctx)
584*1031c584SApple OSS Distributions {
585*1031c584SApple OSS Distributions if (ctx->ctx_allocated) {
586*1031c584SApple OSS Distributions logmem_free_locked(ctx->ctx_logmem, ctx->ctx_buffer, ctx->ctx_buffer_sz);
587*1031c584SApple OSS Distributions }
588*1031c584SApple OSS Distributions }
589