interrupts_and_exceptions.py revision 5812:d12ff89c7692
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 t6, tsl, [1, t0, t6], dataSize=8
122    br label("processCSDescriptor")
123globalCSDescriptor:
124    ld t6, tsg, [1, t0, t6], dataSize=8
125processCSDescriptor:
126    chks t2, t6, dataSize=8
127
128    # This actually updates state which is wrong. It should wait until we know
129    # we're not going to fault. Unfortunately, that's hard to do.
130    wrdl cs, t6, t2
131    wrsel cs, t2
132
133    #CPL = temp_CPL
134
135
136###
137### Get the new stack pointer and stack segment off the old stack if necessary,
138### and piggyback on the logic to check the new RIP value.
139###
140    #IF ((64BIT_MODE) || (temp_CPL!=CPL))
141    #{
142
143    #(64BIT_MODE)
144    andi t0, t4, 0xE, flags=(EZF,)
145    # Since we just found out we're in 64 bit mode, take advantage and
146    # do the appropriate RIP checks.
147    br label("doPopStackStuffAndCheckRIP"), flags=(CEZF,)
148
149    # Here, we know we're -not- in 64 bit mode, so we should do the
150    # appropriate/other RIP checks.
151    # if temp_RIP > CS.limit throw #GP(0)
152    rdlimit t6, cs
153    subi t0, t1, t6, flags=(ECF,)
154    fault "new GeneralProtection(0)", flags=(CECF,)
155
156    #(temp_CPL!=CPL)
157    srli t7, t4, 4
158    xor t7, t7, t5
159    andi t0, t7, 0x3, flags=(EZF,)
160    br label("doPopStackStuff"), flags=(nCEZF,)
161    # We can modify user visible state here because we're know
162    # we're done with things that can fault.
163    addi rsp, rsp, "3 * env.stackSize"
164    br label("fallThroughPopStackStuff")
165
166doPopStackStuffAndCheckRIP:
167    # Check if the RIP is canonical.
168    sra t7, t1, 47, flags=(EZF,), dataSize=ssz
169    # if t7 isn't 0 or -1, it wasn't canonical.
170    br label("doPopStackStuff"), flags=(CEZF,)
171    addi t0, t7, 1, flags=(EZF,), dataSize=ssz
172    fault "new GeneralProtection(0)", flags=(nCEZF,)
173
174doPopStackStuff:
175    #    POP.v temp_RSP
176    ld t6, ss, [1, t0, rsp], "3 * env.dataSize", dataSize=ssz
177    #    POP.v temp_SS
178    ld t2, ss, [1, t0, rsp], "4 * env.dataSize", dataSize=ssz
179    #    SS = READ_DESCRIPTOR (temp_SS, ss_chk)
180    andi t0, t2, 0xFC, flags=(EZF,), dataSize=2
181    br label("processSSDescriptor"), flags=(CEZF,)
182    andi t7, t2, 0xF8, dataSize=8
183    andi t0, t2, 0x4, flags=(EZF,), dataSize=2
184    br label("globalSSDescriptor"), flags=(CEZF,)
185    ld t7, tsl, [1, t0, t7], dataSize=8
186    br label("processSSDescriptor")
187globalSSDescriptor:
188    ld t7, tsg, [1, t0, t7], dataSize=8
189processSSDescriptor:
190    chks t2, t7, dataSize=8
191
192    # This actually updates state which is wrong. It should wait until we know
193    # we're not going to fault. Unfortunately, that's hard to do.
194    wrdl ss, t7, t2
195    wrsel ss, t2
196
197###
198### From this point downwards, we can't fault. We can update user visible state.
199###
200    #    RSP.s = temp_RSP
201    mov rsp, rsp, t6, dataSize=ssz
202
203    #}
204
205fallThroughPopStackStuff:
206
207    #IF (changing CPL)
208    #{
209    srli t7, t4, 4
210    xor t7, t7, t5
211    andi t0, t7, 0x3, flags=(EZF,)
212    br label("skipSegmentSquashing"), flags=(CEZF,)
213
214    # The attribute register needs to keep track of more info before this will
215    # work the way it needs to.
216    #    FOR (seg = ES, DS, FS, GS)
217    #        IF ((seg.attr.dpl < cpl && ((seg.attr.type = 'data')
218    #            || (seg.attr.type = 'non-conforming-code')))
219    #        {
220    #            seg = NULL
221    #        }
222    #}
223
224skipSegmentSquashing:
225
226    # Ignore this for now.
227    #RFLAGS.v = temp_RFLAGS
228    wrflags t0, t3
229    #  VIF,VIP,IOPL only changed if (old_CPL = 0)
230    #  IF only changed if (old_CPL <= old_RFLAGS.IOPL)
231    #  VM unchanged
232    #  RF cleared
233
234    #RIP = temp_RIP
235    wrip t0, t1, dataSize=ssz
236};
237
238def macroop IRET_VIRT {
239    panic "Virtual mode iret isn't implemented!"
240};
241'''
242#let {{
243#    class INT(Inst):
244#       "GenFault ${new UnimpInstFault}"
245#    class INTO(Inst):
246#       "GenFault ${new UnimpInstFault}"
247#}};
248