1# Copyright (c) 2007 The Hewlett-Packard Development Company
2# All rights reserved.
3#
4# The license below extends only to copyright in the software and shall
5# not be construed as granting a license to any other intellectual
6# property including but not limited to intellectual property relating
7# to a hardware implementation of the functionality of the software
8# licensed hereunder.  You may use the software subject to the license
9# terms below provided that you ensure that this notice is replicated
10# unmodified and in its entirety in all distributions of the software,
11# modified or unmodified, in source code or in binary form.
12#
13# Redistribution and use in source and binary forms, with or without
14# modification, are permitted provided that the following conditions are
15# met: redistributions of source code must retain the above copyright
16# notice, this list of conditions and the following disclaimer;
17# redistributions in binary form must reproduce the above copyright
18# notice, this list of conditions and the following disclaimer in the
19# documentation and/or other materials provided with the distribution;
20# neither the name of the copyright holders nor the names of its
21# contributors may be used to endorse or promote products derived from
22# this software without specific prior written permission.
23#
24# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35#
36# Authors: Gabe Black
37
38microcode = '''
39def macroop SYSCALL_64
40{
41    # All 1s.
42    limm t1, "(uint64_t)(-1)", dataSize=8
43
44    # Save the next RIP.
45    rdip rcx
46
47    # Stick rflags with RF masked into r11.
48    rflags t2
49    limm t3, "~RFBit", dataSize=8
50    and r11, t2, t3, dataSize=8
51
52    rdval t3, star
53    srli t3, t3, 32, dataSize=8
54    andi t3, t3, 0xFC, dataSize=1
55
56    # Set up CS.
57    wrsel cs, t3
58    wrbase cs, t0, dataSize=8
59    wrlimit cs, t1, dataSize=4
60    # Not writable, read/execute-able, not expandDown,
61    # dpl=0, defaultSize=0, long mode
62    limm t4, ((0 << 0)  | (0  << 2)  | (0 << 3)   | \
63              (1 << 4)  | (0  << 5)  | (1 << 6)   | \
64              (1 << 7)  | (10 << 8)  | (0 << 12)  | \
65              (1 << 13) | (0  << 14) | (1 << 15)), dataSize=8
66    wrattr cs, t4
67
68    # Set up SS.
69    addi t3, t3, 8
70    wrsel ss, t3
71    wrbase ss, t0, dataSize=8
72    wrlimit ss, t1, dataSize=4
73    # Writable, readable, not expandDown,
74    # dpl=0, defaultSize=0, not long mode
75    limm t4, ((0 << 0)  | (0  << 2)  | (1 << 3)   | \
76              (0 << 4)  | (0  << 5)  | (1 << 6)   | \
77              (1 << 7)  | (2  << 8)  | (1 << 12)  | \
78              (1 << 13) | (0  << 14) | (1 << 15)), dataSize=8
79    wrattr ss, t4
80
81    # Set the new rip.
82    rdval t7, lstar, dataSize=8
83    wrip t0, t7, dataSize=8
84
85    # Mask the flags against sf_mask and leave RF turned off.
86    rdval t3, sf_mask, dataSize=8
87    xor t3, t3, t1, dataSize=8
88    and t3, t3, r11, dataSize=8
89    wrflags t3, t0
90};
91
92def macroop SYSCALL_COMPAT
93{
94    # All 1s.
95    limm t1, "(uint64_t)(-1)", dataSize=8
96
97    # Save the next RIP.
98    rdip rcx
99
100    # Stick rflags with RF masked into r11.
101    rflags t2
102    limm t3, "~RFBit", dataSize=8
103    and r11, t2, t3, dataSize=8
104
105    rdval t3, star
106    srli t3, t3, 32, dataSize=8
107    andi t3, t3, 0xFC, dataSize=1
108
109    # Set up CS.
110    wrsel cs, t3
111    wrbase cs, t0, dataSize=8
112    wrlimit cs, t1, dataSize=4
113    # Not writable, read/execute-able, not expandDown,
114    # dpl=0, defaultSize=0, long mode
115    limm t4, ((0 << 0)  | (0  << 2)  | (0 << 3)   | \
116              (1 << 4)  | (0  << 5)  | (1 << 6)   | \
117              (1 << 7)  | (10 << 8)  | (0 << 12)  | \
118              (1 << 13) | (0  << 14) | (1 << 15)), dataSize=8
119    wrattr cs, t4
120
121    # Set up SS.
122    addi t3, t3, 8
123    wrsel ss, t3
124    wrbase ss, t0, dataSize=8
125    wrlimit ss, t1, dataSize=4
126    # Writable, readable, not expandDown,
127    # dpl=0, defaultSize=0, not long mode
128    limm t4, ((0 << 0)  | (0  << 2)  | (1 << 3)   | \
129              (0 << 4)  | (0  << 5)  | (1 << 6)   | \
130              (1 << 7)  | (2  << 8)  | (1 << 12)  | \
131              (1 << 13) | (0  << 14) | (1 << 15)), dataSize=8
132    wrattr ss, t4
133
134    # Set the new rip.
135    rdval t7, cstar
136    wrip t0, t7
137
138    # Mask the flags against sf_mask and leave RF turned off.
139    rdval t3, sf_mask, dataSize=8
140    xor t3, t3, t1, dataSize=8
141    and t3, t3, r11, dataSize=8
142    wrflags t3, t0
143};
144
145def macroop SYSCALL_LEGACY
146{
147    panic "The syscall instruction isn't implemented in legacy mode."
148};
149
150def macroop SYSRET_TO_64
151{
152    # All 1s.
153    limm t1, "(uint64_t)(-1)", dataSize=8
154
155    rdval t3, star
156    srli t3, t3, 48, dataSize=8
157    ori t3, t3, 3, dataSize=1
158
159    # Set rflags to r11 with RF and VM cleared.
160    limm t4, "~(RFBit | VMBit)", dataSize=8
161    and t4, t4, r11, dataSize=8
162    wrflags t4, t0
163
164    # Set up CS.
165    addi t4, t3, 16, dataSize=8
166    wrsel cs, t4
167    wrbase cs, t0, dataSize=8
168    wrlimit cs, t1, dataSize=4
169    # Not writable, read/execute-able, not expandDown,
170    # dpl=3, defaultSize=0, long mode
171    limm t4, ((3 << 0)  | (0  << 2)  | (0 << 3)   | \
172              (1 << 4)  | (0  << 5)  | (1 << 6)   | \
173              (1 << 7)  | (10 << 8)  | (0 << 12)  | \
174              (1 << 13) | (0  << 14) | (1 << 15)), dataSize=8
175    wrattr cs, t4
176
177    # Only the selector is changed for SS.
178    addi t4, t3, 8, dataSize=8
179    wrsel ss, t4
180
181    # Set the RIP back.
182    wrip rcx, t0, dataSize=8
183};
184
185def macroop SYSRET_TO_COMPAT
186{
187    # All 1s.
188    limm t1, "(uint64_t)(-1)", dataSize=8
189
190    rdval t3, star
191    srli t3, t3, 48, dataSize=8
192    ori t3, t3, 3, dataSize=1
193
194    # Set rflags to r11 with RF and VM cleared.
195    limm t4, "~(RFBit | VMBit)", dataSize=8
196    and t4, t4, r11, dataSize=8
197    wrflags t4, t0
198
199    # Set up CS.
200    wrsel cs, t3
201    wrbase cs, t0, dataSize=8
202    wrlimit cs, t1, dataSize=4
203    # Not writable, read/execute-able, not expandDown,
204    # dpl=3, defaultSize=1, not long mode
205    limm t4, ((3 << 0)  | (0  << 2)  | (0 << 3)   | \
206              (1 << 4)  | (0  << 5)  | (1 << 6)   | \
207              (1 << 7)  | (10 << 8)  | (0 << 12)  | \
208              (1 << 13) | (0  << 14) | (1 << 15)), dataSize=8
209    wrattr cs, t4
210
211    # Only the selector is changed for SS.
212    addi t4, t3, 8, dataSize=8
213    wrsel ss, t4
214
215    # Set the RIP back.
216    wrip rcx, t0, dataSize=8
217};
218
219def macroop SYSRET_NON_64
220{
221    panic "The sysret instruction isn't implemented in legacy mode."
222};
223'''
224#let {{
225#    class SYSENTER(Inst):
226#       "GenFault ${new UnimpInstFault}"
227#    class SYSEXIT(Inst):
228#       "GenFault ${new UnimpInstFault}"
229#}};
230