Loading...
/*
 * Copyright (c) 2019 Apple Inc. All rights reserved.
 *
 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
 *
 * This file contains Original Code and/or Modifications of Original Code
 * as defined in and that are subject to the Apple Public Source License
 * Version 2.0 (the 'License'). You may not use this file except in
 * compliance with the License. The rights granted to you under the License
 * may not be used to create, or enable the creation or redistribution of,
 * unlawful or unlicensed copies of an Apple operating system, or to
 * circumvent, violate, or enable the circumvention or violation of, any
 * terms of an Apple operating system software license agreement.
 *
 * Please obtain a copy of the License at
 * http://www.opensource.apple.com/apsl/ and read it before using this file.
 *
 * The Original Code and all software distributed under the License are
 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
 * Please see the License for the specific language governing rights and
 * limitations under the License.
 *
 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
 */

#ifndef _REASON_H_
#define _REASON_H_

#include <stdint.h>

__BEGIN_DECLS

#ifdef KERNEL_PRIVATE

#include <kern/kern_cdata.h>

#ifdef XNU_KERNEL_PRIVATE
#include <os/refcnt.h>
#include <kern/locks.h>

typedef struct os_reason {
	decl_lck_mtx_data(, osr_lock);
	os_refcnt_t                     osr_refcount;
	uint32_t                        osr_namespace;
	uint64_t                        osr_code;
	uint64_t                        osr_flags;
	uint32_t                        osr_bufsize;
	struct kcdata_descriptor        osr_kcd_descriptor;
	char                            *osr_kcd_buf;
} *os_reason_t;

#define OS_REASON_NULL ((os_reason_t) NULL)

/* We only include 800 bytes of the exit reason description to not blow through the panic buffer */
#define LAUNCHD_PANIC_REASON_STRING_MAXLEN "800"

void os_reason_init(void);

os_reason_t build_userspace_exit_reason(uint32_t reason_namespace, uint64_t reason_code, user_addr_t payload, uint32_t payload_size,
    user_addr_t reason_string, uint64_t reason_flags);
char *exit_reason_get_string_desc(os_reason_t exit_reason);

/* The blocking allocation is currently not exported to KEXTs */
int os_reason_alloc_buffer(os_reason_t cur_reason, uint32_t osr_bufsize);

typedef struct _exception_info {
	int os_reason;
	int signal;
	exception_type_t exception_type;
	mach_exception_data_type_t mx_code;
	mach_exception_data_type_t mx_subcode;
	struct kt_info {
		int kt_subsys;
		uint32_t kt_error;
	} kt_info;
} exception_info_t;

#define PX_FLAGS_NONE           0
/* think twice about userspace debugging experience before using PX_DEBUG_NO_HONOR */
#define PX_DEBUG_NO_HONOR       (1 << 0) /* force exit even when debugging */
#define PX_KTRIAGE              (1 << 1) /* leave a ktriage record */
#define PX_PSIGNAL              (1 << 2) /* send sig instead of forced exit */
#define PX_NO_EXCEPTION_UTHREAD (1 << 3) /* do not set bsdthread exception */

int exit_with_mach_exception(struct proc *p, exception_info_t exception, uint32_t flags);
#if CONFIG_EXCLAVES
int exit_with_exclave_exception(struct proc *p, exception_info_t exception, uint32_t flags);
#endif

#else /* XNU_KERNEL_PRIVATE */

typedef void * os_reason_t;

#endif /* XNU_KERNEL_PRIVATE */

os_reason_t os_reason_create(uint32_t osr_namespace, uint64_t osr_code);
int os_reason_alloc_buffer_noblock(os_reason_t cur_reason, uint32_t osr_bufsize);
struct kcdata_descriptor * os_reason_get_kcdata_descriptor(os_reason_t cur_reason);
void os_reason_ref(os_reason_t cur_reason);
void os_reason_free(os_reason_t cur_reason);
void os_reason_set_flags(os_reason_t cur_reason, uint64_t flags);
void os_reason_set_description_data(os_reason_t cur_reason, uint32_t type, void *reason_data, uint32_t reason_data_len);
#endif /* KERNEL_PRIVATE */

/*
 * Reason namespaces.
 */
#define OS_REASON_INVALID       0
#define OS_REASON_JETSAM        1
#define OS_REASON_SIGNAL        2
#define OS_REASON_CODESIGNING   3
#define OS_REASON_HANGTRACER    4
#define OS_REASON_TEST          5
#define OS_REASON_DYLD          6
#define OS_REASON_LIBXPC        7
#define OS_REASON_OBJC          8
#define OS_REASON_EXEC          9
#define OS_REASON_SPRINGBOARD   10
#define OS_REASON_TCC           11
#define OS_REASON_REPORTCRASH   12
#define OS_REASON_COREANIMATION 13
#define OS_REASON_AGGREGATED    14
#define OS_REASON_RUNNINGBOARD  15
#define OS_REASON_ASSERTIOND    OS_REASON_RUNNINGBOARD  /* old name */
#define OS_REASON_SKYWALK       16
#define OS_REASON_SETTINGS      17
#define OS_REASON_LIBSYSTEM     18
#define OS_REASON_FOUNDATION    19
#define OS_REASON_WATCHDOG      20
#define OS_REASON_METAL         21
#define OS_REASON_WATCHKIT      22
#define OS_REASON_GUARD         23
#define OS_REASON_ANALYTICS     24
#define OS_REASON_SANDBOX       25
#define OS_REASON_SECURITY      26
#define OS_REASON_ENDPOINTSECURITY      27
#define OS_REASON_PAC_EXCEPTION 28
#define OS_REASON_BLUETOOTH_CHIP 29
#define OS_REASON_PORT_SPACE    30
#define OS_REASON_WEBKIT        31
#define OS_REASON_BACKLIGHTSERVICES 32
#define OS_REASON_MEDIA 33
#define OS_REASON_ROSETTA 34
#define OS_REASON_LIBIGNITION 35
#define OS_REASON_BOOTMOUNT 36