]> git.saurik.com Git - apple/xnu.git/blob - pexpert/arm/pe_consistent_debug.c
xnu-4570.20.62.tar.gz
[apple/xnu.git] / pexpert / arm / pe_consistent_debug.c
1 /*
2 * Copyright (C) 2011-2013 Apple Inc. All rights reserved.
3 *
4 * This document is the property of Apple Inc.
5 * It is considered confidential and proprietary.
6 *
7 * This document may not be reproduced or transmitted in any form,
8 * in whole or in part, without the express written permission of
9 * Apple Inc.
10 */
11
12 #include <pexpert/pexpert.h>
13 #include <pexpert/arm/consistent_debug.h>
14 #include <pexpert/device_tree.h>
15 #include <libkern/OSAtomic.h>
16 #include <machine/machine_routines.h>
17
18 static dbg_registry_t * consistent_debug_registry = NULL;
19
20 static dbg_record_header_t* consistent_debug_allocate_entry(void) {
21 unsigned int i;
22
23 if (!consistent_debug_registry)
24 return NULL;
25 for (i = 0; i < consistent_debug_registry->top_level_header.num_records; i++) {
26 dbg_record_header_t *record = &consistent_debug_registry->records[i];
27 if (OSCompareAndSwap64(kDbgIdUnusedEntry, kDbgIdReservedEntry, &record->record_id)) {
28 // Reserved an entry at position i.
29 return (dbg_record_header_t*)record;
30 }
31 }
32 return NULL;
33 }
34
35 int PE_consistent_debug_inherit(void)
36 {
37 DTEntry entryP;
38 uintptr_t *prop_data;
39 uintptr_t root_pointer = 0;
40 uint32_t size;
41
42 if ((DTLookupEntry(NULL, "/chosen", &entryP) == kSuccess))
43 if (DTGetProperty(entryP, "consistent-debug-root", (void **)&prop_data, &size) == kSuccess)
44 root_pointer = prop_data[0];
45 if (root_pointer == 0)
46 return -1;
47 consistent_debug_registry = (dbg_registry_t *)ml_map_high_window(root_pointer, sizeof(dbg_registry_t));
48 return 0;
49 }
50
51 int PE_consistent_debug_register(uint64_t record_id, uint64_t physaddr, uint64_t length)
52 {
53 dbg_record_header_t *allocated_header = consistent_debug_allocate_entry();
54 if (allocated_header == NULL)
55 return -1;
56 allocated_header->length = length;
57 allocated_header->physaddr = physaddr;
58 // Make sure the hdr/length are visible before the record_id.
59 __asm__ volatile("dmb ish" : : : "memory");
60 allocated_header->record_id = record_id;
61 return 0;
62 }
63
64 int PE_consistent_debug_enabled(void)
65 {
66 return (consistent_debug_registry != NULL);
67 }
68