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*
38consistent_debug_allocate_entry(void)
39{
40 unsigned int i;
41
42 if (!consistent_debug_registry) {
43 return NULL;
44 }
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;
50 }
51 }
52 return NULL;
53}
54
55boolean_t
56PE_consistent_debug_lookup_entry(uint64_t record_id, uint64_t *phys_addr, uint64_t *length)
57{
58 assert(phys_addr != NULL);
59 assert(length != NULL);
60
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;
65
66 return true;
67 }
68 }
69
70 return false;
71}
72
73int
74PE_consistent_debug_inherit(void)
75{
76 DTEntry entryP;
77 uintptr_t const *prop_data;
78 uintptr_t root_pointer = 0;
79 uint32_t size;
80
81 if ((SecureDTLookupEntry(NULL, pathName: "/chosen", foundEntry: &entryP) == kSuccess)) {
82 if (SecureDTGetProperty(entry: entryP, propertyName: "consistent-debug-root", propertyValue: (void const **)&prop_data, propertySize: &size) == kSuccess) {
83 root_pointer = prop_data[0];
84 }
85 }
86 if (root_pointer == 0) {
87 return -1;
88 }
89 consistent_debug_registry = (dbg_registry_t *)ml_map_high_window(phys_addr: root_pointer, len: sizeof(dbg_registry_t));
90 return 0;
91}
92
93int
94PE_consistent_debug_register(uint64_t record_id, uint64_t physaddr, uint64_t length)
95{
96 dbg_record_header_t *allocated_header = consistent_debug_allocate_entry();
97 if (allocated_header == NULL) {
98 return -1;
99 }
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;
105 return 0;
106}
107
108int
109PE_consistent_debug_enabled(void)
110{
111 return consistent_debug_registry != NULL;
112}
113