xref: /freebsd/contrib/llvm-project/lldb/include/lldb/Core/EmulateInstruction.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
1 //===-- EmulateInstruction.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 #ifndef LLDB_CORE_EMULATEINSTRUCTION_H
10 #define LLDB_CORE_EMULATEINSTRUCTION_H
11 
12 #include <optional>
13 #include <string>
14 
15 #include "lldb/Core/Address.h"
16 #include "lldb/Core/Opcode.h"
17 #include "lldb/Core/PluginInterface.h"
18 #include "lldb/Utility/ArchSpec.h"
19 #include "lldb/lldb-defines.h"
20 #include "lldb/lldb-enumerations.h"
21 #include "lldb/lldb-private-enumerations.h"
22 #include "lldb/lldb-private-types.h"
23 #include "lldb/lldb-types.h"
24 
25 #include "llvm/Support/Error.h"
26 
27 #include <cstddef>
28 #include <cstdint>
29 
30 namespace lldb_private {
31 class OptionValueDictionary;
32 class RegisterContext;
33 class RegisterValue;
34 class Stream;
35 class Target;
36 class UnwindPlan;
37 class EmulateInstruction;
38 
39 using BreakpointLocations = std::vector<lldb::addr_t>;
40 
41 class SingleStepBreakpointLocationsPredictor {
42 public:
SingleStepBreakpointLocationsPredictor(std::unique_ptr<EmulateInstruction> emulator_up)43   SingleStepBreakpointLocationsPredictor(
44       std::unique_ptr<EmulateInstruction> emulator_up)
45       : m_emulator_up{std::move(emulator_up)} {}
46 
47   virtual BreakpointLocations GetBreakpointLocations(Status &status);
48 
49   virtual llvm::Expected<unsigned>
GetBreakpointSize(lldb::addr_t bp_addr)50   GetBreakpointSize([[maybe_unused]] lldb::addr_t bp_addr) {
51     return 4;
52   }
53 
54   virtual ~SingleStepBreakpointLocationsPredictor() = default;
55 
56 protected:
57   // This function retrieves the address of the next instruction as it appears
58   // in the binary file. Essentially, it reads the value of the PC register,
59   // determines the size of the current instruction (where the PC is pointing),
60   // and returns the sum of these two values.
61   lldb::addr_t GetNextInstructionAddress(Status &error);
62 
63   lldb::addr_t GetBreakpointLocationAddress(lldb::addr_t entry_pc,
64                                             Status &error);
65 
66   std::unique_ptr<EmulateInstruction> m_emulator_up;
67   bool m_emulation_result = false;
68 };
69 
70 /// \class EmulateInstruction EmulateInstruction.h
71 /// "lldb/Core/EmulateInstruction.h"
72 /// A class that allows emulation of CPU opcodes.
73 ///
74 /// This class is a plug-in interface that is accessed through the standard
75 /// static FindPlugin function call in the EmulateInstruction class. The
76 /// FindPlugin takes a target triple and returns a new object if there is a
77 /// plug-in that supports the architecture and OS. Four callbacks and a baton
78 /// are provided. The four callbacks are read register, write register, read
79 /// memory and write memory.
80 ///
81 /// This class is currently designed for these main use cases: - Auto
82 /// generation of Call Frame Information (CFI) from assembly code - Predicting
83 /// single step breakpoint locations - Emulating instructions for breakpoint
84 /// traps
85 ///
86 /// Objects can be asked to read an instruction which will cause a call to the
87 /// read register callback to get the PC, followed by a read memory call to
88 /// read the opcode. If ReadInstruction () returns true, then a call to
89 /// EmulateInstruction::EvaluateInstruction () can be made. At this point the
90 /// EmulateInstruction subclass will use all of the callbacks to emulate an
91 /// instruction.
92 ///
93 /// Clients that provide the callbacks can either do the read/write
94 /// registers/memory to actually emulate the instruction on a real or virtual
95 /// CPU, or watch for the EmulateInstruction::Context which is context for the
96 /// read/write register/memory which explains why the callback is being
97 /// called. Examples of a context are: "pushing register 3 onto the stack at
98 /// offset -12", or "adjusting stack pointer by -16". This extra context
99 /// allows the generation of
100 /// CFI information from assembly code without having to actually do
101 /// the read/write register/memory.
102 ///
103 /// Clients must be prepared that not all instructions for an Instruction Set
104 /// Architecture (ISA) will be emulated.
105 ///
106 /// Subclasses at the very least should implement the instructions that save
107 /// and restore registers onto the stack and adjustment to the stack pointer.
108 /// By just implementing a few instructions for an ISA that are the typical
109 /// prologue opcodes, you can then generate CFI using a class that will soon
110 /// be available.
111 ///
112 /// Implementing all of the instructions that affect the PC can then allow
113 /// single step prediction support.
114 ///
115 /// Implementing all of the instructions allows for emulation of opcodes for
116 /// breakpoint traps and will pave the way for "thread centric" debugging. The
117 /// current debugging model is "process centric" where all threads must be
118 /// stopped when any thread is stopped; when hitting software breakpoints we
119 /// must disable the breakpoint by restoring the original breakpoint opcode,
120 /// single stepping and restoring the breakpoint trap. If all threads were
121 /// allowed to run then other threads could miss the breakpoint.
122 ///
123 /// This class centralizes the code that usually is done in separate code
124 /// paths in a debugger (single step prediction, finding save restore
125 /// locations of registers for unwinding stack frame variables) and emulating
126 /// the instruction is just a bonus.
127 
128 class EmulateInstruction : public PluginInterface {
129 public:
130   static EmulateInstruction *FindPlugin(const ArchSpec &arch,
131                                         InstructionType supported_inst_type,
132                                         const char *plugin_name);
133 
134   enum ContextType {
135     eContextInvalid = 0,
136     // Read an instruction opcode from memory
137     eContextReadOpcode,
138 
139     // Usually used for writing a register value whose source value is an
140     // immediate
141     eContextImmediate,
142 
143     // Exclusively used when saving a register to the stack as part of the
144     // prologue
145     eContextPushRegisterOnStack,
146 
147     // Exclusively used when restoring a register off the stack as part of the
148     // epilogue
149     eContextPopRegisterOffStack,
150 
151     // Add or subtract a value from the stack
152     eContextAdjustStackPointer,
153 
154     // Adjust the frame pointer for the current frame
155     eContextSetFramePointer,
156 
157     // Typically in an epilogue sequence.  Copy the frame pointer back into the
158     // stack pointer, use SP for CFA calculations again.
159     eContextRestoreStackPointer,
160 
161     // Add or subtract a value from a base address register (other than SP)
162     eContextAdjustBaseRegister,
163 
164     // Add or subtract a value from the PC or store a value to the PC.
165     eContextAdjustPC,
166 
167     // Used in WriteRegister callbacks to indicate where the
168     eContextRegisterPlusOffset,
169 
170     // Used in WriteMemory callback to indicate where the data came from
171     eContextRegisterStore,
172 
173     eContextRegisterLoad,
174 
175     // Used when performing a PC-relative branch where the
176     eContextRelativeBranchImmediate,
177 
178     // Used when performing an absolute branch where the
179     eContextAbsoluteBranchRegister,
180 
181     // Used when performing a supervisor call to an operating system to provide
182     // a service:
183     eContextSupervisorCall,
184 
185     // Used when performing a MemU operation to read the PC-relative offset
186     // from an address.
187     eContextTableBranchReadMemory,
188 
189     // Used when random bits are written into a register
190     eContextWriteRegisterRandomBits,
191 
192     // Used when random bits are written to memory
193     eContextWriteMemoryRandomBits,
194 
195     eContextArithmetic,
196 
197     eContextAdvancePC,
198 
199     eContextReturnFromException
200   };
201 
202   enum InfoType {
203     eInfoTypeRegisterPlusOffset,
204     eInfoTypeRegisterPlusIndirectOffset,
205     eInfoTypeRegisterToRegisterPlusOffset,
206     eInfoTypeRegisterToRegisterPlusIndirectOffset,
207     eInfoTypeRegisterRegisterOperands,
208     eInfoTypeOffset,
209     eInfoTypeRegister,
210     eInfoTypeImmediate,
211     eInfoTypeImmediateSigned,
212     eInfoTypeAddress,
213     eInfoTypeISAAndImmediate,
214     eInfoTypeISAAndImmediateSigned,
215     eInfoTypeISA,
216     eInfoTypeNoArgs
217   };
218 
219   struct Context {
220     ContextType type = eContextInvalid;
221 
222   private:
223     enum InfoType info_type = eInfoTypeNoArgs;
224 
225   public:
GetInfoTypeContext226     enum InfoType GetInfoType() const { return info_type; }
227     union ContextInfo {
228       struct RegisterPlusOffset {
229         RegisterInfo reg;      // base register
230         int64_t signed_offset; // signed offset added to base register
231       } RegisterPlusOffset;
232 
233       struct RegisterPlusIndirectOffset {
234         RegisterInfo base_reg;   // base register number
235         RegisterInfo offset_reg; // offset register kind
236       } RegisterPlusIndirectOffset;
237 
238       struct RegisterToRegisterPlusOffset {
239         RegisterInfo data_reg; // source/target register for data
240         RegisterInfo base_reg; // base register for address calculation
241         int64_t offset;        // offset for address calculation
242       } RegisterToRegisterPlusOffset;
243 
244       struct RegisterToRegisterPlusIndirectOffset {
245         RegisterInfo base_reg;   // base register for address calculation
246         RegisterInfo offset_reg; // offset register for address calculation
247         RegisterInfo data_reg;   // source/target register for data
248       } RegisterToRegisterPlusIndirectOffset;
249 
250       struct RegisterRegisterOperands {
251         RegisterInfo
252             operand1; // register containing first operand for binary op
253         RegisterInfo
254             operand2; // register containing second operand for binary op
255       } RegisterRegisterOperands;
256 
257       int64_t signed_offset; // signed offset by which to adjust self (for
258                              // registers only)
259 
260       RegisterInfo reg; // plain register
261 
262       uint64_t unsigned_immediate; // unsigned immediate value
263       int64_t signed_immediate;    // signed immediate value
264 
265       lldb::addr_t address; // direct address
266 
267       struct ISAAndImmediate {
268         uint32_t isa;
269         uint32_t unsigned_data32; // immediate data
270       } ISAAndImmediate;
271 
272       struct ISAAndImmediateSigned {
273         uint32_t isa;
274         int32_t signed_data32; // signed immediate data
275       } ISAAndImmediateSigned;
276 
277       uint32_t isa;
278     } info;
279     static_assert(std::is_trivial<ContextInfo>::value,
280                   "ContextInfo must be trivial.");
281 
282     Context() = default;
283 
SetRegisterPlusOffsetContext284     void SetRegisterPlusOffset(RegisterInfo base_reg, int64_t signed_offset) {
285       info_type = eInfoTypeRegisterPlusOffset;
286       info.RegisterPlusOffset.reg = base_reg;
287       info.RegisterPlusOffset.signed_offset = signed_offset;
288     }
289 
SetRegisterPlusIndirectOffsetContext290     void SetRegisterPlusIndirectOffset(RegisterInfo base_reg,
291                                        RegisterInfo offset_reg) {
292       info_type = eInfoTypeRegisterPlusIndirectOffset;
293       info.RegisterPlusIndirectOffset.base_reg = base_reg;
294       info.RegisterPlusIndirectOffset.offset_reg = offset_reg;
295     }
296 
SetRegisterToRegisterPlusOffsetContext297     void SetRegisterToRegisterPlusOffset(RegisterInfo data_reg,
298                                          RegisterInfo base_reg,
299                                          int64_t offset) {
300       info_type = eInfoTypeRegisterToRegisterPlusOffset;
301       info.RegisterToRegisterPlusOffset.data_reg = data_reg;
302       info.RegisterToRegisterPlusOffset.base_reg = base_reg;
303       info.RegisterToRegisterPlusOffset.offset = offset;
304     }
305 
SetRegisterToRegisterPlusIndirectOffsetContext306     void SetRegisterToRegisterPlusIndirectOffset(RegisterInfo base_reg,
307                                                  RegisterInfo offset_reg,
308                                                  RegisterInfo data_reg) {
309       info_type = eInfoTypeRegisterToRegisterPlusIndirectOffset;
310       info.RegisterToRegisterPlusIndirectOffset.base_reg = base_reg;
311       info.RegisterToRegisterPlusIndirectOffset.offset_reg = offset_reg;
312       info.RegisterToRegisterPlusIndirectOffset.data_reg = data_reg;
313     }
314 
SetRegisterRegisterOperandsContext315     void SetRegisterRegisterOperands(RegisterInfo op1_reg,
316                                      RegisterInfo op2_reg) {
317       info_type = eInfoTypeRegisterRegisterOperands;
318       info.RegisterRegisterOperands.operand1 = op1_reg;
319       info.RegisterRegisterOperands.operand2 = op2_reg;
320     }
321 
SetOffsetContext322     void SetOffset(int64_t signed_offset) {
323       info_type = eInfoTypeOffset;
324       info.signed_offset = signed_offset;
325     }
326 
SetRegisterContext327     void SetRegister(RegisterInfo reg) {
328       info_type = eInfoTypeRegister;
329       info.reg = reg;
330     }
331 
SetImmediateContext332     void SetImmediate(uint64_t immediate) {
333       info_type = eInfoTypeImmediate;
334       info.unsigned_immediate = immediate;
335     }
336 
SetImmediateSignedContext337     void SetImmediateSigned(int64_t signed_immediate) {
338       info_type = eInfoTypeImmediateSigned;
339       info.signed_immediate = signed_immediate;
340     }
341 
SetAddressContext342     void SetAddress(lldb::addr_t address) {
343       info_type = eInfoTypeAddress;
344       info.address = address;
345     }
SetISAAndImmediateContext346     void SetISAAndImmediate(uint32_t isa, uint32_t data) {
347       info_type = eInfoTypeISAAndImmediate;
348       info.ISAAndImmediate.isa = isa;
349       info.ISAAndImmediate.unsigned_data32 = data;
350     }
351 
SetISAAndImmediateSignedContext352     void SetISAAndImmediateSigned(uint32_t isa, int32_t data) {
353       info_type = eInfoTypeISAAndImmediateSigned;
354       info.ISAAndImmediateSigned.isa = isa;
355       info.ISAAndImmediateSigned.signed_data32 = data;
356     }
357 
SetISAContext358     void SetISA(uint32_t isa) {
359       info_type = eInfoTypeISA;
360       info.isa = isa;
361     }
362 
SetNoArgsContext363     void SetNoArgs() { info_type = eInfoTypeNoArgs; }
364 
365     void Dump(Stream &s, EmulateInstruction *instruction) const;
366   };
367 
368   typedef size_t (*ReadMemoryCallback)(EmulateInstruction *instruction,
369                                        void *baton, const Context &context,
370                                        lldb::addr_t addr, void *dst,
371                                        size_t length);
372 
373   typedef size_t (*WriteMemoryCallback)(EmulateInstruction *instruction,
374                                         void *baton, const Context &context,
375                                         lldb::addr_t addr, const void *dst,
376                                         size_t length);
377 
378   typedef bool (*ReadRegisterCallback)(EmulateInstruction *instruction,
379                                        void *baton,
380                                        const RegisterInfo *reg_info,
381                                        RegisterValue &reg_value);
382 
383   typedef bool (*WriteRegisterCallback)(EmulateInstruction *instruction,
384                                         void *baton, const Context &context,
385                                         const RegisterInfo *reg_info,
386                                         const RegisterValue &reg_value);
387 
388   // Type to represent the condition of an instruction. The UINT32 value is
389   // reserved for the unconditional case and all other value can be used in an
390   // architecture dependent way.
391   typedef uint32_t InstructionCondition;
392   static const InstructionCondition UnconditionalCondition = UINT32_MAX;
393 
394   EmulateInstruction(const ArchSpec &arch);
395 
396   ~EmulateInstruction() override = default;
397 
398   // Mandatory overrides
399   virtual bool
400   SupportsEmulatingInstructionsOfType(InstructionType inst_type) = 0;
401 
402   virtual bool SetTargetTriple(const ArchSpec &arch) = 0;
403 
404   virtual bool ReadInstruction() = 0;
405 
GetLastInstrSize()406   virtual std::optional<uint32_t> GetLastInstrSize() { return std::nullopt; }
407 
408   virtual bool EvaluateInstruction(uint32_t evaluate_options) = 0;
409 
GetInstructionCondition()410   virtual InstructionCondition GetInstructionCondition() {
411     return UnconditionalCondition;
412   }
413 
414   virtual bool TestEmulation(Stream &out_stream, ArchSpec &arch,
415                              OptionValueDictionary *test_data) = 0;
416 
417   virtual std::optional<RegisterInfo>
418   GetRegisterInfo(lldb::RegisterKind reg_kind, uint32_t reg_num) = 0;
419 
420   // Optional overrides
421   virtual bool SetInstruction(const Opcode &insn_opcode,
422                               const Address &inst_addr, Target *target);
423 
424   virtual bool CreateFunctionEntryUnwind(UnwindPlan &unwind_plan);
425 
426   static const char *TranslateRegister(lldb::RegisterKind reg_kind,
427                                        uint32_t reg_num, std::string &reg_name);
428 
429   // RegisterInfo variants
430   std::optional<RegisterValue> ReadRegister(const RegisterInfo &reg_info);
431 
432   uint64_t ReadRegisterUnsigned(const RegisterInfo &reg_info,
433                                 uint64_t fail_value, bool *success_ptr);
434 
435   bool WriteRegister(const Context &context, const RegisterInfo &ref_info,
436                      const RegisterValue &reg_value);
437 
438   bool WriteRegisterUnsigned(const Context &context,
439                              const RegisterInfo &reg_info, uint64_t reg_value);
440 
441   // Register kind and number variants
442   bool ReadRegister(lldb::RegisterKind reg_kind, uint32_t reg_num,
443                     RegisterValue &reg_value);
444 
445   bool WriteRegister(const Context &context, lldb::RegisterKind reg_kind,
446                      uint32_t reg_num, const RegisterValue &reg_value);
447 
448   uint64_t ReadRegisterUnsigned(lldb::RegisterKind reg_kind, uint32_t reg_num,
449                                 uint64_t fail_value, bool *success_ptr);
450 
451   bool WriteRegisterUnsigned(const Context &context,
452                              lldb::RegisterKind reg_kind, uint32_t reg_num,
453                              uint64_t reg_value);
454 
455   size_t ReadMemory(const Context &context, lldb::addr_t addr, void *dst,
456                     size_t dst_len);
457 
458   uint64_t ReadMemoryUnsigned(const Context &context, lldb::addr_t addr,
459                               size_t byte_size, uint64_t fail_value,
460                               bool *success_ptr);
461 
462   bool WriteMemory(const Context &context, lldb::addr_t addr, const void *src,
463                    size_t src_len);
464 
465   bool WriteMemoryUnsigned(const Context &context, lldb::addr_t addr,
466                            uint64_t uval, size_t uval_byte_size);
467 
GetAddressByteSize()468   uint32_t GetAddressByteSize() const { return m_arch.GetAddressByteSize(); }
469 
GetByteOrder()470   lldb::ByteOrder GetByteOrder() const { return m_arch.GetByteOrder(); }
471 
GetOpcode()472   const Opcode &GetOpcode() const { return m_opcode; }
473 
GetAddress()474   lldb::addr_t GetAddress() const { return m_addr; }
475 
GetArchitecture()476   const ArchSpec &GetArchitecture() const { return m_arch; }
477 
478   static size_t ReadMemoryFrame(EmulateInstruction *instruction, void *baton,
479                                 const Context &context, lldb::addr_t addr,
480                                 void *dst, size_t length);
481 
482   static size_t WriteMemoryFrame(EmulateInstruction *instruction, void *baton,
483                                  const Context &context, lldb::addr_t addr,
484                                  const void *dst, size_t length);
485 
486   static bool ReadRegisterFrame(EmulateInstruction *instruction, void *baton,
487                                 const RegisterInfo *reg_info,
488                                 RegisterValue &reg_value);
489 
490   static bool WriteRegisterFrame(EmulateInstruction *instruction, void *baton,
491                                  const Context &context,
492                                  const RegisterInfo *reg_info,
493                                  const RegisterValue &reg_value);
494 
495   static size_t ReadMemoryDefault(EmulateInstruction *instruction, void *baton,
496                                   const Context &context, lldb::addr_t addr,
497                                   void *dst, size_t length);
498 
499   static size_t WriteMemoryDefault(EmulateInstruction *instruction, void *baton,
500                                    const Context &context, lldb::addr_t addr,
501                                    const void *dst, size_t length);
502 
503   static bool ReadRegisterDefault(EmulateInstruction *instruction, void *baton,
504                                   const RegisterInfo *reg_info,
505                                   RegisterValue &reg_value);
506 
507   static bool WriteRegisterDefault(EmulateInstruction *instruction, void *baton,
508                                    const Context &context,
509                                    const RegisterInfo *reg_info,
510                                    const RegisterValue &reg_value);
511 
512   void SetBaton(void *baton);
513 
514   void SetCallbacks(ReadMemoryCallback read_mem_callback,
515                     WriteMemoryCallback write_mem_callback,
516                     ReadRegisterCallback read_reg_callback,
517                     WriteRegisterCallback write_reg_callback);
518 
519   void SetReadMemCallback(ReadMemoryCallback read_mem_callback);
520 
521   void SetWriteMemCallback(WriteMemoryCallback write_mem_callback);
522 
523   void SetReadRegCallback(ReadRegisterCallback read_reg_callback);
524 
525   void SetWriteRegCallback(WriteRegisterCallback write_reg_callback);
526 
527   static bool GetBestRegisterKindAndNumber(const RegisterInfo *reg_info,
528                                            lldb::RegisterKind &reg_kind,
529                                            uint32_t &reg_num);
530 
531   static uint32_t GetInternalRegisterNumber(RegisterContext *reg_ctx,
532                                             const RegisterInfo &reg_info);
533 
534   static std::unique_ptr<SingleStepBreakpointLocationsPredictor>
535   CreateBreakpointLocationPredictor(
536       std::unique_ptr<EmulateInstruction> emulator_up);
537 
538   // Helper functions
539   std::optional<lldb::addr_t> ReadPC();
540   bool WritePC(lldb::addr_t addr);
541 
542 protected:
543   using BreakpointLocationsPredictorCreator =
544       std::function<std::unique_ptr<SingleStepBreakpointLocationsPredictor>(
545           std::unique_ptr<EmulateInstruction>)>;
546 
547   ArchSpec m_arch;
548   void *m_baton = nullptr;
549   ReadMemoryCallback m_read_mem_callback = &ReadMemoryDefault;
550   WriteMemoryCallback m_write_mem_callback = &WriteMemoryDefault;
551   ReadRegisterCallback m_read_reg_callback = &ReadRegisterDefault;
552   WriteRegisterCallback m_write_reg_callback = &WriteRegisterDefault;
553   lldb::addr_t m_addr = LLDB_INVALID_ADDRESS;
554   Opcode m_opcode;
555 
556 private:
557   virtual BreakpointLocationsPredictorCreator
GetSingleStepBreakpointLocationsPredictorCreator()558   GetSingleStepBreakpointLocationsPredictorCreator() {
559     if (!m_arch.IsMIPS() && !m_arch.GetTriple().isPPC64() &&
560         !m_arch.GetTriple().isLoongArch()) {
561       // Unsupported architecture
562       return [](std::unique_ptr<EmulateInstruction> emulator_up) {
563         return nullptr;
564       };
565     }
566     return [](std::unique_ptr<EmulateInstruction> emulator_up) {
567       return std::make_unique<SingleStepBreakpointLocationsPredictor>(
568           std::move(emulator_up));
569     };
570   }
571 
572   // For EmulateInstruction only
573   EmulateInstruction(const EmulateInstruction &) = delete;
574   const EmulateInstruction &operator=(const EmulateInstruction &) = delete;
575 };
576 
577 } // namespace lldb_private
578 
579 #endif // LLDB_CORE_EMULATEINSTRUCTION_H
580