xref: /qemu/bsd-user/x86_64/target_arch_cpu.c (revision e2a74729181c51a0c9dff03bb7a99fc267bec996)
1 /*
2  *  x86_64 cpu related code
3  *
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
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
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include <sys/types.h>
20 
21 #include "qemu/osdep.h"
22 #include "cpu.h"
23 #include "qemu.h"
24 #include "qemu/timer.h"
25 
26 #include "target_arch.h"
27 
28 static uint64_t *idt_table;
29 
30 uint64_t cpu_get_tsc(CPUX86State *env)
31 {
32     return cpu_get_host_ticks();
33 }
34 
35 int cpu_get_pic_interrupt(CPUX86State *env)
36 {
37     return -1;
38 }
39 
40 void bsd_x86_64_write_dt(void *ptr, unsigned long addr,
41         unsigned long limit, int flags)
42 {
43     unsigned int e1, e2;
44     uint32_t *p;
45     e1 = (addr << 16) | (limit & 0xffff);
46     e2 = ((addr >> 16) & 0xff) | (addr & 0xff000000) | (limit & 0x000f0000);
47     e2 |= flags;
48     p = ptr;
49     p[0] = tswap32(e1);
50     p[1] = tswap32(e2);
51 }
52 
53 static void set_gate64(void *ptr, unsigned int type, unsigned int dpl,
54         uint64_t addr, unsigned int sel)
55 {
56     uint32_t *p, e1, e2;
57     e1 = (addr & 0xffff) | (sel << 16);
58     e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8);
59     p = ptr;
60     p[0] = tswap32(e1);
61     p[1] = tswap32(e2);
62     p[2] = tswap32(addr >> 32);
63     p[3] = 0;
64 }
65 
66 /* only dpl matters as we do only user space emulation */
67 void bsd_x86_64_set_idt(int n, unsigned int dpl)
68 {
69     set_gate64(idt_table + n * 2, 0, dpl, 0, 0);
70 }
71 
72 void bsd_x86_64_set_idt_base(uint64_t base)
73 {
74     idt_table = g2h_untagged(base);
75 }
76