1 //===-- DNBArch.h -----------------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 //  Created by Greg Clayton on 6/24/07.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef __DebugNubArch_h__
14 #define __DebugNubArch_h__
15 
16 #include "DNBDefs.h"
17 #include "MacOSX/MachException.h"
18 
19 #include <mach/mach.h>
20 #include <stdio.h>
21 
22 struct DNBRegisterValue;
23 struct DNBRegisterSetInfo;
24 class DNBArchProtocol;
25 class MachThread;
26 
27 typedef DNBArchProtocol *(*DNBArchCallbackCreate)(MachThread *thread);
28 typedef const DNBRegisterSetInfo *(*DNBArchCallbackGetRegisterSetInfo)(
29     nub_size_t *num_reg_sets);
30 typedef const uint8_t *(*DNBArchCallbackGetBreakpointOpcode)(
31     nub_size_t byte_size);
32 
33 typedef struct DNBArchPluginInfoTag {
34   uint32_t cpu_type;
35   DNBArchCallbackCreate Create;
36   DNBArchCallbackGetRegisterSetInfo GetRegisterSetInfo;
37   DNBArchCallbackGetBreakpointOpcode GetBreakpointOpcode;
38 } DNBArchPluginInfo;
39 
40 class DNBArchProtocol {
41 public:
42   static DNBArchProtocol *Create(MachThread *thread);
43 
44   static uint32_t GetRegisterCPUType();
45 
46   static const DNBRegisterSetInfo *GetRegisterSetInfo(nub_size_t *num_reg_sets);
47 
48   static const uint8_t *GetBreakpointOpcode(nub_size_t byte_size);
49 
50   static void RegisterArchPlugin(const DNBArchPluginInfo &arch_info);
51 
52   static uint32_t GetArchitecture();
53 
54   static bool SetArchitecture(uint32_t cpu_type);
55 
56   DNBArchProtocol() : m_save_id(0) {}
57 
58   virtual ~DNBArchProtocol() {}
59   virtual bool GetRegisterValue(uint32_t set, uint32_t reg,
60                                 DNBRegisterValue *value) = 0;
61   virtual bool SetRegisterValue(uint32_t set, uint32_t reg,
62                                 const DNBRegisterValue *value) = 0;
63   virtual nub_size_t GetRegisterContext(void *buf, nub_size_t buf_len) = 0;
64   virtual nub_size_t SetRegisterContext(const void *buf,
65                                         nub_size_t buf_len) = 0;
66   virtual uint32_t SaveRegisterState() = 0;
67   virtual bool RestoreRegisterState(uint32_t save_id) = 0;
68 
69   virtual kern_return_t GetRegisterState(int set, bool force) = 0;
70   virtual kern_return_t SetRegisterState(int set) = 0;
71   virtual bool RegisterSetStateIsValid(int set) const = 0;
72 
73   virtual uint64_t GetPC(uint64_t failValue) = 0; // Get program counter
74   virtual kern_return_t SetPC(uint64_t value) = 0;
75   virtual uint64_t GetSP(uint64_t failValue) = 0; // Get stack pointer
76   virtual void ThreadWillResume() = 0;
77   virtual bool ThreadDidStop() = 0;
78   virtual bool NotifyException(MachException::Data &exc) { return false; }
79   virtual uint32_t NumSupportedHardwareBreakpoints() { return 0; }
80   virtual uint32_t NumSupportedHardwareWatchpoints() { return 0; }
81   virtual uint32_t EnableHardwareBreakpoint(nub_addr_t addr, nub_size_t size) {
82     return INVALID_NUB_HW_INDEX;
83   }
84   virtual uint32_t EnableHardwareWatchpoint(nub_addr_t addr, nub_size_t size,
85                                             bool read, bool write,
86                                             bool also_set_on_task) {
87     return INVALID_NUB_HW_INDEX;
88   }
89   virtual bool DisableHardwareBreakpoint(uint32_t hw_index) { return false; }
90   virtual bool DisableHardwareWatchpoint(uint32_t hw_index,
91                                          bool also_set_on_task) {
92     return false;
93   }
94   virtual uint32_t GetHardwareWatchpointHit(nub_addr_t &addr) {
95     return INVALID_NUB_HW_INDEX;
96   }
97   virtual bool StepNotComplete() { return false; }
98 
99 protected:
100   friend class MachThread;
101 
102   uint32_t GetNextRegisterStateSaveID() { return ++m_save_id; }
103 
104   enum {
105     Trans_Pending =
106         0, // Transaction is pending, and checkpoint state has been snapshotted.
107     Trans_Done = 1, // Transaction is done, the current state is committed, and
108                     // checkpoint state is irrelevant.
109     Trans_Rolled_Back = 2 // Transaction is done, the current state has been
110                           // rolled back to the checkpoint state.
111   };
112   virtual bool StartTransForHWP() { return true; }
113   virtual bool RollbackTransForHWP() { return true; }
114   virtual bool FinishTransForHWP() { return true; }
115 
116   uint32_t m_save_id; // An always incrementing integer ID used with
117                       // SaveRegisterState/RestoreRegisterState
118 };
119 
120 #include "MacOSX/arm/DNBArchImpl.h"
121 #include "MacOSX/arm64/DNBArchImplARM64.h"
122 #include "MacOSX/i386/DNBArchImplI386.h"
123 #include "MacOSX/ppc/DNBArchImpl.h"
124 #include "MacOSX/x86_64/DNBArchImplX86_64.h"
125 
126 #endif
127