2 * Copyright (C) 2016 Veertu Inc,
3 * Copyright (C) 2017 Veertu Inc,
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include <sys/types.h>
22 #include <sys/ioctl.h>
25 #include "qemu-common.h"
29 typedef enum x86_exception
{
30 EXCEPTION_DE
, /* divide error */
31 EXCEPTION_DB
, /* debug fault */
32 EXCEPTION_NMI
, /* non-maskable interrupt */
33 EXCEPTION_BP
, /* breakpoint trap */
34 EXCEPTION_OF
, /* overflow trap */
35 EXCEPTION_BR
, /* boundary range exceeded fault */
36 EXCEPTION_UD
, /* undefined opcode */
37 EXCEPTION_NM
, /* device not available */
38 EXCEPTION_DF
, /* double fault */
39 EXCEPTION_RSVD
, /* not defined */
40 EXCEPTION_TS
, /* invalid TSS fault */
41 EXCEPTION_NP
, /* not present fault */
42 EXCEPTION_GP
, /* general protection fault */
43 EXCEPTION_PF
, /* page fault */
44 EXCEPTION_RSVD2
, /* not defined */
47 /* general purpose regs */
48 typedef enum x86_reg_name
{
68 typedef enum x86_reg_segment
{
79 typedef struct x86_register
{
82 uint64_t rrx
; /* full 64 bit */
85 uint32_t erx
; /* low 32 bit part */
86 uint32_t hi32_unused1
;
89 uint16_t rx
; /* low 16 bit part */
90 uint16_t hi16_unused1
;
91 uint32_t hi32_unused2
;
94 uint8_t lx
; /* low 8 bit part */
95 uint8_t hx
; /* high 8 bit */
96 uint16_t hi16_unused2
;
97 uint32_t hi32_unused3
;
100 } __attribute__ ((__packed__
)) x86_register
;
102 typedef enum x86_rflags
{
103 RFLAGS_CF
= (1L << 0),
104 RFLAGS_PF
= (1L << 2),
105 RFLAGS_AF
= (1L << 4),
106 RFLAGS_ZF
= (1L << 6),
107 RFLAGS_SF
= (1L << 7),
108 RFLAGS_TF
= (1L << 8),
109 RFLAGS_IF
= (1L << 9),
110 RFLAGS_DF
= (1L << 10),
111 RFLAGS_OF
= (1L << 11),
112 RFLAGS_IOPL
= (3L << 12),
113 RFLAGS_NT
= (1L << 14),
114 RFLAGS_RF
= (1L << 16),
115 RFLAGS_VM
= (1L << 17),
116 RFLAGS_AC
= (1L << 18),
117 RFLAGS_VIF
= (1L << 19),
118 RFLAGS_VIP
= (1L << 20),
119 RFLAGS_ID
= (1L << 21),
122 /* rflags register */
123 typedef struct x86_reg_flags
{
130 uint32_t hi32_unused1
;
155 uint32_t hi32_unused2
;
158 } __attribute__ ((__packed__
)) x86_reg_flags
;
160 typedef enum x86_reg_efer
{
161 EFER_SCE
= (1L << 0),
162 EFER_LME
= (1L << 8),
163 EFER_LMA
= (1L << 10),
164 EFER_NXE
= (1L << 11),
165 EFER_SVME
= (1L << 12),
166 EFER_FXSR
= (1L << 14),
169 typedef struct x86_efer
{
171 } __attribute__ ((__packed__
)) x86_efer
;
173 typedef enum x86_reg_cr0
{
187 typedef enum x86_reg_cr4
{
197 CR4_OSFXSR
= (1L << 9),
198 CR4_OSXMMEXCPT
= (1L << 10),
199 CR4_VMXE
= (1L << 13),
200 CR4_SMXE
= (1L << 14),
201 CR4_FSGSBASE
= (1L << 16),
202 CR4_PCIDE
= (1L << 17),
203 CR4_OSXSAVE
= (1L << 18),
204 CR4_SMEP
= (1L << 20),
207 /* 16 bit Task State Segment */
208 typedef struct x86_tss_segment16
{
231 } __attribute__((packed
)) x86_tss_segment16
;
233 /* 32 bit Task State Segment */
234 typedef struct x86_tss_segment32
{
262 } __attribute__ ((__packed__
)) x86_tss_segment32
;
264 /* 64 bit Task State Segment */
265 typedef struct x86_tss_segment64
{
281 } __attribute__ ((__packed__
)) x86_tss_segment64
;
283 /* segment descriptors */
284 typedef struct x86_segment_descriptor
{
298 } __attribute__ ((__packed__
)) x86_segment_descriptor
;
300 static inline uint32_t x86_segment_base(x86_segment_descriptor
*desc
)
302 return (uint32_t)((desc
->base2
<< 24) | (desc
->base1
<< 16) | desc
->base0
);
305 static inline void x86_set_segment_base(x86_segment_descriptor
*desc
,
308 desc
->base2
= base
>> 24;
309 desc
->base1
= (base
>> 16) & 0xff;
310 desc
->base0
= base
& 0xffff;
313 static inline uint32_t x86_segment_limit(x86_segment_descriptor
*desc
)
315 uint32_t limit
= (uint32_t)((desc
->limit1
<< 16) | desc
->limit0
);
317 return (limit
<< 12) | 0xfff;
322 static inline void x86_set_segment_limit(x86_segment_descriptor
*desc
,
325 desc
->limit0
= limit
& 0xffff;
326 desc
->limit1
= limit
>> 16;
329 typedef struct x86_call_gate
{
331 uint64_t selector
:16;
332 uint64_t param_count
:4;
338 } __attribute__ ((__packed__
)) x86_call_gate
;
340 static inline uint32_t x86_call_gate_offset(x86_call_gate
*gate
)
342 return (uint32_t)((gate
->offset1
<< 16) | gate
->offset0
);
348 typedef struct x68_segment_selector
{
357 } __attribute__ ((__packed__
)) x68_segment_selector
;
359 typedef struct lazy_flags
{
364 /* Definition of hvf_x86_state is here */
365 struct HVFX86EmulatorState
{
369 struct x86_register regs
[16];
370 struct x86_reg_flags rflags
;
371 struct lazy_flags lflags
;
372 struct x86_efer efer
;
373 uint8_t mmio_buf
[4096];
376 /* useful register access macros */
377 #define RIP(cpu) (cpu->hvf_emul->rip)
378 #define EIP(cpu) ((uint32_t)cpu->hvf_emul->rip)
379 #define RFLAGS(cpu) (cpu->hvf_emul->rflags.rflags)
380 #define EFLAGS(cpu) (cpu->hvf_emul->rflags.eflags)
382 #define RRX(cpu, reg) (cpu->hvf_emul->regs[reg].rrx)
383 #define RAX(cpu) RRX(cpu, REG_RAX)
384 #define RCX(cpu) RRX(cpu, REG_RCX)
385 #define RDX(cpu) RRX(cpu, REG_RDX)
386 #define RBX(cpu) RRX(cpu, REG_RBX)
387 #define RSP(cpu) RRX(cpu, REG_RSP)
388 #define RBP(cpu) RRX(cpu, REG_RBP)
389 #define RSI(cpu) RRX(cpu, REG_RSI)
390 #define RDI(cpu) RRX(cpu, REG_RDI)
391 #define R8(cpu) RRX(cpu, REG_R8)
392 #define R9(cpu) RRX(cpu, REG_R9)
393 #define R10(cpu) RRX(cpu, REG_R10)
394 #define R11(cpu) RRX(cpu, REG_R11)
395 #define R12(cpu) RRX(cpu, REG_R12)
396 #define R13(cpu) RRX(cpu, REG_R13)
397 #define R14(cpu) RRX(cpu, REG_R14)
398 #define R15(cpu) RRX(cpu, REG_R15)
400 #define ERX(cpu, reg) (cpu->hvf_emul->regs[reg].erx)
401 #define EAX(cpu) ERX(cpu, REG_RAX)
402 #define ECX(cpu) ERX(cpu, REG_RCX)
403 #define EDX(cpu) ERX(cpu, REG_RDX)
404 #define EBX(cpu) ERX(cpu, REG_RBX)
405 #define ESP(cpu) ERX(cpu, REG_RSP)
406 #define EBP(cpu) ERX(cpu, REG_RBP)
407 #define ESI(cpu) ERX(cpu, REG_RSI)
408 #define EDI(cpu) ERX(cpu, REG_RDI)
410 #define RX(cpu, reg) (cpu->hvf_emul->regs[reg].rx)
411 #define AX(cpu) RX(cpu, REG_RAX)
412 #define CX(cpu) RX(cpu, REG_RCX)
413 #define DX(cpu) RX(cpu, REG_RDX)
414 #define BP(cpu) RX(cpu, REG_RBP)
415 #define SP(cpu) RX(cpu, REG_RSP)
416 #define BX(cpu) RX(cpu, REG_RBX)
417 #define SI(cpu) RX(cpu, REG_RSI)
418 #define DI(cpu) RX(cpu, REG_RDI)
420 #define RL(cpu, reg) (cpu->hvf_emul->regs[reg].lx)
421 #define AL(cpu) RL(cpu, REG_RAX)
422 #define CL(cpu) RL(cpu, REG_RCX)
423 #define DL(cpu) RL(cpu, REG_RDX)
424 #define BL(cpu) RL(cpu, REG_RBX)
426 #define RH(cpu, reg) (cpu->hvf_emul->regs[reg].hx)
427 #define AH(cpu) RH(cpu, REG_RAX)
428 #define CH(cpu) RH(cpu, REG_RCX)
429 #define DH(cpu) RH(cpu, REG_RDX)
430 #define BH(cpu) RH(cpu, REG_RBX)
432 /* deal with GDT/LDT descriptors in memory */
433 bool x86_read_segment_descriptor(struct CPUState
*cpu
,
434 struct x86_segment_descriptor
*desc
,
435 x68_segment_selector sel
);
436 bool x86_write_segment_descriptor(struct CPUState
*cpu
,
437 struct x86_segment_descriptor
*desc
,
438 x68_segment_selector sel
);
440 bool x86_read_call_gate(struct CPUState
*cpu
, struct x86_call_gate
*idt_desc
,
444 bool x86_is_protected(struct CPUState
*cpu
);
445 bool x86_is_real(struct CPUState
*cpu
);
446 bool x86_is_v8086(struct CPUState
*cpu
);
447 bool x86_is_long_mode(struct CPUState
*cpu
);
448 bool x86_is_long64_mode(struct CPUState
*cpu
);
449 bool x86_is_paging_mode(struct CPUState
*cpu
);
450 bool x86_is_pae_enabled(struct CPUState
*cpu
);
452 addr_t
linear_addr(struct CPUState
*cpu
, addr_t addr
, x86_reg_segment seg
);
453 addr_t
linear_addr_size(struct CPUState
*cpu
, addr_t addr
, int size
,
454 x86_reg_segment seg
);
455 addr_t
linear_rip(struct CPUState
*cpu
, addr_t rip
);
457 static inline uint64_t rdtscp(void)
460 __asm__
__volatile__("rdtscp; " /* serializing read of tsc */
461 "shl $32,%%rdx; " /* shift higher 32 bits stored in rdx up */
462 "or %%rdx,%%rax" /* and or onto rax */
463 : "=a"(tsc
) /* output to tsc variable */
465 : "%rcx", "%rdx"); /* rcx and rdx are clobbered */