system_calls.py revision 5908
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)"
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