interrupts_and_exceptions.py (6298:9af8736c26be) interrupts_and_exceptions.py (6344:b7104eda0795)
1# Copyright (c) 2007-2008 The Hewlett-Packard Development Company
2# All rights reserved.
3#
4# Redistribution and use of this software in source and binary forms,
5# with or without modification, are permitted provided that the
6# following conditions are met:
7#
8# The software must be used only for Non-Commercial Use which means any
9# use which is NOT directed to receiving any direct monetary
10# compensation for, or commercial advantage from such use. Illustrative
11# examples of non-commercial use are academic research, personal study,
12# teaching, education and corporate research & development.
13# Illustrative examples of commercial use are distributing products for
14# commercial advantage and providing services using the software for
15# commercial advantage.
16#
17# If you wish to use this software or functionality therein that may be
18# covered by patents for commercial use, please contact:
19# Director of Intellectual Property Licensing
20# Office of Strategy and Technology
21# Hewlett-Packard Company
22# 1501 Page Mill Road
23# Palo Alto, California 94304
24#
25# Redistributions of source code must retain the above copyright notice,
26# this list of conditions and the following disclaimer. Redistributions
27# in binary form must reproduce the above copyright notice, this list of
28# conditions and the following disclaimer in the documentation and/or
29# other materials provided with the distribution. Neither the name of
30# the COPYRIGHT HOLDER(s), HEWLETT-PACKARD COMPANY, nor the names of its
31# contributors may be used to endorse or promote products derived from
32# this software without specific prior written permission. No right of
33# sublicense is granted herewith. Derivatives of the software and
34# output created using the software may be prepared, but only for
35# Non-Commercial Uses. Derivatives of the software may be shared with
36# others provided: (i) the others agree to abide by the list of
37# conditions herein which includes the Non-Commercial Use restrictions;
38# and (ii) such Derivatives of the software include the above copyright
39# notice to acknowledge the contribution from this software where
40# applicable, this list of conditions and the disclaimer below.
41#
42# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
43# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
44# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
45# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
46# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
47# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
48# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
49# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
50# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
51# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
52# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
53#
54# Authors: Gabe Black
55
56microcode = '''
57def macroop IRET_REAL {
58 panic "Real mode iret isn't implemented!"
59};
60
61def macroop IRET_PROT {
62 .adjust_env oszIn64Override
63
64 # Check for a nested task. This isn't supported at the moment.
65 rflag t1, NT
66 panic "Task switching with iret is unimplemented!", flags=(nCEZF,)
67
68 #t1 = temp_RIP
69 #t2 = temp_CS
70 #t3 = temp_RFLAGS
71 #t4 = handy m5 register
72
73 # Pop temp_RIP, temp_CS, and temp_RFLAGS
74 ld t1, ss, [1, t0, rsp], "0 * env.stackSize", dataSize=ssz
75 ld t2, ss, [1, t0, rsp], "1 * env.stackSize", dataSize=ssz
76 ld t3, ss, [1, t0, rsp], "2 * env.stackSize", dataSize=ssz
77
78 # Read the handy m5 register for use later
79 rdm5reg t4
80
81
82###
83### Handle if we're returning to virtual 8086 mode.
84###
85
86 #IF ((temp_RFLAGS.VM=1) && (CPL=0) && (LEGACY_MODE))
87 # IRET_FROM_PROTECTED_TO_VIRTUAL
88
89 #temp_RFLAGS.VM != 1
90 rcri t0, t3, 18, flags=(ECF,)
91 br label("protToVirtFallThrough"), flags=(nCECF,)
92
93 #CPL=0
94 andi t0, t4, 0x30, flags=(EZF,)
95 br label("protToVirtFallThrough"), flags=(nCEZF,)
96
97 #(LEGACY_MODE)
98 rcri t0, t4, 1, flags=(ECF,)
99 br label("protToVirtFallThrough"), flags=(nCECF,)
100
101 panic "iret to virtual mode not supported"
102
103protToVirtFallThrough:
104
105
106
107 #temp_CPL = temp_CS.rpl
108 andi t5, t2, 0x3
109
110
111###
112### Read in the info for the new CS segment.
113###
114
115 #CS = READ_DESCRIPTOR (temp_CS, iret_chk)
116 andi t0, t2, 0xFC, flags=(EZF,), dataSize=2
117 br label("processCSDescriptor"), flags=(CEZF,)
118 andi t6, t2, 0xF8, dataSize=8
119 andi t0, t2, 0x4, flags=(EZF,), dataSize=2
120 br label("globalCSDescriptor"), flags=(CEZF,)
121 ld t8, tsl, [1, t0, t6], dataSize=8
122 br label("processCSDescriptor")
123globalCSDescriptor:
124 ld t8, tsg, [1, t0, t6], dataSize=8
125processCSDescriptor:
126 chks t2, t6, dataSize=8
127
128
129###
130### Get the new stack pointer and stack segment off the old stack if necessary,
131### and piggyback on the logic to check the new RIP value.
132###
133 #IF ((64BIT_MODE) || (temp_CPL!=CPL))
134 #{
135
136 #(64BIT_MODE)
137 andi t0, t4, 0xE, flags=(EZF,)
138 # Since we just found out we're in 64 bit mode, take advantage and
139 # do the appropriate RIP checks.
140 br label("doPopStackStuffAndCheckRIP"), flags=(CEZF,)
141
142 # Here, we know we're -not- in 64 bit mode, so we should do the
143 # appropriate/other RIP checks.
144 # if temp_RIP > CS.limit throw #GP(0)
145 rdlimit t6, cs, dataSize=8
1# Copyright (c) 2007-2008 The Hewlett-Packard Development Company
2# All rights reserved.
3#
4# Redistribution and use of this software in source and binary forms,
5# with or without modification, are permitted provided that the
6# following conditions are met:
7#
8# The software must be used only for Non-Commercial Use which means any
9# use which is NOT directed to receiving any direct monetary
10# compensation for, or commercial advantage from such use. Illustrative
11# examples of non-commercial use are academic research, personal study,
12# teaching, education and corporate research & development.
13# Illustrative examples of commercial use are distributing products for
14# commercial advantage and providing services using the software for
15# commercial advantage.
16#
17# If you wish to use this software or functionality therein that may be
18# covered by patents for commercial use, please contact:
19# Director of Intellectual Property Licensing
20# Office of Strategy and Technology
21# Hewlett-Packard Company
22# 1501 Page Mill Road
23# Palo Alto, California 94304
24#
25# Redistributions of source code must retain the above copyright notice,
26# this list of conditions and the following disclaimer. Redistributions
27# in binary form must reproduce the above copyright notice, this list of
28# conditions and the following disclaimer in the documentation and/or
29# other materials provided with the distribution. Neither the name of
30# the COPYRIGHT HOLDER(s), HEWLETT-PACKARD COMPANY, nor the names of its
31# contributors may be used to endorse or promote products derived from
32# this software without specific prior written permission. No right of
33# sublicense is granted herewith. Derivatives of the software and
34# output created using the software may be prepared, but only for
35# Non-Commercial Uses. Derivatives of the software may be shared with
36# others provided: (i) the others agree to abide by the list of
37# conditions herein which includes the Non-Commercial Use restrictions;
38# and (ii) such Derivatives of the software include the above copyright
39# notice to acknowledge the contribution from this software where
40# applicable, this list of conditions and the disclaimer below.
41#
42# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
43# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
44# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
45# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
46# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
47# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
48# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
49# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
50# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
51# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
52# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
53#
54# Authors: Gabe Black
55
56microcode = '''
57def macroop IRET_REAL {
58 panic "Real mode iret isn't implemented!"
59};
60
61def macroop IRET_PROT {
62 .adjust_env oszIn64Override
63
64 # Check for a nested task. This isn't supported at the moment.
65 rflag t1, NT
66 panic "Task switching with iret is unimplemented!", flags=(nCEZF,)
67
68 #t1 = temp_RIP
69 #t2 = temp_CS
70 #t3 = temp_RFLAGS
71 #t4 = handy m5 register
72
73 # Pop temp_RIP, temp_CS, and temp_RFLAGS
74 ld t1, ss, [1, t0, rsp], "0 * env.stackSize", dataSize=ssz
75 ld t2, ss, [1, t0, rsp], "1 * env.stackSize", dataSize=ssz
76 ld t3, ss, [1, t0, rsp], "2 * env.stackSize", dataSize=ssz
77
78 # Read the handy m5 register for use later
79 rdm5reg t4
80
81
82###
83### Handle if we're returning to virtual 8086 mode.
84###
85
86 #IF ((temp_RFLAGS.VM=1) && (CPL=0) && (LEGACY_MODE))
87 # IRET_FROM_PROTECTED_TO_VIRTUAL
88
89 #temp_RFLAGS.VM != 1
90 rcri t0, t3, 18, flags=(ECF,)
91 br label("protToVirtFallThrough"), flags=(nCECF,)
92
93 #CPL=0
94 andi t0, t4, 0x30, flags=(EZF,)
95 br label("protToVirtFallThrough"), flags=(nCEZF,)
96
97 #(LEGACY_MODE)
98 rcri t0, t4, 1, flags=(ECF,)
99 br label("protToVirtFallThrough"), flags=(nCECF,)
100
101 panic "iret to virtual mode not supported"
102
103protToVirtFallThrough:
104
105
106
107 #temp_CPL = temp_CS.rpl
108 andi t5, t2, 0x3
109
110
111###
112### Read in the info for the new CS segment.
113###
114
115 #CS = READ_DESCRIPTOR (temp_CS, iret_chk)
116 andi t0, t2, 0xFC, flags=(EZF,), dataSize=2
117 br label("processCSDescriptor"), flags=(CEZF,)
118 andi t6, t2, 0xF8, dataSize=8
119 andi t0, t2, 0x4, flags=(EZF,), dataSize=2
120 br label("globalCSDescriptor"), flags=(CEZF,)
121 ld t8, tsl, [1, t0, t6], dataSize=8
122 br label("processCSDescriptor")
123globalCSDescriptor:
124 ld t8, tsg, [1, t0, t6], dataSize=8
125processCSDescriptor:
126 chks t2, t6, dataSize=8
127
128
129###
130### Get the new stack pointer and stack segment off the old stack if necessary,
131### and piggyback on the logic to check the new RIP value.
132###
133 #IF ((64BIT_MODE) || (temp_CPL!=CPL))
134 #{
135
136 #(64BIT_MODE)
137 andi t0, t4, 0xE, flags=(EZF,)
138 # Since we just found out we're in 64 bit mode, take advantage and
139 # do the appropriate RIP checks.
140 br label("doPopStackStuffAndCheckRIP"), flags=(CEZF,)
141
142 # Here, we know we're -not- in 64 bit mode, so we should do the
143 # appropriate/other RIP checks.
144 # if temp_RIP > CS.limit throw #GP(0)
145 rdlimit t6, cs, dataSize=8
146 subi t0, t1, t6, flags=(ECF,)
146 sub t0, t1, t6, flags=(ECF,)
147 fault "new GeneralProtection(0)", flags=(CECF,)
148
149 #(temp_CPL!=CPL)
150 srli t7, t4, 4
151 xor t7, t7, t5
152 andi t0, t7, 0x3, flags=(EZF,)
153 br label("doPopStackStuff"), flags=(nCEZF,)
154 # We can modify user visible state here because we're know
155 # we're done with things that can fault.
156 addi rsp, rsp, "3 * env.stackSize"
157 br label("fallThroughPopStackStuff")
158
159doPopStackStuffAndCheckRIP:
160 # Check if the RIP is canonical.
161 srai t7, t1, 47, flags=(EZF,), dataSize=ssz
162 # if t7 isn't 0 or -1, it wasn't canonical.
163 br label("doPopStackStuff"), flags=(CEZF,)
164 addi t0, t7, 1, flags=(EZF,), dataSize=ssz
165 fault "new GeneralProtection(0)", flags=(nCEZF,)
166
167doPopStackStuff:
168 # POP.v temp_RSP
169 ld t6, ss, [1, t0, rsp], "3 * env.dataSize", dataSize=ssz
170 # POP.v temp_SS
171 ld t9, ss, [1, t0, rsp], "4 * env.dataSize", dataSize=ssz
172 # SS = READ_DESCRIPTOR (temp_SS, ss_chk)
173 andi t0, t9, 0xFC, flags=(EZF,), dataSize=2
174 br label("processSSDescriptor"), flags=(CEZF,)
175 andi t7, t9, 0xF8, dataSize=8
176 andi t0, t9, 0x4, flags=(EZF,), dataSize=2
177 br label("globalSSDescriptor"), flags=(CEZF,)
178 ld t7, tsl, [1, t0, t7], dataSize=8
179 br label("processSSDescriptor")
180globalSSDescriptor:
181 ld t7, tsg, [1, t0, t7], dataSize=8
182processSSDescriptor:
183 chks t9, t7, dataSize=8
184
185 # This actually updates state which is wrong. It should wait until we know
186 # we're not going to fault. Unfortunately, that's hard to do.
187 wrdl ss, t7, t9
188 wrsel ss, t9
189
190###
191### From this point downwards, we can't fault. We can update user visible state.
192###
193 # RSP.s = temp_RSP
194 mov rsp, rsp, t6, dataSize=ssz
195
196 #}
197
198fallThroughPopStackStuff:
199
200 # Update CS
201 wrdl cs, t8, t2
202 wrsel cs, t2
203
204 #CPL = temp_CPL
205
206 #IF (changing CPL)
207 #{
208 srli t7, t4, 4
209 xor t7, t7, t5
210 andi t0, t7, 0x3, flags=(EZF,)
211 br label("skipSegmentSquashing"), flags=(CEZF,)
212
213 # The attribute register needs to keep track of more info before this will
214 # work the way it needs to.
215 # FOR (seg = ES, DS, FS, GS)
216 # IF ((seg.attr.dpl < cpl && ((seg.attr.type = 'data')
217 # || (seg.attr.type = 'non-conforming-code')))
218 # {
219 # seg = NULL
220 # }
221 #}
222
223skipSegmentSquashing:
224
225 # Ignore this for now.
226 #RFLAGS.v = temp_RFLAGS
227 wrflags t0, t3
228 # VIF,VIP,IOPL only changed if (old_CPL = 0)
229 # IF only changed if (old_CPL <= old_RFLAGS.IOPL)
230 # VM unchanged
231 # RF cleared
232
233 #RIP = temp_RIP
234 wrip t0, t1, dataSize=ssz
235};
236
237def macroop IRET_VIRT {
238 panic "Virtual mode iret isn't implemented!"
239};
240'''
241#let {{
242# class INT(Inst):
243# "GenFault ${new UnimpInstFault}"
244# class INTO(Inst):
245# "GenFault ${new UnimpInstFault}"
246#}};
147 fault "new GeneralProtection(0)", flags=(CECF,)
148
149 #(temp_CPL!=CPL)
150 srli t7, t4, 4
151 xor t7, t7, t5
152 andi t0, t7, 0x3, flags=(EZF,)
153 br label("doPopStackStuff"), flags=(nCEZF,)
154 # We can modify user visible state here because we're know
155 # we're done with things that can fault.
156 addi rsp, rsp, "3 * env.stackSize"
157 br label("fallThroughPopStackStuff")
158
159doPopStackStuffAndCheckRIP:
160 # Check if the RIP is canonical.
161 srai t7, t1, 47, flags=(EZF,), dataSize=ssz
162 # if t7 isn't 0 or -1, it wasn't canonical.
163 br label("doPopStackStuff"), flags=(CEZF,)
164 addi t0, t7, 1, flags=(EZF,), dataSize=ssz
165 fault "new GeneralProtection(0)", flags=(nCEZF,)
166
167doPopStackStuff:
168 # POP.v temp_RSP
169 ld t6, ss, [1, t0, rsp], "3 * env.dataSize", dataSize=ssz
170 # POP.v temp_SS
171 ld t9, ss, [1, t0, rsp], "4 * env.dataSize", dataSize=ssz
172 # SS = READ_DESCRIPTOR (temp_SS, ss_chk)
173 andi t0, t9, 0xFC, flags=(EZF,), dataSize=2
174 br label("processSSDescriptor"), flags=(CEZF,)
175 andi t7, t9, 0xF8, dataSize=8
176 andi t0, t9, 0x4, flags=(EZF,), dataSize=2
177 br label("globalSSDescriptor"), flags=(CEZF,)
178 ld t7, tsl, [1, t0, t7], dataSize=8
179 br label("processSSDescriptor")
180globalSSDescriptor:
181 ld t7, tsg, [1, t0, t7], dataSize=8
182processSSDescriptor:
183 chks t9, t7, dataSize=8
184
185 # This actually updates state which is wrong. It should wait until we know
186 # we're not going to fault. Unfortunately, that's hard to do.
187 wrdl ss, t7, t9
188 wrsel ss, t9
189
190###
191### From this point downwards, we can't fault. We can update user visible state.
192###
193 # RSP.s = temp_RSP
194 mov rsp, rsp, t6, dataSize=ssz
195
196 #}
197
198fallThroughPopStackStuff:
199
200 # Update CS
201 wrdl cs, t8, t2
202 wrsel cs, t2
203
204 #CPL = temp_CPL
205
206 #IF (changing CPL)
207 #{
208 srli t7, t4, 4
209 xor t7, t7, t5
210 andi t0, t7, 0x3, flags=(EZF,)
211 br label("skipSegmentSquashing"), flags=(CEZF,)
212
213 # The attribute register needs to keep track of more info before this will
214 # work the way it needs to.
215 # FOR (seg = ES, DS, FS, GS)
216 # IF ((seg.attr.dpl < cpl && ((seg.attr.type = 'data')
217 # || (seg.attr.type = 'non-conforming-code')))
218 # {
219 # seg = NULL
220 # }
221 #}
222
223skipSegmentSquashing:
224
225 # Ignore this for now.
226 #RFLAGS.v = temp_RFLAGS
227 wrflags t0, t3
228 # VIF,VIP,IOPL only changed if (old_CPL = 0)
229 # IF only changed if (old_CPL <= old_RFLAGS.IOPL)
230 # VM unchanged
231 # RF cleared
232
233 #RIP = temp_RIP
234 wrip t0, t1, dataSize=ssz
235};
236
237def macroop IRET_VIRT {
238 panic "Virtual mode iret isn't implemented!"
239};
240'''
241#let {{
242# class INT(Inst):
243# "GenFault ${new UnimpInstFault}"
244# class INTO(Inst):
245# "GenFault ${new UnimpInstFault}"
246#}};