system_calls.py revision 6062
111660Stushar@ece.gatech.edu# Copyright (c) 2007 The Hewlett-Packard Development Company
211660Stushar@ece.gatech.edu# All rights reserved.
311660Stushar@ece.gatech.edu#
411660Stushar@ece.gatech.edu# Redistribution and use of this software in source and binary forms,
511660Stushar@ece.gatech.edu# with or without modification, are permitted provided that the
611660Stushar@ece.gatech.edu# following conditions are met:
711660Stushar@ece.gatech.edu#
811660Stushar@ece.gatech.edu# The software must be used only for Non-Commercial Use which means any
911660Stushar@ece.gatech.edu# use which is NOT directed to receiving any direct monetary
1011660Stushar@ece.gatech.edu# compensation for, or commercial advantage from such use.  Illustrative
1111660Stushar@ece.gatech.edu# examples of non-commercial use are academic research, personal study,
1211660Stushar@ece.gatech.edu# teaching, education and corporate research & development.
1311660Stushar@ece.gatech.edu# Illustrative examples of commercial use are distributing products for
1411660Stushar@ece.gatech.edu# commercial advantage and providing services using the software for
1511660Stushar@ece.gatech.edu# commercial advantage.
1611660Stushar@ece.gatech.edu#
1711660Stushar@ece.gatech.edu# If you wish to use this software or functionality therein that may be
1811660Stushar@ece.gatech.edu# covered by patents for commercial use, please contact:
1911660Stushar@ece.gatech.edu#     Director of Intellectual Property Licensing
2011660Stushar@ece.gatech.edu#     Office of Strategy and Technology
2111660Stushar@ece.gatech.edu#     Hewlett-Packard Company
2211660Stushar@ece.gatech.edu#     1501 Page Mill Road
2311660Stushar@ece.gatech.edu#     Palo Alto, California  94304
2411660Stushar@ece.gatech.edu#
2511660Stushar@ece.gatech.edu# Redistributions of source code must retain the above copyright notice,
2611660Stushar@ece.gatech.edu# this list of conditions and the following disclaimer.  Redistributions
2711660Stushar@ece.gatech.edu# in binary form must reproduce the above copyright notice, this list of
2811660Stushar@ece.gatech.edu# conditions and the following disclaimer in the documentation and/or
2911660Stushar@ece.gatech.edu# other materials provided with the distribution.  Neither the name of
3011660Stushar@ece.gatech.edu# the COPYRIGHT HOLDER(s), HEWLETT-PACKARD COMPANY, nor the names of its
3111660Stushar@ece.gatech.edu# contributors may be used to endorse or promote products derived from
3211660Stushar@ece.gatech.edu# this software without specific prior written permission.  No right of
3311660Stushar@ece.gatech.edu# sublicense is granted herewith.  Derivatives of the software and
3411660Stushar@ece.gatech.edu# output created using the software may be prepared, but only for
3512065Snikos.nikoleris@arm.com# Non-Commercial Uses.  Derivatives of the software may be shared with
3611660Stushar@ece.gatech.edu# others provided: (i) the others agree to abide by the list of
3711660Stushar@ece.gatech.edu# conditions herein which includes the Non-Commercial Use restrictions;
3811660Stushar@ece.gatech.edu# and (ii) such Derivatives of the software include the above copyright
3911660Stushar@ece.gatech.edu# notice to acknowledge the contribution from this software where
4011660Stushar@ece.gatech.edu# applicable, this list of conditions and the disclaimer below.
4111660Stushar@ece.gatech.edu#
4211660Stushar@ece.gatech.edu# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4311660Stushar@ece.gatech.edu# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
4411660Stushar@ece.gatech.edu# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
4511660Stushar@ece.gatech.edu# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
4611660Stushar@ece.gatech.edu# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
4711660Stushar@ece.gatech.edu# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
4811660Stushar@ece.gatech.edu# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
4911660Stushar@ece.gatech.edu# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
5011660Stushar@ece.gatech.edu# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
5111660Stushar@ece.gatech.edu# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
5211660Stushar@ece.gatech.edu# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5311660Stushar@ece.gatech.edu#
5411660Stushar@ece.gatech.edu# Authors: Gabe Black
5511660Stushar@ece.gatech.edu
5611660Stushar@ece.gatech.edumicrocode = '''
5711660Stushar@ece.gatech.edudef macroop SYSCALL_64
5811660Stushar@ece.gatech.edu{
5911660Stushar@ece.gatech.edu    # All 1s.
6011660Stushar@ece.gatech.edu    limm t1, "(uint64_t)(-1)", dataSize=8
6111660Stushar@ece.gatech.edu
6211660Stushar@ece.gatech.edu    # Save the next RIP.
6311660Stushar@ece.gatech.edu    rdip rcx
6411660Stushar@ece.gatech.edu
6511660Stushar@ece.gatech.edu    # Stick rflags with RF masked into r11.
6611660Stushar@ece.gatech.edu    rflags t2
6711660Stushar@ece.gatech.edu    limm t3, "~RFBit", dataSize=8
6811660Stushar@ece.gatech.edu    andi r11, t2, t3, dataSize=8
6911660Stushar@ece.gatech.edu
7011660Stushar@ece.gatech.edu    rdval t3, star
7111660Stushar@ece.gatech.edu    srli t3, t3, 32, dataSize=8
7211660Stushar@ece.gatech.edu    andi t3, t3, 0xFC, dataSize=1
7311660Stushar@ece.gatech.edu
7411660Stushar@ece.gatech.edu    # Set up CS.
7511660Stushar@ece.gatech.edu    wrsel cs, t3
7611660Stushar@ece.gatech.edu    wrbase cs, t0, dataSize=8
7711660Stushar@ece.gatech.edu    wrlimit cs, t1, dataSize=4
7811660Stushar@ece.gatech.edu    # Not writable, read/execute-able, not expandDown,
7911660Stushar@ece.gatech.edu    # dpl=0, defaultSize=0, long mode
8011660Stushar@ece.gatech.edu    limm t4, ((0 << 0) | (1 << 1) | (0 << 2) | \
8111660Stushar@ece.gatech.edu              (0 << 3) | (0 << 5) | (1 << 6)), dataSize=8
8211660Stushar@ece.gatech.edu    wrattr cs, t4
8311660Stushar@ece.gatech.edu
8411660Stushar@ece.gatech.edu    # Set up SS.
8511660Stushar@ece.gatech.edu    addi t3, t3, 8
8611660Stushar@ece.gatech.edu    wrsel ss, t3
8711660Stushar@ece.gatech.edu    wrbase ss, t0, dataSize=8
8811660Stushar@ece.gatech.edu    wrlimit ss, t1, dataSize=4
8911660Stushar@ece.gatech.edu    # Writable, readable, not expandDown,
9011660Stushar@ece.gatech.edu    # dpl=0, defaultSize=0, not long mode
9111660Stushar@ece.gatech.edu    limm t4, ((1 << 0) | (1 << 1) | (0 << 2) | \
9211660Stushar@ece.gatech.edu              (0 << 3) | (0 << 5) | (0 << 6)), dataSize=8
9311660Stushar@ece.gatech.edu    wrattr ss, t4
9411660Stushar@ece.gatech.edu
9511660Stushar@ece.gatech.edu    # Set the new rip.
9611660Stushar@ece.gatech.edu    rdval t7, lstar
9711660Stushar@ece.gatech.edu    wrip t0, t7
9811660Stushar@ece.gatech.edu
9911660Stushar@ece.gatech.edu    # Mask the flags against sf_mask and leave RF turned off.
10011660Stushar@ece.gatech.edu    rdval t3, sf_mask, dataSize=8
10111660Stushar@ece.gatech.edu    xor t3, t3, t1, dataSize=8
10211660Stushar@ece.gatech.edu    and t3, t3, r11, dataSize=8
10312065Snikos.nikoleris@arm.com    wrflags t3, t0
10412065Snikos.nikoleris@arm.com};
10512065Snikos.nikoleris@arm.com
10611660Stushar@ece.gatech.edudef macroop SYSCALL_COMPAT
10711660Stushar@ece.gatech.edu{
10811660Stushar@ece.gatech.edu    # All 1s.
10911660Stushar@ece.gatech.edu    limm t1, "(uint64_t)(-1)", dataSize=8
11011660Stushar@ece.gatech.edu
11111660Stushar@ece.gatech.edu    # Save the next RIP.
11211660Stushar@ece.gatech.edu    rdip rcx
11311660Stushar@ece.gatech.edu
11411660Stushar@ece.gatech.edu    # Stick rflags with RF masked into r11.
11511660Stushar@ece.gatech.edu    rflags t2
116    limm t3, "~RFBit", dataSize=8
117    andi r11, t2, t3, dataSize=8
118
119    rdval t3, star
120    srli t3, t3, 32, dataSize=8
121    andi t3, t3, 0xFC, dataSize=1
122
123    # Set up CS.
124    wrsel cs, t3
125    wrbase cs, t0, dataSize=8
126    wrlimit cs, t1, dataSize=4
127    # Not writable, read/execute-able, not expandDown,
128    # dpl=0, defaultSize=0, long mode
129    limm t4, ((0 << 0) | (1 << 1) | (0 << 2) | \
130              (0 << 3) | (0 << 5) | (1 << 6)), dataSize=8
131    wrattr cs, t4
132
133    # Set up SS.
134    addi t3, t3, 8
135    wrsel ss, t3
136    wrbase ss, t0, dataSize=8
137    wrlimit ss, t1, dataSize=4
138    # Writable, readable, not expandDown,
139    # dpl=0, defaultSize=0, not long mode
140    limm t4, ((1 << 0) | (1 << 1) | (0 << 2) | \
141              (0 << 3) | (0 << 5) | (0 << 6)), dataSize=8
142    wrattr ss, t4
143
144    # Set the new rip.
145    rdval t7, cstar
146    wrip t0, t7
147
148    # Mask the flags against sf_mask and leave RF turned off.
149    rdval t3, sf_mask, dataSize=8
150    xor t3, t3, t1, dataSize=8
151    and t3, t3, r11, dataSize=8
152    wrflags t3, t0
153};
154
155def macroop SYSCALL_LEGACY
156{
157    panic "The syscall instruction isn't implemented in legacy mode."
158};
159
160def macroop SYSRET_TO_64
161{
162    # All 1s.
163    limm t1, "(uint64_t)(-1)", dataSize=8
164
165    rdval t3, star
166    srli t3, t3, 48, dataSize=8
167    ori t3, t3, 3, dataSize=1
168
169    # Set rflags to r11 with RF and VM cleared.
170    limm t4, "~(RFBit | VMBit)", dataSize=8
171    and t4, t4, r11, dataSize=8
172    wrflags t4, t0
173
174    # Set up CS.
175    addi t4, t3, 16, dataSize=8
176    wrsel cs, t4
177    wrbase cs, t0, dataSize=8
178    wrlimit cs, t1, dataSize=4
179    # Not writable, read/execute-able, not expandDown,
180    # dpl=3, defaultSize=0, long mode
181    limm t4, ((0 << 0) | (1 << 1) | (0 << 2) | \
182              (3 << 3) | (0 << 5) | (1 << 6)), dataSize=8
183    wrattr cs, t4
184
185    # Only the selector is changed for SS.
186    addi t4, t3, 8, dataSize=8
187    wrsel ss, t4
188
189    # Set the RIP back.
190    wrip rcx, t0, dataSize=8
191};
192
193def macroop SYSRET_TO_COMPAT
194{
195    # All 1s.
196    limm t1, "(uint64_t)(-1)", dataSize=8
197
198    rdval t3, star
199    srli t3, t3, 48, dataSize=8
200    ori t3, t3, 3, dataSize=1
201
202    # Set rflags to r11 with RF and VM cleared.
203    limm t4, "~(RFBit | VMBit)", dataSize=8
204    and t4, t4, r11, dataSize=8
205    wrflags t4, t0
206
207    # Set up CS.
208    wrsel cs, t3
209    wrbase cs, t0, dataSize=8
210    wrlimit cs, t1, dataSize=4
211    # Not writable, read/execute-able, not expandDown,
212    # dpl=3, defaultSize=1, not long mode
213    limm t4, ((0 << 0) | (1 << 1) | (0 << 2) | \
214              (3 << 3) | (1 << 5) | (0 << 6)), dataSize=8
215    wrattr cs, t4
216
217    # Only the selector is changed for SS.
218    addi t4, t3, 8, dataSize=8
219    wrsel ss, t4
220
221    # Set the RIP back.
222    wrip rcx, t0, dataSize=8
223};
224
225def macroop SYSRET_NON_64
226{
227    panic "The sysret instruction isn't implemented in legacy mode."
228};
229'''
230#let {{
231#    class SYSENTER(Inst):
232#       "GenFault ${new UnimpInstFault}"
233#    class SYSEXIT(Inst):
234#       "GenFault ${new UnimpInstFault}"
235#}};
236