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
* consistent_debug_allocate_entry(void) {
40 if (!consistent_debug_registry
)
42 for (i
= 0; i
< consistent_debug_registry
->top_level_header
.num_records
; i
++) {
43 dbg_record_header_t
*record
= &consistent_debug_registry
->records
[i
];
44 if (OSCompareAndSwap64(kDbgIdUnusedEntry
, kDbgIdReservedEntry
, &record
->record_id
)) {
45 // Reserved an entry at position i.
46 return (dbg_record_header_t
*)record
;
52 int PE_consistent_debug_inherit(void)
56 uintptr_t root_pointer
= 0;
59 if ((DTLookupEntry(NULL
, "/chosen", &entryP
) == kSuccess
))
60 if (DTGetProperty(entryP
, "consistent-debug-root", (void **)&prop_data
, &size
) == kSuccess
)
61 root_pointer
= prop_data
[0];
62 if (root_pointer
== 0)
64 consistent_debug_registry
= (dbg_registry_t
*)ml_map_high_window(root_pointer
, sizeof(dbg_registry_t
));
68 int PE_consistent_debug_register(uint64_t record_id
, uint64_t physaddr
, uint64_t length
)
70 dbg_record_header_t
*allocated_header
= consistent_debug_allocate_entry();
71 if (allocated_header
== NULL
)
73 allocated_header
->length
= length
;
74 allocated_header
->physaddr
= physaddr
;
75 // Make sure the hdr/length are visible before the record_id.
76 __asm__
volatile("dmb ish" : : : "memory");
77 allocated_header
->record_id
= record_id
;
81 int PE_consistent_debug_enabled(void)
83 return (consistent_debug_registry
!= NULL
);