system_calls.py revision 5908:c24a1ffc4ad0
112855Sgabeblack@google.com# Copyright (c) 2007 The Hewlett-Packard Development Company
212855Sgabeblack@google.com# All rights reserved.
312855Sgabeblack@google.com#
412855Sgabeblack@google.com# Redistribution and use of this software in source and binary forms,
512855Sgabeblack@google.com# with or without modification, are permitted provided that the
612855Sgabeblack@google.com# following conditions are met:
712855Sgabeblack@google.com#
812855Sgabeblack@google.com# The software must be used only for Non-Commercial Use which means any
912855Sgabeblack@google.com# use which is NOT directed to receiving any direct monetary
1012855Sgabeblack@google.com# compensation for, or commercial advantage from such use.  Illustrative
1112855Sgabeblack@google.com# examples of non-commercial use are academic research, personal study,
1212855Sgabeblack@google.com# teaching, education and corporate research & development.
1312855Sgabeblack@google.com# Illustrative examples of commercial use are distributing products for
1412855Sgabeblack@google.com# commercial advantage and providing services using the software for
1512855Sgabeblack@google.com# commercial advantage.
1612855Sgabeblack@google.com#
1712855Sgabeblack@google.com# If you wish to use this software or functionality therein that may be
1812855Sgabeblack@google.com# covered by patents for commercial use, please contact:
1912855Sgabeblack@google.com#     Director of Intellectual Property Licensing
2012855Sgabeblack@google.com#     Office of Strategy and Technology
2112855Sgabeblack@google.com#     Hewlett-Packard Company
2212855Sgabeblack@google.com#     1501 Page Mill Road
2312855Sgabeblack@google.com#     Palo Alto, California  94304
2412855Sgabeblack@google.com#
2512855Sgabeblack@google.com# Redistributions of source code must retain the above copyright notice,
2612855Sgabeblack@google.com# this list of conditions and the following disclaimer.  Redistributions
2712855Sgabeblack@google.com# in binary form must reproduce the above copyright notice, this list of
2812855Sgabeblack@google.com# conditions and the following disclaimer in the documentation and/or
2912855Sgabeblack@google.com# other materials provided with the distribution.  Neither the name of
3012855Sgabeblack@google.com# the COPYRIGHT HOLDER(s), HEWLETT-PACKARD COMPANY, nor the names of its
3112855Sgabeblack@google.com# contributors may be used to endorse or promote products derived from
3212855Sgabeblack@google.com# this software without specific prior written permission.  No right of
3312855Sgabeblack@google.com# sublicense is granted herewith.  Derivatives of the software and
3412855Sgabeblack@google.com# output created using the software may be prepared, but only for
3512855Sgabeblack@google.com# Non-Commercial Uses.  Derivatives of the software may be shared with
3612855Sgabeblack@google.com# others provided: (i) the others agree to abide by the list of
3712855Sgabeblack@google.com# conditions herein which includes the Non-Commercial Use restrictions;
3812855Sgabeblack@google.com# and (ii) such Derivatives of the software include the above copyright
3912855Sgabeblack@google.com# notice to acknowledge the contribution from this software where
4012855Sgabeblack@google.com# applicable, this list of conditions and the disclaimer below.
4112855Sgabeblack@google.com#
4212855Sgabeblack@google.com# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4312855Sgabeblack@google.com# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
4412855Sgabeblack@google.com# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
4512855Sgabeblack@google.com# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
4612855Sgabeblack@google.com# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
4712855Sgabeblack@google.com# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
4812855Sgabeblack@google.com# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
4912855Sgabeblack@google.com# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
5012855Sgabeblack@google.com# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
5112855Sgabeblack@google.com# (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 SYSCALL_64
58{
59    # All 1s.
60    limm t1, "(uint64_t)(-1)"
61
62    # Save the next RIP.
63    rdip rcx
64
65    # Stick rflags with RF masked into r11.
66    rflags t2
67    limm t3, "~RFBit"
68    andi r11, t2, t3, dataSize=8
69
70    rdval t3, star
71    srli t3, t3, 32, dataSize=8
72    andi t3, t3, 0xFC, dataSize=1
73
74    # Set up CS.
75    wrsel cs, t3
76    wrbase cs, t0, dataSize=8
77    wrlimit cs, t1, dataSize=4
78    # Not writable, read/execute-able, not expandDown,
79    # dpl=0, defaultSize=0, long mode
80    limm t4, ((0 << 0) | (1 << 1) | (0 << 2) | \
81              (0 << 3) | (0 << 5) | (1 << 6))
82    wrattr cs, t4
83
84    # Set up SS.
85    addi t3, t3, 8
86    wrsel ss, t3
87    wrbase ss, t0, dataSize=8
88    wrlimit ss, t1, dataSize=4
89    # Writable, readable, not expandDown,
90    # dpl=0, defaultSize=0, not long mode
91    limm t4, ((1 << 0) | (1 << 1) | (0 << 2) | \
92              (0 << 3) | (0 << 5) | (0 << 6))
93    wrattr ss, t4
94
95    # Set the new rip.
96    rdval t7, lstar
97    wrip t0, t7
98
99    # Mask the flags against sf_mask and leave RF turned off.
100    rdval t3, sf_mask, dataSize=8
101    xor t3, t3, t1, dataSize=8
102    and t3, t3, r11, dataSize=8
103    wrflags t3, t0
104};
105
106def macroop SYSCALL_COMPAT
107{
108    # All 1s.
109    limm t1, "(uint64_t)(-1)"
110
111    # Save the next RIP.
112    rdip rcx
113
114    # Stick rflags with RF masked into r11.
115    rflags t2
116    limm t3, "~RFBit"
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))
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))
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)"
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)"
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))
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)"
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)"
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))
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