File: mwait.cc

package info (click to toggle)
bochs 3.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 25,244 kB
  • sloc: cpp: 270,331; ansic: 25,334; sh: 8,371; makefile: 5,512; yacc: 1,485; asm: 395; perl: 359; lex: 318; csh: 3
file content (377 lines) | stat: -rw-r--r-- 12,029 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/////////////////////////////////////////////////////////////////////////
// $Id$
/////////////////////////////////////////////////////////////////////////
//
//  Copyright (C) 2019-2023  The Bochs Project
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA B 02110-1301 USA
//
/////////////////////////////////////////////////////////////////////////

#define NEED_CPU_REG_SHORTCUTS 1
#include "bochs.h"
#include "gui/siminterface.h"
#include "param_names.h"
#include "cpu.h"
#define LOG_THIS BX_CPU_THIS_PTR

#if BX_SUPPORT_SVM
#include "svm.h"
#endif

#if BX_SUPPORT_APIC
#include "apic.h"
#endif

#include "pc_system.h"

#include "decoder/ia_opcodes.h"

#if BX_SUPPORT_MONITOR_MWAIT
bool BX_CPU_C::is_monitor(bx_phy_address begin_addr, unsigned len)
{
  if (! BX_CPU_THIS_PTR monitor.armed()) return false;

  bx_phy_address monitor_begin = BX_CPU_THIS_PTR monitor.monitor_addr;
  bx_phy_address monitor_end = monitor_begin + CACHE_LINE_SIZE - 1;

  bx_phy_address end_addr = begin_addr + len;
  if (begin_addr >= monitor_end || end_addr <= monitor_begin)
    return false;
  else
    return true;
}

void BX_CPU_C::check_monitor(bx_phy_address begin_addr, unsigned len)
{
  if (is_monitor(begin_addr, len)) wakeup_monitor();
}

void BX_CPU_C::wakeup_monitor(void)
{
  // wakeup from MWAIT state
  if(BX_CPU_THIS_PTR activity_state >= BX_ACTIVITY_STATE_MWAIT)
     BX_CPU_THIS_PTR activity_state = BX_ACTIVITY_STATE_ACTIVE;
  // clear monitor
  BX_CPU_THIS_PTR monitor.reset_monitor();
  // deactivate mwaitx timer if was active to avoid its redundant firing
  BX_CPU_THIS_PTR lapic->deactivate_mwaitx_timer();
}
#endif

void BX_CPP_AttrRegparmN(1) BX_CPU_C::MONITOR(bxInstruction_c *i)
{
#if BX_SUPPORT_MONITOR_MWAIT
  BX_DEBUG(("%s instruction executed RAX = 0x" FMT_ADDRX, i->getIaOpcodeNameShort(), RAX));

  if (i->getIaOpcode() == BX_IA_MONITOR) {
    // CPL is always 0 in real mode
    if (CPL != 0) {
      BX_DEBUG(("%s: instruction not recognized when CPL != 0", i->getIaOpcodeNameShort()));
      exception(BX_UD_EXCEPTION, 0);
    }

#if BX_SUPPORT_VMX
    if (BX_CPU_THIS_PTR in_vmx_guest) {
      if (BX_CPU_THIS_PTR vmcs.vmexec_ctrls1.MONITOR_VMEXIT()) {
        VMexit(VMX_VMEXIT_MONITOR, 0);
      }
    }
#endif
  }

  if (RCX != 0) {
    BX_ERROR(("%s: no optional extensions supported", i->getIaOpcodeNameShort()));
    exception(BX_GP_EXCEPTION, 0);
  }

  bx_address eaddr = RAX & i->asize_mask();

  // MONITOR/MONITORX performs the same segmentation and paging checks as a 1-byte read
  tickle_read_virtual(i->seg(), eaddr);

  bx_phy_address paddr = BX_CPU_THIS_PTR address_xlation.paddress1;
#if BX_SUPPORT_MEMTYPE
  if (BX_CPU_THIS_PTR address_xlation.memtype1 != BX_MEMTYPE_WB) {
    BX_DEBUG(("%s for non-WB memory type phys_addr=0x" FMT_PHY_ADDRX, i->getIaOpcodeNameShort(), BX_CPU_THIS_PTR monitor.monitor_addr));
    BX_NEXT_INSTR(i);
  }
#endif

#if BX_SUPPORT_SVM
  if (BX_CPU_THIS_PTR in_svm_guest) {
    if (SVM_INTERCEPT(SVM_INTERCEPT1_MONITOR)) Svm_Vmexit(SVM_VMEXIT_MONITOR);
  }
#endif

  // Set the monitor immediately. If monitor is still armed when we MWAIT,
  // the processor will stall.
  bx_pc_system.invlpg(paddr);

  BX_CPU_THIS_PTR monitor.arm(paddr, (i->getIaOpcode() == BX_IA_MONITOR) ? BX_MONITOR_ARMED_BY_MONITOR : BX_MONITOR_ARMED_BY_MONITORX);

  BX_DEBUG(("%s: for phys_addr=0x" FMT_PHY_ADDRX, i->getIaOpcodeNameShort(), BX_CPU_THIS_PTR monitor.monitor_addr));
#endif

  BX_NEXT_INSTR(i);
}

enum {
  BX_MWAIT_WAKEUP_ON_EVENT_WHEN_INTERRUPT_DISABLE = 0x1,
  BX_MWAIT_TIMED_MWAITX = 0x2,
  BX_MWAIT_MONITORLESS_MWAIT = 0x4
};

void BX_CPP_AttrRegparmN(1) BX_CPU_C::MWAIT(bxInstruction_c *i)
{
#if BX_SUPPORT_MONITOR_MWAIT
  BX_DEBUG(("%s instruction executed ECX = 0x%08x", i->getIaOpcodeNameShort(), ECX));

  if (i->getIaOpcode() == BX_IA_MWAIT) {
    // CPL is always 0 in real mode
    if (CPL != 0) {
      BX_DEBUG(("%s: instruction not recognized when CPL != 0", i->getIaOpcodeNameShort()));
      exception(BX_UD_EXCEPTION, 0);
    }

#if BX_SUPPORT_VMX
    if (BX_CPU_THIS_PTR in_vmx_guest) {
      if (BX_CPU_THIS_PTR vmcs.vmexec_ctrls1.MWAIT_VMEXIT()) {
        VMexit(VMX_VMEXIT_MWAIT, BX_CPU_THIS_PTR monitor.armed_by_monitor());
      }
    }
#endif
  }

  // extension supported:
  //   ECX[0] - interrupt MWAIT even if EFLAGS.IF = 0
  //   ECX[1] - timed MWAITX (MWAITX only)
  //   ECX[2] - monitorless MWAIT
  // all other bits are reserved
  Bit64u supported_bits = BX_MWAIT_WAKEUP_ON_EVENT_WHEN_INTERRUPT_DISABLE;
  if (i->getIaOpcode() == BX_IA_MWAITX)
    supported_bits |= BX_MWAIT_TIMED_MWAITX;
  if (BX_CPUID_SUPPORT_ISA_EXTENSION(BX_ISA_MONITORLESS_MWAIT))
    supported_bits |= BX_MWAIT_MONITORLESS_MWAIT;

  if (RCX & ~supported_bits) {
    BX_ERROR(("%s: incorrect optional extensions in RCX", i->getIaOpcodeNameShort()));
    exception(BX_GP_EXCEPTION, 0);
  }

#if BX_SUPPORT_SVM
  if (BX_CPU_THIS_PTR in_svm_guest) {
    if (SVM_INTERCEPT(SVM_INTERCEPT1_MWAIT_ARMED))
      if (BX_CPU_THIS_PTR monitor.armed()) Svm_Vmexit(SVM_VMEXIT_MWAIT_CONDITIONAL);

    if (SVM_INTERCEPT(SVM_INTERCEPT1_MWAIT)) Svm_Vmexit(SVM_VMEXIT_MWAIT);
  }
#endif

  if (! (ECX & BX_MWAIT_MONITORLESS_MWAIT)) {
    // If monitor has already triggered, we just return.
    bool monitor_armed = true;
    if (i->getIaOpcode() == BX_IA_MWAITX) {
      if (! BX_CPU_THIS_PTR monitor.armed_by_monitorx())
        monitor_armed = false;
    }
    else {
      if (! BX_CPU_THIS_PTR monitor.armed_by_monitor())
        monitor_armed = false;
    }

    if (! monitor_armed) {
      BX_DEBUG(("%s: the MONITOR was not armed or already triggered", i->getIaOpcodeNameShort()));
      BX_NEXT_TRACE(i);
    }
  }
  else {
    BX_CPU_THIS_PTR monitor.reset_monitor(); // Monitorless MWAIT
  }

  static bool mwait_is_nop = SIM->get_param_bool(BXPN_MWAIT_IS_NOP)->get();
  if (mwait_is_nop) {
    BX_NEXT_TRACE(i);
  }

  // stops instruction execution and places the processor in a optimized
  // state.  Events that cause exit from MWAIT state are:
  // A store from another processor to monitored range, any unmasked
  // interrupt, including INTR, NMI, SMI, INIT or reset will resume
  // the execution. Any far control transfer between MONITOR and MWAIT
  // resets the monitoring logic.

  Bit32u new_state = BX_ACTIVITY_STATE_MWAIT;
  if (ECX & 0x1) {
#if BX_SUPPORT_VMX
    // When "interrupt window exiting" VMX control is set MWAIT instruction
    // won't cause the processor to enter sleep state with EFLAGS.IF = 0
    if (BX_CPU_THIS_PTR in_vmx_guest) {
      if (BX_CPU_THIS_PTR vmcs.vmexec_ctrls1.INTERRUPT_WINDOW_VMEXIT() && ! BX_CPU_THIS_PTR get_IF()) {
        BX_NEXT_TRACE(i);
      }
    }
#endif
    new_state = BX_ACTIVITY_STATE_MWAIT_IF;
  }

  BX_INSTR_MWAIT(BX_CPU_ID, BX_CPU_THIS_PTR monitor.monitor_addr, CACHE_LINE_SIZE, ECX);

  if (i->getIaOpcode() == BX_IA_MWAITX) {
    if ((ECX & 0x2) != 0 && EBX != 0) {
      BX_CPU_THIS_PTR lapic->set_mwaitx_timer(EBX);
    }
  }

  enter_sleep_state(new_state);
#endif

  BX_NEXT_TRACE(i);
}

void BX_CPP_AttrRegparmN(1) BX_CPU_C::UMONITOR_Eq(bxInstruction_c *i)
{
#if BX_SUPPORT_MONITOR_MWAIT

#if BX_SUPPORT_VMX
  if (BX_CPU_THIS_PTR in_vmx_guest) {
    if (! BX_CPU_THIS_PTR vmcs.vmexec_ctrls2.UMWAIT_TPAUSE_VMEXIT()) {
      BX_DEBUG(("%s: instruction is not enabled in VMX guest", i->getIaOpcodeNameShort()));
      exception(BX_UD_EXCEPTION, 0);
    }
  }
#endif

#if BX_SUPPORT_X86_64
  bx_address eaddr = BX_READ_64BIT_REG(i->dst()) & i->asize_mask();
#else
  bx_address eaddr = BX_READ_32BIT_REG(i->dst()) & i->asize_mask();
#endif

  // UMONITOR performs the same segmentation and paging checks as a 1-byte read
  tickle_read_virtual(i->seg(), eaddr);

  bx_phy_address paddr = BX_CPU_THIS_PTR address_xlation.paddress1;
#if BX_SUPPORT_MEMTYPE
  if (BX_CPU_THIS_PTR address_xlation.memtype1 != BX_MEMTYPE_WB) {
    BX_DEBUG(("UMONITOR for non-WB memory type phys_addr=0x" FMT_PHY_ADDRX, BX_CPU_THIS_PTR monitor.monitor_addr));
    BX_NEXT_INSTR(i);
  }
#endif

  // Set the monitor immediately. If monitor is still armed when we MWAIT,
  // the processor will stall.
  bx_pc_system.invlpg(paddr);

  BX_CPU_THIS_PTR monitor.arm(paddr, BX_MONITOR_ARMED_BY_UMONITOR);

  BX_DEBUG(("UMONITOR for phys_addr=0x" FMT_PHY_ADDRX, BX_CPU_THIS_PTR monitor.monitor_addr));
#endif

  BX_NEXT_INSTR(i);
}

void BX_CPP_AttrRegparmN(1) BX_CPU_C::UMWAIT_Ed(bxInstruction_c *i)
{
#if BX_SUPPORT_MONITOR_MWAIT
  BX_DEBUG(("%s instruction executed EAX = 0x%08x EDX = 0x%08x", i->getIaOpcodeNameShort(), EAX, EDX));

#if BX_SUPPORT_VMX
  if (BX_CPU_THIS_PTR in_vmx_guest) {
    if (! BX_CPU_THIS_PTR vmcs.vmexec_ctrls2.UMWAIT_TPAUSE_VMEXIT()) {
      BX_DEBUG(("%s: instruction is not enabled in VMX guest", i->getIaOpcodeNameShort()));
      exception(BX_UD_EXCEPTION, 0);
    }
  }
#endif

  if (BX_CPU_THIS_PTR cr4.get_TSD() && CPL != 0) {
    BX_ERROR(("%s: not allowed to use instruction !", i->getIaOpcodeNameShort()));
    exception(BX_GP_EXCEPTION, 0);
  }

#if BX_SUPPORT_VMX
  if (BX_CPU_THIS_PTR in_vmx_guest && BX_CPU_THIS_PTR vmcs.vmexec_ctrls1.RDTSC_VMEXIT()) {
    VMexit((i->getIaOpcode() == BX_IA_TPAUSE_Ed) ? VMX_VMEXIT_TPAUSE : VMX_VMEXIT_UMWAIT, 0);
  }
#endif

  Bit32u req_sleep_state = BX_READ_32BIT_REG(i->dst());
  if (req_sleep_state & ~0x1) {
    BX_ERROR(("%s: incorrect sleep state 0x%08x - #GP(0)", i->getIaOpcodeNameShort(), req_sleep_state));
    exception(BX_GP_EXCEPTION, 0);
  }

  clearEFlagsOSZAPC();

  if (i->getIaOpcode() != BX_IA_TPAUSE_Ed) {
    // If monitor has already triggered, we just return.
    if (! BX_CPU_THIS_PTR monitor.armed_by_umonitor()) {
      BX_DEBUG(("%s: the UMONITOR was not armed or already triggered", i->getIaOpcodeNameShort()));
      BX_NEXT_TRACE(i);
    }
  }
  else {
    BX_CPU_THIS_PTR monitor.reset_umonitor();
  }

  static bool mwait_is_nop = SIM->get_param_bool(BXPN_MWAIT_IS_NOP)->get();
  if (mwait_is_nop) {
    BX_NEXT_TRACE(i);
  }

  Bit64u tsc = get_Virtual_TSC();
  Bit64u instr_deadline = GET64_FROM_HI32_LO32(EDX, EAX);
  if (instr_deadline <= tsc) {
    BX_DEBUG(("%s: requested deadline is in the past", i->getIaOpcodeNameShort()));
    BX_NEXT_TRACE(i);
  }
  Bit64u instr_delay = instr_deadline - tsc;

  Bit32u umwait_control_max_delay = (BX_CPU_THIS_PTR msr.ia32_umwait_ctrl & ~0x3);
//bool using_os_deadline = false; // FIXME
  if (umwait_control_max_delay && umwait_control_max_delay < instr_delay) {
    instr_delay = umwait_control_max_delay;
//  using_os_deadline = true;
  }

  BX_ASSERT(instr_delay > 0);
#if BX_SUPPORT_VMX
  instr_delay = compute_physical_TSC_delay(instr_delay);
#endif

  if (instr_delay == 0) {
    BX_DEBUG(("%s: requested delay is 0", i->getIaOpcodeNameShort()));
    BX_NEXT_TRACE(i);
  }

  BX_DEBUG(("%s entering sleep state with delay=" FMT_LL "d", i->getIaOpcodeNameShort(), instr_delay));

  BX_CPU_THIS_PTR lapic->set_mwaitx_timer(instr_delay);

  // An external interrupt causes the processor to exit the implementation-dependent optimized state 
  // regardless of whether maskable-interrupts are inhibited (EFLAGS.IF =0)
  Bit32u sleep_state = BX_ACTIVITY_STATE_MWAIT_IF;

  BX_INSTR_MWAIT(BX_CPU_ID, BX_CPU_THIS_PTR monitor.monitor_addr, CACHE_LINE_SIZE, sleep_state);

  enter_sleep_state(sleep_state);

//if (using_os_deadline && tsc >= deadline)
//  assert_CF();
#endif

  BX_NEXT_TRACE(i);
}