TEMU  4.0
The Terma Emulator
CodePatterns.h
Go to the documentation of this file.
1 //===-------------------------------------------------------*- TableGen -*-===//
2 //
3 // TEMU: The Terma Emulator
4 // (c) Terma 2022
5 // Authors: Mattias Holm <maho(at)terma.com>
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef TEMU_CODE_PATTERNS_H
10 #define TEMU_CODE_PATTERNS_H
11 #include "temu-c/Support/Objsys.h"
12 #include <stdint.h>
13 
14 // NOTICE: EXPERIMENTAL API
15 // This API is in early form and is seen as experimental.
16 // You are recommended to not use it at this point in time.
17 // The API is used internally, however, in practice only IDLE detection
18 // is working at this point in time.
19 
20 typedef enum {
21  tePA_Idle, //!< Enter idle when running pattern
22  tePA_Skip, //!< Skip over code pattern (currently not supported)
23  tePA_Call, //!< Call function but run code after function returns (currently not supported)
24  tePA_CallAndSkip, //!< Call function and skip the matched instructions (currently not supported)
25  tePA_Invalid, //!< Invalid action, do not use! Internal API use only.
26 } temu_PatternAction;
27 
28 typedef struct {
29  uint32_t Instruction; //!< Instruction value
30  uint32_t Mask; //!< Instruction mask (use to ignore register numbers etc)
31 } temu_CodePatternEntry;
32 
33 /*!
34  * Code pattern structure
35  *
36  * The code pattern structure exists to make it possible to define a static list of code patterns.
37  * The patterns should be installed on a processor using the code pattern interface.
38  *
39  * The code pattern can be used to attach custom actions,
40  * either at a specific physical address
41  * or when a sequence of instructions have been matched.
42  *
43  * TEMU uses this mechanism internally to detect idle-loops.
44  *
45  * A pattern is matched if the physical address of the start matches,
46  * and all instructions in the pattern match.
47  * Zero length patterns are legal if the physical address mask is non-zero.
48  * Normally, skip length and pattern length should be the same.
49  *
50  * It is possible to skip backwards.
51  * It is also possible to skip more or less than the matched instructions.
52  *
53  * When the pattern is installed, it is injected into a trie.
54  * The injected structure is after that no longer used and can be disposed.
55  *
56  * Patterns are matched at decode time, for both interpreter
57  * and binary translator.
58  *
59  * For the interpreter, a pseudo instruction will be inserted to carry out the action.
60  *
61  * In the case of processors with branch delay slots (e.g. SPARC),
62  * the pattern is only triggered if the nPC follows the PC.
63  *
64  * Pattern entries match individual instruction chunks,
65  * i.e. for Thumb2 an entry matches a 16-bit part,
66  * meaning two entries will be needed for a 32-bit instruction.
67  *
68  * Note that processors are expected to implement one pattern interface per instruction set.
69  *
70  * Patterns are not matched if they cross page boundaries.
71  */
72 typedef struct {
73  uint64_t PhysicalAddress; //!< Physical address matched in the decoder (currently ignored)
74  uint64_t PhysicalAddressMask; //!< Physical address mask matched in the
75  //!< decoder, set to 0 to ignore address (currently ignored)
76  temu_PatternAction Action; //!< Action to take on this match
77 
78  //! Function to call for actions tePA_Call and tePA_CallAndSkip, first
79  //! argument is the processor pointer (currently ignored)
80  void (*Callback)(void *, void *);
81  void *CallbackData; //!< Data passed in second parameter to callback (currently ignored)
82  int SkipLength; //!< Number of instructions to skip (in case of tePA_Skip and tePA_CallAndSkip)
83  unsigned PatternLength; //!< Number of pattern entries
84  const temu_CodePatternEntry *Pattern; // Pattern to match
85 } temu_CodePattern;
86 
87 typedef struct {
88  void (*installPattern)(void *obj, temu_CodePattern *pattern);
89 } temu_CodePatternIface;
90 #define TEMU_CODE_PATTERN_IFACE_TYPE "temu::CodePatternIface"
91 TEMU_IFACE_REFERENCE_TYPE(temu_CodePattern);
92 
93 #endif // !TEMU_CODE_PATTERNS_H
temu_CodePattern::CallbackData
void * CallbackData
Data passed in second parameter to callback (currently ignored)
Definition: CodePatterns.h:81
tePA_Invalid
@ tePA_Invalid
Invalid action, do not use! Internal API use only.
Definition: CodePatterns.h:25
tePA_CallAndSkip
@ tePA_CallAndSkip
Call function and skip the matched instructions (currently not supported)
Definition: CodePatterns.h:24
temu_CodePattern::PhysicalAddress
uint64_t PhysicalAddress
Physical address matched in the decoder (currently ignored)
Definition: CodePatterns.h:73
temu_CodePattern::Callback
void(* Callback)(void *, void *)
Definition: CodePatterns.h:80
tePA_Call
@ tePA_Call
Call function but run code after function returns (currently not supported)
Definition: CodePatterns.h:23
temu_CodePattern::PhysicalAddressMask
uint64_t PhysicalAddressMask
Definition: CodePatterns.h:74
tePA_Skip
@ tePA_Skip
Skip over code pattern (currently not supported)
Definition: CodePatterns.h:22
temu_CodePatternIface::installPattern
void(* installPattern)(void *obj, temu_CodePattern *pattern)
Definition: CodePatterns.h:88
temu_CodePattern::PatternLength
unsigned PatternLength
Number of pattern entries.
Definition: CodePatterns.h:83
temu_CodePattern::Pattern
const temu_CodePatternEntry * Pattern
Definition: CodePatterns.h:84
temu_CodePattern::Action
temu_PatternAction Action
Action to take on this match.
Definition: CodePatterns.h:76
temu_CodePatternEntry::Mask
uint32_t Mask
Instruction mask (use to ignore register numbers etc)
Definition: CodePatterns.h:30
temu_CodePattern::SkipLength
int SkipLength
Number of instructions to skip (in case of tePA_Skip and tePA_CallAndSkip)
Definition: CodePatterns.h:82
temu_CodePatternEntry::Instruction
uint32_t Instruction
Instruction value.
Definition: CodePatterns.h:29
tePA_Idle
@ tePA_Idle
Enter idle when running pattern.
Definition: CodePatterns.h:21