Emulator.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "Emulator.h"
  27. #include "SoftCPU.h"
  28. #include <AK/LogStream.h>
  29. #include <Kernel/API/Syscall.h>
  30. #include <stdio.h>
  31. #include <unistd.h>
  32. namespace UserspaceEmulator {
  33. static constexpr u32 stack_location = 0x10000000;
  34. static constexpr size_t stack_size = 64 * KB;
  35. class SimpleRegion final : public SoftMMU::Region {
  36. public:
  37. SimpleRegion(u32 base, u32 size)
  38. : Region(base, size)
  39. {
  40. m_data = (u8*)malloc(size);
  41. }
  42. ~SimpleRegion()
  43. {
  44. free(m_data);
  45. }
  46. virtual u8 read8(u32 offset) override
  47. {
  48. ASSERT(offset < size());
  49. return *reinterpret_cast<const u8*>(m_data + offset);
  50. }
  51. virtual u16 read16(u32 offset) override
  52. {
  53. ASSERT(offset + 1 < size());
  54. return *reinterpret_cast<const u16*>(m_data + offset);
  55. }
  56. virtual u32 read32(u32 offset) override
  57. {
  58. ASSERT(offset + 3 < size());
  59. return *reinterpret_cast<const u32*>(m_data + offset);
  60. }
  61. virtual void write8(u32 offset, u8 value) override
  62. {
  63. ASSERT(offset < size());
  64. *reinterpret_cast<u8*>(m_data + offset) = value;
  65. }
  66. virtual void write16(u32 offset, u16 value) override
  67. {
  68. ASSERT(offset + 1 < size());
  69. *reinterpret_cast<u16*>(m_data + offset) = value;
  70. }
  71. virtual void write32(u32 offset, u32 value) override
  72. {
  73. ASSERT(offset + 3 < size());
  74. *reinterpret_cast<u32*>(m_data + offset) = value;
  75. }
  76. private:
  77. u8* m_data { nullptr };
  78. };
  79. Emulator::Emulator()
  80. : m_cpu(*this)
  81. {
  82. setup_stack();
  83. }
  84. void Emulator::setup_stack()
  85. {
  86. auto stack_region = make<SimpleRegion>(stack_location, stack_size);
  87. m_mmu.add_region(move(stack_region));
  88. m_cpu.set_esp(stack_location + stack_size);
  89. m_cpu.push32(0);
  90. m_cpu.push32(0);
  91. m_cpu.push32(0);
  92. m_cpu.push32(0);
  93. }
  94. int Emulator::exec(X86::SimpleInstructionStream& stream, u32 base)
  95. {
  96. size_t offset = 0;
  97. while (!m_shutdown) {
  98. auto insn = X86::Instruction::from_stream(stream, true, true);
  99. out() << "\033[33;1m" << insn.to_string(base + offset) << "\033[0m";
  100. // FIXME: Remove this hack once it's no longer needed :^)
  101. if (insn.mnemonic() == "RET")
  102. break;
  103. (m_cpu.*insn.handler())(insn);
  104. m_cpu.dump();
  105. offset += insn.length();
  106. }
  107. return m_exit_status;
  108. }
  109. u32 Emulator::virt_syscall(u32 function, u32 arg1, u32 arg2, u32 arg3)
  110. {
  111. (void)arg2;
  112. (void)arg3;
  113. printf("Syscall: %s (%x)\n", Syscall::to_string((Syscall::Function)function), function);
  114. switch (function) {
  115. case SC_getuid:
  116. return virt$getuid();
  117. case SC_exit:
  118. virt$exit((int)arg1);
  119. return 0;
  120. default:
  121. warn() << "Unimplemented syscall!";
  122. TODO();
  123. }
  124. }
  125. uid_t Emulator::virt$getuid()
  126. {
  127. return getuid();
  128. }
  129. void Emulator::virt$exit(int status)
  130. {
  131. out() << "exit(" << status << "), shutting down!";
  132. m_exit_status = status;
  133. m_shutdown = true;
  134. }
  135. }