-
Notifications
You must be signed in to change notification settings - Fork 65
/
BochsPlugin.class.st
278 lines (253 loc) · 11 KB
/
BochsPlugin.class.st
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
"
BochsPlugin is the abstract superclass for plugins that interface to the Bochs C++ IA32/x64 processor emulator..
Instance Variables
prevInterruptCheckChain: <Symbol/function pointer>
prevInterruptCheckChain
- the previous value of the interruptCheckChain function pointer
"
Class {
#name : #BochsPlugin,
#superclass : #SmartSyntaxInterpreterPlugin,
#instVars : [
'prevInterruptCheckChain'
],
#pools : [
'VMBasicConstants'
],
#category : #'Cog-ProcessorPlugins'
}
{ #category : #translation }
BochsPlugin class >> declareCVarsIn: aCCodeGenerator [
"prevInterruptCheckChain lives in sqBochsIA32|X64Plugin.c"
self ~~ BochsPlugin ifTrue:
[super declareCVarsIn: aCCodeGenerator.
aCCodeGenerator removeVariable: 'prevInterruptCheckChain']
]
{ #category : #translation }
BochsPlugin class >> hasHeaderFile [
"We need a header to declare newcpu and pull in bochs.h & cpu.h"
^true
]
{ #category : #translation }
BochsPlugin class >> shouldBeTranslated [
"Is this class intended to be translated as a plugin, perhaps specific to a platform?
Most subclasses should answer true, but some such as simulation-only versions
should answer false for various reasons."
^super shouldBeTranslated and: [self ~~ BochsPlugin]
]
{ #category : #interruption }
BochsPlugin >> forceStopOnInterrupt [
<returnTypeC: #void>
interpreterProxy getInterruptPending ifTrue:
[self forceStopRunning]
]
{ #category : #primitives }
BochsPlugin >> primitiveDisassembleAt: address "<Integer>" InMemory: memory [ "<Bitmap|ByteArray|WordArray>"
"Return an Array of the instruction length and its decompilation as a string for the instruction at address in memory."
| cpuAlien cpu instrLenOrErr resultObj log logLen logObj logObjData |
<var: #cpu type: #'void *'>
<var: #log type: #'char *'>
<var: #logLen type: #long>
<var: #logObjData type: #'char *'>
cpuAlien := self primitive: #primitiveDisassembleAtInMemory
parameters: #(Unsigned WordsOrBytes)
receiver: #Oop.
(cpu := self cCoerceSimple: (self startOfData: cpuAlien) to: #'void *') = 0 ifTrue:
[^interpreterProxy primitiveFailFor: PrimErrBadReceiver].
instrLenOrErr := self disassembleFor: cpu
At: address
In: memory
Size: (interpreterProxy byteSizeOf: memory cPtrAsOop).
instrLenOrErr < 0 ifTrue:
[^interpreterProxy primitiveFailFor: PrimErrInappropriate].
log := self getlog: (self cCode: [self addressOf: logLen] inSmalltalk: [logLen := 0]).
resultObj := interpreterProxy instantiateClass: interpreterProxy classArray indexableSize: 2.
resultObj = 0 ifTrue:
[^interpreterProxy primitiveFailFor: PrimErrNoMemory].
"Easier keeping the damn thing on the stack than using pushRemappableOop:/popRemappableOop.
Where is topRemappableOop when you need it?"
interpreterProxy pushRemappableOop: resultObj.
logObj := interpreterProxy
instantiateClass: interpreterProxy classString
indexableSize: logLen.
interpreterProxy failed ifTrue:
[interpreterProxy popRemappableOop.
^interpreterProxy primitiveFailFor: PrimErrNoMemory].
logObjData := interpreterProxy arrayValueOf: logObj.
self mem: logObjData cp: log y: logLen.
resultObj := interpreterProxy popRemappableOop.
interpreterProxy
storePointer: 0
ofObject: resultObj
withValue: (interpreterProxy integerObjectOf: instrLenOrErr).
interpreterProxy storePointer: 1 ofObject: resultObj withValue: logObj.
^resultObj
]
{ #category : #primitives }
BochsPlugin >> primitiveErrorAndLog [
| log logLen resultObj logObj logObjData |
<var: #log type: #'char *'>
<var: #logLen type: #long>
<var: #logObjData type: #'char *'>
self primitive: #primitiveErrorAndLog parameters: #().
log := self getlog: (self cCode: [self addressOf: logLen] inSmalltalk: [logLen := 0]).
resultObj := interpreterProxy instantiateClass: interpreterProxy classArray indexableSize: 2.
resultObj = 0 ifTrue:
[^interpreterProxy primitiveFailFor: PrimErrNoMemory].
interpreterProxy
storePointer: 0
ofObject: resultObj
withValue: (interpreterProxy integerObjectOf: self errorAcorn).
logLen > 0 ifTrue:
[interpreterProxy pushRemappableOop: resultObj.
logObj := interpreterProxy
instantiateClass: interpreterProxy classString
indexableSize: logLen.
interpreterProxy failed ifTrue:
[interpreterProxy popRemappableOop.
^interpreterProxy primitiveFailFor: PrimErrNoMemory].
resultObj := interpreterProxy popRemappableOop.
logObjData := interpreterProxy arrayValueOf: logObj.
self mem: logObjData cp: log y: logLen.
interpreterProxy storePointer: 1 ofObject: resultObj withValue: logObj].
interpreterProxy pop: 1 thenPush: resultObj
]
{ #category : #primitives }
BochsPlugin >> primitiveFlushICacheFrom: startAddress "<Integer>" To: endAddress [ "<Integer>"
"Flush the icache in the requested range"
| cpuAlien cpu |
<var: #cpu type: #'void *'>
cpuAlien := self primitive: #primitiveFlushICacheFromTo
parameters: #(Unsigned Unsigned)
receiver: #Oop.
(cpu := self cCoerceSimple: (self startOfData: cpuAlien) to: #'void *') = 0 ifTrue:
[^interpreterProxy primitiveFailFor: PrimErrBadReceiver].
self flushICache: cpu From: startAddress To: endAddress
]
{ #category : #primitives }
BochsPlugin >> primitiveNewCPU [
| cpu |
<var: #cpu type: #'void *'>
self primitive: #primitiveNewCPU parameters: #().
cpu := self newCPU.
cpu = 0 ifTrue:
[^interpreterProxy primitiveFail].
interpreterProxy
pop: 1
thenPush: (interpreterProxy positiveMachineIntegerFor:
(self cCoerceSimple: cpu
to: 'usqIntptr_t'))
]
{ #category : #primitives }
BochsPlugin >> primitiveResetCPU [
| cpuAlien cpu maybeErr |
<var: #cpu type: #'void *'>
cpuAlien := self primitive: #primitiveResetCPU parameters: #() receiver: #Oop.
(cpu := self cCoerceSimple: (self startOfData: cpuAlien) to: #'void *') = 0 ifTrue:
[^interpreterProxy primitiveFailFor: PrimErrBadReceiver].
maybeErr := self resetCPU: cpu.
maybeErr ~= 0 ifTrue:
[^interpreterProxy primitiveFailFor: PrimErrInappropriate].
^cpuAlien
]
{ #category : #primitives }
BochsPlugin >> primitiveRunInMemory: memory "<Bitmap|ByteArray|WordArray>" minimumAddress: minAddress "<Integer>" maximumAddress: maxAddress "<Integer>" readOnlyBelow: minWriteMaxExecAddress [ "<Integer>"
"Run the cpu using the first argument as the memory and the following arguments defining valid addresses, running until it halts or hits an exception."
| cpuAlien cpu memorySize maybeErr |
<var: #cpu type: #'void *'>
cpuAlien := self primitive: #primitiveRunInMemoryMinAddressMaxAddressReadWrite
parameters: #(WordsOrBytes Unsigned Unsigned Unsigned)
receiver: #Oop.
(cpu := self cCoerceSimple: (self startOfData: cpuAlien) to: #'void *') = 0 ifTrue:
[^interpreterProxy primitiveFailFor: PrimErrBadReceiver].
prevInterruptCheckChain := interpreterProxy setInterruptCheckChain: #forceStopOnInterrupt asSymbol.
prevInterruptCheckChain = #forceStopOnInterrupt asSymbol ifTrue:
[prevInterruptCheckChain = 0].
memorySize := interpreterProxy byteSizeOf: memory cPtrAsOop.
maybeErr := self runCPU: cpu
In: memory
Size: (memorySize min: maxAddress)
MinAddressRead: minAddress
Write: minWriteMaxExecAddress.
interpreterProxy setInterruptCheckChain: prevInterruptCheckChain.
maybeErr ~= 0 ifTrue:
[^interpreterProxy primitiveFailFor: PrimErrInappropriate].
^cpuAlien
]
{ #category : #primitives }
BochsPlugin >> primitiveRunInMemory: memory "<Bitmap|ByteArray|WordArray>" minimumAddress: minAddress "<Integer>" readOnlyBelow: minWriteMaxExecAddress [ "<Integer>"
"Run the cpu using the first argument as the memory and the following arguments defining valid addresses, running until it halts or hits an exception."
| cpuAlien cpu maybeErr |
<var: #cpu type: #'void *'>
cpuAlien := self primitive: #primitiveRunInMemoryMinimumAddressReadWrite
parameters: #(WordsOrBytes Unsigned Unsigned)
receiver: #Oop.
(cpu := self cCoerceSimple: (self startOfData: cpuAlien) to: #'void *') = 0 ifTrue:
[^interpreterProxy primitiveFailFor: PrimErrBadReceiver].
prevInterruptCheckChain := interpreterProxy setInterruptCheckChain: #forceStopOnInterrupt asSymbol.
prevInterruptCheckChain = #forceStopOnInterrupt asSymbol ifTrue:
[prevInterruptCheckChain = 0].
maybeErr := self runCPU: cpu
In: memory
Size: (interpreterProxy byteSizeOf: memory cPtrAsOop)
MinAddressRead: minAddress
Write: minWriteMaxExecAddress.
interpreterProxy setInterruptCheckChain: prevInterruptCheckChain.
maybeErr ~= 0 ifTrue:
[^interpreterProxy primitiveFailFor: PrimErrInappropriate].
^cpuAlien
]
{ #category : #primitives }
BochsPlugin >> primitiveSingleStepInMemory: memory "<Bitmap|ByteArray|WordArray>" minimumAddress: minAddress "<Integer>" maximumAddress: maxAddress "<Integer>" readOnlyBelow: minWriteMaxExecAddress [ "<Integer>"
"Single-step the cpu using the first argument as the memory and the following arguments defining valid addresses."
| cpuAlien cpu memorySize maybeErr |
<var: #cpu type: #'void *'>
cpuAlien := self primitive: #primitiveSingleStepInMemoryMinAddressMaxAddressReadWrite
parameters: #(WordsOrBytes Unsigned Unsigned Unsigned)
receiver: #Oop.
(cpu := self cCoerceSimple: (self startOfData: cpuAlien) to: #'void *') = 0 ifTrue:
[^interpreterProxy primitiveFailFor: PrimErrBadReceiver].
memorySize := interpreterProxy byteSizeOf: memory cPtrAsOop.
maybeErr := self singleStepCPU: cpu
In: memory
Size: (memorySize min: maxAddress)
MinAddressRead: minAddress
Write: minWriteMaxExecAddress.
maybeErr ~= 0 ifTrue:
[^interpreterProxy primitiveFailFor: PrimErrInappropriate].
^cpuAlien
]
{ #category : #primitives }
BochsPlugin >> primitiveSingleStepInMemory: memory "<Bitmap|ByteArray|WordArray>" minimumAddress: minAddress "<Integer>" readOnlyBelow: minWriteMaxExecAddress [ "<Integer>"
"Single-step the cpu using the first argument as the memory and the following arguments defining valid addresses."
| cpuAlien cpu maybeErr |
<var: #cpu type: #'void *'>
cpuAlien := self primitive: #primitiveSingleStepInMemoryMinimumAddressReadWrite
parameters: #(WordsOrBytes Unsigned Unsigned)
receiver: #Oop.
(cpu := self cCoerceSimple: (self startOfData: cpuAlien) to: #'void *') = 0 ifTrue:
[^interpreterProxy primitiveFailFor: PrimErrBadReceiver].
maybeErr := self singleStepCPU: cpu
In: memory
Size: (interpreterProxy byteSizeOf: memory cPtrAsOop)
MinAddressRead: minAddress
Write: minWriteMaxExecAddress.
maybeErr ~= 0 ifTrue:
[^interpreterProxy primitiveFailFor: PrimErrInappropriate].
^cpuAlien
]
{ #category : #'alien support' }
BochsPlugin >> sizeField: rcvr [
"Answer the first field of rcvr which is assumed to be an Alien of at least 8 bytes"
<inline: true>
^self longAt: rcvr + interpreterProxy baseHeaderSize
]
{ #category : #'alien support' }
BochsPlugin >> startOfData: rcvr [ "<Alien oop> ^<Integer>"
"Answer the start of rcvr's data. For direct aliens this is the address of
the second field. For indirect and pointer aliens it is what the second field points to."
<inline: true>
^(self sizeField: rcvr) > 0
ifTrue: [rcvr + interpreterProxy baseHeaderSize + interpreterProxy bytesPerOop]
ifFalse: [self longAt: rcvr + interpreterProxy baseHeaderSize + interpreterProxy bytesPerOop]
]