-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickints.c
More file actions
174 lines (149 loc) · 6.62 KB
/
quickints.c
File metadata and controls
174 lines (149 loc) · 6.62 KB
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
#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
#include <exec/execbase.h>
#include <exec/types.h>
#include <exec/exec.h>
#include <proto/exec.h>
#include <sys/cdefs.h>
//#define DEBUG
// Global to cache BadQuickInt value
static __used ULONG BadQuickInt = 0;
static __used BOOL BadQuickIntFound = FALSE;
ULONG __stdargs GetQuickVec();
ULONG __stdargs ReleaseVec();
#undef ObtainQuickVector
#ifdef DEBUG
#define dprintf(x...) printf(x)
#else
#define dprintf(x...)
#endif
ULONG __stdargs ObtainQuickVector(APTR vector)
{
ULONG vectorNum;
asm volatile("move.l %0,d2" : : "g" (vector) : "d2");
vectorNum = Supervisor(GetQuickVec);
// Pass vector via register a0 and call supervisor
asm volatile(
" bra 1f \n"
" .globl _GetQuickVec \n"
"_GetQuickVec: \n"
" move.l d2,a0 \n"
" movem.l d1-d5/a1-a3,-(sp) \n"
" sub.l a1,a1 \n" // Clear a1 (assume VBR=0)
" or.w #0x0700,sr \n"
" btst #0,(296+1)(a6) \n" // AFB_68010 in AttnFlags+1
" beq.s .NoVBR \n"
".chip 68010 \n"
" movec vbr,a1 \n"
".chip 68000 \n"
".NoVBR: \n"
// Find most common vector value between 68-255
" lea 0x110(a1),a2 \n" // Start at vector 68
" moveq #0,d2 \n" // Max count
" moveq #0,d3 \n" // BadQuickInt value
// Outer loop - for each unique value
" move.l #(255-68-1),d4 \n" // Counter for vectors 68-255
".FindLoop1: \n"
" move.l (a2)+,d0 \n" // Get vector value
" moveq #1,d1 \n" // Count = 1
// Inner loop - count occurrences
" move.l a2,a3 \n" // Start from next vector
" move.l d4,d5 \n" // Remaining vectors
" beq.s .CheckMax \n"
" subq.l #1,d5 \n"
" beq.s .CheckMax \n"
".FindLoop2: \n"
" cmp.l (a3)+,d0 \n"
" bne.s .Next2 \n"
" addq.l #1,d1 \n" // Increment count
".Next2: \n"
" subq.l #1,d5 \n"
" bne.s .FindLoop2 \n"
".CheckMax: \n"
" cmp.l d2,d1 \n" // Compare with max count
" ble.s .Next1 \n"
" move.l d1,d2 \n" // New max count
" move.l d0,d3 \n" // Save value as BadQuickInt
".Next1: \n"
" dbf d4,.FindLoop1 \n"
// Store BadQuickInt
" move.l d3,_BadQuickInt \n"
" move.w #-1,_BadQuickIntFound \n"
".SkipFind: \n"
" lea 0x400(a1),a2 \n" // Point at end of user vectors
" move.l #255,d0 \n" // Last vector number
" move.l _BadQuickInt,d1 \n" // Get BadQuickInt value
".SearchLoop: \n"
" cmp.l -(a2),d1 \n" // Check if empty
" dbeq d0,.SearchLoop \n"
" bne.s .NoVector \n"
" cmp.w #68,d0 \n" // Check if >= 68
" bcs.s .NoVector \n"
" move.l a0,(a2) \n" // Store the vector
" movem.l (sp)+,d1-d5/a1-a3 \n"
" rte \n" // Return with vector number in d0
".NoVector: \n"
" moveq #0,d0 \n" // Return failure
" movem.l (sp)+,d1-d5/a1-a3 \n"
" rte \n"
"1: \n"
: // No outputs
:
: "cc", "memory"
);
if (BadQuickIntFound) {
dprintf("BadQuickInt = 0x%08lx\n", BadQuickInt);
} else {
dprintf("BadQuickInt not found.\n");
}
return vectorNum;
}
// Function to release a quick vector
void ReleaseQuickVector(ULONG vectorNum)
{
__unused APTR oldVec;
// Validate vector number
if (vectorNum < 68 || vectorNum > 255) {
dprintf("Invalid QuickInt vector number.\n");
return; // Invalid vector number
}
// Must have found BadQuickInt before we can release
if (!BadQuickIntFound) {
dprintf("BadQuickInt was not found. Can't restore.\n");
BadQuickInt = (ULONG)NULL;
// Continue but restore with NULL
}
oldVec = (APTR)Supervisor(ReleaseVec);
dprintf(" old vector = 0x%08lx\n", (ULONG)oldVec);
// Call supervisor code to restore the vector
asm volatile(
" bra.s 1f \n"
" .globl _ReleaseVec \n"
"_ReleaseVec: \n"
" move.l a1,-(sp) \n"
" move.l %0,d0 \n" // Put vector number in d0
" or.w #0x0700,sr \n" // Disable interrupts
// Get VBR
" sub.l a1,a1 \n" // Clear a1 (assume VBR=0)
" btst #0,(296+1)(a6) \n" // AFB_68010 in AttnFlags+1
" beq.s .NoVBR2 \n"
".chip 68010 \n"
" movec vbr,a1 \n"
".chip 68000 \n"
".NoVBR2: \n"
// Calculate vector address
" lsl.l #2,d0 \n" // Vector number * 4
" add.l d0,a1 \n" // Point to specific vector
// Read Original QuickInt handler (for debugging)
" move.l (a1),d0 \n"
// Restore BadQuickInt
" move.l _BadQuickInt,(a1) \n"
" move.l (sp)+,a1 \n"
" rte \n" // Return from exception
"1: \n"
: // No outputs
: "g" (vectorNum)
: "cc", "memory"
);
}