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/>.
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 target_ulong sm_state
;
50 qemu_log_mask(CPU_LOG_INT
, "SMM: enter\n");
51 log_cpu_state_mask(CPU_LOG_INT
, CPU(cpu
), CPU_DUMP_CCOP
);
53 env
->hflags
|= HF_SMM_MASK
;
56 sm_state
= env
->smbase
+ 0x8000;
59 for (i
= 0; i
< 6; i
++) {
61 offset
= 0x7e00 + i
* 16;
62 stw_phys(sm_state
+ offset
, dt
->selector
);
63 stw_phys(sm_state
+ offset
+ 2, (dt
->flags
>> 8) & 0xf0ff);
64 stl_phys(sm_state
+ offset
+ 4, dt
->limit
);
65 stq_phys(sm_state
+ offset
+ 8, dt
->base
);
68 stq_phys(sm_state
+ 0x7e68, env
->gdt
.base
);
69 stl_phys(sm_state
+ 0x7e64, env
->gdt
.limit
);
71 stw_phys(sm_state
+ 0x7e70, env
->ldt
.selector
);
72 stq_phys(sm_state
+ 0x7e78, env
->ldt
.base
);
73 stl_phys(sm_state
+ 0x7e74, env
->ldt
.limit
);
74 stw_phys(sm_state
+ 0x7e72, (env
->ldt
.flags
>> 8) & 0xf0ff);
76 stq_phys(sm_state
+ 0x7e88, env
->idt
.base
);
77 stl_phys(sm_state
+ 0x7e84, env
->idt
.limit
);
79 stw_phys(sm_state
+ 0x7e90, env
->tr
.selector
);
80 stq_phys(sm_state
+ 0x7e98, env
->tr
.base
);
81 stl_phys(sm_state
+ 0x7e94, env
->tr
.limit
);
82 stw_phys(sm_state
+ 0x7e92, (env
->tr
.flags
>> 8) & 0xf0ff);
84 stq_phys(sm_state
+ 0x7ed0, env
->efer
);
86 stq_phys(sm_state
+ 0x7ff8, env
->regs
[R_EAX
]);
87 stq_phys(sm_state
+ 0x7ff0, env
->regs
[R_ECX
]);
88 stq_phys(sm_state
+ 0x7fe8, env
->regs
[R_EDX
]);
89 stq_phys(sm_state
+ 0x7fe0, env
->regs
[R_EBX
]);
90 stq_phys(sm_state
+ 0x7fd8, env
->regs
[R_ESP
]);
91 stq_phys(sm_state
+ 0x7fd0, env
->regs
[R_EBP
]);
92 stq_phys(sm_state
+ 0x7fc8, env
->regs
[R_ESI
]);
93 stq_phys(sm_state
+ 0x7fc0, env
->regs
[R_EDI
]);
94 for (i
= 8; i
< 16; i
++) {
95 stq_phys(sm_state
+ 0x7ff8 - i
* 8, env
->regs
[i
]);
97 stq_phys(sm_state
+ 0x7f78, env
->eip
);
98 stl_phys(sm_state
+ 0x7f70, cpu_compute_eflags(env
));
99 stl_phys(sm_state
+ 0x7f68, env
->dr
[6]);
100 stl_phys(sm_state
+ 0x7f60, env
->dr
[7]);
102 stl_phys(sm_state
+ 0x7f48, env
->cr
[4]);
103 stl_phys(sm_state
+ 0x7f50, env
->cr
[3]);
104 stl_phys(sm_state
+ 0x7f58, env
->cr
[0]);
106 stl_phys(sm_state
+ 0x7efc, SMM_REVISION_ID
);
107 stl_phys(sm_state
+ 0x7f00, env
->smbase
);
109 stl_phys(sm_state
+ 0x7ffc, env
->cr
[0]);
110 stl_phys(sm_state
+ 0x7ff8, env
->cr
[3]);
111 stl_phys(sm_state
+ 0x7ff4, cpu_compute_eflags(env
));
112 stl_phys(sm_state
+ 0x7ff0, env
->eip
);
113 stl_phys(sm_state
+ 0x7fec, env
->regs
[R_EDI
]);
114 stl_phys(sm_state
+ 0x7fe8, env
->regs
[R_ESI
]);
115 stl_phys(sm_state
+ 0x7fe4, env
->regs
[R_EBP
]);
116 stl_phys(sm_state
+ 0x7fe0, env
->regs
[R_ESP
]);
117 stl_phys(sm_state
+ 0x7fdc, env
->regs
[R_EBX
]);
118 stl_phys(sm_state
+ 0x7fd8, env
->regs
[R_EDX
]);
119 stl_phys(sm_state
+ 0x7fd4, env
->regs
[R_ECX
]);
120 stl_phys(sm_state
+ 0x7fd0, env
->regs
[R_EAX
]);
121 stl_phys(sm_state
+ 0x7fcc, env
->dr
[6]);
122 stl_phys(sm_state
+ 0x7fc8, env
->dr
[7]);
124 stl_phys(sm_state
+ 0x7fc4, env
->tr
.selector
);
125 stl_phys(sm_state
+ 0x7f64, env
->tr
.base
);
126 stl_phys(sm_state
+ 0x7f60, env
->tr
.limit
);
127 stl_phys(sm_state
+ 0x7f5c, (env
->tr
.flags
>> 8) & 0xf0ff);
129 stl_phys(sm_state
+ 0x7fc0, env
->ldt
.selector
);
130 stl_phys(sm_state
+ 0x7f80, env
->ldt
.base
);
131 stl_phys(sm_state
+ 0x7f7c, env
->ldt
.limit
);
132 stl_phys(sm_state
+ 0x7f78, (env
->ldt
.flags
>> 8) & 0xf0ff);
134 stl_phys(sm_state
+ 0x7f74, env
->gdt
.base
);
135 stl_phys(sm_state
+ 0x7f70, env
->gdt
.limit
);
137 stl_phys(sm_state
+ 0x7f58, env
->idt
.base
);
138 stl_phys(sm_state
+ 0x7f54, env
->idt
.limit
);
140 for (i
= 0; i
< 6; i
++) {
143 offset
= 0x7f84 + i
* 12;
145 offset
= 0x7f2c + (i
- 3) * 12;
147 stl_phys(sm_state
+ 0x7fa8 + i
* 4, dt
->selector
);
148 stl_phys(sm_state
+ offset
+ 8, dt
->base
);
149 stl_phys(sm_state
+ offset
+ 4, dt
->limit
);
150 stl_phys(sm_state
+ offset
, (dt
->flags
>> 8) & 0xf0ff);
152 stl_phys(sm_state
+ 0x7f14, env
->cr
[4]);
154 stl_phys(sm_state
+ 0x7efc, SMM_REVISION_ID
);
155 stl_phys(sm_state
+ 0x7ef8, env
->smbase
);
157 /* init SMM cpu state */
160 cpu_load_efer(env
, 0);
162 cpu_load_eflags(env
, 0, ~(CC_O
| CC_S
| CC_Z
| CC_A
| CC_P
| CC_C
|
164 env
->eip
= 0x00008000;
165 cpu_x86_load_seg_cache(env
, R_CS
, (env
->smbase
>> 4) & 0xffff, env
->smbase
,
167 cpu_x86_load_seg_cache(env
, R_DS
, 0, 0, 0xffffffff, 0);
168 cpu_x86_load_seg_cache(env
, R_ES
, 0, 0, 0xffffffff, 0);
169 cpu_x86_load_seg_cache(env
, R_SS
, 0, 0, 0xffffffff, 0);
170 cpu_x86_load_seg_cache(env
, R_FS
, 0, 0, 0xffffffff, 0);
171 cpu_x86_load_seg_cache(env
, R_GS
, 0, 0, 0xffffffff, 0);
173 cpu_x86_update_cr0(env
,
174 env
->cr
[0] & ~(CR0_PE_MASK
| CR0_EM_MASK
| CR0_TS_MASK
|
176 cpu_x86_update_cr4(env
, 0);
177 env
->dr
[7] = 0x00000400;
178 CC_OP
= CC_OP_EFLAGS
;
181 void helper_rsm(CPUX86State
*env
)
183 X86CPU
*cpu
= x86_env_get_cpu(env
);
184 target_ulong sm_state
;
188 sm_state
= env
->smbase
+ 0x8000;
190 cpu_load_efer(env
, ldq_phys(sm_state
+ 0x7ed0));
192 for (i
= 0; i
< 6; i
++) {
193 offset
= 0x7e00 + i
* 16;
194 cpu_x86_load_seg_cache(env
, i
,
195 lduw_phys(sm_state
+ offset
),
196 ldq_phys(sm_state
+ offset
+ 8),
197 ldl_phys(sm_state
+ offset
+ 4),
198 (lduw_phys(sm_state
+ offset
+ 2) &
202 env
->gdt
.base
= ldq_phys(sm_state
+ 0x7e68);
203 env
->gdt
.limit
= ldl_phys(sm_state
+ 0x7e64);
205 env
->ldt
.selector
= lduw_phys(sm_state
+ 0x7e70);
206 env
->ldt
.base
= ldq_phys(sm_state
+ 0x7e78);
207 env
->ldt
.limit
= ldl_phys(sm_state
+ 0x7e74);
208 env
->ldt
.flags
= (lduw_phys(sm_state
+ 0x7e72) & 0xf0ff) << 8;
210 env
->idt
.base
= ldq_phys(sm_state
+ 0x7e88);
211 env
->idt
.limit
= ldl_phys(sm_state
+ 0x7e84);
213 env
->tr
.selector
= lduw_phys(sm_state
+ 0x7e90);
214 env
->tr
.base
= ldq_phys(sm_state
+ 0x7e98);
215 env
->tr
.limit
= ldl_phys(sm_state
+ 0x7e94);
216 env
->tr
.flags
= (lduw_phys(sm_state
+ 0x7e92) & 0xf0ff) << 8;
218 env
->regs
[R_EAX
] = ldq_phys(sm_state
+ 0x7ff8);
219 env
->regs
[R_ECX
] = ldq_phys(sm_state
+ 0x7ff0);
220 env
->regs
[R_EDX
] = ldq_phys(sm_state
+ 0x7fe8);
221 env
->regs
[R_EBX
] = ldq_phys(sm_state
+ 0x7fe0);
222 env
->regs
[R_ESP
] = ldq_phys(sm_state
+ 0x7fd8);
223 env
->regs
[R_EBP
] = ldq_phys(sm_state
+ 0x7fd0);
224 env
->regs
[R_ESI
] = ldq_phys(sm_state
+ 0x7fc8);
225 env
->regs
[R_EDI
] = ldq_phys(sm_state
+ 0x7fc0);
226 for (i
= 8; i
< 16; i
++) {
227 env
->regs
[i
] = ldq_phys(sm_state
+ 0x7ff8 - i
* 8);
229 env
->eip
= ldq_phys(sm_state
+ 0x7f78);
230 cpu_load_eflags(env
, ldl_phys(sm_state
+ 0x7f70),
231 ~(CC_O
| CC_S
| CC_Z
| CC_A
| CC_P
| CC_C
| DF_MASK
));
232 env
->dr
[6] = ldl_phys(sm_state
+ 0x7f68);
233 env
->dr
[7] = ldl_phys(sm_state
+ 0x7f60);
235 cpu_x86_update_cr4(env
, ldl_phys(sm_state
+ 0x7f48));
236 cpu_x86_update_cr3(env
, ldl_phys(sm_state
+ 0x7f50));
237 cpu_x86_update_cr0(env
, ldl_phys(sm_state
+ 0x7f58));
239 val
= ldl_phys(sm_state
+ 0x7efc); /* revision ID */
241 env
->smbase
= ldl_phys(sm_state
+ 0x7f00) & ~0x7fff;
244 cpu_x86_update_cr0(env
, ldl_phys(sm_state
+ 0x7ffc));
245 cpu_x86_update_cr3(env
, ldl_phys(sm_state
+ 0x7ff8));
246 cpu_load_eflags(env
, ldl_phys(sm_state
+ 0x7ff4),
247 ~(CC_O
| CC_S
| CC_Z
| CC_A
| CC_P
| CC_C
| DF_MASK
));
248 env
->eip
= ldl_phys(sm_state
+ 0x7ff0);
249 env
->regs
[R_EDI
] = ldl_phys(sm_state
+ 0x7fec);
250 env
->regs
[R_ESI
] = ldl_phys(sm_state
+ 0x7fe8);
251 env
->regs
[R_EBP
] = ldl_phys(sm_state
+ 0x7fe4);
252 env
->regs
[R_ESP
] = ldl_phys(sm_state
+ 0x7fe0);
253 env
->regs
[R_EBX
] = ldl_phys(sm_state
+ 0x7fdc);
254 env
->regs
[R_EDX
] = ldl_phys(sm_state
+ 0x7fd8);
255 env
->regs
[R_ECX
] = ldl_phys(sm_state
+ 0x7fd4);
256 env
->regs
[R_EAX
] = ldl_phys(sm_state
+ 0x7fd0);
257 env
->dr
[6] = ldl_phys(sm_state
+ 0x7fcc);
258 env
->dr
[7] = ldl_phys(sm_state
+ 0x7fc8);
260 env
->tr
.selector
= ldl_phys(sm_state
+ 0x7fc4) & 0xffff;
261 env
->tr
.base
= ldl_phys(sm_state
+ 0x7f64);
262 env
->tr
.limit
= ldl_phys(sm_state
+ 0x7f60);
263 env
->tr
.flags
= (ldl_phys(sm_state
+ 0x7f5c) & 0xf0ff) << 8;
265 env
->ldt
.selector
= ldl_phys(sm_state
+ 0x7fc0) & 0xffff;
266 env
->ldt
.base
= ldl_phys(sm_state
+ 0x7f80);
267 env
->ldt
.limit
= ldl_phys(sm_state
+ 0x7f7c);
268 env
->ldt
.flags
= (ldl_phys(sm_state
+ 0x7f78) & 0xf0ff) << 8;
270 env
->gdt
.base
= ldl_phys(sm_state
+ 0x7f74);
271 env
->gdt
.limit
= ldl_phys(sm_state
+ 0x7f70);
273 env
->idt
.base
= ldl_phys(sm_state
+ 0x7f58);
274 env
->idt
.limit
= ldl_phys(sm_state
+ 0x7f54);
276 for (i
= 0; i
< 6; i
++) {
278 offset
= 0x7f84 + i
* 12;
280 offset
= 0x7f2c + (i
- 3) * 12;
282 cpu_x86_load_seg_cache(env
, i
,
283 ldl_phys(sm_state
+ 0x7fa8 + i
* 4) & 0xffff,
284 ldl_phys(sm_state
+ offset
+ 8),
285 ldl_phys(sm_state
+ offset
+ 4),
286 (ldl_phys(sm_state
+ offset
) & 0xf0ff) << 8);
288 cpu_x86_update_cr4(env
, ldl_phys(sm_state
+ 0x7f14));
290 val
= ldl_phys(sm_state
+ 0x7efc); /* revision ID */
292 env
->smbase
= ldl_phys(sm_state
+ 0x7ef8) & ~0x7fff;
295 CC_OP
= CC_OP_EFLAGS
;
296 env
->hflags
&= ~HF_SMM_MASK
;
299 qemu_log_mask(CPU_LOG_INT
, "SMM: after RSM\n");
300 log_cpu_state_mask(CPU_LOG_INT
, CPU(cpu
), CPU_DUMP_CCOP
);
303 #endif /* !CONFIG_USER_ONLY */