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