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.1 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/>.
22 typedef struct x86_register
{
25 uint64_t rrx
; /* full 64 bit */
28 uint32_t erx
; /* low 32 bit part */
29 uint32_t hi32_unused1
;
32 uint16_t rx
; /* low 16 bit part */
33 uint16_t hi16_unused1
;
34 uint32_t hi32_unused2
;
37 uint8_t lx
; /* low 8 bit part */
38 uint8_t hx
; /* high 8 bit */
39 uint16_t hi16_unused2
;
40 uint32_t hi32_unused3
;
43 } __attribute__ ((__packed__
)) x86_register
;
45 /* 16 bit Task State Segment */
46 typedef struct x86_tss_segment16
{
69 } __attribute__((packed
)) x86_tss_segment16
;
71 /* 32 bit Task State Segment */
72 typedef struct x86_tss_segment32
{
100 } __attribute__ ((__packed__
)) x86_tss_segment32
;
102 /* 64 bit Task State Segment */
103 typedef struct x86_tss_segment64
{
119 } __attribute__ ((__packed__
)) x86_tss_segment64
;
121 /* segment descriptors */
122 typedef struct x86_segment_descriptor
{
136 } __attribute__ ((__packed__
)) x86_segment_descriptor
;
138 static inline uint32_t x86_segment_base(x86_segment_descriptor
*desc
)
140 return (uint32_t)((desc
->base2
<< 24) | (desc
->base1
<< 16) | desc
->base0
);
143 static inline void x86_set_segment_base(x86_segment_descriptor
*desc
,
146 desc
->base2
= base
>> 24;
147 desc
->base1
= (base
>> 16) & 0xff;
148 desc
->base0
= base
& 0xffff;
151 static inline uint32_t x86_segment_limit(x86_segment_descriptor
*desc
)
153 uint32_t limit
= (uint32_t)((desc
->limit1
<< 16) | desc
->limit0
);
155 return (limit
<< 12) | 0xfff;
160 static inline void x86_set_segment_limit(x86_segment_descriptor
*desc
,
163 desc
->limit0
= limit
& 0xffff;
164 desc
->limit1
= limit
>> 16;
167 typedef struct x86_call_gate
{
169 uint64_t selector
:16;
170 uint64_t param_count
:4;
176 } __attribute__ ((__packed__
)) x86_call_gate
;
178 static inline uint32_t x86_call_gate_offset(x86_call_gate
*gate
)
180 return (uint32_t)((gate
->offset1
<< 16) | gate
->offset0
);
186 typedef struct x68_segment_selector
{
195 } __attribute__ ((__packed__
)) x68_segment_selector
;
197 /* useful register access macros */
198 #define x86_reg(cpu, reg) ((x86_register *) &cpu->regs[reg])
200 #define RRX(cpu, reg) (x86_reg(cpu, reg)->rrx)
201 #define RAX(cpu) RRX(cpu, R_EAX)
202 #define RCX(cpu) RRX(cpu, R_ECX)
203 #define RDX(cpu) RRX(cpu, R_EDX)
204 #define RBX(cpu) RRX(cpu, R_EBX)
205 #define RSP(cpu) RRX(cpu, R_ESP)
206 #define RBP(cpu) RRX(cpu, R_EBP)
207 #define RSI(cpu) RRX(cpu, R_ESI)
208 #define RDI(cpu) RRX(cpu, R_EDI)
209 #define R8(cpu) RRX(cpu, R_R8)
210 #define R9(cpu) RRX(cpu, R_R9)
211 #define R10(cpu) RRX(cpu, R_R10)
212 #define R11(cpu) RRX(cpu, R_R11)
213 #define R12(cpu) RRX(cpu, R_R12)
214 #define R13(cpu) RRX(cpu, R_R13)
215 #define R14(cpu) RRX(cpu, R_R14)
216 #define R15(cpu) RRX(cpu, R_R15)
218 #define ERX(cpu, reg) (x86_reg(cpu, reg)->erx)
219 #define EAX(cpu) ERX(cpu, R_EAX)
220 #define ECX(cpu) ERX(cpu, R_ECX)
221 #define EDX(cpu) ERX(cpu, R_EDX)
222 #define EBX(cpu) ERX(cpu, R_EBX)
223 #define ESP(cpu) ERX(cpu, R_ESP)
224 #define EBP(cpu) ERX(cpu, R_EBP)
225 #define ESI(cpu) ERX(cpu, R_ESI)
226 #define EDI(cpu) ERX(cpu, R_EDI)
228 #define RX(cpu, reg) (x86_reg(cpu, reg)->rx)
229 #define AX(cpu) RX(cpu, R_EAX)
230 #define CX(cpu) RX(cpu, R_ECX)
231 #define DX(cpu) RX(cpu, R_EDX)
232 #define BP(cpu) RX(cpu, R_EBP)
233 #define SP(cpu) RX(cpu, R_ESP)
234 #define BX(cpu) RX(cpu, R_EBX)
235 #define SI(cpu) RX(cpu, R_ESI)
236 #define DI(cpu) RX(cpu, R_EDI)
238 #define RL(cpu, reg) (x86_reg(cpu, reg)->lx)
239 #define AL(cpu) RL(cpu, R_EAX)
240 #define CL(cpu) RL(cpu, R_ECX)
241 #define DL(cpu) RL(cpu, R_EDX)
242 #define BL(cpu) RL(cpu, R_EBX)
244 #define RH(cpu, reg) (x86_reg(cpu, reg)->hx)
245 #define AH(cpu) RH(cpu, R_EAX)
246 #define CH(cpu) RH(cpu, R_ECX)
247 #define DH(cpu) RH(cpu, R_EDX)
248 #define BH(cpu) RH(cpu, R_EBX)
250 /* deal with GDT/LDT descriptors in memory */
251 bool x86_read_segment_descriptor(struct CPUState
*cpu
,
252 struct x86_segment_descriptor
*desc
,
253 x68_segment_selector sel
);
254 bool x86_write_segment_descriptor(struct CPUState
*cpu
,
255 struct x86_segment_descriptor
*desc
,
256 x68_segment_selector sel
);
258 bool x86_read_call_gate(struct CPUState
*cpu
, struct x86_call_gate
*idt_desc
,
262 bool x86_is_protected(struct CPUState
*cpu
);
263 bool x86_is_real(struct CPUState
*cpu
);
264 bool x86_is_v8086(struct CPUState
*cpu
);
265 bool x86_is_long_mode(struct CPUState
*cpu
);
266 bool x86_is_long64_mode(struct CPUState
*cpu
);
267 bool x86_is_paging_mode(struct CPUState
*cpu
);
268 bool x86_is_pae_enabled(struct CPUState
*cpu
);
271 target_ulong
linear_addr(struct CPUState
*cpu
, target_ulong addr
, enum X86Seg seg
);
272 target_ulong
linear_addr_size(struct CPUState
*cpu
, target_ulong addr
, int size
,
274 target_ulong
linear_rip(struct CPUState
*cpu
, target_ulong rip
);
276 static inline uint64_t rdtscp(void)
279 __asm__
__volatile__("rdtscp; " /* serializing read of tsc */
280 "shl $32,%%rdx; " /* shift higher 32 bits stored in rdx up */
281 "or %%rdx,%%rax" /* and or onto rax */
282 : "=a"(tsc
) /* output to tsc variable */
284 : "%rcx", "%rdx"); /* rcx and rdx are clobbered */