1/*
2 * Copyright (c) 2011-2018 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#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>
34
35static dbg_registry_t * consistent_debug_registry = NULL;
36
37static dbg_record_header_t* consistent_debug_allocate_entry(void) {
38 unsigned int i;
39
40 if (!consistent_debug_registry)
41 return NULL;
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;
47 }
48 }
49 return NULL;
50}
51
52int PE_consistent_debug_inherit(void)
53{
54 DTEntry entryP;
55 uintptr_t *prop_data;
56 uintptr_t root_pointer = 0;
57 uint32_t size;
58
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)
63 return -1;
64 consistent_debug_registry = (dbg_registry_t *)ml_map_high_window(root_pointer, sizeof(dbg_registry_t));
65 return 0;
66}
67
68int PE_consistent_debug_register(uint64_t record_id, uint64_t physaddr, uint64_t length)
69{
70 dbg_record_header_t *allocated_header = consistent_debug_allocate_entry();
71 if (allocated_header == NULL)
72 return -1;
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;
78 return 0;
79}
80
81int PE_consistent_debug_enabled(void)
82{
83 return (consistent_debug_registry != NULL);
84}
85
86