system_calls.py revision 6222:9ee4a06a960b
1# Copyright (c) 2007 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 SYSCALL_64
58{
59    # All 1s.
60    limm t1, "(uint64_t)(-1)", dataSize=8
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", dataSize=8
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)  | (0  << 2)  | (0 << 3)   | \
81              (1 << 4)  | (0  << 5)  | (1 << 6)   | \
82              (1 << 7)  | (10 << 8)  | (0 << 12)  | \
83              (1 << 13) | (0  << 14) | (1 << 15)), dataSize=8
84    wrattr cs, t4
85
86    # Set up SS.
87    addi t3, t3, 8
88    wrsel ss, t3
89    wrbase ss, t0, dataSize=8
90    wrlimit ss, t1, dataSize=4
91    # Writable, readable, not expandDown,
92    # dpl=0, defaultSize=0, not long mode
93    limm t4, ((0 << 0)  | (0  << 2)  | (1 << 3)   | \
94              (0 << 4)  | (0  << 5)  | (1 << 6)   | \
95              (1 << 7)  | (2  << 8)  | (1 << 12)  | \
96              (1 << 13) | (0  << 14) | (1 << 15)), dataSize=8
97    wrattr ss, t4
98
99    # Set the new rip.
100    rdval t7, lstar
101    wrip t0, t7
102
103    # Mask the flags against sf_mask and leave RF turned off.
104    rdval t3, sf_mask, dataSize=8
105    xor t3, t3, t1, dataSize=8
106    and t3, t3, r11, dataSize=8
107    wrflags t3, t0
108};
109
110def macroop SYSCALL_COMPAT
111{
112    # All 1s.
113    limm t1, "(uint64_t)(-1)", dataSize=8
114
115    # Save the next RIP.
116    rdip rcx
117
118    # Stick rflags with RF masked into r11.
119    rflags t2
120    limm t3, "~RFBit", dataSize=8
121    andi r11, t2, t3, dataSize=8
122
123    rdval t3, star
124    srli t3, t3, 32, dataSize=8
125    andi t3, t3, 0xFC, dataSize=1
126
127    # Set up CS.
128    wrsel cs, t3
129    wrbase cs, t0, dataSize=8
130    wrlimit cs, t1, dataSize=4
131    # Not writable, read/execute-able, not expandDown,
132    # dpl=0, defaultSize=0, long mode
133    limm t4, ((0 << 0)  | (0  << 2)  | (0 << 3)   | \
134              (1 << 4)  | (0  << 5)  | (1 << 6)   | \
135              (1 << 7)  | (10 << 8)  | (0 << 12)  | \
136              (1 << 13) | (0  << 14) | (1 << 15)), dataSize=8
137    wrattr cs, t4
138
139    # Set up SS.
140    addi t3, t3, 8
141    wrsel ss, t3
142    wrbase ss, t0, dataSize=8
143    wrlimit ss, t1, dataSize=4
144    # Writable, readable, not expandDown,
145    # dpl=0, defaultSize=0, not long mode
146    limm t4, ((0 << 0)  | (0  << 2)  | (1 << 3)   | \
147              (0 << 4)  | (0  << 5)  | (1 << 6)   | \
148              (1 << 7)  | (2  << 8)  | (1 << 12)  | \
149              (1 << 13) | (0  << 14) | (1 << 15)), dataSize=8
150    wrattr ss, t4
151
152    # Set the new rip.
153    rdval t7, cstar
154    wrip t0, t7
155
156    # Mask the flags against sf_mask and leave RF turned off.
157    rdval t3, sf_mask, dataSize=8
158    xor t3, t3, t1, dataSize=8
159    and t3, t3, r11, dataSize=8
160    wrflags t3, t0
161};
162
163def macroop SYSCALL_LEGACY
164{
165    panic "The syscall instruction isn't implemented in legacy mode."
166};
167
168def macroop SYSRET_TO_64
169{
170    # All 1s.
171    limm t1, "(uint64_t)(-1)", dataSize=8
172
173    rdval t3, star
174    srli t3, t3, 48, dataSize=8
175    ori t3, t3, 3, dataSize=1
176
177    # Set rflags to r11 with RF and VM cleared.
178    limm t4, "~(RFBit | VMBit)", dataSize=8
179    and t4, t4, r11, dataSize=8
180    wrflags t4, t0
181
182    # Set up CS.
183    addi t4, t3, 16, dataSize=8
184    wrsel cs, t4
185    wrbase cs, t0, dataSize=8
186    wrlimit cs, t1, dataSize=4
187    # Not writable, read/execute-able, not expandDown,
188    # dpl=3, defaultSize=0, long mode
189    limm t4, ((3 << 0)  | (0  << 2)  | (0 << 3)   | \
190              (1 << 4)  | (0  << 5)  | (1 << 6)   | \
191              (1 << 7)  | (10 << 8)  | (0 << 12)  | \
192              (1 << 13) | (0  << 14) | (1 << 15)), dataSize=8
193    wrattr cs, t4
194
195    # Only the selector is changed for SS.
196    addi t4, t3, 8, dataSize=8
197    wrsel ss, t4
198
199    # Set the RIP back.
200    wrip rcx, t0, dataSize=8
201};
202
203def macroop SYSRET_TO_COMPAT
204{
205    # All 1s.
206    limm t1, "(uint64_t)(-1)", dataSize=8
207
208    rdval t3, star
209    srli t3, t3, 48, dataSize=8
210    ori t3, t3, 3, dataSize=1
211
212    # Set rflags to r11 with RF and VM cleared.
213    limm t4, "~(RFBit | VMBit)", dataSize=8
214    and t4, t4, r11, dataSize=8
215    wrflags t4, t0
216
217    # Set up CS.
218    wrsel cs, t3
219    wrbase cs, t0, dataSize=8
220    wrlimit cs, t1, dataSize=4
221    # Not writable, read/execute-able, not expandDown,
222    # dpl=3, defaultSize=1, not long mode
223    limm t4, ((3 << 0)  | (0  << 2)  | (0 << 3)   | \
224              (1 << 4)  | (0  << 5)  | (1 << 6)   | \
225              (1 << 7)  | (10 << 8)  | (0 << 12)  | \
226              (1 << 13) | (0  << 14) | (1 << 15)), dataSize=8
227    wrattr cs, t4
228
229    # Only the selector is changed for SS.
230    addi t4, t3, 8, dataSize=8
231    wrsel ss, t4
232
233    # Set the RIP back.
234    wrip rcx, t0, dataSize=8
235};
236
237def macroop SYSRET_NON_64
238{
239    panic "The sysret instruction isn't implemented in legacy mode."
240};
241'''
242#let {{
243#    class SYSENTER(Inst):
244#       "GenFault ${new UnimpInstFault}"
245#    class SYSEXIT(Inst):
246#       "GenFault ${new UnimpInstFault}"
247#}};
248