| 1 | /* |
| 2 | * Copyright (c) 2022 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 | #ifndef _KERN_EXT_PANICLOG_H_ |
| 30 | #define _KERN_EXT_PANICLOG_H_ |
| 31 | |
| 32 | #include <sys/queue.h> |
| 33 | #include <uuid/uuid.h> |
| 34 | #include <os/base.h> |
| 35 | |
| 36 | #define EXT_PANICLOG_ENABLE 1 |
| 37 | |
| 38 | #define EXT_PANICLOG_VERSION 1 |
| 39 | |
| 40 | #define MAX_DATA_ID_SIZE 32 |
| 41 | |
| 42 | #define MAX_EXT_PANICLOG_SIZE 32 * 1024 |
| 43 | |
| 44 | #define MAX_EXT_PANICLOG_LOGS 100 |
| 45 | |
| 46 | /* |
| 47 | * From the panic log metrics, we estimate that the paniclog takes up |
| 48 | * ~15K bytes and other log takes ~1K bytes. This 64K bytes ensures that |
| 49 | * we have enough space for other log and nested panics |
| 50 | */ |
| 51 | #define OTHER_LOG_REQUIRED_SIZE (64 * 1024) |
| 52 | |
| 53 | #define PANIC_WITH_DATA_UUID "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" |
| 54 | #define PANIC_WITH_DATA_MAX_LEN 2048 |
| 55 | #define PANIC_WITH_DATA_DATA_ID "Panic with Data Buffer" |
| 56 | |
| 57 | #define EXTPANICLOG_ENTITLEMENT "com.apple.private.allow-ext_paniclog" |
| 58 | |
| 59 | #if KERNEL_PRIVATE |
| 60 | |
| 61 | OS_CLOSED_OPTIONS(ext_paniclog_create_options, uint32_t, |
| 62 | EXT_PANICLOG_OPTIONS_NONE = 0x0, |
| 63 | EXT_PANICLOG_OPTIONS_WITH_BUFFER = 0x1); |
| 64 | |
| 65 | enum ext_paniclog_test_options { |
| 66 | EXT_PANICLOG_TEST_HANDLE_CREATE = 1, |
| 67 | EXT_PANICLOG_TEST_SET_ACTIVE_INACTIVE, |
| 68 | EXT_PANICLOG_TEST_INSERT_DATA, |
| 69 | EXT_PANICLOG_TEST_WRITE_PANIC_DATA, |
| 70 | EXT_PANICLOG_TEST_MULTIPLE_HANDLES, |
| 71 | EXT_PANICLOG_TEST_MULTIPLE_HANDLES_PANIC, |
| 72 | EXT_PANICLOG_TEST_INSERT_DUMMY_HANDLES, |
| 73 | EXT_PANICLOG_TEST_INSERT_STRUCT_HANDLES, |
| 74 | EXT_PANICLOG_TEST_END, |
| 75 | }; |
| 76 | |
| 77 | typedef struct ext_paniclog_handle { |
| 78 | LIST_ENTRY(ext_paniclog_handle) handles; |
| 79 | uuid_t uuid; |
| 80 | char data_id[MAX_DATA_ID_SIZE]; |
| 81 | void * XNU_PTRAUTH_SIGNED_PTR("ext_paniclog_handle.buf_addr" ) buf_addr; |
| 82 | uint32_t max_len; |
| 83 | uint32_t used_len; |
| 84 | ext_paniclog_create_options_t options; |
| 85 | uint8_t active; |
| 86 | } ext_paniclog_handle_t; |
| 87 | |
| 88 | typedef struct { |
| 89 | uint32_t ; |
| 90 | uuid_t ; |
| 91 | } ; |
| 92 | |
| 93 | void ext_paniclog_init(void); |
| 94 | int ext_paniclog_handle_set_active(ext_paniclog_handle_t *handle); |
| 95 | int ext_paniclog_handle_set_inactive(ext_paniclog_handle_t *handle); |
| 96 | ext_paniclog_handle_t *ext_paniclog_handle_alloc_with_uuid(uuid_t uuid, const char *data_id, uint32_t max_len, ext_paniclog_create_options_t options); |
| 97 | ext_paniclog_handle_t *ext_paniclog_handle_alloc_with_buffer(uuid_t uuid, const char *data_id, uint32_t max_len, void * buff, ext_paniclog_create_options_t options); |
| 98 | void ext_paniclog_handle_free(ext_paniclog_handle_t *handle); |
| 99 | int ext_paniclog_insert_data(ext_paniclog_handle_t *handle, void *addr, uint32_t len); |
| 100 | int ext_paniclog_append_data(ext_paniclog_handle_t *handle, void *addr, uint32_t len); |
| 101 | void *ext_paniclog_get_buffer(ext_paniclog_handle_t *handle); |
| 102 | uint32_t ext_paniclog_write_panicdata(void); |
| 103 | void *ext_paniclog_claim_buffer(ext_paniclog_handle_t *handle); |
| 104 | int ext_paniclog_yield_buffer(ext_paniclog_handle_t *handle, uint32_t used_len); |
| 105 | int ext_paniclog_set_used_len(ext_paniclog_handle_t *handle, uint32_t used_len); |
| 106 | bool is_debug_ptr_in_ext_paniclog(void); |
| 107 | |
| 108 | /* |
| 109 | * This function is used to panic and add a buffer data to the extensible paniclog. |
| 110 | * uuid here is used to decode the data. |
| 111 | */ |
| 112 | __abortlike __printflike(4, 5) |
| 113 | void panic_with_data(uuid_t uuid, void *addr, uint32_t len, const char *format, ...); |
| 114 | int ext_paniclog_test_hook(uint32_t option); |
| 115 | void ext_paniclog_panic_with_data(uuid_t uuid, void *addr, uint32_t len); |
| 116 | #endif // KERNEL_PRIVATE |
| 117 | |
| 118 | #endif // _KERN_EXT_PANICLOG_H_ |
| 119 | |