target_arch_cpu.c 1.8 KB

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