4 * Copyright (c) 2003 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 #include "qemu/osdep.h"
21 #include "qemu/main-loop.h"
23 #include "exec/helper-proto.h"
28 #if defined(CONFIG_USER_ONLY)
30 void do_smm_enter(X86CPU
*cpu
)
34 void helper_rsm(CPUX86State
*env
)
41 #define SMM_REVISION_ID 0x00020064
43 #define SMM_REVISION_ID 0x00020000
46 void do_smm_enter(X86CPU
*cpu
)
48 CPUX86State
*env
= &cpu
->env
;
49 CPUState
*cs
= CPU(cpu
);
50 target_ulong sm_state
;
54 qemu_log_mask(CPU_LOG_INT
, "SMM: enter\n");
55 log_cpu_state_mask(CPU_LOG_INT
, CPU(cpu
), CPU_DUMP_CCOP
);
57 env
->hflags
|= HF_SMM_MASK
;
58 if (env
->hflags2
& HF2_NMI_MASK
) {
59 env
->hflags2
|= HF2_SMM_INSIDE_NMI_MASK
;
61 env
->hflags2
|= HF2_NMI_MASK
;
64 sm_state
= env
->smbase
+ 0x8000;
67 for (i
= 0; i
< 6; i
++) {
69 offset
= 0x7e00 + i
* 16;
70 x86_stw_phys(cs
, sm_state
+ offset
, dt
->selector
);
71 x86_stw_phys(cs
, sm_state
+ offset
+ 2, (dt
->flags
>> 8) & 0xf0ff);
72 x86_stl_phys(cs
, sm_state
+ offset
+ 4, dt
->limit
);
73 x86_stq_phys(cs
, sm_state
+ offset
+ 8, dt
->base
);
76 x86_stq_phys(cs
, sm_state
+ 0x7e68, env
->gdt
.base
);
77 x86_stl_phys(cs
, sm_state
+ 0x7e64, env
->gdt
.limit
);
79 x86_stw_phys(cs
, sm_state
+ 0x7e70, env
->ldt
.selector
);
80 x86_stq_phys(cs
, sm_state
+ 0x7e78, env
->ldt
.base
);
81 x86_stl_phys(cs
, sm_state
+ 0x7e74, env
->ldt
.limit
);
82 x86_stw_phys(cs
, sm_state
+ 0x7e72, (env
->ldt
.flags
>> 8) & 0xf0ff);
84 x86_stq_phys(cs
, sm_state
+ 0x7e88, env
->idt
.base
);
85 x86_stl_phys(cs
, sm_state
+ 0x7e84, env
->idt
.limit
);
87 x86_stw_phys(cs
, sm_state
+ 0x7e90, env
->tr
.selector
);
88 x86_stq_phys(cs
, sm_state
+ 0x7e98, env
->tr
.base
);
89 x86_stl_phys(cs
, sm_state
+ 0x7e94, env
->tr
.limit
);
90 x86_stw_phys(cs
, sm_state
+ 0x7e92, (env
->tr
.flags
>> 8) & 0xf0ff);
92 /* ??? Vol 1, 16.5.6 Intel MPX and SMM says that IA32_BNDCFGS
93 is saved at offset 7ED0. Vol 3, 34.4.1.1, Table 32-2, has
94 7EA0-7ED7 as "reserved". What's this, and what's really
95 supposed to happen? */
96 x86_stq_phys(cs
, sm_state
+ 0x7ed0, env
->efer
);
98 x86_stq_phys(cs
, sm_state
+ 0x7ff8, env
->regs
[R_EAX
]);
99 x86_stq_phys(cs
, sm_state
+ 0x7ff0, env
->regs
[R_ECX
]);
100 x86_stq_phys(cs
, sm_state
+ 0x7fe8, env
->regs
[R_EDX
]);
101 x86_stq_phys(cs
, sm_state
+ 0x7fe0, env
->regs
[R_EBX
]);
102 x86_stq_phys(cs
, sm_state
+ 0x7fd8, env
->regs
[R_ESP
]);
103 x86_stq_phys(cs
, sm_state
+ 0x7fd0, env
->regs
[R_EBP
]);
104 x86_stq_phys(cs
, sm_state
+ 0x7fc8, env
->regs
[R_ESI
]);
105 x86_stq_phys(cs
, sm_state
+ 0x7fc0, env
->regs
[R_EDI
]);
106 for (i
= 8; i
< 16; i
++) {
107 x86_stq_phys(cs
, sm_state
+ 0x7ff8 - i
* 8, env
->regs
[i
]);
109 x86_stq_phys(cs
, sm_state
+ 0x7f78, env
->eip
);
110 x86_stl_phys(cs
, sm_state
+ 0x7f70, cpu_compute_eflags(env
));
111 x86_stl_phys(cs
, sm_state
+ 0x7f68, env
->dr
[6]);
112 x86_stl_phys(cs
, sm_state
+ 0x7f60, env
->dr
[7]);
114 x86_stl_phys(cs
, sm_state
+ 0x7f48, env
->cr
[4]);
115 x86_stq_phys(cs
, sm_state
+ 0x7f50, env
->cr
[3]);
116 x86_stl_phys(cs
, sm_state
+ 0x7f58, env
->cr
[0]);
118 x86_stl_phys(cs
, sm_state
+ 0x7efc, SMM_REVISION_ID
);
119 x86_stl_phys(cs
, sm_state
+ 0x7f00, env
->smbase
);
121 x86_stl_phys(cs
, sm_state
+ 0x7ffc, env
->cr
[0]);
122 x86_stl_phys(cs
, sm_state
+ 0x7ff8, env
->cr
[3]);
123 x86_stl_phys(cs
, sm_state
+ 0x7ff4, cpu_compute_eflags(env
));
124 x86_stl_phys(cs
, sm_state
+ 0x7ff0, env
->eip
);
125 x86_stl_phys(cs
, sm_state
+ 0x7fec, env
->regs
[R_EDI
]);
126 x86_stl_phys(cs
, sm_state
+ 0x7fe8, env
->regs
[R_ESI
]);
127 x86_stl_phys(cs
, sm_state
+ 0x7fe4, env
->regs
[R_EBP
]);
128 x86_stl_phys(cs
, sm_state
+ 0x7fe0, env
->regs
[R_ESP
]);
129 x86_stl_phys(cs
, sm_state
+ 0x7fdc, env
->regs
[R_EBX
]);
130 x86_stl_phys(cs
, sm_state
+ 0x7fd8, env
->regs
[R_EDX
]);
131 x86_stl_phys(cs
, sm_state
+ 0x7fd4, env
->regs
[R_ECX
]);
132 x86_stl_phys(cs
, sm_state
+ 0x7fd0, env
->regs
[R_EAX
]);
133 x86_stl_phys(cs
, sm_state
+ 0x7fcc, env
->dr
[6]);
134 x86_stl_phys(cs
, sm_state
+ 0x7fc8, env
->dr
[7]);
136 x86_stl_phys(cs
, sm_state
+ 0x7fc4, env
->tr
.selector
);
137 x86_stl_phys(cs
, sm_state
+ 0x7f64, env
->tr
.base
);
138 x86_stl_phys(cs
, sm_state
+ 0x7f60, env
->tr
.limit
);
139 x86_stl_phys(cs
, sm_state
+ 0x7f5c, (env
->tr
.flags
>> 8) & 0xf0ff);
141 x86_stl_phys(cs
, sm_state
+ 0x7fc0, env
->ldt
.selector
);
142 x86_stl_phys(cs
, sm_state
+ 0x7f80, env
->ldt
.base
);
143 x86_stl_phys(cs
, sm_state
+ 0x7f7c, env
->ldt
.limit
);
144 x86_stl_phys(cs
, sm_state
+ 0x7f78, (env
->ldt
.flags
>> 8) & 0xf0ff);
146 x86_stl_phys(cs
, sm_state
+ 0x7f74, env
->gdt
.base
);
147 x86_stl_phys(cs
, sm_state
+ 0x7f70, env
->gdt
.limit
);
149 x86_stl_phys(cs
, sm_state
+ 0x7f58, env
->idt
.base
);
150 x86_stl_phys(cs
, sm_state
+ 0x7f54, env
->idt
.limit
);
152 for (i
= 0; i
< 6; i
++) {
155 offset
= 0x7f84 + i
* 12;
157 offset
= 0x7f2c + (i
- 3) * 12;
159 x86_stl_phys(cs
, sm_state
+ 0x7fa8 + i
* 4, dt
->selector
);
160 x86_stl_phys(cs
, sm_state
+ offset
+ 8, dt
->base
);
161 x86_stl_phys(cs
, sm_state
+ offset
+ 4, dt
->limit
);
162 x86_stl_phys(cs
, sm_state
+ offset
, (dt
->flags
>> 8) & 0xf0ff);
164 x86_stl_phys(cs
, sm_state
+ 0x7f14, env
->cr
[4]);
166 x86_stl_phys(cs
, sm_state
+ 0x7efc, SMM_REVISION_ID
);
167 x86_stl_phys(cs
, sm_state
+ 0x7ef8, env
->smbase
);
169 /* init SMM cpu state */
172 cpu_load_efer(env
, 0);
174 cpu_load_eflags(env
, 0, ~(CC_O
| CC_S
| CC_Z
| CC_A
| CC_P
| CC_C
|
176 env
->eip
= 0x00008000;
177 cpu_x86_update_cr0(env
,
178 env
->cr
[0] & ~(CR0_PE_MASK
| CR0_EM_MASK
| CR0_TS_MASK
|
180 cpu_x86_update_cr4(env
, 0);
181 env
->dr
[7] = 0x00000400;
183 cpu_x86_load_seg_cache(env
, R_CS
, (env
->smbase
>> 4) & 0xffff, env
->smbase
,
185 DESC_P_MASK
| DESC_S_MASK
| DESC_W_MASK
|
186 DESC_G_MASK
| DESC_A_MASK
);
187 cpu_x86_load_seg_cache(env
, R_DS
, 0, 0, 0xffffffff,
188 DESC_P_MASK
| DESC_S_MASK
| DESC_W_MASK
|
189 DESC_G_MASK
| DESC_A_MASK
);
190 cpu_x86_load_seg_cache(env
, R_ES
, 0, 0, 0xffffffff,
191 DESC_P_MASK
| DESC_S_MASK
| DESC_W_MASK
|
192 DESC_G_MASK
| DESC_A_MASK
);
193 cpu_x86_load_seg_cache(env
, R_SS
, 0, 0, 0xffffffff,
194 DESC_P_MASK
| DESC_S_MASK
| DESC_W_MASK
|
195 DESC_G_MASK
| DESC_A_MASK
);
196 cpu_x86_load_seg_cache(env
, R_FS
, 0, 0, 0xffffffff,
197 DESC_P_MASK
| DESC_S_MASK
| DESC_W_MASK
|
198 DESC_G_MASK
| DESC_A_MASK
);
199 cpu_x86_load_seg_cache(env
, R_GS
, 0, 0, 0xffffffff,
200 DESC_P_MASK
| DESC_S_MASK
| DESC_W_MASK
|
201 DESC_G_MASK
| DESC_A_MASK
);
204 void helper_rsm(CPUX86State
*env
)
206 X86CPU
*cpu
= x86_env_get_cpu(env
);
207 CPUState
*cs
= CPU(cpu
);
208 target_ulong sm_state
;
212 sm_state
= env
->smbase
+ 0x8000;
214 cpu_load_efer(env
, x86_ldq_phys(cs
, sm_state
+ 0x7ed0));
216 env
->gdt
.base
= x86_ldq_phys(cs
, sm_state
+ 0x7e68);
217 env
->gdt
.limit
= x86_ldl_phys(cs
, sm_state
+ 0x7e64);
219 env
->ldt
.selector
= x86_lduw_phys(cs
, sm_state
+ 0x7e70);
220 env
->ldt
.base
= x86_ldq_phys(cs
, sm_state
+ 0x7e78);
221 env
->ldt
.limit
= x86_ldl_phys(cs
, sm_state
+ 0x7e74);
222 env
->ldt
.flags
= (x86_lduw_phys(cs
, sm_state
+ 0x7e72) & 0xf0ff) << 8;
224 env
->idt
.base
= x86_ldq_phys(cs
, sm_state
+ 0x7e88);
225 env
->idt
.limit
= x86_ldl_phys(cs
, sm_state
+ 0x7e84);
227 env
->tr
.selector
= x86_lduw_phys(cs
, sm_state
+ 0x7e90);
228 env
->tr
.base
= x86_ldq_phys(cs
, sm_state
+ 0x7e98);
229 env
->tr
.limit
= x86_ldl_phys(cs
, sm_state
+ 0x7e94);
230 env
->tr
.flags
= (x86_lduw_phys(cs
, sm_state
+ 0x7e92) & 0xf0ff) << 8;
232 env
->regs
[R_EAX
] = x86_ldq_phys(cs
, sm_state
+ 0x7ff8);
233 env
->regs
[R_ECX
] = x86_ldq_phys(cs
, sm_state
+ 0x7ff0);
234 env
->regs
[R_EDX
] = x86_ldq_phys(cs
, sm_state
+ 0x7fe8);
235 env
->regs
[R_EBX
] = x86_ldq_phys(cs
, sm_state
+ 0x7fe0);
236 env
->regs
[R_ESP
] = x86_ldq_phys(cs
, sm_state
+ 0x7fd8);
237 env
->regs
[R_EBP
] = x86_ldq_phys(cs
, sm_state
+ 0x7fd0);
238 env
->regs
[R_ESI
] = x86_ldq_phys(cs
, sm_state
+ 0x7fc8);
239 env
->regs
[R_EDI
] = x86_ldq_phys(cs
, sm_state
+ 0x7fc0);
240 for (i
= 8; i
< 16; i
++) {
241 env
->regs
[i
] = x86_ldq_phys(cs
, sm_state
+ 0x7ff8 - i
* 8);
243 env
->eip
= x86_ldq_phys(cs
, sm_state
+ 0x7f78);
244 cpu_load_eflags(env
, x86_ldl_phys(cs
, sm_state
+ 0x7f70),
245 ~(CC_O
| CC_S
| CC_Z
| CC_A
| CC_P
| CC_C
| DF_MASK
));
246 env
->dr
[6] = x86_ldl_phys(cs
, sm_state
+ 0x7f68);
247 env
->dr
[7] = x86_ldl_phys(cs
, sm_state
+ 0x7f60);
249 cpu_x86_update_cr4(env
, x86_ldl_phys(cs
, sm_state
+ 0x7f48));
250 cpu_x86_update_cr3(env
, x86_ldq_phys(cs
, sm_state
+ 0x7f50));
251 cpu_x86_update_cr0(env
, x86_ldl_phys(cs
, sm_state
+ 0x7f58));
253 for (i
= 0; i
< 6; i
++) {
254 offset
= 0x7e00 + i
* 16;
255 cpu_x86_load_seg_cache(env
, i
,
256 x86_lduw_phys(cs
, sm_state
+ offset
),
257 x86_ldq_phys(cs
, sm_state
+ offset
+ 8),
258 x86_ldl_phys(cs
, sm_state
+ offset
+ 4),
259 (x86_lduw_phys(cs
, sm_state
+ offset
+ 2) &
263 val
= x86_ldl_phys(cs
, sm_state
+ 0x7efc); /* revision ID */
265 env
->smbase
= x86_ldl_phys(cs
, sm_state
+ 0x7f00);
268 cpu_x86_update_cr0(env
, x86_ldl_phys(cs
, sm_state
+ 0x7ffc));
269 cpu_x86_update_cr3(env
, x86_ldl_phys(cs
, sm_state
+ 0x7ff8));
270 cpu_load_eflags(env
, x86_ldl_phys(cs
, sm_state
+ 0x7ff4),
271 ~(CC_O
| CC_S
| CC_Z
| CC_A
| CC_P
| CC_C
| DF_MASK
));
272 env
->eip
= x86_ldl_phys(cs
, sm_state
+ 0x7ff0);
273 env
->regs
[R_EDI
] = x86_ldl_phys(cs
, sm_state
+ 0x7fec);
274 env
->regs
[R_ESI
] = x86_ldl_phys(cs
, sm_state
+ 0x7fe8);
275 env
->regs
[R_EBP
] = x86_ldl_phys(cs
, sm_state
+ 0x7fe4);
276 env
->regs
[R_ESP
] = x86_ldl_phys(cs
, sm_state
+ 0x7fe0);
277 env
->regs
[R_EBX
] = x86_ldl_phys(cs
, sm_state
+ 0x7fdc);
278 env
->regs
[R_EDX
] = x86_ldl_phys(cs
, sm_state
+ 0x7fd8);
279 env
->regs
[R_ECX
] = x86_ldl_phys(cs
, sm_state
+ 0x7fd4);
280 env
->regs
[R_EAX
] = x86_ldl_phys(cs
, sm_state
+ 0x7fd0);
281 env
->dr
[6] = x86_ldl_phys(cs
, sm_state
+ 0x7fcc);
282 env
->dr
[7] = x86_ldl_phys(cs
, sm_state
+ 0x7fc8);
284 env
->tr
.selector
= x86_ldl_phys(cs
, sm_state
+ 0x7fc4) & 0xffff;
285 env
->tr
.base
= x86_ldl_phys(cs
, sm_state
+ 0x7f64);
286 env
->tr
.limit
= x86_ldl_phys(cs
, sm_state
+ 0x7f60);
287 env
->tr
.flags
= (x86_ldl_phys(cs
, sm_state
+ 0x7f5c) & 0xf0ff) << 8;
289 env
->ldt
.selector
= x86_ldl_phys(cs
, sm_state
+ 0x7fc0) & 0xffff;
290 env
->ldt
.base
= x86_ldl_phys(cs
, sm_state
+ 0x7f80);
291 env
->ldt
.limit
= x86_ldl_phys(cs
, sm_state
+ 0x7f7c);
292 env
->ldt
.flags
= (x86_ldl_phys(cs
, sm_state
+ 0x7f78) & 0xf0ff) << 8;
294 env
->gdt
.base
= x86_ldl_phys(cs
, sm_state
+ 0x7f74);
295 env
->gdt
.limit
= x86_ldl_phys(cs
, sm_state
+ 0x7f70);
297 env
->idt
.base
= x86_ldl_phys(cs
, sm_state
+ 0x7f58);
298 env
->idt
.limit
= x86_ldl_phys(cs
, sm_state
+ 0x7f54);
300 for (i
= 0; i
< 6; i
++) {
302 offset
= 0x7f84 + i
* 12;
304 offset
= 0x7f2c + (i
- 3) * 12;
306 cpu_x86_load_seg_cache(env
, i
,
308 sm_state
+ 0x7fa8 + i
* 4) & 0xffff,
309 x86_ldl_phys(cs
, sm_state
+ offset
+ 8),
310 x86_ldl_phys(cs
, sm_state
+ offset
+ 4),
312 sm_state
+ offset
) & 0xf0ff) << 8);
314 cpu_x86_update_cr4(env
, x86_ldl_phys(cs
, sm_state
+ 0x7f14));
316 val
= x86_ldl_phys(cs
, sm_state
+ 0x7efc); /* revision ID */
318 env
->smbase
= x86_ldl_phys(cs
, sm_state
+ 0x7ef8);
321 if ((env
->hflags2
& HF2_SMM_INSIDE_NMI_MASK
) == 0) {
322 env
->hflags2
&= ~HF2_NMI_MASK
;
324 env
->hflags2
&= ~HF2_SMM_INSIDE_NMI_MASK
;
325 env
->hflags
&= ~HF_SMM_MASK
;
327 qemu_log_mask(CPU_LOG_INT
, "SMM: after RSM\n");
328 log_cpu_state_mask(CPU_LOG_INT
, CPU(cpu
), CPU_DUMP_CCOP
);
331 #endif /* !CONFIG_USER_ONLY */