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/>.
21 #include "exec/helper-proto.h"
25 #if defined(CONFIG_USER_ONLY)
27 void do_smm_enter(X86CPU
*cpu
)
31 void helper_rsm(CPUX86State
*env
)
38 #define SMM_REVISION_ID 0x00020064
40 #define SMM_REVISION_ID 0x00020000
43 void do_smm_enter(X86CPU
*cpu
)
45 CPUX86State
*env
= &cpu
->env
;
46 CPUState
*cs
= CPU(cpu
);
47 target_ulong sm_state
;
51 qemu_log_mask(CPU_LOG_INT
, "SMM: enter\n");
52 log_cpu_state_mask(CPU_LOG_INT
, CPU(cpu
), CPU_DUMP_CCOP
);
54 env
->hflags
|= HF_SMM_MASK
;
57 sm_state
= env
->smbase
+ 0x8000;
60 for (i
= 0; i
< 6; i
++) {
62 offset
= 0x7e00 + i
* 16;
63 stw_phys(cs
->as
, sm_state
+ offset
, dt
->selector
);
64 stw_phys(cs
->as
, sm_state
+ offset
+ 2, (dt
->flags
>> 8) & 0xf0ff);
65 stl_phys(cs
->as
, sm_state
+ offset
+ 4, dt
->limit
);
66 stq_phys(cs
->as
, sm_state
+ offset
+ 8, dt
->base
);
69 stq_phys(cs
->as
, sm_state
+ 0x7e68, env
->gdt
.base
);
70 stl_phys(cs
->as
, sm_state
+ 0x7e64, env
->gdt
.limit
);
72 stw_phys(cs
->as
, sm_state
+ 0x7e70, env
->ldt
.selector
);
73 stq_phys(cs
->as
, sm_state
+ 0x7e78, env
->ldt
.base
);
74 stl_phys(cs
->as
, sm_state
+ 0x7e74, env
->ldt
.limit
);
75 stw_phys(cs
->as
, sm_state
+ 0x7e72, (env
->ldt
.flags
>> 8) & 0xf0ff);
77 stq_phys(cs
->as
, sm_state
+ 0x7e88, env
->idt
.base
);
78 stl_phys(cs
->as
, sm_state
+ 0x7e84, env
->idt
.limit
);
80 stw_phys(cs
->as
, sm_state
+ 0x7e90, env
->tr
.selector
);
81 stq_phys(cs
->as
, sm_state
+ 0x7e98, env
->tr
.base
);
82 stl_phys(cs
->as
, sm_state
+ 0x7e94, env
->tr
.limit
);
83 stw_phys(cs
->as
, sm_state
+ 0x7e92, (env
->tr
.flags
>> 8) & 0xf0ff);
85 stq_phys(cs
->as
, sm_state
+ 0x7ed0, env
->efer
);
87 stq_phys(cs
->as
, sm_state
+ 0x7ff8, env
->regs
[R_EAX
]);
88 stq_phys(cs
->as
, sm_state
+ 0x7ff0, env
->regs
[R_ECX
]);
89 stq_phys(cs
->as
, sm_state
+ 0x7fe8, env
->regs
[R_EDX
]);
90 stq_phys(cs
->as
, sm_state
+ 0x7fe0, env
->regs
[R_EBX
]);
91 stq_phys(cs
->as
, sm_state
+ 0x7fd8, env
->regs
[R_ESP
]);
92 stq_phys(cs
->as
, sm_state
+ 0x7fd0, env
->regs
[R_EBP
]);
93 stq_phys(cs
->as
, sm_state
+ 0x7fc8, env
->regs
[R_ESI
]);
94 stq_phys(cs
->as
, sm_state
+ 0x7fc0, env
->regs
[R_EDI
]);
95 for (i
= 8; i
< 16; i
++) {
96 stq_phys(cs
->as
, sm_state
+ 0x7ff8 - i
* 8, env
->regs
[i
]);
98 stq_phys(cs
->as
, sm_state
+ 0x7f78, env
->eip
);
99 stl_phys(cs
->as
, sm_state
+ 0x7f70, cpu_compute_eflags(env
));
100 stl_phys(cs
->as
, sm_state
+ 0x7f68, env
->dr
[6]);
101 stl_phys(cs
->as
, sm_state
+ 0x7f60, env
->dr
[7]);
103 stl_phys(cs
->as
, sm_state
+ 0x7f48, env
->cr
[4]);
104 stl_phys(cs
->as
, sm_state
+ 0x7f50, env
->cr
[3]);
105 stl_phys(cs
->as
, sm_state
+ 0x7f58, env
->cr
[0]);
107 stl_phys(cs
->as
, sm_state
+ 0x7efc, SMM_REVISION_ID
);
108 stl_phys(cs
->as
, sm_state
+ 0x7f00, env
->smbase
);
110 stl_phys(cs
->as
, sm_state
+ 0x7ffc, env
->cr
[0]);
111 stl_phys(cs
->as
, sm_state
+ 0x7ff8, env
->cr
[3]);
112 stl_phys(cs
->as
, sm_state
+ 0x7ff4, cpu_compute_eflags(env
));
113 stl_phys(cs
->as
, sm_state
+ 0x7ff0, env
->eip
);
114 stl_phys(cs
->as
, sm_state
+ 0x7fec, env
->regs
[R_EDI
]);
115 stl_phys(cs
->as
, sm_state
+ 0x7fe8, env
->regs
[R_ESI
]);
116 stl_phys(cs
->as
, sm_state
+ 0x7fe4, env
->regs
[R_EBP
]);
117 stl_phys(cs
->as
, sm_state
+ 0x7fe0, env
->regs
[R_ESP
]);
118 stl_phys(cs
->as
, sm_state
+ 0x7fdc, env
->regs
[R_EBX
]);
119 stl_phys(cs
->as
, sm_state
+ 0x7fd8, env
->regs
[R_EDX
]);
120 stl_phys(cs
->as
, sm_state
+ 0x7fd4, env
->regs
[R_ECX
]);
121 stl_phys(cs
->as
, sm_state
+ 0x7fd0, env
->regs
[R_EAX
]);
122 stl_phys(cs
->as
, sm_state
+ 0x7fcc, env
->dr
[6]);
123 stl_phys(cs
->as
, sm_state
+ 0x7fc8, env
->dr
[7]);
125 stl_phys(cs
->as
, sm_state
+ 0x7fc4, env
->tr
.selector
);
126 stl_phys(cs
->as
, sm_state
+ 0x7f64, env
->tr
.base
);
127 stl_phys(cs
->as
, sm_state
+ 0x7f60, env
->tr
.limit
);
128 stl_phys(cs
->as
, sm_state
+ 0x7f5c, (env
->tr
.flags
>> 8) & 0xf0ff);
130 stl_phys(cs
->as
, sm_state
+ 0x7fc0, env
->ldt
.selector
);
131 stl_phys(cs
->as
, sm_state
+ 0x7f80, env
->ldt
.base
);
132 stl_phys(cs
->as
, sm_state
+ 0x7f7c, env
->ldt
.limit
);
133 stl_phys(cs
->as
, sm_state
+ 0x7f78, (env
->ldt
.flags
>> 8) & 0xf0ff);
135 stl_phys(cs
->as
, sm_state
+ 0x7f74, env
->gdt
.base
);
136 stl_phys(cs
->as
, sm_state
+ 0x7f70, env
->gdt
.limit
);
138 stl_phys(cs
->as
, sm_state
+ 0x7f58, env
->idt
.base
);
139 stl_phys(cs
->as
, sm_state
+ 0x7f54, env
->idt
.limit
);
141 for (i
= 0; i
< 6; i
++) {
144 offset
= 0x7f84 + i
* 12;
146 offset
= 0x7f2c + (i
- 3) * 12;
148 stl_phys(cs
->as
, sm_state
+ 0x7fa8 + i
* 4, dt
->selector
);
149 stl_phys(cs
->as
, sm_state
+ offset
+ 8, dt
->base
);
150 stl_phys(cs
->as
, sm_state
+ offset
+ 4, dt
->limit
);
151 stl_phys(cs
->as
, sm_state
+ offset
, (dt
->flags
>> 8) & 0xf0ff);
153 stl_phys(cs
->as
, sm_state
+ 0x7f14, env
->cr
[4]);
155 stl_phys(cs
->as
, sm_state
+ 0x7efc, SMM_REVISION_ID
);
156 stl_phys(cs
->as
, sm_state
+ 0x7ef8, env
->smbase
);
158 /* init SMM cpu state */
161 cpu_load_efer(env
, 0);
163 cpu_load_eflags(env
, 0, ~(CC_O
| CC_S
| CC_Z
| CC_A
| CC_P
| CC_C
|
165 env
->eip
= 0x00008000;
166 cpu_x86_update_cr0(env
,
167 env
->cr
[0] & ~(CR0_PE_MASK
| CR0_EM_MASK
| CR0_TS_MASK
|
169 cpu_x86_update_cr4(env
, 0);
170 env
->dr
[7] = 0x00000400;
172 cpu_x86_load_seg_cache(env
, R_CS
, (env
->smbase
>> 4) & 0xffff, env
->smbase
,
174 DESC_P_MASK
| DESC_S_MASK
| DESC_W_MASK
|
176 cpu_x86_load_seg_cache(env
, R_DS
, 0, 0, 0xffffffff,
177 DESC_P_MASK
| DESC_S_MASK
| DESC_W_MASK
|
179 cpu_x86_load_seg_cache(env
, R_ES
, 0, 0, 0xffffffff,
180 DESC_P_MASK
| DESC_S_MASK
| DESC_W_MASK
|
182 cpu_x86_load_seg_cache(env
, R_SS
, 0, 0, 0xffffffff,
183 DESC_P_MASK
| DESC_S_MASK
| DESC_W_MASK
|
185 cpu_x86_load_seg_cache(env
, R_FS
, 0, 0, 0xffffffff,
186 DESC_P_MASK
| DESC_S_MASK
| DESC_W_MASK
|
188 cpu_x86_load_seg_cache(env
, R_GS
, 0, 0, 0xffffffff,
189 DESC_P_MASK
| DESC_S_MASK
| DESC_W_MASK
|
193 void helper_rsm(CPUX86State
*env
)
195 X86CPU
*cpu
= x86_env_get_cpu(env
);
196 CPUState
*cs
= CPU(cpu
);
197 target_ulong sm_state
;
201 sm_state
= env
->smbase
+ 0x8000;
203 cpu_load_efer(env
, ldq_phys(cs
->as
, sm_state
+ 0x7ed0));
205 env
->gdt
.base
= ldq_phys(cs
->as
, sm_state
+ 0x7e68);
206 env
->gdt
.limit
= ldl_phys(cs
->as
, sm_state
+ 0x7e64);
208 env
->ldt
.selector
= lduw_phys(cs
->as
, sm_state
+ 0x7e70);
209 env
->ldt
.base
= ldq_phys(cs
->as
, sm_state
+ 0x7e78);
210 env
->ldt
.limit
= ldl_phys(cs
->as
, sm_state
+ 0x7e74);
211 env
->ldt
.flags
= (lduw_phys(cs
->as
, sm_state
+ 0x7e72) & 0xf0ff) << 8;
213 env
->idt
.base
= ldq_phys(cs
->as
, sm_state
+ 0x7e88);
214 env
->idt
.limit
= ldl_phys(cs
->as
, sm_state
+ 0x7e84);
216 env
->tr
.selector
= lduw_phys(cs
->as
, sm_state
+ 0x7e90);
217 env
->tr
.base
= ldq_phys(cs
->as
, sm_state
+ 0x7e98);
218 env
->tr
.limit
= ldl_phys(cs
->as
, sm_state
+ 0x7e94);
219 env
->tr
.flags
= (lduw_phys(cs
->as
, sm_state
+ 0x7e92) & 0xf0ff) << 8;
221 env
->regs
[R_EAX
] = ldq_phys(cs
->as
, sm_state
+ 0x7ff8);
222 env
->regs
[R_ECX
] = ldq_phys(cs
->as
, sm_state
+ 0x7ff0);
223 env
->regs
[R_EDX
] = ldq_phys(cs
->as
, sm_state
+ 0x7fe8);
224 env
->regs
[R_EBX
] = ldq_phys(cs
->as
, sm_state
+ 0x7fe0);
225 env
->regs
[R_ESP
] = ldq_phys(cs
->as
, sm_state
+ 0x7fd8);
226 env
->regs
[R_EBP
] = ldq_phys(cs
->as
, sm_state
+ 0x7fd0);
227 env
->regs
[R_ESI
] = ldq_phys(cs
->as
, sm_state
+ 0x7fc8);
228 env
->regs
[R_EDI
] = ldq_phys(cs
->as
, sm_state
+ 0x7fc0);
229 for (i
= 8; i
< 16; i
++) {
230 env
->regs
[i
] = ldq_phys(cs
->as
, sm_state
+ 0x7ff8 - i
* 8);
232 env
->eip
= ldq_phys(cs
->as
, sm_state
+ 0x7f78);
233 cpu_load_eflags(env
, ldl_phys(cs
->as
, sm_state
+ 0x7f70),
234 ~(CC_O
| CC_S
| CC_Z
| CC_A
| CC_P
| CC_C
| DF_MASK
));
235 env
->dr
[6] = ldl_phys(cs
->as
, sm_state
+ 0x7f68);
236 env
->dr
[7] = ldl_phys(cs
->as
, sm_state
+ 0x7f60);
238 cpu_x86_update_cr4(env
, ldl_phys(cs
->as
, sm_state
+ 0x7f48));
239 cpu_x86_update_cr3(env
, ldl_phys(cs
->as
, sm_state
+ 0x7f50));
240 cpu_x86_update_cr0(env
, ldl_phys(cs
->as
, sm_state
+ 0x7f58));
242 for (i
= 0; i
< 6; i
++) {
243 offset
= 0x7e00 + i
* 16;
244 cpu_x86_load_seg_cache(env
, i
,
245 lduw_phys(cs
->as
, sm_state
+ offset
),
246 ldq_phys(cs
->as
, sm_state
+ offset
+ 8),
247 ldl_phys(cs
->as
, sm_state
+ offset
+ 4),
248 (lduw_phys(cs
->as
, sm_state
+ offset
+ 2) &
252 val
= ldl_phys(cs
->as
, sm_state
+ 0x7efc); /* revision ID */
254 env
->smbase
= ldl_phys(cs
->as
, sm_state
+ 0x7f00) & ~0x7fff;
257 cpu_x86_update_cr0(env
, ldl_phys(cs
->as
, sm_state
+ 0x7ffc));
258 cpu_x86_update_cr3(env
, ldl_phys(cs
->as
, sm_state
+ 0x7ff8));
259 cpu_load_eflags(env
, ldl_phys(cs
->as
, sm_state
+ 0x7ff4),
260 ~(CC_O
| CC_S
| CC_Z
| CC_A
| CC_P
| CC_C
| DF_MASK
));
261 env
->eip
= ldl_phys(cs
->as
, sm_state
+ 0x7ff0);
262 env
->regs
[R_EDI
] = ldl_phys(cs
->as
, sm_state
+ 0x7fec);
263 env
->regs
[R_ESI
] = ldl_phys(cs
->as
, sm_state
+ 0x7fe8);
264 env
->regs
[R_EBP
] = ldl_phys(cs
->as
, sm_state
+ 0x7fe4);
265 env
->regs
[R_ESP
] = ldl_phys(cs
->as
, sm_state
+ 0x7fe0);
266 env
->regs
[R_EBX
] = ldl_phys(cs
->as
, sm_state
+ 0x7fdc);
267 env
->regs
[R_EDX
] = ldl_phys(cs
->as
, sm_state
+ 0x7fd8);
268 env
->regs
[R_ECX
] = ldl_phys(cs
->as
, sm_state
+ 0x7fd4);
269 env
->regs
[R_EAX
] = ldl_phys(cs
->as
, sm_state
+ 0x7fd0);
270 env
->dr
[6] = ldl_phys(cs
->as
, sm_state
+ 0x7fcc);
271 env
->dr
[7] = ldl_phys(cs
->as
, sm_state
+ 0x7fc8);
273 env
->tr
.selector
= ldl_phys(cs
->as
, sm_state
+ 0x7fc4) & 0xffff;
274 env
->tr
.base
= ldl_phys(cs
->as
, sm_state
+ 0x7f64);
275 env
->tr
.limit
= ldl_phys(cs
->as
, sm_state
+ 0x7f60);
276 env
->tr
.flags
= (ldl_phys(cs
->as
, sm_state
+ 0x7f5c) & 0xf0ff) << 8;
278 env
->ldt
.selector
= ldl_phys(cs
->as
, sm_state
+ 0x7fc0) & 0xffff;
279 env
->ldt
.base
= ldl_phys(cs
->as
, sm_state
+ 0x7f80);
280 env
->ldt
.limit
= ldl_phys(cs
->as
, sm_state
+ 0x7f7c);
281 env
->ldt
.flags
= (ldl_phys(cs
->as
, sm_state
+ 0x7f78) & 0xf0ff) << 8;
283 env
->gdt
.base
= ldl_phys(cs
->as
, sm_state
+ 0x7f74);
284 env
->gdt
.limit
= ldl_phys(cs
->as
, sm_state
+ 0x7f70);
286 env
->idt
.base
= ldl_phys(cs
->as
, sm_state
+ 0x7f58);
287 env
->idt
.limit
= ldl_phys(cs
->as
, sm_state
+ 0x7f54);
289 for (i
= 0; i
< 6; i
++) {
291 offset
= 0x7f84 + i
* 12;
293 offset
= 0x7f2c + (i
- 3) * 12;
295 cpu_x86_load_seg_cache(env
, i
,
297 sm_state
+ 0x7fa8 + i
* 4) & 0xffff,
298 ldl_phys(cs
->as
, sm_state
+ offset
+ 8),
299 ldl_phys(cs
->as
, sm_state
+ offset
+ 4),
301 sm_state
+ offset
) & 0xf0ff) << 8);
303 cpu_x86_update_cr4(env
, ldl_phys(cs
->as
, sm_state
+ 0x7f14));
305 val
= ldl_phys(cs
->as
, sm_state
+ 0x7efc); /* revision ID */
307 env
->smbase
= ldl_phys(cs
->as
, sm_state
+ 0x7ef8) & ~0x7fff;
310 env
->hflags
&= ~HF_SMM_MASK
;
313 qemu_log_mask(CPU_LOG_INT
, "SMM: after RSM\n");
314 log_cpu_state_mask(CPU_LOG_INT
, CPU(cpu
), CPU_DUMP_CCOP
);
317 #endif /* !CONFIG_USER_ONLY */