2 * Copyright (c) 2011-2018 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include <pexpert/pexpert.h>
30 #include <pexpert/arm/consistent_debug.h>
31 #include <pexpert/device_tree.h>
32 #include <libkern/OSAtomic.h>
33 #include <machine/machine_routines.h>
35 static dbg_registry_t
* consistent_debug_registry
= NULL
;
37 static dbg_record_header_t
*
38 consistent_debug_allocate_entry(void)
42 if (!consistent_debug_registry
) {
45 for (i
= 0; i
< consistent_debug_registry
->top_level_header
.num_records
; i
++) {
46 dbg_record_header_t
*record
= &consistent_debug_registry
->records
[i
];
47 if (OSCompareAndSwap64(kDbgIdUnusedEntry
, kDbgIdReservedEntry
, &record
->record_id
)) {
48 // Reserved an entry at position i.
49 return (dbg_record_header_t
*)record
;
56 PE_consistent_debug_lookup_entry(uint64_t record_id
, uint64_t *phys_addr
, uint64_t *length
)
58 assert(phys_addr
!= NULL
);
59 assert(length
!= NULL
);
61 for (unsigned int i
= 0; i
< consistent_debug_registry
->top_level_header
.num_records
; i
++) {
62 if (consistent_debug_registry
->records
[i
].record_id
== record_id
) {
63 *phys_addr
= consistent_debug_registry
->records
[i
].physaddr
;
64 *length
= consistent_debug_registry
->records
[i
].length
;
74 PE_consistent_debug_inherit(void)
77 uintptr_t const *prop_data
;
78 uintptr_t root_pointer
= 0;
81 if ((SecureDTLookupEntry(NULL
, "/chosen", &entryP
) == kSuccess
)) {
82 if (SecureDTGetProperty(entryP
, "consistent-debug-root", (void const **)&prop_data
, &size
) == kSuccess
) {
83 root_pointer
= prop_data
[0];
86 if (root_pointer
== 0) {
89 consistent_debug_registry
= (dbg_registry_t
*)ml_map_high_window(root_pointer
, sizeof(dbg_registry_t
));
94 PE_consistent_debug_register(uint64_t record_id
, uint64_t physaddr
, uint64_t length
)
96 dbg_record_header_t
*allocated_header
= consistent_debug_allocate_entry();
97 if (allocated_header
== NULL
) {
100 allocated_header
->length
= length
;
101 allocated_header
->physaddr
= physaddr
;
102 // Make sure the hdr/length are visible before the record_id.
103 __asm__
volatile ("dmb ish" : : : "memory");
104 allocated_header
->record_id
= record_id
;
109 PE_consistent_debug_enabled(void)
111 return consistent_debug_registry
!= NULL
;